gcc/
[official-gcc.git] / gcc / combine.c
blob1bee2c7f4222e02514ce010f8ae714a4a417ca2b
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "toplev.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file. */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105 #include "df.h"
106 #include "cgraph.h"
108 /* Number of attempts to combine instructions in this function. */
110 static int combine_attempts;
112 /* Number of attempts that got as far as substitution in this function. */
114 static int combine_merges;
116 /* Number of instructions combined with added SETs in this function. */
118 static int combine_extras;
120 /* Number of instructions combined in this function. */
122 static int combine_successes;
124 /* Totals over entire compilation. */
126 static int total_attempts, total_merges, total_extras, total_successes;
128 /* combine_instructions may try to replace the right hand side of the
129 second instruction with the value of an associated REG_EQUAL note
130 before throwing it at try_combine. That is problematic when there
131 is a REG_DEAD note for a register used in the old right hand side
132 and can cause distribute_notes to do wrong things. This is the
133 second instruction if it has been so modified, null otherwise. */
135 static rtx i2mod;
137 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
139 static rtx i2mod_old_rhs;
141 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
143 static rtx i2mod_new_rhs;
145 typedef struct reg_stat_struct {
146 /* Record last point of death of (hard or pseudo) register n. */
147 rtx last_death;
149 /* Record last point of modification of (hard or pseudo) register n. */
150 rtx last_set;
152 /* The next group of fields allows the recording of the last value assigned
153 to (hard or pseudo) register n. We use this information to see if an
154 operation being processed is redundant given a prior operation performed
155 on the register. For example, an `and' with a constant is redundant if
156 all the zero bits are already known to be turned off.
158 We use an approach similar to that used by cse, but change it in the
159 following ways:
161 (1) We do not want to reinitialize at each label.
162 (2) It is useful, but not critical, to know the actual value assigned
163 to a register. Often just its form is helpful.
165 Therefore, we maintain the following fields:
167 last_set_value the last value assigned
168 last_set_label records the value of label_tick when the
169 register was assigned
170 last_set_table_tick records the value of label_tick when a
171 value using the register is assigned
172 last_set_invalid set to nonzero when it is not valid
173 to use the value of this register in some
174 register's value
176 To understand the usage of these tables, it is important to understand
177 the distinction between the value in last_set_value being valid and
178 the register being validly contained in some other expression in the
179 table.
181 (The next two parameters are out of date).
183 reg_stat[i].last_set_value is valid if it is nonzero, and either
184 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186 Register I may validly appear in any expression returned for the value
187 of another register if reg_n_sets[i] is 1. It may also appear in the
188 value for register J if reg_stat[j].last_set_invalid is zero, or
189 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191 If an expression is found in the table containing a register which may
192 not validly appear in an expression, the register is replaced by
193 something that won't match, (clobber (const_int 0)). */
195 /* Record last value assigned to (hard or pseudo) register n. */
197 rtx last_set_value;
199 /* Record the value of label_tick when an expression involving register n
200 is placed in last_set_value. */
202 int last_set_table_tick;
204 /* Record the value of label_tick when the value for register n is placed in
205 last_set_value. */
207 int last_set_label;
209 /* These fields are maintained in parallel with last_set_value and are
210 used to store the mode in which the register was last set, the bits
211 that were known to be zero when it was last set, and the number of
212 sign bits copies it was known to have when it was last set. */
214 unsigned HOST_WIDE_INT last_set_nonzero_bits;
215 char last_set_sign_bit_copies;
216 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
218 /* Set nonzero if references to register n in expressions should not be
219 used. last_set_invalid is set nonzero when this register is being
220 assigned to and last_set_table_tick == label_tick. */
222 char last_set_invalid;
224 /* Some registers that are set more than once and used in more than one
225 basic block are nevertheless always set in similar ways. For example,
226 a QImode register may be loaded from memory in two places on a machine
227 where byte loads zero extend.
229 We record in the following fields if a register has some leading bits
230 that are always equal to the sign bit, and what we know about the
231 nonzero bits of a register, specifically which bits are known to be
232 zero.
234 If an entry is zero, it means that we don't know anything special. */
236 unsigned char sign_bit_copies;
238 unsigned HOST_WIDE_INT nonzero_bits;
240 /* Record the value of the label_tick when the last truncation
241 happened. The field truncated_to_mode is only valid if
242 truncation_label == label_tick. */
244 int truncation_label;
246 /* Record the last truncation seen for this register. If truncation
247 is not a nop to this mode we might be able to save an explicit
248 truncation if we know that value already contains a truncated
249 value. */
251 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
252 } reg_stat_type;
254 DEF_VEC_O(reg_stat_type);
255 DEF_VEC_ALLOC_O(reg_stat_type,heap);
257 static VEC(reg_stat_type,heap) *reg_stat;
259 /* Record the luid of the last insn that invalidated memory
260 (anything that writes memory, and subroutine calls, but not pushes). */
262 static int mem_last_set;
264 /* Record the luid of the last CALL_INSN
265 so we can tell whether a potential combination crosses any calls. */
267 static int last_call_luid;
269 /* When `subst' is called, this is the insn that is being modified
270 (by combining in a previous insn). The PATTERN of this insn
271 is still the old pattern partially modified and it should not be
272 looked at, but this may be used to examine the successors of the insn
273 to judge whether a simplification is valid. */
275 static rtx subst_insn;
277 /* This is the lowest LUID that `subst' is currently dealing with.
278 get_last_value will not return a value if the register was set at or
279 after this LUID. If not for this mechanism, we could get confused if
280 I2 or I1 in try_combine were an insn that used the old value of a register
281 to obtain a new value. In that case, we might erroneously get the
282 new value of the register when we wanted the old one. */
284 static int subst_low_luid;
286 /* This contains any hard registers that are used in newpat; reg_dead_at_p
287 must consider all these registers to be always live. */
289 static HARD_REG_SET newpat_used_regs;
291 /* This is an insn to which a LOG_LINKS entry has been added. If this
292 insn is the earlier than I2 or I3, combine should rescan starting at
293 that location. */
295 static rtx added_links_insn;
297 /* Basic block in which we are performing combines. */
298 static basic_block this_basic_block;
299 static bool optimize_this_for_speed_p;
302 /* Length of the currently allocated uid_insn_cost array. */
304 static int max_uid_known;
306 /* The following array records the insn_rtx_cost for every insn
307 in the instruction stream. */
309 static int *uid_insn_cost;
311 /* The following array records the LOG_LINKS for every insn in the
312 instruction stream as an INSN_LIST rtx. */
314 static rtx *uid_log_links;
316 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
317 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
319 /* Incremented for each basic block. */
321 static int label_tick;
323 /* Reset to label_tick for each extended basic block in scanning order. */
325 static int label_tick_ebb_start;
327 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
328 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
330 static enum machine_mode nonzero_bits_mode;
332 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
333 be safely used. It is zero while computing them and after combine has
334 completed. This former test prevents propagating values based on
335 previously set values, which can be incorrect if a variable is modified
336 in a loop. */
338 static int nonzero_sign_valid;
341 /* Record one modification to rtl structure
342 to be undone by storing old_contents into *where. */
344 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE };
346 struct undo
348 struct undo *next;
349 enum undo_kind kind;
350 union { rtx r; int i; enum machine_mode m; } old_contents;
351 union { rtx *r; int *i; } where;
354 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
355 num_undo says how many are currently recorded.
357 other_insn is nonzero if we have modified some other insn in the process
358 of working on subst_insn. It must be verified too. */
360 struct undobuf
362 struct undo *undos;
363 struct undo *frees;
364 rtx other_insn;
367 static struct undobuf undobuf;
369 /* Number of times the pseudo being substituted for
370 was found and replaced. */
372 static int n_occurrences;
374 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
375 enum machine_mode,
376 unsigned HOST_WIDE_INT,
377 unsigned HOST_WIDE_INT *);
378 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
379 enum machine_mode,
380 unsigned int, unsigned int *);
381 static void do_SUBST (rtx *, rtx);
382 static void do_SUBST_INT (int *, int);
383 static void init_reg_last (void);
384 static void setup_incoming_promotions (rtx);
385 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
386 static int cant_combine_insn_p (rtx);
387 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
388 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
389 static int contains_muldiv (rtx);
390 static rtx try_combine (rtx, rtx, rtx, int *);
391 static void undo_all (void);
392 static void undo_commit (void);
393 static rtx *find_split_point (rtx *, rtx, bool);
394 static rtx subst (rtx, rtx, rtx, int, int);
395 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
396 static rtx simplify_if_then_else (rtx);
397 static rtx simplify_set (rtx);
398 static rtx simplify_logical (rtx);
399 static rtx expand_compound_operation (rtx);
400 static const_rtx expand_field_assignment (const_rtx);
401 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
402 rtx, unsigned HOST_WIDE_INT, int, int, int);
403 static rtx extract_left_shift (rtx, int);
404 static rtx make_compound_operation (rtx, enum rtx_code);
405 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
406 unsigned HOST_WIDE_INT *);
407 static rtx canon_reg_for_combine (rtx, rtx);
408 static rtx force_to_mode (rtx, enum machine_mode,
409 unsigned HOST_WIDE_INT, int);
410 static rtx if_then_else_cond (rtx, rtx *, rtx *);
411 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
412 static int rtx_equal_for_field_assignment_p (rtx, rtx);
413 static rtx make_field_assignment (rtx);
414 static rtx apply_distributive_law (rtx);
415 static rtx distribute_and_simplify_rtx (rtx, int);
416 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
417 unsigned HOST_WIDE_INT);
418 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
419 unsigned HOST_WIDE_INT);
420 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
421 HOST_WIDE_INT, enum machine_mode, int *);
422 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
423 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
424 int);
425 static int recog_for_combine (rtx *, rtx, rtx *);
426 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
427 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
428 static void update_table_tick (rtx);
429 static void record_value_for_reg (rtx, rtx, rtx);
430 static void check_promoted_subreg (rtx, rtx);
431 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
432 static void record_dead_and_set_regs (rtx);
433 static int get_last_value_validate (rtx *, rtx, int, int);
434 static rtx get_last_value (const_rtx);
435 static int use_crosses_set_p (const_rtx, int);
436 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
437 static int reg_dead_at_p (rtx, rtx);
438 static void move_deaths (rtx, rtx, int, rtx, rtx *);
439 static int reg_bitfield_target_p (rtx, rtx);
440 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
441 static void distribute_links (rtx);
442 static void mark_used_regs_combine (rtx);
443 static void record_promoted_value (rtx, rtx);
444 static int unmentioned_reg_p_1 (rtx *, void *);
445 static bool unmentioned_reg_p (rtx, rtx);
446 static int record_truncated_value (rtx *, void *);
447 static void record_truncated_values (rtx *, void *);
448 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
449 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
452 /* It is not safe to use ordinary gen_lowpart in combine.
453 See comments in gen_lowpart_for_combine. */
454 #undef RTL_HOOKS_GEN_LOWPART
455 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
457 /* Our implementation of gen_lowpart never emits a new pseudo. */
458 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
459 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
461 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
462 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
464 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
465 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
467 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
468 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
470 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
473 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
474 PATTERN can not be split. Otherwise, it returns an insn sequence.
475 This is a wrapper around split_insns which ensures that the
476 reg_stat vector is made larger if the splitter creates a new
477 register. */
479 static rtx
480 combine_split_insns (rtx pattern, rtx insn)
482 rtx ret;
483 unsigned int nregs;
485 ret = split_insns (pattern, insn);
486 nregs = max_reg_num ();
487 if (nregs > VEC_length (reg_stat_type, reg_stat))
488 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
489 return ret;
492 /* This is used by find_single_use to locate an rtx in LOC that
493 contains exactly one use of DEST, which is typically either a REG
494 or CC0. It returns a pointer to the innermost rtx expression
495 containing DEST. Appearances of DEST that are being used to
496 totally replace it are not counted. */
498 static rtx *
499 find_single_use_1 (rtx dest, rtx *loc)
501 rtx x = *loc;
502 enum rtx_code code = GET_CODE (x);
503 rtx *result = NULL;
504 rtx *this_result;
505 int i;
506 const char *fmt;
508 switch (code)
510 case CONST_INT:
511 case CONST:
512 case LABEL_REF:
513 case SYMBOL_REF:
514 case CONST_DOUBLE:
515 case CONST_VECTOR:
516 case CLOBBER:
517 return 0;
519 case SET:
520 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
521 of a REG that occupies all of the REG, the insn uses DEST if
522 it is mentioned in the destination or the source. Otherwise, we
523 need just check the source. */
524 if (GET_CODE (SET_DEST (x)) != CC0
525 && GET_CODE (SET_DEST (x)) != PC
526 && !REG_P (SET_DEST (x))
527 && ! (GET_CODE (SET_DEST (x)) == SUBREG
528 && REG_P (SUBREG_REG (SET_DEST (x)))
529 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
530 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
531 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
532 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
533 break;
535 return find_single_use_1 (dest, &SET_SRC (x));
537 case MEM:
538 case SUBREG:
539 return find_single_use_1 (dest, &XEXP (x, 0));
541 default:
542 break;
545 /* If it wasn't one of the common cases above, check each expression and
546 vector of this code. Look for a unique usage of DEST. */
548 fmt = GET_RTX_FORMAT (code);
549 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
551 if (fmt[i] == 'e')
553 if (dest == XEXP (x, i)
554 || (REG_P (dest) && REG_P (XEXP (x, i))
555 && REGNO (dest) == REGNO (XEXP (x, i))))
556 this_result = loc;
557 else
558 this_result = find_single_use_1 (dest, &XEXP (x, i));
560 if (result == NULL)
561 result = this_result;
562 else if (this_result)
563 /* Duplicate usage. */
564 return NULL;
566 else if (fmt[i] == 'E')
568 int j;
570 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
572 if (XVECEXP (x, i, j) == dest
573 || (REG_P (dest)
574 && REG_P (XVECEXP (x, i, j))
575 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
576 this_result = loc;
577 else
578 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
580 if (result == NULL)
581 result = this_result;
582 else if (this_result)
583 return NULL;
588 return result;
592 /* See if DEST, produced in INSN, is used only a single time in the
593 sequel. If so, return a pointer to the innermost rtx expression in which
594 it is used.
596 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
598 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
599 care about REG_DEAD notes or LOG_LINKS.
601 Otherwise, we find the single use by finding an insn that has a
602 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
603 only referenced once in that insn, we know that it must be the first
604 and last insn referencing DEST. */
606 static rtx *
607 find_single_use (rtx dest, rtx insn, rtx *ploc)
609 basic_block bb;
610 rtx next;
611 rtx *result;
612 rtx link;
614 #ifdef HAVE_cc0
615 if (dest == cc0_rtx)
617 next = NEXT_INSN (insn);
618 if (next == 0
619 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
620 return 0;
622 result = find_single_use_1 (dest, &PATTERN (next));
623 if (result && ploc)
624 *ploc = next;
625 return result;
627 #endif
629 if (!REG_P (dest))
630 return 0;
632 bb = BLOCK_FOR_INSN (insn);
633 for (next = NEXT_INSN (insn);
634 next && BLOCK_FOR_INSN (next) == bb;
635 next = NEXT_INSN (next))
636 if (INSN_P (next) && dead_or_set_p (next, dest))
638 for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
639 if (XEXP (link, 0) == insn)
640 break;
642 if (link)
644 result = find_single_use_1 (dest, &PATTERN (next));
645 if (ploc)
646 *ploc = next;
647 return result;
651 return 0;
654 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
655 insn. The substitution can be undone by undo_all. If INTO is already
656 set to NEWVAL, do not record this change. Because computing NEWVAL might
657 also call SUBST, we have to compute it before we put anything into
658 the undo table. */
660 static void
661 do_SUBST (rtx *into, rtx newval)
663 struct undo *buf;
664 rtx oldval = *into;
666 if (oldval == newval)
667 return;
669 /* We'd like to catch as many invalid transformations here as
670 possible. Unfortunately, there are way too many mode changes
671 that are perfectly valid, so we'd waste too much effort for
672 little gain doing the checks here. Focus on catching invalid
673 transformations involving integer constants. */
674 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
675 && CONST_INT_P (newval))
677 /* Sanity check that we're replacing oldval with a CONST_INT
678 that is a valid sign-extension for the original mode. */
679 gcc_assert (INTVAL (newval)
680 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
682 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
683 CONST_INT is not valid, because after the replacement, the
684 original mode would be gone. Unfortunately, we can't tell
685 when do_SUBST is called to replace the operand thereof, so we
686 perform this test on oldval instead, checking whether an
687 invalid replacement took place before we got here. */
688 gcc_assert (!(GET_CODE (oldval) == SUBREG
689 && CONST_INT_P (SUBREG_REG (oldval))));
690 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
691 && CONST_INT_P (XEXP (oldval, 0))));
694 if (undobuf.frees)
695 buf = undobuf.frees, undobuf.frees = buf->next;
696 else
697 buf = XNEW (struct undo);
699 buf->kind = UNDO_RTX;
700 buf->where.r = into;
701 buf->old_contents.r = oldval;
702 *into = newval;
704 buf->next = undobuf.undos, undobuf.undos = buf;
707 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
709 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
710 for the value of a HOST_WIDE_INT value (including CONST_INT) is
711 not safe. */
713 static void
714 do_SUBST_INT (int *into, int newval)
716 struct undo *buf;
717 int oldval = *into;
719 if (oldval == newval)
720 return;
722 if (undobuf.frees)
723 buf = undobuf.frees, undobuf.frees = buf->next;
724 else
725 buf = XNEW (struct undo);
727 buf->kind = UNDO_INT;
728 buf->where.i = into;
729 buf->old_contents.i = oldval;
730 *into = newval;
732 buf->next = undobuf.undos, undobuf.undos = buf;
735 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
737 /* Similar to SUBST, but just substitute the mode. This is used when
738 changing the mode of a pseudo-register, so that any other
739 references to the entry in the regno_reg_rtx array will change as
740 well. */
742 static void
743 do_SUBST_MODE (rtx *into, enum machine_mode newval)
745 struct undo *buf;
746 enum machine_mode oldval = GET_MODE (*into);
748 if (oldval == newval)
749 return;
751 if (undobuf.frees)
752 buf = undobuf.frees, undobuf.frees = buf->next;
753 else
754 buf = XNEW (struct undo);
756 buf->kind = UNDO_MODE;
757 buf->where.r = into;
758 buf->old_contents.m = oldval;
759 adjust_reg_mode (*into, newval);
761 buf->next = undobuf.undos, undobuf.undos = buf;
764 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
766 /* Subroutine of try_combine. Determine whether the combine replacement
767 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
768 insn_rtx_cost that the original instruction sequence I1, I2, I3 and
769 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
770 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
771 function returns false, if the costs of all instructions can be
772 estimated, and the replacements are more expensive than the original
773 sequence. */
775 static bool
776 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
777 rtx newotherpat)
779 int i1_cost, i2_cost, i3_cost;
780 int new_i2_cost, new_i3_cost;
781 int old_cost, new_cost;
783 /* Lookup the original insn_rtx_costs. */
784 i2_cost = INSN_COST (i2);
785 i3_cost = INSN_COST (i3);
787 if (i1)
789 i1_cost = INSN_COST (i1);
790 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
791 ? i1_cost + i2_cost + i3_cost : 0;
793 else
795 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
796 i1_cost = 0;
799 /* Calculate the replacement insn_rtx_costs. */
800 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
801 if (newi2pat)
803 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
804 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
805 ? new_i2_cost + new_i3_cost : 0;
807 else
809 new_cost = new_i3_cost;
810 new_i2_cost = 0;
813 if (undobuf.other_insn)
815 int old_other_cost, new_other_cost;
817 old_other_cost = INSN_COST (undobuf.other_insn);
818 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
819 if (old_other_cost > 0 && new_other_cost > 0)
821 old_cost += old_other_cost;
822 new_cost += new_other_cost;
824 else
825 old_cost = 0;
828 /* Disallow this recombination if both new_cost and old_cost are
829 greater than zero, and new_cost is greater than old cost. */
830 if (old_cost > 0
831 && new_cost > old_cost)
833 if (dump_file)
835 if (i1)
837 fprintf (dump_file,
838 "rejecting combination of insns %d, %d and %d\n",
839 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
840 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
841 i1_cost, i2_cost, i3_cost, old_cost);
843 else
845 fprintf (dump_file,
846 "rejecting combination of insns %d and %d\n",
847 INSN_UID (i2), INSN_UID (i3));
848 fprintf (dump_file, "original costs %d + %d = %d\n",
849 i2_cost, i3_cost, old_cost);
852 if (newi2pat)
854 fprintf (dump_file, "replacement costs %d + %d = %d\n",
855 new_i2_cost, new_i3_cost, new_cost);
857 else
858 fprintf (dump_file, "replacement cost %d\n", new_cost);
861 return false;
864 /* Update the uid_insn_cost array with the replacement costs. */
865 INSN_COST (i2) = new_i2_cost;
866 INSN_COST (i3) = new_i3_cost;
867 if (i1)
868 INSN_COST (i1) = 0;
870 return true;
874 /* Delete any insns that copy a register to itself. */
876 static void
877 delete_noop_moves (void)
879 rtx insn, next;
880 basic_block bb;
882 FOR_EACH_BB (bb)
884 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
886 next = NEXT_INSN (insn);
887 if (INSN_P (insn) && noop_move_p (insn))
889 if (dump_file)
890 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
892 delete_insn_and_edges (insn);
899 /* Fill in log links field for all insns. */
901 static void
902 create_log_links (void)
904 basic_block bb;
905 rtx *next_use, insn;
906 df_ref *def_vec, *use_vec;
908 next_use = XCNEWVEC (rtx, max_reg_num ());
910 /* Pass through each block from the end, recording the uses of each
911 register and establishing log links when def is encountered.
912 Note that we do not clear next_use array in order to save time,
913 so we have to test whether the use is in the same basic block as def.
915 There are a few cases below when we do not consider the definition or
916 usage -- these are taken from original flow.c did. Don't ask me why it is
917 done this way; I don't know and if it works, I don't want to know. */
919 FOR_EACH_BB (bb)
921 FOR_BB_INSNS_REVERSE (bb, insn)
923 if (!NONDEBUG_INSN_P (insn))
924 continue;
926 /* Log links are created only once. */
927 gcc_assert (!LOG_LINKS (insn));
929 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
931 df_ref def = *def_vec;
932 int regno = DF_REF_REGNO (def);
933 rtx use_insn;
935 if (!next_use[regno])
936 continue;
938 /* Do not consider if it is pre/post modification in MEM. */
939 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
940 continue;
942 /* Do not make the log link for frame pointer. */
943 if ((regno == FRAME_POINTER_REGNUM
944 && (! reload_completed || frame_pointer_needed))
945 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
946 || (regno == HARD_FRAME_POINTER_REGNUM
947 && (! reload_completed || frame_pointer_needed))
948 #endif
949 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
950 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
951 #endif
953 continue;
955 use_insn = next_use[regno];
956 if (BLOCK_FOR_INSN (use_insn) == bb)
958 /* flow.c claimed:
960 We don't build a LOG_LINK for hard registers contained
961 in ASM_OPERANDs. If these registers get replaced,
962 we might wind up changing the semantics of the insn,
963 even if reload can make what appear to be valid
964 assignments later. */
965 if (regno >= FIRST_PSEUDO_REGISTER
966 || asm_noperands (PATTERN (use_insn)) < 0)
968 /* Don't add duplicate links between instructions. */
969 rtx links;
970 for (links = LOG_LINKS (use_insn); links;
971 links = XEXP (links, 1))
972 if (insn == XEXP (links, 0))
973 break;
975 if (!links)
976 LOG_LINKS (use_insn) =
977 alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
980 next_use[regno] = NULL_RTX;
983 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
985 df_ref use = *use_vec;
986 int regno = DF_REF_REGNO (use);
988 /* Do not consider the usage of the stack pointer
989 by function call. */
990 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
991 continue;
993 next_use[regno] = insn;
998 free (next_use);
1001 /* Clear LOG_LINKS fields of insns. */
1003 static void
1004 clear_log_links (void)
1006 rtx insn;
1008 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1009 if (INSN_P (insn))
1010 free_INSN_LIST_list (&LOG_LINKS (insn));
1013 /* Main entry point for combiner. F is the first insn of the function.
1014 NREGS is the first unused pseudo-reg number.
1016 Return nonzero if the combiner has turned an indirect jump
1017 instruction into a direct jump. */
1018 static int
1019 combine_instructions (rtx f, unsigned int nregs)
1021 rtx insn, next;
1022 #ifdef HAVE_cc0
1023 rtx prev;
1024 #endif
1025 rtx links, nextlinks;
1026 rtx first;
1027 basic_block last_bb;
1029 int new_direct_jump_p = 0;
1031 for (first = f; first && !INSN_P (first); )
1032 first = NEXT_INSN (first);
1033 if (!first)
1034 return 0;
1036 combine_attempts = 0;
1037 combine_merges = 0;
1038 combine_extras = 0;
1039 combine_successes = 0;
1041 rtl_hooks = combine_rtl_hooks;
1043 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1045 init_recog_no_volatile ();
1047 /* Allocate array for insn info. */
1048 max_uid_known = get_max_uid ();
1049 uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1050 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1052 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1054 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1055 problems when, for example, we have j <<= 1 in a loop. */
1057 nonzero_sign_valid = 0;
1058 label_tick = label_tick_ebb_start = 1;
1060 /* Scan all SETs and see if we can deduce anything about what
1061 bits are known to be zero for some registers and how many copies
1062 of the sign bit are known to exist for those registers.
1064 Also set any known values so that we can use it while searching
1065 for what bits are known to be set. */
1067 setup_incoming_promotions (first);
1068 /* Allow the entry block and the first block to fall into the same EBB.
1069 Conceptually the incoming promotions are assigned to the entry block. */
1070 last_bb = ENTRY_BLOCK_PTR;
1072 create_log_links ();
1073 FOR_EACH_BB (this_basic_block)
1075 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1076 last_call_luid = 0;
1077 mem_last_set = -1;
1079 label_tick++;
1080 if (!single_pred_p (this_basic_block)
1081 || single_pred (this_basic_block) != last_bb)
1082 label_tick_ebb_start = label_tick;
1083 last_bb = this_basic_block;
1085 FOR_BB_INSNS (this_basic_block, insn)
1086 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1088 subst_low_luid = DF_INSN_LUID (insn);
1089 subst_insn = insn;
1091 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1092 insn);
1093 record_dead_and_set_regs (insn);
1095 #ifdef AUTO_INC_DEC
1096 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1097 if (REG_NOTE_KIND (links) == REG_INC)
1098 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1099 insn);
1100 #endif
1102 /* Record the current insn_rtx_cost of this instruction. */
1103 if (NONJUMP_INSN_P (insn))
1104 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1105 optimize_this_for_speed_p);
1106 if (dump_file)
1107 fprintf(dump_file, "insn_cost %d: %d\n",
1108 INSN_UID (insn), INSN_COST (insn));
1112 nonzero_sign_valid = 1;
1114 /* Now scan all the insns in forward order. */
1115 label_tick = label_tick_ebb_start = 1;
1116 init_reg_last ();
1117 setup_incoming_promotions (first);
1118 last_bb = ENTRY_BLOCK_PTR;
1120 FOR_EACH_BB (this_basic_block)
1122 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1123 last_call_luid = 0;
1124 mem_last_set = -1;
1126 label_tick++;
1127 if (!single_pred_p (this_basic_block)
1128 || single_pred (this_basic_block) != last_bb)
1129 label_tick_ebb_start = label_tick;
1130 last_bb = this_basic_block;
1132 rtl_profile_for_bb (this_basic_block);
1133 for (insn = BB_HEAD (this_basic_block);
1134 insn != NEXT_INSN (BB_END (this_basic_block));
1135 insn = next ? next : NEXT_INSN (insn))
1137 next = 0;
1138 if (NONDEBUG_INSN_P (insn))
1140 /* See if we know about function return values before this
1141 insn based upon SUBREG flags. */
1142 check_promoted_subreg (insn, PATTERN (insn));
1144 /* See if we can find hardregs and subreg of pseudos in
1145 narrower modes. This could help turning TRUNCATEs
1146 into SUBREGs. */
1147 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1149 /* Try this insn with each insn it links back to. */
1151 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1152 if ((next = try_combine (insn, XEXP (links, 0),
1153 NULL_RTX, &new_direct_jump_p)) != 0)
1154 goto retry;
1156 /* Try each sequence of three linked insns ending with this one. */
1158 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1160 rtx link = XEXP (links, 0);
1162 /* If the linked insn has been replaced by a note, then there
1163 is no point in pursuing this chain any further. */
1164 if (NOTE_P (link))
1165 continue;
1167 for (nextlinks = LOG_LINKS (link);
1168 nextlinks;
1169 nextlinks = XEXP (nextlinks, 1))
1170 if ((next = try_combine (insn, link,
1171 XEXP (nextlinks, 0),
1172 &new_direct_jump_p)) != 0)
1173 goto retry;
1176 #ifdef HAVE_cc0
1177 /* Try to combine a jump insn that uses CC0
1178 with a preceding insn that sets CC0, and maybe with its
1179 logical predecessor as well.
1180 This is how we make decrement-and-branch insns.
1181 We need this special code because data flow connections
1182 via CC0 do not get entered in LOG_LINKS. */
1184 if (JUMP_P (insn)
1185 && (prev = prev_nonnote_insn (insn)) != 0
1186 && NONJUMP_INSN_P (prev)
1187 && sets_cc0_p (PATTERN (prev)))
1189 if ((next = try_combine (insn, prev,
1190 NULL_RTX, &new_direct_jump_p)) != 0)
1191 goto retry;
1193 for (nextlinks = LOG_LINKS (prev); nextlinks;
1194 nextlinks = XEXP (nextlinks, 1))
1195 if ((next = try_combine (insn, prev,
1196 XEXP (nextlinks, 0),
1197 &new_direct_jump_p)) != 0)
1198 goto retry;
1201 /* Do the same for an insn that explicitly references CC0. */
1202 if (NONJUMP_INSN_P (insn)
1203 && (prev = prev_nonnote_insn (insn)) != 0
1204 && NONJUMP_INSN_P (prev)
1205 && sets_cc0_p (PATTERN (prev))
1206 && GET_CODE (PATTERN (insn)) == SET
1207 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1209 if ((next = try_combine (insn, prev,
1210 NULL_RTX, &new_direct_jump_p)) != 0)
1211 goto retry;
1213 for (nextlinks = LOG_LINKS (prev); nextlinks;
1214 nextlinks = XEXP (nextlinks, 1))
1215 if ((next = try_combine (insn, prev,
1216 XEXP (nextlinks, 0),
1217 &new_direct_jump_p)) != 0)
1218 goto retry;
1221 /* Finally, see if any of the insns that this insn links to
1222 explicitly references CC0. If so, try this insn, that insn,
1223 and its predecessor if it sets CC0. */
1224 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1225 if (NONJUMP_INSN_P (XEXP (links, 0))
1226 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1227 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1228 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1229 && NONJUMP_INSN_P (prev)
1230 && sets_cc0_p (PATTERN (prev))
1231 && (next = try_combine (insn, XEXP (links, 0),
1232 prev, &new_direct_jump_p)) != 0)
1233 goto retry;
1234 #endif
1236 /* Try combining an insn with two different insns whose results it
1237 uses. */
1238 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1239 for (nextlinks = XEXP (links, 1); nextlinks;
1240 nextlinks = XEXP (nextlinks, 1))
1241 if ((next = try_combine (insn, XEXP (links, 0),
1242 XEXP (nextlinks, 0),
1243 &new_direct_jump_p)) != 0)
1244 goto retry;
1246 /* Try this insn with each REG_EQUAL note it links back to. */
1247 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1249 rtx set, note;
1250 rtx temp = XEXP (links, 0);
1251 if ((set = single_set (temp)) != 0
1252 && (note = find_reg_equal_equiv_note (temp)) != 0
1253 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1254 /* Avoid using a register that may already been marked
1255 dead by an earlier instruction. */
1256 && ! unmentioned_reg_p (note, SET_SRC (set))
1257 && (GET_MODE (note) == VOIDmode
1258 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1259 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1261 /* Temporarily replace the set's source with the
1262 contents of the REG_EQUAL note. The insn will
1263 be deleted or recognized by try_combine. */
1264 rtx orig = SET_SRC (set);
1265 SET_SRC (set) = note;
1266 i2mod = temp;
1267 i2mod_old_rhs = copy_rtx (orig);
1268 i2mod_new_rhs = copy_rtx (note);
1269 next = try_combine (insn, i2mod, NULL_RTX,
1270 &new_direct_jump_p);
1271 i2mod = NULL_RTX;
1272 if (next)
1273 goto retry;
1274 SET_SRC (set) = orig;
1278 if (!NOTE_P (insn))
1279 record_dead_and_set_regs (insn);
1281 retry:
1287 default_rtl_profile ();
1288 clear_log_links ();
1289 clear_bb_flags ();
1290 new_direct_jump_p |= purge_all_dead_edges ();
1291 delete_noop_moves ();
1293 /* Clean up. */
1294 free (uid_log_links);
1295 free (uid_insn_cost);
1296 VEC_free (reg_stat_type, heap, reg_stat);
1299 struct undo *undo, *next;
1300 for (undo = undobuf.frees; undo; undo = next)
1302 next = undo->next;
1303 free (undo);
1305 undobuf.frees = 0;
1308 total_attempts += combine_attempts;
1309 total_merges += combine_merges;
1310 total_extras += combine_extras;
1311 total_successes += combine_successes;
1313 nonzero_sign_valid = 0;
1314 rtl_hooks = general_rtl_hooks;
1316 /* Make recognizer allow volatile MEMs again. */
1317 init_recog ();
1319 return new_direct_jump_p;
1322 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1324 static void
1325 init_reg_last (void)
1327 unsigned int i;
1328 reg_stat_type *p;
1330 for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
1331 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1334 /* Set up any promoted values for incoming argument registers. */
1336 static void
1337 setup_incoming_promotions (rtx first)
1339 tree arg;
1340 bool strictly_local = false;
1342 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1343 arg = TREE_CHAIN (arg))
1345 rtx x, reg = DECL_INCOMING_RTL (arg);
1346 int uns1, uns3;
1347 enum machine_mode mode1, mode2, mode3, mode4;
1349 /* Only continue if the incoming argument is in a register. */
1350 if (!REG_P (reg))
1351 continue;
1353 /* Determine, if possible, whether all call sites of the current
1354 function lie within the current compilation unit. (This does
1355 take into account the exporting of a function via taking its
1356 address, and so forth.) */
1357 strictly_local = cgraph_local_info (current_function_decl)->local;
1359 /* The mode and signedness of the argument before any promotions happen
1360 (equal to the mode of the pseudo holding it at that stage). */
1361 mode1 = TYPE_MODE (TREE_TYPE (arg));
1362 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1364 /* The mode and signedness of the argument after any source language and
1365 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1366 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1367 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1369 /* The mode and signedness of the argument as it is actually passed,
1370 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1371 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1372 TREE_TYPE (cfun->decl), 0);
1374 /* The mode of the register in which the argument is being passed. */
1375 mode4 = GET_MODE (reg);
1377 /* Eliminate sign extensions in the callee when:
1378 (a) A mode promotion has occurred; */
1379 if (mode1 == mode3)
1380 continue;
1381 /* (b) The mode of the register is the same as the mode of
1382 the argument as it is passed; */
1383 if (mode3 != mode4)
1384 continue;
1385 /* (c) There's no language level extension; */
1386 if (mode1 == mode2)
1388 /* (c.1) All callers are from the current compilation unit. If that's
1389 the case we don't have to rely on an ABI, we only have to know
1390 what we're generating right now, and we know that we will do the
1391 mode1 to mode2 promotion with the given sign. */
1392 else if (!strictly_local)
1393 continue;
1394 /* (c.2) The combination of the two promotions is useful. This is
1395 true when the signs match, or if the first promotion is unsigned.
1396 In the later case, (sign_extend (zero_extend x)) is the same as
1397 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1398 else if (uns1)
1399 uns3 = true;
1400 else if (uns3)
1401 continue;
1403 /* Record that the value was promoted from mode1 to mode3,
1404 so that any sign extension at the head of the current
1405 function may be eliminated. */
1406 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1407 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1408 record_value_for_reg (reg, first, x);
1412 /* Called via note_stores. If X is a pseudo that is narrower than
1413 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1415 If we are setting only a portion of X and we can't figure out what
1416 portion, assume all bits will be used since we don't know what will
1417 be happening.
1419 Similarly, set how many bits of X are known to be copies of the sign bit
1420 at all locations in the function. This is the smallest number implied
1421 by any set of X. */
1423 static void
1424 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1426 rtx insn = (rtx) data;
1427 unsigned int num;
1429 if (REG_P (x)
1430 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1431 /* If this register is undefined at the start of the file, we can't
1432 say what its contents were. */
1433 && ! REGNO_REG_SET_P
1434 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1435 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1437 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1439 if (set == 0 || GET_CODE (set) == CLOBBER)
1441 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1442 rsp->sign_bit_copies = 1;
1443 return;
1446 /* If this register is being initialized using itself, and the
1447 register is uninitialized in this basic block, and there are
1448 no LOG_LINKS which set the register, then part of the
1449 register is uninitialized. In that case we can't assume
1450 anything about the number of nonzero bits.
1452 ??? We could do better if we checked this in
1453 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1454 could avoid making assumptions about the insn which initially
1455 sets the register, while still using the information in other
1456 insns. We would have to be careful to check every insn
1457 involved in the combination. */
1459 if (insn
1460 && reg_referenced_p (x, PATTERN (insn))
1461 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1462 REGNO (x)))
1464 rtx link;
1466 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1468 if (dead_or_set_p (XEXP (link, 0), x))
1469 break;
1471 if (!link)
1473 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1474 rsp->sign_bit_copies = 1;
1475 return;
1479 /* If this is a complex assignment, see if we can convert it into a
1480 simple assignment. */
1481 set = expand_field_assignment (set);
1483 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1484 set what we know about X. */
1486 if (SET_DEST (set) == x
1487 || (GET_CODE (SET_DEST (set)) == SUBREG
1488 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1489 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1490 && SUBREG_REG (SET_DEST (set)) == x))
1492 rtx src = SET_SRC (set);
1494 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1495 /* If X is narrower than a word and SRC is a non-negative
1496 constant that would appear negative in the mode of X,
1497 sign-extend it for use in reg_stat[].nonzero_bits because some
1498 machines (maybe most) will actually do the sign-extension
1499 and this is the conservative approach.
1501 ??? For 2.5, try to tighten up the MD files in this regard
1502 instead of this kludge. */
1504 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1505 && CONST_INT_P (src)
1506 && INTVAL (src) > 0
1507 && 0 != (INTVAL (src)
1508 & ((HOST_WIDE_INT) 1
1509 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1510 src = GEN_INT (INTVAL (src)
1511 | ((HOST_WIDE_INT) (-1)
1512 << GET_MODE_BITSIZE (GET_MODE (x))));
1513 #endif
1515 /* Don't call nonzero_bits if it cannot change anything. */
1516 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1517 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1518 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1519 if (rsp->sign_bit_copies == 0
1520 || rsp->sign_bit_copies > num)
1521 rsp->sign_bit_copies = num;
1523 else
1525 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1526 rsp->sign_bit_copies = 1;
1531 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1532 insns that were previously combined into I3 or that will be combined
1533 into the merger of INSN and I3.
1535 Return 0 if the combination is not allowed for any reason.
1537 If the combination is allowed, *PDEST will be set to the single
1538 destination of INSN and *PSRC to the single source, and this function
1539 will return 1. */
1541 static int
1542 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1543 rtx *pdest, rtx *psrc)
1545 int i;
1546 const_rtx set = 0;
1547 rtx src, dest;
1548 rtx p;
1549 #ifdef AUTO_INC_DEC
1550 rtx link;
1551 #endif
1552 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1553 && next_active_insn (succ) == i3)
1554 : next_active_insn (insn) == i3);
1556 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1557 or a PARALLEL consisting of such a SET and CLOBBERs.
1559 If INSN has CLOBBER parallel parts, ignore them for our processing.
1560 By definition, these happen during the execution of the insn. When it
1561 is merged with another insn, all bets are off. If they are, in fact,
1562 needed and aren't also supplied in I3, they may be added by
1563 recog_for_combine. Otherwise, it won't match.
1565 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1566 note.
1568 Get the source and destination of INSN. If more than one, can't
1569 combine. */
1571 if (GET_CODE (PATTERN (insn)) == SET)
1572 set = PATTERN (insn);
1573 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1574 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1576 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1578 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1580 switch (GET_CODE (elt))
1582 /* This is important to combine floating point insns
1583 for the SH4 port. */
1584 case USE:
1585 /* Combining an isolated USE doesn't make sense.
1586 We depend here on combinable_i3pat to reject them. */
1587 /* The code below this loop only verifies that the inputs of
1588 the SET in INSN do not change. We call reg_set_between_p
1589 to verify that the REG in the USE does not change between
1590 I3 and INSN.
1591 If the USE in INSN was for a pseudo register, the matching
1592 insn pattern will likely match any register; combining this
1593 with any other USE would only be safe if we knew that the
1594 used registers have identical values, or if there was
1595 something to tell them apart, e.g. different modes. For
1596 now, we forgo such complicated tests and simply disallow
1597 combining of USES of pseudo registers with any other USE. */
1598 if (REG_P (XEXP (elt, 0))
1599 && GET_CODE (PATTERN (i3)) == PARALLEL)
1601 rtx i3pat = PATTERN (i3);
1602 int i = XVECLEN (i3pat, 0) - 1;
1603 unsigned int regno = REGNO (XEXP (elt, 0));
1607 rtx i3elt = XVECEXP (i3pat, 0, i);
1609 if (GET_CODE (i3elt) == USE
1610 && REG_P (XEXP (i3elt, 0))
1611 && (REGNO (XEXP (i3elt, 0)) == regno
1612 ? reg_set_between_p (XEXP (elt, 0),
1613 PREV_INSN (insn), i3)
1614 : regno >= FIRST_PSEUDO_REGISTER))
1615 return 0;
1617 while (--i >= 0);
1619 break;
1621 /* We can ignore CLOBBERs. */
1622 case CLOBBER:
1623 break;
1625 case SET:
1626 /* Ignore SETs whose result isn't used but not those that
1627 have side-effects. */
1628 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1629 && insn_nothrow_p (insn)
1630 && !side_effects_p (elt))
1631 break;
1633 /* If we have already found a SET, this is a second one and
1634 so we cannot combine with this insn. */
1635 if (set)
1636 return 0;
1638 set = elt;
1639 break;
1641 default:
1642 /* Anything else means we can't combine. */
1643 return 0;
1647 if (set == 0
1648 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1649 so don't do anything with it. */
1650 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1651 return 0;
1653 else
1654 return 0;
1656 if (set == 0)
1657 return 0;
1659 set = expand_field_assignment (set);
1660 src = SET_SRC (set), dest = SET_DEST (set);
1662 /* Don't eliminate a store in the stack pointer. */
1663 if (dest == stack_pointer_rtx
1664 /* Don't combine with an insn that sets a register to itself if it has
1665 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1666 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1667 /* Can't merge an ASM_OPERANDS. */
1668 || GET_CODE (src) == ASM_OPERANDS
1669 /* Can't merge a function call. */
1670 || GET_CODE (src) == CALL
1671 /* Don't eliminate a function call argument. */
1672 || (CALL_P (i3)
1673 && (find_reg_fusage (i3, USE, dest)
1674 || (REG_P (dest)
1675 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1676 && global_regs[REGNO (dest)])))
1677 /* Don't substitute into an incremented register. */
1678 || FIND_REG_INC_NOTE (i3, dest)
1679 || (succ && FIND_REG_INC_NOTE (succ, dest))
1680 /* Don't substitute into a non-local goto, this confuses CFG. */
1681 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1682 /* Make sure that DEST is not used after SUCC but before I3. */
1683 || (succ && ! all_adjacent
1684 && reg_used_between_p (dest, succ, i3))
1685 /* Make sure that the value that is to be substituted for the register
1686 does not use any registers whose values alter in between. However,
1687 If the insns are adjacent, a use can't cross a set even though we
1688 think it might (this can happen for a sequence of insns each setting
1689 the same destination; last_set of that register might point to
1690 a NOTE). If INSN has a REG_EQUIV note, the register is always
1691 equivalent to the memory so the substitution is valid even if there
1692 are intervening stores. Also, don't move a volatile asm or
1693 UNSPEC_VOLATILE across any other insns. */
1694 || (! all_adjacent
1695 && (((!MEM_P (src)
1696 || ! find_reg_note (insn, REG_EQUIV, src))
1697 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1698 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1699 || GET_CODE (src) == UNSPEC_VOLATILE))
1700 /* Don't combine across a CALL_INSN, because that would possibly
1701 change whether the life span of some REGs crosses calls or not,
1702 and it is a pain to update that information.
1703 Exception: if source is a constant, moving it later can't hurt.
1704 Accept that as a special case. */
1705 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1706 return 0;
1708 /* DEST must either be a REG or CC0. */
1709 if (REG_P (dest))
1711 /* If register alignment is being enforced for multi-word items in all
1712 cases except for parameters, it is possible to have a register copy
1713 insn referencing a hard register that is not allowed to contain the
1714 mode being copied and which would not be valid as an operand of most
1715 insns. Eliminate this problem by not combining with such an insn.
1717 Also, on some machines we don't want to extend the life of a hard
1718 register. */
1720 if (REG_P (src)
1721 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1722 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1723 /* Don't extend the life of a hard register unless it is
1724 user variable (if we have few registers) or it can't
1725 fit into the desired register (meaning something special
1726 is going on).
1727 Also avoid substituting a return register into I3, because
1728 reload can't handle a conflict with constraints of other
1729 inputs. */
1730 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1731 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1732 return 0;
1734 else if (GET_CODE (dest) != CC0)
1735 return 0;
1738 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1739 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1740 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1742 /* Don't substitute for a register intended as a clobberable
1743 operand. */
1744 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1745 if (rtx_equal_p (reg, dest))
1746 return 0;
1748 /* If the clobber represents an earlyclobber operand, we must not
1749 substitute an expression containing the clobbered register.
1750 As we do not analyze the constraint strings here, we have to
1751 make the conservative assumption. However, if the register is
1752 a fixed hard reg, the clobber cannot represent any operand;
1753 we leave it up to the machine description to either accept or
1754 reject use-and-clobber patterns. */
1755 if (!REG_P (reg)
1756 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1757 || !fixed_regs[REGNO (reg)])
1758 if (reg_overlap_mentioned_p (reg, src))
1759 return 0;
1762 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1763 or not), reject, unless nothing volatile comes between it and I3 */
1765 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1767 /* Make sure succ doesn't contain a volatile reference. */
1768 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1769 return 0;
1771 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1772 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1773 return 0;
1776 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1777 to be an explicit register variable, and was chosen for a reason. */
1779 if (GET_CODE (src) == ASM_OPERANDS
1780 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1781 return 0;
1783 /* If there are any volatile insns between INSN and I3, reject, because
1784 they might affect machine state. */
1786 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1787 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1788 return 0;
1790 /* If INSN contains an autoincrement or autodecrement, make sure that
1791 register is not used between there and I3, and not already used in
1792 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1793 Also insist that I3 not be a jump; if it were one
1794 and the incremented register were spilled, we would lose. */
1796 #ifdef AUTO_INC_DEC
1797 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1798 if (REG_NOTE_KIND (link) == REG_INC
1799 && (JUMP_P (i3)
1800 || reg_used_between_p (XEXP (link, 0), insn, i3)
1801 || (pred != NULL_RTX
1802 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1803 || (succ != NULL_RTX
1804 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1805 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1806 return 0;
1807 #endif
1809 #ifdef HAVE_cc0
1810 /* Don't combine an insn that follows a CC0-setting insn.
1811 An insn that uses CC0 must not be separated from the one that sets it.
1812 We do, however, allow I2 to follow a CC0-setting insn if that insn
1813 is passed as I1; in that case it will be deleted also.
1814 We also allow combining in this case if all the insns are adjacent
1815 because that would leave the two CC0 insns adjacent as well.
1816 It would be more logical to test whether CC0 occurs inside I1 or I2,
1817 but that would be much slower, and this ought to be equivalent. */
1819 p = prev_nonnote_insn (insn);
1820 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1821 && ! all_adjacent)
1822 return 0;
1823 #endif
1825 /* If we get here, we have passed all the tests and the combination is
1826 to be allowed. */
1828 *pdest = dest;
1829 *psrc = src;
1831 return 1;
1834 /* LOC is the location within I3 that contains its pattern or the component
1835 of a PARALLEL of the pattern. We validate that it is valid for combining.
1837 One problem is if I3 modifies its output, as opposed to replacing it
1838 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1839 so would produce an insn that is not equivalent to the original insns.
1841 Consider:
1843 (set (reg:DI 101) (reg:DI 100))
1844 (set (subreg:SI (reg:DI 101) 0) <foo>)
1846 This is NOT equivalent to:
1848 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1849 (set (reg:DI 101) (reg:DI 100))])
1851 Not only does this modify 100 (in which case it might still be valid
1852 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1854 We can also run into a problem if I2 sets a register that I1
1855 uses and I1 gets directly substituted into I3 (not via I2). In that
1856 case, we would be getting the wrong value of I2DEST into I3, so we
1857 must reject the combination. This case occurs when I2 and I1 both
1858 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1859 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1860 of a SET must prevent combination from occurring.
1862 Before doing the above check, we first try to expand a field assignment
1863 into a set of logical operations.
1865 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1866 we place a register that is both set and used within I3. If more than one
1867 such register is detected, we fail.
1869 Return 1 if the combination is valid, zero otherwise. */
1871 static int
1872 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1873 int i1_not_in_src, rtx *pi3dest_killed)
1875 rtx x = *loc;
1877 if (GET_CODE (x) == SET)
1879 rtx set = x ;
1880 rtx dest = SET_DEST (set);
1881 rtx src = SET_SRC (set);
1882 rtx inner_dest = dest;
1883 rtx subdest;
1885 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1886 || GET_CODE (inner_dest) == SUBREG
1887 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1888 inner_dest = XEXP (inner_dest, 0);
1890 /* Check for the case where I3 modifies its output, as discussed
1891 above. We don't want to prevent pseudos from being combined
1892 into the address of a MEM, so only prevent the combination if
1893 i1 or i2 set the same MEM. */
1894 if ((inner_dest != dest &&
1895 (!MEM_P (inner_dest)
1896 || rtx_equal_p (i2dest, inner_dest)
1897 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1898 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1899 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1901 /* This is the same test done in can_combine_p except we can't test
1902 all_adjacent; we don't have to, since this instruction will stay
1903 in place, thus we are not considering increasing the lifetime of
1904 INNER_DEST.
1906 Also, if this insn sets a function argument, combining it with
1907 something that might need a spill could clobber a previous
1908 function argument; the all_adjacent test in can_combine_p also
1909 checks this; here, we do a more specific test for this case. */
1911 || (REG_P (inner_dest)
1912 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1913 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1914 GET_MODE (inner_dest))))
1915 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1916 return 0;
1918 /* If DEST is used in I3, it is being killed in this insn, so
1919 record that for later. We have to consider paradoxical
1920 subregs here, since they kill the whole register, but we
1921 ignore partial subregs, STRICT_LOW_PART, etc.
1922 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1923 STACK_POINTER_REGNUM, since these are always considered to be
1924 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1925 subdest = dest;
1926 if (GET_CODE (subdest) == SUBREG
1927 && (GET_MODE_SIZE (GET_MODE (subdest))
1928 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1929 subdest = SUBREG_REG (subdest);
1930 if (pi3dest_killed
1931 && REG_P (subdest)
1932 && reg_referenced_p (subdest, PATTERN (i3))
1933 && REGNO (subdest) != FRAME_POINTER_REGNUM
1934 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1935 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1936 #endif
1937 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1938 && (REGNO (subdest) != ARG_POINTER_REGNUM
1939 || ! fixed_regs [REGNO (subdest)])
1940 #endif
1941 && REGNO (subdest) != STACK_POINTER_REGNUM)
1943 if (*pi3dest_killed)
1944 return 0;
1946 *pi3dest_killed = subdest;
1950 else if (GET_CODE (x) == PARALLEL)
1952 int i;
1954 for (i = 0; i < XVECLEN (x, 0); i++)
1955 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1956 i1_not_in_src, pi3dest_killed))
1957 return 0;
1960 return 1;
1963 /* Return 1 if X is an arithmetic expression that contains a multiplication
1964 and division. We don't count multiplications by powers of two here. */
1966 static int
1967 contains_muldiv (rtx x)
1969 switch (GET_CODE (x))
1971 case MOD: case DIV: case UMOD: case UDIV:
1972 return 1;
1974 case MULT:
1975 return ! (CONST_INT_P (XEXP (x, 1))
1976 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1977 default:
1978 if (BINARY_P (x))
1979 return contains_muldiv (XEXP (x, 0))
1980 || contains_muldiv (XEXP (x, 1));
1982 if (UNARY_P (x))
1983 return contains_muldiv (XEXP (x, 0));
1985 return 0;
1989 /* Determine whether INSN can be used in a combination. Return nonzero if
1990 not. This is used in try_combine to detect early some cases where we
1991 can't perform combinations. */
1993 static int
1994 cant_combine_insn_p (rtx insn)
1996 rtx set;
1997 rtx src, dest;
1999 /* If this isn't really an insn, we can't do anything.
2000 This can occur when flow deletes an insn that it has merged into an
2001 auto-increment address. */
2002 if (! INSN_P (insn))
2003 return 1;
2005 /* Never combine loads and stores involving hard regs that are likely
2006 to be spilled. The register allocator can usually handle such
2007 reg-reg moves by tying. If we allow the combiner to make
2008 substitutions of likely-spilled regs, reload might die.
2009 As an exception, we allow combinations involving fixed regs; these are
2010 not available to the register allocator so there's no risk involved. */
2012 set = single_set (insn);
2013 if (! set)
2014 return 0;
2015 src = SET_SRC (set);
2016 dest = SET_DEST (set);
2017 if (GET_CODE (src) == SUBREG)
2018 src = SUBREG_REG (src);
2019 if (GET_CODE (dest) == SUBREG)
2020 dest = SUBREG_REG (dest);
2021 if (REG_P (src) && REG_P (dest)
2022 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
2023 && ! fixed_regs[REGNO (src)]
2024 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
2025 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
2026 && ! fixed_regs[REGNO (dest)]
2027 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
2028 return 1;
2030 return 0;
2033 struct likely_spilled_retval_info
2035 unsigned regno, nregs;
2036 unsigned mask;
2039 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2040 hard registers that are known to be written to / clobbered in full. */
2041 static void
2042 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2044 struct likely_spilled_retval_info *const info =
2045 (struct likely_spilled_retval_info *) data;
2046 unsigned regno, nregs;
2047 unsigned new_mask;
2049 if (!REG_P (XEXP (set, 0)))
2050 return;
2051 regno = REGNO (x);
2052 if (regno >= info->regno + info->nregs)
2053 return;
2054 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2055 if (regno + nregs <= info->regno)
2056 return;
2057 new_mask = (2U << (nregs - 1)) - 1;
2058 if (regno < info->regno)
2059 new_mask >>= info->regno - regno;
2060 else
2061 new_mask <<= regno - info->regno;
2062 info->mask &= ~new_mask;
2065 /* Return nonzero iff part of the return value is live during INSN, and
2066 it is likely spilled. This can happen when more than one insn is needed
2067 to copy the return value, e.g. when we consider to combine into the
2068 second copy insn for a complex value. */
2070 static int
2071 likely_spilled_retval_p (rtx insn)
2073 rtx use = BB_END (this_basic_block);
2074 rtx reg, p;
2075 unsigned regno, nregs;
2076 /* We assume here that no machine mode needs more than
2077 32 hard registers when the value overlaps with a register
2078 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2079 unsigned mask;
2080 struct likely_spilled_retval_info info;
2082 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2083 return 0;
2084 reg = XEXP (PATTERN (use), 0);
2085 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2086 return 0;
2087 regno = REGNO (reg);
2088 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2089 if (nregs == 1)
2090 return 0;
2091 mask = (2U << (nregs - 1)) - 1;
2093 /* Disregard parts of the return value that are set later. */
2094 info.regno = regno;
2095 info.nregs = nregs;
2096 info.mask = mask;
2097 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2098 if (INSN_P (p))
2099 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2100 mask = info.mask;
2102 /* Check if any of the (probably) live return value registers is
2103 likely spilled. */
2104 nregs --;
2107 if ((mask & 1 << nregs)
2108 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2109 return 1;
2110 } while (nregs--);
2111 return 0;
2114 /* Adjust INSN after we made a change to its destination.
2116 Changing the destination can invalidate notes that say something about
2117 the results of the insn and a LOG_LINK pointing to the insn. */
2119 static void
2120 adjust_for_new_dest (rtx insn)
2122 /* For notes, be conservative and simply remove them. */
2123 remove_reg_equal_equiv_notes (insn);
2125 /* The new insn will have a destination that was previously the destination
2126 of an insn just above it. Call distribute_links to make a LOG_LINK from
2127 the next use of that destination. */
2128 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2130 df_insn_rescan (insn);
2133 /* Return TRUE if combine can reuse reg X in mode MODE.
2134 ADDED_SETS is nonzero if the original set is still required. */
2135 static bool
2136 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2138 unsigned int regno;
2140 if (!REG_P(x))
2141 return false;
2143 regno = REGNO (x);
2144 /* Allow hard registers if the new mode is legal, and occupies no more
2145 registers than the old mode. */
2146 if (regno < FIRST_PSEUDO_REGISTER)
2147 return (HARD_REGNO_MODE_OK (regno, mode)
2148 && (hard_regno_nregs[regno][GET_MODE (x)]
2149 >= hard_regno_nregs[regno][mode]));
2151 /* Or a pseudo that is only used once. */
2152 return (REG_N_SETS (regno) == 1 && !added_sets
2153 && !REG_USERVAR_P (x));
2157 /* Check whether X, the destination of a set, refers to part of
2158 the register specified by REG. */
2160 static bool
2161 reg_subword_p (rtx x, rtx reg)
2163 /* Check that reg is an integer mode register. */
2164 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2165 return false;
2167 if (GET_CODE (x) == STRICT_LOW_PART
2168 || GET_CODE (x) == ZERO_EXTRACT)
2169 x = XEXP (x, 0);
2171 return GET_CODE (x) == SUBREG
2172 && SUBREG_REG (x) == reg
2173 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2176 #ifdef AUTO_INC_DEC
2177 /* Replace auto-increment addressing modes with explicit operations to
2178 access the same addresses without modifying the corresponding
2179 registers. If AFTER holds, SRC is meant to be reused after the
2180 side effect, otherwise it is to be reused before that. */
2182 static rtx
2183 cleanup_auto_inc_dec (rtx src, bool after, enum machine_mode mem_mode)
2185 rtx x = src;
2186 const RTX_CODE code = GET_CODE (x);
2187 int i;
2188 const char *fmt;
2190 switch (code)
2192 case REG:
2193 case CONST_INT:
2194 case CONST_DOUBLE:
2195 case CONST_FIXED:
2196 case CONST_VECTOR:
2197 case SYMBOL_REF:
2198 case CODE_LABEL:
2199 case PC:
2200 case CC0:
2201 case SCRATCH:
2202 /* SCRATCH must be shared because they represent distinct values. */
2203 return x;
2204 case CLOBBER:
2205 if (REG_P (XEXP (x, 0)) && REGNO (XEXP (x, 0)) < FIRST_PSEUDO_REGISTER)
2206 return x;
2207 break;
2209 case CONST:
2210 if (shared_const_p (x))
2211 return x;
2212 break;
2214 case MEM:
2215 mem_mode = GET_MODE (x);
2216 break;
2218 case PRE_INC:
2219 case PRE_DEC:
2220 case POST_INC:
2221 case POST_DEC:
2222 gcc_assert (mem_mode != VOIDmode && mem_mode != BLKmode);
2223 if (after == (code == PRE_INC || code == PRE_DEC))
2224 x = cleanup_auto_inc_dec (XEXP (x, 0), after, mem_mode);
2225 else
2226 x = gen_rtx_PLUS (GET_MODE (x),
2227 cleanup_auto_inc_dec (XEXP (x, 0), after, mem_mode),
2228 GEN_INT ((code == PRE_INC || code == POST_INC)
2229 ? GET_MODE_SIZE (mem_mode)
2230 : -GET_MODE_SIZE (mem_mode)));
2231 return x;
2233 case PRE_MODIFY:
2234 case POST_MODIFY:
2235 if (after == (code == PRE_MODIFY))
2236 x = XEXP (x, 0);
2237 else
2238 x = XEXP (x, 1);
2239 return cleanup_auto_inc_dec (x, after, mem_mode);
2241 default:
2242 break;
2245 /* Copy the various flags, fields, and other information. We assume
2246 that all fields need copying, and then clear the fields that should
2247 not be copied. That is the sensible default behavior, and forces
2248 us to explicitly document why we are *not* copying a flag. */
2249 x = shallow_copy_rtx (x);
2251 /* We do not copy the USED flag, which is used as a mark bit during
2252 walks over the RTL. */
2253 RTX_FLAG (x, used) = 0;
2255 /* We do not copy FRAME_RELATED for INSNs. */
2256 if (INSN_P (x))
2257 RTX_FLAG (x, frame_related) = 0;
2259 fmt = GET_RTX_FORMAT (code);
2260 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2261 if (fmt[i] == 'e')
2262 XEXP (x, i) = cleanup_auto_inc_dec (XEXP (x, i), after, mem_mode);
2263 else if (fmt[i] == 'E' || fmt[i] == 'V')
2265 int j;
2266 XVEC (x, i) = rtvec_alloc (XVECLEN (x, i));
2267 for (j = 0; j < XVECLEN (x, i); j++)
2268 XVECEXP (x, i, j)
2269 = cleanup_auto_inc_dec (XVECEXP (src, i, j), after, mem_mode);
2272 return x;
2274 #endif
2276 /* Auxiliary data structure for propagate_for_debug_stmt. */
2278 struct rtx_subst_pair
2280 rtx to;
2281 bool adjusted;
2282 bool after;
2285 /* DATA points to an rtx_subst_pair. Return the value that should be
2286 substituted. */
2288 static rtx
2289 propagate_for_debug_subst (rtx from, const_rtx old_rtx, void *data)
2291 struct rtx_subst_pair *pair = (struct rtx_subst_pair *)data;
2293 if (!rtx_equal_p (from, old_rtx))
2294 return NULL_RTX;
2295 if (!pair->adjusted)
2297 pair->adjusted = true;
2298 #ifdef AUTO_INC_DEC
2299 pair->to = cleanup_auto_inc_dec (pair->to, pair->after, VOIDmode);
2300 #else
2301 pair->to = copy_rtx (pair->to);
2302 #endif
2303 pair->to = make_compound_operation (pair->to, SET);
2304 return pair->to;
2306 return copy_rtx (pair->to);
2309 /* Replace occurrences of DEST with SRC in DEBUG_INSNs between INSN
2310 and LAST. If MOVE holds, debug insns must also be moved past
2311 LAST. */
2313 static void
2314 propagate_for_debug (rtx insn, rtx last, rtx dest, rtx src, bool move)
2316 rtx next, move_pos = move ? last : NULL_RTX, loc;
2318 struct rtx_subst_pair p;
2319 p.to = src;
2320 p.adjusted = false;
2321 p.after = move;
2323 next = NEXT_INSN (insn);
2324 while (next != last)
2326 insn = next;
2327 next = NEXT_INSN (insn);
2328 if (DEBUG_INSN_P (insn))
2330 loc = simplify_replace_fn_rtx (INSN_VAR_LOCATION_LOC (insn),
2331 dest, propagate_for_debug_subst, &p);
2332 if (loc == INSN_VAR_LOCATION_LOC (insn))
2333 continue;
2334 INSN_VAR_LOCATION_LOC (insn) = loc;
2335 if (move_pos)
2337 remove_insn (insn);
2338 PREV_INSN (insn) = NEXT_INSN (insn) = NULL_RTX;
2339 move_pos = emit_debug_insn_after (insn, move_pos);
2341 else
2342 df_insn_rescan (insn);
2347 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2348 Note that the INSN should be deleted *after* removing dead edges, so
2349 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2350 but not for a (set (pc) (label_ref FOO)). */
2352 static void
2353 update_cfg_for_uncondjump (rtx insn)
2355 basic_block bb = BLOCK_FOR_INSN (insn);
2356 bool at_end = (BB_END (bb) == insn);
2358 if (at_end)
2359 purge_dead_edges (bb);
2361 delete_insn (insn);
2362 if (at_end && EDGE_COUNT (bb->succs) == 1)
2363 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2367 /* Try to combine the insns I1 and I2 into I3.
2368 Here I1 and I2 appear earlier than I3.
2369 I1 can be zero; then we combine just I2 into I3.
2371 If we are combining three insns and the resulting insn is not recognized,
2372 try splitting it into two insns. If that happens, I2 and I3 are retained
2373 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
2374 are pseudo-deleted.
2376 Return 0 if the combination does not work. Then nothing is changed.
2377 If we did the combination, return the insn at which combine should
2378 resume scanning.
2380 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2381 new direct jump instruction. */
2383 static rtx
2384 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2386 /* New patterns for I3 and I2, respectively. */
2387 rtx newpat, newi2pat = 0;
2388 rtvec newpat_vec_with_clobbers = 0;
2389 int substed_i2 = 0, substed_i1 = 0;
2390 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
2391 int added_sets_1, added_sets_2;
2392 /* Total number of SETs to put into I3. */
2393 int total_sets;
2394 /* Nonzero if I2's body now appears in I3. */
2395 int i2_is_used;
2396 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2397 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2398 /* Contains I3 if the destination of I3 is used in its source, which means
2399 that the old life of I3 is being killed. If that usage is placed into
2400 I2 and not in I3, a REG_DEAD note must be made. */
2401 rtx i3dest_killed = 0;
2402 /* SET_DEST and SET_SRC of I2 and I1. */
2403 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0;
2404 /* Set if I2DEST was reused as a scratch register. */
2405 bool i2scratch = false;
2406 /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases. */
2407 rtx i1pat = 0, i2pat = 0;
2408 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2409 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2410 int i2dest_killed = 0, i1dest_killed = 0;
2411 int i1_feeds_i3 = 0;
2412 /* Notes that must be added to REG_NOTES in I3 and I2. */
2413 rtx new_i3_notes, new_i2_notes;
2414 /* Notes that we substituted I3 into I2 instead of the normal case. */
2415 int i3_subst_into_i2 = 0;
2416 /* Notes that I1, I2 or I3 is a MULT operation. */
2417 int have_mult = 0;
2418 int swap_i2i3 = 0;
2419 int changed_i3_dest = 0;
2421 int maxreg;
2422 rtx temp;
2423 rtx link;
2424 rtx other_pat = 0;
2425 rtx new_other_notes;
2426 int i;
2428 /* Exit early if one of the insns involved can't be used for
2429 combinations. */
2430 if (cant_combine_insn_p (i3)
2431 || cant_combine_insn_p (i2)
2432 || (i1 && cant_combine_insn_p (i1))
2433 || likely_spilled_retval_p (i3))
2434 return 0;
2436 combine_attempts++;
2437 undobuf.other_insn = 0;
2439 /* Reset the hard register usage information. */
2440 CLEAR_HARD_REG_SET (newpat_used_regs);
2442 if (dump_file && (dump_flags & TDF_DETAILS))
2444 if (i1)
2445 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2446 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2447 else
2448 fprintf (dump_file, "\nTrying %d -> %d:\n",
2449 INSN_UID (i2), INSN_UID (i3));
2452 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
2453 code below, set I1 to be the earlier of the two insns. */
2454 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2455 temp = i1, i1 = i2, i2 = temp;
2457 added_links_insn = 0;
2459 /* First check for one important special-case that the code below will
2460 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2461 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2462 we may be able to replace that destination with the destination of I3.
2463 This occurs in the common code where we compute both a quotient and
2464 remainder into a structure, in which case we want to do the computation
2465 directly into the structure to avoid register-register copies.
2467 Note that this case handles both multiple sets in I2 and also
2468 cases where I2 has a number of CLOBBER or PARALLELs.
2470 We make very conservative checks below and only try to handle the
2471 most common cases of this. For example, we only handle the case
2472 where I2 and I3 are adjacent to avoid making difficult register
2473 usage tests. */
2475 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2476 && REG_P (SET_SRC (PATTERN (i3)))
2477 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2478 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2479 && GET_CODE (PATTERN (i2)) == PARALLEL
2480 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2481 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2482 below would need to check what is inside (and reg_overlap_mentioned_p
2483 doesn't support those codes anyway). Don't allow those destinations;
2484 the resulting insn isn't likely to be recognized anyway. */
2485 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2486 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2487 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2488 SET_DEST (PATTERN (i3)))
2489 && next_active_insn (i2) == i3)
2491 rtx p2 = PATTERN (i2);
2493 /* Make sure that the destination of I3,
2494 which we are going to substitute into one output of I2,
2495 is not used within another output of I2. We must avoid making this:
2496 (parallel [(set (mem (reg 69)) ...)
2497 (set (reg 69) ...)])
2498 which is not well-defined as to order of actions.
2499 (Besides, reload can't handle output reloads for this.)
2501 The problem can also happen if the dest of I3 is a memory ref,
2502 if another dest in I2 is an indirect memory ref. */
2503 for (i = 0; i < XVECLEN (p2, 0); i++)
2504 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2505 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2506 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2507 SET_DEST (XVECEXP (p2, 0, i))))
2508 break;
2510 if (i == XVECLEN (p2, 0))
2511 for (i = 0; i < XVECLEN (p2, 0); i++)
2512 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2513 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2514 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2516 combine_merges++;
2518 subst_insn = i3;
2519 subst_low_luid = DF_INSN_LUID (i2);
2521 added_sets_2 = added_sets_1 = 0;
2522 i2src = SET_DEST (PATTERN (i3));
2523 i2dest = SET_SRC (PATTERN (i3));
2524 i2dest_killed = dead_or_set_p (i2, i2dest);
2526 /* Replace the dest in I2 with our dest and make the resulting
2527 insn the new pattern for I3. Then skip to where we
2528 validate the pattern. Everything was set up above. */
2529 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2530 SET_DEST (PATTERN (i3)));
2532 newpat = p2;
2533 i3_subst_into_i2 = 1;
2534 goto validate_replacement;
2538 /* If I2 is setting a pseudo to a constant and I3 is setting some
2539 sub-part of it to another constant, merge them by making a new
2540 constant. */
2541 if (i1 == 0
2542 && (temp = single_set (i2)) != 0
2543 && (CONST_INT_P (SET_SRC (temp))
2544 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2545 && GET_CODE (PATTERN (i3)) == SET
2546 && (CONST_INT_P (SET_SRC (PATTERN (i3)))
2547 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2548 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2550 rtx dest = SET_DEST (PATTERN (i3));
2551 int offset = -1;
2552 int width = 0;
2554 if (GET_CODE (dest) == ZERO_EXTRACT)
2556 if (CONST_INT_P (XEXP (dest, 1))
2557 && CONST_INT_P (XEXP (dest, 2)))
2559 width = INTVAL (XEXP (dest, 1));
2560 offset = INTVAL (XEXP (dest, 2));
2561 dest = XEXP (dest, 0);
2562 if (BITS_BIG_ENDIAN)
2563 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2566 else
2568 if (GET_CODE (dest) == STRICT_LOW_PART)
2569 dest = XEXP (dest, 0);
2570 width = GET_MODE_BITSIZE (GET_MODE (dest));
2571 offset = 0;
2574 if (offset >= 0)
2576 /* If this is the low part, we're done. */
2577 if (subreg_lowpart_p (dest))
2579 /* Handle the case where inner is twice the size of outer. */
2580 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2581 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2582 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2583 /* Otherwise give up for now. */
2584 else
2585 offset = -1;
2588 if (offset >= 0
2589 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2590 <= HOST_BITS_PER_DOUBLE_INT))
2592 double_int m, o, i;
2593 rtx inner = SET_SRC (PATTERN (i3));
2594 rtx outer = SET_SRC (temp);
2596 o = rtx_to_double_int (outer);
2597 i = rtx_to_double_int (inner);
2599 m = double_int_mask (width);
2600 i = double_int_and (i, m);
2601 m = double_int_lshift (m, offset, HOST_BITS_PER_DOUBLE_INT, false);
2602 i = double_int_lshift (i, offset, HOST_BITS_PER_DOUBLE_INT, false);
2603 o = double_int_ior (double_int_and (o, double_int_not (m)), i);
2605 combine_merges++;
2606 subst_insn = i3;
2607 subst_low_luid = DF_INSN_LUID (i2);
2608 added_sets_2 = added_sets_1 = 0;
2609 i2dest = SET_DEST (temp);
2610 i2dest_killed = dead_or_set_p (i2, i2dest);
2612 /* Replace the source in I2 with the new constant and make the
2613 resulting insn the new pattern for I3. Then skip to where we
2614 validate the pattern. Everything was set up above. */
2615 SUBST (SET_SRC (temp),
2616 immed_double_int_const (o, GET_MODE (SET_DEST (temp))));
2618 newpat = PATTERN (i2);
2620 /* The dest of I3 has been replaced with the dest of I2. */
2621 changed_i3_dest = 1;
2622 goto validate_replacement;
2626 #ifndef HAVE_cc0
2627 /* If we have no I1 and I2 looks like:
2628 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2629 (set Y OP)])
2630 make up a dummy I1 that is
2631 (set Y OP)
2632 and change I2 to be
2633 (set (reg:CC X) (compare:CC Y (const_int 0)))
2635 (We can ignore any trailing CLOBBERs.)
2637 This undoes a previous combination and allows us to match a branch-and-
2638 decrement insn. */
2640 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2641 && XVECLEN (PATTERN (i2), 0) >= 2
2642 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2643 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2644 == MODE_CC)
2645 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2646 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2647 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2648 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2649 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2650 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2652 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2653 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2654 break;
2656 if (i == 1)
2658 /* We make I1 with the same INSN_UID as I2. This gives it
2659 the same DF_INSN_LUID for value tracking. Our fake I1 will
2660 never appear in the insn stream so giving it the same INSN_UID
2661 as I2 will not cause a problem. */
2663 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2664 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2665 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2667 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2668 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2669 SET_DEST (PATTERN (i1)));
2672 #endif
2674 /* Verify that I2 and I1 are valid for combining. */
2675 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2676 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2678 undo_all ();
2679 return 0;
2682 /* Record whether I2DEST is used in I2SRC and similarly for the other
2683 cases. Knowing this will help in register status updating below. */
2684 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2685 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2686 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2687 i2dest_killed = dead_or_set_p (i2, i2dest);
2688 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2690 /* See if I1 directly feeds into I3. It does if I1DEST is not used
2691 in I2SRC. */
2692 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2694 /* Ensure that I3's pattern can be the destination of combines. */
2695 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2696 i1 && i2dest_in_i1src && i1_feeds_i3,
2697 &i3dest_killed))
2699 undo_all ();
2700 return 0;
2703 /* See if any of the insns is a MULT operation. Unless one is, we will
2704 reject a combination that is, since it must be slower. Be conservative
2705 here. */
2706 if (GET_CODE (i2src) == MULT
2707 || (i1 != 0 && GET_CODE (i1src) == MULT)
2708 || (GET_CODE (PATTERN (i3)) == SET
2709 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2710 have_mult = 1;
2712 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2713 We used to do this EXCEPT in one case: I3 has a post-inc in an
2714 output operand. However, that exception can give rise to insns like
2715 mov r3,(r3)+
2716 which is a famous insn on the PDP-11 where the value of r3 used as the
2717 source was model-dependent. Avoid this sort of thing. */
2719 #if 0
2720 if (!(GET_CODE (PATTERN (i3)) == SET
2721 && REG_P (SET_SRC (PATTERN (i3)))
2722 && MEM_P (SET_DEST (PATTERN (i3)))
2723 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2724 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2725 /* It's not the exception. */
2726 #endif
2727 #ifdef AUTO_INC_DEC
2728 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2729 if (REG_NOTE_KIND (link) == REG_INC
2730 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2731 || (i1 != 0
2732 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2734 undo_all ();
2735 return 0;
2737 #endif
2739 /* See if the SETs in I1 or I2 need to be kept around in the merged
2740 instruction: whenever the value set there is still needed past I3.
2741 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2743 For the SET in I1, we have two cases: If I1 and I2 independently
2744 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2745 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2746 in I1 needs to be kept around unless I1DEST dies or is set in either
2747 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2748 I1DEST. If so, we know I1 feeds into I2. */
2750 added_sets_2 = ! dead_or_set_p (i3, i2dest);
2752 added_sets_1
2753 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2754 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2756 /* If the set in I2 needs to be kept around, we must make a copy of
2757 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2758 PATTERN (I2), we are only substituting for the original I1DEST, not into
2759 an already-substituted copy. This also prevents making self-referential
2760 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2761 I2DEST. */
2763 if (added_sets_2)
2765 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2766 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2767 else
2768 i2pat = copy_rtx (PATTERN (i2));
2771 if (added_sets_1)
2773 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2774 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2775 else
2776 i1pat = copy_rtx (PATTERN (i1));
2779 combine_merges++;
2781 /* Substitute in the latest insn for the regs set by the earlier ones. */
2783 maxreg = max_reg_num ();
2785 subst_insn = i3;
2787 #ifndef HAVE_cc0
2788 /* Many machines that don't use CC0 have insns that can both perform an
2789 arithmetic operation and set the condition code. These operations will
2790 be represented as a PARALLEL with the first element of the vector
2791 being a COMPARE of an arithmetic operation with the constant zero.
2792 The second element of the vector will set some pseudo to the result
2793 of the same arithmetic operation. If we simplify the COMPARE, we won't
2794 match such a pattern and so will generate an extra insn. Here we test
2795 for this case, where both the comparison and the operation result are
2796 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2797 I2SRC. Later we will make the PARALLEL that contains I2. */
2799 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2800 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2801 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2802 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2804 #ifdef SELECT_CC_MODE
2805 rtx *cc_use;
2806 enum machine_mode compare_mode;
2807 #endif
2809 newpat = PATTERN (i3);
2810 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2812 i2_is_used = 1;
2814 #ifdef SELECT_CC_MODE
2815 /* See if a COMPARE with the operand we substituted in should be done
2816 with the mode that is currently being used. If not, do the same
2817 processing we do in `subst' for a SET; namely, if the destination
2818 is used only once, try to replace it with a register of the proper
2819 mode and also replace the COMPARE. */
2820 if (undobuf.other_insn == 0
2821 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2822 &undobuf.other_insn))
2823 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2824 i2src, const0_rtx))
2825 != GET_MODE (SET_DEST (newpat))))
2827 if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2828 compare_mode))
2830 unsigned int regno = REGNO (SET_DEST (newpat));
2831 rtx new_dest;
2833 if (regno < FIRST_PSEUDO_REGISTER)
2834 new_dest = gen_rtx_REG (compare_mode, regno);
2835 else
2837 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2838 new_dest = regno_reg_rtx[regno];
2841 SUBST (SET_DEST (newpat), new_dest);
2842 SUBST (XEXP (*cc_use, 0), new_dest);
2843 SUBST (SET_SRC (newpat),
2844 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2846 else
2847 undobuf.other_insn = 0;
2849 #endif
2851 else
2852 #endif
2854 /* It is possible that the source of I2 or I1 may be performing
2855 an unneeded operation, such as a ZERO_EXTEND of something
2856 that is known to have the high part zero. Handle that case
2857 by letting subst look at the innermost one of them.
2859 Another way to do this would be to have a function that tries
2860 to simplify a single insn instead of merging two or more
2861 insns. We don't do this because of the potential of infinite
2862 loops and because of the potential extra memory required.
2863 However, doing it the way we are is a bit of a kludge and
2864 doesn't catch all cases.
2866 But only do this if -fexpensive-optimizations since it slows
2867 things down and doesn't usually win.
2869 This is not done in the COMPARE case above because the
2870 unmodified I2PAT is used in the PARALLEL and so a pattern
2871 with a modified I2SRC would not match. */
2873 if (flag_expensive_optimizations)
2875 /* Pass pc_rtx so no substitutions are done, just
2876 simplifications. */
2877 if (i1)
2879 subst_low_luid = DF_INSN_LUID (i1);
2880 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2882 else
2884 subst_low_luid = DF_INSN_LUID (i2);
2885 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2889 n_occurrences = 0; /* `subst' counts here */
2891 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2892 need to make a unique copy of I2SRC each time we substitute it
2893 to avoid self-referential rtl. */
2895 subst_low_luid = DF_INSN_LUID (i2);
2896 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2897 ! i1_feeds_i3 && i1dest_in_i1src);
2898 substed_i2 = 1;
2900 /* Record whether i2's body now appears within i3's body. */
2901 i2_is_used = n_occurrences;
2904 /* If we already got a failure, don't try to do more. Otherwise,
2905 try to substitute in I1 if we have it. */
2907 if (i1 && GET_CODE (newpat) != CLOBBER)
2909 /* Check that an autoincrement side-effect on I1 has not been lost.
2910 This happens if I1DEST is mentioned in I2 and dies there, and
2911 has disappeared from the new pattern. */
2912 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2913 && !i1_feeds_i3
2914 && dead_or_set_p (i2, i1dest)
2915 && !reg_overlap_mentioned_p (i1dest, newpat))
2916 /* Before we can do this substitution, we must redo the test done
2917 above (see detailed comments there) that ensures that I1DEST
2918 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2919 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, 0, 0))
2921 undo_all ();
2922 return 0;
2925 n_occurrences = 0;
2926 subst_low_luid = DF_INSN_LUID (i1);
2927 newpat = subst (newpat, i1dest, i1src, 0, 0);
2928 substed_i1 = 1;
2931 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2932 to count all the ways that I2SRC and I1SRC can be used. */
2933 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2934 && i2_is_used + added_sets_2 > 1)
2935 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2936 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2937 > 1))
2938 /* Fail if we tried to make a new register. */
2939 || max_reg_num () != maxreg
2940 /* Fail if we couldn't do something and have a CLOBBER. */
2941 || GET_CODE (newpat) == CLOBBER
2942 /* Fail if this new pattern is a MULT and we didn't have one before
2943 at the outer level. */
2944 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2945 && ! have_mult))
2947 undo_all ();
2948 return 0;
2951 /* If the actions of the earlier insns must be kept
2952 in addition to substituting them into the latest one,
2953 we must make a new PARALLEL for the latest insn
2954 to hold additional the SETs. */
2956 if (added_sets_1 || added_sets_2)
2958 combine_extras++;
2960 if (GET_CODE (newpat) == PARALLEL)
2962 rtvec old = XVEC (newpat, 0);
2963 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2964 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2965 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2966 sizeof (old->elem[0]) * old->num_elem);
2968 else
2970 rtx old = newpat;
2971 total_sets = 1 + added_sets_1 + added_sets_2;
2972 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2973 XVECEXP (newpat, 0, 0) = old;
2976 if (added_sets_1)
2977 XVECEXP (newpat, 0, --total_sets) = i1pat;
2979 if (added_sets_2)
2981 /* If there is no I1, use I2's body as is. We used to also not do
2982 the subst call below if I2 was substituted into I3,
2983 but that could lose a simplification. */
2984 if (i1 == 0)
2985 XVECEXP (newpat, 0, --total_sets) = i2pat;
2986 else
2987 /* See comment where i2pat is assigned. */
2988 XVECEXP (newpat, 0, --total_sets)
2989 = subst (i2pat, i1dest, i1src, 0, 0);
2993 validate_replacement:
2995 /* Note which hard regs this insn has as inputs. */
2996 mark_used_regs_combine (newpat);
2998 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2999 consider splitting this pattern, we might need these clobbers. */
3000 if (i1 && GET_CODE (newpat) == PARALLEL
3001 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3003 int len = XVECLEN (newpat, 0);
3005 newpat_vec_with_clobbers = rtvec_alloc (len);
3006 for (i = 0; i < len; i++)
3007 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3010 /* Is the result of combination a valid instruction? */
3011 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3013 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3014 the second SET's destination is a register that is unused and isn't
3015 marked as an instruction that might trap in an EH region. In that case,
3016 we just need the first SET. This can occur when simplifying a divmod
3017 insn. We *must* test for this case here because the code below that
3018 splits two independent SETs doesn't handle this case correctly when it
3019 updates the register status.
3021 It's pointless doing this if we originally had two sets, one from
3022 i3, and one from i2. Combining then splitting the parallel results
3023 in the original i2 again plus an invalid insn (which we delete).
3024 The net effect is only to move instructions around, which makes
3025 debug info less accurate.
3027 Also check the case where the first SET's destination is unused.
3028 That would not cause incorrect code, but does cause an unneeded
3029 insn to remain. */
3031 if (insn_code_number < 0
3032 && !(added_sets_2 && i1 == 0)
3033 && GET_CODE (newpat) == PARALLEL
3034 && XVECLEN (newpat, 0) == 2
3035 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3036 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3037 && asm_noperands (newpat) < 0)
3039 rtx set0 = XVECEXP (newpat, 0, 0);
3040 rtx set1 = XVECEXP (newpat, 0, 1);
3042 if (((REG_P (SET_DEST (set1))
3043 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3044 || (GET_CODE (SET_DEST (set1)) == SUBREG
3045 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3046 && insn_nothrow_p (i3)
3047 && !side_effects_p (SET_SRC (set1)))
3049 newpat = set0;
3050 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3053 else if (((REG_P (SET_DEST (set0))
3054 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3055 || (GET_CODE (SET_DEST (set0)) == SUBREG
3056 && find_reg_note (i3, REG_UNUSED,
3057 SUBREG_REG (SET_DEST (set0)))))
3058 && insn_nothrow_p (i3)
3059 && !side_effects_p (SET_SRC (set0)))
3061 newpat = set1;
3062 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3064 if (insn_code_number >= 0)
3065 changed_i3_dest = 1;
3069 /* If we were combining three insns and the result is a simple SET
3070 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3071 insns. There are two ways to do this. It can be split using a
3072 machine-specific method (like when you have an addition of a large
3073 constant) or by combine in the function find_split_point. */
3075 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3076 && asm_noperands (newpat) < 0)
3078 rtx parallel, m_split, *split;
3080 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3081 use I2DEST as a scratch register will help. In the latter case,
3082 convert I2DEST to the mode of the source of NEWPAT if we can. */
3084 m_split = combine_split_insns (newpat, i3);
3086 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3087 inputs of NEWPAT. */
3089 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3090 possible to try that as a scratch reg. This would require adding
3091 more code to make it work though. */
3093 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3095 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3097 /* First try to split using the original register as a
3098 scratch register. */
3099 parallel = gen_rtx_PARALLEL (VOIDmode,
3100 gen_rtvec (2, newpat,
3101 gen_rtx_CLOBBER (VOIDmode,
3102 i2dest)));
3103 m_split = combine_split_insns (parallel, i3);
3105 /* If that didn't work, try changing the mode of I2DEST if
3106 we can. */
3107 if (m_split == 0
3108 && new_mode != GET_MODE (i2dest)
3109 && new_mode != VOIDmode
3110 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3112 enum machine_mode old_mode = GET_MODE (i2dest);
3113 rtx ni2dest;
3115 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3116 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3117 else
3119 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3120 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3123 parallel = (gen_rtx_PARALLEL
3124 (VOIDmode,
3125 gen_rtvec (2, newpat,
3126 gen_rtx_CLOBBER (VOIDmode,
3127 ni2dest))));
3128 m_split = combine_split_insns (parallel, i3);
3130 if (m_split == 0
3131 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3133 struct undo *buf;
3135 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3136 buf = undobuf.undos;
3137 undobuf.undos = buf->next;
3138 buf->next = undobuf.frees;
3139 undobuf.frees = buf;
3143 i2scratch = m_split != 0;
3146 /* If recog_for_combine has discarded clobbers, try to use them
3147 again for the split. */
3148 if (m_split == 0 && newpat_vec_with_clobbers)
3150 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3151 m_split = combine_split_insns (parallel, i3);
3154 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
3156 m_split = PATTERN (m_split);
3157 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
3158 if (insn_code_number >= 0)
3159 newpat = m_split;
3161 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
3162 && (next_real_insn (i2) == i3
3163 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3165 rtx i2set, i3set;
3166 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3167 newi2pat = PATTERN (m_split);
3169 i3set = single_set (NEXT_INSN (m_split));
3170 i2set = single_set (m_split);
3172 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3174 /* If I2 or I3 has multiple SETs, we won't know how to track
3175 register status, so don't use these insns. If I2's destination
3176 is used between I2 and I3, we also can't use these insns. */
3178 if (i2_code_number >= 0 && i2set && i3set
3179 && (next_real_insn (i2) == i3
3180 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3181 insn_code_number = recog_for_combine (&newi3pat, i3,
3182 &new_i3_notes);
3183 if (insn_code_number >= 0)
3184 newpat = newi3pat;
3186 /* It is possible that both insns now set the destination of I3.
3187 If so, we must show an extra use of it. */
3189 if (insn_code_number >= 0)
3191 rtx new_i3_dest = SET_DEST (i3set);
3192 rtx new_i2_dest = SET_DEST (i2set);
3194 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3195 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3196 || GET_CODE (new_i3_dest) == SUBREG)
3197 new_i3_dest = XEXP (new_i3_dest, 0);
3199 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3200 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3201 || GET_CODE (new_i2_dest) == SUBREG)
3202 new_i2_dest = XEXP (new_i2_dest, 0);
3204 if (REG_P (new_i3_dest)
3205 && REG_P (new_i2_dest)
3206 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3207 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3211 /* If we can split it and use I2DEST, go ahead and see if that
3212 helps things be recognized. Verify that none of the registers
3213 are set between I2 and I3. */
3214 if (insn_code_number < 0
3215 && (split = find_split_point (&newpat, i3, false)) != 0
3216 #ifdef HAVE_cc0
3217 && REG_P (i2dest)
3218 #endif
3219 /* We need I2DEST in the proper mode. If it is a hard register
3220 or the only use of a pseudo, we can change its mode.
3221 Make sure we don't change a hard register to have a mode that
3222 isn't valid for it, or change the number of registers. */
3223 && (GET_MODE (*split) == GET_MODE (i2dest)
3224 || GET_MODE (*split) == VOIDmode
3225 || can_change_dest_mode (i2dest, added_sets_2,
3226 GET_MODE (*split)))
3227 && (next_real_insn (i2) == i3
3228 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3229 /* We can't overwrite I2DEST if its value is still used by
3230 NEWPAT. */
3231 && ! reg_referenced_p (i2dest, newpat))
3233 rtx newdest = i2dest;
3234 enum rtx_code split_code = GET_CODE (*split);
3235 enum machine_mode split_mode = GET_MODE (*split);
3236 bool subst_done = false;
3237 newi2pat = NULL_RTX;
3239 i2scratch = true;
3241 /* *SPLIT may be part of I2SRC, so make sure we have the
3242 original expression around for later debug processing.
3243 We should not need I2SRC any more in other cases. */
3244 if (MAY_HAVE_DEBUG_INSNS)
3245 i2src = copy_rtx (i2src);
3246 else
3247 i2src = NULL;
3249 /* Get NEWDEST as a register in the proper mode. We have already
3250 validated that we can do this. */
3251 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3253 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3254 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3255 else
3257 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3258 newdest = regno_reg_rtx[REGNO (i2dest)];
3262 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3263 an ASHIFT. This can occur if it was inside a PLUS and hence
3264 appeared to be a memory address. This is a kludge. */
3265 if (split_code == MULT
3266 && CONST_INT_P (XEXP (*split, 1))
3267 && INTVAL (XEXP (*split, 1)) > 0
3268 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3270 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3271 XEXP (*split, 0), GEN_INT (i)));
3272 /* Update split_code because we may not have a multiply
3273 anymore. */
3274 split_code = GET_CODE (*split);
3277 #ifdef INSN_SCHEDULING
3278 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3279 be written as a ZERO_EXTEND. */
3280 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3282 #ifdef LOAD_EXTEND_OP
3283 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3284 what it really is. */
3285 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3286 == SIGN_EXTEND)
3287 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3288 SUBREG_REG (*split)));
3289 else
3290 #endif
3291 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3292 SUBREG_REG (*split)));
3294 #endif
3296 /* Attempt to split binary operators using arithmetic identities. */
3297 if (BINARY_P (SET_SRC (newpat))
3298 && split_mode == GET_MODE (SET_SRC (newpat))
3299 && ! side_effects_p (SET_SRC (newpat)))
3301 rtx setsrc = SET_SRC (newpat);
3302 enum machine_mode mode = GET_MODE (setsrc);
3303 enum rtx_code code = GET_CODE (setsrc);
3304 rtx src_op0 = XEXP (setsrc, 0);
3305 rtx src_op1 = XEXP (setsrc, 1);
3307 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3308 if (rtx_equal_p (src_op0, src_op1))
3310 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3311 SUBST (XEXP (setsrc, 0), newdest);
3312 SUBST (XEXP (setsrc, 1), newdest);
3313 subst_done = true;
3315 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3316 else if ((code == PLUS || code == MULT)
3317 && GET_CODE (src_op0) == code
3318 && GET_CODE (XEXP (src_op0, 0)) == code
3319 && (INTEGRAL_MODE_P (mode)
3320 || (FLOAT_MODE_P (mode)
3321 && flag_unsafe_math_optimizations)))
3323 rtx p = XEXP (XEXP (src_op0, 0), 0);
3324 rtx q = XEXP (XEXP (src_op0, 0), 1);
3325 rtx r = XEXP (src_op0, 1);
3326 rtx s = src_op1;
3328 /* Split both "((X op Y) op X) op Y" and
3329 "((X op Y) op Y) op X" as "T op T" where T is
3330 "X op Y". */
3331 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3332 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3334 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3335 XEXP (src_op0, 0));
3336 SUBST (XEXP (setsrc, 0), newdest);
3337 SUBST (XEXP (setsrc, 1), newdest);
3338 subst_done = true;
3340 /* Split "((X op X) op Y) op Y)" as "T op T" where
3341 T is "X op Y". */
3342 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3344 rtx tmp = simplify_gen_binary (code, mode, p, r);
3345 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3346 SUBST (XEXP (setsrc, 0), newdest);
3347 SUBST (XEXP (setsrc, 1), newdest);
3348 subst_done = true;
3353 if (!subst_done)
3355 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3356 SUBST (*split, newdest);
3359 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3361 /* recog_for_combine might have added CLOBBERs to newi2pat.
3362 Make sure NEWPAT does not depend on the clobbered regs. */
3363 if (GET_CODE (newi2pat) == PARALLEL)
3364 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3365 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3367 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3368 if (reg_overlap_mentioned_p (reg, newpat))
3370 undo_all ();
3371 return 0;
3375 /* If the split point was a MULT and we didn't have one before,
3376 don't use one now. */
3377 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3378 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3382 /* Check for a case where we loaded from memory in a narrow mode and
3383 then sign extended it, but we need both registers. In that case,
3384 we have a PARALLEL with both loads from the same memory location.
3385 We can split this into a load from memory followed by a register-register
3386 copy. This saves at least one insn, more if register allocation can
3387 eliminate the copy.
3389 We cannot do this if the destination of the first assignment is a
3390 condition code register or cc0. We eliminate this case by making sure
3391 the SET_DEST and SET_SRC have the same mode.
3393 We cannot do this if the destination of the second assignment is
3394 a register that we have already assumed is zero-extended. Similarly
3395 for a SUBREG of such a register. */
3397 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3398 && GET_CODE (newpat) == PARALLEL
3399 && XVECLEN (newpat, 0) == 2
3400 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3401 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3402 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3403 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3404 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3405 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3406 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3407 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3408 DF_INSN_LUID (i2))
3409 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3410 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3411 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3412 (REG_P (temp)
3413 && VEC_index (reg_stat_type, reg_stat,
3414 REGNO (temp))->nonzero_bits != 0
3415 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3416 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3417 && (VEC_index (reg_stat_type, reg_stat,
3418 REGNO (temp))->nonzero_bits
3419 != GET_MODE_MASK (word_mode))))
3420 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3421 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3422 (REG_P (temp)
3423 && VEC_index (reg_stat_type, reg_stat,
3424 REGNO (temp))->nonzero_bits != 0
3425 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3426 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3427 && (VEC_index (reg_stat_type, reg_stat,
3428 REGNO (temp))->nonzero_bits
3429 != GET_MODE_MASK (word_mode)))))
3430 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3431 SET_SRC (XVECEXP (newpat, 0, 1)))
3432 && ! find_reg_note (i3, REG_UNUSED,
3433 SET_DEST (XVECEXP (newpat, 0, 0))))
3435 rtx ni2dest;
3437 newi2pat = XVECEXP (newpat, 0, 0);
3438 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3439 newpat = XVECEXP (newpat, 0, 1);
3440 SUBST (SET_SRC (newpat),
3441 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3442 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3444 if (i2_code_number >= 0)
3445 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3447 if (insn_code_number >= 0)
3448 swap_i2i3 = 1;
3451 /* Similarly, check for a case where we have a PARALLEL of two independent
3452 SETs but we started with three insns. In this case, we can do the sets
3453 as two separate insns. This case occurs when some SET allows two
3454 other insns to combine, but the destination of that SET is still live. */
3456 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3457 && GET_CODE (newpat) == PARALLEL
3458 && XVECLEN (newpat, 0) == 2
3459 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3460 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3461 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3462 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3463 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3464 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3465 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3466 DF_INSN_LUID (i2))
3467 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3468 XVECEXP (newpat, 0, 0))
3469 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3470 XVECEXP (newpat, 0, 1))
3471 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3472 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3473 #ifdef HAVE_cc0
3474 /* We cannot split the parallel into two sets if both sets
3475 reference cc0. */
3476 && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3477 && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3478 #endif
3481 /* Normally, it doesn't matter which of the two is done first,
3482 but it does if one references cc0. In that case, it has to
3483 be first. */
3484 #ifdef HAVE_cc0
3485 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3487 newi2pat = XVECEXP (newpat, 0, 0);
3488 newpat = XVECEXP (newpat, 0, 1);
3490 else
3491 #endif
3493 newi2pat = XVECEXP (newpat, 0, 1);
3494 newpat = XVECEXP (newpat, 0, 0);
3497 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3499 if (i2_code_number >= 0)
3500 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3503 /* If it still isn't recognized, fail and change things back the way they
3504 were. */
3505 if ((insn_code_number < 0
3506 /* Is the result a reasonable ASM_OPERANDS? */
3507 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3509 undo_all ();
3510 return 0;
3513 /* If we had to change another insn, make sure it is valid also. */
3514 if (undobuf.other_insn)
3516 CLEAR_HARD_REG_SET (newpat_used_regs);
3518 other_pat = PATTERN (undobuf.other_insn);
3519 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3520 &new_other_notes);
3522 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3524 undo_all ();
3525 return 0;
3529 #ifdef HAVE_cc0
3530 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3531 they are adjacent to each other or not. */
3533 rtx p = prev_nonnote_insn (i3);
3534 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3535 && sets_cc0_p (newi2pat))
3537 undo_all ();
3538 return 0;
3541 #endif
3543 /* Only allow this combination if insn_rtx_costs reports that the
3544 replacement instructions are cheaper than the originals. */
3545 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3547 undo_all ();
3548 return 0;
3551 if (MAY_HAVE_DEBUG_INSNS)
3553 struct undo *undo;
3555 for (undo = undobuf.undos; undo; undo = undo->next)
3556 if (undo->kind == UNDO_MODE)
3558 rtx reg = *undo->where.r;
3559 enum machine_mode new_mode = GET_MODE (reg);
3560 enum machine_mode old_mode = undo->old_contents.m;
3562 /* Temporarily revert mode back. */
3563 adjust_reg_mode (reg, old_mode);
3565 if (reg == i2dest && i2scratch)
3567 /* If we used i2dest as a scratch register with a
3568 different mode, substitute it for the original
3569 i2src while its original mode is temporarily
3570 restored, and then clear i2scratch so that we don't
3571 do it again later. */
3572 propagate_for_debug (i2, i3, reg, i2src, false);
3573 i2scratch = false;
3574 /* Put back the new mode. */
3575 adjust_reg_mode (reg, new_mode);
3577 else
3579 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3580 rtx first, last;
3582 if (reg == i2dest)
3584 first = i2;
3585 last = i3;
3587 else
3589 first = i3;
3590 last = undobuf.other_insn;
3591 gcc_assert (last);
3594 /* We're dealing with a reg that changed mode but not
3595 meaning, so we want to turn it into a subreg for
3596 the new mode. However, because of REG sharing and
3597 because its mode had already changed, we have to do
3598 it in two steps. First, replace any debug uses of
3599 reg, with its original mode temporarily restored,
3600 with this copy we have created; then, replace the
3601 copy with the SUBREG of the original shared reg,
3602 once again changed to the new mode. */
3603 propagate_for_debug (first, last, reg, tempreg, false);
3604 adjust_reg_mode (reg, new_mode);
3605 propagate_for_debug (first, last, tempreg,
3606 lowpart_subreg (old_mode, reg, new_mode),
3607 false);
3612 /* If we will be able to accept this, we have made a
3613 change to the destination of I3. This requires us to
3614 do a few adjustments. */
3616 if (changed_i3_dest)
3618 PATTERN (i3) = newpat;
3619 adjust_for_new_dest (i3);
3622 /* We now know that we can do this combination. Merge the insns and
3623 update the status of registers and LOG_LINKS. */
3625 if (undobuf.other_insn)
3627 rtx note, next;
3629 PATTERN (undobuf.other_insn) = other_pat;
3631 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3632 are still valid. Then add any non-duplicate notes added by
3633 recog_for_combine. */
3634 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3636 next = XEXP (note, 1);
3638 if (REG_NOTE_KIND (note) == REG_UNUSED
3639 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3640 remove_note (undobuf.other_insn, note);
3643 distribute_notes (new_other_notes, undobuf.other_insn,
3644 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3647 if (swap_i2i3)
3649 rtx insn;
3650 rtx link;
3651 rtx ni2dest;
3653 /* I3 now uses what used to be its destination and which is now
3654 I2's destination. This requires us to do a few adjustments. */
3655 PATTERN (i3) = newpat;
3656 adjust_for_new_dest (i3);
3658 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3659 so we still will.
3661 However, some later insn might be using I2's dest and have
3662 a LOG_LINK pointing at I3. We must remove this link.
3663 The simplest way to remove the link is to point it at I1,
3664 which we know will be a NOTE. */
3666 /* newi2pat is usually a SET here; however, recog_for_combine might
3667 have added some clobbers. */
3668 if (GET_CODE (newi2pat) == PARALLEL)
3669 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3670 else
3671 ni2dest = SET_DEST (newi2pat);
3673 for (insn = NEXT_INSN (i3);
3674 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3675 || insn != BB_HEAD (this_basic_block->next_bb));
3676 insn = NEXT_INSN (insn))
3678 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3680 for (link = LOG_LINKS (insn); link;
3681 link = XEXP (link, 1))
3682 if (XEXP (link, 0) == i3)
3683 XEXP (link, 0) = i1;
3685 break;
3691 rtx i3notes, i2notes, i1notes = 0;
3692 rtx i3links, i2links, i1links = 0;
3693 rtx midnotes = 0;
3694 unsigned int regno;
3695 /* Compute which registers we expect to eliminate. newi2pat may be setting
3696 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3697 same as i3dest, in which case newi2pat may be setting i1dest. */
3698 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3699 || i2dest_in_i2src || i2dest_in_i1src
3700 || !i2dest_killed
3701 ? 0 : i2dest);
3702 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3703 || (newi2pat && reg_set_p (i1dest, newi2pat))
3704 || !i1dest_killed
3705 ? 0 : i1dest);
3707 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3708 clear them. */
3709 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3710 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3711 if (i1)
3712 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3714 /* Ensure that we do not have something that should not be shared but
3715 occurs multiple times in the new insns. Check this by first
3716 resetting all the `used' flags and then copying anything is shared. */
3718 reset_used_flags (i3notes);
3719 reset_used_flags (i2notes);
3720 reset_used_flags (i1notes);
3721 reset_used_flags (newpat);
3722 reset_used_flags (newi2pat);
3723 if (undobuf.other_insn)
3724 reset_used_flags (PATTERN (undobuf.other_insn));
3726 i3notes = copy_rtx_if_shared (i3notes);
3727 i2notes = copy_rtx_if_shared (i2notes);
3728 i1notes = copy_rtx_if_shared (i1notes);
3729 newpat = copy_rtx_if_shared (newpat);
3730 newi2pat = copy_rtx_if_shared (newi2pat);
3731 if (undobuf.other_insn)
3732 reset_used_flags (PATTERN (undobuf.other_insn));
3734 INSN_CODE (i3) = insn_code_number;
3735 PATTERN (i3) = newpat;
3737 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3739 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3741 reset_used_flags (call_usage);
3742 call_usage = copy_rtx (call_usage);
3744 if (substed_i2)
3746 /* I2SRC must still be meaningful at this point. Some splitting
3747 operations can invalidate I2SRC, but those operations do not
3748 apply to calls. */
3749 gcc_assert (i2src);
3750 replace_rtx (call_usage, i2dest, i2src);
3753 if (substed_i1)
3754 replace_rtx (call_usage, i1dest, i1src);
3756 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3759 if (undobuf.other_insn)
3760 INSN_CODE (undobuf.other_insn) = other_code_number;
3762 /* We had one special case above where I2 had more than one set and
3763 we replaced a destination of one of those sets with the destination
3764 of I3. In that case, we have to update LOG_LINKS of insns later
3765 in this basic block. Note that this (expensive) case is rare.
3767 Also, in this case, we must pretend that all REG_NOTEs for I2
3768 actually came from I3, so that REG_UNUSED notes from I2 will be
3769 properly handled. */
3771 if (i3_subst_into_i2)
3773 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3774 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3775 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3776 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3777 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3778 && ! find_reg_note (i2, REG_UNUSED,
3779 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3780 for (temp = NEXT_INSN (i2);
3781 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3782 || BB_HEAD (this_basic_block) != temp);
3783 temp = NEXT_INSN (temp))
3784 if (temp != i3 && INSN_P (temp))
3785 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3786 if (XEXP (link, 0) == i2)
3787 XEXP (link, 0) = i3;
3789 if (i3notes)
3791 rtx link = i3notes;
3792 while (XEXP (link, 1))
3793 link = XEXP (link, 1);
3794 XEXP (link, 1) = i2notes;
3796 else
3797 i3notes = i2notes;
3798 i2notes = 0;
3801 LOG_LINKS (i3) = 0;
3802 REG_NOTES (i3) = 0;
3803 LOG_LINKS (i2) = 0;
3804 REG_NOTES (i2) = 0;
3806 if (newi2pat)
3808 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
3809 propagate_for_debug (i2, i3, i2dest, i2src, false);
3810 INSN_CODE (i2) = i2_code_number;
3811 PATTERN (i2) = newi2pat;
3813 else
3815 if (MAY_HAVE_DEBUG_INSNS && i2src)
3816 propagate_for_debug (i2, i3, i2dest, i2src, i3_subst_into_i2);
3817 SET_INSN_DELETED (i2);
3820 if (i1)
3822 LOG_LINKS (i1) = 0;
3823 REG_NOTES (i1) = 0;
3824 if (MAY_HAVE_DEBUG_INSNS)
3825 propagate_for_debug (i1, i3, i1dest, i1src, false);
3826 SET_INSN_DELETED (i1);
3829 /* Get death notes for everything that is now used in either I3 or
3830 I2 and used to die in a previous insn. If we built two new
3831 patterns, move from I1 to I2 then I2 to I3 so that we get the
3832 proper movement on registers that I2 modifies. */
3834 if (newi2pat)
3836 move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3837 move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3839 else
3840 move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3841 i3, &midnotes);
3843 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
3844 if (i3notes)
3845 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3846 elim_i2, elim_i1);
3847 if (i2notes)
3848 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3849 elim_i2, elim_i1);
3850 if (i1notes)
3851 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3852 elim_i2, elim_i1);
3853 if (midnotes)
3854 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3855 elim_i2, elim_i1);
3857 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
3858 know these are REG_UNUSED and want them to go to the desired insn,
3859 so we always pass it as i3. */
3861 if (newi2pat && new_i2_notes)
3862 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3864 if (new_i3_notes)
3865 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3867 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
3868 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
3869 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
3870 in that case, it might delete I2. Similarly for I2 and I1.
3871 Show an additional death due to the REG_DEAD note we make here. If
3872 we discard it in distribute_notes, we will decrement it again. */
3874 if (i3dest_killed)
3876 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3877 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
3878 NULL_RTX),
3879 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3880 else
3881 distribute_notes (alloc_reg_note (REG_DEAD, i3dest_killed,
3882 NULL_RTX),
3883 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3884 elim_i2, elim_i1);
3887 if (i2dest_in_i2src)
3889 if (newi2pat && reg_set_p (i2dest, newi2pat))
3890 distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
3891 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3892 else
3893 distribute_notes (alloc_reg_note (REG_DEAD, i2dest, NULL_RTX),
3894 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3895 NULL_RTX, NULL_RTX);
3898 if (i1dest_in_i1src)
3900 if (newi2pat && reg_set_p (i1dest, newi2pat))
3901 distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
3902 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3903 else
3904 distribute_notes (alloc_reg_note (REG_DEAD, i1dest, NULL_RTX),
3905 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3906 NULL_RTX, NULL_RTX);
3909 distribute_links (i3links);
3910 distribute_links (i2links);
3911 distribute_links (i1links);
3913 if (REG_P (i2dest))
3915 rtx link;
3916 rtx i2_insn = 0, i2_val = 0, set;
3918 /* The insn that used to set this register doesn't exist, and
3919 this life of the register may not exist either. See if one of
3920 I3's links points to an insn that sets I2DEST. If it does,
3921 that is now the last known value for I2DEST. If we don't update
3922 this and I2 set the register to a value that depended on its old
3923 contents, we will get confused. If this insn is used, thing
3924 will be set correctly in combine_instructions. */
3926 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3927 if ((set = single_set (XEXP (link, 0))) != 0
3928 && rtx_equal_p (i2dest, SET_DEST (set)))
3929 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3931 record_value_for_reg (i2dest, i2_insn, i2_val);
3933 /* If the reg formerly set in I2 died only once and that was in I3,
3934 zero its use count so it won't make `reload' do any work. */
3935 if (! added_sets_2
3936 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3937 && ! i2dest_in_i2src)
3939 regno = REGNO (i2dest);
3940 INC_REG_N_SETS (regno, -1);
3944 if (i1 && REG_P (i1dest))
3946 rtx link;
3947 rtx i1_insn = 0, i1_val = 0, set;
3949 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3950 if ((set = single_set (XEXP (link, 0))) != 0
3951 && rtx_equal_p (i1dest, SET_DEST (set)))
3952 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3954 record_value_for_reg (i1dest, i1_insn, i1_val);
3956 regno = REGNO (i1dest);
3957 if (! added_sets_1 && ! i1dest_in_i1src)
3958 INC_REG_N_SETS (regno, -1);
3961 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3962 been made to this insn. The order of
3963 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3964 can affect nonzero_bits of newpat */
3965 if (newi2pat)
3966 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3967 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3970 if (undobuf.other_insn != NULL_RTX)
3972 if (dump_file)
3974 fprintf (dump_file, "modifying other_insn ");
3975 dump_insn_slim (dump_file, undobuf.other_insn);
3977 df_insn_rescan (undobuf.other_insn);
3980 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3982 if (dump_file)
3984 fprintf (dump_file, "modifying insn i1 ");
3985 dump_insn_slim (dump_file, i1);
3987 df_insn_rescan (i1);
3990 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3992 if (dump_file)
3994 fprintf (dump_file, "modifying insn i2 ");
3995 dump_insn_slim (dump_file, i2);
3997 df_insn_rescan (i2);
4000 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4002 if (dump_file)
4004 fprintf (dump_file, "modifying insn i3 ");
4005 dump_insn_slim (dump_file, i3);
4007 df_insn_rescan (i3);
4010 /* Set new_direct_jump_p if a new return or simple jump instruction
4011 has been created. Adjust the CFG accordingly. */
4013 if (returnjump_p (i3) || any_uncondjump_p (i3))
4015 *new_direct_jump_p = 1;
4016 mark_jump_label (PATTERN (i3), i3, 0);
4017 update_cfg_for_uncondjump (i3);
4020 if (undobuf.other_insn != NULL_RTX
4021 && (returnjump_p (undobuf.other_insn)
4022 || any_uncondjump_p (undobuf.other_insn)))
4024 *new_direct_jump_p = 1;
4025 update_cfg_for_uncondjump (undobuf.other_insn);
4028 /* A noop might also need cleaning up of CFG, if it comes from the
4029 simplification of a jump. */
4030 if (GET_CODE (newpat) == SET
4031 && SET_SRC (newpat) == pc_rtx
4032 && SET_DEST (newpat) == pc_rtx)
4034 *new_direct_jump_p = 1;
4035 update_cfg_for_uncondjump (i3);
4038 combine_successes++;
4039 undo_commit ();
4041 if (added_links_insn
4042 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4043 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4044 return added_links_insn;
4045 else
4046 return newi2pat ? i2 : i3;
4049 /* Undo all the modifications recorded in undobuf. */
4051 static void
4052 undo_all (void)
4054 struct undo *undo, *next;
4056 for (undo = undobuf.undos; undo; undo = next)
4058 next = undo->next;
4059 switch (undo->kind)
4061 case UNDO_RTX:
4062 *undo->where.r = undo->old_contents.r;
4063 break;
4064 case UNDO_INT:
4065 *undo->where.i = undo->old_contents.i;
4066 break;
4067 case UNDO_MODE:
4068 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4069 break;
4070 default:
4071 gcc_unreachable ();
4074 undo->next = undobuf.frees;
4075 undobuf.frees = undo;
4078 undobuf.undos = 0;
4081 /* We've committed to accepting the changes we made. Move all
4082 of the undos to the free list. */
4084 static void
4085 undo_commit (void)
4087 struct undo *undo, *next;
4089 for (undo = undobuf.undos; undo; undo = next)
4091 next = undo->next;
4092 undo->next = undobuf.frees;
4093 undobuf.frees = undo;
4095 undobuf.undos = 0;
4098 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4099 where we have an arithmetic expression and return that point. LOC will
4100 be inside INSN.
4102 try_combine will call this function to see if an insn can be split into
4103 two insns. */
4105 static rtx *
4106 find_split_point (rtx *loc, rtx insn, bool set_src)
4108 rtx x = *loc;
4109 enum rtx_code code = GET_CODE (x);
4110 rtx *split;
4111 unsigned HOST_WIDE_INT len = 0;
4112 HOST_WIDE_INT pos = 0;
4113 int unsignedp = 0;
4114 rtx inner = NULL_RTX;
4116 /* First special-case some codes. */
4117 switch (code)
4119 case SUBREG:
4120 #ifdef INSN_SCHEDULING
4121 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4122 point. */
4123 if (MEM_P (SUBREG_REG (x)))
4124 return loc;
4125 #endif
4126 return find_split_point (&SUBREG_REG (x), insn, false);
4128 case MEM:
4129 #ifdef HAVE_lo_sum
4130 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4131 using LO_SUM and HIGH. */
4132 if (GET_CODE (XEXP (x, 0)) == CONST
4133 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4135 enum machine_mode address_mode
4136 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (x));
4138 SUBST (XEXP (x, 0),
4139 gen_rtx_LO_SUM (address_mode,
4140 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4141 XEXP (x, 0)));
4142 return &XEXP (XEXP (x, 0), 0);
4144 #endif
4146 /* If we have a PLUS whose second operand is a constant and the
4147 address is not valid, perhaps will can split it up using
4148 the machine-specific way to split large constants. We use
4149 the first pseudo-reg (one of the virtual regs) as a placeholder;
4150 it will not remain in the result. */
4151 if (GET_CODE (XEXP (x, 0)) == PLUS
4152 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4153 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4154 MEM_ADDR_SPACE (x)))
4156 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4157 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4158 XEXP (x, 0)),
4159 subst_insn);
4161 /* This should have produced two insns, each of which sets our
4162 placeholder. If the source of the second is a valid address,
4163 we can make put both sources together and make a split point
4164 in the middle. */
4166 if (seq
4167 && NEXT_INSN (seq) != NULL_RTX
4168 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4169 && NONJUMP_INSN_P (seq)
4170 && GET_CODE (PATTERN (seq)) == SET
4171 && SET_DEST (PATTERN (seq)) == reg
4172 && ! reg_mentioned_p (reg,
4173 SET_SRC (PATTERN (seq)))
4174 && NONJUMP_INSN_P (NEXT_INSN (seq))
4175 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4176 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4177 && memory_address_addr_space_p
4178 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4179 MEM_ADDR_SPACE (x)))
4181 rtx src1 = SET_SRC (PATTERN (seq));
4182 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4184 /* Replace the placeholder in SRC2 with SRC1. If we can
4185 find where in SRC2 it was placed, that can become our
4186 split point and we can replace this address with SRC2.
4187 Just try two obvious places. */
4189 src2 = replace_rtx (src2, reg, src1);
4190 split = 0;
4191 if (XEXP (src2, 0) == src1)
4192 split = &XEXP (src2, 0);
4193 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4194 && XEXP (XEXP (src2, 0), 0) == src1)
4195 split = &XEXP (XEXP (src2, 0), 0);
4197 if (split)
4199 SUBST (XEXP (x, 0), src2);
4200 return split;
4204 /* If that didn't work, perhaps the first operand is complex and
4205 needs to be computed separately, so make a split point there.
4206 This will occur on machines that just support REG + CONST
4207 and have a constant moved through some previous computation. */
4209 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4210 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4211 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4212 return &XEXP (XEXP (x, 0), 0);
4215 /* If we have a PLUS whose first operand is complex, try computing it
4216 separately by making a split there. */
4217 if (GET_CODE (XEXP (x, 0)) == PLUS
4218 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4219 MEM_ADDR_SPACE (x))
4220 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4221 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4222 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4223 return &XEXP (XEXP (x, 0), 0);
4224 break;
4226 case SET:
4227 #ifdef HAVE_cc0
4228 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4229 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4230 we need to put the operand into a register. So split at that
4231 point. */
4233 if (SET_DEST (x) == cc0_rtx
4234 && GET_CODE (SET_SRC (x)) != COMPARE
4235 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4236 && !OBJECT_P (SET_SRC (x))
4237 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4238 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4239 return &SET_SRC (x);
4240 #endif
4242 /* See if we can split SET_SRC as it stands. */
4243 split = find_split_point (&SET_SRC (x), insn, true);
4244 if (split && split != &SET_SRC (x))
4245 return split;
4247 /* See if we can split SET_DEST as it stands. */
4248 split = find_split_point (&SET_DEST (x), insn, false);
4249 if (split && split != &SET_DEST (x))
4250 return split;
4252 /* See if this is a bitfield assignment with everything constant. If
4253 so, this is an IOR of an AND, so split it into that. */
4254 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4255 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
4256 <= HOST_BITS_PER_WIDE_INT)
4257 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4258 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4259 && CONST_INT_P (SET_SRC (x))
4260 && ((INTVAL (XEXP (SET_DEST (x), 1))
4261 + INTVAL (XEXP (SET_DEST (x), 2)))
4262 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4263 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4265 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4266 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4267 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4268 rtx dest = XEXP (SET_DEST (x), 0);
4269 enum machine_mode mode = GET_MODE (dest);
4270 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
4271 rtx or_mask;
4273 if (BITS_BIG_ENDIAN)
4274 pos = GET_MODE_BITSIZE (mode) - len - pos;
4276 or_mask = gen_int_mode (src << pos, mode);
4277 if (src == mask)
4278 SUBST (SET_SRC (x),
4279 simplify_gen_binary (IOR, mode, dest, or_mask));
4280 else
4282 rtx negmask = gen_int_mode (~(mask << pos), mode);
4283 SUBST (SET_SRC (x),
4284 simplify_gen_binary (IOR, mode,
4285 simplify_gen_binary (AND, mode,
4286 dest, negmask),
4287 or_mask));
4290 SUBST (SET_DEST (x), dest);
4292 split = find_split_point (&SET_SRC (x), insn, true);
4293 if (split && split != &SET_SRC (x))
4294 return split;
4297 /* Otherwise, see if this is an operation that we can split into two.
4298 If so, try to split that. */
4299 code = GET_CODE (SET_SRC (x));
4301 switch (code)
4303 case AND:
4304 /* If we are AND'ing with a large constant that is only a single
4305 bit and the result is only being used in a context where we
4306 need to know if it is zero or nonzero, replace it with a bit
4307 extraction. This will avoid the large constant, which might
4308 have taken more than one insn to make. If the constant were
4309 not a valid argument to the AND but took only one insn to make,
4310 this is no worse, but if it took more than one insn, it will
4311 be better. */
4313 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4314 && REG_P (XEXP (SET_SRC (x), 0))
4315 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4316 && REG_P (SET_DEST (x))
4317 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4318 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4319 && XEXP (*split, 0) == SET_DEST (x)
4320 && XEXP (*split, 1) == const0_rtx)
4322 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4323 XEXP (SET_SRC (x), 0),
4324 pos, NULL_RTX, 1, 1, 0, 0);
4325 if (extraction != 0)
4327 SUBST (SET_SRC (x), extraction);
4328 return find_split_point (loc, insn, false);
4331 break;
4333 case NE:
4334 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4335 is known to be on, this can be converted into a NEG of a shift. */
4336 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4337 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4338 && 1 <= (pos = exact_log2
4339 (nonzero_bits (XEXP (SET_SRC (x), 0),
4340 GET_MODE (XEXP (SET_SRC (x), 0))))))
4342 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4344 SUBST (SET_SRC (x),
4345 gen_rtx_NEG (mode,
4346 gen_rtx_LSHIFTRT (mode,
4347 XEXP (SET_SRC (x), 0),
4348 GEN_INT (pos))));
4350 split = find_split_point (&SET_SRC (x), insn, true);
4351 if (split && split != &SET_SRC (x))
4352 return split;
4354 break;
4356 case SIGN_EXTEND:
4357 inner = XEXP (SET_SRC (x), 0);
4359 /* We can't optimize if either mode is a partial integer
4360 mode as we don't know how many bits are significant
4361 in those modes. */
4362 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4363 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4364 break;
4366 pos = 0;
4367 len = GET_MODE_BITSIZE (GET_MODE (inner));
4368 unsignedp = 0;
4369 break;
4371 case SIGN_EXTRACT:
4372 case ZERO_EXTRACT:
4373 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4374 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4376 inner = XEXP (SET_SRC (x), 0);
4377 len = INTVAL (XEXP (SET_SRC (x), 1));
4378 pos = INTVAL (XEXP (SET_SRC (x), 2));
4380 if (BITS_BIG_ENDIAN)
4381 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4382 unsignedp = (code == ZERO_EXTRACT);
4384 break;
4386 default:
4387 break;
4390 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4392 enum machine_mode mode = GET_MODE (SET_SRC (x));
4394 /* For unsigned, we have a choice of a shift followed by an
4395 AND or two shifts. Use two shifts for field sizes where the
4396 constant might be too large. We assume here that we can
4397 always at least get 8-bit constants in an AND insn, which is
4398 true for every current RISC. */
4400 if (unsignedp && len <= 8)
4402 SUBST (SET_SRC (x),
4403 gen_rtx_AND (mode,
4404 gen_rtx_LSHIFTRT
4405 (mode, gen_lowpart (mode, inner),
4406 GEN_INT (pos)),
4407 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4409 split = find_split_point (&SET_SRC (x), insn, true);
4410 if (split && split != &SET_SRC (x))
4411 return split;
4413 else
4415 SUBST (SET_SRC (x),
4416 gen_rtx_fmt_ee
4417 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4418 gen_rtx_ASHIFT (mode,
4419 gen_lowpart (mode, inner),
4420 GEN_INT (GET_MODE_BITSIZE (mode)
4421 - len - pos)),
4422 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4424 split = find_split_point (&SET_SRC (x), insn, true);
4425 if (split && split != &SET_SRC (x))
4426 return split;
4430 /* See if this is a simple operation with a constant as the second
4431 operand. It might be that this constant is out of range and hence
4432 could be used as a split point. */
4433 if (BINARY_P (SET_SRC (x))
4434 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4435 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4436 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4437 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4438 return &XEXP (SET_SRC (x), 1);
4440 /* Finally, see if this is a simple operation with its first operand
4441 not in a register. The operation might require this operand in a
4442 register, so return it as a split point. We can always do this
4443 because if the first operand were another operation, we would have
4444 already found it as a split point. */
4445 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4446 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4447 return &XEXP (SET_SRC (x), 0);
4449 return 0;
4451 case AND:
4452 case IOR:
4453 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4454 it is better to write this as (not (ior A B)) so we can split it.
4455 Similarly for IOR. */
4456 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4458 SUBST (*loc,
4459 gen_rtx_NOT (GET_MODE (x),
4460 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4461 GET_MODE (x),
4462 XEXP (XEXP (x, 0), 0),
4463 XEXP (XEXP (x, 1), 0))));
4464 return find_split_point (loc, insn, set_src);
4467 /* Many RISC machines have a large set of logical insns. If the
4468 second operand is a NOT, put it first so we will try to split the
4469 other operand first. */
4470 if (GET_CODE (XEXP (x, 1)) == NOT)
4472 rtx tem = XEXP (x, 0);
4473 SUBST (XEXP (x, 0), XEXP (x, 1));
4474 SUBST (XEXP (x, 1), tem);
4476 break;
4478 case PLUS:
4479 case MINUS:
4480 /* Split at a multiply-accumulate instruction. However if this is
4481 the SET_SRC, we likely do not have such an instruction and it's
4482 worthless to try this split. */
4483 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4484 return loc;
4486 default:
4487 break;
4490 /* Otherwise, select our actions depending on our rtx class. */
4491 switch (GET_RTX_CLASS (code))
4493 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4494 case RTX_TERNARY:
4495 split = find_split_point (&XEXP (x, 2), insn, false);
4496 if (split)
4497 return split;
4498 /* ... fall through ... */
4499 case RTX_BIN_ARITH:
4500 case RTX_COMM_ARITH:
4501 case RTX_COMPARE:
4502 case RTX_COMM_COMPARE:
4503 split = find_split_point (&XEXP (x, 1), insn, false);
4504 if (split)
4505 return split;
4506 /* ... fall through ... */
4507 case RTX_UNARY:
4508 /* Some machines have (and (shift ...) ...) insns. If X is not
4509 an AND, but XEXP (X, 0) is, use it as our split point. */
4510 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4511 return &XEXP (x, 0);
4513 split = find_split_point (&XEXP (x, 0), insn, false);
4514 if (split)
4515 return split;
4516 return loc;
4518 default:
4519 /* Otherwise, we don't have a split point. */
4520 return 0;
4524 /* Throughout X, replace FROM with TO, and return the result.
4525 The result is TO if X is FROM;
4526 otherwise the result is X, but its contents may have been modified.
4527 If they were modified, a record was made in undobuf so that
4528 undo_all will (among other things) return X to its original state.
4530 If the number of changes necessary is too much to record to undo,
4531 the excess changes are not made, so the result is invalid.
4532 The changes already made can still be undone.
4533 undobuf.num_undo is incremented for such changes, so by testing that
4534 the caller can tell whether the result is valid.
4536 `n_occurrences' is incremented each time FROM is replaced.
4538 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4540 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4541 by copying if `n_occurrences' is nonzero. */
4543 static rtx
4544 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4546 enum rtx_code code = GET_CODE (x);
4547 enum machine_mode op0_mode = VOIDmode;
4548 const char *fmt;
4549 int len, i;
4550 rtx new_rtx;
4552 /* Two expressions are equal if they are identical copies of a shared
4553 RTX or if they are both registers with the same register number
4554 and mode. */
4556 #define COMBINE_RTX_EQUAL_P(X,Y) \
4557 ((X) == (Y) \
4558 || (REG_P (X) && REG_P (Y) \
4559 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4561 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4563 n_occurrences++;
4564 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4567 /* If X and FROM are the same register but different modes, they
4568 will not have been seen as equal above. However, the log links code
4569 will make a LOG_LINKS entry for that case. If we do nothing, we
4570 will try to rerecognize our original insn and, when it succeeds,
4571 we will delete the feeding insn, which is incorrect.
4573 So force this insn not to match in this (rare) case. */
4574 if (! in_dest && code == REG && REG_P (from)
4575 && reg_overlap_mentioned_p (x, from))
4576 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4578 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4579 of which may contain things that can be combined. */
4580 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4581 return x;
4583 /* It is possible to have a subexpression appear twice in the insn.
4584 Suppose that FROM is a register that appears within TO.
4585 Then, after that subexpression has been scanned once by `subst',
4586 the second time it is scanned, TO may be found. If we were
4587 to scan TO here, we would find FROM within it and create a
4588 self-referent rtl structure which is completely wrong. */
4589 if (COMBINE_RTX_EQUAL_P (x, to))
4590 return to;
4592 /* Parallel asm_operands need special attention because all of the
4593 inputs are shared across the arms. Furthermore, unsharing the
4594 rtl results in recognition failures. Failure to handle this case
4595 specially can result in circular rtl.
4597 Solve this by doing a normal pass across the first entry of the
4598 parallel, and only processing the SET_DESTs of the subsequent
4599 entries. Ug. */
4601 if (code == PARALLEL
4602 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4603 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4605 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4607 /* If this substitution failed, this whole thing fails. */
4608 if (GET_CODE (new_rtx) == CLOBBER
4609 && XEXP (new_rtx, 0) == const0_rtx)
4610 return new_rtx;
4612 SUBST (XVECEXP (x, 0, 0), new_rtx);
4614 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4616 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4618 if (!REG_P (dest)
4619 && GET_CODE (dest) != CC0
4620 && GET_CODE (dest) != PC)
4622 new_rtx = subst (dest, from, to, 0, unique_copy);
4624 /* If this substitution failed, this whole thing fails. */
4625 if (GET_CODE (new_rtx) == CLOBBER
4626 && XEXP (new_rtx, 0) == const0_rtx)
4627 return new_rtx;
4629 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4633 else
4635 len = GET_RTX_LENGTH (code);
4636 fmt = GET_RTX_FORMAT (code);
4638 /* We don't need to process a SET_DEST that is a register, CC0,
4639 or PC, so set up to skip this common case. All other cases
4640 where we want to suppress replacing something inside a
4641 SET_SRC are handled via the IN_DEST operand. */
4642 if (code == SET
4643 && (REG_P (SET_DEST (x))
4644 || GET_CODE (SET_DEST (x)) == CC0
4645 || GET_CODE (SET_DEST (x)) == PC))
4646 fmt = "ie";
4648 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4649 constant. */
4650 if (fmt[0] == 'e')
4651 op0_mode = GET_MODE (XEXP (x, 0));
4653 for (i = 0; i < len; i++)
4655 if (fmt[i] == 'E')
4657 int j;
4658 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4660 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4662 new_rtx = (unique_copy && n_occurrences
4663 ? copy_rtx (to) : to);
4664 n_occurrences++;
4666 else
4668 new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
4669 unique_copy);
4671 /* If this substitution failed, this whole thing
4672 fails. */
4673 if (GET_CODE (new_rtx) == CLOBBER
4674 && XEXP (new_rtx, 0) == const0_rtx)
4675 return new_rtx;
4678 SUBST (XVECEXP (x, i, j), new_rtx);
4681 else if (fmt[i] == 'e')
4683 /* If this is a register being set, ignore it. */
4684 new_rtx = XEXP (x, i);
4685 if (in_dest
4686 && i == 0
4687 && (((code == SUBREG || code == ZERO_EXTRACT)
4688 && REG_P (new_rtx))
4689 || code == STRICT_LOW_PART))
4692 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4694 /* In general, don't install a subreg involving two
4695 modes not tieable. It can worsen register
4696 allocation, and can even make invalid reload
4697 insns, since the reg inside may need to be copied
4698 from in the outside mode, and that may be invalid
4699 if it is an fp reg copied in integer mode.
4701 We allow two exceptions to this: It is valid if
4702 it is inside another SUBREG and the mode of that
4703 SUBREG and the mode of the inside of TO is
4704 tieable and it is valid if X is a SET that copies
4705 FROM to CC0. */
4707 if (GET_CODE (to) == SUBREG
4708 && ! MODES_TIEABLE_P (GET_MODE (to),
4709 GET_MODE (SUBREG_REG (to)))
4710 && ! (code == SUBREG
4711 && MODES_TIEABLE_P (GET_MODE (x),
4712 GET_MODE (SUBREG_REG (to))))
4713 #ifdef HAVE_cc0
4714 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4715 #endif
4717 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4719 #ifdef CANNOT_CHANGE_MODE_CLASS
4720 if (code == SUBREG
4721 && REG_P (to)
4722 && REGNO (to) < FIRST_PSEUDO_REGISTER
4723 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4724 GET_MODE (to),
4725 GET_MODE (x)))
4726 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4727 #endif
4729 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4730 n_occurrences++;
4732 else
4733 /* If we are in a SET_DEST, suppress most cases unless we
4734 have gone inside a MEM, in which case we want to
4735 simplify the address. We assume here that things that
4736 are actually part of the destination have their inner
4737 parts in the first expression. This is true for SUBREG,
4738 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4739 things aside from REG and MEM that should appear in a
4740 SET_DEST. */
4741 new_rtx = subst (XEXP (x, i), from, to,
4742 (((in_dest
4743 && (code == SUBREG || code == STRICT_LOW_PART
4744 || code == ZERO_EXTRACT))
4745 || code == SET)
4746 && i == 0), unique_copy);
4748 /* If we found that we will have to reject this combination,
4749 indicate that by returning the CLOBBER ourselves, rather than
4750 an expression containing it. This will speed things up as
4751 well as prevent accidents where two CLOBBERs are considered
4752 to be equal, thus producing an incorrect simplification. */
4754 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
4755 return new_rtx;
4757 if (GET_CODE (x) == SUBREG
4758 && (CONST_INT_P (new_rtx)
4759 || GET_CODE (new_rtx) == CONST_DOUBLE))
4761 enum machine_mode mode = GET_MODE (x);
4763 x = simplify_subreg (GET_MODE (x), new_rtx,
4764 GET_MODE (SUBREG_REG (x)),
4765 SUBREG_BYTE (x));
4766 if (! x)
4767 x = gen_rtx_CLOBBER (mode, const0_rtx);
4769 else if (CONST_INT_P (new_rtx)
4770 && GET_CODE (x) == ZERO_EXTEND)
4772 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4773 new_rtx, GET_MODE (XEXP (x, 0)));
4774 gcc_assert (x);
4776 else
4777 SUBST (XEXP (x, i), new_rtx);
4782 /* Check if we are loading something from the constant pool via float
4783 extension; in this case we would undo compress_float_constant
4784 optimization and degenerate constant load to an immediate value. */
4785 if (GET_CODE (x) == FLOAT_EXTEND
4786 && MEM_P (XEXP (x, 0))
4787 && MEM_READONLY_P (XEXP (x, 0)))
4789 rtx tmp = avoid_constant_pool_reference (x);
4790 if (x != tmp)
4791 return x;
4794 /* Try to simplify X. If the simplification changed the code, it is likely
4795 that further simplification will help, so loop, but limit the number
4796 of repetitions that will be performed. */
4798 for (i = 0; i < 4; i++)
4800 /* If X is sufficiently simple, don't bother trying to do anything
4801 with it. */
4802 if (code != CONST_INT && code != REG && code != CLOBBER)
4803 x = combine_simplify_rtx (x, op0_mode, in_dest);
4805 if (GET_CODE (x) == code)
4806 break;
4808 code = GET_CODE (x);
4810 /* We no longer know the original mode of operand 0 since we
4811 have changed the form of X) */
4812 op0_mode = VOIDmode;
4815 return x;
4818 /* Simplify X, a piece of RTL. We just operate on the expression at the
4819 outer level; call `subst' to simplify recursively. Return the new
4820 expression.
4822 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4823 if we are inside a SET_DEST. */
4825 static rtx
4826 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4828 enum rtx_code code = GET_CODE (x);
4829 enum machine_mode mode = GET_MODE (x);
4830 rtx temp;
4831 int i;
4833 /* If this is a commutative operation, put a constant last and a complex
4834 expression first. We don't need to do this for comparisons here. */
4835 if (COMMUTATIVE_ARITH_P (x)
4836 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4838 temp = XEXP (x, 0);
4839 SUBST (XEXP (x, 0), XEXP (x, 1));
4840 SUBST (XEXP (x, 1), temp);
4843 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4844 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4845 things. Check for cases where both arms are testing the same
4846 condition.
4848 Don't do anything if all operands are very simple. */
4850 if ((BINARY_P (x)
4851 && ((!OBJECT_P (XEXP (x, 0))
4852 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4853 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4854 || (!OBJECT_P (XEXP (x, 1))
4855 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4856 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4857 || (UNARY_P (x)
4858 && (!OBJECT_P (XEXP (x, 0))
4859 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4860 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4862 rtx cond, true_rtx, false_rtx;
4864 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4865 if (cond != 0
4866 /* If everything is a comparison, what we have is highly unlikely
4867 to be simpler, so don't use it. */
4868 && ! (COMPARISON_P (x)
4869 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4871 rtx cop1 = const0_rtx;
4872 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4874 if (cond_code == NE && COMPARISON_P (cond))
4875 return x;
4877 /* Simplify the alternative arms; this may collapse the true and
4878 false arms to store-flag values. Be careful to use copy_rtx
4879 here since true_rtx or false_rtx might share RTL with x as a
4880 result of the if_then_else_cond call above. */
4881 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4882 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4884 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4885 is unlikely to be simpler. */
4886 if (general_operand (true_rtx, VOIDmode)
4887 && general_operand (false_rtx, VOIDmode))
4889 enum rtx_code reversed;
4891 /* Restarting if we generate a store-flag expression will cause
4892 us to loop. Just drop through in this case. */
4894 /* If the result values are STORE_FLAG_VALUE and zero, we can
4895 just make the comparison operation. */
4896 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4897 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4898 cond, cop1);
4899 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4900 && ((reversed = reversed_comparison_code_parts
4901 (cond_code, cond, cop1, NULL))
4902 != UNKNOWN))
4903 x = simplify_gen_relational (reversed, mode, VOIDmode,
4904 cond, cop1);
4906 /* Likewise, we can make the negate of a comparison operation
4907 if the result values are - STORE_FLAG_VALUE and zero. */
4908 else if (CONST_INT_P (true_rtx)
4909 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4910 && false_rtx == const0_rtx)
4911 x = simplify_gen_unary (NEG, mode,
4912 simplify_gen_relational (cond_code,
4913 mode, VOIDmode,
4914 cond, cop1),
4915 mode);
4916 else if (CONST_INT_P (false_rtx)
4917 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4918 && true_rtx == const0_rtx
4919 && ((reversed = reversed_comparison_code_parts
4920 (cond_code, cond, cop1, NULL))
4921 != UNKNOWN))
4922 x = simplify_gen_unary (NEG, mode,
4923 simplify_gen_relational (reversed,
4924 mode, VOIDmode,
4925 cond, cop1),
4926 mode);
4927 else
4928 return gen_rtx_IF_THEN_ELSE (mode,
4929 simplify_gen_relational (cond_code,
4930 mode,
4931 VOIDmode,
4932 cond,
4933 cop1),
4934 true_rtx, false_rtx);
4936 code = GET_CODE (x);
4937 op0_mode = VOIDmode;
4942 /* Try to fold this expression in case we have constants that weren't
4943 present before. */
4944 temp = 0;
4945 switch (GET_RTX_CLASS (code))
4947 case RTX_UNARY:
4948 if (op0_mode == VOIDmode)
4949 op0_mode = GET_MODE (XEXP (x, 0));
4950 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4951 break;
4952 case RTX_COMPARE:
4953 case RTX_COMM_COMPARE:
4955 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4956 if (cmp_mode == VOIDmode)
4958 cmp_mode = GET_MODE (XEXP (x, 1));
4959 if (cmp_mode == VOIDmode)
4960 cmp_mode = op0_mode;
4962 temp = simplify_relational_operation (code, mode, cmp_mode,
4963 XEXP (x, 0), XEXP (x, 1));
4965 break;
4966 case RTX_COMM_ARITH:
4967 case RTX_BIN_ARITH:
4968 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4969 break;
4970 case RTX_BITFIELD_OPS:
4971 case RTX_TERNARY:
4972 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4973 XEXP (x, 1), XEXP (x, 2));
4974 break;
4975 default:
4976 break;
4979 if (temp)
4981 x = temp;
4982 code = GET_CODE (temp);
4983 op0_mode = VOIDmode;
4984 mode = GET_MODE (temp);
4987 /* First see if we can apply the inverse distributive law. */
4988 if (code == PLUS || code == MINUS
4989 || code == AND || code == IOR || code == XOR)
4991 x = apply_distributive_law (x);
4992 code = GET_CODE (x);
4993 op0_mode = VOIDmode;
4996 /* If CODE is an associative operation not otherwise handled, see if we
4997 can associate some operands. This can win if they are constants or
4998 if they are logically related (i.e. (a & b) & a). */
4999 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5000 || code == AND || code == IOR || code == XOR
5001 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5002 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5003 || (flag_associative_math && FLOAT_MODE_P (mode))))
5005 if (GET_CODE (XEXP (x, 0)) == code)
5007 rtx other = XEXP (XEXP (x, 0), 0);
5008 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5009 rtx inner_op1 = XEXP (x, 1);
5010 rtx inner;
5012 /* Make sure we pass the constant operand if any as the second
5013 one if this is a commutative operation. */
5014 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5016 rtx tem = inner_op0;
5017 inner_op0 = inner_op1;
5018 inner_op1 = tem;
5020 inner = simplify_binary_operation (code == MINUS ? PLUS
5021 : code == DIV ? MULT
5022 : code,
5023 mode, inner_op0, inner_op1);
5025 /* For commutative operations, try the other pair if that one
5026 didn't simplify. */
5027 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5029 other = XEXP (XEXP (x, 0), 1);
5030 inner = simplify_binary_operation (code, mode,
5031 XEXP (XEXP (x, 0), 0),
5032 XEXP (x, 1));
5035 if (inner)
5036 return simplify_gen_binary (code, mode, other, inner);
5040 /* A little bit of algebraic simplification here. */
5041 switch (code)
5043 case MEM:
5044 /* Ensure that our address has any ASHIFTs converted to MULT in case
5045 address-recognizing predicates are called later. */
5046 temp = make_compound_operation (XEXP (x, 0), MEM);
5047 SUBST (XEXP (x, 0), temp);
5048 break;
5050 case SUBREG:
5051 if (op0_mode == VOIDmode)
5052 op0_mode = GET_MODE (SUBREG_REG (x));
5054 /* See if this can be moved to simplify_subreg. */
5055 if (CONSTANT_P (SUBREG_REG (x))
5056 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5057 /* Don't call gen_lowpart if the inner mode
5058 is VOIDmode and we cannot simplify it, as SUBREG without
5059 inner mode is invalid. */
5060 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5061 || gen_lowpart_common (mode, SUBREG_REG (x))))
5062 return gen_lowpart (mode, SUBREG_REG (x));
5064 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5065 break;
5067 rtx temp;
5068 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5069 SUBREG_BYTE (x));
5070 if (temp)
5071 return temp;
5074 /* Don't change the mode of the MEM if that would change the meaning
5075 of the address. */
5076 if (MEM_P (SUBREG_REG (x))
5077 && (MEM_VOLATILE_P (SUBREG_REG (x))
5078 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
5079 return gen_rtx_CLOBBER (mode, const0_rtx);
5081 /* Note that we cannot do any narrowing for non-constants since
5082 we might have been counting on using the fact that some bits were
5083 zero. We now do this in the SET. */
5085 break;
5087 case NEG:
5088 temp = expand_compound_operation (XEXP (x, 0));
5090 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5091 replaced by (lshiftrt X C). This will convert
5092 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5094 if (GET_CODE (temp) == ASHIFTRT
5095 && CONST_INT_P (XEXP (temp, 1))
5096 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
5097 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5098 INTVAL (XEXP (temp, 1)));
5100 /* If X has only a single bit that might be nonzero, say, bit I, convert
5101 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5102 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5103 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5104 or a SUBREG of one since we'd be making the expression more
5105 complex if it was just a register. */
5107 if (!REG_P (temp)
5108 && ! (GET_CODE (temp) == SUBREG
5109 && REG_P (SUBREG_REG (temp)))
5110 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5112 rtx temp1 = simplify_shift_const
5113 (NULL_RTX, ASHIFTRT, mode,
5114 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5115 GET_MODE_BITSIZE (mode) - 1 - i),
5116 GET_MODE_BITSIZE (mode) - 1 - i);
5118 /* If all we did was surround TEMP with the two shifts, we
5119 haven't improved anything, so don't use it. Otherwise,
5120 we are better off with TEMP1. */
5121 if (GET_CODE (temp1) != ASHIFTRT
5122 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5123 || XEXP (XEXP (temp1, 0), 0) != temp)
5124 return temp1;
5126 break;
5128 case TRUNCATE:
5129 /* We can't handle truncation to a partial integer mode here
5130 because we don't know the real bitsize of the partial
5131 integer mode. */
5132 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5133 break;
5135 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5136 SUBST (XEXP (x, 0),
5137 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5138 GET_MODE_MASK (mode), 0));
5140 /* We can truncate a constant value and return it. */
5141 if (CONST_INT_P (XEXP (x, 0)))
5142 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5144 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5145 whose value is a comparison can be replaced with a subreg if
5146 STORE_FLAG_VALUE permits. */
5147 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5148 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5149 && (temp = get_last_value (XEXP (x, 0)))
5150 && COMPARISON_P (temp))
5151 return gen_lowpart (mode, XEXP (x, 0));
5152 break;
5154 case CONST:
5155 /* (const (const X)) can become (const X). Do it this way rather than
5156 returning the inner CONST since CONST can be shared with a
5157 REG_EQUAL note. */
5158 if (GET_CODE (XEXP (x, 0)) == CONST)
5159 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5160 break;
5162 #ifdef HAVE_lo_sum
5163 case LO_SUM:
5164 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5165 can add in an offset. find_split_point will split this address up
5166 again if it doesn't match. */
5167 if (GET_CODE (XEXP (x, 0)) == HIGH
5168 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5169 return XEXP (x, 1);
5170 break;
5171 #endif
5173 case PLUS:
5174 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5175 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5176 bit-field and can be replaced by either a sign_extend or a
5177 sign_extract. The `and' may be a zero_extend and the two
5178 <c>, -<c> constants may be reversed. */
5179 if (GET_CODE (XEXP (x, 0)) == XOR
5180 && CONST_INT_P (XEXP (x, 1))
5181 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5182 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5183 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5184 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5185 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5186 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5187 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5188 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5189 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
5190 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5191 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5192 == (unsigned int) i + 1))))
5193 return simplify_shift_const
5194 (NULL_RTX, ASHIFTRT, mode,
5195 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5196 XEXP (XEXP (XEXP (x, 0), 0), 0),
5197 GET_MODE_BITSIZE (mode) - (i + 1)),
5198 GET_MODE_BITSIZE (mode) - (i + 1));
5200 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5201 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5202 the bitsize of the mode - 1. This allows simplification of
5203 "a = (b & 8) == 0;" */
5204 if (XEXP (x, 1) == constm1_rtx
5205 && !REG_P (XEXP (x, 0))
5206 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5207 && REG_P (SUBREG_REG (XEXP (x, 0))))
5208 && nonzero_bits (XEXP (x, 0), mode) == 1)
5209 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5210 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5211 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5212 GET_MODE_BITSIZE (mode) - 1),
5213 GET_MODE_BITSIZE (mode) - 1);
5215 /* If we are adding two things that have no bits in common, convert
5216 the addition into an IOR. This will often be further simplified,
5217 for example in cases like ((a & 1) + (a & 2)), which can
5218 become a & 3. */
5220 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5221 && (nonzero_bits (XEXP (x, 0), mode)
5222 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5224 /* Try to simplify the expression further. */
5225 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5226 temp = combine_simplify_rtx (tor, mode, in_dest);
5228 /* If we could, great. If not, do not go ahead with the IOR
5229 replacement, since PLUS appears in many special purpose
5230 address arithmetic instructions. */
5231 if (GET_CODE (temp) != CLOBBER && temp != tor)
5232 return temp;
5234 break;
5236 case MINUS:
5237 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5238 (and <foo> (const_int pow2-1)) */
5239 if (GET_CODE (XEXP (x, 1)) == AND
5240 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5241 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5242 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5243 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5244 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5245 break;
5247 case MULT:
5248 /* If we have (mult (plus A B) C), apply the distributive law and then
5249 the inverse distributive law to see if things simplify. This
5250 occurs mostly in addresses, often when unrolling loops. */
5252 if (GET_CODE (XEXP (x, 0)) == PLUS)
5254 rtx result = distribute_and_simplify_rtx (x, 0);
5255 if (result)
5256 return result;
5259 /* Try simplify a*(b/c) as (a*b)/c. */
5260 if (FLOAT_MODE_P (mode) && flag_associative_math
5261 && GET_CODE (XEXP (x, 0)) == DIV)
5263 rtx tem = simplify_binary_operation (MULT, mode,
5264 XEXP (XEXP (x, 0), 0),
5265 XEXP (x, 1));
5266 if (tem)
5267 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5269 break;
5271 case UDIV:
5272 /* If this is a divide by a power of two, treat it as a shift if
5273 its first operand is a shift. */
5274 if (CONST_INT_P (XEXP (x, 1))
5275 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
5276 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5277 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5278 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5279 || GET_CODE (XEXP (x, 0)) == ROTATE
5280 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5281 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5282 break;
5284 case EQ: case NE:
5285 case GT: case GTU: case GE: case GEU:
5286 case LT: case LTU: case LE: case LEU:
5287 case UNEQ: case LTGT:
5288 case UNGT: case UNGE:
5289 case UNLT: case UNLE:
5290 case UNORDERED: case ORDERED:
5291 /* If the first operand is a condition code, we can't do anything
5292 with it. */
5293 if (GET_CODE (XEXP (x, 0)) == COMPARE
5294 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5295 && ! CC0_P (XEXP (x, 0))))
5297 rtx op0 = XEXP (x, 0);
5298 rtx op1 = XEXP (x, 1);
5299 enum rtx_code new_code;
5301 if (GET_CODE (op0) == COMPARE)
5302 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5304 /* Simplify our comparison, if possible. */
5305 new_code = simplify_comparison (code, &op0, &op1);
5307 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5308 if only the low-order bit is possibly nonzero in X (such as when
5309 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5310 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5311 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5312 (plus X 1).
5314 Remove any ZERO_EXTRACT we made when thinking this was a
5315 comparison. It may now be simpler to use, e.g., an AND. If a
5316 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5317 the call to make_compound_operation in the SET case. */
5319 if (STORE_FLAG_VALUE == 1
5320 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5321 && op1 == const0_rtx
5322 && mode == GET_MODE (op0)
5323 && nonzero_bits (op0, mode) == 1)
5324 return gen_lowpart (mode,
5325 expand_compound_operation (op0));
5327 else if (STORE_FLAG_VALUE == 1
5328 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5329 && op1 == const0_rtx
5330 && mode == GET_MODE (op0)
5331 && (num_sign_bit_copies (op0, mode)
5332 == GET_MODE_BITSIZE (mode)))
5334 op0 = expand_compound_operation (op0);
5335 return simplify_gen_unary (NEG, mode,
5336 gen_lowpart (mode, op0),
5337 mode);
5340 else if (STORE_FLAG_VALUE == 1
5341 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5342 && op1 == const0_rtx
5343 && mode == GET_MODE (op0)
5344 && nonzero_bits (op0, mode) == 1)
5346 op0 = expand_compound_operation (op0);
5347 return simplify_gen_binary (XOR, mode,
5348 gen_lowpart (mode, op0),
5349 const1_rtx);
5352 else if (STORE_FLAG_VALUE == 1
5353 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5354 && op1 == const0_rtx
5355 && mode == GET_MODE (op0)
5356 && (num_sign_bit_copies (op0, mode)
5357 == GET_MODE_BITSIZE (mode)))
5359 op0 = expand_compound_operation (op0);
5360 return plus_constant (gen_lowpart (mode, op0), 1);
5363 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5364 those above. */
5365 if (STORE_FLAG_VALUE == -1
5366 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5367 && op1 == const0_rtx
5368 && (num_sign_bit_copies (op0, mode)
5369 == GET_MODE_BITSIZE (mode)))
5370 return gen_lowpart (mode,
5371 expand_compound_operation (op0));
5373 else if (STORE_FLAG_VALUE == -1
5374 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5375 && op1 == const0_rtx
5376 && mode == GET_MODE (op0)
5377 && nonzero_bits (op0, mode) == 1)
5379 op0 = expand_compound_operation (op0);
5380 return simplify_gen_unary (NEG, mode,
5381 gen_lowpart (mode, op0),
5382 mode);
5385 else if (STORE_FLAG_VALUE == -1
5386 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5387 && op1 == const0_rtx
5388 && mode == GET_MODE (op0)
5389 && (num_sign_bit_copies (op0, mode)
5390 == GET_MODE_BITSIZE (mode)))
5392 op0 = expand_compound_operation (op0);
5393 return simplify_gen_unary (NOT, mode,
5394 gen_lowpart (mode, op0),
5395 mode);
5398 /* If X is 0/1, (eq X 0) is X-1. */
5399 else if (STORE_FLAG_VALUE == -1
5400 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5401 && op1 == const0_rtx
5402 && mode == GET_MODE (op0)
5403 && nonzero_bits (op0, mode) == 1)
5405 op0 = expand_compound_operation (op0);
5406 return plus_constant (gen_lowpart (mode, op0), -1);
5409 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5410 one bit that might be nonzero, we can convert (ne x 0) to
5411 (ashift x c) where C puts the bit in the sign bit. Remove any
5412 AND with STORE_FLAG_VALUE when we are done, since we are only
5413 going to test the sign bit. */
5414 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5415 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5416 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5417 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5418 && op1 == const0_rtx
5419 && mode == GET_MODE (op0)
5420 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5422 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5423 expand_compound_operation (op0),
5424 GET_MODE_BITSIZE (mode) - 1 - i);
5425 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5426 return XEXP (x, 0);
5427 else
5428 return x;
5431 /* If the code changed, return a whole new comparison. */
5432 if (new_code != code)
5433 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5435 /* Otherwise, keep this operation, but maybe change its operands.
5436 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5437 SUBST (XEXP (x, 0), op0);
5438 SUBST (XEXP (x, 1), op1);
5440 break;
5442 case IF_THEN_ELSE:
5443 return simplify_if_then_else (x);
5445 case ZERO_EXTRACT:
5446 case SIGN_EXTRACT:
5447 case ZERO_EXTEND:
5448 case SIGN_EXTEND:
5449 /* If we are processing SET_DEST, we are done. */
5450 if (in_dest)
5451 return x;
5453 return expand_compound_operation (x);
5455 case SET:
5456 return simplify_set (x);
5458 case AND:
5459 case IOR:
5460 return simplify_logical (x);
5462 case ASHIFT:
5463 case LSHIFTRT:
5464 case ASHIFTRT:
5465 case ROTATE:
5466 case ROTATERT:
5467 /* If this is a shift by a constant amount, simplify it. */
5468 if (CONST_INT_P (XEXP (x, 1)))
5469 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5470 INTVAL (XEXP (x, 1)));
5472 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5473 SUBST (XEXP (x, 1),
5474 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5475 ((HOST_WIDE_INT) 1
5476 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5477 - 1,
5478 0));
5479 break;
5481 default:
5482 break;
5485 return x;
5488 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5490 static rtx
5491 simplify_if_then_else (rtx x)
5493 enum machine_mode mode = GET_MODE (x);
5494 rtx cond = XEXP (x, 0);
5495 rtx true_rtx = XEXP (x, 1);
5496 rtx false_rtx = XEXP (x, 2);
5497 enum rtx_code true_code = GET_CODE (cond);
5498 int comparison_p = COMPARISON_P (cond);
5499 rtx temp;
5500 int i;
5501 enum rtx_code false_code;
5502 rtx reversed;
5504 /* Simplify storing of the truth value. */
5505 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5506 return simplify_gen_relational (true_code, mode, VOIDmode,
5507 XEXP (cond, 0), XEXP (cond, 1));
5509 /* Also when the truth value has to be reversed. */
5510 if (comparison_p
5511 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5512 && (reversed = reversed_comparison (cond, mode)))
5513 return reversed;
5515 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5516 in it is being compared against certain values. Get the true and false
5517 comparisons and see if that says anything about the value of each arm. */
5519 if (comparison_p
5520 && ((false_code = reversed_comparison_code (cond, NULL))
5521 != UNKNOWN)
5522 && REG_P (XEXP (cond, 0)))
5524 HOST_WIDE_INT nzb;
5525 rtx from = XEXP (cond, 0);
5526 rtx true_val = XEXP (cond, 1);
5527 rtx false_val = true_val;
5528 int swapped = 0;
5530 /* If FALSE_CODE is EQ, swap the codes and arms. */
5532 if (false_code == EQ)
5534 swapped = 1, true_code = EQ, false_code = NE;
5535 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5538 /* If we are comparing against zero and the expression being tested has
5539 only a single bit that might be nonzero, that is its value when it is
5540 not equal to zero. Similarly if it is known to be -1 or 0. */
5542 if (true_code == EQ && true_val == const0_rtx
5543 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5545 false_code = EQ;
5546 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5548 else if (true_code == EQ && true_val == const0_rtx
5549 && (num_sign_bit_copies (from, GET_MODE (from))
5550 == GET_MODE_BITSIZE (GET_MODE (from))))
5552 false_code = EQ;
5553 false_val = constm1_rtx;
5556 /* Now simplify an arm if we know the value of the register in the
5557 branch and it is used in the arm. Be careful due to the potential
5558 of locally-shared RTL. */
5560 if (reg_mentioned_p (from, true_rtx))
5561 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5562 from, true_val),
5563 pc_rtx, pc_rtx, 0, 0);
5564 if (reg_mentioned_p (from, false_rtx))
5565 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5566 from, false_val),
5567 pc_rtx, pc_rtx, 0, 0);
5569 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5570 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5572 true_rtx = XEXP (x, 1);
5573 false_rtx = XEXP (x, 2);
5574 true_code = GET_CODE (cond);
5577 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5578 reversed, do so to avoid needing two sets of patterns for
5579 subtract-and-branch insns. Similarly if we have a constant in the true
5580 arm, the false arm is the same as the first operand of the comparison, or
5581 the false arm is more complicated than the true arm. */
5583 if (comparison_p
5584 && reversed_comparison_code (cond, NULL) != UNKNOWN
5585 && (true_rtx == pc_rtx
5586 || (CONSTANT_P (true_rtx)
5587 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
5588 || true_rtx == const0_rtx
5589 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5590 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5591 && !OBJECT_P (false_rtx))
5592 || reg_mentioned_p (true_rtx, false_rtx)
5593 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5595 true_code = reversed_comparison_code (cond, NULL);
5596 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5597 SUBST (XEXP (x, 1), false_rtx);
5598 SUBST (XEXP (x, 2), true_rtx);
5600 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5601 cond = XEXP (x, 0);
5603 /* It is possible that the conditional has been simplified out. */
5604 true_code = GET_CODE (cond);
5605 comparison_p = COMPARISON_P (cond);
5608 /* If the two arms are identical, we don't need the comparison. */
5610 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5611 return true_rtx;
5613 /* Convert a == b ? b : a to "a". */
5614 if (true_code == EQ && ! side_effects_p (cond)
5615 && !HONOR_NANS (mode)
5616 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5617 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5618 return false_rtx;
5619 else if (true_code == NE && ! side_effects_p (cond)
5620 && !HONOR_NANS (mode)
5621 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5622 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5623 return true_rtx;
5625 /* Look for cases where we have (abs x) or (neg (abs X)). */
5627 if (GET_MODE_CLASS (mode) == MODE_INT
5628 && comparison_p
5629 && XEXP (cond, 1) == const0_rtx
5630 && GET_CODE (false_rtx) == NEG
5631 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5632 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5633 && ! side_effects_p (true_rtx))
5634 switch (true_code)
5636 case GT:
5637 case GE:
5638 return simplify_gen_unary (ABS, mode, true_rtx, mode);
5639 case LT:
5640 case LE:
5641 return
5642 simplify_gen_unary (NEG, mode,
5643 simplify_gen_unary (ABS, mode, true_rtx, mode),
5644 mode);
5645 default:
5646 break;
5649 /* Look for MIN or MAX. */
5651 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5652 && comparison_p
5653 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5654 && rtx_equal_p (XEXP (cond, 1), false_rtx)
5655 && ! side_effects_p (cond))
5656 switch (true_code)
5658 case GE:
5659 case GT:
5660 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5661 case LE:
5662 case LT:
5663 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5664 case GEU:
5665 case GTU:
5666 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5667 case LEU:
5668 case LTU:
5669 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5670 default:
5671 break;
5674 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5675 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5676 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5677 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5678 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5679 neither 1 or -1, but it isn't worth checking for. */
5681 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5682 && comparison_p
5683 && GET_MODE_CLASS (mode) == MODE_INT
5684 && ! side_effects_p (x))
5686 rtx t = make_compound_operation (true_rtx, SET);
5687 rtx f = make_compound_operation (false_rtx, SET);
5688 rtx cond_op0 = XEXP (cond, 0);
5689 rtx cond_op1 = XEXP (cond, 1);
5690 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5691 enum machine_mode m = mode;
5692 rtx z = 0, c1 = NULL_RTX;
5694 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5695 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5696 || GET_CODE (t) == ASHIFT
5697 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5698 && rtx_equal_p (XEXP (t, 0), f))
5699 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5701 /* If an identity-zero op is commutative, check whether there
5702 would be a match if we swapped the operands. */
5703 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5704 || GET_CODE (t) == XOR)
5705 && rtx_equal_p (XEXP (t, 1), f))
5706 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5707 else if (GET_CODE (t) == SIGN_EXTEND
5708 && (GET_CODE (XEXP (t, 0)) == PLUS
5709 || GET_CODE (XEXP (t, 0)) == MINUS
5710 || GET_CODE (XEXP (t, 0)) == IOR
5711 || GET_CODE (XEXP (t, 0)) == XOR
5712 || GET_CODE (XEXP (t, 0)) == ASHIFT
5713 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5714 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5715 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5716 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5717 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5718 && (num_sign_bit_copies (f, GET_MODE (f))
5719 > (unsigned int)
5720 (GET_MODE_BITSIZE (mode)
5721 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5723 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5724 extend_op = SIGN_EXTEND;
5725 m = GET_MODE (XEXP (t, 0));
5727 else if (GET_CODE (t) == SIGN_EXTEND
5728 && (GET_CODE (XEXP (t, 0)) == PLUS
5729 || GET_CODE (XEXP (t, 0)) == IOR
5730 || GET_CODE (XEXP (t, 0)) == XOR)
5731 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5732 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5733 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5734 && (num_sign_bit_copies (f, GET_MODE (f))
5735 > (unsigned int)
5736 (GET_MODE_BITSIZE (mode)
5737 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5739 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5740 extend_op = SIGN_EXTEND;
5741 m = GET_MODE (XEXP (t, 0));
5743 else if (GET_CODE (t) == ZERO_EXTEND
5744 && (GET_CODE (XEXP (t, 0)) == PLUS
5745 || GET_CODE (XEXP (t, 0)) == MINUS
5746 || GET_CODE (XEXP (t, 0)) == IOR
5747 || GET_CODE (XEXP (t, 0)) == XOR
5748 || GET_CODE (XEXP (t, 0)) == ASHIFT
5749 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5750 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5751 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5752 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5753 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5754 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5755 && ((nonzero_bits (f, GET_MODE (f))
5756 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5757 == 0))
5759 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5760 extend_op = ZERO_EXTEND;
5761 m = GET_MODE (XEXP (t, 0));
5763 else if (GET_CODE (t) == ZERO_EXTEND
5764 && (GET_CODE (XEXP (t, 0)) == PLUS
5765 || GET_CODE (XEXP (t, 0)) == IOR
5766 || GET_CODE (XEXP (t, 0)) == XOR)
5767 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5768 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5769 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5770 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5771 && ((nonzero_bits (f, GET_MODE (f))
5772 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5773 == 0))
5775 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5776 extend_op = ZERO_EXTEND;
5777 m = GET_MODE (XEXP (t, 0));
5780 if (z)
5782 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5783 cond_op0, cond_op1),
5784 pc_rtx, pc_rtx, 0, 0);
5785 temp = simplify_gen_binary (MULT, m, temp,
5786 simplify_gen_binary (MULT, m, c1,
5787 const_true_rtx));
5788 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5789 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5791 if (extend_op != UNKNOWN)
5792 temp = simplify_gen_unary (extend_op, mode, temp, m);
5794 return temp;
5798 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5799 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5800 negation of a single bit, we can convert this operation to a shift. We
5801 can actually do this more generally, but it doesn't seem worth it. */
5803 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5804 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
5805 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5806 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5807 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5808 == GET_MODE_BITSIZE (mode))
5809 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5810 return
5811 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5812 gen_lowpart (mode, XEXP (cond, 0)), i);
5814 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5815 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5816 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
5817 && GET_MODE (XEXP (cond, 0)) == mode
5818 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5819 == nonzero_bits (XEXP (cond, 0), mode)
5820 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5821 return XEXP (cond, 0);
5823 return x;
5826 /* Simplify X, a SET expression. Return the new expression. */
5828 static rtx
5829 simplify_set (rtx x)
5831 rtx src = SET_SRC (x);
5832 rtx dest = SET_DEST (x);
5833 enum machine_mode mode
5834 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5835 rtx other_insn;
5836 rtx *cc_use;
5838 /* (set (pc) (return)) gets written as (return). */
5839 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5840 return src;
5842 /* Now that we know for sure which bits of SRC we are using, see if we can
5843 simplify the expression for the object knowing that we only need the
5844 low-order bits. */
5846 if (GET_MODE_CLASS (mode) == MODE_INT
5847 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5849 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5850 SUBST (SET_SRC (x), src);
5853 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5854 the comparison result and try to simplify it unless we already have used
5855 undobuf.other_insn. */
5856 if ((GET_MODE_CLASS (mode) == MODE_CC
5857 || GET_CODE (src) == COMPARE
5858 || CC0_P (dest))
5859 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5860 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5861 && COMPARISON_P (*cc_use)
5862 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5864 enum rtx_code old_code = GET_CODE (*cc_use);
5865 enum rtx_code new_code;
5866 rtx op0, op1, tmp;
5867 int other_changed = 0;
5868 enum machine_mode compare_mode = GET_MODE (dest);
5870 if (GET_CODE (src) == COMPARE)
5871 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5872 else
5873 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5875 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5876 op0, op1);
5877 if (!tmp)
5878 new_code = old_code;
5879 else if (!CONSTANT_P (tmp))
5881 new_code = GET_CODE (tmp);
5882 op0 = XEXP (tmp, 0);
5883 op1 = XEXP (tmp, 1);
5885 else
5887 rtx pat = PATTERN (other_insn);
5888 undobuf.other_insn = other_insn;
5889 SUBST (*cc_use, tmp);
5891 /* Attempt to simplify CC user. */
5892 if (GET_CODE (pat) == SET)
5894 rtx new_rtx = simplify_rtx (SET_SRC (pat));
5895 if (new_rtx != NULL_RTX)
5896 SUBST (SET_SRC (pat), new_rtx);
5899 /* Convert X into a no-op move. */
5900 SUBST (SET_DEST (x), pc_rtx);
5901 SUBST (SET_SRC (x), pc_rtx);
5902 return x;
5905 /* Simplify our comparison, if possible. */
5906 new_code = simplify_comparison (new_code, &op0, &op1);
5908 #ifdef SELECT_CC_MODE
5909 /* If this machine has CC modes other than CCmode, check to see if we
5910 need to use a different CC mode here. */
5911 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5912 compare_mode = GET_MODE (op0);
5913 else
5914 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5916 #ifndef HAVE_cc0
5917 /* If the mode changed, we have to change SET_DEST, the mode in the
5918 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5919 a hard register, just build new versions with the proper mode. If it
5920 is a pseudo, we lose unless it is only time we set the pseudo, in
5921 which case we can safely change its mode. */
5922 if (compare_mode != GET_MODE (dest))
5924 if (can_change_dest_mode (dest, 0, compare_mode))
5926 unsigned int regno = REGNO (dest);
5927 rtx new_dest;
5929 if (regno < FIRST_PSEUDO_REGISTER)
5930 new_dest = gen_rtx_REG (compare_mode, regno);
5931 else
5933 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5934 new_dest = regno_reg_rtx[regno];
5937 SUBST (SET_DEST (x), new_dest);
5938 SUBST (XEXP (*cc_use, 0), new_dest);
5939 other_changed = 1;
5941 dest = new_dest;
5944 #endif /* cc0 */
5945 #endif /* SELECT_CC_MODE */
5947 /* If the code changed, we have to build a new comparison in
5948 undobuf.other_insn. */
5949 if (new_code != old_code)
5951 int other_changed_previously = other_changed;
5952 unsigned HOST_WIDE_INT mask;
5953 rtx old_cc_use = *cc_use;
5955 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5956 dest, const0_rtx));
5957 other_changed = 1;
5959 /* If the only change we made was to change an EQ into an NE or
5960 vice versa, OP0 has only one bit that might be nonzero, and OP1
5961 is zero, check if changing the user of the condition code will
5962 produce a valid insn. If it won't, we can keep the original code
5963 in that insn by surrounding our operation with an XOR. */
5965 if (((old_code == NE && new_code == EQ)
5966 || (old_code == EQ && new_code == NE))
5967 && ! other_changed_previously && op1 == const0_rtx
5968 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5969 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5971 rtx pat = PATTERN (other_insn), note = 0;
5973 if ((recog_for_combine (&pat, other_insn, &note) < 0
5974 && ! check_asm_operands (pat)))
5976 *cc_use = old_cc_use;
5977 other_changed = 0;
5979 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5980 op0, GEN_INT (mask));
5985 if (other_changed)
5986 undobuf.other_insn = other_insn;
5988 /* Otherwise, if we didn't previously have a COMPARE in the
5989 correct mode, we need one. */
5990 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5992 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5993 src = SET_SRC (x);
5995 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5997 SUBST (SET_SRC (x), op0);
5998 src = SET_SRC (x);
6000 /* Otherwise, update the COMPARE if needed. */
6001 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6003 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6004 src = SET_SRC (x);
6007 else
6009 /* Get SET_SRC in a form where we have placed back any
6010 compound expressions. Then do the checks below. */
6011 src = make_compound_operation (src, SET);
6012 SUBST (SET_SRC (x), src);
6015 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6016 and X being a REG or (subreg (reg)), we may be able to convert this to
6017 (set (subreg:m2 x) (op)).
6019 We can always do this if M1 is narrower than M2 because that means that
6020 we only care about the low bits of the result.
6022 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6023 perform a narrower operation than requested since the high-order bits will
6024 be undefined. On machine where it is defined, this transformation is safe
6025 as long as M1 and M2 have the same number of words. */
6027 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6028 && !OBJECT_P (SUBREG_REG (src))
6029 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6030 / UNITS_PER_WORD)
6031 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6032 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6033 #ifndef WORD_REGISTER_OPERATIONS
6034 && (GET_MODE_SIZE (GET_MODE (src))
6035 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6036 #endif
6037 #ifdef CANNOT_CHANGE_MODE_CLASS
6038 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6039 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6040 GET_MODE (SUBREG_REG (src)),
6041 GET_MODE (src)))
6042 #endif
6043 && (REG_P (dest)
6044 || (GET_CODE (dest) == SUBREG
6045 && REG_P (SUBREG_REG (dest)))))
6047 SUBST (SET_DEST (x),
6048 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6049 dest));
6050 SUBST (SET_SRC (x), SUBREG_REG (src));
6052 src = SET_SRC (x), dest = SET_DEST (x);
6055 #ifdef HAVE_cc0
6056 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6057 in SRC. */
6058 if (dest == cc0_rtx
6059 && GET_CODE (src) == SUBREG
6060 && subreg_lowpart_p (src)
6061 && (GET_MODE_BITSIZE (GET_MODE (src))
6062 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
6064 rtx inner = SUBREG_REG (src);
6065 enum machine_mode inner_mode = GET_MODE (inner);
6067 /* Here we make sure that we don't have a sign bit on. */
6068 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
6069 && (nonzero_bits (inner, inner_mode)
6070 < ((unsigned HOST_WIDE_INT) 1
6071 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
6073 SUBST (SET_SRC (x), inner);
6074 src = SET_SRC (x);
6077 #endif
6079 #ifdef LOAD_EXTEND_OP
6080 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6081 would require a paradoxical subreg. Replace the subreg with a
6082 zero_extend to avoid the reload that would otherwise be required. */
6084 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6085 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6086 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6087 && SUBREG_BYTE (src) == 0
6088 && (GET_MODE_SIZE (GET_MODE (src))
6089 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6090 && MEM_P (SUBREG_REG (src)))
6092 SUBST (SET_SRC (x),
6093 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6094 GET_MODE (src), SUBREG_REG (src)));
6096 src = SET_SRC (x);
6098 #endif
6100 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6101 are comparing an item known to be 0 or -1 against 0, use a logical
6102 operation instead. Check for one of the arms being an IOR of the other
6103 arm with some value. We compute three terms to be IOR'ed together. In
6104 practice, at most two will be nonzero. Then we do the IOR's. */
6106 if (GET_CODE (dest) != PC
6107 && GET_CODE (src) == IF_THEN_ELSE
6108 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6109 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6110 && XEXP (XEXP (src, 0), 1) == const0_rtx
6111 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6112 #ifdef HAVE_conditional_move
6113 && ! can_conditionally_move_p (GET_MODE (src))
6114 #endif
6115 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6116 GET_MODE (XEXP (XEXP (src, 0), 0)))
6117 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
6118 && ! side_effects_p (src))
6120 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6121 ? XEXP (src, 1) : XEXP (src, 2));
6122 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6123 ? XEXP (src, 2) : XEXP (src, 1));
6124 rtx term1 = const0_rtx, term2, term3;
6126 if (GET_CODE (true_rtx) == IOR
6127 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6128 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6129 else if (GET_CODE (true_rtx) == IOR
6130 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6131 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6132 else if (GET_CODE (false_rtx) == IOR
6133 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6134 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6135 else if (GET_CODE (false_rtx) == IOR
6136 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6137 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6139 term2 = simplify_gen_binary (AND, GET_MODE (src),
6140 XEXP (XEXP (src, 0), 0), true_rtx);
6141 term3 = simplify_gen_binary (AND, GET_MODE (src),
6142 simplify_gen_unary (NOT, GET_MODE (src),
6143 XEXP (XEXP (src, 0), 0),
6144 GET_MODE (src)),
6145 false_rtx);
6147 SUBST (SET_SRC (x),
6148 simplify_gen_binary (IOR, GET_MODE (src),
6149 simplify_gen_binary (IOR, GET_MODE (src),
6150 term1, term2),
6151 term3));
6153 src = SET_SRC (x);
6156 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6157 whole thing fail. */
6158 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6159 return src;
6160 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6161 return dest;
6162 else
6163 /* Convert this into a field assignment operation, if possible. */
6164 return make_field_assignment (x);
6167 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6168 result. */
6170 static rtx
6171 simplify_logical (rtx x)
6173 enum machine_mode mode = GET_MODE (x);
6174 rtx op0 = XEXP (x, 0);
6175 rtx op1 = XEXP (x, 1);
6177 switch (GET_CODE (x))
6179 case AND:
6180 /* We can call simplify_and_const_int only if we don't lose
6181 any (sign) bits when converting INTVAL (op1) to
6182 "unsigned HOST_WIDE_INT". */
6183 if (CONST_INT_P (op1)
6184 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
6185 || INTVAL (op1) > 0))
6187 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6188 if (GET_CODE (x) != AND)
6189 return x;
6191 op0 = XEXP (x, 0);
6192 op1 = XEXP (x, 1);
6195 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6196 apply the distributive law and then the inverse distributive
6197 law to see if things simplify. */
6198 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6200 rtx result = distribute_and_simplify_rtx (x, 0);
6201 if (result)
6202 return result;
6204 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6206 rtx result = distribute_and_simplify_rtx (x, 1);
6207 if (result)
6208 return result;
6210 break;
6212 case IOR:
6213 /* If we have (ior (and A B) C), apply the distributive law and then
6214 the inverse distributive law to see if things simplify. */
6216 if (GET_CODE (op0) == AND)
6218 rtx result = distribute_and_simplify_rtx (x, 0);
6219 if (result)
6220 return result;
6223 if (GET_CODE (op1) == AND)
6225 rtx result = distribute_and_simplify_rtx (x, 1);
6226 if (result)
6227 return result;
6229 break;
6231 default:
6232 gcc_unreachable ();
6235 return x;
6238 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6239 operations" because they can be replaced with two more basic operations.
6240 ZERO_EXTEND is also considered "compound" because it can be replaced with
6241 an AND operation, which is simpler, though only one operation.
6243 The function expand_compound_operation is called with an rtx expression
6244 and will convert it to the appropriate shifts and AND operations,
6245 simplifying at each stage.
6247 The function make_compound_operation is called to convert an expression
6248 consisting of shifts and ANDs into the equivalent compound expression.
6249 It is the inverse of this function, loosely speaking. */
6251 static rtx
6252 expand_compound_operation (rtx x)
6254 unsigned HOST_WIDE_INT pos = 0, len;
6255 int unsignedp = 0;
6256 unsigned int modewidth;
6257 rtx tem;
6259 switch (GET_CODE (x))
6261 case ZERO_EXTEND:
6262 unsignedp = 1;
6263 case SIGN_EXTEND:
6264 /* We can't necessarily use a const_int for a multiword mode;
6265 it depends on implicitly extending the value.
6266 Since we don't know the right way to extend it,
6267 we can't tell whether the implicit way is right.
6269 Even for a mode that is no wider than a const_int,
6270 we can't win, because we need to sign extend one of its bits through
6271 the rest of it, and we don't know which bit. */
6272 if (CONST_INT_P (XEXP (x, 0)))
6273 return x;
6275 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6276 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6277 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6278 reloaded. If not for that, MEM's would very rarely be safe.
6280 Reject MODEs bigger than a word, because we might not be able
6281 to reference a two-register group starting with an arbitrary register
6282 (and currently gen_lowpart might crash for a SUBREG). */
6284 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6285 return x;
6287 /* Reject MODEs that aren't scalar integers because turning vector
6288 or complex modes into shifts causes problems. */
6290 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6291 return x;
6293 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6294 /* If the inner object has VOIDmode (the only way this can happen
6295 is if it is an ASM_OPERANDS), we can't do anything since we don't
6296 know how much masking to do. */
6297 if (len == 0)
6298 return x;
6300 break;
6302 case ZERO_EXTRACT:
6303 unsignedp = 1;
6305 /* ... fall through ... */
6307 case SIGN_EXTRACT:
6308 /* If the operand is a CLOBBER, just return it. */
6309 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6310 return XEXP (x, 0);
6312 if (!CONST_INT_P (XEXP (x, 1))
6313 || !CONST_INT_P (XEXP (x, 2))
6314 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6315 return x;
6317 /* Reject MODEs that aren't scalar integers because turning vector
6318 or complex modes into shifts causes problems. */
6320 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6321 return x;
6323 len = INTVAL (XEXP (x, 1));
6324 pos = INTVAL (XEXP (x, 2));
6326 /* This should stay within the object being extracted, fail otherwise. */
6327 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6328 return x;
6330 if (BITS_BIG_ENDIAN)
6331 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6333 break;
6335 default:
6336 return x;
6338 /* Convert sign extension to zero extension, if we know that the high
6339 bit is not set, as this is easier to optimize. It will be converted
6340 back to cheaper alternative in make_extraction. */
6341 if (GET_CODE (x) == SIGN_EXTEND
6342 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6343 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6344 & ~(((unsigned HOST_WIDE_INT)
6345 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6346 >> 1))
6347 == 0)))
6349 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6350 rtx temp2 = expand_compound_operation (temp);
6352 /* Make sure this is a profitable operation. */
6353 if (rtx_cost (x, SET, optimize_this_for_speed_p)
6354 > rtx_cost (temp2, SET, optimize_this_for_speed_p))
6355 return temp2;
6356 else if (rtx_cost (x, SET, optimize_this_for_speed_p)
6357 > rtx_cost (temp, SET, optimize_this_for_speed_p))
6358 return temp;
6359 else
6360 return x;
6363 /* We can optimize some special cases of ZERO_EXTEND. */
6364 if (GET_CODE (x) == ZERO_EXTEND)
6366 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6367 know that the last value didn't have any inappropriate bits
6368 set. */
6369 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6370 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6371 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6372 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6373 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6374 return XEXP (XEXP (x, 0), 0);
6376 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6377 if (GET_CODE (XEXP (x, 0)) == SUBREG
6378 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6379 && subreg_lowpart_p (XEXP (x, 0))
6380 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6381 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6382 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6383 return SUBREG_REG (XEXP (x, 0));
6385 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6386 is a comparison and STORE_FLAG_VALUE permits. This is like
6387 the first case, but it works even when GET_MODE (x) is larger
6388 than HOST_WIDE_INT. */
6389 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6390 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6391 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6392 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6393 <= HOST_BITS_PER_WIDE_INT)
6394 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6395 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6396 return XEXP (XEXP (x, 0), 0);
6398 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6399 if (GET_CODE (XEXP (x, 0)) == SUBREG
6400 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6401 && subreg_lowpart_p (XEXP (x, 0))
6402 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6403 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6404 <= HOST_BITS_PER_WIDE_INT)
6405 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6406 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6407 return SUBREG_REG (XEXP (x, 0));
6411 /* If we reach here, we want to return a pair of shifts. The inner
6412 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6413 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6414 logical depending on the value of UNSIGNEDP.
6416 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6417 converted into an AND of a shift.
6419 We must check for the case where the left shift would have a negative
6420 count. This can happen in a case like (x >> 31) & 255 on machines
6421 that can't shift by a constant. On those machines, we would first
6422 combine the shift with the AND to produce a variable-position
6423 extraction. Then the constant of 31 would be substituted in to produce
6424 a such a position. */
6426 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6427 if (modewidth + len >= pos)
6429 enum machine_mode mode = GET_MODE (x);
6430 tem = gen_lowpart (mode, XEXP (x, 0));
6431 if (!tem || GET_CODE (tem) == CLOBBER)
6432 return x;
6433 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6434 tem, modewidth - pos - len);
6435 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6436 mode, tem, modewidth - len);
6438 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6439 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6440 simplify_shift_const (NULL_RTX, LSHIFTRT,
6441 GET_MODE (x),
6442 XEXP (x, 0), pos),
6443 ((HOST_WIDE_INT) 1 << len) - 1);
6444 else
6445 /* Any other cases we can't handle. */
6446 return x;
6448 /* If we couldn't do this for some reason, return the original
6449 expression. */
6450 if (GET_CODE (tem) == CLOBBER)
6451 return x;
6453 return tem;
6456 /* X is a SET which contains an assignment of one object into
6457 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6458 or certain SUBREGS). If possible, convert it into a series of
6459 logical operations.
6461 We half-heartedly support variable positions, but do not at all
6462 support variable lengths. */
6464 static const_rtx
6465 expand_field_assignment (const_rtx x)
6467 rtx inner;
6468 rtx pos; /* Always counts from low bit. */
6469 int len;
6470 rtx mask, cleared, masked;
6471 enum machine_mode compute_mode;
6473 /* Loop until we find something we can't simplify. */
6474 while (1)
6476 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6477 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6479 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6480 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6481 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6483 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6484 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6486 inner = XEXP (SET_DEST (x), 0);
6487 len = INTVAL (XEXP (SET_DEST (x), 1));
6488 pos = XEXP (SET_DEST (x), 2);
6490 /* A constant position should stay within the width of INNER. */
6491 if (CONST_INT_P (pos)
6492 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6493 break;
6495 if (BITS_BIG_ENDIAN)
6497 if (CONST_INT_P (pos))
6498 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6499 - INTVAL (pos));
6500 else if (GET_CODE (pos) == MINUS
6501 && CONST_INT_P (XEXP (pos, 1))
6502 && (INTVAL (XEXP (pos, 1))
6503 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6504 /* If position is ADJUST - X, new position is X. */
6505 pos = XEXP (pos, 0);
6506 else
6507 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6508 GEN_INT (GET_MODE_BITSIZE (
6509 GET_MODE (inner))
6510 - len),
6511 pos);
6515 /* A SUBREG between two modes that occupy the same numbers of words
6516 can be done by moving the SUBREG to the source. */
6517 else if (GET_CODE (SET_DEST (x)) == SUBREG
6518 /* We need SUBREGs to compute nonzero_bits properly. */
6519 && nonzero_sign_valid
6520 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6521 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6522 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6523 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6525 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6526 gen_lowpart
6527 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6528 SET_SRC (x)));
6529 continue;
6531 else
6532 break;
6534 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6535 inner = SUBREG_REG (inner);
6537 compute_mode = GET_MODE (inner);
6539 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6540 if (! SCALAR_INT_MODE_P (compute_mode))
6542 enum machine_mode imode;
6544 /* Don't do anything for vector or complex integral types. */
6545 if (! FLOAT_MODE_P (compute_mode))
6546 break;
6548 /* Try to find an integral mode to pun with. */
6549 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6550 if (imode == BLKmode)
6551 break;
6553 compute_mode = imode;
6554 inner = gen_lowpart (imode, inner);
6557 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6558 if (len >= HOST_BITS_PER_WIDE_INT)
6559 break;
6561 /* Now compute the equivalent expression. Make a copy of INNER
6562 for the SET_DEST in case it is a MEM into which we will substitute;
6563 we don't want shared RTL in that case. */
6564 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6565 cleared = simplify_gen_binary (AND, compute_mode,
6566 simplify_gen_unary (NOT, compute_mode,
6567 simplify_gen_binary (ASHIFT,
6568 compute_mode,
6569 mask, pos),
6570 compute_mode),
6571 inner);
6572 masked = simplify_gen_binary (ASHIFT, compute_mode,
6573 simplify_gen_binary (
6574 AND, compute_mode,
6575 gen_lowpart (compute_mode, SET_SRC (x)),
6576 mask),
6577 pos);
6579 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6580 simplify_gen_binary (IOR, compute_mode,
6581 cleared, masked));
6584 return x;
6587 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6588 it is an RTX that represents a variable starting position; otherwise,
6589 POS is the (constant) starting bit position (counted from the LSB).
6591 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6592 signed reference.
6594 IN_DEST is nonzero if this is a reference in the destination of a
6595 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6596 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6597 be used.
6599 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6600 ZERO_EXTRACT should be built even for bits starting at bit 0.
6602 MODE is the desired mode of the result (if IN_DEST == 0).
6604 The result is an RTX for the extraction or NULL_RTX if the target
6605 can't handle it. */
6607 static rtx
6608 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6609 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6610 int in_dest, int in_compare)
6612 /* This mode describes the size of the storage area
6613 to fetch the overall value from. Within that, we
6614 ignore the POS lowest bits, etc. */
6615 enum machine_mode is_mode = GET_MODE (inner);
6616 enum machine_mode inner_mode;
6617 enum machine_mode wanted_inner_mode;
6618 enum machine_mode wanted_inner_reg_mode = word_mode;
6619 enum machine_mode pos_mode = word_mode;
6620 enum machine_mode extraction_mode = word_mode;
6621 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6622 rtx new_rtx = 0;
6623 rtx orig_pos_rtx = pos_rtx;
6624 HOST_WIDE_INT orig_pos;
6626 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6628 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6629 consider just the QI as the memory to extract from.
6630 The subreg adds or removes high bits; its mode is
6631 irrelevant to the meaning of this extraction,
6632 since POS and LEN count from the lsb. */
6633 if (MEM_P (SUBREG_REG (inner)))
6634 is_mode = GET_MODE (SUBREG_REG (inner));
6635 inner = SUBREG_REG (inner);
6637 else if (GET_CODE (inner) == ASHIFT
6638 && CONST_INT_P (XEXP (inner, 1))
6639 && pos_rtx == 0 && pos == 0
6640 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6642 /* We're extracting the least significant bits of an rtx
6643 (ashift X (const_int C)), where LEN > C. Extract the
6644 least significant (LEN - C) bits of X, giving an rtx
6645 whose mode is MODE, then shift it left C times. */
6646 new_rtx = make_extraction (mode, XEXP (inner, 0),
6647 0, 0, len - INTVAL (XEXP (inner, 1)),
6648 unsignedp, in_dest, in_compare);
6649 if (new_rtx != 0)
6650 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
6653 inner_mode = GET_MODE (inner);
6655 if (pos_rtx && CONST_INT_P (pos_rtx))
6656 pos = INTVAL (pos_rtx), pos_rtx = 0;
6658 /* See if this can be done without an extraction. We never can if the
6659 width of the field is not the same as that of some integer mode. For
6660 registers, we can only avoid the extraction if the position is at the
6661 low-order bit and this is either not in the destination or we have the
6662 appropriate STRICT_LOW_PART operation available.
6664 For MEM, we can avoid an extract if the field starts on an appropriate
6665 boundary and we can change the mode of the memory reference. */
6667 if (tmode != BLKmode
6668 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6669 && !MEM_P (inner)
6670 && (inner_mode == tmode
6671 || !REG_P (inner)
6672 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6673 GET_MODE_BITSIZE (inner_mode))
6674 || reg_truncated_to_mode (tmode, inner))
6675 && (! in_dest
6676 || (REG_P (inner)
6677 && have_insn_for (STRICT_LOW_PART, tmode))))
6678 || (MEM_P (inner) && pos_rtx == 0
6679 && (pos
6680 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6681 : BITS_PER_UNIT)) == 0
6682 /* We can't do this if we are widening INNER_MODE (it
6683 may not be aligned, for one thing). */
6684 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6685 && (inner_mode == tmode
6686 || (! mode_dependent_address_p (XEXP (inner, 0))
6687 && ! MEM_VOLATILE_P (inner))))))
6689 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6690 field. If the original and current mode are the same, we need not
6691 adjust the offset. Otherwise, we do if bytes big endian.
6693 If INNER is not a MEM, get a piece consisting of just the field
6694 of interest (in this case POS % BITS_PER_WORD must be 0). */
6696 if (MEM_P (inner))
6698 HOST_WIDE_INT offset;
6700 /* POS counts from lsb, but make OFFSET count in memory order. */
6701 if (BYTES_BIG_ENDIAN)
6702 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6703 else
6704 offset = pos / BITS_PER_UNIT;
6706 new_rtx = adjust_address_nv (inner, tmode, offset);
6708 else if (REG_P (inner))
6710 if (tmode != inner_mode)
6712 /* We can't call gen_lowpart in a DEST since we
6713 always want a SUBREG (see below) and it would sometimes
6714 return a new hard register. */
6715 if (pos || in_dest)
6717 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6719 if (WORDS_BIG_ENDIAN
6720 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6721 final_word = ((GET_MODE_SIZE (inner_mode)
6722 - GET_MODE_SIZE (tmode))
6723 / UNITS_PER_WORD) - final_word;
6725 final_word *= UNITS_PER_WORD;
6726 if (BYTES_BIG_ENDIAN &&
6727 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6728 final_word += (GET_MODE_SIZE (inner_mode)
6729 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6731 /* Avoid creating invalid subregs, for example when
6732 simplifying (x>>32)&255. */
6733 if (!validate_subreg (tmode, inner_mode, inner, final_word))
6734 return NULL_RTX;
6736 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
6738 else
6739 new_rtx = gen_lowpart (tmode, inner);
6741 else
6742 new_rtx = inner;
6744 else
6745 new_rtx = force_to_mode (inner, tmode,
6746 len >= HOST_BITS_PER_WIDE_INT
6747 ? ~(unsigned HOST_WIDE_INT) 0
6748 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6751 /* If this extraction is going into the destination of a SET,
6752 make a STRICT_LOW_PART unless we made a MEM. */
6754 if (in_dest)
6755 return (MEM_P (new_rtx) ? new_rtx
6756 : (GET_CODE (new_rtx) != SUBREG
6757 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6758 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
6760 if (mode == tmode)
6761 return new_rtx;
6763 if (CONST_INT_P (new_rtx)
6764 || GET_CODE (new_rtx) == CONST_DOUBLE)
6765 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6766 mode, new_rtx, tmode);
6768 /* If we know that no extraneous bits are set, and that the high
6769 bit is not set, convert the extraction to the cheaper of
6770 sign and zero extension, that are equivalent in these cases. */
6771 if (flag_expensive_optimizations
6772 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6773 && ((nonzero_bits (new_rtx, tmode)
6774 & ~(((unsigned HOST_WIDE_INT)
6775 GET_MODE_MASK (tmode))
6776 >> 1))
6777 == 0)))
6779 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
6780 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
6782 /* Prefer ZERO_EXTENSION, since it gives more information to
6783 backends. */
6784 if (rtx_cost (temp, SET, optimize_this_for_speed_p)
6785 <= rtx_cost (temp1, SET, optimize_this_for_speed_p))
6786 return temp;
6787 return temp1;
6790 /* Otherwise, sign- or zero-extend unless we already are in the
6791 proper mode. */
6793 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6794 mode, new_rtx));
6797 /* Unless this is a COMPARE or we have a funny memory reference,
6798 don't do anything with zero-extending field extracts starting at
6799 the low-order bit since they are simple AND operations. */
6800 if (pos_rtx == 0 && pos == 0 && ! in_dest
6801 && ! in_compare && unsignedp)
6802 return 0;
6804 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6805 if the position is not a constant and the length is not 1. In all
6806 other cases, we would only be going outside our object in cases when
6807 an original shift would have been undefined. */
6808 if (MEM_P (inner)
6809 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6810 || (pos_rtx != 0 && len != 1)))
6811 return 0;
6813 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6814 and the mode for the result. */
6815 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6817 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6818 pos_mode = mode_for_extraction (EP_insv, 2);
6819 extraction_mode = mode_for_extraction (EP_insv, 3);
6822 if (! in_dest && unsignedp
6823 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6825 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6826 pos_mode = mode_for_extraction (EP_extzv, 3);
6827 extraction_mode = mode_for_extraction (EP_extzv, 0);
6830 if (! in_dest && ! unsignedp
6831 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6833 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6834 pos_mode = mode_for_extraction (EP_extv, 3);
6835 extraction_mode = mode_for_extraction (EP_extv, 0);
6838 /* Never narrow an object, since that might not be safe. */
6840 if (mode != VOIDmode
6841 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6842 extraction_mode = mode;
6844 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6845 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6846 pos_mode = GET_MODE (pos_rtx);
6848 /* If this is not from memory, the desired mode is the preferred mode
6849 for an extraction pattern's first input operand, or word_mode if there
6850 is none. */
6851 if (!MEM_P (inner))
6852 wanted_inner_mode = wanted_inner_reg_mode;
6853 else
6855 /* Be careful not to go beyond the extracted object and maintain the
6856 natural alignment of the memory. */
6857 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6858 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6859 > GET_MODE_BITSIZE (wanted_inner_mode))
6861 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6862 gcc_assert (wanted_inner_mode != VOIDmode);
6865 /* If we have to change the mode of memory and cannot, the desired mode
6866 is EXTRACTION_MODE. */
6867 if (inner_mode != wanted_inner_mode
6868 && (mode_dependent_address_p (XEXP (inner, 0))
6869 || MEM_VOLATILE_P (inner)
6870 || pos_rtx))
6871 wanted_inner_mode = extraction_mode;
6874 orig_pos = pos;
6876 if (BITS_BIG_ENDIAN)
6878 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6879 BITS_BIG_ENDIAN style. If position is constant, compute new
6880 position. Otherwise, build subtraction.
6881 Note that POS is relative to the mode of the original argument.
6882 If it's a MEM we need to recompute POS relative to that.
6883 However, if we're extracting from (or inserting into) a register,
6884 we want to recompute POS relative to wanted_inner_mode. */
6885 int width = (MEM_P (inner)
6886 ? GET_MODE_BITSIZE (is_mode)
6887 : GET_MODE_BITSIZE (wanted_inner_mode));
6889 if (pos_rtx == 0)
6890 pos = width - len - pos;
6891 else
6892 pos_rtx
6893 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6894 /* POS may be less than 0 now, but we check for that below.
6895 Note that it can only be less than 0 if !MEM_P (inner). */
6898 /* If INNER has a wider mode, and this is a constant extraction, try to
6899 make it smaller and adjust the byte to point to the byte containing
6900 the value. */
6901 if (wanted_inner_mode != VOIDmode
6902 && inner_mode != wanted_inner_mode
6903 && ! pos_rtx
6904 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6905 && MEM_P (inner)
6906 && ! mode_dependent_address_p (XEXP (inner, 0))
6907 && ! MEM_VOLATILE_P (inner))
6909 int offset = 0;
6911 /* The computations below will be correct if the machine is big
6912 endian in both bits and bytes or little endian in bits and bytes.
6913 If it is mixed, we must adjust. */
6915 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6916 adjust OFFSET to compensate. */
6917 if (BYTES_BIG_ENDIAN
6918 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6919 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6921 /* We can now move to the desired byte. */
6922 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6923 * GET_MODE_SIZE (wanted_inner_mode);
6924 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6926 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6927 && is_mode != wanted_inner_mode)
6928 offset = (GET_MODE_SIZE (is_mode)
6929 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6931 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6934 /* If INNER is not memory, get it into the proper mode. If we are changing
6935 its mode, POS must be a constant and smaller than the size of the new
6936 mode. */
6937 else if (!MEM_P (inner))
6939 /* On the LHS, don't create paradoxical subregs implicitely truncating
6940 the register unless TRULY_NOOP_TRUNCATION. */
6941 if (in_dest
6942 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (inner)),
6943 GET_MODE_BITSIZE (wanted_inner_mode)))
6944 return NULL_RTX;
6946 if (GET_MODE (inner) != wanted_inner_mode
6947 && (pos_rtx != 0
6948 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6949 return NULL_RTX;
6951 if (orig_pos < 0)
6952 return NULL_RTX;
6954 inner = force_to_mode (inner, wanted_inner_mode,
6955 pos_rtx
6956 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6957 ? ~(unsigned HOST_WIDE_INT) 0
6958 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6959 << orig_pos),
6963 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6964 have to zero extend. Otherwise, we can just use a SUBREG. */
6965 if (pos_rtx != 0
6966 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6968 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6970 /* If we know that no extraneous bits are set, and that the high
6971 bit is not set, convert extraction to cheaper one - either
6972 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6973 cases. */
6974 if (flag_expensive_optimizations
6975 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6976 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6977 & ~(((unsigned HOST_WIDE_INT)
6978 GET_MODE_MASK (GET_MODE (pos_rtx)))
6979 >> 1))
6980 == 0)))
6982 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6984 /* Prefer ZERO_EXTENSION, since it gives more information to
6985 backends. */
6986 if (rtx_cost (temp1, SET, optimize_this_for_speed_p)
6987 < rtx_cost (temp, SET, optimize_this_for_speed_p))
6988 temp = temp1;
6990 pos_rtx = temp;
6992 else if (pos_rtx != 0
6993 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6994 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6996 /* Make POS_RTX unless we already have it and it is correct. If we don't
6997 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6998 be a CONST_INT. */
6999 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7000 pos_rtx = orig_pos_rtx;
7002 else if (pos_rtx == 0)
7003 pos_rtx = GEN_INT (pos);
7005 /* Make the required operation. See if we can use existing rtx. */
7006 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7007 extraction_mode, inner, GEN_INT (len), pos_rtx);
7008 if (! in_dest)
7009 new_rtx = gen_lowpart (mode, new_rtx);
7011 return new_rtx;
7014 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7015 with any other operations in X. Return X without that shift if so. */
7017 static rtx
7018 extract_left_shift (rtx x, int count)
7020 enum rtx_code code = GET_CODE (x);
7021 enum machine_mode mode = GET_MODE (x);
7022 rtx tem;
7024 switch (code)
7026 case ASHIFT:
7027 /* This is the shift itself. If it is wide enough, we will return
7028 either the value being shifted if the shift count is equal to
7029 COUNT or a shift for the difference. */
7030 if (CONST_INT_P (XEXP (x, 1))
7031 && INTVAL (XEXP (x, 1)) >= count)
7032 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7033 INTVAL (XEXP (x, 1)) - count);
7034 break;
7036 case NEG: case NOT:
7037 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7038 return simplify_gen_unary (code, mode, tem, mode);
7040 break;
7042 case PLUS: case IOR: case XOR: case AND:
7043 /* If we can safely shift this constant and we find the inner shift,
7044 make a new operation. */
7045 if (CONST_INT_P (XEXP (x, 1))
7046 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
7047 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7048 return simplify_gen_binary (code, mode, tem,
7049 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
7051 break;
7053 default:
7054 break;
7057 return 0;
7060 /* Look at the expression rooted at X. Look for expressions
7061 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7062 Form these expressions.
7064 Return the new rtx, usually just X.
7066 Also, for machines like the VAX that don't have logical shift insns,
7067 try to convert logical to arithmetic shift operations in cases where
7068 they are equivalent. This undoes the canonicalizations to logical
7069 shifts done elsewhere.
7071 We try, as much as possible, to re-use rtl expressions to save memory.
7073 IN_CODE says what kind of expression we are processing. Normally, it is
7074 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7075 being kludges), it is MEM. When processing the arguments of a comparison
7076 or a COMPARE against zero, it is COMPARE. */
7078 static rtx
7079 make_compound_operation (rtx x, enum rtx_code in_code)
7081 enum rtx_code code = GET_CODE (x);
7082 enum machine_mode mode = GET_MODE (x);
7083 int mode_width = GET_MODE_BITSIZE (mode);
7084 rtx rhs, lhs;
7085 enum rtx_code next_code;
7086 int i, j;
7087 rtx new_rtx = 0;
7088 rtx tem;
7089 const char *fmt;
7091 /* Select the code to be used in recursive calls. Once we are inside an
7092 address, we stay there. If we have a comparison, set to COMPARE,
7093 but once inside, go back to our default of SET. */
7095 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
7096 : ((code == COMPARE || COMPARISON_P (x))
7097 && XEXP (x, 1) == const0_rtx) ? COMPARE
7098 : in_code == COMPARE ? SET : in_code);
7100 /* Process depending on the code of this operation. If NEW is set
7101 nonzero, it will be returned. */
7103 switch (code)
7105 case ASHIFT:
7106 /* Convert shifts by constants into multiplications if inside
7107 an address. */
7108 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7109 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7110 && INTVAL (XEXP (x, 1)) >= 0)
7112 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7113 new_rtx = gen_rtx_MULT (mode, new_rtx,
7114 GEN_INT ((HOST_WIDE_INT) 1
7115 << INTVAL (XEXP (x, 1))));
7117 break;
7119 case AND:
7120 /* If the second operand is not a constant, we can't do anything
7121 with it. */
7122 if (!CONST_INT_P (XEXP (x, 1)))
7123 break;
7125 /* If the constant is a power of two minus one and the first operand
7126 is a logical right shift, make an extraction. */
7127 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7128 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7130 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7131 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7132 0, in_code == COMPARE);
7135 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7136 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7137 && subreg_lowpart_p (XEXP (x, 0))
7138 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7139 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7141 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7142 next_code);
7143 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7144 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7145 0, in_code == COMPARE);
7147 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7148 else if ((GET_CODE (XEXP (x, 0)) == XOR
7149 || GET_CODE (XEXP (x, 0)) == IOR)
7150 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7151 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7152 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7154 /* Apply the distributive law, and then try to make extractions. */
7155 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7156 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7157 XEXP (x, 1)),
7158 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7159 XEXP (x, 1)));
7160 new_rtx = make_compound_operation (new_rtx, in_code);
7163 /* If we are have (and (rotate X C) M) and C is larger than the number
7164 of bits in M, this is an extraction. */
7166 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7167 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7168 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
7169 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7171 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7172 new_rtx = make_extraction (mode, new_rtx,
7173 (GET_MODE_BITSIZE (mode)
7174 - INTVAL (XEXP (XEXP (x, 0), 1))),
7175 NULL_RTX, i, 1, 0, in_code == COMPARE);
7178 /* On machines without logical shifts, if the operand of the AND is
7179 a logical shift and our mask turns off all the propagated sign
7180 bits, we can replace the logical shift with an arithmetic shift. */
7181 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7182 && !have_insn_for (LSHIFTRT, mode)
7183 && have_insn_for (ASHIFTRT, mode)
7184 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7185 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7186 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7187 && mode_width <= HOST_BITS_PER_WIDE_INT)
7189 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7191 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7192 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7193 SUBST (XEXP (x, 0),
7194 gen_rtx_ASHIFTRT (mode,
7195 make_compound_operation
7196 (XEXP (XEXP (x, 0), 0), next_code),
7197 XEXP (XEXP (x, 0), 1)));
7200 /* If the constant is one less than a power of two, this might be
7201 representable by an extraction even if no shift is present.
7202 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7203 we are in a COMPARE. */
7204 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
7205 new_rtx = make_extraction (mode,
7206 make_compound_operation (XEXP (x, 0),
7207 next_code),
7208 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7210 /* If we are in a comparison and this is an AND with a power of two,
7211 convert this into the appropriate bit extract. */
7212 else if (in_code == COMPARE
7213 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
7214 new_rtx = make_extraction (mode,
7215 make_compound_operation (XEXP (x, 0),
7216 next_code),
7217 i, NULL_RTX, 1, 1, 0, 1);
7219 break;
7221 case LSHIFTRT:
7222 /* If the sign bit is known to be zero, replace this with an
7223 arithmetic shift. */
7224 if (have_insn_for (ASHIFTRT, mode)
7225 && ! have_insn_for (LSHIFTRT, mode)
7226 && mode_width <= HOST_BITS_PER_WIDE_INT
7227 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7229 new_rtx = gen_rtx_ASHIFTRT (mode,
7230 make_compound_operation (XEXP (x, 0),
7231 next_code),
7232 XEXP (x, 1));
7233 break;
7236 /* ... fall through ... */
7238 case ASHIFTRT:
7239 lhs = XEXP (x, 0);
7240 rhs = XEXP (x, 1);
7242 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7243 this is a SIGN_EXTRACT. */
7244 if (CONST_INT_P (rhs)
7245 && GET_CODE (lhs) == ASHIFT
7246 && CONST_INT_P (XEXP (lhs, 1))
7247 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7248 && INTVAL (rhs) < mode_width)
7250 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7251 new_rtx = make_extraction (mode, new_rtx,
7252 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7253 NULL_RTX, mode_width - INTVAL (rhs),
7254 code == LSHIFTRT, 0, in_code == COMPARE);
7255 break;
7258 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7259 If so, try to merge the shifts into a SIGN_EXTEND. We could
7260 also do this for some cases of SIGN_EXTRACT, but it doesn't
7261 seem worth the effort; the case checked for occurs on Alpha. */
7263 if (!OBJECT_P (lhs)
7264 && ! (GET_CODE (lhs) == SUBREG
7265 && (OBJECT_P (SUBREG_REG (lhs))))
7266 && CONST_INT_P (rhs)
7267 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7268 && INTVAL (rhs) < mode_width
7269 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7270 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7271 0, NULL_RTX, mode_width - INTVAL (rhs),
7272 code == LSHIFTRT, 0, in_code == COMPARE);
7274 break;
7276 case SUBREG:
7277 /* Call ourselves recursively on the inner expression. If we are
7278 narrowing the object and it has a different RTL code from
7279 what it originally did, do this SUBREG as a force_to_mode. */
7281 tem = make_compound_operation (SUBREG_REG (x), in_code);
7284 rtx simplified = simplify_subreg (mode, tem, GET_MODE (SUBREG_REG (x)),
7285 SUBREG_BYTE (x));
7287 if (simplified)
7288 tem = simplified;
7290 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7291 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
7292 && subreg_lowpart_p (x))
7294 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7297 /* If we have something other than a SUBREG, we might have
7298 done an expansion, so rerun ourselves. */
7299 if (GET_CODE (newer) != SUBREG)
7300 newer = make_compound_operation (newer, in_code);
7302 /* force_to_mode can expand compounds. If it just re-expanded the
7303 compound use gen_lowpart instead to convert to the desired
7304 mode. */
7305 if (rtx_equal_p (newer, x))
7306 return gen_lowpart (GET_MODE (x), tem);
7308 return newer;
7311 if (simplified)
7312 return tem;
7314 break;
7316 default:
7317 break;
7320 if (new_rtx)
7322 x = gen_lowpart (mode, new_rtx);
7323 code = GET_CODE (x);
7326 /* Now recursively process each operand of this operation. */
7327 fmt = GET_RTX_FORMAT (code);
7328 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7329 if (fmt[i] == 'e')
7331 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7332 SUBST (XEXP (x, i), new_rtx);
7334 else if (fmt[i] == 'E')
7335 for (j = 0; j < XVECLEN (x, i); j++)
7337 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7338 SUBST (XVECEXP (x, i, j), new_rtx);
7341 /* If this is a commutative operation, the changes to the operands
7342 may have made it noncanonical. */
7343 if (COMMUTATIVE_ARITH_P (x)
7344 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7346 tem = XEXP (x, 0);
7347 SUBST (XEXP (x, 0), XEXP (x, 1));
7348 SUBST (XEXP (x, 1), tem);
7351 return x;
7354 /* Given M see if it is a value that would select a field of bits
7355 within an item, but not the entire word. Return -1 if not.
7356 Otherwise, return the starting position of the field, where 0 is the
7357 low-order bit.
7359 *PLEN is set to the length of the field. */
7361 static int
7362 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7364 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7365 int pos = exact_log2 (m & -m);
7366 int len = 0;
7368 if (pos >= 0)
7369 /* Now shift off the low-order zero bits and see if we have a
7370 power of two minus 1. */
7371 len = exact_log2 ((m >> pos) + 1);
7373 if (len <= 0)
7374 pos = -1;
7376 *plen = len;
7377 return pos;
7380 /* If X refers to a register that equals REG in value, replace these
7381 references with REG. */
7382 static rtx
7383 canon_reg_for_combine (rtx x, rtx reg)
7385 rtx op0, op1, op2;
7386 const char *fmt;
7387 int i;
7388 bool copied;
7390 enum rtx_code code = GET_CODE (x);
7391 switch (GET_RTX_CLASS (code))
7393 case RTX_UNARY:
7394 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7395 if (op0 != XEXP (x, 0))
7396 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7397 GET_MODE (reg));
7398 break;
7400 case RTX_BIN_ARITH:
7401 case RTX_COMM_ARITH:
7402 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7403 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7404 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7405 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7406 break;
7408 case RTX_COMPARE:
7409 case RTX_COMM_COMPARE:
7410 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7411 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7412 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7413 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7414 GET_MODE (op0), op0, op1);
7415 break;
7417 case RTX_TERNARY:
7418 case RTX_BITFIELD_OPS:
7419 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7420 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7421 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7422 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7423 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7424 GET_MODE (op0), op0, op1, op2);
7426 case RTX_OBJ:
7427 if (REG_P (x))
7429 if (rtx_equal_p (get_last_value (reg), x)
7430 || rtx_equal_p (reg, get_last_value (x)))
7431 return reg;
7432 else
7433 break;
7436 /* fall through */
7438 default:
7439 fmt = GET_RTX_FORMAT (code);
7440 copied = false;
7441 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7442 if (fmt[i] == 'e')
7444 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7445 if (op != XEXP (x, i))
7447 if (!copied)
7449 copied = true;
7450 x = copy_rtx (x);
7452 XEXP (x, i) = op;
7455 else if (fmt[i] == 'E')
7457 int j;
7458 for (j = 0; j < XVECLEN (x, i); j++)
7460 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7461 if (op != XVECEXP (x, i, j))
7463 if (!copied)
7465 copied = true;
7466 x = copy_rtx (x);
7468 XVECEXP (x, i, j) = op;
7473 break;
7476 return x;
7479 /* Return X converted to MODE. If the value is already truncated to
7480 MODE we can just return a subreg even though in the general case we
7481 would need an explicit truncation. */
7483 static rtx
7484 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7486 if (!CONST_INT_P (x)
7487 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
7488 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7489 GET_MODE_BITSIZE (GET_MODE (x)))
7490 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
7492 /* Bit-cast X into an integer mode. */
7493 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
7494 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
7495 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
7496 x, GET_MODE (x));
7499 return gen_lowpart (mode, x);
7502 /* See if X can be simplified knowing that we will only refer to it in
7503 MODE and will only refer to those bits that are nonzero in MASK.
7504 If other bits are being computed or if masking operations are done
7505 that select a superset of the bits in MASK, they can sometimes be
7506 ignored.
7508 Return a possibly simplified expression, but always convert X to
7509 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7511 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7512 are all off in X. This is used when X will be complemented, by either
7513 NOT, NEG, or XOR. */
7515 static rtx
7516 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7517 int just_select)
7519 enum rtx_code code = GET_CODE (x);
7520 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7521 enum machine_mode op_mode;
7522 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7523 rtx op0, op1, temp;
7525 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7526 code below will do the wrong thing since the mode of such an
7527 expression is VOIDmode.
7529 Also do nothing if X is a CLOBBER; this can happen if X was
7530 the return value from a call to gen_lowpart. */
7531 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7532 return x;
7534 /* We want to perform the operation is its present mode unless we know
7535 that the operation is valid in MODE, in which case we do the operation
7536 in MODE. */
7537 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7538 && have_insn_for (code, mode))
7539 ? mode : GET_MODE (x));
7541 /* It is not valid to do a right-shift in a narrower mode
7542 than the one it came in with. */
7543 if ((code == LSHIFTRT || code == ASHIFTRT)
7544 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7545 op_mode = GET_MODE (x);
7547 /* Truncate MASK to fit OP_MODE. */
7548 if (op_mode)
7549 mask &= GET_MODE_MASK (op_mode);
7551 /* When we have an arithmetic operation, or a shift whose count we
7552 do not know, we need to assume that all bits up to the highest-order
7553 bit in MASK will be needed. This is how we form such a mask. */
7554 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7555 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7556 else
7557 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7558 - 1);
7560 /* Determine what bits of X are guaranteed to be (non)zero. */
7561 nonzero = nonzero_bits (x, mode);
7563 /* If none of the bits in X are needed, return a zero. */
7564 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7565 x = const0_rtx;
7567 /* If X is a CONST_INT, return a new one. Do this here since the
7568 test below will fail. */
7569 if (CONST_INT_P (x))
7571 if (SCALAR_INT_MODE_P (mode))
7572 return gen_int_mode (INTVAL (x) & mask, mode);
7573 else
7575 x = GEN_INT (INTVAL (x) & mask);
7576 return gen_lowpart_common (mode, x);
7580 /* If X is narrower than MODE and we want all the bits in X's mode, just
7581 get X in the proper mode. */
7582 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7583 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7584 return gen_lowpart (mode, x);
7586 /* We can ignore the effect of a SUBREG if it narrows the mode or
7587 if the constant masks to zero all the bits the mode doesn't have. */
7588 if (GET_CODE (x) == SUBREG
7589 && subreg_lowpart_p (x)
7590 && ((GET_MODE_SIZE (GET_MODE (x))
7591 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7592 || (0 == (mask
7593 & GET_MODE_MASK (GET_MODE (x))
7594 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7595 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7597 /* The arithmetic simplifications here only work for scalar integer modes. */
7598 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
7599 return gen_lowpart_or_truncate (mode, x);
7601 switch (code)
7603 case CLOBBER:
7604 /* If X is a (clobber (const_int)), return it since we know we are
7605 generating something that won't match. */
7606 return x;
7608 case SIGN_EXTEND:
7609 case ZERO_EXTEND:
7610 case ZERO_EXTRACT:
7611 case SIGN_EXTRACT:
7612 x = expand_compound_operation (x);
7613 if (GET_CODE (x) != code)
7614 return force_to_mode (x, mode, mask, next_select);
7615 break;
7617 case TRUNCATE:
7618 /* Similarly for a truncate. */
7619 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7621 case AND:
7622 /* If this is an AND with a constant, convert it into an AND
7623 whose constant is the AND of that constant with MASK. If it
7624 remains an AND of MASK, delete it since it is redundant. */
7626 if (CONST_INT_P (XEXP (x, 1)))
7628 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7629 mask & INTVAL (XEXP (x, 1)));
7631 /* If X is still an AND, see if it is an AND with a mask that
7632 is just some low-order bits. If so, and it is MASK, we don't
7633 need it. */
7635 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
7636 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7637 == mask))
7638 x = XEXP (x, 0);
7640 /* If it remains an AND, try making another AND with the bits
7641 in the mode mask that aren't in MASK turned on. If the
7642 constant in the AND is wide enough, this might make a
7643 cheaper constant. */
7645 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
7646 && GET_MODE_MASK (GET_MODE (x)) != mask
7647 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7649 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7650 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7651 int width = GET_MODE_BITSIZE (GET_MODE (x));
7652 rtx y;
7654 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7655 number, sign extend it. */
7656 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7657 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7658 cval |= (HOST_WIDE_INT) -1 << width;
7660 y = simplify_gen_binary (AND, GET_MODE (x),
7661 XEXP (x, 0), GEN_INT (cval));
7662 if (rtx_cost (y, SET, optimize_this_for_speed_p)
7663 < rtx_cost (x, SET, optimize_this_for_speed_p))
7664 x = y;
7667 break;
7670 goto binop;
7672 case PLUS:
7673 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7674 low-order bits (as in an alignment operation) and FOO is already
7675 aligned to that boundary, mask C1 to that boundary as well.
7676 This may eliminate that PLUS and, later, the AND. */
7679 unsigned int width = GET_MODE_BITSIZE (mode);
7680 unsigned HOST_WIDE_INT smask = mask;
7682 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7683 number, sign extend it. */
7685 if (width < HOST_BITS_PER_WIDE_INT
7686 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7687 smask |= (HOST_WIDE_INT) -1 << width;
7689 if (CONST_INT_P (XEXP (x, 1))
7690 && exact_log2 (- smask) >= 0
7691 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7692 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7693 return force_to_mode (plus_constant (XEXP (x, 0),
7694 (INTVAL (XEXP (x, 1)) & smask)),
7695 mode, smask, next_select);
7698 /* ... fall through ... */
7700 case MULT:
7701 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7702 most significant bit in MASK since carries from those bits will
7703 affect the bits we are interested in. */
7704 mask = fuller_mask;
7705 goto binop;
7707 case MINUS:
7708 /* If X is (minus C Y) where C's least set bit is larger than any bit
7709 in the mask, then we may replace with (neg Y). */
7710 if (CONST_INT_P (XEXP (x, 0))
7711 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7712 & -INTVAL (XEXP (x, 0))))
7713 > mask))
7715 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7716 GET_MODE (x));
7717 return force_to_mode (x, mode, mask, next_select);
7720 /* Similarly, if C contains every bit in the fuller_mask, then we may
7721 replace with (not Y). */
7722 if (CONST_INT_P (XEXP (x, 0))
7723 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7724 == INTVAL (XEXP (x, 0))))
7726 x = simplify_gen_unary (NOT, GET_MODE (x),
7727 XEXP (x, 1), GET_MODE (x));
7728 return force_to_mode (x, mode, mask, next_select);
7731 mask = fuller_mask;
7732 goto binop;
7734 case IOR:
7735 case XOR:
7736 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7737 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7738 operation which may be a bitfield extraction. Ensure that the
7739 constant we form is not wider than the mode of X. */
7741 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7742 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7743 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7744 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7745 && CONST_INT_P (XEXP (x, 1))
7746 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7747 + floor_log2 (INTVAL (XEXP (x, 1))))
7748 < GET_MODE_BITSIZE (GET_MODE (x)))
7749 && (INTVAL (XEXP (x, 1))
7750 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7752 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7753 << INTVAL (XEXP (XEXP (x, 0), 1)));
7754 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7755 XEXP (XEXP (x, 0), 0), temp);
7756 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7757 XEXP (XEXP (x, 0), 1));
7758 return force_to_mode (x, mode, mask, next_select);
7761 binop:
7762 /* For most binary operations, just propagate into the operation and
7763 change the mode if we have an operation of that mode. */
7765 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
7766 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
7768 /* If we ended up truncating both operands, truncate the result of the
7769 operation instead. */
7770 if (GET_CODE (op0) == TRUNCATE
7771 && GET_CODE (op1) == TRUNCATE)
7773 op0 = XEXP (op0, 0);
7774 op1 = XEXP (op1, 0);
7777 op0 = gen_lowpart_or_truncate (op_mode, op0);
7778 op1 = gen_lowpart_or_truncate (op_mode, op1);
7780 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7781 x = simplify_gen_binary (code, op_mode, op0, op1);
7782 break;
7784 case ASHIFT:
7785 /* For left shifts, do the same, but just for the first operand.
7786 However, we cannot do anything with shifts where we cannot
7787 guarantee that the counts are smaller than the size of the mode
7788 because such a count will have a different meaning in a
7789 wider mode. */
7791 if (! (CONST_INT_P (XEXP (x, 1))
7792 && INTVAL (XEXP (x, 1)) >= 0
7793 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7794 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7795 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7796 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7797 break;
7799 /* If the shift count is a constant and we can do arithmetic in
7800 the mode of the shift, refine which bits we need. Otherwise, use the
7801 conservative form of the mask. */
7802 if (CONST_INT_P (XEXP (x, 1))
7803 && INTVAL (XEXP (x, 1)) >= 0
7804 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7805 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7806 mask >>= INTVAL (XEXP (x, 1));
7807 else
7808 mask = fuller_mask;
7810 op0 = gen_lowpart_or_truncate (op_mode,
7811 force_to_mode (XEXP (x, 0), op_mode,
7812 mask, next_select));
7814 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7815 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7816 break;
7818 case LSHIFTRT:
7819 /* Here we can only do something if the shift count is a constant,
7820 this shift constant is valid for the host, and we can do arithmetic
7821 in OP_MODE. */
7823 if (CONST_INT_P (XEXP (x, 1))
7824 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7825 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7827 rtx inner = XEXP (x, 0);
7828 unsigned HOST_WIDE_INT inner_mask;
7830 /* Select the mask of the bits we need for the shift operand. */
7831 inner_mask = mask << INTVAL (XEXP (x, 1));
7833 /* We can only change the mode of the shift if we can do arithmetic
7834 in the mode of the shift and INNER_MASK is no wider than the
7835 width of X's mode. */
7836 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7837 op_mode = GET_MODE (x);
7839 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7841 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7842 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7845 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7846 shift and AND produces only copies of the sign bit (C2 is one less
7847 than a power of two), we can do this with just a shift. */
7849 if (GET_CODE (x) == LSHIFTRT
7850 && CONST_INT_P (XEXP (x, 1))
7851 /* The shift puts one of the sign bit copies in the least significant
7852 bit. */
7853 && ((INTVAL (XEXP (x, 1))
7854 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7855 >= GET_MODE_BITSIZE (GET_MODE (x)))
7856 && exact_log2 (mask + 1) >= 0
7857 /* Number of bits left after the shift must be more than the mask
7858 needs. */
7859 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7860 <= GET_MODE_BITSIZE (GET_MODE (x)))
7861 /* Must be more sign bit copies than the mask needs. */
7862 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7863 >= exact_log2 (mask + 1)))
7864 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7865 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7866 - exact_log2 (mask + 1)));
7868 goto shiftrt;
7870 case ASHIFTRT:
7871 /* If we are just looking for the sign bit, we don't need this shift at
7872 all, even if it has a variable count. */
7873 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7874 && (mask == ((unsigned HOST_WIDE_INT) 1
7875 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7876 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7878 /* If this is a shift by a constant, get a mask that contains those bits
7879 that are not copies of the sign bit. We then have two cases: If
7880 MASK only includes those bits, this can be a logical shift, which may
7881 allow simplifications. If MASK is a single-bit field not within
7882 those bits, we are requesting a copy of the sign bit and hence can
7883 shift the sign bit to the appropriate location. */
7885 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
7886 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7888 int i;
7890 /* If the considered data is wider than HOST_WIDE_INT, we can't
7891 represent a mask for all its bits in a single scalar.
7892 But we only care about the lower bits, so calculate these. */
7894 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7896 nonzero = ~(HOST_WIDE_INT) 0;
7898 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7899 is the number of bits a full-width mask would have set.
7900 We need only shift if these are fewer than nonzero can
7901 hold. If not, we must keep all bits set in nonzero. */
7903 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7904 < HOST_BITS_PER_WIDE_INT)
7905 nonzero >>= INTVAL (XEXP (x, 1))
7906 + HOST_BITS_PER_WIDE_INT
7907 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7909 else
7911 nonzero = GET_MODE_MASK (GET_MODE (x));
7912 nonzero >>= INTVAL (XEXP (x, 1));
7915 if ((mask & ~nonzero) == 0)
7917 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7918 XEXP (x, 0), INTVAL (XEXP (x, 1)));
7919 if (GET_CODE (x) != ASHIFTRT)
7920 return force_to_mode (x, mode, mask, next_select);
7923 else if ((i = exact_log2 (mask)) >= 0)
7925 x = simplify_shift_const
7926 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7927 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7929 if (GET_CODE (x) != ASHIFTRT)
7930 return force_to_mode (x, mode, mask, next_select);
7934 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7935 even if the shift count isn't a constant. */
7936 if (mask == 1)
7937 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7938 XEXP (x, 0), XEXP (x, 1));
7940 shiftrt:
7942 /* If this is a zero- or sign-extension operation that just affects bits
7943 we don't care about, remove it. Be sure the call above returned
7944 something that is still a shift. */
7946 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7947 && CONST_INT_P (XEXP (x, 1))
7948 && INTVAL (XEXP (x, 1)) >= 0
7949 && (INTVAL (XEXP (x, 1))
7950 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7951 && GET_CODE (XEXP (x, 0)) == ASHIFT
7952 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7953 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7954 next_select);
7956 break;
7958 case ROTATE:
7959 case ROTATERT:
7960 /* If the shift count is constant and we can do computations
7961 in the mode of X, compute where the bits we care about are.
7962 Otherwise, we can't do anything. Don't change the mode of
7963 the shift or propagate MODE into the shift, though. */
7964 if (CONST_INT_P (XEXP (x, 1))
7965 && INTVAL (XEXP (x, 1)) >= 0)
7967 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7968 GET_MODE (x), GEN_INT (mask),
7969 XEXP (x, 1));
7970 if (temp && CONST_INT_P (temp))
7971 SUBST (XEXP (x, 0),
7972 force_to_mode (XEXP (x, 0), GET_MODE (x),
7973 INTVAL (temp), next_select));
7975 break;
7977 case NEG:
7978 /* If we just want the low-order bit, the NEG isn't needed since it
7979 won't change the low-order bit. */
7980 if (mask == 1)
7981 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7983 /* We need any bits less significant than the most significant bit in
7984 MASK since carries from those bits will affect the bits we are
7985 interested in. */
7986 mask = fuller_mask;
7987 goto unop;
7989 case NOT:
7990 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7991 same as the XOR case above. Ensure that the constant we form is not
7992 wider than the mode of X. */
7994 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7995 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7996 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7997 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7998 < GET_MODE_BITSIZE (GET_MODE (x)))
7999 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8001 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8002 GET_MODE (x));
8003 temp = simplify_gen_binary (XOR, GET_MODE (x),
8004 XEXP (XEXP (x, 0), 0), temp);
8005 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8006 temp, XEXP (XEXP (x, 0), 1));
8008 return force_to_mode (x, mode, mask, next_select);
8011 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8012 use the full mask inside the NOT. */
8013 mask = fuller_mask;
8015 unop:
8016 op0 = gen_lowpart_or_truncate (op_mode,
8017 force_to_mode (XEXP (x, 0), mode, mask,
8018 next_select));
8019 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8020 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8021 break;
8023 case NE:
8024 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8025 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8026 which is equal to STORE_FLAG_VALUE. */
8027 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
8028 && GET_MODE (XEXP (x, 0)) == mode
8029 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8030 && (nonzero_bits (XEXP (x, 0), mode)
8031 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8032 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8034 break;
8036 case IF_THEN_ELSE:
8037 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8038 written in a narrower mode. We play it safe and do not do so. */
8040 SUBST (XEXP (x, 1),
8041 gen_lowpart_or_truncate (GET_MODE (x),
8042 force_to_mode (XEXP (x, 1), mode,
8043 mask, next_select)));
8044 SUBST (XEXP (x, 2),
8045 gen_lowpart_or_truncate (GET_MODE (x),
8046 force_to_mode (XEXP (x, 2), mode,
8047 mask, next_select)));
8048 break;
8050 default:
8051 break;
8054 /* Ensure we return a value of the proper mode. */
8055 return gen_lowpart_or_truncate (mode, x);
8058 /* Return nonzero if X is an expression that has one of two values depending on
8059 whether some other value is zero or nonzero. In that case, we return the
8060 value that is being tested, *PTRUE is set to the value if the rtx being
8061 returned has a nonzero value, and *PFALSE is set to the other alternative.
8063 If we return zero, we set *PTRUE and *PFALSE to X. */
8065 static rtx
8066 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8068 enum machine_mode mode = GET_MODE (x);
8069 enum rtx_code code = GET_CODE (x);
8070 rtx cond0, cond1, true0, true1, false0, false1;
8071 unsigned HOST_WIDE_INT nz;
8073 /* If we are comparing a value against zero, we are done. */
8074 if ((code == NE || code == EQ)
8075 && XEXP (x, 1) == const0_rtx)
8077 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8078 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8079 return XEXP (x, 0);
8082 /* If this is a unary operation whose operand has one of two values, apply
8083 our opcode to compute those values. */
8084 else if (UNARY_P (x)
8085 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8087 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8088 *pfalse = simplify_gen_unary (code, mode, false0,
8089 GET_MODE (XEXP (x, 0)));
8090 return cond0;
8093 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8094 make can't possibly match and would suppress other optimizations. */
8095 else if (code == COMPARE)
8098 /* If this is a binary operation, see if either side has only one of two
8099 values. If either one does or if both do and they are conditional on
8100 the same value, compute the new true and false values. */
8101 else if (BINARY_P (x))
8103 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8104 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8106 if ((cond0 != 0 || cond1 != 0)
8107 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8109 /* If if_then_else_cond returned zero, then true/false are the
8110 same rtl. We must copy one of them to prevent invalid rtl
8111 sharing. */
8112 if (cond0 == 0)
8113 true0 = copy_rtx (true0);
8114 else if (cond1 == 0)
8115 true1 = copy_rtx (true1);
8117 if (COMPARISON_P (x))
8119 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8120 true0, true1);
8121 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8122 false0, false1);
8124 else
8126 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8127 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8130 return cond0 ? cond0 : cond1;
8133 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8134 operands is zero when the other is nonzero, and vice-versa,
8135 and STORE_FLAG_VALUE is 1 or -1. */
8137 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8138 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8139 || code == UMAX)
8140 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8142 rtx op0 = XEXP (XEXP (x, 0), 1);
8143 rtx op1 = XEXP (XEXP (x, 1), 1);
8145 cond0 = XEXP (XEXP (x, 0), 0);
8146 cond1 = XEXP (XEXP (x, 1), 0);
8148 if (COMPARISON_P (cond0)
8149 && COMPARISON_P (cond1)
8150 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8151 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8152 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8153 || ((swap_condition (GET_CODE (cond0))
8154 == reversed_comparison_code (cond1, NULL))
8155 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8156 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8157 && ! side_effects_p (x))
8159 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8160 *pfalse = simplify_gen_binary (MULT, mode,
8161 (code == MINUS
8162 ? simplify_gen_unary (NEG, mode,
8163 op1, mode)
8164 : op1),
8165 const_true_rtx);
8166 return cond0;
8170 /* Similarly for MULT, AND and UMIN, except that for these the result
8171 is always zero. */
8172 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8173 && (code == MULT || code == AND || code == UMIN)
8174 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8176 cond0 = XEXP (XEXP (x, 0), 0);
8177 cond1 = XEXP (XEXP (x, 1), 0);
8179 if (COMPARISON_P (cond0)
8180 && COMPARISON_P (cond1)
8181 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8182 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8183 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8184 || ((swap_condition (GET_CODE (cond0))
8185 == reversed_comparison_code (cond1, NULL))
8186 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8187 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8188 && ! side_effects_p (x))
8190 *ptrue = *pfalse = const0_rtx;
8191 return cond0;
8196 else if (code == IF_THEN_ELSE)
8198 /* If we have IF_THEN_ELSE already, extract the condition and
8199 canonicalize it if it is NE or EQ. */
8200 cond0 = XEXP (x, 0);
8201 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8202 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8203 return XEXP (cond0, 0);
8204 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8206 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8207 return XEXP (cond0, 0);
8209 else
8210 return cond0;
8213 /* If X is a SUBREG, we can narrow both the true and false values
8214 if the inner expression, if there is a condition. */
8215 else if (code == SUBREG
8216 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8217 &true0, &false0)))
8219 true0 = simplify_gen_subreg (mode, true0,
8220 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8221 false0 = simplify_gen_subreg (mode, false0,
8222 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8223 if (true0 && false0)
8225 *ptrue = true0;
8226 *pfalse = false0;
8227 return cond0;
8231 /* If X is a constant, this isn't special and will cause confusions
8232 if we treat it as such. Likewise if it is equivalent to a constant. */
8233 else if (CONSTANT_P (x)
8234 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8237 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8238 will be least confusing to the rest of the compiler. */
8239 else if (mode == BImode)
8241 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8242 return x;
8245 /* If X is known to be either 0 or -1, those are the true and
8246 false values when testing X. */
8247 else if (x == constm1_rtx || x == const0_rtx
8248 || (mode != VOIDmode
8249 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
8251 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8252 return x;
8255 /* Likewise for 0 or a single bit. */
8256 else if (SCALAR_INT_MODE_P (mode)
8257 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8258 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8260 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8261 return x;
8264 /* Otherwise fail; show no condition with true and false values the same. */
8265 *ptrue = *pfalse = x;
8266 return 0;
8269 /* Return the value of expression X given the fact that condition COND
8270 is known to be true when applied to REG as its first operand and VAL
8271 as its second. X is known to not be shared and so can be modified in
8272 place.
8274 We only handle the simplest cases, and specifically those cases that
8275 arise with IF_THEN_ELSE expressions. */
8277 static rtx
8278 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8280 enum rtx_code code = GET_CODE (x);
8281 rtx temp;
8282 const char *fmt;
8283 int i, j;
8285 if (side_effects_p (x))
8286 return x;
8288 /* If either operand of the condition is a floating point value,
8289 then we have to avoid collapsing an EQ comparison. */
8290 if (cond == EQ
8291 && rtx_equal_p (x, reg)
8292 && ! FLOAT_MODE_P (GET_MODE (x))
8293 && ! FLOAT_MODE_P (GET_MODE (val)))
8294 return val;
8296 if (cond == UNEQ && rtx_equal_p (x, reg))
8297 return val;
8299 /* If X is (abs REG) and we know something about REG's relationship
8300 with zero, we may be able to simplify this. */
8302 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8303 switch (cond)
8305 case GE: case GT: case EQ:
8306 return XEXP (x, 0);
8307 case LT: case LE:
8308 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8309 XEXP (x, 0),
8310 GET_MODE (XEXP (x, 0)));
8311 default:
8312 break;
8315 /* The only other cases we handle are MIN, MAX, and comparisons if the
8316 operands are the same as REG and VAL. */
8318 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8320 if (rtx_equal_p (XEXP (x, 0), val))
8321 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8323 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8325 if (COMPARISON_P (x))
8327 if (comparison_dominates_p (cond, code))
8328 return const_true_rtx;
8330 code = reversed_comparison_code (x, NULL);
8331 if (code != UNKNOWN
8332 && comparison_dominates_p (cond, code))
8333 return const0_rtx;
8334 else
8335 return x;
8337 else if (code == SMAX || code == SMIN
8338 || code == UMIN || code == UMAX)
8340 int unsignedp = (code == UMIN || code == UMAX);
8342 /* Do not reverse the condition when it is NE or EQ.
8343 This is because we cannot conclude anything about
8344 the value of 'SMAX (x, y)' when x is not equal to y,
8345 but we can when x equals y. */
8346 if ((code == SMAX || code == UMAX)
8347 && ! (cond == EQ || cond == NE))
8348 cond = reverse_condition (cond);
8350 switch (cond)
8352 case GE: case GT:
8353 return unsignedp ? x : XEXP (x, 1);
8354 case LE: case LT:
8355 return unsignedp ? x : XEXP (x, 0);
8356 case GEU: case GTU:
8357 return unsignedp ? XEXP (x, 1) : x;
8358 case LEU: case LTU:
8359 return unsignedp ? XEXP (x, 0) : x;
8360 default:
8361 break;
8366 else if (code == SUBREG)
8368 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8369 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8371 if (SUBREG_REG (x) != r)
8373 /* We must simplify subreg here, before we lose track of the
8374 original inner_mode. */
8375 new_rtx = simplify_subreg (GET_MODE (x), r,
8376 inner_mode, SUBREG_BYTE (x));
8377 if (new_rtx)
8378 return new_rtx;
8379 else
8380 SUBST (SUBREG_REG (x), r);
8383 return x;
8385 /* We don't have to handle SIGN_EXTEND here, because even in the
8386 case of replacing something with a modeless CONST_INT, a
8387 CONST_INT is already (supposed to be) a valid sign extension for
8388 its narrower mode, which implies it's already properly
8389 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8390 story is different. */
8391 else if (code == ZERO_EXTEND)
8393 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8394 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8396 if (XEXP (x, 0) != r)
8398 /* We must simplify the zero_extend here, before we lose
8399 track of the original inner_mode. */
8400 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8401 r, inner_mode);
8402 if (new_rtx)
8403 return new_rtx;
8404 else
8405 SUBST (XEXP (x, 0), r);
8408 return x;
8411 fmt = GET_RTX_FORMAT (code);
8412 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8414 if (fmt[i] == 'e')
8415 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8416 else if (fmt[i] == 'E')
8417 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8418 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8419 cond, reg, val));
8422 return x;
8425 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8426 assignment as a field assignment. */
8428 static int
8429 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8431 if (x == y || rtx_equal_p (x, y))
8432 return 1;
8434 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8435 return 0;
8437 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8438 Note that all SUBREGs of MEM are paradoxical; otherwise they
8439 would have been rewritten. */
8440 if (MEM_P (x) && GET_CODE (y) == SUBREG
8441 && MEM_P (SUBREG_REG (y))
8442 && rtx_equal_p (SUBREG_REG (y),
8443 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8444 return 1;
8446 if (MEM_P (y) && GET_CODE (x) == SUBREG
8447 && MEM_P (SUBREG_REG (x))
8448 && rtx_equal_p (SUBREG_REG (x),
8449 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8450 return 1;
8452 /* We used to see if get_last_value of X and Y were the same but that's
8453 not correct. In one direction, we'll cause the assignment to have
8454 the wrong destination and in the case, we'll import a register into this
8455 insn that might have already have been dead. So fail if none of the
8456 above cases are true. */
8457 return 0;
8460 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8461 Return that assignment if so.
8463 We only handle the most common cases. */
8465 static rtx
8466 make_field_assignment (rtx x)
8468 rtx dest = SET_DEST (x);
8469 rtx src = SET_SRC (x);
8470 rtx assign;
8471 rtx rhs, lhs;
8472 HOST_WIDE_INT c1;
8473 HOST_WIDE_INT pos;
8474 unsigned HOST_WIDE_INT len;
8475 rtx other;
8476 enum machine_mode mode;
8478 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8479 a clear of a one-bit field. We will have changed it to
8480 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8481 for a SUBREG. */
8483 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8484 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
8485 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8486 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8488 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8489 1, 1, 1, 0);
8490 if (assign != 0)
8491 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8492 return x;
8495 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8496 && subreg_lowpart_p (XEXP (src, 0))
8497 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8498 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8499 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8500 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
8501 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8502 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8504 assign = make_extraction (VOIDmode, dest, 0,
8505 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8506 1, 1, 1, 0);
8507 if (assign != 0)
8508 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8509 return x;
8512 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8513 one-bit field. */
8514 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8515 && XEXP (XEXP (src, 0), 0) == const1_rtx
8516 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8518 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8519 1, 1, 1, 0);
8520 if (assign != 0)
8521 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8522 return x;
8525 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8526 SRC is an AND with all bits of that field set, then we can discard
8527 the AND. */
8528 if (GET_CODE (dest) == ZERO_EXTRACT
8529 && CONST_INT_P (XEXP (dest, 1))
8530 && GET_CODE (src) == AND
8531 && CONST_INT_P (XEXP (src, 1)))
8533 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8534 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8535 unsigned HOST_WIDE_INT ze_mask;
8537 if (width >= HOST_BITS_PER_WIDE_INT)
8538 ze_mask = -1;
8539 else
8540 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8542 /* Complete overlap. We can remove the source AND. */
8543 if ((and_mask & ze_mask) == ze_mask)
8544 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8546 /* Partial overlap. We can reduce the source AND. */
8547 if ((and_mask & ze_mask) != and_mask)
8549 mode = GET_MODE (src);
8550 src = gen_rtx_AND (mode, XEXP (src, 0),
8551 gen_int_mode (and_mask & ze_mask, mode));
8552 return gen_rtx_SET (VOIDmode, dest, src);
8556 /* The other case we handle is assignments into a constant-position
8557 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8558 a mask that has all one bits except for a group of zero bits and
8559 OTHER is known to have zeros where C1 has ones, this is such an
8560 assignment. Compute the position and length from C1. Shift OTHER
8561 to the appropriate position, force it to the required mode, and
8562 make the extraction. Check for the AND in both operands. */
8564 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8565 return x;
8567 rhs = expand_compound_operation (XEXP (src, 0));
8568 lhs = expand_compound_operation (XEXP (src, 1));
8570 if (GET_CODE (rhs) == AND
8571 && CONST_INT_P (XEXP (rhs, 1))
8572 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8573 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8574 else if (GET_CODE (lhs) == AND
8575 && CONST_INT_P (XEXP (lhs, 1))
8576 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8577 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8578 else
8579 return x;
8581 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8582 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8583 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8584 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8585 return x;
8587 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8588 if (assign == 0)
8589 return x;
8591 /* The mode to use for the source is the mode of the assignment, or of
8592 what is inside a possible STRICT_LOW_PART. */
8593 mode = (GET_CODE (assign) == STRICT_LOW_PART
8594 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8596 /* Shift OTHER right POS places and make it the source, restricting it
8597 to the proper length and mode. */
8599 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8600 GET_MODE (src),
8601 other, pos),
8602 dest);
8603 src = force_to_mode (src, mode,
8604 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8605 ? ~(unsigned HOST_WIDE_INT) 0
8606 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8609 /* If SRC is masked by an AND that does not make a difference in
8610 the value being stored, strip it. */
8611 if (GET_CODE (assign) == ZERO_EXTRACT
8612 && CONST_INT_P (XEXP (assign, 1))
8613 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8614 && GET_CODE (src) == AND
8615 && CONST_INT_P (XEXP (src, 1))
8616 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8617 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8618 src = XEXP (src, 0);
8620 return gen_rtx_SET (VOIDmode, assign, src);
8623 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8624 if so. */
8626 static rtx
8627 apply_distributive_law (rtx x)
8629 enum rtx_code code = GET_CODE (x);
8630 enum rtx_code inner_code;
8631 rtx lhs, rhs, other;
8632 rtx tem;
8634 /* Distributivity is not true for floating point as it can change the
8635 value. So we don't do it unless -funsafe-math-optimizations. */
8636 if (FLOAT_MODE_P (GET_MODE (x))
8637 && ! flag_unsafe_math_optimizations)
8638 return x;
8640 /* The outer operation can only be one of the following: */
8641 if (code != IOR && code != AND && code != XOR
8642 && code != PLUS && code != MINUS)
8643 return x;
8645 lhs = XEXP (x, 0);
8646 rhs = XEXP (x, 1);
8648 /* If either operand is a primitive we can't do anything, so get out
8649 fast. */
8650 if (OBJECT_P (lhs) || OBJECT_P (rhs))
8651 return x;
8653 lhs = expand_compound_operation (lhs);
8654 rhs = expand_compound_operation (rhs);
8655 inner_code = GET_CODE (lhs);
8656 if (inner_code != GET_CODE (rhs))
8657 return x;
8659 /* See if the inner and outer operations distribute. */
8660 switch (inner_code)
8662 case LSHIFTRT:
8663 case ASHIFTRT:
8664 case AND:
8665 case IOR:
8666 /* These all distribute except over PLUS. */
8667 if (code == PLUS || code == MINUS)
8668 return x;
8669 break;
8671 case MULT:
8672 if (code != PLUS && code != MINUS)
8673 return x;
8674 break;
8676 case ASHIFT:
8677 /* This is also a multiply, so it distributes over everything. */
8678 break;
8680 case SUBREG:
8681 /* Non-paradoxical SUBREGs distributes over all operations,
8682 provided the inner modes and byte offsets are the same, this
8683 is an extraction of a low-order part, we don't convert an fp
8684 operation to int or vice versa, this is not a vector mode,
8685 and we would not be converting a single-word operation into a
8686 multi-word operation. The latter test is not required, but
8687 it prevents generating unneeded multi-word operations. Some
8688 of the previous tests are redundant given the latter test,
8689 but are retained because they are required for correctness.
8691 We produce the result slightly differently in this case. */
8693 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8694 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8695 || ! subreg_lowpart_p (lhs)
8696 || (GET_MODE_CLASS (GET_MODE (lhs))
8697 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8698 || (GET_MODE_SIZE (GET_MODE (lhs))
8699 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8700 || VECTOR_MODE_P (GET_MODE (lhs))
8701 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8702 /* Result might need to be truncated. Don't change mode if
8703 explicit truncation is needed. */
8704 || !TRULY_NOOP_TRUNCATION
8705 (GET_MODE_BITSIZE (GET_MODE (x)),
8706 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8707 return x;
8709 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8710 SUBREG_REG (lhs), SUBREG_REG (rhs));
8711 return gen_lowpart (GET_MODE (x), tem);
8713 default:
8714 return x;
8717 /* Set LHS and RHS to the inner operands (A and B in the example
8718 above) and set OTHER to the common operand (C in the example).
8719 There is only one way to do this unless the inner operation is
8720 commutative. */
8721 if (COMMUTATIVE_ARITH_P (lhs)
8722 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8723 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8724 else if (COMMUTATIVE_ARITH_P (lhs)
8725 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8726 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8727 else if (COMMUTATIVE_ARITH_P (lhs)
8728 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8729 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8730 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8731 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8732 else
8733 return x;
8735 /* Form the new inner operation, seeing if it simplifies first. */
8736 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8738 /* There is one exception to the general way of distributing:
8739 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8740 if (code == XOR && inner_code == IOR)
8742 inner_code = AND;
8743 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8746 /* We may be able to continuing distributing the result, so call
8747 ourselves recursively on the inner operation before forming the
8748 outer operation, which we return. */
8749 return simplify_gen_binary (inner_code, GET_MODE (x),
8750 apply_distributive_law (tem), other);
8753 /* See if X is of the form (* (+ A B) C), and if so convert to
8754 (+ (* A C) (* B C)) and try to simplify.
8756 Most of the time, this results in no change. However, if some of
8757 the operands are the same or inverses of each other, simplifications
8758 will result.
8760 For example, (and (ior A B) (not B)) can occur as the result of
8761 expanding a bit field assignment. When we apply the distributive
8762 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8763 which then simplifies to (and (A (not B))).
8765 Note that no checks happen on the validity of applying the inverse
8766 distributive law. This is pointless since we can do it in the
8767 few places where this routine is called.
8769 N is the index of the term that is decomposed (the arithmetic operation,
8770 i.e. (+ A B) in the first example above). !N is the index of the term that
8771 is distributed, i.e. of C in the first example above. */
8772 static rtx
8773 distribute_and_simplify_rtx (rtx x, int n)
8775 enum machine_mode mode;
8776 enum rtx_code outer_code, inner_code;
8777 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8779 /* Distributivity is not true for floating point as it can change the
8780 value. So we don't do it unless -funsafe-math-optimizations. */
8781 if (FLOAT_MODE_P (GET_MODE (x))
8782 && ! flag_unsafe_math_optimizations)
8783 return NULL_RTX;
8785 decomposed = XEXP (x, n);
8786 if (!ARITHMETIC_P (decomposed))
8787 return NULL_RTX;
8789 mode = GET_MODE (x);
8790 outer_code = GET_CODE (x);
8791 distributed = XEXP (x, !n);
8793 inner_code = GET_CODE (decomposed);
8794 inner_op0 = XEXP (decomposed, 0);
8795 inner_op1 = XEXP (decomposed, 1);
8797 /* Special case (and (xor B C) (not A)), which is equivalent to
8798 (xor (ior A B) (ior A C)) */
8799 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8801 distributed = XEXP (distributed, 0);
8802 outer_code = IOR;
8805 if (n == 0)
8807 /* Distribute the second term. */
8808 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8809 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8811 else
8813 /* Distribute the first term. */
8814 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8815 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8818 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8819 new_op0, new_op1));
8820 if (GET_CODE (tmp) != outer_code
8821 && rtx_cost (tmp, SET, optimize_this_for_speed_p)
8822 < rtx_cost (x, SET, optimize_this_for_speed_p))
8823 return tmp;
8825 return NULL_RTX;
8828 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8829 in MODE. Return an equivalent form, if different from (and VAROP
8830 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8832 static rtx
8833 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8834 unsigned HOST_WIDE_INT constop)
8836 unsigned HOST_WIDE_INT nonzero;
8837 unsigned HOST_WIDE_INT orig_constop;
8838 rtx orig_varop;
8839 int i;
8841 orig_varop = varop;
8842 orig_constop = constop;
8843 if (GET_CODE (varop) == CLOBBER)
8844 return NULL_RTX;
8846 /* Simplify VAROP knowing that we will be only looking at some of the
8847 bits in it.
8849 Note by passing in CONSTOP, we guarantee that the bits not set in
8850 CONSTOP are not significant and will never be examined. We must
8851 ensure that is the case by explicitly masking out those bits
8852 before returning. */
8853 varop = force_to_mode (varop, mode, constop, 0);
8855 /* If VAROP is a CLOBBER, we will fail so return it. */
8856 if (GET_CODE (varop) == CLOBBER)
8857 return varop;
8859 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8860 to VAROP and return the new constant. */
8861 if (CONST_INT_P (varop))
8862 return gen_int_mode (INTVAL (varop) & constop, mode);
8864 /* See what bits may be nonzero in VAROP. Unlike the general case of
8865 a call to nonzero_bits, here we don't care about bits outside
8866 MODE. */
8868 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8870 /* Turn off all bits in the constant that are known to already be zero.
8871 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8872 which is tested below. */
8874 constop &= nonzero;
8876 /* If we don't have any bits left, return zero. */
8877 if (constop == 0)
8878 return const0_rtx;
8880 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8881 a power of two, we can replace this with an ASHIFT. */
8882 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8883 && (i = exact_log2 (constop)) >= 0)
8884 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8886 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8887 or XOR, then try to apply the distributive law. This may eliminate
8888 operations if either branch can be simplified because of the AND.
8889 It may also make some cases more complex, but those cases probably
8890 won't match a pattern either with or without this. */
8892 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8893 return
8894 gen_lowpart
8895 (mode,
8896 apply_distributive_law
8897 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8898 simplify_and_const_int (NULL_RTX,
8899 GET_MODE (varop),
8900 XEXP (varop, 0),
8901 constop),
8902 simplify_and_const_int (NULL_RTX,
8903 GET_MODE (varop),
8904 XEXP (varop, 1),
8905 constop))));
8907 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8908 the AND and see if one of the operands simplifies to zero. If so, we
8909 may eliminate it. */
8911 if (GET_CODE (varop) == PLUS
8912 && exact_log2 (constop + 1) >= 0)
8914 rtx o0, o1;
8916 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8917 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8918 if (o0 == const0_rtx)
8919 return o1;
8920 if (o1 == const0_rtx)
8921 return o0;
8924 /* Make a SUBREG if necessary. If we can't make it, fail. */
8925 varop = gen_lowpart (mode, varop);
8926 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8927 return NULL_RTX;
8929 /* If we are only masking insignificant bits, return VAROP. */
8930 if (constop == nonzero)
8931 return varop;
8933 if (varop == orig_varop && constop == orig_constop)
8934 return NULL_RTX;
8936 /* Otherwise, return an AND. */
8937 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8941 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8942 in MODE.
8944 Return an equivalent form, if different from X. Otherwise, return X. If
8945 X is zero, we are to always construct the equivalent form. */
8947 static rtx
8948 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8949 unsigned HOST_WIDE_INT constop)
8951 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8952 if (tem)
8953 return tem;
8955 if (!x)
8956 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8957 gen_int_mode (constop, mode));
8958 if (GET_MODE (x) != mode)
8959 x = gen_lowpart (mode, x);
8960 return x;
8963 /* Given a REG, X, compute which bits in X can be nonzero.
8964 We don't care about bits outside of those defined in MODE.
8966 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8967 a shift, AND, or zero_extract, we can do better. */
8969 static rtx
8970 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8971 const_rtx known_x ATTRIBUTE_UNUSED,
8972 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8973 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8974 unsigned HOST_WIDE_INT *nonzero)
8976 rtx tem;
8977 reg_stat_type *rsp;
8979 /* If X is a register whose nonzero bits value is current, use it.
8980 Otherwise, if X is a register whose value we can find, use that
8981 value. Otherwise, use the previously-computed global nonzero bits
8982 for this register. */
8984 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8985 if (rsp->last_set_value != 0
8986 && (rsp->last_set_mode == mode
8987 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8988 && GET_MODE_CLASS (mode) == MODE_INT))
8989 && ((rsp->last_set_label >= label_tick_ebb_start
8990 && rsp->last_set_label < label_tick)
8991 || (rsp->last_set_label == label_tick
8992 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8993 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8994 && REG_N_SETS (REGNO (x)) == 1
8995 && !REGNO_REG_SET_P
8996 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8998 *nonzero &= rsp->last_set_nonzero_bits;
8999 return NULL;
9002 tem = get_last_value (x);
9004 if (tem)
9006 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9007 /* If X is narrower than MODE and TEM is a non-negative
9008 constant that would appear negative in the mode of X,
9009 sign-extend it for use in reg_nonzero_bits because some
9010 machines (maybe most) will actually do the sign-extension
9011 and this is the conservative approach.
9013 ??? For 2.5, try to tighten up the MD files in this regard
9014 instead of this kludge. */
9016 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
9017 && CONST_INT_P (tem)
9018 && INTVAL (tem) > 0
9019 && 0 != (INTVAL (tem)
9020 & ((HOST_WIDE_INT) 1
9021 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9022 tem = GEN_INT (INTVAL (tem)
9023 | ((HOST_WIDE_INT) (-1)
9024 << GET_MODE_BITSIZE (GET_MODE (x))));
9025 #endif
9026 return tem;
9028 else if (nonzero_sign_valid && rsp->nonzero_bits)
9030 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9032 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
9033 /* We don't know anything about the upper bits. */
9034 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9035 *nonzero &= mask;
9038 return NULL;
9041 /* Return the number of bits at the high-order end of X that are known to
9042 be equal to the sign bit. X will be used in mode MODE; if MODE is
9043 VOIDmode, X will be used in its own mode. The returned value will always
9044 be between 1 and the number of bits in MODE. */
9046 static rtx
9047 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9048 const_rtx known_x ATTRIBUTE_UNUSED,
9049 enum machine_mode known_mode
9050 ATTRIBUTE_UNUSED,
9051 unsigned int known_ret ATTRIBUTE_UNUSED,
9052 unsigned int *result)
9054 rtx tem;
9055 reg_stat_type *rsp;
9057 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
9058 if (rsp->last_set_value != 0
9059 && rsp->last_set_mode == mode
9060 && ((rsp->last_set_label >= label_tick_ebb_start
9061 && rsp->last_set_label < label_tick)
9062 || (rsp->last_set_label == label_tick
9063 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9064 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9065 && REG_N_SETS (REGNO (x)) == 1
9066 && !REGNO_REG_SET_P
9067 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
9069 *result = rsp->last_set_sign_bit_copies;
9070 return NULL;
9073 tem = get_last_value (x);
9074 if (tem != 0)
9075 return tem;
9077 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9078 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
9079 *result = rsp->sign_bit_copies;
9081 return NULL;
9084 /* Return the number of "extended" bits there are in X, when interpreted
9085 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9086 unsigned quantities, this is the number of high-order zero bits.
9087 For signed quantities, this is the number of copies of the sign bit
9088 minus 1. In both case, this function returns the number of "spare"
9089 bits. For example, if two quantities for which this function returns
9090 at least 1 are added, the addition is known not to overflow.
9092 This function will always return 0 unless called during combine, which
9093 implies that it must be called from a define_split. */
9095 unsigned int
9096 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9098 if (nonzero_sign_valid == 0)
9099 return 0;
9101 return (unsignedp
9102 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9103 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9104 - floor_log2 (nonzero_bits (x, mode)))
9105 : 0)
9106 : num_sign_bit_copies (x, mode) - 1);
9109 /* This function is called from `simplify_shift_const' to merge two
9110 outer operations. Specifically, we have already found that we need
9111 to perform operation *POP0 with constant *PCONST0 at the outermost
9112 position. We would now like to also perform OP1 with constant CONST1
9113 (with *POP0 being done last).
9115 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9116 the resulting operation. *PCOMP_P is set to 1 if we would need to
9117 complement the innermost operand, otherwise it is unchanged.
9119 MODE is the mode in which the operation will be done. No bits outside
9120 the width of this mode matter. It is assumed that the width of this mode
9121 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9123 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9124 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9125 result is simply *PCONST0.
9127 If the resulting operation cannot be expressed as one operation, we
9128 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9130 static int
9131 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9133 enum rtx_code op0 = *pop0;
9134 HOST_WIDE_INT const0 = *pconst0;
9136 const0 &= GET_MODE_MASK (mode);
9137 const1 &= GET_MODE_MASK (mode);
9139 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9140 if (op0 == AND)
9141 const1 &= const0;
9143 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9144 if OP0 is SET. */
9146 if (op1 == UNKNOWN || op0 == SET)
9147 return 1;
9149 else if (op0 == UNKNOWN)
9150 op0 = op1, const0 = const1;
9152 else if (op0 == op1)
9154 switch (op0)
9156 case AND:
9157 const0 &= const1;
9158 break;
9159 case IOR:
9160 const0 |= const1;
9161 break;
9162 case XOR:
9163 const0 ^= const1;
9164 break;
9165 case PLUS:
9166 const0 += const1;
9167 break;
9168 case NEG:
9169 op0 = UNKNOWN;
9170 break;
9171 default:
9172 break;
9176 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9177 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9178 return 0;
9180 /* If the two constants aren't the same, we can't do anything. The
9181 remaining six cases can all be done. */
9182 else if (const0 != const1)
9183 return 0;
9185 else
9186 switch (op0)
9188 case IOR:
9189 if (op1 == AND)
9190 /* (a & b) | b == b */
9191 op0 = SET;
9192 else /* op1 == XOR */
9193 /* (a ^ b) | b == a | b */
9195 break;
9197 case XOR:
9198 if (op1 == AND)
9199 /* (a & b) ^ b == (~a) & b */
9200 op0 = AND, *pcomp_p = 1;
9201 else /* op1 == IOR */
9202 /* (a | b) ^ b == a & ~b */
9203 op0 = AND, const0 = ~const0;
9204 break;
9206 case AND:
9207 if (op1 == IOR)
9208 /* (a | b) & b == b */
9209 op0 = SET;
9210 else /* op1 == XOR */
9211 /* (a ^ b) & b) == (~a) & b */
9212 *pcomp_p = 1;
9213 break;
9214 default:
9215 break;
9218 /* Check for NO-OP cases. */
9219 const0 &= GET_MODE_MASK (mode);
9220 if (const0 == 0
9221 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9222 op0 = UNKNOWN;
9223 else if (const0 == 0 && op0 == AND)
9224 op0 = SET;
9225 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9226 && op0 == AND)
9227 op0 = UNKNOWN;
9229 *pop0 = op0;
9231 /* ??? Slightly redundant with the above mask, but not entirely.
9232 Moving this above means we'd have to sign-extend the mode mask
9233 for the final test. */
9234 if (op0 != UNKNOWN && op0 != NEG)
9235 *pconst0 = trunc_int_for_mode (const0, mode);
9237 return 1;
9240 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9241 the shift in. The original shift operation CODE is performed on OP in
9242 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9243 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9244 result of the shift is subject to operation OUTER_CODE with operand
9245 OUTER_CONST. */
9247 static enum machine_mode
9248 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9249 enum machine_mode orig_mode, enum machine_mode mode,
9250 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9252 if (orig_mode == mode)
9253 return mode;
9254 gcc_assert (GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (orig_mode));
9256 /* In general we can't perform in wider mode for right shift and rotate. */
9257 switch (code)
9259 case ASHIFTRT:
9260 /* We can still widen if the bits brought in from the left are identical
9261 to the sign bit of ORIG_MODE. */
9262 if (num_sign_bit_copies (op, mode)
9263 > (unsigned) (GET_MODE_BITSIZE (mode)
9264 - GET_MODE_BITSIZE (orig_mode)))
9265 return mode;
9266 return orig_mode;
9268 case LSHIFTRT:
9269 /* Similarly here but with zero bits. */
9270 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9271 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9272 return mode;
9274 /* We can also widen if the bits brought in will be masked off. This
9275 operation is performed in ORIG_MODE. */
9276 if (outer_code == AND)
9278 int care_bits = low_bitmask_len (orig_mode, outer_const);
9280 if (care_bits >= 0
9281 && GET_MODE_BITSIZE (orig_mode) - care_bits >= count)
9282 return mode;
9284 /* fall through */
9286 case ROTATE:
9287 return orig_mode;
9289 case ROTATERT:
9290 gcc_unreachable ();
9292 default:
9293 return mode;
9297 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9298 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
9299 simplify it. Otherwise, return a simplified value.
9301 The shift is normally computed in the widest mode we find in VAROP, as
9302 long as it isn't a different number of words than RESULT_MODE. Exceptions
9303 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9305 static rtx
9306 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9307 rtx varop, int orig_count)
9309 enum rtx_code orig_code = code;
9310 rtx orig_varop = varop;
9311 int count;
9312 enum machine_mode mode = result_mode;
9313 enum machine_mode shift_mode, tmode;
9314 unsigned int mode_words
9315 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9316 /* We form (outer_op (code varop count) (outer_const)). */
9317 enum rtx_code outer_op = UNKNOWN;
9318 HOST_WIDE_INT outer_const = 0;
9319 int complement_p = 0;
9320 rtx new_rtx, x;
9322 /* Make sure and truncate the "natural" shift on the way in. We don't
9323 want to do this inside the loop as it makes it more difficult to
9324 combine shifts. */
9325 if (SHIFT_COUNT_TRUNCATED)
9326 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9328 /* If we were given an invalid count, don't do anything except exactly
9329 what was requested. */
9331 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9332 return NULL_RTX;
9334 count = orig_count;
9336 /* Unless one of the branches of the `if' in this loop does a `continue',
9337 we will `break' the loop after the `if'. */
9339 while (count != 0)
9341 /* If we have an operand of (clobber (const_int 0)), fail. */
9342 if (GET_CODE (varop) == CLOBBER)
9343 return NULL_RTX;
9345 /* Convert ROTATERT to ROTATE. */
9346 if (code == ROTATERT)
9348 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9349 code = ROTATE;
9350 if (VECTOR_MODE_P (result_mode))
9351 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9352 else
9353 count = bitsize - count;
9356 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9357 mode, outer_op, outer_const);
9359 /* Handle cases where the count is greater than the size of the mode
9360 minus 1. For ASHIFT, use the size minus one as the count (this can
9361 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9362 take the count modulo the size. For other shifts, the result is
9363 zero.
9365 Since these shifts are being produced by the compiler by combining
9366 multiple operations, each of which are defined, we know what the
9367 result is supposed to be. */
9369 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9371 if (code == ASHIFTRT)
9372 count = GET_MODE_BITSIZE (shift_mode) - 1;
9373 else if (code == ROTATE || code == ROTATERT)
9374 count %= GET_MODE_BITSIZE (shift_mode);
9375 else
9377 /* We can't simply return zero because there may be an
9378 outer op. */
9379 varop = const0_rtx;
9380 count = 0;
9381 break;
9385 /* If we discovered we had to complement VAROP, leave. Making a NOT
9386 here would cause an infinite loop. */
9387 if (complement_p)
9388 break;
9390 /* An arithmetic right shift of a quantity known to be -1 or 0
9391 is a no-op. */
9392 if (code == ASHIFTRT
9393 && (num_sign_bit_copies (varop, shift_mode)
9394 == GET_MODE_BITSIZE (shift_mode)))
9396 count = 0;
9397 break;
9400 /* If we are doing an arithmetic right shift and discarding all but
9401 the sign bit copies, this is equivalent to doing a shift by the
9402 bitsize minus one. Convert it into that shift because it will often
9403 allow other simplifications. */
9405 if (code == ASHIFTRT
9406 && (count + num_sign_bit_copies (varop, shift_mode)
9407 >= GET_MODE_BITSIZE (shift_mode)))
9408 count = GET_MODE_BITSIZE (shift_mode) - 1;
9410 /* We simplify the tests below and elsewhere by converting
9411 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9412 `make_compound_operation' will convert it to an ASHIFTRT for
9413 those machines (such as VAX) that don't have an LSHIFTRT. */
9414 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9415 && code == ASHIFTRT
9416 && ((nonzero_bits (varop, shift_mode)
9417 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9418 == 0))
9419 code = LSHIFTRT;
9421 if (((code == LSHIFTRT
9422 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9423 && !(nonzero_bits (varop, shift_mode) >> count))
9424 || (code == ASHIFT
9425 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9426 && !((nonzero_bits (varop, shift_mode) << count)
9427 & GET_MODE_MASK (shift_mode))))
9428 && !side_effects_p (varop))
9429 varop = const0_rtx;
9431 switch (GET_CODE (varop))
9433 case SIGN_EXTEND:
9434 case ZERO_EXTEND:
9435 case SIGN_EXTRACT:
9436 case ZERO_EXTRACT:
9437 new_rtx = expand_compound_operation (varop);
9438 if (new_rtx != varop)
9440 varop = new_rtx;
9441 continue;
9443 break;
9445 case MEM:
9446 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9447 minus the width of a smaller mode, we can do this with a
9448 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9449 if ((code == ASHIFTRT || code == LSHIFTRT)
9450 && ! mode_dependent_address_p (XEXP (varop, 0))
9451 && ! MEM_VOLATILE_P (varop)
9452 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9453 MODE_INT, 1)) != BLKmode)
9455 new_rtx = adjust_address_nv (varop, tmode,
9456 BYTES_BIG_ENDIAN ? 0
9457 : count / BITS_PER_UNIT);
9459 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9460 : ZERO_EXTEND, mode, new_rtx);
9461 count = 0;
9462 continue;
9464 break;
9466 case SUBREG:
9467 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9468 the same number of words as what we've seen so far. Then store
9469 the widest mode in MODE. */
9470 if (subreg_lowpart_p (varop)
9471 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9472 > GET_MODE_SIZE (GET_MODE (varop)))
9473 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9474 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9475 == mode_words)
9477 varop = SUBREG_REG (varop);
9478 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9479 mode = GET_MODE (varop);
9480 continue;
9482 break;
9484 case MULT:
9485 /* Some machines use MULT instead of ASHIFT because MULT
9486 is cheaper. But it is still better on those machines to
9487 merge two shifts into one. */
9488 if (CONST_INT_P (XEXP (varop, 1))
9489 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9491 varop
9492 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9493 XEXP (varop, 0),
9494 GEN_INT (exact_log2 (
9495 INTVAL (XEXP (varop, 1)))));
9496 continue;
9498 break;
9500 case UDIV:
9501 /* Similar, for when divides are cheaper. */
9502 if (CONST_INT_P (XEXP (varop, 1))
9503 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9505 varop
9506 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9507 XEXP (varop, 0),
9508 GEN_INT (exact_log2 (
9509 INTVAL (XEXP (varop, 1)))));
9510 continue;
9512 break;
9514 case ASHIFTRT:
9515 /* If we are extracting just the sign bit of an arithmetic
9516 right shift, that shift is not needed. However, the sign
9517 bit of a wider mode may be different from what would be
9518 interpreted as the sign bit in a narrower mode, so, if
9519 the result is narrower, don't discard the shift. */
9520 if (code == LSHIFTRT
9521 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9522 && (GET_MODE_BITSIZE (result_mode)
9523 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9525 varop = XEXP (varop, 0);
9526 continue;
9529 /* ... fall through ... */
9531 case LSHIFTRT:
9532 case ASHIFT:
9533 case ROTATE:
9534 /* Here we have two nested shifts. The result is usually the
9535 AND of a new shift with a mask. We compute the result below. */
9536 if (CONST_INT_P (XEXP (varop, 1))
9537 && INTVAL (XEXP (varop, 1)) >= 0
9538 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9539 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9540 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9541 && !VECTOR_MODE_P (result_mode))
9543 enum rtx_code first_code = GET_CODE (varop);
9544 unsigned int first_count = INTVAL (XEXP (varop, 1));
9545 unsigned HOST_WIDE_INT mask;
9546 rtx mask_rtx;
9548 /* We have one common special case. We can't do any merging if
9549 the inner code is an ASHIFTRT of a smaller mode. However, if
9550 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9551 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9552 we can convert it to
9553 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9554 This simplifies certain SIGN_EXTEND operations. */
9555 if (code == ASHIFT && first_code == ASHIFTRT
9556 && count == (GET_MODE_BITSIZE (result_mode)
9557 - GET_MODE_BITSIZE (GET_MODE (varop))))
9559 /* C3 has the low-order C1 bits zero. */
9561 mask = (GET_MODE_MASK (mode)
9562 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9564 varop = simplify_and_const_int (NULL_RTX, result_mode,
9565 XEXP (varop, 0), mask);
9566 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9567 varop, count);
9568 count = first_count;
9569 code = ASHIFTRT;
9570 continue;
9573 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9574 than C1 high-order bits equal to the sign bit, we can convert
9575 this to either an ASHIFT or an ASHIFTRT depending on the
9576 two counts.
9578 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9580 if (code == ASHIFTRT && first_code == ASHIFT
9581 && GET_MODE (varop) == shift_mode
9582 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9583 > first_count))
9585 varop = XEXP (varop, 0);
9586 count -= first_count;
9587 if (count < 0)
9589 count = -count;
9590 code = ASHIFT;
9593 continue;
9596 /* There are some cases we can't do. If CODE is ASHIFTRT,
9597 we can only do this if FIRST_CODE is also ASHIFTRT.
9599 We can't do the case when CODE is ROTATE and FIRST_CODE is
9600 ASHIFTRT.
9602 If the mode of this shift is not the mode of the outer shift,
9603 we can't do this if either shift is a right shift or ROTATE.
9605 Finally, we can't do any of these if the mode is too wide
9606 unless the codes are the same.
9608 Handle the case where the shift codes are the same
9609 first. */
9611 if (code == first_code)
9613 if (GET_MODE (varop) != result_mode
9614 && (code == ASHIFTRT || code == LSHIFTRT
9615 || code == ROTATE))
9616 break;
9618 count += first_count;
9619 varop = XEXP (varop, 0);
9620 continue;
9623 if (code == ASHIFTRT
9624 || (code == ROTATE && first_code == ASHIFTRT)
9625 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9626 || (GET_MODE (varop) != result_mode
9627 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9628 || first_code == ROTATE
9629 || code == ROTATE)))
9630 break;
9632 /* To compute the mask to apply after the shift, shift the
9633 nonzero bits of the inner shift the same way the
9634 outer shift will. */
9636 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9638 mask_rtx
9639 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9640 GEN_INT (count));
9642 /* Give up if we can't compute an outer operation to use. */
9643 if (mask_rtx == 0
9644 || !CONST_INT_P (mask_rtx)
9645 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9646 INTVAL (mask_rtx),
9647 result_mode, &complement_p))
9648 break;
9650 /* If the shifts are in the same direction, we add the
9651 counts. Otherwise, we subtract them. */
9652 if ((code == ASHIFTRT || code == LSHIFTRT)
9653 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9654 count += first_count;
9655 else
9656 count -= first_count;
9658 /* If COUNT is positive, the new shift is usually CODE,
9659 except for the two exceptions below, in which case it is
9660 FIRST_CODE. If the count is negative, FIRST_CODE should
9661 always be used */
9662 if (count > 0
9663 && ((first_code == ROTATE && code == ASHIFT)
9664 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9665 code = first_code;
9666 else if (count < 0)
9667 code = first_code, count = -count;
9669 varop = XEXP (varop, 0);
9670 continue;
9673 /* If we have (A << B << C) for any shift, we can convert this to
9674 (A << C << B). This wins if A is a constant. Only try this if
9675 B is not a constant. */
9677 else if (GET_CODE (varop) == code
9678 && CONST_INT_P (XEXP (varop, 0))
9679 && !CONST_INT_P (XEXP (varop, 1)))
9681 rtx new_rtx = simplify_const_binary_operation (code, mode,
9682 XEXP (varop, 0),
9683 GEN_INT (count));
9684 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
9685 count = 0;
9686 continue;
9688 break;
9690 case NOT:
9691 if (VECTOR_MODE_P (mode))
9692 break;
9694 /* Make this fit the case below. */
9695 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9696 GEN_INT (GET_MODE_MASK (mode)));
9697 continue;
9699 case IOR:
9700 case AND:
9701 case XOR:
9702 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9703 with C the size of VAROP - 1 and the shift is logical if
9704 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9705 we have an (le X 0) operation. If we have an arithmetic shift
9706 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9707 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9709 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9710 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9711 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9712 && (code == LSHIFTRT || code == ASHIFTRT)
9713 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9714 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9716 count = 0;
9717 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9718 const0_rtx);
9720 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9721 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9723 continue;
9726 /* If we have (shift (logical)), move the logical to the outside
9727 to allow it to possibly combine with another logical and the
9728 shift to combine with another shift. This also canonicalizes to
9729 what a ZERO_EXTRACT looks like. Also, some machines have
9730 (and (shift)) insns. */
9732 if (CONST_INT_P (XEXP (varop, 1))
9733 /* We can't do this if we have (ashiftrt (xor)) and the
9734 constant has its sign bit set in shift_mode. */
9735 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9736 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9737 shift_mode))
9738 && (new_rtx = simplify_const_binary_operation (code, result_mode,
9739 XEXP (varop, 1),
9740 GEN_INT (count))) != 0
9741 && CONST_INT_P (new_rtx)
9742 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9743 INTVAL (new_rtx), result_mode, &complement_p))
9745 varop = XEXP (varop, 0);
9746 continue;
9749 /* If we can't do that, try to simplify the shift in each arm of the
9750 logical expression, make a new logical expression, and apply
9751 the inverse distributive law. This also can't be done
9752 for some (ashiftrt (xor)). */
9753 if (CONST_INT_P (XEXP (varop, 1))
9754 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9755 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9756 shift_mode)))
9758 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9759 XEXP (varop, 0), count);
9760 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9761 XEXP (varop, 1), count);
9763 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9764 lhs, rhs);
9765 varop = apply_distributive_law (varop);
9767 count = 0;
9768 continue;
9770 break;
9772 case EQ:
9773 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9774 says that the sign bit can be tested, FOO has mode MODE, C is
9775 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9776 that may be nonzero. */
9777 if (code == LSHIFTRT
9778 && XEXP (varop, 1) == const0_rtx
9779 && GET_MODE (XEXP (varop, 0)) == result_mode
9780 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9781 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9782 && STORE_FLAG_VALUE == -1
9783 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9784 && merge_outer_ops (&outer_op, &outer_const, XOR,
9785 (HOST_WIDE_INT) 1, result_mode,
9786 &complement_p))
9788 varop = XEXP (varop, 0);
9789 count = 0;
9790 continue;
9792 break;
9794 case NEG:
9795 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9796 than the number of bits in the mode is equivalent to A. */
9797 if (code == LSHIFTRT
9798 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9799 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9801 varop = XEXP (varop, 0);
9802 count = 0;
9803 continue;
9806 /* NEG commutes with ASHIFT since it is multiplication. Move the
9807 NEG outside to allow shifts to combine. */
9808 if (code == ASHIFT
9809 && merge_outer_ops (&outer_op, &outer_const, NEG,
9810 (HOST_WIDE_INT) 0, result_mode,
9811 &complement_p))
9813 varop = XEXP (varop, 0);
9814 continue;
9816 break;
9818 case PLUS:
9819 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9820 is one less than the number of bits in the mode is
9821 equivalent to (xor A 1). */
9822 if (code == LSHIFTRT
9823 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9824 && XEXP (varop, 1) == constm1_rtx
9825 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9826 && merge_outer_ops (&outer_op, &outer_const, XOR,
9827 (HOST_WIDE_INT) 1, result_mode,
9828 &complement_p))
9830 count = 0;
9831 varop = XEXP (varop, 0);
9832 continue;
9835 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9836 that might be nonzero in BAR are those being shifted out and those
9837 bits are known zero in FOO, we can replace the PLUS with FOO.
9838 Similarly in the other operand order. This code occurs when
9839 we are computing the size of a variable-size array. */
9841 if ((code == ASHIFTRT || code == LSHIFTRT)
9842 && count < HOST_BITS_PER_WIDE_INT
9843 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9844 && (nonzero_bits (XEXP (varop, 1), result_mode)
9845 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9847 varop = XEXP (varop, 0);
9848 continue;
9850 else if ((code == ASHIFTRT || code == LSHIFTRT)
9851 && count < HOST_BITS_PER_WIDE_INT
9852 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9853 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9854 >> count)
9855 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9856 & nonzero_bits (XEXP (varop, 1),
9857 result_mode)))
9859 varop = XEXP (varop, 1);
9860 continue;
9863 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9864 if (code == ASHIFT
9865 && CONST_INT_P (XEXP (varop, 1))
9866 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
9867 XEXP (varop, 1),
9868 GEN_INT (count))) != 0
9869 && CONST_INT_P (new_rtx)
9870 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9871 INTVAL (new_rtx), result_mode, &complement_p))
9873 varop = XEXP (varop, 0);
9874 continue;
9877 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9878 signbit', and attempt to change the PLUS to an XOR and move it to
9879 the outer operation as is done above in the AND/IOR/XOR case
9880 leg for shift(logical). See details in logical handling above
9881 for reasoning in doing so. */
9882 if (code == LSHIFTRT
9883 && CONST_INT_P (XEXP (varop, 1))
9884 && mode_signbit_p (result_mode, XEXP (varop, 1))
9885 && (new_rtx = simplify_const_binary_operation (code, result_mode,
9886 XEXP (varop, 1),
9887 GEN_INT (count))) != 0
9888 && CONST_INT_P (new_rtx)
9889 && merge_outer_ops (&outer_op, &outer_const, XOR,
9890 INTVAL (new_rtx), result_mode, &complement_p))
9892 varop = XEXP (varop, 0);
9893 continue;
9896 break;
9898 case MINUS:
9899 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9900 with C the size of VAROP - 1 and the shift is logical if
9901 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9902 we have a (gt X 0) operation. If the shift is arithmetic with
9903 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9904 we have a (neg (gt X 0)) operation. */
9906 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9907 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9908 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9909 && (code == LSHIFTRT || code == ASHIFTRT)
9910 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
9911 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9912 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9914 count = 0;
9915 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9916 const0_rtx);
9918 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9919 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9921 continue;
9923 break;
9925 case TRUNCATE:
9926 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9927 if the truncate does not affect the value. */
9928 if (code == LSHIFTRT
9929 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9930 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
9931 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9932 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9933 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9935 rtx varop_inner = XEXP (varop, 0);
9937 varop_inner
9938 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9939 XEXP (varop_inner, 0),
9940 GEN_INT
9941 (count + INTVAL (XEXP (varop_inner, 1))));
9942 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9943 count = 0;
9944 continue;
9946 break;
9948 default:
9949 break;
9952 break;
9955 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
9956 outer_op, outer_const);
9958 /* We have now finished analyzing the shift. The result should be
9959 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9960 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9961 to the result of the shift. OUTER_CONST is the relevant constant,
9962 but we must turn off all bits turned off in the shift. */
9964 if (outer_op == UNKNOWN
9965 && orig_code == code && orig_count == count
9966 && varop == orig_varop
9967 && shift_mode == GET_MODE (varop))
9968 return NULL_RTX;
9970 /* Make a SUBREG if necessary. If we can't make it, fail. */
9971 varop = gen_lowpart (shift_mode, varop);
9972 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9973 return NULL_RTX;
9975 /* If we have an outer operation and we just made a shift, it is
9976 possible that we could have simplified the shift were it not
9977 for the outer operation. So try to do the simplification
9978 recursively. */
9980 if (outer_op != UNKNOWN)
9981 x = simplify_shift_const_1 (code, shift_mode, varop, count);
9982 else
9983 x = NULL_RTX;
9985 if (x == NULL_RTX)
9986 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9988 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9989 turn off all the bits that the shift would have turned off. */
9990 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9991 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9992 GET_MODE_MASK (result_mode) >> orig_count);
9994 /* Do the remainder of the processing in RESULT_MODE. */
9995 x = gen_lowpart_or_truncate (result_mode, x);
9997 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9998 operation. */
9999 if (complement_p)
10000 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10002 if (outer_op != UNKNOWN)
10004 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10005 && GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10006 outer_const = trunc_int_for_mode (outer_const, result_mode);
10008 if (outer_op == AND)
10009 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10010 else if (outer_op == SET)
10012 /* This means that we have determined that the result is
10013 equivalent to a constant. This should be rare. */
10014 if (!side_effects_p (x))
10015 x = GEN_INT (outer_const);
10017 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10018 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10019 else
10020 x = simplify_gen_binary (outer_op, result_mode, x,
10021 GEN_INT (outer_const));
10024 return x;
10027 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10028 The result of the shift is RESULT_MODE. If we cannot simplify it,
10029 return X or, if it is NULL, synthesize the expression with
10030 simplify_gen_binary. Otherwise, return a simplified value.
10032 The shift is normally computed in the widest mode we find in VAROP, as
10033 long as it isn't a different number of words than RESULT_MODE. Exceptions
10034 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10036 static rtx
10037 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10038 rtx varop, int count)
10040 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10041 if (tem)
10042 return tem;
10044 if (!x)
10045 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10046 if (GET_MODE (x) != result_mode)
10047 x = gen_lowpart (result_mode, x);
10048 return x;
10052 /* Like recog, but we receive the address of a pointer to a new pattern.
10053 We try to match the rtx that the pointer points to.
10054 If that fails, we may try to modify or replace the pattern,
10055 storing the replacement into the same pointer object.
10057 Modifications include deletion or addition of CLOBBERs.
10059 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10060 the CLOBBERs are placed.
10062 The value is the final insn code from the pattern ultimately matched,
10063 or -1. */
10065 static int
10066 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
10068 rtx pat = *pnewpat;
10069 int insn_code_number;
10070 int num_clobbers_to_add = 0;
10071 int i;
10072 rtx notes = 0;
10073 rtx old_notes, old_pat;
10075 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10076 we use to indicate that something didn't match. If we find such a
10077 thing, force rejection. */
10078 if (GET_CODE (pat) == PARALLEL)
10079 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10080 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10081 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10082 return -1;
10084 old_pat = PATTERN (insn);
10085 old_notes = REG_NOTES (insn);
10086 PATTERN (insn) = pat;
10087 REG_NOTES (insn) = 0;
10089 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10090 if (dump_file && (dump_flags & TDF_DETAILS))
10092 if (insn_code_number < 0)
10093 fputs ("Failed to match this instruction:\n", dump_file);
10094 else
10095 fputs ("Successfully matched this instruction:\n", dump_file);
10096 print_rtl_single (dump_file, pat);
10099 /* If it isn't, there is the possibility that we previously had an insn
10100 that clobbered some register as a side effect, but the combined
10101 insn doesn't need to do that. So try once more without the clobbers
10102 unless this represents an ASM insn. */
10104 if (insn_code_number < 0 && ! check_asm_operands (pat)
10105 && GET_CODE (pat) == PARALLEL)
10107 int pos;
10109 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10110 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10112 if (i != pos)
10113 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10114 pos++;
10117 SUBST_INT (XVECLEN (pat, 0), pos);
10119 if (pos == 1)
10120 pat = XVECEXP (pat, 0, 0);
10122 PATTERN (insn) = pat;
10123 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10124 if (dump_file && (dump_flags & TDF_DETAILS))
10126 if (insn_code_number < 0)
10127 fputs ("Failed to match this instruction:\n", dump_file);
10128 else
10129 fputs ("Successfully matched this instruction:\n", dump_file);
10130 print_rtl_single (dump_file, pat);
10133 PATTERN (insn) = old_pat;
10134 REG_NOTES (insn) = old_notes;
10136 /* Recognize all noop sets, these will be killed by followup pass. */
10137 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10138 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10140 /* If we had any clobbers to add, make a new pattern than contains
10141 them. Then check to make sure that all of them are dead. */
10142 if (num_clobbers_to_add)
10144 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10145 rtvec_alloc (GET_CODE (pat) == PARALLEL
10146 ? (XVECLEN (pat, 0)
10147 + num_clobbers_to_add)
10148 : num_clobbers_to_add + 1));
10150 if (GET_CODE (pat) == PARALLEL)
10151 for (i = 0; i < XVECLEN (pat, 0); i++)
10152 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10153 else
10154 XVECEXP (newpat, 0, 0) = pat;
10156 add_clobbers (newpat, insn_code_number);
10158 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10159 i < XVECLEN (newpat, 0); i++)
10161 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10162 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10163 return -1;
10164 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10166 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10167 notes = alloc_reg_note (REG_UNUSED,
10168 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10171 pat = newpat;
10174 *pnewpat = pat;
10175 *pnotes = notes;
10177 return insn_code_number;
10180 /* Like gen_lowpart_general but for use by combine. In combine it
10181 is not possible to create any new pseudoregs. However, it is
10182 safe to create invalid memory addresses, because combine will
10183 try to recognize them and all they will do is make the combine
10184 attempt fail.
10186 If for some reason this cannot do its job, an rtx
10187 (clobber (const_int 0)) is returned.
10188 An insn containing that will not be recognized. */
10190 static rtx
10191 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10193 enum machine_mode imode = GET_MODE (x);
10194 unsigned int osize = GET_MODE_SIZE (omode);
10195 unsigned int isize = GET_MODE_SIZE (imode);
10196 rtx result;
10198 if (omode == imode)
10199 return x;
10201 /* Return identity if this is a CONST or symbolic reference. */
10202 if (omode == Pmode
10203 && (GET_CODE (x) == CONST
10204 || GET_CODE (x) == SYMBOL_REF
10205 || GET_CODE (x) == LABEL_REF))
10206 return x;
10208 /* We can only support MODE being wider than a word if X is a
10209 constant integer or has a mode the same size. */
10210 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10211 && ! ((imode == VOIDmode
10212 && (CONST_INT_P (x)
10213 || GET_CODE (x) == CONST_DOUBLE))
10214 || isize == osize))
10215 goto fail;
10217 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10218 won't know what to do. So we will strip off the SUBREG here and
10219 process normally. */
10220 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10222 x = SUBREG_REG (x);
10224 /* For use in case we fall down into the address adjustments
10225 further below, we need to adjust the known mode and size of
10226 x; imode and isize, since we just adjusted x. */
10227 imode = GET_MODE (x);
10229 if (imode == omode)
10230 return x;
10232 isize = GET_MODE_SIZE (imode);
10235 result = gen_lowpart_common (omode, x);
10237 if (result)
10238 return result;
10240 if (MEM_P (x))
10242 int offset = 0;
10244 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10245 address. */
10246 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10247 goto fail;
10249 /* If we want to refer to something bigger than the original memref,
10250 generate a paradoxical subreg instead. That will force a reload
10251 of the original memref X. */
10252 if (isize < osize)
10253 return gen_rtx_SUBREG (omode, x, 0);
10255 if (WORDS_BIG_ENDIAN)
10256 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10258 /* Adjust the address so that the address-after-the-data is
10259 unchanged. */
10260 if (BYTES_BIG_ENDIAN)
10261 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10263 return adjust_address_nv (x, omode, offset);
10266 /* If X is a comparison operator, rewrite it in a new mode. This
10267 probably won't match, but may allow further simplifications. */
10268 else if (COMPARISON_P (x))
10269 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10271 /* If we couldn't simplify X any other way, just enclose it in a
10272 SUBREG. Normally, this SUBREG won't match, but some patterns may
10273 include an explicit SUBREG or we may simplify it further in combine. */
10274 else
10276 int offset = 0;
10277 rtx res;
10279 offset = subreg_lowpart_offset (omode, imode);
10280 if (imode == VOIDmode)
10282 imode = int_mode_for_mode (omode);
10283 x = gen_lowpart_common (imode, x);
10284 if (x == NULL)
10285 goto fail;
10287 res = simplify_gen_subreg (omode, x, imode, offset);
10288 if (res)
10289 return res;
10292 fail:
10293 return gen_rtx_CLOBBER (omode, const0_rtx);
10296 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10297 comparison code that will be tested.
10299 The result is a possibly different comparison code to use. *POP0 and
10300 *POP1 may be updated.
10302 It is possible that we might detect that a comparison is either always
10303 true or always false. However, we do not perform general constant
10304 folding in combine, so this knowledge isn't useful. Such tautologies
10305 should have been detected earlier. Hence we ignore all such cases. */
10307 static enum rtx_code
10308 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10310 rtx op0 = *pop0;
10311 rtx op1 = *pop1;
10312 rtx tem, tem1;
10313 int i;
10314 enum machine_mode mode, tmode;
10316 /* Try a few ways of applying the same transformation to both operands. */
10317 while (1)
10319 #ifndef WORD_REGISTER_OPERATIONS
10320 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10321 so check specially. */
10322 if (code != GTU && code != GEU && code != LTU && code != LEU
10323 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10324 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10325 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10326 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10327 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10328 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10329 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10330 && CONST_INT_P (XEXP (op0, 1))
10331 && XEXP (op0, 1) == XEXP (op1, 1)
10332 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10333 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10334 && (INTVAL (XEXP (op0, 1))
10335 == (GET_MODE_BITSIZE (GET_MODE (op0))
10336 - (GET_MODE_BITSIZE
10337 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10339 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10340 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10342 #endif
10344 /* If both operands are the same constant shift, see if we can ignore the
10345 shift. We can if the shift is a rotate or if the bits shifted out of
10346 this shift are known to be zero for both inputs and if the type of
10347 comparison is compatible with the shift. */
10348 if (GET_CODE (op0) == GET_CODE (op1)
10349 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10350 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10351 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10352 && (code != GT && code != LT && code != GE && code != LE))
10353 || (GET_CODE (op0) == ASHIFTRT
10354 && (code != GTU && code != LTU
10355 && code != GEU && code != LEU)))
10356 && CONST_INT_P (XEXP (op0, 1))
10357 && INTVAL (XEXP (op0, 1)) >= 0
10358 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10359 && XEXP (op0, 1) == XEXP (op1, 1))
10361 enum machine_mode mode = GET_MODE (op0);
10362 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10363 int shift_count = INTVAL (XEXP (op0, 1));
10365 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10366 mask &= (mask >> shift_count) << shift_count;
10367 else if (GET_CODE (op0) == ASHIFT)
10368 mask = (mask & (mask << shift_count)) >> shift_count;
10370 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10371 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10372 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10373 else
10374 break;
10377 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10378 SUBREGs are of the same mode, and, in both cases, the AND would
10379 be redundant if the comparison was done in the narrower mode,
10380 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10381 and the operand's possibly nonzero bits are 0xffffff01; in that case
10382 if we only care about QImode, we don't need the AND). This case
10383 occurs if the output mode of an scc insn is not SImode and
10384 STORE_FLAG_VALUE == 1 (e.g., the 386).
10386 Similarly, check for a case where the AND's are ZERO_EXTEND
10387 operations from some narrower mode even though a SUBREG is not
10388 present. */
10390 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10391 && CONST_INT_P (XEXP (op0, 1))
10392 && CONST_INT_P (XEXP (op1, 1)))
10394 rtx inner_op0 = XEXP (op0, 0);
10395 rtx inner_op1 = XEXP (op1, 0);
10396 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10397 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10398 int changed = 0;
10400 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10401 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10402 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10403 && (GET_MODE (SUBREG_REG (inner_op0))
10404 == GET_MODE (SUBREG_REG (inner_op1)))
10405 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10406 <= HOST_BITS_PER_WIDE_INT)
10407 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10408 GET_MODE (SUBREG_REG (inner_op0)))))
10409 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10410 GET_MODE (SUBREG_REG (inner_op1))))))
10412 op0 = SUBREG_REG (inner_op0);
10413 op1 = SUBREG_REG (inner_op1);
10415 /* The resulting comparison is always unsigned since we masked
10416 off the original sign bit. */
10417 code = unsigned_condition (code);
10419 changed = 1;
10422 else if (c0 == c1)
10423 for (tmode = GET_CLASS_NARROWEST_MODE
10424 (GET_MODE_CLASS (GET_MODE (op0)));
10425 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10426 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10428 op0 = gen_lowpart (tmode, inner_op0);
10429 op1 = gen_lowpart (tmode, inner_op1);
10430 code = unsigned_condition (code);
10431 changed = 1;
10432 break;
10435 if (! changed)
10436 break;
10439 /* If both operands are NOT, we can strip off the outer operation
10440 and adjust the comparison code for swapped operands; similarly for
10441 NEG, except that this must be an equality comparison. */
10442 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10443 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10444 && (code == EQ || code == NE)))
10445 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10447 else
10448 break;
10451 /* If the first operand is a constant, swap the operands and adjust the
10452 comparison code appropriately, but don't do this if the second operand
10453 is already a constant integer. */
10454 if (swap_commutative_operands_p (op0, op1))
10456 tem = op0, op0 = op1, op1 = tem;
10457 code = swap_condition (code);
10460 /* We now enter a loop during which we will try to simplify the comparison.
10461 For the most part, we only are concerned with comparisons with zero,
10462 but some things may really be comparisons with zero but not start
10463 out looking that way. */
10465 while (CONST_INT_P (op1))
10467 enum machine_mode mode = GET_MODE (op0);
10468 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10469 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10470 int equality_comparison_p;
10471 int sign_bit_comparison_p;
10472 int unsigned_comparison_p;
10473 HOST_WIDE_INT const_op;
10475 /* We only want to handle integral modes. This catches VOIDmode,
10476 CCmode, and the floating-point modes. An exception is that we
10477 can handle VOIDmode if OP0 is a COMPARE or a comparison
10478 operation. */
10480 if (GET_MODE_CLASS (mode) != MODE_INT
10481 && ! (mode == VOIDmode
10482 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10483 break;
10485 /* Get the constant we are comparing against and turn off all bits
10486 not on in our mode. */
10487 const_op = INTVAL (op1);
10488 if (mode != VOIDmode)
10489 const_op = trunc_int_for_mode (const_op, mode);
10490 op1 = GEN_INT (const_op);
10492 /* If we are comparing against a constant power of two and the value
10493 being compared can only have that single bit nonzero (e.g., it was
10494 `and'ed with that bit), we can replace this with a comparison
10495 with zero. */
10496 if (const_op
10497 && (code == EQ || code == NE || code == GE || code == GEU
10498 || code == LT || code == LTU)
10499 && mode_width <= HOST_BITS_PER_WIDE_INT
10500 && exact_log2 (const_op) >= 0
10501 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10503 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10504 op1 = const0_rtx, const_op = 0;
10507 /* Similarly, if we are comparing a value known to be either -1 or
10508 0 with -1, change it to the opposite comparison against zero. */
10510 if (const_op == -1
10511 && (code == EQ || code == NE || code == GT || code == LE
10512 || code == GEU || code == LTU)
10513 && num_sign_bit_copies (op0, mode) == mode_width)
10515 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10516 op1 = const0_rtx, const_op = 0;
10519 /* Do some canonicalizations based on the comparison code. We prefer
10520 comparisons against zero and then prefer equality comparisons.
10521 If we can reduce the size of a constant, we will do that too. */
10523 switch (code)
10525 case LT:
10526 /* < C is equivalent to <= (C - 1) */
10527 if (const_op > 0)
10529 const_op -= 1;
10530 op1 = GEN_INT (const_op);
10531 code = LE;
10532 /* ... fall through to LE case below. */
10534 else
10535 break;
10537 case LE:
10538 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10539 if (const_op < 0)
10541 const_op += 1;
10542 op1 = GEN_INT (const_op);
10543 code = LT;
10546 /* If we are doing a <= 0 comparison on a value known to have
10547 a zero sign bit, we can replace this with == 0. */
10548 else if (const_op == 0
10549 && mode_width <= HOST_BITS_PER_WIDE_INT
10550 && (nonzero_bits (op0, mode)
10551 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10552 code = EQ;
10553 break;
10555 case GE:
10556 /* >= C is equivalent to > (C - 1). */
10557 if (const_op > 0)
10559 const_op -= 1;
10560 op1 = GEN_INT (const_op);
10561 code = GT;
10562 /* ... fall through to GT below. */
10564 else
10565 break;
10567 case GT:
10568 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10569 if (const_op < 0)
10571 const_op += 1;
10572 op1 = GEN_INT (const_op);
10573 code = GE;
10576 /* If we are doing a > 0 comparison on a value known to have
10577 a zero sign bit, we can replace this with != 0. */
10578 else if (const_op == 0
10579 && mode_width <= HOST_BITS_PER_WIDE_INT
10580 && (nonzero_bits (op0, mode)
10581 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10582 code = NE;
10583 break;
10585 case LTU:
10586 /* < C is equivalent to <= (C - 1). */
10587 if (const_op > 0)
10589 const_op -= 1;
10590 op1 = GEN_INT (const_op);
10591 code = LEU;
10592 /* ... fall through ... */
10595 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10596 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10597 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10599 const_op = 0, op1 = const0_rtx;
10600 code = GE;
10601 break;
10603 else
10604 break;
10606 case LEU:
10607 /* unsigned <= 0 is equivalent to == 0 */
10608 if (const_op == 0)
10609 code = EQ;
10611 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10612 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10613 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10615 const_op = 0, op1 = const0_rtx;
10616 code = GE;
10618 break;
10620 case GEU:
10621 /* >= C is equivalent to > (C - 1). */
10622 if (const_op > 1)
10624 const_op -= 1;
10625 op1 = GEN_INT (const_op);
10626 code = GTU;
10627 /* ... fall through ... */
10630 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10631 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10632 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10634 const_op = 0, op1 = const0_rtx;
10635 code = LT;
10636 break;
10638 else
10639 break;
10641 case GTU:
10642 /* unsigned > 0 is equivalent to != 0 */
10643 if (const_op == 0)
10644 code = NE;
10646 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10647 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10648 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10650 const_op = 0, op1 = const0_rtx;
10651 code = LT;
10653 break;
10655 default:
10656 break;
10659 /* Compute some predicates to simplify code below. */
10661 equality_comparison_p = (code == EQ || code == NE);
10662 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10663 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10664 || code == GEU);
10666 /* If this is a sign bit comparison and we can do arithmetic in
10667 MODE, say that we will only be needing the sign bit of OP0. */
10668 if (sign_bit_comparison_p
10669 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10670 op0 = force_to_mode (op0, mode,
10671 ((HOST_WIDE_INT) 1
10672 << (GET_MODE_BITSIZE (mode) - 1)),
10675 /* Now try cases based on the opcode of OP0. If none of the cases
10676 does a "continue", we exit this loop immediately after the
10677 switch. */
10679 switch (GET_CODE (op0))
10681 case ZERO_EXTRACT:
10682 /* If we are extracting a single bit from a variable position in
10683 a constant that has only a single bit set and are comparing it
10684 with zero, we can convert this into an equality comparison
10685 between the position and the location of the single bit. */
10686 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10687 have already reduced the shift count modulo the word size. */
10688 if (!SHIFT_COUNT_TRUNCATED
10689 && CONST_INT_P (XEXP (op0, 0))
10690 && XEXP (op0, 1) == const1_rtx
10691 && equality_comparison_p && const_op == 0
10692 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10694 if (BITS_BIG_ENDIAN)
10696 enum machine_mode new_mode
10697 = mode_for_extraction (EP_extzv, 1);
10698 if (new_mode == MAX_MACHINE_MODE)
10699 i = BITS_PER_WORD - 1 - i;
10700 else
10702 mode = new_mode;
10703 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10707 op0 = XEXP (op0, 2);
10708 op1 = GEN_INT (i);
10709 const_op = i;
10711 /* Result is nonzero iff shift count is equal to I. */
10712 code = reverse_condition (code);
10713 continue;
10716 /* ... fall through ... */
10718 case SIGN_EXTRACT:
10719 tem = expand_compound_operation (op0);
10720 if (tem != op0)
10722 op0 = tem;
10723 continue;
10725 break;
10727 case NOT:
10728 /* If testing for equality, we can take the NOT of the constant. */
10729 if (equality_comparison_p
10730 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10732 op0 = XEXP (op0, 0);
10733 op1 = tem;
10734 continue;
10737 /* If just looking at the sign bit, reverse the sense of the
10738 comparison. */
10739 if (sign_bit_comparison_p)
10741 op0 = XEXP (op0, 0);
10742 code = (code == GE ? LT : GE);
10743 continue;
10745 break;
10747 case NEG:
10748 /* If testing for equality, we can take the NEG of the constant. */
10749 if (equality_comparison_p
10750 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10752 op0 = XEXP (op0, 0);
10753 op1 = tem;
10754 continue;
10757 /* The remaining cases only apply to comparisons with zero. */
10758 if (const_op != 0)
10759 break;
10761 /* When X is ABS or is known positive,
10762 (neg X) is < 0 if and only if X != 0. */
10764 if (sign_bit_comparison_p
10765 && (GET_CODE (XEXP (op0, 0)) == ABS
10766 || (mode_width <= HOST_BITS_PER_WIDE_INT
10767 && (nonzero_bits (XEXP (op0, 0), mode)
10768 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10770 op0 = XEXP (op0, 0);
10771 code = (code == LT ? NE : EQ);
10772 continue;
10775 /* If we have NEG of something whose two high-order bits are the
10776 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10777 if (num_sign_bit_copies (op0, mode) >= 2)
10779 op0 = XEXP (op0, 0);
10780 code = swap_condition (code);
10781 continue;
10783 break;
10785 case ROTATE:
10786 /* If we are testing equality and our count is a constant, we
10787 can perform the inverse operation on our RHS. */
10788 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
10789 && (tem = simplify_binary_operation (ROTATERT, mode,
10790 op1, XEXP (op0, 1))) != 0)
10792 op0 = XEXP (op0, 0);
10793 op1 = tem;
10794 continue;
10797 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10798 a particular bit. Convert it to an AND of a constant of that
10799 bit. This will be converted into a ZERO_EXTRACT. */
10800 if (const_op == 0 && sign_bit_comparison_p
10801 && CONST_INT_P (XEXP (op0, 1))
10802 && mode_width <= HOST_BITS_PER_WIDE_INT)
10804 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10805 ((HOST_WIDE_INT) 1
10806 << (mode_width - 1
10807 - INTVAL (XEXP (op0, 1)))));
10808 code = (code == LT ? NE : EQ);
10809 continue;
10812 /* Fall through. */
10814 case ABS:
10815 /* ABS is ignorable inside an equality comparison with zero. */
10816 if (const_op == 0 && equality_comparison_p)
10818 op0 = XEXP (op0, 0);
10819 continue;
10821 break;
10823 case SIGN_EXTEND:
10824 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10825 (compare FOO CONST) if CONST fits in FOO's mode and we
10826 are either testing inequality or have an unsigned
10827 comparison with ZERO_EXTEND or a signed comparison with
10828 SIGN_EXTEND. But don't do it if we don't have a compare
10829 insn of the given mode, since we'd have to revert it
10830 later on, and then we wouldn't know whether to sign- or
10831 zero-extend. */
10832 mode = GET_MODE (XEXP (op0, 0));
10833 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10834 && ! unsigned_comparison_p
10835 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10836 && ((unsigned HOST_WIDE_INT) const_op
10837 < (((unsigned HOST_WIDE_INT) 1
10838 << (GET_MODE_BITSIZE (mode) - 1))))
10839 && have_insn_for (COMPARE, mode))
10841 op0 = XEXP (op0, 0);
10842 continue;
10844 break;
10846 case SUBREG:
10847 /* Check for the case where we are comparing A - C1 with C2, that is
10849 (subreg:MODE (plus (A) (-C1))) op (C2)
10851 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10852 comparison in the wider mode. One of the following two conditions
10853 must be true in order for this to be valid:
10855 1. The mode extension results in the same bit pattern being added
10856 on both sides and the comparison is equality or unsigned. As
10857 C2 has been truncated to fit in MODE, the pattern can only be
10858 all 0s or all 1s.
10860 2. The mode extension results in the sign bit being copied on
10861 each side.
10863 The difficulty here is that we have predicates for A but not for
10864 (A - C1) so we need to check that C1 is within proper bounds so
10865 as to perturbate A as little as possible. */
10867 if (mode_width <= HOST_BITS_PER_WIDE_INT
10868 && subreg_lowpart_p (op0)
10869 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10870 && GET_CODE (SUBREG_REG (op0)) == PLUS
10871 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
10873 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10874 rtx a = XEXP (SUBREG_REG (op0), 0);
10875 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10877 if ((c1 > 0
10878 && (unsigned HOST_WIDE_INT) c1
10879 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10880 && (equality_comparison_p || unsigned_comparison_p)
10881 /* (A - C1) zero-extends if it is positive and sign-extends
10882 if it is negative, C2 both zero- and sign-extends. */
10883 && ((0 == (nonzero_bits (a, inner_mode)
10884 & ~GET_MODE_MASK (mode))
10885 && const_op >= 0)
10886 /* (A - C1) sign-extends if it is positive and 1-extends
10887 if it is negative, C2 both sign- and 1-extends. */
10888 || (num_sign_bit_copies (a, inner_mode)
10889 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10890 - mode_width)
10891 && const_op < 0)))
10892 || ((unsigned HOST_WIDE_INT) c1
10893 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10894 /* (A - C1) always sign-extends, like C2. */
10895 && num_sign_bit_copies (a, inner_mode)
10896 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10897 - (mode_width - 1))))
10899 op0 = SUBREG_REG (op0);
10900 continue;
10904 /* If the inner mode is narrower and we are extracting the low part,
10905 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10906 if (subreg_lowpart_p (op0)
10907 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10908 /* Fall through */ ;
10909 else
10910 break;
10912 /* ... fall through ... */
10914 case ZERO_EXTEND:
10915 mode = GET_MODE (XEXP (op0, 0));
10916 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10917 && (unsigned_comparison_p || equality_comparison_p)
10918 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10919 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10920 && have_insn_for (COMPARE, mode))
10922 op0 = XEXP (op0, 0);
10923 continue;
10925 break;
10927 case PLUS:
10928 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10929 this for equality comparisons due to pathological cases involving
10930 overflows. */
10931 if (equality_comparison_p
10932 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10933 op1, XEXP (op0, 1))))
10935 op0 = XEXP (op0, 0);
10936 op1 = tem;
10937 continue;
10940 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10941 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10942 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10944 op0 = XEXP (XEXP (op0, 0), 0);
10945 code = (code == LT ? EQ : NE);
10946 continue;
10948 break;
10950 case MINUS:
10951 /* We used to optimize signed comparisons against zero, but that
10952 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10953 arrive here as equality comparisons, or (GEU, LTU) are
10954 optimized away. No need to special-case them. */
10956 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10957 (eq B (minus A C)), whichever simplifies. We can only do
10958 this for equality comparisons due to pathological cases involving
10959 overflows. */
10960 if (equality_comparison_p
10961 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10962 XEXP (op0, 1), op1)))
10964 op0 = XEXP (op0, 0);
10965 op1 = tem;
10966 continue;
10969 if (equality_comparison_p
10970 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10971 XEXP (op0, 0), op1)))
10973 op0 = XEXP (op0, 1);
10974 op1 = tem;
10975 continue;
10978 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10979 of bits in X minus 1, is one iff X > 0. */
10980 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10981 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
10982 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10983 == mode_width - 1
10984 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10986 op0 = XEXP (op0, 1);
10987 code = (code == GE ? LE : GT);
10988 continue;
10990 break;
10992 case XOR:
10993 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10994 if C is zero or B is a constant. */
10995 if (equality_comparison_p
10996 && 0 != (tem = simplify_binary_operation (XOR, mode,
10997 XEXP (op0, 1), op1)))
10999 op0 = XEXP (op0, 0);
11000 op1 = tem;
11001 continue;
11003 break;
11005 case EQ: case NE:
11006 case UNEQ: case LTGT:
11007 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11008 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11009 case UNORDERED: case ORDERED:
11010 /* We can't do anything if OP0 is a condition code value, rather
11011 than an actual data value. */
11012 if (const_op != 0
11013 || CC0_P (XEXP (op0, 0))
11014 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11015 break;
11017 /* Get the two operands being compared. */
11018 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11019 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11020 else
11021 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11023 /* Check for the cases where we simply want the result of the
11024 earlier test or the opposite of that result. */
11025 if (code == NE || code == EQ
11026 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11027 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11028 && (STORE_FLAG_VALUE
11029 & (((HOST_WIDE_INT) 1
11030 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11031 && (code == LT || code == GE)))
11033 enum rtx_code new_code;
11034 if (code == LT || code == NE)
11035 new_code = GET_CODE (op0);
11036 else
11037 new_code = reversed_comparison_code (op0, NULL);
11039 if (new_code != UNKNOWN)
11041 code = new_code;
11042 op0 = tem;
11043 op1 = tem1;
11044 continue;
11047 break;
11049 case IOR:
11050 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11051 iff X <= 0. */
11052 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11053 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11054 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11056 op0 = XEXP (op0, 1);
11057 code = (code == GE ? GT : LE);
11058 continue;
11060 break;
11062 case AND:
11063 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11064 will be converted to a ZERO_EXTRACT later. */
11065 if (const_op == 0 && equality_comparison_p
11066 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11067 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11069 op0 = simplify_and_const_int
11070 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
11071 XEXP (op0, 1),
11072 XEXP (XEXP (op0, 0), 1)),
11073 (HOST_WIDE_INT) 1);
11074 continue;
11077 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11078 zero and X is a comparison and C1 and C2 describe only bits set
11079 in STORE_FLAG_VALUE, we can compare with X. */
11080 if (const_op == 0 && equality_comparison_p
11081 && mode_width <= HOST_BITS_PER_WIDE_INT
11082 && CONST_INT_P (XEXP (op0, 1))
11083 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11084 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11085 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11086 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11088 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11089 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11090 if ((~STORE_FLAG_VALUE & mask) == 0
11091 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11092 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11093 && COMPARISON_P (tem))))
11095 op0 = XEXP (XEXP (op0, 0), 0);
11096 continue;
11100 /* If we are doing an equality comparison of an AND of a bit equal
11101 to the sign bit, replace this with a LT or GE comparison of
11102 the underlying value. */
11103 if (equality_comparison_p
11104 && const_op == 0
11105 && CONST_INT_P (XEXP (op0, 1))
11106 && mode_width <= HOST_BITS_PER_WIDE_INT
11107 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11108 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11110 op0 = XEXP (op0, 0);
11111 code = (code == EQ ? GE : LT);
11112 continue;
11115 /* If this AND operation is really a ZERO_EXTEND from a narrower
11116 mode, the constant fits within that mode, and this is either an
11117 equality or unsigned comparison, try to do this comparison in
11118 the narrower mode.
11120 Note that in:
11122 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11123 -> (ne:DI (reg:SI 4) (const_int 0))
11125 unless TRULY_NOOP_TRUNCATION allows it or the register is
11126 known to hold a value of the required mode the
11127 transformation is invalid. */
11128 if ((equality_comparison_p || unsigned_comparison_p)
11129 && CONST_INT_P (XEXP (op0, 1))
11130 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
11131 & GET_MODE_MASK (mode))
11132 + 1)) >= 0
11133 && const_op >> i == 0
11134 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11135 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
11136 GET_MODE_BITSIZE (GET_MODE (op0)))
11137 || (REG_P (XEXP (op0, 0))
11138 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11140 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11141 continue;
11144 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11145 fits in both M1 and M2 and the SUBREG is either paradoxical
11146 or represents the low part, permute the SUBREG and the AND
11147 and try again. */
11148 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11150 unsigned HOST_WIDE_INT c1;
11151 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11152 /* Require an integral mode, to avoid creating something like
11153 (AND:SF ...). */
11154 if (SCALAR_INT_MODE_P (tmode)
11155 /* It is unsafe to commute the AND into the SUBREG if the
11156 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11157 not defined. As originally written the upper bits
11158 have a defined value due to the AND operation.
11159 However, if we commute the AND inside the SUBREG then
11160 they no longer have defined values and the meaning of
11161 the code has been changed. */
11162 && (0
11163 #ifdef WORD_REGISTER_OPERATIONS
11164 || (mode_width > GET_MODE_BITSIZE (tmode)
11165 && mode_width <= BITS_PER_WORD)
11166 #endif
11167 || (mode_width <= GET_MODE_BITSIZE (tmode)
11168 && subreg_lowpart_p (XEXP (op0, 0))))
11169 && CONST_INT_P (XEXP (op0, 1))
11170 && mode_width <= HOST_BITS_PER_WIDE_INT
11171 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11172 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11173 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11174 && c1 != mask
11175 && c1 != GET_MODE_MASK (tmode))
11177 op0 = simplify_gen_binary (AND, tmode,
11178 SUBREG_REG (XEXP (op0, 0)),
11179 gen_int_mode (c1, tmode));
11180 op0 = gen_lowpart (mode, op0);
11181 continue;
11185 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11186 if (const_op == 0 && equality_comparison_p
11187 && XEXP (op0, 1) == const1_rtx
11188 && GET_CODE (XEXP (op0, 0)) == NOT)
11190 op0 = simplify_and_const_int
11191 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11192 code = (code == NE ? EQ : NE);
11193 continue;
11196 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11197 (eq (and (lshiftrt X) 1) 0).
11198 Also handle the case where (not X) is expressed using xor. */
11199 if (const_op == 0 && equality_comparison_p
11200 && XEXP (op0, 1) == const1_rtx
11201 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11203 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11204 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11206 if (GET_CODE (shift_op) == NOT
11207 || (GET_CODE (shift_op) == XOR
11208 && CONST_INT_P (XEXP (shift_op, 1))
11209 && CONST_INT_P (shift_count)
11210 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11211 && (INTVAL (XEXP (shift_op, 1))
11212 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
11214 op0 = simplify_and_const_int
11215 (NULL_RTX, mode,
11216 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
11217 (HOST_WIDE_INT) 1);
11218 code = (code == NE ? EQ : NE);
11219 continue;
11222 break;
11224 case ASHIFT:
11225 /* If we have (compare (ashift FOO N) (const_int C)) and
11226 the high order N bits of FOO (N+1 if an inequality comparison)
11227 are known to be zero, we can do this by comparing FOO with C
11228 shifted right N bits so long as the low-order N bits of C are
11229 zero. */
11230 if (CONST_INT_P (XEXP (op0, 1))
11231 && INTVAL (XEXP (op0, 1)) >= 0
11232 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11233 < HOST_BITS_PER_WIDE_INT)
11234 && ((const_op
11235 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11236 && mode_width <= HOST_BITS_PER_WIDE_INT
11237 && (nonzero_bits (XEXP (op0, 0), mode)
11238 & ~(mask >> (INTVAL (XEXP (op0, 1))
11239 + ! equality_comparison_p))) == 0)
11241 /* We must perform a logical shift, not an arithmetic one,
11242 as we want the top N bits of C to be zero. */
11243 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11245 temp >>= INTVAL (XEXP (op0, 1));
11246 op1 = gen_int_mode (temp, mode);
11247 op0 = XEXP (op0, 0);
11248 continue;
11251 /* If we are doing a sign bit comparison, it means we are testing
11252 a particular bit. Convert it to the appropriate AND. */
11253 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11254 && mode_width <= HOST_BITS_PER_WIDE_INT)
11256 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11257 ((HOST_WIDE_INT) 1
11258 << (mode_width - 1
11259 - INTVAL (XEXP (op0, 1)))));
11260 code = (code == LT ? NE : EQ);
11261 continue;
11264 /* If this an equality comparison with zero and we are shifting
11265 the low bit to the sign bit, we can convert this to an AND of the
11266 low-order bit. */
11267 if (const_op == 0 && equality_comparison_p
11268 && CONST_INT_P (XEXP (op0, 1))
11269 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11270 == mode_width - 1)
11272 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11273 (HOST_WIDE_INT) 1);
11274 continue;
11276 break;
11278 case ASHIFTRT:
11279 /* If this is an equality comparison with zero, we can do this
11280 as a logical shift, which might be much simpler. */
11281 if (equality_comparison_p && const_op == 0
11282 && CONST_INT_P (XEXP (op0, 1)))
11284 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11285 XEXP (op0, 0),
11286 INTVAL (XEXP (op0, 1)));
11287 continue;
11290 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11291 do the comparison in a narrower mode. */
11292 if (! unsigned_comparison_p
11293 && CONST_INT_P (XEXP (op0, 1))
11294 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11295 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11296 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11297 MODE_INT, 1)) != BLKmode
11298 && (((unsigned HOST_WIDE_INT) const_op
11299 + (GET_MODE_MASK (tmode) >> 1) + 1)
11300 <= GET_MODE_MASK (tmode)))
11302 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11303 continue;
11306 /* Likewise if OP0 is a PLUS of a sign extension with a
11307 constant, which is usually represented with the PLUS
11308 between the shifts. */
11309 if (! unsigned_comparison_p
11310 && CONST_INT_P (XEXP (op0, 1))
11311 && GET_CODE (XEXP (op0, 0)) == PLUS
11312 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11313 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11314 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11315 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11316 MODE_INT, 1)) != BLKmode
11317 && (((unsigned HOST_WIDE_INT) const_op
11318 + (GET_MODE_MASK (tmode) >> 1) + 1)
11319 <= GET_MODE_MASK (tmode)))
11321 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11322 rtx add_const = XEXP (XEXP (op0, 0), 1);
11323 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11324 add_const, XEXP (op0, 1));
11326 op0 = simplify_gen_binary (PLUS, tmode,
11327 gen_lowpart (tmode, inner),
11328 new_const);
11329 continue;
11332 /* ... fall through ... */
11333 case LSHIFTRT:
11334 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11335 the low order N bits of FOO are known to be zero, we can do this
11336 by comparing FOO with C shifted left N bits so long as no
11337 overflow occurs. */
11338 if (CONST_INT_P (XEXP (op0, 1))
11339 && INTVAL (XEXP (op0, 1)) >= 0
11340 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11341 && mode_width <= HOST_BITS_PER_WIDE_INT
11342 && (nonzero_bits (XEXP (op0, 0), mode)
11343 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11344 && (((unsigned HOST_WIDE_INT) const_op
11345 + (GET_CODE (op0) != LSHIFTRT
11346 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11347 + 1)
11348 : 0))
11349 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11351 /* If the shift was logical, then we must make the condition
11352 unsigned. */
11353 if (GET_CODE (op0) == LSHIFTRT)
11354 code = unsigned_condition (code);
11356 const_op <<= INTVAL (XEXP (op0, 1));
11357 op1 = GEN_INT (const_op);
11358 op0 = XEXP (op0, 0);
11359 continue;
11362 /* If we are using this shift to extract just the sign bit, we
11363 can replace this with an LT or GE comparison. */
11364 if (const_op == 0
11365 && (equality_comparison_p || sign_bit_comparison_p)
11366 && CONST_INT_P (XEXP (op0, 1))
11367 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11368 == mode_width - 1)
11370 op0 = XEXP (op0, 0);
11371 code = (code == NE || code == GT ? LT : GE);
11372 continue;
11374 break;
11376 default:
11377 break;
11380 break;
11383 /* Now make any compound operations involved in this comparison. Then,
11384 check for an outmost SUBREG on OP0 that is not doing anything or is
11385 paradoxical. The latter transformation must only be performed when
11386 it is known that the "extra" bits will be the same in op0 and op1 or
11387 that they don't matter. There are three cases to consider:
11389 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11390 care bits and we can assume they have any convenient value. So
11391 making the transformation is safe.
11393 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11394 In this case the upper bits of op0 are undefined. We should not make
11395 the simplification in that case as we do not know the contents of
11396 those bits.
11398 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11399 UNKNOWN. In that case we know those bits are zeros or ones. We must
11400 also be sure that they are the same as the upper bits of op1.
11402 We can never remove a SUBREG for a non-equality comparison because
11403 the sign bit is in a different place in the underlying object. */
11405 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11406 op1 = make_compound_operation (op1, SET);
11408 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11409 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11410 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11411 && (code == NE || code == EQ))
11413 if (GET_MODE_SIZE (GET_MODE (op0))
11414 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11416 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11417 implemented. */
11418 if (REG_P (SUBREG_REG (op0)))
11420 op0 = SUBREG_REG (op0);
11421 op1 = gen_lowpart (GET_MODE (op0), op1);
11424 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11425 <= HOST_BITS_PER_WIDE_INT)
11426 && (nonzero_bits (SUBREG_REG (op0),
11427 GET_MODE (SUBREG_REG (op0)))
11428 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11430 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11432 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11433 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11434 op0 = SUBREG_REG (op0), op1 = tem;
11438 /* We now do the opposite procedure: Some machines don't have compare
11439 insns in all modes. If OP0's mode is an integer mode smaller than a
11440 word and we can't do a compare in that mode, see if there is a larger
11441 mode for which we can do the compare. There are a number of cases in
11442 which we can use the wider mode. */
11444 mode = GET_MODE (op0);
11445 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11446 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11447 && ! have_insn_for (COMPARE, mode))
11448 for (tmode = GET_MODE_WIDER_MODE (mode);
11449 (tmode != VOIDmode
11450 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11451 tmode = GET_MODE_WIDER_MODE (tmode))
11452 if (have_insn_for (COMPARE, tmode))
11454 int zero_extended;
11456 /* If this is a test for negative, we can make an explicit
11457 test of the sign bit. Test this first so we can use
11458 a paradoxical subreg to extend OP0. */
11460 if (op1 == const0_rtx && (code == LT || code == GE)
11461 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11463 op0 = simplify_gen_binary (AND, tmode,
11464 gen_lowpart (tmode, op0),
11465 GEN_INT ((HOST_WIDE_INT) 1
11466 << (GET_MODE_BITSIZE (mode)
11467 - 1)));
11468 code = (code == LT) ? NE : EQ;
11469 break;
11472 /* If the only nonzero bits in OP0 and OP1 are those in the
11473 narrower mode and this is an equality or unsigned comparison,
11474 we can use the wider mode. Similarly for sign-extended
11475 values, in which case it is true for all comparisons. */
11476 zero_extended = ((code == EQ || code == NE
11477 || code == GEU || code == GTU
11478 || code == LEU || code == LTU)
11479 && (nonzero_bits (op0, tmode)
11480 & ~GET_MODE_MASK (mode)) == 0
11481 && ((CONST_INT_P (op1)
11482 || (nonzero_bits (op1, tmode)
11483 & ~GET_MODE_MASK (mode)) == 0)));
11485 if (zero_extended
11486 || ((num_sign_bit_copies (op0, tmode)
11487 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11488 - GET_MODE_BITSIZE (mode)))
11489 && (num_sign_bit_copies (op1, tmode)
11490 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11491 - GET_MODE_BITSIZE (mode)))))
11493 /* If OP0 is an AND and we don't have an AND in MODE either,
11494 make a new AND in the proper mode. */
11495 if (GET_CODE (op0) == AND
11496 && !have_insn_for (AND, mode))
11497 op0 = simplify_gen_binary (AND, tmode,
11498 gen_lowpart (tmode,
11499 XEXP (op0, 0)),
11500 gen_lowpart (tmode,
11501 XEXP (op0, 1)));
11502 else
11504 if (zero_extended)
11506 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
11507 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
11509 else
11511 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
11512 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
11514 break;
11519 #ifdef CANONICALIZE_COMPARISON
11520 /* If this machine only supports a subset of valid comparisons, see if we
11521 can convert an unsupported one into a supported one. */
11522 CANONICALIZE_COMPARISON (code, op0, op1);
11523 #endif
11525 *pop0 = op0;
11526 *pop1 = op1;
11528 return code;
11531 /* Utility function for record_value_for_reg. Count number of
11532 rtxs in X. */
11533 static int
11534 count_rtxs (rtx x)
11536 enum rtx_code code = GET_CODE (x);
11537 const char *fmt;
11538 int i, j, ret = 1;
11540 if (GET_RTX_CLASS (code) == '2'
11541 || GET_RTX_CLASS (code) == 'c')
11543 rtx x0 = XEXP (x, 0);
11544 rtx x1 = XEXP (x, 1);
11546 if (x0 == x1)
11547 return 1 + 2 * count_rtxs (x0);
11549 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11550 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11551 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11552 return 2 + 2 * count_rtxs (x0)
11553 + count_rtxs (x == XEXP (x1, 0)
11554 ? XEXP (x1, 1) : XEXP (x1, 0));
11556 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11557 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11558 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11559 return 2 + 2 * count_rtxs (x1)
11560 + count_rtxs (x == XEXP (x0, 0)
11561 ? XEXP (x0, 1) : XEXP (x0, 0));
11564 fmt = GET_RTX_FORMAT (code);
11565 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11566 if (fmt[i] == 'e')
11567 ret += count_rtxs (XEXP (x, i));
11568 else if (fmt[i] == 'E')
11569 for (j = 0; j < XVECLEN (x, i); j++)
11570 ret += count_rtxs (XVECEXP (x, i, j));
11572 return ret;
11575 /* Utility function for following routine. Called when X is part of a value
11576 being stored into last_set_value. Sets last_set_table_tick
11577 for each register mentioned. Similar to mention_regs in cse.c */
11579 static void
11580 update_table_tick (rtx x)
11582 enum rtx_code code = GET_CODE (x);
11583 const char *fmt = GET_RTX_FORMAT (code);
11584 int i, j;
11586 if (code == REG)
11588 unsigned int regno = REGNO (x);
11589 unsigned int endregno = END_REGNO (x);
11590 unsigned int r;
11592 for (r = regno; r < endregno; r++)
11594 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11595 rsp->last_set_table_tick = label_tick;
11598 return;
11601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11602 if (fmt[i] == 'e')
11604 /* Check for identical subexpressions. If x contains
11605 identical subexpression we only have to traverse one of
11606 them. */
11607 if (i == 0 && ARITHMETIC_P (x))
11609 /* Note that at this point x1 has already been
11610 processed. */
11611 rtx x0 = XEXP (x, 0);
11612 rtx x1 = XEXP (x, 1);
11614 /* If x0 and x1 are identical then there is no need to
11615 process x0. */
11616 if (x0 == x1)
11617 break;
11619 /* If x0 is identical to a subexpression of x1 then while
11620 processing x1, x0 has already been processed. Thus we
11621 are done with x. */
11622 if (ARITHMETIC_P (x1)
11623 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11624 break;
11626 /* If x1 is identical to a subexpression of x0 then we
11627 still have to process the rest of x0. */
11628 if (ARITHMETIC_P (x0)
11629 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11631 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11632 break;
11636 update_table_tick (XEXP (x, i));
11638 else if (fmt[i] == 'E')
11639 for (j = 0; j < XVECLEN (x, i); j++)
11640 update_table_tick (XVECEXP (x, i, j));
11643 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11644 are saying that the register is clobbered and we no longer know its
11645 value. If INSN is zero, don't update reg_stat[].last_set; this is
11646 only permitted with VALUE also zero and is used to invalidate the
11647 register. */
11649 static void
11650 record_value_for_reg (rtx reg, rtx insn, rtx value)
11652 unsigned int regno = REGNO (reg);
11653 unsigned int endregno = END_REGNO (reg);
11654 unsigned int i;
11655 reg_stat_type *rsp;
11657 /* If VALUE contains REG and we have a previous value for REG, substitute
11658 the previous value. */
11659 if (value && insn && reg_overlap_mentioned_p (reg, value))
11661 rtx tem;
11663 /* Set things up so get_last_value is allowed to see anything set up to
11664 our insn. */
11665 subst_low_luid = DF_INSN_LUID (insn);
11666 tem = get_last_value (reg);
11668 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11669 it isn't going to be useful and will take a lot of time to process,
11670 so just use the CLOBBER. */
11672 if (tem)
11674 if (ARITHMETIC_P (tem)
11675 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11676 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11677 tem = XEXP (tem, 0);
11678 else if (count_occurrences (value, reg, 1) >= 2)
11680 /* If there are two or more occurrences of REG in VALUE,
11681 prevent the value from growing too much. */
11682 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11683 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11686 value = replace_rtx (copy_rtx (value), reg, tem);
11690 /* For each register modified, show we don't know its value, that
11691 we don't know about its bitwise content, that its value has been
11692 updated, and that we don't know the location of the death of the
11693 register. */
11694 for (i = regno; i < endregno; i++)
11696 rsp = VEC_index (reg_stat_type, reg_stat, i);
11698 if (insn)
11699 rsp->last_set = insn;
11701 rsp->last_set_value = 0;
11702 rsp->last_set_mode = VOIDmode;
11703 rsp->last_set_nonzero_bits = 0;
11704 rsp->last_set_sign_bit_copies = 0;
11705 rsp->last_death = 0;
11706 rsp->truncated_to_mode = VOIDmode;
11709 /* Mark registers that are being referenced in this value. */
11710 if (value)
11711 update_table_tick (value);
11713 /* Now update the status of each register being set.
11714 If someone is using this register in this block, set this register
11715 to invalid since we will get confused between the two lives in this
11716 basic block. This makes using this register always invalid. In cse, we
11717 scan the table to invalidate all entries using this register, but this
11718 is too much work for us. */
11720 for (i = regno; i < endregno; i++)
11722 rsp = VEC_index (reg_stat_type, reg_stat, i);
11723 rsp->last_set_label = label_tick;
11724 if (!insn
11725 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11726 rsp->last_set_invalid = 1;
11727 else
11728 rsp->last_set_invalid = 0;
11731 /* The value being assigned might refer to X (like in "x++;"). In that
11732 case, we must replace it with (clobber (const_int 0)) to prevent
11733 infinite loops. */
11734 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11735 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
11737 value = copy_rtx (value);
11738 if (!get_last_value_validate (&value, insn, label_tick, 1))
11739 value = 0;
11742 /* For the main register being modified, update the value, the mode, the
11743 nonzero bits, and the number of sign bit copies. */
11745 rsp->last_set_value = value;
11747 if (value)
11749 enum machine_mode mode = GET_MODE (reg);
11750 subst_low_luid = DF_INSN_LUID (insn);
11751 rsp->last_set_mode = mode;
11752 if (GET_MODE_CLASS (mode) == MODE_INT
11753 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11754 mode = nonzero_bits_mode;
11755 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11756 rsp->last_set_sign_bit_copies
11757 = num_sign_bit_copies (value, GET_MODE (reg));
11761 /* Called via note_stores from record_dead_and_set_regs to handle one
11762 SET or CLOBBER in an insn. DATA is the instruction in which the
11763 set is occurring. */
11765 static void
11766 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11768 rtx record_dead_insn = (rtx) data;
11770 if (GET_CODE (dest) == SUBREG)
11771 dest = SUBREG_REG (dest);
11773 if (!record_dead_insn)
11775 if (REG_P (dest))
11776 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11777 return;
11780 if (REG_P (dest))
11782 /* If we are setting the whole register, we know its value. Otherwise
11783 show that we don't know the value. We can handle SUBREG in
11784 some cases. */
11785 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11786 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11787 else if (GET_CODE (setter) == SET
11788 && GET_CODE (SET_DEST (setter)) == SUBREG
11789 && SUBREG_REG (SET_DEST (setter)) == dest
11790 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11791 && subreg_lowpart_p (SET_DEST (setter)))
11792 record_value_for_reg (dest, record_dead_insn,
11793 gen_lowpart (GET_MODE (dest),
11794 SET_SRC (setter)));
11795 else
11796 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11798 else if (MEM_P (dest)
11799 /* Ignore pushes, they clobber nothing. */
11800 && ! push_operand (dest, GET_MODE (dest)))
11801 mem_last_set = DF_INSN_LUID (record_dead_insn);
11804 /* Update the records of when each REG was most recently set or killed
11805 for the things done by INSN. This is the last thing done in processing
11806 INSN in the combiner loop.
11808 We update reg_stat[], in particular fields last_set, last_set_value,
11809 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11810 last_death, and also the similar information mem_last_set (which insn
11811 most recently modified memory) and last_call_luid (which insn was the
11812 most recent subroutine call). */
11814 static void
11815 record_dead_and_set_regs (rtx insn)
11817 rtx link;
11818 unsigned int i;
11820 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11822 if (REG_NOTE_KIND (link) == REG_DEAD
11823 && REG_P (XEXP (link, 0)))
11825 unsigned int regno = REGNO (XEXP (link, 0));
11826 unsigned int endregno = END_REGNO (XEXP (link, 0));
11828 for (i = regno; i < endregno; i++)
11830 reg_stat_type *rsp;
11832 rsp = VEC_index (reg_stat_type, reg_stat, i);
11833 rsp->last_death = insn;
11836 else if (REG_NOTE_KIND (link) == REG_INC)
11837 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11840 if (CALL_P (insn))
11842 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11843 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11845 reg_stat_type *rsp;
11847 rsp = VEC_index (reg_stat_type, reg_stat, i);
11848 rsp->last_set_invalid = 1;
11849 rsp->last_set = insn;
11850 rsp->last_set_value = 0;
11851 rsp->last_set_mode = VOIDmode;
11852 rsp->last_set_nonzero_bits = 0;
11853 rsp->last_set_sign_bit_copies = 0;
11854 rsp->last_death = 0;
11855 rsp->truncated_to_mode = VOIDmode;
11858 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11860 /* We can't combine into a call pattern. Remember, though, that
11861 the return value register is set at this LUID. We could
11862 still replace a register with the return value from the
11863 wrong subroutine call! */
11864 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11866 else
11867 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11870 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11871 register present in the SUBREG, so for each such SUBREG go back and
11872 adjust nonzero and sign bit information of the registers that are
11873 known to have some zero/sign bits set.
11875 This is needed because when combine blows the SUBREGs away, the
11876 information on zero/sign bits is lost and further combines can be
11877 missed because of that. */
11879 static void
11880 record_promoted_value (rtx insn, rtx subreg)
11882 rtx links, set;
11883 unsigned int regno = REGNO (SUBREG_REG (subreg));
11884 enum machine_mode mode = GET_MODE (subreg);
11886 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11887 return;
11889 for (links = LOG_LINKS (insn); links;)
11891 reg_stat_type *rsp;
11893 insn = XEXP (links, 0);
11894 set = single_set (insn);
11896 if (! set || !REG_P (SET_DEST (set))
11897 || REGNO (SET_DEST (set)) != regno
11898 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11900 links = XEXP (links, 1);
11901 continue;
11904 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11905 if (rsp->last_set == insn)
11907 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11908 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11911 if (REG_P (SET_SRC (set)))
11913 regno = REGNO (SET_SRC (set));
11914 links = LOG_LINKS (insn);
11916 else
11917 break;
11921 /* Check if X, a register, is known to contain a value already
11922 truncated to MODE. In this case we can use a subreg to refer to
11923 the truncated value even though in the generic case we would need
11924 an explicit truncation. */
11926 static bool
11927 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11929 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11930 enum machine_mode truncated = rsp->truncated_to_mode;
11932 if (truncated == 0
11933 || rsp->truncation_label < label_tick_ebb_start)
11934 return false;
11935 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11936 return true;
11937 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11938 GET_MODE_BITSIZE (truncated)))
11939 return true;
11940 return false;
11943 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
11944 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
11945 might be able to turn a truncate into a subreg using this information.
11946 Return -1 if traversing *P is complete or 0 otherwise. */
11948 static int
11949 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
11951 rtx x = *p;
11952 enum machine_mode truncated_mode;
11953 reg_stat_type *rsp;
11955 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11957 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11958 truncated_mode = GET_MODE (x);
11960 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11961 return -1;
11963 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11964 GET_MODE_BITSIZE (original_mode)))
11965 return -1;
11967 x = SUBREG_REG (x);
11969 /* ??? For hard-regs we now record everything. We might be able to
11970 optimize this using last_set_mode. */
11971 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11972 truncated_mode = GET_MODE (x);
11973 else
11974 return 0;
11976 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11977 if (rsp->truncated_to_mode == 0
11978 || rsp->truncation_label < label_tick_ebb_start
11979 || (GET_MODE_SIZE (truncated_mode)
11980 < GET_MODE_SIZE (rsp->truncated_to_mode)))
11982 rsp->truncated_to_mode = truncated_mode;
11983 rsp->truncation_label = label_tick;
11986 return -1;
11989 /* Callback for note_uses. Find hardregs and subregs of pseudos and
11990 the modes they are used in. This can help truning TRUNCATEs into
11991 SUBREGs. */
11993 static void
11994 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
11996 for_each_rtx (x, record_truncated_value, NULL);
11999 /* Scan X for promoted SUBREGs. For each one found,
12000 note what it implies to the registers used in it. */
12002 static void
12003 check_promoted_subreg (rtx insn, rtx x)
12005 if (GET_CODE (x) == SUBREG
12006 && SUBREG_PROMOTED_VAR_P (x)
12007 && REG_P (SUBREG_REG (x)))
12008 record_promoted_value (insn, x);
12009 else
12011 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12012 int i, j;
12014 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12015 switch (format[i])
12017 case 'e':
12018 check_promoted_subreg (insn, XEXP (x, i));
12019 break;
12020 case 'V':
12021 case 'E':
12022 if (XVEC (x, i) != 0)
12023 for (j = 0; j < XVECLEN (x, i); j++)
12024 check_promoted_subreg (insn, XVECEXP (x, i, j));
12025 break;
12030 /* Verify that all the registers and memory references mentioned in *LOC are
12031 still valid. *LOC was part of a value set in INSN when label_tick was
12032 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12033 the invalid references with (clobber (const_int 0)) and return 1. This
12034 replacement is useful because we often can get useful information about
12035 the form of a value (e.g., if it was produced by a shift that always
12036 produces -1 or 0) even though we don't know exactly what registers it
12037 was produced from. */
12039 static int
12040 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
12042 rtx x = *loc;
12043 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12044 int len = GET_RTX_LENGTH (GET_CODE (x));
12045 int i, j;
12047 if (REG_P (x))
12049 unsigned int regno = REGNO (x);
12050 unsigned int endregno = END_REGNO (x);
12051 unsigned int j;
12053 for (j = regno; j < endregno; j++)
12055 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
12056 if (rsp->last_set_invalid
12057 /* If this is a pseudo-register that was only set once and not
12058 live at the beginning of the function, it is always valid. */
12059 || (! (regno >= FIRST_PSEUDO_REGISTER
12060 && REG_N_SETS (regno) == 1
12061 && (!REGNO_REG_SET_P
12062 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno)))
12063 && rsp->last_set_label > tick))
12065 if (replace)
12066 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12067 return replace;
12071 return 1;
12073 /* If this is a memory reference, make sure that there were no stores after
12074 it that might have clobbered the value. We don't have alias info, so we
12075 assume any store invalidates it. Moreover, we only have local UIDs, so
12076 we also assume that there were stores in the intervening basic blocks. */
12077 else if (MEM_P (x) && !MEM_READONLY_P (x)
12078 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12080 if (replace)
12081 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12082 return replace;
12085 for (i = 0; i < len; i++)
12087 if (fmt[i] == 'e')
12089 /* Check for identical subexpressions. If x contains
12090 identical subexpression we only have to traverse one of
12091 them. */
12092 if (i == 1 && ARITHMETIC_P (x))
12094 /* Note that at this point x0 has already been checked
12095 and found valid. */
12096 rtx x0 = XEXP (x, 0);
12097 rtx x1 = XEXP (x, 1);
12099 /* If x0 and x1 are identical then x is also valid. */
12100 if (x0 == x1)
12101 return 1;
12103 /* If x1 is identical to a subexpression of x0 then
12104 while checking x0, x1 has already been checked. Thus
12105 it is valid and so as x. */
12106 if (ARITHMETIC_P (x0)
12107 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12108 return 1;
12110 /* If x0 is identical to a subexpression of x1 then x is
12111 valid iff the rest of x1 is valid. */
12112 if (ARITHMETIC_P (x1)
12113 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12114 return
12115 get_last_value_validate (&XEXP (x1,
12116 x0 == XEXP (x1, 0) ? 1 : 0),
12117 insn, tick, replace);
12120 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12121 replace) == 0)
12122 return 0;
12124 else if (fmt[i] == 'E')
12125 for (j = 0; j < XVECLEN (x, i); j++)
12126 if (get_last_value_validate (&XVECEXP (x, i, j),
12127 insn, tick, replace) == 0)
12128 return 0;
12131 /* If we haven't found a reason for it to be invalid, it is valid. */
12132 return 1;
12135 /* Get the last value assigned to X, if known. Some registers
12136 in the value may be replaced with (clobber (const_int 0)) if their value
12137 is known longer known reliably. */
12139 static rtx
12140 get_last_value (const_rtx x)
12142 unsigned int regno;
12143 rtx value;
12144 reg_stat_type *rsp;
12146 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12147 then convert it to the desired mode. If this is a paradoxical SUBREG,
12148 we cannot predict what values the "extra" bits might have. */
12149 if (GET_CODE (x) == SUBREG
12150 && subreg_lowpart_p (x)
12151 && (GET_MODE_SIZE (GET_MODE (x))
12152 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
12153 && (value = get_last_value (SUBREG_REG (x))) != 0)
12154 return gen_lowpart (GET_MODE (x), value);
12156 if (!REG_P (x))
12157 return 0;
12159 regno = REGNO (x);
12160 rsp = VEC_index (reg_stat_type, reg_stat, regno);
12161 value = rsp->last_set_value;
12163 /* If we don't have a value, or if it isn't for this basic block and
12164 it's either a hard register, set more than once, or it's a live
12165 at the beginning of the function, return 0.
12167 Because if it's not live at the beginning of the function then the reg
12168 is always set before being used (is never used without being set).
12169 And, if it's set only once, and it's always set before use, then all
12170 uses must have the same last value, even if it's not from this basic
12171 block. */
12173 if (value == 0
12174 || (rsp->last_set_label < label_tick_ebb_start
12175 && (regno < FIRST_PSEUDO_REGISTER
12176 || REG_N_SETS (regno) != 1
12177 || REGNO_REG_SET_P
12178 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
12179 return 0;
12181 /* If the value was set in a later insn than the ones we are processing,
12182 we can't use it even if the register was only set once. */
12183 if (rsp->last_set_label == label_tick
12184 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12185 return 0;
12187 /* If the value has all its registers valid, return it. */
12188 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12189 return value;
12191 /* Otherwise, make a copy and replace any invalid register with
12192 (clobber (const_int 0)). If that fails for some reason, return 0. */
12194 value = copy_rtx (value);
12195 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12196 return value;
12198 return 0;
12201 /* Return nonzero if expression X refers to a REG or to memory
12202 that is set in an instruction more recent than FROM_LUID. */
12204 static int
12205 use_crosses_set_p (const_rtx x, int from_luid)
12207 const char *fmt;
12208 int i;
12209 enum rtx_code code = GET_CODE (x);
12211 if (code == REG)
12213 unsigned int regno = REGNO (x);
12214 unsigned endreg = END_REGNO (x);
12216 #ifdef PUSH_ROUNDING
12217 /* Don't allow uses of the stack pointer to be moved,
12218 because we don't know whether the move crosses a push insn. */
12219 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12220 return 1;
12221 #endif
12222 for (; regno < endreg; regno++)
12224 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12225 if (rsp->last_set
12226 && rsp->last_set_label == label_tick
12227 && DF_INSN_LUID (rsp->last_set) > from_luid)
12228 return 1;
12230 return 0;
12233 if (code == MEM && mem_last_set > from_luid)
12234 return 1;
12236 fmt = GET_RTX_FORMAT (code);
12238 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12240 if (fmt[i] == 'E')
12242 int j;
12243 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12244 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12245 return 1;
12247 else if (fmt[i] == 'e'
12248 && use_crosses_set_p (XEXP (x, i), from_luid))
12249 return 1;
12251 return 0;
12254 /* Define three variables used for communication between the following
12255 routines. */
12257 static unsigned int reg_dead_regno, reg_dead_endregno;
12258 static int reg_dead_flag;
12260 /* Function called via note_stores from reg_dead_at_p.
12262 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12263 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12265 static void
12266 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12268 unsigned int regno, endregno;
12270 if (!REG_P (dest))
12271 return;
12273 regno = REGNO (dest);
12274 endregno = END_REGNO (dest);
12275 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12276 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12279 /* Return nonzero if REG is known to be dead at INSN.
12281 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12282 referencing REG, it is dead. If we hit a SET referencing REG, it is
12283 live. Otherwise, see if it is live or dead at the start of the basic
12284 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12285 must be assumed to be always live. */
12287 static int
12288 reg_dead_at_p (rtx reg, rtx insn)
12290 basic_block block;
12291 unsigned int i;
12293 /* Set variables for reg_dead_at_p_1. */
12294 reg_dead_regno = REGNO (reg);
12295 reg_dead_endregno = END_REGNO (reg);
12297 reg_dead_flag = 0;
12299 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12300 we allow the machine description to decide whether use-and-clobber
12301 patterns are OK. */
12302 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12304 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12305 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12306 return 0;
12309 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12310 beginning of basic block. */
12311 block = BLOCK_FOR_INSN (insn);
12312 for (;;)
12314 if (INSN_P (insn))
12316 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12317 if (reg_dead_flag)
12318 return reg_dead_flag == 1 ? 1 : 0;
12320 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12321 return 1;
12324 if (insn == BB_HEAD (block))
12325 break;
12327 insn = PREV_INSN (insn);
12330 /* Look at live-in sets for the basic block that we were in. */
12331 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12332 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12333 return 0;
12335 return 1;
12338 /* Note hard registers in X that are used. */
12340 static void
12341 mark_used_regs_combine (rtx x)
12343 RTX_CODE code = GET_CODE (x);
12344 unsigned int regno;
12345 int i;
12347 switch (code)
12349 case LABEL_REF:
12350 case SYMBOL_REF:
12351 case CONST_INT:
12352 case CONST:
12353 case CONST_DOUBLE:
12354 case CONST_VECTOR:
12355 case PC:
12356 case ADDR_VEC:
12357 case ADDR_DIFF_VEC:
12358 case ASM_INPUT:
12359 #ifdef HAVE_cc0
12360 /* CC0 must die in the insn after it is set, so we don't need to take
12361 special note of it here. */
12362 case CC0:
12363 #endif
12364 return;
12366 case CLOBBER:
12367 /* If we are clobbering a MEM, mark any hard registers inside the
12368 address as used. */
12369 if (MEM_P (XEXP (x, 0)))
12370 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12371 return;
12373 case REG:
12374 regno = REGNO (x);
12375 /* A hard reg in a wide mode may really be multiple registers.
12376 If so, mark all of them just like the first. */
12377 if (regno < FIRST_PSEUDO_REGISTER)
12379 /* None of this applies to the stack, frame or arg pointers. */
12380 if (regno == STACK_POINTER_REGNUM
12381 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12382 || regno == HARD_FRAME_POINTER_REGNUM
12383 #endif
12384 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12385 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12386 #endif
12387 || regno == FRAME_POINTER_REGNUM)
12388 return;
12390 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12392 return;
12394 case SET:
12396 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12397 the address. */
12398 rtx testreg = SET_DEST (x);
12400 while (GET_CODE (testreg) == SUBREG
12401 || GET_CODE (testreg) == ZERO_EXTRACT
12402 || GET_CODE (testreg) == STRICT_LOW_PART)
12403 testreg = XEXP (testreg, 0);
12405 if (MEM_P (testreg))
12406 mark_used_regs_combine (XEXP (testreg, 0));
12408 mark_used_regs_combine (SET_SRC (x));
12410 return;
12412 default:
12413 break;
12416 /* Recursively scan the operands of this expression. */
12419 const char *fmt = GET_RTX_FORMAT (code);
12421 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12423 if (fmt[i] == 'e')
12424 mark_used_regs_combine (XEXP (x, i));
12425 else if (fmt[i] == 'E')
12427 int j;
12429 for (j = 0; j < XVECLEN (x, i); j++)
12430 mark_used_regs_combine (XVECEXP (x, i, j));
12436 /* Remove register number REGNO from the dead registers list of INSN.
12438 Return the note used to record the death, if there was one. */
12441 remove_death (unsigned int regno, rtx insn)
12443 rtx note = find_regno_note (insn, REG_DEAD, regno);
12445 if (note)
12446 remove_note (insn, note);
12448 return note;
12451 /* For each register (hardware or pseudo) used within expression X, if its
12452 death is in an instruction with luid between FROM_LUID (inclusive) and
12453 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12454 list headed by PNOTES.
12456 That said, don't move registers killed by maybe_kill_insn.
12458 This is done when X is being merged by combination into TO_INSN. These
12459 notes will then be distributed as needed. */
12461 static void
12462 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12463 rtx *pnotes)
12465 const char *fmt;
12466 int len, i;
12467 enum rtx_code code = GET_CODE (x);
12469 if (code == REG)
12471 unsigned int regno = REGNO (x);
12472 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12474 /* Don't move the register if it gets killed in between from and to. */
12475 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12476 && ! reg_referenced_p (x, maybe_kill_insn))
12477 return;
12479 if (where_dead
12480 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
12481 && DF_INSN_LUID (where_dead) >= from_luid
12482 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12484 rtx note = remove_death (regno, where_dead);
12486 /* It is possible for the call above to return 0. This can occur
12487 when last_death points to I2 or I1 that we combined with.
12488 In that case make a new note.
12490 We must also check for the case where X is a hard register
12491 and NOTE is a death note for a range of hard registers
12492 including X. In that case, we must put REG_DEAD notes for
12493 the remaining registers in place of NOTE. */
12495 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12496 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12497 > GET_MODE_SIZE (GET_MODE (x))))
12499 unsigned int deadregno = REGNO (XEXP (note, 0));
12500 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12501 unsigned int ourend = END_HARD_REGNO (x);
12502 unsigned int i;
12504 for (i = deadregno; i < deadend; i++)
12505 if (i < regno || i >= ourend)
12506 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12509 /* If we didn't find any note, or if we found a REG_DEAD note that
12510 covers only part of the given reg, and we have a multi-reg hard
12511 register, then to be safe we must check for REG_DEAD notes
12512 for each register other than the first. They could have
12513 their own REG_DEAD notes lying around. */
12514 else if ((note == 0
12515 || (note != 0
12516 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12517 < GET_MODE_SIZE (GET_MODE (x)))))
12518 && regno < FIRST_PSEUDO_REGISTER
12519 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12521 unsigned int ourend = END_HARD_REGNO (x);
12522 unsigned int i, offset;
12523 rtx oldnotes = 0;
12525 if (note)
12526 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12527 else
12528 offset = 1;
12530 for (i = regno + offset; i < ourend; i++)
12531 move_deaths (regno_reg_rtx[i],
12532 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12535 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12537 XEXP (note, 1) = *pnotes;
12538 *pnotes = note;
12540 else
12541 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
12544 return;
12547 else if (GET_CODE (x) == SET)
12549 rtx dest = SET_DEST (x);
12551 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12553 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12554 that accesses one word of a multi-word item, some
12555 piece of everything register in the expression is used by
12556 this insn, so remove any old death. */
12557 /* ??? So why do we test for equality of the sizes? */
12559 if (GET_CODE (dest) == ZERO_EXTRACT
12560 || GET_CODE (dest) == STRICT_LOW_PART
12561 || (GET_CODE (dest) == SUBREG
12562 && (((GET_MODE_SIZE (GET_MODE (dest))
12563 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12564 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12565 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12567 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12568 return;
12571 /* If this is some other SUBREG, we know it replaces the entire
12572 value, so use that as the destination. */
12573 if (GET_CODE (dest) == SUBREG)
12574 dest = SUBREG_REG (dest);
12576 /* If this is a MEM, adjust deaths of anything used in the address.
12577 For a REG (the only other possibility), the entire value is
12578 being replaced so the old value is not used in this insn. */
12580 if (MEM_P (dest))
12581 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12582 to_insn, pnotes);
12583 return;
12586 else if (GET_CODE (x) == CLOBBER)
12587 return;
12589 len = GET_RTX_LENGTH (code);
12590 fmt = GET_RTX_FORMAT (code);
12592 for (i = 0; i < len; i++)
12594 if (fmt[i] == 'E')
12596 int j;
12597 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12598 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12599 to_insn, pnotes);
12601 else if (fmt[i] == 'e')
12602 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12606 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12607 pattern of an insn. X must be a REG. */
12609 static int
12610 reg_bitfield_target_p (rtx x, rtx body)
12612 int i;
12614 if (GET_CODE (body) == SET)
12616 rtx dest = SET_DEST (body);
12617 rtx target;
12618 unsigned int regno, tregno, endregno, endtregno;
12620 if (GET_CODE (dest) == ZERO_EXTRACT)
12621 target = XEXP (dest, 0);
12622 else if (GET_CODE (dest) == STRICT_LOW_PART)
12623 target = SUBREG_REG (XEXP (dest, 0));
12624 else
12625 return 0;
12627 if (GET_CODE (target) == SUBREG)
12628 target = SUBREG_REG (target);
12630 if (!REG_P (target))
12631 return 0;
12633 tregno = REGNO (target), regno = REGNO (x);
12634 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12635 return target == x;
12637 endtregno = end_hard_regno (GET_MODE (target), tregno);
12638 endregno = end_hard_regno (GET_MODE (x), regno);
12640 return endregno > tregno && regno < endtregno;
12643 else if (GET_CODE (body) == PARALLEL)
12644 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12645 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12646 return 1;
12648 return 0;
12651 /* Return the next insn after INSN that is neither a NOTE nor a
12652 DEBUG_INSN. This routine does not look inside SEQUENCEs. */
12654 static rtx
12655 next_nonnote_nondebug_insn (rtx insn)
12657 while (insn)
12659 insn = NEXT_INSN (insn);
12660 if (insn == 0)
12661 break;
12662 if (NOTE_P (insn))
12663 continue;
12664 if (DEBUG_INSN_P (insn))
12665 continue;
12666 break;
12669 return insn;
12674 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12675 as appropriate. I3 and I2 are the insns resulting from the combination
12676 insns including FROM (I2 may be zero).
12678 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12679 not need REG_DEAD notes because they are being substituted for. This
12680 saves searching in the most common cases.
12682 Each note in the list is either ignored or placed on some insns, depending
12683 on the type of note. */
12685 static void
12686 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12687 rtx elim_i1)
12689 rtx note, next_note;
12690 rtx tem;
12692 for (note = notes; note; note = next_note)
12694 rtx place = 0, place2 = 0;
12696 next_note = XEXP (note, 1);
12697 switch (REG_NOTE_KIND (note))
12699 case REG_BR_PROB:
12700 case REG_BR_PRED:
12701 /* Doesn't matter much where we put this, as long as it's somewhere.
12702 It is preferable to keep these notes on branches, which is most
12703 likely to be i3. */
12704 place = i3;
12705 break;
12707 case REG_VALUE_PROFILE:
12708 /* Just get rid of this note, as it is unused later anyway. */
12709 break;
12711 case REG_NON_LOCAL_GOTO:
12712 if (JUMP_P (i3))
12713 place = i3;
12714 else
12716 gcc_assert (i2 && JUMP_P (i2));
12717 place = i2;
12719 break;
12721 case REG_EH_REGION:
12722 /* These notes must remain with the call or trapping instruction. */
12723 if (CALL_P (i3))
12724 place = i3;
12725 else if (i2 && CALL_P (i2))
12726 place = i2;
12727 else
12729 gcc_assert (cfun->can_throw_non_call_exceptions);
12730 if (may_trap_p (i3))
12731 place = i3;
12732 else if (i2 && may_trap_p (i2))
12733 place = i2;
12734 /* ??? Otherwise assume we've combined things such that we
12735 can now prove that the instructions can't trap. Drop the
12736 note in this case. */
12738 break;
12740 case REG_NORETURN:
12741 case REG_SETJMP:
12742 /* These notes must remain with the call. It should not be
12743 possible for both I2 and I3 to be a call. */
12744 if (CALL_P (i3))
12745 place = i3;
12746 else
12748 gcc_assert (i2 && CALL_P (i2));
12749 place = i2;
12751 break;
12753 case REG_UNUSED:
12754 /* Any clobbers for i3 may still exist, and so we must process
12755 REG_UNUSED notes from that insn.
12757 Any clobbers from i2 or i1 can only exist if they were added by
12758 recog_for_combine. In that case, recog_for_combine created the
12759 necessary REG_UNUSED notes. Trying to keep any original
12760 REG_UNUSED notes from these insns can cause incorrect output
12761 if it is for the same register as the original i3 dest.
12762 In that case, we will notice that the register is set in i3,
12763 and then add a REG_UNUSED note for the destination of i3, which
12764 is wrong. However, it is possible to have REG_UNUSED notes from
12765 i2 or i1 for register which were both used and clobbered, so
12766 we keep notes from i2 or i1 if they will turn into REG_DEAD
12767 notes. */
12769 /* If this register is set or clobbered in I3, put the note there
12770 unless there is one already. */
12771 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12773 if (from_insn != i3)
12774 break;
12776 if (! (REG_P (XEXP (note, 0))
12777 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12778 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12779 place = i3;
12781 /* Otherwise, if this register is used by I3, then this register
12782 now dies here, so we must put a REG_DEAD note here unless there
12783 is one already. */
12784 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12785 && ! (REG_P (XEXP (note, 0))
12786 ? find_regno_note (i3, REG_DEAD,
12787 REGNO (XEXP (note, 0)))
12788 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12790 PUT_REG_NOTE_KIND (note, REG_DEAD);
12791 place = i3;
12793 break;
12795 case REG_EQUAL:
12796 case REG_EQUIV:
12797 case REG_NOALIAS:
12798 /* These notes say something about results of an insn. We can
12799 only support them if they used to be on I3 in which case they
12800 remain on I3. Otherwise they are ignored.
12802 If the note refers to an expression that is not a constant, we
12803 must also ignore the note since we cannot tell whether the
12804 equivalence is still true. It might be possible to do
12805 slightly better than this (we only have a problem if I2DEST
12806 or I1DEST is present in the expression), but it doesn't
12807 seem worth the trouble. */
12809 if (from_insn == i3
12810 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12811 place = i3;
12812 break;
12814 case REG_INC:
12815 /* These notes say something about how a register is used. They must
12816 be present on any use of the register in I2 or I3. */
12817 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12818 place = i3;
12820 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12822 if (place)
12823 place2 = i2;
12824 else
12825 place = i2;
12827 break;
12829 case REG_LABEL_TARGET:
12830 case REG_LABEL_OPERAND:
12831 /* This can show up in several ways -- either directly in the
12832 pattern, or hidden off in the constant pool with (or without?)
12833 a REG_EQUAL note. */
12834 /* ??? Ignore the without-reg_equal-note problem for now. */
12835 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12836 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12837 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12838 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12839 place = i3;
12841 if (i2
12842 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12843 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12844 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12845 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12847 if (place)
12848 place2 = i2;
12849 else
12850 place = i2;
12853 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12854 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12855 there. */
12856 if (place && JUMP_P (place)
12857 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12858 && (JUMP_LABEL (place) == NULL
12859 || JUMP_LABEL (place) == XEXP (note, 0)))
12861 rtx label = JUMP_LABEL (place);
12863 if (!label)
12864 JUMP_LABEL (place) = XEXP (note, 0);
12865 else if (LABEL_P (label))
12866 LABEL_NUSES (label)--;
12869 if (place2 && JUMP_P (place2)
12870 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12871 && (JUMP_LABEL (place2) == NULL
12872 || JUMP_LABEL (place2) == XEXP (note, 0)))
12874 rtx label = JUMP_LABEL (place2);
12876 if (!label)
12877 JUMP_LABEL (place2) = XEXP (note, 0);
12878 else if (LABEL_P (label))
12879 LABEL_NUSES (label)--;
12880 place2 = 0;
12882 break;
12884 case REG_NONNEG:
12885 /* This note says something about the value of a register prior
12886 to the execution of an insn. It is too much trouble to see
12887 if the note is still correct in all situations. It is better
12888 to simply delete it. */
12889 break;
12891 case REG_DEAD:
12892 /* If we replaced the right hand side of FROM_INSN with a
12893 REG_EQUAL note, the original use of the dying register
12894 will not have been combined into I3 and I2. In such cases,
12895 FROM_INSN is guaranteed to be the first of the combined
12896 instructions, so we simply need to search back before
12897 FROM_INSN for the previous use or set of this register,
12898 then alter the notes there appropriately.
12900 If the register is used as an input in I3, it dies there.
12901 Similarly for I2, if it is nonzero and adjacent to I3.
12903 If the register is not used as an input in either I3 or I2
12904 and it is not one of the registers we were supposed to eliminate,
12905 there are two possibilities. We might have a non-adjacent I2
12906 or we might have somehow eliminated an additional register
12907 from a computation. For example, we might have had A & B where
12908 we discover that B will always be zero. In this case we will
12909 eliminate the reference to A.
12911 In both cases, we must search to see if we can find a previous
12912 use of A and put the death note there. */
12914 if (from_insn
12915 && from_insn == i2mod
12916 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12917 tem = from_insn;
12918 else
12920 if (from_insn
12921 && CALL_P (from_insn)
12922 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12923 place = from_insn;
12924 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12925 place = i3;
12926 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
12927 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12928 place = i2;
12929 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12930 && !(i2mod
12931 && reg_overlap_mentioned_p (XEXP (note, 0),
12932 i2mod_old_rhs)))
12933 || rtx_equal_p (XEXP (note, 0), elim_i1))
12934 break;
12935 tem = i3;
12938 if (place == 0)
12940 basic_block bb = this_basic_block;
12942 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12944 if (!NONDEBUG_INSN_P (tem))
12946 if (tem == BB_HEAD (bb))
12947 break;
12948 continue;
12951 /* If the register is being set at TEM, see if that is all
12952 TEM is doing. If so, delete TEM. Otherwise, make this
12953 into a REG_UNUSED note instead. Don't delete sets to
12954 global register vars. */
12955 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12956 || !global_regs[REGNO (XEXP (note, 0))])
12957 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12959 rtx set = single_set (tem);
12960 rtx inner_dest = 0;
12961 #ifdef HAVE_cc0
12962 rtx cc0_setter = NULL_RTX;
12963 #endif
12965 if (set != 0)
12966 for (inner_dest = SET_DEST (set);
12967 (GET_CODE (inner_dest) == STRICT_LOW_PART
12968 || GET_CODE (inner_dest) == SUBREG
12969 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12970 inner_dest = XEXP (inner_dest, 0))
12973 /* Verify that it was the set, and not a clobber that
12974 modified the register.
12976 CC0 targets must be careful to maintain setter/user
12977 pairs. If we cannot delete the setter due to side
12978 effects, mark the user with an UNUSED note instead
12979 of deleting it. */
12981 if (set != 0 && ! side_effects_p (SET_SRC (set))
12982 && rtx_equal_p (XEXP (note, 0), inner_dest)
12983 #ifdef HAVE_cc0
12984 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12985 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12986 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12987 #endif
12990 /* Move the notes and links of TEM elsewhere.
12991 This might delete other dead insns recursively.
12992 First set the pattern to something that won't use
12993 any register. */
12994 rtx old_notes = REG_NOTES (tem);
12996 PATTERN (tem) = pc_rtx;
12997 REG_NOTES (tem) = NULL;
12999 distribute_notes (old_notes, tem, tem, NULL_RTX,
13000 NULL_RTX, NULL_RTX);
13001 distribute_links (LOG_LINKS (tem));
13003 SET_INSN_DELETED (tem);
13004 if (tem == i2)
13005 i2 = NULL_RTX;
13007 #ifdef HAVE_cc0
13008 /* Delete the setter too. */
13009 if (cc0_setter)
13011 PATTERN (cc0_setter) = pc_rtx;
13012 old_notes = REG_NOTES (cc0_setter);
13013 REG_NOTES (cc0_setter) = NULL;
13015 distribute_notes (old_notes, cc0_setter,
13016 cc0_setter, NULL_RTX,
13017 NULL_RTX, NULL_RTX);
13018 distribute_links (LOG_LINKS (cc0_setter));
13020 SET_INSN_DELETED (cc0_setter);
13021 if (cc0_setter == i2)
13022 i2 = NULL_RTX;
13024 #endif
13026 else
13028 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13030 /* If there isn't already a REG_UNUSED note, put one
13031 here. Do not place a REG_DEAD note, even if
13032 the register is also used here; that would not
13033 match the algorithm used in lifetime analysis
13034 and can cause the consistency check in the
13035 scheduler to fail. */
13036 if (! find_regno_note (tem, REG_UNUSED,
13037 REGNO (XEXP (note, 0))))
13038 place = tem;
13039 break;
13042 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
13043 || (CALL_P (tem)
13044 && find_reg_fusage (tem, USE, XEXP (note, 0))))
13046 place = tem;
13048 /* If we are doing a 3->2 combination, and we have a
13049 register which formerly died in i3 and was not used
13050 by i2, which now no longer dies in i3 and is used in
13051 i2 but does not die in i2, and place is between i2
13052 and i3, then we may need to move a link from place to
13053 i2. */
13054 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13055 && from_insn
13056 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13057 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13059 rtx links = LOG_LINKS (place);
13060 LOG_LINKS (place) = 0;
13061 distribute_links (links);
13063 break;
13066 if (tem == BB_HEAD (bb))
13067 break;
13072 /* If the register is set or already dead at PLACE, we needn't do
13073 anything with this note if it is still a REG_DEAD note.
13074 We check here if it is set at all, not if is it totally replaced,
13075 which is what `dead_or_set_p' checks, so also check for it being
13076 set partially. */
13078 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13080 unsigned int regno = REGNO (XEXP (note, 0));
13081 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
13083 if (dead_or_set_p (place, XEXP (note, 0))
13084 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13086 /* Unless the register previously died in PLACE, clear
13087 last_death. [I no longer understand why this is
13088 being done.] */
13089 if (rsp->last_death != place)
13090 rsp->last_death = 0;
13091 place = 0;
13093 else
13094 rsp->last_death = place;
13096 /* If this is a death note for a hard reg that is occupying
13097 multiple registers, ensure that we are still using all
13098 parts of the object. If we find a piece of the object
13099 that is unused, we must arrange for an appropriate REG_DEAD
13100 note to be added for it. However, we can't just emit a USE
13101 and tag the note to it, since the register might actually
13102 be dead; so we recourse, and the recursive call then finds
13103 the previous insn that used this register. */
13105 if (place && regno < FIRST_PSEUDO_REGISTER
13106 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13108 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13109 int all_used = 1;
13110 unsigned int i;
13112 for (i = regno; i < endregno; i++)
13113 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13114 && ! find_regno_fusage (place, USE, i))
13115 || dead_or_set_regno_p (place, i))
13116 all_used = 0;
13118 if (! all_used)
13120 /* Put only REG_DEAD notes for pieces that are
13121 not already dead or set. */
13123 for (i = regno; i < endregno;
13124 i += hard_regno_nregs[i][reg_raw_mode[i]])
13126 rtx piece = regno_reg_rtx[i];
13127 basic_block bb = this_basic_block;
13129 if (! dead_or_set_p (place, piece)
13130 && ! reg_bitfield_target_p (piece,
13131 PATTERN (place)))
13133 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13134 NULL_RTX);
13136 distribute_notes (new_note, place, place,
13137 NULL_RTX, NULL_RTX, NULL_RTX);
13139 else if (! refers_to_regno_p (i, i + 1,
13140 PATTERN (place), 0)
13141 && ! find_regno_fusage (place, USE, i))
13142 for (tem = PREV_INSN (place); ;
13143 tem = PREV_INSN (tem))
13145 if (!NONDEBUG_INSN_P (tem))
13147 if (tem == BB_HEAD (bb))
13148 break;
13149 continue;
13151 if (dead_or_set_p (tem, piece)
13152 || reg_bitfield_target_p (piece,
13153 PATTERN (tem)))
13155 add_reg_note (tem, REG_UNUSED, piece);
13156 break;
13162 place = 0;
13166 break;
13168 default:
13169 /* Any other notes should not be present at this point in the
13170 compilation. */
13171 gcc_unreachable ();
13174 if (place)
13176 XEXP (note, 1) = REG_NOTES (place);
13177 REG_NOTES (place) = note;
13180 if (place2)
13181 add_reg_note (place2, REG_NOTE_KIND (note), XEXP (note, 0));
13185 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13186 I3, I2, and I1 to new locations. This is also called to add a link
13187 pointing at I3 when I3's destination is changed. */
13189 static void
13190 distribute_links (rtx links)
13192 rtx link, next_link;
13194 for (link = links; link; link = next_link)
13196 rtx place = 0;
13197 rtx insn;
13198 rtx set, reg;
13200 next_link = XEXP (link, 1);
13202 /* If the insn that this link points to is a NOTE or isn't a single
13203 set, ignore it. In the latter case, it isn't clear what we
13204 can do other than ignore the link, since we can't tell which
13205 register it was for. Such links wouldn't be used by combine
13206 anyway.
13208 It is not possible for the destination of the target of the link to
13209 have been changed by combine. The only potential of this is if we
13210 replace I3, I2, and I1 by I3 and I2. But in that case the
13211 destination of I2 also remains unchanged. */
13213 if (NOTE_P (XEXP (link, 0))
13214 || (set = single_set (XEXP (link, 0))) == 0)
13215 continue;
13217 reg = SET_DEST (set);
13218 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13219 || GET_CODE (reg) == STRICT_LOW_PART)
13220 reg = XEXP (reg, 0);
13222 /* A LOG_LINK is defined as being placed on the first insn that uses
13223 a register and points to the insn that sets the register. Start
13224 searching at the next insn after the target of the link and stop
13225 when we reach a set of the register or the end of the basic block.
13227 Note that this correctly handles the link that used to point from
13228 I3 to I2. Also note that not much searching is typically done here
13229 since most links don't point very far away. */
13231 for (insn = NEXT_INSN (XEXP (link, 0));
13232 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13233 || BB_HEAD (this_basic_block->next_bb) != insn));
13234 insn = NEXT_INSN (insn))
13235 if (DEBUG_INSN_P (insn))
13236 continue;
13237 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13239 if (reg_referenced_p (reg, PATTERN (insn)))
13240 place = insn;
13241 break;
13243 else if (CALL_P (insn)
13244 && find_reg_fusage (insn, USE, reg))
13246 place = insn;
13247 break;
13249 else if (INSN_P (insn) && reg_set_p (reg, insn))
13250 break;
13252 /* If we found a place to put the link, place it there unless there
13253 is already a link to the same insn as LINK at that point. */
13255 if (place)
13257 rtx link2;
13259 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13260 if (XEXP (link2, 0) == XEXP (link, 0))
13261 break;
13263 if (link2 == 0)
13265 XEXP (link, 1) = LOG_LINKS (place);
13266 LOG_LINKS (place) = link;
13268 /* Set added_links_insn to the earliest insn we added a
13269 link to. */
13270 if (added_links_insn == 0
13271 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13272 added_links_insn = place;
13278 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13279 Check whether the expression pointer to by LOC is a register or
13280 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13281 Otherwise return zero. */
13283 static int
13284 unmentioned_reg_p_1 (rtx *loc, void *expr)
13286 rtx x = *loc;
13288 if (x != NULL_RTX
13289 && (REG_P (x) || MEM_P (x))
13290 && ! reg_mentioned_p (x, (rtx) expr))
13291 return 1;
13292 return 0;
13295 /* Check for any register or memory mentioned in EQUIV that is not
13296 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13297 of EXPR where some registers may have been replaced by constants. */
13299 static bool
13300 unmentioned_reg_p (rtx equiv, rtx expr)
13302 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13305 void
13306 dump_combine_stats (FILE *file)
13308 fprintf
13309 (file,
13310 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13311 combine_attempts, combine_merges, combine_extras, combine_successes);
13314 void
13315 dump_combine_total_stats (FILE *file)
13317 fprintf
13318 (file,
13319 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13320 total_attempts, total_merges, total_extras, total_successes);
13323 static bool
13324 gate_handle_combine (void)
13326 return (optimize > 0);
13329 /* Try combining insns through substitution. */
13330 static unsigned int
13331 rest_of_handle_combine (void)
13333 int rebuild_jump_labels_after_combine;
13335 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13336 df_note_add_problem ();
13337 df_analyze ();
13339 regstat_init_n_sets_and_refs ();
13341 rebuild_jump_labels_after_combine
13342 = combine_instructions (get_insns (), max_reg_num ());
13344 /* Combining insns may have turned an indirect jump into a
13345 direct jump. Rebuild the JUMP_LABEL fields of jumping
13346 instructions. */
13347 if (rebuild_jump_labels_after_combine)
13349 timevar_push (TV_JUMP);
13350 rebuild_jump_labels (get_insns ());
13351 cleanup_cfg (0);
13352 timevar_pop (TV_JUMP);
13355 regstat_free_n_sets_and_refs ();
13356 return 0;
13359 struct rtl_opt_pass pass_combine =
13362 RTL_PASS,
13363 "combine", /* name */
13364 gate_handle_combine, /* gate */
13365 rest_of_handle_combine, /* execute */
13366 NULL, /* sub */
13367 NULL, /* next */
13368 0, /* static_pass_number */
13369 TV_COMBINE, /* tv_id */
13370 PROP_cfglayout, /* properties_required */
13371 0, /* properties_provided */
13372 0, /* properties_destroyed */
13373 0, /* todo_flags_start */
13374 TODO_dump_func |
13375 TODO_df_finish | TODO_verify_rtl_sharing |
13376 TODO_ggc_collect, /* todo_flags_finish */