PR target/11052
[official-gcc.git] / gcc / combine.c
blobd476bf0ac82377e3ac7a0e292c8be2d1bee57a3b
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 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 "tm_p.h"
79 #include "flags.h"
80 #include "regs.h"
81 #include "hard-reg-set.h"
82 #include "basic-block.h"
83 #include "insn-config.h"
84 #include "function.h"
85 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
86 #include "expr.h"
87 #include "insn-attr.h"
88 #include "recog.h"
89 #include "real.h"
90 #include "toplev.h"
92 /* It is not safe to use ordinary gen_lowpart in combine.
93 Use gen_lowpart_for_combine instead. See comments there. */
94 #define gen_lowpart dont_use_gen_lowpart_you_dummy
96 /* Number of attempts to combine instructions in this function. */
98 static int combine_attempts;
100 /* Number of attempts that got as far as substitution in this function. */
102 static int combine_merges;
104 /* Number of instructions combined with added SETs in this function. */
106 static int combine_extras;
108 /* Number of instructions combined in this function. */
110 static int combine_successes;
112 /* Totals over entire compilation. */
114 static int total_attempts, total_merges, total_extras, total_successes;
117 /* Vector mapping INSN_UIDs to cuids.
118 The cuids are like uids but increase monotonically always.
119 Combine always uses cuids so that it can compare them.
120 But actually renumbering the uids, which we used to do,
121 proves to be a bad idea because it makes it hard to compare
122 the dumps produced by earlier passes with those from later passes. */
124 static int *uid_cuid;
125 static int max_uid_cuid;
127 /* Get the cuid of an insn. */
129 #define INSN_CUID(INSN) \
130 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
132 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
133 BITS_PER_WORD would invoke undefined behavior. Work around it. */
135 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
136 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
138 #define nonzero_bits(X, M) \
139 cached_nonzero_bits (X, M, NULL_RTX, VOIDmode, 0)
141 #define num_sign_bit_copies(X, M) \
142 cached_num_sign_bit_copies (X, M, NULL_RTX, VOIDmode, 0)
144 /* Maximum register number, which is the size of the tables below. */
146 static unsigned int combine_max_regno;
148 /* Record last point of death of (hard or pseudo) register n. */
150 static rtx *reg_last_death;
152 /* Record last point of modification of (hard or pseudo) register n. */
154 static rtx *reg_last_set;
156 /* Record the cuid of the last insn that invalidated memory
157 (anything that writes memory, and subroutine calls, but not pushes). */
159 static int mem_last_set;
161 /* Record the cuid of the last CALL_INSN
162 so we can tell whether a potential combination crosses any calls. */
164 static int last_call_cuid;
166 /* When `subst' is called, this is the insn that is being modified
167 (by combining in a previous insn). The PATTERN of this insn
168 is still the old pattern partially modified and it should not be
169 looked at, but this may be used to examine the successors of the insn
170 to judge whether a simplification is valid. */
172 static rtx subst_insn;
174 /* This is the lowest CUID that `subst' is currently dealing with.
175 get_last_value will not return a value if the register was set at or
176 after this CUID. If not for this mechanism, we could get confused if
177 I2 or I1 in try_combine were an insn that used the old value of a register
178 to obtain a new value. In that case, we might erroneously get the
179 new value of the register when we wanted the old one. */
181 static int subst_low_cuid;
183 /* This contains any hard registers that are used in newpat; reg_dead_at_p
184 must consider all these registers to be always live. */
186 static HARD_REG_SET newpat_used_regs;
188 /* This is an insn to which a LOG_LINKS entry has been added. If this
189 insn is the earlier than I2 or I3, combine should rescan starting at
190 that location. */
192 static rtx added_links_insn;
194 /* Basic block in which we are performing combines. */
195 static basic_block this_basic_block;
197 /* A bitmap indicating which blocks had registers go dead at entry.
198 After combine, we'll need to re-do global life analysis with
199 those blocks as starting points. */
200 static sbitmap refresh_blocks;
202 /* The next group of arrays allows the recording of the last value assigned
203 to (hard or pseudo) register n. We use this information to see if an
204 operation being processed is redundant given a prior operation performed
205 on the register. For example, an `and' with a constant is redundant if
206 all the zero bits are already known to be turned off.
208 We use an approach similar to that used by cse, but change it in the
209 following ways:
211 (1) We do not want to reinitialize at each label.
212 (2) It is useful, but not critical, to know the actual value assigned
213 to a register. Often just its form is helpful.
215 Therefore, we maintain the following arrays:
217 reg_last_set_value the last value assigned
218 reg_last_set_label records the value of label_tick when the
219 register was assigned
220 reg_last_set_table_tick records the value of label_tick when a
221 value using the register is assigned
222 reg_last_set_invalid set to nonzero when it is not valid
223 to use the value of this register in some
224 register's value
226 To understand the usage of these tables, it is important to understand
227 the distinction between the value in reg_last_set_value being valid
228 and the register being validly contained in some other expression in the
229 table.
231 Entry I in reg_last_set_value is valid if it is nonzero, and either
232 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
234 Register I may validly appear in any expression returned for the value
235 of another register if reg_n_sets[i] is 1. It may also appear in the
236 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
237 reg_last_set_invalid[j] is zero.
239 If an expression is found in the table containing a register which may
240 not validly appear in an expression, the register is replaced by
241 something that won't match, (clobber (const_int 0)).
243 reg_last_set_invalid[i] is set nonzero when register I is being assigned
244 to and reg_last_set_table_tick[i] == label_tick. */
246 /* Record last value assigned to (hard or pseudo) register n. */
248 static rtx *reg_last_set_value;
250 /* Record the value of label_tick when the value for register n is placed in
251 reg_last_set_value[n]. */
253 static int *reg_last_set_label;
255 /* Record the value of label_tick when an expression involving register n
256 is placed in reg_last_set_value. */
258 static int *reg_last_set_table_tick;
260 /* Set nonzero if references to register n in expressions should not be
261 used. */
263 static char *reg_last_set_invalid;
265 /* Incremented for each label. */
267 static int label_tick;
269 /* Some registers that are set more than once and used in more than one
270 basic block are nevertheless always set in similar ways. For example,
271 a QImode register may be loaded from memory in two places on a machine
272 where byte loads zero extend.
274 We record in the following array what we know about the nonzero
275 bits of a register, specifically which bits are known to be zero.
277 If an entry is zero, it means that we don't know anything special. */
279 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
281 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
282 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
284 static enum machine_mode nonzero_bits_mode;
286 /* Nonzero if we know that a register has some leading bits that are always
287 equal to the sign bit. */
289 static unsigned char *reg_sign_bit_copies;
291 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
292 It is zero while computing them and after combine has completed. This
293 former test prevents propagating values based on previously set values,
294 which can be incorrect if a variable is modified in a loop. */
296 static int nonzero_sign_valid;
298 /* These arrays are maintained in parallel with reg_last_set_value
299 and are used to store the mode in which the register was last set,
300 the bits that were known to be zero when it was last set, and the
301 number of sign bits copies it was known to have when it was last set. */
303 static enum machine_mode *reg_last_set_mode;
304 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
305 static char *reg_last_set_sign_bit_copies;
307 /* Record one modification to rtl structure
308 to be undone by storing old_contents into *where.
309 is_int is 1 if the contents are an int. */
311 struct undo
313 struct undo *next;
314 int is_int;
315 union {rtx r; int i;} old_contents;
316 union {rtx *r; int *i;} where;
319 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
320 num_undo says how many are currently recorded.
322 other_insn is nonzero if we have modified some other insn in the process
323 of working on subst_insn. It must be verified too. */
325 struct undobuf
327 struct undo *undos;
328 struct undo *frees;
329 rtx other_insn;
332 static struct undobuf undobuf;
334 /* Number of times the pseudo being substituted for
335 was found and replaced. */
337 static int n_occurrences;
339 static void do_SUBST PARAMS ((rtx *, rtx));
340 static void do_SUBST_INT PARAMS ((int *, int));
341 static void init_reg_last_arrays PARAMS ((void));
342 static void setup_incoming_promotions PARAMS ((void));
343 static void set_nonzero_bits_and_sign_copies PARAMS ((rtx, rtx, void *));
344 static int cant_combine_insn_p PARAMS ((rtx));
345 static int can_combine_p PARAMS ((rtx, rtx, rtx, rtx, rtx *, rtx *));
346 static int sets_function_arg_p PARAMS ((rtx));
347 static int combinable_i3pat PARAMS ((rtx, rtx *, rtx, rtx, int, rtx *));
348 static int contains_muldiv PARAMS ((rtx));
349 static rtx try_combine PARAMS ((rtx, rtx, rtx, int *));
350 static void undo_all PARAMS ((void));
351 static void undo_commit PARAMS ((void));
352 static rtx *find_split_point PARAMS ((rtx *, rtx));
353 static rtx subst PARAMS ((rtx, rtx, rtx, int, int));
354 static rtx combine_simplify_rtx PARAMS ((rtx, enum machine_mode, int, int));
355 static rtx simplify_if_then_else PARAMS ((rtx));
356 static rtx simplify_set PARAMS ((rtx));
357 static rtx simplify_logical PARAMS ((rtx, int));
358 static rtx expand_compound_operation PARAMS ((rtx));
359 static rtx expand_field_assignment PARAMS ((rtx));
360 static rtx make_extraction PARAMS ((enum machine_mode, rtx, HOST_WIDE_INT,
361 rtx, unsigned HOST_WIDE_INT, int,
362 int, int));
363 static rtx extract_left_shift PARAMS ((rtx, int));
364 static rtx make_compound_operation PARAMS ((rtx, enum rtx_code));
365 static int get_pos_from_mask PARAMS ((unsigned HOST_WIDE_INT,
366 unsigned HOST_WIDE_INT *));
367 static rtx force_to_mode PARAMS ((rtx, enum machine_mode,
368 unsigned HOST_WIDE_INT, rtx, int));
369 static rtx if_then_else_cond PARAMS ((rtx, rtx *, rtx *));
370 static rtx known_cond PARAMS ((rtx, enum rtx_code, rtx, rtx));
371 static int rtx_equal_for_field_assignment_p PARAMS ((rtx, rtx));
372 static rtx make_field_assignment PARAMS ((rtx));
373 static rtx apply_distributive_law PARAMS ((rtx));
374 static rtx simplify_and_const_int PARAMS ((rtx, enum machine_mode, rtx,
375 unsigned HOST_WIDE_INT));
376 static unsigned HOST_WIDE_INT cached_nonzero_bits
377 PARAMS ((rtx, enum machine_mode, rtx,
378 enum machine_mode,
379 unsigned HOST_WIDE_INT));
380 static unsigned HOST_WIDE_INT nonzero_bits1
381 PARAMS ((rtx, enum machine_mode, rtx,
382 enum machine_mode,
383 unsigned HOST_WIDE_INT));
384 static unsigned int cached_num_sign_bit_copies
385 PARAMS ((rtx, enum machine_mode, rtx,
386 enum machine_mode, unsigned int));
387 static unsigned int num_sign_bit_copies1
388 PARAMS ((rtx, enum machine_mode, rtx,
389 enum machine_mode, unsigned int));
390 static int merge_outer_ops PARAMS ((enum rtx_code *, HOST_WIDE_INT *,
391 enum rtx_code, HOST_WIDE_INT,
392 enum machine_mode, int *));
393 static rtx simplify_shift_const PARAMS ((rtx, enum rtx_code, enum machine_mode,
394 rtx, int));
395 static int recog_for_combine PARAMS ((rtx *, rtx, rtx *));
396 static rtx gen_lowpart_for_combine PARAMS ((enum machine_mode, rtx));
397 static rtx gen_binary PARAMS ((enum rtx_code, enum machine_mode,
398 rtx, rtx));
399 static enum rtx_code simplify_comparison PARAMS ((enum rtx_code, rtx *, rtx *));
400 static void update_table_tick PARAMS ((rtx));
401 static void record_value_for_reg PARAMS ((rtx, rtx, rtx));
402 static void check_promoted_subreg PARAMS ((rtx, rtx));
403 static void record_dead_and_set_regs_1 PARAMS ((rtx, rtx, void *));
404 static void record_dead_and_set_regs PARAMS ((rtx));
405 static int get_last_value_validate PARAMS ((rtx *, rtx, int, int));
406 static rtx get_last_value PARAMS ((rtx));
407 static int use_crosses_set_p PARAMS ((rtx, int));
408 static void reg_dead_at_p_1 PARAMS ((rtx, rtx, void *));
409 static int reg_dead_at_p PARAMS ((rtx, rtx));
410 static void move_deaths PARAMS ((rtx, rtx, int, rtx, rtx *));
411 static int reg_bitfield_target_p PARAMS ((rtx, rtx));
412 static void distribute_notes PARAMS ((rtx, rtx, rtx, rtx));
413 static void distribute_links PARAMS ((rtx));
414 static void mark_used_regs_combine PARAMS ((rtx));
415 static int insn_cuid PARAMS ((rtx));
416 static void record_promoted_value PARAMS ((rtx, rtx));
417 static rtx reversed_comparison PARAMS ((rtx, enum machine_mode, rtx, rtx));
418 static enum rtx_code combine_reversed_comparison_code PARAMS ((rtx));
420 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
421 insn. The substitution can be undone by undo_all. If INTO is already
422 set to NEWVAL, do not record this change. Because computing NEWVAL might
423 also call SUBST, we have to compute it before we put anything into
424 the undo table. */
426 static void
427 do_SUBST (into, newval)
428 rtx *into, newval;
430 struct undo *buf;
431 rtx oldval = *into;
433 if (oldval == newval)
434 return;
436 /* We'd like to catch as many invalid transformations here as
437 possible. Unfortunately, there are way too many mode changes
438 that are perfectly valid, so we'd waste too much effort for
439 little gain doing the checks here. Focus on catching invalid
440 transformations involving integer constants. */
441 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
442 && GET_CODE (newval) == CONST_INT)
444 /* Sanity check that we're replacing oldval with a CONST_INT
445 that is a valid sign-extension for the original mode. */
446 if (INTVAL (newval) != trunc_int_for_mode (INTVAL (newval),
447 GET_MODE (oldval)))
448 abort ();
450 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
451 CONST_INT is not valid, because after the replacement, the
452 original mode would be gone. Unfortunately, we can't tell
453 when do_SUBST is called to replace the operand thereof, so we
454 perform this test on oldval instead, checking whether an
455 invalid replacement took place before we got here. */
456 if ((GET_CODE (oldval) == SUBREG
457 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT)
458 || (GET_CODE (oldval) == ZERO_EXTEND
459 && GET_CODE (XEXP (oldval, 0)) == CONST_INT))
460 abort ();
463 if (undobuf.frees)
464 buf = undobuf.frees, undobuf.frees = buf->next;
465 else
466 buf = (struct undo *) xmalloc (sizeof (struct undo));
468 buf->is_int = 0;
469 buf->where.r = into;
470 buf->old_contents.r = oldval;
471 *into = newval;
473 buf->next = undobuf.undos, undobuf.undos = buf;
476 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
478 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
479 for the value of a HOST_WIDE_INT value (including CONST_INT) is
480 not safe. */
482 static void
483 do_SUBST_INT (into, newval)
484 int *into, newval;
486 struct undo *buf;
487 int oldval = *into;
489 if (oldval == newval)
490 return;
492 if (undobuf.frees)
493 buf = undobuf.frees, undobuf.frees = buf->next;
494 else
495 buf = (struct undo *) xmalloc (sizeof (struct undo));
497 buf->is_int = 1;
498 buf->where.i = into;
499 buf->old_contents.i = oldval;
500 *into = newval;
502 buf->next = undobuf.undos, undobuf.undos = buf;
505 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
507 /* Main entry point for combiner. F is the first insn of the function.
508 NREGS is the first unused pseudo-reg number.
510 Return nonzero if the combiner has turned an indirect jump
511 instruction into a direct jump. */
513 combine_instructions (f, nregs)
514 rtx f;
515 unsigned int nregs;
517 rtx insn, next;
518 #ifdef HAVE_cc0
519 rtx prev;
520 #endif
521 int i;
522 rtx links, nextlinks;
524 int new_direct_jump_p = 0;
526 combine_attempts = 0;
527 combine_merges = 0;
528 combine_extras = 0;
529 combine_successes = 0;
531 combine_max_regno = nregs;
533 reg_nonzero_bits = ((unsigned HOST_WIDE_INT *)
534 xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT)));
535 reg_sign_bit_copies
536 = (unsigned char *) xcalloc (nregs, sizeof (unsigned char));
538 reg_last_death = (rtx *) xmalloc (nregs * sizeof (rtx));
539 reg_last_set = (rtx *) xmalloc (nregs * sizeof (rtx));
540 reg_last_set_value = (rtx *) xmalloc (nregs * sizeof (rtx));
541 reg_last_set_table_tick = (int *) xmalloc (nregs * sizeof (int));
542 reg_last_set_label = (int *) xmalloc (nregs * sizeof (int));
543 reg_last_set_invalid = (char *) xmalloc (nregs * sizeof (char));
544 reg_last_set_mode
545 = (enum machine_mode *) xmalloc (nregs * sizeof (enum machine_mode));
546 reg_last_set_nonzero_bits
547 = (unsigned HOST_WIDE_INT *) xmalloc (nregs * sizeof (HOST_WIDE_INT));
548 reg_last_set_sign_bit_copies
549 = (char *) xmalloc (nregs * sizeof (char));
551 init_reg_last_arrays ();
553 init_recog_no_volatile ();
555 /* Compute maximum uid value so uid_cuid can be allocated. */
557 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
558 if (INSN_UID (insn) > i)
559 i = INSN_UID (insn);
561 uid_cuid = (int *) xmalloc ((i + 1) * sizeof (int));
562 max_uid_cuid = i;
564 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
566 /* Don't use reg_nonzero_bits when computing it. This can cause problems
567 when, for example, we have j <<= 1 in a loop. */
569 nonzero_sign_valid = 0;
571 /* Compute the mapping from uids to cuids.
572 Cuids are numbers assigned to insns, like uids,
573 except that cuids increase monotonically through the code.
575 Scan all SETs and see if we can deduce anything about what
576 bits are known to be zero for some registers and how many copies
577 of the sign bit are known to exist for those registers.
579 Also set any known values so that we can use it while searching
580 for what bits are known to be set. */
582 label_tick = 1;
584 setup_incoming_promotions ();
586 refresh_blocks = sbitmap_alloc (last_basic_block);
587 sbitmap_zero (refresh_blocks);
589 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
591 uid_cuid[INSN_UID (insn)] = ++i;
592 subst_low_cuid = i;
593 subst_insn = insn;
595 if (INSN_P (insn))
597 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
598 NULL);
599 record_dead_and_set_regs (insn);
601 #ifdef AUTO_INC_DEC
602 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
603 if (REG_NOTE_KIND (links) == REG_INC)
604 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
605 NULL);
606 #endif
609 if (GET_CODE (insn) == CODE_LABEL)
610 label_tick++;
613 nonzero_sign_valid = 1;
615 /* Now scan all the insns in forward order. */
617 label_tick = 1;
618 last_call_cuid = 0;
619 mem_last_set = 0;
620 init_reg_last_arrays ();
621 setup_incoming_promotions ();
623 FOR_EACH_BB (this_basic_block)
625 for (insn = this_basic_block->head;
626 insn != NEXT_INSN (this_basic_block->end);
627 insn = next ? next : NEXT_INSN (insn))
629 next = 0;
631 if (GET_CODE (insn) == CODE_LABEL)
632 label_tick++;
634 else if (INSN_P (insn))
636 /* See if we know about function return values before this
637 insn based upon SUBREG flags. */
638 check_promoted_subreg (insn, PATTERN (insn));
640 /* Try this insn with each insn it links back to. */
642 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
643 if ((next = try_combine (insn, XEXP (links, 0),
644 NULL_RTX, &new_direct_jump_p)) != 0)
645 goto retry;
647 /* Try each sequence of three linked insns ending with this one. */
649 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
651 rtx link = XEXP (links, 0);
653 /* If the linked insn has been replaced by a note, then there
654 is no point in pursuing this chain any further. */
655 if (GET_CODE (link) == NOTE)
656 continue;
658 for (nextlinks = LOG_LINKS (link);
659 nextlinks;
660 nextlinks = XEXP (nextlinks, 1))
661 if ((next = try_combine (insn, link,
662 XEXP (nextlinks, 0),
663 &new_direct_jump_p)) != 0)
664 goto retry;
667 #ifdef HAVE_cc0
668 /* Try to combine a jump insn that uses CC0
669 with a preceding insn that sets CC0, and maybe with its
670 logical predecessor as well.
671 This is how we make decrement-and-branch insns.
672 We need this special code because data flow connections
673 via CC0 do not get entered in LOG_LINKS. */
675 if (GET_CODE (insn) == JUMP_INSN
676 && (prev = prev_nonnote_insn (insn)) != 0
677 && GET_CODE (prev) == INSN
678 && sets_cc0_p (PATTERN (prev)))
680 if ((next = try_combine (insn, prev,
681 NULL_RTX, &new_direct_jump_p)) != 0)
682 goto retry;
684 for (nextlinks = LOG_LINKS (prev); nextlinks;
685 nextlinks = XEXP (nextlinks, 1))
686 if ((next = try_combine (insn, prev,
687 XEXP (nextlinks, 0),
688 &new_direct_jump_p)) != 0)
689 goto retry;
692 /* Do the same for an insn that explicitly references CC0. */
693 if (GET_CODE (insn) == INSN
694 && (prev = prev_nonnote_insn (insn)) != 0
695 && GET_CODE (prev) == INSN
696 && sets_cc0_p (PATTERN (prev))
697 && GET_CODE (PATTERN (insn)) == SET
698 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
700 if ((next = try_combine (insn, prev,
701 NULL_RTX, &new_direct_jump_p)) != 0)
702 goto retry;
704 for (nextlinks = LOG_LINKS (prev); nextlinks;
705 nextlinks = XEXP (nextlinks, 1))
706 if ((next = try_combine (insn, prev,
707 XEXP (nextlinks, 0),
708 &new_direct_jump_p)) != 0)
709 goto retry;
712 /* Finally, see if any of the insns that this insn links to
713 explicitly references CC0. If so, try this insn, that insn,
714 and its predecessor if it sets CC0. */
715 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
716 if (GET_CODE (XEXP (links, 0)) == INSN
717 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
718 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
719 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
720 && GET_CODE (prev) == INSN
721 && sets_cc0_p (PATTERN (prev))
722 && (next = try_combine (insn, XEXP (links, 0),
723 prev, &new_direct_jump_p)) != 0)
724 goto retry;
725 #endif
727 /* Try combining an insn with two different insns whose results it
728 uses. */
729 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
730 for (nextlinks = XEXP (links, 1); nextlinks;
731 nextlinks = XEXP (nextlinks, 1))
732 if ((next = try_combine (insn, XEXP (links, 0),
733 XEXP (nextlinks, 0),
734 &new_direct_jump_p)) != 0)
735 goto retry;
737 if (GET_CODE (insn) != NOTE)
738 record_dead_and_set_regs (insn);
740 retry:
745 clear_bb_flags ();
747 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
748 BASIC_BLOCK (i)->flags |= BB_DIRTY);
749 new_direct_jump_p |= purge_all_dead_edges (0);
750 delete_noop_moves (f);
752 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
753 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
754 | PROP_KILL_DEAD_CODE);
756 /* Clean up. */
757 sbitmap_free (refresh_blocks);
758 free (reg_nonzero_bits);
759 free (reg_sign_bit_copies);
760 free (reg_last_death);
761 free (reg_last_set);
762 free (reg_last_set_value);
763 free (reg_last_set_table_tick);
764 free (reg_last_set_label);
765 free (reg_last_set_invalid);
766 free (reg_last_set_mode);
767 free (reg_last_set_nonzero_bits);
768 free (reg_last_set_sign_bit_copies);
769 free (uid_cuid);
772 struct undo *undo, *next;
773 for (undo = undobuf.frees; undo; undo = next)
775 next = undo->next;
776 free (undo);
778 undobuf.frees = 0;
781 total_attempts += combine_attempts;
782 total_merges += combine_merges;
783 total_extras += combine_extras;
784 total_successes += combine_successes;
786 nonzero_sign_valid = 0;
788 /* Make recognizer allow volatile MEMs again. */
789 init_recog ();
791 return new_direct_jump_p;
794 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
796 static void
797 init_reg_last_arrays ()
799 unsigned int nregs = combine_max_regno;
801 memset ((char *) reg_last_death, 0, nregs * sizeof (rtx));
802 memset ((char *) reg_last_set, 0, nregs * sizeof (rtx));
803 memset ((char *) reg_last_set_value, 0, nregs * sizeof (rtx));
804 memset ((char *) reg_last_set_table_tick, 0, nregs * sizeof (int));
805 memset ((char *) reg_last_set_label, 0, nregs * sizeof (int));
806 memset (reg_last_set_invalid, 0, nregs * sizeof (char));
807 memset ((char *) reg_last_set_mode, 0, nregs * sizeof (enum machine_mode));
808 memset ((char *) reg_last_set_nonzero_bits, 0, nregs * sizeof (HOST_WIDE_INT));
809 memset (reg_last_set_sign_bit_copies, 0, nregs * sizeof (char));
812 /* Set up any promoted values for incoming argument registers. */
814 static void
815 setup_incoming_promotions ()
817 #ifdef PROMOTE_FUNCTION_ARGS
818 unsigned int regno;
819 rtx reg;
820 enum machine_mode mode;
821 int unsignedp;
822 rtx first = get_insns ();
824 #ifndef OUTGOING_REGNO
825 #define OUTGOING_REGNO(N) N
826 #endif
827 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
828 /* Check whether this register can hold an incoming pointer
829 argument. FUNCTION_ARG_REGNO_P tests outgoing register
830 numbers, so translate if necessary due to register windows. */
831 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
832 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
834 record_value_for_reg
835 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
836 : SIGN_EXTEND),
837 GET_MODE (reg),
838 gen_rtx_CLOBBER (mode, const0_rtx)));
840 #endif
843 /* Called via note_stores. If X is a pseudo that is narrower than
844 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
846 If we are setting only a portion of X and we can't figure out what
847 portion, assume all bits will be used since we don't know what will
848 be happening.
850 Similarly, set how many bits of X are known to be copies of the sign bit
851 at all locations in the function. This is the smallest number implied
852 by any set of X. */
854 static void
855 set_nonzero_bits_and_sign_copies (x, set, data)
856 rtx x;
857 rtx set;
858 void *data ATTRIBUTE_UNUSED;
860 unsigned int num;
862 if (GET_CODE (x) == REG
863 && REGNO (x) >= FIRST_PSEUDO_REGISTER
864 /* If this register is undefined at the start of the file, we can't
865 say what its contents were. */
866 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
867 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
869 if (set == 0 || GET_CODE (set) == CLOBBER)
871 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
872 reg_sign_bit_copies[REGNO (x)] = 1;
873 return;
876 /* If this is a complex assignment, see if we can convert it into a
877 simple assignment. */
878 set = expand_field_assignment (set);
880 /* If this is a simple assignment, or we have a paradoxical SUBREG,
881 set what we know about X. */
883 if (SET_DEST (set) == x
884 || (GET_CODE (SET_DEST (set)) == SUBREG
885 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
886 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
887 && SUBREG_REG (SET_DEST (set)) == x))
889 rtx src = SET_SRC (set);
891 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
892 /* If X is narrower than a word and SRC is a non-negative
893 constant that would appear negative in the mode of X,
894 sign-extend it for use in reg_nonzero_bits because some
895 machines (maybe most) will actually do the sign-extension
896 and this is the conservative approach.
898 ??? For 2.5, try to tighten up the MD files in this regard
899 instead of this kludge. */
901 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
902 && GET_CODE (src) == CONST_INT
903 && INTVAL (src) > 0
904 && 0 != (INTVAL (src)
905 & ((HOST_WIDE_INT) 1
906 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
907 src = GEN_INT (INTVAL (src)
908 | ((HOST_WIDE_INT) (-1)
909 << GET_MODE_BITSIZE (GET_MODE (x))));
910 #endif
912 /* Don't call nonzero_bits if it cannot change anything. */
913 if (reg_nonzero_bits[REGNO (x)] != ~(unsigned HOST_WIDE_INT) 0)
914 reg_nonzero_bits[REGNO (x)]
915 |= nonzero_bits (src, nonzero_bits_mode);
916 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
917 if (reg_sign_bit_copies[REGNO (x)] == 0
918 || reg_sign_bit_copies[REGNO (x)] > num)
919 reg_sign_bit_copies[REGNO (x)] = num;
921 else
923 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
924 reg_sign_bit_copies[REGNO (x)] = 1;
929 /* See if INSN can be combined into I3. PRED and SUCC are optionally
930 insns that were previously combined into I3 or that will be combined
931 into the merger of INSN and I3.
933 Return 0 if the combination is not allowed for any reason.
935 If the combination is allowed, *PDEST will be set to the single
936 destination of INSN and *PSRC to the single source, and this function
937 will return 1. */
939 static int
940 can_combine_p (insn, i3, pred, succ, pdest, psrc)
941 rtx insn;
942 rtx i3;
943 rtx pred ATTRIBUTE_UNUSED;
944 rtx succ;
945 rtx *pdest, *psrc;
947 int i;
948 rtx set = 0, src, dest;
949 rtx p;
950 #ifdef AUTO_INC_DEC
951 rtx link;
952 #endif
953 int all_adjacent = (succ ? (next_active_insn (insn) == succ
954 && next_active_insn (succ) == i3)
955 : next_active_insn (insn) == i3);
957 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
958 or a PARALLEL consisting of such a SET and CLOBBERs.
960 If INSN has CLOBBER parallel parts, ignore them for our processing.
961 By definition, these happen during the execution of the insn. When it
962 is merged with another insn, all bets are off. If they are, in fact,
963 needed and aren't also supplied in I3, they may be added by
964 recog_for_combine. Otherwise, it won't match.
966 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
967 note.
969 Get the source and destination of INSN. If more than one, can't
970 combine. */
972 if (GET_CODE (PATTERN (insn)) == SET)
973 set = PATTERN (insn);
974 else if (GET_CODE (PATTERN (insn)) == PARALLEL
975 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
977 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
979 rtx elt = XVECEXP (PATTERN (insn), 0, i);
981 switch (GET_CODE (elt))
983 /* This is important to combine floating point insns
984 for the SH4 port. */
985 case USE:
986 /* Combining an isolated USE doesn't make sense.
987 We depend here on combinable_i3pat to reject them. */
988 /* The code below this loop only verifies that the inputs of
989 the SET in INSN do not change. We call reg_set_between_p
990 to verify that the REG in the USE does not change between
991 I3 and INSN.
992 If the USE in INSN was for a pseudo register, the matching
993 insn pattern will likely match any register; combining this
994 with any other USE would only be safe if we knew that the
995 used registers have identical values, or if there was
996 something to tell them apart, e.g. different modes. For
997 now, we forgo such complicated tests and simply disallow
998 combining of USES of pseudo registers with any other USE. */
999 if (GET_CODE (XEXP (elt, 0)) == REG
1000 && GET_CODE (PATTERN (i3)) == PARALLEL)
1002 rtx i3pat = PATTERN (i3);
1003 int i = XVECLEN (i3pat, 0) - 1;
1004 unsigned int regno = REGNO (XEXP (elt, 0));
1008 rtx i3elt = XVECEXP (i3pat, 0, i);
1010 if (GET_CODE (i3elt) == USE
1011 && GET_CODE (XEXP (i3elt, 0)) == REG
1012 && (REGNO (XEXP (i3elt, 0)) == regno
1013 ? reg_set_between_p (XEXP (elt, 0),
1014 PREV_INSN (insn), i3)
1015 : regno >= FIRST_PSEUDO_REGISTER))
1016 return 0;
1018 while (--i >= 0);
1020 break;
1022 /* We can ignore CLOBBERs. */
1023 case CLOBBER:
1024 break;
1026 case SET:
1027 /* Ignore SETs whose result isn't used but not those that
1028 have side-effects. */
1029 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1030 && ! side_effects_p (elt))
1031 break;
1033 /* If we have already found a SET, this is a second one and
1034 so we cannot combine with this insn. */
1035 if (set)
1036 return 0;
1038 set = elt;
1039 break;
1041 default:
1042 /* Anything else means we can't combine. */
1043 return 0;
1047 if (set == 0
1048 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1049 so don't do anything with it. */
1050 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1051 return 0;
1053 else
1054 return 0;
1056 if (set == 0)
1057 return 0;
1059 set = expand_field_assignment (set);
1060 src = SET_SRC (set), dest = SET_DEST (set);
1062 /* Don't eliminate a store in the stack pointer. */
1063 if (dest == stack_pointer_rtx
1064 /* Don't combine with an insn that sets a register to itself if it has
1065 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1066 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1067 /* Can't merge an ASM_OPERANDS. */
1068 || GET_CODE (src) == ASM_OPERANDS
1069 /* Can't merge a function call. */
1070 || GET_CODE (src) == CALL
1071 /* Don't eliminate a function call argument. */
1072 || (GET_CODE (i3) == CALL_INSN
1073 && (find_reg_fusage (i3, USE, dest)
1074 || (GET_CODE (dest) == REG
1075 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1076 && global_regs[REGNO (dest)])))
1077 /* Don't substitute into an incremented register. */
1078 || FIND_REG_INC_NOTE (i3, dest)
1079 || (succ && FIND_REG_INC_NOTE (succ, dest))
1080 #if 0
1081 /* Don't combine the end of a libcall into anything. */
1082 /* ??? This gives worse code, and appears to be unnecessary, since no
1083 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1084 use REG_RETVAL notes for noconflict blocks, but other code here
1085 makes sure that those insns don't disappear. */
1086 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1087 #endif
1088 /* Make sure that DEST is not used after SUCC but before I3. */
1089 || (succ && ! all_adjacent
1090 && reg_used_between_p (dest, succ, i3))
1091 /* Make sure that the value that is to be substituted for the register
1092 does not use any registers whose values alter in between. However,
1093 If the insns are adjacent, a use can't cross a set even though we
1094 think it might (this can happen for a sequence of insns each setting
1095 the same destination; reg_last_set of that register might point to
1096 a NOTE). If INSN has a REG_EQUIV note, the register is always
1097 equivalent to the memory so the substitution is valid even if there
1098 are intervening stores. Also, don't move a volatile asm or
1099 UNSPEC_VOLATILE across any other insns. */
1100 || (! all_adjacent
1101 && (((GET_CODE (src) != MEM
1102 || ! find_reg_note (insn, REG_EQUIV, src))
1103 && use_crosses_set_p (src, INSN_CUID (insn)))
1104 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1105 || GET_CODE (src) == UNSPEC_VOLATILE))
1106 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1107 better register allocation by not doing the combine. */
1108 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1109 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1110 /* Don't combine across a CALL_INSN, because that would possibly
1111 change whether the life span of some REGs crosses calls or not,
1112 and it is a pain to update that information.
1113 Exception: if source is a constant, moving it later can't hurt.
1114 Accept that special case, because it helps -fforce-addr a lot. */
1115 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1116 return 0;
1118 /* DEST must either be a REG or CC0. */
1119 if (GET_CODE (dest) == REG)
1121 /* If register alignment is being enforced for multi-word items in all
1122 cases except for parameters, it is possible to have a register copy
1123 insn referencing a hard register that is not allowed to contain the
1124 mode being copied and which would not be valid as an operand of most
1125 insns. Eliminate this problem by not combining with such an insn.
1127 Also, on some machines we don't want to extend the life of a hard
1128 register. */
1130 if (GET_CODE (src) == REG
1131 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1132 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1133 /* Don't extend the life of a hard register unless it is
1134 user variable (if we have few registers) or it can't
1135 fit into the desired register (meaning something special
1136 is going on).
1137 Also avoid substituting a return register into I3, because
1138 reload can't handle a conflict with constraints of other
1139 inputs. */
1140 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1141 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1142 return 0;
1144 else if (GET_CODE (dest) != CC0)
1145 return 0;
1147 /* Don't substitute for a register intended as a clobberable operand.
1148 Similarly, don't substitute an expression containing a register that
1149 will be clobbered in I3. */
1150 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1151 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1152 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1153 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1154 src)
1155 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1156 return 0;
1158 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1159 or not), reject, unless nothing volatile comes between it and I3 */
1161 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1163 /* Make sure succ doesn't contain a volatile reference. */
1164 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1165 return 0;
1167 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1168 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1169 return 0;
1172 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1173 to be an explicit register variable, and was chosen for a reason. */
1175 if (GET_CODE (src) == ASM_OPERANDS
1176 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1177 return 0;
1179 /* If there are any volatile insns between INSN and I3, reject, because
1180 they might affect machine state. */
1182 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1183 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1184 return 0;
1186 /* If INSN or I2 contains an autoincrement or autodecrement,
1187 make sure that register is not used between there and I3,
1188 and not already used in I3 either.
1189 Also insist that I3 not be a jump; if it were one
1190 and the incremented register were spilled, we would lose. */
1192 #ifdef AUTO_INC_DEC
1193 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1194 if (REG_NOTE_KIND (link) == REG_INC
1195 && (GET_CODE (i3) == JUMP_INSN
1196 || reg_used_between_p (XEXP (link, 0), insn, i3)
1197 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1198 return 0;
1199 #endif
1201 #ifdef HAVE_cc0
1202 /* Don't combine an insn that follows a CC0-setting insn.
1203 An insn that uses CC0 must not be separated from the one that sets it.
1204 We do, however, allow I2 to follow a CC0-setting insn if that insn
1205 is passed as I1; in that case it will be deleted also.
1206 We also allow combining in this case if all the insns are adjacent
1207 because that would leave the two CC0 insns adjacent as well.
1208 It would be more logical to test whether CC0 occurs inside I1 or I2,
1209 but that would be much slower, and this ought to be equivalent. */
1211 p = prev_nonnote_insn (insn);
1212 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1213 && ! all_adjacent)
1214 return 0;
1215 #endif
1217 /* If we get here, we have passed all the tests and the combination is
1218 to be allowed. */
1220 *pdest = dest;
1221 *psrc = src;
1223 return 1;
1226 /* Check if PAT is an insn - or a part of it - used to set up an
1227 argument for a function in a hard register. */
1229 static int
1230 sets_function_arg_p (pat)
1231 rtx pat;
1233 int i;
1234 rtx inner_dest;
1236 switch (GET_CODE (pat))
1238 case INSN:
1239 return sets_function_arg_p (PATTERN (pat));
1241 case PARALLEL:
1242 for (i = XVECLEN (pat, 0); --i >= 0;)
1243 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1244 return 1;
1246 break;
1248 case SET:
1249 inner_dest = SET_DEST (pat);
1250 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1251 || GET_CODE (inner_dest) == SUBREG
1252 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1253 inner_dest = XEXP (inner_dest, 0);
1255 return (GET_CODE (inner_dest) == REG
1256 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1257 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1259 default:
1260 break;
1263 return 0;
1266 /* LOC is the location within I3 that contains its pattern or the component
1267 of a PARALLEL of the pattern. We validate that it is valid for combining.
1269 One problem is if I3 modifies its output, as opposed to replacing it
1270 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1271 so would produce an insn that is not equivalent to the original insns.
1273 Consider:
1275 (set (reg:DI 101) (reg:DI 100))
1276 (set (subreg:SI (reg:DI 101) 0) <foo>)
1278 This is NOT equivalent to:
1280 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1281 (set (reg:DI 101) (reg:DI 100))])
1283 Not only does this modify 100 (in which case it might still be valid
1284 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1286 We can also run into a problem if I2 sets a register that I1
1287 uses and I1 gets directly substituted into I3 (not via I2). In that
1288 case, we would be getting the wrong value of I2DEST into I3, so we
1289 must reject the combination. This case occurs when I2 and I1 both
1290 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1291 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1292 of a SET must prevent combination from occurring.
1294 Before doing the above check, we first try to expand a field assignment
1295 into a set of logical operations.
1297 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1298 we place a register that is both set and used within I3. If more than one
1299 such register is detected, we fail.
1301 Return 1 if the combination is valid, zero otherwise. */
1303 static int
1304 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1305 rtx i3;
1306 rtx *loc;
1307 rtx i2dest;
1308 rtx i1dest;
1309 int i1_not_in_src;
1310 rtx *pi3dest_killed;
1312 rtx x = *loc;
1314 if (GET_CODE (x) == SET)
1316 rtx set = x ;
1317 rtx dest = SET_DEST (set);
1318 rtx src = SET_SRC (set);
1319 rtx inner_dest = dest;
1321 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1322 || GET_CODE (inner_dest) == SUBREG
1323 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1324 inner_dest = XEXP (inner_dest, 0);
1326 /* Check for the case where I3 modifies its output, as
1327 discussed above. */
1328 if ((inner_dest != dest
1329 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1330 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1332 /* This is the same test done in can_combine_p except we can't test
1333 all_adjacent; we don't have to, since this instruction will stay
1334 in place, thus we are not considering increasing the lifetime of
1335 INNER_DEST.
1337 Also, if this insn sets a function argument, combining it with
1338 something that might need a spill could clobber a previous
1339 function argument; the all_adjacent test in can_combine_p also
1340 checks this; here, we do a more specific test for this case. */
1342 || (GET_CODE (inner_dest) == REG
1343 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1344 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1345 GET_MODE (inner_dest))))
1346 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1347 return 0;
1349 /* If DEST is used in I3, it is being killed in this insn,
1350 so record that for later.
1351 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1352 STACK_POINTER_REGNUM, since these are always considered to be
1353 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1354 if (pi3dest_killed && GET_CODE (dest) == REG
1355 && reg_referenced_p (dest, PATTERN (i3))
1356 && REGNO (dest) != FRAME_POINTER_REGNUM
1357 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1358 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1359 #endif
1360 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1361 && (REGNO (dest) != ARG_POINTER_REGNUM
1362 || ! fixed_regs [REGNO (dest)])
1363 #endif
1364 && REGNO (dest) != STACK_POINTER_REGNUM)
1366 if (*pi3dest_killed)
1367 return 0;
1369 *pi3dest_killed = dest;
1373 else if (GET_CODE (x) == PARALLEL)
1375 int i;
1377 for (i = 0; i < XVECLEN (x, 0); i++)
1378 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1379 i1_not_in_src, pi3dest_killed))
1380 return 0;
1383 return 1;
1386 /* Return 1 if X is an arithmetic expression that contains a multiplication
1387 and division. We don't count multiplications by powers of two here. */
1389 static int
1390 contains_muldiv (x)
1391 rtx x;
1393 switch (GET_CODE (x))
1395 case MOD: case DIV: case UMOD: case UDIV:
1396 return 1;
1398 case MULT:
1399 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1400 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1401 default:
1402 switch (GET_RTX_CLASS (GET_CODE (x)))
1404 case 'c': case '<': case '2':
1405 return contains_muldiv (XEXP (x, 0))
1406 || contains_muldiv (XEXP (x, 1));
1408 case '1':
1409 return contains_muldiv (XEXP (x, 0));
1411 default:
1412 return 0;
1417 /* Determine whether INSN can be used in a combination. Return nonzero if
1418 not. This is used in try_combine to detect early some cases where we
1419 can't perform combinations. */
1421 static int
1422 cant_combine_insn_p (insn)
1423 rtx insn;
1425 rtx set;
1426 rtx src, dest;
1428 /* If this isn't really an insn, we can't do anything.
1429 This can occur when flow deletes an insn that it has merged into an
1430 auto-increment address. */
1431 if (! INSN_P (insn))
1432 return 1;
1434 /* Never combine loads and stores involving hard regs that are likely
1435 to be spilled. The register allocator can usually handle such
1436 reg-reg moves by tying. If we allow the combiner to make
1437 substitutions of likely-spilled regs, we may abort in reload.
1438 As an exception, we allow combinations involving fixed regs; these are
1439 not available to the register allocator so there's no risk involved. */
1441 set = single_set (insn);
1442 if (! set)
1443 return 0;
1444 src = SET_SRC (set);
1445 dest = SET_DEST (set);
1446 if (GET_CODE (src) == SUBREG)
1447 src = SUBREG_REG (src);
1448 if (GET_CODE (dest) == SUBREG)
1449 dest = SUBREG_REG (dest);
1450 if (REG_P (src) && REG_P (dest)
1451 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1452 && ! fixed_regs[REGNO (src)]
1453 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1454 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1455 && ! fixed_regs[REGNO (dest)]
1456 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1457 return 1;
1459 return 0;
1462 /* Try to combine the insns I1 and I2 into I3.
1463 Here I1 and I2 appear earlier than I3.
1464 I1 can be zero; then we combine just I2 into I3.
1466 If we are combining three insns and the resulting insn is not recognized,
1467 try splitting it into two insns. If that happens, I2 and I3 are retained
1468 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1469 are pseudo-deleted.
1471 Return 0 if the combination does not work. Then nothing is changed.
1472 If we did the combination, return the insn at which combine should
1473 resume scanning.
1475 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1476 new direct jump instruction. */
1478 static rtx
1479 try_combine (i3, i2, i1, new_direct_jump_p)
1480 rtx i3, i2, i1;
1481 int *new_direct_jump_p;
1483 /* New patterns for I3 and I2, respectively. */
1484 rtx newpat, newi2pat = 0;
1485 int substed_i2 = 0, substed_i1 = 0;
1486 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1487 int added_sets_1, added_sets_2;
1488 /* Total number of SETs to put into I3. */
1489 int total_sets;
1490 /* Nonzero is I2's body now appears in I3. */
1491 int i2_is_used;
1492 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1493 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1494 /* Contains I3 if the destination of I3 is used in its source, which means
1495 that the old life of I3 is being killed. If that usage is placed into
1496 I2 and not in I3, a REG_DEAD note must be made. */
1497 rtx i3dest_killed = 0;
1498 /* SET_DEST and SET_SRC of I2 and I1. */
1499 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1500 /* PATTERN (I2), or a copy of it in certain cases. */
1501 rtx i2pat;
1502 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1503 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1504 int i1_feeds_i3 = 0;
1505 /* Notes that must be added to REG_NOTES in I3 and I2. */
1506 rtx new_i3_notes, new_i2_notes;
1507 /* Notes that we substituted I3 into I2 instead of the normal case. */
1508 int i3_subst_into_i2 = 0;
1509 /* Notes that I1, I2 or I3 is a MULT operation. */
1510 int have_mult = 0;
1512 int maxreg;
1513 rtx temp;
1514 rtx link;
1515 int i;
1517 /* Exit early if one of the insns involved can't be used for
1518 combinations. */
1519 if (cant_combine_insn_p (i3)
1520 || cant_combine_insn_p (i2)
1521 || (i1 && cant_combine_insn_p (i1))
1522 /* We also can't do anything if I3 has a
1523 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1524 libcall. */
1525 #if 0
1526 /* ??? This gives worse code, and appears to be unnecessary, since no
1527 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1528 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1529 #endif
1531 return 0;
1533 combine_attempts++;
1534 undobuf.other_insn = 0;
1536 /* Reset the hard register usage information. */
1537 CLEAR_HARD_REG_SET (newpat_used_regs);
1539 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1540 code below, set I1 to be the earlier of the two insns. */
1541 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1542 temp = i1, i1 = i2, i2 = temp;
1544 added_links_insn = 0;
1546 /* First check for one important special-case that the code below will
1547 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1548 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1549 we may be able to replace that destination with the destination of I3.
1550 This occurs in the common code where we compute both a quotient and
1551 remainder into a structure, in which case we want to do the computation
1552 directly into the structure to avoid register-register copies.
1554 Note that this case handles both multiple sets in I2 and also
1555 cases where I2 has a number of CLOBBER or PARALLELs.
1557 We make very conservative checks below and only try to handle the
1558 most common cases of this. For example, we only handle the case
1559 where I2 and I3 are adjacent to avoid making difficult register
1560 usage tests. */
1562 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1563 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1564 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1565 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1566 && GET_CODE (PATTERN (i2)) == PARALLEL
1567 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1568 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1569 below would need to check what is inside (and reg_overlap_mentioned_p
1570 doesn't support those codes anyway). Don't allow those destinations;
1571 the resulting insn isn't likely to be recognized anyway. */
1572 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1573 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1574 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1575 SET_DEST (PATTERN (i3)))
1576 && next_real_insn (i2) == i3)
1578 rtx p2 = PATTERN (i2);
1580 /* Make sure that the destination of I3,
1581 which we are going to substitute into one output of I2,
1582 is not used within another output of I2. We must avoid making this:
1583 (parallel [(set (mem (reg 69)) ...)
1584 (set (reg 69) ...)])
1585 which is not well-defined as to order of actions.
1586 (Besides, reload can't handle output reloads for this.)
1588 The problem can also happen if the dest of I3 is a memory ref,
1589 if another dest in I2 is an indirect memory ref. */
1590 for (i = 0; i < XVECLEN (p2, 0); i++)
1591 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1592 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1593 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1594 SET_DEST (XVECEXP (p2, 0, i))))
1595 break;
1597 if (i == XVECLEN (p2, 0))
1598 for (i = 0; i < XVECLEN (p2, 0); i++)
1599 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1600 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1601 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1603 combine_merges++;
1605 subst_insn = i3;
1606 subst_low_cuid = INSN_CUID (i2);
1608 added_sets_2 = added_sets_1 = 0;
1609 i2dest = SET_SRC (PATTERN (i3));
1611 /* Replace the dest in I2 with our dest and make the resulting
1612 insn the new pattern for I3. Then skip to where we
1613 validate the pattern. Everything was set up above. */
1614 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1615 SET_DEST (PATTERN (i3)));
1617 newpat = p2;
1618 i3_subst_into_i2 = 1;
1619 goto validate_replacement;
1623 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1624 one of those words to another constant, merge them by making a new
1625 constant. */
1626 if (i1 == 0
1627 && (temp = single_set (i2)) != 0
1628 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1629 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1630 && GET_CODE (SET_DEST (temp)) == REG
1631 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1632 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1633 && GET_CODE (PATTERN (i3)) == SET
1634 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1635 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1636 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1637 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1638 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1640 HOST_WIDE_INT lo, hi;
1642 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1643 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1644 else
1646 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1647 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1650 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1652 /* We don't handle the case of the target word being wider
1653 than a host wide int. */
1654 if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1655 abort ();
1657 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1658 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1659 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1661 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1662 hi = INTVAL (SET_SRC (PATTERN (i3)));
1663 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1665 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1666 >> (HOST_BITS_PER_WIDE_INT - 1));
1668 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1669 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1670 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1671 (INTVAL (SET_SRC (PATTERN (i3)))));
1672 if (hi == sign)
1673 hi = lo < 0 ? -1 : 0;
1675 else
1676 /* We don't handle the case of the higher word not fitting
1677 entirely in either hi or lo. */
1678 abort ();
1680 combine_merges++;
1681 subst_insn = i3;
1682 subst_low_cuid = INSN_CUID (i2);
1683 added_sets_2 = added_sets_1 = 0;
1684 i2dest = SET_DEST (temp);
1686 SUBST (SET_SRC (temp),
1687 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1689 newpat = PATTERN (i2);
1690 goto validate_replacement;
1693 #ifndef HAVE_cc0
1694 /* If we have no I1 and I2 looks like:
1695 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1696 (set Y OP)])
1697 make up a dummy I1 that is
1698 (set Y OP)
1699 and change I2 to be
1700 (set (reg:CC X) (compare:CC Y (const_int 0)))
1702 (We can ignore any trailing CLOBBERs.)
1704 This undoes a previous combination and allows us to match a branch-and-
1705 decrement insn. */
1707 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1708 && XVECLEN (PATTERN (i2), 0) >= 2
1709 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1710 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1711 == MODE_CC)
1712 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1713 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1714 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1715 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1716 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1717 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1719 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1720 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1721 break;
1723 if (i == 1)
1725 /* We make I1 with the same INSN_UID as I2. This gives it
1726 the same INSN_CUID for value tracking. Our fake I1 will
1727 never appear in the insn stream so giving it the same INSN_UID
1728 as I2 will not cause a problem. */
1730 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1731 BLOCK_FOR_INSN (i2), INSN_SCOPE (i2),
1732 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1733 NULL_RTX);
1735 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1736 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1737 SET_DEST (PATTERN (i1)));
1740 #endif
1742 /* Verify that I2 and I1 are valid for combining. */
1743 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1744 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1746 undo_all ();
1747 return 0;
1750 /* Record whether I2DEST is used in I2SRC and similarly for the other
1751 cases. Knowing this will help in register status updating below. */
1752 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1753 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1754 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1756 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1757 in I2SRC. */
1758 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1760 /* Ensure that I3's pattern can be the destination of combines. */
1761 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1762 i1 && i2dest_in_i1src && i1_feeds_i3,
1763 &i3dest_killed))
1765 undo_all ();
1766 return 0;
1769 /* See if any of the insns is a MULT operation. Unless one is, we will
1770 reject a combination that is, since it must be slower. Be conservative
1771 here. */
1772 if (GET_CODE (i2src) == MULT
1773 || (i1 != 0 && GET_CODE (i1src) == MULT)
1774 || (GET_CODE (PATTERN (i3)) == SET
1775 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1776 have_mult = 1;
1778 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1779 We used to do this EXCEPT in one case: I3 has a post-inc in an
1780 output operand. However, that exception can give rise to insns like
1781 mov r3,(r3)+
1782 which is a famous insn on the PDP-11 where the value of r3 used as the
1783 source was model-dependent. Avoid this sort of thing. */
1785 #if 0
1786 if (!(GET_CODE (PATTERN (i3)) == SET
1787 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1788 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1789 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1790 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1791 /* It's not the exception. */
1792 #endif
1793 #ifdef AUTO_INC_DEC
1794 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1795 if (REG_NOTE_KIND (link) == REG_INC
1796 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1797 || (i1 != 0
1798 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1800 undo_all ();
1801 return 0;
1803 #endif
1805 /* See if the SETs in I1 or I2 need to be kept around in the merged
1806 instruction: whenever the value set there is still needed past I3.
1807 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1809 For the SET in I1, we have two cases: If I1 and I2 independently
1810 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1811 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1812 in I1 needs to be kept around unless I1DEST dies or is set in either
1813 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1814 I1DEST. If so, we know I1 feeds into I2. */
1816 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1818 added_sets_1
1819 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1820 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1822 /* If the set in I2 needs to be kept around, we must make a copy of
1823 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1824 PATTERN (I2), we are only substituting for the original I1DEST, not into
1825 an already-substituted copy. This also prevents making self-referential
1826 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1827 I2DEST. */
1829 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1830 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1831 : PATTERN (i2));
1833 if (added_sets_2)
1834 i2pat = copy_rtx (i2pat);
1836 combine_merges++;
1838 /* Substitute in the latest insn for the regs set by the earlier ones. */
1840 maxreg = max_reg_num ();
1842 subst_insn = i3;
1844 /* It is possible that the source of I2 or I1 may be performing an
1845 unneeded operation, such as a ZERO_EXTEND of something that is known
1846 to have the high part zero. Handle that case by letting subst look at
1847 the innermost one of them.
1849 Another way to do this would be to have a function that tries to
1850 simplify a single insn instead of merging two or more insns. We don't
1851 do this because of the potential of infinite loops and because
1852 of the potential extra memory required. However, doing it the way
1853 we are is a bit of a kludge and doesn't catch all cases.
1855 But only do this if -fexpensive-optimizations since it slows things down
1856 and doesn't usually win. */
1858 if (flag_expensive_optimizations)
1860 /* Pass pc_rtx so no substitutions are done, just simplifications.
1861 The cases that we are interested in here do not involve the few
1862 cases were is_replaced is checked. */
1863 if (i1)
1865 subst_low_cuid = INSN_CUID (i1);
1866 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1868 else
1870 subst_low_cuid = INSN_CUID (i2);
1871 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1875 #ifndef HAVE_cc0
1876 /* Many machines that don't use CC0 have insns that can both perform an
1877 arithmetic operation and set the condition code. These operations will
1878 be represented as a PARALLEL with the first element of the vector
1879 being a COMPARE of an arithmetic operation with the constant zero.
1880 The second element of the vector will set some pseudo to the result
1881 of the same arithmetic operation. If we simplify the COMPARE, we won't
1882 match such a pattern and so will generate an extra insn. Here we test
1883 for this case, where both the comparison and the operation result are
1884 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1885 I2SRC. Later we will make the PARALLEL that contains I2. */
1887 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1888 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1889 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1890 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1892 #ifdef EXTRA_CC_MODES
1893 rtx *cc_use;
1894 enum machine_mode compare_mode;
1895 #endif
1897 newpat = PATTERN (i3);
1898 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1900 i2_is_used = 1;
1902 #ifdef EXTRA_CC_MODES
1903 /* See if a COMPARE with the operand we substituted in should be done
1904 with the mode that is currently being used. If not, do the same
1905 processing we do in `subst' for a SET; namely, if the destination
1906 is used only once, try to replace it with a register of the proper
1907 mode and also replace the COMPARE. */
1908 if (undobuf.other_insn == 0
1909 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1910 &undobuf.other_insn))
1911 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1912 i2src, const0_rtx))
1913 != GET_MODE (SET_DEST (newpat))))
1915 unsigned int regno = REGNO (SET_DEST (newpat));
1916 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1918 if (regno < FIRST_PSEUDO_REGISTER
1919 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1920 && ! REG_USERVAR_P (SET_DEST (newpat))))
1922 if (regno >= FIRST_PSEUDO_REGISTER)
1923 SUBST (regno_reg_rtx[regno], new_dest);
1925 SUBST (SET_DEST (newpat), new_dest);
1926 SUBST (XEXP (*cc_use, 0), new_dest);
1927 SUBST (SET_SRC (newpat),
1928 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1930 else
1931 undobuf.other_insn = 0;
1933 #endif
1935 else
1936 #endif
1938 n_occurrences = 0; /* `subst' counts here */
1940 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1941 need to make a unique copy of I2SRC each time we substitute it
1942 to avoid self-referential rtl. */
1944 subst_low_cuid = INSN_CUID (i2);
1945 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1946 ! i1_feeds_i3 && i1dest_in_i1src);
1947 substed_i2 = 1;
1949 /* Record whether i2's body now appears within i3's body. */
1950 i2_is_used = n_occurrences;
1953 /* If we already got a failure, don't try to do more. Otherwise,
1954 try to substitute in I1 if we have it. */
1956 if (i1 && GET_CODE (newpat) != CLOBBER)
1958 /* Before we can do this substitution, we must redo the test done
1959 above (see detailed comments there) that ensures that I1DEST
1960 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1962 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1963 0, (rtx*) 0))
1965 undo_all ();
1966 return 0;
1969 n_occurrences = 0;
1970 subst_low_cuid = INSN_CUID (i1);
1971 newpat = subst (newpat, i1dest, i1src, 0, 0);
1972 substed_i1 = 1;
1975 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1976 to count all the ways that I2SRC and I1SRC can be used. */
1977 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1978 && i2_is_used + added_sets_2 > 1)
1979 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1980 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1981 > 1))
1982 /* Fail if we tried to make a new register (we used to abort, but there's
1983 really no reason to). */
1984 || max_reg_num () != maxreg
1985 /* Fail if we couldn't do something and have a CLOBBER. */
1986 || GET_CODE (newpat) == CLOBBER
1987 /* Fail if this new pattern is a MULT and we didn't have one before
1988 at the outer level. */
1989 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1990 && ! have_mult))
1992 undo_all ();
1993 return 0;
1996 /* If the actions of the earlier insns must be kept
1997 in addition to substituting them into the latest one,
1998 we must make a new PARALLEL for the latest insn
1999 to hold additional the SETs. */
2001 if (added_sets_1 || added_sets_2)
2003 combine_extras++;
2005 if (GET_CODE (newpat) == PARALLEL)
2007 rtvec old = XVEC (newpat, 0);
2008 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2009 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2010 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2011 sizeof (old->elem[0]) * old->num_elem);
2013 else
2015 rtx old = newpat;
2016 total_sets = 1 + added_sets_1 + added_sets_2;
2017 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2018 XVECEXP (newpat, 0, 0) = old;
2021 if (added_sets_1)
2022 XVECEXP (newpat, 0, --total_sets)
2023 = (GET_CODE (PATTERN (i1)) == PARALLEL
2024 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2026 if (added_sets_2)
2028 /* If there is no I1, use I2's body as is. We used to also not do
2029 the subst call below if I2 was substituted into I3,
2030 but that could lose a simplification. */
2031 if (i1 == 0)
2032 XVECEXP (newpat, 0, --total_sets) = i2pat;
2033 else
2034 /* See comment where i2pat is assigned. */
2035 XVECEXP (newpat, 0, --total_sets)
2036 = subst (i2pat, i1dest, i1src, 0, 0);
2040 /* We come here when we are replacing a destination in I2 with the
2041 destination of I3. */
2042 validate_replacement:
2044 /* Note which hard regs this insn has as inputs. */
2045 mark_used_regs_combine (newpat);
2047 /* Is the result of combination a valid instruction? */
2048 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2050 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2051 the second SET's destination is a register that is unused. In that case,
2052 we just need the first SET. This can occur when simplifying a divmod
2053 insn. We *must* test for this case here because the code below that
2054 splits two independent SETs doesn't handle this case correctly when it
2055 updates the register status. Also check the case where the first
2056 SET's destination is unused. That would not cause incorrect code, but
2057 does cause an unneeded insn to remain. */
2059 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2060 && XVECLEN (newpat, 0) == 2
2061 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2062 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2063 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
2064 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
2065 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
2066 && asm_noperands (newpat) < 0)
2068 newpat = XVECEXP (newpat, 0, 0);
2069 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2072 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2073 && XVECLEN (newpat, 0) == 2
2074 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2075 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2076 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
2077 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
2078 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
2079 && asm_noperands (newpat) < 0)
2081 newpat = XVECEXP (newpat, 0, 1);
2082 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2085 /* If we were combining three insns and the result is a simple SET
2086 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2087 insns. There are two ways to do this. It can be split using a
2088 machine-specific method (like when you have an addition of a large
2089 constant) or by combine in the function find_split_point. */
2091 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2092 && asm_noperands (newpat) < 0)
2094 rtx m_split, *split;
2095 rtx ni2dest = i2dest;
2097 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2098 use I2DEST as a scratch register will help. In the latter case,
2099 convert I2DEST to the mode of the source of NEWPAT if we can. */
2101 m_split = split_insns (newpat, i3);
2103 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2104 inputs of NEWPAT. */
2106 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2107 possible to try that as a scratch reg. This would require adding
2108 more code to make it work though. */
2110 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2112 /* If I2DEST is a hard register or the only use of a pseudo,
2113 we can change its mode. */
2114 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2115 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2116 && GET_CODE (i2dest) == REG
2117 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2118 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2119 && ! REG_USERVAR_P (i2dest))))
2120 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2121 REGNO (i2dest));
2123 m_split = split_insns (gen_rtx_PARALLEL
2124 (VOIDmode,
2125 gen_rtvec (2, newpat,
2126 gen_rtx_CLOBBER (VOIDmode,
2127 ni2dest))),
2128 i3);
2129 /* If the split with the mode-changed register didn't work, try
2130 the original register. */
2131 if (! m_split && ni2dest != i2dest)
2133 ni2dest = i2dest;
2134 m_split = split_insns (gen_rtx_PARALLEL
2135 (VOIDmode,
2136 gen_rtvec (2, newpat,
2137 gen_rtx_CLOBBER (VOIDmode,
2138 i2dest))),
2139 i3);
2143 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2145 m_split = PATTERN (m_split);
2146 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2147 if (insn_code_number >= 0)
2148 newpat = m_split;
2150 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2151 && (next_real_insn (i2) == i3
2152 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2154 rtx i2set, i3set;
2155 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2156 newi2pat = PATTERN (m_split);
2158 i3set = single_set (NEXT_INSN (m_split));
2159 i2set = single_set (m_split);
2161 /* In case we changed the mode of I2DEST, replace it in the
2162 pseudo-register table here. We can't do it above in case this
2163 code doesn't get executed and we do a split the other way. */
2165 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2166 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2168 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2170 /* If I2 or I3 has multiple SETs, we won't know how to track
2171 register status, so don't use these insns. If I2's destination
2172 is used between I2 and I3, we also can't use these insns. */
2174 if (i2_code_number >= 0 && i2set && i3set
2175 && (next_real_insn (i2) == i3
2176 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2177 insn_code_number = recog_for_combine (&newi3pat, i3,
2178 &new_i3_notes);
2179 if (insn_code_number >= 0)
2180 newpat = newi3pat;
2182 /* It is possible that both insns now set the destination of I3.
2183 If so, we must show an extra use of it. */
2185 if (insn_code_number >= 0)
2187 rtx new_i3_dest = SET_DEST (i3set);
2188 rtx new_i2_dest = SET_DEST (i2set);
2190 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2191 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2192 || GET_CODE (new_i3_dest) == SUBREG)
2193 new_i3_dest = XEXP (new_i3_dest, 0);
2195 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2196 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2197 || GET_CODE (new_i2_dest) == SUBREG)
2198 new_i2_dest = XEXP (new_i2_dest, 0);
2200 if (GET_CODE (new_i3_dest) == REG
2201 && GET_CODE (new_i2_dest) == REG
2202 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2203 REG_N_SETS (REGNO (new_i2_dest))++;
2207 /* If we can split it and use I2DEST, go ahead and see if that
2208 helps things be recognized. Verify that none of the registers
2209 are set between I2 and I3. */
2210 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2211 #ifdef HAVE_cc0
2212 && GET_CODE (i2dest) == REG
2213 #endif
2214 /* We need I2DEST in the proper mode. If it is a hard register
2215 or the only use of a pseudo, we can change its mode. */
2216 && (GET_MODE (*split) == GET_MODE (i2dest)
2217 || GET_MODE (*split) == VOIDmode
2218 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2219 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2220 && ! REG_USERVAR_P (i2dest)))
2221 && (next_real_insn (i2) == i3
2222 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2223 /* We can't overwrite I2DEST if its value is still used by
2224 NEWPAT. */
2225 && ! reg_referenced_p (i2dest, newpat))
2227 rtx newdest = i2dest;
2228 enum rtx_code split_code = GET_CODE (*split);
2229 enum machine_mode split_mode = GET_MODE (*split);
2231 /* Get NEWDEST as a register in the proper mode. We have already
2232 validated that we can do this. */
2233 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2235 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2237 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2238 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2241 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2242 an ASHIFT. This can occur if it was inside a PLUS and hence
2243 appeared to be a memory address. This is a kludge. */
2244 if (split_code == MULT
2245 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2246 && INTVAL (XEXP (*split, 1)) > 0
2247 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2249 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2250 XEXP (*split, 0), GEN_INT (i)));
2251 /* Update split_code because we may not have a multiply
2252 anymore. */
2253 split_code = GET_CODE (*split);
2256 #ifdef INSN_SCHEDULING
2257 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2258 be written as a ZERO_EXTEND. */
2259 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2261 #ifdef LOAD_EXTEND_OP
2262 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2263 what it really is. */
2264 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2265 == SIGN_EXTEND)
2266 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2267 SUBREG_REG (*split)));
2268 else
2269 #endif
2270 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2271 SUBREG_REG (*split)));
2273 #endif
2275 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2276 SUBST (*split, newdest);
2277 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2279 /* If the split point was a MULT and we didn't have one before,
2280 don't use one now. */
2281 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2282 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2286 /* Check for a case where we loaded from memory in a narrow mode and
2287 then sign extended it, but we need both registers. In that case,
2288 we have a PARALLEL with both loads from the same memory location.
2289 We can split this into a load from memory followed by a register-register
2290 copy. This saves at least one insn, more if register allocation can
2291 eliminate the copy.
2293 We cannot do this if the destination of the first assignment is a
2294 condition code register or cc0. We eliminate this case by making sure
2295 the SET_DEST and SET_SRC have the same mode.
2297 We cannot do this if the destination of the second assignment is
2298 a register that we have already assumed is zero-extended. Similarly
2299 for a SUBREG of such a register. */
2301 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2302 && GET_CODE (newpat) == PARALLEL
2303 && XVECLEN (newpat, 0) == 2
2304 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2305 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2306 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2307 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2308 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2309 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2310 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2311 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2312 INSN_CUID (i2))
2313 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2314 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2315 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2316 (GET_CODE (temp) == REG
2317 && reg_nonzero_bits[REGNO (temp)] != 0
2318 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2319 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2320 && (reg_nonzero_bits[REGNO (temp)]
2321 != GET_MODE_MASK (word_mode))))
2322 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2323 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2324 (GET_CODE (temp) == REG
2325 && reg_nonzero_bits[REGNO (temp)] != 0
2326 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2327 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2328 && (reg_nonzero_bits[REGNO (temp)]
2329 != GET_MODE_MASK (word_mode)))))
2330 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2331 SET_SRC (XVECEXP (newpat, 0, 1)))
2332 && ! find_reg_note (i3, REG_UNUSED,
2333 SET_DEST (XVECEXP (newpat, 0, 0))))
2335 rtx ni2dest;
2337 newi2pat = XVECEXP (newpat, 0, 0);
2338 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2339 newpat = XVECEXP (newpat, 0, 1);
2340 SUBST (SET_SRC (newpat),
2341 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2342 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2344 if (i2_code_number >= 0)
2345 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2347 if (insn_code_number >= 0)
2349 rtx insn;
2350 rtx link;
2352 /* If we will be able to accept this, we have made a change to the
2353 destination of I3. This can invalidate a LOG_LINKS pointing
2354 to I3. No other part of combine.c makes such a transformation.
2356 The new I3 will have a destination that was previously the
2357 destination of I1 or I2 and which was used in i2 or I3. Call
2358 distribute_links to make a LOG_LINK from the next use of
2359 that destination. */
2361 PATTERN (i3) = newpat;
2362 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2364 /* I3 now uses what used to be its destination and which is
2365 now I2's destination. That means we need a LOG_LINK from
2366 I3 to I2. But we used to have one, so we still will.
2368 However, some later insn might be using I2's dest and have
2369 a LOG_LINK pointing at I3. We must remove this link.
2370 The simplest way to remove the link is to point it at I1,
2371 which we know will be a NOTE. */
2373 for (insn = NEXT_INSN (i3);
2374 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2375 || insn != this_basic_block->next_bb->head);
2376 insn = NEXT_INSN (insn))
2378 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2380 for (link = LOG_LINKS (insn); link;
2381 link = XEXP (link, 1))
2382 if (XEXP (link, 0) == i3)
2383 XEXP (link, 0) = i1;
2385 break;
2391 /* Similarly, check for a case where we have a PARALLEL of two independent
2392 SETs but we started with three insns. In this case, we can do the sets
2393 as two separate insns. This case occurs when some SET allows two
2394 other insns to combine, but the destination of that SET is still live. */
2396 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2397 && GET_CODE (newpat) == PARALLEL
2398 && XVECLEN (newpat, 0) == 2
2399 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2400 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2401 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2402 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2403 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2404 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2405 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2406 INSN_CUID (i2))
2407 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2408 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2409 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2410 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2411 XVECEXP (newpat, 0, 0))
2412 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2413 XVECEXP (newpat, 0, 1))
2414 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2415 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2417 /* Normally, it doesn't matter which of the two is done first,
2418 but it does if one references cc0. In that case, it has to
2419 be first. */
2420 #ifdef HAVE_cc0
2421 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2423 newi2pat = XVECEXP (newpat, 0, 0);
2424 newpat = XVECEXP (newpat, 0, 1);
2426 else
2427 #endif
2429 newi2pat = XVECEXP (newpat, 0, 1);
2430 newpat = XVECEXP (newpat, 0, 0);
2433 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2435 if (i2_code_number >= 0)
2436 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2439 /* If it still isn't recognized, fail and change things back the way they
2440 were. */
2441 if ((insn_code_number < 0
2442 /* Is the result a reasonable ASM_OPERANDS? */
2443 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2445 undo_all ();
2446 return 0;
2449 /* If we had to change another insn, make sure it is valid also. */
2450 if (undobuf.other_insn)
2452 rtx other_pat = PATTERN (undobuf.other_insn);
2453 rtx new_other_notes;
2454 rtx note, next;
2456 CLEAR_HARD_REG_SET (newpat_used_regs);
2458 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2459 &new_other_notes);
2461 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2463 undo_all ();
2464 return 0;
2467 PATTERN (undobuf.other_insn) = other_pat;
2469 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2470 are still valid. Then add any non-duplicate notes added by
2471 recog_for_combine. */
2472 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2474 next = XEXP (note, 1);
2476 if (REG_NOTE_KIND (note) == REG_UNUSED
2477 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2479 if (GET_CODE (XEXP (note, 0)) == REG)
2480 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2482 remove_note (undobuf.other_insn, note);
2486 for (note = new_other_notes; note; note = XEXP (note, 1))
2487 if (GET_CODE (XEXP (note, 0)) == REG)
2488 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2490 distribute_notes (new_other_notes, undobuf.other_insn,
2491 undobuf.other_insn, NULL_RTX);
2493 #ifdef HAVE_cc0
2494 /* If I2 is the setter CC0 and I3 is the user CC0 then check whether
2495 they are adjacent to each other or not. */
2497 rtx p = prev_nonnote_insn (i3);
2498 if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2499 && sets_cc0_p (newi2pat))
2501 undo_all ();
2502 return 0;
2505 #endif
2507 /* We now know that we can do this combination. Merge the insns and
2508 update the status of registers and LOG_LINKS. */
2511 rtx i3notes, i2notes, i1notes = 0;
2512 rtx i3links, i2links, i1links = 0;
2513 rtx midnotes = 0;
2514 unsigned int regno;
2516 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2517 clear them. */
2518 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2519 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2520 if (i1)
2521 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2523 /* Ensure that we do not have something that should not be shared but
2524 occurs multiple times in the new insns. Check this by first
2525 resetting all the `used' flags and then copying anything is shared. */
2527 reset_used_flags (i3notes);
2528 reset_used_flags (i2notes);
2529 reset_used_flags (i1notes);
2530 reset_used_flags (newpat);
2531 reset_used_flags (newi2pat);
2532 if (undobuf.other_insn)
2533 reset_used_flags (PATTERN (undobuf.other_insn));
2535 i3notes = copy_rtx_if_shared (i3notes);
2536 i2notes = copy_rtx_if_shared (i2notes);
2537 i1notes = copy_rtx_if_shared (i1notes);
2538 newpat = copy_rtx_if_shared (newpat);
2539 newi2pat = copy_rtx_if_shared (newi2pat);
2540 if (undobuf.other_insn)
2541 reset_used_flags (PATTERN (undobuf.other_insn));
2543 INSN_CODE (i3) = insn_code_number;
2544 PATTERN (i3) = newpat;
2546 if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2548 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2550 reset_used_flags (call_usage);
2551 call_usage = copy_rtx (call_usage);
2553 if (substed_i2)
2554 replace_rtx (call_usage, i2dest, i2src);
2556 if (substed_i1)
2557 replace_rtx (call_usage, i1dest, i1src);
2559 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2562 if (undobuf.other_insn)
2563 INSN_CODE (undobuf.other_insn) = other_code_number;
2565 /* We had one special case above where I2 had more than one set and
2566 we replaced a destination of one of those sets with the destination
2567 of I3. In that case, we have to update LOG_LINKS of insns later
2568 in this basic block. Note that this (expensive) case is rare.
2570 Also, in this case, we must pretend that all REG_NOTEs for I2
2571 actually came from I3, so that REG_UNUSED notes from I2 will be
2572 properly handled. */
2574 if (i3_subst_into_i2)
2576 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2577 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2578 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2579 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2580 && ! find_reg_note (i2, REG_UNUSED,
2581 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2582 for (temp = NEXT_INSN (i2);
2583 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2584 || this_basic_block->head != temp);
2585 temp = NEXT_INSN (temp))
2586 if (temp != i3 && INSN_P (temp))
2587 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2588 if (XEXP (link, 0) == i2)
2589 XEXP (link, 0) = i3;
2591 if (i3notes)
2593 rtx link = i3notes;
2594 while (XEXP (link, 1))
2595 link = XEXP (link, 1);
2596 XEXP (link, 1) = i2notes;
2598 else
2599 i3notes = i2notes;
2600 i2notes = 0;
2603 LOG_LINKS (i3) = 0;
2604 REG_NOTES (i3) = 0;
2605 LOG_LINKS (i2) = 0;
2606 REG_NOTES (i2) = 0;
2608 if (newi2pat)
2610 INSN_CODE (i2) = i2_code_number;
2611 PATTERN (i2) = newi2pat;
2613 else
2615 PUT_CODE (i2, NOTE);
2616 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2617 NOTE_SOURCE_FILE (i2) = 0;
2620 if (i1)
2622 LOG_LINKS (i1) = 0;
2623 REG_NOTES (i1) = 0;
2624 PUT_CODE (i1, NOTE);
2625 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2626 NOTE_SOURCE_FILE (i1) = 0;
2629 /* Get death notes for everything that is now used in either I3 or
2630 I2 and used to die in a previous insn. If we built two new
2631 patterns, move from I1 to I2 then I2 to I3 so that we get the
2632 proper movement on registers that I2 modifies. */
2634 if (newi2pat)
2636 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2637 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2639 else
2640 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2641 i3, &midnotes);
2643 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2644 if (i3notes)
2645 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2646 if (i2notes)
2647 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2648 if (i1notes)
2649 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2650 if (midnotes)
2651 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2653 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2654 know these are REG_UNUSED and want them to go to the desired insn,
2655 so we always pass it as i3. We have not counted the notes in
2656 reg_n_deaths yet, so we need to do so now. */
2658 if (newi2pat && new_i2_notes)
2660 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2661 if (GET_CODE (XEXP (temp, 0)) == REG)
2662 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2664 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2667 if (new_i3_notes)
2669 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2670 if (GET_CODE (XEXP (temp, 0)) == REG)
2671 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2673 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2676 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2677 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2678 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2679 in that case, it might delete I2. Similarly for I2 and I1.
2680 Show an additional death due to the REG_DEAD note we make here. If
2681 we discard it in distribute_notes, we will decrement it again. */
2683 if (i3dest_killed)
2685 if (GET_CODE (i3dest_killed) == REG)
2686 REG_N_DEATHS (REGNO (i3dest_killed))++;
2688 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2689 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2690 NULL_RTX),
2691 NULL_RTX, i2, NULL_RTX);
2692 else
2693 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2694 NULL_RTX),
2695 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2698 if (i2dest_in_i2src)
2700 if (GET_CODE (i2dest) == REG)
2701 REG_N_DEATHS (REGNO (i2dest))++;
2703 if (newi2pat && reg_set_p (i2dest, newi2pat))
2704 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2705 NULL_RTX, i2, NULL_RTX);
2706 else
2707 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2708 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2711 if (i1dest_in_i1src)
2713 if (GET_CODE (i1dest) == REG)
2714 REG_N_DEATHS (REGNO (i1dest))++;
2716 if (newi2pat && reg_set_p (i1dest, newi2pat))
2717 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2718 NULL_RTX, i2, NULL_RTX);
2719 else
2720 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2721 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2724 distribute_links (i3links);
2725 distribute_links (i2links);
2726 distribute_links (i1links);
2728 if (GET_CODE (i2dest) == REG)
2730 rtx link;
2731 rtx i2_insn = 0, i2_val = 0, set;
2733 /* The insn that used to set this register doesn't exist, and
2734 this life of the register may not exist either. See if one of
2735 I3's links points to an insn that sets I2DEST. If it does,
2736 that is now the last known value for I2DEST. If we don't update
2737 this and I2 set the register to a value that depended on its old
2738 contents, we will get confused. If this insn is used, thing
2739 will be set correctly in combine_instructions. */
2741 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2742 if ((set = single_set (XEXP (link, 0))) != 0
2743 && rtx_equal_p (i2dest, SET_DEST (set)))
2744 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2746 record_value_for_reg (i2dest, i2_insn, i2_val);
2748 /* If the reg formerly set in I2 died only once and that was in I3,
2749 zero its use count so it won't make `reload' do any work. */
2750 if (! added_sets_2
2751 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2752 && ! i2dest_in_i2src)
2754 regno = REGNO (i2dest);
2755 REG_N_SETS (regno)--;
2759 if (i1 && GET_CODE (i1dest) == REG)
2761 rtx link;
2762 rtx i1_insn = 0, i1_val = 0, set;
2764 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2765 if ((set = single_set (XEXP (link, 0))) != 0
2766 && rtx_equal_p (i1dest, SET_DEST (set)))
2767 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2769 record_value_for_reg (i1dest, i1_insn, i1_val);
2771 regno = REGNO (i1dest);
2772 if (! added_sets_1 && ! i1dest_in_i1src)
2773 REG_N_SETS (regno)--;
2776 /* Update reg_nonzero_bits et al for any changes that may have been made
2777 to this insn. The order of set_nonzero_bits_and_sign_copies() is
2778 important. Because newi2pat can affect nonzero_bits of newpat */
2779 if (newi2pat)
2780 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2781 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2783 /* Set new_direct_jump_p if a new return or simple jump instruction
2784 has been created.
2786 If I3 is now an unconditional jump, ensure that it has a
2787 BARRIER following it since it may have initially been a
2788 conditional jump. It may also be the last nonnote insn. */
2790 if (returnjump_p (i3) || any_uncondjump_p (i3))
2792 *new_direct_jump_p = 1;
2794 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2795 || GET_CODE (temp) != BARRIER)
2796 emit_barrier_after (i3);
2799 if (undobuf.other_insn != NULL_RTX
2800 && (returnjump_p (undobuf.other_insn)
2801 || any_uncondjump_p (undobuf.other_insn)))
2803 *new_direct_jump_p = 1;
2805 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2806 || GET_CODE (temp) != BARRIER)
2807 emit_barrier_after (undobuf.other_insn);
2810 /* An NOOP jump does not need barrier, but it does need cleaning up
2811 of CFG. */
2812 if (GET_CODE (newpat) == SET
2813 && SET_SRC (newpat) == pc_rtx
2814 && SET_DEST (newpat) == pc_rtx)
2815 *new_direct_jump_p = 1;
2818 combine_successes++;
2819 undo_commit ();
2821 if (added_links_insn
2822 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2823 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2824 return added_links_insn;
2825 else
2826 return newi2pat ? i2 : i3;
2829 /* Undo all the modifications recorded in undobuf. */
2831 static void
2832 undo_all ()
2834 struct undo *undo, *next;
2836 for (undo = undobuf.undos; undo; undo = next)
2838 next = undo->next;
2839 if (undo->is_int)
2840 *undo->where.i = undo->old_contents.i;
2841 else
2842 *undo->where.r = undo->old_contents.r;
2844 undo->next = undobuf.frees;
2845 undobuf.frees = undo;
2848 undobuf.undos = 0;
2851 /* We've committed to accepting the changes we made. Move all
2852 of the undos to the free list. */
2854 static void
2855 undo_commit ()
2857 struct undo *undo, *next;
2859 for (undo = undobuf.undos; undo; undo = next)
2861 next = undo->next;
2862 undo->next = undobuf.frees;
2863 undobuf.frees = undo;
2865 undobuf.undos = 0;
2869 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2870 where we have an arithmetic expression and return that point. LOC will
2871 be inside INSN.
2873 try_combine will call this function to see if an insn can be split into
2874 two insns. */
2876 static rtx *
2877 find_split_point (loc, insn)
2878 rtx *loc;
2879 rtx insn;
2881 rtx x = *loc;
2882 enum rtx_code code = GET_CODE (x);
2883 rtx *split;
2884 unsigned HOST_WIDE_INT len = 0;
2885 HOST_WIDE_INT pos = 0;
2886 int unsignedp = 0;
2887 rtx inner = NULL_RTX;
2889 /* First special-case some codes. */
2890 switch (code)
2892 case SUBREG:
2893 #ifdef INSN_SCHEDULING
2894 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2895 point. */
2896 if (GET_CODE (SUBREG_REG (x)) == MEM)
2897 return loc;
2898 #endif
2899 return find_split_point (&SUBREG_REG (x), insn);
2901 case MEM:
2902 #ifdef HAVE_lo_sum
2903 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2904 using LO_SUM and HIGH. */
2905 if (GET_CODE (XEXP (x, 0)) == CONST
2906 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2908 SUBST (XEXP (x, 0),
2909 gen_rtx_LO_SUM (Pmode,
2910 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2911 XEXP (x, 0)));
2912 return &XEXP (XEXP (x, 0), 0);
2914 #endif
2916 /* If we have a PLUS whose second operand is a constant and the
2917 address is not valid, perhaps will can split it up using
2918 the machine-specific way to split large constants. We use
2919 the first pseudo-reg (one of the virtual regs) as a placeholder;
2920 it will not remain in the result. */
2921 if (GET_CODE (XEXP (x, 0)) == PLUS
2922 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2923 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2925 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2926 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2927 subst_insn);
2929 /* This should have produced two insns, each of which sets our
2930 placeholder. If the source of the second is a valid address,
2931 we can make put both sources together and make a split point
2932 in the middle. */
2934 if (seq
2935 && NEXT_INSN (seq) != NULL_RTX
2936 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2937 && GET_CODE (seq) == INSN
2938 && GET_CODE (PATTERN (seq)) == SET
2939 && SET_DEST (PATTERN (seq)) == reg
2940 && ! reg_mentioned_p (reg,
2941 SET_SRC (PATTERN (seq)))
2942 && GET_CODE (NEXT_INSN (seq)) == INSN
2943 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2944 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2945 && memory_address_p (GET_MODE (x),
2946 SET_SRC (PATTERN (NEXT_INSN (seq)))))
2948 rtx src1 = SET_SRC (PATTERN (seq));
2949 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2951 /* Replace the placeholder in SRC2 with SRC1. If we can
2952 find where in SRC2 it was placed, that can become our
2953 split point and we can replace this address with SRC2.
2954 Just try two obvious places. */
2956 src2 = replace_rtx (src2, reg, src1);
2957 split = 0;
2958 if (XEXP (src2, 0) == src1)
2959 split = &XEXP (src2, 0);
2960 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2961 && XEXP (XEXP (src2, 0), 0) == src1)
2962 split = &XEXP (XEXP (src2, 0), 0);
2964 if (split)
2966 SUBST (XEXP (x, 0), src2);
2967 return split;
2971 /* If that didn't work, perhaps the first operand is complex and
2972 needs to be computed separately, so make a split point there.
2973 This will occur on machines that just support REG + CONST
2974 and have a constant moved through some previous computation. */
2976 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2977 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2978 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2979 == 'o')))
2980 return &XEXP (XEXP (x, 0), 0);
2982 break;
2984 case SET:
2985 #ifdef HAVE_cc0
2986 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2987 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2988 we need to put the operand into a register. So split at that
2989 point. */
2991 if (SET_DEST (x) == cc0_rtx
2992 && GET_CODE (SET_SRC (x)) != COMPARE
2993 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2994 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2995 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2996 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2997 return &SET_SRC (x);
2998 #endif
3000 /* See if we can split SET_SRC as it stands. */
3001 split = find_split_point (&SET_SRC (x), insn);
3002 if (split && split != &SET_SRC (x))
3003 return split;
3005 /* See if we can split SET_DEST as it stands. */
3006 split = find_split_point (&SET_DEST (x), insn);
3007 if (split && split != &SET_DEST (x))
3008 return split;
3010 /* See if this is a bitfield assignment with everything constant. If
3011 so, this is an IOR of an AND, so split it into that. */
3012 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3013 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3014 <= HOST_BITS_PER_WIDE_INT)
3015 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3016 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3017 && GET_CODE (SET_SRC (x)) == CONST_INT
3018 && ((INTVAL (XEXP (SET_DEST (x), 1))
3019 + INTVAL (XEXP (SET_DEST (x), 2)))
3020 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3021 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3023 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3024 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3025 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3026 rtx dest = XEXP (SET_DEST (x), 0);
3027 enum machine_mode mode = GET_MODE (dest);
3028 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3030 if (BITS_BIG_ENDIAN)
3031 pos = GET_MODE_BITSIZE (mode) - len - pos;
3033 if (src == mask)
3034 SUBST (SET_SRC (x),
3035 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3036 else
3037 SUBST (SET_SRC (x),
3038 gen_binary (IOR, mode,
3039 gen_binary (AND, mode, dest,
3040 gen_int_mode (~(mask << pos),
3041 mode)),
3042 GEN_INT (src << pos)));
3044 SUBST (SET_DEST (x), dest);
3046 split = find_split_point (&SET_SRC (x), insn);
3047 if (split && split != &SET_SRC (x))
3048 return split;
3051 /* Otherwise, see if this is an operation that we can split into two.
3052 If so, try to split that. */
3053 code = GET_CODE (SET_SRC (x));
3055 switch (code)
3057 case AND:
3058 /* If we are AND'ing with a large constant that is only a single
3059 bit and the result is only being used in a context where we
3060 need to know if it is zero or nonzero, replace it with a bit
3061 extraction. This will avoid the large constant, which might
3062 have taken more than one insn to make. If the constant were
3063 not a valid argument to the AND but took only one insn to make,
3064 this is no worse, but if it took more than one insn, it will
3065 be better. */
3067 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3068 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3069 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3070 && GET_CODE (SET_DEST (x)) == REG
3071 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3072 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3073 && XEXP (*split, 0) == SET_DEST (x)
3074 && XEXP (*split, 1) == const0_rtx)
3076 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3077 XEXP (SET_SRC (x), 0),
3078 pos, NULL_RTX, 1, 1, 0, 0);
3079 if (extraction != 0)
3081 SUBST (SET_SRC (x), extraction);
3082 return find_split_point (loc, insn);
3085 break;
3087 case NE:
3088 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3089 is known to be on, this can be converted into a NEG of a shift. */
3090 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3091 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3092 && 1 <= (pos = exact_log2
3093 (nonzero_bits (XEXP (SET_SRC (x), 0),
3094 GET_MODE (XEXP (SET_SRC (x), 0))))))
3096 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3098 SUBST (SET_SRC (x),
3099 gen_rtx_NEG (mode,
3100 gen_rtx_LSHIFTRT (mode,
3101 XEXP (SET_SRC (x), 0),
3102 GEN_INT (pos))));
3104 split = find_split_point (&SET_SRC (x), insn);
3105 if (split && split != &SET_SRC (x))
3106 return split;
3108 break;
3110 case SIGN_EXTEND:
3111 inner = XEXP (SET_SRC (x), 0);
3113 /* We can't optimize if either mode is a partial integer
3114 mode as we don't know how many bits are significant
3115 in those modes. */
3116 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3117 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3118 break;
3120 pos = 0;
3121 len = GET_MODE_BITSIZE (GET_MODE (inner));
3122 unsignedp = 0;
3123 break;
3125 case SIGN_EXTRACT:
3126 case ZERO_EXTRACT:
3127 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3128 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3130 inner = XEXP (SET_SRC (x), 0);
3131 len = INTVAL (XEXP (SET_SRC (x), 1));
3132 pos = INTVAL (XEXP (SET_SRC (x), 2));
3134 if (BITS_BIG_ENDIAN)
3135 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3136 unsignedp = (code == ZERO_EXTRACT);
3138 break;
3140 default:
3141 break;
3144 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3146 enum machine_mode mode = GET_MODE (SET_SRC (x));
3148 /* For unsigned, we have a choice of a shift followed by an
3149 AND or two shifts. Use two shifts for field sizes where the
3150 constant might be too large. We assume here that we can
3151 always at least get 8-bit constants in an AND insn, which is
3152 true for every current RISC. */
3154 if (unsignedp && len <= 8)
3156 SUBST (SET_SRC (x),
3157 gen_rtx_AND (mode,
3158 gen_rtx_LSHIFTRT
3159 (mode, gen_lowpart_for_combine (mode, inner),
3160 GEN_INT (pos)),
3161 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3163 split = find_split_point (&SET_SRC (x), insn);
3164 if (split && split != &SET_SRC (x))
3165 return split;
3167 else
3169 SUBST (SET_SRC (x),
3170 gen_rtx_fmt_ee
3171 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3172 gen_rtx_ASHIFT (mode,
3173 gen_lowpart_for_combine (mode, inner),
3174 GEN_INT (GET_MODE_BITSIZE (mode)
3175 - len - pos)),
3176 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3178 split = find_split_point (&SET_SRC (x), insn);
3179 if (split && split != &SET_SRC (x))
3180 return split;
3184 /* See if this is a simple operation with a constant as the second
3185 operand. It might be that this constant is out of range and hence
3186 could be used as a split point. */
3187 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3188 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3189 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3190 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3191 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3192 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3193 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3194 == 'o'))))
3195 return &XEXP (SET_SRC (x), 1);
3197 /* Finally, see if this is a simple operation with its first operand
3198 not in a register. The operation might require this operand in a
3199 register, so return it as a split point. We can always do this
3200 because if the first operand were another operation, we would have
3201 already found it as a split point. */
3202 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3203 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3204 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3205 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3206 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3207 return &XEXP (SET_SRC (x), 0);
3209 return 0;
3211 case AND:
3212 case IOR:
3213 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3214 it is better to write this as (not (ior A B)) so we can split it.
3215 Similarly for IOR. */
3216 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3218 SUBST (*loc,
3219 gen_rtx_NOT (GET_MODE (x),
3220 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3221 GET_MODE (x),
3222 XEXP (XEXP (x, 0), 0),
3223 XEXP (XEXP (x, 1), 0))));
3224 return find_split_point (loc, insn);
3227 /* Many RISC machines have a large set of logical insns. If the
3228 second operand is a NOT, put it first so we will try to split the
3229 other operand first. */
3230 if (GET_CODE (XEXP (x, 1)) == NOT)
3232 rtx tem = XEXP (x, 0);
3233 SUBST (XEXP (x, 0), XEXP (x, 1));
3234 SUBST (XEXP (x, 1), tem);
3236 break;
3238 default:
3239 break;
3242 /* Otherwise, select our actions depending on our rtx class. */
3243 switch (GET_RTX_CLASS (code))
3245 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3246 case '3':
3247 split = find_split_point (&XEXP (x, 2), insn);
3248 if (split)
3249 return split;
3250 /* ... fall through ... */
3251 case '2':
3252 case 'c':
3253 case '<':
3254 split = find_split_point (&XEXP (x, 1), insn);
3255 if (split)
3256 return split;
3257 /* ... fall through ... */
3258 case '1':
3259 /* Some machines have (and (shift ...) ...) insns. If X is not
3260 an AND, but XEXP (X, 0) is, use it as our split point. */
3261 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3262 return &XEXP (x, 0);
3264 split = find_split_point (&XEXP (x, 0), insn);
3265 if (split)
3266 return split;
3267 return loc;
3270 /* Otherwise, we don't have a split point. */
3271 return 0;
3274 /* Throughout X, replace FROM with TO, and return the result.
3275 The result is TO if X is FROM;
3276 otherwise the result is X, but its contents may have been modified.
3277 If they were modified, a record was made in undobuf so that
3278 undo_all will (among other things) return X to its original state.
3280 If the number of changes necessary is too much to record to undo,
3281 the excess changes are not made, so the result is invalid.
3282 The changes already made can still be undone.
3283 undobuf.num_undo is incremented for such changes, so by testing that
3284 the caller can tell whether the result is valid.
3286 `n_occurrences' is incremented each time FROM is replaced.
3288 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3290 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3291 by copying if `n_occurrences' is nonzero. */
3293 static rtx
3294 subst (x, from, to, in_dest, unique_copy)
3295 rtx x, from, to;
3296 int in_dest;
3297 int unique_copy;
3299 enum rtx_code code = GET_CODE (x);
3300 enum machine_mode op0_mode = VOIDmode;
3301 const char *fmt;
3302 int len, i;
3303 rtx new;
3305 /* Two expressions are equal if they are identical copies of a shared
3306 RTX or if they are both registers with the same register number
3307 and mode. */
3309 #define COMBINE_RTX_EQUAL_P(X,Y) \
3310 ((X) == (Y) \
3311 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3312 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3314 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3316 n_occurrences++;
3317 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3320 /* If X and FROM are the same register but different modes, they will
3321 not have been seen as equal above. However, flow.c will make a
3322 LOG_LINKS entry for that case. If we do nothing, we will try to
3323 rerecognize our original insn and, when it succeeds, we will
3324 delete the feeding insn, which is incorrect.
3326 So force this insn not to match in this (rare) case. */
3327 if (! in_dest && code == REG && GET_CODE (from) == REG
3328 && REGNO (x) == REGNO (from))
3329 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3331 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3332 of which may contain things that can be combined. */
3333 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3334 return x;
3336 /* It is possible to have a subexpression appear twice in the insn.
3337 Suppose that FROM is a register that appears within TO.
3338 Then, after that subexpression has been scanned once by `subst',
3339 the second time it is scanned, TO may be found. If we were
3340 to scan TO here, we would find FROM within it and create a
3341 self-referent rtl structure which is completely wrong. */
3342 if (COMBINE_RTX_EQUAL_P (x, to))
3343 return to;
3345 /* Parallel asm_operands need special attention because all of the
3346 inputs are shared across the arms. Furthermore, unsharing the
3347 rtl results in recognition failures. Failure to handle this case
3348 specially can result in circular rtl.
3350 Solve this by doing a normal pass across the first entry of the
3351 parallel, and only processing the SET_DESTs of the subsequent
3352 entries. Ug. */
3354 if (code == PARALLEL
3355 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3356 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3358 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3360 /* If this substitution failed, this whole thing fails. */
3361 if (GET_CODE (new) == CLOBBER
3362 && XEXP (new, 0) == const0_rtx)
3363 return new;
3365 SUBST (XVECEXP (x, 0, 0), new);
3367 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3369 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3371 if (GET_CODE (dest) != REG
3372 && GET_CODE (dest) != CC0
3373 && GET_CODE (dest) != PC)
3375 new = subst (dest, from, to, 0, unique_copy);
3377 /* If this substitution failed, this whole thing fails. */
3378 if (GET_CODE (new) == CLOBBER
3379 && XEXP (new, 0) == const0_rtx)
3380 return new;
3382 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3386 else
3388 len = GET_RTX_LENGTH (code);
3389 fmt = GET_RTX_FORMAT (code);
3391 /* We don't need to process a SET_DEST that is a register, CC0,
3392 or PC, so set up to skip this common case. All other cases
3393 where we want to suppress replacing something inside a
3394 SET_SRC are handled via the IN_DEST operand. */
3395 if (code == SET
3396 && (GET_CODE (SET_DEST (x)) == REG
3397 || GET_CODE (SET_DEST (x)) == CC0
3398 || GET_CODE (SET_DEST (x)) == PC))
3399 fmt = "ie";
3401 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3402 constant. */
3403 if (fmt[0] == 'e')
3404 op0_mode = GET_MODE (XEXP (x, 0));
3406 for (i = 0; i < len; i++)
3408 if (fmt[i] == 'E')
3410 int j;
3411 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3413 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3415 new = (unique_copy && n_occurrences
3416 ? copy_rtx (to) : to);
3417 n_occurrences++;
3419 else
3421 new = subst (XVECEXP (x, i, j), from, to, 0,
3422 unique_copy);
3424 /* If this substitution failed, this whole thing
3425 fails. */
3426 if (GET_CODE (new) == CLOBBER
3427 && XEXP (new, 0) == const0_rtx)
3428 return new;
3431 SUBST (XVECEXP (x, i, j), new);
3434 else if (fmt[i] == 'e')
3436 /* If this is a register being set, ignore it. */
3437 new = XEXP (x, i);
3438 if (in_dest
3439 && (code == SUBREG || code == STRICT_LOW_PART
3440 || code == ZERO_EXTRACT)
3441 && i == 0
3442 && GET_CODE (new) == REG)
3445 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3447 /* In general, don't install a subreg involving two
3448 modes not tieable. It can worsen register
3449 allocation, and can even make invalid reload
3450 insns, since the reg inside may need to be copied
3451 from in the outside mode, and that may be invalid
3452 if it is an fp reg copied in integer mode.
3454 We allow two exceptions to this: It is valid if
3455 it is inside another SUBREG and the mode of that
3456 SUBREG and the mode of the inside of TO is
3457 tieable and it is valid if X is a SET that copies
3458 FROM to CC0. */
3460 if (GET_CODE (to) == SUBREG
3461 && ! MODES_TIEABLE_P (GET_MODE (to),
3462 GET_MODE (SUBREG_REG (to)))
3463 && ! (code == SUBREG
3464 && MODES_TIEABLE_P (GET_MODE (x),
3465 GET_MODE (SUBREG_REG (to))))
3466 #ifdef HAVE_cc0
3467 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3468 #endif
3470 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3472 #ifdef CANNOT_CHANGE_MODE_CLASS
3473 if (code == SUBREG
3474 && GET_CODE (to) == REG
3475 && REGNO (to) < FIRST_PSEUDO_REGISTER
3476 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3477 GET_MODE (to),
3478 GET_MODE (x)))
3479 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3480 #endif
3482 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3483 n_occurrences++;
3485 else
3486 /* If we are in a SET_DEST, suppress most cases unless we
3487 have gone inside a MEM, in which case we want to
3488 simplify the address. We assume here that things that
3489 are actually part of the destination have their inner
3490 parts in the first expression. This is true for SUBREG,
3491 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3492 things aside from REG and MEM that should appear in a
3493 SET_DEST. */
3494 new = subst (XEXP (x, i), from, to,
3495 (((in_dest
3496 && (code == SUBREG || code == STRICT_LOW_PART
3497 || code == ZERO_EXTRACT))
3498 || code == SET)
3499 && i == 0), unique_copy);
3501 /* If we found that we will have to reject this combination,
3502 indicate that by returning the CLOBBER ourselves, rather than
3503 an expression containing it. This will speed things up as
3504 well as prevent accidents where two CLOBBERs are considered
3505 to be equal, thus producing an incorrect simplification. */
3507 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3508 return new;
3510 if (GET_CODE (new) == CONST_INT && GET_CODE (x) == SUBREG)
3512 enum machine_mode mode = GET_MODE (x);
3514 x = simplify_subreg (GET_MODE (x), new,
3515 GET_MODE (SUBREG_REG (x)),
3516 SUBREG_BYTE (x));
3517 if (! x)
3518 x = gen_rtx_CLOBBER (mode, const0_rtx);
3520 else if (GET_CODE (new) == CONST_INT
3521 && GET_CODE (x) == ZERO_EXTEND)
3523 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3524 new, GET_MODE (XEXP (x, 0)));
3525 if (! x)
3526 abort ();
3528 else
3529 SUBST (XEXP (x, i), new);
3534 /* Try to simplify X. If the simplification changed the code, it is likely
3535 that further simplification will help, so loop, but limit the number
3536 of repetitions that will be performed. */
3538 for (i = 0; i < 4; i++)
3540 /* If X is sufficiently simple, don't bother trying to do anything
3541 with it. */
3542 if (code != CONST_INT && code != REG && code != CLOBBER)
3543 x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3545 if (GET_CODE (x) == code)
3546 break;
3548 code = GET_CODE (x);
3550 /* We no longer know the original mode of operand 0 since we
3551 have changed the form of X) */
3552 op0_mode = VOIDmode;
3555 return x;
3558 /* Simplify X, a piece of RTL. We just operate on the expression at the
3559 outer level; call `subst' to simplify recursively. Return the new
3560 expression.
3562 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3563 will be the iteration even if an expression with a code different from
3564 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3566 static rtx
3567 combine_simplify_rtx (x, op0_mode, last, in_dest)
3568 rtx x;
3569 enum machine_mode op0_mode;
3570 int last;
3571 int in_dest;
3573 enum rtx_code code = GET_CODE (x);
3574 enum machine_mode mode = GET_MODE (x);
3575 rtx temp;
3576 rtx reversed;
3577 int i;
3579 /* If this is a commutative operation, put a constant last and a complex
3580 expression first. We don't need to do this for comparisons here. */
3581 if (GET_RTX_CLASS (code) == 'c'
3582 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3584 temp = XEXP (x, 0);
3585 SUBST (XEXP (x, 0), XEXP (x, 1));
3586 SUBST (XEXP (x, 1), temp);
3589 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3590 sign extension of a PLUS with a constant, reverse the order of the sign
3591 extension and the addition. Note that this not the same as the original
3592 code, but overflow is undefined for signed values. Also note that the
3593 PLUS will have been partially moved "inside" the sign-extension, so that
3594 the first operand of X will really look like:
3595 (ashiftrt (plus (ashift A C4) C5) C4).
3596 We convert this to
3597 (plus (ashiftrt (ashift A C4) C2) C4)
3598 and replace the first operand of X with that expression. Later parts
3599 of this function may simplify the expression further.
3601 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3602 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3603 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3605 We do this to simplify address expressions. */
3607 if ((code == PLUS || code == MINUS || code == MULT)
3608 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3609 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3610 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3611 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3612 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3613 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3614 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3615 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3616 XEXP (XEXP (XEXP (x, 0), 0), 1),
3617 XEXP (XEXP (x, 0), 1))) != 0)
3619 rtx new
3620 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3621 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3622 INTVAL (XEXP (XEXP (x, 0), 1)));
3624 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3625 INTVAL (XEXP (XEXP (x, 0), 1)));
3627 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3630 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3631 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3632 things. Check for cases where both arms are testing the same
3633 condition.
3635 Don't do anything if all operands are very simple. */
3637 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3638 || GET_RTX_CLASS (code) == '<')
3639 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3640 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3641 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3642 == 'o')))
3643 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3644 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3645 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3646 == 'o')))))
3647 || (GET_RTX_CLASS (code) == '1'
3648 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3649 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3650 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3651 == 'o'))))))
3653 rtx cond, true_rtx, false_rtx;
3655 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3656 if (cond != 0
3657 /* If everything is a comparison, what we have is highly unlikely
3658 to be simpler, so don't use it. */
3659 && ! (GET_RTX_CLASS (code) == '<'
3660 && (GET_RTX_CLASS (GET_CODE (true_rtx)) == '<'
3661 || GET_RTX_CLASS (GET_CODE (false_rtx)) == '<')))
3663 rtx cop1 = const0_rtx;
3664 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3666 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3667 return x;
3669 /* Simplify the alternative arms; this may collapse the true and
3670 false arms to store-flag values. */
3671 true_rtx = subst (true_rtx, pc_rtx, pc_rtx, 0, 0);
3672 false_rtx = subst (false_rtx, pc_rtx, pc_rtx, 0, 0);
3674 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3675 is unlikely to be simpler. */
3676 if (general_operand (true_rtx, VOIDmode)
3677 && general_operand (false_rtx, VOIDmode))
3679 enum rtx_code reversed;
3681 /* Restarting if we generate a store-flag expression will cause
3682 us to loop. Just drop through in this case. */
3684 /* If the result values are STORE_FLAG_VALUE and zero, we can
3685 just make the comparison operation. */
3686 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3687 x = gen_binary (cond_code, mode, cond, cop1);
3688 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3689 && ((reversed = reversed_comparison_code_parts
3690 (cond_code, cond, cop1, NULL))
3691 != UNKNOWN))
3692 x = gen_binary (reversed, mode, cond, cop1);
3694 /* Likewise, we can make the negate of a comparison operation
3695 if the result values are - STORE_FLAG_VALUE and zero. */
3696 else if (GET_CODE (true_rtx) == CONST_INT
3697 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3698 && false_rtx == const0_rtx)
3699 x = simplify_gen_unary (NEG, mode,
3700 gen_binary (cond_code, mode, cond,
3701 cop1),
3702 mode);
3703 else if (GET_CODE (false_rtx) == CONST_INT
3704 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3705 && true_rtx == const0_rtx
3706 && ((reversed = reversed_comparison_code_parts
3707 (cond_code, cond, cop1, NULL))
3708 != UNKNOWN))
3709 x = simplify_gen_unary (NEG, mode,
3710 gen_binary (reversed, mode,
3711 cond, cop1),
3712 mode);
3713 else
3714 return gen_rtx_IF_THEN_ELSE (mode,
3715 gen_binary (cond_code, VOIDmode,
3716 cond, cop1),
3717 true_rtx, false_rtx);
3719 code = GET_CODE (x);
3720 op0_mode = VOIDmode;
3725 /* Try to fold this expression in case we have constants that weren't
3726 present before. */
3727 temp = 0;
3728 switch (GET_RTX_CLASS (code))
3730 case '1':
3731 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3732 break;
3733 case '<':
3735 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3736 if (cmp_mode == VOIDmode)
3738 cmp_mode = GET_MODE (XEXP (x, 1));
3739 if (cmp_mode == VOIDmode)
3740 cmp_mode = op0_mode;
3742 temp = simplify_relational_operation (code, cmp_mode,
3743 XEXP (x, 0), XEXP (x, 1));
3745 #ifdef FLOAT_STORE_FLAG_VALUE
3746 if (temp != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3748 if (temp == const0_rtx)
3749 temp = CONST0_RTX (mode);
3750 else
3751 temp = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE (mode),
3752 mode);
3754 #endif
3755 break;
3756 case 'c':
3757 case '2':
3758 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3759 break;
3760 case 'b':
3761 case '3':
3762 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3763 XEXP (x, 1), XEXP (x, 2));
3764 break;
3767 if (temp)
3769 x = temp;
3770 code = GET_CODE (temp);
3771 op0_mode = VOIDmode;
3772 mode = GET_MODE (temp);
3775 /* First see if we can apply the inverse distributive law. */
3776 if (code == PLUS || code == MINUS
3777 || code == AND || code == IOR || code == XOR)
3779 x = apply_distributive_law (x);
3780 code = GET_CODE (x);
3781 op0_mode = VOIDmode;
3784 /* If CODE is an associative operation not otherwise handled, see if we
3785 can associate some operands. This can win if they are constants or
3786 if they are logically related (i.e. (a & b) & a). */
3787 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3788 || code == AND || code == IOR || code == XOR
3789 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3790 && ((INTEGRAL_MODE_P (mode) && code != DIV)
3791 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3793 if (GET_CODE (XEXP (x, 0)) == code)
3795 rtx other = XEXP (XEXP (x, 0), 0);
3796 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3797 rtx inner_op1 = XEXP (x, 1);
3798 rtx inner;
3800 /* Make sure we pass the constant operand if any as the second
3801 one if this is a commutative operation. */
3802 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3804 rtx tem = inner_op0;
3805 inner_op0 = inner_op1;
3806 inner_op1 = tem;
3808 inner = simplify_binary_operation (code == MINUS ? PLUS
3809 : code == DIV ? MULT
3810 : code,
3811 mode, inner_op0, inner_op1);
3813 /* For commutative operations, try the other pair if that one
3814 didn't simplify. */
3815 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3817 other = XEXP (XEXP (x, 0), 1);
3818 inner = simplify_binary_operation (code, mode,
3819 XEXP (XEXP (x, 0), 0),
3820 XEXP (x, 1));
3823 if (inner)
3824 return gen_binary (code, mode, other, inner);
3828 /* A little bit of algebraic simplification here. */
3829 switch (code)
3831 case MEM:
3832 /* Ensure that our address has any ASHIFTs converted to MULT in case
3833 address-recognizing predicates are called later. */
3834 temp = make_compound_operation (XEXP (x, 0), MEM);
3835 SUBST (XEXP (x, 0), temp);
3836 break;
3838 case SUBREG:
3839 if (op0_mode == VOIDmode)
3840 op0_mode = GET_MODE (SUBREG_REG (x));
3842 /* simplify_subreg can't use gen_lowpart_for_combine. */
3843 if (CONSTANT_P (SUBREG_REG (x))
3844 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3845 /* Don't call gen_lowpart_for_combine if the inner mode
3846 is VOIDmode and we cannot simplify it, as SUBREG without
3847 inner mode is invalid. */
3848 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3849 || gen_lowpart_common (mode, SUBREG_REG (x))))
3850 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3852 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3853 break;
3855 rtx temp;
3856 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3857 SUBREG_BYTE (x));
3858 if (temp)
3859 return temp;
3862 /* Don't change the mode of the MEM if that would change the meaning
3863 of the address. */
3864 if (GET_CODE (SUBREG_REG (x)) == MEM
3865 && (MEM_VOLATILE_P (SUBREG_REG (x))
3866 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3867 return gen_rtx_CLOBBER (mode, const0_rtx);
3869 /* Note that we cannot do any narrowing for non-constants since
3870 we might have been counting on using the fact that some bits were
3871 zero. We now do this in the SET. */
3873 break;
3875 case NOT:
3876 /* (not (plus X -1)) can become (neg X). */
3877 if (GET_CODE (XEXP (x, 0)) == PLUS
3878 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3879 return gen_rtx_NEG (mode, XEXP (XEXP (x, 0), 0));
3881 /* Similarly, (not (neg X)) is (plus X -1). */
3882 if (GET_CODE (XEXP (x, 0)) == NEG)
3883 return gen_rtx_PLUS (mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3885 /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
3886 if (GET_CODE (XEXP (x, 0)) == XOR
3887 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3888 && (temp = simplify_unary_operation (NOT, mode,
3889 XEXP (XEXP (x, 0), 1),
3890 mode)) != 0)
3891 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3893 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3894 other than 1, but that is not valid. We could do a similar
3895 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3896 but this doesn't seem common enough to bother with. */
3897 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3898 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3899 return gen_rtx_ROTATE (mode, simplify_gen_unary (NOT, mode,
3900 const1_rtx, mode),
3901 XEXP (XEXP (x, 0), 1));
3903 if (GET_CODE (XEXP (x, 0)) == SUBREG
3904 && subreg_lowpart_p (XEXP (x, 0))
3905 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3906 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3907 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3908 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3910 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3912 x = gen_rtx_ROTATE (inner_mode,
3913 simplify_gen_unary (NOT, inner_mode, const1_rtx,
3914 inner_mode),
3915 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3916 return gen_lowpart_for_combine (mode, x);
3919 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3920 reversing the comparison code if valid. */
3921 if (STORE_FLAG_VALUE == -1
3922 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3923 && (reversed = reversed_comparison (x, mode, XEXP (XEXP (x, 0), 0),
3924 XEXP (XEXP (x, 0), 1))))
3925 return reversed;
3927 /* (not (ashiftrt foo C)) where C is the number of bits in FOO minus 1
3928 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3929 perform the above simplification. */
3931 if (STORE_FLAG_VALUE == -1
3932 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3933 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3934 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3935 return gen_rtx_GE (mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3937 /* Apply De Morgan's laws to reduce number of patterns for machines
3938 with negating logical insns (and-not, nand, etc.). If result has
3939 only one NOT, put it first, since that is how the patterns are
3940 coded. */
3942 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3944 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3945 enum machine_mode op_mode;
3947 op_mode = GET_MODE (in1);
3948 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3950 op_mode = GET_MODE (in2);
3951 if (op_mode == VOIDmode)
3952 op_mode = mode;
3953 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3955 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3957 rtx tem = in2;
3958 in2 = in1; in1 = tem;
3961 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3962 mode, in1, in2);
3964 break;
3966 case NEG:
3967 /* (neg (plus X 1)) can become (not X). */
3968 if (GET_CODE (XEXP (x, 0)) == PLUS
3969 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3970 return gen_rtx_NOT (mode, XEXP (XEXP (x, 0), 0));
3972 /* Similarly, (neg (not X)) is (plus X 1). */
3973 if (GET_CODE (XEXP (x, 0)) == NOT)
3974 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3976 /* (neg (minus X Y)) can become (minus Y X). This transformation
3977 isn't safe for modes with signed zeros, since if X and Y are
3978 both +0, (minus Y X) is the same as (minus X Y). If the rounding
3979 mode is towards +infinity (or -infinity) then the two expressions
3980 will be rounded differently. */
3981 if (GET_CODE (XEXP (x, 0)) == MINUS
3982 && !HONOR_SIGNED_ZEROS (mode)
3983 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3984 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3985 XEXP (XEXP (x, 0), 0));
3987 /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
3988 if (GET_CODE (XEXP (x, 0)) == PLUS
3989 && !HONOR_SIGNED_ZEROS (mode)
3990 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3992 temp = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 0), 0), mode);
3993 temp = combine_simplify_rtx (temp, mode, last, in_dest);
3994 return gen_binary (MINUS, mode, temp, XEXP (XEXP (x, 0), 1));
3997 /* (neg (mult A B)) becomes (mult (neg A) B).
3998 This works even for floating-point values. */
3999 if (GET_CODE (XEXP (x, 0)) == MULT)
4001 temp = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 0), 0), mode);
4002 return gen_binary (MULT, mode, temp, XEXP (XEXP (x, 0), 1));
4005 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
4006 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
4007 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4008 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
4010 /* NEG commutes with ASHIFT since it is multiplication. Only do this
4011 if we can then eliminate the NEG (e.g.,
4012 if the operand is a constant). */
4014 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
4016 temp = simplify_unary_operation (NEG, mode,
4017 XEXP (XEXP (x, 0), 0), mode);
4018 if (temp)
4019 return gen_binary (ASHIFT, mode, temp, XEXP (XEXP (x, 0), 1));
4022 temp = expand_compound_operation (XEXP (x, 0));
4024 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4025 replaced by (lshiftrt X C). This will convert
4026 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4028 if (GET_CODE (temp) == ASHIFTRT
4029 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4030 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4031 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4032 INTVAL (XEXP (temp, 1)));
4034 /* If X has only a single bit that might be nonzero, say, bit I, convert
4035 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4036 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4037 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4038 or a SUBREG of one since we'd be making the expression more
4039 complex if it was just a register. */
4041 if (GET_CODE (temp) != REG
4042 && ! (GET_CODE (temp) == SUBREG
4043 && GET_CODE (SUBREG_REG (temp)) == REG)
4044 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4046 rtx temp1 = simplify_shift_const
4047 (NULL_RTX, ASHIFTRT, mode,
4048 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4049 GET_MODE_BITSIZE (mode) - 1 - i),
4050 GET_MODE_BITSIZE (mode) - 1 - i);
4052 /* If all we did was surround TEMP with the two shifts, we
4053 haven't improved anything, so don't use it. Otherwise,
4054 we are better off with TEMP1. */
4055 if (GET_CODE (temp1) != ASHIFTRT
4056 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4057 || XEXP (XEXP (temp1, 0), 0) != temp)
4058 return temp1;
4060 break;
4062 case TRUNCATE:
4063 /* We can't handle truncation to a partial integer mode here
4064 because we don't know the real bitsize of the partial
4065 integer mode. */
4066 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4067 break;
4069 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4070 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4071 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4072 SUBST (XEXP (x, 0),
4073 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4074 GET_MODE_MASK (mode), NULL_RTX, 0));
4076 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
4077 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4078 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4079 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4080 return XEXP (XEXP (x, 0), 0);
4082 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4083 (OP:SI foo:SI) if OP is NEG or ABS. */
4084 if ((GET_CODE (XEXP (x, 0)) == ABS
4085 || GET_CODE (XEXP (x, 0)) == NEG)
4086 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4087 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4088 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4089 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4090 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4092 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4093 (truncate:SI x). */
4094 if (GET_CODE (XEXP (x, 0)) == SUBREG
4095 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4096 && subreg_lowpart_p (XEXP (x, 0)))
4097 return SUBREG_REG (XEXP (x, 0));
4099 /* If we know that the value is already truncated, we can
4100 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4101 is nonzero for the corresponding modes. But don't do this
4102 for an (LSHIFTRT (MULT ...)) since this will cause problems
4103 with the umulXi3_highpart patterns. */
4104 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4105 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4106 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4107 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4108 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4109 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4110 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4112 /* A truncate of a comparison can be replaced with a subreg if
4113 STORE_FLAG_VALUE permits. This is like the previous test,
4114 but it works even if the comparison is done in a mode larger
4115 than HOST_BITS_PER_WIDE_INT. */
4116 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4117 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4118 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4119 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4121 /* Similarly, a truncate of a register whose value is a
4122 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4123 permits. */
4124 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4125 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4126 && (temp = get_last_value (XEXP (x, 0)))
4127 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
4128 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4130 break;
4132 case FLOAT_TRUNCATE:
4133 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4134 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4135 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4136 return XEXP (XEXP (x, 0), 0);
4138 /* (float_truncate:SF (float_truncate:DF foo:XF))
4139 = (float_truncate:SF foo:XF).
4140 This may elliminate double rounding, so it is unsafe.
4142 (float_truncate:SF (float_extend:XF foo:DF))
4143 = (float_truncate:SF foo:DF).
4145 (float_truncate:DF (float_extend:XF foo:SF))
4146 = (float_extend:SF foo:DF). */
4147 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4148 && flag_unsafe_math_optimizations)
4149 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4150 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4151 0)))
4152 > GET_MODE_SIZE (mode)
4153 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4154 mode,
4155 XEXP (XEXP (x, 0), 0), mode);
4157 /* (float_truncate (float x)) is (float x) */
4158 if (GET_CODE (XEXP (x, 0)) == FLOAT
4159 && (flag_unsafe_math_optimizations
4160 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4161 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4162 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4163 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4164 return simplify_gen_unary (FLOAT, mode,
4165 XEXP (XEXP (x, 0), 0),
4166 GET_MODE (XEXP (XEXP (x, 0), 0)));
4168 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4169 (OP:SF foo:SF) if OP is NEG or ABS. */
4170 if ((GET_CODE (XEXP (x, 0)) == ABS
4171 || GET_CODE (XEXP (x, 0)) == NEG)
4172 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4173 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4174 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4175 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4177 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4178 is (float_truncate:SF x). */
4179 if (GET_CODE (XEXP (x, 0)) == SUBREG
4180 && subreg_lowpart_p (XEXP (x, 0))
4181 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4182 return SUBREG_REG (XEXP (x, 0));
4183 break;
4184 case FLOAT_EXTEND:
4185 /* (float_extend (float_extend x)) is (float_extend x)
4187 (float_extend (float x)) is (float x) assuming that double
4188 rounding can't happen.
4190 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4191 || (GET_CODE (XEXP (x, 0)) == FLOAT
4192 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4193 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4194 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4195 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4196 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4197 XEXP (XEXP (x, 0), 0),
4198 GET_MODE (XEXP (XEXP (x, 0), 0)));
4200 break;
4201 #ifdef HAVE_cc0
4202 case COMPARE:
4203 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4204 using cc0, in which case we want to leave it as a COMPARE
4205 so we can distinguish it from a register-register-copy. */
4206 if (XEXP (x, 1) == const0_rtx)
4207 return XEXP (x, 0);
4209 /* x - 0 is the same as x unless x's mode has signed zeros and
4210 allows rounding towards -infinity. Under those conditions,
4211 0 - 0 is -0. */
4212 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4213 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4214 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4215 return XEXP (x, 0);
4216 break;
4217 #endif
4219 case CONST:
4220 /* (const (const X)) can become (const X). Do it this way rather than
4221 returning the inner CONST since CONST can be shared with a
4222 REG_EQUAL note. */
4223 if (GET_CODE (XEXP (x, 0)) == CONST)
4224 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4225 break;
4227 #ifdef HAVE_lo_sum
4228 case LO_SUM:
4229 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4230 can add in an offset. find_split_point will split this address up
4231 again if it doesn't match. */
4232 if (GET_CODE (XEXP (x, 0)) == HIGH
4233 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4234 return XEXP (x, 1);
4235 break;
4236 #endif
4238 case PLUS:
4239 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4241 if (GET_CODE (XEXP (x, 0)) == MULT
4242 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4244 rtx in1, in2;
4246 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4247 in2 = XEXP (XEXP (x, 0), 1);
4248 return gen_binary (MINUS, mode, XEXP (x, 1),
4249 gen_binary (MULT, mode, in1, in2));
4252 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4253 outermost. That's because that's the way indexed addresses are
4254 supposed to appear. This code used to check many more cases, but
4255 they are now checked elsewhere. */
4256 if (GET_CODE (XEXP (x, 0)) == PLUS
4257 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4258 return gen_binary (PLUS, mode,
4259 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4260 XEXP (x, 1)),
4261 XEXP (XEXP (x, 0), 1));
4263 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4264 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4265 bit-field and can be replaced by either a sign_extend or a
4266 sign_extract. The `and' may be a zero_extend and the two
4267 <c>, -<c> constants may be reversed. */
4268 if (GET_CODE (XEXP (x, 0)) == XOR
4269 && GET_CODE (XEXP (x, 1)) == CONST_INT
4270 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4271 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4272 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4273 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4274 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4275 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4276 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4277 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4278 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4279 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4280 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4281 == (unsigned int) i + 1))))
4282 return simplify_shift_const
4283 (NULL_RTX, ASHIFTRT, mode,
4284 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4285 XEXP (XEXP (XEXP (x, 0), 0), 0),
4286 GET_MODE_BITSIZE (mode) - (i + 1)),
4287 GET_MODE_BITSIZE (mode) - (i + 1));
4289 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4290 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4291 is 1. This produces better code than the alternative immediately
4292 below. */
4293 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4294 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4295 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4296 && (reversed = reversed_comparison (XEXP (x, 0), mode,
4297 XEXP (XEXP (x, 0), 0),
4298 XEXP (XEXP (x, 0), 1))))
4299 return
4300 simplify_gen_unary (NEG, mode, reversed, mode);
4302 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4303 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4304 the bitsize of the mode - 1. This allows simplification of
4305 "a = (b & 8) == 0;" */
4306 if (XEXP (x, 1) == constm1_rtx
4307 && GET_CODE (XEXP (x, 0)) != REG
4308 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4309 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4310 && nonzero_bits (XEXP (x, 0), mode) == 1)
4311 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4312 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4313 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4314 GET_MODE_BITSIZE (mode) - 1),
4315 GET_MODE_BITSIZE (mode) - 1);
4317 /* If we are adding two things that have no bits in common, convert
4318 the addition into an IOR. This will often be further simplified,
4319 for example in cases like ((a & 1) + (a & 2)), which can
4320 become a & 3. */
4322 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4323 && (nonzero_bits (XEXP (x, 0), mode)
4324 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4326 /* Try to simplify the expression further. */
4327 rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4328 temp = combine_simplify_rtx (tor, mode, last, in_dest);
4330 /* If we could, great. If not, do not go ahead with the IOR
4331 replacement, since PLUS appears in many special purpose
4332 address arithmetic instructions. */
4333 if (GET_CODE (temp) != CLOBBER && temp != tor)
4334 return temp;
4336 break;
4338 case MINUS:
4339 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4340 by reversing the comparison code if valid. */
4341 if (STORE_FLAG_VALUE == 1
4342 && XEXP (x, 0) == const1_rtx
4343 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4344 && (reversed = reversed_comparison (XEXP (x, 1), mode,
4345 XEXP (XEXP (x, 1), 0),
4346 XEXP (XEXP (x, 1), 1))))
4347 return reversed;
4349 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4350 (and <foo> (const_int pow2-1)) */
4351 if (GET_CODE (XEXP (x, 1)) == AND
4352 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4353 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4354 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4355 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4356 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4358 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4360 if (GET_CODE (XEXP (x, 1)) == MULT
4361 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4363 rtx in1, in2;
4365 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4366 in2 = XEXP (XEXP (x, 1), 1);
4367 return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4368 XEXP (x, 0));
4371 /* Canonicalize (minus (neg A) (mult B C)) to
4372 (minus (mult (neg B) C) A). */
4373 if (GET_CODE (XEXP (x, 1)) == MULT
4374 && GET_CODE (XEXP (x, 0)) == NEG)
4376 rtx in1, in2;
4378 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4379 in2 = XEXP (XEXP (x, 1), 1);
4380 return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4381 XEXP (XEXP (x, 0), 0));
4384 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4385 integers. */
4386 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4387 return gen_binary (MINUS, mode,
4388 gen_binary (MINUS, mode, XEXP (x, 0),
4389 XEXP (XEXP (x, 1), 0)),
4390 XEXP (XEXP (x, 1), 1));
4391 break;
4393 case MULT:
4394 /* If we have (mult (plus A B) C), apply the distributive law and then
4395 the inverse distributive law to see if things simplify. This
4396 occurs mostly in addresses, often when unrolling loops. */
4398 if (GET_CODE (XEXP (x, 0)) == PLUS)
4400 x = apply_distributive_law
4401 (gen_binary (PLUS, mode,
4402 gen_binary (MULT, mode,
4403 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4404 gen_binary (MULT, mode,
4405 XEXP (XEXP (x, 0), 1),
4406 copy_rtx (XEXP (x, 1)))));
4408 if (GET_CODE (x) != MULT)
4409 return x;
4411 /* Try simplify a*(b/c) as (a*b)/c. */
4412 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4413 && GET_CODE (XEXP (x, 0)) == DIV)
4415 rtx tem = simplify_binary_operation (MULT, mode,
4416 XEXP (XEXP (x, 0), 0),
4417 XEXP (x, 1));
4418 if (tem)
4419 return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4421 break;
4423 case UDIV:
4424 /* If this is a divide by a power of two, treat it as a shift if
4425 its first operand is a shift. */
4426 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4427 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4428 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4429 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4430 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4431 || GET_CODE (XEXP (x, 0)) == ROTATE
4432 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4433 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4434 break;
4436 case EQ: case NE:
4437 case GT: case GTU: case GE: case GEU:
4438 case LT: case LTU: case LE: case LEU:
4439 case UNEQ: case LTGT:
4440 case UNGT: case UNGE:
4441 case UNLT: case UNLE:
4442 case UNORDERED: case ORDERED:
4443 /* If the first operand is a condition code, we can't do anything
4444 with it. */
4445 if (GET_CODE (XEXP (x, 0)) == COMPARE
4446 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4447 && ! CC0_P (XEXP (x, 0))))
4449 rtx op0 = XEXP (x, 0);
4450 rtx op1 = XEXP (x, 1);
4451 enum rtx_code new_code;
4453 if (GET_CODE (op0) == COMPARE)
4454 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4456 /* Simplify our comparison, if possible. */
4457 new_code = simplify_comparison (code, &op0, &op1);
4459 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4460 if only the low-order bit is possibly nonzero in X (such as when
4461 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4462 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4463 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4464 (plus X 1).
4466 Remove any ZERO_EXTRACT we made when thinking this was a
4467 comparison. It may now be simpler to use, e.g., an AND. If a
4468 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4469 the call to make_compound_operation in the SET case. */
4471 if (STORE_FLAG_VALUE == 1
4472 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4473 && op1 == const0_rtx
4474 && mode == GET_MODE (op0)
4475 && nonzero_bits (op0, mode) == 1)
4476 return gen_lowpart_for_combine (mode,
4477 expand_compound_operation (op0));
4479 else if (STORE_FLAG_VALUE == 1
4480 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4481 && op1 == const0_rtx
4482 && mode == GET_MODE (op0)
4483 && (num_sign_bit_copies (op0, mode)
4484 == GET_MODE_BITSIZE (mode)))
4486 op0 = expand_compound_operation (op0);
4487 return simplify_gen_unary (NEG, mode,
4488 gen_lowpart_for_combine (mode, op0),
4489 mode);
4492 else if (STORE_FLAG_VALUE == 1
4493 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4494 && op1 == const0_rtx
4495 && mode == GET_MODE (op0)
4496 && nonzero_bits (op0, mode) == 1)
4498 op0 = expand_compound_operation (op0);
4499 return gen_binary (XOR, mode,
4500 gen_lowpart_for_combine (mode, op0),
4501 const1_rtx);
4504 else if (STORE_FLAG_VALUE == 1
4505 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4506 && op1 == const0_rtx
4507 && mode == GET_MODE (op0)
4508 && (num_sign_bit_copies (op0, mode)
4509 == GET_MODE_BITSIZE (mode)))
4511 op0 = expand_compound_operation (op0);
4512 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4515 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4516 those above. */
4517 if (STORE_FLAG_VALUE == -1
4518 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4519 && op1 == const0_rtx
4520 && (num_sign_bit_copies (op0, mode)
4521 == GET_MODE_BITSIZE (mode)))
4522 return gen_lowpart_for_combine (mode,
4523 expand_compound_operation (op0));
4525 else if (STORE_FLAG_VALUE == -1
4526 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4527 && op1 == const0_rtx
4528 && mode == GET_MODE (op0)
4529 && nonzero_bits (op0, mode) == 1)
4531 op0 = expand_compound_operation (op0);
4532 return simplify_gen_unary (NEG, mode,
4533 gen_lowpart_for_combine (mode, op0),
4534 mode);
4537 else if (STORE_FLAG_VALUE == -1
4538 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4539 && op1 == const0_rtx
4540 && mode == GET_MODE (op0)
4541 && (num_sign_bit_copies (op0, mode)
4542 == GET_MODE_BITSIZE (mode)))
4544 op0 = expand_compound_operation (op0);
4545 return simplify_gen_unary (NOT, mode,
4546 gen_lowpart_for_combine (mode, op0),
4547 mode);
4550 /* If X is 0/1, (eq X 0) is X-1. */
4551 else if (STORE_FLAG_VALUE == -1
4552 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4553 && op1 == const0_rtx
4554 && mode == GET_MODE (op0)
4555 && nonzero_bits (op0, mode) == 1)
4557 op0 = expand_compound_operation (op0);
4558 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4561 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4562 one bit that might be nonzero, we can convert (ne x 0) to
4563 (ashift x c) where C puts the bit in the sign bit. Remove any
4564 AND with STORE_FLAG_VALUE when we are done, since we are only
4565 going to test the sign bit. */
4566 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4567 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4568 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4569 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4570 && op1 == const0_rtx
4571 && mode == GET_MODE (op0)
4572 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4574 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4575 expand_compound_operation (op0),
4576 GET_MODE_BITSIZE (mode) - 1 - i);
4577 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4578 return XEXP (x, 0);
4579 else
4580 return x;
4583 /* If the code changed, return a whole new comparison. */
4584 if (new_code != code)
4585 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4587 /* Otherwise, keep this operation, but maybe change its operands.
4588 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4589 SUBST (XEXP (x, 0), op0);
4590 SUBST (XEXP (x, 1), op1);
4592 break;
4594 case IF_THEN_ELSE:
4595 return simplify_if_then_else (x);
4597 case ZERO_EXTRACT:
4598 case SIGN_EXTRACT:
4599 case ZERO_EXTEND:
4600 case SIGN_EXTEND:
4601 /* If we are processing SET_DEST, we are done. */
4602 if (in_dest)
4603 return x;
4605 return expand_compound_operation (x);
4607 case SET:
4608 return simplify_set (x);
4610 case AND:
4611 case IOR:
4612 case XOR:
4613 return simplify_logical (x, last);
4615 case ABS:
4616 /* (abs (neg <foo>)) -> (abs <foo>) */
4617 if (GET_CODE (XEXP (x, 0)) == NEG)
4618 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4620 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4621 do nothing. */
4622 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4623 break;
4625 /* If operand is something known to be positive, ignore the ABS. */
4626 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4627 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4628 <= HOST_BITS_PER_WIDE_INT)
4629 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4630 & ((HOST_WIDE_INT) 1
4631 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4632 == 0)))
4633 return XEXP (x, 0);
4635 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4636 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4637 return gen_rtx_NEG (mode, XEXP (x, 0));
4639 break;
4641 case FFS:
4642 /* (ffs (*_extend <X>)) = (ffs <X>) */
4643 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4644 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4645 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4646 break;
4648 case POPCOUNT:
4649 case PARITY:
4650 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4651 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4652 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4653 break;
4655 case FLOAT:
4656 /* (float (sign_extend <X>)) = (float <X>). */
4657 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4658 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4659 break;
4661 case ASHIFT:
4662 case LSHIFTRT:
4663 case ASHIFTRT:
4664 case ROTATE:
4665 case ROTATERT:
4666 /* If this is a shift by a constant amount, simplify it. */
4667 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4668 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4669 INTVAL (XEXP (x, 1)));
4671 #ifdef SHIFT_COUNT_TRUNCATED
4672 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4673 SUBST (XEXP (x, 1),
4674 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4675 ((HOST_WIDE_INT) 1
4676 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4677 - 1,
4678 NULL_RTX, 0));
4679 #endif
4681 break;
4683 case VEC_SELECT:
4685 rtx op0 = XEXP (x, 0);
4686 rtx op1 = XEXP (x, 1);
4687 int len;
4689 if (GET_CODE (op1) != PARALLEL)
4690 abort ();
4691 len = XVECLEN (op1, 0);
4692 if (len == 1
4693 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4694 && GET_CODE (op0) == VEC_CONCAT)
4696 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4698 /* Try to find the element in the VEC_CONCAT. */
4699 for (;;)
4701 if (GET_MODE (op0) == GET_MODE (x))
4702 return op0;
4703 if (GET_CODE (op0) == VEC_CONCAT)
4705 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4706 if (op0_size < offset)
4707 op0 = XEXP (op0, 0);
4708 else
4710 offset -= op0_size;
4711 op0 = XEXP (op0, 1);
4714 else
4715 break;
4720 break;
4722 default:
4723 break;
4726 return x;
4729 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4731 static rtx
4732 simplify_if_then_else (x)
4733 rtx x;
4735 enum machine_mode mode = GET_MODE (x);
4736 rtx cond = XEXP (x, 0);
4737 rtx true_rtx = XEXP (x, 1);
4738 rtx false_rtx = XEXP (x, 2);
4739 enum rtx_code true_code = GET_CODE (cond);
4740 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4741 rtx temp;
4742 int i;
4743 enum rtx_code false_code;
4744 rtx reversed;
4746 /* Simplify storing of the truth value. */
4747 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4748 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4750 /* Also when the truth value has to be reversed. */
4751 if (comparison_p
4752 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4753 && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4754 XEXP (cond, 1))))
4755 return reversed;
4757 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4758 in it is being compared against certain values. Get the true and false
4759 comparisons and see if that says anything about the value of each arm. */
4761 if (comparison_p
4762 && ((false_code = combine_reversed_comparison_code (cond))
4763 != UNKNOWN)
4764 && GET_CODE (XEXP (cond, 0)) == REG)
4766 HOST_WIDE_INT nzb;
4767 rtx from = XEXP (cond, 0);
4768 rtx true_val = XEXP (cond, 1);
4769 rtx false_val = true_val;
4770 int swapped = 0;
4772 /* If FALSE_CODE is EQ, swap the codes and arms. */
4774 if (false_code == EQ)
4776 swapped = 1, true_code = EQ, false_code = NE;
4777 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4780 /* If we are comparing against zero and the expression being tested has
4781 only a single bit that might be nonzero, that is its value when it is
4782 not equal to zero. Similarly if it is known to be -1 or 0. */
4784 if (true_code == EQ && true_val == const0_rtx
4785 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4786 false_code = EQ, false_val = GEN_INT (nzb);
4787 else if (true_code == EQ && true_val == const0_rtx
4788 && (num_sign_bit_copies (from, GET_MODE (from))
4789 == GET_MODE_BITSIZE (GET_MODE (from))))
4790 false_code = EQ, false_val = constm1_rtx;
4792 /* Now simplify an arm if we know the value of the register in the
4793 branch and it is used in the arm. Be careful due to the potential
4794 of locally-shared RTL. */
4796 if (reg_mentioned_p (from, true_rtx))
4797 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4798 from, true_val),
4799 pc_rtx, pc_rtx, 0, 0);
4800 if (reg_mentioned_p (from, false_rtx))
4801 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4802 from, false_val),
4803 pc_rtx, pc_rtx, 0, 0);
4805 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4806 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4808 true_rtx = XEXP (x, 1);
4809 false_rtx = XEXP (x, 2);
4810 true_code = GET_CODE (cond);
4813 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4814 reversed, do so to avoid needing two sets of patterns for
4815 subtract-and-branch insns. Similarly if we have a constant in the true
4816 arm, the false arm is the same as the first operand of the comparison, or
4817 the false arm is more complicated than the true arm. */
4819 if (comparison_p
4820 && combine_reversed_comparison_code (cond) != UNKNOWN
4821 && (true_rtx == pc_rtx
4822 || (CONSTANT_P (true_rtx)
4823 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4824 || true_rtx == const0_rtx
4825 || (GET_RTX_CLASS (GET_CODE (true_rtx)) == 'o'
4826 && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4827 || (GET_CODE (true_rtx) == SUBREG
4828 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true_rtx))) == 'o'
4829 && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4830 || reg_mentioned_p (true_rtx, false_rtx)
4831 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4833 true_code = reversed_comparison_code (cond, NULL);
4834 SUBST (XEXP (x, 0),
4835 reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4836 XEXP (cond, 1)));
4838 SUBST (XEXP (x, 1), false_rtx);
4839 SUBST (XEXP (x, 2), true_rtx);
4841 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4842 cond = XEXP (x, 0);
4844 /* It is possible that the conditional has been simplified out. */
4845 true_code = GET_CODE (cond);
4846 comparison_p = GET_RTX_CLASS (true_code) == '<';
4849 /* If the two arms are identical, we don't need the comparison. */
4851 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4852 return true_rtx;
4854 /* Convert a == b ? b : a to "a". */
4855 if (true_code == EQ && ! side_effects_p (cond)
4856 && !HONOR_NANS (mode)
4857 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4858 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4859 return false_rtx;
4860 else if (true_code == NE && ! side_effects_p (cond)
4861 && !HONOR_NANS (mode)
4862 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4863 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4864 return true_rtx;
4866 /* Look for cases where we have (abs x) or (neg (abs X)). */
4868 if (GET_MODE_CLASS (mode) == MODE_INT
4869 && GET_CODE (false_rtx) == NEG
4870 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4871 && comparison_p
4872 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4873 && ! side_effects_p (true_rtx))
4874 switch (true_code)
4876 case GT:
4877 case GE:
4878 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4879 case LT:
4880 case LE:
4881 return
4882 simplify_gen_unary (NEG, mode,
4883 simplify_gen_unary (ABS, mode, true_rtx, mode),
4884 mode);
4885 default:
4886 break;
4889 /* Look for MIN or MAX. */
4891 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4892 && comparison_p
4893 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4894 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4895 && ! side_effects_p (cond))
4896 switch (true_code)
4898 case GE:
4899 case GT:
4900 return gen_binary (SMAX, mode, true_rtx, false_rtx);
4901 case LE:
4902 case LT:
4903 return gen_binary (SMIN, mode, true_rtx, false_rtx);
4904 case GEU:
4905 case GTU:
4906 return gen_binary (UMAX, mode, true_rtx, false_rtx);
4907 case LEU:
4908 case LTU:
4909 return gen_binary (UMIN, mode, true_rtx, false_rtx);
4910 default:
4911 break;
4914 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4915 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4916 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4917 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4918 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4919 neither 1 or -1, but it isn't worth checking for. */
4921 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4922 && comparison_p
4923 && GET_MODE_CLASS (mode) == MODE_INT
4924 && ! side_effects_p (x))
4926 rtx t = make_compound_operation (true_rtx, SET);
4927 rtx f = make_compound_operation (false_rtx, SET);
4928 rtx cond_op0 = XEXP (cond, 0);
4929 rtx cond_op1 = XEXP (cond, 1);
4930 enum rtx_code op = NIL, extend_op = NIL;
4931 enum machine_mode m = mode;
4932 rtx z = 0, c1 = NULL_RTX;
4934 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4935 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4936 || GET_CODE (t) == ASHIFT
4937 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4938 && rtx_equal_p (XEXP (t, 0), f))
4939 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4941 /* If an identity-zero op is commutative, check whether there
4942 would be a match if we swapped the operands. */
4943 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4944 || GET_CODE (t) == XOR)
4945 && rtx_equal_p (XEXP (t, 1), f))
4946 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4947 else if (GET_CODE (t) == SIGN_EXTEND
4948 && (GET_CODE (XEXP (t, 0)) == PLUS
4949 || GET_CODE (XEXP (t, 0)) == MINUS
4950 || GET_CODE (XEXP (t, 0)) == IOR
4951 || GET_CODE (XEXP (t, 0)) == XOR
4952 || GET_CODE (XEXP (t, 0)) == ASHIFT
4953 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4954 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4955 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4956 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4957 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4958 && (num_sign_bit_copies (f, GET_MODE (f))
4959 > (unsigned int)
4960 (GET_MODE_BITSIZE (mode)
4961 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4963 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4964 extend_op = SIGN_EXTEND;
4965 m = GET_MODE (XEXP (t, 0));
4967 else if (GET_CODE (t) == SIGN_EXTEND
4968 && (GET_CODE (XEXP (t, 0)) == PLUS
4969 || GET_CODE (XEXP (t, 0)) == IOR
4970 || GET_CODE (XEXP (t, 0)) == XOR)
4971 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4972 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4973 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4974 && (num_sign_bit_copies (f, GET_MODE (f))
4975 > (unsigned int)
4976 (GET_MODE_BITSIZE (mode)
4977 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4979 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4980 extend_op = SIGN_EXTEND;
4981 m = GET_MODE (XEXP (t, 0));
4983 else if (GET_CODE (t) == ZERO_EXTEND
4984 && (GET_CODE (XEXP (t, 0)) == PLUS
4985 || GET_CODE (XEXP (t, 0)) == MINUS
4986 || GET_CODE (XEXP (t, 0)) == IOR
4987 || GET_CODE (XEXP (t, 0)) == XOR
4988 || GET_CODE (XEXP (t, 0)) == ASHIFT
4989 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4990 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4991 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4992 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4993 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4994 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4995 && ((nonzero_bits (f, GET_MODE (f))
4996 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4997 == 0))
4999 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5000 extend_op = ZERO_EXTEND;
5001 m = GET_MODE (XEXP (t, 0));
5003 else if (GET_CODE (t) == ZERO_EXTEND
5004 && (GET_CODE (XEXP (t, 0)) == PLUS
5005 || GET_CODE (XEXP (t, 0)) == IOR
5006 || GET_CODE (XEXP (t, 0)) == XOR)
5007 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5008 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5009 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5010 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5011 && ((nonzero_bits (f, GET_MODE (f))
5012 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5013 == 0))
5015 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5016 extend_op = ZERO_EXTEND;
5017 m = GET_MODE (XEXP (t, 0));
5020 if (z)
5022 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
5023 pc_rtx, pc_rtx, 0, 0);
5024 temp = gen_binary (MULT, m, temp,
5025 gen_binary (MULT, m, c1, const_true_rtx));
5026 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5027 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
5029 if (extend_op != NIL)
5030 temp = simplify_gen_unary (extend_op, mode, temp, m);
5032 return temp;
5036 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5037 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5038 negation of a single bit, we can convert this operation to a shift. We
5039 can actually do this more generally, but it doesn't seem worth it. */
5041 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5042 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5043 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5044 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5045 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5046 == GET_MODE_BITSIZE (mode))
5047 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5048 return
5049 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5050 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
5052 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5053 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5054 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5055 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5056 == nonzero_bits (XEXP (cond, 0), mode)
5057 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5058 return XEXP (cond, 0);
5060 return x;
5063 /* Simplify X, a SET expression. Return the new expression. */
5065 static rtx
5066 simplify_set (x)
5067 rtx x;
5069 rtx src = SET_SRC (x);
5070 rtx dest = SET_DEST (x);
5071 enum machine_mode mode
5072 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5073 rtx other_insn;
5074 rtx *cc_use;
5076 /* (set (pc) (return)) gets written as (return). */
5077 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5078 return src;
5080 /* Now that we know for sure which bits of SRC we are using, see if we can
5081 simplify the expression for the object knowing that we only need the
5082 low-order bits. */
5084 if (GET_MODE_CLASS (mode) == MODE_INT
5085 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5087 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5088 SUBST (SET_SRC (x), src);
5091 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5092 the comparison result and try to simplify it unless we already have used
5093 undobuf.other_insn. */
5094 if ((GET_MODE_CLASS (mode) == MODE_CC
5095 || GET_CODE (src) == COMPARE
5096 || CC0_P (dest))
5097 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5098 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5099 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
5100 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5102 enum rtx_code old_code = GET_CODE (*cc_use);
5103 enum rtx_code new_code;
5104 rtx op0, op1, tmp;
5105 int other_changed = 0;
5106 enum machine_mode compare_mode = GET_MODE (dest);
5107 enum machine_mode tmp_mode;
5109 if (GET_CODE (src) == COMPARE)
5110 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5111 else
5112 op0 = src, op1 = const0_rtx;
5114 /* Check whether the comparison is known at compile time. */
5115 if (GET_MODE (op0) != VOIDmode)
5116 tmp_mode = GET_MODE (op0);
5117 else if (GET_MODE (op1) != VOIDmode)
5118 tmp_mode = GET_MODE (op1);
5119 else
5120 tmp_mode = compare_mode;
5121 tmp = simplify_relational_operation (old_code, tmp_mode, op0, op1);
5122 if (tmp != NULL_RTX)
5124 rtx pat = PATTERN (other_insn);
5125 undobuf.other_insn = other_insn;
5126 SUBST (*cc_use, tmp);
5128 /* Attempt to simplify CC user. */
5129 if (GET_CODE (pat) == SET)
5131 rtx new = simplify_rtx (SET_SRC (pat));
5132 if (new != NULL_RTX)
5133 SUBST (SET_SRC (pat), new);
5136 /* Convert X into a no-op move. */
5137 SUBST (SET_DEST (x), pc_rtx);
5138 SUBST (SET_SRC (x), pc_rtx);
5139 return x;
5142 /* Simplify our comparison, if possible. */
5143 new_code = simplify_comparison (old_code, &op0, &op1);
5145 #ifdef EXTRA_CC_MODES
5146 /* If this machine has CC modes other than CCmode, check to see if we
5147 need to use a different CC mode here. */
5148 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5149 #endif /* EXTRA_CC_MODES */
5151 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
5152 /* If the mode changed, we have to change SET_DEST, the mode in the
5153 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5154 a hard register, just build new versions with the proper mode. If it
5155 is a pseudo, we lose unless it is only time we set the pseudo, in
5156 which case we can safely change its mode. */
5157 if (compare_mode != GET_MODE (dest))
5159 unsigned int regno = REGNO (dest);
5160 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5162 if (regno < FIRST_PSEUDO_REGISTER
5163 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5165 if (regno >= FIRST_PSEUDO_REGISTER)
5166 SUBST (regno_reg_rtx[regno], new_dest);
5168 SUBST (SET_DEST (x), new_dest);
5169 SUBST (XEXP (*cc_use, 0), new_dest);
5170 other_changed = 1;
5172 dest = new_dest;
5175 #endif
5177 /* If the code changed, we have to build a new comparison in
5178 undobuf.other_insn. */
5179 if (new_code != old_code)
5181 unsigned HOST_WIDE_INT mask;
5183 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5184 dest, const0_rtx));
5186 /* If the only change we made was to change an EQ into an NE or
5187 vice versa, OP0 has only one bit that might be nonzero, and OP1
5188 is zero, check if changing the user of the condition code will
5189 produce a valid insn. If it won't, we can keep the original code
5190 in that insn by surrounding our operation with an XOR. */
5192 if (((old_code == NE && new_code == EQ)
5193 || (old_code == EQ && new_code == NE))
5194 && ! other_changed && op1 == const0_rtx
5195 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5196 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5198 rtx pat = PATTERN (other_insn), note = 0;
5200 if ((recog_for_combine (&pat, other_insn, &note) < 0
5201 && ! check_asm_operands (pat)))
5203 PUT_CODE (*cc_use, old_code);
5204 other_insn = 0;
5206 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5210 other_changed = 1;
5213 if (other_changed)
5214 undobuf.other_insn = other_insn;
5216 #ifdef HAVE_cc0
5217 /* If we are now comparing against zero, change our source if
5218 needed. If we do not use cc0, we always have a COMPARE. */
5219 if (op1 == const0_rtx && dest == cc0_rtx)
5221 SUBST (SET_SRC (x), op0);
5222 src = op0;
5224 else
5225 #endif
5227 /* Otherwise, if we didn't previously have a COMPARE in the
5228 correct mode, we need one. */
5229 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5231 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5232 src = SET_SRC (x);
5234 else
5236 /* Otherwise, update the COMPARE if needed. */
5237 SUBST (XEXP (src, 0), op0);
5238 SUBST (XEXP (src, 1), op1);
5241 else
5243 /* Get SET_SRC in a form where we have placed back any
5244 compound expressions. Then do the checks below. */
5245 src = make_compound_operation (src, SET);
5246 SUBST (SET_SRC (x), src);
5249 #ifdef WORD_REGISTER_OPERATIONS
5250 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5251 and X being a REG or (subreg (reg)), we may be able to convert this to
5252 (set (subreg:m2 x) (op)).
5254 On a machine where WORD_REGISTER_OPERATIONS is defined, this
5255 transformation is safe as long as M1 and M2 have the same number
5256 of words.
5258 However, on a machine without WORD_REGISTER_OPERATIONS defined,
5259 we cannot apply this transformation because it would create a
5260 paradoxical subreg in SET_DEST. */
5262 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5263 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
5264 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5265 / UNITS_PER_WORD)
5266 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5267 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5268 #ifdef CANNOT_CHANGE_MODE_CLASS
5269 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5270 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5271 GET_MODE (SUBREG_REG (src)),
5272 GET_MODE (src)))
5273 #endif
5274 && (GET_CODE (dest) == REG
5275 || (GET_CODE (dest) == SUBREG
5276 && GET_CODE (SUBREG_REG (dest)) == REG)))
5278 SUBST (SET_DEST (x),
5279 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
5280 dest));
5281 SUBST (SET_SRC (x), SUBREG_REG (src));
5283 src = SET_SRC (x), dest = SET_DEST (x);
5285 #endif
5287 #ifdef HAVE_cc0
5288 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5289 in SRC. */
5290 if (dest == cc0_rtx
5291 && GET_CODE (src) == SUBREG
5292 && subreg_lowpart_p (src)
5293 && (GET_MODE_BITSIZE (GET_MODE (src))
5294 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5296 rtx inner = SUBREG_REG (src);
5297 enum machine_mode inner_mode = GET_MODE (inner);
5299 /* Here we make sure that we don't have a sign bit on. */
5300 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5301 && (nonzero_bits (inner, inner_mode)
5302 < ((unsigned HOST_WIDE_INT) 1
5303 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5305 SUBST (SET_SRC (x), inner);
5306 src = SET_SRC (x);
5309 #endif
5311 #ifdef LOAD_EXTEND_OP
5312 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5313 would require a paradoxical subreg. Replace the subreg with a
5314 zero_extend to avoid the reload that would otherwise be required. */
5316 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5317 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5318 && SUBREG_BYTE (src) == 0
5319 && (GET_MODE_SIZE (GET_MODE (src))
5320 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5321 && GET_CODE (SUBREG_REG (src)) == MEM)
5323 SUBST (SET_SRC (x),
5324 gen_rtx (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5325 GET_MODE (src), SUBREG_REG (src)));
5327 src = SET_SRC (x);
5329 #endif
5331 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5332 are comparing an item known to be 0 or -1 against 0, use a logical
5333 operation instead. Check for one of the arms being an IOR of the other
5334 arm with some value. We compute three terms to be IOR'ed together. In
5335 practice, at most two will be nonzero. Then we do the IOR's. */
5337 if (GET_CODE (dest) != PC
5338 && GET_CODE (src) == IF_THEN_ELSE
5339 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5340 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5341 && XEXP (XEXP (src, 0), 1) == const0_rtx
5342 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5343 #ifdef HAVE_conditional_move
5344 && ! can_conditionally_move_p (GET_MODE (src))
5345 #endif
5346 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5347 GET_MODE (XEXP (XEXP (src, 0), 0)))
5348 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5349 && ! side_effects_p (src))
5351 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5352 ? XEXP (src, 1) : XEXP (src, 2));
5353 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5354 ? XEXP (src, 2) : XEXP (src, 1));
5355 rtx term1 = const0_rtx, term2, term3;
5357 if (GET_CODE (true_rtx) == IOR
5358 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5359 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5360 else if (GET_CODE (true_rtx) == IOR
5361 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5362 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5363 else if (GET_CODE (false_rtx) == IOR
5364 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5365 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5366 else if (GET_CODE (false_rtx) == IOR
5367 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5368 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5370 term2 = gen_binary (AND, GET_MODE (src),
5371 XEXP (XEXP (src, 0), 0), true_rtx);
5372 term3 = gen_binary (AND, GET_MODE (src),
5373 simplify_gen_unary (NOT, GET_MODE (src),
5374 XEXP (XEXP (src, 0), 0),
5375 GET_MODE (src)),
5376 false_rtx);
5378 SUBST (SET_SRC (x),
5379 gen_binary (IOR, GET_MODE (src),
5380 gen_binary (IOR, GET_MODE (src), term1, term2),
5381 term3));
5383 src = SET_SRC (x);
5386 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5387 whole thing fail. */
5388 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5389 return src;
5390 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5391 return dest;
5392 else
5393 /* Convert this into a field assignment operation, if possible. */
5394 return make_field_assignment (x);
5397 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5398 result. LAST is nonzero if this is the last retry. */
5400 static rtx
5401 simplify_logical (x, last)
5402 rtx x;
5403 int last;
5405 enum machine_mode mode = GET_MODE (x);
5406 rtx op0 = XEXP (x, 0);
5407 rtx op1 = XEXP (x, 1);
5408 rtx reversed;
5410 switch (GET_CODE (x))
5412 case AND:
5413 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5414 insn (and may simplify more). */
5415 if (GET_CODE (op0) == XOR
5416 && rtx_equal_p (XEXP (op0, 0), op1)
5417 && ! side_effects_p (op1))
5418 x = gen_binary (AND, mode,
5419 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5420 op1);
5422 if (GET_CODE (op0) == XOR
5423 && rtx_equal_p (XEXP (op0, 1), op1)
5424 && ! side_effects_p (op1))
5425 x = gen_binary (AND, mode,
5426 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5427 op1);
5429 /* Similarly for (~(A ^ B)) & A. */
5430 if (GET_CODE (op0) == NOT
5431 && GET_CODE (XEXP (op0, 0)) == XOR
5432 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5433 && ! side_effects_p (op1))
5434 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5436 if (GET_CODE (op0) == NOT
5437 && GET_CODE (XEXP (op0, 0)) == XOR
5438 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5439 && ! side_effects_p (op1))
5440 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5442 /* We can call simplify_and_const_int only if we don't lose
5443 any (sign) bits when converting INTVAL (op1) to
5444 "unsigned HOST_WIDE_INT". */
5445 if (GET_CODE (op1) == CONST_INT
5446 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5447 || INTVAL (op1) > 0))
5449 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5451 /* If we have (ior (and (X C1) C2)) and the next restart would be
5452 the last, simplify this by making C1 as small as possible
5453 and then exit. */
5454 if (last
5455 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5456 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5457 && GET_CODE (op1) == CONST_INT)
5458 return gen_binary (IOR, mode,
5459 gen_binary (AND, mode, XEXP (op0, 0),
5460 GEN_INT (INTVAL (XEXP (op0, 1))
5461 & ~INTVAL (op1))), op1);
5463 if (GET_CODE (x) != AND)
5464 return x;
5466 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5467 || GET_RTX_CLASS (GET_CODE (x)) == '2')
5468 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5471 /* Convert (A | B) & A to A. */
5472 if (GET_CODE (op0) == IOR
5473 && (rtx_equal_p (XEXP (op0, 0), op1)
5474 || rtx_equal_p (XEXP (op0, 1), op1))
5475 && ! side_effects_p (XEXP (op0, 0))
5476 && ! side_effects_p (XEXP (op0, 1)))
5477 return op1;
5479 /* In the following group of tests (and those in case IOR below),
5480 we start with some combination of logical operations and apply
5481 the distributive law followed by the inverse distributive law.
5482 Most of the time, this results in no change. However, if some of
5483 the operands are the same or inverses of each other, simplifications
5484 will result.
5486 For example, (and (ior A B) (not B)) can occur as the result of
5487 expanding a bit field assignment. When we apply the distributive
5488 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5489 which then simplifies to (and (A (not B))).
5491 If we have (and (ior A B) C), apply the distributive law and then
5492 the inverse distributive law to see if things simplify. */
5494 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5496 x = apply_distributive_law
5497 (gen_binary (GET_CODE (op0), mode,
5498 gen_binary (AND, mode, XEXP (op0, 0), op1),
5499 gen_binary (AND, mode, XEXP (op0, 1),
5500 copy_rtx (op1))));
5501 if (GET_CODE (x) != AND)
5502 return x;
5505 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5506 return apply_distributive_law
5507 (gen_binary (GET_CODE (op1), mode,
5508 gen_binary (AND, mode, XEXP (op1, 0), op0),
5509 gen_binary (AND, mode, XEXP (op1, 1),
5510 copy_rtx (op0))));
5512 /* Similarly, taking advantage of the fact that
5513 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5515 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5516 return apply_distributive_law
5517 (gen_binary (XOR, mode,
5518 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5519 gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5520 XEXP (op1, 1))));
5522 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5523 return apply_distributive_law
5524 (gen_binary (XOR, mode,
5525 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5526 gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5527 break;
5529 case IOR:
5530 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5531 if (GET_CODE (op1) == CONST_INT
5532 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5533 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5534 return op1;
5536 /* Convert (A & B) | A to A. */
5537 if (GET_CODE (op0) == AND
5538 && (rtx_equal_p (XEXP (op0, 0), op1)
5539 || rtx_equal_p (XEXP (op0, 1), op1))
5540 && ! side_effects_p (XEXP (op0, 0))
5541 && ! side_effects_p (XEXP (op0, 1)))
5542 return op1;
5544 /* If we have (ior (and A B) C), apply the distributive law and then
5545 the inverse distributive law to see if things simplify. */
5547 if (GET_CODE (op0) == AND)
5549 x = apply_distributive_law
5550 (gen_binary (AND, mode,
5551 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5552 gen_binary (IOR, mode, XEXP (op0, 1),
5553 copy_rtx (op1))));
5555 if (GET_CODE (x) != IOR)
5556 return x;
5559 if (GET_CODE (op1) == AND)
5561 x = apply_distributive_law
5562 (gen_binary (AND, mode,
5563 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5564 gen_binary (IOR, mode, XEXP (op1, 1),
5565 copy_rtx (op0))));
5567 if (GET_CODE (x) != IOR)
5568 return x;
5571 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5572 mode size to (rotate A CX). */
5574 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5575 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5576 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5577 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5578 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5579 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5580 == GET_MODE_BITSIZE (mode)))
5581 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5582 (GET_CODE (op0) == ASHIFT
5583 ? XEXP (op0, 1) : XEXP (op1, 1)));
5585 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5586 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5587 does not affect any of the bits in OP1, it can really be done
5588 as a PLUS and we can associate. We do this by seeing if OP1
5589 can be safely shifted left C bits. */
5590 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5591 && GET_CODE (XEXP (op0, 0)) == PLUS
5592 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5593 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5594 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5596 int count = INTVAL (XEXP (op0, 1));
5597 HOST_WIDE_INT mask = INTVAL (op1) << count;
5599 if (mask >> count == INTVAL (op1)
5600 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5602 SUBST (XEXP (XEXP (op0, 0), 1),
5603 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5604 return op0;
5607 break;
5609 case XOR:
5610 /* If we are XORing two things that have no bits in common,
5611 convert them into an IOR. This helps to detect rotation encoded
5612 using those methods and possibly other simplifications. */
5614 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5615 && (nonzero_bits (op0, mode)
5616 & nonzero_bits (op1, mode)) == 0)
5617 return (gen_binary (IOR, mode, op0, op1));
5619 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5620 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5621 (NOT y). */
5623 int num_negated = 0;
5625 if (GET_CODE (op0) == NOT)
5626 num_negated++, op0 = XEXP (op0, 0);
5627 if (GET_CODE (op1) == NOT)
5628 num_negated++, op1 = XEXP (op1, 0);
5630 if (num_negated == 2)
5632 SUBST (XEXP (x, 0), op0);
5633 SUBST (XEXP (x, 1), op1);
5635 else if (num_negated == 1)
5636 return
5637 simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5638 mode);
5641 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5642 correspond to a machine insn or result in further simplifications
5643 if B is a constant. */
5645 if (GET_CODE (op0) == AND
5646 && rtx_equal_p (XEXP (op0, 1), op1)
5647 && ! side_effects_p (op1))
5648 return gen_binary (AND, mode,
5649 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5650 op1);
5652 else if (GET_CODE (op0) == AND
5653 && rtx_equal_p (XEXP (op0, 0), op1)
5654 && ! side_effects_p (op1))
5655 return gen_binary (AND, mode,
5656 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5657 op1);
5659 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5660 comparison if STORE_FLAG_VALUE is 1. */
5661 if (STORE_FLAG_VALUE == 1
5662 && op1 == const1_rtx
5663 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5664 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5665 XEXP (op0, 1))))
5666 return reversed;
5668 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5669 is (lt foo (const_int 0)), so we can perform the above
5670 simplification if STORE_FLAG_VALUE is 1. */
5672 if (STORE_FLAG_VALUE == 1
5673 && op1 == const1_rtx
5674 && GET_CODE (op0) == LSHIFTRT
5675 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5676 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5677 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5679 /* (xor (comparison foo bar) (const_int sign-bit))
5680 when STORE_FLAG_VALUE is the sign bit. */
5681 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5682 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5683 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5684 && op1 == const_true_rtx
5685 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5686 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5687 XEXP (op0, 1))))
5688 return reversed;
5690 break;
5692 default:
5693 abort ();
5696 return x;
5699 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5700 operations" because they can be replaced with two more basic operations.
5701 ZERO_EXTEND is also considered "compound" because it can be replaced with
5702 an AND operation, which is simpler, though only one operation.
5704 The function expand_compound_operation is called with an rtx expression
5705 and will convert it to the appropriate shifts and AND operations,
5706 simplifying at each stage.
5708 The function make_compound_operation is called to convert an expression
5709 consisting of shifts and ANDs into the equivalent compound expression.
5710 It is the inverse of this function, loosely speaking. */
5712 static rtx
5713 expand_compound_operation (x)
5714 rtx x;
5716 unsigned HOST_WIDE_INT pos = 0, len;
5717 int unsignedp = 0;
5718 unsigned int modewidth;
5719 rtx tem;
5721 switch (GET_CODE (x))
5723 case ZERO_EXTEND:
5724 unsignedp = 1;
5725 case SIGN_EXTEND:
5726 /* We can't necessarily use a const_int for a multiword mode;
5727 it depends on implicitly extending the value.
5728 Since we don't know the right way to extend it,
5729 we can't tell whether the implicit way is right.
5731 Even for a mode that is no wider than a const_int,
5732 we can't win, because we need to sign extend one of its bits through
5733 the rest of it, and we don't know which bit. */
5734 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5735 return x;
5737 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5738 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5739 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5740 reloaded. If not for that, MEM's would very rarely be safe.
5742 Reject MODEs bigger than a word, because we might not be able
5743 to reference a two-register group starting with an arbitrary register
5744 (and currently gen_lowpart might crash for a SUBREG). */
5746 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5747 return x;
5749 /* Reject MODEs that aren't scalar integers because turning vector
5750 or complex modes into shifts causes problems. */
5752 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5753 return x;
5755 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5756 /* If the inner object has VOIDmode (the only way this can happen
5757 is if it is an ASM_OPERANDS), we can't do anything since we don't
5758 know how much masking to do. */
5759 if (len == 0)
5760 return x;
5762 break;
5764 case ZERO_EXTRACT:
5765 unsignedp = 1;
5766 case SIGN_EXTRACT:
5767 /* If the operand is a CLOBBER, just return it. */
5768 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5769 return XEXP (x, 0);
5771 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5772 || GET_CODE (XEXP (x, 2)) != CONST_INT
5773 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5774 return x;
5776 /* Reject MODEs that aren't scalar integers because turning vector
5777 or complex modes into shifts causes problems. */
5779 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5780 return x;
5782 len = INTVAL (XEXP (x, 1));
5783 pos = INTVAL (XEXP (x, 2));
5785 /* If this goes outside the object being extracted, replace the object
5786 with a (use (mem ...)) construct that only combine understands
5787 and is used only for this purpose. */
5788 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5789 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5791 if (BITS_BIG_ENDIAN)
5792 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5794 break;
5796 default:
5797 return x;
5799 /* Convert sign extension to zero extension, if we know that the high
5800 bit is not set, as this is easier to optimize. It will be converted
5801 back to cheaper alternative in make_extraction. */
5802 if (GET_CODE (x) == SIGN_EXTEND
5803 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5804 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5805 & ~(((unsigned HOST_WIDE_INT)
5806 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5807 >> 1))
5808 == 0)))
5810 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5811 rtx temp2 = expand_compound_operation (temp);
5813 /* Make sure this is a profitable operation. */
5814 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5815 return temp2;
5816 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5817 return temp;
5818 else
5819 return x;
5822 /* We can optimize some special cases of ZERO_EXTEND. */
5823 if (GET_CODE (x) == ZERO_EXTEND)
5825 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5826 know that the last value didn't have any inappropriate bits
5827 set. */
5828 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5829 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5830 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5831 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5832 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5833 return XEXP (XEXP (x, 0), 0);
5835 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5836 if (GET_CODE (XEXP (x, 0)) == SUBREG
5837 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5838 && subreg_lowpart_p (XEXP (x, 0))
5839 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5840 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5841 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5842 return SUBREG_REG (XEXP (x, 0));
5844 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5845 is a comparison and STORE_FLAG_VALUE permits. This is like
5846 the first case, but it works even when GET_MODE (x) is larger
5847 than HOST_WIDE_INT. */
5848 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5849 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5850 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5851 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5852 <= HOST_BITS_PER_WIDE_INT)
5853 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5854 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5855 return XEXP (XEXP (x, 0), 0);
5857 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5858 if (GET_CODE (XEXP (x, 0)) == SUBREG
5859 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5860 && subreg_lowpart_p (XEXP (x, 0))
5861 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5862 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5863 <= HOST_BITS_PER_WIDE_INT)
5864 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5865 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5866 return SUBREG_REG (XEXP (x, 0));
5870 /* If we reach here, we want to return a pair of shifts. The inner
5871 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5872 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5873 logical depending on the value of UNSIGNEDP.
5875 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5876 converted into an AND of a shift.
5878 We must check for the case where the left shift would have a negative
5879 count. This can happen in a case like (x >> 31) & 255 on machines
5880 that can't shift by a constant. On those machines, we would first
5881 combine the shift with the AND to produce a variable-position
5882 extraction. Then the constant of 31 would be substituted in to produce
5883 a such a position. */
5885 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5886 if (modewidth + len >= pos)
5887 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5888 GET_MODE (x),
5889 simplify_shift_const (NULL_RTX, ASHIFT,
5890 GET_MODE (x),
5891 XEXP (x, 0),
5892 modewidth - pos - len),
5893 modewidth - len);
5895 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5896 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5897 simplify_shift_const (NULL_RTX, LSHIFTRT,
5898 GET_MODE (x),
5899 XEXP (x, 0), pos),
5900 ((HOST_WIDE_INT) 1 << len) - 1);
5901 else
5902 /* Any other cases we can't handle. */
5903 return x;
5905 /* If we couldn't do this for some reason, return the original
5906 expression. */
5907 if (GET_CODE (tem) == CLOBBER)
5908 return x;
5910 return tem;
5913 /* X is a SET which contains an assignment of one object into
5914 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5915 or certain SUBREGS). If possible, convert it into a series of
5916 logical operations.
5918 We half-heartedly support variable positions, but do not at all
5919 support variable lengths. */
5921 static rtx
5922 expand_field_assignment (x)
5923 rtx x;
5925 rtx inner;
5926 rtx pos; /* Always counts from low bit. */
5927 int len;
5928 rtx mask;
5929 enum machine_mode compute_mode;
5931 /* Loop until we find something we can't simplify. */
5932 while (1)
5934 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5935 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5937 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5938 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5939 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5941 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5942 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5944 inner = XEXP (SET_DEST (x), 0);
5945 len = INTVAL (XEXP (SET_DEST (x), 1));
5946 pos = XEXP (SET_DEST (x), 2);
5948 /* If the position is constant and spans the width of INNER,
5949 surround INNER with a USE to indicate this. */
5950 if (GET_CODE (pos) == CONST_INT
5951 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5952 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5954 if (BITS_BIG_ENDIAN)
5956 if (GET_CODE (pos) == CONST_INT)
5957 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5958 - INTVAL (pos));
5959 else if (GET_CODE (pos) == MINUS
5960 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5961 && (INTVAL (XEXP (pos, 1))
5962 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5963 /* If position is ADJUST - X, new position is X. */
5964 pos = XEXP (pos, 0);
5965 else
5966 pos = gen_binary (MINUS, GET_MODE (pos),
5967 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5968 - len),
5969 pos);
5973 /* A SUBREG between two modes that occupy the same numbers of words
5974 can be done by moving the SUBREG to the source. */
5975 else if (GET_CODE (SET_DEST (x)) == SUBREG
5976 /* We need SUBREGs to compute nonzero_bits properly. */
5977 && nonzero_sign_valid
5978 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5979 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5980 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5981 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5983 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5984 gen_lowpart_for_combine
5985 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5986 SET_SRC (x)));
5987 continue;
5989 else
5990 break;
5992 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5993 inner = SUBREG_REG (inner);
5995 compute_mode = GET_MODE (inner);
5997 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
5998 if (! SCALAR_INT_MODE_P (compute_mode))
6000 enum machine_mode imode;
6002 /* Don't do anything for vector or complex integral types. */
6003 if (! FLOAT_MODE_P (compute_mode))
6004 break;
6006 /* Try to find an integral mode to pun with. */
6007 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6008 if (imode == BLKmode)
6009 break;
6011 compute_mode = imode;
6012 inner = gen_lowpart_for_combine (imode, inner);
6015 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6016 if (len < HOST_BITS_PER_WIDE_INT)
6017 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6018 else
6019 break;
6021 /* Now compute the equivalent expression. Make a copy of INNER
6022 for the SET_DEST in case it is a MEM into which we will substitute;
6023 we don't want shared RTL in that case. */
6024 x = gen_rtx_SET
6025 (VOIDmode, copy_rtx (inner),
6026 gen_binary (IOR, compute_mode,
6027 gen_binary (AND, compute_mode,
6028 simplify_gen_unary (NOT, compute_mode,
6029 gen_binary (ASHIFT,
6030 compute_mode,
6031 mask, pos),
6032 compute_mode),
6033 inner),
6034 gen_binary (ASHIFT, compute_mode,
6035 gen_binary (AND, compute_mode,
6036 gen_lowpart_for_combine
6037 (compute_mode, SET_SRC (x)),
6038 mask),
6039 pos)));
6042 return x;
6045 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6046 it is an RTX that represents a variable starting position; otherwise,
6047 POS is the (constant) starting bit position (counted from the LSB).
6049 INNER may be a USE. This will occur when we started with a bitfield
6050 that went outside the boundary of the object in memory, which is
6051 allowed on most machines. To isolate this case, we produce a USE
6052 whose mode is wide enough and surround the MEM with it. The only
6053 code that understands the USE is this routine. If it is not removed,
6054 it will cause the resulting insn not to match.
6056 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6057 signed reference.
6059 IN_DEST is nonzero if this is a reference in the destination of a
6060 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6061 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6062 be used.
6064 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6065 ZERO_EXTRACT should be built even for bits starting at bit 0.
6067 MODE is the desired mode of the result (if IN_DEST == 0).
6069 The result is an RTX for the extraction or NULL_RTX if the target
6070 can't handle it. */
6072 static rtx
6073 make_extraction (mode, inner, pos, pos_rtx, len,
6074 unsignedp, in_dest, in_compare)
6075 enum machine_mode mode;
6076 rtx inner;
6077 HOST_WIDE_INT pos;
6078 rtx pos_rtx;
6079 unsigned HOST_WIDE_INT len;
6080 int unsignedp;
6081 int in_dest, in_compare;
6083 /* This mode describes the size of the storage area
6084 to fetch the overall value from. Within that, we
6085 ignore the POS lowest bits, etc. */
6086 enum machine_mode is_mode = GET_MODE (inner);
6087 enum machine_mode inner_mode;
6088 enum machine_mode wanted_inner_mode = byte_mode;
6089 enum machine_mode wanted_inner_reg_mode = word_mode;
6090 enum machine_mode pos_mode = word_mode;
6091 enum machine_mode extraction_mode = word_mode;
6092 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6093 int spans_byte = 0;
6094 rtx new = 0;
6095 rtx orig_pos_rtx = pos_rtx;
6096 HOST_WIDE_INT orig_pos;
6098 /* Get some information about INNER and get the innermost object. */
6099 if (GET_CODE (inner) == USE)
6100 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
6101 /* We don't need to adjust the position because we set up the USE
6102 to pretend that it was a full-word object. */
6103 spans_byte = 1, inner = XEXP (inner, 0);
6104 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6106 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6107 consider just the QI as the memory to extract from.
6108 The subreg adds or removes high bits; its mode is
6109 irrelevant to the meaning of this extraction,
6110 since POS and LEN count from the lsb. */
6111 if (GET_CODE (SUBREG_REG (inner)) == MEM)
6112 is_mode = GET_MODE (SUBREG_REG (inner));
6113 inner = SUBREG_REG (inner);
6115 else if (GET_CODE (inner) == ASHIFT
6116 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6117 && pos_rtx == 0 && pos == 0
6118 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6120 /* We're extracting the least significant bits of an rtx
6121 (ashift X (const_int C)), where LEN > C. Extract the
6122 least significant (LEN - C) bits of X, giving an rtx
6123 whose mode is MODE, then shift it left C times. */
6124 new = make_extraction (mode, XEXP (inner, 0),
6125 0, 0, len - INTVAL (XEXP (inner, 1)),
6126 unsignedp, in_dest, in_compare);
6127 if (new != 0)
6128 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6131 inner_mode = GET_MODE (inner);
6133 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6134 pos = INTVAL (pos_rtx), pos_rtx = 0;
6136 /* See if this can be done without an extraction. We never can if the
6137 width of the field is not the same as that of some integer mode. For
6138 registers, we can only avoid the extraction if the position is at the
6139 low-order bit and this is either not in the destination or we have the
6140 appropriate STRICT_LOW_PART operation available.
6142 For MEM, we can avoid an extract if the field starts on an appropriate
6143 boundary and we can change the mode of the memory reference. However,
6144 we cannot directly access the MEM if we have a USE and the underlying
6145 MEM is not TMODE. This combination means that MEM was being used in a
6146 context where bits outside its mode were being referenced; that is only
6147 valid in bit-field insns. */
6149 if (tmode != BLKmode
6150 && ! (spans_byte && inner_mode != tmode)
6151 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6152 && GET_CODE (inner) != MEM
6153 && (! in_dest
6154 || (GET_CODE (inner) == REG
6155 && have_insn_for (STRICT_LOW_PART, tmode))))
6156 || (GET_CODE (inner) == MEM && pos_rtx == 0
6157 && (pos
6158 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6159 : BITS_PER_UNIT)) == 0
6160 /* We can't do this if we are widening INNER_MODE (it
6161 may not be aligned, for one thing). */
6162 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6163 && (inner_mode == tmode
6164 || (! mode_dependent_address_p (XEXP (inner, 0))
6165 && ! MEM_VOLATILE_P (inner))))))
6167 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6168 field. If the original and current mode are the same, we need not
6169 adjust the offset. Otherwise, we do if bytes big endian.
6171 If INNER is not a MEM, get a piece consisting of just the field
6172 of interest (in this case POS % BITS_PER_WORD must be 0). */
6174 if (GET_CODE (inner) == MEM)
6176 HOST_WIDE_INT offset;
6178 /* POS counts from lsb, but make OFFSET count in memory order. */
6179 if (BYTES_BIG_ENDIAN)
6180 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6181 else
6182 offset = pos / BITS_PER_UNIT;
6184 new = adjust_address_nv (inner, tmode, offset);
6186 else if (GET_CODE (inner) == REG)
6188 /* We can't call gen_lowpart_for_combine here since we always want
6189 a SUBREG and it would sometimes return a new hard register. */
6190 if (tmode != inner_mode)
6192 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6194 if (WORDS_BIG_ENDIAN
6195 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6196 final_word = ((GET_MODE_SIZE (inner_mode)
6197 - GET_MODE_SIZE (tmode))
6198 / UNITS_PER_WORD) - final_word;
6200 final_word *= UNITS_PER_WORD;
6201 if (BYTES_BIG_ENDIAN &&
6202 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6203 final_word += (GET_MODE_SIZE (inner_mode)
6204 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6206 /* Avoid creating invalid subregs, for example when
6207 simplifying (x>>32)&255. */
6208 if (final_word >= GET_MODE_SIZE (inner_mode))
6209 return NULL_RTX;
6211 new = gen_rtx_SUBREG (tmode, inner, final_word);
6213 else
6214 new = inner;
6216 else
6217 new = force_to_mode (inner, tmode,
6218 len >= HOST_BITS_PER_WIDE_INT
6219 ? ~(unsigned HOST_WIDE_INT) 0
6220 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6221 NULL_RTX, 0);
6223 /* If this extraction is going into the destination of a SET,
6224 make a STRICT_LOW_PART unless we made a MEM. */
6226 if (in_dest)
6227 return (GET_CODE (new) == MEM ? new
6228 : (GET_CODE (new) != SUBREG
6229 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6230 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6232 if (mode == tmode)
6233 return new;
6235 if (GET_CODE (new) == CONST_INT)
6236 return gen_int_mode (INTVAL (new), mode);
6238 /* If we know that no extraneous bits are set, and that the high
6239 bit is not set, convert the extraction to the cheaper of
6240 sign and zero extension, that are equivalent in these cases. */
6241 if (flag_expensive_optimizations
6242 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6243 && ((nonzero_bits (new, tmode)
6244 & ~(((unsigned HOST_WIDE_INT)
6245 GET_MODE_MASK (tmode))
6246 >> 1))
6247 == 0)))
6249 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6250 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6252 /* Prefer ZERO_EXTENSION, since it gives more information to
6253 backends. */
6254 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6255 return temp;
6256 return temp1;
6259 /* Otherwise, sign- or zero-extend unless we already are in the
6260 proper mode. */
6262 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6263 mode, new));
6266 /* Unless this is a COMPARE or we have a funny memory reference,
6267 don't do anything with zero-extending field extracts starting at
6268 the low-order bit since they are simple AND operations. */
6269 if (pos_rtx == 0 && pos == 0 && ! in_dest
6270 && ! in_compare && ! spans_byte && unsignedp)
6271 return 0;
6273 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6274 we would be spanning bytes or if the position is not a constant and the
6275 length is not 1. In all other cases, we would only be going outside
6276 our object in cases when an original shift would have been
6277 undefined. */
6278 if (! spans_byte && GET_CODE (inner) == MEM
6279 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6280 || (pos_rtx != 0 && len != 1)))
6281 return 0;
6283 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6284 and the mode for the result. */
6285 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6287 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6288 pos_mode = mode_for_extraction (EP_insv, 2);
6289 extraction_mode = mode_for_extraction (EP_insv, 3);
6292 if (! in_dest && unsignedp
6293 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6295 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6296 pos_mode = mode_for_extraction (EP_extzv, 3);
6297 extraction_mode = mode_for_extraction (EP_extzv, 0);
6300 if (! in_dest && ! unsignedp
6301 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6303 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6304 pos_mode = mode_for_extraction (EP_extv, 3);
6305 extraction_mode = mode_for_extraction (EP_extv, 0);
6308 /* Never narrow an object, since that might not be safe. */
6310 if (mode != VOIDmode
6311 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6312 extraction_mode = mode;
6314 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6315 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6316 pos_mode = GET_MODE (pos_rtx);
6318 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6319 if we have to change the mode of memory and cannot, the desired mode is
6320 EXTRACTION_MODE. */
6321 if (GET_CODE (inner) != MEM)
6322 wanted_inner_mode = wanted_inner_reg_mode;
6323 else if (inner_mode != wanted_inner_mode
6324 && (mode_dependent_address_p (XEXP (inner, 0))
6325 || MEM_VOLATILE_P (inner)))
6326 wanted_inner_mode = extraction_mode;
6328 orig_pos = pos;
6330 if (BITS_BIG_ENDIAN)
6332 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6333 BITS_BIG_ENDIAN style. If position is constant, compute new
6334 position. Otherwise, build subtraction.
6335 Note that POS is relative to the mode of the original argument.
6336 If it's a MEM we need to recompute POS relative to that.
6337 However, if we're extracting from (or inserting into) a register,
6338 we want to recompute POS relative to wanted_inner_mode. */
6339 int width = (GET_CODE (inner) == MEM
6340 ? GET_MODE_BITSIZE (is_mode)
6341 : GET_MODE_BITSIZE (wanted_inner_mode));
6343 if (pos_rtx == 0)
6344 pos = width - len - pos;
6345 else
6346 pos_rtx
6347 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6348 /* POS may be less than 0 now, but we check for that below.
6349 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
6352 /* If INNER has a wider mode, make it smaller. If this is a constant
6353 extract, try to adjust the byte to point to the byte containing
6354 the value. */
6355 if (wanted_inner_mode != VOIDmode
6356 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6357 && ((GET_CODE (inner) == MEM
6358 && (inner_mode == wanted_inner_mode
6359 || (! mode_dependent_address_p (XEXP (inner, 0))
6360 && ! MEM_VOLATILE_P (inner))))))
6362 int offset = 0;
6364 /* The computations below will be correct if the machine is big
6365 endian in both bits and bytes or little endian in bits and bytes.
6366 If it is mixed, we must adjust. */
6368 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6369 adjust OFFSET to compensate. */
6370 if (BYTES_BIG_ENDIAN
6371 && ! spans_byte
6372 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6373 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6375 /* If this is a constant position, we can move to the desired byte. */
6376 if (pos_rtx == 0)
6378 offset += pos / BITS_PER_UNIT;
6379 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6382 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6383 && ! spans_byte
6384 && is_mode != wanted_inner_mode)
6385 offset = (GET_MODE_SIZE (is_mode)
6386 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6388 if (offset != 0 || inner_mode != wanted_inner_mode)
6389 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6392 /* If INNER is not memory, we can always get it into the proper mode. If we
6393 are changing its mode, POS must be a constant and smaller than the size
6394 of the new mode. */
6395 else if (GET_CODE (inner) != MEM)
6397 if (GET_MODE (inner) != wanted_inner_mode
6398 && (pos_rtx != 0
6399 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6400 return 0;
6402 inner = force_to_mode (inner, wanted_inner_mode,
6403 pos_rtx
6404 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6405 ? ~(unsigned HOST_WIDE_INT) 0
6406 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6407 << orig_pos),
6408 NULL_RTX, 0);
6411 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6412 have to zero extend. Otherwise, we can just use a SUBREG. */
6413 if (pos_rtx != 0
6414 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6416 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6418 /* If we know that no extraneous bits are set, and that the high
6419 bit is not set, convert extraction to cheaper one - either
6420 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6421 cases. */
6422 if (flag_expensive_optimizations
6423 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6424 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6425 & ~(((unsigned HOST_WIDE_INT)
6426 GET_MODE_MASK (GET_MODE (pos_rtx)))
6427 >> 1))
6428 == 0)))
6430 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6432 /* Prefer ZERO_EXTENSION, since it gives more information to
6433 backends. */
6434 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6435 temp = temp1;
6437 pos_rtx = temp;
6439 else if (pos_rtx != 0
6440 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6441 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6443 /* Make POS_RTX unless we already have it and it is correct. If we don't
6444 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6445 be a CONST_INT. */
6446 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6447 pos_rtx = orig_pos_rtx;
6449 else if (pos_rtx == 0)
6450 pos_rtx = GEN_INT (pos);
6452 /* Make the required operation. See if we can use existing rtx. */
6453 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6454 extraction_mode, inner, GEN_INT (len), pos_rtx);
6455 if (! in_dest)
6456 new = gen_lowpart_for_combine (mode, new);
6458 return new;
6461 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6462 with any other operations in X. Return X without that shift if so. */
6464 static rtx
6465 extract_left_shift (x, count)
6466 rtx x;
6467 int count;
6469 enum rtx_code code = GET_CODE (x);
6470 enum machine_mode mode = GET_MODE (x);
6471 rtx tem;
6473 switch (code)
6475 case ASHIFT:
6476 /* This is the shift itself. If it is wide enough, we will return
6477 either the value being shifted if the shift count is equal to
6478 COUNT or a shift for the difference. */
6479 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6480 && INTVAL (XEXP (x, 1)) >= count)
6481 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6482 INTVAL (XEXP (x, 1)) - count);
6483 break;
6485 case NEG: case NOT:
6486 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6487 return simplify_gen_unary (code, mode, tem, mode);
6489 break;
6491 case PLUS: case IOR: case XOR: case AND:
6492 /* If we can safely shift this constant and we find the inner shift,
6493 make a new operation. */
6494 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6495 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6496 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6497 return gen_binary (code, mode, tem,
6498 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6500 break;
6502 default:
6503 break;
6506 return 0;
6509 /* Look at the expression rooted at X. Look for expressions
6510 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6511 Form these expressions.
6513 Return the new rtx, usually just X.
6515 Also, for machines like the VAX that don't have logical shift insns,
6516 try to convert logical to arithmetic shift operations in cases where
6517 they are equivalent. This undoes the canonicalizations to logical
6518 shifts done elsewhere.
6520 We try, as much as possible, to re-use rtl expressions to save memory.
6522 IN_CODE says what kind of expression we are processing. Normally, it is
6523 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6524 being kludges), it is MEM. When processing the arguments of a comparison
6525 or a COMPARE against zero, it is COMPARE. */
6527 static rtx
6528 make_compound_operation (x, in_code)
6529 rtx x;
6530 enum rtx_code in_code;
6532 enum rtx_code code = GET_CODE (x);
6533 enum machine_mode mode = GET_MODE (x);
6534 int mode_width = GET_MODE_BITSIZE (mode);
6535 rtx rhs, lhs;
6536 enum rtx_code next_code;
6537 int i;
6538 rtx new = 0;
6539 rtx tem;
6540 const char *fmt;
6542 /* Select the code to be used in recursive calls. Once we are inside an
6543 address, we stay there. If we have a comparison, set to COMPARE,
6544 but once inside, go back to our default of SET. */
6546 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6547 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6548 && XEXP (x, 1) == const0_rtx) ? COMPARE
6549 : in_code == COMPARE ? SET : in_code);
6551 /* Process depending on the code of this operation. If NEW is set
6552 nonzero, it will be returned. */
6554 switch (code)
6556 case ASHIFT:
6557 /* Convert shifts by constants into multiplications if inside
6558 an address. */
6559 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6560 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6561 && INTVAL (XEXP (x, 1)) >= 0)
6563 new = make_compound_operation (XEXP (x, 0), next_code);
6564 new = gen_rtx_MULT (mode, new,
6565 GEN_INT ((HOST_WIDE_INT) 1
6566 << INTVAL (XEXP (x, 1))));
6568 break;
6570 case AND:
6571 /* If the second operand is not a constant, we can't do anything
6572 with it. */
6573 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6574 break;
6576 /* If the constant is a power of two minus one and the first operand
6577 is a logical right shift, make an extraction. */
6578 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6579 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6581 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6582 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6583 0, in_code == COMPARE);
6586 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6587 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6588 && subreg_lowpart_p (XEXP (x, 0))
6589 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6590 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6592 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6593 next_code);
6594 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6595 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6596 0, in_code == COMPARE);
6598 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6599 else if ((GET_CODE (XEXP (x, 0)) == XOR
6600 || GET_CODE (XEXP (x, 0)) == IOR)
6601 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6602 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6603 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6605 /* Apply the distributive law, and then try to make extractions. */
6606 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6607 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6608 XEXP (x, 1)),
6609 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6610 XEXP (x, 1)));
6611 new = make_compound_operation (new, in_code);
6614 /* If we are have (and (rotate X C) M) and C is larger than the number
6615 of bits in M, this is an extraction. */
6617 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6618 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6619 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6620 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6622 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6623 new = make_extraction (mode, new,
6624 (GET_MODE_BITSIZE (mode)
6625 - INTVAL (XEXP (XEXP (x, 0), 1))),
6626 NULL_RTX, i, 1, 0, in_code == COMPARE);
6629 /* On machines without logical shifts, if the operand of the AND is
6630 a logical shift and our mask turns off all the propagated sign
6631 bits, we can replace the logical shift with an arithmetic shift. */
6632 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6633 && !have_insn_for (LSHIFTRT, mode)
6634 && have_insn_for (ASHIFTRT, mode)
6635 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6636 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6637 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6638 && mode_width <= HOST_BITS_PER_WIDE_INT)
6640 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6642 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6643 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6644 SUBST (XEXP (x, 0),
6645 gen_rtx_ASHIFTRT (mode,
6646 make_compound_operation
6647 (XEXP (XEXP (x, 0), 0), next_code),
6648 XEXP (XEXP (x, 0), 1)));
6651 /* If the constant is one less than a power of two, this might be
6652 representable by an extraction even if no shift is present.
6653 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6654 we are in a COMPARE. */
6655 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6656 new = make_extraction (mode,
6657 make_compound_operation (XEXP (x, 0),
6658 next_code),
6659 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6661 /* If we are in a comparison and this is an AND with a power of two,
6662 convert this into the appropriate bit extract. */
6663 else if (in_code == COMPARE
6664 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6665 new = make_extraction (mode,
6666 make_compound_operation (XEXP (x, 0),
6667 next_code),
6668 i, NULL_RTX, 1, 1, 0, 1);
6670 break;
6672 case LSHIFTRT:
6673 /* If the sign bit is known to be zero, replace this with an
6674 arithmetic shift. */
6675 if (have_insn_for (ASHIFTRT, mode)
6676 && ! have_insn_for (LSHIFTRT, mode)
6677 && mode_width <= HOST_BITS_PER_WIDE_INT
6678 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6680 new = gen_rtx_ASHIFTRT (mode,
6681 make_compound_operation (XEXP (x, 0),
6682 next_code),
6683 XEXP (x, 1));
6684 break;
6687 /* ... fall through ... */
6689 case ASHIFTRT:
6690 lhs = XEXP (x, 0);
6691 rhs = XEXP (x, 1);
6693 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6694 this is a SIGN_EXTRACT. */
6695 if (GET_CODE (rhs) == CONST_INT
6696 && GET_CODE (lhs) == ASHIFT
6697 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6698 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6700 new = make_compound_operation (XEXP (lhs, 0), next_code);
6701 new = make_extraction (mode, new,
6702 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6703 NULL_RTX, mode_width - INTVAL (rhs),
6704 code == LSHIFTRT, 0, in_code == COMPARE);
6705 break;
6708 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6709 If so, try to merge the shifts into a SIGN_EXTEND. We could
6710 also do this for some cases of SIGN_EXTRACT, but it doesn't
6711 seem worth the effort; the case checked for occurs on Alpha. */
6713 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6714 && ! (GET_CODE (lhs) == SUBREG
6715 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6716 && GET_CODE (rhs) == CONST_INT
6717 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6718 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6719 new = make_extraction (mode, make_compound_operation (new, next_code),
6720 0, NULL_RTX, mode_width - INTVAL (rhs),
6721 code == LSHIFTRT, 0, in_code == COMPARE);
6723 break;
6725 case SUBREG:
6726 /* Call ourselves recursively on the inner expression. If we are
6727 narrowing the object and it has a different RTL code from
6728 what it originally did, do this SUBREG as a force_to_mode. */
6730 tem = make_compound_operation (SUBREG_REG (x), in_code);
6731 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6732 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6733 && subreg_lowpart_p (x))
6735 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6736 NULL_RTX, 0);
6738 /* If we have something other than a SUBREG, we might have
6739 done an expansion, so rerun ourselves. */
6740 if (GET_CODE (newer) != SUBREG)
6741 newer = make_compound_operation (newer, in_code);
6743 return newer;
6746 /* If this is a paradoxical subreg, and the new code is a sign or
6747 zero extension, omit the subreg and widen the extension. If it
6748 is a regular subreg, we can still get rid of the subreg by not
6749 widening so much, or in fact removing the extension entirely. */
6750 if ((GET_CODE (tem) == SIGN_EXTEND
6751 || GET_CODE (tem) == ZERO_EXTEND)
6752 && subreg_lowpart_p (x))
6754 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6755 || (GET_MODE_SIZE (mode) >
6756 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6758 if (! SCALAR_INT_MODE_P (mode))
6759 break;
6760 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6762 else
6763 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6764 return tem;
6766 break;
6768 default:
6769 break;
6772 if (new)
6774 x = gen_lowpart_for_combine (mode, new);
6775 code = GET_CODE (x);
6778 /* Now recursively process each operand of this operation. */
6779 fmt = GET_RTX_FORMAT (code);
6780 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6781 if (fmt[i] == 'e')
6783 new = make_compound_operation (XEXP (x, i), next_code);
6784 SUBST (XEXP (x, i), new);
6787 return x;
6790 /* Given M see if it is a value that would select a field of bits
6791 within an item, but not the entire word. Return -1 if not.
6792 Otherwise, return the starting position of the field, where 0 is the
6793 low-order bit.
6795 *PLEN is set to the length of the field. */
6797 static int
6798 get_pos_from_mask (m, plen)
6799 unsigned HOST_WIDE_INT m;
6800 unsigned HOST_WIDE_INT *plen;
6802 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6803 int pos = exact_log2 (m & -m);
6804 int len;
6806 if (pos < 0)
6807 return -1;
6809 /* Now shift off the low-order zero bits and see if we have a power of
6810 two minus 1. */
6811 len = exact_log2 ((m >> pos) + 1);
6813 if (len <= 0)
6814 return -1;
6816 *plen = len;
6817 return pos;
6820 /* See if X can be simplified knowing that we will only refer to it in
6821 MODE and will only refer to those bits that are nonzero in MASK.
6822 If other bits are being computed or if masking operations are done
6823 that select a superset of the bits in MASK, they can sometimes be
6824 ignored.
6826 Return a possibly simplified expression, but always convert X to
6827 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6829 Also, if REG is nonzero and X is a register equal in value to REG,
6830 replace X with REG.
6832 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6833 are all off in X. This is used when X will be complemented, by either
6834 NOT, NEG, or XOR. */
6836 static rtx
6837 force_to_mode (x, mode, mask, reg, just_select)
6838 rtx x;
6839 enum machine_mode mode;
6840 unsigned HOST_WIDE_INT mask;
6841 rtx reg;
6842 int just_select;
6844 enum rtx_code code = GET_CODE (x);
6845 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6846 enum machine_mode op_mode;
6847 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6848 rtx op0, op1, temp;
6850 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6851 code below will do the wrong thing since the mode of such an
6852 expression is VOIDmode.
6854 Also do nothing if X is a CLOBBER; this can happen if X was
6855 the return value from a call to gen_lowpart_for_combine. */
6856 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6857 return x;
6859 /* We want to perform the operation is its present mode unless we know
6860 that the operation is valid in MODE, in which case we do the operation
6861 in MODE. */
6862 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6863 && have_insn_for (code, mode))
6864 ? mode : GET_MODE (x));
6866 /* It is not valid to do a right-shift in a narrower mode
6867 than the one it came in with. */
6868 if ((code == LSHIFTRT || code == ASHIFTRT)
6869 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6870 op_mode = GET_MODE (x);
6872 /* Truncate MASK to fit OP_MODE. */
6873 if (op_mode)
6874 mask &= GET_MODE_MASK (op_mode);
6876 /* When we have an arithmetic operation, or a shift whose count we
6877 do not know, we need to assume that all bit the up to the highest-order
6878 bit in MASK will be needed. This is how we form such a mask. */
6879 if (op_mode)
6880 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6881 ? GET_MODE_MASK (op_mode)
6882 : (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6883 - 1));
6884 else
6885 fuller_mask = ~(HOST_WIDE_INT) 0;
6887 /* Determine what bits of X are guaranteed to be (non)zero. */
6888 nonzero = nonzero_bits (x, mode);
6890 /* If none of the bits in X are needed, return a zero. */
6891 if (! just_select && (nonzero & mask) == 0)
6892 x = const0_rtx;
6894 /* If X is a CONST_INT, return a new one. Do this here since the
6895 test below will fail. */
6896 if (GET_CODE (x) == CONST_INT)
6898 if (SCALAR_INT_MODE_P (mode))
6899 return gen_int_mode (INTVAL (x) & mask, mode);
6900 else
6902 x = GEN_INT (INTVAL (x) & mask);
6903 return gen_lowpart_common (mode, x);
6907 /* If X is narrower than MODE and we want all the bits in X's mode, just
6908 get X in the proper mode. */
6909 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6910 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6911 return gen_lowpart_for_combine (mode, x);
6913 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6914 MASK are already known to be zero in X, we need not do anything. */
6915 if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6916 return x;
6918 switch (code)
6920 case CLOBBER:
6921 /* If X is a (clobber (const_int)), return it since we know we are
6922 generating something that won't match. */
6923 return x;
6925 case USE:
6926 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6927 spanned the boundary of the MEM. If we are now masking so it is
6928 within that boundary, we don't need the USE any more. */
6929 if (! BITS_BIG_ENDIAN
6930 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6931 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6932 break;
6934 case SIGN_EXTEND:
6935 case ZERO_EXTEND:
6936 case ZERO_EXTRACT:
6937 case SIGN_EXTRACT:
6938 x = expand_compound_operation (x);
6939 if (GET_CODE (x) != code)
6940 return force_to_mode (x, mode, mask, reg, next_select);
6941 break;
6943 case REG:
6944 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6945 || rtx_equal_p (reg, get_last_value (x))))
6946 x = reg;
6947 break;
6949 case SUBREG:
6950 if (subreg_lowpart_p (x)
6951 /* We can ignore the effect of this SUBREG if it narrows the mode or
6952 if the constant masks to zero all the bits the mode doesn't
6953 have. */
6954 && ((GET_MODE_SIZE (GET_MODE (x))
6955 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6956 || (0 == (mask
6957 & GET_MODE_MASK (GET_MODE (x))
6958 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6959 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6960 break;
6962 case AND:
6963 /* If this is an AND with a constant, convert it into an AND
6964 whose constant is the AND of that constant with MASK. If it
6965 remains an AND of MASK, delete it since it is redundant. */
6967 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6969 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6970 mask & INTVAL (XEXP (x, 1)));
6972 /* If X is still an AND, see if it is an AND with a mask that
6973 is just some low-order bits. If so, and it is MASK, we don't
6974 need it. */
6976 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6977 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6978 == mask))
6979 x = XEXP (x, 0);
6981 /* If it remains an AND, try making another AND with the bits
6982 in the mode mask that aren't in MASK turned on. If the
6983 constant in the AND is wide enough, this might make a
6984 cheaper constant. */
6986 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6987 && GET_MODE_MASK (GET_MODE (x)) != mask
6988 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6990 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6991 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6992 int width = GET_MODE_BITSIZE (GET_MODE (x));
6993 rtx y;
6995 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6996 number, sign extend it. */
6997 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6998 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6999 cval |= (HOST_WIDE_INT) -1 << width;
7001 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
7002 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7003 x = y;
7006 break;
7009 goto binop;
7011 case PLUS:
7012 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7013 low-order bits (as in an alignment operation) and FOO is already
7014 aligned to that boundary, mask C1 to that boundary as well.
7015 This may eliminate that PLUS and, later, the AND. */
7018 unsigned int width = GET_MODE_BITSIZE (mode);
7019 unsigned HOST_WIDE_INT smask = mask;
7021 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7022 number, sign extend it. */
7024 if (width < HOST_BITS_PER_WIDE_INT
7025 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7026 smask |= (HOST_WIDE_INT) -1 << width;
7028 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7029 && exact_log2 (- smask) >= 0
7030 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7031 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7032 return force_to_mode (plus_constant (XEXP (x, 0),
7033 (INTVAL (XEXP (x, 1)) & smask)),
7034 mode, smask, reg, next_select);
7037 /* ... fall through ... */
7039 case MULT:
7040 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7041 most significant bit in MASK since carries from those bits will
7042 affect the bits we are interested in. */
7043 mask = fuller_mask;
7044 goto binop;
7046 case MINUS:
7047 /* If X is (minus C Y) where C's least set bit is larger than any bit
7048 in the mask, then we may replace with (neg Y). */
7049 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7050 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7051 & -INTVAL (XEXP (x, 0))))
7052 > mask))
7054 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7055 GET_MODE (x));
7056 return force_to_mode (x, mode, mask, reg, next_select);
7059 /* Similarly, if C contains every bit in the fuller_mask, then we may
7060 replace with (not Y). */
7061 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7062 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7063 == INTVAL (XEXP (x, 0))))
7065 x = simplify_gen_unary (NOT, GET_MODE (x),
7066 XEXP (x, 1), GET_MODE (x));
7067 return force_to_mode (x, mode, mask, reg, next_select);
7070 mask = fuller_mask;
7071 goto binop;
7073 case IOR:
7074 case XOR:
7075 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7076 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7077 operation which may be a bitfield extraction. Ensure that the
7078 constant we form is not wider than the mode of X. */
7080 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7081 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7082 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7083 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7084 && GET_CODE (XEXP (x, 1)) == CONST_INT
7085 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7086 + floor_log2 (INTVAL (XEXP (x, 1))))
7087 < GET_MODE_BITSIZE (GET_MODE (x)))
7088 && (INTVAL (XEXP (x, 1))
7089 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7091 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7092 << INTVAL (XEXP (XEXP (x, 0), 1)));
7093 temp = gen_binary (GET_CODE (x), GET_MODE (x),
7094 XEXP (XEXP (x, 0), 0), temp);
7095 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
7096 XEXP (XEXP (x, 0), 1));
7097 return force_to_mode (x, mode, mask, reg, next_select);
7100 binop:
7101 /* For most binary operations, just propagate into the operation and
7102 change the mode if we have an operation of that mode. */
7104 op0 = gen_lowpart_for_combine (op_mode,
7105 force_to_mode (XEXP (x, 0), mode, mask,
7106 reg, next_select));
7107 op1 = gen_lowpart_for_combine (op_mode,
7108 force_to_mode (XEXP (x, 1), mode, mask,
7109 reg, next_select));
7111 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7112 x = gen_binary (code, op_mode, op0, op1);
7113 break;
7115 case ASHIFT:
7116 /* For left shifts, do the same, but just for the first operand.
7117 However, we cannot do anything with shifts where we cannot
7118 guarantee that the counts are smaller than the size of the mode
7119 because such a count will have a different meaning in a
7120 wider mode. */
7122 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7123 && INTVAL (XEXP (x, 1)) >= 0
7124 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7125 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7126 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7127 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7128 break;
7130 /* If the shift count is a constant and we can do arithmetic in
7131 the mode of the shift, refine which bits we need. Otherwise, use the
7132 conservative form of the mask. */
7133 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7134 && INTVAL (XEXP (x, 1)) >= 0
7135 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7136 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7137 mask >>= INTVAL (XEXP (x, 1));
7138 else
7139 mask = fuller_mask;
7141 op0 = gen_lowpart_for_combine (op_mode,
7142 force_to_mode (XEXP (x, 0), op_mode,
7143 mask, reg, next_select));
7145 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7146 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7147 break;
7149 case LSHIFTRT:
7150 /* Here we can only do something if the shift count is a constant,
7151 this shift constant is valid for the host, and we can do arithmetic
7152 in OP_MODE. */
7154 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7155 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7156 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7158 rtx inner = XEXP (x, 0);
7159 unsigned HOST_WIDE_INT inner_mask;
7161 /* Select the mask of the bits we need for the shift operand. */
7162 inner_mask = mask << INTVAL (XEXP (x, 1));
7164 /* We can only change the mode of the shift if we can do arithmetic
7165 in the mode of the shift and INNER_MASK is no wider than the
7166 width of OP_MODE. */
7167 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7168 || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7169 op_mode = GET_MODE (x);
7171 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7173 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7174 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7177 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7178 shift and AND produces only copies of the sign bit (C2 is one less
7179 than a power of two), we can do this with just a shift. */
7181 if (GET_CODE (x) == LSHIFTRT
7182 && GET_CODE (XEXP (x, 1)) == CONST_INT
7183 /* The shift puts one of the sign bit copies in the least significant
7184 bit. */
7185 && ((INTVAL (XEXP (x, 1))
7186 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7187 >= GET_MODE_BITSIZE (GET_MODE (x)))
7188 && exact_log2 (mask + 1) >= 0
7189 /* Number of bits left after the shift must be more than the mask
7190 needs. */
7191 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7192 <= GET_MODE_BITSIZE (GET_MODE (x)))
7193 /* Must be more sign bit copies than the mask needs. */
7194 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7195 >= exact_log2 (mask + 1)))
7196 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7197 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7198 - exact_log2 (mask + 1)));
7200 goto shiftrt;
7202 case ASHIFTRT:
7203 /* If we are just looking for the sign bit, we don't need this shift at
7204 all, even if it has a variable count. */
7205 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7206 && (mask == ((unsigned HOST_WIDE_INT) 1
7207 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7208 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7210 /* If this is a shift by a constant, get a mask that contains those bits
7211 that are not copies of the sign bit. We then have two cases: If
7212 MASK only includes those bits, this can be a logical shift, which may
7213 allow simplifications. If MASK is a single-bit field not within
7214 those bits, we are requesting a copy of the sign bit and hence can
7215 shift the sign bit to the appropriate location. */
7217 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7218 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7220 int i = -1;
7222 /* If the considered data is wider than HOST_WIDE_INT, we can't
7223 represent a mask for all its bits in a single scalar.
7224 But we only care about the lower bits, so calculate these. */
7226 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7228 nonzero = ~(HOST_WIDE_INT) 0;
7230 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7231 is the number of bits a full-width mask would have set.
7232 We need only shift if these are fewer than nonzero can
7233 hold. If not, we must keep all bits set in nonzero. */
7235 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7236 < HOST_BITS_PER_WIDE_INT)
7237 nonzero >>= INTVAL (XEXP (x, 1))
7238 + HOST_BITS_PER_WIDE_INT
7239 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7241 else
7243 nonzero = GET_MODE_MASK (GET_MODE (x));
7244 nonzero >>= INTVAL (XEXP (x, 1));
7247 if ((mask & ~nonzero) == 0
7248 || (i = exact_log2 (mask)) >= 0)
7250 x = simplify_shift_const
7251 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7252 i < 0 ? INTVAL (XEXP (x, 1))
7253 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7255 if (GET_CODE (x) != ASHIFTRT)
7256 return force_to_mode (x, mode, mask, reg, next_select);
7260 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7261 even if the shift count isn't a constant. */
7262 if (mask == 1)
7263 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7265 shiftrt:
7267 /* If this is a zero- or sign-extension operation that just affects bits
7268 we don't care about, remove it. Be sure the call above returned
7269 something that is still a shift. */
7271 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7272 && GET_CODE (XEXP (x, 1)) == CONST_INT
7273 && INTVAL (XEXP (x, 1)) >= 0
7274 && (INTVAL (XEXP (x, 1))
7275 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7276 && GET_CODE (XEXP (x, 0)) == ASHIFT
7277 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7278 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
7279 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7280 reg, next_select);
7282 break;
7284 case ROTATE:
7285 case ROTATERT:
7286 /* If the shift count is constant and we can do computations
7287 in the mode of X, compute where the bits we care about are.
7288 Otherwise, we can't do anything. Don't change the mode of
7289 the shift or propagate MODE into the shift, though. */
7290 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7291 && INTVAL (XEXP (x, 1)) >= 0)
7293 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7294 GET_MODE (x), GEN_INT (mask),
7295 XEXP (x, 1));
7296 if (temp && GET_CODE (temp) == CONST_INT)
7297 SUBST (XEXP (x, 0),
7298 force_to_mode (XEXP (x, 0), GET_MODE (x),
7299 INTVAL (temp), reg, next_select));
7301 break;
7303 case NEG:
7304 /* If we just want the low-order bit, the NEG isn't needed since it
7305 won't change the low-order bit. */
7306 if (mask == 1)
7307 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7309 /* We need any bits less significant than the most significant bit in
7310 MASK since carries from those bits will affect the bits we are
7311 interested in. */
7312 mask = fuller_mask;
7313 goto unop;
7315 case NOT:
7316 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7317 same as the XOR case above. Ensure that the constant we form is not
7318 wider than the mode of X. */
7320 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7321 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7322 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7323 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7324 < GET_MODE_BITSIZE (GET_MODE (x)))
7325 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7327 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7328 GET_MODE (x));
7329 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7330 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7332 return force_to_mode (x, mode, mask, reg, next_select);
7335 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7336 use the full mask inside the NOT. */
7337 mask = fuller_mask;
7339 unop:
7340 op0 = gen_lowpart_for_combine (op_mode,
7341 force_to_mode (XEXP (x, 0), mode, mask,
7342 reg, next_select));
7343 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7344 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7345 break;
7347 case NE:
7348 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7349 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7350 which is equal to STORE_FLAG_VALUE. */
7351 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7352 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7353 && (nonzero_bits (XEXP (x, 0), mode)
7354 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7355 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7357 break;
7359 case IF_THEN_ELSE:
7360 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7361 written in a narrower mode. We play it safe and do not do so. */
7363 SUBST (XEXP (x, 1),
7364 gen_lowpart_for_combine (GET_MODE (x),
7365 force_to_mode (XEXP (x, 1), mode,
7366 mask, reg, next_select)));
7367 SUBST (XEXP (x, 2),
7368 gen_lowpart_for_combine (GET_MODE (x),
7369 force_to_mode (XEXP (x, 2), mode,
7370 mask, reg, next_select)));
7371 break;
7373 default:
7374 break;
7377 /* Ensure we return a value of the proper mode. */
7378 return gen_lowpart_for_combine (mode, x);
7381 /* Return nonzero if X is an expression that has one of two values depending on
7382 whether some other value is zero or nonzero. In that case, we return the
7383 value that is being tested, *PTRUE is set to the value if the rtx being
7384 returned has a nonzero value, and *PFALSE is set to the other alternative.
7386 If we return zero, we set *PTRUE and *PFALSE to X. */
7388 static rtx
7389 if_then_else_cond (x, ptrue, pfalse)
7390 rtx x;
7391 rtx *ptrue, *pfalse;
7393 enum machine_mode mode = GET_MODE (x);
7394 enum rtx_code code = GET_CODE (x);
7395 rtx cond0, cond1, true0, true1, false0, false1;
7396 unsigned HOST_WIDE_INT nz;
7398 /* If we are comparing a value against zero, we are done. */
7399 if ((code == NE || code == EQ)
7400 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
7402 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7403 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7404 return XEXP (x, 0);
7407 /* If this is a unary operation whose operand has one of two values, apply
7408 our opcode to compute those values. */
7409 else if (GET_RTX_CLASS (code) == '1'
7410 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7412 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7413 *pfalse = simplify_gen_unary (code, mode, false0,
7414 GET_MODE (XEXP (x, 0)));
7415 return cond0;
7418 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7419 make can't possibly match and would suppress other optimizations. */
7420 else if (code == COMPARE)
7423 /* If this is a binary operation, see if either side has only one of two
7424 values. If either one does or if both do and they are conditional on
7425 the same value, compute the new true and false values. */
7426 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7427 || GET_RTX_CLASS (code) == '<')
7429 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7430 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7432 if ((cond0 != 0 || cond1 != 0)
7433 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7435 /* If if_then_else_cond returned zero, then true/false are the
7436 same rtl. We must copy one of them to prevent invalid rtl
7437 sharing. */
7438 if (cond0 == 0)
7439 true0 = copy_rtx (true0);
7440 else if (cond1 == 0)
7441 true1 = copy_rtx (true1);
7443 *ptrue = gen_binary (code, mode, true0, true1);
7444 *pfalse = gen_binary (code, mode, false0, false1);
7445 return cond0 ? cond0 : cond1;
7448 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7449 operands is zero when the other is nonzero, and vice-versa,
7450 and STORE_FLAG_VALUE is 1 or -1. */
7452 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7453 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7454 || code == UMAX)
7455 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7457 rtx op0 = XEXP (XEXP (x, 0), 1);
7458 rtx op1 = XEXP (XEXP (x, 1), 1);
7460 cond0 = XEXP (XEXP (x, 0), 0);
7461 cond1 = XEXP (XEXP (x, 1), 0);
7463 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7464 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7465 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7466 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7467 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7468 || ((swap_condition (GET_CODE (cond0))
7469 == combine_reversed_comparison_code (cond1))
7470 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7471 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7472 && ! side_effects_p (x))
7474 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7475 *pfalse = gen_binary (MULT, mode,
7476 (code == MINUS
7477 ? simplify_gen_unary (NEG, mode, op1,
7478 mode)
7479 : op1),
7480 const_true_rtx);
7481 return cond0;
7485 /* Similarly for MULT, AND and UMIN, except that for these the result
7486 is always zero. */
7487 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7488 && (code == MULT || code == AND || code == UMIN)
7489 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7491 cond0 = XEXP (XEXP (x, 0), 0);
7492 cond1 = XEXP (XEXP (x, 1), 0);
7494 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7495 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7496 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7497 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7498 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7499 || ((swap_condition (GET_CODE (cond0))
7500 == combine_reversed_comparison_code (cond1))
7501 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7502 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7503 && ! side_effects_p (x))
7505 *ptrue = *pfalse = const0_rtx;
7506 return cond0;
7511 else if (code == IF_THEN_ELSE)
7513 /* If we have IF_THEN_ELSE already, extract the condition and
7514 canonicalize it if it is NE or EQ. */
7515 cond0 = XEXP (x, 0);
7516 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7517 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7518 return XEXP (cond0, 0);
7519 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7521 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7522 return XEXP (cond0, 0);
7524 else
7525 return cond0;
7528 /* If X is a SUBREG, we can narrow both the true and false values
7529 if the inner expression, if there is a condition. */
7530 else if (code == SUBREG
7531 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7532 &true0, &false0)))
7534 *ptrue = simplify_gen_subreg (mode, true0,
7535 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7536 *pfalse = simplify_gen_subreg (mode, false0,
7537 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7539 return cond0;
7542 /* If X is a constant, this isn't special and will cause confusions
7543 if we treat it as such. Likewise if it is equivalent to a constant. */
7544 else if (CONSTANT_P (x)
7545 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7548 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7549 will be least confusing to the rest of the compiler. */
7550 else if (mode == BImode)
7552 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7553 return x;
7556 /* If X is known to be either 0 or -1, those are the true and
7557 false values when testing X. */
7558 else if (x == constm1_rtx || x == const0_rtx
7559 || (mode != VOIDmode
7560 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7562 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7563 return x;
7566 /* Likewise for 0 or a single bit. */
7567 else if (mode != VOIDmode
7568 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7569 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7571 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7572 return x;
7575 /* Otherwise fail; show no condition with true and false values the same. */
7576 *ptrue = *pfalse = x;
7577 return 0;
7580 /* Return the value of expression X given the fact that condition COND
7581 is known to be true when applied to REG as its first operand and VAL
7582 as its second. X is known to not be shared and so can be modified in
7583 place.
7585 We only handle the simplest cases, and specifically those cases that
7586 arise with IF_THEN_ELSE expressions. */
7588 static rtx
7589 known_cond (x, cond, reg, val)
7590 rtx x;
7591 enum rtx_code cond;
7592 rtx reg, val;
7594 enum rtx_code code = GET_CODE (x);
7595 rtx temp;
7596 const char *fmt;
7597 int i, j;
7599 if (side_effects_p (x))
7600 return x;
7602 /* If either operand of the condition is a floating point value,
7603 then we have to avoid collapsing an EQ comparison. */
7604 if (cond == EQ
7605 && rtx_equal_p (x, reg)
7606 && ! FLOAT_MODE_P (GET_MODE (x))
7607 && ! FLOAT_MODE_P (GET_MODE (val)))
7608 return val;
7610 if (cond == UNEQ && rtx_equal_p (x, reg))
7611 return val;
7613 /* If X is (abs REG) and we know something about REG's relationship
7614 with zero, we may be able to simplify this. */
7616 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7617 switch (cond)
7619 case GE: case GT: case EQ:
7620 return XEXP (x, 0);
7621 case LT: case LE:
7622 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7623 XEXP (x, 0),
7624 GET_MODE (XEXP (x, 0)));
7625 default:
7626 break;
7629 /* The only other cases we handle are MIN, MAX, and comparisons if the
7630 operands are the same as REG and VAL. */
7632 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7634 if (rtx_equal_p (XEXP (x, 0), val))
7635 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7637 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7639 if (GET_RTX_CLASS (code) == '<')
7641 if (comparison_dominates_p (cond, code))
7642 return const_true_rtx;
7644 code = combine_reversed_comparison_code (x);
7645 if (code != UNKNOWN
7646 && comparison_dominates_p (cond, code))
7647 return const0_rtx;
7648 else
7649 return x;
7651 else if (code == SMAX || code == SMIN
7652 || code == UMIN || code == UMAX)
7654 int unsignedp = (code == UMIN || code == UMAX);
7656 /* Do not reverse the condition when it is NE or EQ.
7657 This is because we cannot conclude anything about
7658 the value of 'SMAX (x, y)' when x is not equal to y,
7659 but we can when x equals y. */
7660 if ((code == SMAX || code == UMAX)
7661 && ! (cond == EQ || cond == NE))
7662 cond = reverse_condition (cond);
7664 switch (cond)
7666 case GE: case GT:
7667 return unsignedp ? x : XEXP (x, 1);
7668 case LE: case LT:
7669 return unsignedp ? x : XEXP (x, 0);
7670 case GEU: case GTU:
7671 return unsignedp ? XEXP (x, 1) : x;
7672 case LEU: case LTU:
7673 return unsignedp ? XEXP (x, 0) : x;
7674 default:
7675 break;
7680 else if (code == SUBREG)
7682 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7683 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7685 if (SUBREG_REG (x) != r)
7687 /* We must simplify subreg here, before we lose track of the
7688 original inner_mode. */
7689 new = simplify_subreg (GET_MODE (x), r,
7690 inner_mode, SUBREG_BYTE (x));
7691 if (new)
7692 return new;
7693 else
7694 SUBST (SUBREG_REG (x), r);
7697 return x;
7699 /* We don't have to handle SIGN_EXTEND here, because even in the
7700 case of replacing something with a modeless CONST_INT, a
7701 CONST_INT is already (supposed to be) a valid sign extension for
7702 its narrower mode, which implies it's already properly
7703 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7704 story is different. */
7705 else if (code == ZERO_EXTEND)
7707 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7708 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7710 if (XEXP (x, 0) != r)
7712 /* We must simplify the zero_extend here, before we lose
7713 track of the original inner_mode. */
7714 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7715 r, inner_mode);
7716 if (new)
7717 return new;
7718 else
7719 SUBST (XEXP (x, 0), r);
7722 return x;
7725 fmt = GET_RTX_FORMAT (code);
7726 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7728 if (fmt[i] == 'e')
7729 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7730 else if (fmt[i] == 'E')
7731 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7732 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7733 cond, reg, val));
7736 return x;
7739 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7740 assignment as a field assignment. */
7742 static int
7743 rtx_equal_for_field_assignment_p (x, y)
7744 rtx x;
7745 rtx y;
7747 if (x == y || rtx_equal_p (x, y))
7748 return 1;
7750 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7751 return 0;
7753 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7754 Note that all SUBREGs of MEM are paradoxical; otherwise they
7755 would have been rewritten. */
7756 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7757 && GET_CODE (SUBREG_REG (y)) == MEM
7758 && rtx_equal_p (SUBREG_REG (y),
7759 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7760 return 1;
7762 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7763 && GET_CODE (SUBREG_REG (x)) == MEM
7764 && rtx_equal_p (SUBREG_REG (x),
7765 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7766 return 1;
7768 /* We used to see if get_last_value of X and Y were the same but that's
7769 not correct. In one direction, we'll cause the assignment to have
7770 the wrong destination and in the case, we'll import a register into this
7771 insn that might have already have been dead. So fail if none of the
7772 above cases are true. */
7773 return 0;
7776 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7777 Return that assignment if so.
7779 We only handle the most common cases. */
7781 static rtx
7782 make_field_assignment (x)
7783 rtx x;
7785 rtx dest = SET_DEST (x);
7786 rtx src = SET_SRC (x);
7787 rtx assign;
7788 rtx rhs, lhs;
7789 HOST_WIDE_INT c1;
7790 HOST_WIDE_INT pos;
7791 unsigned HOST_WIDE_INT len;
7792 rtx other;
7793 enum machine_mode mode;
7795 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7796 a clear of a one-bit field. We will have changed it to
7797 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7798 for a SUBREG. */
7800 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7801 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7802 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7803 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7805 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7806 1, 1, 1, 0);
7807 if (assign != 0)
7808 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7809 return x;
7812 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7813 && subreg_lowpart_p (XEXP (src, 0))
7814 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7815 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7816 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7817 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7818 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7820 assign = make_extraction (VOIDmode, dest, 0,
7821 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7822 1, 1, 1, 0);
7823 if (assign != 0)
7824 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7825 return x;
7828 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7829 one-bit field. */
7830 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7831 && XEXP (XEXP (src, 0), 0) == const1_rtx
7832 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7834 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7835 1, 1, 1, 0);
7836 if (assign != 0)
7837 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7838 return x;
7841 /* The other case we handle is assignments into a constant-position
7842 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7843 a mask that has all one bits except for a group of zero bits and
7844 OTHER is known to have zeros where C1 has ones, this is such an
7845 assignment. Compute the position and length from C1. Shift OTHER
7846 to the appropriate position, force it to the required mode, and
7847 make the extraction. Check for the AND in both operands. */
7849 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7850 return x;
7852 rhs = expand_compound_operation (XEXP (src, 0));
7853 lhs = expand_compound_operation (XEXP (src, 1));
7855 if (GET_CODE (rhs) == AND
7856 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7857 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7858 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7859 else if (GET_CODE (lhs) == AND
7860 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7861 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7862 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7863 else
7864 return x;
7866 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7867 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7868 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7869 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7870 return x;
7872 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7873 if (assign == 0)
7874 return x;
7876 /* The mode to use for the source is the mode of the assignment, or of
7877 what is inside a possible STRICT_LOW_PART. */
7878 mode = (GET_CODE (assign) == STRICT_LOW_PART
7879 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7881 /* Shift OTHER right POS places and make it the source, restricting it
7882 to the proper length and mode. */
7884 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7885 GET_MODE (src), other, pos),
7886 mode,
7887 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7888 ? ~(unsigned HOST_WIDE_INT) 0
7889 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7890 dest, 0);
7892 /* If SRC is masked by an AND that does not make a difference in
7893 the value being stored, strip it. */
7894 if (GET_CODE (assign) == ZERO_EXTRACT
7895 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7896 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7897 && GET_CODE (src) == AND
7898 && GET_CODE (XEXP (src, 1)) == CONST_INT
7899 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7900 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7901 src = XEXP (src, 0);
7903 return gen_rtx_SET (VOIDmode, assign, src);
7906 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7907 if so. */
7909 static rtx
7910 apply_distributive_law (x)
7911 rtx x;
7913 enum rtx_code code = GET_CODE (x);
7914 rtx lhs, rhs, other;
7915 rtx tem;
7916 enum rtx_code inner_code;
7918 /* Distributivity is not true for floating point.
7919 It can change the value. So don't do it.
7920 -- rms and moshier@world.std.com. */
7921 if (FLOAT_MODE_P (GET_MODE (x)))
7922 return x;
7924 /* The outer operation can only be one of the following: */
7925 if (code != IOR && code != AND && code != XOR
7926 && code != PLUS && code != MINUS)
7927 return x;
7929 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7931 /* If either operand is a primitive we can't do anything, so get out
7932 fast. */
7933 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7934 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7935 return x;
7937 lhs = expand_compound_operation (lhs);
7938 rhs = expand_compound_operation (rhs);
7939 inner_code = GET_CODE (lhs);
7940 if (inner_code != GET_CODE (rhs))
7941 return x;
7943 /* See if the inner and outer operations distribute. */
7944 switch (inner_code)
7946 case LSHIFTRT:
7947 case ASHIFTRT:
7948 case AND:
7949 case IOR:
7950 /* These all distribute except over PLUS. */
7951 if (code == PLUS || code == MINUS)
7952 return x;
7953 break;
7955 case MULT:
7956 if (code != PLUS && code != MINUS)
7957 return x;
7958 break;
7960 case ASHIFT:
7961 /* This is also a multiply, so it distributes over everything. */
7962 break;
7964 case SUBREG:
7965 /* Non-paradoxical SUBREGs distributes over all operations, provided
7966 the inner modes and byte offsets are the same, this is an extraction
7967 of a low-order part, we don't convert an fp operation to int or
7968 vice versa, and we would not be converting a single-word
7969 operation into a multi-word operation. The latter test is not
7970 required, but it prevents generating unneeded multi-word operations.
7971 Some of the previous tests are redundant given the latter test, but
7972 are retained because they are required for correctness.
7974 We produce the result slightly differently in this case. */
7976 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7977 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7978 || ! subreg_lowpart_p (lhs)
7979 || (GET_MODE_CLASS (GET_MODE (lhs))
7980 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7981 || (GET_MODE_SIZE (GET_MODE (lhs))
7982 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7983 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7984 return x;
7986 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7987 SUBREG_REG (lhs), SUBREG_REG (rhs));
7988 return gen_lowpart_for_combine (GET_MODE (x), tem);
7990 default:
7991 return x;
7994 /* Set LHS and RHS to the inner operands (A and B in the example
7995 above) and set OTHER to the common operand (C in the example).
7996 These is only one way to do this unless the inner operation is
7997 commutative. */
7998 if (GET_RTX_CLASS (inner_code) == 'c'
7999 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8000 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8001 else if (GET_RTX_CLASS (inner_code) == 'c'
8002 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8003 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8004 else if (GET_RTX_CLASS (inner_code) == 'c'
8005 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8006 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8007 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8008 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8009 else
8010 return x;
8012 /* Form the new inner operation, seeing if it simplifies first. */
8013 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
8015 /* There is one exception to the general way of distributing:
8016 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
8017 if (code == XOR && inner_code == IOR)
8019 inner_code = AND;
8020 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8023 /* We may be able to continuing distributing the result, so call
8024 ourselves recursively on the inner operation before forming the
8025 outer operation, which we return. */
8026 return gen_binary (inner_code, GET_MODE (x),
8027 apply_distributive_law (tem), other);
8030 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8031 in MODE.
8033 Return an equivalent form, if different from X. Otherwise, return X. If
8034 X is zero, we are to always construct the equivalent form. */
8036 static rtx
8037 simplify_and_const_int (x, mode, varop, constop)
8038 rtx x;
8039 enum machine_mode mode;
8040 rtx varop;
8041 unsigned HOST_WIDE_INT constop;
8043 unsigned HOST_WIDE_INT nonzero;
8044 int i;
8046 /* Simplify VAROP knowing that we will be only looking at some of the
8047 bits in it.
8049 Note by passing in CONSTOP, we guarantee that the bits not set in
8050 CONSTOP are not significant and will never be examined. We must
8051 ensure that is the case by explicitly masking out those bits
8052 before returning. */
8053 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8055 /* If VAROP is a CLOBBER, we will fail so return it. */
8056 if (GET_CODE (varop) == CLOBBER)
8057 return varop;
8059 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8060 to VAROP and return the new constant. */
8061 if (GET_CODE (varop) == CONST_INT)
8062 return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
8064 /* See what bits may be nonzero in VAROP. Unlike the general case of
8065 a call to nonzero_bits, here we don't care about bits outside
8066 MODE. */
8068 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8070 /* Turn off all bits in the constant that are known to already be zero.
8071 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8072 which is tested below. */
8074 constop &= nonzero;
8076 /* If we don't have any bits left, return zero. */
8077 if (constop == 0)
8078 return const0_rtx;
8080 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8081 a power of two, we can replace this with an ASHIFT. */
8082 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8083 && (i = exact_log2 (constop)) >= 0)
8084 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8086 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8087 or XOR, then try to apply the distributive law. This may eliminate
8088 operations if either branch can be simplified because of the AND.
8089 It may also make some cases more complex, but those cases probably
8090 won't match a pattern either with or without this. */
8092 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8093 return
8094 gen_lowpart_for_combine
8095 (mode,
8096 apply_distributive_law
8097 (gen_binary (GET_CODE (varop), GET_MODE (varop),
8098 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8099 XEXP (varop, 0), constop),
8100 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8101 XEXP (varop, 1), constop))));
8103 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8104 the AND and see if one of the operands simplifies to zero. If so, we
8105 may eliminate it. */
8107 if (GET_CODE (varop) == PLUS
8108 && exact_log2 (constop + 1) >= 0)
8110 rtx o0, o1;
8112 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8113 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8114 if (o0 == const0_rtx)
8115 return o1;
8116 if (o1 == const0_rtx)
8117 return o0;
8120 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
8121 if we already had one (just check for the simplest cases). */
8122 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8123 && GET_MODE (XEXP (x, 0)) == mode
8124 && SUBREG_REG (XEXP (x, 0)) == varop)
8125 varop = XEXP (x, 0);
8126 else
8127 varop = gen_lowpart_for_combine (mode, varop);
8129 /* If we can't make the SUBREG, try to return what we were given. */
8130 if (GET_CODE (varop) == CLOBBER)
8131 return x ? x : varop;
8133 /* If we are only masking insignificant bits, return VAROP. */
8134 if (constop == nonzero)
8135 x = varop;
8136 else
8138 /* Otherwise, return an AND. */
8139 constop = trunc_int_for_mode (constop, mode);
8140 /* See how much, if any, of X we can use. */
8141 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8142 x = gen_binary (AND, mode, varop, GEN_INT (constop));
8144 else
8146 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8147 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8148 SUBST (XEXP (x, 1), GEN_INT (constop));
8150 SUBST (XEXP (x, 0), varop);
8154 return x;
8157 #define nonzero_bits_with_known(X, MODE) \
8158 cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
8160 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8161 It avoids exponential behavior in nonzero_bits1 when X has
8162 identical subexpressions on the first or the second level. */
8164 static unsigned HOST_WIDE_INT
8165 cached_nonzero_bits (x, mode, known_x, known_mode, known_ret)
8166 rtx x;
8167 enum machine_mode mode;
8168 rtx known_x;
8169 enum machine_mode known_mode;
8170 unsigned HOST_WIDE_INT known_ret;
8172 if (x == known_x && mode == known_mode)
8173 return known_ret;
8175 /* Try to find identical subexpressions. If found call
8176 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8177 precomputed value for the subexpression as KNOWN_RET. */
8179 if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8180 || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8182 rtx x0 = XEXP (x, 0);
8183 rtx x1 = XEXP (x, 1);
8185 /* Check the first level. */
8186 if (x0 == x1)
8187 return nonzero_bits1 (x, mode, x0, mode,
8188 nonzero_bits_with_known (x0, mode));
8190 /* Check the second level. */
8191 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8192 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8193 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8194 return nonzero_bits1 (x, mode, x1, mode,
8195 nonzero_bits_with_known (x1, mode));
8197 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8198 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8199 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8200 return nonzero_bits1 (x, mode, x0, mode,
8201 nonzero_bits_with_known (x0, mode));
8204 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8207 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8208 We don't let nonzero_bits recur into num_sign_bit_copies, because that
8209 is less useful. We can't allow both, because that results in exponential
8210 run time recursion. There is a nullstone testcase that triggered
8211 this. This macro avoids accidental uses of num_sign_bit_copies. */
8212 #define cached_num_sign_bit_copies()
8214 /* Given an expression, X, compute which bits in X can be nonzero.
8215 We don't care about bits outside of those defined in MODE.
8217 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8218 a shift, AND, or zero_extract, we can do better. */
8220 static unsigned HOST_WIDE_INT
8221 nonzero_bits1 (x, mode, known_x, known_mode, known_ret)
8222 rtx x;
8223 enum machine_mode mode;
8224 rtx known_x;
8225 enum machine_mode known_mode;
8226 unsigned HOST_WIDE_INT known_ret;
8228 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8229 unsigned HOST_WIDE_INT inner_nz;
8230 enum rtx_code code;
8231 unsigned int mode_width = GET_MODE_BITSIZE (mode);
8232 rtx tem;
8234 /* For floating-point values, assume all bits are needed. */
8235 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8236 return nonzero;
8238 /* If X is wider than MODE, use its mode instead. */
8239 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8241 mode = GET_MODE (x);
8242 nonzero = GET_MODE_MASK (mode);
8243 mode_width = GET_MODE_BITSIZE (mode);
8246 if (mode_width > HOST_BITS_PER_WIDE_INT)
8247 /* Our only callers in this case look for single bit values. So
8248 just return the mode mask. Those tests will then be false. */
8249 return nonzero;
8251 #ifndef WORD_REGISTER_OPERATIONS
8252 /* If MODE is wider than X, but both are a single word for both the host
8253 and target machines, we can compute this from which bits of the
8254 object might be nonzero in its own mode, taking into account the fact
8255 that on many CISC machines, accessing an object in a wider mode
8256 causes the high-order bits to become undefined. So they are
8257 not known to be zero. */
8259 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8260 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8261 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8262 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8264 nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8265 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8266 return nonzero;
8268 #endif
8270 code = GET_CODE (x);
8271 switch (code)
8273 case REG:
8274 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8275 /* If pointers extend unsigned and this is a pointer in Pmode, say that
8276 all the bits above ptr_mode are known to be zero. */
8277 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8278 && REG_POINTER (x))
8279 nonzero &= GET_MODE_MASK (ptr_mode);
8280 #endif
8282 /* Include declared information about alignment of pointers. */
8283 /* ??? We don't properly preserve REG_POINTER changes across
8284 pointer-to-integer casts, so we can't trust it except for
8285 things that we know must be pointers. See execute/960116-1.c. */
8286 if ((x == stack_pointer_rtx
8287 || x == frame_pointer_rtx
8288 || x == arg_pointer_rtx)
8289 && REGNO_POINTER_ALIGN (REGNO (x)))
8291 unsigned HOST_WIDE_INT alignment
8292 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8294 #ifdef PUSH_ROUNDING
8295 /* If PUSH_ROUNDING is defined, it is possible for the
8296 stack to be momentarily aligned only to that amount,
8297 so we pick the least alignment. */
8298 if (x == stack_pointer_rtx && PUSH_ARGS)
8299 alignment = MIN (PUSH_ROUNDING (1), alignment);
8300 #endif
8302 nonzero &= ~(alignment - 1);
8305 /* If X is a register whose nonzero bits value is current, use it.
8306 Otherwise, if X is a register whose value we can find, use that
8307 value. Otherwise, use the previously-computed global nonzero bits
8308 for this register. */
8310 if (reg_last_set_value[REGNO (x)] != 0
8311 && (reg_last_set_mode[REGNO (x)] == mode
8312 || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8313 && GET_MODE_CLASS (mode) == MODE_INT))
8314 && (reg_last_set_label[REGNO (x)] == label_tick
8315 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8316 && REG_N_SETS (REGNO (x)) == 1
8317 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8318 REGNO (x))))
8319 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8320 return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8322 tem = get_last_value (x);
8324 if (tem)
8326 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8327 /* If X is narrower than MODE and TEM is a non-negative
8328 constant that would appear negative in the mode of X,
8329 sign-extend it for use in reg_nonzero_bits because some
8330 machines (maybe most) will actually do the sign-extension
8331 and this is the conservative approach.
8333 ??? For 2.5, try to tighten up the MD files in this regard
8334 instead of this kludge. */
8336 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8337 && GET_CODE (tem) == CONST_INT
8338 && INTVAL (tem) > 0
8339 && 0 != (INTVAL (tem)
8340 & ((HOST_WIDE_INT) 1
8341 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8342 tem = GEN_INT (INTVAL (tem)
8343 | ((HOST_WIDE_INT) (-1)
8344 << GET_MODE_BITSIZE (GET_MODE (x))));
8345 #endif
8346 return nonzero_bits_with_known (tem, mode) & nonzero;
8348 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8350 unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8352 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8353 /* We don't know anything about the upper bits. */
8354 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8355 return nonzero & mask;
8357 else
8358 return nonzero;
8360 case CONST_INT:
8361 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8362 /* If X is negative in MODE, sign-extend the value. */
8363 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8364 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8365 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8366 #endif
8368 return INTVAL (x);
8370 case MEM:
8371 #ifdef LOAD_EXTEND_OP
8372 /* In many, if not most, RISC machines, reading a byte from memory
8373 zeros the rest of the register. Noticing that fact saves a lot
8374 of extra zero-extends. */
8375 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8376 nonzero &= GET_MODE_MASK (GET_MODE (x));
8377 #endif
8378 break;
8380 case EQ: case NE:
8381 case UNEQ: case LTGT:
8382 case GT: case GTU: case UNGT:
8383 case LT: case LTU: case UNLT:
8384 case GE: case GEU: case UNGE:
8385 case LE: case LEU: case UNLE:
8386 case UNORDERED: case ORDERED:
8388 /* If this produces an integer result, we know which bits are set.
8389 Code here used to clear bits outside the mode of X, but that is
8390 now done above. */
8392 if (GET_MODE_CLASS (mode) == MODE_INT
8393 && mode_width <= HOST_BITS_PER_WIDE_INT)
8394 nonzero = STORE_FLAG_VALUE;
8395 break;
8397 case NEG:
8398 #if 0
8399 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8400 and num_sign_bit_copies. */
8401 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8402 == GET_MODE_BITSIZE (GET_MODE (x)))
8403 nonzero = 1;
8404 #endif
8406 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8407 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8408 break;
8410 case ABS:
8411 #if 0
8412 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8413 and num_sign_bit_copies. */
8414 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8415 == GET_MODE_BITSIZE (GET_MODE (x)))
8416 nonzero = 1;
8417 #endif
8418 break;
8420 case TRUNCATE:
8421 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8422 & GET_MODE_MASK (mode));
8423 break;
8425 case ZERO_EXTEND:
8426 nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8427 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8428 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8429 break;
8431 case SIGN_EXTEND:
8432 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8433 Otherwise, show all the bits in the outer mode but not the inner
8434 may be nonzero. */
8435 inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8436 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8438 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8439 if (inner_nz
8440 & (((HOST_WIDE_INT) 1
8441 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8442 inner_nz |= (GET_MODE_MASK (mode)
8443 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8446 nonzero &= inner_nz;
8447 break;
8449 case AND:
8450 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8451 & nonzero_bits_with_known (XEXP (x, 1), mode));
8452 break;
8454 case XOR: case IOR:
8455 case UMIN: case UMAX: case SMIN: case SMAX:
8457 unsigned HOST_WIDE_INT nonzero0 =
8458 nonzero_bits_with_known (XEXP (x, 0), mode);
8460 /* Don't call nonzero_bits for the second time if it cannot change
8461 anything. */
8462 if ((nonzero & nonzero0) != nonzero)
8463 nonzero &= (nonzero0
8464 | nonzero_bits_with_known (XEXP (x, 1), mode));
8466 break;
8468 case PLUS: case MINUS:
8469 case MULT:
8470 case DIV: case UDIV:
8471 case MOD: case UMOD:
8472 /* We can apply the rules of arithmetic to compute the number of
8473 high- and low-order zero bits of these operations. We start by
8474 computing the width (position of the highest-order nonzero bit)
8475 and the number of low-order zero bits for each value. */
8477 unsigned HOST_WIDE_INT nz0 =
8478 nonzero_bits_with_known (XEXP (x, 0), mode);
8479 unsigned HOST_WIDE_INT nz1 =
8480 nonzero_bits_with_known (XEXP (x, 1), mode);
8481 int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8482 int width0 = floor_log2 (nz0) + 1;
8483 int width1 = floor_log2 (nz1) + 1;
8484 int low0 = floor_log2 (nz0 & -nz0);
8485 int low1 = floor_log2 (nz1 & -nz1);
8486 HOST_WIDE_INT op0_maybe_minusp
8487 = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8488 HOST_WIDE_INT op1_maybe_minusp
8489 = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8490 unsigned int result_width = mode_width;
8491 int result_low = 0;
8493 switch (code)
8495 case PLUS:
8496 result_width = MAX (width0, width1) + 1;
8497 result_low = MIN (low0, low1);
8498 break;
8499 case MINUS:
8500 result_low = MIN (low0, low1);
8501 break;
8502 case MULT:
8503 result_width = width0 + width1;
8504 result_low = low0 + low1;
8505 break;
8506 case DIV:
8507 if (width1 == 0)
8508 break;
8509 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8510 result_width = width0;
8511 break;
8512 case UDIV:
8513 if (width1 == 0)
8514 break;
8515 result_width = width0;
8516 break;
8517 case MOD:
8518 if (width1 == 0)
8519 break;
8520 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8521 result_width = MIN (width0, width1);
8522 result_low = MIN (low0, low1);
8523 break;
8524 case UMOD:
8525 if (width1 == 0)
8526 break;
8527 result_width = MIN (width0, width1);
8528 result_low = MIN (low0, low1);
8529 break;
8530 default:
8531 abort ();
8534 if (result_width < mode_width)
8535 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8537 if (result_low > 0)
8538 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8540 #ifdef POINTERS_EXTEND_UNSIGNED
8541 /* If pointers extend unsigned and this is an addition or subtraction
8542 to a pointer in Pmode, all the bits above ptr_mode are known to be
8543 zero. */
8544 if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8545 && (code == PLUS || code == MINUS)
8546 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8547 nonzero &= GET_MODE_MASK (ptr_mode);
8548 #endif
8550 break;
8552 case ZERO_EXTRACT:
8553 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8554 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8555 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8556 break;
8558 case SUBREG:
8559 /* If this is a SUBREG formed for a promoted variable that has
8560 been zero-extended, we know that at least the high-order bits
8561 are zero, though others might be too. */
8563 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8564 nonzero = (GET_MODE_MASK (GET_MODE (x))
8565 & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8567 /* If the inner mode is a single word for both the host and target
8568 machines, we can compute this from which bits of the inner
8569 object might be nonzero. */
8570 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8571 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8572 <= HOST_BITS_PER_WIDE_INT))
8574 nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8576 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8577 /* If this is a typical RISC machine, we only have to worry
8578 about the way loads are extended. */
8579 if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8580 ? (((nonzero
8581 & (((unsigned HOST_WIDE_INT) 1
8582 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8583 != 0))
8584 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8585 || GET_CODE (SUBREG_REG (x)) != MEM)
8586 #endif
8588 /* On many CISC machines, accessing an object in a wider mode
8589 causes the high-order bits to become undefined. So they are
8590 not known to be zero. */
8591 if (GET_MODE_SIZE (GET_MODE (x))
8592 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8593 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8594 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8597 break;
8599 case ASHIFTRT:
8600 case LSHIFTRT:
8601 case ASHIFT:
8602 case ROTATE:
8603 /* The nonzero bits are in two classes: any bits within MODE
8604 that aren't in GET_MODE (x) are always significant. The rest of the
8605 nonzero bits are those that are significant in the operand of
8606 the shift when shifted the appropriate number of bits. This
8607 shows that high-order bits are cleared by the right shift and
8608 low-order bits by left shifts. */
8609 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8610 && INTVAL (XEXP (x, 1)) >= 0
8611 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8613 enum machine_mode inner_mode = GET_MODE (x);
8614 unsigned int width = GET_MODE_BITSIZE (inner_mode);
8615 int count = INTVAL (XEXP (x, 1));
8616 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8617 unsigned HOST_WIDE_INT op_nonzero =
8618 nonzero_bits_with_known (XEXP (x, 0), mode);
8619 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8620 unsigned HOST_WIDE_INT outer = 0;
8622 if (mode_width > width)
8623 outer = (op_nonzero & nonzero & ~mode_mask);
8625 if (code == LSHIFTRT)
8626 inner >>= count;
8627 else if (code == ASHIFTRT)
8629 inner >>= count;
8631 /* If the sign bit may have been nonzero before the shift, we
8632 need to mark all the places it could have been copied to
8633 by the shift as possibly nonzero. */
8634 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8635 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8637 else if (code == ASHIFT)
8638 inner <<= count;
8639 else
8640 inner = ((inner << (count % width)
8641 | (inner >> (width - (count % width)))) & mode_mask);
8643 nonzero &= (outer | inner);
8645 break;
8647 case FFS:
8648 case POPCOUNT:
8649 /* This is at most the number of bits in the mode. */
8650 nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8651 break;
8653 case CLZ:
8654 /* If CLZ has a known value at zero, then the nonzero bits are
8655 that value, plus the number of bits in the mode minus one. */
8656 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8657 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8658 else
8659 nonzero = -1;
8660 break;
8662 case CTZ:
8663 /* If CTZ has a known value at zero, then the nonzero bits are
8664 that value, plus the number of bits in the mode minus one. */
8665 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8666 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8667 else
8668 nonzero = -1;
8669 break;
8671 case PARITY:
8672 nonzero = 1;
8673 break;
8675 case IF_THEN_ELSE:
8676 nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8677 | nonzero_bits_with_known (XEXP (x, 2), mode));
8678 break;
8680 default:
8681 break;
8684 return nonzero;
8687 /* See the macro definition above. */
8688 #undef cached_num_sign_bit_copies
8690 #define num_sign_bit_copies_with_known(X, M) \
8691 cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8693 /* The function cached_num_sign_bit_copies is a wrapper around
8694 num_sign_bit_copies1. It avoids exponential behavior in
8695 num_sign_bit_copies1 when X has identical subexpressions on the
8696 first or the second level. */
8698 static unsigned int
8699 cached_num_sign_bit_copies (x, mode, known_x, known_mode, known_ret)
8700 rtx x;
8701 enum machine_mode mode;
8702 rtx known_x;
8703 enum machine_mode known_mode;
8704 unsigned int known_ret;
8706 if (x == known_x && mode == known_mode)
8707 return known_ret;
8709 /* Try to find identical subexpressions. If found call
8710 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8711 the precomputed value for the subexpression as KNOWN_RET. */
8713 if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8714 || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8716 rtx x0 = XEXP (x, 0);
8717 rtx x1 = XEXP (x, 1);
8719 /* Check the first level. */
8720 if (x0 == x1)
8721 return
8722 num_sign_bit_copies1 (x, mode, x0, mode,
8723 num_sign_bit_copies_with_known (x0, mode));
8725 /* Check the second level. */
8726 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8727 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8728 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8729 return
8730 num_sign_bit_copies1 (x, mode, x1, mode,
8731 num_sign_bit_copies_with_known (x1, mode));
8733 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8734 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8735 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8736 return
8737 num_sign_bit_copies1 (x, mode, x0, mode,
8738 num_sign_bit_copies_with_known (x0, mode));
8741 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8744 /* Return the number of bits at the high-order end of X that are known to
8745 be equal to the sign bit. X will be used in mode MODE; if MODE is
8746 VOIDmode, X will be used in its own mode. The returned value will always
8747 be between 1 and the number of bits in MODE. */
8749 static unsigned int
8750 num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret)
8751 rtx x;
8752 enum machine_mode mode;
8753 rtx known_x;
8754 enum machine_mode known_mode;
8755 unsigned int known_ret;
8757 enum rtx_code code = GET_CODE (x);
8758 unsigned int bitwidth;
8759 int num0, num1, result;
8760 unsigned HOST_WIDE_INT nonzero;
8761 rtx tem;
8763 /* If we weren't given a mode, use the mode of X. If the mode is still
8764 VOIDmode, we don't know anything. Likewise if one of the modes is
8765 floating-point. */
8767 if (mode == VOIDmode)
8768 mode = GET_MODE (x);
8770 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8771 return 1;
8773 bitwidth = GET_MODE_BITSIZE (mode);
8775 /* For a smaller object, just ignore the high bits. */
8776 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8778 num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8779 return MAX (1,
8780 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8783 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8785 #ifndef WORD_REGISTER_OPERATIONS
8786 /* If this machine does not do all register operations on the entire
8787 register and MODE is wider than the mode of X, we can say nothing
8788 at all about the high-order bits. */
8789 return 1;
8790 #else
8791 /* Likewise on machines that do, if the mode of the object is smaller
8792 than a word and loads of that size don't sign extend, we can say
8793 nothing about the high order bits. */
8794 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8795 #ifdef LOAD_EXTEND_OP
8796 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8797 #endif
8799 return 1;
8800 #endif
8803 switch (code)
8805 case REG:
8807 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8808 /* If pointers extend signed and this is a pointer in Pmode, say that
8809 all the bits above ptr_mode are known to be sign bit copies. */
8810 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8811 && REG_POINTER (x))
8812 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8813 #endif
8815 if (reg_last_set_value[REGNO (x)] != 0
8816 && reg_last_set_mode[REGNO (x)] == mode
8817 && (reg_last_set_label[REGNO (x)] == label_tick
8818 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8819 && REG_N_SETS (REGNO (x)) == 1
8820 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8821 REGNO (x))))
8822 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8823 return reg_last_set_sign_bit_copies[REGNO (x)];
8825 tem = get_last_value (x);
8826 if (tem != 0)
8827 return num_sign_bit_copies_with_known (tem, mode);
8829 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8830 && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8831 return reg_sign_bit_copies[REGNO (x)];
8832 break;
8834 case MEM:
8835 #ifdef LOAD_EXTEND_OP
8836 /* Some RISC machines sign-extend all loads of smaller than a word. */
8837 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8838 return MAX (1, ((int) bitwidth
8839 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8840 #endif
8841 break;
8843 case CONST_INT:
8844 /* If the constant is negative, take its 1's complement and remask.
8845 Then see how many zero bits we have. */
8846 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8847 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8848 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8849 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8851 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8853 case SUBREG:
8854 /* If this is a SUBREG for a promoted object that is sign-extended
8855 and we are looking at it in a wider mode, we know that at least the
8856 high-order bits are known to be sign bit copies. */
8858 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8860 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8861 return MAX ((int) bitwidth
8862 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8863 num0);
8866 /* For a smaller object, just ignore the high bits. */
8867 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8869 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8870 return MAX (1, (num0
8871 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8872 - bitwidth)));
8875 #ifdef WORD_REGISTER_OPERATIONS
8876 #ifdef LOAD_EXTEND_OP
8877 /* For paradoxical SUBREGs on machines where all register operations
8878 affect the entire register, just look inside. Note that we are
8879 passing MODE to the recursive call, so the number of sign bit copies
8880 will remain relative to that mode, not the inner mode. */
8882 /* This works only if loads sign extend. Otherwise, if we get a
8883 reload for the inner part, it may be loaded from the stack, and
8884 then we lose all sign bit copies that existed before the store
8885 to the stack. */
8887 if ((GET_MODE_SIZE (GET_MODE (x))
8888 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8889 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8890 && GET_CODE (SUBREG_REG (x)) == MEM)
8891 return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8892 #endif
8893 #endif
8894 break;
8896 case SIGN_EXTRACT:
8897 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8898 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8899 break;
8901 case SIGN_EXTEND:
8902 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8903 + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8905 case TRUNCATE:
8906 /* For a smaller object, just ignore the high bits. */
8907 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8908 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8909 - bitwidth)));
8911 case NOT:
8912 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8914 case ROTATE: case ROTATERT:
8915 /* If we are rotating left by a number of bits less than the number
8916 of sign bit copies, we can just subtract that amount from the
8917 number. */
8918 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8919 && INTVAL (XEXP (x, 1)) >= 0
8920 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8922 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8923 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8924 : (int) bitwidth - INTVAL (XEXP (x, 1))));
8926 break;
8928 case NEG:
8929 /* In general, this subtracts one sign bit copy. But if the value
8930 is known to be positive, the number of sign bit copies is the
8931 same as that of the input. Finally, if the input has just one bit
8932 that might be nonzero, all the bits are copies of the sign bit. */
8933 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8934 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8935 return num0 > 1 ? num0 - 1 : 1;
8937 nonzero = nonzero_bits (XEXP (x, 0), mode);
8938 if (nonzero == 1)
8939 return bitwidth;
8941 if (num0 > 1
8942 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8943 num0--;
8945 return num0;
8947 case IOR: case AND: case XOR:
8948 case SMIN: case SMAX: case UMIN: case UMAX:
8949 /* Logical operations will preserve the number of sign-bit copies.
8950 MIN and MAX operations always return one of the operands. */
8951 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8952 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8953 return MIN (num0, num1);
8955 case PLUS: case MINUS:
8956 /* For addition and subtraction, we can have a 1-bit carry. However,
8957 if we are subtracting 1 from a positive number, there will not
8958 be such a carry. Furthermore, if the positive number is known to
8959 be 0 or 1, we know the result is either -1 or 0. */
8961 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8962 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8964 nonzero = nonzero_bits (XEXP (x, 0), mode);
8965 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8966 return (nonzero == 1 || nonzero == 0 ? bitwidth
8967 : bitwidth - floor_log2 (nonzero) - 1);
8970 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8971 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8972 result = MAX (1, MIN (num0, num1) - 1);
8974 #ifdef POINTERS_EXTEND_UNSIGNED
8975 /* If pointers extend signed and this is an addition or subtraction
8976 to a pointer in Pmode, all the bits above ptr_mode are known to be
8977 sign bit copies. */
8978 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8979 && (code == PLUS || code == MINUS)
8980 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8981 result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8982 - GET_MODE_BITSIZE (ptr_mode) + 1),
8983 result);
8984 #endif
8985 return result;
8987 case MULT:
8988 /* The number of bits of the product is the sum of the number of
8989 bits of both terms. However, unless one of the terms if known
8990 to be positive, we must allow for an additional bit since negating
8991 a negative number can remove one sign bit copy. */
8993 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8994 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8996 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8997 if (result > 0
8998 && (bitwidth > HOST_BITS_PER_WIDE_INT
8999 || (((nonzero_bits (XEXP (x, 0), mode)
9000 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
9001 && ((nonzero_bits (XEXP (x, 1), mode)
9002 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
9003 result--;
9005 return MAX (1, result);
9007 case UDIV:
9008 /* The result must be <= the first operand. If the first operand
9009 has the high bit set, we know nothing about the number of sign
9010 bit copies. */
9011 if (bitwidth > HOST_BITS_PER_WIDE_INT)
9012 return 1;
9013 else if ((nonzero_bits (XEXP (x, 0), mode)
9014 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
9015 return 1;
9016 else
9017 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9019 case UMOD:
9020 /* The result must be <= the second operand. */
9021 return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9023 case DIV:
9024 /* Similar to unsigned division, except that we have to worry about
9025 the case where the divisor is negative, in which case we have
9026 to add 1. */
9027 result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9028 if (result > 1
9029 && (bitwidth > HOST_BITS_PER_WIDE_INT
9030 || (nonzero_bits (XEXP (x, 1), mode)
9031 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
9032 result--;
9034 return result;
9036 case MOD:
9037 result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9038 if (result > 1
9039 && (bitwidth > HOST_BITS_PER_WIDE_INT
9040 || (nonzero_bits (XEXP (x, 1), mode)
9041 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
9042 result--;
9044 return result;
9046 case ASHIFTRT:
9047 /* Shifts by a constant add to the number of bits equal to the
9048 sign bit. */
9049 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9050 if (GET_CODE (XEXP (x, 1)) == CONST_INT
9051 && INTVAL (XEXP (x, 1)) > 0)
9052 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
9054 return num0;
9056 case ASHIFT:
9057 /* Left shifts destroy copies. */
9058 if (GET_CODE (XEXP (x, 1)) != CONST_INT
9059 || INTVAL (XEXP (x, 1)) < 0
9060 || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
9061 return 1;
9063 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9064 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
9066 case IF_THEN_ELSE:
9067 num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9068 num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
9069 return MIN (num0, num1);
9071 case EQ: case NE: case GE: case GT: case LE: case LT:
9072 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
9073 case GEU: case GTU: case LEU: case LTU:
9074 case UNORDERED: case ORDERED:
9075 /* If the constant is negative, take its 1's complement and remask.
9076 Then see how many zero bits we have. */
9077 nonzero = STORE_FLAG_VALUE;
9078 if (bitwidth <= HOST_BITS_PER_WIDE_INT
9079 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
9080 nonzero = (~nonzero) & GET_MODE_MASK (mode);
9082 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
9083 break;
9085 default:
9086 break;
9089 /* If we haven't been able to figure it out by one of the above rules,
9090 see if some of the high-order bits are known to be zero. If so,
9091 count those bits and return one less than that amount. If we can't
9092 safely compute the mask for this mode, always return BITWIDTH. */
9094 if (bitwidth > HOST_BITS_PER_WIDE_INT)
9095 return 1;
9097 nonzero = nonzero_bits (x, mode);
9098 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
9099 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
9102 /* Return the number of "extended" bits there are in X, when interpreted
9103 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9104 unsigned quantities, this is the number of high-order zero bits.
9105 For signed quantities, this is the number of copies of the sign bit
9106 minus 1. In both case, this function returns the number of "spare"
9107 bits. For example, if two quantities for which this function returns
9108 at least 1 are added, the addition is known not to overflow.
9110 This function will always return 0 unless called during combine, which
9111 implies that it must be called from a define_split. */
9113 unsigned int
9114 extended_count (x, mode, unsignedp)
9115 rtx x;
9116 enum machine_mode mode;
9117 int unsignedp;
9119 if (nonzero_sign_valid == 0)
9120 return 0;
9122 return (unsignedp
9123 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9124 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9125 - floor_log2 (nonzero_bits (x, mode)))
9126 : 0)
9127 : num_sign_bit_copies (x, mode) - 1);
9130 /* This function is called from `simplify_shift_const' to merge two
9131 outer operations. Specifically, we have already found that we need
9132 to perform operation *POP0 with constant *PCONST0 at the outermost
9133 position. We would now like to also perform OP1 with constant CONST1
9134 (with *POP0 being done last).
9136 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9137 the resulting operation. *PCOMP_P is set to 1 if we would need to
9138 complement the innermost operand, otherwise it is unchanged.
9140 MODE is the mode in which the operation will be done. No bits outside
9141 the width of this mode matter. It is assumed that the width of this mode
9142 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9144 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
9145 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9146 result is simply *PCONST0.
9148 If the resulting operation cannot be expressed as one operation, we
9149 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9151 static int
9152 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
9153 enum rtx_code *pop0;
9154 HOST_WIDE_INT *pconst0;
9155 enum rtx_code op1;
9156 HOST_WIDE_INT const1;
9157 enum machine_mode mode;
9158 int *pcomp_p;
9160 enum rtx_code op0 = *pop0;
9161 HOST_WIDE_INT const0 = *pconst0;
9163 const0 &= GET_MODE_MASK (mode);
9164 const1 &= GET_MODE_MASK (mode);
9166 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9167 if (op0 == AND)
9168 const1 &= const0;
9170 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
9171 if OP0 is SET. */
9173 if (op1 == NIL || op0 == SET)
9174 return 1;
9176 else if (op0 == NIL)
9177 op0 = op1, const0 = const1;
9179 else if (op0 == op1)
9181 switch (op0)
9183 case AND:
9184 const0 &= const1;
9185 break;
9186 case IOR:
9187 const0 |= const1;
9188 break;
9189 case XOR:
9190 const0 ^= const1;
9191 break;
9192 case PLUS:
9193 const0 += const1;
9194 break;
9195 case NEG:
9196 op0 = NIL;
9197 break;
9198 default:
9199 break;
9203 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9204 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9205 return 0;
9207 /* If the two constants aren't the same, we can't do anything. The
9208 remaining six cases can all be done. */
9209 else if (const0 != const1)
9210 return 0;
9212 else
9213 switch (op0)
9215 case IOR:
9216 if (op1 == AND)
9217 /* (a & b) | b == b */
9218 op0 = SET;
9219 else /* op1 == XOR */
9220 /* (a ^ b) | b == a | b */
9222 break;
9224 case XOR:
9225 if (op1 == AND)
9226 /* (a & b) ^ b == (~a) & b */
9227 op0 = AND, *pcomp_p = 1;
9228 else /* op1 == IOR */
9229 /* (a | b) ^ b == a & ~b */
9230 op0 = AND, const0 = ~const0;
9231 break;
9233 case AND:
9234 if (op1 == IOR)
9235 /* (a | b) & b == b */
9236 op0 = SET;
9237 else /* op1 == XOR */
9238 /* (a ^ b) & b) == (~a) & b */
9239 *pcomp_p = 1;
9240 break;
9241 default:
9242 break;
9245 /* Check for NO-OP cases. */
9246 const0 &= GET_MODE_MASK (mode);
9247 if (const0 == 0
9248 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9249 op0 = NIL;
9250 else if (const0 == 0 && op0 == AND)
9251 op0 = SET;
9252 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9253 && op0 == AND)
9254 op0 = NIL;
9256 /* ??? Slightly redundant with the above mask, but not entirely.
9257 Moving this above means we'd have to sign-extend the mode mask
9258 for the final test. */
9259 const0 = trunc_int_for_mode (const0, mode);
9261 *pop0 = op0;
9262 *pconst0 = const0;
9264 return 1;
9267 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9268 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
9269 that we started with.
9271 The shift is normally computed in the widest mode we find in VAROP, as
9272 long as it isn't a different number of words than RESULT_MODE. Exceptions
9273 are ASHIFTRT and ROTATE, which are always done in their original mode, */
9275 static rtx
9276 simplify_shift_const (x, code, result_mode, varop, orig_count)
9277 rtx x;
9278 enum rtx_code code;
9279 enum machine_mode result_mode;
9280 rtx varop;
9281 int orig_count;
9283 enum rtx_code orig_code = code;
9284 unsigned int count;
9285 int signed_count;
9286 enum machine_mode mode = result_mode;
9287 enum machine_mode shift_mode, tmode;
9288 unsigned int mode_words
9289 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9290 /* We form (outer_op (code varop count) (outer_const)). */
9291 enum rtx_code outer_op = NIL;
9292 HOST_WIDE_INT outer_const = 0;
9293 rtx const_rtx;
9294 int complement_p = 0;
9295 rtx new;
9297 /* Make sure and truncate the "natural" shift on the way in. We don't
9298 want to do this inside the loop as it makes it more difficult to
9299 combine shifts. */
9300 #ifdef SHIFT_COUNT_TRUNCATED
9301 if (SHIFT_COUNT_TRUNCATED)
9302 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9303 #endif
9305 /* If we were given an invalid count, don't do anything except exactly
9306 what was requested. */
9308 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9310 if (x)
9311 return x;
9313 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9316 count = orig_count;
9318 /* Unless one of the branches of the `if' in this loop does a `continue',
9319 we will `break' the loop after the `if'. */
9321 while (count != 0)
9323 /* If we have an operand of (clobber (const_int 0)), just return that
9324 value. */
9325 if (GET_CODE (varop) == CLOBBER)
9326 return varop;
9328 /* If we discovered we had to complement VAROP, leave. Making a NOT
9329 here would cause an infinite loop. */
9330 if (complement_p)
9331 break;
9333 /* Convert ROTATERT to ROTATE. */
9334 if (code == ROTATERT)
9336 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9337 code = ROTATE;
9338 if (VECTOR_MODE_P (result_mode))
9339 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9340 else
9341 count = bitsize - count;
9344 /* We need to determine what mode we will do the shift in. If the
9345 shift is a right shift or a ROTATE, we must always do it in the mode
9346 it was originally done in. Otherwise, we can do it in MODE, the
9347 widest mode encountered. */
9348 shift_mode
9349 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9350 ? result_mode : mode);
9352 /* Handle cases where the count is greater than the size of the mode
9353 minus 1. For ASHIFT, use the size minus one as the count (this can
9354 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9355 take the count modulo the size. For other shifts, the result is
9356 zero.
9358 Since these shifts are being produced by the compiler by combining
9359 multiple operations, each of which are defined, we know what the
9360 result is supposed to be. */
9362 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9364 if (code == ASHIFTRT)
9365 count = GET_MODE_BITSIZE (shift_mode) - 1;
9366 else if (code == ROTATE || code == ROTATERT)
9367 count %= GET_MODE_BITSIZE (shift_mode);
9368 else
9370 /* We can't simply return zero because there may be an
9371 outer op. */
9372 varop = const0_rtx;
9373 count = 0;
9374 break;
9378 /* An arithmetic right shift of a quantity known to be -1 or 0
9379 is a no-op. */
9380 if (code == ASHIFTRT
9381 && (num_sign_bit_copies (varop, shift_mode)
9382 == GET_MODE_BITSIZE (shift_mode)))
9384 count = 0;
9385 break;
9388 /* If we are doing an arithmetic right shift and discarding all but
9389 the sign bit copies, this is equivalent to doing a shift by the
9390 bitsize minus one. Convert it into that shift because it will often
9391 allow other simplifications. */
9393 if (code == ASHIFTRT
9394 && (count + num_sign_bit_copies (varop, shift_mode)
9395 >= GET_MODE_BITSIZE (shift_mode)))
9396 count = GET_MODE_BITSIZE (shift_mode) - 1;
9398 /* We simplify the tests below and elsewhere by converting
9399 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9400 `make_compound_operation' will convert it to an ASHIFTRT for
9401 those machines (such as VAX) that don't have an LSHIFTRT. */
9402 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9403 && code == ASHIFTRT
9404 && ((nonzero_bits (varop, shift_mode)
9405 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9406 == 0))
9407 code = LSHIFTRT;
9409 if (code == LSHIFTRT
9410 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9411 && !(nonzero_bits (varop, shift_mode) >> count))
9412 varop = const0_rtx;
9413 if (code == ASHIFT
9414 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9415 && !((nonzero_bits (varop, shift_mode) << count)
9416 & GET_MODE_MASK (shift_mode)))
9417 varop = const0_rtx;
9419 switch (GET_CODE (varop))
9421 case SIGN_EXTEND:
9422 case ZERO_EXTEND:
9423 case SIGN_EXTRACT:
9424 case ZERO_EXTRACT:
9425 new = expand_compound_operation (varop);
9426 if (new != varop)
9428 varop = new;
9429 continue;
9431 break;
9433 case MEM:
9434 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9435 minus the width of a smaller mode, we can do this with a
9436 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9437 if ((code == ASHIFTRT || code == LSHIFTRT)
9438 && ! mode_dependent_address_p (XEXP (varop, 0))
9439 && ! MEM_VOLATILE_P (varop)
9440 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9441 MODE_INT, 1)) != BLKmode)
9443 new = adjust_address_nv (varop, tmode,
9444 BYTES_BIG_ENDIAN ? 0
9445 : count / BITS_PER_UNIT);
9447 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9448 : ZERO_EXTEND, mode, new);
9449 count = 0;
9450 continue;
9452 break;
9454 case USE:
9455 /* Similar to the case above, except that we can only do this if
9456 the resulting mode is the same as that of the underlying
9457 MEM and adjust the address depending on the *bits* endianness
9458 because of the way that bit-field extract insns are defined. */
9459 if ((code == ASHIFTRT || code == LSHIFTRT)
9460 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9461 MODE_INT, 1)) != BLKmode
9462 && tmode == GET_MODE (XEXP (varop, 0)))
9464 if (BITS_BIG_ENDIAN)
9465 new = XEXP (varop, 0);
9466 else
9468 new = copy_rtx (XEXP (varop, 0));
9469 SUBST (XEXP (new, 0),
9470 plus_constant (XEXP (new, 0),
9471 count / BITS_PER_UNIT));
9474 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9475 : ZERO_EXTEND, mode, new);
9476 count = 0;
9477 continue;
9479 break;
9481 case SUBREG:
9482 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9483 the same number of words as what we've seen so far. Then store
9484 the widest mode in MODE. */
9485 if (subreg_lowpart_p (varop)
9486 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9487 > GET_MODE_SIZE (GET_MODE (varop)))
9488 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9489 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9490 == mode_words)
9492 varop = SUBREG_REG (varop);
9493 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9494 mode = GET_MODE (varop);
9495 continue;
9497 break;
9499 case MULT:
9500 /* Some machines use MULT instead of ASHIFT because MULT
9501 is cheaper. But it is still better on those machines to
9502 merge two shifts into one. */
9503 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9504 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9506 varop
9507 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9508 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9509 continue;
9511 break;
9513 case UDIV:
9514 /* Similar, for when divides are cheaper. */
9515 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9516 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9518 varop
9519 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9520 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9521 continue;
9523 break;
9525 case ASHIFTRT:
9526 /* If we are extracting just the sign bit of an arithmetic
9527 right shift, that shift is not needed. However, the sign
9528 bit of a wider mode may be different from what would be
9529 interpreted as the sign bit in a narrower mode, so, if
9530 the result is narrower, don't discard the shift. */
9531 if (code == LSHIFTRT
9532 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9533 && (GET_MODE_BITSIZE (result_mode)
9534 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9536 varop = XEXP (varop, 0);
9537 continue;
9540 /* ... fall through ... */
9542 case LSHIFTRT:
9543 case ASHIFT:
9544 case ROTATE:
9545 /* Here we have two nested shifts. The result is usually the
9546 AND of a new shift with a mask. We compute the result below. */
9547 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9548 && INTVAL (XEXP (varop, 1)) >= 0
9549 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9550 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9551 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9553 enum rtx_code first_code = GET_CODE (varop);
9554 unsigned int first_count = INTVAL (XEXP (varop, 1));
9555 unsigned HOST_WIDE_INT mask;
9556 rtx mask_rtx;
9558 /* We have one common special case. We can't do any merging if
9559 the inner code is an ASHIFTRT of a smaller mode. However, if
9560 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9561 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9562 we can convert it to
9563 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9564 This simplifies certain SIGN_EXTEND operations. */
9565 if (code == ASHIFT && first_code == ASHIFTRT
9566 && count == (unsigned int)
9567 (GET_MODE_BITSIZE (result_mode)
9568 - GET_MODE_BITSIZE (GET_MODE (varop))))
9570 /* C3 has the low-order C1 bits zero. */
9572 mask = (GET_MODE_MASK (mode)
9573 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9575 varop = simplify_and_const_int (NULL_RTX, result_mode,
9576 XEXP (varop, 0), mask);
9577 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9578 varop, count);
9579 count = first_count;
9580 code = ASHIFTRT;
9581 continue;
9584 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9585 than C1 high-order bits equal to the sign bit, we can convert
9586 this to either an ASHIFT or an ASHIFTRT depending on the
9587 two counts.
9589 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9591 if (code == ASHIFTRT && first_code == ASHIFT
9592 && GET_MODE (varop) == shift_mode
9593 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9594 > first_count))
9596 varop = XEXP (varop, 0);
9598 signed_count = count - first_count;
9599 if (signed_count < 0)
9600 count = -signed_count, code = ASHIFT;
9601 else
9602 count = signed_count;
9604 continue;
9607 /* There are some cases we can't do. If CODE is ASHIFTRT,
9608 we can only do this if FIRST_CODE is also ASHIFTRT.
9610 We can't do the case when CODE is ROTATE and FIRST_CODE is
9611 ASHIFTRT.
9613 If the mode of this shift is not the mode of the outer shift,
9614 we can't do this if either shift is a right shift or ROTATE.
9616 Finally, we can't do any of these if the mode is too wide
9617 unless the codes are the same.
9619 Handle the case where the shift codes are the same
9620 first. */
9622 if (code == first_code)
9624 if (GET_MODE (varop) != result_mode
9625 && (code == ASHIFTRT || code == LSHIFTRT
9626 || code == ROTATE))
9627 break;
9629 count += first_count;
9630 varop = XEXP (varop, 0);
9631 continue;
9634 if (code == ASHIFTRT
9635 || (code == ROTATE && first_code == ASHIFTRT)
9636 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9637 || (GET_MODE (varop) != result_mode
9638 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9639 || first_code == ROTATE
9640 || code == ROTATE)))
9641 break;
9643 /* To compute the mask to apply after the shift, shift the
9644 nonzero bits of the inner shift the same way the
9645 outer shift will. */
9647 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9649 mask_rtx
9650 = simplify_binary_operation (code, result_mode, mask_rtx,
9651 GEN_INT (count));
9653 /* Give up if we can't compute an outer operation to use. */
9654 if (mask_rtx == 0
9655 || GET_CODE (mask_rtx) != CONST_INT
9656 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9657 INTVAL (mask_rtx),
9658 result_mode, &complement_p))
9659 break;
9661 /* If the shifts are in the same direction, we add the
9662 counts. Otherwise, we subtract them. */
9663 signed_count = count;
9664 if ((code == ASHIFTRT || code == LSHIFTRT)
9665 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9666 signed_count += first_count;
9667 else
9668 signed_count -= first_count;
9670 /* If COUNT is positive, the new shift is usually CODE,
9671 except for the two exceptions below, in which case it is
9672 FIRST_CODE. If the count is negative, FIRST_CODE should
9673 always be used */
9674 if (signed_count > 0
9675 && ((first_code == ROTATE && code == ASHIFT)
9676 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9677 code = first_code, count = signed_count;
9678 else if (signed_count < 0)
9679 code = first_code, count = -signed_count;
9680 else
9681 count = signed_count;
9683 varop = XEXP (varop, 0);
9684 continue;
9687 /* If we have (A << B << C) for any shift, we can convert this to
9688 (A << C << B). This wins if A is a constant. Only try this if
9689 B is not a constant. */
9691 else if (GET_CODE (varop) == code
9692 && GET_CODE (XEXP (varop, 1)) != CONST_INT
9693 && 0 != (new
9694 = simplify_binary_operation (code, mode,
9695 XEXP (varop, 0),
9696 GEN_INT (count))))
9698 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9699 count = 0;
9700 continue;
9702 break;
9704 case NOT:
9705 /* Make this fit the case below. */
9706 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9707 GEN_INT (GET_MODE_MASK (mode)));
9708 continue;
9710 case IOR:
9711 case AND:
9712 case XOR:
9713 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9714 with C the size of VAROP - 1 and the shift is logical if
9715 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9716 we have an (le X 0) operation. If we have an arithmetic shift
9717 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9718 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9720 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9721 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9722 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9723 && (code == LSHIFTRT || code == ASHIFTRT)
9724 && count == (unsigned int)
9725 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9726 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9728 count = 0;
9729 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9730 const0_rtx);
9732 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9733 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9735 continue;
9738 /* If we have (shift (logical)), move the logical to the outside
9739 to allow it to possibly combine with another logical and the
9740 shift to combine with another shift. This also canonicalizes to
9741 what a ZERO_EXTRACT looks like. Also, some machines have
9742 (and (shift)) insns. */
9744 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9745 && (new = simplify_binary_operation (code, result_mode,
9746 XEXP (varop, 1),
9747 GEN_INT (count))) != 0
9748 && GET_CODE (new) == CONST_INT
9749 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9750 INTVAL (new), result_mode, &complement_p))
9752 varop = XEXP (varop, 0);
9753 continue;
9756 /* If we can't do that, try to simplify the shift in each arm of the
9757 logical expression, make a new logical expression, and apply
9758 the inverse distributive law. */
9760 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9761 XEXP (varop, 0), count);
9762 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9763 XEXP (varop, 1), count);
9765 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9766 varop = apply_distributive_law (varop);
9768 count = 0;
9770 break;
9772 case EQ:
9773 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9774 says that the sign bit can be tested, FOO has mode MODE, C is
9775 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9776 that may be nonzero. */
9777 if (code == LSHIFTRT
9778 && XEXP (varop, 1) == const0_rtx
9779 && GET_MODE (XEXP (varop, 0)) == result_mode
9780 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9781 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9782 && ((STORE_FLAG_VALUE
9783 & ((HOST_WIDE_INT) 1
9784 < (GET_MODE_BITSIZE (result_mode) - 1))))
9785 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9786 && merge_outer_ops (&outer_op, &outer_const, XOR,
9787 (HOST_WIDE_INT) 1, result_mode,
9788 &complement_p))
9790 varop = XEXP (varop, 0);
9791 count = 0;
9792 continue;
9794 break;
9796 case NEG:
9797 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9798 than the number of bits in the mode is equivalent to A. */
9799 if (code == LSHIFTRT
9800 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9801 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9803 varop = XEXP (varop, 0);
9804 count = 0;
9805 continue;
9808 /* NEG commutes with ASHIFT since it is multiplication. Move the
9809 NEG outside to allow shifts to combine. */
9810 if (code == ASHIFT
9811 && merge_outer_ops (&outer_op, &outer_const, NEG,
9812 (HOST_WIDE_INT) 0, result_mode,
9813 &complement_p))
9815 varop = XEXP (varop, 0);
9816 continue;
9818 break;
9820 case PLUS:
9821 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9822 is one less than the number of bits in the mode is
9823 equivalent to (xor A 1). */
9824 if (code == LSHIFTRT
9825 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9826 && XEXP (varop, 1) == constm1_rtx
9827 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9828 && merge_outer_ops (&outer_op, &outer_const, XOR,
9829 (HOST_WIDE_INT) 1, result_mode,
9830 &complement_p))
9832 count = 0;
9833 varop = XEXP (varop, 0);
9834 continue;
9837 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9838 that might be nonzero in BAR are those being shifted out and those
9839 bits are known zero in FOO, we can replace the PLUS with FOO.
9840 Similarly in the other operand order. This code occurs when
9841 we are computing the size of a variable-size array. */
9843 if ((code == ASHIFTRT || code == LSHIFTRT)
9844 && count < HOST_BITS_PER_WIDE_INT
9845 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9846 && (nonzero_bits (XEXP (varop, 1), result_mode)
9847 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9849 varop = XEXP (varop, 0);
9850 continue;
9852 else if ((code == ASHIFTRT || code == LSHIFTRT)
9853 && count < HOST_BITS_PER_WIDE_INT
9854 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9855 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9856 >> count)
9857 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9858 & nonzero_bits (XEXP (varop, 1),
9859 result_mode)))
9861 varop = XEXP (varop, 1);
9862 continue;
9865 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9866 if (code == ASHIFT
9867 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9868 && (new = simplify_binary_operation (ASHIFT, result_mode,
9869 XEXP (varop, 1),
9870 GEN_INT (count))) != 0
9871 && GET_CODE (new) == CONST_INT
9872 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9873 INTVAL (new), result_mode, &complement_p))
9875 varop = XEXP (varop, 0);
9876 continue;
9878 break;
9880 case MINUS:
9881 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9882 with C the size of VAROP - 1 and the shift is logical if
9883 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9884 we have a (gt X 0) operation. If the shift is arithmetic with
9885 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9886 we have a (neg (gt X 0)) operation. */
9888 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9889 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9890 && count == (unsigned int)
9891 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9892 && (code == LSHIFTRT || code == ASHIFTRT)
9893 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9894 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9895 == count
9896 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9898 count = 0;
9899 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9900 const0_rtx);
9902 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9903 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9905 continue;
9907 break;
9909 case TRUNCATE:
9910 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9911 if the truncate does not affect the value. */
9912 if (code == LSHIFTRT
9913 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9914 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9915 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9916 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9917 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9919 rtx varop_inner = XEXP (varop, 0);
9921 varop_inner
9922 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9923 XEXP (varop_inner, 0),
9924 GEN_INT
9925 (count + INTVAL (XEXP (varop_inner, 1))));
9926 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9927 count = 0;
9928 continue;
9930 break;
9932 default:
9933 break;
9936 break;
9939 /* We need to determine what mode to do the shift in. If the shift is
9940 a right shift or ROTATE, we must always do it in the mode it was
9941 originally done in. Otherwise, we can do it in MODE, the widest mode
9942 encountered. The code we care about is that of the shift that will
9943 actually be done, not the shift that was originally requested. */
9944 shift_mode
9945 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9946 ? result_mode : mode);
9948 /* We have now finished analyzing the shift. The result should be
9949 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9950 OUTER_OP is non-NIL, it is an operation that needs to be applied
9951 to the result of the shift. OUTER_CONST is the relevant constant,
9952 but we must turn off all bits turned off in the shift.
9954 If we were passed a value for X, see if we can use any pieces of
9955 it. If not, make new rtx. */
9957 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9958 && GET_CODE (XEXP (x, 1)) == CONST_INT
9959 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9960 const_rtx = XEXP (x, 1);
9961 else
9962 const_rtx = GEN_INT (count);
9964 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9965 && GET_MODE (XEXP (x, 0)) == shift_mode
9966 && SUBREG_REG (XEXP (x, 0)) == varop)
9967 varop = XEXP (x, 0);
9968 else if (GET_MODE (varop) != shift_mode)
9969 varop = gen_lowpart_for_combine (shift_mode, varop);
9971 /* If we can't make the SUBREG, try to return what we were given. */
9972 if (GET_CODE (varop) == CLOBBER)
9973 return x ? x : varop;
9975 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9976 if (new != 0)
9977 x = new;
9978 else
9979 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9981 /* If we have an outer operation and we just made a shift, it is
9982 possible that we could have simplified the shift were it not
9983 for the outer operation. So try to do the simplification
9984 recursively. */
9986 if (outer_op != NIL && GET_CODE (x) == code
9987 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9988 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9989 INTVAL (XEXP (x, 1)));
9991 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9992 turn off all the bits that the shift would have turned off. */
9993 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9994 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9995 GET_MODE_MASK (result_mode) >> orig_count);
9997 /* Do the remainder of the processing in RESULT_MODE. */
9998 x = gen_lowpart_for_combine (result_mode, x);
10000 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10001 operation. */
10002 if (complement_p)
10003 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10005 if (outer_op != NIL)
10007 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10008 outer_const = trunc_int_for_mode (outer_const, result_mode);
10010 if (outer_op == AND)
10011 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10012 else if (outer_op == SET)
10013 /* This means that we have determined that the result is
10014 equivalent to a constant. This should be rare. */
10015 x = GEN_INT (outer_const);
10016 else if (GET_RTX_CLASS (outer_op) == '1')
10017 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10018 else
10019 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
10022 return x;
10025 /* Like recog, but we receive the address of a pointer to a new pattern.
10026 We try to match the rtx that the pointer points to.
10027 If that fails, we may try to modify or replace the pattern,
10028 storing the replacement into the same pointer object.
10030 Modifications include deletion or addition of CLOBBERs.
10032 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10033 the CLOBBERs are placed.
10035 The value is the final insn code from the pattern ultimately matched,
10036 or -1. */
10038 static int
10039 recog_for_combine (pnewpat, insn, pnotes)
10040 rtx *pnewpat;
10041 rtx insn;
10042 rtx *pnotes;
10044 rtx pat = *pnewpat;
10045 int insn_code_number;
10046 int num_clobbers_to_add = 0;
10047 int i;
10048 rtx notes = 0;
10049 rtx dummy_insn;
10051 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10052 we use to indicate that something didn't match. If we find such a
10053 thing, force rejection. */
10054 if (GET_CODE (pat) == PARALLEL)
10055 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10056 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10057 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10058 return -1;
10060 /* *pnewpat does not have to be actual PATTERN (insn), so make a dummy
10061 instruction for pattern recognition. */
10062 dummy_insn = shallow_copy_rtx (insn);
10063 PATTERN (dummy_insn) = pat;
10064 REG_NOTES (dummy_insn) = 0;
10066 insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
10068 /* If it isn't, there is the possibility that we previously had an insn
10069 that clobbered some register as a side effect, but the combined
10070 insn doesn't need to do that. So try once more without the clobbers
10071 unless this represents an ASM insn. */
10073 if (insn_code_number < 0 && ! check_asm_operands (pat)
10074 && GET_CODE (pat) == PARALLEL)
10076 int pos;
10078 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10079 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10081 if (i != pos)
10082 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10083 pos++;
10086 SUBST_INT (XVECLEN (pat, 0), pos);
10088 if (pos == 1)
10089 pat = XVECEXP (pat, 0, 0);
10091 PATTERN (dummy_insn) = pat;
10092 insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
10095 /* Recognize all noop sets, these will be killed by followup pass. */
10096 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10097 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10099 /* If we had any clobbers to add, make a new pattern than contains
10100 them. Then check to make sure that all of them are dead. */
10101 if (num_clobbers_to_add)
10103 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10104 rtvec_alloc (GET_CODE (pat) == PARALLEL
10105 ? (XVECLEN (pat, 0)
10106 + num_clobbers_to_add)
10107 : num_clobbers_to_add + 1));
10109 if (GET_CODE (pat) == PARALLEL)
10110 for (i = 0; i < XVECLEN (pat, 0); i++)
10111 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10112 else
10113 XVECEXP (newpat, 0, 0) = pat;
10115 add_clobbers (newpat, insn_code_number);
10117 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10118 i < XVECLEN (newpat, 0); i++)
10120 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
10121 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10122 return -1;
10123 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
10124 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10126 pat = newpat;
10129 *pnewpat = pat;
10130 *pnotes = notes;
10132 return insn_code_number;
10135 /* Like gen_lowpart but for use by combine. In combine it is not possible
10136 to create any new pseudoregs. However, it is safe to create
10137 invalid memory addresses, because combine will try to recognize
10138 them and all they will do is make the combine attempt fail.
10140 If for some reason this cannot do its job, an rtx
10141 (clobber (const_int 0)) is returned.
10142 An insn containing that will not be recognized. */
10144 #undef gen_lowpart
10146 static rtx
10147 gen_lowpart_for_combine (mode, x)
10148 enum machine_mode mode;
10149 rtx x;
10151 rtx result;
10153 if (GET_MODE (x) == mode)
10154 return x;
10156 /* Return identity if this is a CONST or symbolic
10157 reference. */
10158 if (mode == Pmode
10159 && (GET_CODE (x) == CONST
10160 || GET_CODE (x) == SYMBOL_REF
10161 || GET_CODE (x) == LABEL_REF))
10162 return x;
10164 /* We can only support MODE being wider than a word if X is a
10165 constant integer or has a mode the same size. */
10167 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
10168 && ! ((GET_MODE (x) == VOIDmode
10169 && (GET_CODE (x) == CONST_INT
10170 || GET_CODE (x) == CONST_DOUBLE))
10171 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
10172 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10174 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10175 won't know what to do. So we will strip off the SUBREG here and
10176 process normally. */
10177 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
10179 x = SUBREG_REG (x);
10180 if (GET_MODE (x) == mode)
10181 return x;
10184 result = gen_lowpart_common (mode, x);
10185 #ifdef CANNOT_CHANGE_MODE_CLASS
10186 if (result != 0
10187 && GET_CODE (result) == SUBREG
10188 && GET_CODE (SUBREG_REG (result)) == REG
10189 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10190 bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10191 * MAX_MACHINE_MODE
10192 + GET_MODE (result));
10193 #endif
10195 if (result)
10196 return result;
10198 if (GET_CODE (x) == MEM)
10200 int offset = 0;
10202 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10203 address. */
10204 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10205 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10207 /* If we want to refer to something bigger than the original memref,
10208 generate a perverse subreg instead. That will force a reload
10209 of the original memref X. */
10210 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10211 return gen_rtx_SUBREG (mode, x, 0);
10213 if (WORDS_BIG_ENDIAN)
10214 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10215 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10217 if (BYTES_BIG_ENDIAN)
10219 /* Adjust the address so that the address-after-the-data is
10220 unchanged. */
10221 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10222 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10225 return adjust_address_nv (x, mode, offset);
10228 /* If X is a comparison operator, rewrite it in a new mode. This
10229 probably won't match, but may allow further simplifications. */
10230 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
10231 return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10233 /* If we couldn't simplify X any other way, just enclose it in a
10234 SUBREG. Normally, this SUBREG won't match, but some patterns may
10235 include an explicit SUBREG or we may simplify it further in combine. */
10236 else
10238 int offset = 0;
10239 rtx res;
10240 enum machine_mode sub_mode = GET_MODE (x);
10242 offset = subreg_lowpart_offset (mode, sub_mode);
10243 if (sub_mode == VOIDmode)
10245 sub_mode = int_mode_for_mode (mode);
10246 x = gen_lowpart_common (sub_mode, x);
10247 if (x == 0)
10248 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10250 res = simplify_gen_subreg (mode, x, sub_mode, offset);
10251 if (res)
10252 return res;
10253 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10257 /* These routines make binary and unary operations by first seeing if they
10258 fold; if not, a new expression is allocated. */
10260 static rtx
10261 gen_binary (code, mode, op0, op1)
10262 enum rtx_code code;
10263 enum machine_mode mode;
10264 rtx op0, op1;
10266 rtx result;
10267 rtx tem;
10269 if (GET_RTX_CLASS (code) == 'c'
10270 && swap_commutative_operands_p (op0, op1))
10271 tem = op0, op0 = op1, op1 = tem;
10273 if (GET_RTX_CLASS (code) == '<')
10275 enum machine_mode op_mode = GET_MODE (op0);
10277 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10278 just (REL_OP X Y). */
10279 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10281 op1 = XEXP (op0, 1);
10282 op0 = XEXP (op0, 0);
10283 op_mode = GET_MODE (op0);
10286 if (op_mode == VOIDmode)
10287 op_mode = GET_MODE (op1);
10288 result = simplify_relational_operation (code, op_mode, op0, op1);
10290 else
10291 result = simplify_binary_operation (code, mode, op0, op1);
10293 if (result)
10294 return result;
10296 /* Put complex operands first and constants second. */
10297 if (GET_RTX_CLASS (code) == 'c'
10298 && swap_commutative_operands_p (op0, op1))
10299 return gen_rtx_fmt_ee (code, mode, op1, op0);
10301 /* If we are turning off bits already known off in OP0, we need not do
10302 an AND. */
10303 else if (code == AND && GET_CODE (op1) == CONST_INT
10304 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10305 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10306 return op0;
10308 return gen_rtx_fmt_ee (code, mode, op0, op1);
10311 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10312 comparison code that will be tested.
10314 The result is a possibly different comparison code to use. *POP0 and
10315 *POP1 may be updated.
10317 It is possible that we might detect that a comparison is either always
10318 true or always false. However, we do not perform general constant
10319 folding in combine, so this knowledge isn't useful. Such tautologies
10320 should have been detected earlier. Hence we ignore all such cases. */
10322 static enum rtx_code
10323 simplify_comparison (code, pop0, pop1)
10324 enum rtx_code code;
10325 rtx *pop0;
10326 rtx *pop1;
10328 rtx op0 = *pop0;
10329 rtx op1 = *pop1;
10330 rtx tem, tem1;
10331 int i;
10332 enum machine_mode mode, tmode;
10334 /* Try a few ways of applying the same transformation to both operands. */
10335 while (1)
10337 #ifndef WORD_REGISTER_OPERATIONS
10338 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10339 so check specially. */
10340 if (code != GTU && code != GEU && code != LTU && code != LEU
10341 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10342 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10343 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10344 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10345 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10346 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10347 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10348 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10349 && GET_CODE (XEXP (op1, 1)) == CONST_INT
10350 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10351 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
10352 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
10353 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
10354 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
10355 && (INTVAL (XEXP (op0, 1))
10356 == (GET_MODE_BITSIZE (GET_MODE (op0))
10357 - (GET_MODE_BITSIZE
10358 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10360 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10361 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10363 #endif
10365 /* If both operands are the same constant shift, see if we can ignore the
10366 shift. We can if the shift is a rotate or if the bits shifted out of
10367 this shift are known to be zero for both inputs and if the type of
10368 comparison is compatible with the shift. */
10369 if (GET_CODE (op0) == GET_CODE (op1)
10370 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10371 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10372 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10373 && (code != GT && code != LT && code != GE && code != LE))
10374 || (GET_CODE (op0) == ASHIFTRT
10375 && (code != GTU && code != LTU
10376 && code != GEU && code != LEU)))
10377 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10378 && INTVAL (XEXP (op0, 1)) >= 0
10379 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10380 && XEXP (op0, 1) == XEXP (op1, 1))
10382 enum machine_mode mode = GET_MODE (op0);
10383 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10384 int shift_count = INTVAL (XEXP (op0, 1));
10386 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10387 mask &= (mask >> shift_count) << shift_count;
10388 else if (GET_CODE (op0) == ASHIFT)
10389 mask = (mask & (mask << shift_count)) >> shift_count;
10391 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10392 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10393 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10394 else
10395 break;
10398 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10399 SUBREGs are of the same mode, and, in both cases, the AND would
10400 be redundant if the comparison was done in the narrower mode,
10401 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10402 and the operand's possibly nonzero bits are 0xffffff01; in that case
10403 if we only care about QImode, we don't need the AND). This case
10404 occurs if the output mode of an scc insn is not SImode and
10405 STORE_FLAG_VALUE == 1 (e.g., the 386).
10407 Similarly, check for a case where the AND's are ZERO_EXTEND
10408 operations from some narrower mode even though a SUBREG is not
10409 present. */
10411 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10412 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10413 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10415 rtx inner_op0 = XEXP (op0, 0);
10416 rtx inner_op1 = XEXP (op1, 0);
10417 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10418 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10419 int changed = 0;
10421 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10422 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10423 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10424 && (GET_MODE (SUBREG_REG (inner_op0))
10425 == GET_MODE (SUBREG_REG (inner_op1)))
10426 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10427 <= HOST_BITS_PER_WIDE_INT)
10428 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10429 GET_MODE (SUBREG_REG (inner_op0)))))
10430 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10431 GET_MODE (SUBREG_REG (inner_op1))))))
10433 op0 = SUBREG_REG (inner_op0);
10434 op1 = SUBREG_REG (inner_op1);
10436 /* The resulting comparison is always unsigned since we masked
10437 off the original sign bit. */
10438 code = unsigned_condition (code);
10440 changed = 1;
10443 else if (c0 == c1)
10444 for (tmode = GET_CLASS_NARROWEST_MODE
10445 (GET_MODE_CLASS (GET_MODE (op0)));
10446 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10447 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10449 op0 = gen_lowpart_for_combine (tmode, inner_op0);
10450 op1 = gen_lowpart_for_combine (tmode, inner_op1);
10451 code = unsigned_condition (code);
10452 changed = 1;
10453 break;
10456 if (! changed)
10457 break;
10460 /* If both operands are NOT, we can strip off the outer operation
10461 and adjust the comparison code for swapped operands; similarly for
10462 NEG, except that this must be an equality comparison. */
10463 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10464 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10465 && (code == EQ || code == NE)))
10466 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10468 else
10469 break;
10472 /* If the first operand is a constant, swap the operands and adjust the
10473 comparison code appropriately, but don't do this if the second operand
10474 is already a constant integer. */
10475 if (swap_commutative_operands_p (op0, op1))
10477 tem = op0, op0 = op1, op1 = tem;
10478 code = swap_condition (code);
10481 /* We now enter a loop during which we will try to simplify the comparison.
10482 For the most part, we only are concerned with comparisons with zero,
10483 but some things may really be comparisons with zero but not start
10484 out looking that way. */
10486 while (GET_CODE (op1) == CONST_INT)
10488 enum machine_mode mode = GET_MODE (op0);
10489 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10490 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10491 int equality_comparison_p;
10492 int sign_bit_comparison_p;
10493 int unsigned_comparison_p;
10494 HOST_WIDE_INT const_op;
10496 /* We only want to handle integral modes. This catches VOIDmode,
10497 CCmode, and the floating-point modes. An exception is that we
10498 can handle VOIDmode if OP0 is a COMPARE or a comparison
10499 operation. */
10501 if (GET_MODE_CLASS (mode) != MODE_INT
10502 && ! (mode == VOIDmode
10503 && (GET_CODE (op0) == COMPARE
10504 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
10505 break;
10507 /* Get the constant we are comparing against and turn off all bits
10508 not on in our mode. */
10509 const_op = INTVAL (op1);
10510 if (mode != VOIDmode)
10511 const_op = trunc_int_for_mode (const_op, mode);
10512 op1 = GEN_INT (const_op);
10514 /* If we are comparing against a constant power of two and the value
10515 being compared can only have that single bit nonzero (e.g., it was
10516 `and'ed with that bit), we can replace this with a comparison
10517 with zero. */
10518 if (const_op
10519 && (code == EQ || code == NE || code == GE || code == GEU
10520 || code == LT || code == LTU)
10521 && mode_width <= HOST_BITS_PER_WIDE_INT
10522 && exact_log2 (const_op) >= 0
10523 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10525 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10526 op1 = const0_rtx, const_op = 0;
10529 /* Similarly, if we are comparing a value known to be either -1 or
10530 0 with -1, change it to the opposite comparison against zero. */
10532 if (const_op == -1
10533 && (code == EQ || code == NE || code == GT || code == LE
10534 || code == GEU || code == LTU)
10535 && num_sign_bit_copies (op0, mode) == mode_width)
10537 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10538 op1 = const0_rtx, const_op = 0;
10541 /* Do some canonicalizations based on the comparison code. We prefer
10542 comparisons against zero and then prefer equality comparisons.
10543 If we can reduce the size of a constant, we will do that too. */
10545 switch (code)
10547 case LT:
10548 /* < C is equivalent to <= (C - 1) */
10549 if (const_op > 0)
10551 const_op -= 1;
10552 op1 = GEN_INT (const_op);
10553 code = LE;
10554 /* ... fall through to LE case below. */
10556 else
10557 break;
10559 case LE:
10560 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10561 if (const_op < 0)
10563 const_op += 1;
10564 op1 = GEN_INT (const_op);
10565 code = LT;
10568 /* If we are doing a <= 0 comparison on a value known to have
10569 a zero sign bit, we can replace this with == 0. */
10570 else if (const_op == 0
10571 && mode_width <= HOST_BITS_PER_WIDE_INT
10572 && (nonzero_bits (op0, mode)
10573 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10574 code = EQ;
10575 break;
10577 case GE:
10578 /* >= C is equivalent to > (C - 1). */
10579 if (const_op > 0)
10581 const_op -= 1;
10582 op1 = GEN_INT (const_op);
10583 code = GT;
10584 /* ... fall through to GT below. */
10586 else
10587 break;
10589 case GT:
10590 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10591 if (const_op < 0)
10593 const_op += 1;
10594 op1 = GEN_INT (const_op);
10595 code = GE;
10598 /* If we are doing a > 0 comparison on a value known to have
10599 a zero sign bit, we can replace this with != 0. */
10600 else if (const_op == 0
10601 && mode_width <= HOST_BITS_PER_WIDE_INT
10602 && (nonzero_bits (op0, mode)
10603 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10604 code = NE;
10605 break;
10607 case LTU:
10608 /* < C is equivalent to <= (C - 1). */
10609 if (const_op > 0)
10611 const_op -= 1;
10612 op1 = GEN_INT (const_op);
10613 code = LEU;
10614 /* ... fall through ... */
10617 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10618 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10619 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10621 const_op = 0, op1 = const0_rtx;
10622 code = GE;
10623 break;
10625 else
10626 break;
10628 case LEU:
10629 /* unsigned <= 0 is equivalent to == 0 */
10630 if (const_op == 0)
10631 code = EQ;
10633 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10634 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10635 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10637 const_op = 0, op1 = const0_rtx;
10638 code = GE;
10640 break;
10642 case GEU:
10643 /* >= C is equivalent to < (C - 1). */
10644 if (const_op > 1)
10646 const_op -= 1;
10647 op1 = GEN_INT (const_op);
10648 code = GTU;
10649 /* ... fall through ... */
10652 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10653 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10654 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10656 const_op = 0, op1 = const0_rtx;
10657 code = LT;
10658 break;
10660 else
10661 break;
10663 case GTU:
10664 /* unsigned > 0 is equivalent to != 0 */
10665 if (const_op == 0)
10666 code = NE;
10668 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10669 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10670 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10672 const_op = 0, op1 = const0_rtx;
10673 code = LT;
10675 break;
10677 default:
10678 break;
10681 /* Compute some predicates to simplify code below. */
10683 equality_comparison_p = (code == EQ || code == NE);
10684 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10685 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10686 || code == GEU);
10688 /* If this is a sign bit comparison and we can do arithmetic in
10689 MODE, say that we will only be needing the sign bit of OP0. */
10690 if (sign_bit_comparison_p
10691 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10692 op0 = force_to_mode (op0, mode,
10693 ((HOST_WIDE_INT) 1
10694 << (GET_MODE_BITSIZE (mode) - 1)),
10695 NULL_RTX, 0);
10697 /* Now try cases based on the opcode of OP0. If none of the cases
10698 does a "continue", we exit this loop immediately after the
10699 switch. */
10701 switch (GET_CODE (op0))
10703 case ZERO_EXTRACT:
10704 /* If we are extracting a single bit from a variable position in
10705 a constant that has only a single bit set and are comparing it
10706 with zero, we can convert this into an equality comparison
10707 between the position and the location of the single bit. */
10709 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10710 && XEXP (op0, 1) == const1_rtx
10711 && equality_comparison_p && const_op == 0
10712 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10714 if (BITS_BIG_ENDIAN)
10716 enum machine_mode new_mode
10717 = mode_for_extraction (EP_extzv, 1);
10718 if (new_mode == MAX_MACHINE_MODE)
10719 i = BITS_PER_WORD - 1 - i;
10720 else
10722 mode = new_mode;
10723 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10727 op0 = XEXP (op0, 2);
10728 op1 = GEN_INT (i);
10729 const_op = i;
10731 /* Result is nonzero iff shift count is equal to I. */
10732 code = reverse_condition (code);
10733 continue;
10736 /* ... fall through ... */
10738 case SIGN_EXTRACT:
10739 tem = expand_compound_operation (op0);
10740 if (tem != op0)
10742 op0 = tem;
10743 continue;
10745 break;
10747 case NOT:
10748 /* If testing for equality, we can take the NOT of the constant. */
10749 if (equality_comparison_p
10750 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10752 op0 = XEXP (op0, 0);
10753 op1 = tem;
10754 continue;
10757 /* If just looking at the sign bit, reverse the sense of the
10758 comparison. */
10759 if (sign_bit_comparison_p)
10761 op0 = XEXP (op0, 0);
10762 code = (code == GE ? LT : GE);
10763 continue;
10765 break;
10767 case NEG:
10768 /* If testing for equality, we can take the NEG of the constant. */
10769 if (equality_comparison_p
10770 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10772 op0 = XEXP (op0, 0);
10773 op1 = tem;
10774 continue;
10777 /* The remaining cases only apply to comparisons with zero. */
10778 if (const_op != 0)
10779 break;
10781 /* When X is ABS or is known positive,
10782 (neg X) is < 0 if and only if X != 0. */
10784 if (sign_bit_comparison_p
10785 && (GET_CODE (XEXP (op0, 0)) == ABS
10786 || (mode_width <= HOST_BITS_PER_WIDE_INT
10787 && (nonzero_bits (XEXP (op0, 0), mode)
10788 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10790 op0 = XEXP (op0, 0);
10791 code = (code == LT ? NE : EQ);
10792 continue;
10795 /* If we have NEG of something whose two high-order bits are the
10796 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10797 if (num_sign_bit_copies (op0, mode) >= 2)
10799 op0 = XEXP (op0, 0);
10800 code = swap_condition (code);
10801 continue;
10803 break;
10805 case ROTATE:
10806 /* If we are testing equality and our count is a constant, we
10807 can perform the inverse operation on our RHS. */
10808 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10809 && (tem = simplify_binary_operation (ROTATERT, mode,
10810 op1, XEXP (op0, 1))) != 0)
10812 op0 = XEXP (op0, 0);
10813 op1 = tem;
10814 continue;
10817 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10818 a particular bit. Convert it to an AND of a constant of that
10819 bit. This will be converted into a ZERO_EXTRACT. */
10820 if (const_op == 0 && sign_bit_comparison_p
10821 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10822 && mode_width <= HOST_BITS_PER_WIDE_INT)
10824 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10825 ((HOST_WIDE_INT) 1
10826 << (mode_width - 1
10827 - INTVAL (XEXP (op0, 1)))));
10828 code = (code == LT ? NE : EQ);
10829 continue;
10832 /* Fall through. */
10834 case ABS:
10835 /* ABS is ignorable inside an equality comparison with zero. */
10836 if (const_op == 0 && equality_comparison_p)
10838 op0 = XEXP (op0, 0);
10839 continue;
10841 break;
10843 case SIGN_EXTEND:
10844 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10845 to (compare FOO CONST) if CONST fits in FOO's mode and we
10846 are either testing inequality or have an unsigned comparison
10847 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10848 if (! unsigned_comparison_p
10849 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10850 <= HOST_BITS_PER_WIDE_INT)
10851 && ((unsigned HOST_WIDE_INT) const_op
10852 < (((unsigned HOST_WIDE_INT) 1
10853 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10855 op0 = XEXP (op0, 0);
10856 continue;
10858 break;
10860 case SUBREG:
10861 /* Check for the case where we are comparing A - C1 with C2,
10862 both constants are smaller than 1/2 the maximum positive
10863 value in MODE, and the comparison is equality or unsigned.
10864 In that case, if A is either zero-extended to MODE or has
10865 sufficient sign bits so that the high-order bit in MODE
10866 is a copy of the sign in the inner mode, we can prove that it is
10867 safe to do the operation in the wider mode. This simplifies
10868 many range checks. */
10870 if (mode_width <= HOST_BITS_PER_WIDE_INT
10871 && subreg_lowpart_p (op0)
10872 && GET_CODE (SUBREG_REG (op0)) == PLUS
10873 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10874 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10875 && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10876 < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10877 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10878 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10879 GET_MODE (SUBREG_REG (op0)))
10880 & ~GET_MODE_MASK (mode))
10881 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10882 GET_MODE (SUBREG_REG (op0)))
10883 > (unsigned int)
10884 (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10885 - GET_MODE_BITSIZE (mode)))))
10887 op0 = SUBREG_REG (op0);
10888 continue;
10891 /* If the inner mode is narrower and we are extracting the low part,
10892 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10893 if (subreg_lowpart_p (op0)
10894 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10895 /* Fall through */ ;
10896 else
10897 break;
10899 /* ... fall through ... */
10901 case ZERO_EXTEND:
10902 if ((unsigned_comparison_p || equality_comparison_p)
10903 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10904 <= HOST_BITS_PER_WIDE_INT)
10905 && ((unsigned HOST_WIDE_INT) const_op
10906 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10908 op0 = XEXP (op0, 0);
10909 continue;
10911 break;
10913 case PLUS:
10914 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10915 this for equality comparisons due to pathological cases involving
10916 overflows. */
10917 if (equality_comparison_p
10918 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10919 op1, XEXP (op0, 1))))
10921 op0 = XEXP (op0, 0);
10922 op1 = tem;
10923 continue;
10926 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10927 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10928 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10930 op0 = XEXP (XEXP (op0, 0), 0);
10931 code = (code == LT ? EQ : NE);
10932 continue;
10934 break;
10936 case MINUS:
10937 /* We used to optimize signed comparisons against zero, but that
10938 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10939 arrive here as equality comparisons, or (GEU, LTU) are
10940 optimized away. No need to special-case them. */
10942 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10943 (eq B (minus A C)), whichever simplifies. We can only do
10944 this for equality comparisons due to pathological cases involving
10945 overflows. */
10946 if (equality_comparison_p
10947 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10948 XEXP (op0, 1), op1)))
10950 op0 = XEXP (op0, 0);
10951 op1 = tem;
10952 continue;
10955 if (equality_comparison_p
10956 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10957 XEXP (op0, 0), op1)))
10959 op0 = XEXP (op0, 1);
10960 op1 = tem;
10961 continue;
10964 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10965 of bits in X minus 1, is one iff X > 0. */
10966 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10967 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10968 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10969 == mode_width - 1
10970 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10972 op0 = XEXP (op0, 1);
10973 code = (code == GE ? LE : GT);
10974 continue;
10976 break;
10978 case XOR:
10979 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10980 if C is zero or B is a constant. */
10981 if (equality_comparison_p
10982 && 0 != (tem = simplify_binary_operation (XOR, mode,
10983 XEXP (op0, 1), op1)))
10985 op0 = XEXP (op0, 0);
10986 op1 = tem;
10987 continue;
10989 break;
10991 case EQ: case NE:
10992 case UNEQ: case LTGT:
10993 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10994 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10995 case UNORDERED: case ORDERED:
10996 /* We can't do anything if OP0 is a condition code value, rather
10997 than an actual data value. */
10998 if (const_op != 0
10999 || CC0_P (XEXP (op0, 0))
11000 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11001 break;
11003 /* Get the two operands being compared. */
11004 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11005 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11006 else
11007 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11009 /* Check for the cases where we simply want the result of the
11010 earlier test or the opposite of that result. */
11011 if (code == NE || code == EQ
11012 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11013 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11014 && (STORE_FLAG_VALUE
11015 & (((HOST_WIDE_INT) 1
11016 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11017 && (code == LT || code == GE)))
11019 enum rtx_code new_code;
11020 if (code == LT || code == NE)
11021 new_code = GET_CODE (op0);
11022 else
11023 new_code = combine_reversed_comparison_code (op0);
11025 if (new_code != UNKNOWN)
11027 code = new_code;
11028 op0 = tem;
11029 op1 = tem1;
11030 continue;
11033 break;
11035 case IOR:
11036 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11037 iff X <= 0. */
11038 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11039 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11040 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11042 op0 = XEXP (op0, 1);
11043 code = (code == GE ? GT : LE);
11044 continue;
11046 break;
11048 case AND:
11049 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11050 will be converted to a ZERO_EXTRACT later. */
11051 if (const_op == 0 && equality_comparison_p
11052 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11053 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11055 op0 = simplify_and_const_int
11056 (op0, mode, gen_rtx_LSHIFTRT (mode,
11057 XEXP (op0, 1),
11058 XEXP (XEXP (op0, 0), 1)),
11059 (HOST_WIDE_INT) 1);
11060 continue;
11063 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11064 zero and X is a comparison and C1 and C2 describe only bits set
11065 in STORE_FLAG_VALUE, we can compare with X. */
11066 if (const_op == 0 && equality_comparison_p
11067 && mode_width <= HOST_BITS_PER_WIDE_INT
11068 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11069 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11070 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11071 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11072 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11074 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11075 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11076 if ((~STORE_FLAG_VALUE & mask) == 0
11077 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
11078 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11079 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
11081 op0 = XEXP (XEXP (op0, 0), 0);
11082 continue;
11086 /* If we are doing an equality comparison of an AND of a bit equal
11087 to the sign bit, replace this with a LT or GE comparison of
11088 the underlying value. */
11089 if (equality_comparison_p
11090 && const_op == 0
11091 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11092 && mode_width <= HOST_BITS_PER_WIDE_INT
11093 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11094 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11096 op0 = XEXP (op0, 0);
11097 code = (code == EQ ? GE : LT);
11098 continue;
11101 /* If this AND operation is really a ZERO_EXTEND from a narrower
11102 mode, the constant fits within that mode, and this is either an
11103 equality or unsigned comparison, try to do this comparison in
11104 the narrower mode. */
11105 if ((equality_comparison_p || unsigned_comparison_p)
11106 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11107 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
11108 & GET_MODE_MASK (mode))
11109 + 1)) >= 0
11110 && const_op >> i == 0
11111 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
11113 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
11114 continue;
11117 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11118 fits in both M1 and M2 and the SUBREG is either paradoxical
11119 or represents the low part, permute the SUBREG and the AND
11120 and try again. */
11121 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11123 unsigned HOST_WIDE_INT c1;
11124 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11125 /* Require an integral mode, to avoid creating something like
11126 (AND:SF ...). */
11127 if (SCALAR_INT_MODE_P (tmode)
11128 /* It is unsafe to commute the AND into the SUBREG if the
11129 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11130 not defined. As originally written the upper bits
11131 have a defined value due to the AND operation.
11132 However, if we commute the AND inside the SUBREG then
11133 they no longer have defined values and the meaning of
11134 the code has been changed. */
11135 && (0
11136 #ifdef WORD_REGISTER_OPERATIONS
11137 || (mode_width > GET_MODE_BITSIZE (tmode)
11138 && mode_width <= BITS_PER_WORD)
11139 #endif
11140 || (mode_width <= GET_MODE_BITSIZE (tmode)
11141 && subreg_lowpart_p (XEXP (op0, 0))))
11142 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11143 && mode_width <= HOST_BITS_PER_WIDE_INT
11144 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11145 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11146 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11147 && c1 != mask
11148 && c1 != GET_MODE_MASK (tmode))
11150 op0 = gen_binary (AND, tmode,
11151 SUBREG_REG (XEXP (op0, 0)),
11152 gen_int_mode (c1, tmode));
11153 op0 = gen_lowpart_for_combine (mode, op0);
11154 continue;
11158 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11159 (eq (and (lshiftrt X) 1) 0). */
11160 if (const_op == 0 && equality_comparison_p
11161 && XEXP (op0, 1) == const1_rtx
11162 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11163 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == NOT)
11165 op0 = simplify_and_const_int
11166 (op0, mode,
11167 gen_rtx_LSHIFTRT (mode, XEXP (XEXP (XEXP (op0, 0), 0), 0),
11168 XEXP (XEXP (op0, 0), 1)),
11169 (HOST_WIDE_INT) 1);
11170 code = (code == NE ? EQ : NE);
11171 continue;
11173 break;
11175 case ASHIFT:
11176 /* If we have (compare (ashift FOO N) (const_int C)) and
11177 the high order N bits of FOO (N+1 if an inequality comparison)
11178 are known to be zero, we can do this by comparing FOO with C
11179 shifted right N bits so long as the low-order N bits of C are
11180 zero. */
11181 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11182 && INTVAL (XEXP (op0, 1)) >= 0
11183 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11184 < HOST_BITS_PER_WIDE_INT)
11185 && ((const_op
11186 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11187 && mode_width <= HOST_BITS_PER_WIDE_INT
11188 && (nonzero_bits (XEXP (op0, 0), mode)
11189 & ~(mask >> (INTVAL (XEXP (op0, 1))
11190 + ! equality_comparison_p))) == 0)
11192 /* We must perform a logical shift, not an arithmetic one,
11193 as we want the top N bits of C to be zero. */
11194 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11196 temp >>= INTVAL (XEXP (op0, 1));
11197 op1 = gen_int_mode (temp, mode);
11198 op0 = XEXP (op0, 0);
11199 continue;
11202 /* If we are doing a sign bit comparison, it means we are testing
11203 a particular bit. Convert it to the appropriate AND. */
11204 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11205 && mode_width <= HOST_BITS_PER_WIDE_INT)
11207 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11208 ((HOST_WIDE_INT) 1
11209 << (mode_width - 1
11210 - INTVAL (XEXP (op0, 1)))));
11211 code = (code == LT ? NE : EQ);
11212 continue;
11215 /* If this an equality comparison with zero and we are shifting
11216 the low bit to the sign bit, we can convert this to an AND of the
11217 low-order bit. */
11218 if (const_op == 0 && equality_comparison_p
11219 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11220 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11221 == mode_width - 1)
11223 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11224 (HOST_WIDE_INT) 1);
11225 continue;
11227 break;
11229 case ASHIFTRT:
11230 /* If this is an equality comparison with zero, we can do this
11231 as a logical shift, which might be much simpler. */
11232 if (equality_comparison_p && const_op == 0
11233 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11235 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11236 XEXP (op0, 0),
11237 INTVAL (XEXP (op0, 1)));
11238 continue;
11241 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11242 do the comparison in a narrower mode. */
11243 if (! unsigned_comparison_p
11244 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11245 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11246 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11247 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11248 MODE_INT, 1)) != BLKmode
11249 && (((unsigned HOST_WIDE_INT) const_op
11250 + (GET_MODE_MASK (tmode) >> 1) + 1)
11251 <= GET_MODE_MASK (tmode)))
11253 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
11254 continue;
11257 /* Likewise if OP0 is a PLUS of a sign extension with a
11258 constant, which is usually represented with the PLUS
11259 between the shifts. */
11260 if (! unsigned_comparison_p
11261 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11262 && GET_CODE (XEXP (op0, 0)) == PLUS
11263 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11264 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11265 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11266 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11267 MODE_INT, 1)) != BLKmode
11268 && (((unsigned HOST_WIDE_INT) const_op
11269 + (GET_MODE_MASK (tmode) >> 1) + 1)
11270 <= GET_MODE_MASK (tmode)))
11272 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11273 rtx add_const = XEXP (XEXP (op0, 0), 1);
11274 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11275 XEXP (op0, 1));
11277 op0 = gen_binary (PLUS, tmode,
11278 gen_lowpart_for_combine (tmode, inner),
11279 new_const);
11280 continue;
11283 /* ... fall through ... */
11284 case LSHIFTRT:
11285 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11286 the low order N bits of FOO are known to be zero, we can do this
11287 by comparing FOO with C shifted left N bits so long as no
11288 overflow occurs. */
11289 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11290 && INTVAL (XEXP (op0, 1)) >= 0
11291 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11292 && mode_width <= HOST_BITS_PER_WIDE_INT
11293 && (nonzero_bits (XEXP (op0, 0), mode)
11294 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11295 && (((unsigned HOST_WIDE_INT) const_op
11296 + (GET_CODE (op0) != LSHIFTRT
11297 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11298 + 1)
11299 : 0))
11300 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11302 /* If the shift was logical, then we must make the condition
11303 unsigned. */
11304 if (GET_CODE (op0) == LSHIFTRT)
11305 code = unsigned_condition (code);
11307 const_op <<= INTVAL (XEXP (op0, 1));
11308 op1 = GEN_INT (const_op);
11309 op0 = XEXP (op0, 0);
11310 continue;
11313 /* If we are using this shift to extract just the sign bit, we
11314 can replace this with an LT or GE comparison. */
11315 if (const_op == 0
11316 && (equality_comparison_p || sign_bit_comparison_p)
11317 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11318 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11319 == mode_width - 1)
11321 op0 = XEXP (op0, 0);
11322 code = (code == NE || code == GT ? LT : GE);
11323 continue;
11325 break;
11327 default:
11328 break;
11331 break;
11334 /* Now make any compound operations involved in this comparison. Then,
11335 check for an outmost SUBREG on OP0 that is not doing anything or is
11336 paradoxical. The latter transformation must only be performed when
11337 it is known that the "extra" bits will be the same in op0 and op1 or
11338 that they don't matter. There are three cases to consider:
11340 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11341 care bits and we can assume they have any convenient value. So
11342 making the transformation is safe.
11344 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11345 In this case the upper bits of op0 are undefined. We should not make
11346 the simplification in that case as we do not know the contents of
11347 those bits.
11349 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11350 NIL. In that case we know those bits are zeros or ones. We must
11351 also be sure that they are the same as the upper bits of op1.
11353 We can never remove a SUBREG for a non-equality comparison because
11354 the sign bit is in a different place in the underlying object. */
11356 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11357 op1 = make_compound_operation (op1, SET);
11359 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11360 /* Case 3 above, to sometimes allow (subreg (mem x)), isn't
11361 implemented. */
11362 && GET_CODE (SUBREG_REG (op0)) == REG
11363 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11364 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11365 && (code == NE || code == EQ))
11367 if (GET_MODE_SIZE (GET_MODE (op0))
11368 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11370 op0 = SUBREG_REG (op0);
11371 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
11373 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11374 <= HOST_BITS_PER_WIDE_INT)
11375 && (nonzero_bits (SUBREG_REG (op0),
11376 GET_MODE (SUBREG_REG (op0)))
11377 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11379 tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)), op1);
11381 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11382 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11383 op0 = SUBREG_REG (op0), op1 = tem;
11387 /* We now do the opposite procedure: Some machines don't have compare
11388 insns in all modes. If OP0's mode is an integer mode smaller than a
11389 word and we can't do a compare in that mode, see if there is a larger
11390 mode for which we can do the compare. There are a number of cases in
11391 which we can use the wider mode. */
11393 mode = GET_MODE (op0);
11394 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11395 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11396 && ! have_insn_for (COMPARE, mode))
11397 for (tmode = GET_MODE_WIDER_MODE (mode);
11398 (tmode != VOIDmode
11399 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11400 tmode = GET_MODE_WIDER_MODE (tmode))
11401 if (have_insn_for (COMPARE, tmode))
11403 int zero_extended;
11405 /* If the only nonzero bits in OP0 and OP1 are those in the
11406 narrower mode and this is an equality or unsigned comparison,
11407 we can use the wider mode. Similarly for sign-extended
11408 values, in which case it is true for all comparisons. */
11409 zero_extended = ((code == EQ || code == NE
11410 || code == GEU || code == GTU
11411 || code == LEU || code == LTU)
11412 && (nonzero_bits (op0, tmode)
11413 & ~GET_MODE_MASK (mode)) == 0
11414 && ((GET_CODE (op1) == CONST_INT
11415 || (nonzero_bits (op1, tmode)
11416 & ~GET_MODE_MASK (mode)) == 0)));
11418 if (zero_extended
11419 || ((num_sign_bit_copies (op0, tmode)
11420 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11421 - GET_MODE_BITSIZE (mode)))
11422 && (num_sign_bit_copies (op1, tmode)
11423 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11424 - GET_MODE_BITSIZE (mode)))))
11426 /* If OP0 is an AND and we don't have an AND in MODE either,
11427 make a new AND in the proper mode. */
11428 if (GET_CODE (op0) == AND
11429 && !have_insn_for (AND, mode))
11430 op0 = gen_binary (AND, tmode,
11431 gen_lowpart_for_combine (tmode,
11432 XEXP (op0, 0)),
11433 gen_lowpart_for_combine (tmode,
11434 XEXP (op0, 1)));
11436 op0 = gen_lowpart_for_combine (tmode, op0);
11437 if (zero_extended && GET_CODE (op1) == CONST_INT)
11438 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11439 op1 = gen_lowpart_for_combine (tmode, op1);
11440 break;
11443 /* If this is a test for negative, we can make an explicit
11444 test of the sign bit. */
11446 if (op1 == const0_rtx && (code == LT || code == GE)
11447 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11449 op0 = gen_binary (AND, tmode,
11450 gen_lowpart_for_combine (tmode, op0),
11451 GEN_INT ((HOST_WIDE_INT) 1
11452 << (GET_MODE_BITSIZE (mode) - 1)));
11453 code = (code == LT) ? NE : EQ;
11454 break;
11458 #ifdef CANONICALIZE_COMPARISON
11459 /* If this machine only supports a subset of valid comparisons, see if we
11460 can convert an unsupported one into a supported one. */
11461 CANONICALIZE_COMPARISON (code, op0, op1);
11462 #endif
11464 *pop0 = op0;
11465 *pop1 = op1;
11467 return code;
11470 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11471 searching backward. */
11472 static enum rtx_code
11473 combine_reversed_comparison_code (exp)
11474 rtx exp;
11476 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11477 rtx x;
11479 if (code1 != UNKNOWN
11480 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11481 return code1;
11482 /* Otherwise try and find where the condition codes were last set and
11483 use that. */
11484 x = get_last_value (XEXP (exp, 0));
11485 if (!x || GET_CODE (x) != COMPARE)
11486 return UNKNOWN;
11487 return reversed_comparison_code_parts (GET_CODE (exp),
11488 XEXP (x, 0), XEXP (x, 1), NULL);
11491 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11492 Return NULL_RTX in case we fail to do the reversal. */
11493 static rtx
11494 reversed_comparison (exp, mode, op0, op1)
11495 rtx exp, op0, op1;
11496 enum machine_mode mode;
11498 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11499 if (reversed_code == UNKNOWN)
11500 return NULL_RTX;
11501 else
11502 return gen_binary (reversed_code, mode, op0, op1);
11505 /* Utility function for following routine. Called when X is part of a value
11506 being stored into reg_last_set_value. Sets reg_last_set_table_tick
11507 for each register mentioned. Similar to mention_regs in cse.c */
11509 static void
11510 update_table_tick (x)
11511 rtx x;
11513 enum rtx_code code = GET_CODE (x);
11514 const char *fmt = GET_RTX_FORMAT (code);
11515 int i;
11517 if (code == REG)
11519 unsigned int regno = REGNO (x);
11520 unsigned int endregno
11521 = regno + (regno < FIRST_PSEUDO_REGISTER
11522 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11523 unsigned int r;
11525 for (r = regno; r < endregno; r++)
11526 reg_last_set_table_tick[r] = label_tick;
11528 return;
11531 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11532 /* Note that we can't have an "E" in values stored; see
11533 get_last_value_validate. */
11534 if (fmt[i] == 'e')
11536 /* Check for identical subexpressions. If x contains
11537 identical subexpression we only have to traverse one of
11538 them. */
11539 if (i == 0
11540 && (GET_RTX_CLASS (code) == '2'
11541 || GET_RTX_CLASS (code) == 'c'))
11543 /* Note that at this point x1 has already been
11544 processed. */
11545 rtx x0 = XEXP (x, 0);
11546 rtx x1 = XEXP (x, 1);
11548 /* If x0 and x1 are identical then there is no need to
11549 process x0. */
11550 if (x0 == x1)
11551 break;
11553 /* If x0 is identical to a subexpression of x1 then while
11554 processing x1, x0 has already been processed. Thus we
11555 are done with x. */
11556 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11557 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11558 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11559 break;
11561 /* If x1 is identical to a subexpression of x0 then we
11562 still have to process the rest of x0. */
11563 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11564 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11565 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11567 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11568 break;
11572 update_table_tick (XEXP (x, i));
11576 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11577 are saying that the register is clobbered and we no longer know its
11578 value. If INSN is zero, don't update reg_last_set; this is only permitted
11579 with VALUE also zero and is used to invalidate the register. */
11581 static void
11582 record_value_for_reg (reg, insn, value)
11583 rtx reg;
11584 rtx insn;
11585 rtx value;
11587 unsigned int regno = REGNO (reg);
11588 unsigned int endregno
11589 = regno + (regno < FIRST_PSEUDO_REGISTER
11590 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
11591 unsigned int i;
11593 /* If VALUE contains REG and we have a previous value for REG, substitute
11594 the previous value. */
11595 if (value && insn && reg_overlap_mentioned_p (reg, value))
11597 rtx tem;
11599 /* Set things up so get_last_value is allowed to see anything set up to
11600 our insn. */
11601 subst_low_cuid = INSN_CUID (insn);
11602 tem = get_last_value (reg);
11604 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11605 it isn't going to be useful and will take a lot of time to process,
11606 so just use the CLOBBER. */
11608 if (tem)
11610 if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
11611 || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
11612 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11613 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11614 tem = XEXP (tem, 0);
11616 value = replace_rtx (copy_rtx (value), reg, tem);
11620 /* For each register modified, show we don't know its value, that
11621 we don't know about its bitwise content, that its value has been
11622 updated, and that we don't know the location of the death of the
11623 register. */
11624 for (i = regno; i < endregno; i++)
11626 if (insn)
11627 reg_last_set[i] = insn;
11629 reg_last_set_value[i] = 0;
11630 reg_last_set_mode[i] = 0;
11631 reg_last_set_nonzero_bits[i] = 0;
11632 reg_last_set_sign_bit_copies[i] = 0;
11633 reg_last_death[i] = 0;
11636 /* Mark registers that are being referenced in this value. */
11637 if (value)
11638 update_table_tick (value);
11640 /* Now update the status of each register being set.
11641 If someone is using this register in this block, set this register
11642 to invalid since we will get confused between the two lives in this
11643 basic block. This makes using this register always invalid. In cse, we
11644 scan the table to invalidate all entries using this register, but this
11645 is too much work for us. */
11647 for (i = regno; i < endregno; i++)
11649 reg_last_set_label[i] = label_tick;
11650 if (value && reg_last_set_table_tick[i] == label_tick)
11651 reg_last_set_invalid[i] = 1;
11652 else
11653 reg_last_set_invalid[i] = 0;
11656 /* The value being assigned might refer to X (like in "x++;"). In that
11657 case, we must replace it with (clobber (const_int 0)) to prevent
11658 infinite loops. */
11659 if (value && ! get_last_value_validate (&value, insn,
11660 reg_last_set_label[regno], 0))
11662 value = copy_rtx (value);
11663 if (! get_last_value_validate (&value, insn,
11664 reg_last_set_label[regno], 1))
11665 value = 0;
11668 /* For the main register being modified, update the value, the mode, the
11669 nonzero bits, and the number of sign bit copies. */
11671 reg_last_set_value[regno] = value;
11673 if (value)
11675 enum machine_mode mode = GET_MODE (reg);
11676 subst_low_cuid = INSN_CUID (insn);
11677 reg_last_set_mode[regno] = mode;
11678 if (GET_MODE_CLASS (mode) == MODE_INT
11679 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11680 mode = nonzero_bits_mode;
11681 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11682 reg_last_set_sign_bit_copies[regno]
11683 = num_sign_bit_copies (value, GET_MODE (reg));
11687 /* Called via note_stores from record_dead_and_set_regs to handle one
11688 SET or CLOBBER in an insn. DATA is the instruction in which the
11689 set is occurring. */
11691 static void
11692 record_dead_and_set_regs_1 (dest, setter, data)
11693 rtx dest, setter;
11694 void *data;
11696 rtx record_dead_insn = (rtx) data;
11698 if (GET_CODE (dest) == SUBREG)
11699 dest = SUBREG_REG (dest);
11701 if (GET_CODE (dest) == REG)
11703 /* If we are setting the whole register, we know its value. Otherwise
11704 show that we don't know the value. We can handle SUBREG in
11705 some cases. */
11706 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11707 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11708 else if (GET_CODE (setter) == SET
11709 && GET_CODE (SET_DEST (setter)) == SUBREG
11710 && SUBREG_REG (SET_DEST (setter)) == dest
11711 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11712 && subreg_lowpart_p (SET_DEST (setter)))
11713 record_value_for_reg (dest, record_dead_insn,
11714 gen_lowpart_for_combine (GET_MODE (dest),
11715 SET_SRC (setter)));
11716 else
11717 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11719 else if (GET_CODE (dest) == MEM
11720 /* Ignore pushes, they clobber nothing. */
11721 && ! push_operand (dest, GET_MODE (dest)))
11722 mem_last_set = INSN_CUID (record_dead_insn);
11725 /* Update the records of when each REG was most recently set or killed
11726 for the things done by INSN. This is the last thing done in processing
11727 INSN in the combiner loop.
11729 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11730 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11731 and also the similar information mem_last_set (which insn most recently
11732 modified memory) and last_call_cuid (which insn was the most recent
11733 subroutine call). */
11735 static void
11736 record_dead_and_set_regs (insn)
11737 rtx insn;
11739 rtx link;
11740 unsigned int i;
11742 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11744 if (REG_NOTE_KIND (link) == REG_DEAD
11745 && GET_CODE (XEXP (link, 0)) == REG)
11747 unsigned int regno = REGNO (XEXP (link, 0));
11748 unsigned int endregno
11749 = regno + (regno < FIRST_PSEUDO_REGISTER
11750 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
11751 : 1);
11753 for (i = regno; i < endregno; i++)
11754 reg_last_death[i] = insn;
11756 else if (REG_NOTE_KIND (link) == REG_INC)
11757 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11760 if (GET_CODE (insn) == CALL_INSN)
11762 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11763 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11765 reg_last_set_value[i] = 0;
11766 reg_last_set_mode[i] = 0;
11767 reg_last_set_nonzero_bits[i] = 0;
11768 reg_last_set_sign_bit_copies[i] = 0;
11769 reg_last_death[i] = 0;
11772 last_call_cuid = mem_last_set = INSN_CUID (insn);
11774 /* Don't bother recording what this insn does. It might set the
11775 return value register, but we can't combine into a call
11776 pattern anyway, so there's no point trying (and it may cause
11777 a crash, if e.g. we wind up asking for last_set_value of a
11778 SUBREG of the return value register). */
11779 return;
11782 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11785 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11786 register present in the SUBREG, so for each such SUBREG go back and
11787 adjust nonzero and sign bit information of the registers that are
11788 known to have some zero/sign bits set.
11790 This is needed because when combine blows the SUBREGs away, the
11791 information on zero/sign bits is lost and further combines can be
11792 missed because of that. */
11794 static void
11795 record_promoted_value (insn, subreg)
11796 rtx insn;
11797 rtx subreg;
11799 rtx links, set;
11800 unsigned int regno = REGNO (SUBREG_REG (subreg));
11801 enum machine_mode mode = GET_MODE (subreg);
11803 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11804 return;
11806 for (links = LOG_LINKS (insn); links;)
11808 insn = XEXP (links, 0);
11809 set = single_set (insn);
11811 if (! set || GET_CODE (SET_DEST (set)) != REG
11812 || REGNO (SET_DEST (set)) != regno
11813 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11815 links = XEXP (links, 1);
11816 continue;
11819 if (reg_last_set[regno] == insn)
11821 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11822 reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11825 if (GET_CODE (SET_SRC (set)) == REG)
11827 regno = REGNO (SET_SRC (set));
11828 links = LOG_LINKS (insn);
11830 else
11831 break;
11835 /* Scan X for promoted SUBREGs. For each one found,
11836 note what it implies to the registers used in it. */
11838 static void
11839 check_promoted_subreg (insn, x)
11840 rtx insn;
11841 rtx x;
11843 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11844 && GET_CODE (SUBREG_REG (x)) == REG)
11845 record_promoted_value (insn, x);
11846 else
11848 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11849 int i, j;
11851 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11852 switch (format[i])
11854 case 'e':
11855 check_promoted_subreg (insn, XEXP (x, i));
11856 break;
11857 case 'V':
11858 case 'E':
11859 if (XVEC (x, i) != 0)
11860 for (j = 0; j < XVECLEN (x, i); j++)
11861 check_promoted_subreg (insn, XVECEXP (x, i, j));
11862 break;
11867 /* Utility routine for the following function. Verify that all the registers
11868 mentioned in *LOC are valid when *LOC was part of a value set when
11869 label_tick == TICK. Return 0 if some are not.
11871 If REPLACE is nonzero, replace the invalid reference with
11872 (clobber (const_int 0)) and return 1. This replacement is useful because
11873 we often can get useful information about the form of a value (e.g., if
11874 it was produced by a shift that always produces -1 or 0) even though
11875 we don't know exactly what registers it was produced from. */
11877 static int
11878 get_last_value_validate (loc, insn, tick, replace)
11879 rtx *loc;
11880 rtx insn;
11881 int tick;
11882 int replace;
11884 rtx x = *loc;
11885 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11886 int len = GET_RTX_LENGTH (GET_CODE (x));
11887 int i;
11889 if (GET_CODE (x) == REG)
11891 unsigned int regno = REGNO (x);
11892 unsigned int endregno
11893 = regno + (regno < FIRST_PSEUDO_REGISTER
11894 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11895 unsigned int j;
11897 for (j = regno; j < endregno; j++)
11898 if (reg_last_set_invalid[j]
11899 /* If this is a pseudo-register that was only set once and not
11900 live at the beginning of the function, it is always valid. */
11901 || (! (regno >= FIRST_PSEUDO_REGISTER
11902 && REG_N_SETS (regno) == 1
11903 && (! REGNO_REG_SET_P
11904 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11905 && reg_last_set_label[j] > tick))
11907 if (replace)
11908 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11909 return replace;
11912 return 1;
11914 /* If this is a memory reference, make sure that there were
11915 no stores after it that might have clobbered the value. We don't
11916 have alias info, so we assume any store invalidates it. */
11917 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11918 && INSN_CUID (insn) <= mem_last_set)
11920 if (replace)
11921 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11922 return replace;
11925 for (i = 0; i < len; i++)
11927 if (fmt[i] == 'e')
11929 /* Check for identical subexpressions. If x contains
11930 identical subexpression we only have to traverse one of
11931 them. */
11932 if (i == 1
11933 && (GET_RTX_CLASS (GET_CODE (x)) == '2'
11934 || GET_RTX_CLASS (GET_CODE (x)) == 'c'))
11936 /* Note that at this point x0 has already been checked
11937 and found valid. */
11938 rtx x0 = XEXP (x, 0);
11939 rtx x1 = XEXP (x, 1);
11941 /* If x0 and x1 are identical then x is also valid. */
11942 if (x0 == x1)
11943 return 1;
11945 /* If x1 is identical to a subexpression of x0 then
11946 while checking x0, x1 has already been checked. Thus
11947 it is valid and so as x. */
11948 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11949 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11950 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11951 return 1;
11953 /* If x0 is identical to a subexpression of x1 then x is
11954 valid iff the rest of x1 is valid. */
11955 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11956 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11957 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11958 return
11959 get_last_value_validate (&XEXP (x1,
11960 x0 == XEXP (x1, 0) ? 1 : 0),
11961 insn, tick, replace);
11964 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11965 replace) == 0)
11966 return 0;
11968 /* Don't bother with these. They shouldn't occur anyway. */
11969 else if (fmt[i] == 'E')
11970 return 0;
11973 /* If we haven't found a reason for it to be invalid, it is valid. */
11974 return 1;
11977 /* Get the last value assigned to X, if known. Some registers
11978 in the value may be replaced with (clobber (const_int 0)) if their value
11979 is known longer known reliably. */
11981 static rtx
11982 get_last_value (x)
11983 rtx x;
11985 unsigned int regno;
11986 rtx value;
11988 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11989 then convert it to the desired mode. If this is a paradoxical SUBREG,
11990 we cannot predict what values the "extra" bits might have. */
11991 if (GET_CODE (x) == SUBREG
11992 && subreg_lowpart_p (x)
11993 && (GET_MODE_SIZE (GET_MODE (x))
11994 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11995 && (value = get_last_value (SUBREG_REG (x))) != 0)
11996 return gen_lowpart_for_combine (GET_MODE (x), value);
11998 if (GET_CODE (x) != REG)
11999 return 0;
12001 regno = REGNO (x);
12002 value = reg_last_set_value[regno];
12004 /* If we don't have a value, or if it isn't for this basic block and
12005 it's either a hard register, set more than once, or it's a live
12006 at the beginning of the function, return 0.
12008 Because if it's not live at the beginning of the function then the reg
12009 is always set before being used (is never used without being set).
12010 And, if it's set only once, and it's always set before use, then all
12011 uses must have the same last value, even if it's not from this basic
12012 block. */
12014 if (value == 0
12015 || (reg_last_set_label[regno] != label_tick
12016 && (regno < FIRST_PSEUDO_REGISTER
12017 || REG_N_SETS (regno) != 1
12018 || (REGNO_REG_SET_P
12019 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
12020 return 0;
12022 /* If the value was set in a later insn than the ones we are processing,
12023 we can't use it even if the register was only set once. */
12024 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
12025 return 0;
12027 /* If the value has all its registers valid, return it. */
12028 if (get_last_value_validate (&value, reg_last_set[regno],
12029 reg_last_set_label[regno], 0))
12030 return value;
12032 /* Otherwise, make a copy and replace any invalid register with
12033 (clobber (const_int 0)). If that fails for some reason, return 0. */
12035 value = copy_rtx (value);
12036 if (get_last_value_validate (&value, reg_last_set[regno],
12037 reg_last_set_label[regno], 1))
12038 return value;
12040 return 0;
12043 /* Return nonzero if expression X refers to a REG or to memory
12044 that is set in an instruction more recent than FROM_CUID. */
12046 static int
12047 use_crosses_set_p (x, from_cuid)
12048 rtx x;
12049 int from_cuid;
12051 const char *fmt;
12052 int i;
12053 enum rtx_code code = GET_CODE (x);
12055 if (code == REG)
12057 unsigned int regno = REGNO (x);
12058 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
12059 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
12061 #ifdef PUSH_ROUNDING
12062 /* Don't allow uses of the stack pointer to be moved,
12063 because we don't know whether the move crosses a push insn. */
12064 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12065 return 1;
12066 #endif
12067 for (; regno < endreg; regno++)
12068 if (reg_last_set[regno]
12069 && INSN_CUID (reg_last_set[regno]) > from_cuid)
12070 return 1;
12071 return 0;
12074 if (code == MEM && mem_last_set > from_cuid)
12075 return 1;
12077 fmt = GET_RTX_FORMAT (code);
12079 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12081 if (fmt[i] == 'E')
12083 int j;
12084 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12085 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
12086 return 1;
12088 else if (fmt[i] == 'e'
12089 && use_crosses_set_p (XEXP (x, i), from_cuid))
12090 return 1;
12092 return 0;
12095 /* Define three variables used for communication between the following
12096 routines. */
12098 static unsigned int reg_dead_regno, reg_dead_endregno;
12099 static int reg_dead_flag;
12101 /* Function called via note_stores from reg_dead_at_p.
12103 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12104 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12106 static void
12107 reg_dead_at_p_1 (dest, x, data)
12108 rtx dest;
12109 rtx x;
12110 void *data ATTRIBUTE_UNUSED;
12112 unsigned int regno, endregno;
12114 if (GET_CODE (dest) != REG)
12115 return;
12117 regno = REGNO (dest);
12118 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
12119 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
12121 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12122 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12125 /* Return nonzero if REG is known to be dead at INSN.
12127 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12128 referencing REG, it is dead. If we hit a SET referencing REG, it is
12129 live. Otherwise, see if it is live or dead at the start of the basic
12130 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12131 must be assumed to be always live. */
12133 static int
12134 reg_dead_at_p (reg, insn)
12135 rtx reg;
12136 rtx insn;
12138 basic_block block;
12139 unsigned int i;
12141 /* Set variables for reg_dead_at_p_1. */
12142 reg_dead_regno = REGNO (reg);
12143 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
12144 ? HARD_REGNO_NREGS (reg_dead_regno,
12145 GET_MODE (reg))
12146 : 1);
12148 reg_dead_flag = 0;
12150 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
12151 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12153 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12154 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
12155 return 0;
12158 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
12159 beginning of function. */
12160 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
12161 insn = prev_nonnote_insn (insn))
12163 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12164 if (reg_dead_flag)
12165 return reg_dead_flag == 1 ? 1 : 0;
12167 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12168 return 1;
12171 /* Get the basic block that we were in. */
12172 if (insn == 0)
12173 block = ENTRY_BLOCK_PTR->next_bb;
12174 else
12176 FOR_EACH_BB (block)
12177 if (insn == block->head)
12178 break;
12180 if (block == EXIT_BLOCK_PTR)
12181 return 0;
12184 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12185 if (REGNO_REG_SET_P (block->global_live_at_start, i))
12186 return 0;
12188 return 1;
12191 /* Note hard registers in X that are used. This code is similar to
12192 that in flow.c, but much simpler since we don't care about pseudos. */
12194 static void
12195 mark_used_regs_combine (x)
12196 rtx x;
12198 RTX_CODE code = GET_CODE (x);
12199 unsigned int regno;
12200 int i;
12202 switch (code)
12204 case LABEL_REF:
12205 case SYMBOL_REF:
12206 case CONST_INT:
12207 case CONST:
12208 case CONST_DOUBLE:
12209 case CONST_VECTOR:
12210 case PC:
12211 case ADDR_VEC:
12212 case ADDR_DIFF_VEC:
12213 case ASM_INPUT:
12214 #ifdef HAVE_cc0
12215 /* CC0 must die in the insn after it is set, so we don't need to take
12216 special note of it here. */
12217 case CC0:
12218 #endif
12219 return;
12221 case CLOBBER:
12222 /* If we are clobbering a MEM, mark any hard registers inside the
12223 address as used. */
12224 if (GET_CODE (XEXP (x, 0)) == MEM)
12225 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12226 return;
12228 case REG:
12229 regno = REGNO (x);
12230 /* A hard reg in a wide mode may really be multiple registers.
12231 If so, mark all of them just like the first. */
12232 if (regno < FIRST_PSEUDO_REGISTER)
12234 unsigned int endregno, r;
12236 /* None of this applies to the stack, frame or arg pointers. */
12237 if (regno == STACK_POINTER_REGNUM
12238 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12239 || regno == HARD_FRAME_POINTER_REGNUM
12240 #endif
12241 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12242 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12243 #endif
12244 || regno == FRAME_POINTER_REGNUM)
12245 return;
12247 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12248 for (r = regno; r < endregno; r++)
12249 SET_HARD_REG_BIT (newpat_used_regs, r);
12251 return;
12253 case SET:
12255 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12256 the address. */
12257 rtx testreg = SET_DEST (x);
12259 while (GET_CODE (testreg) == SUBREG
12260 || GET_CODE (testreg) == ZERO_EXTRACT
12261 || GET_CODE (testreg) == SIGN_EXTRACT
12262 || GET_CODE (testreg) == STRICT_LOW_PART)
12263 testreg = XEXP (testreg, 0);
12265 if (GET_CODE (testreg) == MEM)
12266 mark_used_regs_combine (XEXP (testreg, 0));
12268 mark_used_regs_combine (SET_SRC (x));
12270 return;
12272 default:
12273 break;
12276 /* Recursively scan the operands of this expression. */
12279 const char *fmt = GET_RTX_FORMAT (code);
12281 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12283 if (fmt[i] == 'e')
12284 mark_used_regs_combine (XEXP (x, i));
12285 else if (fmt[i] == 'E')
12287 int j;
12289 for (j = 0; j < XVECLEN (x, i); j++)
12290 mark_used_regs_combine (XVECEXP (x, i, j));
12296 /* Remove register number REGNO from the dead registers list of INSN.
12298 Return the note used to record the death, if there was one. */
12301 remove_death (regno, insn)
12302 unsigned int regno;
12303 rtx insn;
12305 rtx note = find_regno_note (insn, REG_DEAD, regno);
12307 if (note)
12309 REG_N_DEATHS (regno)--;
12310 remove_note (insn, note);
12313 return note;
12316 /* For each register (hardware or pseudo) used within expression X, if its
12317 death is in an instruction with cuid between FROM_CUID (inclusive) and
12318 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12319 list headed by PNOTES.
12321 That said, don't move registers killed by maybe_kill_insn.
12323 This is done when X is being merged by combination into TO_INSN. These
12324 notes will then be distributed as needed. */
12326 static void
12327 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
12328 rtx x;
12329 rtx maybe_kill_insn;
12330 int from_cuid;
12331 rtx to_insn;
12332 rtx *pnotes;
12334 const char *fmt;
12335 int len, i;
12336 enum rtx_code code = GET_CODE (x);
12338 if (code == REG)
12340 unsigned int regno = REGNO (x);
12341 rtx where_dead = reg_last_death[regno];
12342 rtx before_dead, after_dead;
12344 /* Don't move the register if it gets killed in between from and to. */
12345 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12346 && ! reg_referenced_p (x, maybe_kill_insn))
12347 return;
12349 /* WHERE_DEAD could be a USE insn made by combine, so first we
12350 make sure that we have insns with valid INSN_CUID values. */
12351 before_dead = where_dead;
12352 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12353 before_dead = PREV_INSN (before_dead);
12355 after_dead = where_dead;
12356 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12357 after_dead = NEXT_INSN (after_dead);
12359 if (before_dead && after_dead
12360 && INSN_CUID (before_dead) >= from_cuid
12361 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12362 || (where_dead != after_dead
12363 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12365 rtx note = remove_death (regno, where_dead);
12367 /* It is possible for the call above to return 0. This can occur
12368 when reg_last_death points to I2 or I1 that we combined with.
12369 In that case make a new note.
12371 We must also check for the case where X is a hard register
12372 and NOTE is a death note for a range of hard registers
12373 including X. In that case, we must put REG_DEAD notes for
12374 the remaining registers in place of NOTE. */
12376 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12377 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12378 > GET_MODE_SIZE (GET_MODE (x))))
12380 unsigned int deadregno = REGNO (XEXP (note, 0));
12381 unsigned int deadend
12382 = (deadregno + HARD_REGNO_NREGS (deadregno,
12383 GET_MODE (XEXP (note, 0))));
12384 unsigned int ourend
12385 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12386 unsigned int i;
12388 for (i = deadregno; i < deadend; i++)
12389 if (i < regno || i >= ourend)
12390 REG_NOTES (where_dead)
12391 = gen_rtx_EXPR_LIST (REG_DEAD,
12392 regno_reg_rtx[i],
12393 REG_NOTES (where_dead));
12396 /* If we didn't find any note, or if we found a REG_DEAD note that
12397 covers only part of the given reg, and we have a multi-reg hard
12398 register, then to be safe we must check for REG_DEAD notes
12399 for each register other than the first. They could have
12400 their own REG_DEAD notes lying around. */
12401 else if ((note == 0
12402 || (note != 0
12403 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12404 < GET_MODE_SIZE (GET_MODE (x)))))
12405 && regno < FIRST_PSEUDO_REGISTER
12406 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
12408 unsigned int ourend
12409 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12410 unsigned int i, offset;
12411 rtx oldnotes = 0;
12413 if (note)
12414 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
12415 else
12416 offset = 1;
12418 for (i = regno + offset; i < ourend; i++)
12419 move_deaths (regno_reg_rtx[i],
12420 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12423 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12425 XEXP (note, 1) = *pnotes;
12426 *pnotes = note;
12428 else
12429 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12431 REG_N_DEATHS (regno)++;
12434 return;
12437 else if (GET_CODE (x) == SET)
12439 rtx dest = SET_DEST (x);
12441 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12443 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12444 that accesses one word of a multi-word item, some
12445 piece of everything register in the expression is used by
12446 this insn, so remove any old death. */
12447 /* ??? So why do we test for equality of the sizes? */
12449 if (GET_CODE (dest) == ZERO_EXTRACT
12450 || GET_CODE (dest) == STRICT_LOW_PART
12451 || (GET_CODE (dest) == SUBREG
12452 && (((GET_MODE_SIZE (GET_MODE (dest))
12453 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12454 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12455 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12457 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12458 return;
12461 /* If this is some other SUBREG, we know it replaces the entire
12462 value, so use that as the destination. */
12463 if (GET_CODE (dest) == SUBREG)
12464 dest = SUBREG_REG (dest);
12466 /* If this is a MEM, adjust deaths of anything used in the address.
12467 For a REG (the only other possibility), the entire value is
12468 being replaced so the old value is not used in this insn. */
12470 if (GET_CODE (dest) == MEM)
12471 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12472 to_insn, pnotes);
12473 return;
12476 else if (GET_CODE (x) == CLOBBER)
12477 return;
12479 len = GET_RTX_LENGTH (code);
12480 fmt = GET_RTX_FORMAT (code);
12482 for (i = 0; i < len; i++)
12484 if (fmt[i] == 'E')
12486 int j;
12487 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12488 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12489 to_insn, pnotes);
12491 else if (fmt[i] == 'e')
12492 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12496 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12497 pattern of an insn. X must be a REG. */
12499 static int
12500 reg_bitfield_target_p (x, body)
12501 rtx x;
12502 rtx body;
12504 int i;
12506 if (GET_CODE (body) == SET)
12508 rtx dest = SET_DEST (body);
12509 rtx target;
12510 unsigned int regno, tregno, endregno, endtregno;
12512 if (GET_CODE (dest) == ZERO_EXTRACT)
12513 target = XEXP (dest, 0);
12514 else if (GET_CODE (dest) == STRICT_LOW_PART)
12515 target = SUBREG_REG (XEXP (dest, 0));
12516 else
12517 return 0;
12519 if (GET_CODE (target) == SUBREG)
12520 target = SUBREG_REG (target);
12522 if (GET_CODE (target) != REG)
12523 return 0;
12525 tregno = REGNO (target), regno = REGNO (x);
12526 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12527 return target == x;
12529 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
12530 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12532 return endregno > tregno && regno < endtregno;
12535 else if (GET_CODE (body) == PARALLEL)
12536 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12537 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12538 return 1;
12540 return 0;
12543 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12544 as appropriate. I3 and I2 are the insns resulting from the combination
12545 insns including FROM (I2 may be zero).
12547 Each note in the list is either ignored or placed on some insns, depending
12548 on the type of note. */
12550 static void
12551 distribute_notes (notes, from_insn, i3, i2)
12552 rtx notes;
12553 rtx from_insn;
12554 rtx i3, i2;
12556 rtx note, next_note;
12557 rtx tem;
12559 for (note = notes; note; note = next_note)
12561 rtx place = 0, place2 = 0;
12563 /* If this NOTE references a pseudo register, ensure it references
12564 the latest copy of that register. */
12565 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12566 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12567 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12569 next_note = XEXP (note, 1);
12570 switch (REG_NOTE_KIND (note))
12572 case REG_BR_PROB:
12573 case REG_BR_PRED:
12574 /* Doesn't matter much where we put this, as long as it's somewhere.
12575 It is preferable to keep these notes on branches, which is most
12576 likely to be i3. */
12577 place = i3;
12578 break;
12580 case REG_VTABLE_REF:
12581 /* ??? Should remain with *a particular* memory load. Given the
12582 nature of vtable data, the last insn seems relatively safe. */
12583 place = i3;
12584 break;
12586 case REG_NON_LOCAL_GOTO:
12587 if (GET_CODE (i3) == JUMP_INSN)
12588 place = i3;
12589 else if (i2 && GET_CODE (i2) == JUMP_INSN)
12590 place = i2;
12591 else
12592 abort ();
12593 break;
12595 case REG_EH_REGION:
12596 /* These notes must remain with the call or trapping instruction. */
12597 if (GET_CODE (i3) == CALL_INSN)
12598 place = i3;
12599 else if (i2 && GET_CODE (i2) == CALL_INSN)
12600 place = i2;
12601 else if (flag_non_call_exceptions)
12603 if (may_trap_p (i3))
12604 place = i3;
12605 else if (i2 && may_trap_p (i2))
12606 place = i2;
12607 /* ??? Otherwise assume we've combined things such that we
12608 can now prove that the instructions can't trap. Drop the
12609 note in this case. */
12611 else
12612 abort ();
12613 break;
12615 case REG_NORETURN:
12616 case REG_SETJMP:
12617 /* These notes must remain with the call. It should not be
12618 possible for both I2 and I3 to be a call. */
12619 if (GET_CODE (i3) == CALL_INSN)
12620 place = i3;
12621 else if (i2 && GET_CODE (i2) == CALL_INSN)
12622 place = i2;
12623 else
12624 abort ();
12625 break;
12627 case REG_UNUSED:
12628 /* Any clobbers for i3 may still exist, and so we must process
12629 REG_UNUSED notes from that insn.
12631 Any clobbers from i2 or i1 can only exist if they were added by
12632 recog_for_combine. In that case, recog_for_combine created the
12633 necessary REG_UNUSED notes. Trying to keep any original
12634 REG_UNUSED notes from these insns can cause incorrect output
12635 if it is for the same register as the original i3 dest.
12636 In that case, we will notice that the register is set in i3,
12637 and then add a REG_UNUSED note for the destination of i3, which
12638 is wrong. However, it is possible to have REG_UNUSED notes from
12639 i2 or i1 for register which were both used and clobbered, so
12640 we keep notes from i2 or i1 if they will turn into REG_DEAD
12641 notes. */
12643 /* If this register is set or clobbered in I3, put the note there
12644 unless there is one already. */
12645 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12647 if (from_insn != i3)
12648 break;
12650 if (! (GET_CODE (XEXP (note, 0)) == REG
12651 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12652 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12653 place = i3;
12655 /* Otherwise, if this register is used by I3, then this register
12656 now dies here, so we must put a REG_DEAD note here unless there
12657 is one already. */
12658 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12659 && ! (GET_CODE (XEXP (note, 0)) == REG
12660 ? find_regno_note (i3, REG_DEAD,
12661 REGNO (XEXP (note, 0)))
12662 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12664 PUT_REG_NOTE_KIND (note, REG_DEAD);
12665 place = i3;
12667 break;
12669 case REG_EQUAL:
12670 case REG_EQUIV:
12671 case REG_NOALIAS:
12672 /* These notes say something about results of an insn. We can
12673 only support them if they used to be on I3 in which case they
12674 remain on I3. Otherwise they are ignored.
12676 If the note refers to an expression that is not a constant, we
12677 must also ignore the note since we cannot tell whether the
12678 equivalence is still true. It might be possible to do
12679 slightly better than this (we only have a problem if I2DEST
12680 or I1DEST is present in the expression), but it doesn't
12681 seem worth the trouble. */
12683 if (from_insn == i3
12684 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12685 place = i3;
12686 break;
12688 case REG_INC:
12689 case REG_NO_CONFLICT:
12690 /* These notes say something about how a register is used. They must
12691 be present on any use of the register in I2 or I3. */
12692 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12693 place = i3;
12695 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12697 if (place)
12698 place2 = i2;
12699 else
12700 place = i2;
12702 break;
12704 case REG_LABEL:
12705 /* This can show up in several ways -- either directly in the
12706 pattern, or hidden off in the constant pool with (or without?)
12707 a REG_EQUAL note. */
12708 /* ??? Ignore the without-reg_equal-note problem for now. */
12709 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12710 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12711 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12712 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12713 place = i3;
12715 if (i2
12716 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12717 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12718 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12719 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12721 if (place)
12722 place2 = i2;
12723 else
12724 place = i2;
12727 /* Don't attach REG_LABEL note to a JUMP_INSN which has
12728 JUMP_LABEL already. Instead, decrement LABEL_NUSES. */
12729 if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12731 if (JUMP_LABEL (place) != XEXP (note, 0))
12732 abort ();
12733 if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12734 LABEL_NUSES (JUMP_LABEL (place))--;
12735 place = 0;
12737 if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12739 if (JUMP_LABEL (place2) != XEXP (note, 0))
12740 abort ();
12741 if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12742 LABEL_NUSES (JUMP_LABEL (place2))--;
12743 place2 = 0;
12745 break;
12747 case REG_NONNEG:
12748 case REG_WAS_0:
12749 /* These notes say something about the value of a register prior
12750 to the execution of an insn. It is too much trouble to see
12751 if the note is still correct in all situations. It is better
12752 to simply delete it. */
12753 break;
12755 case REG_RETVAL:
12756 /* If the insn previously containing this note still exists,
12757 put it back where it was. Otherwise move it to the previous
12758 insn. Adjust the corresponding REG_LIBCALL note. */
12759 if (GET_CODE (from_insn) != NOTE)
12760 place = from_insn;
12761 else
12763 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12764 place = prev_real_insn (from_insn);
12765 if (tem && place)
12766 XEXP (tem, 0) = place;
12767 /* If we're deleting the last remaining instruction of a
12768 libcall sequence, don't add the notes. */
12769 else if (XEXP (note, 0) == from_insn)
12770 tem = place = 0;
12772 break;
12774 case REG_LIBCALL:
12775 /* This is handled similarly to REG_RETVAL. */
12776 if (GET_CODE (from_insn) != NOTE)
12777 place = from_insn;
12778 else
12780 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12781 place = next_real_insn (from_insn);
12782 if (tem && place)
12783 XEXP (tem, 0) = place;
12784 /* If we're deleting the last remaining instruction of a
12785 libcall sequence, don't add the notes. */
12786 else if (XEXP (note, 0) == from_insn)
12787 tem = place = 0;
12789 break;
12791 case REG_DEAD:
12792 /* If the register is used as an input in I3, it dies there.
12793 Similarly for I2, if it is nonzero and adjacent to I3.
12795 If the register is not used as an input in either I3 or I2
12796 and it is not one of the registers we were supposed to eliminate,
12797 there are two possibilities. We might have a non-adjacent I2
12798 or we might have somehow eliminated an additional register
12799 from a computation. For example, we might have had A & B where
12800 we discover that B will always be zero. In this case we will
12801 eliminate the reference to A.
12803 In both cases, we must search to see if we can find a previous
12804 use of A and put the death note there. */
12806 if (from_insn
12807 && GET_CODE (from_insn) == CALL_INSN
12808 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12809 place = from_insn;
12810 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12811 place = i3;
12812 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12813 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12814 place = i2;
12816 if (place == 0)
12818 basic_block bb = this_basic_block;
12820 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12822 if (! INSN_P (tem))
12824 if (tem == bb->head)
12825 break;
12826 continue;
12829 /* If the register is being set at TEM, see if that is all
12830 TEM is doing. If so, delete TEM. Otherwise, make this
12831 into a REG_UNUSED note instead. */
12832 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12834 rtx set = single_set (tem);
12835 rtx inner_dest = 0;
12836 #ifdef HAVE_cc0
12837 rtx cc0_setter = NULL_RTX;
12838 #endif
12840 if (set != 0)
12841 for (inner_dest = SET_DEST (set);
12842 (GET_CODE (inner_dest) == STRICT_LOW_PART
12843 || GET_CODE (inner_dest) == SUBREG
12844 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12845 inner_dest = XEXP (inner_dest, 0))
12848 /* Verify that it was the set, and not a clobber that
12849 modified the register.
12851 CC0 targets must be careful to maintain setter/user
12852 pairs. If we cannot delete the setter due to side
12853 effects, mark the user with an UNUSED note instead
12854 of deleting it. */
12856 if (set != 0 && ! side_effects_p (SET_SRC (set))
12857 && rtx_equal_p (XEXP (note, 0), inner_dest)
12858 #ifdef HAVE_cc0
12859 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12860 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12861 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12862 #endif
12865 /* Move the notes and links of TEM elsewhere.
12866 This might delete other dead insns recursively.
12867 First set the pattern to something that won't use
12868 any register. */
12870 PATTERN (tem) = pc_rtx;
12872 distribute_notes (REG_NOTES (tem), tem, tem,
12873 NULL_RTX);
12874 distribute_links (LOG_LINKS (tem));
12876 PUT_CODE (tem, NOTE);
12877 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12878 NOTE_SOURCE_FILE (tem) = 0;
12880 #ifdef HAVE_cc0
12881 /* Delete the setter too. */
12882 if (cc0_setter)
12884 PATTERN (cc0_setter) = pc_rtx;
12886 distribute_notes (REG_NOTES (cc0_setter),
12887 cc0_setter, cc0_setter,
12888 NULL_RTX);
12889 distribute_links (LOG_LINKS (cc0_setter));
12891 PUT_CODE (cc0_setter, NOTE);
12892 NOTE_LINE_NUMBER (cc0_setter)
12893 = NOTE_INSN_DELETED;
12894 NOTE_SOURCE_FILE (cc0_setter) = 0;
12896 #endif
12898 /* If the register is both set and used here, put the
12899 REG_DEAD note here, but place a REG_UNUSED note
12900 here too unless there already is one. */
12901 else if (reg_referenced_p (XEXP (note, 0),
12902 PATTERN (tem)))
12904 place = tem;
12906 if (! find_regno_note (tem, REG_UNUSED,
12907 REGNO (XEXP (note, 0))))
12908 REG_NOTES (tem)
12909 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
12910 REG_NOTES (tem));
12912 else
12914 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12916 /* If there isn't already a REG_UNUSED note, put one
12917 here. */
12918 if (! find_regno_note (tem, REG_UNUSED,
12919 REGNO (XEXP (note, 0))))
12920 place = tem;
12921 break;
12924 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12925 || (GET_CODE (tem) == CALL_INSN
12926 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12928 place = tem;
12930 /* If we are doing a 3->2 combination, and we have a
12931 register which formerly died in i3 and was not used
12932 by i2, which now no longer dies in i3 and is used in
12933 i2 but does not die in i2, and place is between i2
12934 and i3, then we may need to move a link from place to
12935 i2. */
12936 if (i2 && INSN_UID (place) <= max_uid_cuid
12937 && INSN_CUID (place) > INSN_CUID (i2)
12938 && from_insn
12939 && INSN_CUID (from_insn) > INSN_CUID (i2)
12940 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12942 rtx links = LOG_LINKS (place);
12943 LOG_LINKS (place) = 0;
12944 distribute_links (links);
12946 break;
12949 if (tem == bb->head)
12950 break;
12953 /* We haven't found an insn for the death note and it
12954 is still a REG_DEAD note, but we have hit the beginning
12955 of the block. If the existing life info says the reg
12956 was dead, there's nothing left to do. Otherwise, we'll
12957 need to do a global life update after combine. */
12958 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12959 && REGNO_REG_SET_P (bb->global_live_at_start,
12960 REGNO (XEXP (note, 0))))
12961 SET_BIT (refresh_blocks, this_basic_block->index);
12964 /* If the register is set or already dead at PLACE, we needn't do
12965 anything with this note if it is still a REG_DEAD note.
12966 We can here if it is set at all, not if is it totally replace,
12967 which is what `dead_or_set_p' checks, so also check for it being
12968 set partially. */
12970 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12972 unsigned int regno = REGNO (XEXP (note, 0));
12974 /* Similarly, if the instruction on which we want to place
12975 the note is a noop, we'll need do a global live update
12976 after we remove them in delete_noop_moves. */
12977 if (noop_move_p (place))
12978 SET_BIT (refresh_blocks, this_basic_block->index);
12980 if (dead_or_set_p (place, XEXP (note, 0))
12981 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12983 /* Unless the register previously died in PLACE, clear
12984 reg_last_death. [I no longer understand why this is
12985 being done.] */
12986 if (reg_last_death[regno] != place)
12987 reg_last_death[regno] = 0;
12988 place = 0;
12990 else
12991 reg_last_death[regno] = place;
12993 /* If this is a death note for a hard reg that is occupying
12994 multiple registers, ensure that we are still using all
12995 parts of the object. If we find a piece of the object
12996 that is unused, we must arrange for an appropriate REG_DEAD
12997 note to be added for it. However, we can't just emit a USE
12998 and tag the note to it, since the register might actually
12999 be dead; so we recourse, and the recursive call then finds
13000 the previous insn that used this register. */
13002 if (place && regno < FIRST_PSEUDO_REGISTER
13003 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
13005 unsigned int endregno
13006 = regno + HARD_REGNO_NREGS (regno,
13007 GET_MODE (XEXP (note, 0)));
13008 int all_used = 1;
13009 unsigned int i;
13011 for (i = regno; i < endregno; i++)
13012 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13013 && ! find_regno_fusage (place, USE, i))
13014 || dead_or_set_regno_p (place, i))
13015 all_used = 0;
13017 if (! all_used)
13019 /* Put only REG_DEAD notes for pieces that are
13020 not already dead or set. */
13022 for (i = regno; i < endregno;
13023 i += HARD_REGNO_NREGS (i, reg_raw_mode[i]))
13025 rtx piece = regno_reg_rtx[i];
13026 basic_block bb = this_basic_block;
13028 if (! dead_or_set_p (place, piece)
13029 && ! reg_bitfield_target_p (piece,
13030 PATTERN (place)))
13032 rtx new_note
13033 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
13035 distribute_notes (new_note, place, place,
13036 NULL_RTX);
13038 else if (! refers_to_regno_p (i, i + 1,
13039 PATTERN (place), 0)
13040 && ! find_regno_fusage (place, USE, i))
13041 for (tem = PREV_INSN (place); ;
13042 tem = PREV_INSN (tem))
13044 if (! INSN_P (tem))
13046 if (tem == bb->head)
13048 SET_BIT (refresh_blocks,
13049 this_basic_block->index);
13050 break;
13052 continue;
13054 if (dead_or_set_p (tem, piece)
13055 || reg_bitfield_target_p (piece,
13056 PATTERN (tem)))
13058 REG_NOTES (tem)
13059 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
13060 REG_NOTES (tem));
13061 break;
13067 place = 0;
13071 break;
13073 default:
13074 /* Any other notes should not be present at this point in the
13075 compilation. */
13076 abort ();
13079 if (place)
13081 XEXP (note, 1) = REG_NOTES (place);
13082 REG_NOTES (place) = note;
13084 else if ((REG_NOTE_KIND (note) == REG_DEAD
13085 || REG_NOTE_KIND (note) == REG_UNUSED)
13086 && GET_CODE (XEXP (note, 0)) == REG)
13087 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
13089 if (place2)
13091 if ((REG_NOTE_KIND (note) == REG_DEAD
13092 || REG_NOTE_KIND (note) == REG_UNUSED)
13093 && GET_CODE (XEXP (note, 0)) == REG)
13094 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
13096 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
13097 REG_NOTE_KIND (note),
13098 XEXP (note, 0),
13099 REG_NOTES (place2));
13104 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13105 I3, I2, and I1 to new locations. This is also called in one case to
13106 add a link pointing at I3 when I3's destination is changed. */
13108 static void
13109 distribute_links (links)
13110 rtx links;
13112 rtx link, next_link;
13114 for (link = links; link; link = next_link)
13116 rtx place = 0;
13117 rtx insn;
13118 rtx set, reg;
13120 next_link = XEXP (link, 1);
13122 /* If the insn that this link points to is a NOTE or isn't a single
13123 set, ignore it. In the latter case, it isn't clear what we
13124 can do other than ignore the link, since we can't tell which
13125 register it was for. Such links wouldn't be used by combine
13126 anyway.
13128 It is not possible for the destination of the target of the link to
13129 have been changed by combine. The only potential of this is if we
13130 replace I3, I2, and I1 by I3 and I2. But in that case the
13131 destination of I2 also remains unchanged. */
13133 if (GET_CODE (XEXP (link, 0)) == NOTE
13134 || (set = single_set (XEXP (link, 0))) == 0)
13135 continue;
13137 reg = SET_DEST (set);
13138 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13139 || GET_CODE (reg) == SIGN_EXTRACT
13140 || GET_CODE (reg) == STRICT_LOW_PART)
13141 reg = XEXP (reg, 0);
13143 /* A LOG_LINK is defined as being placed on the first insn that uses
13144 a register and points to the insn that sets the register. Start
13145 searching at the next insn after the target of the link and stop
13146 when we reach a set of the register or the end of the basic block.
13148 Note that this correctly handles the link that used to point from
13149 I3 to I2. Also note that not much searching is typically done here
13150 since most links don't point very far away. */
13152 for (insn = NEXT_INSN (XEXP (link, 0));
13153 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13154 || this_basic_block->next_bb->head != insn));
13155 insn = NEXT_INSN (insn))
13156 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13158 if (reg_referenced_p (reg, PATTERN (insn)))
13159 place = insn;
13160 break;
13162 else if (GET_CODE (insn) == CALL_INSN
13163 && find_reg_fusage (insn, USE, reg))
13165 place = insn;
13166 break;
13169 /* If we found a place to put the link, place it there unless there
13170 is already a link to the same insn as LINK at that point. */
13172 if (place)
13174 rtx link2;
13176 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13177 if (XEXP (link2, 0) == XEXP (link, 0))
13178 break;
13180 if (link2 == 0)
13182 XEXP (link, 1) = LOG_LINKS (place);
13183 LOG_LINKS (place) = link;
13185 /* Set added_links_insn to the earliest insn we added a
13186 link to. */
13187 if (added_links_insn == 0
13188 || INSN_CUID (added_links_insn) > INSN_CUID (place))
13189 added_links_insn = place;
13195 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
13197 static int
13198 insn_cuid (insn)
13199 rtx insn;
13201 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13202 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13203 insn = NEXT_INSN (insn);
13205 if (INSN_UID (insn) > max_uid_cuid)
13206 abort ();
13208 return INSN_CUID (insn);
13211 void
13212 dump_combine_stats (file)
13213 FILE *file;
13215 fnotice
13216 (file,
13217 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13218 combine_attempts, combine_merges, combine_extras, combine_successes);
13221 void
13222 dump_combine_total_stats (file)
13223 FILE *file;
13225 fnotice
13226 (file,
13227 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13228 total_attempts, total_merges, total_extras, total_successes);