* c-decl.c (c_expand_body): Check TYPE_SIZE_UNIT (ret_type)
[official-gcc.git] / gcc / combine.c
blob9b721af4df4d96ff0dccf526adc77e8c210dd28c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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 - reg_n_refs is not adjusted in the rare case when a register is
57 no longer required in a computation
58 - there are extremely rare cases (see distribute_regnotes) when a
59 REG_DEAD note is lost
60 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61 removed because there is no way to know which register it was
62 linking
64 To simplify substitution, we combine only when the earlier insn(s)
65 consist of only a single assignment. To simplify updating afterward,
66 we never combine when a subroutine call appears in the middle.
68 Since we do not represent assignments to CC0 explicitly except when that
69 is all an insn does, there is no LOG_LINKS entry in an insn that uses
70 the condition code for the insn that set the condition code.
71 Fortunately, these two insns must be consecutive.
72 Therefore, every JUMP_INSN is taken to have an implicit logical link
73 to the preceding insn. This is not quite right, since non-jumps can
74 also use the condition code; but in practice such insns would not
75 combine anyway. */
77 #include "config.h"
78 #include "system.h"
79 #include "rtl.h"
80 #include "tm_p.h"
81 #include "flags.h"
82 #include "regs.h"
83 #include "hard-reg-set.h"
84 #include "basic-block.h"
85 #include "insn-config.h"
86 #include "function.h"
87 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
88 #include "expr.h"
89 #include "insn-flags.h"
90 #include "insn-codes.h"
91 #include "insn-attr.h"
92 #include "recog.h"
93 #include "real.h"
94 #include "toplev.h"
96 #ifndef ACCUMULATE_OUTGOING_ARGS
97 #define ACCUMULATE_OUTGOING_ARGS 0
98 #endif
100 /* Supply a default definition for PUSH_ARGS. */
101 #ifndef PUSH_ARGS
102 #ifdef PUSH_ROUNDING
103 #define PUSH_ARGS !ACCUMULATE_OUTGOING_ARGS
104 #else
105 #define PUSH_ARGS 0
106 #endif
107 #endif
109 /* It is not safe to use ordinary gen_lowpart in combine.
110 Use gen_lowpart_for_combine instead. See comments there. */
111 #define gen_lowpart dont_use_gen_lowpart_you_dummy
113 /* Number of attempts to combine instructions in this function. */
115 static int combine_attempts;
117 /* Number of attempts that got as far as substitution in this function. */
119 static int combine_merges;
121 /* Number of instructions combined with added SETs in this function. */
123 static int combine_extras;
125 /* Number of instructions combined in this function. */
127 static int combine_successes;
129 /* Totals over entire compilation. */
131 static int total_attempts, total_merges, total_extras, total_successes;
134 /* Vector mapping INSN_UIDs to cuids.
135 The cuids are like uids but increase monotonically always.
136 Combine always uses cuids so that it can compare them.
137 But actually renumbering the uids, which we used to do,
138 proves to be a bad idea because it makes it hard to compare
139 the dumps produced by earlier passes with those from later passes. */
141 static int *uid_cuid;
142 static int max_uid_cuid;
144 /* Get the cuid of an insn. */
146 #define INSN_CUID(INSN) \
147 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
149 /* Maximum register number, which is the size of the tables below. */
151 static unsigned int combine_max_regno;
153 /* Record last point of death of (hard or pseudo) register n. */
155 static rtx *reg_last_death;
157 /* Record last point of modification of (hard or pseudo) register n. */
159 static rtx *reg_last_set;
161 /* Record the cuid of the last insn that invalidated memory
162 (anything that writes memory, and subroutine calls, but not pushes). */
164 static int mem_last_set;
166 /* Record the cuid of the last CALL_INSN
167 so we can tell whether a potential combination crosses any calls. */
169 static int last_call_cuid;
171 /* When `subst' is called, this is the insn that is being modified
172 (by combining in a previous insn). The PATTERN of this insn
173 is still the old pattern partially modified and it should not be
174 looked at, but this may be used to examine the successors of the insn
175 to judge whether a simplification is valid. */
177 static rtx subst_insn;
179 /* This is an insn that belongs before subst_insn, but is not currently
180 on the insn chain. */
182 static rtx subst_prev_insn;
184 /* This is the lowest CUID that `subst' is currently dealing with.
185 get_last_value will not return a value if the register was set at or
186 after this CUID. If not for this mechanism, we could get confused if
187 I2 or I1 in try_combine were an insn that used the old value of a register
188 to obtain a new value. In that case, we might erroneously get the
189 new value of the register when we wanted the old one. */
191 static int subst_low_cuid;
193 /* This contains any hard registers that are used in newpat; reg_dead_at_p
194 must consider all these registers to be always live. */
196 static HARD_REG_SET newpat_used_regs;
198 /* This is an insn to which a LOG_LINKS entry has been added. If this
199 insn is the earlier than I2 or I3, combine should rescan starting at
200 that location. */
202 static rtx added_links_insn;
204 /* Basic block number of the block in which we are performing combines. */
205 static int this_basic_block;
207 /* A bitmap indicating which blocks had registers go dead at entry.
208 After combine, we'll need to re-do global life analysis with
209 those blocks as starting points. */
210 static sbitmap refresh_blocks;
211 static int need_refresh;
213 /* The next group of arrays allows the recording of the last value assigned
214 to (hard or pseudo) register n. We use this information to see if a
215 operation being processed is redundant given a prior operation performed
216 on the register. For example, an `and' with a constant is redundant if
217 all the zero bits are already known to be turned off.
219 We use an approach similar to that used by cse, but change it in the
220 following ways:
222 (1) We do not want to reinitialize at each label.
223 (2) It is useful, but not critical, to know the actual value assigned
224 to a register. Often just its form is helpful.
226 Therefore, we maintain the following arrays:
228 reg_last_set_value the last value assigned
229 reg_last_set_label records the value of label_tick when the
230 register was assigned
231 reg_last_set_table_tick records the value of label_tick when a
232 value using the register is assigned
233 reg_last_set_invalid set to non-zero when it is not valid
234 to use the value of this register in some
235 register's value
237 To understand the usage of these tables, it is important to understand
238 the distinction between the value in reg_last_set_value being valid
239 and the register being validly contained in some other expression in the
240 table.
242 Entry I in reg_last_set_value is valid if it is non-zero, and either
243 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
245 Register I may validly appear in any expression returned for the value
246 of another register if reg_n_sets[i] is 1. It may also appear in the
247 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
248 reg_last_set_invalid[j] is zero.
250 If an expression is found in the table containing a register which may
251 not validly appear in an expression, the register is replaced by
252 something that won't match, (clobber (const_int 0)).
254 reg_last_set_invalid[i] is set non-zero when register I is being assigned
255 to and reg_last_set_table_tick[i] == label_tick. */
257 /* Record last value assigned to (hard or pseudo) register n. */
259 static rtx *reg_last_set_value;
261 /* Record the value of label_tick when the value for register n is placed in
262 reg_last_set_value[n]. */
264 static int *reg_last_set_label;
266 /* Record the value of label_tick when an expression involving register n
267 is placed in reg_last_set_value. */
269 static int *reg_last_set_table_tick;
271 /* Set non-zero if references to register n in expressions should not be
272 used. */
274 static char *reg_last_set_invalid;
276 /* Incremented for each label. */
278 static int label_tick;
280 /* Some registers that are set more than once and used in more than one
281 basic block are nevertheless always set in similar ways. For example,
282 a QImode register may be loaded from memory in two places on a machine
283 where byte loads zero extend.
285 We record in the following array what we know about the nonzero
286 bits of a register, specifically which bits are known to be zero.
288 If an entry is zero, it means that we don't know anything special. */
290 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
292 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
293 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
295 static enum machine_mode nonzero_bits_mode;
297 /* Nonzero if we know that a register has some leading bits that are always
298 equal to the sign bit. */
300 static unsigned char *reg_sign_bit_copies;
302 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
303 It is zero while computing them and after combine has completed. This
304 former test prevents propagating values based on previously set values,
305 which can be incorrect if a variable is modified in a loop. */
307 static int nonzero_sign_valid;
309 /* These arrays are maintained in parallel with reg_last_set_value
310 and are used to store the mode in which the register was last set,
311 the bits that were known to be zero when it was last set, and the
312 number of sign bits copies it was known to have when it was last set. */
314 static enum machine_mode *reg_last_set_mode;
315 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
316 static char *reg_last_set_sign_bit_copies;
318 /* Record one modification to rtl structure
319 to be undone by storing old_contents into *where.
320 is_int is 1 if the contents are an int. */
322 struct undo
324 struct undo *next;
325 int is_int;
326 union {rtx r; unsigned int i;} old_contents;
327 union {rtx *r; unsigned int *i;} where;
330 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
331 num_undo says how many are currently recorded.
333 other_insn is nonzero if we have modified some other insn in the process
334 of working on subst_insn. It must be verified too.
336 previous_undos is the value of undobuf.undos when we started processing
337 this substitution. This will prevent gen_rtx_combine from re-used a piece
338 from the previous expression. Doing so can produce circular rtl
339 structures. */
341 struct undobuf
343 struct undo *undos;
344 struct undo *frees;
345 struct undo *previous_undos;
346 rtx other_insn;
349 static struct undobuf undobuf;
351 /* Number of times the pseudo being substituted for
352 was found and replaced. */
354 static int n_occurrences;
356 static void do_SUBST PARAMS ((rtx *, rtx));
357 static void do_SUBST_INT PARAMS ((unsigned int *,
358 unsigned int));
359 static void init_reg_last_arrays PARAMS ((void));
360 static void setup_incoming_promotions PARAMS ((void));
361 static void set_nonzero_bits_and_sign_copies PARAMS ((rtx, rtx, void *));
362 static int cant_combine_insn_p PARAMS ((rtx));
363 static int can_combine_p PARAMS ((rtx, rtx, rtx, rtx, rtx *, rtx *));
364 static int sets_function_arg_p PARAMS ((rtx));
365 static int combinable_i3pat PARAMS ((rtx, rtx *, rtx, rtx, int, rtx *));
366 static int contains_muldiv PARAMS ((rtx));
367 static rtx try_combine PARAMS ((rtx, rtx, rtx, int *));
368 static void undo_all PARAMS ((void));
369 static void undo_commit PARAMS ((void));
370 static rtx *find_split_point PARAMS ((rtx *, rtx));
371 static rtx subst PARAMS ((rtx, rtx, rtx, int, int));
372 static rtx combine_simplify_rtx PARAMS ((rtx, enum machine_mode, int, int));
373 static rtx simplify_if_then_else PARAMS ((rtx));
374 static rtx simplify_set PARAMS ((rtx));
375 static rtx simplify_logical PARAMS ((rtx, int));
376 static rtx expand_compound_operation PARAMS ((rtx));
377 static rtx expand_field_assignment PARAMS ((rtx));
378 static rtx make_extraction PARAMS ((enum machine_mode, rtx, HOST_WIDE_INT,
379 rtx, unsigned HOST_WIDE_INT, int,
380 int, int));
381 static rtx extract_left_shift PARAMS ((rtx, int));
382 static rtx make_compound_operation PARAMS ((rtx, enum rtx_code));
383 static int get_pos_from_mask PARAMS ((unsigned HOST_WIDE_INT,
384 unsigned HOST_WIDE_INT *));
385 static rtx force_to_mode PARAMS ((rtx, enum machine_mode,
386 unsigned HOST_WIDE_INT, rtx, int));
387 static rtx if_then_else_cond PARAMS ((rtx, rtx *, rtx *));
388 static rtx known_cond PARAMS ((rtx, enum rtx_code, rtx, rtx));
389 static int rtx_equal_for_field_assignment_p PARAMS ((rtx, rtx));
390 static rtx make_field_assignment PARAMS ((rtx));
391 static rtx apply_distributive_law PARAMS ((rtx));
392 static rtx simplify_and_const_int PARAMS ((rtx, enum machine_mode, rtx,
393 unsigned HOST_WIDE_INT));
394 static unsigned HOST_WIDE_INT nonzero_bits PARAMS ((rtx, enum machine_mode));
395 static unsigned int num_sign_bit_copies PARAMS ((rtx, enum machine_mode));
396 static int merge_outer_ops PARAMS ((enum rtx_code *, HOST_WIDE_INT *,
397 enum rtx_code, HOST_WIDE_INT,
398 enum machine_mode, int *));
399 static rtx simplify_shift_const PARAMS ((rtx, enum rtx_code, enum machine_mode,
400 rtx, int));
401 static int recog_for_combine PARAMS ((rtx *, rtx, rtx *));
402 static rtx gen_lowpart_for_combine PARAMS ((enum machine_mode, rtx));
403 static rtx gen_rtx_combine PARAMS ((enum rtx_code code, enum machine_mode mode,
404 ...));
405 static rtx gen_binary PARAMS ((enum rtx_code, enum machine_mode,
406 rtx, rtx));
407 static rtx gen_unary PARAMS ((enum rtx_code, enum machine_mode,
408 enum machine_mode, rtx));
409 static enum rtx_code simplify_comparison PARAMS ((enum rtx_code, rtx *, rtx *));
410 static void update_table_tick PARAMS ((rtx));
411 static void record_value_for_reg PARAMS ((rtx, rtx, rtx));
412 static void check_promoted_subreg PARAMS ((rtx, rtx));
413 static void record_dead_and_set_regs_1 PARAMS ((rtx, rtx, void *));
414 static void record_dead_and_set_regs PARAMS ((rtx));
415 static int get_last_value_validate PARAMS ((rtx *, rtx, int, int));
416 static rtx get_last_value PARAMS ((rtx));
417 static int use_crosses_set_p PARAMS ((rtx, int));
418 static void reg_dead_at_p_1 PARAMS ((rtx, rtx, void *));
419 static int reg_dead_at_p PARAMS ((rtx, rtx));
420 static void move_deaths PARAMS ((rtx, rtx, int, rtx, rtx *));
421 static int reg_bitfield_target_p PARAMS ((rtx, rtx));
422 static void distribute_notes PARAMS ((rtx, rtx, rtx, rtx, rtx, rtx));
423 static void distribute_links PARAMS ((rtx));
424 static void mark_used_regs_combine PARAMS ((rtx));
425 static int insn_cuid PARAMS ((rtx));
426 static void record_promoted_value PARAMS ((rtx, rtx));
427 static rtx reversed_comparison PARAMS ((rtx, enum machine_mode, rtx, rtx));
428 static enum rtx_code combine_reversed_comparison_code PARAMS ((rtx));
430 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
431 insn. The substitution can be undone by undo_all. If INTO is already
432 set to NEWVAL, do not record this change. Because computing NEWVAL might
433 also call SUBST, we have to compute it before we put anything into
434 the undo table. */
436 static void
437 do_SUBST (into, newval)
438 rtx *into, newval;
440 struct undo *buf;
441 rtx oldval = *into;
443 if (oldval == newval)
444 return;
446 if (undobuf.frees)
447 buf = undobuf.frees, undobuf.frees = buf->next;
448 else
449 buf = (struct undo *) xmalloc (sizeof (struct undo));
451 buf->is_int = 0;
452 buf->where.r = into;
453 buf->old_contents.r = oldval;
454 *into = newval;
456 buf->next = undobuf.undos, undobuf.undos = buf;
459 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
461 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
462 for the value of a HOST_WIDE_INT value (including CONST_INT) is
463 not safe. */
465 static void
466 do_SUBST_INT (into, newval)
467 unsigned int *into, newval;
469 struct undo *buf;
470 unsigned int oldval = *into;
472 if (oldval == newval)
473 return;
475 if (undobuf.frees)
476 buf = undobuf.frees, undobuf.frees = buf->next;
477 else
478 buf = (struct undo *) xmalloc (sizeof (struct undo));
480 buf->is_int = 1;
481 buf->where.i = into;
482 buf->old_contents.i = oldval;
483 *into = newval;
485 buf->next = undobuf.undos, undobuf.undos = buf;
488 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
490 /* Main entry point for combiner. F is the first insn of the function.
491 NREGS is the first unused pseudo-reg number.
493 Return non-zero if the combiner has turned an indirect jump
494 instruction into a direct jump. */
496 combine_instructions (f, nregs)
497 rtx f;
498 unsigned int nregs;
500 register rtx insn, next;
501 #ifdef HAVE_cc0
502 register rtx prev;
503 #endif
504 register int i;
505 register rtx links, nextlinks;
507 int new_direct_jump_p = 0;
509 combine_attempts = 0;
510 combine_merges = 0;
511 combine_extras = 0;
512 combine_successes = 0;
514 combine_max_regno = nregs;
516 reg_nonzero_bits = ((unsigned HOST_WIDE_INT *)
517 xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT)));
518 reg_sign_bit_copies
519 = (unsigned char *) xcalloc (nregs, sizeof (unsigned char));
521 reg_last_death = (rtx *) xmalloc (nregs * sizeof (rtx));
522 reg_last_set = (rtx *) xmalloc (nregs * sizeof (rtx));
523 reg_last_set_value = (rtx *) xmalloc (nregs * sizeof (rtx));
524 reg_last_set_table_tick = (int *) xmalloc (nregs * sizeof (int));
525 reg_last_set_label = (int *) xmalloc (nregs * sizeof (int));
526 reg_last_set_invalid = (char *) xmalloc (nregs * sizeof (char));
527 reg_last_set_mode
528 = (enum machine_mode *) xmalloc (nregs * sizeof (enum machine_mode));
529 reg_last_set_nonzero_bits
530 = (unsigned HOST_WIDE_INT *) xmalloc (nregs * sizeof (HOST_WIDE_INT));
531 reg_last_set_sign_bit_copies
532 = (char *) xmalloc (nregs * sizeof (char));
534 init_reg_last_arrays ();
536 init_recog_no_volatile ();
538 /* Compute maximum uid value so uid_cuid can be allocated. */
540 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
541 if (INSN_UID (insn) > i)
542 i = INSN_UID (insn);
544 uid_cuid = (int *) xmalloc ((i + 1) * sizeof (int));
545 max_uid_cuid = i;
547 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
549 /* Don't use reg_nonzero_bits when computing it. This can cause problems
550 when, for example, we have j <<= 1 in a loop. */
552 nonzero_sign_valid = 0;
554 /* Compute the mapping from uids to cuids.
555 Cuids are numbers assigned to insns, like uids,
556 except that cuids increase monotonically through the code.
558 Scan all SETs and see if we can deduce anything about what
559 bits are known to be zero for some registers and how many copies
560 of the sign bit are known to exist for those registers.
562 Also set any known values so that we can use it while searching
563 for what bits are known to be set. */
565 label_tick = 1;
567 /* We need to initialize it here, because record_dead_and_set_regs may call
568 get_last_value. */
569 subst_prev_insn = NULL_RTX;
571 setup_incoming_promotions ();
573 refresh_blocks = sbitmap_alloc (n_basic_blocks);
574 sbitmap_zero (refresh_blocks);
575 need_refresh = 0;
577 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
579 uid_cuid[INSN_UID (insn)] = ++i;
580 subst_low_cuid = i;
581 subst_insn = insn;
583 if (INSN_P (insn))
585 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
586 NULL);
587 record_dead_and_set_regs (insn);
589 #ifdef AUTO_INC_DEC
590 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
591 if (REG_NOTE_KIND (links) == REG_INC)
592 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
593 NULL);
594 #endif
597 if (GET_CODE (insn) == CODE_LABEL)
598 label_tick++;
601 nonzero_sign_valid = 1;
603 /* Now scan all the insns in forward order. */
605 this_basic_block = -1;
606 label_tick = 1;
607 last_call_cuid = 0;
608 mem_last_set = 0;
609 init_reg_last_arrays ();
610 setup_incoming_promotions ();
612 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
614 next = 0;
616 /* If INSN starts a new basic block, update our basic block number. */
617 if (this_basic_block + 1 < n_basic_blocks
618 && BLOCK_HEAD (this_basic_block + 1) == insn)
619 this_basic_block++;
621 if (GET_CODE (insn) == CODE_LABEL)
622 label_tick++;
624 else if (INSN_P (insn))
626 /* See if we know about function return values before this
627 insn based upon SUBREG flags. */
628 check_promoted_subreg (insn, PATTERN (insn));
630 /* Try this insn with each insn it links back to. */
632 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
633 if ((next = try_combine (insn, XEXP (links, 0),
634 NULL_RTX, &new_direct_jump_p)) != 0)
635 goto retry;
637 /* Try each sequence of three linked insns ending with this one. */
639 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
641 rtx link = XEXP (links, 0);
643 /* If the linked insn has been replaced by a note, then there
644 is no point in persuing this chain any further. */
645 if (GET_CODE (link) == NOTE)
646 break;
648 for (nextlinks = LOG_LINKS (link);
649 nextlinks;
650 nextlinks = XEXP (nextlinks, 1))
651 if ((next = try_combine (insn, XEXP (links, 0),
652 XEXP (nextlinks, 0),
653 &new_direct_jump_p)) != 0)
654 goto retry;
657 #ifdef HAVE_cc0
658 /* Try to combine a jump insn that uses CC0
659 with a preceding insn that sets CC0, and maybe with its
660 logical predecessor as well.
661 This is how we make decrement-and-branch insns.
662 We need this special code because data flow connections
663 via CC0 do not get entered in LOG_LINKS. */
665 if (GET_CODE (insn) == JUMP_INSN
666 && (prev = prev_nonnote_insn (insn)) != 0
667 && GET_CODE (prev) == INSN
668 && sets_cc0_p (PATTERN (prev)))
670 if ((next = try_combine (insn, prev,
671 NULL_RTX, &new_direct_jump_p)) != 0)
672 goto retry;
674 for (nextlinks = LOG_LINKS (prev); nextlinks;
675 nextlinks = XEXP (nextlinks, 1))
676 if ((next = try_combine (insn, prev,
677 XEXP (nextlinks, 0),
678 &new_direct_jump_p)) != 0)
679 goto retry;
682 /* Do the same for an insn that explicitly references CC0. */
683 if (GET_CODE (insn) == INSN
684 && (prev = prev_nonnote_insn (insn)) != 0
685 && GET_CODE (prev) == INSN
686 && sets_cc0_p (PATTERN (prev))
687 && GET_CODE (PATTERN (insn)) == SET
688 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
690 if ((next = try_combine (insn, prev,
691 NULL_RTX, &new_direct_jump_p)) != 0)
692 goto retry;
694 for (nextlinks = LOG_LINKS (prev); nextlinks;
695 nextlinks = XEXP (nextlinks, 1))
696 if ((next = try_combine (insn, prev,
697 XEXP (nextlinks, 0),
698 &new_direct_jump_p)) != 0)
699 goto retry;
702 /* Finally, see if any of the insns that this insn links to
703 explicitly references CC0. If so, try this insn, that insn,
704 and its predecessor if it sets CC0. */
705 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
706 if (GET_CODE (XEXP (links, 0)) == INSN
707 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
708 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
709 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
710 && GET_CODE (prev) == INSN
711 && sets_cc0_p (PATTERN (prev))
712 && (next = try_combine (insn, XEXP (links, 0),
713 prev, &new_direct_jump_p)) != 0)
714 goto retry;
715 #endif
717 /* Try combining an insn with two different insns whose results it
718 uses. */
719 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
720 for (nextlinks = XEXP (links, 1); nextlinks;
721 nextlinks = XEXP (nextlinks, 1))
722 if ((next = try_combine (insn, XEXP (links, 0),
723 XEXP (nextlinks, 0),
724 &new_direct_jump_p)) != 0)
725 goto retry;
727 if (GET_CODE (insn) != NOTE)
728 record_dead_and_set_regs (insn);
730 retry:
735 if (need_refresh)
737 compute_bb_for_insn (get_max_uid ());
738 update_life_info (refresh_blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
739 PROP_DEATH_NOTES);
742 /* Clean up. */
743 sbitmap_free (refresh_blocks);
744 free (reg_nonzero_bits);
745 free (reg_sign_bit_copies);
746 free (reg_last_death);
747 free (reg_last_set);
748 free (reg_last_set_value);
749 free (reg_last_set_table_tick);
750 free (reg_last_set_label);
751 free (reg_last_set_invalid);
752 free (reg_last_set_mode);
753 free (reg_last_set_nonzero_bits);
754 free (reg_last_set_sign_bit_copies);
755 free (uid_cuid);
758 struct undo *undo, *next;
759 for (undo = undobuf.frees; undo; undo = next)
761 next = undo->next;
762 free (undo);
764 undobuf.frees = 0;
767 total_attempts += combine_attempts;
768 total_merges += combine_merges;
769 total_extras += combine_extras;
770 total_successes += combine_successes;
772 nonzero_sign_valid = 0;
774 /* Make recognizer allow volatile MEMs again. */
775 init_recog ();
777 return new_direct_jump_p;
780 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
782 static void
783 init_reg_last_arrays ()
785 unsigned int nregs = combine_max_regno;
787 memset ((char *) reg_last_death, 0, nregs * sizeof (rtx));
788 memset ((char *) reg_last_set, 0, nregs * sizeof (rtx));
789 memset ((char *) reg_last_set_value, 0, nregs * sizeof (rtx));
790 memset ((char *) reg_last_set_table_tick, 0, nregs * sizeof (int));
791 memset ((char *) reg_last_set_label, 0, nregs * sizeof (int));
792 memset (reg_last_set_invalid, 0, nregs * sizeof (char));
793 memset ((char *) reg_last_set_mode, 0, nregs * sizeof (enum machine_mode));
794 memset ((char *) reg_last_set_nonzero_bits, 0, nregs * sizeof (HOST_WIDE_INT));
795 memset (reg_last_set_sign_bit_copies, 0, nregs * sizeof (char));
798 /* Set up any promoted values for incoming argument registers. */
800 static void
801 setup_incoming_promotions ()
803 #ifdef PROMOTE_FUNCTION_ARGS
804 unsigned int regno;
805 rtx reg;
806 enum machine_mode mode;
807 int unsignedp;
808 rtx first = get_insns ();
810 #ifndef OUTGOING_REGNO
811 #define OUTGOING_REGNO(N) N
812 #endif
813 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
814 /* Check whether this register can hold an incoming pointer
815 argument. FUNCTION_ARG_REGNO_P tests outgoing register
816 numbers, so translate if necessary due to register windows. */
817 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
818 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
820 record_value_for_reg
821 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
822 : SIGN_EXTEND),
823 GET_MODE (reg),
824 gen_rtx_CLOBBER (mode, const0_rtx)));
826 #endif
829 /* Called via note_stores. If X is a pseudo that is narrower than
830 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
832 If we are setting only a portion of X and we can't figure out what
833 portion, assume all bits will be used since we don't know what will
834 be happening.
836 Similarly, set how many bits of X are known to be copies of the sign bit
837 at all locations in the function. This is the smallest number implied
838 by any set of X. */
840 static void
841 set_nonzero_bits_and_sign_copies (x, set, data)
842 rtx x;
843 rtx set;
844 void *data ATTRIBUTE_UNUSED;
846 unsigned int num;
848 if (GET_CODE (x) == REG
849 && REGNO (x) >= FIRST_PSEUDO_REGISTER
850 /* If this register is undefined at the start of the file, we can't
851 say what its contents were. */
852 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, REGNO (x))
853 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
855 if (set == 0 || GET_CODE (set) == CLOBBER)
857 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
858 reg_sign_bit_copies[REGNO (x)] = 1;
859 return;
862 /* If this is a complex assignment, see if we can convert it into a
863 simple assignment. */
864 set = expand_field_assignment (set);
866 /* If this is a simple assignment, or we have a paradoxical SUBREG,
867 set what we know about X. */
869 if (SET_DEST (set) == x
870 || (GET_CODE (SET_DEST (set)) == SUBREG
871 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
872 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
873 && SUBREG_REG (SET_DEST (set)) == x))
875 rtx src = SET_SRC (set);
877 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
878 /* If X is narrower than a word and SRC is a non-negative
879 constant that would appear negative in the mode of X,
880 sign-extend it for use in reg_nonzero_bits because some
881 machines (maybe most) will actually do the sign-extension
882 and this is the conservative approach.
884 ??? For 2.5, try to tighten up the MD files in this regard
885 instead of this kludge. */
887 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
888 && GET_CODE (src) == CONST_INT
889 && INTVAL (src) > 0
890 && 0 != (INTVAL (src)
891 & ((HOST_WIDE_INT) 1
892 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
893 src = GEN_INT (INTVAL (src)
894 | ((HOST_WIDE_INT) (-1)
895 << GET_MODE_BITSIZE (GET_MODE (x))));
896 #endif
898 reg_nonzero_bits[REGNO (x)]
899 |= nonzero_bits (src, nonzero_bits_mode);
900 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
901 if (reg_sign_bit_copies[REGNO (x)] == 0
902 || reg_sign_bit_copies[REGNO (x)] > num)
903 reg_sign_bit_copies[REGNO (x)] = num;
905 else
907 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
908 reg_sign_bit_copies[REGNO (x)] = 1;
913 /* See if INSN can be combined into I3. PRED and SUCC are optionally
914 insns that were previously combined into I3 or that will be combined
915 into the merger of INSN and I3.
917 Return 0 if the combination is not allowed for any reason.
919 If the combination is allowed, *PDEST will be set to the single
920 destination of INSN and *PSRC to the single source, and this function
921 will return 1. */
923 static int
924 can_combine_p (insn, i3, pred, succ, pdest, psrc)
925 rtx insn;
926 rtx i3;
927 rtx pred ATTRIBUTE_UNUSED;
928 rtx succ;
929 rtx *pdest, *psrc;
931 int i;
932 rtx set = 0, src, dest;
933 rtx p;
934 #ifdef AUTO_INC_DEC
935 rtx link;
936 #endif
937 int all_adjacent = (succ ? (next_active_insn (insn) == succ
938 && next_active_insn (succ) == i3)
939 : next_active_insn (insn) == i3);
941 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
942 or a PARALLEL consisting of such a SET and CLOBBERs.
944 If INSN has CLOBBER parallel parts, ignore them for our processing.
945 By definition, these happen during the execution of the insn. When it
946 is merged with another insn, all bets are off. If they are, in fact,
947 needed and aren't also supplied in I3, they may be added by
948 recog_for_combine. Otherwise, it won't match.
950 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
951 note.
953 Get the source and destination of INSN. If more than one, can't
954 combine. */
956 if (GET_CODE (PATTERN (insn)) == SET)
957 set = PATTERN (insn);
958 else if (GET_CODE (PATTERN (insn)) == PARALLEL
959 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
961 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
963 rtx elt = XVECEXP (PATTERN (insn), 0, i);
965 switch (GET_CODE (elt))
967 /* This is important to combine floating point insns
968 for the SH4 port. */
969 case USE:
970 /* Combining an isolated USE doesn't make sense.
971 We depend here on combinable_i3_pat to reject them. */
972 /* The code below this loop only verifies that the inputs of
973 the SET in INSN do not change. We call reg_set_between_p
974 to verify that the REG in the USE does not change betweeen
975 I3 and INSN.
976 If the USE in INSN was for a pseudo register, the matching
977 insn pattern will likely match any register; combining this
978 with any other USE would only be safe if we knew that the
979 used registers have identical values, or if there was
980 something to tell them apart, e.g. different modes. For
981 now, we forgo such compilcated tests and simply disallow
982 combining of USES of pseudo registers with any other USE. */
983 if (GET_CODE (XEXP (elt, 0)) == REG
984 && GET_CODE (PATTERN (i3)) == PARALLEL)
986 rtx i3pat = PATTERN (i3);
987 int i = XVECLEN (i3pat, 0) - 1;
988 unsigned int regno = REGNO (XEXP (elt, 0));
992 rtx i3elt = XVECEXP (i3pat, 0, i);
994 if (GET_CODE (i3elt) == USE
995 && GET_CODE (XEXP (i3elt, 0)) == REG
996 && (REGNO (XEXP (i3elt, 0)) == regno
997 ? reg_set_between_p (XEXP (elt, 0),
998 PREV_INSN (insn), i3)
999 : regno >= FIRST_PSEUDO_REGISTER))
1000 return 0;
1002 while (--i >= 0);
1004 break;
1006 /* We can ignore CLOBBERs. */
1007 case CLOBBER:
1008 break;
1010 case SET:
1011 /* Ignore SETs whose result isn't used but not those that
1012 have side-effects. */
1013 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1014 && ! side_effects_p (elt))
1015 break;
1017 /* If we have already found a SET, this is a second one and
1018 so we cannot combine with this insn. */
1019 if (set)
1020 return 0;
1022 set = elt;
1023 break;
1025 default:
1026 /* Anything else means we can't combine. */
1027 return 0;
1031 if (set == 0
1032 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1033 so don't do anything with it. */
1034 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1035 return 0;
1037 else
1038 return 0;
1040 if (set == 0)
1041 return 0;
1043 set = expand_field_assignment (set);
1044 src = SET_SRC (set), dest = SET_DEST (set);
1046 /* Don't eliminate a store in the stack pointer. */
1047 if (dest == stack_pointer_rtx
1048 /* If we couldn't eliminate a field assignment, we can't combine. */
1049 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
1050 /* Don't combine with an insn that sets a register to itself if it has
1051 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1052 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1053 /* Can't merge an ASM_OPERANDS. */
1054 || GET_CODE (src) == ASM_OPERANDS
1055 /* Can't merge a function call. */
1056 || GET_CODE (src) == CALL
1057 /* Don't eliminate a function call argument. */
1058 || (GET_CODE (i3) == CALL_INSN
1059 && (find_reg_fusage (i3, USE, dest)
1060 || (GET_CODE (dest) == REG
1061 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1062 && global_regs[REGNO (dest)])))
1063 /* Don't substitute into an incremented register. */
1064 || FIND_REG_INC_NOTE (i3, dest)
1065 || (succ && FIND_REG_INC_NOTE (succ, dest))
1066 #if 0
1067 /* Don't combine the end of a libcall into anything. */
1068 /* ??? This gives worse code, and appears to be unnecessary, since no
1069 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1070 use REG_RETVAL notes for noconflict blocks, but other code here
1071 makes sure that those insns don't disappear. */
1072 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1073 #endif
1074 /* Make sure that DEST is not used after SUCC but before I3. */
1075 || (succ && ! all_adjacent
1076 && reg_used_between_p (dest, succ, i3))
1077 /* Make sure that the value that is to be substituted for the register
1078 does not use any registers whose values alter in between. However,
1079 If the insns are adjacent, a use can't cross a set even though we
1080 think it might (this can happen for a sequence of insns each setting
1081 the same destination; reg_last_set of that register might point to
1082 a NOTE). If INSN has a REG_EQUIV note, the register is always
1083 equivalent to the memory so the substitution is valid even if there
1084 are intervening stores. Also, don't move a volatile asm or
1085 UNSPEC_VOLATILE across any other insns. */
1086 || (! all_adjacent
1087 && (((GET_CODE (src) != MEM
1088 || ! find_reg_note (insn, REG_EQUIV, src))
1089 && use_crosses_set_p (src, INSN_CUID (insn)))
1090 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1091 || GET_CODE (src) == UNSPEC_VOLATILE))
1092 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1093 better register allocation by not doing the combine. */
1094 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1095 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1096 /* Don't combine across a CALL_INSN, because that would possibly
1097 change whether the life span of some REGs crosses calls or not,
1098 and it is a pain to update that information.
1099 Exception: if source is a constant, moving it later can't hurt.
1100 Accept that special case, because it helps -fforce-addr a lot. */
1101 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1102 return 0;
1104 /* DEST must either be a REG or CC0. */
1105 if (GET_CODE (dest) == REG)
1107 /* If register alignment is being enforced for multi-word items in all
1108 cases except for parameters, it is possible to have a register copy
1109 insn referencing a hard register that is not allowed to contain the
1110 mode being copied and which would not be valid as an operand of most
1111 insns. Eliminate this problem by not combining with such an insn.
1113 Also, on some machines we don't want to extend the life of a hard
1114 register. */
1116 if (GET_CODE (src) == REG
1117 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1118 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1119 /* Don't extend the life of a hard register unless it is
1120 user variable (if we have few registers) or it can't
1121 fit into the desired register (meaning something special
1122 is going on).
1123 Also avoid substituting a return register into I3, because
1124 reload can't handle a conflict with constraints of other
1125 inputs. */
1126 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1127 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1128 return 0;
1130 else if (GET_CODE (dest) != CC0)
1131 return 0;
1133 /* Don't substitute for a register intended as a clobberable operand.
1134 Similarly, don't substitute an expression containing a register that
1135 will be clobbered in I3. */
1136 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1137 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1138 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1139 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1140 src)
1141 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1142 return 0;
1144 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1145 or not), reject, unless nothing volatile comes between it and I3 */
1147 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1149 /* Make sure succ doesn't contain a volatile reference. */
1150 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1151 return 0;
1153 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1154 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1155 return 0;
1158 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1159 to be an explicit register variable, and was chosen for a reason. */
1161 if (GET_CODE (src) == ASM_OPERANDS
1162 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1163 return 0;
1165 /* If there are any volatile insns between INSN and I3, reject, because
1166 they might affect machine state. */
1168 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1169 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1170 return 0;
1172 /* If INSN or I2 contains an autoincrement or autodecrement,
1173 make sure that register is not used between there and I3,
1174 and not already used in I3 either.
1175 Also insist that I3 not be a jump; if it were one
1176 and the incremented register were spilled, we would lose. */
1178 #ifdef AUTO_INC_DEC
1179 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1180 if (REG_NOTE_KIND (link) == REG_INC
1181 && (GET_CODE (i3) == JUMP_INSN
1182 || reg_used_between_p (XEXP (link, 0), insn, i3)
1183 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1184 return 0;
1185 #endif
1187 #ifdef HAVE_cc0
1188 /* Don't combine an insn that follows a CC0-setting insn.
1189 An insn that uses CC0 must not be separated from the one that sets it.
1190 We do, however, allow I2 to follow a CC0-setting insn if that insn
1191 is passed as I1; in that case it will be deleted also.
1192 We also allow combining in this case if all the insns are adjacent
1193 because that would leave the two CC0 insns adjacent as well.
1194 It would be more logical to test whether CC0 occurs inside I1 or I2,
1195 but that would be much slower, and this ought to be equivalent. */
1197 p = prev_nonnote_insn (insn);
1198 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1199 && ! all_adjacent)
1200 return 0;
1201 #endif
1203 /* If we get here, we have passed all the tests and the combination is
1204 to be allowed. */
1206 *pdest = dest;
1207 *psrc = src;
1209 return 1;
1212 /* Check if PAT is an insn - or a part of it - used to set up an
1213 argument for a function in a hard register. */
1215 static int
1216 sets_function_arg_p (pat)
1217 rtx pat;
1219 int i;
1220 rtx inner_dest;
1222 switch (GET_CODE (pat))
1224 case INSN:
1225 return sets_function_arg_p (PATTERN (pat));
1227 case PARALLEL:
1228 for (i = XVECLEN (pat, 0); --i >= 0;)
1229 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1230 return 1;
1232 break;
1234 case SET:
1235 inner_dest = SET_DEST (pat);
1236 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1237 || GET_CODE (inner_dest) == SUBREG
1238 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1239 inner_dest = XEXP (inner_dest, 0);
1241 return (GET_CODE (inner_dest) == REG
1242 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1243 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1245 default:
1246 break;
1249 return 0;
1252 /* LOC is the location within I3 that contains its pattern or the component
1253 of a PARALLEL of the pattern. We validate that it is valid for combining.
1255 One problem is if I3 modifies its output, as opposed to replacing it
1256 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1257 so would produce an insn that is not equivalent to the original insns.
1259 Consider:
1261 (set (reg:DI 101) (reg:DI 100))
1262 (set (subreg:SI (reg:DI 101) 0) <foo>)
1264 This is NOT equivalent to:
1266 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1267 (set (reg:DI 101) (reg:DI 100))])
1269 Not only does this modify 100 (in which case it might still be valid
1270 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1272 We can also run into a problem if I2 sets a register that I1
1273 uses and I1 gets directly substituted into I3 (not via I2). In that
1274 case, we would be getting the wrong value of I2DEST into I3, so we
1275 must reject the combination. This case occurs when I2 and I1 both
1276 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1277 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1278 of a SET must prevent combination from occurring.
1280 Before doing the above check, we first try to expand a field assignment
1281 into a set of logical operations.
1283 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1284 we place a register that is both set and used within I3. If more than one
1285 such register is detected, we fail.
1287 Return 1 if the combination is valid, zero otherwise. */
1289 static int
1290 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1291 rtx i3;
1292 rtx *loc;
1293 rtx i2dest;
1294 rtx i1dest;
1295 int i1_not_in_src;
1296 rtx *pi3dest_killed;
1298 rtx x = *loc;
1300 if (GET_CODE (x) == SET)
1302 rtx set = expand_field_assignment (x);
1303 rtx dest = SET_DEST (set);
1304 rtx src = SET_SRC (set);
1305 rtx inner_dest = dest;
1307 #if 0
1308 rtx inner_src = src;
1309 #endif
1311 SUBST (*loc, set);
1313 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1314 || GET_CODE (inner_dest) == SUBREG
1315 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1316 inner_dest = XEXP (inner_dest, 0);
1318 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1319 was added. */
1320 #if 0
1321 while (GET_CODE (inner_src) == STRICT_LOW_PART
1322 || GET_CODE (inner_src) == SUBREG
1323 || GET_CODE (inner_src) == ZERO_EXTRACT)
1324 inner_src = XEXP (inner_src, 0);
1326 /* If it is better that two different modes keep two different pseudos,
1327 avoid combining them. This avoids producing the following pattern
1328 on a 386:
1329 (set (subreg:SI (reg/v:QI 21) 0)
1330 (lshiftrt:SI (reg/v:SI 20)
1331 (const_int 24)))
1332 If that were made, reload could not handle the pair of
1333 reg 20/21, since it would try to get any GENERAL_REGS
1334 but some of them don't handle QImode. */
1336 if (rtx_equal_p (inner_src, i2dest)
1337 && GET_CODE (inner_dest) == REG
1338 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1339 return 0;
1340 #endif
1342 /* Check for the case where I3 modifies its output, as
1343 discussed above. */
1344 if ((inner_dest != dest
1345 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1346 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1348 /* This is the same test done in can_combine_p except we can't test
1349 all_adjacent; we don't have to, since this instruction will stay
1350 in place, thus we are not considering increasing the lifetime of
1351 INNER_DEST.
1353 Also, if this insn sets a function argument, combining it with
1354 something that might need a spill could clobber a previous
1355 function argument; the all_adjacent test in can_combine_p also
1356 checks this; here, we do a more specific test for this case. */
1358 || (GET_CODE (inner_dest) == REG
1359 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1360 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1361 GET_MODE (inner_dest))))
1362 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1363 return 0;
1365 /* If DEST is used in I3, it is being killed in this insn,
1366 so record that for later.
1367 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1368 STACK_POINTER_REGNUM, since these are always considered to be
1369 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1370 if (pi3dest_killed && GET_CODE (dest) == REG
1371 && reg_referenced_p (dest, PATTERN (i3))
1372 && REGNO (dest) != FRAME_POINTER_REGNUM
1373 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1374 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1375 #endif
1376 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1377 && (REGNO (dest) != ARG_POINTER_REGNUM
1378 || ! fixed_regs [REGNO (dest)])
1379 #endif
1380 && REGNO (dest) != STACK_POINTER_REGNUM)
1382 if (*pi3dest_killed)
1383 return 0;
1385 *pi3dest_killed = dest;
1389 else if (GET_CODE (x) == PARALLEL)
1391 int i;
1393 for (i = 0; i < XVECLEN (x, 0); i++)
1394 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1395 i1_not_in_src, pi3dest_killed))
1396 return 0;
1399 return 1;
1402 /* Return 1 if X is an arithmetic expression that contains a multiplication
1403 and division. We don't count multiplications by powers of two here. */
1405 static int
1406 contains_muldiv (x)
1407 rtx x;
1409 switch (GET_CODE (x))
1411 case MOD: case DIV: case UMOD: case UDIV:
1412 return 1;
1414 case MULT:
1415 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1416 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1417 default:
1418 switch (GET_RTX_CLASS (GET_CODE (x)))
1420 case 'c': case '<': case '2':
1421 return contains_muldiv (XEXP (x, 0))
1422 || contains_muldiv (XEXP (x, 1));
1424 case '1':
1425 return contains_muldiv (XEXP (x, 0));
1427 default:
1428 return 0;
1433 /* Determine whether INSN can be used in a combination. Return nonzero if
1434 not. This is used in try_combine to detect early some cases where we
1435 can't perform combinations. */
1437 static int
1438 cant_combine_insn_p (insn)
1439 rtx insn;
1441 rtx set;
1442 rtx src, dest;
1444 /* If this isn't really an insn, we can't do anything.
1445 This can occur when flow deletes an insn that it has merged into an
1446 auto-increment address. */
1447 if (! INSN_P (insn))
1448 return 1;
1450 /* Never combine loads and stores involving hard regs. The register
1451 allocator can usually handle such reg-reg moves by tying. If we allow
1452 the combiner to make substitutions of hard regs, we risk aborting in
1453 reload on machines that have SMALL_REGISTER_CLASSES.
1454 As an exception, we allow combinations involving fixed regs; these are
1455 not available to the register allocator so there's no risk involved. */
1457 set = single_set (insn);
1458 if (! set)
1459 return 0;
1460 src = SET_SRC (set);
1461 dest = SET_DEST (set);
1462 if (GET_CODE (src) == SUBREG)
1463 src = SUBREG_REG (src);
1464 if (GET_CODE (dest) == SUBREG)
1465 dest = SUBREG_REG (dest);
1466 if (REG_P (src) && REG_P (dest)
1467 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1468 && ! fixed_regs[REGNO (src)])
1469 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1470 && ! fixed_regs[REGNO (dest)])))
1471 return 1;
1473 return 0;
1476 /* Try to combine the insns I1 and I2 into I3.
1477 Here I1 and I2 appear earlier than I3.
1478 I1 can be zero; then we combine just I2 into I3.
1480 It we are combining three insns and the resulting insn is not recognized,
1481 try splitting it into two insns. If that happens, I2 and I3 are retained
1482 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1483 are pseudo-deleted.
1485 Return 0 if the combination does not work. Then nothing is changed.
1486 If we did the combination, return the insn at which combine should
1487 resume scanning.
1489 Set NEW_DIRECT_JUMP_P to a non-zero value if try_combine creates a
1490 new direct jump instruction. */
1492 static rtx
1493 try_combine (i3, i2, i1, new_direct_jump_p)
1494 register rtx i3, i2, i1;
1495 register int *new_direct_jump_p;
1497 /* New patterns for I3 and I2, respectively. */
1498 rtx newpat, newi2pat = 0;
1499 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1500 int added_sets_1, added_sets_2;
1501 /* Total number of SETs to put into I3. */
1502 int total_sets;
1503 /* Nonzero is I2's body now appears in I3. */
1504 int i2_is_used;
1505 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1506 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1507 /* Contains I3 if the destination of I3 is used in its source, which means
1508 that the old life of I3 is being killed. If that usage is placed into
1509 I2 and not in I3, a REG_DEAD note must be made. */
1510 rtx i3dest_killed = 0;
1511 /* SET_DEST and SET_SRC of I2 and I1. */
1512 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1513 /* PATTERN (I2), or a copy of it in certain cases. */
1514 rtx i2pat;
1515 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1516 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1517 int i1_feeds_i3 = 0;
1518 /* Notes that must be added to REG_NOTES in I3 and I2. */
1519 rtx new_i3_notes, new_i2_notes;
1520 /* Notes that we substituted I3 into I2 instead of the normal case. */
1521 int i3_subst_into_i2 = 0;
1522 /* Notes that I1, I2 or I3 is a MULT operation. */
1523 int have_mult = 0;
1525 int maxreg;
1526 rtx temp;
1527 register rtx link;
1528 int i;
1530 /* Exit early if one of the insns involved can't be used for
1531 combinations. */
1532 if (cant_combine_insn_p (i3)
1533 || cant_combine_insn_p (i2)
1534 || (i1 && cant_combine_insn_p (i1))
1535 /* We also can't do anything if I3 has a
1536 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1537 libcall. */
1538 #if 0
1539 /* ??? This gives worse code, and appears to be unnecessary, since no
1540 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1541 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1542 #endif
1544 return 0;
1546 combine_attempts++;
1547 undobuf.other_insn = 0;
1549 /* Reset the hard register usage information. */
1550 CLEAR_HARD_REG_SET (newpat_used_regs);
1552 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1553 code below, set I1 to be the earlier of the two insns. */
1554 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1555 temp = i1, i1 = i2, i2 = temp;
1557 added_links_insn = 0;
1559 /* First check for one important special-case that the code below will
1560 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1561 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1562 we may be able to replace that destination with the destination of I3.
1563 This occurs in the common code where we compute both a quotient and
1564 remainder into a structure, in which case we want to do the computation
1565 directly into the structure to avoid register-register copies.
1567 Note that this case handles both multiple sets in I2 and also
1568 cases where I2 has a number of CLOBBER or PARALLELs.
1570 We make very conservative checks below and only try to handle the
1571 most common cases of this. For example, we only handle the case
1572 where I2 and I3 are adjacent to avoid making difficult register
1573 usage tests. */
1575 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1576 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1577 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1578 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1579 && GET_CODE (PATTERN (i2)) == PARALLEL
1580 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1581 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1582 below would need to check what is inside (and reg_overlap_mentioned_p
1583 doesn't support those codes anyway). Don't allow those destinations;
1584 the resulting insn isn't likely to be recognized anyway. */
1585 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1586 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1587 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1588 SET_DEST (PATTERN (i3)))
1589 && next_real_insn (i2) == i3)
1591 rtx p2 = PATTERN (i2);
1593 /* Make sure that the destination of I3,
1594 which we are going to substitute into one output of I2,
1595 is not used within another output of I2. We must avoid making this:
1596 (parallel [(set (mem (reg 69)) ...)
1597 (set (reg 69) ...)])
1598 which is not well-defined as to order of actions.
1599 (Besides, reload can't handle output reloads for this.)
1601 The problem can also happen if the dest of I3 is a memory ref,
1602 if another dest in I2 is an indirect memory ref. */
1603 for (i = 0; i < XVECLEN (p2, 0); i++)
1604 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1605 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1606 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1607 SET_DEST (XVECEXP (p2, 0, i))))
1608 break;
1610 if (i == XVECLEN (p2, 0))
1611 for (i = 0; i < XVECLEN (p2, 0); i++)
1612 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1613 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1614 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1616 combine_merges++;
1618 subst_insn = i3;
1619 subst_low_cuid = INSN_CUID (i2);
1621 added_sets_2 = added_sets_1 = 0;
1622 i2dest = SET_SRC (PATTERN (i3));
1624 /* Replace the dest in I2 with our dest and make the resulting
1625 insn the new pattern for I3. Then skip to where we
1626 validate the pattern. Everything was set up above. */
1627 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1628 SET_DEST (PATTERN (i3)));
1630 newpat = p2;
1631 i3_subst_into_i2 = 1;
1632 goto validate_replacement;
1636 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1637 one of those words to another constant, merge them by making a new
1638 constant. */
1639 if (i1 == 0
1640 && (temp = single_set (i2)) != 0
1641 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1642 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1643 && GET_CODE (SET_DEST (temp)) == REG
1644 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1645 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1646 && GET_CODE (PATTERN (i3)) == SET
1647 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1648 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1649 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1650 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1651 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1653 HOST_WIDE_INT lo, hi;
1655 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1656 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1657 else
1659 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1660 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1663 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1664 lo = INTVAL (SET_SRC (PATTERN (i3)));
1665 else
1666 hi = INTVAL (SET_SRC (PATTERN (i3)));
1668 combine_merges++;
1669 subst_insn = i3;
1670 subst_low_cuid = INSN_CUID (i2);
1671 added_sets_2 = added_sets_1 = 0;
1672 i2dest = SET_DEST (temp);
1674 SUBST (SET_SRC (temp),
1675 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1677 newpat = PATTERN (i2);
1678 goto validate_replacement;
1681 #ifndef HAVE_cc0
1682 /* If we have no I1 and I2 looks like:
1683 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1684 (set Y OP)])
1685 make up a dummy I1 that is
1686 (set Y OP)
1687 and change I2 to be
1688 (set (reg:CC X) (compare:CC Y (const_int 0)))
1690 (We can ignore any trailing CLOBBERs.)
1692 This undoes a previous combination and allows us to match a branch-and-
1693 decrement insn. */
1695 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1696 && XVECLEN (PATTERN (i2), 0) >= 2
1697 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1698 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1699 == MODE_CC)
1700 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1701 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1702 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1703 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1704 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1705 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1707 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1708 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1709 break;
1711 if (i == 1)
1713 /* We make I1 with the same INSN_UID as I2. This gives it
1714 the same INSN_CUID for value tracking. Our fake I1 will
1715 never appear in the insn stream so giving it the same INSN_UID
1716 as I2 will not cause a problem. */
1718 subst_prev_insn = i1
1719 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1720 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1721 NULL_RTX);
1723 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1724 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1725 SET_DEST (PATTERN (i1)));
1728 #endif
1730 /* Verify that I2 and I1 are valid for combining. */
1731 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1732 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1734 undo_all ();
1735 return 0;
1738 /* Record whether I2DEST is used in I2SRC and similarly for the other
1739 cases. Knowing this will help in register status updating below. */
1740 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1741 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1742 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1744 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1745 in I2SRC. */
1746 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1748 /* Ensure that I3's pattern can be the destination of combines. */
1749 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1750 i1 && i2dest_in_i1src && i1_feeds_i3,
1751 &i3dest_killed))
1753 undo_all ();
1754 return 0;
1757 /* See if any of the insns is a MULT operation. Unless one is, we will
1758 reject a combination that is, since it must be slower. Be conservative
1759 here. */
1760 if (GET_CODE (i2src) == MULT
1761 || (i1 != 0 && GET_CODE (i1src) == MULT)
1762 || (GET_CODE (PATTERN (i3)) == SET
1763 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1764 have_mult = 1;
1766 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1767 We used to do this EXCEPT in one case: I3 has a post-inc in an
1768 output operand. However, that exception can give rise to insns like
1769 mov r3,(r3)+
1770 which is a famous insn on the PDP-11 where the value of r3 used as the
1771 source was model-dependent. Avoid this sort of thing. */
1773 #if 0
1774 if (!(GET_CODE (PATTERN (i3)) == SET
1775 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1776 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1777 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1778 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1779 /* It's not the exception. */
1780 #endif
1781 #ifdef AUTO_INC_DEC
1782 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1783 if (REG_NOTE_KIND (link) == REG_INC
1784 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1785 || (i1 != 0
1786 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1788 undo_all ();
1789 return 0;
1791 #endif
1793 /* See if the SETs in I1 or I2 need to be kept around in the merged
1794 instruction: whenever the value set there is still needed past I3.
1795 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1797 For the SET in I1, we have two cases: If I1 and I2 independently
1798 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1799 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1800 in I1 needs to be kept around unless I1DEST dies or is set in either
1801 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1802 I1DEST. If so, we know I1 feeds into I2. */
1804 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1806 added_sets_1
1807 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1808 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1810 /* If the set in I2 needs to be kept around, we must make a copy of
1811 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1812 PATTERN (I2), we are only substituting for the original I1DEST, not into
1813 an already-substituted copy. This also prevents making self-referential
1814 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1815 I2DEST. */
1817 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1818 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1819 : PATTERN (i2));
1821 if (added_sets_2)
1822 i2pat = copy_rtx (i2pat);
1824 combine_merges++;
1826 /* Substitute in the latest insn for the regs set by the earlier ones. */
1828 maxreg = max_reg_num ();
1830 subst_insn = i3;
1832 /* It is possible that the source of I2 or I1 may be performing an
1833 unneeded operation, such as a ZERO_EXTEND of something that is known
1834 to have the high part zero. Handle that case by letting subst look at
1835 the innermost one of them.
1837 Another way to do this would be to have a function that tries to
1838 simplify a single insn instead of merging two or more insns. We don't
1839 do this because of the potential of infinite loops and because
1840 of the potential extra memory required. However, doing it the way
1841 we are is a bit of a kludge and doesn't catch all cases.
1843 But only do this if -fexpensive-optimizations since it slows things down
1844 and doesn't usually win. */
1846 if (flag_expensive_optimizations)
1848 /* Pass pc_rtx so no substitutions are done, just simplifications.
1849 The cases that we are interested in here do not involve the few
1850 cases were is_replaced is checked. */
1851 if (i1)
1853 subst_low_cuid = INSN_CUID (i1);
1854 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1856 else
1858 subst_low_cuid = INSN_CUID (i2);
1859 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1862 undobuf.previous_undos = undobuf.undos;
1865 #ifndef HAVE_cc0
1866 /* Many machines that don't use CC0 have insns that can both perform an
1867 arithmetic operation and set the condition code. These operations will
1868 be represented as a PARALLEL with the first element of the vector
1869 being a COMPARE of an arithmetic operation with the constant zero.
1870 The second element of the vector will set some pseudo to the result
1871 of the same arithmetic operation. If we simplify the COMPARE, we won't
1872 match such a pattern and so will generate an extra insn. Here we test
1873 for this case, where both the comparison and the operation result are
1874 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1875 I2SRC. Later we will make the PARALLEL that contains I2. */
1877 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1878 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1879 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1880 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1882 #ifdef EXTRA_CC_MODES
1883 rtx *cc_use;
1884 enum machine_mode compare_mode;
1885 #endif
1887 newpat = PATTERN (i3);
1888 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1890 i2_is_used = 1;
1892 #ifdef EXTRA_CC_MODES
1893 /* See if a COMPARE with the operand we substituted in should be done
1894 with the mode that is currently being used. If not, do the same
1895 processing we do in `subst' for a SET; namely, if the destination
1896 is used only once, try to replace it with a register of the proper
1897 mode and also replace the COMPARE. */
1898 if (undobuf.other_insn == 0
1899 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1900 &undobuf.other_insn))
1901 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1902 i2src, const0_rtx))
1903 != GET_MODE (SET_DEST (newpat))))
1905 unsigned int regno = REGNO (SET_DEST (newpat));
1906 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1908 if (regno < FIRST_PSEUDO_REGISTER
1909 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1910 && ! REG_USERVAR_P (SET_DEST (newpat))))
1912 if (regno >= FIRST_PSEUDO_REGISTER)
1913 SUBST (regno_reg_rtx[regno], new_dest);
1915 SUBST (SET_DEST (newpat), new_dest);
1916 SUBST (XEXP (*cc_use, 0), new_dest);
1917 SUBST (SET_SRC (newpat),
1918 gen_rtx_combine (COMPARE, compare_mode,
1919 i2src, const0_rtx));
1921 else
1922 undobuf.other_insn = 0;
1924 #endif
1926 else
1927 #endif
1929 n_occurrences = 0; /* `subst' counts here */
1931 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1932 need to make a unique copy of I2SRC each time we substitute it
1933 to avoid self-referential rtl. */
1935 subst_low_cuid = INSN_CUID (i2);
1936 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1937 ! i1_feeds_i3 && i1dest_in_i1src);
1938 undobuf.previous_undos = undobuf.undos;
1940 /* Record whether i2's body now appears within i3's body. */
1941 i2_is_used = n_occurrences;
1944 /* If we already got a failure, don't try to do more. Otherwise,
1945 try to substitute in I1 if we have it. */
1947 if (i1 && GET_CODE (newpat) != CLOBBER)
1949 /* Before we can do this substitution, we must redo the test done
1950 above (see detailed comments there) that ensures that I1DEST
1951 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1953 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1954 0, NULL_PTR))
1956 undo_all ();
1957 return 0;
1960 n_occurrences = 0;
1961 subst_low_cuid = INSN_CUID (i1);
1962 newpat = subst (newpat, i1dest, i1src, 0, 0);
1963 undobuf.previous_undos = undobuf.undos;
1966 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1967 to count all the ways that I2SRC and I1SRC can be used. */
1968 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1969 && i2_is_used + added_sets_2 > 1)
1970 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1971 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1972 > 1))
1973 /* Fail if we tried to make a new register (we used to abort, but there's
1974 really no reason to). */
1975 || max_reg_num () != maxreg
1976 /* Fail if we couldn't do something and have a CLOBBER. */
1977 || GET_CODE (newpat) == CLOBBER
1978 /* Fail if this new pattern is a MULT and we didn't have one before
1979 at the outer level. */
1980 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1981 && ! have_mult))
1983 undo_all ();
1984 return 0;
1987 /* If the actions of the earlier insns must be kept
1988 in addition to substituting them into the latest one,
1989 we must make a new PARALLEL for the latest insn
1990 to hold additional the SETs. */
1992 if (added_sets_1 || added_sets_2)
1994 combine_extras++;
1996 if (GET_CODE (newpat) == PARALLEL)
1998 rtvec old = XVEC (newpat, 0);
1999 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2000 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2001 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
2002 sizeof (old->elem[0]) * old->num_elem);
2004 else
2006 rtx old = newpat;
2007 total_sets = 1 + added_sets_1 + added_sets_2;
2008 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2009 XVECEXP (newpat, 0, 0) = old;
2012 if (added_sets_1)
2013 XVECEXP (newpat, 0, --total_sets)
2014 = (GET_CODE (PATTERN (i1)) == PARALLEL
2015 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2017 if (added_sets_2)
2019 /* If there is no I1, use I2's body as is. We used to also not do
2020 the subst call below if I2 was substituted into I3,
2021 but that could lose a simplification. */
2022 if (i1 == 0)
2023 XVECEXP (newpat, 0, --total_sets) = i2pat;
2024 else
2025 /* See comment where i2pat is assigned. */
2026 XVECEXP (newpat, 0, --total_sets)
2027 = subst (i2pat, i1dest, i1src, 0, 0);
2031 /* We come here when we are replacing a destination in I2 with the
2032 destination of I3. */
2033 validate_replacement:
2035 /* Note which hard regs this insn has as inputs. */
2036 mark_used_regs_combine (newpat);
2038 /* Is the result of combination a valid instruction? */
2039 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2041 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2042 the second SET's destination is a register that is unused. In that case,
2043 we just need the first SET. This can occur when simplifying a divmod
2044 insn. We *must* test for this case here because the code below that
2045 splits two independent SETs doesn't handle this case correctly when it
2046 updates the register status. Also check the case where the first
2047 SET's destination is unused. That would not cause incorrect code, but
2048 does cause an unneeded insn to remain. */
2050 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2051 && XVECLEN (newpat, 0) == 2
2052 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2053 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2054 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
2055 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
2056 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
2057 && asm_noperands (newpat) < 0)
2059 newpat = XVECEXP (newpat, 0, 0);
2060 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2063 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2064 && XVECLEN (newpat, 0) == 2
2065 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2066 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2067 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
2068 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
2069 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
2070 && asm_noperands (newpat) < 0)
2072 newpat = XVECEXP (newpat, 0, 1);
2073 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2076 /* If we were combining three insns and the result is a simple SET
2077 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2078 insns. There are two ways to do this. It can be split using a
2079 machine-specific method (like when you have an addition of a large
2080 constant) or by combine in the function find_split_point. */
2082 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2083 && asm_noperands (newpat) < 0)
2085 rtx m_split, *split;
2086 rtx ni2dest = i2dest;
2088 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2089 use I2DEST as a scratch register will help. In the latter case,
2090 convert I2DEST to the mode of the source of NEWPAT if we can. */
2092 m_split = split_insns (newpat, i3);
2094 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2095 inputs of NEWPAT. */
2097 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2098 possible to try that as a scratch reg. This would require adding
2099 more code to make it work though. */
2101 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2103 /* If I2DEST is a hard register or the only use of a pseudo,
2104 we can change its mode. */
2105 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2106 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2107 && GET_CODE (i2dest) == REG
2108 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2109 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2110 && ! REG_USERVAR_P (i2dest))))
2111 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2112 REGNO (i2dest));
2114 m_split = split_insns (gen_rtx_PARALLEL
2115 (VOIDmode,
2116 gen_rtvec (2, newpat,
2117 gen_rtx_CLOBBER (VOIDmode,
2118 ni2dest))),
2119 i3);
2122 if (m_split && GET_CODE (m_split) != SEQUENCE)
2124 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2125 if (insn_code_number >= 0)
2126 newpat = m_split;
2128 else if (m_split && GET_CODE (m_split) == SEQUENCE
2129 && XVECLEN (m_split, 0) == 2
2130 && (next_real_insn (i2) == i3
2131 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
2132 INSN_CUID (i2))))
2134 rtx i2set, i3set;
2135 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
2136 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
2138 i3set = single_set (XVECEXP (m_split, 0, 1));
2139 i2set = single_set (XVECEXP (m_split, 0, 0));
2141 /* In case we changed the mode of I2DEST, replace it in the
2142 pseudo-register table here. We can't do it above in case this
2143 code doesn't get executed and we do a split the other way. */
2145 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2146 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2148 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2150 /* If I2 or I3 has multiple SETs, we won't know how to track
2151 register status, so don't use these insns. If I2's destination
2152 is used between I2 and I3, we also can't use these insns. */
2154 if (i2_code_number >= 0 && i2set && i3set
2155 && (next_real_insn (i2) == i3
2156 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2157 insn_code_number = recog_for_combine (&newi3pat, i3,
2158 &new_i3_notes);
2159 if (insn_code_number >= 0)
2160 newpat = newi3pat;
2162 /* It is possible that both insns now set the destination of I3.
2163 If so, we must show an extra use of it. */
2165 if (insn_code_number >= 0)
2167 rtx new_i3_dest = SET_DEST (i3set);
2168 rtx new_i2_dest = SET_DEST (i2set);
2170 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2171 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2172 || GET_CODE (new_i3_dest) == SUBREG)
2173 new_i3_dest = XEXP (new_i3_dest, 0);
2175 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2176 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2177 || GET_CODE (new_i2_dest) == SUBREG)
2178 new_i2_dest = XEXP (new_i2_dest, 0);
2180 if (GET_CODE (new_i3_dest) == REG
2181 && GET_CODE (new_i2_dest) == REG
2182 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2183 REG_N_SETS (REGNO (new_i2_dest))++;
2187 /* If we can split it and use I2DEST, go ahead and see if that
2188 helps things be recognized. Verify that none of the registers
2189 are set between I2 and I3. */
2190 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2191 #ifdef HAVE_cc0
2192 && GET_CODE (i2dest) == REG
2193 #endif
2194 /* We need I2DEST in the proper mode. If it is a hard register
2195 or the only use of a pseudo, we can change its mode. */
2196 && (GET_MODE (*split) == GET_MODE (i2dest)
2197 || GET_MODE (*split) == VOIDmode
2198 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2199 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2200 && ! REG_USERVAR_P (i2dest)))
2201 && (next_real_insn (i2) == i3
2202 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2203 /* We can't overwrite I2DEST if its value is still used by
2204 NEWPAT. */
2205 && ! reg_referenced_p (i2dest, newpat))
2207 rtx newdest = i2dest;
2208 enum rtx_code split_code = GET_CODE (*split);
2209 enum machine_mode split_mode = GET_MODE (*split);
2211 /* Get NEWDEST as a register in the proper mode. We have already
2212 validated that we can do this. */
2213 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2215 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2217 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2218 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2221 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2222 an ASHIFT. This can occur if it was inside a PLUS and hence
2223 appeared to be a memory address. This is a kludge. */
2224 if (split_code == MULT
2225 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2226 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2228 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2229 XEXP (*split, 0), GEN_INT (i)));
2230 /* Update split_code because we may not have a multiply
2231 anymore. */
2232 split_code = GET_CODE (*split);
2235 #ifdef INSN_SCHEDULING
2236 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2237 be written as a ZERO_EXTEND. */
2238 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2239 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2240 XEXP (*split, 0)));
2241 #endif
2243 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2244 SUBST (*split, newdest);
2245 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2247 /* If the split point was a MULT and we didn't have one before,
2248 don't use one now. */
2249 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2250 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2254 /* Check for a case where we loaded from memory in a narrow mode and
2255 then sign extended it, but we need both registers. In that case,
2256 we have a PARALLEL with both loads from the same memory location.
2257 We can split this into a load from memory followed by a register-register
2258 copy. This saves at least one insn, more if register allocation can
2259 eliminate the copy.
2261 We cannot do this if the destination of the second assignment is
2262 a register that we have already assumed is zero-extended. Similarly
2263 for a SUBREG of such a register. */
2265 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2266 && GET_CODE (newpat) == PARALLEL
2267 && XVECLEN (newpat, 0) == 2
2268 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2269 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2270 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2271 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2272 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2273 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2274 INSN_CUID (i2))
2275 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2276 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2277 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2278 (GET_CODE (temp) == REG
2279 && reg_nonzero_bits[REGNO (temp)] != 0
2280 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2281 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2282 && (reg_nonzero_bits[REGNO (temp)]
2283 != GET_MODE_MASK (word_mode))))
2284 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2285 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2286 (GET_CODE (temp) == REG
2287 && reg_nonzero_bits[REGNO (temp)] != 0
2288 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2289 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2290 && (reg_nonzero_bits[REGNO (temp)]
2291 != GET_MODE_MASK (word_mode)))))
2292 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2293 SET_SRC (XVECEXP (newpat, 0, 1)))
2294 && ! find_reg_note (i3, REG_UNUSED,
2295 SET_DEST (XVECEXP (newpat, 0, 0))))
2297 rtx ni2dest;
2299 newi2pat = XVECEXP (newpat, 0, 0);
2300 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2301 newpat = XVECEXP (newpat, 0, 1);
2302 SUBST (SET_SRC (newpat),
2303 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2304 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2306 if (i2_code_number >= 0)
2307 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2309 if (insn_code_number >= 0)
2311 rtx insn;
2312 rtx link;
2314 /* If we will be able to accept this, we have made a change to the
2315 destination of I3. This can invalidate a LOG_LINKS pointing
2316 to I3. No other part of combine.c makes such a transformation.
2318 The new I3 will have a destination that was previously the
2319 destination of I1 or I2 and which was used in i2 or I3. Call
2320 distribute_links to make a LOG_LINK from the next use of
2321 that destination. */
2323 PATTERN (i3) = newpat;
2324 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2326 /* I3 now uses what used to be its destination and which is
2327 now I2's destination. That means we need a LOG_LINK from
2328 I3 to I2. But we used to have one, so we still will.
2330 However, some later insn might be using I2's dest and have
2331 a LOG_LINK pointing at I3. We must remove this link.
2332 The simplest way to remove the link is to point it at I1,
2333 which we know will be a NOTE. */
2335 for (insn = NEXT_INSN (i3);
2336 insn && (this_basic_block == n_basic_blocks - 1
2337 || insn != BLOCK_HEAD (this_basic_block + 1));
2338 insn = NEXT_INSN (insn))
2340 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2342 for (link = LOG_LINKS (insn); link;
2343 link = XEXP (link, 1))
2344 if (XEXP (link, 0) == i3)
2345 XEXP (link, 0) = i1;
2347 break;
2353 /* Similarly, check for a case where we have a PARALLEL of two independent
2354 SETs but we started with three insns. In this case, we can do the sets
2355 as two separate insns. This case occurs when some SET allows two
2356 other insns to combine, but the destination of that SET is still live. */
2358 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2359 && GET_CODE (newpat) == PARALLEL
2360 && XVECLEN (newpat, 0) == 2
2361 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2362 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2363 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2364 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2365 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2366 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2367 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2368 INSN_CUID (i2))
2369 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2370 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2371 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2372 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2373 XVECEXP (newpat, 0, 0))
2374 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2375 XVECEXP (newpat, 0, 1))
2376 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2377 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2379 /* Normally, it doesn't matter which of the two is done first,
2380 but it does if one references cc0. In that case, it has to
2381 be first. */
2382 #ifdef HAVE_cc0
2383 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2385 newi2pat = XVECEXP (newpat, 0, 0);
2386 newpat = XVECEXP (newpat, 0, 1);
2388 else
2389 #endif
2391 newi2pat = XVECEXP (newpat, 0, 1);
2392 newpat = XVECEXP (newpat, 0, 0);
2395 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2397 if (i2_code_number >= 0)
2398 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2401 /* If it still isn't recognized, fail and change things back the way they
2402 were. */
2403 if ((insn_code_number < 0
2404 /* Is the result a reasonable ASM_OPERANDS? */
2405 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2407 undo_all ();
2408 return 0;
2411 /* If we had to change another insn, make sure it is valid also. */
2412 if (undobuf.other_insn)
2414 rtx other_pat = PATTERN (undobuf.other_insn);
2415 rtx new_other_notes;
2416 rtx note, next;
2418 CLEAR_HARD_REG_SET (newpat_used_regs);
2420 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2421 &new_other_notes);
2423 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2425 undo_all ();
2426 return 0;
2429 PATTERN (undobuf.other_insn) = other_pat;
2431 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2432 are still valid. Then add any non-duplicate notes added by
2433 recog_for_combine. */
2434 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2436 next = XEXP (note, 1);
2438 if (REG_NOTE_KIND (note) == REG_UNUSED
2439 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2441 if (GET_CODE (XEXP (note, 0)) == REG)
2442 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2444 remove_note (undobuf.other_insn, note);
2448 for (note = new_other_notes; note; note = XEXP (note, 1))
2449 if (GET_CODE (XEXP (note, 0)) == REG)
2450 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2452 distribute_notes (new_other_notes, undobuf.other_insn,
2453 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2455 #ifdef HAVE_cc0
2456 /* If I2 is the setter CC0 and I3 is the user CC0 then check whether
2457 they are adjacent to each other or not. */
2459 rtx p = prev_nonnote_insn (i3);
2460 if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2461 && sets_cc0_p (newi2pat))
2463 undo_all ();
2464 return 0;
2467 #endif
2469 /* We now know that we can do this combination. Merge the insns and
2470 update the status of registers and LOG_LINKS. */
2473 rtx i3notes, i2notes, i1notes = 0;
2474 rtx i3links, i2links, i1links = 0;
2475 rtx midnotes = 0;
2476 unsigned int regno;
2477 /* Compute which registers we expect to eliminate. newi2pat may be setting
2478 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2479 same as i3dest, in which case newi2pat may be setting i1dest. */
2480 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2481 || i2dest_in_i2src || i2dest_in_i1src
2482 ? 0 : i2dest);
2483 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2484 || (newi2pat && reg_set_p (i1dest, newi2pat))
2485 ? 0 : i1dest);
2487 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2488 clear them. */
2489 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2490 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2491 if (i1)
2492 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2494 /* Ensure that we do not have something that should not be shared but
2495 occurs multiple times in the new insns. Check this by first
2496 resetting all the `used' flags and then copying anything is shared. */
2498 reset_used_flags (i3notes);
2499 reset_used_flags (i2notes);
2500 reset_used_flags (i1notes);
2501 reset_used_flags (newpat);
2502 reset_used_flags (newi2pat);
2503 if (undobuf.other_insn)
2504 reset_used_flags (PATTERN (undobuf.other_insn));
2506 i3notes = copy_rtx_if_shared (i3notes);
2507 i2notes = copy_rtx_if_shared (i2notes);
2508 i1notes = copy_rtx_if_shared (i1notes);
2509 newpat = copy_rtx_if_shared (newpat);
2510 newi2pat = copy_rtx_if_shared (newi2pat);
2511 if (undobuf.other_insn)
2512 reset_used_flags (PATTERN (undobuf.other_insn));
2514 INSN_CODE (i3) = insn_code_number;
2515 PATTERN (i3) = newpat;
2516 if (undobuf.other_insn)
2517 INSN_CODE (undobuf.other_insn) = other_code_number;
2519 /* We had one special case above where I2 had more than one set and
2520 we replaced a destination of one of those sets with the destination
2521 of I3. In that case, we have to update LOG_LINKS of insns later
2522 in this basic block. Note that this (expensive) case is rare.
2524 Also, in this case, we must pretend that all REG_NOTEs for I2
2525 actually came from I3, so that REG_UNUSED notes from I2 will be
2526 properly handled. */
2528 if (i3_subst_into_i2)
2530 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2531 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2532 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2533 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2534 && ! find_reg_note (i2, REG_UNUSED,
2535 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2536 for (temp = NEXT_INSN (i2);
2537 temp && (this_basic_block == n_basic_blocks - 1
2538 || BLOCK_HEAD (this_basic_block) != temp);
2539 temp = NEXT_INSN (temp))
2540 if (temp != i3 && INSN_P (temp))
2541 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2542 if (XEXP (link, 0) == i2)
2543 XEXP (link, 0) = i3;
2545 if (i3notes)
2547 rtx link = i3notes;
2548 while (XEXP (link, 1))
2549 link = XEXP (link, 1);
2550 XEXP (link, 1) = i2notes;
2552 else
2553 i3notes = i2notes;
2554 i2notes = 0;
2557 LOG_LINKS (i3) = 0;
2558 REG_NOTES (i3) = 0;
2559 LOG_LINKS (i2) = 0;
2560 REG_NOTES (i2) = 0;
2562 if (newi2pat)
2564 INSN_CODE (i2) = i2_code_number;
2565 PATTERN (i2) = newi2pat;
2567 else
2569 PUT_CODE (i2, NOTE);
2570 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2571 NOTE_SOURCE_FILE (i2) = 0;
2574 if (i1)
2576 LOG_LINKS (i1) = 0;
2577 REG_NOTES (i1) = 0;
2578 PUT_CODE (i1, NOTE);
2579 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2580 NOTE_SOURCE_FILE (i1) = 0;
2583 /* Get death notes for everything that is now used in either I3 or
2584 I2 and used to die in a previous insn. If we built two new
2585 patterns, move from I1 to I2 then I2 to I3 so that we get the
2586 proper movement on registers that I2 modifies. */
2588 if (newi2pat)
2590 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2591 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2593 else
2594 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2595 i3, &midnotes);
2597 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2598 if (i3notes)
2599 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2600 elim_i2, elim_i1);
2601 if (i2notes)
2602 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2603 elim_i2, elim_i1);
2604 if (i1notes)
2605 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2606 elim_i2, elim_i1);
2607 if (midnotes)
2608 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2609 elim_i2, elim_i1);
2611 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2612 know these are REG_UNUSED and want them to go to the desired insn,
2613 so we always pass it as i3. We have not counted the notes in
2614 reg_n_deaths yet, so we need to do so now. */
2616 if (newi2pat && new_i2_notes)
2618 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2619 if (GET_CODE (XEXP (temp, 0)) == REG)
2620 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2622 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2625 if (new_i3_notes)
2627 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2628 if (GET_CODE (XEXP (temp, 0)) == REG)
2629 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2631 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2634 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2635 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2636 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2637 in that case, it might delete I2. Similarly for I2 and I1.
2638 Show an additional death due to the REG_DEAD note we make here. If
2639 we discard it in distribute_notes, we will decrement it again. */
2641 if (i3dest_killed)
2643 if (GET_CODE (i3dest_killed) == REG)
2644 REG_N_DEATHS (REGNO (i3dest_killed))++;
2646 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2647 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2648 NULL_RTX),
2649 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2650 else
2651 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2652 NULL_RTX),
2653 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2654 elim_i2, elim_i1);
2657 if (i2dest_in_i2src)
2659 if (GET_CODE (i2dest) == REG)
2660 REG_N_DEATHS (REGNO (i2dest))++;
2662 if (newi2pat && reg_set_p (i2dest, newi2pat))
2663 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2664 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2665 else
2666 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2667 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2668 NULL_RTX, NULL_RTX);
2671 if (i1dest_in_i1src)
2673 if (GET_CODE (i1dest) == REG)
2674 REG_N_DEATHS (REGNO (i1dest))++;
2676 if (newi2pat && reg_set_p (i1dest, newi2pat))
2677 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2678 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2679 else
2680 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2681 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2682 NULL_RTX, NULL_RTX);
2685 distribute_links (i3links);
2686 distribute_links (i2links);
2687 distribute_links (i1links);
2689 if (GET_CODE (i2dest) == REG)
2691 rtx link;
2692 rtx i2_insn = 0, i2_val = 0, set;
2694 /* The insn that used to set this register doesn't exist, and
2695 this life of the register may not exist either. See if one of
2696 I3's links points to an insn that sets I2DEST. If it does,
2697 that is now the last known value for I2DEST. If we don't update
2698 this and I2 set the register to a value that depended on its old
2699 contents, we will get confused. If this insn is used, thing
2700 will be set correctly in combine_instructions. */
2702 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2703 if ((set = single_set (XEXP (link, 0))) != 0
2704 && rtx_equal_p (i2dest, SET_DEST (set)))
2705 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2707 record_value_for_reg (i2dest, i2_insn, i2_val);
2709 /* If the reg formerly set in I2 died only once and that was in I3,
2710 zero its use count so it won't make `reload' do any work. */
2711 if (! added_sets_2
2712 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2713 && ! i2dest_in_i2src)
2715 regno = REGNO (i2dest);
2716 REG_N_SETS (regno)--;
2720 if (i1 && GET_CODE (i1dest) == REG)
2722 rtx link;
2723 rtx i1_insn = 0, i1_val = 0, set;
2725 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2726 if ((set = single_set (XEXP (link, 0))) != 0
2727 && rtx_equal_p (i1dest, SET_DEST (set)))
2728 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2730 record_value_for_reg (i1dest, i1_insn, i1_val);
2732 regno = REGNO (i1dest);
2733 if (! added_sets_1 && ! i1dest_in_i1src)
2734 REG_N_SETS (regno)--;
2737 /* Update reg_nonzero_bits et al for any changes that may have been made
2738 to this insn. The order of set_nonzero_bits_and_sign_copies() is
2739 important. Because newi2pat can affect nonzero_bits of newpat */
2740 if (newi2pat)
2741 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2742 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2744 /* Set new_direct_jump_p if a new return or simple jump instruction
2745 has been created.
2747 If I3 is now an unconditional jump, ensure that it has a
2748 BARRIER following it since it may have initially been a
2749 conditional jump. It may also be the last nonnote insn. */
2751 if (GET_CODE (newpat) == RETURN || any_uncondjump_p (i3))
2753 *new_direct_jump_p = 1;
2755 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2756 || GET_CODE (temp) != BARRIER)
2757 emit_barrier_after (i3);
2761 combine_successes++;
2762 undo_commit ();
2764 /* Clear this here, so that subsequent get_last_value calls are not
2765 affected. */
2766 subst_prev_insn = NULL_RTX;
2768 if (added_links_insn
2769 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2770 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2771 return added_links_insn;
2772 else
2773 return newi2pat ? i2 : i3;
2776 /* Undo all the modifications recorded in undobuf. */
2778 static void
2779 undo_all ()
2781 struct undo *undo, *next;
2783 for (undo = undobuf.undos; undo; undo = next)
2785 next = undo->next;
2786 if (undo->is_int)
2787 *undo->where.i = undo->old_contents.i;
2788 else
2789 *undo->where.r = undo->old_contents.r;
2791 undo->next = undobuf.frees;
2792 undobuf.frees = undo;
2795 undobuf.undos = undobuf.previous_undos = 0;
2797 /* Clear this here, so that subsequent get_last_value calls are not
2798 affected. */
2799 subst_prev_insn = NULL_RTX;
2802 /* We've committed to accepting the changes we made. Move all
2803 of the undos to the free list. */
2805 static void
2806 undo_commit ()
2808 struct undo *undo, *next;
2810 for (undo = undobuf.undos; undo; undo = next)
2812 next = undo->next;
2813 undo->next = undobuf.frees;
2814 undobuf.frees = undo;
2816 undobuf.undos = undobuf.previous_undos = 0;
2820 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2821 where we have an arithmetic expression and return that point. LOC will
2822 be inside INSN.
2824 try_combine will call this function to see if an insn can be split into
2825 two insns. */
2827 static rtx *
2828 find_split_point (loc, insn)
2829 rtx *loc;
2830 rtx insn;
2832 rtx x = *loc;
2833 enum rtx_code code = GET_CODE (x);
2834 rtx *split;
2835 unsigned HOST_WIDE_INT len = 0;
2836 HOST_WIDE_INT pos = 0;
2837 int unsignedp = 0;
2838 rtx inner = NULL_RTX;
2840 /* First special-case some codes. */
2841 switch (code)
2843 case SUBREG:
2844 #ifdef INSN_SCHEDULING
2845 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2846 point. */
2847 if (GET_CODE (SUBREG_REG (x)) == MEM)
2848 return loc;
2849 #endif
2850 return find_split_point (&SUBREG_REG (x), insn);
2852 case MEM:
2853 #ifdef HAVE_lo_sum
2854 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2855 using LO_SUM and HIGH. */
2856 if (GET_CODE (XEXP (x, 0)) == CONST
2857 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2859 SUBST (XEXP (x, 0),
2860 gen_rtx_combine (LO_SUM, Pmode,
2861 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2862 XEXP (x, 0)));
2863 return &XEXP (XEXP (x, 0), 0);
2865 #endif
2867 /* If we have a PLUS whose second operand is a constant and the
2868 address is not valid, perhaps will can split it up using
2869 the machine-specific way to split large constants. We use
2870 the first pseudo-reg (one of the virtual regs) as a placeholder;
2871 it will not remain in the result. */
2872 if (GET_CODE (XEXP (x, 0)) == PLUS
2873 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2874 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2876 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2877 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2878 subst_insn);
2880 /* This should have produced two insns, each of which sets our
2881 placeholder. If the source of the second is a valid address,
2882 we can make put both sources together and make a split point
2883 in the middle. */
2885 if (seq && XVECLEN (seq, 0) == 2
2886 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2887 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2888 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2889 && ! reg_mentioned_p (reg,
2890 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2891 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2892 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2893 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2894 && memory_address_p (GET_MODE (x),
2895 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2897 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2898 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2900 /* Replace the placeholder in SRC2 with SRC1. If we can
2901 find where in SRC2 it was placed, that can become our
2902 split point and we can replace this address with SRC2.
2903 Just try two obvious places. */
2905 src2 = replace_rtx (src2, reg, src1);
2906 split = 0;
2907 if (XEXP (src2, 0) == src1)
2908 split = &XEXP (src2, 0);
2909 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2910 && XEXP (XEXP (src2, 0), 0) == src1)
2911 split = &XEXP (XEXP (src2, 0), 0);
2913 if (split)
2915 SUBST (XEXP (x, 0), src2);
2916 return split;
2920 /* If that didn't work, perhaps the first operand is complex and
2921 needs to be computed separately, so make a split point there.
2922 This will occur on machines that just support REG + CONST
2923 and have a constant moved through some previous computation. */
2925 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2926 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2927 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2928 == 'o')))
2929 return &XEXP (XEXP (x, 0), 0);
2931 break;
2933 case SET:
2934 #ifdef HAVE_cc0
2935 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2936 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2937 we need to put the operand into a register. So split at that
2938 point. */
2940 if (SET_DEST (x) == cc0_rtx
2941 && GET_CODE (SET_SRC (x)) != COMPARE
2942 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2943 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2944 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2945 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2946 return &SET_SRC (x);
2947 #endif
2949 /* See if we can split SET_SRC as it stands. */
2950 split = find_split_point (&SET_SRC (x), insn);
2951 if (split && split != &SET_SRC (x))
2952 return split;
2954 /* See if we can split SET_DEST as it stands. */
2955 split = find_split_point (&SET_DEST (x), insn);
2956 if (split && split != &SET_DEST (x))
2957 return split;
2959 /* See if this is a bitfield assignment with everything constant. If
2960 so, this is an IOR of an AND, so split it into that. */
2961 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2962 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2963 <= HOST_BITS_PER_WIDE_INT)
2964 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2965 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2966 && GET_CODE (SET_SRC (x)) == CONST_INT
2967 && ((INTVAL (XEXP (SET_DEST (x), 1))
2968 + INTVAL (XEXP (SET_DEST (x), 2)))
2969 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2970 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2972 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
2973 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
2974 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
2975 rtx dest = XEXP (SET_DEST (x), 0);
2976 enum machine_mode mode = GET_MODE (dest);
2977 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2979 if (BITS_BIG_ENDIAN)
2980 pos = GET_MODE_BITSIZE (mode) - len - pos;
2982 if (src == mask)
2983 SUBST (SET_SRC (x),
2984 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2985 else
2986 SUBST (SET_SRC (x),
2987 gen_binary (IOR, mode,
2988 gen_binary (AND, mode, dest,
2989 GEN_INT (~(mask << pos)
2990 & GET_MODE_MASK (mode))),
2991 GEN_INT (src << pos)));
2993 SUBST (SET_DEST (x), dest);
2995 split = find_split_point (&SET_SRC (x), insn);
2996 if (split && split != &SET_SRC (x))
2997 return split;
3000 /* Otherwise, see if this is an operation that we can split into two.
3001 If so, try to split that. */
3002 code = GET_CODE (SET_SRC (x));
3004 switch (code)
3006 case AND:
3007 /* If we are AND'ing with a large constant that is only a single
3008 bit and the result is only being used in a context where we
3009 need to know if it is zero or non-zero, replace it with a bit
3010 extraction. This will avoid the large constant, which might
3011 have taken more than one insn to make. If the constant were
3012 not a valid argument to the AND but took only one insn to make,
3013 this is no worse, but if it took more than one insn, it will
3014 be better. */
3016 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3017 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3018 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3019 && GET_CODE (SET_DEST (x)) == REG
3020 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
3021 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3022 && XEXP (*split, 0) == SET_DEST (x)
3023 && XEXP (*split, 1) == const0_rtx)
3025 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3026 XEXP (SET_SRC (x), 0),
3027 pos, NULL_RTX, 1, 1, 0, 0);
3028 if (extraction != 0)
3030 SUBST (SET_SRC (x), extraction);
3031 return find_split_point (loc, insn);
3034 break;
3036 case NE:
3037 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3038 is known to be on, this can be converted into a NEG of a shift. */
3039 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3040 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3041 && 1 <= (pos = exact_log2
3042 (nonzero_bits (XEXP (SET_SRC (x), 0),
3043 GET_MODE (XEXP (SET_SRC (x), 0))))))
3045 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3047 SUBST (SET_SRC (x),
3048 gen_rtx_combine (NEG, mode,
3049 gen_rtx_combine (LSHIFTRT, mode,
3050 XEXP (SET_SRC (x), 0),
3051 GEN_INT (pos))));
3053 split = find_split_point (&SET_SRC (x), insn);
3054 if (split && split != &SET_SRC (x))
3055 return split;
3057 break;
3059 case SIGN_EXTEND:
3060 inner = XEXP (SET_SRC (x), 0);
3062 /* We can't optimize if either mode is a partial integer
3063 mode as we don't know how many bits are significant
3064 in those modes. */
3065 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3066 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3067 break;
3069 pos = 0;
3070 len = GET_MODE_BITSIZE (GET_MODE (inner));
3071 unsignedp = 0;
3072 break;
3074 case SIGN_EXTRACT:
3075 case ZERO_EXTRACT:
3076 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3077 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3079 inner = XEXP (SET_SRC (x), 0);
3080 len = INTVAL (XEXP (SET_SRC (x), 1));
3081 pos = INTVAL (XEXP (SET_SRC (x), 2));
3083 if (BITS_BIG_ENDIAN)
3084 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3085 unsignedp = (code == ZERO_EXTRACT);
3087 break;
3089 default:
3090 break;
3093 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3095 enum machine_mode mode = GET_MODE (SET_SRC (x));
3097 /* For unsigned, we have a choice of a shift followed by an
3098 AND or two shifts. Use two shifts for field sizes where the
3099 constant might be too large. We assume here that we can
3100 always at least get 8-bit constants in an AND insn, which is
3101 true for every current RISC. */
3103 if (unsignedp && len <= 8)
3105 SUBST (SET_SRC (x),
3106 gen_rtx_combine
3107 (AND, mode,
3108 gen_rtx_combine (LSHIFTRT, mode,
3109 gen_lowpart_for_combine (mode, inner),
3110 GEN_INT (pos)),
3111 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3113 split = find_split_point (&SET_SRC (x), insn);
3114 if (split && split != &SET_SRC (x))
3115 return split;
3117 else
3119 SUBST (SET_SRC (x),
3120 gen_rtx_combine
3121 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3122 gen_rtx_combine (ASHIFT, mode,
3123 gen_lowpart_for_combine (mode, inner),
3124 GEN_INT (GET_MODE_BITSIZE (mode)
3125 - len - pos)),
3126 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3128 split = find_split_point (&SET_SRC (x), insn);
3129 if (split && split != &SET_SRC (x))
3130 return split;
3134 /* See if this is a simple operation with a constant as the second
3135 operand. It might be that this constant is out of range and hence
3136 could be used as a split point. */
3137 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3138 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3139 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3140 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3141 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3142 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3143 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3144 == 'o'))))
3145 return &XEXP (SET_SRC (x), 1);
3147 /* Finally, see if this is a simple operation with its first operand
3148 not in a register. The operation might require this operand in a
3149 register, so return it as a split point. We can always do this
3150 because if the first operand were another operation, we would have
3151 already found it as a split point. */
3152 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3153 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3154 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3155 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3156 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3157 return &XEXP (SET_SRC (x), 0);
3159 return 0;
3161 case AND:
3162 case IOR:
3163 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3164 it is better to write this as (not (ior A B)) so we can split it.
3165 Similarly for IOR. */
3166 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3168 SUBST (*loc,
3169 gen_rtx_combine (NOT, GET_MODE (x),
3170 gen_rtx_combine (code == IOR ? AND : IOR,
3171 GET_MODE (x),
3172 XEXP (XEXP (x, 0), 0),
3173 XEXP (XEXP (x, 1), 0))));
3174 return find_split_point (loc, insn);
3177 /* Many RISC machines have a large set of logical insns. If the
3178 second operand is a NOT, put it first so we will try to split the
3179 other operand first. */
3180 if (GET_CODE (XEXP (x, 1)) == NOT)
3182 rtx tem = XEXP (x, 0);
3183 SUBST (XEXP (x, 0), XEXP (x, 1));
3184 SUBST (XEXP (x, 1), tem);
3186 break;
3188 default:
3189 break;
3192 /* Otherwise, select our actions depending on our rtx class. */
3193 switch (GET_RTX_CLASS (code))
3195 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3196 case '3':
3197 split = find_split_point (&XEXP (x, 2), insn);
3198 if (split)
3199 return split;
3200 /* ... fall through ... */
3201 case '2':
3202 case 'c':
3203 case '<':
3204 split = find_split_point (&XEXP (x, 1), insn);
3205 if (split)
3206 return split;
3207 /* ... fall through ... */
3208 case '1':
3209 /* Some machines have (and (shift ...) ...) insns. If X is not
3210 an AND, but XEXP (X, 0) is, use it as our split point. */
3211 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3212 return &XEXP (x, 0);
3214 split = find_split_point (&XEXP (x, 0), insn);
3215 if (split)
3216 return split;
3217 return loc;
3220 /* Otherwise, we don't have a split point. */
3221 return 0;
3224 /* Throughout X, replace FROM with TO, and return the result.
3225 The result is TO if X is FROM;
3226 otherwise the result is X, but its contents may have been modified.
3227 If they were modified, a record was made in undobuf so that
3228 undo_all will (among other things) return X to its original state.
3230 If the number of changes necessary is too much to record to undo,
3231 the excess changes are not made, so the result is invalid.
3232 The changes already made can still be undone.
3233 undobuf.num_undo is incremented for such changes, so by testing that
3234 the caller can tell whether the result is valid.
3236 `n_occurrences' is incremented each time FROM is replaced.
3238 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
3240 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
3241 by copying if `n_occurrences' is non-zero. */
3243 static rtx
3244 subst (x, from, to, in_dest, unique_copy)
3245 register rtx x, from, to;
3246 int in_dest;
3247 int unique_copy;
3249 register enum rtx_code code = GET_CODE (x);
3250 enum machine_mode op0_mode = VOIDmode;
3251 register const char *fmt;
3252 register int len, i;
3253 rtx new;
3255 /* Two expressions are equal if they are identical copies of a shared
3256 RTX or if they are both registers with the same register number
3257 and mode. */
3259 #define COMBINE_RTX_EQUAL_P(X,Y) \
3260 ((X) == (Y) \
3261 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3262 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3264 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3266 n_occurrences++;
3267 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3270 /* If X and FROM are the same register but different modes, they will
3271 not have been seen as equal above. However, flow.c will make a
3272 LOG_LINKS entry for that case. If we do nothing, we will try to
3273 rerecognize our original insn and, when it succeeds, we will
3274 delete the feeding insn, which is incorrect.
3276 So force this insn not to match in this (rare) case. */
3277 if (! in_dest && code == REG && GET_CODE (from) == REG
3278 && REGNO (x) == REGNO (from))
3279 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3281 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3282 of which may contain things that can be combined. */
3283 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3284 return x;
3286 /* It is possible to have a subexpression appear twice in the insn.
3287 Suppose that FROM is a register that appears within TO.
3288 Then, after that subexpression has been scanned once by `subst',
3289 the second time it is scanned, TO may be found. If we were
3290 to scan TO here, we would find FROM within it and create a
3291 self-referent rtl structure which is completely wrong. */
3292 if (COMBINE_RTX_EQUAL_P (x, to))
3293 return to;
3295 /* Parallel asm_operands need special attention because all of the
3296 inputs are shared across the arms. Furthermore, unsharing the
3297 rtl results in recognition failures. Failure to handle this case
3298 specially can result in circular rtl.
3300 Solve this by doing a normal pass across the first entry of the
3301 parallel, and only processing the SET_DESTs of the subsequent
3302 entries. Ug. */
3304 if (code == PARALLEL
3305 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3306 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3308 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3310 /* If this substitution failed, this whole thing fails. */
3311 if (GET_CODE (new) == CLOBBER
3312 && XEXP (new, 0) == const0_rtx)
3313 return new;
3315 SUBST (XVECEXP (x, 0, 0), new);
3317 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3319 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3321 if (GET_CODE (dest) != REG
3322 && GET_CODE (dest) != CC0
3323 && GET_CODE (dest) != PC)
3325 new = subst (dest, from, to, 0, unique_copy);
3327 /* If this substitution failed, this whole thing fails. */
3328 if (GET_CODE (new) == CLOBBER
3329 && XEXP (new, 0) == const0_rtx)
3330 return new;
3332 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3336 else
3338 len = GET_RTX_LENGTH (code);
3339 fmt = GET_RTX_FORMAT (code);
3341 /* We don't need to process a SET_DEST that is a register, CC0,
3342 or PC, so set up to skip this common case. All other cases
3343 where we want to suppress replacing something inside a
3344 SET_SRC are handled via the IN_DEST operand. */
3345 if (code == SET
3346 && (GET_CODE (SET_DEST (x)) == REG
3347 || GET_CODE (SET_DEST (x)) == CC0
3348 || GET_CODE (SET_DEST (x)) == PC))
3349 fmt = "ie";
3351 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3352 constant. */
3353 if (fmt[0] == 'e')
3354 op0_mode = GET_MODE (XEXP (x, 0));
3356 for (i = 0; i < len; i++)
3358 if (fmt[i] == 'E')
3360 register int j;
3361 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3363 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3365 new = (unique_copy && n_occurrences
3366 ? copy_rtx (to) : to);
3367 n_occurrences++;
3369 else
3371 new = subst (XVECEXP (x, i, j), from, to, 0,
3372 unique_copy);
3374 /* If this substitution failed, this whole thing
3375 fails. */
3376 if (GET_CODE (new) == CLOBBER
3377 && XEXP (new, 0) == const0_rtx)
3378 return new;
3381 SUBST (XVECEXP (x, i, j), new);
3384 else if (fmt[i] == 'e')
3386 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3388 /* In general, don't install a subreg involving two
3389 modes not tieable. It can worsen register
3390 allocation, and can even make invalid reload
3391 insns, since the reg inside may need to be copied
3392 from in the outside mode, and that may be invalid
3393 if it is an fp reg copied in integer mode.
3395 We allow two exceptions to this: It is valid if
3396 it is inside another SUBREG and the mode of that
3397 SUBREG and the mode of the inside of TO is
3398 tieable and it is valid if X is a SET that copies
3399 FROM to CC0. */
3401 if (GET_CODE (to) == SUBREG
3402 && ! MODES_TIEABLE_P (GET_MODE (to),
3403 GET_MODE (SUBREG_REG (to)))
3404 && ! (code == SUBREG
3405 && MODES_TIEABLE_P (GET_MODE (x),
3406 GET_MODE (SUBREG_REG (to))))
3407 #ifdef HAVE_cc0
3408 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3409 #endif
3411 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3413 #ifdef CLASS_CANNOT_CHANGE_MODE
3414 if (code == SUBREG
3415 && GET_CODE (to) == REG
3416 && REGNO (to) < FIRST_PSEUDO_REGISTER
3417 && (TEST_HARD_REG_BIT
3418 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE],
3419 REGNO (to)))
3420 && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (to),
3421 GET_MODE (x)))
3422 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3423 #endif
3425 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3426 n_occurrences++;
3428 else
3429 /* If we are in a SET_DEST, suppress most cases unless we
3430 have gone inside a MEM, in which case we want to
3431 simplify the address. We assume here that things that
3432 are actually part of the destination have their inner
3433 parts in the first expression. This is true for SUBREG,
3434 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3435 things aside from REG and MEM that should appear in a
3436 SET_DEST. */
3437 new = subst (XEXP (x, i), from, to,
3438 (((in_dest
3439 && (code == SUBREG || code == STRICT_LOW_PART
3440 || code == ZERO_EXTRACT))
3441 || code == SET)
3442 && i == 0), unique_copy);
3444 /* If we found that we will have to reject this combination,
3445 indicate that by returning the CLOBBER ourselves, rather than
3446 an expression containing it. This will speed things up as
3447 well as prevent accidents where two CLOBBERs are considered
3448 to be equal, thus producing an incorrect simplification. */
3450 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3451 return new;
3453 SUBST (XEXP (x, i), new);
3458 /* Try to simplify X. If the simplification changed the code, it is likely
3459 that further simplification will help, so loop, but limit the number
3460 of repetitions that will be performed. */
3462 for (i = 0; i < 4; i++)
3464 /* If X is sufficiently simple, don't bother trying to do anything
3465 with it. */
3466 if (code != CONST_INT && code != REG && code != CLOBBER)
3467 x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3469 if (GET_CODE (x) == code)
3470 break;
3472 code = GET_CODE (x);
3474 /* We no longer know the original mode of operand 0 since we
3475 have changed the form of X) */
3476 op0_mode = VOIDmode;
3479 return x;
3482 /* Simplify X, a piece of RTL. We just operate on the expression at the
3483 outer level; call `subst' to simplify recursively. Return the new
3484 expression.
3486 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3487 will be the iteration even if an expression with a code different from
3488 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3490 static rtx
3491 combine_simplify_rtx (x, op0_mode, last, in_dest)
3492 rtx x;
3493 enum machine_mode op0_mode;
3494 int last;
3495 int in_dest;
3497 enum rtx_code code = GET_CODE (x);
3498 enum machine_mode mode = GET_MODE (x);
3499 rtx temp;
3500 rtx reversed;
3501 int i;
3503 /* If this is a commutative operation, put a constant last and a complex
3504 expression first. We don't need to do this for comparisons here. */
3505 if (GET_RTX_CLASS (code) == 'c'
3506 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3507 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3508 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3509 || (GET_CODE (XEXP (x, 0)) == SUBREG
3510 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3511 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3513 temp = XEXP (x, 0);
3514 SUBST (XEXP (x, 0), XEXP (x, 1));
3515 SUBST (XEXP (x, 1), temp);
3518 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3519 sign extension of a PLUS with a constant, reverse the order of the sign
3520 extension and the addition. Note that this not the same as the original
3521 code, but overflow is undefined for signed values. Also note that the
3522 PLUS will have been partially moved "inside" the sign-extension, so that
3523 the first operand of X will really look like:
3524 (ashiftrt (plus (ashift A C4) C5) C4).
3525 We convert this to
3526 (plus (ashiftrt (ashift A C4) C2) C4)
3527 and replace the first operand of X with that expression. Later parts
3528 of this function may simplify the expression further.
3530 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3531 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3532 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3534 We do this to simplify address expressions. */
3536 if ((code == PLUS || code == MINUS || code == MULT)
3537 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3538 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3539 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3540 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3541 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3542 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3543 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3544 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3545 XEXP (XEXP (XEXP (x, 0), 0), 1),
3546 XEXP (XEXP (x, 0), 1))) != 0)
3548 rtx new
3549 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3550 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3551 INTVAL (XEXP (XEXP (x, 0), 1)));
3553 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3554 INTVAL (XEXP (XEXP (x, 0), 1)));
3556 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3559 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3560 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3561 things. Check for cases where both arms are testing the same
3562 condition.
3564 Don't do anything if all operands are very simple. */
3566 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3567 || GET_RTX_CLASS (code) == '<')
3568 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3569 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3570 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3571 == 'o')))
3572 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3573 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3574 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3575 == 'o')))))
3576 || (GET_RTX_CLASS (code) == '1'
3577 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3578 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3579 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3580 == 'o'))))))
3582 rtx cond, true, false;
3584 cond = if_then_else_cond (x, &true, &false);
3585 if (cond != 0
3586 /* If everything is a comparison, what we have is highly unlikely
3587 to be simpler, so don't use it. */
3588 && ! (GET_RTX_CLASS (code) == '<'
3589 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3590 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3592 rtx cop1 = const0_rtx;
3593 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3595 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3596 return x;
3598 /* Simplify the alternative arms; this may collapse the true and
3599 false arms to store-flag values. */
3600 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3601 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3603 /* If true and false are not general_operands, an if_then_else
3604 is unlikely to be simpler. */
3605 if (general_operand (true, VOIDmode)
3606 && general_operand (false, VOIDmode))
3608 /* Restarting if we generate a store-flag expression will cause
3609 us to loop. Just drop through in this case. */
3611 /* If the result values are STORE_FLAG_VALUE and zero, we can
3612 just make the comparison operation. */
3613 if (true == const_true_rtx && false == const0_rtx)
3614 x = gen_binary (cond_code, mode, cond, cop1);
3615 else if (true == const0_rtx && false == const_true_rtx)
3616 x = gen_binary (reverse_condition (cond_code),
3617 mode, cond, cop1);
3619 /* Likewise, we can make the negate of a comparison operation
3620 if the result values are - STORE_FLAG_VALUE and zero. */
3621 else if (GET_CODE (true) == CONST_INT
3622 && INTVAL (true) == - STORE_FLAG_VALUE
3623 && false == const0_rtx)
3624 x = gen_unary (NEG, mode, mode,
3625 gen_binary (cond_code, mode, cond, cop1));
3626 else if (GET_CODE (false) == CONST_INT
3627 && INTVAL (false) == - STORE_FLAG_VALUE
3628 && true == const0_rtx)
3629 x = gen_unary (NEG, mode, mode,
3630 gen_binary (reverse_condition (cond_code),
3631 mode, cond, cop1));
3632 else
3633 return gen_rtx_IF_THEN_ELSE (mode,
3634 gen_binary (cond_code, VOIDmode,
3635 cond, cop1),
3636 true, false);
3638 code = GET_CODE (x);
3639 op0_mode = VOIDmode;
3644 /* Try to fold this expression in case we have constants that weren't
3645 present before. */
3646 temp = 0;
3647 switch (GET_RTX_CLASS (code))
3649 case '1':
3650 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3651 break;
3652 case '<':
3654 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3655 if (cmp_mode == VOIDmode)
3657 cmp_mode = GET_MODE (XEXP (x, 1));
3658 if (cmp_mode == VOIDmode)
3659 cmp_mode = op0_mode;
3661 temp = simplify_relational_operation (code, cmp_mode,
3662 XEXP (x, 0), XEXP (x, 1));
3664 #ifdef FLOAT_STORE_FLAG_VALUE
3665 if (temp != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3667 if (temp == const0_rtx)
3668 temp = CONST0_RTX (mode);
3669 else
3670 temp = immed_real_const_1 (FLOAT_STORE_FLAG_VALUE (mode), mode);
3672 #endif
3673 break;
3674 case 'c':
3675 case '2':
3676 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3677 break;
3678 case 'b':
3679 case '3':
3680 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3681 XEXP (x, 1), XEXP (x, 2));
3682 break;
3685 if (temp)
3686 x = temp, code = GET_CODE (temp);
3688 /* First see if we can apply the inverse distributive law. */
3689 if (code == PLUS || code == MINUS
3690 || code == AND || code == IOR || code == XOR)
3692 x = apply_distributive_law (x);
3693 code = GET_CODE (x);
3696 /* If CODE is an associative operation not otherwise handled, see if we
3697 can associate some operands. This can win if they are constants or
3698 if they are logically related (i.e. (a & b) & a. */
3699 if ((code == PLUS || code == MINUS
3700 || code == MULT || code == AND || code == IOR || code == XOR
3701 || code == DIV || code == UDIV
3702 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3703 && INTEGRAL_MODE_P (mode))
3705 if (GET_CODE (XEXP (x, 0)) == code)
3707 rtx other = XEXP (XEXP (x, 0), 0);
3708 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3709 rtx inner_op1 = XEXP (x, 1);
3710 rtx inner;
3712 /* Make sure we pass the constant operand if any as the second
3713 one if this is a commutative operation. */
3714 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3716 rtx tem = inner_op0;
3717 inner_op0 = inner_op1;
3718 inner_op1 = tem;
3720 inner = simplify_binary_operation (code == MINUS ? PLUS
3721 : code == DIV ? MULT
3722 : code == UDIV ? MULT
3723 : code,
3724 mode, inner_op0, inner_op1);
3726 /* For commutative operations, try the other pair if that one
3727 didn't simplify. */
3728 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3730 other = XEXP (XEXP (x, 0), 1);
3731 inner = simplify_binary_operation (code, mode,
3732 XEXP (XEXP (x, 0), 0),
3733 XEXP (x, 1));
3736 if (inner)
3737 return gen_binary (code, mode, other, inner);
3741 /* A little bit of algebraic simplification here. */
3742 switch (code)
3744 case MEM:
3745 /* Ensure that our address has any ASHIFTs converted to MULT in case
3746 address-recognizing predicates are called later. */
3747 temp = make_compound_operation (XEXP (x, 0), MEM);
3748 SUBST (XEXP (x, 0), temp);
3749 break;
3751 case SUBREG:
3752 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3753 is paradoxical. If we can't do that safely, then it becomes
3754 something nonsensical so that this combination won't take place. */
3756 if (GET_CODE (SUBREG_REG (x)) == MEM
3757 && (GET_MODE_SIZE (mode)
3758 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3760 rtx inner = SUBREG_REG (x);
3761 int endian_offset = 0;
3762 /* Don't change the mode of the MEM
3763 if that would change the meaning of the address. */
3764 if (MEM_VOLATILE_P (SUBREG_REG (x))
3765 || mode_dependent_address_p (XEXP (inner, 0)))
3766 return gen_rtx_CLOBBER (mode, const0_rtx);
3768 if (BYTES_BIG_ENDIAN)
3770 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3771 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3772 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3773 endian_offset -= (UNITS_PER_WORD
3774 - GET_MODE_SIZE (GET_MODE (inner)));
3776 /* Note if the plus_constant doesn't make a valid address
3777 then this combination won't be accepted. */
3778 x = gen_rtx_MEM (mode,
3779 plus_constant (XEXP (inner, 0),
3780 (SUBREG_WORD (x) * UNITS_PER_WORD
3781 + endian_offset)));
3782 MEM_COPY_ATTRIBUTES (x, inner);
3783 return x;
3786 /* If we are in a SET_DEST, these other cases can't apply. */
3787 if (in_dest)
3788 return x;
3790 /* Changing mode twice with SUBREG => just change it once,
3791 or not at all if changing back to starting mode. */
3792 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3794 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3795 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3796 return SUBREG_REG (SUBREG_REG (x));
3798 SUBST_INT (SUBREG_WORD (x),
3799 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3800 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3803 /* SUBREG of a hard register => just change the register number
3804 and/or mode. If the hard register is not valid in that mode,
3805 suppress this combination. If the hard register is the stack,
3806 frame, or argument pointer, leave this as a SUBREG. */
3808 if (GET_CODE (SUBREG_REG (x)) == REG
3809 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3810 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3811 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3812 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3813 #endif
3814 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3815 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3816 #endif
3817 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3819 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3820 mode))
3821 return gen_rtx_REG (mode,
3822 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3823 else
3824 return gen_rtx_CLOBBER (mode, const0_rtx);
3827 /* For a constant, try to pick up the part we want. Handle a full
3828 word and low-order part. Only do this if we are narrowing
3829 the constant; if it is being widened, we have no idea what
3830 the extra bits will have been set to. */
3832 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3833 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3834 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3835 && GET_MODE_CLASS (mode) == MODE_INT)
3837 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3838 0, op0_mode);
3839 if (temp)
3840 return temp;
3843 /* If we want a subreg of a constant, at offset 0,
3844 take the low bits. On a little-endian machine, that's
3845 always valid. On a big-endian machine, it's valid
3846 only if the constant's mode fits in one word. Note that we
3847 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3848 if (CONSTANT_P (SUBREG_REG (x))
3849 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3850 || ! WORDS_BIG_ENDIAN)
3851 ? SUBREG_WORD (x) == 0
3852 : (SUBREG_WORD (x)
3853 == ((GET_MODE_SIZE (op0_mode)
3854 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3855 / UNITS_PER_WORD)))
3856 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3857 && (! WORDS_BIG_ENDIAN
3858 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3859 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3861 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3862 since we are saying that the high bits don't matter. */
3863 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3864 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3866 if (GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD
3867 && (WORDS_BIG_ENDIAN || SUBREG_WORD (x) != 0))
3868 return operand_subword (SUBREG_REG (x), SUBREG_WORD (x), 0, mode);
3869 return SUBREG_REG (x);
3872 /* Note that we cannot do any narrowing for non-constants since
3873 we might have been counting on using the fact that some bits were
3874 zero. We now do this in the SET. */
3876 break;
3878 case NOT:
3879 /* (not (plus X -1)) can become (neg X). */
3880 if (GET_CODE (XEXP (x, 0)) == PLUS
3881 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3882 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3884 /* Similarly, (not (neg X)) is (plus X -1). */
3885 if (GET_CODE (XEXP (x, 0)) == NEG)
3886 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3887 constm1_rtx);
3889 /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
3890 if (GET_CODE (XEXP (x, 0)) == XOR
3891 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3892 && (temp = simplify_unary_operation (NOT, mode,
3893 XEXP (XEXP (x, 0), 1),
3894 mode)) != 0)
3895 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3897 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3898 other than 1, but that is not valid. We could do a similar
3899 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3900 but this doesn't seem common enough to bother with. */
3901 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3902 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3903 return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3904 XEXP (XEXP (x, 0), 1));
3906 if (GET_CODE (XEXP (x, 0)) == SUBREG
3907 && subreg_lowpart_p (XEXP (x, 0))
3908 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3909 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3910 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3911 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3913 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3915 x = gen_rtx_ROTATE (inner_mode,
3916 gen_unary (NOT, inner_mode, inner_mode,
3917 const1_rtx),
3918 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3919 return gen_lowpart_for_combine (mode, x);
3922 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3923 reversing the comparison code if valid. */
3924 if (STORE_FLAG_VALUE == -1
3925 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3926 && (reversed = reversed_comparison (x, mode, XEXP (XEXP (x, 0), 0),
3927 XEXP (XEXP (x, 0), 1))))
3928 return reversed;
3930 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3931 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3932 perform the above simplification. */
3934 if (STORE_FLAG_VALUE == -1
3935 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3936 && XEXP (x, 1) == const1_rtx
3937 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3938 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3939 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3941 /* Apply De Morgan's laws to reduce number of patterns for machines
3942 with negating logical insns (and-not, nand, etc.). If result has
3943 only one NOT, put it first, since that is how the patterns are
3944 coded. */
3946 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3948 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3949 enum machine_mode op_mode;
3951 op_mode = GET_MODE (in1);
3952 in1 = gen_unary (NOT, op_mode, op_mode, in1);
3954 op_mode = GET_MODE (in2);
3955 if (op_mode == VOIDmode)
3956 op_mode = mode;
3957 in2 = gen_unary (NOT, op_mode, op_mode, in2);
3959 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3961 rtx tem = in2;
3962 in2 = in1; in1 = tem;
3965 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3966 mode, in1, in2);
3968 break;
3970 case NEG:
3971 /* (neg (plus X 1)) can become (not X). */
3972 if (GET_CODE (XEXP (x, 0)) == PLUS
3973 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3974 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3976 /* Similarly, (neg (not X)) is (plus X 1). */
3977 if (GET_CODE (XEXP (x, 0)) == NOT)
3978 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3980 /* (neg (minus X Y)) can become (minus Y X). */
3981 if (GET_CODE (XEXP (x, 0)) == MINUS
3982 && (! FLOAT_MODE_P (mode)
3983 /* x-y != -(y-x) with IEEE floating point. */
3984 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3985 || flag_fast_math))
3986 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3987 XEXP (XEXP (x, 0), 0));
3989 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3990 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3991 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3992 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3994 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3995 if we can then eliminate the NEG (e.g.,
3996 if the operand is a constant). */
3998 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
4000 temp = simplify_unary_operation (NEG, mode,
4001 XEXP (XEXP (x, 0), 0), mode);
4002 if (temp)
4004 SUBST (XEXP (XEXP (x, 0), 0), temp);
4005 return XEXP (x, 0);
4009 temp = expand_compound_operation (XEXP (x, 0));
4011 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4012 replaced by (lshiftrt X C). This will convert
4013 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4015 if (GET_CODE (temp) == ASHIFTRT
4016 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4017 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4018 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4019 INTVAL (XEXP (temp, 1)));
4021 /* If X has only a single bit that might be nonzero, say, bit I, convert
4022 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4023 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4024 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4025 or a SUBREG of one since we'd be making the expression more
4026 complex if it was just a register. */
4028 if (GET_CODE (temp) != REG
4029 && ! (GET_CODE (temp) == SUBREG
4030 && GET_CODE (SUBREG_REG (temp)) == REG)
4031 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4033 rtx temp1 = simplify_shift_const
4034 (NULL_RTX, ASHIFTRT, mode,
4035 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4036 GET_MODE_BITSIZE (mode) - 1 - i),
4037 GET_MODE_BITSIZE (mode) - 1 - i);
4039 /* If all we did was surround TEMP with the two shifts, we
4040 haven't improved anything, so don't use it. Otherwise,
4041 we are better off with TEMP1. */
4042 if (GET_CODE (temp1) != ASHIFTRT
4043 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4044 || XEXP (XEXP (temp1, 0), 0) != temp)
4045 return temp1;
4047 break;
4049 case TRUNCATE:
4050 /* We can't handle truncation to a partial integer mode here
4051 because we don't know the real bitsize of the partial
4052 integer mode. */
4053 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4054 break;
4056 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4057 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4058 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4059 SUBST (XEXP (x, 0),
4060 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4061 GET_MODE_MASK (mode), NULL_RTX, 0));
4063 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
4064 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4065 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4066 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4067 return XEXP (XEXP (x, 0), 0);
4069 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4070 (OP:SI foo:SI) if OP is NEG or ABS. */
4071 if ((GET_CODE (XEXP (x, 0)) == ABS
4072 || GET_CODE (XEXP (x, 0)) == NEG)
4073 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4074 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4075 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4076 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
4077 XEXP (XEXP (XEXP (x, 0), 0), 0));
4079 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4080 (truncate:SI x). */
4081 if (GET_CODE (XEXP (x, 0)) == SUBREG
4082 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4083 && subreg_lowpart_p (XEXP (x, 0)))
4084 return SUBREG_REG (XEXP (x, 0));
4086 /* If we know that the value is already truncated, we can
4087 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4088 is nonzero for the corresponding modes. But don't do this
4089 for an (LSHIFTRT (MULT ...)) since this will cause problems
4090 with the umulXi3_highpart patterns. */
4091 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4092 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4093 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4094 >= GET_MODE_BITSIZE (mode) + 1
4095 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4096 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4097 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4099 /* A truncate of a comparison can be replaced with a subreg if
4100 STORE_FLAG_VALUE permits. This is like the previous test,
4101 but it works even if the comparison is done in a mode larger
4102 than HOST_BITS_PER_WIDE_INT. */
4103 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4104 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4105 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4106 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4108 /* Similarly, a truncate of a register whose value is a
4109 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4110 permits. */
4111 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4112 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4113 && (temp = get_last_value (XEXP (x, 0)))
4114 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
4115 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4117 break;
4119 case FLOAT_TRUNCATE:
4120 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4121 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4122 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4123 return XEXP (XEXP (x, 0), 0);
4125 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4126 (OP:SF foo:SF) if OP is NEG or ABS. */
4127 if ((GET_CODE (XEXP (x, 0)) == ABS
4128 || GET_CODE (XEXP (x, 0)) == NEG)
4129 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4130 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4131 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
4132 XEXP (XEXP (XEXP (x, 0), 0), 0));
4134 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4135 is (float_truncate:SF x). */
4136 if (GET_CODE (XEXP (x, 0)) == SUBREG
4137 && subreg_lowpart_p (XEXP (x, 0))
4138 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4139 return SUBREG_REG (XEXP (x, 0));
4140 break;
4142 #ifdef HAVE_cc0
4143 case COMPARE:
4144 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4145 using cc0, in which case we want to leave it as a COMPARE
4146 so we can distinguish it from a register-register-copy. */
4147 if (XEXP (x, 1) == const0_rtx)
4148 return XEXP (x, 0);
4150 /* In IEEE floating point, x-0 is not the same as x. */
4151 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
4152 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
4153 || flag_fast_math)
4154 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4155 return XEXP (x, 0);
4156 break;
4157 #endif
4159 case CONST:
4160 /* (const (const X)) can become (const X). Do it this way rather than
4161 returning the inner CONST since CONST can be shared with a
4162 REG_EQUAL note. */
4163 if (GET_CODE (XEXP (x, 0)) == CONST)
4164 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4165 break;
4167 #ifdef HAVE_lo_sum
4168 case LO_SUM:
4169 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4170 can add in an offset. find_split_point will split this address up
4171 again if it doesn't match. */
4172 if (GET_CODE (XEXP (x, 0)) == HIGH
4173 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4174 return XEXP (x, 1);
4175 break;
4176 #endif
4178 case PLUS:
4179 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4180 outermost. That's because that's the way indexed addresses are
4181 supposed to appear. This code used to check many more cases, but
4182 they are now checked elsewhere. */
4183 if (GET_CODE (XEXP (x, 0)) == PLUS
4184 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4185 return gen_binary (PLUS, mode,
4186 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4187 XEXP (x, 1)),
4188 XEXP (XEXP (x, 0), 1));
4190 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4191 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4192 bit-field and can be replaced by either a sign_extend or a
4193 sign_extract. The `and' may be a zero_extend and the two
4194 <c>, -<c> constants may be reversed. */
4195 if (GET_CODE (XEXP (x, 0)) == XOR
4196 && GET_CODE (XEXP (x, 1)) == CONST_INT
4197 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4198 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4199 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4200 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4201 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4202 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4203 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4204 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4205 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4206 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4207 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4208 == (unsigned int) i + 1))))
4209 return simplify_shift_const
4210 (NULL_RTX, ASHIFTRT, mode,
4211 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4212 XEXP (XEXP (XEXP (x, 0), 0), 0),
4213 GET_MODE_BITSIZE (mode) - (i + 1)),
4214 GET_MODE_BITSIZE (mode) - (i + 1));
4216 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4217 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4218 is 1. This produces better code than the alternative immediately
4219 below. */
4220 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4221 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4222 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4223 && (reversed = reversed_comparison (XEXP (x, 0), mode,
4224 XEXP (XEXP (x, 0), 0),
4225 XEXP (XEXP (x, 0), 1))))
4226 return
4227 gen_unary (NEG, mode, mode, reversed);
4229 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4230 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4231 the bitsize of the mode - 1. This allows simplification of
4232 "a = (b & 8) == 0;" */
4233 if (XEXP (x, 1) == constm1_rtx
4234 && GET_CODE (XEXP (x, 0)) != REG
4235 && ! (GET_CODE (XEXP (x,0)) == SUBREG
4236 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4237 && nonzero_bits (XEXP (x, 0), mode) == 1)
4238 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4239 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4240 gen_rtx_combine (XOR, mode,
4241 XEXP (x, 0), const1_rtx),
4242 GET_MODE_BITSIZE (mode) - 1),
4243 GET_MODE_BITSIZE (mode) - 1);
4245 /* If we are adding two things that have no bits in common, convert
4246 the addition into an IOR. This will often be further simplified,
4247 for example in cases like ((a & 1) + (a & 2)), which can
4248 become a & 3. */
4250 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4251 && (nonzero_bits (XEXP (x, 0), mode)
4252 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4254 /* Try to simplify the expression further. */
4255 rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4256 temp = combine_simplify_rtx (tor, mode, last, in_dest);
4258 /* If we could, great. If not, do not go ahead with the IOR
4259 replacement, since PLUS appears in many special purpose
4260 address arithmetic instructions. */
4261 if (GET_CODE (temp) != CLOBBER && temp != tor)
4262 return temp;
4264 break;
4266 case MINUS:
4267 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4268 by reversing the comparison code if valid. */
4269 if (STORE_FLAG_VALUE == 1
4270 && XEXP (x, 0) == const1_rtx
4271 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4272 && (reversed = reversed_comparison (XEXP (x, 1), mode,
4273 XEXP (XEXP (x, 1), 0),
4274 XEXP (XEXP (x, 1), 1))))
4275 return reversed;
4277 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4278 (and <foo> (const_int pow2-1)) */
4279 if (GET_CODE (XEXP (x, 1)) == AND
4280 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4281 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4282 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4283 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4284 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4286 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4287 integers. */
4288 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4289 return gen_binary (MINUS, mode,
4290 gen_binary (MINUS, mode, XEXP (x, 0),
4291 XEXP (XEXP (x, 1), 0)),
4292 XEXP (XEXP (x, 1), 1));
4293 break;
4295 case MULT:
4296 /* If we have (mult (plus A B) C), apply the distributive law and then
4297 the inverse distributive law to see if things simplify. This
4298 occurs mostly in addresses, often when unrolling loops. */
4300 if (GET_CODE (XEXP (x, 0)) == PLUS)
4302 x = apply_distributive_law
4303 (gen_binary (PLUS, mode,
4304 gen_binary (MULT, mode,
4305 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4306 gen_binary (MULT, mode,
4307 XEXP (XEXP (x, 0), 1),
4308 copy_rtx (XEXP (x, 1)))));
4310 if (GET_CODE (x) != MULT)
4311 return x;
4313 break;
4315 case UDIV:
4316 /* If this is a divide by a power of two, treat it as a shift if
4317 its first operand is a shift. */
4318 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4319 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4320 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4321 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4322 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4323 || GET_CODE (XEXP (x, 0)) == ROTATE
4324 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4325 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4326 break;
4328 case EQ: case NE:
4329 case GT: case GTU: case GE: case GEU:
4330 case LT: case LTU: case LE: case LEU:
4331 case UNEQ: case LTGT:
4332 case UNGT: case UNGE:
4333 case UNLT: case UNLE:
4334 case UNORDERED: case ORDERED:
4335 /* If the first operand is a condition code, we can't do anything
4336 with it. */
4337 if (GET_CODE (XEXP (x, 0)) == COMPARE
4338 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4339 #ifdef HAVE_cc0
4340 && XEXP (x, 0) != cc0_rtx
4341 #endif
4344 rtx op0 = XEXP (x, 0);
4345 rtx op1 = XEXP (x, 1);
4346 enum rtx_code new_code;
4348 if (GET_CODE (op0) == COMPARE)
4349 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4351 /* Simplify our comparison, if possible. */
4352 new_code = simplify_comparison (code, &op0, &op1);
4354 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4355 if only the low-order bit is possibly nonzero in X (such as when
4356 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4357 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4358 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4359 (plus X 1).
4361 Remove any ZERO_EXTRACT we made when thinking this was a
4362 comparison. It may now be simpler to use, e.g., an AND. If a
4363 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4364 the call to make_compound_operation in the SET case. */
4366 if (STORE_FLAG_VALUE == 1
4367 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4368 && op1 == const0_rtx
4369 && mode == GET_MODE (op0)
4370 && nonzero_bits (op0, mode) == 1)
4371 return gen_lowpart_for_combine (mode,
4372 expand_compound_operation (op0));
4374 else if (STORE_FLAG_VALUE == 1
4375 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4376 && op1 == const0_rtx
4377 && mode == GET_MODE (op0)
4378 && (num_sign_bit_copies (op0, mode)
4379 == GET_MODE_BITSIZE (mode)))
4381 op0 = expand_compound_operation (op0);
4382 return gen_unary (NEG, mode, mode,
4383 gen_lowpart_for_combine (mode, op0));
4386 else if (STORE_FLAG_VALUE == 1
4387 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4388 && op1 == const0_rtx
4389 && mode == GET_MODE (op0)
4390 && nonzero_bits (op0, mode) == 1)
4392 op0 = expand_compound_operation (op0);
4393 return gen_binary (XOR, mode,
4394 gen_lowpart_for_combine (mode, op0),
4395 const1_rtx);
4398 else if (STORE_FLAG_VALUE == 1
4399 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4400 && op1 == const0_rtx
4401 && mode == GET_MODE (op0)
4402 && (num_sign_bit_copies (op0, mode)
4403 == GET_MODE_BITSIZE (mode)))
4405 op0 = expand_compound_operation (op0);
4406 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4409 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4410 those above. */
4411 if (STORE_FLAG_VALUE == -1
4412 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4413 && op1 == const0_rtx
4414 && (num_sign_bit_copies (op0, mode)
4415 == GET_MODE_BITSIZE (mode)))
4416 return gen_lowpart_for_combine (mode,
4417 expand_compound_operation (op0));
4419 else if (STORE_FLAG_VALUE == -1
4420 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4421 && op1 == const0_rtx
4422 && mode == GET_MODE (op0)
4423 && nonzero_bits (op0, mode) == 1)
4425 op0 = expand_compound_operation (op0);
4426 return gen_unary (NEG, mode, mode,
4427 gen_lowpart_for_combine (mode, op0));
4430 else if (STORE_FLAG_VALUE == -1
4431 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4432 && op1 == const0_rtx
4433 && mode == GET_MODE (op0)
4434 && (num_sign_bit_copies (op0, mode)
4435 == GET_MODE_BITSIZE (mode)))
4437 op0 = expand_compound_operation (op0);
4438 return gen_unary (NOT, mode, mode,
4439 gen_lowpart_for_combine (mode, op0));
4442 /* If X is 0/1, (eq X 0) is X-1. */
4443 else if (STORE_FLAG_VALUE == -1
4444 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4445 && op1 == const0_rtx
4446 && mode == GET_MODE (op0)
4447 && nonzero_bits (op0, mode) == 1)
4449 op0 = expand_compound_operation (op0);
4450 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4453 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4454 one bit that might be nonzero, we can convert (ne x 0) to
4455 (ashift x c) where C puts the bit in the sign bit. Remove any
4456 AND with STORE_FLAG_VALUE when we are done, since we are only
4457 going to test the sign bit. */
4458 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4459 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4460 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4461 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE(mode)-1))
4462 && op1 == const0_rtx
4463 && mode == GET_MODE (op0)
4464 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4466 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4467 expand_compound_operation (op0),
4468 GET_MODE_BITSIZE (mode) - 1 - i);
4469 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4470 return XEXP (x, 0);
4471 else
4472 return x;
4475 /* If the code changed, return a whole new comparison. */
4476 if (new_code != code)
4477 return gen_rtx_combine (new_code, mode, op0, op1);
4479 /* Otherwise, keep this operation, but maybe change its operands.
4480 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4481 SUBST (XEXP (x, 0), op0);
4482 SUBST (XEXP (x, 1), op1);
4484 break;
4486 case IF_THEN_ELSE:
4487 return simplify_if_then_else (x);
4489 case ZERO_EXTRACT:
4490 case SIGN_EXTRACT:
4491 case ZERO_EXTEND:
4492 case SIGN_EXTEND:
4493 /* If we are processing SET_DEST, we are done. */
4494 if (in_dest)
4495 return x;
4497 return expand_compound_operation (x);
4499 case SET:
4500 return simplify_set (x);
4502 case AND:
4503 case IOR:
4504 case XOR:
4505 return simplify_logical (x, last);
4507 case ABS:
4508 /* (abs (neg <foo>)) -> (abs <foo>) */
4509 if (GET_CODE (XEXP (x, 0)) == NEG)
4510 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4512 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4513 do nothing. */
4514 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4515 break;
4517 /* If operand is something known to be positive, ignore the ABS. */
4518 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4519 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4520 <= HOST_BITS_PER_WIDE_INT)
4521 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4522 & ((HOST_WIDE_INT) 1
4523 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4524 == 0)))
4525 return XEXP (x, 0);
4527 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4528 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4529 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4531 break;
4533 case FFS:
4534 /* (ffs (*_extend <X>)) = (ffs <X>) */
4535 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4536 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4537 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4538 break;
4540 case FLOAT:
4541 /* (float (sign_extend <X>)) = (float <X>). */
4542 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4543 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4544 break;
4546 case ASHIFT:
4547 case LSHIFTRT:
4548 case ASHIFTRT:
4549 case ROTATE:
4550 case ROTATERT:
4551 /* If this is a shift by a constant amount, simplify it. */
4552 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4553 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4554 INTVAL (XEXP (x, 1)));
4556 #ifdef SHIFT_COUNT_TRUNCATED
4557 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4558 SUBST (XEXP (x, 1),
4559 force_to_mode (XEXP (x, 1), GET_MODE (x),
4560 ((HOST_WIDE_INT) 1
4561 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4562 - 1,
4563 NULL_RTX, 0));
4564 #endif
4566 break;
4568 case VEC_SELECT:
4570 rtx op0 = XEXP (x, 0);
4571 rtx op1 = XEXP (x, 1);
4572 int len;
4574 if (GET_CODE (op1) != PARALLEL)
4575 abort ();
4576 len = XVECLEN (op1, 0);
4577 if (len == 1
4578 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4579 && GET_CODE (op0) == VEC_CONCAT)
4581 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4583 /* Try to find the element in the VEC_CONCAT. */
4584 for (;;)
4586 if (GET_MODE (op0) == GET_MODE (x))
4587 return op0;
4588 if (GET_CODE (op0) == VEC_CONCAT)
4590 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4591 if (op0_size < offset)
4592 op0 = XEXP (op0, 0);
4593 else
4595 offset -= op0_size;
4596 op0 = XEXP (op0, 1);
4599 else
4600 break;
4605 break;
4607 default:
4608 break;
4611 return x;
4614 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4616 static rtx
4617 simplify_if_then_else (x)
4618 rtx x;
4620 enum machine_mode mode = GET_MODE (x);
4621 rtx cond = XEXP (x, 0);
4622 rtx true = XEXP (x, 1);
4623 rtx false = XEXP (x, 2);
4624 enum rtx_code true_code = GET_CODE (cond);
4625 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4626 rtx temp;
4627 int i;
4628 enum rtx_code false_code;
4629 rtx reversed;
4631 /* Simplify storing of the truth value. */
4632 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4633 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4635 /* Also when the truth value has to be reversed. */
4636 if (comparison_p
4637 && true == const0_rtx && false == const_true_rtx
4638 && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4639 XEXP (cond, 1))))
4640 return reversed;
4642 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4643 in it is being compared against certain values. Get the true and false
4644 comparisons and see if that says anything about the value of each arm. */
4646 if (comparison_p
4647 && ((false_code = combine_reversed_comparison_code (cond))
4648 != UNKNOWN)
4649 && GET_CODE (XEXP (cond, 0)) == REG)
4651 HOST_WIDE_INT nzb;
4652 rtx from = XEXP (cond, 0);
4653 rtx true_val = XEXP (cond, 1);
4654 rtx false_val = true_val;
4655 int swapped = 0;
4657 /* If FALSE_CODE is EQ, swap the codes and arms. */
4659 if (false_code == EQ)
4661 swapped = 1, true_code = EQ, false_code = NE;
4662 temp = true, true = false, false = temp;
4665 /* If we are comparing against zero and the expression being tested has
4666 only a single bit that might be nonzero, that is its value when it is
4667 not equal to zero. Similarly if it is known to be -1 or 0. */
4669 if (true_code == EQ && true_val == const0_rtx
4670 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4671 false_code = EQ, false_val = GEN_INT (nzb);
4672 else if (true_code == EQ && true_val == const0_rtx
4673 && (num_sign_bit_copies (from, GET_MODE (from))
4674 == GET_MODE_BITSIZE (GET_MODE (from))))
4675 false_code = EQ, false_val = constm1_rtx;
4677 /* Now simplify an arm if we know the value of the register in the
4678 branch and it is used in the arm. Be careful due to the potential
4679 of locally-shared RTL. */
4681 if (reg_mentioned_p (from, true))
4682 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4683 pc_rtx, pc_rtx, 0, 0);
4684 if (reg_mentioned_p (from, false))
4685 false = subst (known_cond (copy_rtx (false), false_code,
4686 from, false_val),
4687 pc_rtx, pc_rtx, 0, 0);
4689 SUBST (XEXP (x, 1), swapped ? false : true);
4690 SUBST (XEXP (x, 2), swapped ? true : false);
4692 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4695 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4696 reversed, do so to avoid needing two sets of patterns for
4697 subtract-and-branch insns. Similarly if we have a constant in the true
4698 arm, the false arm is the same as the first operand of the comparison, or
4699 the false arm is more complicated than the true arm. */
4701 if (comparison_p
4702 && combine_reversed_comparison_code (cond) != UNKNOWN
4703 && (true == pc_rtx
4704 || (CONSTANT_P (true)
4705 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4706 || true == const0_rtx
4707 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4708 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4709 || (GET_CODE (true) == SUBREG
4710 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4711 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4712 || reg_mentioned_p (true, false)
4713 || rtx_equal_p (false, XEXP (cond, 0))))
4715 true_code = reversed_comparison_code (cond, NULL);
4716 SUBST (XEXP (x, 0),
4717 reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4718 XEXP (cond, 1)));
4720 SUBST (XEXP (x, 1), false);
4721 SUBST (XEXP (x, 2), true);
4723 temp = true, true = false, false = temp, cond = XEXP (x, 0);
4725 /* It is possible that the conditional has been simplified out. */
4726 true_code = GET_CODE (cond);
4727 comparison_p = GET_RTX_CLASS (true_code) == '<';
4730 /* If the two arms are identical, we don't need the comparison. */
4732 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4733 return true;
4735 /* Convert a == b ? b : a to "a". */
4736 if (true_code == EQ && ! side_effects_p (cond)
4737 && (! FLOAT_MODE_P (mode) || flag_fast_math)
4738 && rtx_equal_p (XEXP (cond, 0), false)
4739 && rtx_equal_p (XEXP (cond, 1), true))
4740 return false;
4741 else if (true_code == NE && ! side_effects_p (cond)
4742 && (! FLOAT_MODE_P (mode) || flag_fast_math)
4743 && rtx_equal_p (XEXP (cond, 0), true)
4744 && rtx_equal_p (XEXP (cond, 1), false))
4745 return true;
4747 /* Look for cases where we have (abs x) or (neg (abs X)). */
4749 if (GET_MODE_CLASS (mode) == MODE_INT
4750 && GET_CODE (false) == NEG
4751 && rtx_equal_p (true, XEXP (false, 0))
4752 && comparison_p
4753 && rtx_equal_p (true, XEXP (cond, 0))
4754 && ! side_effects_p (true))
4755 switch (true_code)
4757 case GT:
4758 case GE:
4759 return gen_unary (ABS, mode, mode, true);
4760 case LT:
4761 case LE:
4762 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4763 default:
4764 break;
4767 /* Look for MIN or MAX. */
4769 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4770 && comparison_p
4771 && rtx_equal_p (XEXP (cond, 0), true)
4772 && rtx_equal_p (XEXP (cond, 1), false)
4773 && ! side_effects_p (cond))
4774 switch (true_code)
4776 case GE:
4777 case GT:
4778 return gen_binary (SMAX, mode, true, false);
4779 case LE:
4780 case LT:
4781 return gen_binary (SMIN, mode, true, false);
4782 case GEU:
4783 case GTU:
4784 return gen_binary (UMAX, mode, true, false);
4785 case LEU:
4786 case LTU:
4787 return gen_binary (UMIN, mode, true, false);
4788 default:
4789 break;
4792 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4793 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4794 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4795 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4796 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4797 neither 1 or -1, but it isn't worth checking for. */
4799 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4800 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4802 rtx t = make_compound_operation (true, SET);
4803 rtx f = make_compound_operation (false, SET);
4804 rtx cond_op0 = XEXP (cond, 0);
4805 rtx cond_op1 = XEXP (cond, 1);
4806 enum rtx_code op = NIL, extend_op = NIL;
4807 enum machine_mode m = mode;
4808 rtx z = 0, c1 = NULL_RTX;
4810 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4811 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4812 || GET_CODE (t) == ASHIFT
4813 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4814 && rtx_equal_p (XEXP (t, 0), f))
4815 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4817 /* If an identity-zero op is commutative, check whether there
4818 would be a match if we swapped the operands. */
4819 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4820 || GET_CODE (t) == XOR)
4821 && rtx_equal_p (XEXP (t, 1), f))
4822 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4823 else if (GET_CODE (t) == SIGN_EXTEND
4824 && (GET_CODE (XEXP (t, 0)) == PLUS
4825 || GET_CODE (XEXP (t, 0)) == MINUS
4826 || GET_CODE (XEXP (t, 0)) == IOR
4827 || GET_CODE (XEXP (t, 0)) == XOR
4828 || GET_CODE (XEXP (t, 0)) == ASHIFT
4829 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4830 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4831 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4832 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4833 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4834 && (num_sign_bit_copies (f, GET_MODE (f))
4835 > (GET_MODE_BITSIZE (mode)
4836 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4838 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4839 extend_op = SIGN_EXTEND;
4840 m = GET_MODE (XEXP (t, 0));
4842 else if (GET_CODE (t) == SIGN_EXTEND
4843 && (GET_CODE (XEXP (t, 0)) == PLUS
4844 || GET_CODE (XEXP (t, 0)) == IOR
4845 || GET_CODE (XEXP (t, 0)) == XOR)
4846 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4847 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4848 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4849 && (num_sign_bit_copies (f, GET_MODE (f))
4850 > (GET_MODE_BITSIZE (mode)
4851 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4853 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4854 extend_op = SIGN_EXTEND;
4855 m = GET_MODE (XEXP (t, 0));
4857 else if (GET_CODE (t) == ZERO_EXTEND
4858 && (GET_CODE (XEXP (t, 0)) == PLUS
4859 || GET_CODE (XEXP (t, 0)) == MINUS
4860 || GET_CODE (XEXP (t, 0)) == IOR
4861 || GET_CODE (XEXP (t, 0)) == XOR
4862 || GET_CODE (XEXP (t, 0)) == ASHIFT
4863 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4864 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4865 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4866 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4867 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4868 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4869 && ((nonzero_bits (f, GET_MODE (f))
4870 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4871 == 0))
4873 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4874 extend_op = ZERO_EXTEND;
4875 m = GET_MODE (XEXP (t, 0));
4877 else if (GET_CODE (t) == ZERO_EXTEND
4878 && (GET_CODE (XEXP (t, 0)) == PLUS
4879 || GET_CODE (XEXP (t, 0)) == IOR
4880 || GET_CODE (XEXP (t, 0)) == XOR)
4881 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4882 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4883 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4884 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4885 && ((nonzero_bits (f, GET_MODE (f))
4886 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4887 == 0))
4889 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4890 extend_op = ZERO_EXTEND;
4891 m = GET_MODE (XEXP (t, 0));
4894 if (z)
4896 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4897 pc_rtx, pc_rtx, 0, 0);
4898 temp = gen_binary (MULT, m, temp,
4899 gen_binary (MULT, m, c1, const_true_rtx));
4900 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4901 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4903 if (extend_op != NIL)
4904 temp = gen_unary (extend_op, mode, m, temp);
4906 return temp;
4910 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4911 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4912 negation of a single bit, we can convert this operation to a shift. We
4913 can actually do this more generally, but it doesn't seem worth it. */
4915 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4916 && false == const0_rtx && GET_CODE (true) == CONST_INT
4917 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4918 && (i = exact_log2 (INTVAL (true))) >= 0)
4919 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4920 == GET_MODE_BITSIZE (mode))
4921 && (i = exact_log2 (-INTVAL (true))) >= 0)))
4922 return
4923 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4924 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4926 return x;
4929 /* Simplify X, a SET expression. Return the new expression. */
4931 static rtx
4932 simplify_set (x)
4933 rtx x;
4935 rtx src = SET_SRC (x);
4936 rtx dest = SET_DEST (x);
4937 enum machine_mode mode
4938 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4939 rtx other_insn;
4940 rtx *cc_use;
4942 /* (set (pc) (return)) gets written as (return). */
4943 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4944 return src;
4946 /* Now that we know for sure which bits of SRC we are using, see if we can
4947 simplify the expression for the object knowing that we only need the
4948 low-order bits. */
4950 if (GET_MODE_CLASS (mode) == MODE_INT)
4952 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
4953 SUBST (SET_SRC (x), src);
4956 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4957 the comparison result and try to simplify it unless we already have used
4958 undobuf.other_insn. */
4959 if ((GET_CODE (src) == COMPARE
4960 #ifdef HAVE_cc0
4961 || dest == cc0_rtx
4962 #endif
4964 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4965 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4966 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4967 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4969 enum rtx_code old_code = GET_CODE (*cc_use);
4970 enum rtx_code new_code;
4971 rtx op0, op1;
4972 int other_changed = 0;
4973 enum machine_mode compare_mode = GET_MODE (dest);
4975 if (GET_CODE (src) == COMPARE)
4976 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4977 else
4978 op0 = src, op1 = const0_rtx;
4980 /* Simplify our comparison, if possible. */
4981 new_code = simplify_comparison (old_code, &op0, &op1);
4983 #ifdef EXTRA_CC_MODES
4984 /* If this machine has CC modes other than CCmode, check to see if we
4985 need to use a different CC mode here. */
4986 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4987 #endif /* EXTRA_CC_MODES */
4989 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4990 /* If the mode changed, we have to change SET_DEST, the mode in the
4991 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4992 a hard register, just build new versions with the proper mode. If it
4993 is a pseudo, we lose unless it is only time we set the pseudo, in
4994 which case we can safely change its mode. */
4995 if (compare_mode != GET_MODE (dest))
4997 unsigned int regno = REGNO (dest);
4998 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5000 if (regno < FIRST_PSEUDO_REGISTER
5001 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5003 if (regno >= FIRST_PSEUDO_REGISTER)
5004 SUBST (regno_reg_rtx[regno], new_dest);
5006 SUBST (SET_DEST (x), new_dest);
5007 SUBST (XEXP (*cc_use, 0), new_dest);
5008 other_changed = 1;
5010 dest = new_dest;
5013 #endif
5015 /* If the code changed, we have to build a new comparison in
5016 undobuf.other_insn. */
5017 if (new_code != old_code)
5019 unsigned HOST_WIDE_INT mask;
5021 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
5022 dest, const0_rtx));
5024 /* If the only change we made was to change an EQ into an NE or
5025 vice versa, OP0 has only one bit that might be nonzero, and OP1
5026 is zero, check if changing the user of the condition code will
5027 produce a valid insn. If it won't, we can keep the original code
5028 in that insn by surrounding our operation with an XOR. */
5030 if (((old_code == NE && new_code == EQ)
5031 || (old_code == EQ && new_code == NE))
5032 && ! other_changed && op1 == const0_rtx
5033 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5034 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5036 rtx pat = PATTERN (other_insn), note = 0;
5038 if ((recog_for_combine (&pat, other_insn, &note) < 0
5039 && ! check_asm_operands (pat)))
5041 PUT_CODE (*cc_use, old_code);
5042 other_insn = 0;
5044 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5048 other_changed = 1;
5051 if (other_changed)
5052 undobuf.other_insn = other_insn;
5054 #ifdef HAVE_cc0
5055 /* If we are now comparing against zero, change our source if
5056 needed. If we do not use cc0, we always have a COMPARE. */
5057 if (op1 == const0_rtx && dest == cc0_rtx)
5059 SUBST (SET_SRC (x), op0);
5060 src = op0;
5062 else
5063 #endif
5065 /* Otherwise, if we didn't previously have a COMPARE in the
5066 correct mode, we need one. */
5067 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5069 SUBST (SET_SRC (x),
5070 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
5071 src = SET_SRC (x);
5073 else
5075 /* Otherwise, update the COMPARE if needed. */
5076 SUBST (XEXP (src, 0), op0);
5077 SUBST (XEXP (src, 1), op1);
5080 else
5082 /* Get SET_SRC in a form where we have placed back any
5083 compound expressions. Then do the checks below. */
5084 src = make_compound_operation (src, SET);
5085 SUBST (SET_SRC (x), src);
5088 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5089 and X being a REG or (subreg (reg)), we may be able to convert this to
5090 (set (subreg:m2 x) (op)).
5092 We can always do this if M1 is narrower than M2 because that means that
5093 we only care about the low bits of the result.
5095 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5096 perform a narrower operation than requested since the high-order bits will
5097 be undefined. On machine where it is defined, this transformation is safe
5098 as long as M1 and M2 have the same number of words. */
5100 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5101 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
5102 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5103 / UNITS_PER_WORD)
5104 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5105 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5106 #ifndef WORD_REGISTER_OPERATIONS
5107 && (GET_MODE_SIZE (GET_MODE (src))
5108 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5109 #endif
5110 #ifdef CLASS_CANNOT_CHANGE_MODE
5111 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5112 && (TEST_HARD_REG_BIT
5113 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_MODE],
5114 REGNO (dest)))
5115 && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (src),
5116 GET_MODE (SUBREG_REG (src))))
5117 #endif
5118 && (GET_CODE (dest) == REG
5119 || (GET_CODE (dest) == SUBREG
5120 && GET_CODE (SUBREG_REG (dest)) == REG)))
5122 SUBST (SET_DEST (x),
5123 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
5124 dest));
5125 SUBST (SET_SRC (x), SUBREG_REG (src));
5127 src = SET_SRC (x), dest = SET_DEST (x);
5130 #ifdef LOAD_EXTEND_OP
5131 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5132 would require a paradoxical subreg. Replace the subreg with a
5133 zero_extend to avoid the reload that would otherwise be required. */
5135 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5136 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5137 && SUBREG_WORD (src) == 0
5138 && (GET_MODE_SIZE (GET_MODE (src))
5139 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5140 && GET_CODE (SUBREG_REG (src)) == MEM)
5142 SUBST (SET_SRC (x),
5143 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5144 GET_MODE (src), XEXP (src, 0)));
5146 src = SET_SRC (x);
5148 #endif
5150 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5151 are comparing an item known to be 0 or -1 against 0, use a logical
5152 operation instead. Check for one of the arms being an IOR of the other
5153 arm with some value. We compute three terms to be IOR'ed together. In
5154 practice, at most two will be nonzero. Then we do the IOR's. */
5156 if (GET_CODE (dest) != PC
5157 && GET_CODE (src) == IF_THEN_ELSE
5158 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5159 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5160 && XEXP (XEXP (src, 0), 1) == const0_rtx
5161 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5162 #ifdef HAVE_conditional_move
5163 && ! can_conditionally_move_p (GET_MODE (src))
5164 #endif
5165 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5166 GET_MODE (XEXP (XEXP (src, 0), 0)))
5167 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5168 && ! side_effects_p (src))
5170 rtx true = (GET_CODE (XEXP (src, 0)) == NE
5171 ? XEXP (src, 1) : XEXP (src, 2));
5172 rtx false = (GET_CODE (XEXP (src, 0)) == NE
5173 ? XEXP (src, 2) : XEXP (src, 1));
5174 rtx term1 = const0_rtx, term2, term3;
5176 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
5177 term1 = false, true = XEXP (true, 1), false = const0_rtx;
5178 else if (GET_CODE (true) == IOR
5179 && rtx_equal_p (XEXP (true, 1), false))
5180 term1 = false, true = XEXP (true, 0), false = const0_rtx;
5181 else if (GET_CODE (false) == IOR
5182 && rtx_equal_p (XEXP (false, 0), true))
5183 term1 = true, false = XEXP (false, 1), true = const0_rtx;
5184 else if (GET_CODE (false) == IOR
5185 && rtx_equal_p (XEXP (false, 1), true))
5186 term1 = true, false = XEXP (false, 0), true = const0_rtx;
5188 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
5189 term3 = gen_binary (AND, GET_MODE (src),
5190 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
5191 XEXP (XEXP (src, 0), 0)),
5192 false);
5194 SUBST (SET_SRC (x),
5195 gen_binary (IOR, GET_MODE (src),
5196 gen_binary (IOR, GET_MODE (src), term1, term2),
5197 term3));
5199 src = SET_SRC (x);
5202 #ifdef HAVE_conditional_arithmetic
5203 /* If we have conditional arithmetic and the operand of a SET is
5204 a conditional expression, replace this with an IF_THEN_ELSE.
5205 We can either have a conditional expression or a MULT of that expression
5206 with a constant. */
5207 if ((GET_RTX_CLASS (GET_CODE (src)) == '1'
5208 || GET_RTX_CLASS (GET_CODE (src)) == '2'
5209 || GET_RTX_CLASS (GET_CODE (src)) == 'c')
5210 && (GET_RTX_CLASS (GET_CODE (XEXP (src, 0))) == '<'
5211 || (GET_CODE (XEXP (src, 0)) == MULT
5212 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (src, 0), 0))) == '<'
5213 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT)))
5215 rtx cond = XEXP (src, 0);
5216 rtx true_val = const1_rtx;
5217 rtx false_arm, true_arm;
5218 rtx reversed;
5220 if (GET_CODE (cond) == MULT)
5222 true_val = XEXP (cond, 1);
5223 cond = XEXP (cond, 0);
5226 if (GET_RTX_CLASS (GET_CODE (src)) == '1')
5228 true_arm = gen_unary (GET_CODE (src), GET_MODE (src),
5229 GET_MODE (XEXP (src, 0)), true_val);
5230 false_arm = gen_unary (GET_CODE (src), GET_MODE (src),
5231 GET_MODE (XEXP (src, 0)), const0_rtx);
5233 else
5235 true_arm = gen_binary (GET_CODE (src), GET_MODE (src),
5236 true_val, XEXP (src, 1));
5237 false_arm = gen_binary (GET_CODE (src), GET_MODE (src),
5238 const0_rtx, XEXP (src, 1));
5241 /* Canonicalize if true_arm is the simpler one. */
5242 if (GET_RTX_CLASS (GET_CODE (true_arm)) == 'o'
5243 && GET_RTX_CLASS (GET_CODE (false_arm)) != 'o'
5244 && (reversed = reversed_comparison_code (cond, GET_MODE (cond),
5245 XEXP (cond, 0),
5246 XEXP (cond, 1))))
5248 rtx temp = true_arm;
5250 true_arm = false_arm;
5251 false_arm = temp;
5253 cond = reversed;
5256 src = gen_rtx_combine (IF_THEN_ELSE, GET_MODE (src),
5257 gen_rtx_combine (GET_CODE (cond), VOIDmode,
5258 XEXP (cond, 0),
5259 XEXP (cond, 1)),
5260 true_arm, false_arm);
5261 SUBST (SET_SRC (x), src);
5263 #endif
5265 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5266 whole thing fail. */
5267 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5268 return src;
5269 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5270 return dest;
5271 else
5272 /* Convert this into a field assignment operation, if possible. */
5273 return make_field_assignment (x);
5276 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5277 result. LAST is nonzero if this is the last retry. */
5279 static rtx
5280 simplify_logical (x, last)
5281 rtx x;
5282 int last;
5284 enum machine_mode mode = GET_MODE (x);
5285 rtx op0 = XEXP (x, 0);
5286 rtx op1 = XEXP (x, 1);
5287 rtx reversed;
5289 switch (GET_CODE (x))
5291 case AND:
5292 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5293 insn (and may simplify more). */
5294 if (GET_CODE (op0) == XOR
5295 && rtx_equal_p (XEXP (op0, 0), op1)
5296 && ! side_effects_p (op1))
5297 x = gen_binary (AND, mode,
5298 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
5300 if (GET_CODE (op0) == XOR
5301 && rtx_equal_p (XEXP (op0, 1), op1)
5302 && ! side_effects_p (op1))
5303 x = gen_binary (AND, mode,
5304 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
5306 /* Similarly for (~(A ^ B)) & A. */
5307 if (GET_CODE (op0) == NOT
5308 && GET_CODE (XEXP (op0, 0)) == XOR
5309 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5310 && ! side_effects_p (op1))
5311 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5313 if (GET_CODE (op0) == NOT
5314 && GET_CODE (XEXP (op0, 0)) == XOR
5315 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5316 && ! side_effects_p (op1))
5317 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5319 /* We can call simplify_and_const_int only if we don't lose
5320 any (sign) bits when converting INTVAL (op1) to
5321 "unsigned HOST_WIDE_INT". */
5322 if (GET_CODE (op1) == CONST_INT
5323 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5324 || INTVAL (op1) > 0))
5326 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5328 /* If we have (ior (and (X C1) C2)) and the next restart would be
5329 the last, simplify this by making C1 as small as possible
5330 and then exit. */
5331 if (last
5332 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5333 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5334 && GET_CODE (op1) == CONST_INT)
5335 return gen_binary (IOR, mode,
5336 gen_binary (AND, mode, XEXP (op0, 0),
5337 GEN_INT (INTVAL (XEXP (op0, 1))
5338 & ~INTVAL (op1))), op1);
5340 if (GET_CODE (x) != AND)
5341 return x;
5343 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5344 || GET_RTX_CLASS (GET_CODE (x)) == '2')
5345 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5348 /* Convert (A | B) & A to A. */
5349 if (GET_CODE (op0) == IOR
5350 && (rtx_equal_p (XEXP (op0, 0), op1)
5351 || rtx_equal_p (XEXP (op0, 1), op1))
5352 && ! side_effects_p (XEXP (op0, 0))
5353 && ! side_effects_p (XEXP (op0, 1)))
5354 return op1;
5356 /* In the following group of tests (and those in case IOR below),
5357 we start with some combination of logical operations and apply
5358 the distributive law followed by the inverse distributive law.
5359 Most of the time, this results in no change. However, if some of
5360 the operands are the same or inverses of each other, simplifications
5361 will result.
5363 For example, (and (ior A B) (not B)) can occur as the result of
5364 expanding a bit field assignment. When we apply the distributive
5365 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5366 which then simplifies to (and (A (not B))).
5368 If we have (and (ior A B) C), apply the distributive law and then
5369 the inverse distributive law to see if things simplify. */
5371 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5373 x = apply_distributive_law
5374 (gen_binary (GET_CODE (op0), mode,
5375 gen_binary (AND, mode, XEXP (op0, 0), op1),
5376 gen_binary (AND, mode, XEXP (op0, 1),
5377 copy_rtx (op1))));
5378 if (GET_CODE (x) != AND)
5379 return x;
5382 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5383 return apply_distributive_law
5384 (gen_binary (GET_CODE (op1), mode,
5385 gen_binary (AND, mode, XEXP (op1, 0), op0),
5386 gen_binary (AND, mode, XEXP (op1, 1),
5387 copy_rtx (op0))));
5389 /* Similarly, taking advantage of the fact that
5390 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5392 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5393 return apply_distributive_law
5394 (gen_binary (XOR, mode,
5395 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5396 gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5397 XEXP (op1, 1))));
5399 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5400 return apply_distributive_law
5401 (gen_binary (XOR, mode,
5402 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5403 gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5404 break;
5406 case IOR:
5407 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5408 if (GET_CODE (op1) == CONST_INT
5409 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5410 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5411 return op1;
5413 /* Convert (A & B) | A to A. */
5414 if (GET_CODE (op0) == AND
5415 && (rtx_equal_p (XEXP (op0, 0), op1)
5416 || rtx_equal_p (XEXP (op0, 1), op1))
5417 && ! side_effects_p (XEXP (op0, 0))
5418 && ! side_effects_p (XEXP (op0, 1)))
5419 return op1;
5421 /* If we have (ior (and A B) C), apply the distributive law and then
5422 the inverse distributive law to see if things simplify. */
5424 if (GET_CODE (op0) == AND)
5426 x = apply_distributive_law
5427 (gen_binary (AND, mode,
5428 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5429 gen_binary (IOR, mode, XEXP (op0, 1),
5430 copy_rtx (op1))));
5432 if (GET_CODE (x) != IOR)
5433 return x;
5436 if (GET_CODE (op1) == AND)
5438 x = apply_distributive_law
5439 (gen_binary (AND, mode,
5440 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5441 gen_binary (IOR, mode, XEXP (op1, 1),
5442 copy_rtx (op0))));
5444 if (GET_CODE (x) != IOR)
5445 return x;
5448 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5449 mode size to (rotate A CX). */
5451 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5452 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5453 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5454 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5455 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5456 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5457 == GET_MODE_BITSIZE (mode)))
5458 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5459 (GET_CODE (op0) == ASHIFT
5460 ? XEXP (op0, 1) : XEXP (op1, 1)));
5462 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5463 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5464 does not affect any of the bits in OP1, it can really be done
5465 as a PLUS and we can associate. We do this by seeing if OP1
5466 can be safely shifted left C bits. */
5467 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5468 && GET_CODE (XEXP (op0, 0)) == PLUS
5469 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5470 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5471 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5473 int count = INTVAL (XEXP (op0, 1));
5474 HOST_WIDE_INT mask = INTVAL (op1) << count;
5476 if (mask >> count == INTVAL (op1)
5477 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5479 SUBST (XEXP (XEXP (op0, 0), 1),
5480 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5481 return op0;
5484 break;
5486 case XOR:
5487 /* If we are XORing two things that have no bits in common,
5488 convert them into an IOR. This helps to detect rotation encoded
5489 using those methods and possibly other simplifications. */
5491 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5492 && (nonzero_bits (op0, mode)
5493 & nonzero_bits (op1, mode)) == 0)
5494 return (gen_binary (IOR, mode, op0, op1));
5496 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5497 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5498 (NOT y). */
5500 int num_negated = 0;
5502 if (GET_CODE (op0) == NOT)
5503 num_negated++, op0 = XEXP (op0, 0);
5504 if (GET_CODE (op1) == NOT)
5505 num_negated++, op1 = XEXP (op1, 0);
5507 if (num_negated == 2)
5509 SUBST (XEXP (x, 0), op0);
5510 SUBST (XEXP (x, 1), op1);
5512 else if (num_negated == 1)
5513 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5516 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5517 correspond to a machine insn or result in further simplifications
5518 if B is a constant. */
5520 if (GET_CODE (op0) == AND
5521 && rtx_equal_p (XEXP (op0, 1), op1)
5522 && ! side_effects_p (op1))
5523 return gen_binary (AND, mode,
5524 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5525 op1);
5527 else if (GET_CODE (op0) == AND
5528 && rtx_equal_p (XEXP (op0, 0), op1)
5529 && ! side_effects_p (op1))
5530 return gen_binary (AND, mode,
5531 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5532 op1);
5534 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5535 comparison if STORE_FLAG_VALUE is 1. */
5536 if (STORE_FLAG_VALUE == 1
5537 && op1 == const1_rtx
5538 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5539 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5540 XEXP (op0, 1))))
5541 return reversed;
5543 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5544 is (lt foo (const_int 0)), so we can perform the above
5545 simplification if STORE_FLAG_VALUE is 1. */
5547 if (STORE_FLAG_VALUE == 1
5548 && op1 == const1_rtx
5549 && GET_CODE (op0) == LSHIFTRT
5550 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5551 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5552 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5554 /* (xor (comparison foo bar) (const_int sign-bit))
5555 when STORE_FLAG_VALUE is the sign bit. */
5556 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5557 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5558 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5559 && op1 == const_true_rtx
5560 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5561 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5562 XEXP (op0, 1))))
5563 return reversed;
5565 break;
5567 default:
5568 abort ();
5571 return x;
5574 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5575 operations" because they can be replaced with two more basic operations.
5576 ZERO_EXTEND is also considered "compound" because it can be replaced with
5577 an AND operation, which is simpler, though only one operation.
5579 The function expand_compound_operation is called with an rtx expression
5580 and will convert it to the appropriate shifts and AND operations,
5581 simplifying at each stage.
5583 The function make_compound_operation is called to convert an expression
5584 consisting of shifts and ANDs into the equivalent compound expression.
5585 It is the inverse of this function, loosely speaking. */
5587 static rtx
5588 expand_compound_operation (x)
5589 rtx x;
5591 unsigned HOST_WIDE_INT pos = 0, len;
5592 int unsignedp = 0;
5593 unsigned int modewidth;
5594 rtx tem;
5596 switch (GET_CODE (x))
5598 case ZERO_EXTEND:
5599 unsignedp = 1;
5600 case SIGN_EXTEND:
5601 /* We can't necessarily use a const_int for a multiword mode;
5602 it depends on implicitly extending the value.
5603 Since we don't know the right way to extend it,
5604 we can't tell whether the implicit way is right.
5606 Even for a mode that is no wider than a const_int,
5607 we can't win, because we need to sign extend one of its bits through
5608 the rest of it, and we don't know which bit. */
5609 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5610 return x;
5612 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5613 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5614 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5615 reloaded. If not for that, MEM's would very rarely be safe.
5617 Reject MODEs bigger than a word, because we might not be able
5618 to reference a two-register group starting with an arbitrary register
5619 (and currently gen_lowpart might crash for a SUBREG). */
5621 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5622 return x;
5624 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5625 /* If the inner object has VOIDmode (the only way this can happen
5626 is if it is a ASM_OPERANDS), we can't do anything since we don't
5627 know how much masking to do. */
5628 if (len == 0)
5629 return x;
5631 break;
5633 case ZERO_EXTRACT:
5634 unsignedp = 1;
5635 case SIGN_EXTRACT:
5636 /* If the operand is a CLOBBER, just return it. */
5637 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5638 return XEXP (x, 0);
5640 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5641 || GET_CODE (XEXP (x, 2)) != CONST_INT
5642 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5643 return x;
5645 len = INTVAL (XEXP (x, 1));
5646 pos = INTVAL (XEXP (x, 2));
5648 /* If this goes outside the object being extracted, replace the object
5649 with a (use (mem ...)) construct that only combine understands
5650 and is used only for this purpose. */
5651 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5652 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5654 if (BITS_BIG_ENDIAN)
5655 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5657 break;
5659 default:
5660 return x;
5662 /* Convert sign extension to zero extension, if we know that the high
5663 bit is not set, as this is easier to optimize. It will be converted
5664 back to cheaper alternative in make_extraction. */
5665 if (GET_CODE (x) == SIGN_EXTEND
5666 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5667 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5668 & ~(((unsigned HOST_WIDE_INT)
5669 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5670 >> 1))
5671 == 0)))
5673 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5674 return expand_compound_operation (temp);
5677 /* We can optimize some special cases of ZERO_EXTEND. */
5678 if (GET_CODE (x) == ZERO_EXTEND)
5680 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5681 know that the last value didn't have any inappropriate bits
5682 set. */
5683 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5684 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5685 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5686 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5687 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5688 return XEXP (XEXP (x, 0), 0);
5690 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5691 if (GET_CODE (XEXP (x, 0)) == SUBREG
5692 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5693 && subreg_lowpart_p (XEXP (x, 0))
5694 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5695 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5696 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5697 return SUBREG_REG (XEXP (x, 0));
5699 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5700 is a comparison and STORE_FLAG_VALUE permits. This is like
5701 the first case, but it works even when GET_MODE (x) is larger
5702 than HOST_WIDE_INT. */
5703 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5704 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5705 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5706 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5707 <= HOST_BITS_PER_WIDE_INT)
5708 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5709 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5710 return XEXP (XEXP (x, 0), 0);
5712 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5713 if (GET_CODE (XEXP (x, 0)) == SUBREG
5714 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5715 && subreg_lowpart_p (XEXP (x, 0))
5716 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5717 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5718 <= HOST_BITS_PER_WIDE_INT)
5719 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5720 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5721 return SUBREG_REG (XEXP (x, 0));
5725 /* If we reach here, we want to return a pair of shifts. The inner
5726 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5727 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5728 logical depending on the value of UNSIGNEDP.
5730 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5731 converted into an AND of a shift.
5733 We must check for the case where the left shift would have a negative
5734 count. This can happen in a case like (x >> 31) & 255 on machines
5735 that can't shift by a constant. On those machines, we would first
5736 combine the shift with the AND to produce a variable-position
5737 extraction. Then the constant of 31 would be substituted in to produce
5738 a such a position. */
5740 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5741 if (modewidth + len >= pos)
5742 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5743 GET_MODE (x),
5744 simplify_shift_const (NULL_RTX, ASHIFT,
5745 GET_MODE (x),
5746 XEXP (x, 0),
5747 modewidth - pos - len),
5748 modewidth - len);
5750 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5751 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5752 simplify_shift_const (NULL_RTX, LSHIFTRT,
5753 GET_MODE (x),
5754 XEXP (x, 0), pos),
5755 ((HOST_WIDE_INT) 1 << len) - 1);
5756 else
5757 /* Any other cases we can't handle. */
5758 return x;
5760 /* If we couldn't do this for some reason, return the original
5761 expression. */
5762 if (GET_CODE (tem) == CLOBBER)
5763 return x;
5765 return tem;
5768 /* X is a SET which contains an assignment of one object into
5769 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5770 or certain SUBREGS). If possible, convert it into a series of
5771 logical operations.
5773 We half-heartedly support variable positions, but do not at all
5774 support variable lengths. */
5776 static rtx
5777 expand_field_assignment (x)
5778 rtx x;
5780 rtx inner;
5781 rtx pos; /* Always counts from low bit. */
5782 int len;
5783 rtx mask;
5784 enum machine_mode compute_mode;
5786 /* Loop until we find something we can't simplify. */
5787 while (1)
5789 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5790 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5792 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5793 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5794 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5796 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5797 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5799 inner = XEXP (SET_DEST (x), 0);
5800 len = INTVAL (XEXP (SET_DEST (x), 1));
5801 pos = XEXP (SET_DEST (x), 2);
5803 /* If the position is constant and spans the width of INNER,
5804 surround INNER with a USE to indicate this. */
5805 if (GET_CODE (pos) == CONST_INT
5806 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5807 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5809 if (BITS_BIG_ENDIAN)
5811 if (GET_CODE (pos) == CONST_INT)
5812 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5813 - INTVAL (pos));
5814 else if (GET_CODE (pos) == MINUS
5815 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5816 && (INTVAL (XEXP (pos, 1))
5817 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5818 /* If position is ADJUST - X, new position is X. */
5819 pos = XEXP (pos, 0);
5820 else
5821 pos = gen_binary (MINUS, GET_MODE (pos),
5822 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5823 - len),
5824 pos);
5828 /* A SUBREG between two modes that occupy the same numbers of words
5829 can be done by moving the SUBREG to the source. */
5830 else if (GET_CODE (SET_DEST (x)) == SUBREG
5831 /* We need SUBREGs to compute nonzero_bits properly. */
5832 && nonzero_sign_valid
5833 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5834 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5835 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5836 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5838 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5839 gen_lowpart_for_combine
5840 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5841 SET_SRC (x)));
5842 continue;
5844 else
5845 break;
5847 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5848 inner = SUBREG_REG (inner);
5850 compute_mode = GET_MODE (inner);
5852 /* Don't attempt bitwise arithmetic on non-integral modes. */
5853 if (! INTEGRAL_MODE_P (compute_mode))
5855 enum machine_mode imode;
5857 /* Something is probably seriously wrong if this matches. */
5858 if (! FLOAT_MODE_P (compute_mode))
5859 break;
5861 /* Try to find an integral mode to pun with. */
5862 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5863 if (imode == BLKmode)
5864 break;
5866 compute_mode = imode;
5867 inner = gen_lowpart_for_combine (imode, inner);
5870 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5871 if (len < HOST_BITS_PER_WIDE_INT)
5872 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5873 else
5874 break;
5876 /* Now compute the equivalent expression. Make a copy of INNER
5877 for the SET_DEST in case it is a MEM into which we will substitute;
5878 we don't want shared RTL in that case. */
5879 x = gen_rtx_SET
5880 (VOIDmode, copy_rtx (inner),
5881 gen_binary (IOR, compute_mode,
5882 gen_binary (AND, compute_mode,
5883 gen_unary (NOT, compute_mode,
5884 compute_mode,
5885 gen_binary (ASHIFT,
5886 compute_mode,
5887 mask, pos)),
5888 inner),
5889 gen_binary (ASHIFT, compute_mode,
5890 gen_binary (AND, compute_mode,
5891 gen_lowpart_for_combine
5892 (compute_mode, SET_SRC (x)),
5893 mask),
5894 pos)));
5897 return x;
5900 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5901 it is an RTX that represents a variable starting position; otherwise,
5902 POS is the (constant) starting bit position (counted from the LSB).
5904 INNER may be a USE. This will occur when we started with a bitfield
5905 that went outside the boundary of the object in memory, which is
5906 allowed on most machines. To isolate this case, we produce a USE
5907 whose mode is wide enough and surround the MEM with it. The only
5908 code that understands the USE is this routine. If it is not removed,
5909 it will cause the resulting insn not to match.
5911 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5912 signed reference.
5914 IN_DEST is non-zero if this is a reference in the destination of a
5915 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5916 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5917 be used.
5919 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5920 ZERO_EXTRACT should be built even for bits starting at bit 0.
5922 MODE is the desired mode of the result (if IN_DEST == 0).
5924 The result is an RTX for the extraction or NULL_RTX if the target
5925 can't handle it. */
5927 static rtx
5928 make_extraction (mode, inner, pos, pos_rtx, len,
5929 unsignedp, in_dest, in_compare)
5930 enum machine_mode mode;
5931 rtx inner;
5932 HOST_WIDE_INT pos;
5933 rtx pos_rtx;
5934 unsigned HOST_WIDE_INT len;
5935 int unsignedp;
5936 int in_dest, in_compare;
5938 /* This mode describes the size of the storage area
5939 to fetch the overall value from. Within that, we
5940 ignore the POS lowest bits, etc. */
5941 enum machine_mode is_mode = GET_MODE (inner);
5942 enum machine_mode inner_mode;
5943 enum machine_mode wanted_inner_mode = byte_mode;
5944 enum machine_mode wanted_inner_reg_mode = word_mode;
5945 enum machine_mode pos_mode = word_mode;
5946 enum machine_mode extraction_mode = word_mode;
5947 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5948 int spans_byte = 0;
5949 rtx new = 0;
5950 rtx orig_pos_rtx = pos_rtx;
5951 HOST_WIDE_INT orig_pos;
5953 /* Get some information about INNER and get the innermost object. */
5954 if (GET_CODE (inner) == USE)
5955 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5956 /* We don't need to adjust the position because we set up the USE
5957 to pretend that it was a full-word object. */
5958 spans_byte = 1, inner = XEXP (inner, 0);
5959 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5961 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5962 consider just the QI as the memory to extract from.
5963 The subreg adds or removes high bits; its mode is
5964 irrelevant to the meaning of this extraction,
5965 since POS and LEN count from the lsb. */
5966 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5967 is_mode = GET_MODE (SUBREG_REG (inner));
5968 inner = SUBREG_REG (inner);
5971 inner_mode = GET_MODE (inner);
5973 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5974 pos = INTVAL (pos_rtx), pos_rtx = 0;
5976 /* See if this can be done without an extraction. We never can if the
5977 width of the field is not the same as that of some integer mode. For
5978 registers, we can only avoid the extraction if the position is at the
5979 low-order bit and this is either not in the destination or we have the
5980 appropriate STRICT_LOW_PART operation available.
5982 For MEM, we can avoid an extract if the field starts on an appropriate
5983 boundary and we can change the mode of the memory reference. However,
5984 we cannot directly access the MEM if we have a USE and the underlying
5985 MEM is not TMODE. This combination means that MEM was being used in a
5986 context where bits outside its mode were being referenced; that is only
5987 valid in bit-field insns. */
5989 if (tmode != BLKmode
5990 && ! (spans_byte && inner_mode != tmode)
5991 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5992 && GET_CODE (inner) != MEM
5993 && (! in_dest
5994 || (GET_CODE (inner) == REG
5995 && (movstrict_optab->handlers[(int) tmode].insn_code
5996 != CODE_FOR_nothing))))
5997 || (GET_CODE (inner) == MEM && pos_rtx == 0
5998 && (pos
5999 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6000 : BITS_PER_UNIT)) == 0
6001 /* We can't do this if we are widening INNER_MODE (it
6002 may not be aligned, for one thing). */
6003 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6004 && (inner_mode == tmode
6005 || (! mode_dependent_address_p (XEXP (inner, 0))
6006 && ! MEM_VOLATILE_P (inner))))))
6008 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6009 field. If the original and current mode are the same, we need not
6010 adjust the offset. Otherwise, we do if bytes big endian.
6012 If INNER is not a MEM, get a piece consisting of just the field
6013 of interest (in this case POS % BITS_PER_WORD must be 0). */
6015 if (GET_CODE (inner) == MEM)
6017 int offset;
6018 /* POS counts from lsb, but make OFFSET count in memory order. */
6019 if (BYTES_BIG_ENDIAN)
6020 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6021 else
6022 offset = pos / BITS_PER_UNIT;
6024 new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
6025 MEM_COPY_ATTRIBUTES (new, inner);
6027 else if (GET_CODE (inner) == REG)
6029 /* We can't call gen_lowpart_for_combine here since we always want
6030 a SUBREG and it would sometimes return a new hard register. */
6031 if (tmode != inner_mode)
6032 new = gen_rtx_SUBREG (tmode, inner,
6033 (WORDS_BIG_ENDIAN
6034 && (GET_MODE_SIZE (inner_mode)
6035 > UNITS_PER_WORD)
6036 ? (((GET_MODE_SIZE (inner_mode)
6037 - GET_MODE_SIZE (tmode))
6038 / UNITS_PER_WORD)
6039 - pos / BITS_PER_WORD)
6040 : pos / BITS_PER_WORD));
6041 else
6042 new = inner;
6044 else
6045 new = force_to_mode (inner, tmode,
6046 len >= HOST_BITS_PER_WIDE_INT
6047 ? ~(unsigned HOST_WIDE_INT) 0
6048 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6049 NULL_RTX, 0);
6051 /* If this extraction is going into the destination of a SET,
6052 make a STRICT_LOW_PART unless we made a MEM. */
6054 if (in_dest)
6055 return (GET_CODE (new) == MEM ? new
6056 : (GET_CODE (new) != SUBREG
6057 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6058 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
6060 if (mode == tmode)
6061 return new;
6063 /* If we know that no extraneous bits are set, and that the high
6064 bit is not set, convert the extraction to the cheaper of
6065 sign and zero extension, that are equivalent in these cases. */
6066 if (flag_expensive_optimizations
6067 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6068 && ((nonzero_bits (new, tmode)
6069 & ~(((unsigned HOST_WIDE_INT)
6070 GET_MODE_MASK (tmode))
6071 >> 1))
6072 == 0)))
6074 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6075 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6077 /* Prefer ZERO_EXTENSION, since it gives more information to
6078 backends. */
6079 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6080 return temp;
6081 return temp1;
6084 /* Otherwise, sign- or zero-extend unless we already are in the
6085 proper mode. */
6087 return (gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6088 mode, new));
6091 /* Unless this is a COMPARE or we have a funny memory reference,
6092 don't do anything with zero-extending field extracts starting at
6093 the low-order bit since they are simple AND operations. */
6094 if (pos_rtx == 0 && pos == 0 && ! in_dest
6095 && ! in_compare && ! spans_byte && unsignedp)
6096 return 0;
6098 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6099 we would be spanning bytes or if the position is not a constant and the
6100 length is not 1. In all other cases, we would only be going outside
6101 our object in cases when an original shift would have been
6102 undefined. */
6103 if (! spans_byte && GET_CODE (inner) == MEM
6104 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6105 || (pos_rtx != 0 && len != 1)))
6106 return 0;
6108 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6109 and the mode for the result. */
6110 #ifdef HAVE_insv
6111 if (in_dest)
6113 wanted_inner_reg_mode
6114 = insn_data[(int) CODE_FOR_insv].operand[0].mode;
6115 if (wanted_inner_reg_mode == VOIDmode)
6116 wanted_inner_reg_mode = word_mode;
6118 pos_mode = insn_data[(int) CODE_FOR_insv].operand[2].mode;
6119 if (pos_mode == VOIDmode)
6120 pos_mode = word_mode;
6122 extraction_mode = insn_data[(int) CODE_FOR_insv].operand[3].mode;
6123 if (extraction_mode == VOIDmode)
6124 extraction_mode = word_mode;
6126 #endif
6128 #ifdef HAVE_extzv
6129 if (! in_dest && unsignedp)
6131 wanted_inner_reg_mode
6132 = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
6133 if (wanted_inner_reg_mode == VOIDmode)
6134 wanted_inner_reg_mode = word_mode;
6136 pos_mode = insn_data[(int) CODE_FOR_extzv].operand[3].mode;
6137 if (pos_mode == VOIDmode)
6138 pos_mode = word_mode;
6140 extraction_mode = insn_data[(int) CODE_FOR_extzv].operand[0].mode;
6141 if (extraction_mode == VOIDmode)
6142 extraction_mode = word_mode;
6144 #endif
6146 #ifdef HAVE_extv
6147 if (! in_dest && ! unsignedp)
6149 wanted_inner_reg_mode
6150 = insn_data[(int) CODE_FOR_extv].operand[1].mode;
6151 if (wanted_inner_reg_mode == VOIDmode)
6152 wanted_inner_reg_mode = word_mode;
6154 pos_mode = insn_data[(int) CODE_FOR_extv].operand[3].mode;
6155 if (pos_mode == VOIDmode)
6156 pos_mode = word_mode;
6158 extraction_mode = insn_data[(int) CODE_FOR_extv].operand[0].mode;
6159 if (extraction_mode == VOIDmode)
6160 extraction_mode = word_mode;
6162 #endif
6164 /* Never narrow an object, since that might not be safe. */
6166 if (mode != VOIDmode
6167 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6168 extraction_mode = mode;
6170 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6171 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6172 pos_mode = GET_MODE (pos_rtx);
6174 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6175 if we have to change the mode of memory and cannot, the desired mode is
6176 EXTRACTION_MODE. */
6177 if (GET_CODE (inner) != MEM)
6178 wanted_inner_mode = wanted_inner_reg_mode;
6179 else if (inner_mode != wanted_inner_mode
6180 && (mode_dependent_address_p (XEXP (inner, 0))
6181 || MEM_VOLATILE_P (inner)))
6182 wanted_inner_mode = extraction_mode;
6184 orig_pos = pos;
6186 if (BITS_BIG_ENDIAN)
6188 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6189 BITS_BIG_ENDIAN style. If position is constant, compute new
6190 position. Otherwise, build subtraction.
6191 Note that POS is relative to the mode of the original argument.
6192 If it's a MEM we need to recompute POS relative to that.
6193 However, if we're extracting from (or inserting into) a register,
6194 we want to recompute POS relative to wanted_inner_mode. */
6195 int width = (GET_CODE (inner) == MEM
6196 ? GET_MODE_BITSIZE (is_mode)
6197 : GET_MODE_BITSIZE (wanted_inner_mode));
6199 if (pos_rtx == 0)
6200 pos = width - len - pos;
6201 else
6202 pos_rtx
6203 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
6204 GEN_INT (width - len), pos_rtx);
6205 /* POS may be less than 0 now, but we check for that below.
6206 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
6209 /* If INNER has a wider mode, make it smaller. If this is a constant
6210 extract, try to adjust the byte to point to the byte containing
6211 the value. */
6212 if (wanted_inner_mode != VOIDmode
6213 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6214 && ((GET_CODE (inner) == MEM
6215 && (inner_mode == wanted_inner_mode
6216 || (! mode_dependent_address_p (XEXP (inner, 0))
6217 && ! MEM_VOLATILE_P (inner))))))
6219 int offset = 0;
6221 /* The computations below will be correct if the machine is big
6222 endian in both bits and bytes or little endian in bits and bytes.
6223 If it is mixed, we must adjust. */
6225 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6226 adjust OFFSET to compensate. */
6227 if (BYTES_BIG_ENDIAN
6228 && ! spans_byte
6229 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6230 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6232 /* If this is a constant position, we can move to the desired byte. */
6233 if (pos_rtx == 0)
6235 offset += pos / BITS_PER_UNIT;
6236 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6239 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6240 && ! spans_byte
6241 && is_mode != wanted_inner_mode)
6242 offset = (GET_MODE_SIZE (is_mode)
6243 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6245 if (offset != 0 || inner_mode != wanted_inner_mode)
6247 rtx newmem = gen_rtx_MEM (wanted_inner_mode,
6248 plus_constant (XEXP (inner, 0), offset));
6250 MEM_COPY_ATTRIBUTES (newmem, inner);
6251 inner = newmem;
6255 /* If INNER is not memory, we can always get it into the proper mode. If we
6256 are changing its mode, POS must be a constant and smaller than the size
6257 of the new mode. */
6258 else if (GET_CODE (inner) != MEM)
6260 if (GET_MODE (inner) != wanted_inner_mode
6261 && (pos_rtx != 0
6262 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6263 return 0;
6265 inner = force_to_mode (inner, wanted_inner_mode,
6266 pos_rtx
6267 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6268 ? ~(unsigned HOST_WIDE_INT) 0
6269 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6270 << orig_pos),
6271 NULL_RTX, 0);
6274 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6275 have to zero extend. Otherwise, we can just use a SUBREG. */
6276 if (pos_rtx != 0
6277 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6279 rtx temp = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
6281 /* If we know that no extraneous bits are set, and that the high
6282 bit is not set, convert extraction to cheaper one - eighter
6283 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6284 cases. */
6285 if (flag_expensive_optimizations
6286 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6287 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6288 & ~(((unsigned HOST_WIDE_INT)
6289 GET_MODE_MASK (GET_MODE (pos_rtx)))
6290 >> 1))
6291 == 0)))
6293 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6295 /* Prefer ZERO_EXTENSION, since it gives more information to
6296 backends. */
6297 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6298 temp = temp1;
6300 pos_rtx = temp;
6302 else if (pos_rtx != 0
6303 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6304 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6306 /* Make POS_RTX unless we already have it and it is correct. If we don't
6307 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6308 be a CONST_INT. */
6309 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6310 pos_rtx = orig_pos_rtx;
6312 else if (pos_rtx == 0)
6313 pos_rtx = GEN_INT (pos);
6315 /* Make the required operation. See if we can use existing rtx. */
6316 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6317 extraction_mode, inner, GEN_INT (len), pos_rtx);
6318 if (! in_dest)
6319 new = gen_lowpart_for_combine (mode, new);
6321 return new;
6324 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6325 with any other operations in X. Return X without that shift if so. */
6327 static rtx
6328 extract_left_shift (x, count)
6329 rtx x;
6330 int count;
6332 enum rtx_code code = GET_CODE (x);
6333 enum machine_mode mode = GET_MODE (x);
6334 rtx tem;
6336 switch (code)
6338 case ASHIFT:
6339 /* This is the shift itself. If it is wide enough, we will return
6340 either the value being shifted if the shift count is equal to
6341 COUNT or a shift for the difference. */
6342 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6343 && INTVAL (XEXP (x, 1)) >= count)
6344 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6345 INTVAL (XEXP (x, 1)) - count);
6346 break;
6348 case NEG: case NOT:
6349 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6350 return gen_unary (code, mode, mode, tem);
6352 break;
6354 case PLUS: case IOR: case XOR: case AND:
6355 /* If we can safely shift this constant and we find the inner shift,
6356 make a new operation. */
6357 if (GET_CODE (XEXP (x,1)) == CONST_INT
6358 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6359 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6360 return gen_binary (code, mode, tem,
6361 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6363 break;
6365 default:
6366 break;
6369 return 0;
6372 /* Look at the expression rooted at X. Look for expressions
6373 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6374 Form these expressions.
6376 Return the new rtx, usually just X.
6378 Also, for machines like the Vax that don't have logical shift insns,
6379 try to convert logical to arithmetic shift operations in cases where
6380 they are equivalent. This undoes the canonicalizations to logical
6381 shifts done elsewhere.
6383 We try, as much as possible, to re-use rtl expressions to save memory.
6385 IN_CODE says what kind of expression we are processing. Normally, it is
6386 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6387 being kludges), it is MEM. When processing the arguments of a comparison
6388 or a COMPARE against zero, it is COMPARE. */
6390 static rtx
6391 make_compound_operation (x, in_code)
6392 rtx x;
6393 enum rtx_code in_code;
6395 enum rtx_code code = GET_CODE (x);
6396 enum machine_mode mode = GET_MODE (x);
6397 int mode_width = GET_MODE_BITSIZE (mode);
6398 rtx rhs, lhs;
6399 enum rtx_code next_code;
6400 int i;
6401 rtx new = 0;
6402 rtx tem;
6403 const char *fmt;
6405 /* Select the code to be used in recursive calls. Once we are inside an
6406 address, we stay there. If we have a comparison, set to COMPARE,
6407 but once inside, go back to our default of SET. */
6409 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6410 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6411 && XEXP (x, 1) == const0_rtx) ? COMPARE
6412 : in_code == COMPARE ? SET : in_code);
6414 /* Process depending on the code of this operation. If NEW is set
6415 non-zero, it will be returned. */
6417 switch (code)
6419 case ASHIFT:
6420 /* Convert shifts by constants into multiplications if inside
6421 an address. */
6422 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6423 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6424 && INTVAL (XEXP (x, 1)) >= 0)
6426 new = make_compound_operation (XEXP (x, 0), next_code);
6427 new = gen_rtx_combine (MULT, mode, new,
6428 GEN_INT ((HOST_WIDE_INT) 1
6429 << INTVAL (XEXP (x, 1))));
6431 break;
6433 case AND:
6434 /* If the second operand is not a constant, we can't do anything
6435 with it. */
6436 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6437 break;
6439 /* If the constant is a power of two minus one and the first operand
6440 is a logical right shift, make an extraction. */
6441 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6442 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6444 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6445 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6446 0, in_code == COMPARE);
6449 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6450 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6451 && subreg_lowpart_p (XEXP (x, 0))
6452 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6453 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6455 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6456 next_code);
6457 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6458 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6459 0, in_code == COMPARE);
6461 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6462 else if ((GET_CODE (XEXP (x, 0)) == XOR
6463 || GET_CODE (XEXP (x, 0)) == IOR)
6464 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6465 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6466 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6468 /* Apply the distributive law, and then try to make extractions. */
6469 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
6470 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6471 XEXP (x, 1)),
6472 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6473 XEXP (x, 1)));
6474 new = make_compound_operation (new, in_code);
6477 /* If we are have (and (rotate X C) M) and C is larger than the number
6478 of bits in M, this is an extraction. */
6480 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6481 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6482 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6483 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6485 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6486 new = make_extraction (mode, new,
6487 (GET_MODE_BITSIZE (mode)
6488 - INTVAL (XEXP (XEXP (x, 0), 1))),
6489 NULL_RTX, i, 1, 0, in_code == COMPARE);
6492 /* On machines without logical shifts, if the operand of the AND is
6493 a logical shift and our mask turns off all the propagated sign
6494 bits, we can replace the logical shift with an arithmetic shift. */
6495 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6496 && (lshr_optab->handlers[(int) mode].insn_code
6497 == CODE_FOR_nothing)
6498 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6499 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6500 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6501 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6502 && mode_width <= HOST_BITS_PER_WIDE_INT)
6504 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6506 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6507 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6508 SUBST (XEXP (x, 0),
6509 gen_rtx_combine (ASHIFTRT, mode,
6510 make_compound_operation (XEXP (XEXP (x, 0), 0),
6511 next_code),
6512 XEXP (XEXP (x, 0), 1)));
6515 /* If the constant is one less than a power of two, this might be
6516 representable by an extraction even if no shift is present.
6517 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6518 we are in a COMPARE. */
6519 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6520 new = make_extraction (mode,
6521 make_compound_operation (XEXP (x, 0),
6522 next_code),
6523 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6525 /* If we are in a comparison and this is an AND with a power of two,
6526 convert this into the appropriate bit extract. */
6527 else if (in_code == COMPARE
6528 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6529 new = make_extraction (mode,
6530 make_compound_operation (XEXP (x, 0),
6531 next_code),
6532 i, NULL_RTX, 1, 1, 0, 1);
6534 break;
6536 case LSHIFTRT:
6537 /* If the sign bit is known to be zero, replace this with an
6538 arithmetic shift. */
6539 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
6540 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6541 && mode_width <= HOST_BITS_PER_WIDE_INT
6542 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6544 new = gen_rtx_combine (ASHIFTRT, mode,
6545 make_compound_operation (XEXP (x, 0),
6546 next_code),
6547 XEXP (x, 1));
6548 break;
6551 /* ... fall through ... */
6553 case ASHIFTRT:
6554 lhs = XEXP (x, 0);
6555 rhs = XEXP (x, 1);
6557 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6558 this is a SIGN_EXTRACT. */
6559 if (GET_CODE (rhs) == CONST_INT
6560 && GET_CODE (lhs) == ASHIFT
6561 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6562 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6564 new = make_compound_operation (XEXP (lhs, 0), next_code);
6565 new = make_extraction (mode, new,
6566 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6567 NULL_RTX, mode_width - INTVAL (rhs),
6568 code == LSHIFTRT, 0, in_code == COMPARE);
6569 break;
6572 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6573 If so, try to merge the shifts into a SIGN_EXTEND. We could
6574 also do this for some cases of SIGN_EXTRACT, but it doesn't
6575 seem worth the effort; the case checked for occurs on Alpha. */
6577 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6578 && ! (GET_CODE (lhs) == SUBREG
6579 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6580 && GET_CODE (rhs) == CONST_INT
6581 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6582 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6583 new = make_extraction (mode, make_compound_operation (new, next_code),
6584 0, NULL_RTX, mode_width - INTVAL (rhs),
6585 code == LSHIFTRT, 0, in_code == COMPARE);
6587 break;
6589 case SUBREG:
6590 /* Call ourselves recursively on the inner expression. If we are
6591 narrowing the object and it has a different RTL code from
6592 what it originally did, do this SUBREG as a force_to_mode. */
6594 tem = make_compound_operation (SUBREG_REG (x), in_code);
6595 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6596 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6597 && subreg_lowpart_p (x))
6599 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6600 NULL_RTX, 0);
6602 /* If we have something other than a SUBREG, we might have
6603 done an expansion, so rerun outselves. */
6604 if (GET_CODE (newer) != SUBREG)
6605 newer = make_compound_operation (newer, in_code);
6607 return newer;
6610 /* If this is a paradoxical subreg, and the new code is a sign or
6611 zero extension, omit the subreg and widen the extension. If it
6612 is a regular subreg, we can still get rid of the subreg by not
6613 widening so much, or in fact removing the extension entirely. */
6614 if ((GET_CODE (tem) == SIGN_EXTEND
6615 || GET_CODE (tem) == ZERO_EXTEND)
6616 && subreg_lowpart_p (x))
6618 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6619 || (GET_MODE_SIZE (mode) >
6620 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6621 tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6622 else
6623 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6624 return tem;
6626 break;
6628 default:
6629 break;
6632 if (new)
6634 x = gen_lowpart_for_combine (mode, new);
6635 code = GET_CODE (x);
6638 /* Now recursively process each operand of this operation. */
6639 fmt = GET_RTX_FORMAT (code);
6640 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6641 if (fmt[i] == 'e')
6643 new = make_compound_operation (XEXP (x, i), next_code);
6644 SUBST (XEXP (x, i), new);
6647 return x;
6650 /* Given M see if it is a value that would select a field of bits
6651 within an item, but not the entire word. Return -1 if not.
6652 Otherwise, return the starting position of the field, where 0 is the
6653 low-order bit.
6655 *PLEN is set to the length of the field. */
6657 static int
6658 get_pos_from_mask (m, plen)
6659 unsigned HOST_WIDE_INT m;
6660 unsigned HOST_WIDE_INT *plen;
6662 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6663 int pos = exact_log2 (m & -m);
6664 int len;
6666 if (pos < 0)
6667 return -1;
6669 /* Now shift off the low-order zero bits and see if we have a power of
6670 two minus 1. */
6671 len = exact_log2 ((m >> pos) + 1);
6673 if (len <= 0)
6674 return -1;
6676 *plen = len;
6677 return pos;
6680 /* See if X can be simplified knowing that we will only refer to it in
6681 MODE and will only refer to those bits that are nonzero in MASK.
6682 If other bits are being computed or if masking operations are done
6683 that select a superset of the bits in MASK, they can sometimes be
6684 ignored.
6686 Return a possibly simplified expression, but always convert X to
6687 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6689 Also, if REG is non-zero and X is a register equal in value to REG,
6690 replace X with REG.
6692 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6693 are all off in X. This is used when X will be complemented, by either
6694 NOT, NEG, or XOR. */
6696 static rtx
6697 force_to_mode (x, mode, mask, reg, just_select)
6698 rtx x;
6699 enum machine_mode mode;
6700 unsigned HOST_WIDE_INT mask;
6701 rtx reg;
6702 int just_select;
6704 enum rtx_code code = GET_CODE (x);
6705 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6706 enum machine_mode op_mode;
6707 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6708 rtx op0, op1, temp;
6710 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6711 code below will do the wrong thing since the mode of such an
6712 expression is VOIDmode.
6714 Also do nothing if X is a CLOBBER; this can happen if X was
6715 the return value from a call to gen_lowpart_for_combine. */
6716 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6717 return x;
6719 /* We want to perform the operation is its present mode unless we know
6720 that the operation is valid in MODE, in which case we do the operation
6721 in MODE. */
6722 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6723 && code_to_optab[(int) code] != 0
6724 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6725 != CODE_FOR_nothing))
6726 ? mode : GET_MODE (x));
6728 /* It is not valid to do a right-shift in a narrower mode
6729 than the one it came in with. */
6730 if ((code == LSHIFTRT || code == ASHIFTRT)
6731 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6732 op_mode = GET_MODE (x);
6734 /* Truncate MASK to fit OP_MODE. */
6735 if (op_mode)
6736 mask &= GET_MODE_MASK (op_mode);
6738 /* When we have an arithmetic operation, or a shift whose count we
6739 do not know, we need to assume that all bit the up to the highest-order
6740 bit in MASK will be needed. This is how we form such a mask. */
6741 if (op_mode)
6742 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6743 ? GET_MODE_MASK (op_mode)
6744 : (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6745 - 1));
6746 else
6747 fuller_mask = ~(HOST_WIDE_INT) 0;
6749 /* Determine what bits of X are guaranteed to be (non)zero. */
6750 nonzero = nonzero_bits (x, mode);
6752 /* If none of the bits in X are needed, return a zero. */
6753 if (! just_select && (nonzero & mask) == 0)
6754 return const0_rtx;
6756 /* If X is a CONST_INT, return a new one. Do this here since the
6757 test below will fail. */
6758 if (GET_CODE (x) == CONST_INT)
6760 HOST_WIDE_INT cval = INTVAL (x) & mask;
6761 int width = GET_MODE_BITSIZE (mode);
6763 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6764 number, sign extend it. */
6765 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6766 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6767 cval |= (HOST_WIDE_INT) -1 << width;
6769 return GEN_INT (cval);
6772 /* If X is narrower than MODE and we want all the bits in X's mode, just
6773 get X in the proper mode. */
6774 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6775 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6776 return gen_lowpart_for_combine (mode, x);
6778 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6779 MASK are already known to be zero in X, we need not do anything. */
6780 if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6781 return x;
6783 switch (code)
6785 case CLOBBER:
6786 /* If X is a (clobber (const_int)), return it since we know we are
6787 generating something that won't match. */
6788 return x;
6790 case USE:
6791 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6792 spanned the boundary of the MEM. If we are now masking so it is
6793 within that boundary, we don't need the USE any more. */
6794 if (! BITS_BIG_ENDIAN
6795 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6796 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6797 break;
6799 case SIGN_EXTEND:
6800 case ZERO_EXTEND:
6801 case ZERO_EXTRACT:
6802 case SIGN_EXTRACT:
6803 x = expand_compound_operation (x);
6804 if (GET_CODE (x) != code)
6805 return force_to_mode (x, mode, mask, reg, next_select);
6806 break;
6808 case REG:
6809 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6810 || rtx_equal_p (reg, get_last_value (x))))
6811 x = reg;
6812 break;
6814 case SUBREG:
6815 if (subreg_lowpart_p (x)
6816 /* We can ignore the effect of this SUBREG if it narrows the mode or
6817 if the constant masks to zero all the bits the mode doesn't
6818 have. */
6819 && ((GET_MODE_SIZE (GET_MODE (x))
6820 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6821 || (0 == (mask
6822 & GET_MODE_MASK (GET_MODE (x))
6823 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6824 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6825 break;
6827 case AND:
6828 /* If this is an AND with a constant, convert it into an AND
6829 whose constant is the AND of that constant with MASK. If it
6830 remains an AND of MASK, delete it since it is redundant. */
6832 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6834 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6835 mask & INTVAL (XEXP (x, 1)));
6837 /* If X is still an AND, see if it is an AND with a mask that
6838 is just some low-order bits. If so, and it is MASK, we don't
6839 need it. */
6841 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6842 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == mask)
6843 x = XEXP (x, 0);
6845 /* If it remains an AND, try making another AND with the bits
6846 in the mode mask that aren't in MASK turned on. If the
6847 constant in the AND is wide enough, this might make a
6848 cheaper constant. */
6850 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6851 && GET_MODE_MASK (GET_MODE (x)) != mask
6852 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6854 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6855 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6856 int width = GET_MODE_BITSIZE (GET_MODE (x));
6857 rtx y;
6859 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6860 number, sign extend it. */
6861 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6862 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6863 cval |= (HOST_WIDE_INT) -1 << width;
6865 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6866 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6867 x = y;
6870 break;
6873 goto binop;
6875 case PLUS:
6876 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6877 low-order bits (as in an alignment operation) and FOO is already
6878 aligned to that boundary, mask C1 to that boundary as well.
6879 This may eliminate that PLUS and, later, the AND. */
6882 unsigned int width = GET_MODE_BITSIZE (mode);
6883 unsigned HOST_WIDE_INT smask = mask;
6885 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6886 number, sign extend it. */
6888 if (width < HOST_BITS_PER_WIDE_INT
6889 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6890 smask |= (HOST_WIDE_INT) -1 << width;
6892 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6893 && exact_log2 (- smask) >= 0)
6895 #ifdef STACK_BIAS
6896 if (STACK_BIAS
6897 && (XEXP (x, 0) == stack_pointer_rtx
6898 || XEXP (x, 0) == frame_pointer_rtx))
6900 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6901 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6903 sp_mask &= ~(sp_alignment - 1);
6904 if ((sp_mask & ~smask) == 0
6905 && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~smask) != 0)
6906 return force_to_mode (plus_constant (XEXP (x, 0),
6907 ((INTVAL (XEXP (x, 1)) -
6908 STACK_BIAS) & smask)
6909 + STACK_BIAS),
6910 mode, smask, reg, next_select);
6912 #endif
6913 if ((nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6914 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6915 return force_to_mode (plus_constant (XEXP (x, 0),
6916 (INTVAL (XEXP (x, 1))
6917 & smask)),
6918 mode, smask, reg, next_select);
6922 /* ... fall through ... */
6924 case MULT:
6925 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6926 most significant bit in MASK since carries from those bits will
6927 affect the bits we are interested in. */
6928 mask = fuller_mask;
6929 goto binop;
6931 case MINUS:
6932 /* If X is (minus C Y) where C's least set bit is larger than any bit
6933 in the mask, then we may replace with (neg Y). */
6934 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6935 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6936 & -INTVAL (XEXP (x, 0))))
6937 > mask))
6939 x = gen_unary (NEG, GET_MODE (x), GET_MODE (x), XEXP (x, 1));
6940 return force_to_mode (x, mode, mask, reg, next_select);
6943 /* Similarly, if C contains every bit in the mask, then we may
6944 replace with (not Y). */
6945 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6946 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) mask)
6947 == INTVAL (XEXP (x, 0))))
6949 x = gen_unary (NOT, GET_MODE (x), GET_MODE (x), XEXP (x, 1));
6950 return force_to_mode (x, mode, mask, reg, next_select);
6953 mask = fuller_mask;
6954 goto binop;
6956 case IOR:
6957 case XOR:
6958 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6959 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6960 operation which may be a bitfield extraction. Ensure that the
6961 constant we form is not wider than the mode of X. */
6963 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6964 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6965 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6966 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6967 && GET_CODE (XEXP (x, 1)) == CONST_INT
6968 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6969 + floor_log2 (INTVAL (XEXP (x, 1))))
6970 < GET_MODE_BITSIZE (GET_MODE (x)))
6971 && (INTVAL (XEXP (x, 1))
6972 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6974 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6975 << INTVAL (XEXP (XEXP (x, 0), 1)));
6976 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6977 XEXP (XEXP (x, 0), 0), temp);
6978 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6979 XEXP (XEXP (x, 0), 1));
6980 return force_to_mode (x, mode, mask, reg, next_select);
6983 binop:
6984 /* For most binary operations, just propagate into the operation and
6985 change the mode if we have an operation of that mode. */
6987 op0 = gen_lowpart_for_combine (op_mode,
6988 force_to_mode (XEXP (x, 0), mode, mask,
6989 reg, next_select));
6990 op1 = gen_lowpart_for_combine (op_mode,
6991 force_to_mode (XEXP (x, 1), mode, mask,
6992 reg, next_select));
6994 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6995 MASK since OP1 might have been sign-extended but we never want
6996 to turn on extra bits, since combine might have previously relied
6997 on them being off. */
6998 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6999 && (INTVAL (op1) & mask) != 0)
7000 op1 = GEN_INT (INTVAL (op1) & mask);
7002 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7003 x = gen_binary (code, op_mode, op0, op1);
7004 break;
7006 case ASHIFT:
7007 /* For left shifts, do the same, but just for the first operand.
7008 However, we cannot do anything with shifts where we cannot
7009 guarantee that the counts are smaller than the size of the mode
7010 because such a count will have a different meaning in a
7011 wider mode. */
7013 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7014 && INTVAL (XEXP (x, 1)) >= 0
7015 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7016 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7017 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7018 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7019 break;
7021 /* If the shift count is a constant and we can do arithmetic in
7022 the mode of the shift, refine which bits we need. Otherwise, use the
7023 conservative form of the mask. */
7024 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7025 && INTVAL (XEXP (x, 1)) >= 0
7026 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7027 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7028 mask >>= INTVAL (XEXP (x, 1));
7029 else
7030 mask = fuller_mask;
7032 op0 = gen_lowpart_for_combine (op_mode,
7033 force_to_mode (XEXP (x, 0), op_mode,
7034 mask, reg, next_select));
7036 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7037 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7038 break;
7040 case LSHIFTRT:
7041 /* Here we can only do something if the shift count is a constant,
7042 this shift constant is valid for the host, and we can do arithmetic
7043 in OP_MODE. */
7045 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7046 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7047 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7049 rtx inner = XEXP (x, 0);
7050 unsigned HOST_WIDE_INT inner_mask;
7052 /* Select the mask of the bits we need for the shift operand. */
7053 inner_mask = mask << INTVAL (XEXP (x, 1));
7055 /* We can only change the mode of the shift if we can do arithmetic
7056 in the mode of the shift and INNER_MASK is no wider than the
7057 width of OP_MODE. */
7058 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7059 || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7060 op_mode = GET_MODE (x);
7062 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7064 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7065 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7068 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7069 shift and AND produces only copies of the sign bit (C2 is one less
7070 than a power of two), we can do this with just a shift. */
7072 if (GET_CODE (x) == LSHIFTRT
7073 && GET_CODE (XEXP (x, 1)) == CONST_INT
7074 /* The shift puts one of the sign bit copies in the least significant
7075 bit. */
7076 && ((INTVAL (XEXP (x, 1))
7077 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7078 >= GET_MODE_BITSIZE (GET_MODE (x)))
7079 && exact_log2 (mask + 1) >= 0
7080 /* Number of bits left after the shift must be more than the mask
7081 needs. */
7082 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7083 <= GET_MODE_BITSIZE (GET_MODE (x)))
7084 /* Must be more sign bit copies than the mask needs. */
7085 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7086 >= exact_log2 (mask + 1)))
7087 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7088 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7089 - exact_log2 (mask + 1)));
7091 goto shiftrt;
7093 case ASHIFTRT:
7094 /* If we are just looking for the sign bit, we don't need this shift at
7095 all, even if it has a variable count. */
7096 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7097 && (mask == ((unsigned HOST_WIDE_INT) 1
7098 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7099 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7101 /* If this is a shift by a constant, get a mask that contains those bits
7102 that are not copies of the sign bit. We then have two cases: If
7103 MASK only includes those bits, this can be a logical shift, which may
7104 allow simplifications. If MASK is a single-bit field not within
7105 those bits, we are requesting a copy of the sign bit and hence can
7106 shift the sign bit to the appropriate location. */
7108 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7109 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7111 int i = -1;
7113 /* If the considered data is wider then HOST_WIDE_INT, we can't
7114 represent a mask for all its bits in a single scalar.
7115 But we only care about the lower bits, so calculate these. */
7117 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7119 nonzero = ~(HOST_WIDE_INT) 0;
7121 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7122 is the number of bits a full-width mask would have set.
7123 We need only shift if these are fewer than nonzero can
7124 hold. If not, we must keep all bits set in nonzero. */
7126 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7127 < HOST_BITS_PER_WIDE_INT)
7128 nonzero >>= INTVAL (XEXP (x, 1))
7129 + HOST_BITS_PER_WIDE_INT
7130 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7132 else
7134 nonzero = GET_MODE_MASK (GET_MODE (x));
7135 nonzero >>= INTVAL (XEXP (x, 1));
7138 if ((mask & ~nonzero) == 0
7139 || (i = exact_log2 (mask)) >= 0)
7141 x = simplify_shift_const
7142 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7143 i < 0 ? INTVAL (XEXP (x, 1))
7144 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7146 if (GET_CODE (x) != ASHIFTRT)
7147 return force_to_mode (x, mode, mask, reg, next_select);
7151 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
7152 even if the shift count isn't a constant. */
7153 if (mask == 1)
7154 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7156 shiftrt:
7158 /* If this is a zero- or sign-extension operation that just affects bits
7159 we don't care about, remove it. Be sure the call above returned
7160 something that is still a shift. */
7162 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7163 && GET_CODE (XEXP (x, 1)) == CONST_INT
7164 && INTVAL (XEXP (x, 1)) >= 0
7165 && (INTVAL (XEXP (x, 1))
7166 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7167 && GET_CODE (XEXP (x, 0)) == ASHIFT
7168 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7169 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
7170 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7171 reg, next_select);
7173 break;
7175 case ROTATE:
7176 case ROTATERT:
7177 /* If the shift count is constant and we can do computations
7178 in the mode of X, compute where the bits we care about are.
7179 Otherwise, we can't do anything. Don't change the mode of
7180 the shift or propagate MODE into the shift, though. */
7181 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7182 && INTVAL (XEXP (x, 1)) >= 0)
7184 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7185 GET_MODE (x), GEN_INT (mask),
7186 XEXP (x, 1));
7187 if (temp && GET_CODE(temp) == CONST_INT)
7188 SUBST (XEXP (x, 0),
7189 force_to_mode (XEXP (x, 0), GET_MODE (x),
7190 INTVAL (temp), reg, next_select));
7192 break;
7194 case NEG:
7195 /* If we just want the low-order bit, the NEG isn't needed since it
7196 won't change the low-order bit. */
7197 if (mask == 1)
7198 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7200 /* We need any bits less significant than the most significant bit in
7201 MASK since carries from those bits will affect the bits we are
7202 interested in. */
7203 mask = fuller_mask;
7204 goto unop;
7206 case NOT:
7207 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7208 same as the XOR case above. Ensure that the constant we form is not
7209 wider than the mode of X. */
7211 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7212 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7213 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7214 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7215 < GET_MODE_BITSIZE (GET_MODE (x)))
7216 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7218 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
7219 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7220 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7222 return force_to_mode (x, mode, mask, reg, next_select);
7225 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7226 use the full mask inside the NOT. */
7227 mask = fuller_mask;
7229 unop:
7230 op0 = gen_lowpart_for_combine (op_mode,
7231 force_to_mode (XEXP (x, 0), mode, mask,
7232 reg, next_select));
7233 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7234 x = gen_unary (code, op_mode, op_mode, op0);
7235 break;
7237 case NE:
7238 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7239 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7240 which is equal to STORE_FLAG_VALUE. */
7241 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7242 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7243 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
7244 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7246 break;
7248 case IF_THEN_ELSE:
7249 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7250 written in a narrower mode. We play it safe and do not do so. */
7252 SUBST (XEXP (x, 1),
7253 gen_lowpart_for_combine (GET_MODE (x),
7254 force_to_mode (XEXP (x, 1), mode,
7255 mask, reg, next_select)));
7256 SUBST (XEXP (x, 2),
7257 gen_lowpart_for_combine (GET_MODE (x),
7258 force_to_mode (XEXP (x, 2), mode,
7259 mask, reg,next_select)));
7260 break;
7262 default:
7263 break;
7266 /* Ensure we return a value of the proper mode. */
7267 return gen_lowpart_for_combine (mode, x);
7270 /* Return nonzero if X is an expression that has one of two values depending on
7271 whether some other value is zero or nonzero. In that case, we return the
7272 value that is being tested, *PTRUE is set to the value if the rtx being
7273 returned has a nonzero value, and *PFALSE is set to the other alternative.
7275 If we return zero, we set *PTRUE and *PFALSE to X. */
7277 static rtx
7278 if_then_else_cond (x, ptrue, pfalse)
7279 rtx x;
7280 rtx *ptrue, *pfalse;
7282 enum machine_mode mode = GET_MODE (x);
7283 enum rtx_code code = GET_CODE (x);
7284 rtx cond0, cond1, true0, true1, false0, false1;
7285 unsigned HOST_WIDE_INT nz;
7287 /* If we are comparing a value against zero, we are done. */
7288 if ((code == NE || code == EQ)
7289 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
7291 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7292 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7293 return XEXP (x, 0);
7296 /* If this is a unary operation whose operand has one of two values, apply
7297 our opcode to compute those values. */
7298 else if (GET_RTX_CLASS (code) == '1'
7299 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7301 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
7302 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
7303 return cond0;
7306 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7307 make can't possibly match and would suppress other optimizations. */
7308 else if (code == COMPARE)
7311 /* If this is a binary operation, see if either side has only one of two
7312 values. If either one does or if both do and they are conditional on
7313 the same value, compute the new true and false values. */
7314 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7315 || GET_RTX_CLASS (code) == '<')
7317 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7318 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7320 if ((cond0 != 0 || cond1 != 0)
7321 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7323 /* If if_then_else_cond returned zero, then true/false are the
7324 same rtl. We must copy one of them to prevent invalid rtl
7325 sharing. */
7326 if (cond0 == 0)
7327 true0 = copy_rtx (true0);
7328 else if (cond1 == 0)
7329 true1 = copy_rtx (true1);
7331 *ptrue = gen_binary (code, mode, true0, true1);
7332 *pfalse = gen_binary (code, mode, false0, false1);
7333 return cond0 ? cond0 : cond1;
7336 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7337 operands is zero when the other is non-zero, and vice-versa,
7338 and STORE_FLAG_VALUE is 1 or -1. */
7340 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7341 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7342 || code == UMAX)
7343 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7345 rtx op0 = XEXP (XEXP (x, 0), 1);
7346 rtx op1 = XEXP (XEXP (x, 1), 1);
7348 cond0 = XEXP (XEXP (x, 0), 0);
7349 cond1 = XEXP (XEXP (x, 1), 0);
7351 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7352 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7353 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7354 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7355 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7356 || ((swap_condition (GET_CODE (cond0))
7357 == combine_reversed_comparison_code (cond1))
7358 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7359 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7360 && ! side_effects_p (x))
7362 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7363 *pfalse = gen_binary (MULT, mode,
7364 (code == MINUS
7365 ? gen_unary (NEG, mode, mode, op1) : op1),
7366 const_true_rtx);
7367 return cond0;
7371 /* Similarly for MULT, AND and UMIN, execpt that for these the result
7372 is always zero. */
7373 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7374 && (code == MULT || code == AND || code == UMIN)
7375 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7377 cond0 = XEXP (XEXP (x, 0), 0);
7378 cond1 = XEXP (XEXP (x, 1), 0);
7380 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7381 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7382 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7383 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7384 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7385 || ((swap_condition (GET_CODE (cond0))
7386 == combine_reversed_comparison_code (cond1))
7387 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7388 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7389 && ! side_effects_p (x))
7391 *ptrue = *pfalse = const0_rtx;
7392 return cond0;
7397 else if (code == IF_THEN_ELSE)
7399 /* If we have IF_THEN_ELSE already, extract the condition and
7400 canonicalize it if it is NE or EQ. */
7401 cond0 = XEXP (x, 0);
7402 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7403 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7404 return XEXP (cond0, 0);
7405 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7407 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7408 return XEXP (cond0, 0);
7410 else
7411 return cond0;
7414 /* If X is a normal SUBREG with both inner and outer modes integral,
7415 we can narrow both the true and false values of the inner expression,
7416 if there is a condition. */
7417 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
7418 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
7419 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
7420 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7421 &true0, &false0)))
7423 if ((GET_CODE (SUBREG_REG (x)) == REG
7424 || GET_CODE (SUBREG_REG (x)) == MEM
7425 || CONSTANT_P (SUBREG_REG (x)))
7426 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD
7427 && (WORDS_BIG_ENDIAN || SUBREG_WORD (x) != 0))
7429 true0 = operand_subword (true0, SUBREG_WORD (x), 0,
7430 GET_MODE (SUBREG_REG (x)));
7431 false0 = operand_subword (false0, SUBREG_WORD (x), 0,
7432 GET_MODE (SUBREG_REG (x)));
7434 *ptrue = force_to_mode (true0, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
7435 *pfalse
7436 = force_to_mode (false0, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
7438 return cond0;
7441 /* If X is a constant, this isn't special and will cause confusions
7442 if we treat it as such. Likewise if it is equivalent to a constant. */
7443 else if (CONSTANT_P (x)
7444 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7447 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7448 will be least confusing to the rest of the compiler. */
7449 else if (mode == BImode)
7451 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7452 return x;
7455 /* If X is known to be either 0 or -1, those are the true and
7456 false values when testing X. */
7457 else if (x == constm1_rtx || x == const0_rtx
7458 || (mode != VOIDmode
7459 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7461 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7462 return x;
7465 /* Likewise for 0 or a single bit. */
7466 else if (mode != VOIDmode
7467 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7468 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7470 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
7471 return x;
7474 /* Otherwise fail; show no condition with true and false values the same. */
7475 *ptrue = *pfalse = x;
7476 return 0;
7479 /* Return the value of expression X given the fact that condition COND
7480 is known to be true when applied to REG as its first operand and VAL
7481 as its second. X is known to not be shared and so can be modified in
7482 place.
7484 We only handle the simplest cases, and specifically those cases that
7485 arise with IF_THEN_ELSE expressions. */
7487 static rtx
7488 known_cond (x, cond, reg, val)
7489 rtx x;
7490 enum rtx_code cond;
7491 rtx reg, val;
7493 enum rtx_code code = GET_CODE (x);
7494 rtx temp;
7495 const char *fmt;
7496 int i, j;
7498 if (side_effects_p (x))
7499 return x;
7501 if (cond == EQ && rtx_equal_p (x, reg) && !FLOAT_MODE_P (cond))
7502 return val;
7503 if (cond == UNEQ && rtx_equal_p (x, reg))
7504 return val;
7506 /* If X is (abs REG) and we know something about REG's relationship
7507 with zero, we may be able to simplify this. */
7509 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7510 switch (cond)
7512 case GE: case GT: case EQ:
7513 return XEXP (x, 0);
7514 case LT: case LE:
7515 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
7516 XEXP (x, 0));
7517 default:
7518 break;
7521 /* The only other cases we handle are MIN, MAX, and comparisons if the
7522 operands are the same as REG and VAL. */
7524 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7526 if (rtx_equal_p (XEXP (x, 0), val))
7527 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7529 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7531 if (GET_RTX_CLASS (code) == '<')
7533 if (comparison_dominates_p (cond, code))
7534 return const_true_rtx;
7536 code = combine_reversed_comparison_code (x);
7537 if (code != UNKNOWN
7538 && comparison_dominates_p (cond, code))
7539 return const0_rtx;
7540 else
7541 return x;
7543 else if (code == SMAX || code == SMIN
7544 || code == UMIN || code == UMAX)
7546 int unsignedp = (code == UMIN || code == UMAX);
7548 if (code == SMAX || code == UMAX)
7549 cond = reverse_condition (cond);
7551 switch (cond)
7553 case GE: case GT:
7554 return unsignedp ? x : XEXP (x, 1);
7555 case LE: case LT:
7556 return unsignedp ? x : XEXP (x, 0);
7557 case GEU: case GTU:
7558 return unsignedp ? XEXP (x, 1) : x;
7559 case LEU: case LTU:
7560 return unsignedp ? XEXP (x, 0) : x;
7561 default:
7562 break;
7568 fmt = GET_RTX_FORMAT (code);
7569 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7571 if (fmt[i] == 'e')
7572 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7573 else if (fmt[i] == 'E')
7574 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7575 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7576 cond, reg, val));
7579 return x;
7582 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7583 assignment as a field assignment. */
7585 static int
7586 rtx_equal_for_field_assignment_p (x, y)
7587 rtx x;
7588 rtx y;
7590 if (x == y || rtx_equal_p (x, y))
7591 return 1;
7593 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7594 return 0;
7596 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7597 Note that all SUBREGs of MEM are paradoxical; otherwise they
7598 would have been rewritten. */
7599 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7600 && GET_CODE (SUBREG_REG (y)) == MEM
7601 && rtx_equal_p (SUBREG_REG (y),
7602 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7603 return 1;
7605 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7606 && GET_CODE (SUBREG_REG (x)) == MEM
7607 && rtx_equal_p (SUBREG_REG (x),
7608 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7609 return 1;
7611 /* We used to see if get_last_value of X and Y were the same but that's
7612 not correct. In one direction, we'll cause the assignment to have
7613 the wrong destination and in the case, we'll import a register into this
7614 insn that might have already have been dead. So fail if none of the
7615 above cases are true. */
7616 return 0;
7619 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7620 Return that assignment if so.
7622 We only handle the most common cases. */
7624 static rtx
7625 make_field_assignment (x)
7626 rtx x;
7628 rtx dest = SET_DEST (x);
7629 rtx src = SET_SRC (x);
7630 rtx assign;
7631 rtx rhs, lhs;
7632 HOST_WIDE_INT c1;
7633 HOST_WIDE_INT pos;
7634 unsigned HOST_WIDE_INT len;
7635 rtx other;
7636 enum machine_mode mode;
7638 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7639 a clear of a one-bit field. We will have changed it to
7640 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7641 for a SUBREG. */
7643 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7644 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7645 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7646 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7648 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7649 1, 1, 1, 0);
7650 if (assign != 0)
7651 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7652 return x;
7655 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7656 && subreg_lowpart_p (XEXP (src, 0))
7657 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7658 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7659 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7660 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7661 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7663 assign = make_extraction (VOIDmode, dest, 0,
7664 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7665 1, 1, 1, 0);
7666 if (assign != 0)
7667 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7668 return x;
7671 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7672 one-bit field. */
7673 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7674 && XEXP (XEXP (src, 0), 0) == const1_rtx
7675 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7677 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7678 1, 1, 1, 0);
7679 if (assign != 0)
7680 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7681 return x;
7684 /* The other case we handle is assignments into a constant-position
7685 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7686 a mask that has all one bits except for a group of zero bits and
7687 OTHER is known to have zeros where C1 has ones, this is such an
7688 assignment. Compute the position and length from C1. Shift OTHER
7689 to the appropriate position, force it to the required mode, and
7690 make the extraction. Check for the AND in both operands. */
7692 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7693 return x;
7695 rhs = expand_compound_operation (XEXP (src, 0));
7696 lhs = expand_compound_operation (XEXP (src, 1));
7698 if (GET_CODE (rhs) == AND
7699 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7700 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7701 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7702 else if (GET_CODE (lhs) == AND
7703 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7704 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7705 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7706 else
7707 return x;
7709 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7710 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7711 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7712 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7713 return x;
7715 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7716 if (assign == 0)
7717 return x;
7719 /* The mode to use for the source is the mode of the assignment, or of
7720 what is inside a possible STRICT_LOW_PART. */
7721 mode = (GET_CODE (assign) == STRICT_LOW_PART
7722 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7724 /* Shift OTHER right POS places and make it the source, restricting it
7725 to the proper length and mode. */
7727 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7728 GET_MODE (src), other, pos),
7729 mode,
7730 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7731 ? ~(unsigned HOST_WIDE_INT) 0
7732 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7733 dest, 0);
7735 return gen_rtx_combine (SET, VOIDmode, assign, src);
7738 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7739 if so. */
7741 static rtx
7742 apply_distributive_law (x)
7743 rtx x;
7745 enum rtx_code code = GET_CODE (x);
7746 rtx lhs, rhs, other;
7747 rtx tem;
7748 enum rtx_code inner_code;
7750 /* Distributivity is not true for floating point.
7751 It can change the value. So don't do it.
7752 -- rms and moshier@world.std.com. */
7753 if (FLOAT_MODE_P (GET_MODE (x)))
7754 return x;
7756 /* The outer operation can only be one of the following: */
7757 if (code != IOR && code != AND && code != XOR
7758 && code != PLUS && code != MINUS)
7759 return x;
7761 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7763 /* If either operand is a primitive we can't do anything, so get out
7764 fast. */
7765 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7766 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7767 return x;
7769 lhs = expand_compound_operation (lhs);
7770 rhs = expand_compound_operation (rhs);
7771 inner_code = GET_CODE (lhs);
7772 if (inner_code != GET_CODE (rhs))
7773 return x;
7775 /* See if the inner and outer operations distribute. */
7776 switch (inner_code)
7778 case LSHIFTRT:
7779 case ASHIFTRT:
7780 case AND:
7781 case IOR:
7782 /* These all distribute except over PLUS. */
7783 if (code == PLUS || code == MINUS)
7784 return x;
7785 break;
7787 case MULT:
7788 if (code != PLUS && code != MINUS)
7789 return x;
7790 break;
7792 case ASHIFT:
7793 /* This is also a multiply, so it distributes over everything. */
7794 break;
7796 case SUBREG:
7797 /* Non-paradoxical SUBREGs distributes over all operations, provided
7798 the inner modes and word numbers are the same, this is an extraction
7799 of a low-order part, we don't convert an fp operation to int or
7800 vice versa, and we would not be converting a single-word
7801 operation into a multi-word operation. The latter test is not
7802 required, but it prevents generating unneeded multi-word operations.
7803 Some of the previous tests are redundant given the latter test, but
7804 are retained because they are required for correctness.
7806 We produce the result slightly differently in this case. */
7808 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7809 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7810 || ! subreg_lowpart_p (lhs)
7811 || (GET_MODE_CLASS (GET_MODE (lhs))
7812 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7813 || (GET_MODE_SIZE (GET_MODE (lhs))
7814 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7815 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7816 return x;
7818 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7819 SUBREG_REG (lhs), SUBREG_REG (rhs));
7820 return gen_lowpart_for_combine (GET_MODE (x), tem);
7822 default:
7823 return x;
7826 /* Set LHS and RHS to the inner operands (A and B in the example
7827 above) and set OTHER to the common operand (C in the example).
7828 These is only one way to do this unless the inner operation is
7829 commutative. */
7830 if (GET_RTX_CLASS (inner_code) == 'c'
7831 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7832 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7833 else if (GET_RTX_CLASS (inner_code) == 'c'
7834 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7835 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7836 else if (GET_RTX_CLASS (inner_code) == 'c'
7837 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7838 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7839 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7840 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7841 else
7842 return x;
7844 /* Form the new inner operation, seeing if it simplifies first. */
7845 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7847 /* There is one exception to the general way of distributing:
7848 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7849 if (code == XOR && inner_code == IOR)
7851 inner_code = AND;
7852 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7855 /* We may be able to continuing distributing the result, so call
7856 ourselves recursively on the inner operation before forming the
7857 outer operation, which we return. */
7858 return gen_binary (inner_code, GET_MODE (x),
7859 apply_distributive_law (tem), other);
7862 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7863 in MODE.
7865 Return an equivalent form, if different from X. Otherwise, return X. If
7866 X is zero, we are to always construct the equivalent form. */
7868 static rtx
7869 simplify_and_const_int (x, mode, varop, constop)
7870 rtx x;
7871 enum machine_mode mode;
7872 rtx varop;
7873 unsigned HOST_WIDE_INT constop;
7875 unsigned HOST_WIDE_INT nonzero;
7876 int i;
7878 /* Simplify VAROP knowing that we will be only looking at some of the
7879 bits in it. */
7880 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7882 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7883 CONST_INT, we are done. */
7884 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7885 return varop;
7887 /* See what bits may be nonzero in VAROP. Unlike the general case of
7888 a call to nonzero_bits, here we don't care about bits outside
7889 MODE. */
7891 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7892 nonzero = trunc_int_for_mode (nonzero, mode);
7894 /* Turn off all bits in the constant that are known to already be zero.
7895 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7896 which is tested below. */
7898 constop &= nonzero;
7900 /* If we don't have any bits left, return zero. */
7901 if (constop == 0)
7902 return const0_rtx;
7904 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7905 a power of two, we can replace this with a ASHIFT. */
7906 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7907 && (i = exact_log2 (constop)) >= 0)
7908 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7910 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7911 or XOR, then try to apply the distributive law. This may eliminate
7912 operations if either branch can be simplified because of the AND.
7913 It may also make some cases more complex, but those cases probably
7914 won't match a pattern either with or without this. */
7916 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7917 return
7918 gen_lowpart_for_combine
7919 (mode,
7920 apply_distributive_law
7921 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7922 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7923 XEXP (varop, 0), constop),
7924 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7925 XEXP (varop, 1), constop))));
7927 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7928 if we already had one (just check for the simplest cases). */
7929 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7930 && GET_MODE (XEXP (x, 0)) == mode
7931 && SUBREG_REG (XEXP (x, 0)) == varop)
7932 varop = XEXP (x, 0);
7933 else
7934 varop = gen_lowpart_for_combine (mode, varop);
7936 /* If we can't make the SUBREG, try to return what we were given. */
7937 if (GET_CODE (varop) == CLOBBER)
7938 return x ? x : varop;
7940 /* If we are only masking insignificant bits, return VAROP. */
7941 if (constop == nonzero)
7942 x = varop;
7944 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7945 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7946 x = gen_binary (AND, mode, varop, GEN_INT (constop));
7948 else
7950 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7951 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
7952 SUBST (XEXP (x, 1), GEN_INT (constop));
7954 SUBST (XEXP (x, 0), varop);
7957 return x;
7960 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7961 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7962 is less useful. We can't allow both, because that results in exponential
7963 run time recursion. There is a nullstone testcase that triggered
7964 this. This macro avoids accidental uses of num_sign_bit_copies. */
7965 #define num_sign_bit_copies()
7967 /* Given an expression, X, compute which bits in X can be non-zero.
7968 We don't care about bits outside of those defined in MODE.
7970 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7971 a shift, AND, or zero_extract, we can do better. */
7973 static unsigned HOST_WIDE_INT
7974 nonzero_bits (x, mode)
7975 rtx x;
7976 enum machine_mode mode;
7978 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7979 unsigned HOST_WIDE_INT inner_nz;
7980 enum rtx_code code;
7981 unsigned int mode_width = GET_MODE_BITSIZE (mode);
7982 rtx tem;
7984 /* For floating-point values, assume all bits are needed. */
7985 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7986 return nonzero;
7988 /* If X is wider than MODE, use its mode instead. */
7989 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7991 mode = GET_MODE (x);
7992 nonzero = GET_MODE_MASK (mode);
7993 mode_width = GET_MODE_BITSIZE (mode);
7996 if (mode_width > HOST_BITS_PER_WIDE_INT)
7997 /* Our only callers in this case look for single bit values. So
7998 just return the mode mask. Those tests will then be false. */
7999 return nonzero;
8001 #ifndef WORD_REGISTER_OPERATIONS
8002 /* If MODE is wider than X, but both are a single word for both the host
8003 and target machines, we can compute this from which bits of the
8004 object might be nonzero in its own mode, taking into account the fact
8005 that on many CISC machines, accessing an object in a wider mode
8006 causes the high-order bits to become undefined. So they are
8007 not known to be zero. */
8009 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8010 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8011 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8012 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8014 nonzero &= nonzero_bits (x, GET_MODE (x));
8015 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8016 return nonzero;
8018 #endif
8020 code = GET_CODE (x);
8021 switch (code)
8023 case REG:
8024 #ifdef POINTERS_EXTEND_UNSIGNED
8025 /* If pointers extend unsigned and this is a pointer in Pmode, say that
8026 all the bits above ptr_mode are known to be zero. */
8027 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8028 && REG_POINTER (x))
8029 nonzero &= GET_MODE_MASK (ptr_mode);
8030 #endif
8032 #ifdef STACK_BOUNDARY
8033 /* If this is the stack pointer, we may know something about its
8034 alignment. If PUSH_ROUNDING is defined, it is possible for the
8035 stack to be momentarily aligned only to that amount, so we pick
8036 the least alignment. */
8038 /* We can't check for arg_pointer_rtx here, because it is not
8039 guaranteed to have as much alignment as the stack pointer.
8040 In particular, in the Irix6 n64 ABI, the stack has 128 bit
8041 alignment but the argument pointer has only 64 bit alignment. */
8043 if ((x == frame_pointer_rtx
8044 || x == stack_pointer_rtx
8045 || x == hard_frame_pointer_rtx
8046 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
8047 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
8048 #ifdef STACK_BIAS
8049 && !STACK_BIAS
8050 #endif
8053 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
8055 #ifdef PUSH_ROUNDING
8056 if (REGNO (x) == STACK_POINTER_REGNUM && PUSH_ARGS)
8057 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
8058 #endif
8060 /* We must return here, otherwise we may get a worse result from
8061 one of the choices below. There is nothing useful below as
8062 far as the stack pointer is concerned. */
8063 return nonzero &= ~(sp_alignment - 1);
8065 #endif
8067 /* If X is a register whose nonzero bits value is current, use it.
8068 Otherwise, if X is a register whose value we can find, use that
8069 value. Otherwise, use the previously-computed global nonzero bits
8070 for this register. */
8072 if (reg_last_set_value[REGNO (x)] != 0
8073 && reg_last_set_mode[REGNO (x)] == mode
8074 && (reg_last_set_label[REGNO (x)] == label_tick
8075 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8076 && REG_N_SETS (REGNO (x)) == 1
8077 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
8078 REGNO (x))))
8079 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8080 return reg_last_set_nonzero_bits[REGNO (x)];
8082 tem = get_last_value (x);
8084 if (tem)
8086 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8087 /* If X is narrower than MODE and TEM is a non-negative
8088 constant that would appear negative in the mode of X,
8089 sign-extend it for use in reg_nonzero_bits because some
8090 machines (maybe most) will actually do the sign-extension
8091 and this is the conservative approach.
8093 ??? For 2.5, try to tighten up the MD files in this regard
8094 instead of this kludge. */
8096 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8097 && GET_CODE (tem) == CONST_INT
8098 && INTVAL (tem) > 0
8099 && 0 != (INTVAL (tem)
8100 & ((HOST_WIDE_INT) 1
8101 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8102 tem = GEN_INT (INTVAL (tem)
8103 | ((HOST_WIDE_INT) (-1)
8104 << GET_MODE_BITSIZE (GET_MODE (x))));
8105 #endif
8106 return nonzero_bits (tem, mode);
8108 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8109 return reg_nonzero_bits[REGNO (x)] & nonzero;
8110 else
8111 return nonzero;
8113 case CONST_INT:
8114 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8115 /* If X is negative in MODE, sign-extend the value. */
8116 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8117 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8118 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8119 #endif
8121 return INTVAL (x);
8123 case MEM:
8124 #ifdef LOAD_EXTEND_OP
8125 /* In many, if not most, RISC machines, reading a byte from memory
8126 zeros the rest of the register. Noticing that fact saves a lot
8127 of extra zero-extends. */
8128 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8129 nonzero &= GET_MODE_MASK (GET_MODE (x));
8130 #endif
8131 break;
8133 case EQ: case NE:
8134 case UNEQ: case LTGT:
8135 case GT: case GTU: case UNGT:
8136 case LT: case LTU: case UNLT:
8137 case GE: case GEU: case UNGE:
8138 case LE: case LEU: case UNLE:
8139 case UNORDERED: case ORDERED:
8141 /* If this produces an integer result, we know which bits are set.
8142 Code here used to clear bits outside the mode of X, but that is
8143 now done above. */
8145 if (GET_MODE_CLASS (mode) == MODE_INT
8146 && mode_width <= HOST_BITS_PER_WIDE_INT)
8147 nonzero = STORE_FLAG_VALUE;
8148 break;
8150 case NEG:
8151 #if 0
8152 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8153 and num_sign_bit_copies. */
8154 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8155 == GET_MODE_BITSIZE (GET_MODE (x)))
8156 nonzero = 1;
8157 #endif
8159 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8160 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8161 break;
8163 case ABS:
8164 #if 0
8165 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8166 and num_sign_bit_copies. */
8167 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8168 == GET_MODE_BITSIZE (GET_MODE (x)))
8169 nonzero = 1;
8170 #endif
8171 break;
8173 case TRUNCATE:
8174 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
8175 break;
8177 case ZERO_EXTEND:
8178 nonzero &= nonzero_bits (XEXP (x, 0), mode);
8179 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8180 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8181 break;
8183 case SIGN_EXTEND:
8184 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8185 Otherwise, show all the bits in the outer mode but not the inner
8186 may be non-zero. */
8187 inner_nz = nonzero_bits (XEXP (x, 0), mode);
8188 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8190 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8191 if (inner_nz
8192 & (((HOST_WIDE_INT) 1
8193 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8194 inner_nz |= (GET_MODE_MASK (mode)
8195 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8198 nonzero &= inner_nz;
8199 break;
8201 case AND:
8202 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
8203 & nonzero_bits (XEXP (x, 1), mode));
8204 break;
8206 case XOR: case IOR:
8207 case UMIN: case UMAX: case SMIN: case SMAX:
8208 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
8209 | nonzero_bits (XEXP (x, 1), mode));
8210 break;
8212 case PLUS: case MINUS:
8213 case MULT:
8214 case DIV: case UDIV:
8215 case MOD: case UMOD:
8216 /* We can apply the rules of arithmetic to compute the number of
8217 high- and low-order zero bits of these operations. We start by
8218 computing the width (position of the highest-order non-zero bit)
8219 and the number of low-order zero bits for each value. */
8221 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
8222 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
8223 int width0 = floor_log2 (nz0) + 1;
8224 int width1 = floor_log2 (nz1) + 1;
8225 int low0 = floor_log2 (nz0 & -nz0);
8226 int low1 = floor_log2 (nz1 & -nz1);
8227 HOST_WIDE_INT op0_maybe_minusp
8228 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
8229 HOST_WIDE_INT op1_maybe_minusp
8230 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
8231 unsigned int result_width = mode_width;
8232 int result_low = 0;
8234 switch (code)
8236 case PLUS:
8237 #ifdef STACK_BIAS
8238 if (STACK_BIAS
8239 && (XEXP (x, 0) == stack_pointer_rtx
8240 || XEXP (x, 0) == frame_pointer_rtx)
8241 && GET_CODE (XEXP (x, 1)) == CONST_INT)
8243 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
8245 nz0 = (GET_MODE_MASK (mode) & ~(sp_alignment - 1));
8246 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
8247 width0 = floor_log2 (nz0) + 1;
8248 width1 = floor_log2 (nz1) + 1;
8249 low0 = floor_log2 (nz0 & -nz0);
8250 low1 = floor_log2 (nz1 & -nz1);
8252 #endif
8253 result_width = MAX (width0, width1) + 1;
8254 result_low = MIN (low0, low1);
8255 break;
8256 case MINUS:
8257 result_low = MIN (low0, low1);
8258 break;
8259 case MULT:
8260 result_width = width0 + width1;
8261 result_low = low0 + low1;
8262 break;
8263 case DIV:
8264 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8265 result_width = width0;
8266 break;
8267 case UDIV:
8268 result_width = width0;
8269 break;
8270 case MOD:
8271 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8272 result_width = MIN (width0, width1);
8273 result_low = MIN (low0, low1);
8274 break;
8275 case UMOD:
8276 result_width = MIN (width0, width1);
8277 result_low = MIN (low0, low1);
8278 break;
8279 default:
8280 abort ();
8283 if (result_width < mode_width)
8284 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8286 if (result_low > 0)
8287 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8289 break;
8291 case ZERO_EXTRACT:
8292 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8293 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8294 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8295 break;
8297 case SUBREG:
8298 /* If this is a SUBREG formed for a promoted variable that has
8299 been zero-extended, we know that at least the high-order bits
8300 are zero, though others might be too. */
8302 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
8303 nonzero = (GET_MODE_MASK (GET_MODE (x))
8304 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
8306 /* If the inner mode is a single word for both the host and target
8307 machines, we can compute this from which bits of the inner
8308 object might be nonzero. */
8309 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8310 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8311 <= HOST_BITS_PER_WIDE_INT))
8313 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
8315 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8316 /* If this is a typical RISC machine, we only have to worry
8317 about the way loads are extended. */
8318 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8319 ? (((nonzero
8320 & (((unsigned HOST_WIDE_INT) 1
8321 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8322 != 0))
8323 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8324 #endif
8326 /* On many CISC machines, accessing an object in a wider mode
8327 causes the high-order bits to become undefined. So they are
8328 not known to be zero. */
8329 if (GET_MODE_SIZE (GET_MODE (x))
8330 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8331 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8332 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8335 break;
8337 case ASHIFTRT:
8338 case LSHIFTRT:
8339 case ASHIFT:
8340 case ROTATE:
8341 /* The nonzero bits are in two classes: any bits within MODE
8342 that aren't in GET_MODE (x) are always significant. The rest of the
8343 nonzero bits are those that are significant in the operand of
8344 the shift when shifted the appropriate number of bits. This
8345 shows that high-order bits are cleared by the right shift and
8346 low-order bits by left shifts. */
8347 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8348 && INTVAL (XEXP (x, 1)) >= 0
8349 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8351 enum machine_mode inner_mode = GET_MODE (x);
8352 unsigned int width = GET_MODE_BITSIZE (inner_mode);
8353 int count = INTVAL (XEXP (x, 1));
8354 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8355 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
8356 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8357 unsigned HOST_WIDE_INT outer = 0;
8359 if (mode_width > width)
8360 outer = (op_nonzero & nonzero & ~mode_mask);
8362 if (code == LSHIFTRT)
8363 inner >>= count;
8364 else if (code == ASHIFTRT)
8366 inner >>= count;
8368 /* If the sign bit may have been nonzero before the shift, we
8369 need to mark all the places it could have been copied to
8370 by the shift as possibly nonzero. */
8371 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8372 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8374 else if (code == ASHIFT)
8375 inner <<= count;
8376 else
8377 inner = ((inner << (count % width)
8378 | (inner >> (width - (count % width)))) & mode_mask);
8380 nonzero &= (outer | inner);
8382 break;
8384 case FFS:
8385 /* This is at most the number of bits in the mode. */
8386 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
8387 break;
8389 case IF_THEN_ELSE:
8390 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
8391 | nonzero_bits (XEXP (x, 2), mode));
8392 break;
8394 default:
8395 break;
8398 return nonzero;
8401 /* See the macro definition above. */
8402 #undef num_sign_bit_copies
8404 /* Return the number of bits at the high-order end of X that are known to
8405 be equal to the sign bit. X will be used in mode MODE; if MODE is
8406 VOIDmode, X will be used in its own mode. The returned value will always
8407 be between 1 and the number of bits in MODE. */
8409 static unsigned int
8410 num_sign_bit_copies (x, mode)
8411 rtx x;
8412 enum machine_mode mode;
8414 enum rtx_code code = GET_CODE (x);
8415 unsigned int bitwidth;
8416 int num0, num1, result;
8417 unsigned HOST_WIDE_INT nonzero;
8418 rtx tem;
8420 /* If we weren't given a mode, use the mode of X. If the mode is still
8421 VOIDmode, we don't know anything. Likewise if one of the modes is
8422 floating-point. */
8424 if (mode == VOIDmode)
8425 mode = GET_MODE (x);
8427 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8428 return 1;
8430 bitwidth = GET_MODE_BITSIZE (mode);
8432 /* For a smaller object, just ignore the high bits. */
8433 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8435 num0 = num_sign_bit_copies (x, GET_MODE (x));
8436 return MAX (1,
8437 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8440 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8442 #ifndef WORD_REGISTER_OPERATIONS
8443 /* If this machine does not do all register operations on the entire
8444 register and MODE is wider than the mode of X, we can say nothing
8445 at all about the high-order bits. */
8446 return 1;
8447 #else
8448 /* Likewise on machines that do, if the mode of the object is smaller
8449 than a word and loads of that size don't sign extend, we can say
8450 nothing about the high order bits. */
8451 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8452 #ifdef LOAD_EXTEND_OP
8453 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8454 #endif
8456 return 1;
8457 #endif
8460 switch (code)
8462 case REG:
8464 #ifdef POINTERS_EXTEND_UNSIGNED
8465 /* If pointers extend signed and this is a pointer in Pmode, say that
8466 all the bits above ptr_mode are known to be sign bit copies. */
8467 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8468 && REG_POINTER (x))
8469 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8470 #endif
8472 if (reg_last_set_value[REGNO (x)] != 0
8473 && reg_last_set_mode[REGNO (x)] == mode
8474 && (reg_last_set_label[REGNO (x)] == label_tick
8475 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8476 && REG_N_SETS (REGNO (x)) == 1
8477 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
8478 REGNO (x))))
8479 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8480 return reg_last_set_sign_bit_copies[REGNO (x)];
8482 tem = get_last_value (x);
8483 if (tem != 0)
8484 return num_sign_bit_copies (tem, mode);
8486 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
8487 return reg_sign_bit_copies[REGNO (x)];
8488 break;
8490 case MEM:
8491 #ifdef LOAD_EXTEND_OP
8492 /* Some RISC machines sign-extend all loads of smaller than a word. */
8493 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8494 return MAX (1, ((int) bitwidth
8495 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8496 #endif
8497 break;
8499 case CONST_INT:
8500 /* If the constant is negative, take its 1's complement and remask.
8501 Then see how many zero bits we have. */
8502 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8503 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8504 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8505 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8507 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8509 case SUBREG:
8510 /* If this is a SUBREG for a promoted object that is sign-extended
8511 and we are looking at it in a wider mode, we know that at least the
8512 high-order bits are known to be sign bit copies. */
8514 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8516 num0 = num_sign_bit_copies (SUBREG_REG (x), mode);
8517 return MAX ((int) bitwidth
8518 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8519 num0);
8522 /* For a smaller object, just ignore the high bits. */
8523 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8525 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
8526 return MAX (1, (num0
8527 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8528 - bitwidth)));
8531 #ifdef WORD_REGISTER_OPERATIONS
8532 #ifdef LOAD_EXTEND_OP
8533 /* For paradoxical SUBREGs on machines where all register operations
8534 affect the entire register, just look inside. Note that we are
8535 passing MODE to the recursive call, so the number of sign bit copies
8536 will remain relative to that mode, not the inner mode. */
8538 /* This works only if loads sign extend. Otherwise, if we get a
8539 reload for the inner part, it may be loaded from the stack, and
8540 then we lose all sign bit copies that existed before the store
8541 to the stack. */
8543 if ((GET_MODE_SIZE (GET_MODE (x))
8544 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8545 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
8546 return num_sign_bit_copies (SUBREG_REG (x), mode);
8547 #endif
8548 #endif
8549 break;
8551 case SIGN_EXTRACT:
8552 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8553 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8554 break;
8556 case SIGN_EXTEND:
8557 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8558 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
8560 case TRUNCATE:
8561 /* For a smaller object, just ignore the high bits. */
8562 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
8563 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8564 - bitwidth)));
8566 case NOT:
8567 return num_sign_bit_copies (XEXP (x, 0), mode);
8569 case ROTATE: case ROTATERT:
8570 /* If we are rotating left by a number of bits less than the number
8571 of sign bit copies, we can just subtract that amount from the
8572 number. */
8573 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8574 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
8576 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8577 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8578 : (int) bitwidth - INTVAL (XEXP (x, 1))));
8580 break;
8582 case NEG:
8583 /* In general, this subtracts one sign bit copy. But if the value
8584 is known to be positive, the number of sign bit copies is the
8585 same as that of the input. Finally, if the input has just one bit
8586 that might be nonzero, all the bits are copies of the sign bit. */
8587 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8588 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8589 return num0 > 1 ? num0 - 1 : 1;
8591 nonzero = nonzero_bits (XEXP (x, 0), mode);
8592 if (nonzero == 1)
8593 return bitwidth;
8595 if (num0 > 1
8596 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8597 num0--;
8599 return num0;
8601 case IOR: case AND: case XOR:
8602 case SMIN: case SMAX: case UMIN: case UMAX:
8603 /* Logical operations will preserve the number of sign-bit copies.
8604 MIN and MAX operations always return one of the operands. */
8605 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8606 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8607 return MIN (num0, num1);
8609 case PLUS: case MINUS:
8610 /* For addition and subtraction, we can have a 1-bit carry. However,
8611 if we are subtracting 1 from a positive number, there will not
8612 be such a carry. Furthermore, if the positive number is known to
8613 be 0 or 1, we know the result is either -1 or 0. */
8615 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8616 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8618 nonzero = nonzero_bits (XEXP (x, 0), mode);
8619 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8620 return (nonzero == 1 || nonzero == 0 ? bitwidth
8621 : bitwidth - floor_log2 (nonzero) - 1);
8624 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8625 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8626 return MAX (1, MIN (num0, num1) - 1);
8628 case MULT:
8629 /* The number of bits of the product is the sum of the number of
8630 bits of both terms. However, unless one of the terms if known
8631 to be positive, we must allow for an additional bit since negating
8632 a negative number can remove one sign bit copy. */
8634 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8635 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8637 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8638 if (result > 0
8639 && (bitwidth > HOST_BITS_PER_WIDE_INT
8640 || (((nonzero_bits (XEXP (x, 0), mode)
8641 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8642 && ((nonzero_bits (XEXP (x, 1), mode)
8643 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8644 result--;
8646 return MAX (1, result);
8648 case UDIV:
8649 /* The result must be <= the first operand. If the first operand
8650 has the high bit set, we know nothing about the number of sign
8651 bit copies. */
8652 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8653 return 1;
8654 else if ((nonzero_bits (XEXP (x, 0), mode)
8655 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8656 return 1;
8657 else
8658 return num_sign_bit_copies (XEXP (x, 0), mode);
8660 case UMOD:
8661 /* The result must be <= the scond operand. */
8662 return num_sign_bit_copies (XEXP (x, 1), mode);
8664 case DIV:
8665 /* Similar to unsigned division, except that we have to worry about
8666 the case where the divisor is negative, in which case we have
8667 to add 1. */
8668 result = num_sign_bit_copies (XEXP (x, 0), mode);
8669 if (result > 1
8670 && (bitwidth > HOST_BITS_PER_WIDE_INT
8671 || (nonzero_bits (XEXP (x, 1), mode)
8672 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8673 result--;
8675 return result;
8677 case MOD:
8678 result = num_sign_bit_copies (XEXP (x, 1), mode);
8679 if (result > 1
8680 && (bitwidth > HOST_BITS_PER_WIDE_INT
8681 || (nonzero_bits (XEXP (x, 1), mode)
8682 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8683 result--;
8685 return result;
8687 case ASHIFTRT:
8688 /* Shifts by a constant add to the number of bits equal to the
8689 sign bit. */
8690 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8691 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8692 && INTVAL (XEXP (x, 1)) > 0)
8693 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8695 return num0;
8697 case ASHIFT:
8698 /* Left shifts destroy copies. */
8699 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8700 || INTVAL (XEXP (x, 1)) < 0
8701 || INTVAL (XEXP (x, 1)) >= bitwidth)
8702 return 1;
8704 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8705 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8707 case IF_THEN_ELSE:
8708 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8709 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8710 return MIN (num0, num1);
8712 case EQ: case NE: case GE: case GT: case LE: case LT:
8713 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
8714 case GEU: case GTU: case LEU: case LTU:
8715 case UNORDERED: case ORDERED:
8716 /* If the constant is negative, take its 1's complement and remask.
8717 Then see how many zero bits we have. */
8718 nonzero = STORE_FLAG_VALUE;
8719 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8720 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8721 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8723 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8724 break;
8726 default:
8727 break;
8730 /* If we haven't been able to figure it out by one of the above rules,
8731 see if some of the high-order bits are known to be zero. If so,
8732 count those bits and return one less than that amount. If we can't
8733 safely compute the mask for this mode, always return BITWIDTH. */
8735 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8736 return 1;
8738 nonzero = nonzero_bits (x, mode);
8739 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8740 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8743 /* Return the number of "extended" bits there are in X, when interpreted
8744 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8745 unsigned quantities, this is the number of high-order zero bits.
8746 For signed quantities, this is the number of copies of the sign bit
8747 minus 1. In both case, this function returns the number of "spare"
8748 bits. For example, if two quantities for which this function returns
8749 at least 1 are added, the addition is known not to overflow.
8751 This function will always return 0 unless called during combine, which
8752 implies that it must be called from a define_split. */
8754 unsigned int
8755 extended_count (x, mode, unsignedp)
8756 rtx x;
8757 enum machine_mode mode;
8758 int unsignedp;
8760 if (nonzero_sign_valid == 0)
8761 return 0;
8763 return (unsignedp
8764 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8765 ? (GET_MODE_BITSIZE (mode) - 1
8766 - floor_log2 (nonzero_bits (x, mode)))
8767 : 0)
8768 : num_sign_bit_copies (x, mode) - 1);
8771 /* This function is called from `simplify_shift_const' to merge two
8772 outer operations. Specifically, we have already found that we need
8773 to perform operation *POP0 with constant *PCONST0 at the outermost
8774 position. We would now like to also perform OP1 with constant CONST1
8775 (with *POP0 being done last).
8777 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8778 the resulting operation. *PCOMP_P is set to 1 if we would need to
8779 complement the innermost operand, otherwise it is unchanged.
8781 MODE is the mode in which the operation will be done. No bits outside
8782 the width of this mode matter. It is assumed that the width of this mode
8783 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8785 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8786 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8787 result is simply *PCONST0.
8789 If the resulting operation cannot be expressed as one operation, we
8790 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8792 static int
8793 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8794 enum rtx_code *pop0;
8795 HOST_WIDE_INT *pconst0;
8796 enum rtx_code op1;
8797 HOST_WIDE_INT const1;
8798 enum machine_mode mode;
8799 int *pcomp_p;
8801 enum rtx_code op0 = *pop0;
8802 HOST_WIDE_INT const0 = *pconst0;
8804 const0 &= GET_MODE_MASK (mode);
8805 const1 &= GET_MODE_MASK (mode);
8807 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8808 if (op0 == AND)
8809 const1 &= const0;
8811 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8812 if OP0 is SET. */
8814 if (op1 == NIL || op0 == SET)
8815 return 1;
8817 else if (op0 == NIL)
8818 op0 = op1, const0 = const1;
8820 else if (op0 == op1)
8822 switch (op0)
8824 case AND:
8825 const0 &= const1;
8826 break;
8827 case IOR:
8828 const0 |= const1;
8829 break;
8830 case XOR:
8831 const0 ^= const1;
8832 break;
8833 case PLUS:
8834 const0 += const1;
8835 break;
8836 case NEG:
8837 op0 = NIL;
8838 break;
8839 default:
8840 break;
8844 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8845 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8846 return 0;
8848 /* If the two constants aren't the same, we can't do anything. The
8849 remaining six cases can all be done. */
8850 else if (const0 != const1)
8851 return 0;
8853 else
8854 switch (op0)
8856 case IOR:
8857 if (op1 == AND)
8858 /* (a & b) | b == b */
8859 op0 = SET;
8860 else /* op1 == XOR */
8861 /* (a ^ b) | b == a | b */
8863 break;
8865 case XOR:
8866 if (op1 == AND)
8867 /* (a & b) ^ b == (~a) & b */
8868 op0 = AND, *pcomp_p = 1;
8869 else /* op1 == IOR */
8870 /* (a | b) ^ b == a & ~b */
8871 op0 = AND, *pconst0 = ~const0;
8872 break;
8874 case AND:
8875 if (op1 == IOR)
8876 /* (a | b) & b == b */
8877 op0 = SET;
8878 else /* op1 == XOR */
8879 /* (a ^ b) & b) == (~a) & b */
8880 *pcomp_p = 1;
8881 break;
8882 default:
8883 break;
8886 /* Check for NO-OP cases. */
8887 const0 &= GET_MODE_MASK (mode);
8888 if (const0 == 0
8889 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8890 op0 = NIL;
8891 else if (const0 == 0 && op0 == AND)
8892 op0 = SET;
8893 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8894 && op0 == AND)
8895 op0 = NIL;
8897 /* ??? Slightly redundant with the above mask, but not entirely.
8898 Moving this above means we'd have to sign-extend the mode mask
8899 for the final test. */
8900 const0 = trunc_int_for_mode (const0, mode);
8902 *pop0 = op0;
8903 *pconst0 = const0;
8905 return 1;
8908 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8909 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8910 that we started with.
8912 The shift is normally computed in the widest mode we find in VAROP, as
8913 long as it isn't a different number of words than RESULT_MODE. Exceptions
8914 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8916 static rtx
8917 simplify_shift_const (x, code, result_mode, varop, input_count)
8918 rtx x;
8919 enum rtx_code code;
8920 enum machine_mode result_mode;
8921 rtx varop;
8922 int input_count;
8924 enum rtx_code orig_code = code;
8925 int orig_count = input_count;
8926 unsigned int count;
8927 int signed_count;
8928 enum machine_mode mode = result_mode;
8929 enum machine_mode shift_mode, tmode;
8930 unsigned int mode_words
8931 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8932 /* We form (outer_op (code varop count) (outer_const)). */
8933 enum rtx_code outer_op = NIL;
8934 HOST_WIDE_INT outer_const = 0;
8935 rtx const_rtx;
8936 int complement_p = 0;
8937 rtx new;
8939 /* If we were given an invalid count, don't do anything except exactly
8940 what was requested. */
8942 if (input_count < 0 || input_count > (int) GET_MODE_BITSIZE (mode))
8944 if (x)
8945 return x;
8947 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (input_count));
8950 count = input_count;
8952 /* Make sure and truncate the "natural" shift on the way in. We don't
8953 want to do this inside the loop as it makes it more difficult to
8954 combine shifts. */
8955 #ifdef SHIFT_COUNT_TRUNCATED
8956 if (SHIFT_COUNT_TRUNCATED)
8957 count %= GET_MODE_BITSIZE (mode);
8958 #endif
8960 /* Unless one of the branches of the `if' in this loop does a `continue',
8961 we will `break' the loop after the `if'. */
8963 while (count != 0)
8965 /* If we have an operand of (clobber (const_int 0)), just return that
8966 value. */
8967 if (GET_CODE (varop) == CLOBBER)
8968 return varop;
8970 /* If we discovered we had to complement VAROP, leave. Making a NOT
8971 here would cause an infinite loop. */
8972 if (complement_p)
8973 break;
8975 /* Convert ROTATERT to ROTATE. */
8976 if (code == ROTATERT)
8977 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8979 /* We need to determine what mode we will do the shift in. If the
8980 shift is a right shift or a ROTATE, we must always do it in the mode
8981 it was originally done in. Otherwise, we can do it in MODE, the
8982 widest mode encountered. */
8983 shift_mode
8984 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8985 ? result_mode : mode);
8987 /* Handle cases where the count is greater than the size of the mode
8988 minus 1. For ASHIFT, use the size minus one as the count (this can
8989 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8990 take the count modulo the size. For other shifts, the result is
8991 zero.
8993 Since these shifts are being produced by the compiler by combining
8994 multiple operations, each of which are defined, we know what the
8995 result is supposed to be. */
8997 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8999 if (code == ASHIFTRT)
9000 count = GET_MODE_BITSIZE (shift_mode) - 1;
9001 else if (code == ROTATE || code == ROTATERT)
9002 count %= GET_MODE_BITSIZE (shift_mode);
9003 else
9005 /* We can't simply return zero because there may be an
9006 outer op. */
9007 varop = const0_rtx;
9008 count = 0;
9009 break;
9013 /* An arithmetic right shift of a quantity known to be -1 or 0
9014 is a no-op. */
9015 if (code == ASHIFTRT
9016 && (num_sign_bit_copies (varop, shift_mode)
9017 == GET_MODE_BITSIZE (shift_mode)))
9019 count = 0;
9020 break;
9023 /* If we are doing an arithmetic right shift and discarding all but
9024 the sign bit copies, this is equivalent to doing a shift by the
9025 bitsize minus one. Convert it into that shift because it will often
9026 allow other simplifications. */
9028 if (code == ASHIFTRT
9029 && (count + num_sign_bit_copies (varop, shift_mode)
9030 >= GET_MODE_BITSIZE (shift_mode)))
9031 count = GET_MODE_BITSIZE (shift_mode) - 1;
9033 /* We simplify the tests below and elsewhere by converting
9034 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9035 `make_compound_operation' will convert it to a ASHIFTRT for
9036 those machines (such as Vax) that don't have a LSHIFTRT. */
9037 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9038 && code == ASHIFTRT
9039 && ((nonzero_bits (varop, shift_mode)
9040 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9041 == 0))
9042 code = LSHIFTRT;
9044 switch (GET_CODE (varop))
9046 case SIGN_EXTEND:
9047 case ZERO_EXTEND:
9048 case SIGN_EXTRACT:
9049 case ZERO_EXTRACT:
9050 new = expand_compound_operation (varop);
9051 if (new != varop)
9053 varop = new;
9054 continue;
9056 break;
9058 case MEM:
9059 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9060 minus the width of a smaller mode, we can do this with a
9061 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9062 if ((code == ASHIFTRT || code == LSHIFTRT)
9063 && ! mode_dependent_address_p (XEXP (varop, 0))
9064 && ! MEM_VOLATILE_P (varop)
9065 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9066 MODE_INT, 1)) != BLKmode)
9068 if (BYTES_BIG_ENDIAN)
9069 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
9070 else
9071 new = gen_rtx_MEM (tmode,
9072 plus_constant (XEXP (varop, 0),
9073 count / BITS_PER_UNIT));
9075 MEM_COPY_ATTRIBUTES (new, varop);
9076 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
9077 : ZERO_EXTEND, mode, new);
9078 count = 0;
9079 continue;
9081 break;
9083 case USE:
9084 /* Similar to the case above, except that we can only do this if
9085 the resulting mode is the same as that of the underlying
9086 MEM and adjust the address depending on the *bits* endianness
9087 because of the way that bit-field extract insns are defined. */
9088 if ((code == ASHIFTRT || code == LSHIFTRT)
9089 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9090 MODE_INT, 1)) != BLKmode
9091 && tmode == GET_MODE (XEXP (varop, 0)))
9093 if (BITS_BIG_ENDIAN)
9094 new = XEXP (varop, 0);
9095 else
9097 new = copy_rtx (XEXP (varop, 0));
9098 SUBST (XEXP (new, 0),
9099 plus_constant (XEXP (new, 0),
9100 count / BITS_PER_UNIT));
9103 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
9104 : ZERO_EXTEND, mode, new);
9105 count = 0;
9106 continue;
9108 break;
9110 case SUBREG:
9111 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9112 the same number of words as what we've seen so far. Then store
9113 the widest mode in MODE. */
9114 if (subreg_lowpart_p (varop)
9115 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9116 > GET_MODE_SIZE (GET_MODE (varop)))
9117 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9118 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9119 == mode_words))
9121 varop = SUBREG_REG (varop);
9122 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9123 mode = GET_MODE (varop);
9124 continue;
9126 break;
9128 case MULT:
9129 /* Some machines use MULT instead of ASHIFT because MULT
9130 is cheaper. But it is still better on those machines to
9131 merge two shifts into one. */
9132 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9133 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9135 varop
9136 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9137 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9138 continue;
9140 break;
9142 case UDIV:
9143 /* Similar, for when divides are cheaper. */
9144 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9145 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9147 varop
9148 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9149 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9150 continue;
9152 break;
9154 case ASHIFTRT:
9155 /* If we are extracting just the sign bit of an arithmetic
9156 right shift, that shift is not needed. However, the sign
9157 bit of a wider mode may be different from what would be
9158 interpreted as the sign bit in a narrower mode, so, if
9159 the result is narrower, don't discard the shift. */
9160 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
9161 && (GET_MODE_BITSIZE (result_mode)
9162 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9164 varop = XEXP (varop, 0);
9165 continue;
9168 /* ... fall through ... */
9170 case LSHIFTRT:
9171 case ASHIFT:
9172 case ROTATE:
9173 /* Here we have two nested shifts. The result is usually the
9174 AND of a new shift with a mask. We compute the result below. */
9175 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9176 && INTVAL (XEXP (varop, 1)) >= 0
9177 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9178 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9179 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9181 enum rtx_code first_code = GET_CODE (varop);
9182 unsigned int first_count = INTVAL (XEXP (varop, 1));
9183 unsigned HOST_WIDE_INT mask;
9184 rtx mask_rtx;
9186 /* We have one common special case. We can't do any merging if
9187 the inner code is an ASHIFTRT of a smaller mode. However, if
9188 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9189 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9190 we can convert it to
9191 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9192 This simplifies certain SIGN_EXTEND operations. */
9193 if (code == ASHIFT && first_code == ASHIFTRT
9194 && (GET_MODE_BITSIZE (result_mode)
9195 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
9197 /* C3 has the low-order C1 bits zero. */
9199 mask = (GET_MODE_MASK (mode)
9200 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9202 varop = simplify_and_const_int (NULL_RTX, result_mode,
9203 XEXP (varop, 0), mask);
9204 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9205 varop, count);
9206 count = first_count;
9207 code = ASHIFTRT;
9208 continue;
9211 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9212 than C1 high-order bits equal to the sign bit, we can convert
9213 this to either an ASHIFT or a ASHIFTRT depending on the
9214 two counts.
9216 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9218 if (code == ASHIFTRT && first_code == ASHIFT
9219 && GET_MODE (varop) == shift_mode
9220 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9221 > first_count))
9223 varop = XEXP (varop, 0);
9225 signed_count = count - first_count;
9226 if (signed_count < 0)
9227 count = -signed_count, code = ASHIFT;
9228 else
9229 count = signed_count;
9231 continue;
9234 /* There are some cases we can't do. If CODE is ASHIFTRT,
9235 we can only do this if FIRST_CODE is also ASHIFTRT.
9237 We can't do the case when CODE is ROTATE and FIRST_CODE is
9238 ASHIFTRT.
9240 If the mode of this shift is not the mode of the outer shift,
9241 we can't do this if either shift is a right shift or ROTATE.
9243 Finally, we can't do any of these if the mode is too wide
9244 unless the codes are the same.
9246 Handle the case where the shift codes are the same
9247 first. */
9249 if (code == first_code)
9251 if (GET_MODE (varop) != result_mode
9252 && (code == ASHIFTRT || code == LSHIFTRT
9253 || code == ROTATE))
9254 break;
9256 count += first_count;
9257 varop = XEXP (varop, 0);
9258 continue;
9261 if (code == ASHIFTRT
9262 || (code == ROTATE && first_code == ASHIFTRT)
9263 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9264 || (GET_MODE (varop) != result_mode
9265 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9266 || first_code == ROTATE
9267 || code == ROTATE)))
9268 break;
9270 /* To compute the mask to apply after the shift, shift the
9271 nonzero bits of the inner shift the same way the
9272 outer shift will. */
9274 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9276 mask_rtx
9277 = simplify_binary_operation (code, result_mode, mask_rtx,
9278 GEN_INT (count));
9280 /* Give up if we can't compute an outer operation to use. */
9281 if (mask_rtx == 0
9282 || GET_CODE (mask_rtx) != CONST_INT
9283 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9284 INTVAL (mask_rtx),
9285 result_mode, &complement_p))
9286 break;
9288 /* If the shifts are in the same direction, we add the
9289 counts. Otherwise, we subtract them. */
9290 signed_count = count;
9291 if ((code == ASHIFTRT || code == LSHIFTRT)
9292 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9293 signed_count += first_count;
9294 else
9295 signed_count -= first_count;
9297 /* If COUNT is positive, the new shift is usually CODE,
9298 except for the two exceptions below, in which case it is
9299 FIRST_CODE. If the count is negative, FIRST_CODE should
9300 always be used */
9301 if (signed_count > 0
9302 && ((first_code == ROTATE && code == ASHIFT)
9303 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9304 code = first_code, count = signed_count;
9305 else if (signed_count < 0)
9306 code = first_code, count = -signed_count;
9307 else
9308 count = signed_count;
9310 varop = XEXP (varop, 0);
9311 continue;
9314 /* If we have (A << B << C) for any shift, we can convert this to
9315 (A << C << B). This wins if A is a constant. Only try this if
9316 B is not a constant. */
9318 else if (GET_CODE (varop) == code
9319 && GET_CODE (XEXP (varop, 1)) != CONST_INT
9320 && 0 != (new
9321 = simplify_binary_operation (code, mode,
9322 XEXP (varop, 0),
9323 GEN_INT (count))))
9325 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
9326 count = 0;
9327 continue;
9329 break;
9331 case NOT:
9332 /* Make this fit the case below. */
9333 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
9334 GEN_INT (GET_MODE_MASK (mode)));
9335 continue;
9337 case IOR:
9338 case AND:
9339 case XOR:
9340 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9341 with C the size of VAROP - 1 and the shift is logical if
9342 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9343 we have an (le X 0) operation. If we have an arithmetic shift
9344 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9345 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9347 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9348 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9349 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9350 && (code == LSHIFTRT || code == ASHIFTRT)
9351 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
9352 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9354 count = 0;
9355 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
9356 const0_rtx);
9358 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9359 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9361 continue;
9364 /* If we have (shift (logical)), move the logical to the outside
9365 to allow it to possibly combine with another logical and the
9366 shift to combine with another shift. This also canonicalizes to
9367 what a ZERO_EXTRACT looks like. Also, some machines have
9368 (and (shift)) insns. */
9370 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9371 && (new = simplify_binary_operation (code, result_mode,
9372 XEXP (varop, 1),
9373 GEN_INT (count))) != 0
9374 && GET_CODE (new) == CONST_INT
9375 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9376 INTVAL (new), result_mode, &complement_p))
9378 varop = XEXP (varop, 0);
9379 continue;
9382 /* If we can't do that, try to simplify the shift in each arm of the
9383 logical expression, make a new logical expression, and apply
9384 the inverse distributive law. */
9386 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9387 XEXP (varop, 0), count);
9388 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9389 XEXP (varop, 1), count);
9391 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9392 varop = apply_distributive_law (varop);
9394 count = 0;
9396 break;
9398 case EQ:
9399 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9400 says that the sign bit can be tested, FOO has mode MODE, C is
9401 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9402 that may be nonzero. */
9403 if (code == LSHIFTRT
9404 && XEXP (varop, 1) == const0_rtx
9405 && GET_MODE (XEXP (varop, 0)) == result_mode
9406 && count == GET_MODE_BITSIZE (result_mode) - 1
9407 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9408 && ((STORE_FLAG_VALUE
9409 & ((HOST_WIDE_INT) 1
9410 < (GET_MODE_BITSIZE (result_mode) - 1))))
9411 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9412 && merge_outer_ops (&outer_op, &outer_const, XOR,
9413 (HOST_WIDE_INT) 1, result_mode,
9414 &complement_p))
9416 varop = XEXP (varop, 0);
9417 count = 0;
9418 continue;
9420 break;
9422 case NEG:
9423 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9424 than the number of bits in the mode is equivalent to A. */
9425 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
9426 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9428 varop = XEXP (varop, 0);
9429 count = 0;
9430 continue;
9433 /* NEG commutes with ASHIFT since it is multiplication. Move the
9434 NEG outside to allow shifts to combine. */
9435 if (code == ASHIFT
9436 && merge_outer_ops (&outer_op, &outer_const, NEG,
9437 (HOST_WIDE_INT) 0, result_mode,
9438 &complement_p))
9440 varop = XEXP (varop, 0);
9441 continue;
9443 break;
9445 case PLUS:
9446 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9447 is one less than the number of bits in the mode is
9448 equivalent to (xor A 1). */
9449 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
9450 && XEXP (varop, 1) == constm1_rtx
9451 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9452 && merge_outer_ops (&outer_op, &outer_const, XOR,
9453 (HOST_WIDE_INT) 1, result_mode,
9454 &complement_p))
9456 count = 0;
9457 varop = XEXP (varop, 0);
9458 continue;
9461 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9462 that might be nonzero in BAR are those being shifted out and those
9463 bits are known zero in FOO, we can replace the PLUS with FOO.
9464 Similarly in the other operand order. This code occurs when
9465 we are computing the size of a variable-size array. */
9467 if ((code == ASHIFTRT || code == LSHIFTRT)
9468 && count < HOST_BITS_PER_WIDE_INT
9469 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9470 && (nonzero_bits (XEXP (varop, 1), result_mode)
9471 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9473 varop = XEXP (varop, 0);
9474 continue;
9476 else if ((code == ASHIFTRT || code == LSHIFTRT)
9477 && count < HOST_BITS_PER_WIDE_INT
9478 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9479 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9480 >> count)
9481 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9482 & nonzero_bits (XEXP (varop, 1),
9483 result_mode)))
9485 varop = XEXP (varop, 1);
9486 continue;
9489 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9490 if (code == ASHIFT
9491 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9492 && (new = simplify_binary_operation (ASHIFT, result_mode,
9493 XEXP (varop, 1),
9494 GEN_INT (count))) != 0
9495 && GET_CODE (new) == CONST_INT
9496 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9497 INTVAL (new), result_mode, &complement_p))
9499 varop = XEXP (varop, 0);
9500 continue;
9502 break;
9504 case MINUS:
9505 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9506 with C the size of VAROP - 1 and the shift is logical if
9507 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9508 we have a (gt X 0) operation. If the shift is arithmetic with
9509 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9510 we have a (neg (gt X 0)) operation. */
9512 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9513 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9514 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
9515 && (code == LSHIFTRT || code == ASHIFTRT)
9516 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9517 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9518 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9520 count = 0;
9521 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
9522 const0_rtx);
9524 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9525 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9527 continue;
9529 break;
9531 case TRUNCATE:
9532 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9533 if the truncate does not affect the value. */
9534 if (code == LSHIFTRT
9535 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9536 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9537 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9538 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9539 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9541 rtx varop_inner = XEXP (varop, 0);
9543 varop_inner
9544 = gen_rtx_combine (LSHIFTRT, GET_MODE (varop_inner),
9545 XEXP (varop_inner, 0),
9546 GEN_INT (count
9547 + INTVAL (XEXP (varop_inner, 1))));
9548 varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
9549 varop_inner);
9550 count = 0;
9551 continue;
9553 break;
9555 default:
9556 break;
9559 break;
9562 /* We need to determine what mode to do the shift in. If the shift is
9563 a right shift or ROTATE, we must always do it in the mode it was
9564 originally done in. Otherwise, we can do it in MODE, the widest mode
9565 encountered. The code we care about is that of the shift that will
9566 actually be done, not the shift that was originally requested. */
9567 shift_mode
9568 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9569 ? result_mode : mode);
9571 /* We have now finished analyzing the shift. The result should be
9572 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9573 OUTER_OP is non-NIL, it is an operation that needs to be applied
9574 to the result of the shift. OUTER_CONST is the relevant constant,
9575 but we must turn off all bits turned off in the shift.
9577 If we were passed a value for X, see if we can use any pieces of
9578 it. If not, make new rtx. */
9580 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9581 && GET_CODE (XEXP (x, 1)) == CONST_INT
9582 && INTVAL (XEXP (x, 1)) == count)
9583 const_rtx = XEXP (x, 1);
9584 else
9585 const_rtx = GEN_INT (count);
9587 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9588 && GET_MODE (XEXP (x, 0)) == shift_mode
9589 && SUBREG_REG (XEXP (x, 0)) == varop)
9590 varop = XEXP (x, 0);
9591 else if (GET_MODE (varop) != shift_mode)
9592 varop = gen_lowpart_for_combine (shift_mode, varop);
9594 /* If we can't make the SUBREG, try to return what we were given. */
9595 if (GET_CODE (varop) == CLOBBER)
9596 return x ? x : varop;
9598 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9599 if (new != 0)
9600 x = new;
9601 else
9603 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
9604 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
9606 SUBST (XEXP (x, 0), varop);
9607 SUBST (XEXP (x, 1), const_rtx);
9610 /* If we have an outer operation and we just made a shift, it is
9611 possible that we could have simplified the shift were it not
9612 for the outer operation. So try to do the simplification
9613 recursively. */
9615 if (outer_op != NIL && GET_CODE (x) == code
9616 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9617 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9618 INTVAL (XEXP (x, 1)));
9620 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
9621 turn off all the bits that the shift would have turned off. */
9622 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9623 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9624 GET_MODE_MASK (result_mode) >> orig_count);
9626 /* Do the remainder of the processing in RESULT_MODE. */
9627 x = gen_lowpart_for_combine (result_mode, x);
9629 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9630 operation. */
9631 if (complement_p)
9632 x = gen_unary (NOT, result_mode, result_mode, x);
9634 if (outer_op != NIL)
9636 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9637 outer_const = trunc_int_for_mode (outer_const, result_mode);
9639 if (outer_op == AND)
9640 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9641 else if (outer_op == SET)
9642 /* This means that we have determined that the result is
9643 equivalent to a constant. This should be rare. */
9644 x = GEN_INT (outer_const);
9645 else if (GET_RTX_CLASS (outer_op) == '1')
9646 x = gen_unary (outer_op, result_mode, result_mode, x);
9647 else
9648 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9651 return x;
9654 /* Like recog, but we receive the address of a pointer to a new pattern.
9655 We try to match the rtx that the pointer points to.
9656 If that fails, we may try to modify or replace the pattern,
9657 storing the replacement into the same pointer object.
9659 Modifications include deletion or addition of CLOBBERs.
9661 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9662 the CLOBBERs are placed.
9664 The value is the final insn code from the pattern ultimately matched,
9665 or -1. */
9667 static int
9668 recog_for_combine (pnewpat, insn, pnotes)
9669 rtx *pnewpat;
9670 rtx insn;
9671 rtx *pnotes;
9673 register rtx pat = *pnewpat;
9674 int insn_code_number;
9675 int num_clobbers_to_add = 0;
9676 int i;
9677 rtx notes = 0;
9678 rtx old_notes;
9680 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9681 we use to indicate that something didn't match. If we find such a
9682 thing, force rejection. */
9683 if (GET_CODE (pat) == PARALLEL)
9684 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9685 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9686 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9687 return -1;
9689 /* Remove the old notes prior to trying to recognize the new pattern. */
9690 old_notes = REG_NOTES (insn);
9691 REG_NOTES (insn) = 0;
9693 /* Is the result of combination a valid instruction? */
9694 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9696 /* If it isn't, there is the possibility that we previously had an insn
9697 that clobbered some register as a side effect, but the combined
9698 insn doesn't need to do that. So try once more without the clobbers
9699 unless this represents an ASM insn. */
9701 if (insn_code_number < 0 && ! check_asm_operands (pat)
9702 && GET_CODE (pat) == PARALLEL)
9704 int pos;
9706 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9707 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9709 if (i != pos)
9710 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9711 pos++;
9714 SUBST_INT (XVECLEN (pat, 0), pos);
9716 if (pos == 1)
9717 pat = XVECEXP (pat, 0, 0);
9719 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9722 REG_NOTES (insn) = old_notes;
9724 /* If we had any clobbers to add, make a new pattern than contains
9725 them. Then check to make sure that all of them are dead. */
9726 if (num_clobbers_to_add)
9728 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9729 rtvec_alloc (GET_CODE (pat) == PARALLEL
9730 ? (XVECLEN (pat, 0)
9731 + num_clobbers_to_add)
9732 : num_clobbers_to_add + 1));
9734 if (GET_CODE (pat) == PARALLEL)
9735 for (i = 0; i < XVECLEN (pat, 0); i++)
9736 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9737 else
9738 XVECEXP (newpat, 0, 0) = pat;
9740 add_clobbers (newpat, insn_code_number);
9742 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9743 i < XVECLEN (newpat, 0); i++)
9745 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9746 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9747 return -1;
9748 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9749 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9751 pat = newpat;
9754 *pnewpat = pat;
9755 *pnotes = notes;
9757 return insn_code_number;
9760 /* Like gen_lowpart but for use by combine. In combine it is not possible
9761 to create any new pseudoregs. However, it is safe to create
9762 invalid memory addresses, because combine will try to recognize
9763 them and all they will do is make the combine attempt fail.
9765 If for some reason this cannot do its job, an rtx
9766 (clobber (const_int 0)) is returned.
9767 An insn containing that will not be recognized. */
9769 #undef gen_lowpart
9771 static rtx
9772 gen_lowpart_for_combine (mode, x)
9773 enum machine_mode mode;
9774 register rtx x;
9776 rtx result;
9778 if (GET_MODE (x) == mode)
9779 return x;
9781 /* We can only support MODE being wider than a word if X is a
9782 constant integer or has a mode the same size. */
9784 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9785 && ! ((GET_MODE (x) == VOIDmode
9786 && (GET_CODE (x) == CONST_INT
9787 || GET_CODE (x) == CONST_DOUBLE))
9788 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9789 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9791 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9792 won't know what to do. So we will strip off the SUBREG here and
9793 process normally. */
9794 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9796 x = SUBREG_REG (x);
9797 if (GET_MODE (x) == mode)
9798 return x;
9801 result = gen_lowpart_common (mode, x);
9802 #ifdef CLASS_CANNOT_CHANGE_MODE
9803 if (result != 0
9804 && GET_CODE (result) == SUBREG
9805 && GET_CODE (SUBREG_REG (result)) == REG
9806 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9807 && CLASS_CANNOT_CHANGE_MODE_P (GET_MODE (result),
9808 GET_MODE (SUBREG_REG (result))))
9809 REG_CHANGES_MODE (REGNO (SUBREG_REG (result))) = 1;
9810 #endif
9812 if (result)
9813 return result;
9815 if (GET_CODE (x) == MEM)
9817 register int offset = 0;
9818 rtx new;
9820 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9821 address. */
9822 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9823 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9825 /* If we want to refer to something bigger than the original memref,
9826 generate a perverse subreg instead. That will force a reload
9827 of the original memref X. */
9828 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9829 return gen_rtx_SUBREG (mode, x, 0);
9831 if (WORDS_BIG_ENDIAN)
9832 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9833 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9835 if (BYTES_BIG_ENDIAN)
9837 /* Adjust the address so that the address-after-the-data is
9838 unchanged. */
9839 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9840 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9842 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9843 MEM_COPY_ATTRIBUTES (new, x);
9844 return new;
9847 /* If X is a comparison operator, rewrite it in a new mode. This
9848 probably won't match, but may allow further simplifications. */
9849 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9850 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9852 /* If we couldn't simplify X any other way, just enclose it in a
9853 SUBREG. Normally, this SUBREG won't match, but some patterns may
9854 include an explicit SUBREG or we may simplify it further in combine. */
9855 else
9857 int word = 0;
9859 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9860 word = ((GET_MODE_SIZE (GET_MODE (x))
9861 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9862 / UNITS_PER_WORD);
9863 return gen_rtx_SUBREG (mode, x, word);
9867 /* Make an rtx expression. This is a subset of gen_rtx and only supports
9868 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9870 If the identical expression was previously in the insn (in the undobuf),
9871 it will be returned. Only if it is not found will a new expression
9872 be made. */
9874 /*VARARGS2*/
9875 static rtx
9876 gen_rtx_combine VPARAMS ((enum rtx_code code, enum machine_mode mode, ...))
9878 #ifndef ANSI_PROTOTYPES
9879 enum rtx_code code;
9880 enum machine_mode mode;
9881 #endif
9882 va_list p;
9883 int n_args;
9884 rtx args[3];
9885 int j;
9886 const char *fmt;
9887 rtx rt;
9888 struct undo *undo;
9890 VA_START (p, mode);
9892 #ifndef ANSI_PROTOTYPES
9893 code = va_arg (p, enum rtx_code);
9894 mode = va_arg (p, enum machine_mode);
9895 #endif
9897 n_args = GET_RTX_LENGTH (code);
9898 fmt = GET_RTX_FORMAT (code);
9900 if (n_args == 0 || n_args > 3)
9901 abort ();
9903 /* Get each arg and verify that it is supposed to be an expression. */
9904 for (j = 0; j < n_args; j++)
9906 if (*fmt++ != 'e')
9907 abort ();
9909 args[j] = va_arg (p, rtx);
9912 va_end (p);
9914 /* See if this is in undobuf. Be sure we don't use objects that came
9915 from another insn; this could produce circular rtl structures. */
9917 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9918 if (!undo->is_int
9919 && GET_CODE (undo->old_contents.r) == code
9920 && GET_MODE (undo->old_contents.r) == mode)
9922 for (j = 0; j < n_args; j++)
9923 if (XEXP (undo->old_contents.r, j) != args[j])
9924 break;
9926 if (j == n_args)
9927 return undo->old_contents.r;
9930 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9931 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9932 rt = rtx_alloc (code);
9933 PUT_MODE (rt, mode);
9934 XEXP (rt, 0) = args[0];
9935 if (n_args > 1)
9937 XEXP (rt, 1) = args[1];
9938 if (n_args > 2)
9939 XEXP (rt, 2) = args[2];
9941 return rt;
9944 /* These routines make binary and unary operations by first seeing if they
9945 fold; if not, a new expression is allocated. */
9947 static rtx
9948 gen_binary (code, mode, op0, op1)
9949 enum rtx_code code;
9950 enum machine_mode mode;
9951 rtx op0, op1;
9953 rtx result;
9954 rtx tem;
9956 if (GET_RTX_CLASS (code) == 'c'
9957 && (GET_CODE (op0) == CONST_INT
9958 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9959 tem = op0, op0 = op1, op1 = tem;
9961 if (GET_RTX_CLASS (code) == '<')
9963 enum machine_mode op_mode = GET_MODE (op0);
9965 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9966 just (REL_OP X Y). */
9967 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9969 op1 = XEXP (op0, 1);
9970 op0 = XEXP (op0, 0);
9971 op_mode = GET_MODE (op0);
9974 if (op_mode == VOIDmode)
9975 op_mode = GET_MODE (op1);
9976 result = simplify_relational_operation (code, op_mode, op0, op1);
9978 else
9979 result = simplify_binary_operation (code, mode, op0, op1);
9981 if (result)
9982 return result;
9984 /* Put complex operands first and constants second. */
9985 if (GET_RTX_CLASS (code) == 'c'
9986 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9987 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9988 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9989 || (GET_CODE (op0) == SUBREG
9990 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9991 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9992 return gen_rtx_combine (code, mode, op1, op0);
9994 /* If we are turning off bits already known off in OP0, we need not do
9995 an AND. */
9996 else if (code == AND && GET_CODE (op1) == CONST_INT
9997 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9998 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
9999 return op0;
10001 return gen_rtx_combine (code, mode, op0, op1);
10004 static rtx
10005 gen_unary (code, mode, op0_mode, op0)
10006 enum rtx_code code;
10007 enum machine_mode mode, op0_mode;
10008 rtx op0;
10010 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
10012 if (result)
10013 return result;
10015 return gen_rtx_combine (code, mode, op0);
10018 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10019 comparison code that will be tested.
10021 The result is a possibly different comparison code to use. *POP0 and
10022 *POP1 may be updated.
10024 It is possible that we might detect that a comparison is either always
10025 true or always false. However, we do not perform general constant
10026 folding in combine, so this knowledge isn't useful. Such tautologies
10027 should have been detected earlier. Hence we ignore all such cases. */
10029 static enum rtx_code
10030 simplify_comparison (code, pop0, pop1)
10031 enum rtx_code code;
10032 rtx *pop0;
10033 rtx *pop1;
10035 rtx op0 = *pop0;
10036 rtx op1 = *pop1;
10037 rtx tem, tem1;
10038 int i;
10039 enum machine_mode mode, tmode;
10041 /* Try a few ways of applying the same transformation to both operands. */
10042 while (1)
10044 #ifndef WORD_REGISTER_OPERATIONS
10045 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10046 so check specially. */
10047 if (code != GTU && code != GEU && code != LTU && code != LEU
10048 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10049 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10050 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10051 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10052 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10053 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10054 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10055 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10056 && GET_CODE (XEXP (op1, 1)) == CONST_INT
10057 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10058 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
10059 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
10060 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
10061 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
10062 && (INTVAL (XEXP (op0, 1))
10063 == (GET_MODE_BITSIZE (GET_MODE (op0))
10064 - (GET_MODE_BITSIZE
10065 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10067 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10068 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10070 #endif
10072 /* If both operands are the same constant shift, see if we can ignore the
10073 shift. We can if the shift is a rotate or if the bits shifted out of
10074 this shift are known to be zero for both inputs and if the type of
10075 comparison is compatible with the shift. */
10076 if (GET_CODE (op0) == GET_CODE (op1)
10077 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10078 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10079 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10080 && (code != GT && code != LT && code != GE && code != LE))
10081 || (GET_CODE (op0) == ASHIFTRT
10082 && (code != GTU && code != LTU
10083 && code != GEU && code != GEU)))
10084 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10085 && INTVAL (XEXP (op0, 1)) >= 0
10086 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10087 && XEXP (op0, 1) == XEXP (op1, 1))
10089 enum machine_mode mode = GET_MODE (op0);
10090 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10091 int shift_count = INTVAL (XEXP (op0, 1));
10093 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10094 mask &= (mask >> shift_count) << shift_count;
10095 else if (GET_CODE (op0) == ASHIFT)
10096 mask = (mask & (mask << shift_count)) >> shift_count;
10098 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10099 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10100 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10101 else
10102 break;
10105 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10106 SUBREGs are of the same mode, and, in both cases, the AND would
10107 be redundant if the comparison was done in the narrower mode,
10108 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10109 and the operand's possibly nonzero bits are 0xffffff01; in that case
10110 if we only care about QImode, we don't need the AND). This case
10111 occurs if the output mode of an scc insn is not SImode and
10112 STORE_FLAG_VALUE == 1 (e.g., the 386).
10114 Similarly, check for a case where the AND's are ZERO_EXTEND
10115 operations from some narrower mode even though a SUBREG is not
10116 present. */
10118 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10119 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10120 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10122 rtx inner_op0 = XEXP (op0, 0);
10123 rtx inner_op1 = XEXP (op1, 0);
10124 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10125 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10126 int changed = 0;
10128 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10129 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10130 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10131 && (GET_MODE (SUBREG_REG (inner_op0))
10132 == GET_MODE (SUBREG_REG (inner_op1)))
10133 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10134 <= HOST_BITS_PER_WIDE_INT)
10135 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10136 GET_MODE (SUBREG_REG (inner_op0)))))
10137 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10138 GET_MODE (SUBREG_REG (inner_op1))))))
10140 op0 = SUBREG_REG (inner_op0);
10141 op1 = SUBREG_REG (inner_op1);
10143 /* The resulting comparison is always unsigned since we masked
10144 off the original sign bit. */
10145 code = unsigned_condition (code);
10147 changed = 1;
10150 else if (c0 == c1)
10151 for (tmode = GET_CLASS_NARROWEST_MODE
10152 (GET_MODE_CLASS (GET_MODE (op0)));
10153 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10154 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10156 op0 = gen_lowpart_for_combine (tmode, inner_op0);
10157 op1 = gen_lowpart_for_combine (tmode, inner_op1);
10158 code = unsigned_condition (code);
10159 changed = 1;
10160 break;
10163 if (! changed)
10164 break;
10167 /* If both operands are NOT, we can strip off the outer operation
10168 and adjust the comparison code for swapped operands; similarly for
10169 NEG, except that this must be an equality comparison. */
10170 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10171 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10172 && (code == EQ || code == NE)))
10173 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10175 else
10176 break;
10179 /* If the first operand is a constant, swap the operands and adjust the
10180 comparison code appropriately, but don't do this if the second operand
10181 is already a constant integer. */
10182 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
10184 tem = op0, op0 = op1, op1 = tem;
10185 code = swap_condition (code);
10188 /* We now enter a loop during which we will try to simplify the comparison.
10189 For the most part, we only are concerned with comparisons with zero,
10190 but some things may really be comparisons with zero but not start
10191 out looking that way. */
10193 while (GET_CODE (op1) == CONST_INT)
10195 enum machine_mode mode = GET_MODE (op0);
10196 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10197 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10198 int equality_comparison_p;
10199 int sign_bit_comparison_p;
10200 int unsigned_comparison_p;
10201 HOST_WIDE_INT const_op;
10203 /* We only want to handle integral modes. This catches VOIDmode,
10204 CCmode, and the floating-point modes. An exception is that we
10205 can handle VOIDmode if OP0 is a COMPARE or a comparison
10206 operation. */
10208 if (GET_MODE_CLASS (mode) != MODE_INT
10209 && ! (mode == VOIDmode
10210 && (GET_CODE (op0) == COMPARE
10211 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
10212 break;
10214 /* Get the constant we are comparing against and turn off all bits
10215 not on in our mode. */
10216 const_op = trunc_int_for_mode (INTVAL (op1), mode);
10218 /* If we are comparing against a constant power of two and the value
10219 being compared can only have that single bit nonzero (e.g., it was
10220 `and'ed with that bit), we can replace this with a comparison
10221 with zero. */
10222 if (const_op
10223 && (code == EQ || code == NE || code == GE || code == GEU
10224 || code == LT || code == LTU)
10225 && mode_width <= HOST_BITS_PER_WIDE_INT
10226 && exact_log2 (const_op) >= 0
10227 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10229 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10230 op1 = const0_rtx, const_op = 0;
10233 /* Similarly, if we are comparing a value known to be either -1 or
10234 0 with -1, change it to the opposite comparison against zero. */
10236 if (const_op == -1
10237 && (code == EQ || code == NE || code == GT || code == LE
10238 || code == GEU || code == LTU)
10239 && num_sign_bit_copies (op0, mode) == mode_width)
10241 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10242 op1 = const0_rtx, const_op = 0;
10245 /* Do some canonicalizations based on the comparison code. We prefer
10246 comparisons against zero and then prefer equality comparisons.
10247 If we can reduce the size of a constant, we will do that too. */
10249 switch (code)
10251 case LT:
10252 /* < C is equivalent to <= (C - 1) */
10253 if (const_op > 0)
10255 const_op -= 1;
10256 op1 = GEN_INT (const_op);
10257 code = LE;
10258 /* ... fall through to LE case below. */
10260 else
10261 break;
10263 case LE:
10264 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10265 if (const_op < 0)
10267 const_op += 1;
10268 op1 = GEN_INT (const_op);
10269 code = LT;
10272 /* If we are doing a <= 0 comparison on a value known to have
10273 a zero sign bit, we can replace this with == 0. */
10274 else if (const_op == 0
10275 && mode_width <= HOST_BITS_PER_WIDE_INT
10276 && (nonzero_bits (op0, mode)
10277 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10278 code = EQ;
10279 break;
10281 case GE:
10282 /* >= C is equivalent to > (C - 1). */
10283 if (const_op > 0)
10285 const_op -= 1;
10286 op1 = GEN_INT (const_op);
10287 code = GT;
10288 /* ... fall through to GT below. */
10290 else
10291 break;
10293 case GT:
10294 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10295 if (const_op < 0)
10297 const_op += 1;
10298 op1 = GEN_INT (const_op);
10299 code = GE;
10302 /* If we are doing a > 0 comparison on a value known to have
10303 a zero sign bit, we can replace this with != 0. */
10304 else if (const_op == 0
10305 && mode_width <= HOST_BITS_PER_WIDE_INT
10306 && (nonzero_bits (op0, mode)
10307 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10308 code = NE;
10309 break;
10311 case LTU:
10312 /* < C is equivalent to <= (C - 1). */
10313 if (const_op > 0)
10315 const_op -= 1;
10316 op1 = GEN_INT (const_op);
10317 code = LEU;
10318 /* ... fall through ... */
10321 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10322 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10323 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10325 const_op = 0, op1 = const0_rtx;
10326 code = GE;
10327 break;
10329 else
10330 break;
10332 case LEU:
10333 /* unsigned <= 0 is equivalent to == 0 */
10334 if (const_op == 0)
10335 code = EQ;
10337 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10338 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10339 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10341 const_op = 0, op1 = const0_rtx;
10342 code = GE;
10344 break;
10346 case GEU:
10347 /* >= C is equivalent to < (C - 1). */
10348 if (const_op > 1)
10350 const_op -= 1;
10351 op1 = GEN_INT (const_op);
10352 code = GTU;
10353 /* ... fall through ... */
10356 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10357 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10358 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10360 const_op = 0, op1 = const0_rtx;
10361 code = LT;
10362 break;
10364 else
10365 break;
10367 case GTU:
10368 /* unsigned > 0 is equivalent to != 0 */
10369 if (const_op == 0)
10370 code = NE;
10372 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10373 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10374 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10376 const_op = 0, op1 = const0_rtx;
10377 code = LT;
10379 break;
10381 default:
10382 break;
10385 /* Compute some predicates to simplify code below. */
10387 equality_comparison_p = (code == EQ || code == NE);
10388 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10389 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10390 || code == GEU);
10392 /* If this is a sign bit comparison and we can do arithmetic in
10393 MODE, say that we will only be needing the sign bit of OP0. */
10394 if (sign_bit_comparison_p
10395 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10396 op0 = force_to_mode (op0, mode,
10397 ((HOST_WIDE_INT) 1
10398 << (GET_MODE_BITSIZE (mode) - 1)),
10399 NULL_RTX, 0);
10401 /* Now try cases based on the opcode of OP0. If none of the cases
10402 does a "continue", we exit this loop immediately after the
10403 switch. */
10405 switch (GET_CODE (op0))
10407 case ZERO_EXTRACT:
10408 /* If we are extracting a single bit from a variable position in
10409 a constant that has only a single bit set and are comparing it
10410 with zero, we can convert this into an equality comparison
10411 between the position and the location of the single bit. */
10413 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10414 && XEXP (op0, 1) == const1_rtx
10415 && equality_comparison_p && const_op == 0
10416 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10418 if (BITS_BIG_ENDIAN)
10420 #ifdef HAVE_extzv
10421 mode = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
10422 if (mode == VOIDmode)
10423 mode = word_mode;
10424 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10425 #else
10426 i = BITS_PER_WORD - 1 - i;
10427 #endif
10430 op0 = XEXP (op0, 2);
10431 op1 = GEN_INT (i);
10432 const_op = i;
10434 /* Result is nonzero iff shift count is equal to I. */
10435 code = reverse_condition (code);
10436 continue;
10439 /* ... fall through ... */
10441 case SIGN_EXTRACT:
10442 tem = expand_compound_operation (op0);
10443 if (tem != op0)
10445 op0 = tem;
10446 continue;
10448 break;
10450 case NOT:
10451 /* If testing for equality, we can take the NOT of the constant. */
10452 if (equality_comparison_p
10453 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10455 op0 = XEXP (op0, 0);
10456 op1 = tem;
10457 continue;
10460 /* If just looking at the sign bit, reverse the sense of the
10461 comparison. */
10462 if (sign_bit_comparison_p)
10464 op0 = XEXP (op0, 0);
10465 code = (code == GE ? LT : GE);
10466 continue;
10468 break;
10470 case NEG:
10471 /* If testing for equality, we can take the NEG of the constant. */
10472 if (equality_comparison_p
10473 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10475 op0 = XEXP (op0, 0);
10476 op1 = tem;
10477 continue;
10480 /* The remaining cases only apply to comparisons with zero. */
10481 if (const_op != 0)
10482 break;
10484 /* When X is ABS or is known positive,
10485 (neg X) is < 0 if and only if X != 0. */
10487 if (sign_bit_comparison_p
10488 && (GET_CODE (XEXP (op0, 0)) == ABS
10489 || (mode_width <= HOST_BITS_PER_WIDE_INT
10490 && (nonzero_bits (XEXP (op0, 0), mode)
10491 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10493 op0 = XEXP (op0, 0);
10494 code = (code == LT ? NE : EQ);
10495 continue;
10498 /* If we have NEG of something whose two high-order bits are the
10499 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10500 if (num_sign_bit_copies (op0, mode) >= 2)
10502 op0 = XEXP (op0, 0);
10503 code = swap_condition (code);
10504 continue;
10506 break;
10508 case ROTATE:
10509 /* If we are testing equality and our count is a constant, we
10510 can perform the inverse operation on our RHS. */
10511 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10512 && (tem = simplify_binary_operation (ROTATERT, mode,
10513 op1, XEXP (op0, 1))) != 0)
10515 op0 = XEXP (op0, 0);
10516 op1 = tem;
10517 continue;
10520 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10521 a particular bit. Convert it to an AND of a constant of that
10522 bit. This will be converted into a ZERO_EXTRACT. */
10523 if (const_op == 0 && sign_bit_comparison_p
10524 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10525 && mode_width <= HOST_BITS_PER_WIDE_INT)
10527 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10528 ((HOST_WIDE_INT) 1
10529 << (mode_width - 1
10530 - INTVAL (XEXP (op0, 1)))));
10531 code = (code == LT ? NE : EQ);
10532 continue;
10535 /* Fall through. */
10537 case ABS:
10538 /* ABS is ignorable inside an equality comparison with zero. */
10539 if (const_op == 0 && equality_comparison_p)
10541 op0 = XEXP (op0, 0);
10542 continue;
10544 break;
10546 case SIGN_EXTEND:
10547 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10548 to (compare FOO CONST) if CONST fits in FOO's mode and we
10549 are either testing inequality or have an unsigned comparison
10550 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10551 if (! unsigned_comparison_p
10552 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10553 <= HOST_BITS_PER_WIDE_INT)
10554 && ((unsigned HOST_WIDE_INT) const_op
10555 < (((unsigned HOST_WIDE_INT) 1
10556 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10558 op0 = XEXP (op0, 0);
10559 continue;
10561 break;
10563 case SUBREG:
10564 /* Check for the case where we are comparing A - C1 with C2,
10565 both constants are smaller than 1/2 the maximum positive
10566 value in MODE, and the comparison is equality or unsigned.
10567 In that case, if A is either zero-extended to MODE or has
10568 sufficient sign bits so that the high-order bit in MODE
10569 is a copy of the sign in the inner mode, we can prove that it is
10570 safe to do the operation in the wider mode. This simplifies
10571 many range checks. */
10573 if (mode_width <= HOST_BITS_PER_WIDE_INT
10574 && subreg_lowpart_p (op0)
10575 && GET_CODE (SUBREG_REG (op0)) == PLUS
10576 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10577 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10578 && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10579 < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10580 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10581 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10582 GET_MODE (SUBREG_REG (op0)))
10583 & ~GET_MODE_MASK (mode))
10584 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10585 GET_MODE (SUBREG_REG (op0)))
10586 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10587 - GET_MODE_BITSIZE (mode)))))
10589 op0 = SUBREG_REG (op0);
10590 continue;
10593 /* If the inner mode is narrower and we are extracting the low part,
10594 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10595 if (subreg_lowpart_p (op0)
10596 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10597 /* Fall through */ ;
10598 else
10599 break;
10601 /* ... fall through ... */
10603 case ZERO_EXTEND:
10604 if ((unsigned_comparison_p || equality_comparison_p)
10605 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10606 <= HOST_BITS_PER_WIDE_INT)
10607 && ((unsigned HOST_WIDE_INT) const_op
10608 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10610 op0 = XEXP (op0, 0);
10611 continue;
10613 break;
10615 case PLUS:
10616 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10617 this for equality comparisons due to pathological cases involving
10618 overflows. */
10619 if (equality_comparison_p
10620 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10621 op1, XEXP (op0, 1))))
10623 op0 = XEXP (op0, 0);
10624 op1 = tem;
10625 continue;
10628 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10629 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10630 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10632 op0 = XEXP (XEXP (op0, 0), 0);
10633 code = (code == LT ? EQ : NE);
10634 continue;
10636 break;
10638 case MINUS:
10639 /* We used to optimize signed comparisons against zero, but that
10640 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10641 arrive here as equality comparisons, or (GEU, LTU) are
10642 optimized away. No need to special-case them. */
10644 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10645 (eq B (minus A C)), whichever simplifies. We can only do
10646 this for equality comparisons due to pathological cases involving
10647 overflows. */
10648 if (equality_comparison_p
10649 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10650 XEXP (op0, 1), op1)))
10652 op0 = XEXP (op0, 0);
10653 op1 = tem;
10654 continue;
10657 if (equality_comparison_p
10658 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10659 XEXP (op0, 0), op1)))
10661 op0 = XEXP (op0, 1);
10662 op1 = tem;
10663 continue;
10666 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10667 of bits in X minus 1, is one iff X > 0. */
10668 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10669 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10670 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10671 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10673 op0 = XEXP (op0, 1);
10674 code = (code == GE ? LE : GT);
10675 continue;
10677 break;
10679 case XOR:
10680 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10681 if C is zero or B is a constant. */
10682 if (equality_comparison_p
10683 && 0 != (tem = simplify_binary_operation (XOR, mode,
10684 XEXP (op0, 1), op1)))
10686 op0 = XEXP (op0, 0);
10687 op1 = tem;
10688 continue;
10690 break;
10692 case EQ: case NE:
10693 case UNEQ: case LTGT:
10694 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10695 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10696 case UNORDERED: case ORDERED:
10697 /* We can't do anything if OP0 is a condition code value, rather
10698 than an actual data value. */
10699 if (const_op != 0
10700 #ifdef HAVE_cc0
10701 || XEXP (op0, 0) == cc0_rtx
10702 #endif
10703 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10704 break;
10706 /* Get the two operands being compared. */
10707 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10708 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10709 else
10710 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10712 /* Check for the cases where we simply want the result of the
10713 earlier test or the opposite of that result. */
10714 if (code == NE || code == EQ
10715 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10716 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10717 && (STORE_FLAG_VALUE
10718 & (((HOST_WIDE_INT) 1
10719 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10720 && (code == LT || code == GE)))
10722 enum rtx_code new_code;
10723 if (code == LT || code == NE)
10724 new_code = GET_CODE (op0);
10725 else
10726 new_code = combine_reversed_comparison_code (op0);
10728 if (new_code != UNKNOWN)
10730 code = new_code;
10731 op0 = tem;
10732 op1 = tem1;
10733 continue;
10736 break;
10738 case IOR:
10739 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10740 iff X <= 0. */
10741 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10742 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10743 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10745 op0 = XEXP (op0, 1);
10746 code = (code == GE ? GT : LE);
10747 continue;
10749 break;
10751 case AND:
10752 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10753 will be converted to a ZERO_EXTRACT later. */
10754 if (const_op == 0 && equality_comparison_p
10755 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10756 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10758 op0 = simplify_and_const_int
10759 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10760 XEXP (op0, 1),
10761 XEXP (XEXP (op0, 0), 1)),
10762 (HOST_WIDE_INT) 1);
10763 continue;
10766 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10767 zero and X is a comparison and C1 and C2 describe only bits set
10768 in STORE_FLAG_VALUE, we can compare with X. */
10769 if (const_op == 0 && equality_comparison_p
10770 && mode_width <= HOST_BITS_PER_WIDE_INT
10771 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10772 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10773 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10774 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10775 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10777 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10778 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10779 if ((~STORE_FLAG_VALUE & mask) == 0
10780 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10781 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10782 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10784 op0 = XEXP (XEXP (op0, 0), 0);
10785 continue;
10789 /* If we are doing an equality comparison of an AND of a bit equal
10790 to the sign bit, replace this with a LT or GE comparison of
10791 the underlying value. */
10792 if (equality_comparison_p
10793 && const_op == 0
10794 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10795 && mode_width <= HOST_BITS_PER_WIDE_INT
10796 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10797 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10799 op0 = XEXP (op0, 0);
10800 code = (code == EQ ? GE : LT);
10801 continue;
10804 /* If this AND operation is really a ZERO_EXTEND from a narrower
10805 mode, the constant fits within that mode, and this is either an
10806 equality or unsigned comparison, try to do this comparison in
10807 the narrower mode. */
10808 if ((equality_comparison_p || unsigned_comparison_p)
10809 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10810 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10811 & GET_MODE_MASK (mode))
10812 + 1)) >= 0
10813 && const_op >> i == 0
10814 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10816 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10817 continue;
10820 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10821 in both M1 and M2 and the SUBREG is either paradoxical or
10822 represents the low part, permute the SUBREG and the AND and
10823 try again. */
10824 if (GET_CODE (XEXP (op0, 0)) == SUBREG
10825 && (0
10826 #ifdef WORD_REGISTER_OPERATIONS
10827 || ((mode_width
10828 > (GET_MODE_BITSIZE
10829 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10830 && mode_width <= BITS_PER_WORD)
10831 #endif
10832 || ((mode_width
10833 <= (GET_MODE_BITSIZE
10834 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10835 && subreg_lowpart_p (XEXP (op0, 0))))
10836 #ifndef WORD_REGISTER_OPERATIONS
10837 /* It is unsafe to commute the AND into the SUBREG if the SUBREG
10838 is paradoxical and WORD_REGISTER_OPERATIONS is not defined.
10839 As originally written the upper bits have a defined value
10840 due to the AND operation. However, if we commute the AND
10841 inside the SUBREG then they no longer have defined values
10842 and the meaning of the code has been changed. */
10843 && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
10844 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10845 #endif
10846 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10847 && mode_width <= HOST_BITS_PER_WIDE_INT
10848 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10849 <= HOST_BITS_PER_WIDE_INT)
10850 && (INTVAL (XEXP (op0, 1)) & ~mask) == 0
10851 && 0 == (~GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10852 & INTVAL (XEXP (op0, 1)))
10853 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1)) != mask
10854 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10855 != GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10859 = gen_lowpart_for_combine
10860 (mode,
10861 gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10862 SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10863 continue;
10866 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10867 (eq (and (lshiftrt X) 1) 0). */
10868 if (const_op == 0 && equality_comparison_p
10869 && XEXP (op0, 1) == const1_rtx
10870 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10871 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == NOT)
10873 op0 = simplify_and_const_int
10874 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10875 XEXP (XEXP (XEXP (op0, 0), 0), 0),
10876 XEXP (XEXP (op0, 0), 1)),
10877 (HOST_WIDE_INT) 1);
10878 code = (code == NE ? EQ : NE);
10879 continue;
10881 break;
10883 case ASHIFT:
10884 /* If we have (compare (ashift FOO N) (const_int C)) and
10885 the high order N bits of FOO (N+1 if an inequality comparison)
10886 are known to be zero, we can do this by comparing FOO with C
10887 shifted right N bits so long as the low-order N bits of C are
10888 zero. */
10889 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10890 && INTVAL (XEXP (op0, 1)) >= 0
10891 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10892 < HOST_BITS_PER_WIDE_INT)
10893 && ((const_op
10894 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10895 && mode_width <= HOST_BITS_PER_WIDE_INT
10896 && (nonzero_bits (XEXP (op0, 0), mode)
10897 & ~(mask >> (INTVAL (XEXP (op0, 1))
10898 + ! equality_comparison_p))) == 0)
10900 /* We must perform a logical shift, not an arithmetic one,
10901 as we want the top N bits of C to be zero. */
10902 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10904 temp >>= INTVAL (XEXP (op0, 1));
10905 op1 = GEN_INT (trunc_int_for_mode (temp, mode));
10906 op0 = XEXP (op0, 0);
10907 continue;
10910 /* If we are doing a sign bit comparison, it means we are testing
10911 a particular bit. Convert it to the appropriate AND. */
10912 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10913 && mode_width <= HOST_BITS_PER_WIDE_INT)
10915 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10916 ((HOST_WIDE_INT) 1
10917 << (mode_width - 1
10918 - INTVAL (XEXP (op0, 1)))));
10919 code = (code == LT ? NE : EQ);
10920 continue;
10923 /* If this an equality comparison with zero and we are shifting
10924 the low bit to the sign bit, we can convert this to an AND of the
10925 low-order bit. */
10926 if (const_op == 0 && equality_comparison_p
10927 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10928 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10930 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10931 (HOST_WIDE_INT) 1);
10932 continue;
10934 break;
10936 case ASHIFTRT:
10937 /* If this is an equality comparison with zero, we can do this
10938 as a logical shift, which might be much simpler. */
10939 if (equality_comparison_p && const_op == 0
10940 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10942 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10943 XEXP (op0, 0),
10944 INTVAL (XEXP (op0, 1)));
10945 continue;
10948 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10949 do the comparison in a narrower mode. */
10950 if (! unsigned_comparison_p
10951 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10952 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10953 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10954 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10955 MODE_INT, 1)) != BLKmode
10956 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10957 || ((unsigned HOST_WIDE_INT) -const_op
10958 <= GET_MODE_MASK (tmode))))
10960 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10961 continue;
10964 /* Likewise if OP0 is a PLUS of a sign extension with a
10965 constant, which is usually represented with the PLUS
10966 between the shifts. */
10967 if (! unsigned_comparison_p
10968 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10969 && GET_CODE (XEXP (op0, 0)) == PLUS
10970 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10971 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10972 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10973 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10974 MODE_INT, 1)) != BLKmode
10975 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10976 || ((unsigned HOST_WIDE_INT) -const_op
10977 <= GET_MODE_MASK (tmode))))
10979 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10980 rtx add_const = XEXP (XEXP (op0, 0), 1);
10981 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
10982 XEXP (op0, 1));
10984 op0 = gen_binary (PLUS, tmode,
10985 gen_lowpart_for_combine (tmode, inner),
10986 new_const);
10987 continue;
10990 /* ... fall through ... */
10991 case LSHIFTRT:
10992 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10993 the low order N bits of FOO are known to be zero, we can do this
10994 by comparing FOO with C shifted left N bits so long as no
10995 overflow occurs. */
10996 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10997 && INTVAL (XEXP (op0, 1)) >= 0
10998 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10999 && mode_width <= HOST_BITS_PER_WIDE_INT
11000 && (nonzero_bits (XEXP (op0, 0), mode)
11001 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11002 && (const_op == 0
11003 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
11004 < mode_width)))
11006 const_op <<= INTVAL (XEXP (op0, 1));
11007 op1 = GEN_INT (const_op);
11008 op0 = XEXP (op0, 0);
11009 continue;
11012 /* If we are using this shift to extract just the sign bit, we
11013 can replace this with an LT or GE comparison. */
11014 if (const_op == 0
11015 && (equality_comparison_p || sign_bit_comparison_p)
11016 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11017 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
11019 op0 = XEXP (op0, 0);
11020 code = (code == NE || code == GT ? LT : GE);
11021 continue;
11023 break;
11025 default:
11026 break;
11029 break;
11032 /* Now make any compound operations involved in this comparison. Then,
11033 check for an outmost SUBREG on OP0 that is not doing anything or is
11034 paradoxical. The latter case can only occur when it is known that the
11035 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
11036 We can never remove a SUBREG for a non-equality comparison because the
11037 sign bit is in a different place in the underlying object. */
11039 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11040 op1 = make_compound_operation (op1, SET);
11042 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11043 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11044 && (code == NE || code == EQ)
11045 && ((GET_MODE_SIZE (GET_MODE (op0))
11046 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
11048 op0 = SUBREG_REG (op0);
11049 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
11052 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11053 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11054 && (code == NE || code == EQ)
11055 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11056 <= HOST_BITS_PER_WIDE_INT)
11057 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
11058 & ~GET_MODE_MASK (GET_MODE (op0))) == 0
11059 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
11060 op1),
11061 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11062 & ~GET_MODE_MASK (GET_MODE (op0))) == 0))
11063 op0 = SUBREG_REG (op0), op1 = tem;
11065 /* We now do the opposite procedure: Some machines don't have compare
11066 insns in all modes. If OP0's mode is an integer mode smaller than a
11067 word and we can't do a compare in that mode, see if there is a larger
11068 mode for which we can do the compare. There are a number of cases in
11069 which we can use the wider mode. */
11071 mode = GET_MODE (op0);
11072 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11073 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11074 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
11075 for (tmode = GET_MODE_WIDER_MODE (mode);
11076 (tmode != VOIDmode
11077 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11078 tmode = GET_MODE_WIDER_MODE (tmode))
11079 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
11081 /* If the only nonzero bits in OP0 and OP1 are those in the
11082 narrower mode and this is an equality or unsigned comparison,
11083 we can use the wider mode. Similarly for sign-extended
11084 values, in which case it is true for all comparisons. */
11085 if (((code == EQ || code == NE
11086 || code == GEU || code == GTU || code == LEU || code == LTU)
11087 && (nonzero_bits (op0, tmode) & ~GET_MODE_MASK (mode)) == 0
11088 && (nonzero_bits (op1, tmode) & ~GET_MODE_MASK (mode)) == 0)
11089 || ((num_sign_bit_copies (op0, tmode)
11090 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
11091 && (num_sign_bit_copies (op1, tmode)
11092 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
11094 /* If OP0 is an AND and we don't have an AND in MODE either,
11095 make a new AND in the proper mode. */
11096 if (GET_CODE (op0) == AND
11097 && (add_optab->handlers[(int) mode].insn_code
11098 == CODE_FOR_nothing))
11099 op0 = gen_binary (AND, tmode,
11100 gen_lowpart_for_combine (tmode,
11101 XEXP (op0, 0)),
11102 gen_lowpart_for_combine (tmode,
11103 XEXP (op0, 1)));
11105 op0 = gen_lowpart_for_combine (tmode, op0);
11106 op1 = gen_lowpart_for_combine (tmode, op1);
11107 break;
11110 /* If this is a test for negative, we can make an explicit
11111 test of the sign bit. */
11113 if (op1 == const0_rtx && (code == LT || code == GE)
11114 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11116 op0 = gen_binary (AND, tmode,
11117 gen_lowpart_for_combine (tmode, op0),
11118 GEN_INT ((HOST_WIDE_INT) 1
11119 << (GET_MODE_BITSIZE (mode) - 1)));
11120 code = (code == LT) ? NE : EQ;
11121 break;
11125 #ifdef CANONICALIZE_COMPARISON
11126 /* If this machine only supports a subset of valid comparisons, see if we
11127 can convert an unsupported one into a supported one. */
11128 CANONICALIZE_COMPARISON (code, op0, op1);
11129 #endif
11131 *pop0 = op0;
11132 *pop1 = op1;
11134 return code;
11137 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11138 searching backward. */
11139 static enum rtx_code
11140 combine_reversed_comparison_code (exp)
11141 rtx exp;
11143 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11144 rtx x;
11146 if (code1 != UNKNOWN
11147 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11148 return code1;
11149 /* Otherwise try and find where the condition codes were last set and
11150 use that. */
11151 x = get_last_value (XEXP (exp, 0));
11152 if (!x || GET_CODE (x) != COMPARE)
11153 return UNKNOWN;
11154 return reversed_comparison_code_parts (GET_CODE (exp),
11155 XEXP (x, 0), XEXP (x, 1), NULL);
11157 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11158 Return NULL_RTX in case we fail to do the reversal. */
11159 static rtx
11160 reversed_comparison (exp, mode, op0, op1)
11161 rtx exp, op0, op1;
11162 enum machine_mode mode;
11164 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11165 if (reversed_code == UNKNOWN)
11166 return NULL_RTX;
11167 else
11168 return gen_binary (reversed_code, mode, op0, op1);
11171 /* Utility function for following routine. Called when X is part of a value
11172 being stored into reg_last_set_value. Sets reg_last_set_table_tick
11173 for each register mentioned. Similar to mention_regs in cse.c */
11175 static void
11176 update_table_tick (x)
11177 rtx x;
11179 register enum rtx_code code = GET_CODE (x);
11180 register const char *fmt = GET_RTX_FORMAT (code);
11181 register int i;
11183 if (code == REG)
11185 unsigned int regno = REGNO (x);
11186 unsigned int endregno
11187 = regno + (regno < FIRST_PSEUDO_REGISTER
11188 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11189 unsigned int r;
11191 for (r = regno; r < endregno; r++)
11192 reg_last_set_table_tick[r] = label_tick;
11194 return;
11197 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11198 /* Note that we can't have an "E" in values stored; see
11199 get_last_value_validate. */
11200 if (fmt[i] == 'e')
11201 update_table_tick (XEXP (x, i));
11204 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11205 are saying that the register is clobbered and we no longer know its
11206 value. If INSN is zero, don't update reg_last_set; this is only permitted
11207 with VALUE also zero and is used to invalidate the register. */
11209 static void
11210 record_value_for_reg (reg, insn, value)
11211 rtx reg;
11212 rtx insn;
11213 rtx value;
11215 unsigned int regno = REGNO (reg);
11216 unsigned int endregno
11217 = regno + (regno < FIRST_PSEUDO_REGISTER
11218 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
11219 unsigned int i;
11221 /* If VALUE contains REG and we have a previous value for REG, substitute
11222 the previous value. */
11223 if (value && insn && reg_overlap_mentioned_p (reg, value))
11225 rtx tem;
11227 /* Set things up so get_last_value is allowed to see anything set up to
11228 our insn. */
11229 subst_low_cuid = INSN_CUID (insn);
11230 tem = get_last_value (reg);
11232 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11233 it isn't going to be useful and will take a lot of time to process,
11234 so just use the CLOBBER. */
11236 if (tem)
11238 if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
11239 || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
11240 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11241 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11242 tem = XEXP (tem, 0);
11244 value = replace_rtx (copy_rtx (value), reg, tem);
11248 /* For each register modified, show we don't know its value, that
11249 we don't know about its bitwise content, that its value has been
11250 updated, and that we don't know the location of the death of the
11251 register. */
11252 for (i = regno; i < endregno; i++)
11254 if (insn)
11255 reg_last_set[i] = insn;
11257 reg_last_set_value[i] = 0;
11258 reg_last_set_mode[i] = 0;
11259 reg_last_set_nonzero_bits[i] = 0;
11260 reg_last_set_sign_bit_copies[i] = 0;
11261 reg_last_death[i] = 0;
11264 /* Mark registers that are being referenced in this value. */
11265 if (value)
11266 update_table_tick (value);
11268 /* Now update the status of each register being set.
11269 If someone is using this register in this block, set this register
11270 to invalid since we will get confused between the two lives in this
11271 basic block. This makes using this register always invalid. In cse, we
11272 scan the table to invalidate all entries using this register, but this
11273 is too much work for us. */
11275 for (i = regno; i < endregno; i++)
11277 reg_last_set_label[i] = label_tick;
11278 if (value && reg_last_set_table_tick[i] == label_tick)
11279 reg_last_set_invalid[i] = 1;
11280 else
11281 reg_last_set_invalid[i] = 0;
11284 /* The value being assigned might refer to X (like in "x++;"). In that
11285 case, we must replace it with (clobber (const_int 0)) to prevent
11286 infinite loops. */
11287 if (value && ! get_last_value_validate (&value, insn,
11288 reg_last_set_label[regno], 0))
11290 value = copy_rtx (value);
11291 if (! get_last_value_validate (&value, insn,
11292 reg_last_set_label[regno], 1))
11293 value = 0;
11296 /* For the main register being modified, update the value, the mode, the
11297 nonzero bits, and the number of sign bit copies. */
11299 reg_last_set_value[regno] = value;
11301 if (value)
11303 subst_low_cuid = INSN_CUID (insn);
11304 reg_last_set_mode[regno] = GET_MODE (reg);
11305 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
11306 reg_last_set_sign_bit_copies[regno]
11307 = num_sign_bit_copies (value, GET_MODE (reg));
11311 /* Called via note_stores from record_dead_and_set_regs to handle one
11312 SET or CLOBBER in an insn. DATA is the instruction in which the
11313 set is occurring. */
11315 static void
11316 record_dead_and_set_regs_1 (dest, setter, data)
11317 rtx dest, setter;
11318 void *data;
11320 rtx record_dead_insn = (rtx) data;
11322 if (GET_CODE (dest) == SUBREG)
11323 dest = SUBREG_REG (dest);
11325 if (GET_CODE (dest) == REG)
11327 /* If we are setting the whole register, we know its value. Otherwise
11328 show that we don't know the value. We can handle SUBREG in
11329 some cases. */
11330 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11331 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11332 else if (GET_CODE (setter) == SET
11333 && GET_CODE (SET_DEST (setter)) == SUBREG
11334 && SUBREG_REG (SET_DEST (setter)) == dest
11335 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11336 && subreg_lowpart_p (SET_DEST (setter)))
11337 record_value_for_reg (dest, record_dead_insn,
11338 gen_lowpart_for_combine (GET_MODE (dest),
11339 SET_SRC (setter)));
11340 else
11341 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11343 else if (GET_CODE (dest) == MEM
11344 /* Ignore pushes, they clobber nothing. */
11345 && ! push_operand (dest, GET_MODE (dest)))
11346 mem_last_set = INSN_CUID (record_dead_insn);
11349 /* Update the records of when each REG was most recently set or killed
11350 for the things done by INSN. This is the last thing done in processing
11351 INSN in the combiner loop.
11353 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11354 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11355 and also the similar information mem_last_set (which insn most recently
11356 modified memory) and last_call_cuid (which insn was the most recent
11357 subroutine call). */
11359 static void
11360 record_dead_and_set_regs (insn)
11361 rtx insn;
11363 register rtx link;
11364 unsigned int i;
11366 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11368 if (REG_NOTE_KIND (link) == REG_DEAD
11369 && GET_CODE (XEXP (link, 0)) == REG)
11371 unsigned int regno = REGNO (XEXP (link, 0));
11372 unsigned int endregno
11373 = regno + (regno < FIRST_PSEUDO_REGISTER
11374 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
11375 : 1);
11377 for (i = regno; i < endregno; i++)
11378 reg_last_death[i] = insn;
11380 else if (REG_NOTE_KIND (link) == REG_INC)
11381 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11384 if (GET_CODE (insn) == CALL_INSN)
11386 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11387 if (call_used_regs[i])
11389 reg_last_set_value[i] = 0;
11390 reg_last_set_mode[i] = 0;
11391 reg_last_set_nonzero_bits[i] = 0;
11392 reg_last_set_sign_bit_copies[i] = 0;
11393 reg_last_death[i] = 0;
11396 last_call_cuid = mem_last_set = INSN_CUID (insn);
11399 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11402 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11403 register present in the SUBREG, so for each such SUBREG go back and
11404 adjust nonzero and sign bit information of the registers that are
11405 known to have some zero/sign bits set.
11407 This is needed because when combine blows the SUBREGs away, the
11408 information on zero/sign bits is lost and further combines can be
11409 missed because of that. */
11411 static void
11412 record_promoted_value (insn, subreg)
11413 rtx insn;
11414 rtx subreg;
11416 rtx links, set;
11417 unsigned int regno = REGNO (SUBREG_REG (subreg));
11418 enum machine_mode mode = GET_MODE (subreg);
11420 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11421 return;
11423 for (links = LOG_LINKS (insn); links;)
11425 insn = XEXP (links, 0);
11426 set = single_set (insn);
11428 if (! set || GET_CODE (SET_DEST (set)) != REG
11429 || REGNO (SET_DEST (set)) != regno
11430 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11432 links = XEXP (links, 1);
11433 continue;
11436 if (reg_last_set[regno] == insn)
11438 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
11439 reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11442 if (GET_CODE (SET_SRC (set)) == REG)
11444 regno = REGNO (SET_SRC (set));
11445 links = LOG_LINKS (insn);
11447 else
11448 break;
11452 /* Scan X for promoted SUBREGs. For each one found,
11453 note what it implies to the registers used in it. */
11455 static void
11456 check_promoted_subreg (insn, x)
11457 rtx insn;
11458 rtx x;
11460 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11461 && GET_CODE (SUBREG_REG (x)) == REG)
11462 record_promoted_value (insn, x);
11463 else
11465 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11466 int i, j;
11468 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11469 switch (format[i])
11471 case 'e':
11472 check_promoted_subreg (insn, XEXP (x, i));
11473 break;
11474 case 'V':
11475 case 'E':
11476 if (XVEC (x, i) != 0)
11477 for (j = 0; j < XVECLEN (x, i); j++)
11478 check_promoted_subreg (insn, XVECEXP (x, i, j));
11479 break;
11484 /* Utility routine for the following function. Verify that all the registers
11485 mentioned in *LOC are valid when *LOC was part of a value set when
11486 label_tick == TICK. Return 0 if some are not.
11488 If REPLACE is non-zero, replace the invalid reference with
11489 (clobber (const_int 0)) and return 1. This replacement is useful because
11490 we often can get useful information about the form of a value (e.g., if
11491 it was produced by a shift that always produces -1 or 0) even though
11492 we don't know exactly what registers it was produced from. */
11494 static int
11495 get_last_value_validate (loc, insn, tick, replace)
11496 rtx *loc;
11497 rtx insn;
11498 int tick;
11499 int replace;
11501 rtx x = *loc;
11502 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11503 int len = GET_RTX_LENGTH (GET_CODE (x));
11504 int i;
11506 if (GET_CODE (x) == REG)
11508 unsigned int regno = REGNO (x);
11509 unsigned int endregno
11510 = regno + (regno < FIRST_PSEUDO_REGISTER
11511 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11512 unsigned int j;
11514 for (j = regno; j < endregno; j++)
11515 if (reg_last_set_invalid[j]
11516 /* If this is a pseudo-register that was only set once and not
11517 live at the beginning of the function, it is always valid. */
11518 || (! (regno >= FIRST_PSEUDO_REGISTER
11519 && REG_N_SETS (regno) == 1
11520 && (! REGNO_REG_SET_P
11521 (BASIC_BLOCK (0)->global_live_at_start, regno)))
11522 && reg_last_set_label[j] > tick))
11524 if (replace)
11525 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11526 return replace;
11529 return 1;
11531 /* If this is a memory reference, make sure that there were
11532 no stores after it that might have clobbered the value. We don't
11533 have alias info, so we assume any store invalidates it. */
11534 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11535 && INSN_CUID (insn) <= mem_last_set)
11537 if (replace)
11538 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11539 return replace;
11542 for (i = 0; i < len; i++)
11543 if ((fmt[i] == 'e'
11544 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
11545 /* Don't bother with these. They shouldn't occur anyway. */
11546 || fmt[i] == 'E')
11547 return 0;
11549 /* If we haven't found a reason for it to be invalid, it is valid. */
11550 return 1;
11553 /* Get the last value assigned to X, if known. Some registers
11554 in the value may be replaced with (clobber (const_int 0)) if their value
11555 is known longer known reliably. */
11557 static rtx
11558 get_last_value (x)
11559 rtx x;
11561 unsigned int regno;
11562 rtx value;
11564 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11565 then convert it to the desired mode. If this is a paradoxical SUBREG,
11566 we cannot predict what values the "extra" bits might have. */
11567 if (GET_CODE (x) == SUBREG
11568 && subreg_lowpart_p (x)
11569 && (GET_MODE_SIZE (GET_MODE (x))
11570 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11571 && (value = get_last_value (SUBREG_REG (x))) != 0)
11572 return gen_lowpart_for_combine (GET_MODE (x), value);
11574 if (GET_CODE (x) != REG)
11575 return 0;
11577 regno = REGNO (x);
11578 value = reg_last_set_value[regno];
11580 /* If we don't have a value, or if it isn't for this basic block and
11581 it's either a hard register, set more than once, or it's a live
11582 at the beginning of the function, return 0.
11584 Because if it's not live at the beginnning of the function then the reg
11585 is always set before being used (is never used without being set).
11586 And, if it's set only once, and it's always set before use, then all
11587 uses must have the same last value, even if it's not from this basic
11588 block. */
11590 if (value == 0
11591 || (reg_last_set_label[regno] != label_tick
11592 && (regno < FIRST_PSEUDO_REGISTER
11593 || REG_N_SETS (regno) != 1
11594 || (REGNO_REG_SET_P
11595 (BASIC_BLOCK (0)->global_live_at_start, regno)))))
11596 return 0;
11598 /* If the value was set in a later insn than the ones we are processing,
11599 we can't use it even if the register was only set once. */
11600 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11601 return 0;
11603 /* If the value has all its registers valid, return it. */
11604 if (get_last_value_validate (&value, reg_last_set[regno],
11605 reg_last_set_label[regno], 0))
11606 return value;
11608 /* Otherwise, make a copy and replace any invalid register with
11609 (clobber (const_int 0)). If that fails for some reason, return 0. */
11611 value = copy_rtx (value);
11612 if (get_last_value_validate (&value, reg_last_set[regno],
11613 reg_last_set_label[regno], 1))
11614 return value;
11616 return 0;
11619 /* Return nonzero if expression X refers to a REG or to memory
11620 that is set in an instruction more recent than FROM_CUID. */
11622 static int
11623 use_crosses_set_p (x, from_cuid)
11624 register rtx x;
11625 int from_cuid;
11627 register const char *fmt;
11628 register int i;
11629 register enum rtx_code code = GET_CODE (x);
11631 if (code == REG)
11633 unsigned int regno = REGNO (x);
11634 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11635 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11637 #ifdef PUSH_ROUNDING
11638 /* Don't allow uses of the stack pointer to be moved,
11639 because we don't know whether the move crosses a push insn. */
11640 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11641 return 1;
11642 #endif
11643 for (; regno < endreg; regno++)
11644 if (reg_last_set[regno]
11645 && INSN_CUID (reg_last_set[regno]) > from_cuid)
11646 return 1;
11647 return 0;
11650 if (code == MEM && mem_last_set > from_cuid)
11651 return 1;
11653 fmt = GET_RTX_FORMAT (code);
11655 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11657 if (fmt[i] == 'E')
11659 register int j;
11660 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11661 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11662 return 1;
11664 else if (fmt[i] == 'e'
11665 && use_crosses_set_p (XEXP (x, i), from_cuid))
11666 return 1;
11668 return 0;
11671 /* Define three variables used for communication between the following
11672 routines. */
11674 static unsigned int reg_dead_regno, reg_dead_endregno;
11675 static int reg_dead_flag;
11677 /* Function called via note_stores from reg_dead_at_p.
11679 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11680 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11682 static void
11683 reg_dead_at_p_1 (dest, x, data)
11684 rtx dest;
11685 rtx x;
11686 void *data ATTRIBUTE_UNUSED;
11688 unsigned int regno, endregno;
11690 if (GET_CODE (dest) != REG)
11691 return;
11693 regno = REGNO (dest);
11694 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11695 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11697 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11698 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11701 /* Return non-zero if REG is known to be dead at INSN.
11703 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11704 referencing REG, it is dead. If we hit a SET referencing REG, it is
11705 live. Otherwise, see if it is live or dead at the start of the basic
11706 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11707 must be assumed to be always live. */
11709 static int
11710 reg_dead_at_p (reg, insn)
11711 rtx reg;
11712 rtx insn;
11714 int block;
11715 unsigned int i;
11717 /* Set variables for reg_dead_at_p_1. */
11718 reg_dead_regno = REGNO (reg);
11719 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11720 ? HARD_REGNO_NREGS (reg_dead_regno,
11721 GET_MODE (reg))
11722 : 1);
11724 reg_dead_flag = 0;
11726 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11727 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11729 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11730 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11731 return 0;
11734 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11735 beginning of function. */
11736 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11737 insn = prev_nonnote_insn (insn))
11739 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11740 if (reg_dead_flag)
11741 return reg_dead_flag == 1 ? 1 : 0;
11743 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11744 return 1;
11747 /* Get the basic block number that we were in. */
11748 if (insn == 0)
11749 block = 0;
11750 else
11752 for (block = 0; block < n_basic_blocks; block++)
11753 if (insn == BLOCK_HEAD (block))
11754 break;
11756 if (block == n_basic_blocks)
11757 return 0;
11760 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11761 if (REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start, i))
11762 return 0;
11764 return 1;
11767 /* Note hard registers in X that are used. This code is similar to
11768 that in flow.c, but much simpler since we don't care about pseudos. */
11770 static void
11771 mark_used_regs_combine (x)
11772 rtx x;
11774 RTX_CODE code = GET_CODE (x);
11775 unsigned int regno;
11776 int i;
11778 switch (code)
11780 case LABEL_REF:
11781 case SYMBOL_REF:
11782 case CONST_INT:
11783 case CONST:
11784 case CONST_DOUBLE:
11785 case PC:
11786 case ADDR_VEC:
11787 case ADDR_DIFF_VEC:
11788 case ASM_INPUT:
11789 #ifdef HAVE_cc0
11790 /* CC0 must die in the insn after it is set, so we don't need to take
11791 special note of it here. */
11792 case CC0:
11793 #endif
11794 return;
11796 case CLOBBER:
11797 /* If we are clobbering a MEM, mark any hard registers inside the
11798 address as used. */
11799 if (GET_CODE (XEXP (x, 0)) == MEM)
11800 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11801 return;
11803 case REG:
11804 regno = REGNO (x);
11805 /* A hard reg in a wide mode may really be multiple registers.
11806 If so, mark all of them just like the first. */
11807 if (regno < FIRST_PSEUDO_REGISTER)
11809 unsigned int endregno, r;
11811 /* None of this applies to the stack, frame or arg pointers */
11812 if (regno == STACK_POINTER_REGNUM
11813 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11814 || regno == HARD_FRAME_POINTER_REGNUM
11815 #endif
11816 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11817 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11818 #endif
11819 || regno == FRAME_POINTER_REGNUM)
11820 return;
11822 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11823 for (r = regno; r < endregno; r++)
11824 SET_HARD_REG_BIT (newpat_used_regs, r);
11826 return;
11828 case SET:
11830 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11831 the address. */
11832 register rtx testreg = SET_DEST (x);
11834 while (GET_CODE (testreg) == SUBREG
11835 || GET_CODE (testreg) == ZERO_EXTRACT
11836 || GET_CODE (testreg) == SIGN_EXTRACT
11837 || GET_CODE (testreg) == STRICT_LOW_PART)
11838 testreg = XEXP (testreg, 0);
11840 if (GET_CODE (testreg) == MEM)
11841 mark_used_regs_combine (XEXP (testreg, 0));
11843 mark_used_regs_combine (SET_SRC (x));
11845 return;
11847 default:
11848 break;
11851 /* Recursively scan the operands of this expression. */
11854 register const char *fmt = GET_RTX_FORMAT (code);
11856 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11858 if (fmt[i] == 'e')
11859 mark_used_regs_combine (XEXP (x, i));
11860 else if (fmt[i] == 'E')
11862 register int j;
11864 for (j = 0; j < XVECLEN (x, i); j++)
11865 mark_used_regs_combine (XVECEXP (x, i, j));
11871 /* Remove register number REGNO from the dead registers list of INSN.
11873 Return the note used to record the death, if there was one. */
11876 remove_death (regno, insn)
11877 unsigned int regno;
11878 rtx insn;
11880 register rtx note = find_regno_note (insn, REG_DEAD, regno);
11882 if (note)
11884 REG_N_DEATHS (regno)--;
11885 remove_note (insn, note);
11888 return note;
11891 /* For each register (hardware or pseudo) used within expression X, if its
11892 death is in an instruction with cuid between FROM_CUID (inclusive) and
11893 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11894 list headed by PNOTES.
11896 That said, don't move registers killed by maybe_kill_insn.
11898 This is done when X is being merged by combination into TO_INSN. These
11899 notes will then be distributed as needed. */
11901 static void
11902 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11903 rtx x;
11904 rtx maybe_kill_insn;
11905 int from_cuid;
11906 rtx to_insn;
11907 rtx *pnotes;
11909 register const char *fmt;
11910 register int len, i;
11911 register enum rtx_code code = GET_CODE (x);
11913 if (code == REG)
11915 unsigned int regno = REGNO (x);
11916 register rtx where_dead = reg_last_death[regno];
11917 register rtx before_dead, after_dead;
11919 /* Don't move the register if it gets killed in between from and to */
11920 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11921 && ! reg_referenced_p (x, maybe_kill_insn))
11922 return;
11924 /* WHERE_DEAD could be a USE insn made by combine, so first we
11925 make sure that we have insns with valid INSN_CUID values. */
11926 before_dead = where_dead;
11927 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11928 before_dead = PREV_INSN (before_dead);
11930 after_dead = where_dead;
11931 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11932 after_dead = NEXT_INSN (after_dead);
11934 if (before_dead && after_dead
11935 && INSN_CUID (before_dead) >= from_cuid
11936 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11937 || (where_dead != after_dead
11938 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11940 rtx note = remove_death (regno, where_dead);
11942 /* It is possible for the call above to return 0. This can occur
11943 when reg_last_death points to I2 or I1 that we combined with.
11944 In that case make a new note.
11946 We must also check for the case where X is a hard register
11947 and NOTE is a death note for a range of hard registers
11948 including X. In that case, we must put REG_DEAD notes for
11949 the remaining registers in place of NOTE. */
11951 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11952 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11953 > GET_MODE_SIZE (GET_MODE (x))))
11955 unsigned int deadregno = REGNO (XEXP (note, 0));
11956 unsigned int deadend
11957 = (deadregno + HARD_REGNO_NREGS (deadregno,
11958 GET_MODE (XEXP (note, 0))));
11959 unsigned int ourend
11960 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11961 unsigned int i;
11963 for (i = deadregno; i < deadend; i++)
11964 if (i < regno || i >= ourend)
11965 REG_NOTES (where_dead)
11966 = gen_rtx_EXPR_LIST (REG_DEAD,
11967 gen_rtx_REG (reg_raw_mode[i], i),
11968 REG_NOTES (where_dead));
11971 /* If we didn't find any note, or if we found a REG_DEAD note that
11972 covers only part of the given reg, and we have a multi-reg hard
11973 register, then to be safe we must check for REG_DEAD notes
11974 for each register other than the first. They could have
11975 their own REG_DEAD notes lying around. */
11976 else if ((note == 0
11977 || (note != 0
11978 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11979 < GET_MODE_SIZE (GET_MODE (x)))))
11980 && regno < FIRST_PSEUDO_REGISTER
11981 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11983 unsigned int ourend
11984 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11985 unsigned int i, offset;
11986 rtx oldnotes = 0;
11988 if (note)
11989 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11990 else
11991 offset = 1;
11993 for (i = regno + offset; i < ourend; i++)
11994 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11995 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11998 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12000 XEXP (note, 1) = *pnotes;
12001 *pnotes = note;
12003 else
12004 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12006 REG_N_DEATHS (regno)++;
12009 return;
12012 else if (GET_CODE (x) == SET)
12014 rtx dest = SET_DEST (x);
12016 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12018 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12019 that accesses one word of a multi-word item, some
12020 piece of everything register in the expression is used by
12021 this insn, so remove any old death. */
12023 if (GET_CODE (dest) == ZERO_EXTRACT
12024 || GET_CODE (dest) == STRICT_LOW_PART
12025 || (GET_CODE (dest) == SUBREG
12026 && (((GET_MODE_SIZE (GET_MODE (dest))
12027 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12028 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12029 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12031 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12032 return;
12035 /* If this is some other SUBREG, we know it replaces the entire
12036 value, so use that as the destination. */
12037 if (GET_CODE (dest) == SUBREG)
12038 dest = SUBREG_REG (dest);
12040 /* If this is a MEM, adjust deaths of anything used in the address.
12041 For a REG (the only other possibility), the entire value is
12042 being replaced so the old value is not used in this insn. */
12044 if (GET_CODE (dest) == MEM)
12045 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12046 to_insn, pnotes);
12047 return;
12050 else if (GET_CODE (x) == CLOBBER)
12051 return;
12053 len = GET_RTX_LENGTH (code);
12054 fmt = GET_RTX_FORMAT (code);
12056 for (i = 0; i < len; i++)
12058 if (fmt[i] == 'E')
12060 register int j;
12061 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12062 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12063 to_insn, pnotes);
12065 else if (fmt[i] == 'e')
12066 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12070 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12071 pattern of an insn. X must be a REG. */
12073 static int
12074 reg_bitfield_target_p (x, body)
12075 rtx x;
12076 rtx body;
12078 int i;
12080 if (GET_CODE (body) == SET)
12082 rtx dest = SET_DEST (body);
12083 rtx target;
12084 unsigned int regno, tregno, endregno, endtregno;
12086 if (GET_CODE (dest) == ZERO_EXTRACT)
12087 target = XEXP (dest, 0);
12088 else if (GET_CODE (dest) == STRICT_LOW_PART)
12089 target = SUBREG_REG (XEXP (dest, 0));
12090 else
12091 return 0;
12093 if (GET_CODE (target) == SUBREG)
12094 target = SUBREG_REG (target);
12096 if (GET_CODE (target) != REG)
12097 return 0;
12099 tregno = REGNO (target), regno = REGNO (x);
12100 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12101 return target == x;
12103 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
12104 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12106 return endregno > tregno && regno < endtregno;
12109 else if (GET_CODE (body) == PARALLEL)
12110 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12111 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12112 return 1;
12114 return 0;
12117 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12118 as appropriate. I3 and I2 are the insns resulting from the combination
12119 insns including FROM (I2 may be zero).
12121 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12122 not need REG_DEAD notes because they are being substituted for. This
12123 saves searching in the most common cases.
12125 Each note in the list is either ignored or placed on some insns, depending
12126 on the type of note. */
12128 static void
12129 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
12130 rtx notes;
12131 rtx from_insn;
12132 rtx i3, i2;
12133 rtx elim_i2, elim_i1;
12135 rtx note, next_note;
12136 rtx tem;
12138 for (note = notes; note; note = next_note)
12140 rtx place = 0, place2 = 0;
12142 /* If this NOTE references a pseudo register, ensure it references
12143 the latest copy of that register. */
12144 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12145 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12146 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12148 next_note = XEXP (note, 1);
12149 switch (REG_NOTE_KIND (note))
12151 case REG_BR_PROB:
12152 case REG_EXEC_COUNT:
12153 /* Doesn't matter much where we put this, as long as it's somewhere.
12154 It is preferable to keep these notes on branches, which is most
12155 likely to be i3. */
12156 place = i3;
12157 break;
12159 case REG_NON_LOCAL_GOTO:
12160 if (GET_CODE (i3) == JUMP_INSN)
12161 place = i3;
12162 else if (i2 && GET_CODE (i2) == JUMP_INSN)
12163 place = i2;
12164 else
12165 abort();
12166 break;
12168 case REG_EH_REGION:
12169 case REG_EH_RETHROW:
12170 case REG_NORETURN:
12171 /* These notes must remain with the call. It should not be
12172 possible for both I2 and I3 to be a call. */
12173 if (GET_CODE (i3) == CALL_INSN)
12174 place = i3;
12175 else if (i2 && GET_CODE (i2) == CALL_INSN)
12176 place = i2;
12177 else
12178 abort ();
12179 break;
12181 case REG_UNUSED:
12182 /* Any clobbers for i3 may still exist, and so we must process
12183 REG_UNUSED notes from that insn.
12185 Any clobbers from i2 or i1 can only exist if they were added by
12186 recog_for_combine. In that case, recog_for_combine created the
12187 necessary REG_UNUSED notes. Trying to keep any original
12188 REG_UNUSED notes from these insns can cause incorrect output
12189 if it is for the same register as the original i3 dest.
12190 In that case, we will notice that the register is set in i3,
12191 and then add a REG_UNUSED note for the destination of i3, which
12192 is wrong. However, it is possible to have REG_UNUSED notes from
12193 i2 or i1 for register which were both used and clobbered, so
12194 we keep notes from i2 or i1 if they will turn into REG_DEAD
12195 notes. */
12197 /* If this register is set or clobbered in I3, put the note there
12198 unless there is one already. */
12199 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12201 if (from_insn != i3)
12202 break;
12204 if (! (GET_CODE (XEXP (note, 0)) == REG
12205 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12206 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12207 place = i3;
12209 /* Otherwise, if this register is used by I3, then this register
12210 now dies here, so we must put a REG_DEAD note here unless there
12211 is one already. */
12212 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12213 && ! (GET_CODE (XEXP (note, 0)) == REG
12214 ? find_regno_note (i3, REG_DEAD,
12215 REGNO (XEXP (note, 0)))
12216 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12218 PUT_REG_NOTE_KIND (note, REG_DEAD);
12219 place = i3;
12221 break;
12223 case REG_EQUAL:
12224 case REG_EQUIV:
12225 case REG_NOALIAS:
12226 /* These notes say something about results of an insn. We can
12227 only support them if they used to be on I3 in which case they
12228 remain on I3. Otherwise they are ignored.
12230 If the note refers to an expression that is not a constant, we
12231 must also ignore the note since we cannot tell whether the
12232 equivalence is still true. It might be possible to do
12233 slightly better than this (we only have a problem if I2DEST
12234 or I1DEST is present in the expression), but it doesn't
12235 seem worth the trouble. */
12237 if (from_insn == i3
12238 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12239 place = i3;
12240 break;
12242 case REG_INC:
12243 case REG_NO_CONFLICT:
12244 /* These notes say something about how a register is used. They must
12245 be present on any use of the register in I2 or I3. */
12246 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12247 place = i3;
12249 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12251 if (place)
12252 place2 = i2;
12253 else
12254 place = i2;
12256 break;
12258 case REG_LABEL:
12259 /* This can show up in several ways -- either directly in the
12260 pattern, or hidden off in the constant pool with (or without?)
12261 a REG_EQUAL note. */
12262 /* ??? Ignore the without-reg_equal-note problem for now. */
12263 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12264 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12265 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12266 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12267 place = i3;
12269 if (i2
12270 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12271 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12272 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12273 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12275 if (place)
12276 place2 = i2;
12277 else
12278 place = i2;
12280 break;
12282 case REG_NONNEG:
12283 case REG_WAS_0:
12284 /* These notes say something about the value of a register prior
12285 to the execution of an insn. It is too much trouble to see
12286 if the note is still correct in all situations. It is better
12287 to simply delete it. */
12288 break;
12290 case REG_RETVAL:
12291 /* If the insn previously containing this note still exists,
12292 put it back where it was. Otherwise move it to the previous
12293 insn. Adjust the corresponding REG_LIBCALL note. */
12294 if (GET_CODE (from_insn) != NOTE)
12295 place = from_insn;
12296 else
12298 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12299 place = prev_real_insn (from_insn);
12300 if (tem && place)
12301 XEXP (tem, 0) = place;
12302 /* If we're deleting the last remaining instruction of a
12303 libcall sequence, don't add the notes. */
12304 else if (XEXP (note, 0) == from_insn)
12305 tem = place = 0;
12307 break;
12309 case REG_LIBCALL:
12310 /* This is handled similarly to REG_RETVAL. */
12311 if (GET_CODE (from_insn) != NOTE)
12312 place = from_insn;
12313 else
12315 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12316 place = next_real_insn (from_insn);
12317 if (tem && place)
12318 XEXP (tem, 0) = place;
12319 /* If we're deleting the last remaining instruction of a
12320 libcall sequence, don't add the notes. */
12321 else if (XEXP (note, 0) == from_insn)
12322 tem = place = 0;
12324 break;
12326 case REG_DEAD:
12327 /* If the register is used as an input in I3, it dies there.
12328 Similarly for I2, if it is non-zero and adjacent to I3.
12330 If the register is not used as an input in either I3 or I2
12331 and it is not one of the registers we were supposed to eliminate,
12332 there are two possibilities. We might have a non-adjacent I2
12333 or we might have somehow eliminated an additional register
12334 from a computation. For example, we might have had A & B where
12335 we discover that B will always be zero. In this case we will
12336 eliminate the reference to A.
12338 In both cases, we must search to see if we can find a previous
12339 use of A and put the death note there. */
12341 if (from_insn
12342 && GET_CODE (from_insn) == CALL_INSN
12343 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12344 place = from_insn;
12345 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12346 place = i3;
12347 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12348 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12349 place = i2;
12351 if (rtx_equal_p (XEXP (note, 0), elim_i2)
12352 || rtx_equal_p (XEXP (note, 0), elim_i1))
12353 break;
12355 if (place == 0)
12357 basic_block bb = BASIC_BLOCK (this_basic_block);
12359 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12361 if (! INSN_P (tem))
12363 if (tem == bb->head)
12364 break;
12365 continue;
12368 /* If the register is being set at TEM, see if that is all
12369 TEM is doing. If so, delete TEM. Otherwise, make this
12370 into a REG_UNUSED note instead. */
12371 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12373 rtx set = single_set (tem);
12374 rtx inner_dest = 0;
12375 #ifdef HAVE_cc0
12376 rtx cc0_setter = NULL_RTX;
12377 #endif
12379 if (set != 0)
12380 for (inner_dest = SET_DEST (set);
12381 (GET_CODE (inner_dest) == STRICT_LOW_PART
12382 || GET_CODE (inner_dest) == SUBREG
12383 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12384 inner_dest = XEXP (inner_dest, 0))
12387 /* Verify that it was the set, and not a clobber that
12388 modified the register.
12390 CC0 targets must be careful to maintain setter/user
12391 pairs. If we cannot delete the setter due to side
12392 effects, mark the user with an UNUSED note instead
12393 of deleting it. */
12395 if (set != 0 && ! side_effects_p (SET_SRC (set))
12396 && rtx_equal_p (XEXP (note, 0), inner_dest)
12397 #ifdef HAVE_cc0
12398 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12399 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12400 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12401 #endif
12404 /* Move the notes and links of TEM elsewhere.
12405 This might delete other dead insns recursively.
12406 First set the pattern to something that won't use
12407 any register. */
12409 PATTERN (tem) = pc_rtx;
12411 distribute_notes (REG_NOTES (tem), tem, tem,
12412 NULL_RTX, NULL_RTX, NULL_RTX);
12413 distribute_links (LOG_LINKS (tem));
12415 PUT_CODE (tem, NOTE);
12416 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12417 NOTE_SOURCE_FILE (tem) = 0;
12419 #ifdef HAVE_cc0
12420 /* Delete the setter too. */
12421 if (cc0_setter)
12423 PATTERN (cc0_setter) = pc_rtx;
12425 distribute_notes (REG_NOTES (cc0_setter),
12426 cc0_setter, cc0_setter,
12427 NULL_RTX, NULL_RTX, NULL_RTX);
12428 distribute_links (LOG_LINKS (cc0_setter));
12430 PUT_CODE (cc0_setter, NOTE);
12431 NOTE_LINE_NUMBER (cc0_setter)
12432 = NOTE_INSN_DELETED;
12433 NOTE_SOURCE_FILE (cc0_setter) = 0;
12435 #endif
12437 /* If the register is both set and used here, put the
12438 REG_DEAD note here, but place a REG_UNUSED note
12439 here too unless there already is one. */
12440 else if (reg_referenced_p (XEXP (note, 0),
12441 PATTERN (tem)))
12443 place = tem;
12445 if (! find_regno_note (tem, REG_UNUSED,
12446 REGNO (XEXP (note, 0))))
12447 REG_NOTES (tem)
12448 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
12449 REG_NOTES (tem));
12451 else
12453 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12455 /* If there isn't already a REG_UNUSED note, put one
12456 here. */
12457 if (! find_regno_note (tem, REG_UNUSED,
12458 REGNO (XEXP (note, 0))))
12459 place = tem;
12460 break;
12463 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12464 || (GET_CODE (tem) == CALL_INSN
12465 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12467 place = tem;
12469 /* If we are doing a 3->2 combination, and we have a
12470 register which formerly died in i3 and was not used
12471 by i2, which now no longer dies in i3 and is used in
12472 i2 but does not die in i2, and place is between i2
12473 and i3, then we may need to move a link from place to
12474 i2. */
12475 if (i2 && INSN_UID (place) <= max_uid_cuid
12476 && INSN_CUID (place) > INSN_CUID (i2)
12477 && from_insn
12478 && INSN_CUID (from_insn) > INSN_CUID (i2)
12479 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12481 rtx links = LOG_LINKS (place);
12482 LOG_LINKS (place) = 0;
12483 distribute_links (links);
12485 break;
12488 if (tem == bb->head)
12489 break;
12492 /* We haven't found an insn for the death note and it
12493 is still a REG_DEAD note, but we have hit the beginning
12494 of the block. If the existing life info says the reg
12495 was dead, there's nothing left to do. Otherwise, we'll
12496 need to do a global life update after combine. */
12497 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12498 && REGNO_REG_SET_P (bb->global_live_at_start,
12499 REGNO (XEXP (note, 0))))
12501 SET_BIT (refresh_blocks, this_basic_block);
12502 need_refresh = 1;
12506 /* If the register is set or already dead at PLACE, we needn't do
12507 anything with this note if it is still a REG_DEAD note.
12508 We can here if it is set at all, not if is it totally replace,
12509 which is what `dead_or_set_p' checks, so also check for it being
12510 set partially. */
12512 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12514 unsigned int regno = REGNO (XEXP (note, 0));
12516 if (dead_or_set_p (place, XEXP (note, 0))
12517 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12519 /* Unless the register previously died in PLACE, clear
12520 reg_last_death. [I no longer understand why this is
12521 being done.] */
12522 if (reg_last_death[regno] != place)
12523 reg_last_death[regno] = 0;
12524 place = 0;
12526 else
12527 reg_last_death[regno] = place;
12529 /* If this is a death note for a hard reg that is occupying
12530 multiple registers, ensure that we are still using all
12531 parts of the object. If we find a piece of the object
12532 that is unused, we must arrange for an appropriate REG_DEAD
12533 note to be added for it. However, we can't just emit a USE
12534 and tag the note to it, since the register might actually
12535 be dead; so we recourse, and the recursive call then finds
12536 the previous insn that used this register. */
12538 if (place && regno < FIRST_PSEUDO_REGISTER
12539 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
12541 unsigned int endregno
12542 = regno + HARD_REGNO_NREGS (regno,
12543 GET_MODE (XEXP (note, 0)));
12544 int all_used = 1;
12545 unsigned int i;
12547 for (i = regno; i < endregno; i++)
12548 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12549 && ! find_regno_fusage (place, USE, i))
12550 || dead_or_set_regno_p (place, i))
12551 all_used = 0;
12553 if (! all_used)
12555 /* Put only REG_DEAD notes for pieces that are
12556 not already dead or set. */
12558 for (i = regno; i < endregno;
12559 i += HARD_REGNO_NREGS (i, reg_raw_mode[i]))
12561 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
12562 basic_block bb = BASIC_BLOCK (this_basic_block);
12564 if (! dead_or_set_p (place, piece)
12565 && ! reg_bitfield_target_p (piece,
12566 PATTERN (place)))
12568 rtx new_note
12569 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12571 distribute_notes (new_note, place, place,
12572 NULL_RTX, NULL_RTX, NULL_RTX);
12574 else if (! refers_to_regno_p (i, i + 1,
12575 PATTERN (place), 0)
12576 && ! find_regno_fusage (place, USE, i))
12577 for (tem = PREV_INSN (place); ;
12578 tem = PREV_INSN (tem))
12580 if (! INSN_P (tem))
12582 if (tem == bb->head)
12584 SET_BIT (refresh_blocks,
12585 this_basic_block);
12586 need_refresh = 1;
12587 break;
12589 continue;
12591 if (dead_or_set_p (tem, piece)
12592 || reg_bitfield_target_p (piece,
12593 PATTERN (tem)))
12595 REG_NOTES (tem)
12596 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12597 REG_NOTES (tem));
12598 break;
12604 place = 0;
12608 break;
12610 default:
12611 /* Any other notes should not be present at this point in the
12612 compilation. */
12613 abort ();
12616 if (place)
12618 XEXP (note, 1) = REG_NOTES (place);
12619 REG_NOTES (place) = note;
12621 else if ((REG_NOTE_KIND (note) == REG_DEAD
12622 || REG_NOTE_KIND (note) == REG_UNUSED)
12623 && GET_CODE (XEXP (note, 0)) == REG)
12624 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12626 if (place2)
12628 if ((REG_NOTE_KIND (note) == REG_DEAD
12629 || REG_NOTE_KIND (note) == REG_UNUSED)
12630 && GET_CODE (XEXP (note, 0)) == REG)
12631 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12633 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12634 REG_NOTE_KIND (note),
12635 XEXP (note, 0),
12636 REG_NOTES (place2));
12641 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12642 I3, I2, and I1 to new locations. This is also called in one case to
12643 add a link pointing at I3 when I3's destination is changed. */
12645 static void
12646 distribute_links (links)
12647 rtx links;
12649 rtx link, next_link;
12651 for (link = links; link; link = next_link)
12653 rtx place = 0;
12654 rtx insn;
12655 rtx set, reg;
12657 next_link = XEXP (link, 1);
12659 /* If the insn that this link points to is a NOTE or isn't a single
12660 set, ignore it. In the latter case, it isn't clear what we
12661 can do other than ignore the link, since we can't tell which
12662 register it was for. Such links wouldn't be used by combine
12663 anyway.
12665 It is not possible for the destination of the target of the link to
12666 have been changed by combine. The only potential of this is if we
12667 replace I3, I2, and I1 by I3 and I2. But in that case the
12668 destination of I2 also remains unchanged. */
12670 if (GET_CODE (XEXP (link, 0)) == NOTE
12671 || (set = single_set (XEXP (link, 0))) == 0)
12672 continue;
12674 reg = SET_DEST (set);
12675 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12676 || GET_CODE (reg) == SIGN_EXTRACT
12677 || GET_CODE (reg) == STRICT_LOW_PART)
12678 reg = XEXP (reg, 0);
12680 /* A LOG_LINK is defined as being placed on the first insn that uses
12681 a register and points to the insn that sets the register. Start
12682 searching at the next insn after the target of the link and stop
12683 when we reach a set of the register or the end of the basic block.
12685 Note that this correctly handles the link that used to point from
12686 I3 to I2. Also note that not much searching is typically done here
12687 since most links don't point very far away. */
12689 for (insn = NEXT_INSN (XEXP (link, 0));
12690 (insn && (this_basic_block == n_basic_blocks - 1
12691 || BLOCK_HEAD (this_basic_block + 1) != insn));
12692 insn = NEXT_INSN (insn))
12693 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12695 if (reg_referenced_p (reg, PATTERN (insn)))
12696 place = insn;
12697 break;
12699 else if (GET_CODE (insn) == CALL_INSN
12700 && find_reg_fusage (insn, USE, reg))
12702 place = insn;
12703 break;
12706 /* If we found a place to put the link, place it there unless there
12707 is already a link to the same insn as LINK at that point. */
12709 if (place)
12711 rtx link2;
12713 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12714 if (XEXP (link2, 0) == XEXP (link, 0))
12715 break;
12717 if (link2 == 0)
12719 XEXP (link, 1) = LOG_LINKS (place);
12720 LOG_LINKS (place) = link;
12722 /* Set added_links_insn to the earliest insn we added a
12723 link to. */
12724 if (added_links_insn == 0
12725 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12726 added_links_insn = place;
12732 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12734 static int
12735 insn_cuid (insn)
12736 rtx insn;
12738 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12739 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12740 insn = NEXT_INSN (insn);
12742 if (INSN_UID (insn) > max_uid_cuid)
12743 abort ();
12745 return INSN_CUID (insn);
12748 void
12749 dump_combine_stats (file)
12750 FILE *file;
12752 fnotice
12753 (file,
12754 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12755 combine_attempts, combine_merges, combine_extras, combine_successes);
12758 void
12759 dump_combine_total_stats (file)
12760 FILE *file;
12762 fnotice
12763 (file,
12764 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12765 total_attempts, total_merges, total_extras, total_successes);