* gnat.dg/nan_max.adb: New test.
[official-gcc.git] / gcc / combine.c
blob61df870e9b3983e0eca182b2e711cf50393630af
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This module is essentially the "combiner" phase of the U. of Arizona
21 Portable Optimizer, but redone to work on our list-structured
22 representation for RTL instead of their string representation.
24 The LOG_LINKS of each insn identify the most recent assignment
25 to each REG used in the insn. It is a list of previous insns,
26 each of which contains a SET for a REG that is used in this insn
27 and not used or set in between. LOG_LINKs never cross basic blocks.
28 They were set up by the preceding pass (lifetime analysis).
30 We try to combine each pair of insns joined by a logical link.
31 We also try to combine triplets of insns A, B and C when C has
32 a link back to B and B has a link back to A. Likewise for a
33 small number of quadruplets of insns A, B, C and D for which
34 there's high likelihood of of success.
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 "stor-layout.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "predict.h"
90 #include "vec.h"
91 #include "hashtab.h"
92 #include "hash-set.h"
93 #include "machmode.h"
94 #include "input.h"
95 #include "function.h"
96 #include "dominance.h"
97 #include "cfg.h"
98 #include "cfgrtl.h"
99 #include "cfgcleanup.h"
100 #include "basic-block.h"
101 #include "insn-config.h"
102 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
103 #include "expr.h"
104 #include "insn-attr.h"
105 #include "recog.h"
106 #include "diagnostic-core.h"
107 #include "target.h"
108 #include "insn-codes.h"
109 #include "optabs.h"
110 #include "rtlhooks-def.h"
111 #include "params.h"
112 #include "tree-pass.h"
113 #include "df.h"
114 #include "valtrack.h"
115 #include "hash-map.h"
116 #include "is-a.h"
117 #include "plugin-api.h"
118 #include "ipa-ref.h"
119 #include "cgraph.h"
120 #include "obstack.h"
121 #include "statistics.h"
122 #include "params.h"
123 #include "rtl-iter.h"
125 /* Number of attempts to combine instructions in this function. */
127 static int combine_attempts;
129 /* Number of attempts that got as far as substitution in this function. */
131 static int combine_merges;
133 /* Number of instructions combined with added SETs in this function. */
135 static int combine_extras;
137 /* Number of instructions combined in this function. */
139 static int combine_successes;
141 /* Totals over entire compilation. */
143 static int total_attempts, total_merges, total_extras, total_successes;
145 /* combine_instructions may try to replace the right hand side of the
146 second instruction with the value of an associated REG_EQUAL note
147 before throwing it at try_combine. That is problematic when there
148 is a REG_DEAD note for a register used in the old right hand side
149 and can cause distribute_notes to do wrong things. This is the
150 second instruction if it has been so modified, null otherwise. */
152 static rtx_insn *i2mod;
154 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
156 static rtx i2mod_old_rhs;
158 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
160 static rtx i2mod_new_rhs;
162 typedef struct reg_stat_struct {
163 /* Record last point of death of (hard or pseudo) register n. */
164 rtx_insn *last_death;
166 /* Record last point of modification of (hard or pseudo) register n. */
167 rtx_insn *last_set;
169 /* The next group of fields allows the recording of the last value assigned
170 to (hard or pseudo) register n. We use this information to see if an
171 operation being processed is redundant given a prior operation performed
172 on the register. For example, an `and' with a constant is redundant if
173 all the zero bits are already known to be turned off.
175 We use an approach similar to that used by cse, but change it in the
176 following ways:
178 (1) We do not want to reinitialize at each label.
179 (2) It is useful, but not critical, to know the actual value assigned
180 to a register. Often just its form is helpful.
182 Therefore, we maintain the following fields:
184 last_set_value the last value assigned
185 last_set_label records the value of label_tick when the
186 register was assigned
187 last_set_table_tick records the value of label_tick when a
188 value using the register is assigned
189 last_set_invalid set to nonzero when it is not valid
190 to use the value of this register in some
191 register's value
193 To understand the usage of these tables, it is important to understand
194 the distinction between the value in last_set_value being valid and
195 the register being validly contained in some other expression in the
196 table.
198 (The next two parameters are out of date).
200 reg_stat[i].last_set_value is valid if it is nonzero, and either
201 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
203 Register I may validly appear in any expression returned for the value
204 of another register if reg_n_sets[i] is 1. It may also appear in the
205 value for register J if reg_stat[j].last_set_invalid is zero, or
206 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
208 If an expression is found in the table containing a register which may
209 not validly appear in an expression, the register is replaced by
210 something that won't match, (clobber (const_int 0)). */
212 /* Record last value assigned to (hard or pseudo) register n. */
214 rtx last_set_value;
216 /* Record the value of label_tick when an expression involving register n
217 is placed in last_set_value. */
219 int last_set_table_tick;
221 /* Record the value of label_tick when the value for register n is placed in
222 last_set_value. */
224 int last_set_label;
226 /* These fields are maintained in parallel with last_set_value and are
227 used to store the mode in which the register was last set, the bits
228 that were known to be zero when it was last set, and the number of
229 sign bits copies it was known to have when it was last set. */
231 unsigned HOST_WIDE_INT last_set_nonzero_bits;
232 char last_set_sign_bit_copies;
233 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
235 /* Set nonzero if references to register n in expressions should not be
236 used. last_set_invalid is set nonzero when this register is being
237 assigned to and last_set_table_tick == label_tick. */
239 char last_set_invalid;
241 /* Some registers that are set more than once and used in more than one
242 basic block are nevertheless always set in similar ways. For example,
243 a QImode register may be loaded from memory in two places on a machine
244 where byte loads zero extend.
246 We record in the following fields if a register has some leading bits
247 that are always equal to the sign bit, and what we know about the
248 nonzero bits of a register, specifically which bits are known to be
249 zero.
251 If an entry is zero, it means that we don't know anything special. */
253 unsigned char sign_bit_copies;
255 unsigned HOST_WIDE_INT nonzero_bits;
257 /* Record the value of the label_tick when the last truncation
258 happened. The field truncated_to_mode is only valid if
259 truncation_label == label_tick. */
261 int truncation_label;
263 /* Record the last truncation seen for this register. If truncation
264 is not a nop to this mode we might be able to save an explicit
265 truncation if we know that value already contains a truncated
266 value. */
268 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
269 } reg_stat_type;
272 static vec<reg_stat_type> reg_stat;
274 /* Record the luid of the last insn that invalidated memory
275 (anything that writes memory, and subroutine calls, but not pushes). */
277 static int mem_last_set;
279 /* Record the luid of the last CALL_INSN
280 so we can tell whether a potential combination crosses any calls. */
282 static int last_call_luid;
284 /* When `subst' is called, this is the insn that is being modified
285 (by combining in a previous insn). The PATTERN of this insn
286 is still the old pattern partially modified and it should not be
287 looked at, but this may be used to examine the successors of the insn
288 to judge whether a simplification is valid. */
290 static rtx_insn *subst_insn;
292 /* This is the lowest LUID that `subst' is currently dealing with.
293 get_last_value will not return a value if the register was set at or
294 after this LUID. If not for this mechanism, we could get confused if
295 I2 or I1 in try_combine were an insn that used the old value of a register
296 to obtain a new value. In that case, we might erroneously get the
297 new value of the register when we wanted the old one. */
299 static int subst_low_luid;
301 /* This contains any hard registers that are used in newpat; reg_dead_at_p
302 must consider all these registers to be always live. */
304 static HARD_REG_SET newpat_used_regs;
306 /* This is an insn to which a LOG_LINKS entry has been added. If this
307 insn is the earlier than I2 or I3, combine should rescan starting at
308 that location. */
310 static rtx_insn *added_links_insn;
312 /* Basic block in which we are performing combines. */
313 static basic_block this_basic_block;
314 static bool optimize_this_for_speed_p;
317 /* Length of the currently allocated uid_insn_cost array. */
319 static int max_uid_known;
321 /* The following array records the insn_rtx_cost for every insn
322 in the instruction stream. */
324 static int *uid_insn_cost;
326 /* The following array records the LOG_LINKS for every insn in the
327 instruction stream as struct insn_link pointers. */
329 struct insn_link {
330 rtx_insn *insn;
331 unsigned int regno;
332 struct insn_link *next;
335 static struct insn_link **uid_log_links;
337 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
338 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
340 #define FOR_EACH_LOG_LINK(L, INSN) \
341 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
343 /* Links for LOG_LINKS are allocated from this obstack. */
345 static struct obstack insn_link_obstack;
347 /* Allocate a link. */
349 static inline struct insn_link *
350 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
352 struct insn_link *l
353 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
354 sizeof (struct insn_link));
355 l->insn = insn;
356 l->regno = regno;
357 l->next = next;
358 return l;
361 /* Incremented for each basic block. */
363 static int label_tick;
365 /* Reset to label_tick for each extended basic block in scanning order. */
367 static int label_tick_ebb_start;
369 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
370 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
372 static machine_mode nonzero_bits_mode;
374 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
375 be safely used. It is zero while computing them and after combine has
376 completed. This former test prevents propagating values based on
377 previously set values, which can be incorrect if a variable is modified
378 in a loop. */
380 static int nonzero_sign_valid;
383 /* Record one modification to rtl structure
384 to be undone by storing old_contents into *where. */
386 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
388 struct undo
390 struct undo *next;
391 enum undo_kind kind;
392 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
393 union { rtx *r; int *i; struct insn_link **l; } where;
396 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
397 num_undo says how many are currently recorded.
399 other_insn is nonzero if we have modified some other insn in the process
400 of working on subst_insn. It must be verified too. */
402 struct undobuf
404 struct undo *undos;
405 struct undo *frees;
406 rtx_insn *other_insn;
409 static struct undobuf undobuf;
411 /* Number of times the pseudo being substituted for
412 was found and replaced. */
414 static int n_occurrences;
416 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
417 machine_mode,
418 unsigned HOST_WIDE_INT,
419 unsigned HOST_WIDE_INT *);
420 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
421 machine_mode,
422 unsigned int, unsigned int *);
423 static void do_SUBST (rtx *, rtx);
424 static void do_SUBST_INT (int *, int);
425 static void init_reg_last (void);
426 static void setup_incoming_promotions (rtx_insn *);
427 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
428 static int cant_combine_insn_p (rtx_insn *);
429 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
430 rtx_insn *, rtx_insn *, rtx *, rtx *);
431 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
432 static int contains_muldiv (rtx);
433 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
434 int *, rtx_insn *);
435 static void undo_all (void);
436 static void undo_commit (void);
437 static rtx *find_split_point (rtx *, rtx_insn *, bool);
438 static rtx subst (rtx, rtx, rtx, int, int, int);
439 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
440 static rtx simplify_if_then_else (rtx);
441 static rtx simplify_set (rtx);
442 static rtx simplify_logical (rtx);
443 static rtx expand_compound_operation (rtx);
444 static const_rtx expand_field_assignment (const_rtx);
445 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
446 rtx, unsigned HOST_WIDE_INT, int, int, int);
447 static rtx extract_left_shift (rtx, int);
448 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
449 unsigned HOST_WIDE_INT *);
450 static rtx canon_reg_for_combine (rtx, rtx);
451 static rtx force_to_mode (rtx, machine_mode,
452 unsigned HOST_WIDE_INT, int);
453 static rtx if_then_else_cond (rtx, rtx *, rtx *);
454 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
455 static int rtx_equal_for_field_assignment_p (rtx, rtx);
456 static rtx make_field_assignment (rtx);
457 static rtx apply_distributive_law (rtx);
458 static rtx distribute_and_simplify_rtx (rtx, int);
459 static rtx simplify_and_const_int_1 (machine_mode, rtx,
460 unsigned HOST_WIDE_INT);
461 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
462 unsigned HOST_WIDE_INT);
463 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
464 HOST_WIDE_INT, machine_mode, int *);
465 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
466 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
467 int);
468 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
469 static rtx gen_lowpart_for_combine (machine_mode, rtx);
470 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
471 rtx, rtx *);
472 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
473 static void update_table_tick (rtx);
474 static void record_value_for_reg (rtx, rtx_insn *, rtx);
475 static void check_promoted_subreg (rtx_insn *, rtx);
476 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
477 static void record_dead_and_set_regs (rtx_insn *);
478 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
479 static rtx get_last_value (const_rtx);
480 static int use_crosses_set_p (const_rtx, int);
481 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
482 static int reg_dead_at_p (rtx, rtx_insn *);
483 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
484 static int reg_bitfield_target_p (rtx, rtx);
485 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
486 static void distribute_links (struct insn_link *);
487 static void mark_used_regs_combine (rtx);
488 static void record_promoted_value (rtx_insn *, rtx);
489 static bool unmentioned_reg_p (rtx, rtx);
490 static void record_truncated_values (rtx *, void *);
491 static bool reg_truncated_to_mode (machine_mode, const_rtx);
492 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
495 /* It is not safe to use ordinary gen_lowpart in combine.
496 See comments in gen_lowpart_for_combine. */
497 #undef RTL_HOOKS_GEN_LOWPART
498 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
500 /* Our implementation of gen_lowpart never emits a new pseudo. */
501 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
502 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
504 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
505 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
507 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
508 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
510 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
511 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
513 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
516 /* Convenience wrapper for the canonicalize_comparison target hook.
517 Target hooks cannot use enum rtx_code. */
518 static inline void
519 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
520 bool op0_preserve_value)
522 int code_int = (int)*code;
523 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
524 *code = (enum rtx_code)code_int;
527 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
528 PATTERN can not be split. Otherwise, it returns an insn sequence.
529 This is a wrapper around split_insns which ensures that the
530 reg_stat vector is made larger if the splitter creates a new
531 register. */
533 static rtx_insn *
534 combine_split_insns (rtx pattern, rtx insn)
536 rtx_insn *ret;
537 unsigned int nregs;
539 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
540 nregs = max_reg_num ();
541 if (nregs > reg_stat.length ())
542 reg_stat.safe_grow_cleared (nregs);
543 return ret;
546 /* This is used by find_single_use to locate an rtx in LOC that
547 contains exactly one use of DEST, which is typically either a REG
548 or CC0. It returns a pointer to the innermost rtx expression
549 containing DEST. Appearances of DEST that are being used to
550 totally replace it are not counted. */
552 static rtx *
553 find_single_use_1 (rtx dest, rtx *loc)
555 rtx x = *loc;
556 enum rtx_code code = GET_CODE (x);
557 rtx *result = NULL;
558 rtx *this_result;
559 int i;
560 const char *fmt;
562 switch (code)
564 case CONST:
565 case LABEL_REF:
566 case SYMBOL_REF:
567 CASE_CONST_ANY:
568 case CLOBBER:
569 return 0;
571 case SET:
572 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
573 of a REG that occupies all of the REG, the insn uses DEST if
574 it is mentioned in the destination or the source. Otherwise, we
575 need just check the source. */
576 if (GET_CODE (SET_DEST (x)) != CC0
577 && GET_CODE (SET_DEST (x)) != PC
578 && !REG_P (SET_DEST (x))
579 && ! (GET_CODE (SET_DEST (x)) == SUBREG
580 && REG_P (SUBREG_REG (SET_DEST (x)))
581 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
582 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
583 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
584 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
585 break;
587 return find_single_use_1 (dest, &SET_SRC (x));
589 case MEM:
590 case SUBREG:
591 return find_single_use_1 (dest, &XEXP (x, 0));
593 default:
594 break;
597 /* If it wasn't one of the common cases above, check each expression and
598 vector of this code. Look for a unique usage of DEST. */
600 fmt = GET_RTX_FORMAT (code);
601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
603 if (fmt[i] == 'e')
605 if (dest == XEXP (x, i)
606 || (REG_P (dest) && REG_P (XEXP (x, i))
607 && REGNO (dest) == REGNO (XEXP (x, i))))
608 this_result = loc;
609 else
610 this_result = find_single_use_1 (dest, &XEXP (x, i));
612 if (result == NULL)
613 result = this_result;
614 else if (this_result)
615 /* Duplicate usage. */
616 return NULL;
618 else if (fmt[i] == 'E')
620 int j;
622 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
624 if (XVECEXP (x, i, j) == dest
625 || (REG_P (dest)
626 && REG_P (XVECEXP (x, i, j))
627 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
628 this_result = loc;
629 else
630 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
632 if (result == NULL)
633 result = this_result;
634 else if (this_result)
635 return NULL;
640 return result;
644 /* See if DEST, produced in INSN, is used only a single time in the
645 sequel. If so, return a pointer to the innermost rtx expression in which
646 it is used.
648 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
650 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
651 care about REG_DEAD notes or LOG_LINKS.
653 Otherwise, we find the single use by finding an insn that has a
654 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
655 only referenced once in that insn, we know that it must be the first
656 and last insn referencing DEST. */
658 static rtx *
659 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
661 basic_block bb;
662 rtx_insn *next;
663 rtx *result;
664 struct insn_link *link;
666 #ifdef HAVE_cc0
667 if (dest == cc0_rtx)
669 next = NEXT_INSN (insn);
670 if (next == 0
671 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
672 return 0;
674 result = find_single_use_1 (dest, &PATTERN (next));
675 if (result && ploc)
676 *ploc = next;
677 return result;
679 #endif
681 if (!REG_P (dest))
682 return 0;
684 bb = BLOCK_FOR_INSN (insn);
685 for (next = NEXT_INSN (insn);
686 next && BLOCK_FOR_INSN (next) == bb;
687 next = NEXT_INSN (next))
688 if (INSN_P (next) && dead_or_set_p (next, dest))
690 FOR_EACH_LOG_LINK (link, next)
691 if (link->insn == insn && link->regno == REGNO (dest))
692 break;
694 if (link)
696 result = find_single_use_1 (dest, &PATTERN (next));
697 if (ploc)
698 *ploc = next;
699 return result;
703 return 0;
706 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
707 insn. The substitution can be undone by undo_all. If INTO is already
708 set to NEWVAL, do not record this change. Because computing NEWVAL might
709 also call SUBST, we have to compute it before we put anything into
710 the undo table. */
712 static void
713 do_SUBST (rtx *into, rtx newval)
715 struct undo *buf;
716 rtx oldval = *into;
718 if (oldval == newval)
719 return;
721 /* We'd like to catch as many invalid transformations here as
722 possible. Unfortunately, there are way too many mode changes
723 that are perfectly valid, so we'd waste too much effort for
724 little gain doing the checks here. Focus on catching invalid
725 transformations involving integer constants. */
726 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
727 && CONST_INT_P (newval))
729 /* Sanity check that we're replacing oldval with a CONST_INT
730 that is a valid sign-extension for the original mode. */
731 gcc_assert (INTVAL (newval)
732 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
734 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
735 CONST_INT is not valid, because after the replacement, the
736 original mode would be gone. Unfortunately, we can't tell
737 when do_SUBST is called to replace the operand thereof, so we
738 perform this test on oldval instead, checking whether an
739 invalid replacement took place before we got here. */
740 gcc_assert (!(GET_CODE (oldval) == SUBREG
741 && CONST_INT_P (SUBREG_REG (oldval))));
742 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
743 && CONST_INT_P (XEXP (oldval, 0))));
746 if (undobuf.frees)
747 buf = undobuf.frees, undobuf.frees = buf->next;
748 else
749 buf = XNEW (struct undo);
751 buf->kind = UNDO_RTX;
752 buf->where.r = into;
753 buf->old_contents.r = oldval;
754 *into = newval;
756 buf->next = undobuf.undos, undobuf.undos = buf;
759 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
761 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
762 for the value of a HOST_WIDE_INT value (including CONST_INT) is
763 not safe. */
765 static void
766 do_SUBST_INT (int *into, int newval)
768 struct undo *buf;
769 int oldval = *into;
771 if (oldval == newval)
772 return;
774 if (undobuf.frees)
775 buf = undobuf.frees, undobuf.frees = buf->next;
776 else
777 buf = XNEW (struct undo);
779 buf->kind = UNDO_INT;
780 buf->where.i = into;
781 buf->old_contents.i = oldval;
782 *into = newval;
784 buf->next = undobuf.undos, undobuf.undos = buf;
787 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
789 /* Similar to SUBST, but just substitute the mode. This is used when
790 changing the mode of a pseudo-register, so that any other
791 references to the entry in the regno_reg_rtx array will change as
792 well. */
794 static void
795 do_SUBST_MODE (rtx *into, machine_mode newval)
797 struct undo *buf;
798 machine_mode oldval = GET_MODE (*into);
800 if (oldval == newval)
801 return;
803 if (undobuf.frees)
804 buf = undobuf.frees, undobuf.frees = buf->next;
805 else
806 buf = XNEW (struct undo);
808 buf->kind = UNDO_MODE;
809 buf->where.r = into;
810 buf->old_contents.m = oldval;
811 adjust_reg_mode (*into, newval);
813 buf->next = undobuf.undos, undobuf.undos = buf;
816 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
818 #ifndef HAVE_cc0
819 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
821 static void
822 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
824 struct undo *buf;
825 struct insn_link * oldval = *into;
827 if (oldval == newval)
828 return;
830 if (undobuf.frees)
831 buf = undobuf.frees, undobuf.frees = buf->next;
832 else
833 buf = XNEW (struct undo);
835 buf->kind = UNDO_LINKS;
836 buf->where.l = into;
837 buf->old_contents.l = oldval;
838 *into = newval;
840 buf->next = undobuf.undos, undobuf.undos = buf;
843 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
844 #endif
846 /* Subroutine of try_combine. Determine whether the replacement patterns
847 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
848 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
849 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
850 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
851 of all the instructions can be estimated and the replacements are more
852 expensive than the original sequence. */
854 static bool
855 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
856 rtx newpat, rtx newi2pat, rtx newotherpat)
858 int i0_cost, i1_cost, i2_cost, i3_cost;
859 int new_i2_cost, new_i3_cost;
860 int old_cost, new_cost;
862 /* Lookup the original insn_rtx_costs. */
863 i2_cost = INSN_COST (i2);
864 i3_cost = INSN_COST (i3);
866 if (i1)
868 i1_cost = INSN_COST (i1);
869 if (i0)
871 i0_cost = INSN_COST (i0);
872 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
873 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
875 else
877 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
878 ? i1_cost + i2_cost + i3_cost : 0);
879 i0_cost = 0;
882 else
884 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
885 i1_cost = i0_cost = 0;
888 /* Calculate the replacement insn_rtx_costs. */
889 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
890 if (newi2pat)
892 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
893 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
894 ? new_i2_cost + new_i3_cost : 0;
896 else
898 new_cost = new_i3_cost;
899 new_i2_cost = 0;
902 if (undobuf.other_insn)
904 int old_other_cost, new_other_cost;
906 old_other_cost = INSN_COST (undobuf.other_insn);
907 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
908 if (old_other_cost > 0 && new_other_cost > 0)
910 old_cost += old_other_cost;
911 new_cost += new_other_cost;
913 else
914 old_cost = 0;
917 /* Disallow this combination if both new_cost and old_cost are greater than
918 zero, and new_cost is greater than old cost. */
919 int reject = old_cost > 0 && new_cost > old_cost;
921 if (dump_file)
923 fprintf (dump_file, "%s combination of insns ",
924 reject ? "rejecting" : "allowing");
925 if (i0)
926 fprintf (dump_file, "%d, ", INSN_UID (i0));
927 if (i1)
928 fprintf (dump_file, "%d, ", INSN_UID (i1));
929 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
931 fprintf (dump_file, "original costs ");
932 if (i0)
933 fprintf (dump_file, "%d + ", i0_cost);
934 if (i1)
935 fprintf (dump_file, "%d + ", i1_cost);
936 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
938 if (newi2pat)
939 fprintf (dump_file, "replacement costs %d + %d = %d\n",
940 new_i2_cost, new_i3_cost, new_cost);
941 else
942 fprintf (dump_file, "replacement cost %d\n", new_cost);
945 if (reject)
946 return false;
948 /* Update the uid_insn_cost array with the replacement costs. */
949 INSN_COST (i2) = new_i2_cost;
950 INSN_COST (i3) = new_i3_cost;
951 if (i1)
953 INSN_COST (i1) = 0;
954 if (i0)
955 INSN_COST (i0) = 0;
958 return true;
962 /* Delete any insns that copy a register to itself. */
964 static void
965 delete_noop_moves (void)
967 rtx_insn *insn, *next;
968 basic_block bb;
970 FOR_EACH_BB_FN (bb, cfun)
972 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
974 next = NEXT_INSN (insn);
975 if (INSN_P (insn) && noop_move_p (insn))
977 if (dump_file)
978 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
980 delete_insn_and_edges (insn);
987 /* Return false if we do not want to (or cannot) combine DEF. */
988 static bool
989 can_combine_def_p (df_ref def)
991 /* Do not consider if it is pre/post modification in MEM. */
992 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
993 return false;
995 unsigned int regno = DF_REF_REGNO (def);
997 /* Do not combine frame pointer adjustments. */
998 if ((regno == FRAME_POINTER_REGNUM
999 && (!reload_completed || frame_pointer_needed))
1000 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1001 || (regno == HARD_FRAME_POINTER_REGNUM
1002 && (!reload_completed || frame_pointer_needed))
1003 #endif
1004 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1005 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1006 #endif
1008 return false;
1010 return true;
1013 /* Return false if we do not want to (or cannot) combine USE. */
1014 static bool
1015 can_combine_use_p (df_ref use)
1017 /* Do not consider the usage of the stack pointer by function call. */
1018 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1019 return false;
1021 return true;
1024 /* Fill in log links field for all insns. */
1026 static void
1027 create_log_links (void)
1029 basic_block bb;
1030 rtx_insn **next_use;
1031 rtx_insn *insn;
1032 df_ref def, use;
1034 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1036 /* Pass through each block from the end, recording the uses of each
1037 register and establishing log links when def is encountered.
1038 Note that we do not clear next_use array in order to save time,
1039 so we have to test whether the use is in the same basic block as def.
1041 There are a few cases below when we do not consider the definition or
1042 usage -- these are taken from original flow.c did. Don't ask me why it is
1043 done this way; I don't know and if it works, I don't want to know. */
1045 FOR_EACH_BB_FN (bb, cfun)
1047 FOR_BB_INSNS_REVERSE (bb, insn)
1049 if (!NONDEBUG_INSN_P (insn))
1050 continue;
1052 /* Log links are created only once. */
1053 gcc_assert (!LOG_LINKS (insn));
1055 FOR_EACH_INSN_DEF (def, insn)
1057 unsigned int regno = DF_REF_REGNO (def);
1058 rtx_insn *use_insn;
1060 if (!next_use[regno])
1061 continue;
1063 if (!can_combine_def_p (def))
1064 continue;
1066 use_insn = next_use[regno];
1067 next_use[regno] = NULL;
1069 if (BLOCK_FOR_INSN (use_insn) != bb)
1070 continue;
1072 /* flow.c claimed:
1074 We don't build a LOG_LINK for hard registers contained
1075 in ASM_OPERANDs. If these registers get replaced,
1076 we might wind up changing the semantics of the insn,
1077 even if reload can make what appear to be valid
1078 assignments later. */
1079 if (regno < FIRST_PSEUDO_REGISTER
1080 && asm_noperands (PATTERN (use_insn)) >= 0)
1081 continue;
1083 /* Don't add duplicate links between instructions. */
1084 struct insn_link *links;
1085 FOR_EACH_LOG_LINK (links, use_insn)
1086 if (insn == links->insn && regno == links->regno)
1087 break;
1089 if (!links)
1090 LOG_LINKS (use_insn)
1091 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1094 FOR_EACH_INSN_USE (use, insn)
1095 if (can_combine_use_p (use))
1096 next_use[DF_REF_REGNO (use)] = insn;
1100 free (next_use);
1103 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1104 true if we found a LOG_LINK that proves that A feeds B. This only works
1105 if there are no instructions between A and B which could have a link
1106 depending on A, since in that case we would not record a link for B.
1107 We also check the implicit dependency created by a cc0 setter/user
1108 pair. */
1110 static bool
1111 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1113 struct insn_link *links;
1114 FOR_EACH_LOG_LINK (links, b)
1115 if (links->insn == a)
1116 return true;
1117 #ifdef HAVE_cc0
1118 if (sets_cc0_p (a))
1119 return true;
1120 #endif
1121 return false;
1124 /* Main entry point for combiner. F is the first insn of the function.
1125 NREGS is the first unused pseudo-reg number.
1127 Return nonzero if the combiner has turned an indirect jump
1128 instruction into a direct jump. */
1129 static int
1130 combine_instructions (rtx_insn *f, unsigned int nregs)
1132 rtx_insn *insn, *next;
1133 #ifdef HAVE_cc0
1134 rtx_insn *prev;
1135 #endif
1136 struct insn_link *links, *nextlinks;
1137 rtx_insn *first;
1138 basic_block last_bb;
1140 int new_direct_jump_p = 0;
1142 for (first = f; first && !INSN_P (first); )
1143 first = NEXT_INSN (first);
1144 if (!first)
1145 return 0;
1147 combine_attempts = 0;
1148 combine_merges = 0;
1149 combine_extras = 0;
1150 combine_successes = 0;
1152 rtl_hooks = combine_rtl_hooks;
1154 reg_stat.safe_grow_cleared (nregs);
1156 init_recog_no_volatile ();
1158 /* Allocate array for insn info. */
1159 max_uid_known = get_max_uid ();
1160 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1161 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1162 gcc_obstack_init (&insn_link_obstack);
1164 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1166 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1167 problems when, for example, we have j <<= 1 in a loop. */
1169 nonzero_sign_valid = 0;
1170 label_tick = label_tick_ebb_start = 1;
1172 /* Scan all SETs and see if we can deduce anything about what
1173 bits are known to be zero for some registers and how many copies
1174 of the sign bit are known to exist for those registers.
1176 Also set any known values so that we can use it while searching
1177 for what bits are known to be set. */
1179 setup_incoming_promotions (first);
1180 /* Allow the entry block and the first block to fall into the same EBB.
1181 Conceptually the incoming promotions are assigned to the entry block. */
1182 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1184 create_log_links ();
1185 FOR_EACH_BB_FN (this_basic_block, cfun)
1187 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1188 last_call_luid = 0;
1189 mem_last_set = -1;
1191 label_tick++;
1192 if (!single_pred_p (this_basic_block)
1193 || single_pred (this_basic_block) != last_bb)
1194 label_tick_ebb_start = label_tick;
1195 last_bb = this_basic_block;
1197 FOR_BB_INSNS (this_basic_block, insn)
1198 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1200 #ifdef AUTO_INC_DEC
1201 rtx links;
1202 #endif
1204 subst_low_luid = DF_INSN_LUID (insn);
1205 subst_insn = insn;
1207 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1208 insn);
1209 record_dead_and_set_regs (insn);
1211 #ifdef AUTO_INC_DEC
1212 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1213 if (REG_NOTE_KIND (links) == REG_INC)
1214 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1215 insn);
1216 #endif
1218 /* Record the current insn_rtx_cost of this instruction. */
1219 if (NONJUMP_INSN_P (insn))
1220 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1221 optimize_this_for_speed_p);
1222 if (dump_file)
1223 fprintf (dump_file, "insn_cost %d: %d\n",
1224 INSN_UID (insn), INSN_COST (insn));
1228 nonzero_sign_valid = 1;
1230 /* Now scan all the insns in forward order. */
1231 label_tick = label_tick_ebb_start = 1;
1232 init_reg_last ();
1233 setup_incoming_promotions (first);
1234 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1235 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1237 FOR_EACH_BB_FN (this_basic_block, cfun)
1239 rtx_insn *last_combined_insn = NULL;
1240 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1241 last_call_luid = 0;
1242 mem_last_set = -1;
1244 label_tick++;
1245 if (!single_pred_p (this_basic_block)
1246 || single_pred (this_basic_block) != last_bb)
1247 label_tick_ebb_start = label_tick;
1248 last_bb = this_basic_block;
1250 rtl_profile_for_bb (this_basic_block);
1251 for (insn = BB_HEAD (this_basic_block);
1252 insn != NEXT_INSN (BB_END (this_basic_block));
1253 insn = next ? next : NEXT_INSN (insn))
1255 next = 0;
1256 if (!NONDEBUG_INSN_P (insn))
1257 continue;
1259 while (last_combined_insn
1260 && last_combined_insn->deleted ())
1261 last_combined_insn = PREV_INSN (last_combined_insn);
1262 if (last_combined_insn == NULL_RTX
1263 || BARRIER_P (last_combined_insn)
1264 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1265 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1266 last_combined_insn = insn;
1268 /* See if we know about function return values before this
1269 insn based upon SUBREG flags. */
1270 check_promoted_subreg (insn, PATTERN (insn));
1272 /* See if we can find hardregs and subreg of pseudos in
1273 narrower modes. This could help turning TRUNCATEs
1274 into SUBREGs. */
1275 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1277 /* Try this insn with each insn it links back to. */
1279 FOR_EACH_LOG_LINK (links, insn)
1280 if ((next = try_combine (insn, links->insn, NULL,
1281 NULL, &new_direct_jump_p,
1282 last_combined_insn)) != 0)
1284 statistics_counter_event (cfun, "two-insn combine", 1);
1285 goto retry;
1288 /* Try each sequence of three linked insns ending with this one. */
1290 if (max_combine >= 3)
1291 FOR_EACH_LOG_LINK (links, insn)
1293 rtx_insn *link = links->insn;
1295 /* If the linked insn has been replaced by a note, then there
1296 is no point in pursuing this chain any further. */
1297 if (NOTE_P (link))
1298 continue;
1300 FOR_EACH_LOG_LINK (nextlinks, link)
1301 if ((next = try_combine (insn, link, nextlinks->insn,
1302 NULL, &new_direct_jump_p,
1303 last_combined_insn)) != 0)
1305 statistics_counter_event (cfun, "three-insn combine", 1);
1306 goto retry;
1310 #ifdef HAVE_cc0
1311 /* Try to combine a jump insn that uses CC0
1312 with a preceding insn that sets CC0, and maybe with its
1313 logical predecessor as well.
1314 This is how we make decrement-and-branch insns.
1315 We need this special code because data flow connections
1316 via CC0 do not get entered in LOG_LINKS. */
1318 if (JUMP_P (insn)
1319 && (prev = prev_nonnote_insn (insn)) != 0
1320 && NONJUMP_INSN_P (prev)
1321 && sets_cc0_p (PATTERN (prev)))
1323 if ((next = try_combine (insn, prev, NULL, NULL,
1324 &new_direct_jump_p,
1325 last_combined_insn)) != 0)
1326 goto retry;
1328 FOR_EACH_LOG_LINK (nextlinks, prev)
1329 if ((next = try_combine (insn, prev, nextlinks->insn,
1330 NULL, &new_direct_jump_p,
1331 last_combined_insn)) != 0)
1332 goto retry;
1335 /* Do the same for an insn that explicitly references CC0. */
1336 if (NONJUMP_INSN_P (insn)
1337 && (prev = prev_nonnote_insn (insn)) != 0
1338 && NONJUMP_INSN_P (prev)
1339 && sets_cc0_p (PATTERN (prev))
1340 && GET_CODE (PATTERN (insn)) == SET
1341 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1343 if ((next = try_combine (insn, prev, NULL, NULL,
1344 &new_direct_jump_p,
1345 last_combined_insn)) != 0)
1346 goto retry;
1348 FOR_EACH_LOG_LINK (nextlinks, prev)
1349 if ((next = try_combine (insn, prev, nextlinks->insn,
1350 NULL, &new_direct_jump_p,
1351 last_combined_insn)) != 0)
1352 goto retry;
1355 /* Finally, see if any of the insns that this insn links to
1356 explicitly references CC0. If so, try this insn, that insn,
1357 and its predecessor if it sets CC0. */
1358 FOR_EACH_LOG_LINK (links, insn)
1359 if (NONJUMP_INSN_P (links->insn)
1360 && GET_CODE (PATTERN (links->insn)) == SET
1361 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1362 && (prev = prev_nonnote_insn (links->insn)) != 0
1363 && NONJUMP_INSN_P (prev)
1364 && sets_cc0_p (PATTERN (prev))
1365 && (next = try_combine (insn, links->insn,
1366 prev, NULL, &new_direct_jump_p,
1367 last_combined_insn)) != 0)
1368 goto retry;
1369 #endif
1371 /* Try combining an insn with two different insns whose results it
1372 uses. */
1373 if (max_combine >= 3)
1374 FOR_EACH_LOG_LINK (links, insn)
1375 for (nextlinks = links->next; nextlinks;
1376 nextlinks = nextlinks->next)
1377 if ((next = try_combine (insn, links->insn,
1378 nextlinks->insn, NULL,
1379 &new_direct_jump_p,
1380 last_combined_insn)) != 0)
1383 statistics_counter_event (cfun, "three-insn combine", 1);
1384 goto retry;
1387 /* Try four-instruction combinations. */
1388 if (max_combine >= 4)
1389 FOR_EACH_LOG_LINK (links, insn)
1391 struct insn_link *next1;
1392 rtx_insn *link = links->insn;
1394 /* If the linked insn has been replaced by a note, then there
1395 is no point in pursuing this chain any further. */
1396 if (NOTE_P (link))
1397 continue;
1399 FOR_EACH_LOG_LINK (next1, link)
1401 rtx_insn *link1 = next1->insn;
1402 if (NOTE_P (link1))
1403 continue;
1404 /* I0 -> I1 -> I2 -> I3. */
1405 FOR_EACH_LOG_LINK (nextlinks, link1)
1406 if ((next = try_combine (insn, link, link1,
1407 nextlinks->insn,
1408 &new_direct_jump_p,
1409 last_combined_insn)) != 0)
1411 statistics_counter_event (cfun, "four-insn combine", 1);
1412 goto retry;
1414 /* I0, I1 -> I2, I2 -> I3. */
1415 for (nextlinks = next1->next; nextlinks;
1416 nextlinks = nextlinks->next)
1417 if ((next = try_combine (insn, link, link1,
1418 nextlinks->insn,
1419 &new_direct_jump_p,
1420 last_combined_insn)) != 0)
1422 statistics_counter_event (cfun, "four-insn combine", 1);
1423 goto retry;
1427 for (next1 = links->next; next1; next1 = next1->next)
1429 rtx_insn *link1 = next1->insn;
1430 if (NOTE_P (link1))
1431 continue;
1432 /* I0 -> I2; I1, I2 -> I3. */
1433 FOR_EACH_LOG_LINK (nextlinks, link)
1434 if ((next = try_combine (insn, link, link1,
1435 nextlinks->insn,
1436 &new_direct_jump_p,
1437 last_combined_insn)) != 0)
1439 statistics_counter_event (cfun, "four-insn combine", 1);
1440 goto retry;
1442 /* I0 -> I1; I1, I2 -> I3. */
1443 FOR_EACH_LOG_LINK (nextlinks, link1)
1444 if ((next = try_combine (insn, link, link1,
1445 nextlinks->insn,
1446 &new_direct_jump_p,
1447 last_combined_insn)) != 0)
1449 statistics_counter_event (cfun, "four-insn combine", 1);
1450 goto retry;
1455 /* Try this insn with each REG_EQUAL note it links back to. */
1456 FOR_EACH_LOG_LINK (links, insn)
1458 rtx set, note;
1459 rtx_insn *temp = links->insn;
1460 if ((set = single_set (temp)) != 0
1461 && (note = find_reg_equal_equiv_note (temp)) != 0
1462 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1463 /* Avoid using a register that may already been marked
1464 dead by an earlier instruction. */
1465 && ! unmentioned_reg_p (note, SET_SRC (set))
1466 && (GET_MODE (note) == VOIDmode
1467 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1468 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1470 /* Temporarily replace the set's source with the
1471 contents of the REG_EQUAL note. The insn will
1472 be deleted or recognized by try_combine. */
1473 rtx orig = SET_SRC (set);
1474 SET_SRC (set) = note;
1475 i2mod = temp;
1476 i2mod_old_rhs = copy_rtx (orig);
1477 i2mod_new_rhs = copy_rtx (note);
1478 next = try_combine (insn, i2mod, NULL, NULL,
1479 &new_direct_jump_p,
1480 last_combined_insn);
1481 i2mod = NULL;
1482 if (next)
1484 statistics_counter_event (cfun, "insn-with-note combine", 1);
1485 goto retry;
1487 SET_SRC (set) = orig;
1491 if (!NOTE_P (insn))
1492 record_dead_and_set_regs (insn);
1494 retry:
1499 default_rtl_profile ();
1500 clear_bb_flags ();
1501 new_direct_jump_p |= purge_all_dead_edges ();
1502 delete_noop_moves ();
1504 /* Clean up. */
1505 obstack_free (&insn_link_obstack, NULL);
1506 free (uid_log_links);
1507 free (uid_insn_cost);
1508 reg_stat.release ();
1511 struct undo *undo, *next;
1512 for (undo = undobuf.frees; undo; undo = next)
1514 next = undo->next;
1515 free (undo);
1517 undobuf.frees = 0;
1520 total_attempts += combine_attempts;
1521 total_merges += combine_merges;
1522 total_extras += combine_extras;
1523 total_successes += combine_successes;
1525 nonzero_sign_valid = 0;
1526 rtl_hooks = general_rtl_hooks;
1528 /* Make recognizer allow volatile MEMs again. */
1529 init_recog ();
1531 return new_direct_jump_p;
1534 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1536 static void
1537 init_reg_last (void)
1539 unsigned int i;
1540 reg_stat_type *p;
1542 FOR_EACH_VEC_ELT (reg_stat, i, p)
1543 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1546 /* Set up any promoted values for incoming argument registers. */
1548 static void
1549 setup_incoming_promotions (rtx_insn *first)
1551 tree arg;
1552 bool strictly_local = false;
1554 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1555 arg = DECL_CHAIN (arg))
1557 rtx x, reg = DECL_INCOMING_RTL (arg);
1558 int uns1, uns3;
1559 machine_mode mode1, mode2, mode3, mode4;
1561 /* Only continue if the incoming argument is in a register. */
1562 if (!REG_P (reg))
1563 continue;
1565 /* Determine, if possible, whether all call sites of the current
1566 function lie within the current compilation unit. (This does
1567 take into account the exporting of a function via taking its
1568 address, and so forth.) */
1569 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1571 /* The mode and signedness of the argument before any promotions happen
1572 (equal to the mode of the pseudo holding it at that stage). */
1573 mode1 = TYPE_MODE (TREE_TYPE (arg));
1574 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1576 /* The mode and signedness of the argument after any source language and
1577 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1578 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1579 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1581 /* The mode and signedness of the argument as it is actually passed,
1582 see assign_parm_setup_reg in function.c. */
1583 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1584 TREE_TYPE (cfun->decl), 0);
1586 /* The mode of the register in which the argument is being passed. */
1587 mode4 = GET_MODE (reg);
1589 /* Eliminate sign extensions in the callee when:
1590 (a) A mode promotion has occurred; */
1591 if (mode1 == mode3)
1592 continue;
1593 /* (b) The mode of the register is the same as the mode of
1594 the argument as it is passed; */
1595 if (mode3 != mode4)
1596 continue;
1597 /* (c) There's no language level extension; */
1598 if (mode1 == mode2)
1600 /* (c.1) All callers are from the current compilation unit. If that's
1601 the case we don't have to rely on an ABI, we only have to know
1602 what we're generating right now, and we know that we will do the
1603 mode1 to mode2 promotion with the given sign. */
1604 else if (!strictly_local)
1605 continue;
1606 /* (c.2) The combination of the two promotions is useful. This is
1607 true when the signs match, or if the first promotion is unsigned.
1608 In the later case, (sign_extend (zero_extend x)) is the same as
1609 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1610 else if (uns1)
1611 uns3 = true;
1612 else if (uns3)
1613 continue;
1615 /* Record that the value was promoted from mode1 to mode3,
1616 so that any sign extension at the head of the current
1617 function may be eliminated. */
1618 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1619 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1620 record_value_for_reg (reg, first, x);
1624 /* Called via note_stores. If X is a pseudo that is narrower than
1625 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1627 If we are setting only a portion of X and we can't figure out what
1628 portion, assume all bits will be used since we don't know what will
1629 be happening.
1631 Similarly, set how many bits of X are known to be copies of the sign bit
1632 at all locations in the function. This is the smallest number implied
1633 by any set of X. */
1635 static void
1636 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1638 rtx_insn *insn = (rtx_insn *) data;
1639 unsigned int num;
1641 if (REG_P (x)
1642 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1643 /* If this register is undefined at the start of the file, we can't
1644 say what its contents were. */
1645 && ! REGNO_REG_SET_P
1646 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1647 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1649 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1651 if (set == 0 || GET_CODE (set) == CLOBBER)
1653 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1654 rsp->sign_bit_copies = 1;
1655 return;
1658 /* If this register is being initialized using itself, and the
1659 register is uninitialized in this basic block, and there are
1660 no LOG_LINKS which set the register, then part of the
1661 register is uninitialized. In that case we can't assume
1662 anything about the number of nonzero bits.
1664 ??? We could do better if we checked this in
1665 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1666 could avoid making assumptions about the insn which initially
1667 sets the register, while still using the information in other
1668 insns. We would have to be careful to check every insn
1669 involved in the combination. */
1671 if (insn
1672 && reg_referenced_p (x, PATTERN (insn))
1673 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1674 REGNO (x)))
1676 struct insn_link *link;
1678 FOR_EACH_LOG_LINK (link, insn)
1679 if (dead_or_set_p (link->insn, x))
1680 break;
1681 if (!link)
1683 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1684 rsp->sign_bit_copies = 1;
1685 return;
1689 /* If this is a complex assignment, see if we can convert it into a
1690 simple assignment. */
1691 set = expand_field_assignment (set);
1693 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1694 set what we know about X. */
1696 if (SET_DEST (set) == x
1697 || (paradoxical_subreg_p (SET_DEST (set))
1698 && SUBREG_REG (SET_DEST (set)) == x))
1700 rtx src = SET_SRC (set);
1702 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1703 /* If X is narrower than a word and SRC is a non-negative
1704 constant that would appear negative in the mode of X,
1705 sign-extend it for use in reg_stat[].nonzero_bits because some
1706 machines (maybe most) will actually do the sign-extension
1707 and this is the conservative approach.
1709 ??? For 2.5, try to tighten up the MD files in this regard
1710 instead of this kludge. */
1712 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1713 && CONST_INT_P (src)
1714 && INTVAL (src) > 0
1715 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1716 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1717 #endif
1719 /* Don't call nonzero_bits if it cannot change anything. */
1720 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1721 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1722 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1723 if (rsp->sign_bit_copies == 0
1724 || rsp->sign_bit_copies > num)
1725 rsp->sign_bit_copies = num;
1727 else
1729 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1730 rsp->sign_bit_copies = 1;
1735 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1736 optionally insns that were previously combined into I3 or that will be
1737 combined into the merger of INSN and I3. The order is PRED, PRED2,
1738 INSN, SUCC, SUCC2, I3.
1740 Return 0 if the combination is not allowed for any reason.
1742 If the combination is allowed, *PDEST will be set to the single
1743 destination of INSN and *PSRC to the single source, and this function
1744 will return 1. */
1746 static int
1747 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1748 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1749 rtx *pdest, rtx *psrc)
1751 int i;
1752 const_rtx set = 0;
1753 rtx src, dest;
1754 rtx_insn *p;
1755 #ifdef AUTO_INC_DEC
1756 rtx link;
1757 #endif
1758 bool all_adjacent = true;
1759 int (*is_volatile_p) (const_rtx);
1761 if (succ)
1763 if (succ2)
1765 if (next_active_insn (succ2) != i3)
1766 all_adjacent = false;
1767 if (next_active_insn (succ) != succ2)
1768 all_adjacent = false;
1770 else if (next_active_insn (succ) != i3)
1771 all_adjacent = false;
1772 if (next_active_insn (insn) != succ)
1773 all_adjacent = false;
1775 else if (next_active_insn (insn) != i3)
1776 all_adjacent = false;
1778 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1779 or a PARALLEL consisting of such a SET and CLOBBERs.
1781 If INSN has CLOBBER parallel parts, ignore them for our processing.
1782 By definition, these happen during the execution of the insn. When it
1783 is merged with another insn, all bets are off. If they are, in fact,
1784 needed and aren't also supplied in I3, they may be added by
1785 recog_for_combine. Otherwise, it won't match.
1787 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1788 note.
1790 Get the source and destination of INSN. If more than one, can't
1791 combine. */
1793 if (GET_CODE (PATTERN (insn)) == SET)
1794 set = PATTERN (insn);
1795 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1796 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1798 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1800 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1802 switch (GET_CODE (elt))
1804 /* This is important to combine floating point insns
1805 for the SH4 port. */
1806 case USE:
1807 /* Combining an isolated USE doesn't make sense.
1808 We depend here on combinable_i3pat to reject them. */
1809 /* The code below this loop only verifies that the inputs of
1810 the SET in INSN do not change. We call reg_set_between_p
1811 to verify that the REG in the USE does not change between
1812 I3 and INSN.
1813 If the USE in INSN was for a pseudo register, the matching
1814 insn pattern will likely match any register; combining this
1815 with any other USE would only be safe if we knew that the
1816 used registers have identical values, or if there was
1817 something to tell them apart, e.g. different modes. For
1818 now, we forgo such complicated tests and simply disallow
1819 combining of USES of pseudo registers with any other USE. */
1820 if (REG_P (XEXP (elt, 0))
1821 && GET_CODE (PATTERN (i3)) == PARALLEL)
1823 rtx i3pat = PATTERN (i3);
1824 int i = XVECLEN (i3pat, 0) - 1;
1825 unsigned int regno = REGNO (XEXP (elt, 0));
1829 rtx i3elt = XVECEXP (i3pat, 0, i);
1831 if (GET_CODE (i3elt) == USE
1832 && REG_P (XEXP (i3elt, 0))
1833 && (REGNO (XEXP (i3elt, 0)) == regno
1834 ? reg_set_between_p (XEXP (elt, 0),
1835 PREV_INSN (insn), i3)
1836 : regno >= FIRST_PSEUDO_REGISTER))
1837 return 0;
1839 while (--i >= 0);
1841 break;
1843 /* We can ignore CLOBBERs. */
1844 case CLOBBER:
1845 break;
1847 case SET:
1848 /* Ignore SETs whose result isn't used but not those that
1849 have side-effects. */
1850 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1851 && insn_nothrow_p (insn)
1852 && !side_effects_p (elt))
1853 break;
1855 /* If we have already found a SET, this is a second one and
1856 so we cannot combine with this insn. */
1857 if (set)
1858 return 0;
1860 set = elt;
1861 break;
1863 default:
1864 /* Anything else means we can't combine. */
1865 return 0;
1869 if (set == 0
1870 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1871 so don't do anything with it. */
1872 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1873 return 0;
1875 else
1876 return 0;
1878 if (set == 0)
1879 return 0;
1881 /* The simplification in expand_field_assignment may call back to
1882 get_last_value, so set safe guard here. */
1883 subst_low_luid = DF_INSN_LUID (insn);
1885 set = expand_field_assignment (set);
1886 src = SET_SRC (set), dest = SET_DEST (set);
1888 /* Don't eliminate a store in the stack pointer. */
1889 if (dest == stack_pointer_rtx
1890 /* Don't combine with an insn that sets a register to itself if it has
1891 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1892 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1893 /* Can't merge an ASM_OPERANDS. */
1894 || GET_CODE (src) == ASM_OPERANDS
1895 /* Can't merge a function call. */
1896 || GET_CODE (src) == CALL
1897 /* Don't eliminate a function call argument. */
1898 || (CALL_P (i3)
1899 && (find_reg_fusage (i3, USE, dest)
1900 || (REG_P (dest)
1901 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1902 && global_regs[REGNO (dest)])))
1903 /* Don't substitute into an incremented register. */
1904 || FIND_REG_INC_NOTE (i3, dest)
1905 || (succ && FIND_REG_INC_NOTE (succ, dest))
1906 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1907 /* Don't substitute into a non-local goto, this confuses CFG. */
1908 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1909 /* Make sure that DEST is not used after SUCC but before I3. */
1910 || (!all_adjacent
1911 && ((succ2
1912 && (reg_used_between_p (dest, succ2, i3)
1913 || reg_used_between_p (dest, succ, succ2)))
1914 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1915 /* Make sure that the value that is to be substituted for the register
1916 does not use any registers whose values alter in between. However,
1917 If the insns are adjacent, a use can't cross a set even though we
1918 think it might (this can happen for a sequence of insns each setting
1919 the same destination; last_set of that register might point to
1920 a NOTE). If INSN has a REG_EQUIV note, the register is always
1921 equivalent to the memory so the substitution is valid even if there
1922 are intervening stores. Also, don't move a volatile asm or
1923 UNSPEC_VOLATILE across any other insns. */
1924 || (! all_adjacent
1925 && (((!MEM_P (src)
1926 || ! find_reg_note (insn, REG_EQUIV, src))
1927 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1928 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1929 || GET_CODE (src) == UNSPEC_VOLATILE))
1930 /* Don't combine across a CALL_INSN, because that would possibly
1931 change whether the life span of some REGs crosses calls or not,
1932 and it is a pain to update that information.
1933 Exception: if source is a constant, moving it later can't hurt.
1934 Accept that as a special case. */
1935 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1936 return 0;
1938 /* DEST must either be a REG or CC0. */
1939 if (REG_P (dest))
1941 /* If register alignment is being enforced for multi-word items in all
1942 cases except for parameters, it is possible to have a register copy
1943 insn referencing a hard register that is not allowed to contain the
1944 mode being copied and which would not be valid as an operand of most
1945 insns. Eliminate this problem by not combining with such an insn.
1947 Also, on some machines we don't want to extend the life of a hard
1948 register. */
1950 if (REG_P (src)
1951 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1952 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1953 /* Don't extend the life of a hard register unless it is
1954 user variable (if we have few registers) or it can't
1955 fit into the desired register (meaning something special
1956 is going on).
1957 Also avoid substituting a return register into I3, because
1958 reload can't handle a conflict with constraints of other
1959 inputs. */
1960 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1961 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1962 return 0;
1964 else if (GET_CODE (dest) != CC0)
1965 return 0;
1968 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1969 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1970 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1972 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1974 /* If the clobber represents an earlyclobber operand, we must not
1975 substitute an expression containing the clobbered register.
1976 As we do not analyze the constraint strings here, we have to
1977 make the conservative assumption. However, if the register is
1978 a fixed hard reg, the clobber cannot represent any operand;
1979 we leave it up to the machine description to either accept or
1980 reject use-and-clobber patterns. */
1981 if (!REG_P (reg)
1982 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1983 || !fixed_regs[REGNO (reg)])
1984 if (reg_overlap_mentioned_p (reg, src))
1985 return 0;
1988 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1989 or not), reject, unless nothing volatile comes between it and I3 */
1991 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1993 /* Make sure neither succ nor succ2 contains a volatile reference. */
1994 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1995 return 0;
1996 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1997 return 0;
1998 /* We'll check insns between INSN and I3 below. */
2001 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2002 to be an explicit register variable, and was chosen for a reason. */
2004 if (GET_CODE (src) == ASM_OPERANDS
2005 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2006 return 0;
2008 /* If INSN contains volatile references (specifically volatile MEMs),
2009 we cannot combine across any other volatile references.
2010 Even if INSN doesn't contain volatile references, any intervening
2011 volatile insn might affect machine state. */
2013 is_volatile_p = volatile_refs_p (PATTERN (insn))
2014 ? volatile_refs_p
2015 : volatile_insn_p;
2017 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2018 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2019 return 0;
2021 /* If INSN contains an autoincrement or autodecrement, make sure that
2022 register is not used between there and I3, and not already used in
2023 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2024 Also insist that I3 not be a jump; if it were one
2025 and the incremented register were spilled, we would lose. */
2027 #ifdef AUTO_INC_DEC
2028 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2029 if (REG_NOTE_KIND (link) == REG_INC
2030 && (JUMP_P (i3)
2031 || reg_used_between_p (XEXP (link, 0), insn, i3)
2032 || (pred != NULL_RTX
2033 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2034 || (pred2 != NULL_RTX
2035 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2036 || (succ != NULL_RTX
2037 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2038 || (succ2 != NULL_RTX
2039 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2040 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2041 return 0;
2042 #endif
2044 #ifdef HAVE_cc0
2045 /* Don't combine an insn that follows a CC0-setting insn.
2046 An insn that uses CC0 must not be separated from the one that sets it.
2047 We do, however, allow I2 to follow a CC0-setting insn if that insn
2048 is passed as I1; in that case it will be deleted also.
2049 We also allow combining in this case if all the insns are adjacent
2050 because that would leave the two CC0 insns adjacent as well.
2051 It would be more logical to test whether CC0 occurs inside I1 or I2,
2052 but that would be much slower, and this ought to be equivalent. */
2054 p = prev_nonnote_insn (insn);
2055 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2056 && ! all_adjacent)
2057 return 0;
2058 #endif
2060 /* If we get here, we have passed all the tests and the combination is
2061 to be allowed. */
2063 *pdest = dest;
2064 *psrc = src;
2066 return 1;
2069 /* LOC is the location within I3 that contains its pattern or the component
2070 of a PARALLEL of the pattern. We validate that it is valid for combining.
2072 One problem is if I3 modifies its output, as opposed to replacing it
2073 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2074 doing so would produce an insn that is not equivalent to the original insns.
2076 Consider:
2078 (set (reg:DI 101) (reg:DI 100))
2079 (set (subreg:SI (reg:DI 101) 0) <foo>)
2081 This is NOT equivalent to:
2083 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2084 (set (reg:DI 101) (reg:DI 100))])
2086 Not only does this modify 100 (in which case it might still be valid
2087 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2089 We can also run into a problem if I2 sets a register that I1
2090 uses and I1 gets directly substituted into I3 (not via I2). In that
2091 case, we would be getting the wrong value of I2DEST into I3, so we
2092 must reject the combination. This case occurs when I2 and I1 both
2093 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2094 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2095 of a SET must prevent combination from occurring. The same situation
2096 can occur for I0, in which case I0_NOT_IN_SRC is set.
2098 Before doing the above check, we first try to expand a field assignment
2099 into a set of logical operations.
2101 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2102 we place a register that is both set and used within I3. If more than one
2103 such register is detected, we fail.
2105 Return 1 if the combination is valid, zero otherwise. */
2107 static int
2108 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2109 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2111 rtx x = *loc;
2113 if (GET_CODE (x) == SET)
2115 rtx set = x ;
2116 rtx dest = SET_DEST (set);
2117 rtx src = SET_SRC (set);
2118 rtx inner_dest = dest;
2119 rtx subdest;
2121 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2122 || GET_CODE (inner_dest) == SUBREG
2123 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2124 inner_dest = XEXP (inner_dest, 0);
2126 /* Check for the case where I3 modifies its output, as discussed
2127 above. We don't want to prevent pseudos from being combined
2128 into the address of a MEM, so only prevent the combination if
2129 i1 or i2 set the same MEM. */
2130 if ((inner_dest != dest &&
2131 (!MEM_P (inner_dest)
2132 || rtx_equal_p (i2dest, inner_dest)
2133 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2134 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2135 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2136 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2137 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2139 /* This is the same test done in can_combine_p except we can't test
2140 all_adjacent; we don't have to, since this instruction will stay
2141 in place, thus we are not considering increasing the lifetime of
2142 INNER_DEST.
2144 Also, if this insn sets a function argument, combining it with
2145 something that might need a spill could clobber a previous
2146 function argument; the all_adjacent test in can_combine_p also
2147 checks this; here, we do a more specific test for this case. */
2149 || (REG_P (inner_dest)
2150 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2151 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2152 GET_MODE (inner_dest))))
2153 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2154 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2155 return 0;
2157 /* If DEST is used in I3, it is being killed in this insn, so
2158 record that for later. We have to consider paradoxical
2159 subregs here, since they kill the whole register, but we
2160 ignore partial subregs, STRICT_LOW_PART, etc.
2161 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2162 STACK_POINTER_REGNUM, since these are always considered to be
2163 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2164 subdest = dest;
2165 if (GET_CODE (subdest) == SUBREG
2166 && (GET_MODE_SIZE (GET_MODE (subdest))
2167 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2168 subdest = SUBREG_REG (subdest);
2169 if (pi3dest_killed
2170 && REG_P (subdest)
2171 && reg_referenced_p (subdest, PATTERN (i3))
2172 && REGNO (subdest) != FRAME_POINTER_REGNUM
2173 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2174 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2175 #endif
2176 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2177 && (REGNO (subdest) != ARG_POINTER_REGNUM
2178 || ! fixed_regs [REGNO (subdest)])
2179 #endif
2180 && REGNO (subdest) != STACK_POINTER_REGNUM)
2182 if (*pi3dest_killed)
2183 return 0;
2185 *pi3dest_killed = subdest;
2189 else if (GET_CODE (x) == PARALLEL)
2191 int i;
2193 for (i = 0; i < XVECLEN (x, 0); i++)
2194 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2195 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2196 return 0;
2199 return 1;
2202 /* Return 1 if X is an arithmetic expression that contains a multiplication
2203 and division. We don't count multiplications by powers of two here. */
2205 static int
2206 contains_muldiv (rtx x)
2208 switch (GET_CODE (x))
2210 case MOD: case DIV: case UMOD: case UDIV:
2211 return 1;
2213 case MULT:
2214 return ! (CONST_INT_P (XEXP (x, 1))
2215 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2216 default:
2217 if (BINARY_P (x))
2218 return contains_muldiv (XEXP (x, 0))
2219 || contains_muldiv (XEXP (x, 1));
2221 if (UNARY_P (x))
2222 return contains_muldiv (XEXP (x, 0));
2224 return 0;
2228 /* Determine whether INSN can be used in a combination. Return nonzero if
2229 not. This is used in try_combine to detect early some cases where we
2230 can't perform combinations. */
2232 static int
2233 cant_combine_insn_p (rtx_insn *insn)
2235 rtx set;
2236 rtx src, dest;
2238 /* If this isn't really an insn, we can't do anything.
2239 This can occur when flow deletes an insn that it has merged into an
2240 auto-increment address. */
2241 if (! INSN_P (insn))
2242 return 1;
2244 /* Never combine loads and stores involving hard regs that are likely
2245 to be spilled. The register allocator can usually handle such
2246 reg-reg moves by tying. If we allow the combiner to make
2247 substitutions of likely-spilled regs, reload might die.
2248 As an exception, we allow combinations involving fixed regs; these are
2249 not available to the register allocator so there's no risk involved. */
2251 set = single_set (insn);
2252 if (! set)
2253 return 0;
2254 src = SET_SRC (set);
2255 dest = SET_DEST (set);
2256 if (GET_CODE (src) == SUBREG)
2257 src = SUBREG_REG (src);
2258 if (GET_CODE (dest) == SUBREG)
2259 dest = SUBREG_REG (dest);
2260 if (REG_P (src) && REG_P (dest)
2261 && ((HARD_REGISTER_P (src)
2262 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2263 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2264 || (HARD_REGISTER_P (dest)
2265 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2266 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2267 return 1;
2269 return 0;
2272 struct likely_spilled_retval_info
2274 unsigned regno, nregs;
2275 unsigned mask;
2278 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2279 hard registers that are known to be written to / clobbered in full. */
2280 static void
2281 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2283 struct likely_spilled_retval_info *const info =
2284 (struct likely_spilled_retval_info *) data;
2285 unsigned regno, nregs;
2286 unsigned new_mask;
2288 if (!REG_P (XEXP (set, 0)))
2289 return;
2290 regno = REGNO (x);
2291 if (regno >= info->regno + info->nregs)
2292 return;
2293 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2294 if (regno + nregs <= info->regno)
2295 return;
2296 new_mask = (2U << (nregs - 1)) - 1;
2297 if (regno < info->regno)
2298 new_mask >>= info->regno - regno;
2299 else
2300 new_mask <<= regno - info->regno;
2301 info->mask &= ~new_mask;
2304 /* Return nonzero iff part of the return value is live during INSN, and
2305 it is likely spilled. This can happen when more than one insn is needed
2306 to copy the return value, e.g. when we consider to combine into the
2307 second copy insn for a complex value. */
2309 static int
2310 likely_spilled_retval_p (rtx_insn *insn)
2312 rtx_insn *use = BB_END (this_basic_block);
2313 rtx reg;
2314 rtx_insn *p;
2315 unsigned regno, nregs;
2316 /* We assume here that no machine mode needs more than
2317 32 hard registers when the value overlaps with a register
2318 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2319 unsigned mask;
2320 struct likely_spilled_retval_info info;
2322 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2323 return 0;
2324 reg = XEXP (PATTERN (use), 0);
2325 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2326 return 0;
2327 regno = REGNO (reg);
2328 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2329 if (nregs == 1)
2330 return 0;
2331 mask = (2U << (nregs - 1)) - 1;
2333 /* Disregard parts of the return value that are set later. */
2334 info.regno = regno;
2335 info.nregs = nregs;
2336 info.mask = mask;
2337 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2338 if (INSN_P (p))
2339 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2340 mask = info.mask;
2342 /* Check if any of the (probably) live return value registers is
2343 likely spilled. */
2344 nregs --;
2347 if ((mask & 1 << nregs)
2348 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2349 return 1;
2350 } while (nregs--);
2351 return 0;
2354 /* Adjust INSN after we made a change to its destination.
2356 Changing the destination can invalidate notes that say something about
2357 the results of the insn and a LOG_LINK pointing to the insn. */
2359 static void
2360 adjust_for_new_dest (rtx_insn *insn)
2362 /* For notes, be conservative and simply remove them. */
2363 remove_reg_equal_equiv_notes (insn);
2365 /* The new insn will have a destination that was previously the destination
2366 of an insn just above it. Call distribute_links to make a LOG_LINK from
2367 the next use of that destination. */
2369 rtx set = single_set (insn);
2370 gcc_assert (set);
2372 rtx reg = SET_DEST (set);
2374 while (GET_CODE (reg) == ZERO_EXTRACT
2375 || GET_CODE (reg) == STRICT_LOW_PART
2376 || GET_CODE (reg) == SUBREG)
2377 reg = XEXP (reg, 0);
2378 gcc_assert (REG_P (reg));
2380 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2382 df_insn_rescan (insn);
2385 /* Return TRUE if combine can reuse reg X in mode MODE.
2386 ADDED_SETS is nonzero if the original set is still required. */
2387 static bool
2388 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2390 unsigned int regno;
2392 if (!REG_P (x))
2393 return false;
2395 regno = REGNO (x);
2396 /* Allow hard registers if the new mode is legal, and occupies no more
2397 registers than the old mode. */
2398 if (regno < FIRST_PSEUDO_REGISTER)
2399 return (HARD_REGNO_MODE_OK (regno, mode)
2400 && (hard_regno_nregs[regno][GET_MODE (x)]
2401 >= hard_regno_nregs[regno][mode]));
2403 /* Or a pseudo that is only used once. */
2404 return (REG_N_SETS (regno) == 1 && !added_sets
2405 && !REG_USERVAR_P (x));
2409 /* Check whether X, the destination of a set, refers to part of
2410 the register specified by REG. */
2412 static bool
2413 reg_subword_p (rtx x, rtx reg)
2415 /* Check that reg is an integer mode register. */
2416 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2417 return false;
2419 if (GET_CODE (x) == STRICT_LOW_PART
2420 || GET_CODE (x) == ZERO_EXTRACT)
2421 x = XEXP (x, 0);
2423 return GET_CODE (x) == SUBREG
2424 && SUBREG_REG (x) == reg
2425 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2428 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2429 Note that the INSN should be deleted *after* removing dead edges, so
2430 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2431 but not for a (set (pc) (label_ref FOO)). */
2433 static void
2434 update_cfg_for_uncondjump (rtx_insn *insn)
2436 basic_block bb = BLOCK_FOR_INSN (insn);
2437 gcc_assert (BB_END (bb) == insn);
2439 purge_dead_edges (bb);
2441 delete_insn (insn);
2442 if (EDGE_COUNT (bb->succs) == 1)
2444 rtx_insn *insn;
2446 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2448 /* Remove barriers from the footer if there are any. */
2449 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2450 if (BARRIER_P (insn))
2452 if (PREV_INSN (insn))
2453 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2454 else
2455 BB_FOOTER (bb) = NEXT_INSN (insn);
2456 if (NEXT_INSN (insn))
2457 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2459 else if (LABEL_P (insn))
2460 break;
2464 #ifndef HAVE_cc0
2465 /* Return whether INSN is a PARALLEL of exactly N register SETs followed
2466 by an arbitrary number of CLOBBERs. */
2467 static bool
2468 is_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2470 rtx pat = PATTERN (insn);
2472 if (GET_CODE (pat) != PARALLEL)
2473 return false;
2475 int len = XVECLEN (pat, 0);
2476 if (len < n)
2477 return false;
2479 int i;
2480 for (i = 0; i < n; i++)
2481 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2482 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2483 return false;
2484 for ( ; i < len; i++)
2485 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2486 return false;
2488 return true;
2491 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2492 CLOBBERs), can be split into individual SETs in that order, without
2493 changing semantics. */
2494 static bool
2495 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2497 if (!insn_nothrow_p (insn))
2498 return false;
2500 rtx pat = PATTERN (insn);
2502 int i, j;
2503 for (i = 0; i < n; i++)
2505 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2506 return false;
2508 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2510 for (j = i + 1; j < n; j++)
2511 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2512 return false;
2515 return true;
2517 #endif
2519 /* Try to combine the insns I0, I1 and I2 into I3.
2520 Here I0, I1 and I2 appear earlier than I3.
2521 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2524 If we are combining more than two insns and the resulting insn is not
2525 recognized, try splitting it into two insns. If that happens, I2 and I3
2526 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2527 Otherwise, I0, I1 and I2 are pseudo-deleted.
2529 Return 0 if the combination does not work. Then nothing is changed.
2530 If we did the combination, return the insn at which combine should
2531 resume scanning.
2533 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2534 new direct jump instruction.
2536 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2537 been I3 passed to an earlier try_combine within the same basic
2538 block. */
2540 static rtx_insn *
2541 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2542 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2544 /* New patterns for I3 and I2, respectively. */
2545 rtx newpat, newi2pat = 0;
2546 rtvec newpat_vec_with_clobbers = 0;
2547 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2548 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2549 dead. */
2550 int added_sets_0, added_sets_1, added_sets_2;
2551 /* Total number of SETs to put into I3. */
2552 int total_sets;
2553 /* Nonzero if I2's or I1's body now appears in I3. */
2554 int i2_is_used = 0, i1_is_used = 0;
2555 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2556 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2557 /* Contains I3 if the destination of I3 is used in its source, which means
2558 that the old life of I3 is being killed. If that usage is placed into
2559 I2 and not in I3, a REG_DEAD note must be made. */
2560 rtx i3dest_killed = 0;
2561 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2562 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2563 /* Copy of SET_SRC of I1 and I0, if needed. */
2564 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2565 /* Set if I2DEST was reused as a scratch register. */
2566 bool i2scratch = false;
2567 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2568 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2569 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2570 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2571 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2572 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2573 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2574 /* Notes that must be added to REG_NOTES in I3 and I2. */
2575 rtx new_i3_notes, new_i2_notes;
2576 /* Notes that we substituted I3 into I2 instead of the normal case. */
2577 int i3_subst_into_i2 = 0;
2578 /* Notes that I1, I2 or I3 is a MULT operation. */
2579 int have_mult = 0;
2580 int swap_i2i3 = 0;
2581 int changed_i3_dest = 0;
2583 int maxreg;
2584 rtx_insn *temp_insn;
2585 rtx temp_expr;
2586 struct insn_link *link;
2587 rtx other_pat = 0;
2588 rtx new_other_notes;
2589 int i;
2591 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2592 never be). */
2593 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2594 return 0;
2596 /* Only try four-insn combinations when there's high likelihood of
2597 success. Look for simple insns, such as loads of constants or
2598 binary operations involving a constant. */
2599 if (i0)
2601 int i;
2602 int ngood = 0;
2603 int nshift = 0;
2605 if (!flag_expensive_optimizations)
2606 return 0;
2608 for (i = 0; i < 4; i++)
2610 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2611 rtx set = single_set (insn);
2612 rtx src;
2613 if (!set)
2614 continue;
2615 src = SET_SRC (set);
2616 if (CONSTANT_P (src))
2618 ngood += 2;
2619 break;
2621 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2622 ngood++;
2623 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2624 || GET_CODE (src) == LSHIFTRT)
2625 nshift++;
2627 if (ngood < 2 && nshift < 2)
2628 return 0;
2631 /* Exit early if one of the insns involved can't be used for
2632 combinations. */
2633 if (CALL_P (i2)
2634 || (i1 && CALL_P (i1))
2635 || (i0 && CALL_P (i0))
2636 || cant_combine_insn_p (i3)
2637 || cant_combine_insn_p (i2)
2638 || (i1 && cant_combine_insn_p (i1))
2639 || (i0 && cant_combine_insn_p (i0))
2640 || likely_spilled_retval_p (i3))
2641 return 0;
2643 combine_attempts++;
2644 undobuf.other_insn = 0;
2646 /* Reset the hard register usage information. */
2647 CLEAR_HARD_REG_SET (newpat_used_regs);
2649 if (dump_file && (dump_flags & TDF_DETAILS))
2651 if (i0)
2652 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2653 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2654 else if (i1)
2655 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2656 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2657 else
2658 fprintf (dump_file, "\nTrying %d -> %d:\n",
2659 INSN_UID (i2), INSN_UID (i3));
2662 /* If multiple insns feed into one of I2 or I3, they can be in any
2663 order. To simplify the code below, reorder them in sequence. */
2664 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2665 temp_insn = i2, i2 = i0, i0 = temp_insn;
2666 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2667 temp_insn = i1, i1 = i0, i0 = temp_insn;
2668 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2669 temp_insn = i1, i1 = i2, i2 = temp_insn;
2671 added_links_insn = 0;
2673 /* First check for one important special case that the code below will
2674 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2675 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2676 we may be able to replace that destination with the destination of I3.
2677 This occurs in the common code where we compute both a quotient and
2678 remainder into a structure, in which case we want to do the computation
2679 directly into the structure to avoid register-register copies.
2681 Note that this case handles both multiple sets in I2 and also cases
2682 where I2 has a number of CLOBBERs inside the PARALLEL.
2684 We make very conservative checks below and only try to handle the
2685 most common cases of this. For example, we only handle the case
2686 where I2 and I3 are adjacent to avoid making difficult register
2687 usage tests. */
2689 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2690 && REG_P (SET_SRC (PATTERN (i3)))
2691 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2692 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2693 && GET_CODE (PATTERN (i2)) == PARALLEL
2694 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2695 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2696 below would need to check what is inside (and reg_overlap_mentioned_p
2697 doesn't support those codes anyway). Don't allow those destinations;
2698 the resulting insn isn't likely to be recognized anyway. */
2699 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2700 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2701 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2702 SET_DEST (PATTERN (i3)))
2703 && next_active_insn (i2) == i3)
2705 rtx p2 = PATTERN (i2);
2707 /* Make sure that the destination of I3,
2708 which we are going to substitute into one output of I2,
2709 is not used within another output of I2. We must avoid making this:
2710 (parallel [(set (mem (reg 69)) ...)
2711 (set (reg 69) ...)])
2712 which is not well-defined as to order of actions.
2713 (Besides, reload can't handle output reloads for this.)
2715 The problem can also happen if the dest of I3 is a memory ref,
2716 if another dest in I2 is an indirect memory ref. */
2717 for (i = 0; i < XVECLEN (p2, 0); i++)
2718 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2719 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2720 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2721 SET_DEST (XVECEXP (p2, 0, i))))
2722 break;
2724 /* Make sure this PARALLEL is not an asm. We do not allow combining
2725 that usually (see can_combine_p), so do not here either. */
2726 for (i = 0; i < XVECLEN (p2, 0); i++)
2727 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2728 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2729 break;
2731 if (i == XVECLEN (p2, 0))
2732 for (i = 0; i < XVECLEN (p2, 0); i++)
2733 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2734 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2736 combine_merges++;
2738 subst_insn = i3;
2739 subst_low_luid = DF_INSN_LUID (i2);
2741 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2742 i2src = SET_SRC (XVECEXP (p2, 0, i));
2743 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2744 i2dest_killed = dead_or_set_p (i2, i2dest);
2746 /* Replace the dest in I2 with our dest and make the resulting
2747 insn the new pattern for I3. Then skip to where we validate
2748 the pattern. Everything was set up above. */
2749 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2750 newpat = p2;
2751 i3_subst_into_i2 = 1;
2752 goto validate_replacement;
2756 /* If I2 is setting a pseudo to a constant and I3 is setting some
2757 sub-part of it to another constant, merge them by making a new
2758 constant. */
2759 if (i1 == 0
2760 && (temp_expr = single_set (i2)) != 0
2761 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2762 && GET_CODE (PATTERN (i3)) == SET
2763 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2764 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2766 rtx dest = SET_DEST (PATTERN (i3));
2767 int offset = -1;
2768 int width = 0;
2770 if (GET_CODE (dest) == ZERO_EXTRACT)
2772 if (CONST_INT_P (XEXP (dest, 1))
2773 && CONST_INT_P (XEXP (dest, 2)))
2775 width = INTVAL (XEXP (dest, 1));
2776 offset = INTVAL (XEXP (dest, 2));
2777 dest = XEXP (dest, 0);
2778 if (BITS_BIG_ENDIAN)
2779 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2782 else
2784 if (GET_CODE (dest) == STRICT_LOW_PART)
2785 dest = XEXP (dest, 0);
2786 width = GET_MODE_PRECISION (GET_MODE (dest));
2787 offset = 0;
2790 if (offset >= 0)
2792 /* If this is the low part, we're done. */
2793 if (subreg_lowpart_p (dest))
2795 /* Handle the case where inner is twice the size of outer. */
2796 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2797 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2798 offset += GET_MODE_PRECISION (GET_MODE (dest));
2799 /* Otherwise give up for now. */
2800 else
2801 offset = -1;
2804 if (offset >= 0)
2806 rtx inner = SET_SRC (PATTERN (i3));
2807 rtx outer = SET_SRC (temp_expr);
2809 wide_int o
2810 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2811 std::make_pair (inner, GET_MODE (dest)),
2812 offset, width);
2814 combine_merges++;
2815 subst_insn = i3;
2816 subst_low_luid = DF_INSN_LUID (i2);
2817 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2818 i2dest = SET_DEST (temp_expr);
2819 i2dest_killed = dead_or_set_p (i2, i2dest);
2821 /* Replace the source in I2 with the new constant and make the
2822 resulting insn the new pattern for I3. Then skip to where we
2823 validate the pattern. Everything was set up above. */
2824 SUBST (SET_SRC (temp_expr),
2825 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2827 newpat = PATTERN (i2);
2829 /* The dest of I3 has been replaced with the dest of I2. */
2830 changed_i3_dest = 1;
2831 goto validate_replacement;
2835 #ifndef HAVE_cc0
2836 /* If we have no I1 and I2 looks like:
2837 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2838 (set Y OP)])
2839 make up a dummy I1 that is
2840 (set Y OP)
2841 and change I2 to be
2842 (set (reg:CC X) (compare:CC Y (const_int 0)))
2844 (We can ignore any trailing CLOBBERs.)
2846 This undoes a previous combination and allows us to match a branch-and-
2847 decrement insn. */
2849 if (i1 == 0
2850 && is_parallel_of_n_reg_sets (i2, 2)
2851 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2852 == MODE_CC)
2853 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2854 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2855 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2856 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2857 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2858 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2860 /* We make I1 with the same INSN_UID as I2. This gives it
2861 the same DF_INSN_LUID for value tracking. Our fake I1 will
2862 never appear in the insn stream so giving it the same INSN_UID
2863 as I2 will not cause a problem. */
2865 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2866 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2867 -1, NULL_RTX);
2868 INSN_UID (i1) = INSN_UID (i2);
2870 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2871 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2872 SET_DEST (PATTERN (i1)));
2873 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2874 SUBST_LINK (LOG_LINKS (i2),
2875 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2878 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2879 make those two SETs separate I1 and I2 insns, and make an I0 that is
2880 the original I1. */
2881 if (i0 == 0
2882 && is_parallel_of_n_reg_sets (i2, 2)
2883 && can_split_parallel_of_n_reg_sets (i2, 2)
2884 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2885 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2887 /* If there is no I1, there is no I0 either. */
2888 i0 = i1;
2890 /* We make I1 with the same INSN_UID as I2. This gives it
2891 the same DF_INSN_LUID for value tracking. Our fake I1 will
2892 never appear in the insn stream so giving it the same INSN_UID
2893 as I2 will not cause a problem. */
2895 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2896 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2897 -1, NULL_RTX);
2898 INSN_UID (i1) = INSN_UID (i2);
2900 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2902 #endif
2904 /* Verify that I2 and I1 are valid for combining. */
2905 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2906 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2907 &i1dest, &i1src))
2908 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2909 &i0dest, &i0src)))
2911 undo_all ();
2912 return 0;
2915 /* Record whether I2DEST is used in I2SRC and similarly for the other
2916 cases. Knowing this will help in register status updating below. */
2917 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2918 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2919 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2920 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2921 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2922 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2923 i2dest_killed = dead_or_set_p (i2, i2dest);
2924 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2925 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2927 /* For the earlier insns, determine which of the subsequent ones they
2928 feed. */
2929 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2930 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2931 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2932 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2933 && reg_overlap_mentioned_p (i0dest, i2src))));
2935 /* Ensure that I3's pattern can be the destination of combines. */
2936 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2937 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2938 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2939 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2940 &i3dest_killed))
2942 undo_all ();
2943 return 0;
2946 /* See if any of the insns is a MULT operation. Unless one is, we will
2947 reject a combination that is, since it must be slower. Be conservative
2948 here. */
2949 if (GET_CODE (i2src) == MULT
2950 || (i1 != 0 && GET_CODE (i1src) == MULT)
2951 || (i0 != 0 && GET_CODE (i0src) == MULT)
2952 || (GET_CODE (PATTERN (i3)) == SET
2953 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2954 have_mult = 1;
2956 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2957 We used to do this EXCEPT in one case: I3 has a post-inc in an
2958 output operand. However, that exception can give rise to insns like
2959 mov r3,(r3)+
2960 which is a famous insn on the PDP-11 where the value of r3 used as the
2961 source was model-dependent. Avoid this sort of thing. */
2963 #if 0
2964 if (!(GET_CODE (PATTERN (i3)) == SET
2965 && REG_P (SET_SRC (PATTERN (i3)))
2966 && MEM_P (SET_DEST (PATTERN (i3)))
2967 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2968 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2969 /* It's not the exception. */
2970 #endif
2971 #ifdef AUTO_INC_DEC
2973 rtx link;
2974 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2975 if (REG_NOTE_KIND (link) == REG_INC
2976 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2977 || (i1 != 0
2978 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2980 undo_all ();
2981 return 0;
2984 #endif
2986 /* See if the SETs in I1 or I2 need to be kept around in the merged
2987 instruction: whenever the value set there is still needed past I3.
2988 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2990 For the SET in I1, we have two cases: if I1 and I2 independently feed
2991 into I3, the set in I1 needs to be kept around unless I1DEST dies
2992 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2993 in I1 needs to be kept around unless I1DEST dies or is set in either
2994 I2 or I3. The same considerations apply to I0. */
2996 added_sets_2 = !dead_or_set_p (i3, i2dest);
2998 if (i1)
2999 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3000 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3001 else
3002 added_sets_1 = 0;
3004 if (i0)
3005 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3006 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3007 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3008 && dead_or_set_p (i2, i0dest)));
3009 else
3010 added_sets_0 = 0;
3012 /* We are about to copy insns for the case where they need to be kept
3013 around. Check that they can be copied in the merged instruction. */
3015 if (targetm.cannot_copy_insn_p
3016 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3017 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3018 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3020 undo_all ();
3021 return 0;
3024 /* If the set in I2 needs to be kept around, we must make a copy of
3025 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3026 PATTERN (I2), we are only substituting for the original I1DEST, not into
3027 an already-substituted copy. This also prevents making self-referential
3028 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3029 I2DEST. */
3031 if (added_sets_2)
3033 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3034 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3035 else
3036 i2pat = copy_rtx (PATTERN (i2));
3039 if (added_sets_1)
3041 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3042 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3043 else
3044 i1pat = copy_rtx (PATTERN (i1));
3047 if (added_sets_0)
3049 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3050 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3051 else
3052 i0pat = copy_rtx (PATTERN (i0));
3055 combine_merges++;
3057 /* Substitute in the latest insn for the regs set by the earlier ones. */
3059 maxreg = max_reg_num ();
3061 subst_insn = i3;
3063 #ifndef HAVE_cc0
3064 /* Many machines that don't use CC0 have insns that can both perform an
3065 arithmetic operation and set the condition code. These operations will
3066 be represented as a PARALLEL with the first element of the vector
3067 being a COMPARE of an arithmetic operation with the constant zero.
3068 The second element of the vector will set some pseudo to the result
3069 of the same arithmetic operation. If we simplify the COMPARE, we won't
3070 match such a pattern and so will generate an extra insn. Here we test
3071 for this case, where both the comparison and the operation result are
3072 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3073 I2SRC. Later we will make the PARALLEL that contains I2. */
3075 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3076 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3077 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3078 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3080 rtx newpat_dest;
3081 rtx *cc_use_loc = NULL;
3082 rtx_insn *cc_use_insn = NULL;
3083 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3084 machine_mode compare_mode, orig_compare_mode;
3085 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3087 newpat = PATTERN (i3);
3088 newpat_dest = SET_DEST (newpat);
3089 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3091 if (undobuf.other_insn == 0
3092 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3093 &cc_use_insn)))
3095 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3096 compare_code = simplify_compare_const (compare_code,
3097 GET_MODE (i2dest), op0, &op1);
3098 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3101 /* Do the rest only if op1 is const0_rtx, which may be the
3102 result of simplification. */
3103 if (op1 == const0_rtx)
3105 /* If a single use of the CC is found, prepare to modify it
3106 when SELECT_CC_MODE returns a new CC-class mode, or when
3107 the above simplify_compare_const() returned a new comparison
3108 operator. undobuf.other_insn is assigned the CC use insn
3109 when modifying it. */
3110 if (cc_use_loc)
3112 #ifdef SELECT_CC_MODE
3113 machine_mode new_mode
3114 = SELECT_CC_MODE (compare_code, op0, op1);
3115 if (new_mode != orig_compare_mode
3116 && can_change_dest_mode (SET_DEST (newpat),
3117 added_sets_2, new_mode))
3119 unsigned int regno = REGNO (newpat_dest);
3120 compare_mode = new_mode;
3121 if (regno < FIRST_PSEUDO_REGISTER)
3122 newpat_dest = gen_rtx_REG (compare_mode, regno);
3123 else
3125 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3126 newpat_dest = regno_reg_rtx[regno];
3129 #endif
3130 /* Cases for modifying the CC-using comparison. */
3131 if (compare_code != orig_compare_code
3132 /* ??? Do we need to verify the zero rtx? */
3133 && XEXP (*cc_use_loc, 1) == const0_rtx)
3135 /* Replace cc_use_loc with entire new RTX. */
3136 SUBST (*cc_use_loc,
3137 gen_rtx_fmt_ee (compare_code, compare_mode,
3138 newpat_dest, const0_rtx));
3139 undobuf.other_insn = cc_use_insn;
3141 else if (compare_mode != orig_compare_mode)
3143 /* Just replace the CC reg with a new mode. */
3144 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3145 undobuf.other_insn = cc_use_insn;
3149 /* Now we modify the current newpat:
3150 First, SET_DEST(newpat) is updated if the CC mode has been
3151 altered. For targets without SELECT_CC_MODE, this should be
3152 optimized away. */
3153 if (compare_mode != orig_compare_mode)
3154 SUBST (SET_DEST (newpat), newpat_dest);
3155 /* This is always done to propagate i2src into newpat. */
3156 SUBST (SET_SRC (newpat),
3157 gen_rtx_COMPARE (compare_mode, op0, op1));
3158 /* Create new version of i2pat if needed; the below PARALLEL
3159 creation needs this to work correctly. */
3160 if (! rtx_equal_p (i2src, op0))
3161 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3162 i2_is_used = 1;
3165 #endif
3167 if (i2_is_used == 0)
3169 /* It is possible that the source of I2 or I1 may be performing
3170 an unneeded operation, such as a ZERO_EXTEND of something
3171 that is known to have the high part zero. Handle that case
3172 by letting subst look at the inner insns.
3174 Another way to do this would be to have a function that tries
3175 to simplify a single insn instead of merging two or more
3176 insns. We don't do this because of the potential of infinite
3177 loops and because of the potential extra memory required.
3178 However, doing it the way we are is a bit of a kludge and
3179 doesn't catch all cases.
3181 But only do this if -fexpensive-optimizations since it slows
3182 things down and doesn't usually win.
3184 This is not done in the COMPARE case above because the
3185 unmodified I2PAT is used in the PARALLEL and so a pattern
3186 with a modified I2SRC would not match. */
3188 if (flag_expensive_optimizations)
3190 /* Pass pc_rtx so no substitutions are done, just
3191 simplifications. */
3192 if (i1)
3194 subst_low_luid = DF_INSN_LUID (i1);
3195 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3198 subst_low_luid = DF_INSN_LUID (i2);
3199 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3202 n_occurrences = 0; /* `subst' counts here */
3203 subst_low_luid = DF_INSN_LUID (i2);
3205 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3206 copy of I2SRC each time we substitute it, in order to avoid creating
3207 self-referential RTL when we will be substituting I1SRC for I1DEST
3208 later. Likewise if I0 feeds into I2, either directly or indirectly
3209 through I1, and I0DEST is in I0SRC. */
3210 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3211 (i1_feeds_i2_n && i1dest_in_i1src)
3212 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3213 && i0dest_in_i0src));
3214 substed_i2 = 1;
3216 /* Record whether I2's body now appears within I3's body. */
3217 i2_is_used = n_occurrences;
3220 /* If we already got a failure, don't try to do more. Otherwise, try to
3221 substitute I1 if we have it. */
3223 if (i1 && GET_CODE (newpat) != CLOBBER)
3225 /* Check that an autoincrement side-effect on I1 has not been lost.
3226 This happens if I1DEST is mentioned in I2 and dies there, and
3227 has disappeared from the new pattern. */
3228 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3229 && i1_feeds_i2_n
3230 && dead_or_set_p (i2, i1dest)
3231 && !reg_overlap_mentioned_p (i1dest, newpat))
3232 /* Before we can do this substitution, we must redo the test done
3233 above (see detailed comments there) that ensures I1DEST isn't
3234 mentioned in any SETs in NEWPAT that are field assignments. */
3235 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3236 0, 0, 0))
3238 undo_all ();
3239 return 0;
3242 n_occurrences = 0;
3243 subst_low_luid = DF_INSN_LUID (i1);
3245 /* If the following substitution will modify I1SRC, make a copy of it
3246 for the case where it is substituted for I1DEST in I2PAT later. */
3247 if (added_sets_2 && i1_feeds_i2_n)
3248 i1src_copy = copy_rtx (i1src);
3250 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3251 copy of I1SRC each time we substitute it, in order to avoid creating
3252 self-referential RTL when we will be substituting I0SRC for I0DEST
3253 later. */
3254 newpat = subst (newpat, i1dest, i1src, 0, 0,
3255 i0_feeds_i1_n && i0dest_in_i0src);
3256 substed_i1 = 1;
3258 /* Record whether I1's body now appears within I3's body. */
3259 i1_is_used = n_occurrences;
3262 /* Likewise for I0 if we have it. */
3264 if (i0 && GET_CODE (newpat) != CLOBBER)
3266 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3267 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3268 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3269 && !reg_overlap_mentioned_p (i0dest, newpat))
3270 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3271 0, 0, 0))
3273 undo_all ();
3274 return 0;
3277 /* If the following substitution will modify I0SRC, make a copy of it
3278 for the case where it is substituted for I0DEST in I1PAT later. */
3279 if (added_sets_1 && i0_feeds_i1_n)
3280 i0src_copy = copy_rtx (i0src);
3281 /* And a copy for I0DEST in I2PAT substitution. */
3282 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3283 || (i0_feeds_i2_n)))
3284 i0src_copy2 = copy_rtx (i0src);
3286 n_occurrences = 0;
3287 subst_low_luid = DF_INSN_LUID (i0);
3288 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3289 substed_i0 = 1;
3292 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3293 to count all the ways that I2SRC and I1SRC can be used. */
3294 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3295 && i2_is_used + added_sets_2 > 1)
3296 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3297 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3298 > 1))
3299 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3300 && (n_occurrences + added_sets_0
3301 + (added_sets_1 && i0_feeds_i1_n)
3302 + (added_sets_2 && i0_feeds_i2_n)
3303 > 1))
3304 /* Fail if we tried to make a new register. */
3305 || max_reg_num () != maxreg
3306 /* Fail if we couldn't do something and have a CLOBBER. */
3307 || GET_CODE (newpat) == CLOBBER
3308 /* Fail if this new pattern is a MULT and we didn't have one before
3309 at the outer level. */
3310 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3311 && ! have_mult))
3313 undo_all ();
3314 return 0;
3317 /* If the actions of the earlier insns must be kept
3318 in addition to substituting them into the latest one,
3319 we must make a new PARALLEL for the latest insn
3320 to hold additional the SETs. */
3322 if (added_sets_0 || added_sets_1 || added_sets_2)
3324 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3325 combine_extras++;
3327 if (GET_CODE (newpat) == PARALLEL)
3329 rtvec old = XVEC (newpat, 0);
3330 total_sets = XVECLEN (newpat, 0) + extra_sets;
3331 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3332 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3333 sizeof (old->elem[0]) * old->num_elem);
3335 else
3337 rtx old = newpat;
3338 total_sets = 1 + extra_sets;
3339 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3340 XVECEXP (newpat, 0, 0) = old;
3343 if (added_sets_0)
3344 XVECEXP (newpat, 0, --total_sets) = i0pat;
3346 if (added_sets_1)
3348 rtx t = i1pat;
3349 if (i0_feeds_i1_n)
3350 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3352 XVECEXP (newpat, 0, --total_sets) = t;
3354 if (added_sets_2)
3356 rtx t = i2pat;
3357 if (i1_feeds_i2_n)
3358 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3359 i0_feeds_i1_n && i0dest_in_i0src);
3360 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3361 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3363 XVECEXP (newpat, 0, --total_sets) = t;
3367 validate_replacement:
3369 /* Note which hard regs this insn has as inputs. */
3370 mark_used_regs_combine (newpat);
3372 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3373 consider splitting this pattern, we might need these clobbers. */
3374 if (i1 && GET_CODE (newpat) == PARALLEL
3375 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3377 int len = XVECLEN (newpat, 0);
3379 newpat_vec_with_clobbers = rtvec_alloc (len);
3380 for (i = 0; i < len; i++)
3381 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3384 /* We have recognized nothing yet. */
3385 insn_code_number = -1;
3387 /* See if this is a PARALLEL of two SETs where one SET's destination is
3388 a register that is unused and this isn't marked as an instruction that
3389 might trap in an EH region. In that case, we just need the other SET.
3390 We prefer this over the PARALLEL.
3392 This can occur when simplifying a divmod insn. We *must* test for this
3393 case here because the code below that splits two independent SETs doesn't
3394 handle this case correctly when it updates the register status.
3396 It's pointless doing this if we originally had two sets, one from
3397 i3, and one from i2. Combining then splitting the parallel results
3398 in the original i2 again plus an invalid insn (which we delete).
3399 The net effect is only to move instructions around, which makes
3400 debug info less accurate. */
3402 if (!(added_sets_2 && i1 == 0)
3403 && GET_CODE (newpat) == PARALLEL
3404 && XVECLEN (newpat, 0) == 2
3405 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3406 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3407 && asm_noperands (newpat) < 0)
3409 rtx set0 = XVECEXP (newpat, 0, 0);
3410 rtx set1 = XVECEXP (newpat, 0, 1);
3411 rtx oldpat = newpat;
3413 if (((REG_P (SET_DEST (set1))
3414 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3415 || (GET_CODE (SET_DEST (set1)) == SUBREG
3416 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3417 && insn_nothrow_p (i3)
3418 && !side_effects_p (SET_SRC (set1)))
3420 newpat = set0;
3421 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3424 else if (((REG_P (SET_DEST (set0))
3425 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3426 || (GET_CODE (SET_DEST (set0)) == SUBREG
3427 && find_reg_note (i3, REG_UNUSED,
3428 SUBREG_REG (SET_DEST (set0)))))
3429 && insn_nothrow_p (i3)
3430 && !side_effects_p (SET_SRC (set0)))
3432 newpat = set1;
3433 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3435 if (insn_code_number >= 0)
3436 changed_i3_dest = 1;
3439 if (insn_code_number < 0)
3440 newpat = oldpat;
3443 /* Is the result of combination a valid instruction? */
3444 if (insn_code_number < 0)
3445 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3447 /* If we were combining three insns and the result is a simple SET
3448 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3449 insns. There are two ways to do this. It can be split using a
3450 machine-specific method (like when you have an addition of a large
3451 constant) or by combine in the function find_split_point. */
3453 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3454 && asm_noperands (newpat) < 0)
3456 rtx parallel, *split;
3457 rtx_insn *m_split_insn;
3459 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3460 use I2DEST as a scratch register will help. In the latter case,
3461 convert I2DEST to the mode of the source of NEWPAT if we can. */
3463 m_split_insn = combine_split_insns (newpat, i3);
3465 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3466 inputs of NEWPAT. */
3468 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3469 possible to try that as a scratch reg. This would require adding
3470 more code to make it work though. */
3472 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3474 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3476 /* First try to split using the original register as a
3477 scratch register. */
3478 parallel = gen_rtx_PARALLEL (VOIDmode,
3479 gen_rtvec (2, newpat,
3480 gen_rtx_CLOBBER (VOIDmode,
3481 i2dest)));
3482 m_split_insn = combine_split_insns (parallel, i3);
3484 /* If that didn't work, try changing the mode of I2DEST if
3485 we can. */
3486 if (m_split_insn == 0
3487 && new_mode != GET_MODE (i2dest)
3488 && new_mode != VOIDmode
3489 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3491 machine_mode old_mode = GET_MODE (i2dest);
3492 rtx ni2dest;
3494 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3495 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3496 else
3498 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3499 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3502 parallel = (gen_rtx_PARALLEL
3503 (VOIDmode,
3504 gen_rtvec (2, newpat,
3505 gen_rtx_CLOBBER (VOIDmode,
3506 ni2dest))));
3507 m_split_insn = combine_split_insns (parallel, i3);
3509 if (m_split_insn == 0
3510 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3512 struct undo *buf;
3514 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3515 buf = undobuf.undos;
3516 undobuf.undos = buf->next;
3517 buf->next = undobuf.frees;
3518 undobuf.frees = buf;
3522 i2scratch = m_split_insn != 0;
3525 /* If recog_for_combine has discarded clobbers, try to use them
3526 again for the split. */
3527 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3529 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3530 m_split_insn = combine_split_insns (parallel, i3);
3533 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3535 rtx m_split_pat = PATTERN (m_split_insn);
3536 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3537 if (insn_code_number >= 0)
3538 newpat = m_split_pat;
3540 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3541 && (next_nonnote_nondebug_insn (i2) == i3
3542 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3544 rtx i2set, i3set;
3545 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3546 newi2pat = PATTERN (m_split_insn);
3548 i3set = single_set (NEXT_INSN (m_split_insn));
3549 i2set = single_set (m_split_insn);
3551 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3553 /* If I2 or I3 has multiple SETs, we won't know how to track
3554 register status, so don't use these insns. If I2's destination
3555 is used between I2 and I3, we also can't use these insns. */
3557 if (i2_code_number >= 0 && i2set && i3set
3558 && (next_nonnote_nondebug_insn (i2) == i3
3559 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3560 insn_code_number = recog_for_combine (&newi3pat, i3,
3561 &new_i3_notes);
3562 if (insn_code_number >= 0)
3563 newpat = newi3pat;
3565 /* It is possible that both insns now set the destination of I3.
3566 If so, we must show an extra use of it. */
3568 if (insn_code_number >= 0)
3570 rtx new_i3_dest = SET_DEST (i3set);
3571 rtx new_i2_dest = SET_DEST (i2set);
3573 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3574 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3575 || GET_CODE (new_i3_dest) == SUBREG)
3576 new_i3_dest = XEXP (new_i3_dest, 0);
3578 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3579 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3580 || GET_CODE (new_i2_dest) == SUBREG)
3581 new_i2_dest = XEXP (new_i2_dest, 0);
3583 if (REG_P (new_i3_dest)
3584 && REG_P (new_i2_dest)
3585 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3586 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3590 /* If we can split it and use I2DEST, go ahead and see if that
3591 helps things be recognized. Verify that none of the registers
3592 are set between I2 and I3. */
3593 if (insn_code_number < 0
3594 && (split = find_split_point (&newpat, i3, false)) != 0
3595 #ifdef HAVE_cc0
3596 && REG_P (i2dest)
3597 #endif
3598 /* We need I2DEST in the proper mode. If it is a hard register
3599 or the only use of a pseudo, we can change its mode.
3600 Make sure we don't change a hard register to have a mode that
3601 isn't valid for it, or change the number of registers. */
3602 && (GET_MODE (*split) == GET_MODE (i2dest)
3603 || GET_MODE (*split) == VOIDmode
3604 || can_change_dest_mode (i2dest, added_sets_2,
3605 GET_MODE (*split)))
3606 && (next_nonnote_nondebug_insn (i2) == i3
3607 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3608 /* We can't overwrite I2DEST if its value is still used by
3609 NEWPAT. */
3610 && ! reg_referenced_p (i2dest, newpat))
3612 rtx newdest = i2dest;
3613 enum rtx_code split_code = GET_CODE (*split);
3614 machine_mode split_mode = GET_MODE (*split);
3615 bool subst_done = false;
3616 newi2pat = NULL_RTX;
3618 i2scratch = true;
3620 /* *SPLIT may be part of I2SRC, so make sure we have the
3621 original expression around for later debug processing.
3622 We should not need I2SRC any more in other cases. */
3623 if (MAY_HAVE_DEBUG_INSNS)
3624 i2src = copy_rtx (i2src);
3625 else
3626 i2src = NULL;
3628 /* Get NEWDEST as a register in the proper mode. We have already
3629 validated that we can do this. */
3630 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3632 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3633 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3634 else
3636 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3637 newdest = regno_reg_rtx[REGNO (i2dest)];
3641 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3642 an ASHIFT. This can occur if it was inside a PLUS and hence
3643 appeared to be a memory address. This is a kludge. */
3644 if (split_code == MULT
3645 && CONST_INT_P (XEXP (*split, 1))
3646 && INTVAL (XEXP (*split, 1)) > 0
3647 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3649 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3650 XEXP (*split, 0), GEN_INT (i)));
3651 /* Update split_code because we may not have a multiply
3652 anymore. */
3653 split_code = GET_CODE (*split);
3656 #ifdef INSN_SCHEDULING
3657 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3658 be written as a ZERO_EXTEND. */
3659 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3661 #ifdef LOAD_EXTEND_OP
3662 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3663 what it really is. */
3664 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3665 == SIGN_EXTEND)
3666 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3667 SUBREG_REG (*split)));
3668 else
3669 #endif
3670 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3671 SUBREG_REG (*split)));
3673 #endif
3675 /* Attempt to split binary operators using arithmetic identities. */
3676 if (BINARY_P (SET_SRC (newpat))
3677 && split_mode == GET_MODE (SET_SRC (newpat))
3678 && ! side_effects_p (SET_SRC (newpat)))
3680 rtx setsrc = SET_SRC (newpat);
3681 machine_mode mode = GET_MODE (setsrc);
3682 enum rtx_code code = GET_CODE (setsrc);
3683 rtx src_op0 = XEXP (setsrc, 0);
3684 rtx src_op1 = XEXP (setsrc, 1);
3686 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3687 if (rtx_equal_p (src_op0, src_op1))
3689 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3690 SUBST (XEXP (setsrc, 0), newdest);
3691 SUBST (XEXP (setsrc, 1), newdest);
3692 subst_done = true;
3694 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3695 else if ((code == PLUS || code == MULT)
3696 && GET_CODE (src_op0) == code
3697 && GET_CODE (XEXP (src_op0, 0)) == code
3698 && (INTEGRAL_MODE_P (mode)
3699 || (FLOAT_MODE_P (mode)
3700 && flag_unsafe_math_optimizations)))
3702 rtx p = XEXP (XEXP (src_op0, 0), 0);
3703 rtx q = XEXP (XEXP (src_op0, 0), 1);
3704 rtx r = XEXP (src_op0, 1);
3705 rtx s = src_op1;
3707 /* Split both "((X op Y) op X) op Y" and
3708 "((X op Y) op Y) op X" as "T op T" where T is
3709 "X op Y". */
3710 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3711 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3713 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3714 XEXP (src_op0, 0));
3715 SUBST (XEXP (setsrc, 0), newdest);
3716 SUBST (XEXP (setsrc, 1), newdest);
3717 subst_done = true;
3719 /* Split "((X op X) op Y) op Y)" as "T op T" where
3720 T is "X op Y". */
3721 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3723 rtx tmp = simplify_gen_binary (code, mode, p, r);
3724 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3725 SUBST (XEXP (setsrc, 0), newdest);
3726 SUBST (XEXP (setsrc, 1), newdest);
3727 subst_done = true;
3732 if (!subst_done)
3734 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3735 SUBST (*split, newdest);
3738 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3740 /* recog_for_combine might have added CLOBBERs to newi2pat.
3741 Make sure NEWPAT does not depend on the clobbered regs. */
3742 if (GET_CODE (newi2pat) == PARALLEL)
3743 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3744 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3746 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3747 if (reg_overlap_mentioned_p (reg, newpat))
3749 undo_all ();
3750 return 0;
3754 /* If the split point was a MULT and we didn't have one before,
3755 don't use one now. */
3756 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3757 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3761 /* Check for a case where we loaded from memory in a narrow mode and
3762 then sign extended it, but we need both registers. In that case,
3763 we have a PARALLEL with both loads from the same memory location.
3764 We can split this into a load from memory followed by a register-register
3765 copy. This saves at least one insn, more if register allocation can
3766 eliminate the copy.
3768 We cannot do this if the destination of the first assignment is a
3769 condition code register or cc0. We eliminate this case by making sure
3770 the SET_DEST and SET_SRC have the same mode.
3772 We cannot do this if the destination of the second assignment is
3773 a register that we have already assumed is zero-extended. Similarly
3774 for a SUBREG of such a register. */
3776 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3777 && GET_CODE (newpat) == PARALLEL
3778 && XVECLEN (newpat, 0) == 2
3779 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3780 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3781 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3782 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3783 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3784 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3785 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3786 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3787 DF_INSN_LUID (i2))
3788 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3789 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3790 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3791 (REG_P (temp_expr)
3792 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3793 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3794 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3795 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3796 != GET_MODE_MASK (word_mode))))
3797 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3798 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3799 (REG_P (temp_expr)
3800 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3801 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3802 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3803 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3804 != GET_MODE_MASK (word_mode)))))
3805 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3806 SET_SRC (XVECEXP (newpat, 0, 1)))
3807 && ! find_reg_note (i3, REG_UNUSED,
3808 SET_DEST (XVECEXP (newpat, 0, 0))))
3810 rtx ni2dest;
3812 newi2pat = XVECEXP (newpat, 0, 0);
3813 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3814 newpat = XVECEXP (newpat, 0, 1);
3815 SUBST (SET_SRC (newpat),
3816 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3817 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3819 if (i2_code_number >= 0)
3820 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3822 if (insn_code_number >= 0)
3823 swap_i2i3 = 1;
3826 /* Similarly, check for a case where we have a PARALLEL of two independent
3827 SETs but we started with three insns. In this case, we can do the sets
3828 as two separate insns. This case occurs when some SET allows two
3829 other insns to combine, but the destination of that SET is still live.
3831 Also do this if we started with two insns and (at least) one of the
3832 resulting sets is a noop; this noop will be deleted later. */
3834 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3835 && GET_CODE (newpat) == PARALLEL
3836 && XVECLEN (newpat, 0) == 2
3837 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3838 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3839 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3840 || set_noop_p (XVECEXP (newpat, 0, 1)))
3841 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3842 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3843 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3844 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3845 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3846 XVECEXP (newpat, 0, 0))
3847 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3848 XVECEXP (newpat, 0, 1))
3849 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3850 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3852 rtx set0 = XVECEXP (newpat, 0, 0);
3853 rtx set1 = XVECEXP (newpat, 0, 1);
3855 /* Normally, it doesn't matter which of the two is done first,
3856 but the one that references cc0 can't be the second, and
3857 one which uses any regs/memory set in between i2 and i3 can't
3858 be first. The PARALLEL might also have been pre-existing in i3,
3859 so we need to make sure that we won't wrongly hoist a SET to i2
3860 that would conflict with a death note present in there. */
3861 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3862 && !(REG_P (SET_DEST (set1))
3863 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3864 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3865 && find_reg_note (i2, REG_DEAD,
3866 SUBREG_REG (SET_DEST (set1))))
3867 #ifdef HAVE_cc0
3868 && !reg_referenced_p (cc0_rtx, set0)
3869 #endif
3870 /* If I3 is a jump, ensure that set0 is a jump so that
3871 we do not create invalid RTL. */
3872 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3875 newi2pat = set1;
3876 newpat = set0;
3878 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3879 && !(REG_P (SET_DEST (set0))
3880 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3881 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3882 && find_reg_note (i2, REG_DEAD,
3883 SUBREG_REG (SET_DEST (set0))))
3884 #ifdef HAVE_cc0
3885 && !reg_referenced_p (cc0_rtx, set1)
3886 #endif
3887 /* If I3 is a jump, ensure that set1 is a jump so that
3888 we do not create invalid RTL. */
3889 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3892 newi2pat = set0;
3893 newpat = set1;
3895 else
3897 undo_all ();
3898 return 0;
3901 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3903 if (i2_code_number >= 0)
3905 /* recog_for_combine might have added CLOBBERs to newi2pat.
3906 Make sure NEWPAT does not depend on the clobbered regs. */
3907 if (GET_CODE (newi2pat) == PARALLEL)
3909 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3910 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3912 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3913 if (reg_overlap_mentioned_p (reg, newpat))
3915 undo_all ();
3916 return 0;
3921 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3925 /* If it still isn't recognized, fail and change things back the way they
3926 were. */
3927 if ((insn_code_number < 0
3928 /* Is the result a reasonable ASM_OPERANDS? */
3929 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3931 undo_all ();
3932 return 0;
3935 /* If we had to change another insn, make sure it is valid also. */
3936 if (undobuf.other_insn)
3938 CLEAR_HARD_REG_SET (newpat_used_regs);
3940 other_pat = PATTERN (undobuf.other_insn);
3941 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3942 &new_other_notes);
3944 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3946 undo_all ();
3947 return 0;
3951 #ifdef HAVE_cc0
3952 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3953 they are adjacent to each other or not. */
3955 rtx_insn *p = prev_nonnote_insn (i3);
3956 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3957 && sets_cc0_p (newi2pat))
3959 undo_all ();
3960 return 0;
3963 #endif
3965 /* Only allow this combination if insn_rtx_costs reports that the
3966 replacement instructions are cheaper than the originals. */
3967 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3969 undo_all ();
3970 return 0;
3973 if (MAY_HAVE_DEBUG_INSNS)
3975 struct undo *undo;
3977 for (undo = undobuf.undos; undo; undo = undo->next)
3978 if (undo->kind == UNDO_MODE)
3980 rtx reg = *undo->where.r;
3981 machine_mode new_mode = GET_MODE (reg);
3982 machine_mode old_mode = undo->old_contents.m;
3984 /* Temporarily revert mode back. */
3985 adjust_reg_mode (reg, old_mode);
3987 if (reg == i2dest && i2scratch)
3989 /* If we used i2dest as a scratch register with a
3990 different mode, substitute it for the original
3991 i2src while its original mode is temporarily
3992 restored, and then clear i2scratch so that we don't
3993 do it again later. */
3994 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3995 this_basic_block);
3996 i2scratch = false;
3997 /* Put back the new mode. */
3998 adjust_reg_mode (reg, new_mode);
4000 else
4002 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4003 rtx_insn *first, *last;
4005 if (reg == i2dest)
4007 first = i2;
4008 last = last_combined_insn;
4010 else
4012 first = i3;
4013 last = undobuf.other_insn;
4014 gcc_assert (last);
4015 if (DF_INSN_LUID (last)
4016 < DF_INSN_LUID (last_combined_insn))
4017 last = last_combined_insn;
4020 /* We're dealing with a reg that changed mode but not
4021 meaning, so we want to turn it into a subreg for
4022 the new mode. However, because of REG sharing and
4023 because its mode had already changed, we have to do
4024 it in two steps. First, replace any debug uses of
4025 reg, with its original mode temporarily restored,
4026 with this copy we have created; then, replace the
4027 copy with the SUBREG of the original shared reg,
4028 once again changed to the new mode. */
4029 propagate_for_debug (first, last, reg, tempreg,
4030 this_basic_block);
4031 adjust_reg_mode (reg, new_mode);
4032 propagate_for_debug (first, last, tempreg,
4033 lowpart_subreg (old_mode, reg, new_mode),
4034 this_basic_block);
4039 /* If we will be able to accept this, we have made a
4040 change to the destination of I3. This requires us to
4041 do a few adjustments. */
4043 if (changed_i3_dest)
4045 PATTERN (i3) = newpat;
4046 adjust_for_new_dest (i3);
4049 /* We now know that we can do this combination. Merge the insns and
4050 update the status of registers and LOG_LINKS. */
4052 if (undobuf.other_insn)
4054 rtx note, next;
4056 PATTERN (undobuf.other_insn) = other_pat;
4058 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4059 ensure that they are still valid. Then add any non-duplicate
4060 notes added by recog_for_combine. */
4061 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4063 next = XEXP (note, 1);
4065 if ((REG_NOTE_KIND (note) == REG_DEAD
4066 && !reg_referenced_p (XEXP (note, 0),
4067 PATTERN (undobuf.other_insn)))
4068 ||(REG_NOTE_KIND (note) == REG_UNUSED
4069 && !reg_set_p (XEXP (note, 0),
4070 PATTERN (undobuf.other_insn))))
4071 remove_note (undobuf.other_insn, note);
4074 distribute_notes (new_other_notes, undobuf.other_insn,
4075 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4076 NULL_RTX);
4079 if (swap_i2i3)
4081 rtx_insn *insn;
4082 struct insn_link *link;
4083 rtx ni2dest;
4085 /* I3 now uses what used to be its destination and which is now
4086 I2's destination. This requires us to do a few adjustments. */
4087 PATTERN (i3) = newpat;
4088 adjust_for_new_dest (i3);
4090 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4091 so we still will.
4093 However, some later insn might be using I2's dest and have
4094 a LOG_LINK pointing at I3. We must remove this link.
4095 The simplest way to remove the link is to point it at I1,
4096 which we know will be a NOTE. */
4098 /* newi2pat is usually a SET here; however, recog_for_combine might
4099 have added some clobbers. */
4100 if (GET_CODE (newi2pat) == PARALLEL)
4101 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4102 else
4103 ni2dest = SET_DEST (newi2pat);
4105 for (insn = NEXT_INSN (i3);
4106 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4107 || insn != BB_HEAD (this_basic_block->next_bb));
4108 insn = NEXT_INSN (insn))
4110 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4112 FOR_EACH_LOG_LINK (link, insn)
4113 if (link->insn == i3)
4114 link->insn = i1;
4116 break;
4122 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4123 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4124 rtx midnotes = 0;
4125 int from_luid;
4126 /* Compute which registers we expect to eliminate. newi2pat may be setting
4127 either i3dest or i2dest, so we must check it. */
4128 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4129 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4130 || !i2dest_killed
4131 ? 0 : i2dest);
4132 /* For i1, we need to compute both local elimination and global
4133 elimination information with respect to newi2pat because i1dest
4134 may be the same as i3dest, in which case newi2pat may be setting
4135 i1dest. Global information is used when distributing REG_DEAD
4136 note for i2 and i3, in which case it does matter if newi2pat sets
4137 i1dest or not.
4139 Local information is used when distributing REG_DEAD note for i1,
4140 in which case it doesn't matter if newi2pat sets i1dest or not.
4141 See PR62151, if we have four insns combination:
4142 i0: r0 <- i0src
4143 i1: r1 <- i1src (using r0)
4144 REG_DEAD (r0)
4145 i2: r0 <- i2src (using r1)
4146 i3: r3 <- i3src (using r0)
4147 ix: using r0
4148 From i1's point of view, r0 is eliminated, no matter if it is set
4149 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4150 should be discarded.
4152 Note local information only affects cases in forms like "I1->I2->I3",
4153 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4154 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4155 i0dest anyway. */
4156 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4157 || !i1dest_killed
4158 ? 0 : i1dest);
4159 rtx elim_i1 = (local_elim_i1 == 0
4160 || (newi2pat && reg_set_p (i1dest, newi2pat))
4161 ? 0 : i1dest);
4162 /* Same case as i1. */
4163 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4164 ? 0 : i0dest);
4165 rtx elim_i0 = (local_elim_i0 == 0
4166 || (newi2pat && reg_set_p (i0dest, newi2pat))
4167 ? 0 : i0dest);
4169 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4170 clear them. */
4171 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4172 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4173 if (i1)
4174 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4175 if (i0)
4176 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4178 /* Ensure that we do not have something that should not be shared but
4179 occurs multiple times in the new insns. Check this by first
4180 resetting all the `used' flags and then copying anything is shared. */
4182 reset_used_flags (i3notes);
4183 reset_used_flags (i2notes);
4184 reset_used_flags (i1notes);
4185 reset_used_flags (i0notes);
4186 reset_used_flags (newpat);
4187 reset_used_flags (newi2pat);
4188 if (undobuf.other_insn)
4189 reset_used_flags (PATTERN (undobuf.other_insn));
4191 i3notes = copy_rtx_if_shared (i3notes);
4192 i2notes = copy_rtx_if_shared (i2notes);
4193 i1notes = copy_rtx_if_shared (i1notes);
4194 i0notes = copy_rtx_if_shared (i0notes);
4195 newpat = copy_rtx_if_shared (newpat);
4196 newi2pat = copy_rtx_if_shared (newi2pat);
4197 if (undobuf.other_insn)
4198 reset_used_flags (PATTERN (undobuf.other_insn));
4200 INSN_CODE (i3) = insn_code_number;
4201 PATTERN (i3) = newpat;
4203 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4205 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4207 reset_used_flags (call_usage);
4208 call_usage = copy_rtx (call_usage);
4210 if (substed_i2)
4212 /* I2SRC must still be meaningful at this point. Some splitting
4213 operations can invalidate I2SRC, but those operations do not
4214 apply to calls. */
4215 gcc_assert (i2src);
4216 replace_rtx (call_usage, i2dest, i2src);
4219 if (substed_i1)
4220 replace_rtx (call_usage, i1dest, i1src);
4221 if (substed_i0)
4222 replace_rtx (call_usage, i0dest, i0src);
4224 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4227 if (undobuf.other_insn)
4228 INSN_CODE (undobuf.other_insn) = other_code_number;
4230 /* We had one special case above where I2 had more than one set and
4231 we replaced a destination of one of those sets with the destination
4232 of I3. In that case, we have to update LOG_LINKS of insns later
4233 in this basic block. Note that this (expensive) case is rare.
4235 Also, in this case, we must pretend that all REG_NOTEs for I2
4236 actually came from I3, so that REG_UNUSED notes from I2 will be
4237 properly handled. */
4239 if (i3_subst_into_i2)
4241 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4242 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4243 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4244 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4245 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4246 && ! find_reg_note (i2, REG_UNUSED,
4247 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4248 for (temp_insn = NEXT_INSN (i2);
4249 temp_insn
4250 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4251 || BB_HEAD (this_basic_block) != temp_insn);
4252 temp_insn = NEXT_INSN (temp_insn))
4253 if (temp_insn != i3 && INSN_P (temp_insn))
4254 FOR_EACH_LOG_LINK (link, temp_insn)
4255 if (link->insn == i2)
4256 link->insn = i3;
4258 if (i3notes)
4260 rtx link = i3notes;
4261 while (XEXP (link, 1))
4262 link = XEXP (link, 1);
4263 XEXP (link, 1) = i2notes;
4265 else
4266 i3notes = i2notes;
4267 i2notes = 0;
4270 LOG_LINKS (i3) = NULL;
4271 REG_NOTES (i3) = 0;
4272 LOG_LINKS (i2) = NULL;
4273 REG_NOTES (i2) = 0;
4275 if (newi2pat)
4277 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4278 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4279 this_basic_block);
4280 INSN_CODE (i2) = i2_code_number;
4281 PATTERN (i2) = newi2pat;
4283 else
4285 if (MAY_HAVE_DEBUG_INSNS && i2src)
4286 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4287 this_basic_block);
4288 SET_INSN_DELETED (i2);
4291 if (i1)
4293 LOG_LINKS (i1) = NULL;
4294 REG_NOTES (i1) = 0;
4295 if (MAY_HAVE_DEBUG_INSNS)
4296 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4297 this_basic_block);
4298 SET_INSN_DELETED (i1);
4301 if (i0)
4303 LOG_LINKS (i0) = NULL;
4304 REG_NOTES (i0) = 0;
4305 if (MAY_HAVE_DEBUG_INSNS)
4306 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4307 this_basic_block);
4308 SET_INSN_DELETED (i0);
4311 /* Get death notes for everything that is now used in either I3 or
4312 I2 and used to die in a previous insn. If we built two new
4313 patterns, move from I1 to I2 then I2 to I3 so that we get the
4314 proper movement on registers that I2 modifies. */
4316 if (i0)
4317 from_luid = DF_INSN_LUID (i0);
4318 else if (i1)
4319 from_luid = DF_INSN_LUID (i1);
4320 else
4321 from_luid = DF_INSN_LUID (i2);
4322 if (newi2pat)
4323 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4324 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4326 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4327 if (i3notes)
4328 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4329 elim_i2, elim_i1, elim_i0);
4330 if (i2notes)
4331 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4332 elim_i2, elim_i1, elim_i0);
4333 if (i1notes)
4334 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4335 elim_i2, local_elim_i1, local_elim_i0);
4336 if (i0notes)
4337 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4338 elim_i2, elim_i1, local_elim_i0);
4339 if (midnotes)
4340 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4341 elim_i2, elim_i1, elim_i0);
4343 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4344 know these are REG_UNUSED and want them to go to the desired insn,
4345 so we always pass it as i3. */
4347 if (newi2pat && new_i2_notes)
4348 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4349 NULL_RTX);
4351 if (new_i3_notes)
4352 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4353 NULL_RTX);
4355 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4356 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4357 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4358 in that case, it might delete I2. Similarly for I2 and I1.
4359 Show an additional death due to the REG_DEAD note we make here. If
4360 we discard it in distribute_notes, we will decrement it again. */
4362 if (i3dest_killed)
4364 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4365 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4366 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4367 elim_i1, elim_i0);
4368 else
4369 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4370 elim_i2, elim_i1, elim_i0);
4373 if (i2dest_in_i2src)
4375 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4376 if (newi2pat && reg_set_p (i2dest, newi2pat))
4377 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4378 NULL_RTX, NULL_RTX);
4379 else
4380 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4381 NULL_RTX, NULL_RTX, NULL_RTX);
4384 if (i1dest_in_i1src)
4386 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4387 if (newi2pat && reg_set_p (i1dest, newi2pat))
4388 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4389 NULL_RTX, NULL_RTX);
4390 else
4391 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4392 NULL_RTX, NULL_RTX, NULL_RTX);
4395 if (i0dest_in_i0src)
4397 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4398 if (newi2pat && reg_set_p (i0dest, newi2pat))
4399 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4400 NULL_RTX, NULL_RTX);
4401 else
4402 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4403 NULL_RTX, NULL_RTX, NULL_RTX);
4406 distribute_links (i3links);
4407 distribute_links (i2links);
4408 distribute_links (i1links);
4409 distribute_links (i0links);
4411 if (REG_P (i2dest))
4413 struct insn_link *link;
4414 rtx_insn *i2_insn = 0;
4415 rtx i2_val = 0, set;
4417 /* The insn that used to set this register doesn't exist, and
4418 this life of the register may not exist either. See if one of
4419 I3's links points to an insn that sets I2DEST. If it does,
4420 that is now the last known value for I2DEST. If we don't update
4421 this and I2 set the register to a value that depended on its old
4422 contents, we will get confused. If this insn is used, thing
4423 will be set correctly in combine_instructions. */
4424 FOR_EACH_LOG_LINK (link, i3)
4425 if ((set = single_set (link->insn)) != 0
4426 && rtx_equal_p (i2dest, SET_DEST (set)))
4427 i2_insn = link->insn, i2_val = SET_SRC (set);
4429 record_value_for_reg (i2dest, i2_insn, i2_val);
4431 /* If the reg formerly set in I2 died only once and that was in I3,
4432 zero its use count so it won't make `reload' do any work. */
4433 if (! added_sets_2
4434 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4435 && ! i2dest_in_i2src)
4436 INC_REG_N_SETS (REGNO (i2dest), -1);
4439 if (i1 && REG_P (i1dest))
4441 struct insn_link *link;
4442 rtx_insn *i1_insn = 0;
4443 rtx i1_val = 0, set;
4445 FOR_EACH_LOG_LINK (link, i3)
4446 if ((set = single_set (link->insn)) != 0
4447 && rtx_equal_p (i1dest, SET_DEST (set)))
4448 i1_insn = link->insn, i1_val = SET_SRC (set);
4450 record_value_for_reg (i1dest, i1_insn, i1_val);
4452 if (! added_sets_1 && ! i1dest_in_i1src)
4453 INC_REG_N_SETS (REGNO (i1dest), -1);
4456 if (i0 && REG_P (i0dest))
4458 struct insn_link *link;
4459 rtx_insn *i0_insn = 0;
4460 rtx i0_val = 0, set;
4462 FOR_EACH_LOG_LINK (link, i3)
4463 if ((set = single_set (link->insn)) != 0
4464 && rtx_equal_p (i0dest, SET_DEST (set)))
4465 i0_insn = link->insn, i0_val = SET_SRC (set);
4467 record_value_for_reg (i0dest, i0_insn, i0_val);
4469 if (! added_sets_0 && ! i0dest_in_i0src)
4470 INC_REG_N_SETS (REGNO (i0dest), -1);
4473 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4474 been made to this insn. The order is important, because newi2pat
4475 can affect nonzero_bits of newpat. */
4476 if (newi2pat)
4477 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4478 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4481 if (undobuf.other_insn != NULL_RTX)
4483 if (dump_file)
4485 fprintf (dump_file, "modifying other_insn ");
4486 dump_insn_slim (dump_file, undobuf.other_insn);
4488 df_insn_rescan (undobuf.other_insn);
4491 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4493 if (dump_file)
4495 fprintf (dump_file, "modifying insn i0 ");
4496 dump_insn_slim (dump_file, i0);
4498 df_insn_rescan (i0);
4501 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4503 if (dump_file)
4505 fprintf (dump_file, "modifying insn i1 ");
4506 dump_insn_slim (dump_file, i1);
4508 df_insn_rescan (i1);
4511 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4513 if (dump_file)
4515 fprintf (dump_file, "modifying insn i2 ");
4516 dump_insn_slim (dump_file, i2);
4518 df_insn_rescan (i2);
4521 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4523 if (dump_file)
4525 fprintf (dump_file, "modifying insn i3 ");
4526 dump_insn_slim (dump_file, i3);
4528 df_insn_rescan (i3);
4531 /* Set new_direct_jump_p if a new return or simple jump instruction
4532 has been created. Adjust the CFG accordingly. */
4533 if (returnjump_p (i3) || any_uncondjump_p (i3))
4535 *new_direct_jump_p = 1;
4536 mark_jump_label (PATTERN (i3), i3, 0);
4537 update_cfg_for_uncondjump (i3);
4540 if (undobuf.other_insn != NULL_RTX
4541 && (returnjump_p (undobuf.other_insn)
4542 || any_uncondjump_p (undobuf.other_insn)))
4544 *new_direct_jump_p = 1;
4545 update_cfg_for_uncondjump (undobuf.other_insn);
4548 /* A noop might also need cleaning up of CFG, if it comes from the
4549 simplification of a jump. */
4550 if (JUMP_P (i3)
4551 && GET_CODE (newpat) == SET
4552 && SET_SRC (newpat) == pc_rtx
4553 && SET_DEST (newpat) == pc_rtx)
4555 *new_direct_jump_p = 1;
4556 update_cfg_for_uncondjump (i3);
4559 if (undobuf.other_insn != NULL_RTX
4560 && JUMP_P (undobuf.other_insn)
4561 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4562 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4563 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4565 *new_direct_jump_p = 1;
4566 update_cfg_for_uncondjump (undobuf.other_insn);
4569 combine_successes++;
4570 undo_commit ();
4572 if (added_links_insn
4573 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4574 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4575 return added_links_insn;
4576 else
4577 return newi2pat ? i2 : i3;
4580 /* Undo all the modifications recorded in undobuf. */
4582 static void
4583 undo_all (void)
4585 struct undo *undo, *next;
4587 for (undo = undobuf.undos; undo; undo = next)
4589 next = undo->next;
4590 switch (undo->kind)
4592 case UNDO_RTX:
4593 *undo->where.r = undo->old_contents.r;
4594 break;
4595 case UNDO_INT:
4596 *undo->where.i = undo->old_contents.i;
4597 break;
4598 case UNDO_MODE:
4599 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4600 break;
4601 case UNDO_LINKS:
4602 *undo->where.l = undo->old_contents.l;
4603 break;
4604 default:
4605 gcc_unreachable ();
4608 undo->next = undobuf.frees;
4609 undobuf.frees = undo;
4612 undobuf.undos = 0;
4615 /* We've committed to accepting the changes we made. Move all
4616 of the undos to the free list. */
4618 static void
4619 undo_commit (void)
4621 struct undo *undo, *next;
4623 for (undo = undobuf.undos; undo; undo = next)
4625 next = undo->next;
4626 undo->next = undobuf.frees;
4627 undobuf.frees = undo;
4629 undobuf.undos = 0;
4632 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4633 where we have an arithmetic expression and return that point. LOC will
4634 be inside INSN.
4636 try_combine will call this function to see if an insn can be split into
4637 two insns. */
4639 static rtx *
4640 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4642 rtx x = *loc;
4643 enum rtx_code code = GET_CODE (x);
4644 rtx *split;
4645 unsigned HOST_WIDE_INT len = 0;
4646 HOST_WIDE_INT pos = 0;
4647 int unsignedp = 0;
4648 rtx inner = NULL_RTX;
4650 /* First special-case some codes. */
4651 switch (code)
4653 case SUBREG:
4654 #ifdef INSN_SCHEDULING
4655 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4656 point. */
4657 if (MEM_P (SUBREG_REG (x)))
4658 return loc;
4659 #endif
4660 return find_split_point (&SUBREG_REG (x), insn, false);
4662 case MEM:
4663 #ifdef HAVE_lo_sum
4664 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4665 using LO_SUM and HIGH. */
4666 if (GET_CODE (XEXP (x, 0)) == CONST
4667 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4669 machine_mode address_mode = get_address_mode (x);
4671 SUBST (XEXP (x, 0),
4672 gen_rtx_LO_SUM (address_mode,
4673 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4674 XEXP (x, 0)));
4675 return &XEXP (XEXP (x, 0), 0);
4677 #endif
4679 /* If we have a PLUS whose second operand is a constant and the
4680 address is not valid, perhaps will can split it up using
4681 the machine-specific way to split large constants. We use
4682 the first pseudo-reg (one of the virtual regs) as a placeholder;
4683 it will not remain in the result. */
4684 if (GET_CODE (XEXP (x, 0)) == PLUS
4685 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4686 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4687 MEM_ADDR_SPACE (x)))
4689 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4690 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4691 XEXP (x, 0)),
4692 subst_insn);
4694 /* This should have produced two insns, each of which sets our
4695 placeholder. If the source of the second is a valid address,
4696 we can make put both sources together and make a split point
4697 in the middle. */
4699 if (seq
4700 && NEXT_INSN (seq) != NULL_RTX
4701 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4702 && NONJUMP_INSN_P (seq)
4703 && GET_CODE (PATTERN (seq)) == SET
4704 && SET_DEST (PATTERN (seq)) == reg
4705 && ! reg_mentioned_p (reg,
4706 SET_SRC (PATTERN (seq)))
4707 && NONJUMP_INSN_P (NEXT_INSN (seq))
4708 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4709 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4710 && memory_address_addr_space_p
4711 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4712 MEM_ADDR_SPACE (x)))
4714 rtx src1 = SET_SRC (PATTERN (seq));
4715 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4717 /* Replace the placeholder in SRC2 with SRC1. If we can
4718 find where in SRC2 it was placed, that can become our
4719 split point and we can replace this address with SRC2.
4720 Just try two obvious places. */
4722 src2 = replace_rtx (src2, reg, src1);
4723 split = 0;
4724 if (XEXP (src2, 0) == src1)
4725 split = &XEXP (src2, 0);
4726 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4727 && XEXP (XEXP (src2, 0), 0) == src1)
4728 split = &XEXP (XEXP (src2, 0), 0);
4730 if (split)
4732 SUBST (XEXP (x, 0), src2);
4733 return split;
4737 /* If that didn't work, perhaps the first operand is complex and
4738 needs to be computed separately, so make a split point there.
4739 This will occur on machines that just support REG + CONST
4740 and have a constant moved through some previous computation. */
4742 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4743 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4744 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4745 return &XEXP (XEXP (x, 0), 0);
4748 /* If we have a PLUS whose first operand is complex, try computing it
4749 separately by making a split there. */
4750 if (GET_CODE (XEXP (x, 0)) == PLUS
4751 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4752 MEM_ADDR_SPACE (x))
4753 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4754 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4755 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4756 return &XEXP (XEXP (x, 0), 0);
4757 break;
4759 case SET:
4760 #ifdef HAVE_cc0
4761 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4762 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4763 we need to put the operand into a register. So split at that
4764 point. */
4766 if (SET_DEST (x) == cc0_rtx
4767 && GET_CODE (SET_SRC (x)) != COMPARE
4768 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4769 && !OBJECT_P (SET_SRC (x))
4770 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4771 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4772 return &SET_SRC (x);
4773 #endif
4775 /* See if we can split SET_SRC as it stands. */
4776 split = find_split_point (&SET_SRC (x), insn, true);
4777 if (split && split != &SET_SRC (x))
4778 return split;
4780 /* See if we can split SET_DEST as it stands. */
4781 split = find_split_point (&SET_DEST (x), insn, false);
4782 if (split && split != &SET_DEST (x))
4783 return split;
4785 /* See if this is a bitfield assignment with everything constant. If
4786 so, this is an IOR of an AND, so split it into that. */
4787 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4788 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4789 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4790 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4791 && CONST_INT_P (SET_SRC (x))
4792 && ((INTVAL (XEXP (SET_DEST (x), 1))
4793 + INTVAL (XEXP (SET_DEST (x), 2)))
4794 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4795 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4797 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4798 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4799 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4800 rtx dest = XEXP (SET_DEST (x), 0);
4801 machine_mode mode = GET_MODE (dest);
4802 unsigned HOST_WIDE_INT mask
4803 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4804 rtx or_mask;
4806 if (BITS_BIG_ENDIAN)
4807 pos = GET_MODE_PRECISION (mode) - len - pos;
4809 or_mask = gen_int_mode (src << pos, mode);
4810 if (src == mask)
4811 SUBST (SET_SRC (x),
4812 simplify_gen_binary (IOR, mode, dest, or_mask));
4813 else
4815 rtx negmask = gen_int_mode (~(mask << pos), mode);
4816 SUBST (SET_SRC (x),
4817 simplify_gen_binary (IOR, mode,
4818 simplify_gen_binary (AND, mode,
4819 dest, negmask),
4820 or_mask));
4823 SUBST (SET_DEST (x), dest);
4825 split = find_split_point (&SET_SRC (x), insn, true);
4826 if (split && split != &SET_SRC (x))
4827 return split;
4830 /* Otherwise, see if this is an operation that we can split into two.
4831 If so, try to split that. */
4832 code = GET_CODE (SET_SRC (x));
4834 switch (code)
4836 case AND:
4837 /* If we are AND'ing with a large constant that is only a single
4838 bit and the result is only being used in a context where we
4839 need to know if it is zero or nonzero, replace it with a bit
4840 extraction. This will avoid the large constant, which might
4841 have taken more than one insn to make. If the constant were
4842 not a valid argument to the AND but took only one insn to make,
4843 this is no worse, but if it took more than one insn, it will
4844 be better. */
4846 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4847 && REG_P (XEXP (SET_SRC (x), 0))
4848 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4849 && REG_P (SET_DEST (x))
4850 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4851 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4852 && XEXP (*split, 0) == SET_DEST (x)
4853 && XEXP (*split, 1) == const0_rtx)
4855 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4856 XEXP (SET_SRC (x), 0),
4857 pos, NULL_RTX, 1, 1, 0, 0);
4858 if (extraction != 0)
4860 SUBST (SET_SRC (x), extraction);
4861 return find_split_point (loc, insn, false);
4864 break;
4866 case NE:
4867 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4868 is known to be on, this can be converted into a NEG of a shift. */
4869 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4870 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4871 && 1 <= (pos = exact_log2
4872 (nonzero_bits (XEXP (SET_SRC (x), 0),
4873 GET_MODE (XEXP (SET_SRC (x), 0))))))
4875 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4877 SUBST (SET_SRC (x),
4878 gen_rtx_NEG (mode,
4879 gen_rtx_LSHIFTRT (mode,
4880 XEXP (SET_SRC (x), 0),
4881 GEN_INT (pos))));
4883 split = find_split_point (&SET_SRC (x), insn, true);
4884 if (split && split != &SET_SRC (x))
4885 return split;
4887 break;
4889 case SIGN_EXTEND:
4890 inner = XEXP (SET_SRC (x), 0);
4892 /* We can't optimize if either mode is a partial integer
4893 mode as we don't know how many bits are significant
4894 in those modes. */
4895 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4896 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4897 break;
4899 pos = 0;
4900 len = GET_MODE_PRECISION (GET_MODE (inner));
4901 unsignedp = 0;
4902 break;
4904 case SIGN_EXTRACT:
4905 case ZERO_EXTRACT:
4906 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4907 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4909 inner = XEXP (SET_SRC (x), 0);
4910 len = INTVAL (XEXP (SET_SRC (x), 1));
4911 pos = INTVAL (XEXP (SET_SRC (x), 2));
4913 if (BITS_BIG_ENDIAN)
4914 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4915 unsignedp = (code == ZERO_EXTRACT);
4917 break;
4919 default:
4920 break;
4923 if (len && pos >= 0
4924 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4926 machine_mode mode = GET_MODE (SET_SRC (x));
4928 /* For unsigned, we have a choice of a shift followed by an
4929 AND or two shifts. Use two shifts for field sizes where the
4930 constant might be too large. We assume here that we can
4931 always at least get 8-bit constants in an AND insn, which is
4932 true for every current RISC. */
4934 if (unsignedp && len <= 8)
4936 unsigned HOST_WIDE_INT mask
4937 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4938 SUBST (SET_SRC (x),
4939 gen_rtx_AND (mode,
4940 gen_rtx_LSHIFTRT
4941 (mode, gen_lowpart (mode, inner),
4942 GEN_INT (pos)),
4943 gen_int_mode (mask, mode)));
4945 split = find_split_point (&SET_SRC (x), insn, true);
4946 if (split && split != &SET_SRC (x))
4947 return split;
4949 else
4951 SUBST (SET_SRC (x),
4952 gen_rtx_fmt_ee
4953 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4954 gen_rtx_ASHIFT (mode,
4955 gen_lowpart (mode, inner),
4956 GEN_INT (GET_MODE_PRECISION (mode)
4957 - len - pos)),
4958 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4960 split = find_split_point (&SET_SRC (x), insn, true);
4961 if (split && split != &SET_SRC (x))
4962 return split;
4966 /* See if this is a simple operation with a constant as the second
4967 operand. It might be that this constant is out of range and hence
4968 could be used as a split point. */
4969 if (BINARY_P (SET_SRC (x))
4970 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4971 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4972 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4973 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4974 return &XEXP (SET_SRC (x), 1);
4976 /* Finally, see if this is a simple operation with its first operand
4977 not in a register. The operation might require this operand in a
4978 register, so return it as a split point. We can always do this
4979 because if the first operand were another operation, we would have
4980 already found it as a split point. */
4981 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4982 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4983 return &XEXP (SET_SRC (x), 0);
4985 return 0;
4987 case AND:
4988 case IOR:
4989 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4990 it is better to write this as (not (ior A B)) so we can split it.
4991 Similarly for IOR. */
4992 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4994 SUBST (*loc,
4995 gen_rtx_NOT (GET_MODE (x),
4996 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4997 GET_MODE (x),
4998 XEXP (XEXP (x, 0), 0),
4999 XEXP (XEXP (x, 1), 0))));
5000 return find_split_point (loc, insn, set_src);
5003 /* Many RISC machines have a large set of logical insns. If the
5004 second operand is a NOT, put it first so we will try to split the
5005 other operand first. */
5006 if (GET_CODE (XEXP (x, 1)) == NOT)
5008 rtx tem = XEXP (x, 0);
5009 SUBST (XEXP (x, 0), XEXP (x, 1));
5010 SUBST (XEXP (x, 1), tem);
5012 break;
5014 case PLUS:
5015 case MINUS:
5016 /* Canonicalization can produce (minus A (mult B C)), where C is a
5017 constant. It may be better to try splitting (plus (mult B -C) A)
5018 instead if this isn't a multiply by a power of two. */
5019 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5020 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5021 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5023 machine_mode mode = GET_MODE (x);
5024 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5025 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5026 SUBST (*loc, gen_rtx_PLUS (mode,
5027 gen_rtx_MULT (mode,
5028 XEXP (XEXP (x, 1), 0),
5029 gen_int_mode (other_int,
5030 mode)),
5031 XEXP (x, 0)));
5032 return find_split_point (loc, insn, set_src);
5035 /* Split at a multiply-accumulate instruction. However if this is
5036 the SET_SRC, we likely do not have such an instruction and it's
5037 worthless to try this split. */
5038 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5039 return loc;
5041 default:
5042 break;
5045 /* Otherwise, select our actions depending on our rtx class. */
5046 switch (GET_RTX_CLASS (code))
5048 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5049 case RTX_TERNARY:
5050 split = find_split_point (&XEXP (x, 2), insn, false);
5051 if (split)
5052 return split;
5053 /* ... fall through ... */
5054 case RTX_BIN_ARITH:
5055 case RTX_COMM_ARITH:
5056 case RTX_COMPARE:
5057 case RTX_COMM_COMPARE:
5058 split = find_split_point (&XEXP (x, 1), insn, false);
5059 if (split)
5060 return split;
5061 /* ... fall through ... */
5062 case RTX_UNARY:
5063 /* Some machines have (and (shift ...) ...) insns. If X is not
5064 an AND, but XEXP (X, 0) is, use it as our split point. */
5065 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5066 return &XEXP (x, 0);
5068 split = find_split_point (&XEXP (x, 0), insn, false);
5069 if (split)
5070 return split;
5071 return loc;
5073 default:
5074 /* Otherwise, we don't have a split point. */
5075 return 0;
5079 /* Throughout X, replace FROM with TO, and return the result.
5080 The result is TO if X is FROM;
5081 otherwise the result is X, but its contents may have been modified.
5082 If they were modified, a record was made in undobuf so that
5083 undo_all will (among other things) return X to its original state.
5085 If the number of changes necessary is too much to record to undo,
5086 the excess changes are not made, so the result is invalid.
5087 The changes already made can still be undone.
5088 undobuf.num_undo is incremented for such changes, so by testing that
5089 the caller can tell whether the result is valid.
5091 `n_occurrences' is incremented each time FROM is replaced.
5093 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5095 IN_COND is nonzero if we are at the top level of a condition.
5097 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5098 by copying if `n_occurrences' is nonzero. */
5100 static rtx
5101 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5103 enum rtx_code code = GET_CODE (x);
5104 machine_mode op0_mode = VOIDmode;
5105 const char *fmt;
5106 int len, i;
5107 rtx new_rtx;
5109 /* Two expressions are equal if they are identical copies of a shared
5110 RTX or if they are both registers with the same register number
5111 and mode. */
5113 #define COMBINE_RTX_EQUAL_P(X,Y) \
5114 ((X) == (Y) \
5115 || (REG_P (X) && REG_P (Y) \
5116 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5118 /* Do not substitute into clobbers of regs -- this will never result in
5119 valid RTL. */
5120 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5121 return x;
5123 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5125 n_occurrences++;
5126 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5129 /* If X and FROM are the same register but different modes, they
5130 will not have been seen as equal above. However, the log links code
5131 will make a LOG_LINKS entry for that case. If we do nothing, we
5132 will try to rerecognize our original insn and, when it succeeds,
5133 we will delete the feeding insn, which is incorrect.
5135 So force this insn not to match in this (rare) case. */
5136 if (! in_dest && code == REG && REG_P (from)
5137 && reg_overlap_mentioned_p (x, from))
5138 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5140 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5141 of which may contain things that can be combined. */
5142 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5143 return x;
5145 /* It is possible to have a subexpression appear twice in the insn.
5146 Suppose that FROM is a register that appears within TO.
5147 Then, after that subexpression has been scanned once by `subst',
5148 the second time it is scanned, TO may be found. If we were
5149 to scan TO here, we would find FROM within it and create a
5150 self-referent rtl structure which is completely wrong. */
5151 if (COMBINE_RTX_EQUAL_P (x, to))
5152 return to;
5154 /* Parallel asm_operands need special attention because all of the
5155 inputs are shared across the arms. Furthermore, unsharing the
5156 rtl results in recognition failures. Failure to handle this case
5157 specially can result in circular rtl.
5159 Solve this by doing a normal pass across the first entry of the
5160 parallel, and only processing the SET_DESTs of the subsequent
5161 entries. Ug. */
5163 if (code == PARALLEL
5164 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5165 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5167 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5169 /* If this substitution failed, this whole thing fails. */
5170 if (GET_CODE (new_rtx) == CLOBBER
5171 && XEXP (new_rtx, 0) == const0_rtx)
5172 return new_rtx;
5174 SUBST (XVECEXP (x, 0, 0), new_rtx);
5176 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5178 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5180 if (!REG_P (dest)
5181 && GET_CODE (dest) != CC0
5182 && GET_CODE (dest) != PC)
5184 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5186 /* If this substitution failed, this whole thing fails. */
5187 if (GET_CODE (new_rtx) == CLOBBER
5188 && XEXP (new_rtx, 0) == const0_rtx)
5189 return new_rtx;
5191 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5195 else
5197 len = GET_RTX_LENGTH (code);
5198 fmt = GET_RTX_FORMAT (code);
5200 /* We don't need to process a SET_DEST that is a register, CC0,
5201 or PC, so set up to skip this common case. All other cases
5202 where we want to suppress replacing something inside a
5203 SET_SRC are handled via the IN_DEST operand. */
5204 if (code == SET
5205 && (REG_P (SET_DEST (x))
5206 || GET_CODE (SET_DEST (x)) == CC0
5207 || GET_CODE (SET_DEST (x)) == PC))
5208 fmt = "ie";
5210 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5211 constant. */
5212 if (fmt[0] == 'e')
5213 op0_mode = GET_MODE (XEXP (x, 0));
5215 for (i = 0; i < len; i++)
5217 if (fmt[i] == 'E')
5219 int j;
5220 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5222 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5224 new_rtx = (unique_copy && n_occurrences
5225 ? copy_rtx (to) : to);
5226 n_occurrences++;
5228 else
5230 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5231 unique_copy);
5233 /* If this substitution failed, this whole thing
5234 fails. */
5235 if (GET_CODE (new_rtx) == CLOBBER
5236 && XEXP (new_rtx, 0) == const0_rtx)
5237 return new_rtx;
5240 SUBST (XVECEXP (x, i, j), new_rtx);
5243 else if (fmt[i] == 'e')
5245 /* If this is a register being set, ignore it. */
5246 new_rtx = XEXP (x, i);
5247 if (in_dest
5248 && i == 0
5249 && (((code == SUBREG || code == ZERO_EXTRACT)
5250 && REG_P (new_rtx))
5251 || code == STRICT_LOW_PART))
5254 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5256 /* In general, don't install a subreg involving two
5257 modes not tieable. It can worsen register
5258 allocation, and can even make invalid reload
5259 insns, since the reg inside may need to be copied
5260 from in the outside mode, and that may be invalid
5261 if it is an fp reg copied in integer mode.
5263 We allow two exceptions to this: It is valid if
5264 it is inside another SUBREG and the mode of that
5265 SUBREG and the mode of the inside of TO is
5266 tieable and it is valid if X is a SET that copies
5267 FROM to CC0. */
5269 if (GET_CODE (to) == SUBREG
5270 && ! MODES_TIEABLE_P (GET_MODE (to),
5271 GET_MODE (SUBREG_REG (to)))
5272 && ! (code == SUBREG
5273 && MODES_TIEABLE_P (GET_MODE (x),
5274 GET_MODE (SUBREG_REG (to))))
5275 #ifdef HAVE_cc0
5276 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5277 #endif
5279 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5281 if (code == SUBREG
5282 && REG_P (to)
5283 && REGNO (to) < FIRST_PSEUDO_REGISTER
5284 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5285 SUBREG_BYTE (x),
5286 GET_MODE (x)) < 0)
5287 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5289 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5290 n_occurrences++;
5292 else
5293 /* If we are in a SET_DEST, suppress most cases unless we
5294 have gone inside a MEM, in which case we want to
5295 simplify the address. We assume here that things that
5296 are actually part of the destination have their inner
5297 parts in the first expression. This is true for SUBREG,
5298 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5299 things aside from REG and MEM that should appear in a
5300 SET_DEST. */
5301 new_rtx = subst (XEXP (x, i), from, to,
5302 (((in_dest
5303 && (code == SUBREG || code == STRICT_LOW_PART
5304 || code == ZERO_EXTRACT))
5305 || code == SET)
5306 && i == 0),
5307 code == IF_THEN_ELSE && i == 0,
5308 unique_copy);
5310 /* If we found that we will have to reject this combination,
5311 indicate that by returning the CLOBBER ourselves, rather than
5312 an expression containing it. This will speed things up as
5313 well as prevent accidents where two CLOBBERs are considered
5314 to be equal, thus producing an incorrect simplification. */
5316 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5317 return new_rtx;
5319 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5321 machine_mode mode = GET_MODE (x);
5323 x = simplify_subreg (GET_MODE (x), new_rtx,
5324 GET_MODE (SUBREG_REG (x)),
5325 SUBREG_BYTE (x));
5326 if (! x)
5327 x = gen_rtx_CLOBBER (mode, const0_rtx);
5329 else if (CONST_SCALAR_INT_P (new_rtx)
5330 && GET_CODE (x) == ZERO_EXTEND)
5332 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5333 new_rtx, GET_MODE (XEXP (x, 0)));
5334 gcc_assert (x);
5336 else
5337 SUBST (XEXP (x, i), new_rtx);
5342 /* Check if we are loading something from the constant pool via float
5343 extension; in this case we would undo compress_float_constant
5344 optimization and degenerate constant load to an immediate value. */
5345 if (GET_CODE (x) == FLOAT_EXTEND
5346 && MEM_P (XEXP (x, 0))
5347 && MEM_READONLY_P (XEXP (x, 0)))
5349 rtx tmp = avoid_constant_pool_reference (x);
5350 if (x != tmp)
5351 return x;
5354 /* Try to simplify X. If the simplification changed the code, it is likely
5355 that further simplification will help, so loop, but limit the number
5356 of repetitions that will be performed. */
5358 for (i = 0; i < 4; i++)
5360 /* If X is sufficiently simple, don't bother trying to do anything
5361 with it. */
5362 if (code != CONST_INT && code != REG && code != CLOBBER)
5363 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5365 if (GET_CODE (x) == code)
5366 break;
5368 code = GET_CODE (x);
5370 /* We no longer know the original mode of operand 0 since we
5371 have changed the form of X) */
5372 op0_mode = VOIDmode;
5375 return x;
5378 /* Simplify X, a piece of RTL. We just operate on the expression at the
5379 outer level; call `subst' to simplify recursively. Return the new
5380 expression.
5382 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5383 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5384 of a condition. */
5386 static rtx
5387 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5388 int in_cond)
5390 enum rtx_code code = GET_CODE (x);
5391 machine_mode mode = GET_MODE (x);
5392 rtx temp;
5393 int i;
5395 /* If this is a commutative operation, put a constant last and a complex
5396 expression first. We don't need to do this for comparisons here. */
5397 if (COMMUTATIVE_ARITH_P (x)
5398 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5400 temp = XEXP (x, 0);
5401 SUBST (XEXP (x, 0), XEXP (x, 1));
5402 SUBST (XEXP (x, 1), temp);
5405 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5406 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5407 things. Check for cases where both arms are testing the same
5408 condition.
5410 Don't do anything if all operands are very simple. */
5412 if ((BINARY_P (x)
5413 && ((!OBJECT_P (XEXP (x, 0))
5414 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5415 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5416 || (!OBJECT_P (XEXP (x, 1))
5417 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5418 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5419 || (UNARY_P (x)
5420 && (!OBJECT_P (XEXP (x, 0))
5421 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5422 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5424 rtx cond, true_rtx, false_rtx;
5426 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5427 if (cond != 0
5428 /* If everything is a comparison, what we have is highly unlikely
5429 to be simpler, so don't use it. */
5430 && ! (COMPARISON_P (x)
5431 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5433 rtx cop1 = const0_rtx;
5434 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5436 if (cond_code == NE && COMPARISON_P (cond))
5437 return x;
5439 /* Simplify the alternative arms; this may collapse the true and
5440 false arms to store-flag values. Be careful to use copy_rtx
5441 here since true_rtx or false_rtx might share RTL with x as a
5442 result of the if_then_else_cond call above. */
5443 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5444 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5446 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5447 is unlikely to be simpler. */
5448 if (general_operand (true_rtx, VOIDmode)
5449 && general_operand (false_rtx, VOIDmode))
5451 enum rtx_code reversed;
5453 /* Restarting if we generate a store-flag expression will cause
5454 us to loop. Just drop through in this case. */
5456 /* If the result values are STORE_FLAG_VALUE and zero, we can
5457 just make the comparison operation. */
5458 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5459 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5460 cond, cop1);
5461 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5462 && ((reversed = reversed_comparison_code_parts
5463 (cond_code, cond, cop1, NULL))
5464 != UNKNOWN))
5465 x = simplify_gen_relational (reversed, mode, VOIDmode,
5466 cond, cop1);
5468 /* Likewise, we can make the negate of a comparison operation
5469 if the result values are - STORE_FLAG_VALUE and zero. */
5470 else if (CONST_INT_P (true_rtx)
5471 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5472 && false_rtx == const0_rtx)
5473 x = simplify_gen_unary (NEG, mode,
5474 simplify_gen_relational (cond_code,
5475 mode, VOIDmode,
5476 cond, cop1),
5477 mode);
5478 else if (CONST_INT_P (false_rtx)
5479 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5480 && true_rtx == const0_rtx
5481 && ((reversed = reversed_comparison_code_parts
5482 (cond_code, cond, cop1, NULL))
5483 != UNKNOWN))
5484 x = simplify_gen_unary (NEG, mode,
5485 simplify_gen_relational (reversed,
5486 mode, VOIDmode,
5487 cond, cop1),
5488 mode);
5489 else
5490 return gen_rtx_IF_THEN_ELSE (mode,
5491 simplify_gen_relational (cond_code,
5492 mode,
5493 VOIDmode,
5494 cond,
5495 cop1),
5496 true_rtx, false_rtx);
5498 code = GET_CODE (x);
5499 op0_mode = VOIDmode;
5504 /* Try to fold this expression in case we have constants that weren't
5505 present before. */
5506 temp = 0;
5507 switch (GET_RTX_CLASS (code))
5509 case RTX_UNARY:
5510 if (op0_mode == VOIDmode)
5511 op0_mode = GET_MODE (XEXP (x, 0));
5512 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5513 break;
5514 case RTX_COMPARE:
5515 case RTX_COMM_COMPARE:
5517 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5518 if (cmp_mode == VOIDmode)
5520 cmp_mode = GET_MODE (XEXP (x, 1));
5521 if (cmp_mode == VOIDmode)
5522 cmp_mode = op0_mode;
5524 temp = simplify_relational_operation (code, mode, cmp_mode,
5525 XEXP (x, 0), XEXP (x, 1));
5527 break;
5528 case RTX_COMM_ARITH:
5529 case RTX_BIN_ARITH:
5530 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5531 break;
5532 case RTX_BITFIELD_OPS:
5533 case RTX_TERNARY:
5534 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5535 XEXP (x, 1), XEXP (x, 2));
5536 break;
5537 default:
5538 break;
5541 if (temp)
5543 x = temp;
5544 code = GET_CODE (temp);
5545 op0_mode = VOIDmode;
5546 mode = GET_MODE (temp);
5549 /* First see if we can apply the inverse distributive law. */
5550 if (code == PLUS || code == MINUS
5551 || code == AND || code == IOR || code == XOR)
5553 x = apply_distributive_law (x);
5554 code = GET_CODE (x);
5555 op0_mode = VOIDmode;
5558 /* If CODE is an associative operation not otherwise handled, see if we
5559 can associate some operands. This can win if they are constants or
5560 if they are logically related (i.e. (a & b) & a). */
5561 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5562 || code == AND || code == IOR || code == XOR
5563 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5564 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5565 || (flag_associative_math && FLOAT_MODE_P (mode))))
5567 if (GET_CODE (XEXP (x, 0)) == code)
5569 rtx other = XEXP (XEXP (x, 0), 0);
5570 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5571 rtx inner_op1 = XEXP (x, 1);
5572 rtx inner;
5574 /* Make sure we pass the constant operand if any as the second
5575 one if this is a commutative operation. */
5576 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5578 rtx tem = inner_op0;
5579 inner_op0 = inner_op1;
5580 inner_op1 = tem;
5582 inner = simplify_binary_operation (code == MINUS ? PLUS
5583 : code == DIV ? MULT
5584 : code,
5585 mode, inner_op0, inner_op1);
5587 /* For commutative operations, try the other pair if that one
5588 didn't simplify. */
5589 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5591 other = XEXP (XEXP (x, 0), 1);
5592 inner = simplify_binary_operation (code, mode,
5593 XEXP (XEXP (x, 0), 0),
5594 XEXP (x, 1));
5597 if (inner)
5598 return simplify_gen_binary (code, mode, other, inner);
5602 /* A little bit of algebraic simplification here. */
5603 switch (code)
5605 case MEM:
5606 /* Ensure that our address has any ASHIFTs converted to MULT in case
5607 address-recognizing predicates are called later. */
5608 temp = make_compound_operation (XEXP (x, 0), MEM);
5609 SUBST (XEXP (x, 0), temp);
5610 break;
5612 case SUBREG:
5613 if (op0_mode == VOIDmode)
5614 op0_mode = GET_MODE (SUBREG_REG (x));
5616 /* See if this can be moved to simplify_subreg. */
5617 if (CONSTANT_P (SUBREG_REG (x))
5618 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5619 /* Don't call gen_lowpart if the inner mode
5620 is VOIDmode and we cannot simplify it, as SUBREG without
5621 inner mode is invalid. */
5622 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5623 || gen_lowpart_common (mode, SUBREG_REG (x))))
5624 return gen_lowpart (mode, SUBREG_REG (x));
5626 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5627 break;
5629 rtx temp;
5630 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5631 SUBREG_BYTE (x));
5632 if (temp)
5633 return temp;
5635 /* If op is known to have all lower bits zero, the result is zero. */
5636 if (!in_dest
5637 && SCALAR_INT_MODE_P (mode)
5638 && SCALAR_INT_MODE_P (op0_mode)
5639 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5640 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5641 && HWI_COMPUTABLE_MODE_P (op0_mode)
5642 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5643 & GET_MODE_MASK (mode)) == 0)
5644 return CONST0_RTX (mode);
5647 /* Don't change the mode of the MEM if that would change the meaning
5648 of the address. */
5649 if (MEM_P (SUBREG_REG (x))
5650 && (MEM_VOLATILE_P (SUBREG_REG (x))
5651 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5652 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5653 return gen_rtx_CLOBBER (mode, const0_rtx);
5655 /* Note that we cannot do any narrowing for non-constants since
5656 we might have been counting on using the fact that some bits were
5657 zero. We now do this in the SET. */
5659 break;
5661 case NEG:
5662 temp = expand_compound_operation (XEXP (x, 0));
5664 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5665 replaced by (lshiftrt X C). This will convert
5666 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5668 if (GET_CODE (temp) == ASHIFTRT
5669 && CONST_INT_P (XEXP (temp, 1))
5670 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5671 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5672 INTVAL (XEXP (temp, 1)));
5674 /* If X has only a single bit that might be nonzero, say, bit I, convert
5675 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5676 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5677 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5678 or a SUBREG of one since we'd be making the expression more
5679 complex if it was just a register. */
5681 if (!REG_P (temp)
5682 && ! (GET_CODE (temp) == SUBREG
5683 && REG_P (SUBREG_REG (temp)))
5684 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5686 rtx temp1 = simplify_shift_const
5687 (NULL_RTX, ASHIFTRT, mode,
5688 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5689 GET_MODE_PRECISION (mode) - 1 - i),
5690 GET_MODE_PRECISION (mode) - 1 - i);
5692 /* If all we did was surround TEMP with the two shifts, we
5693 haven't improved anything, so don't use it. Otherwise,
5694 we are better off with TEMP1. */
5695 if (GET_CODE (temp1) != ASHIFTRT
5696 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5697 || XEXP (XEXP (temp1, 0), 0) != temp)
5698 return temp1;
5700 break;
5702 case TRUNCATE:
5703 /* We can't handle truncation to a partial integer mode here
5704 because we don't know the real bitsize of the partial
5705 integer mode. */
5706 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5707 break;
5709 if (HWI_COMPUTABLE_MODE_P (mode))
5710 SUBST (XEXP (x, 0),
5711 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5712 GET_MODE_MASK (mode), 0));
5714 /* We can truncate a constant value and return it. */
5715 if (CONST_INT_P (XEXP (x, 0)))
5716 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5718 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5719 whose value is a comparison can be replaced with a subreg if
5720 STORE_FLAG_VALUE permits. */
5721 if (HWI_COMPUTABLE_MODE_P (mode)
5722 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5723 && (temp = get_last_value (XEXP (x, 0)))
5724 && COMPARISON_P (temp))
5725 return gen_lowpart (mode, XEXP (x, 0));
5726 break;
5728 case CONST:
5729 /* (const (const X)) can become (const X). Do it this way rather than
5730 returning the inner CONST since CONST can be shared with a
5731 REG_EQUAL note. */
5732 if (GET_CODE (XEXP (x, 0)) == CONST)
5733 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5734 break;
5736 #ifdef HAVE_lo_sum
5737 case LO_SUM:
5738 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5739 can add in an offset. find_split_point will split this address up
5740 again if it doesn't match. */
5741 if (GET_CODE (XEXP (x, 0)) == HIGH
5742 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5743 return XEXP (x, 1);
5744 break;
5745 #endif
5747 case PLUS:
5748 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5749 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5750 bit-field and can be replaced by either a sign_extend or a
5751 sign_extract. The `and' may be a zero_extend and the two
5752 <c>, -<c> constants may be reversed. */
5753 if (GET_CODE (XEXP (x, 0)) == XOR
5754 && CONST_INT_P (XEXP (x, 1))
5755 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5756 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5757 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5758 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5759 && HWI_COMPUTABLE_MODE_P (mode)
5760 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5761 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5762 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5763 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5764 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5765 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5766 == (unsigned int) i + 1))))
5767 return simplify_shift_const
5768 (NULL_RTX, ASHIFTRT, mode,
5769 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5770 XEXP (XEXP (XEXP (x, 0), 0), 0),
5771 GET_MODE_PRECISION (mode) - (i + 1)),
5772 GET_MODE_PRECISION (mode) - (i + 1));
5774 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5775 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5776 the bitsize of the mode - 1. This allows simplification of
5777 "a = (b & 8) == 0;" */
5778 if (XEXP (x, 1) == constm1_rtx
5779 && !REG_P (XEXP (x, 0))
5780 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5781 && REG_P (SUBREG_REG (XEXP (x, 0))))
5782 && nonzero_bits (XEXP (x, 0), mode) == 1)
5783 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5784 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5785 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5786 GET_MODE_PRECISION (mode) - 1),
5787 GET_MODE_PRECISION (mode) - 1);
5789 /* If we are adding two things that have no bits in common, convert
5790 the addition into an IOR. This will often be further simplified,
5791 for example in cases like ((a & 1) + (a & 2)), which can
5792 become a & 3. */
5794 if (HWI_COMPUTABLE_MODE_P (mode)
5795 && (nonzero_bits (XEXP (x, 0), mode)
5796 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5798 /* Try to simplify the expression further. */
5799 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5800 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5802 /* If we could, great. If not, do not go ahead with the IOR
5803 replacement, since PLUS appears in many special purpose
5804 address arithmetic instructions. */
5805 if (GET_CODE (temp) != CLOBBER
5806 && (GET_CODE (temp) != IOR
5807 || ((XEXP (temp, 0) != XEXP (x, 0)
5808 || XEXP (temp, 1) != XEXP (x, 1))
5809 && (XEXP (temp, 0) != XEXP (x, 1)
5810 || XEXP (temp, 1) != XEXP (x, 0)))))
5811 return temp;
5813 break;
5815 case MINUS:
5816 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5817 (and <foo> (const_int pow2-1)) */
5818 if (GET_CODE (XEXP (x, 1)) == AND
5819 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5820 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5821 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5822 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5823 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5824 break;
5826 case MULT:
5827 /* If we have (mult (plus A B) C), apply the distributive law and then
5828 the inverse distributive law to see if things simplify. This
5829 occurs mostly in addresses, often when unrolling loops. */
5831 if (GET_CODE (XEXP (x, 0)) == PLUS)
5833 rtx result = distribute_and_simplify_rtx (x, 0);
5834 if (result)
5835 return result;
5838 /* Try simplify a*(b/c) as (a*b)/c. */
5839 if (FLOAT_MODE_P (mode) && flag_associative_math
5840 && GET_CODE (XEXP (x, 0)) == DIV)
5842 rtx tem = simplify_binary_operation (MULT, mode,
5843 XEXP (XEXP (x, 0), 0),
5844 XEXP (x, 1));
5845 if (tem)
5846 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5848 break;
5850 case UDIV:
5851 /* If this is a divide by a power of two, treat it as a shift if
5852 its first operand is a shift. */
5853 if (CONST_INT_P (XEXP (x, 1))
5854 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5855 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5856 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5857 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5858 || GET_CODE (XEXP (x, 0)) == ROTATE
5859 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5860 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5861 break;
5863 case EQ: case NE:
5864 case GT: case GTU: case GE: case GEU:
5865 case LT: case LTU: case LE: case LEU:
5866 case UNEQ: case LTGT:
5867 case UNGT: case UNGE:
5868 case UNLT: case UNLE:
5869 case UNORDERED: case ORDERED:
5870 /* If the first operand is a condition code, we can't do anything
5871 with it. */
5872 if (GET_CODE (XEXP (x, 0)) == COMPARE
5873 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5874 && ! CC0_P (XEXP (x, 0))))
5876 rtx op0 = XEXP (x, 0);
5877 rtx op1 = XEXP (x, 1);
5878 enum rtx_code new_code;
5880 if (GET_CODE (op0) == COMPARE)
5881 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5883 /* Simplify our comparison, if possible. */
5884 new_code = simplify_comparison (code, &op0, &op1);
5886 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5887 if only the low-order bit is possibly nonzero in X (such as when
5888 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5889 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5890 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5891 (plus X 1).
5893 Remove any ZERO_EXTRACT we made when thinking this was a
5894 comparison. It may now be simpler to use, e.g., an AND. If a
5895 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5896 the call to make_compound_operation in the SET case.
5898 Don't apply these optimizations if the caller would
5899 prefer a comparison rather than a value.
5900 E.g., for the condition in an IF_THEN_ELSE most targets need
5901 an explicit comparison. */
5903 if (in_cond)
5906 else if (STORE_FLAG_VALUE == 1
5907 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5908 && op1 == const0_rtx
5909 && mode == GET_MODE (op0)
5910 && nonzero_bits (op0, mode) == 1)
5911 return gen_lowpart (mode,
5912 expand_compound_operation (op0));
5914 else if (STORE_FLAG_VALUE == 1
5915 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5916 && op1 == const0_rtx
5917 && mode == GET_MODE (op0)
5918 && (num_sign_bit_copies (op0, mode)
5919 == GET_MODE_PRECISION (mode)))
5921 op0 = expand_compound_operation (op0);
5922 return simplify_gen_unary (NEG, mode,
5923 gen_lowpart (mode, op0),
5924 mode);
5927 else if (STORE_FLAG_VALUE == 1
5928 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5929 && op1 == const0_rtx
5930 && mode == GET_MODE (op0)
5931 && nonzero_bits (op0, mode) == 1)
5933 op0 = expand_compound_operation (op0);
5934 return simplify_gen_binary (XOR, mode,
5935 gen_lowpart (mode, op0),
5936 const1_rtx);
5939 else if (STORE_FLAG_VALUE == 1
5940 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5941 && op1 == const0_rtx
5942 && mode == GET_MODE (op0)
5943 && (num_sign_bit_copies (op0, mode)
5944 == GET_MODE_PRECISION (mode)))
5946 op0 = expand_compound_operation (op0);
5947 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5950 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5951 those above. */
5952 if (in_cond)
5955 else if (STORE_FLAG_VALUE == -1
5956 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5957 && op1 == const0_rtx
5958 && mode == GET_MODE (op0)
5959 && (num_sign_bit_copies (op0, mode)
5960 == GET_MODE_PRECISION (mode)))
5961 return gen_lowpart (mode,
5962 expand_compound_operation (op0));
5964 else if (STORE_FLAG_VALUE == -1
5965 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5966 && op1 == const0_rtx
5967 && mode == GET_MODE (op0)
5968 && nonzero_bits (op0, mode) == 1)
5970 op0 = expand_compound_operation (op0);
5971 return simplify_gen_unary (NEG, mode,
5972 gen_lowpart (mode, op0),
5973 mode);
5976 else if (STORE_FLAG_VALUE == -1
5977 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5978 && op1 == const0_rtx
5979 && mode == GET_MODE (op0)
5980 && (num_sign_bit_copies (op0, mode)
5981 == GET_MODE_PRECISION (mode)))
5983 op0 = expand_compound_operation (op0);
5984 return simplify_gen_unary (NOT, mode,
5985 gen_lowpart (mode, op0),
5986 mode);
5989 /* If X is 0/1, (eq X 0) is X-1. */
5990 else if (STORE_FLAG_VALUE == -1
5991 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5992 && op1 == const0_rtx
5993 && mode == GET_MODE (op0)
5994 && nonzero_bits (op0, mode) == 1)
5996 op0 = expand_compound_operation (op0);
5997 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6000 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6001 one bit that might be nonzero, we can convert (ne x 0) to
6002 (ashift x c) where C puts the bit in the sign bit. Remove any
6003 AND with STORE_FLAG_VALUE when we are done, since we are only
6004 going to test the sign bit. */
6005 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6006 && HWI_COMPUTABLE_MODE_P (mode)
6007 && val_signbit_p (mode, STORE_FLAG_VALUE)
6008 && op1 == const0_rtx
6009 && mode == GET_MODE (op0)
6010 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6012 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6013 expand_compound_operation (op0),
6014 GET_MODE_PRECISION (mode) - 1 - i);
6015 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6016 return XEXP (x, 0);
6017 else
6018 return x;
6021 /* If the code changed, return a whole new comparison.
6022 We also need to avoid using SUBST in cases where
6023 simplify_comparison has widened a comparison with a CONST_INT,
6024 since in that case the wider CONST_INT may fail the sanity
6025 checks in do_SUBST. */
6026 if (new_code != code
6027 || (CONST_INT_P (op1)
6028 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6029 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6030 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6032 /* Otherwise, keep this operation, but maybe change its operands.
6033 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6034 SUBST (XEXP (x, 0), op0);
6035 SUBST (XEXP (x, 1), op1);
6037 break;
6039 case IF_THEN_ELSE:
6040 return simplify_if_then_else (x);
6042 case ZERO_EXTRACT:
6043 case SIGN_EXTRACT:
6044 case ZERO_EXTEND:
6045 case SIGN_EXTEND:
6046 /* If we are processing SET_DEST, we are done. */
6047 if (in_dest)
6048 return x;
6050 return expand_compound_operation (x);
6052 case SET:
6053 return simplify_set (x);
6055 case AND:
6056 case IOR:
6057 return simplify_logical (x);
6059 case ASHIFT:
6060 case LSHIFTRT:
6061 case ASHIFTRT:
6062 case ROTATE:
6063 case ROTATERT:
6064 /* If this is a shift by a constant amount, simplify it. */
6065 if (CONST_INT_P (XEXP (x, 1)))
6066 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6067 INTVAL (XEXP (x, 1)));
6069 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6070 SUBST (XEXP (x, 1),
6071 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6072 ((unsigned HOST_WIDE_INT) 1
6073 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6074 - 1,
6075 0));
6076 break;
6078 default:
6079 break;
6082 return x;
6085 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6087 static rtx
6088 simplify_if_then_else (rtx x)
6090 machine_mode mode = GET_MODE (x);
6091 rtx cond = XEXP (x, 0);
6092 rtx true_rtx = XEXP (x, 1);
6093 rtx false_rtx = XEXP (x, 2);
6094 enum rtx_code true_code = GET_CODE (cond);
6095 int comparison_p = COMPARISON_P (cond);
6096 rtx temp;
6097 int i;
6098 enum rtx_code false_code;
6099 rtx reversed;
6101 /* Simplify storing of the truth value. */
6102 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6103 return simplify_gen_relational (true_code, mode, VOIDmode,
6104 XEXP (cond, 0), XEXP (cond, 1));
6106 /* Also when the truth value has to be reversed. */
6107 if (comparison_p
6108 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6109 && (reversed = reversed_comparison (cond, mode)))
6110 return reversed;
6112 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6113 in it is being compared against certain values. Get the true and false
6114 comparisons and see if that says anything about the value of each arm. */
6116 if (comparison_p
6117 && ((false_code = reversed_comparison_code (cond, NULL))
6118 != UNKNOWN)
6119 && REG_P (XEXP (cond, 0)))
6121 HOST_WIDE_INT nzb;
6122 rtx from = XEXP (cond, 0);
6123 rtx true_val = XEXP (cond, 1);
6124 rtx false_val = true_val;
6125 int swapped = 0;
6127 /* If FALSE_CODE is EQ, swap the codes and arms. */
6129 if (false_code == EQ)
6131 swapped = 1, true_code = EQ, false_code = NE;
6132 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6135 /* If we are comparing against zero and the expression being tested has
6136 only a single bit that might be nonzero, that is its value when it is
6137 not equal to zero. Similarly if it is known to be -1 or 0. */
6139 if (true_code == EQ && true_val == const0_rtx
6140 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6142 false_code = EQ;
6143 false_val = gen_int_mode (nzb, GET_MODE (from));
6145 else if (true_code == EQ && true_val == const0_rtx
6146 && (num_sign_bit_copies (from, GET_MODE (from))
6147 == GET_MODE_PRECISION (GET_MODE (from))))
6149 false_code = EQ;
6150 false_val = constm1_rtx;
6153 /* Now simplify an arm if we know the value of the register in the
6154 branch and it is used in the arm. Be careful due to the potential
6155 of locally-shared RTL. */
6157 if (reg_mentioned_p (from, true_rtx))
6158 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6159 from, true_val),
6160 pc_rtx, pc_rtx, 0, 0, 0);
6161 if (reg_mentioned_p (from, false_rtx))
6162 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6163 from, false_val),
6164 pc_rtx, pc_rtx, 0, 0, 0);
6166 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6167 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6169 true_rtx = XEXP (x, 1);
6170 false_rtx = XEXP (x, 2);
6171 true_code = GET_CODE (cond);
6174 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6175 reversed, do so to avoid needing two sets of patterns for
6176 subtract-and-branch insns. Similarly if we have a constant in the true
6177 arm, the false arm is the same as the first operand of the comparison, or
6178 the false arm is more complicated than the true arm. */
6180 if (comparison_p
6181 && reversed_comparison_code (cond, NULL) != UNKNOWN
6182 && (true_rtx == pc_rtx
6183 || (CONSTANT_P (true_rtx)
6184 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6185 || true_rtx == const0_rtx
6186 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6187 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6188 && !OBJECT_P (false_rtx))
6189 || reg_mentioned_p (true_rtx, false_rtx)
6190 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6192 true_code = reversed_comparison_code (cond, NULL);
6193 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6194 SUBST (XEXP (x, 1), false_rtx);
6195 SUBST (XEXP (x, 2), true_rtx);
6197 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6198 cond = XEXP (x, 0);
6200 /* It is possible that the conditional has been simplified out. */
6201 true_code = GET_CODE (cond);
6202 comparison_p = COMPARISON_P (cond);
6205 /* If the two arms are identical, we don't need the comparison. */
6207 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6208 return true_rtx;
6210 /* Convert a == b ? b : a to "a". */
6211 if (true_code == EQ && ! side_effects_p (cond)
6212 && !HONOR_NANS (mode)
6213 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6214 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6215 return false_rtx;
6216 else if (true_code == NE && ! side_effects_p (cond)
6217 && !HONOR_NANS (mode)
6218 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6219 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6220 return true_rtx;
6222 /* Look for cases where we have (abs x) or (neg (abs X)). */
6224 if (GET_MODE_CLASS (mode) == MODE_INT
6225 && comparison_p
6226 && XEXP (cond, 1) == const0_rtx
6227 && GET_CODE (false_rtx) == NEG
6228 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6229 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6230 && ! side_effects_p (true_rtx))
6231 switch (true_code)
6233 case GT:
6234 case GE:
6235 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6236 case LT:
6237 case LE:
6238 return
6239 simplify_gen_unary (NEG, mode,
6240 simplify_gen_unary (ABS, mode, true_rtx, mode),
6241 mode);
6242 default:
6243 break;
6246 /* Look for MIN or MAX. */
6248 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6249 && comparison_p
6250 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6251 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6252 && ! side_effects_p (cond))
6253 switch (true_code)
6255 case GE:
6256 case GT:
6257 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6258 case LE:
6259 case LT:
6260 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6261 case GEU:
6262 case GTU:
6263 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6264 case LEU:
6265 case LTU:
6266 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6267 default:
6268 break;
6271 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6272 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6273 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6274 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6275 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6276 neither 1 or -1, but it isn't worth checking for. */
6278 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6279 && comparison_p
6280 && GET_MODE_CLASS (mode) == MODE_INT
6281 && ! side_effects_p (x))
6283 rtx t = make_compound_operation (true_rtx, SET);
6284 rtx f = make_compound_operation (false_rtx, SET);
6285 rtx cond_op0 = XEXP (cond, 0);
6286 rtx cond_op1 = XEXP (cond, 1);
6287 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6288 machine_mode m = mode;
6289 rtx z = 0, c1 = NULL_RTX;
6291 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6292 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6293 || GET_CODE (t) == ASHIFT
6294 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6295 && rtx_equal_p (XEXP (t, 0), f))
6296 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6298 /* If an identity-zero op is commutative, check whether there
6299 would be a match if we swapped the operands. */
6300 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6301 || GET_CODE (t) == XOR)
6302 && rtx_equal_p (XEXP (t, 1), f))
6303 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6304 else if (GET_CODE (t) == SIGN_EXTEND
6305 && (GET_CODE (XEXP (t, 0)) == PLUS
6306 || GET_CODE (XEXP (t, 0)) == MINUS
6307 || GET_CODE (XEXP (t, 0)) == IOR
6308 || GET_CODE (XEXP (t, 0)) == XOR
6309 || GET_CODE (XEXP (t, 0)) == ASHIFT
6310 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6311 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6312 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6313 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6314 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6315 && (num_sign_bit_copies (f, GET_MODE (f))
6316 > (unsigned int)
6317 (GET_MODE_PRECISION (mode)
6318 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6320 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6321 extend_op = SIGN_EXTEND;
6322 m = GET_MODE (XEXP (t, 0));
6324 else if (GET_CODE (t) == SIGN_EXTEND
6325 && (GET_CODE (XEXP (t, 0)) == PLUS
6326 || GET_CODE (XEXP (t, 0)) == IOR
6327 || GET_CODE (XEXP (t, 0)) == XOR)
6328 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6329 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6330 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6331 && (num_sign_bit_copies (f, GET_MODE (f))
6332 > (unsigned int)
6333 (GET_MODE_PRECISION (mode)
6334 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6336 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6337 extend_op = SIGN_EXTEND;
6338 m = GET_MODE (XEXP (t, 0));
6340 else if (GET_CODE (t) == ZERO_EXTEND
6341 && (GET_CODE (XEXP (t, 0)) == PLUS
6342 || GET_CODE (XEXP (t, 0)) == MINUS
6343 || GET_CODE (XEXP (t, 0)) == IOR
6344 || GET_CODE (XEXP (t, 0)) == XOR
6345 || GET_CODE (XEXP (t, 0)) == ASHIFT
6346 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6347 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6348 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6349 && HWI_COMPUTABLE_MODE_P (mode)
6350 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6351 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6352 && ((nonzero_bits (f, GET_MODE (f))
6353 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6354 == 0))
6356 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6357 extend_op = ZERO_EXTEND;
6358 m = GET_MODE (XEXP (t, 0));
6360 else if (GET_CODE (t) == ZERO_EXTEND
6361 && (GET_CODE (XEXP (t, 0)) == PLUS
6362 || GET_CODE (XEXP (t, 0)) == IOR
6363 || GET_CODE (XEXP (t, 0)) == XOR)
6364 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6365 && HWI_COMPUTABLE_MODE_P (mode)
6366 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6367 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6368 && ((nonzero_bits (f, GET_MODE (f))
6369 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6370 == 0))
6372 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6373 extend_op = ZERO_EXTEND;
6374 m = GET_MODE (XEXP (t, 0));
6377 if (z)
6379 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6380 cond_op0, cond_op1),
6381 pc_rtx, pc_rtx, 0, 0, 0);
6382 temp = simplify_gen_binary (MULT, m, temp,
6383 simplify_gen_binary (MULT, m, c1,
6384 const_true_rtx));
6385 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6386 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6388 if (extend_op != UNKNOWN)
6389 temp = simplify_gen_unary (extend_op, mode, temp, m);
6391 return temp;
6395 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6396 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6397 negation of a single bit, we can convert this operation to a shift. We
6398 can actually do this more generally, but it doesn't seem worth it. */
6400 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6401 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6402 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6403 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6404 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6405 == GET_MODE_PRECISION (mode))
6406 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6407 return
6408 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6409 gen_lowpart (mode, XEXP (cond, 0)), i);
6411 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6412 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6413 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6414 && GET_MODE (XEXP (cond, 0)) == mode
6415 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6416 == nonzero_bits (XEXP (cond, 0), mode)
6417 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6418 return XEXP (cond, 0);
6420 return x;
6423 /* Simplify X, a SET expression. Return the new expression. */
6425 static rtx
6426 simplify_set (rtx x)
6428 rtx src = SET_SRC (x);
6429 rtx dest = SET_DEST (x);
6430 machine_mode mode
6431 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6432 rtx_insn *other_insn;
6433 rtx *cc_use;
6435 /* (set (pc) (return)) gets written as (return). */
6436 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6437 return src;
6439 /* Now that we know for sure which bits of SRC we are using, see if we can
6440 simplify the expression for the object knowing that we only need the
6441 low-order bits. */
6443 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6445 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6446 SUBST (SET_SRC (x), src);
6449 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6450 the comparison result and try to simplify it unless we already have used
6451 undobuf.other_insn. */
6452 if ((GET_MODE_CLASS (mode) == MODE_CC
6453 || GET_CODE (src) == COMPARE
6454 || CC0_P (dest))
6455 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6456 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6457 && COMPARISON_P (*cc_use)
6458 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6460 enum rtx_code old_code = GET_CODE (*cc_use);
6461 enum rtx_code new_code;
6462 rtx op0, op1, tmp;
6463 int other_changed = 0;
6464 rtx inner_compare = NULL_RTX;
6465 machine_mode compare_mode = GET_MODE (dest);
6467 if (GET_CODE (src) == COMPARE)
6469 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6470 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6472 inner_compare = op0;
6473 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6476 else
6477 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6479 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6480 op0, op1);
6481 if (!tmp)
6482 new_code = old_code;
6483 else if (!CONSTANT_P (tmp))
6485 new_code = GET_CODE (tmp);
6486 op0 = XEXP (tmp, 0);
6487 op1 = XEXP (tmp, 1);
6489 else
6491 rtx pat = PATTERN (other_insn);
6492 undobuf.other_insn = other_insn;
6493 SUBST (*cc_use, tmp);
6495 /* Attempt to simplify CC user. */
6496 if (GET_CODE (pat) == SET)
6498 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6499 if (new_rtx != NULL_RTX)
6500 SUBST (SET_SRC (pat), new_rtx);
6503 /* Convert X into a no-op move. */
6504 SUBST (SET_DEST (x), pc_rtx);
6505 SUBST (SET_SRC (x), pc_rtx);
6506 return x;
6509 /* Simplify our comparison, if possible. */
6510 new_code = simplify_comparison (new_code, &op0, &op1);
6512 #ifdef SELECT_CC_MODE
6513 /* If this machine has CC modes other than CCmode, check to see if we
6514 need to use a different CC mode here. */
6515 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6516 compare_mode = GET_MODE (op0);
6517 else if (inner_compare
6518 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6519 && new_code == old_code
6520 && op0 == XEXP (inner_compare, 0)
6521 && op1 == XEXP (inner_compare, 1))
6522 compare_mode = GET_MODE (inner_compare);
6523 else
6524 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6526 #ifndef HAVE_cc0
6527 /* If the mode changed, we have to change SET_DEST, the mode in the
6528 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6529 a hard register, just build new versions with the proper mode. If it
6530 is a pseudo, we lose unless it is only time we set the pseudo, in
6531 which case we can safely change its mode. */
6532 if (compare_mode != GET_MODE (dest))
6534 if (can_change_dest_mode (dest, 0, compare_mode))
6536 unsigned int regno = REGNO (dest);
6537 rtx new_dest;
6539 if (regno < FIRST_PSEUDO_REGISTER)
6540 new_dest = gen_rtx_REG (compare_mode, regno);
6541 else
6543 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6544 new_dest = regno_reg_rtx[regno];
6547 SUBST (SET_DEST (x), new_dest);
6548 SUBST (XEXP (*cc_use, 0), new_dest);
6549 other_changed = 1;
6551 dest = new_dest;
6554 #endif /* cc0 */
6555 #endif /* SELECT_CC_MODE */
6557 /* If the code changed, we have to build a new comparison in
6558 undobuf.other_insn. */
6559 if (new_code != old_code)
6561 int other_changed_previously = other_changed;
6562 unsigned HOST_WIDE_INT mask;
6563 rtx old_cc_use = *cc_use;
6565 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6566 dest, const0_rtx));
6567 other_changed = 1;
6569 /* If the only change we made was to change an EQ into an NE or
6570 vice versa, OP0 has only one bit that might be nonzero, and OP1
6571 is zero, check if changing the user of the condition code will
6572 produce a valid insn. If it won't, we can keep the original code
6573 in that insn by surrounding our operation with an XOR. */
6575 if (((old_code == NE && new_code == EQ)
6576 || (old_code == EQ && new_code == NE))
6577 && ! other_changed_previously && op1 == const0_rtx
6578 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6579 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6581 rtx pat = PATTERN (other_insn), note = 0;
6583 if ((recog_for_combine (&pat, other_insn, &note) < 0
6584 && ! check_asm_operands (pat)))
6586 *cc_use = old_cc_use;
6587 other_changed = 0;
6589 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6590 gen_int_mode (mask,
6591 GET_MODE (op0)));
6596 if (other_changed)
6597 undobuf.other_insn = other_insn;
6599 /* Otherwise, if we didn't previously have a COMPARE in the
6600 correct mode, we need one. */
6601 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6603 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6604 src = SET_SRC (x);
6606 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6608 SUBST (SET_SRC (x), op0);
6609 src = SET_SRC (x);
6611 /* Otherwise, update the COMPARE if needed. */
6612 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6614 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6615 src = SET_SRC (x);
6618 else
6620 /* Get SET_SRC in a form where we have placed back any
6621 compound expressions. Then do the checks below. */
6622 src = make_compound_operation (src, SET);
6623 SUBST (SET_SRC (x), src);
6626 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6627 and X being a REG or (subreg (reg)), we may be able to convert this to
6628 (set (subreg:m2 x) (op)).
6630 We can always do this if M1 is narrower than M2 because that means that
6631 we only care about the low bits of the result.
6633 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6634 perform a narrower operation than requested since the high-order bits will
6635 be undefined. On machine where it is defined, this transformation is safe
6636 as long as M1 and M2 have the same number of words. */
6638 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6639 && !OBJECT_P (SUBREG_REG (src))
6640 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6641 / UNITS_PER_WORD)
6642 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6643 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6644 #ifndef WORD_REGISTER_OPERATIONS
6645 && (GET_MODE_SIZE (GET_MODE (src))
6646 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6647 #endif
6648 #ifdef CANNOT_CHANGE_MODE_CLASS
6649 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6650 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6651 GET_MODE (SUBREG_REG (src)),
6652 GET_MODE (src)))
6653 #endif
6654 && (REG_P (dest)
6655 || (GET_CODE (dest) == SUBREG
6656 && REG_P (SUBREG_REG (dest)))))
6658 SUBST (SET_DEST (x),
6659 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6660 dest));
6661 SUBST (SET_SRC (x), SUBREG_REG (src));
6663 src = SET_SRC (x), dest = SET_DEST (x);
6666 #ifdef HAVE_cc0
6667 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6668 in SRC. */
6669 if (dest == cc0_rtx
6670 && GET_CODE (src) == SUBREG
6671 && subreg_lowpart_p (src)
6672 && (GET_MODE_PRECISION (GET_MODE (src))
6673 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6675 rtx inner = SUBREG_REG (src);
6676 machine_mode inner_mode = GET_MODE (inner);
6678 /* Here we make sure that we don't have a sign bit on. */
6679 if (val_signbit_known_clear_p (GET_MODE (src),
6680 nonzero_bits (inner, inner_mode)))
6682 SUBST (SET_SRC (x), inner);
6683 src = SET_SRC (x);
6686 #endif
6688 #ifdef LOAD_EXTEND_OP
6689 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6690 would require a paradoxical subreg. Replace the subreg with a
6691 zero_extend to avoid the reload that would otherwise be required. */
6693 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6694 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6695 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6696 && SUBREG_BYTE (src) == 0
6697 && paradoxical_subreg_p (src)
6698 && MEM_P (SUBREG_REG (src)))
6700 SUBST (SET_SRC (x),
6701 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6702 GET_MODE (src), SUBREG_REG (src)));
6704 src = SET_SRC (x);
6706 #endif
6708 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6709 are comparing an item known to be 0 or -1 against 0, use a logical
6710 operation instead. Check for one of the arms being an IOR of the other
6711 arm with some value. We compute three terms to be IOR'ed together. In
6712 practice, at most two will be nonzero. Then we do the IOR's. */
6714 if (GET_CODE (dest) != PC
6715 && GET_CODE (src) == IF_THEN_ELSE
6716 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6717 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6718 && XEXP (XEXP (src, 0), 1) == const0_rtx
6719 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6720 #ifdef HAVE_conditional_move
6721 && ! can_conditionally_move_p (GET_MODE (src))
6722 #endif
6723 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6724 GET_MODE (XEXP (XEXP (src, 0), 0)))
6725 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6726 && ! side_effects_p (src))
6728 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6729 ? XEXP (src, 1) : XEXP (src, 2));
6730 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6731 ? XEXP (src, 2) : XEXP (src, 1));
6732 rtx term1 = const0_rtx, term2, term3;
6734 if (GET_CODE (true_rtx) == IOR
6735 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6736 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6737 else if (GET_CODE (true_rtx) == IOR
6738 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6739 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6740 else if (GET_CODE (false_rtx) == IOR
6741 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6742 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6743 else if (GET_CODE (false_rtx) == IOR
6744 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6745 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6747 term2 = simplify_gen_binary (AND, GET_MODE (src),
6748 XEXP (XEXP (src, 0), 0), true_rtx);
6749 term3 = simplify_gen_binary (AND, GET_MODE (src),
6750 simplify_gen_unary (NOT, GET_MODE (src),
6751 XEXP (XEXP (src, 0), 0),
6752 GET_MODE (src)),
6753 false_rtx);
6755 SUBST (SET_SRC (x),
6756 simplify_gen_binary (IOR, GET_MODE (src),
6757 simplify_gen_binary (IOR, GET_MODE (src),
6758 term1, term2),
6759 term3));
6761 src = SET_SRC (x);
6764 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6765 whole thing fail. */
6766 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6767 return src;
6768 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6769 return dest;
6770 else
6771 /* Convert this into a field assignment operation, if possible. */
6772 return make_field_assignment (x);
6775 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6776 result. */
6778 static rtx
6779 simplify_logical (rtx x)
6781 machine_mode mode = GET_MODE (x);
6782 rtx op0 = XEXP (x, 0);
6783 rtx op1 = XEXP (x, 1);
6785 switch (GET_CODE (x))
6787 case AND:
6788 /* We can call simplify_and_const_int only if we don't lose
6789 any (sign) bits when converting INTVAL (op1) to
6790 "unsigned HOST_WIDE_INT". */
6791 if (CONST_INT_P (op1)
6792 && (HWI_COMPUTABLE_MODE_P (mode)
6793 || INTVAL (op1) > 0))
6795 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6796 if (GET_CODE (x) != AND)
6797 return x;
6799 op0 = XEXP (x, 0);
6800 op1 = XEXP (x, 1);
6803 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6804 apply the distributive law and then the inverse distributive
6805 law to see if things simplify. */
6806 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6808 rtx result = distribute_and_simplify_rtx (x, 0);
6809 if (result)
6810 return result;
6812 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6814 rtx result = distribute_and_simplify_rtx (x, 1);
6815 if (result)
6816 return result;
6818 break;
6820 case IOR:
6821 /* If we have (ior (and A B) C), apply the distributive law and then
6822 the inverse distributive law to see if things simplify. */
6824 if (GET_CODE (op0) == AND)
6826 rtx result = distribute_and_simplify_rtx (x, 0);
6827 if (result)
6828 return result;
6831 if (GET_CODE (op1) == AND)
6833 rtx result = distribute_and_simplify_rtx (x, 1);
6834 if (result)
6835 return result;
6837 break;
6839 default:
6840 gcc_unreachable ();
6843 return x;
6846 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6847 operations" because they can be replaced with two more basic operations.
6848 ZERO_EXTEND is also considered "compound" because it can be replaced with
6849 an AND operation, which is simpler, though only one operation.
6851 The function expand_compound_operation is called with an rtx expression
6852 and will convert it to the appropriate shifts and AND operations,
6853 simplifying at each stage.
6855 The function make_compound_operation is called to convert an expression
6856 consisting of shifts and ANDs into the equivalent compound expression.
6857 It is the inverse of this function, loosely speaking. */
6859 static rtx
6860 expand_compound_operation (rtx x)
6862 unsigned HOST_WIDE_INT pos = 0, len;
6863 int unsignedp = 0;
6864 unsigned int modewidth;
6865 rtx tem;
6867 switch (GET_CODE (x))
6869 case ZERO_EXTEND:
6870 unsignedp = 1;
6871 case SIGN_EXTEND:
6872 /* We can't necessarily use a const_int for a multiword mode;
6873 it depends on implicitly extending the value.
6874 Since we don't know the right way to extend it,
6875 we can't tell whether the implicit way is right.
6877 Even for a mode that is no wider than a const_int,
6878 we can't win, because we need to sign extend one of its bits through
6879 the rest of it, and we don't know which bit. */
6880 if (CONST_INT_P (XEXP (x, 0)))
6881 return x;
6883 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6884 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6885 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6886 reloaded. If not for that, MEM's would very rarely be safe.
6888 Reject MODEs bigger than a word, because we might not be able
6889 to reference a two-register group starting with an arbitrary register
6890 (and currently gen_lowpart might crash for a SUBREG). */
6892 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6893 return x;
6895 /* Reject MODEs that aren't scalar integers because turning vector
6896 or complex modes into shifts causes problems. */
6898 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6899 return x;
6901 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6902 /* If the inner object has VOIDmode (the only way this can happen
6903 is if it is an ASM_OPERANDS), we can't do anything since we don't
6904 know how much masking to do. */
6905 if (len == 0)
6906 return x;
6908 break;
6910 case ZERO_EXTRACT:
6911 unsignedp = 1;
6913 /* ... fall through ... */
6915 case SIGN_EXTRACT:
6916 /* If the operand is a CLOBBER, just return it. */
6917 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6918 return XEXP (x, 0);
6920 if (!CONST_INT_P (XEXP (x, 1))
6921 || !CONST_INT_P (XEXP (x, 2))
6922 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6923 return x;
6925 /* Reject MODEs that aren't scalar integers because turning vector
6926 or complex modes into shifts causes problems. */
6928 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6929 return x;
6931 len = INTVAL (XEXP (x, 1));
6932 pos = INTVAL (XEXP (x, 2));
6934 /* This should stay within the object being extracted, fail otherwise. */
6935 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6936 return x;
6938 if (BITS_BIG_ENDIAN)
6939 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6941 break;
6943 default:
6944 return x;
6946 /* Convert sign extension to zero extension, if we know that the high
6947 bit is not set, as this is easier to optimize. It will be converted
6948 back to cheaper alternative in make_extraction. */
6949 if (GET_CODE (x) == SIGN_EXTEND
6950 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6951 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6952 & ~(((unsigned HOST_WIDE_INT)
6953 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6954 >> 1))
6955 == 0)))
6957 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6958 rtx temp2 = expand_compound_operation (temp);
6960 /* Make sure this is a profitable operation. */
6961 if (set_src_cost (x, optimize_this_for_speed_p)
6962 > set_src_cost (temp2, optimize_this_for_speed_p))
6963 return temp2;
6964 else if (set_src_cost (x, optimize_this_for_speed_p)
6965 > set_src_cost (temp, optimize_this_for_speed_p))
6966 return temp;
6967 else
6968 return x;
6971 /* We can optimize some special cases of ZERO_EXTEND. */
6972 if (GET_CODE (x) == ZERO_EXTEND)
6974 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6975 know that the last value didn't have any inappropriate bits
6976 set. */
6977 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6978 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6979 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6980 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6981 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6982 return XEXP (XEXP (x, 0), 0);
6984 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6985 if (GET_CODE (XEXP (x, 0)) == SUBREG
6986 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6987 && subreg_lowpart_p (XEXP (x, 0))
6988 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6989 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6990 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6991 return SUBREG_REG (XEXP (x, 0));
6993 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6994 is a comparison and STORE_FLAG_VALUE permits. This is like
6995 the first case, but it works even when GET_MODE (x) is larger
6996 than HOST_WIDE_INT. */
6997 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6998 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6999 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7000 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7001 <= HOST_BITS_PER_WIDE_INT)
7002 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7003 return XEXP (XEXP (x, 0), 0);
7005 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7006 if (GET_CODE (XEXP (x, 0)) == SUBREG
7007 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7008 && subreg_lowpart_p (XEXP (x, 0))
7009 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7010 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7011 <= HOST_BITS_PER_WIDE_INT)
7012 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7013 return SUBREG_REG (XEXP (x, 0));
7017 /* If we reach here, we want to return a pair of shifts. The inner
7018 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7019 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7020 logical depending on the value of UNSIGNEDP.
7022 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7023 converted into an AND of a shift.
7025 We must check for the case where the left shift would have a negative
7026 count. This can happen in a case like (x >> 31) & 255 on machines
7027 that can't shift by a constant. On those machines, we would first
7028 combine the shift with the AND to produce a variable-position
7029 extraction. Then the constant of 31 would be substituted in
7030 to produce such a position. */
7032 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7033 if (modewidth >= pos + len)
7035 machine_mode mode = GET_MODE (x);
7036 tem = gen_lowpart (mode, XEXP (x, 0));
7037 if (!tem || GET_CODE (tem) == CLOBBER)
7038 return x;
7039 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7040 tem, modewidth - pos - len);
7041 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7042 mode, tem, modewidth - len);
7044 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7045 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7046 simplify_shift_const (NULL_RTX, LSHIFTRT,
7047 GET_MODE (x),
7048 XEXP (x, 0), pos),
7049 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7050 else
7051 /* Any other cases we can't handle. */
7052 return x;
7054 /* If we couldn't do this for some reason, return the original
7055 expression. */
7056 if (GET_CODE (tem) == CLOBBER)
7057 return x;
7059 return tem;
7062 /* X is a SET which contains an assignment of one object into
7063 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7064 or certain SUBREGS). If possible, convert it into a series of
7065 logical operations.
7067 We half-heartedly support variable positions, but do not at all
7068 support variable lengths. */
7070 static const_rtx
7071 expand_field_assignment (const_rtx x)
7073 rtx inner;
7074 rtx pos; /* Always counts from low bit. */
7075 int len;
7076 rtx mask, cleared, masked;
7077 machine_mode compute_mode;
7079 /* Loop until we find something we can't simplify. */
7080 while (1)
7082 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7083 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7085 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7086 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7087 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7089 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7090 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7092 inner = XEXP (SET_DEST (x), 0);
7093 len = INTVAL (XEXP (SET_DEST (x), 1));
7094 pos = XEXP (SET_DEST (x), 2);
7096 /* A constant position should stay within the width of INNER. */
7097 if (CONST_INT_P (pos)
7098 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7099 break;
7101 if (BITS_BIG_ENDIAN)
7103 if (CONST_INT_P (pos))
7104 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7105 - INTVAL (pos));
7106 else if (GET_CODE (pos) == MINUS
7107 && CONST_INT_P (XEXP (pos, 1))
7108 && (INTVAL (XEXP (pos, 1))
7109 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7110 /* If position is ADJUST - X, new position is X. */
7111 pos = XEXP (pos, 0);
7112 else
7114 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7115 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7116 gen_int_mode (prec - len,
7117 GET_MODE (pos)),
7118 pos);
7123 /* A SUBREG between two modes that occupy the same numbers of words
7124 can be done by moving the SUBREG to the source. */
7125 else if (GET_CODE (SET_DEST (x)) == SUBREG
7126 /* We need SUBREGs to compute nonzero_bits properly. */
7127 && nonzero_sign_valid
7128 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7129 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7130 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7131 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7133 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7134 gen_lowpart
7135 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7136 SET_SRC (x)));
7137 continue;
7139 else
7140 break;
7142 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7143 inner = SUBREG_REG (inner);
7145 compute_mode = GET_MODE (inner);
7147 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7148 if (! SCALAR_INT_MODE_P (compute_mode))
7150 machine_mode imode;
7152 /* Don't do anything for vector or complex integral types. */
7153 if (! FLOAT_MODE_P (compute_mode))
7154 break;
7156 /* Try to find an integral mode to pun with. */
7157 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7158 if (imode == BLKmode)
7159 break;
7161 compute_mode = imode;
7162 inner = gen_lowpart (imode, inner);
7165 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7166 if (len >= HOST_BITS_PER_WIDE_INT)
7167 break;
7169 /* Now compute the equivalent expression. Make a copy of INNER
7170 for the SET_DEST in case it is a MEM into which we will substitute;
7171 we don't want shared RTL in that case. */
7172 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7173 compute_mode);
7174 cleared = simplify_gen_binary (AND, compute_mode,
7175 simplify_gen_unary (NOT, compute_mode,
7176 simplify_gen_binary (ASHIFT,
7177 compute_mode,
7178 mask, pos),
7179 compute_mode),
7180 inner);
7181 masked = simplify_gen_binary (ASHIFT, compute_mode,
7182 simplify_gen_binary (
7183 AND, compute_mode,
7184 gen_lowpart (compute_mode, SET_SRC (x)),
7185 mask),
7186 pos);
7188 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7189 simplify_gen_binary (IOR, compute_mode,
7190 cleared, masked));
7193 return x;
7196 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7197 it is an RTX that represents the (variable) starting position; otherwise,
7198 POS is the (constant) starting bit position. Both are counted from the LSB.
7200 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7202 IN_DEST is nonzero if this is a reference in the destination of a SET.
7203 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7204 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7205 be used.
7207 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7208 ZERO_EXTRACT should be built even for bits starting at bit 0.
7210 MODE is the desired mode of the result (if IN_DEST == 0).
7212 The result is an RTX for the extraction or NULL_RTX if the target
7213 can't handle it. */
7215 static rtx
7216 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7217 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7218 int in_dest, int in_compare)
7220 /* This mode describes the size of the storage area
7221 to fetch the overall value from. Within that, we
7222 ignore the POS lowest bits, etc. */
7223 machine_mode is_mode = GET_MODE (inner);
7224 machine_mode inner_mode;
7225 machine_mode wanted_inner_mode;
7226 machine_mode wanted_inner_reg_mode = word_mode;
7227 machine_mode pos_mode = word_mode;
7228 machine_mode extraction_mode = word_mode;
7229 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7230 rtx new_rtx = 0;
7231 rtx orig_pos_rtx = pos_rtx;
7232 HOST_WIDE_INT orig_pos;
7234 if (pos_rtx && CONST_INT_P (pos_rtx))
7235 pos = INTVAL (pos_rtx), pos_rtx = 0;
7237 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7239 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7240 consider just the QI as the memory to extract from.
7241 The subreg adds or removes high bits; its mode is
7242 irrelevant to the meaning of this extraction,
7243 since POS and LEN count from the lsb. */
7244 if (MEM_P (SUBREG_REG (inner)))
7245 is_mode = GET_MODE (SUBREG_REG (inner));
7246 inner = SUBREG_REG (inner);
7248 else if (GET_CODE (inner) == ASHIFT
7249 && CONST_INT_P (XEXP (inner, 1))
7250 && pos_rtx == 0 && pos == 0
7251 && len > UINTVAL (XEXP (inner, 1)))
7253 /* We're extracting the least significant bits of an rtx
7254 (ashift X (const_int C)), where LEN > C. Extract the
7255 least significant (LEN - C) bits of X, giving an rtx
7256 whose mode is MODE, then shift it left C times. */
7257 new_rtx = make_extraction (mode, XEXP (inner, 0),
7258 0, 0, len - INTVAL (XEXP (inner, 1)),
7259 unsignedp, in_dest, in_compare);
7260 if (new_rtx != 0)
7261 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7263 else if (GET_CODE (inner) == TRUNCATE)
7264 inner = XEXP (inner, 0);
7266 inner_mode = GET_MODE (inner);
7268 /* See if this can be done without an extraction. We never can if the
7269 width of the field is not the same as that of some integer mode. For
7270 registers, we can only avoid the extraction if the position is at the
7271 low-order bit and this is either not in the destination or we have the
7272 appropriate STRICT_LOW_PART operation available.
7274 For MEM, we can avoid an extract if the field starts on an appropriate
7275 boundary and we can change the mode of the memory reference. */
7277 if (tmode != BLKmode
7278 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7279 && !MEM_P (inner)
7280 && (inner_mode == tmode
7281 || !REG_P (inner)
7282 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7283 || reg_truncated_to_mode (tmode, inner))
7284 && (! in_dest
7285 || (REG_P (inner)
7286 && have_insn_for (STRICT_LOW_PART, tmode))))
7287 || (MEM_P (inner) && pos_rtx == 0
7288 && (pos
7289 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7290 : BITS_PER_UNIT)) == 0
7291 /* We can't do this if we are widening INNER_MODE (it
7292 may not be aligned, for one thing). */
7293 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7294 && (inner_mode == tmode
7295 || (! mode_dependent_address_p (XEXP (inner, 0),
7296 MEM_ADDR_SPACE (inner))
7297 && ! MEM_VOLATILE_P (inner))))))
7299 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7300 field. If the original and current mode are the same, we need not
7301 adjust the offset. Otherwise, we do if bytes big endian.
7303 If INNER is not a MEM, get a piece consisting of just the field
7304 of interest (in this case POS % BITS_PER_WORD must be 0). */
7306 if (MEM_P (inner))
7308 HOST_WIDE_INT offset;
7310 /* POS counts from lsb, but make OFFSET count in memory order. */
7311 if (BYTES_BIG_ENDIAN)
7312 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7313 else
7314 offset = pos / BITS_PER_UNIT;
7316 new_rtx = adjust_address_nv (inner, tmode, offset);
7318 else if (REG_P (inner))
7320 if (tmode != inner_mode)
7322 /* We can't call gen_lowpart in a DEST since we
7323 always want a SUBREG (see below) and it would sometimes
7324 return a new hard register. */
7325 if (pos || in_dest)
7327 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7329 if (WORDS_BIG_ENDIAN
7330 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7331 final_word = ((GET_MODE_SIZE (inner_mode)
7332 - GET_MODE_SIZE (tmode))
7333 / UNITS_PER_WORD) - final_word;
7335 final_word *= UNITS_PER_WORD;
7336 if (BYTES_BIG_ENDIAN &&
7337 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7338 final_word += (GET_MODE_SIZE (inner_mode)
7339 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7341 /* Avoid creating invalid subregs, for example when
7342 simplifying (x>>32)&255. */
7343 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7344 return NULL_RTX;
7346 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7348 else
7349 new_rtx = gen_lowpart (tmode, inner);
7351 else
7352 new_rtx = inner;
7354 else
7355 new_rtx = force_to_mode (inner, tmode,
7356 len >= HOST_BITS_PER_WIDE_INT
7357 ? ~(unsigned HOST_WIDE_INT) 0
7358 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7361 /* If this extraction is going into the destination of a SET,
7362 make a STRICT_LOW_PART unless we made a MEM. */
7364 if (in_dest)
7365 return (MEM_P (new_rtx) ? new_rtx
7366 : (GET_CODE (new_rtx) != SUBREG
7367 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7368 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7370 if (mode == tmode)
7371 return new_rtx;
7373 if (CONST_SCALAR_INT_P (new_rtx))
7374 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7375 mode, new_rtx, tmode);
7377 /* If we know that no extraneous bits are set, and that the high
7378 bit is not set, convert the extraction to the cheaper of
7379 sign and zero extension, that are equivalent in these cases. */
7380 if (flag_expensive_optimizations
7381 && (HWI_COMPUTABLE_MODE_P (tmode)
7382 && ((nonzero_bits (new_rtx, tmode)
7383 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7384 == 0)))
7386 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7387 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7389 /* Prefer ZERO_EXTENSION, since it gives more information to
7390 backends. */
7391 if (set_src_cost (temp, optimize_this_for_speed_p)
7392 <= set_src_cost (temp1, optimize_this_for_speed_p))
7393 return temp;
7394 return temp1;
7397 /* Otherwise, sign- or zero-extend unless we already are in the
7398 proper mode. */
7400 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7401 mode, new_rtx));
7404 /* Unless this is a COMPARE or we have a funny memory reference,
7405 don't do anything with zero-extending field extracts starting at
7406 the low-order bit since they are simple AND operations. */
7407 if (pos_rtx == 0 && pos == 0 && ! in_dest
7408 && ! in_compare && unsignedp)
7409 return 0;
7411 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7412 if the position is not a constant and the length is not 1. In all
7413 other cases, we would only be going outside our object in cases when
7414 an original shift would have been undefined. */
7415 if (MEM_P (inner)
7416 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7417 || (pos_rtx != 0 && len != 1)))
7418 return 0;
7420 enum extraction_pattern pattern = (in_dest ? EP_insv
7421 : unsignedp ? EP_extzv : EP_extv);
7423 /* If INNER is not from memory, we want it to have the mode of a register
7424 extraction pattern's structure operand, or word_mode if there is no
7425 such pattern. The same applies to extraction_mode and pos_mode
7426 and their respective operands.
7428 For memory, assume that the desired extraction_mode and pos_mode
7429 are the same as for a register operation, since at present we don't
7430 have named patterns for aligned memory structures. */
7431 struct extraction_insn insn;
7432 if (get_best_reg_extraction_insn (&insn, pattern,
7433 GET_MODE_BITSIZE (inner_mode), mode))
7435 wanted_inner_reg_mode = insn.struct_mode;
7436 pos_mode = insn.pos_mode;
7437 extraction_mode = insn.field_mode;
7440 /* Never narrow an object, since that might not be safe. */
7442 if (mode != VOIDmode
7443 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7444 extraction_mode = mode;
7446 if (!MEM_P (inner))
7447 wanted_inner_mode = wanted_inner_reg_mode;
7448 else
7450 /* Be careful not to go beyond the extracted object and maintain the
7451 natural alignment of the memory. */
7452 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7453 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7454 > GET_MODE_BITSIZE (wanted_inner_mode))
7456 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7457 gcc_assert (wanted_inner_mode != VOIDmode);
7461 orig_pos = pos;
7463 if (BITS_BIG_ENDIAN)
7465 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7466 BITS_BIG_ENDIAN style. If position is constant, compute new
7467 position. Otherwise, build subtraction.
7468 Note that POS is relative to the mode of the original argument.
7469 If it's a MEM we need to recompute POS relative to that.
7470 However, if we're extracting from (or inserting into) a register,
7471 we want to recompute POS relative to wanted_inner_mode. */
7472 int width = (MEM_P (inner)
7473 ? GET_MODE_BITSIZE (is_mode)
7474 : GET_MODE_BITSIZE (wanted_inner_mode));
7476 if (pos_rtx == 0)
7477 pos = width - len - pos;
7478 else
7479 pos_rtx
7480 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7481 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7482 pos_rtx);
7483 /* POS may be less than 0 now, but we check for that below.
7484 Note that it can only be less than 0 if !MEM_P (inner). */
7487 /* If INNER has a wider mode, and this is a constant extraction, try to
7488 make it smaller and adjust the byte to point to the byte containing
7489 the value. */
7490 if (wanted_inner_mode != VOIDmode
7491 && inner_mode != wanted_inner_mode
7492 && ! pos_rtx
7493 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7494 && MEM_P (inner)
7495 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7496 && ! MEM_VOLATILE_P (inner))
7498 int offset = 0;
7500 /* The computations below will be correct if the machine is big
7501 endian in both bits and bytes or little endian in bits and bytes.
7502 If it is mixed, we must adjust. */
7504 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7505 adjust OFFSET to compensate. */
7506 if (BYTES_BIG_ENDIAN
7507 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7508 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7510 /* We can now move to the desired byte. */
7511 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7512 * GET_MODE_SIZE (wanted_inner_mode);
7513 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7515 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7516 && is_mode != wanted_inner_mode)
7517 offset = (GET_MODE_SIZE (is_mode)
7518 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7520 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7523 /* If INNER is not memory, get it into the proper mode. If we are changing
7524 its mode, POS must be a constant and smaller than the size of the new
7525 mode. */
7526 else if (!MEM_P (inner))
7528 /* On the LHS, don't create paradoxical subregs implicitely truncating
7529 the register unless TRULY_NOOP_TRUNCATION. */
7530 if (in_dest
7531 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7532 wanted_inner_mode))
7533 return NULL_RTX;
7535 if (GET_MODE (inner) != wanted_inner_mode
7536 && (pos_rtx != 0
7537 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7538 return NULL_RTX;
7540 if (orig_pos < 0)
7541 return NULL_RTX;
7543 inner = force_to_mode (inner, wanted_inner_mode,
7544 pos_rtx
7545 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7546 ? ~(unsigned HOST_WIDE_INT) 0
7547 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7548 << orig_pos),
7552 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7553 have to zero extend. Otherwise, we can just use a SUBREG. */
7554 if (pos_rtx != 0
7555 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7557 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7558 GET_MODE (pos_rtx));
7560 /* If we know that no extraneous bits are set, and that the high
7561 bit is not set, convert extraction to cheaper one - either
7562 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7563 cases. */
7564 if (flag_expensive_optimizations
7565 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7566 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7567 & ~(((unsigned HOST_WIDE_INT)
7568 GET_MODE_MASK (GET_MODE (pos_rtx)))
7569 >> 1))
7570 == 0)))
7572 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7573 GET_MODE (pos_rtx));
7575 /* Prefer ZERO_EXTENSION, since it gives more information to
7576 backends. */
7577 if (set_src_cost (temp1, optimize_this_for_speed_p)
7578 < set_src_cost (temp, optimize_this_for_speed_p))
7579 temp = temp1;
7581 pos_rtx = temp;
7584 /* Make POS_RTX unless we already have it and it is correct. If we don't
7585 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7586 be a CONST_INT. */
7587 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7588 pos_rtx = orig_pos_rtx;
7590 else if (pos_rtx == 0)
7591 pos_rtx = GEN_INT (pos);
7593 /* Make the required operation. See if we can use existing rtx. */
7594 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7595 extraction_mode, inner, GEN_INT (len), pos_rtx);
7596 if (! in_dest)
7597 new_rtx = gen_lowpart (mode, new_rtx);
7599 return new_rtx;
7602 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7603 with any other operations in X. Return X without that shift if so. */
7605 static rtx
7606 extract_left_shift (rtx x, int count)
7608 enum rtx_code code = GET_CODE (x);
7609 machine_mode mode = GET_MODE (x);
7610 rtx tem;
7612 switch (code)
7614 case ASHIFT:
7615 /* This is the shift itself. If it is wide enough, we will return
7616 either the value being shifted if the shift count is equal to
7617 COUNT or a shift for the difference. */
7618 if (CONST_INT_P (XEXP (x, 1))
7619 && INTVAL (XEXP (x, 1)) >= count)
7620 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7621 INTVAL (XEXP (x, 1)) - count);
7622 break;
7624 case NEG: case NOT:
7625 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7626 return simplify_gen_unary (code, mode, tem, mode);
7628 break;
7630 case PLUS: case IOR: case XOR: case AND:
7631 /* If we can safely shift this constant and we find the inner shift,
7632 make a new operation. */
7633 if (CONST_INT_P (XEXP (x, 1))
7634 && (UINTVAL (XEXP (x, 1))
7635 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7636 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7638 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7639 return simplify_gen_binary (code, mode, tem,
7640 gen_int_mode (val, mode));
7642 break;
7644 default:
7645 break;
7648 return 0;
7651 /* Look at the expression rooted at X. Look for expressions
7652 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7653 Form these expressions.
7655 Return the new rtx, usually just X.
7657 Also, for machines like the VAX that don't have logical shift insns,
7658 try to convert logical to arithmetic shift operations in cases where
7659 they are equivalent. This undoes the canonicalizations to logical
7660 shifts done elsewhere.
7662 We try, as much as possible, to re-use rtl expressions to save memory.
7664 IN_CODE says what kind of expression we are processing. Normally, it is
7665 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7666 being kludges), it is MEM. When processing the arguments of a comparison
7667 or a COMPARE against zero, it is COMPARE. */
7670 make_compound_operation (rtx x, enum rtx_code in_code)
7672 enum rtx_code code = GET_CODE (x);
7673 machine_mode mode = GET_MODE (x);
7674 int mode_width = GET_MODE_PRECISION (mode);
7675 rtx rhs, lhs;
7676 enum rtx_code next_code;
7677 int i, j;
7678 rtx new_rtx = 0;
7679 rtx tem;
7680 const char *fmt;
7682 /* Select the code to be used in recursive calls. Once we are inside an
7683 address, we stay there. If we have a comparison, set to COMPARE,
7684 but once inside, go back to our default of SET. */
7686 next_code = (code == MEM ? MEM
7687 : ((code == PLUS || code == MINUS)
7688 && SCALAR_INT_MODE_P (mode)) ? MEM
7689 : ((code == COMPARE || COMPARISON_P (x))
7690 && XEXP (x, 1) == const0_rtx) ? COMPARE
7691 : in_code == COMPARE ? SET : in_code);
7693 /* Process depending on the code of this operation. If NEW is set
7694 nonzero, it will be returned. */
7696 switch (code)
7698 case ASHIFT:
7699 /* Convert shifts by constants into multiplications if inside
7700 an address. */
7701 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7702 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7703 && INTVAL (XEXP (x, 1)) >= 0
7704 && SCALAR_INT_MODE_P (mode))
7706 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7707 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7709 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7710 if (GET_CODE (new_rtx) == NEG)
7712 new_rtx = XEXP (new_rtx, 0);
7713 multval = -multval;
7715 multval = trunc_int_for_mode (multval, mode);
7716 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7718 break;
7720 case PLUS:
7721 lhs = XEXP (x, 0);
7722 rhs = XEXP (x, 1);
7723 lhs = make_compound_operation (lhs, next_code);
7724 rhs = make_compound_operation (rhs, next_code);
7725 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7726 && SCALAR_INT_MODE_P (mode))
7728 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7729 XEXP (lhs, 1));
7730 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7732 else if (GET_CODE (lhs) == MULT
7733 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7735 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7736 simplify_gen_unary (NEG, mode,
7737 XEXP (lhs, 1),
7738 mode));
7739 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7741 else
7743 SUBST (XEXP (x, 0), lhs);
7744 SUBST (XEXP (x, 1), rhs);
7745 goto maybe_swap;
7747 x = gen_lowpart (mode, new_rtx);
7748 goto maybe_swap;
7750 case MINUS:
7751 lhs = XEXP (x, 0);
7752 rhs = XEXP (x, 1);
7753 lhs = make_compound_operation (lhs, next_code);
7754 rhs = make_compound_operation (rhs, next_code);
7755 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7756 && SCALAR_INT_MODE_P (mode))
7758 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7759 XEXP (rhs, 1));
7760 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7762 else if (GET_CODE (rhs) == MULT
7763 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7765 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7766 simplify_gen_unary (NEG, mode,
7767 XEXP (rhs, 1),
7768 mode));
7769 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7771 else
7773 SUBST (XEXP (x, 0), lhs);
7774 SUBST (XEXP (x, 1), rhs);
7775 return x;
7777 return gen_lowpart (mode, new_rtx);
7779 case AND:
7780 /* If the second operand is not a constant, we can't do anything
7781 with it. */
7782 if (!CONST_INT_P (XEXP (x, 1)))
7783 break;
7785 /* If the constant is a power of two minus one and the first operand
7786 is a logical right shift, make an extraction. */
7787 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7788 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7790 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7791 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7792 0, in_code == COMPARE);
7795 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7796 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7797 && subreg_lowpart_p (XEXP (x, 0))
7798 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7799 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7801 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7802 next_code);
7803 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7804 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7805 0, in_code == COMPARE);
7807 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7808 else if ((GET_CODE (XEXP (x, 0)) == XOR
7809 || GET_CODE (XEXP (x, 0)) == IOR)
7810 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7811 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7812 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7814 /* Apply the distributive law, and then try to make extractions. */
7815 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7816 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7817 XEXP (x, 1)),
7818 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7819 XEXP (x, 1)));
7820 new_rtx = make_compound_operation (new_rtx, in_code);
7823 /* If we are have (and (rotate X C) M) and C is larger than the number
7824 of bits in M, this is an extraction. */
7826 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7827 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7828 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7829 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7831 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7832 new_rtx = make_extraction (mode, new_rtx,
7833 (GET_MODE_PRECISION (mode)
7834 - INTVAL (XEXP (XEXP (x, 0), 1))),
7835 NULL_RTX, i, 1, 0, in_code == COMPARE);
7838 /* On machines without logical shifts, if the operand of the AND is
7839 a logical shift and our mask turns off all the propagated sign
7840 bits, we can replace the logical shift with an arithmetic shift. */
7841 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7842 && !have_insn_for (LSHIFTRT, mode)
7843 && have_insn_for (ASHIFTRT, mode)
7844 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7845 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7846 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7847 && mode_width <= HOST_BITS_PER_WIDE_INT)
7849 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7851 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7852 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7853 SUBST (XEXP (x, 0),
7854 gen_rtx_ASHIFTRT (mode,
7855 make_compound_operation
7856 (XEXP (XEXP (x, 0), 0), next_code),
7857 XEXP (XEXP (x, 0), 1)));
7860 /* If the constant is one less than a power of two, this might be
7861 representable by an extraction even if no shift is present.
7862 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7863 we are in a COMPARE. */
7864 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7865 new_rtx = make_extraction (mode,
7866 make_compound_operation (XEXP (x, 0),
7867 next_code),
7868 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7870 /* If we are in a comparison and this is an AND with a power of two,
7871 convert this into the appropriate bit extract. */
7872 else if (in_code == COMPARE
7873 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7874 new_rtx = make_extraction (mode,
7875 make_compound_operation (XEXP (x, 0),
7876 next_code),
7877 i, NULL_RTX, 1, 1, 0, 1);
7879 break;
7881 case LSHIFTRT:
7882 /* If the sign bit is known to be zero, replace this with an
7883 arithmetic shift. */
7884 if (have_insn_for (ASHIFTRT, mode)
7885 && ! have_insn_for (LSHIFTRT, mode)
7886 && mode_width <= HOST_BITS_PER_WIDE_INT
7887 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7889 new_rtx = gen_rtx_ASHIFTRT (mode,
7890 make_compound_operation (XEXP (x, 0),
7891 next_code),
7892 XEXP (x, 1));
7893 break;
7896 /* ... fall through ... */
7898 case ASHIFTRT:
7899 lhs = XEXP (x, 0);
7900 rhs = XEXP (x, 1);
7902 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7903 this is a SIGN_EXTRACT. */
7904 if (CONST_INT_P (rhs)
7905 && GET_CODE (lhs) == ASHIFT
7906 && CONST_INT_P (XEXP (lhs, 1))
7907 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7908 && INTVAL (XEXP (lhs, 1)) >= 0
7909 && INTVAL (rhs) < mode_width)
7911 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7912 new_rtx = make_extraction (mode, new_rtx,
7913 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7914 NULL_RTX, mode_width - INTVAL (rhs),
7915 code == LSHIFTRT, 0, in_code == COMPARE);
7916 break;
7919 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7920 If so, try to merge the shifts into a SIGN_EXTEND. We could
7921 also do this for some cases of SIGN_EXTRACT, but it doesn't
7922 seem worth the effort; the case checked for occurs on Alpha. */
7924 if (!OBJECT_P (lhs)
7925 && ! (GET_CODE (lhs) == SUBREG
7926 && (OBJECT_P (SUBREG_REG (lhs))))
7927 && CONST_INT_P (rhs)
7928 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7929 && INTVAL (rhs) < mode_width
7930 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7931 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7932 0, NULL_RTX, mode_width - INTVAL (rhs),
7933 code == LSHIFTRT, 0, in_code == COMPARE);
7935 break;
7937 case SUBREG:
7938 /* Call ourselves recursively on the inner expression. If we are
7939 narrowing the object and it has a different RTL code from
7940 what it originally did, do this SUBREG as a force_to_mode. */
7942 rtx inner = SUBREG_REG (x), simplified;
7943 enum rtx_code subreg_code = in_code;
7945 /* If in_code is COMPARE, it isn't always safe to pass it through
7946 to the recursive make_compound_operation call. */
7947 if (subreg_code == COMPARE
7948 && (!subreg_lowpart_p (x)
7949 || GET_CODE (inner) == SUBREG
7950 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7951 is (const_int 0), rather than
7952 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7953 || (GET_CODE (inner) == AND
7954 && CONST_INT_P (XEXP (inner, 1))
7955 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7956 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7957 >= GET_MODE_BITSIZE (mode))))
7958 subreg_code = SET;
7960 tem = make_compound_operation (inner, subreg_code);
7962 simplified
7963 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7964 if (simplified)
7965 tem = simplified;
7967 if (GET_CODE (tem) != GET_CODE (inner)
7968 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7969 && subreg_lowpart_p (x))
7971 rtx newer
7972 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7974 /* If we have something other than a SUBREG, we might have
7975 done an expansion, so rerun ourselves. */
7976 if (GET_CODE (newer) != SUBREG)
7977 newer = make_compound_operation (newer, in_code);
7979 /* force_to_mode can expand compounds. If it just re-expanded the
7980 compound, use gen_lowpart to convert to the desired mode. */
7981 if (rtx_equal_p (newer, x)
7982 /* Likewise if it re-expanded the compound only partially.
7983 This happens for SUBREG of ZERO_EXTRACT if they extract
7984 the same number of bits. */
7985 || (GET_CODE (newer) == SUBREG
7986 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7987 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7988 && GET_CODE (inner) == AND
7989 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7990 return gen_lowpart (GET_MODE (x), tem);
7992 return newer;
7995 if (simplified)
7996 return tem;
7998 break;
8000 default:
8001 break;
8004 if (new_rtx)
8006 x = gen_lowpart (mode, new_rtx);
8007 code = GET_CODE (x);
8010 /* Now recursively process each operand of this operation. We need to
8011 handle ZERO_EXTEND specially so that we don't lose track of the
8012 inner mode. */
8013 if (GET_CODE (x) == ZERO_EXTEND)
8015 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8016 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8017 new_rtx, GET_MODE (XEXP (x, 0)));
8018 if (tem)
8019 return tem;
8020 SUBST (XEXP (x, 0), new_rtx);
8021 return x;
8024 fmt = GET_RTX_FORMAT (code);
8025 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8026 if (fmt[i] == 'e')
8028 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8029 SUBST (XEXP (x, i), new_rtx);
8031 else if (fmt[i] == 'E')
8032 for (j = 0; j < XVECLEN (x, i); j++)
8034 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8035 SUBST (XVECEXP (x, i, j), new_rtx);
8038 maybe_swap:
8039 /* If this is a commutative operation, the changes to the operands
8040 may have made it noncanonical. */
8041 if (COMMUTATIVE_ARITH_P (x)
8042 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8044 tem = XEXP (x, 0);
8045 SUBST (XEXP (x, 0), XEXP (x, 1));
8046 SUBST (XEXP (x, 1), tem);
8049 return x;
8052 /* Given M see if it is a value that would select a field of bits
8053 within an item, but not the entire word. Return -1 if not.
8054 Otherwise, return the starting position of the field, where 0 is the
8055 low-order bit.
8057 *PLEN is set to the length of the field. */
8059 static int
8060 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8062 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8063 int pos = m ? ctz_hwi (m) : -1;
8064 int len = 0;
8066 if (pos >= 0)
8067 /* Now shift off the low-order zero bits and see if we have a
8068 power of two minus 1. */
8069 len = exact_log2 ((m >> pos) + 1);
8071 if (len <= 0)
8072 pos = -1;
8074 *plen = len;
8075 return pos;
8078 /* If X refers to a register that equals REG in value, replace these
8079 references with REG. */
8080 static rtx
8081 canon_reg_for_combine (rtx x, rtx reg)
8083 rtx op0, op1, op2;
8084 const char *fmt;
8085 int i;
8086 bool copied;
8088 enum rtx_code code = GET_CODE (x);
8089 switch (GET_RTX_CLASS (code))
8091 case RTX_UNARY:
8092 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8093 if (op0 != XEXP (x, 0))
8094 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8095 GET_MODE (reg));
8096 break;
8098 case RTX_BIN_ARITH:
8099 case RTX_COMM_ARITH:
8100 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8101 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8102 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8103 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8104 break;
8106 case RTX_COMPARE:
8107 case RTX_COMM_COMPARE:
8108 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8109 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8110 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8111 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8112 GET_MODE (op0), op0, op1);
8113 break;
8115 case RTX_TERNARY:
8116 case RTX_BITFIELD_OPS:
8117 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8118 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8119 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8120 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8121 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8122 GET_MODE (op0), op0, op1, op2);
8124 case RTX_OBJ:
8125 if (REG_P (x))
8127 if (rtx_equal_p (get_last_value (reg), x)
8128 || rtx_equal_p (reg, get_last_value (x)))
8129 return reg;
8130 else
8131 break;
8134 /* fall through */
8136 default:
8137 fmt = GET_RTX_FORMAT (code);
8138 copied = false;
8139 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8140 if (fmt[i] == 'e')
8142 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8143 if (op != XEXP (x, i))
8145 if (!copied)
8147 copied = true;
8148 x = copy_rtx (x);
8150 XEXP (x, i) = op;
8153 else if (fmt[i] == 'E')
8155 int j;
8156 for (j = 0; j < XVECLEN (x, i); j++)
8158 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8159 if (op != XVECEXP (x, i, j))
8161 if (!copied)
8163 copied = true;
8164 x = copy_rtx (x);
8166 XVECEXP (x, i, j) = op;
8171 break;
8174 return x;
8177 /* Return X converted to MODE. If the value is already truncated to
8178 MODE we can just return a subreg even though in the general case we
8179 would need an explicit truncation. */
8181 static rtx
8182 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8184 if (!CONST_INT_P (x)
8185 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8186 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8187 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8189 /* Bit-cast X into an integer mode. */
8190 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8191 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8192 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8193 x, GET_MODE (x));
8196 return gen_lowpart (mode, x);
8199 /* See if X can be simplified knowing that we will only refer to it in
8200 MODE and will only refer to those bits that are nonzero in MASK.
8201 If other bits are being computed or if masking operations are done
8202 that select a superset of the bits in MASK, they can sometimes be
8203 ignored.
8205 Return a possibly simplified expression, but always convert X to
8206 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8208 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8209 are all off in X. This is used when X will be complemented, by either
8210 NOT, NEG, or XOR. */
8212 static rtx
8213 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8214 int just_select)
8216 enum rtx_code code = GET_CODE (x);
8217 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8218 machine_mode op_mode;
8219 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8220 rtx op0, op1, temp;
8222 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8223 code below will do the wrong thing since the mode of such an
8224 expression is VOIDmode.
8226 Also do nothing if X is a CLOBBER; this can happen if X was
8227 the return value from a call to gen_lowpart. */
8228 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8229 return x;
8231 /* We want to perform the operation in its present mode unless we know
8232 that the operation is valid in MODE, in which case we do the operation
8233 in MODE. */
8234 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8235 && have_insn_for (code, mode))
8236 ? mode : GET_MODE (x));
8238 /* It is not valid to do a right-shift in a narrower mode
8239 than the one it came in with. */
8240 if ((code == LSHIFTRT || code == ASHIFTRT)
8241 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8242 op_mode = GET_MODE (x);
8244 /* Truncate MASK to fit OP_MODE. */
8245 if (op_mode)
8246 mask &= GET_MODE_MASK (op_mode);
8248 /* When we have an arithmetic operation, or a shift whose count we
8249 do not know, we need to assume that all bits up to the highest-order
8250 bit in MASK will be needed. This is how we form such a mask. */
8251 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8252 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8253 else
8254 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8255 - 1);
8257 /* Determine what bits of X are guaranteed to be (non)zero. */
8258 nonzero = nonzero_bits (x, mode);
8260 /* If none of the bits in X are needed, return a zero. */
8261 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8262 x = const0_rtx;
8264 /* If X is a CONST_INT, return a new one. Do this here since the
8265 test below will fail. */
8266 if (CONST_INT_P (x))
8268 if (SCALAR_INT_MODE_P (mode))
8269 return gen_int_mode (INTVAL (x) & mask, mode);
8270 else
8272 x = GEN_INT (INTVAL (x) & mask);
8273 return gen_lowpart_common (mode, x);
8277 /* If X is narrower than MODE and we want all the bits in X's mode, just
8278 get X in the proper mode. */
8279 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8280 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8281 return gen_lowpart (mode, x);
8283 /* We can ignore the effect of a SUBREG if it narrows the mode or
8284 if the constant masks to zero all the bits the mode doesn't have. */
8285 if (GET_CODE (x) == SUBREG
8286 && subreg_lowpart_p (x)
8287 && ((GET_MODE_SIZE (GET_MODE (x))
8288 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8289 || (0 == (mask
8290 & GET_MODE_MASK (GET_MODE (x))
8291 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8292 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8294 /* The arithmetic simplifications here only work for scalar integer modes. */
8295 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8296 return gen_lowpart_or_truncate (mode, x);
8298 switch (code)
8300 case CLOBBER:
8301 /* If X is a (clobber (const_int)), return it since we know we are
8302 generating something that won't match. */
8303 return x;
8305 case SIGN_EXTEND:
8306 case ZERO_EXTEND:
8307 case ZERO_EXTRACT:
8308 case SIGN_EXTRACT:
8309 x = expand_compound_operation (x);
8310 if (GET_CODE (x) != code)
8311 return force_to_mode (x, mode, mask, next_select);
8312 break;
8314 case TRUNCATE:
8315 /* Similarly for a truncate. */
8316 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8318 case AND:
8319 /* If this is an AND with a constant, convert it into an AND
8320 whose constant is the AND of that constant with MASK. If it
8321 remains an AND of MASK, delete it since it is redundant. */
8323 if (CONST_INT_P (XEXP (x, 1)))
8325 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8326 mask & INTVAL (XEXP (x, 1)));
8328 /* If X is still an AND, see if it is an AND with a mask that
8329 is just some low-order bits. If so, and it is MASK, we don't
8330 need it. */
8332 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8333 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8334 == mask))
8335 x = XEXP (x, 0);
8337 /* If it remains an AND, try making another AND with the bits
8338 in the mode mask that aren't in MASK turned on. If the
8339 constant in the AND is wide enough, this might make a
8340 cheaper constant. */
8342 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8343 && GET_MODE_MASK (GET_MODE (x)) != mask
8344 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8346 unsigned HOST_WIDE_INT cval
8347 = UINTVAL (XEXP (x, 1))
8348 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8349 rtx y;
8351 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8352 gen_int_mode (cval, GET_MODE (x)));
8353 if (set_src_cost (y, optimize_this_for_speed_p)
8354 < set_src_cost (x, optimize_this_for_speed_p))
8355 x = y;
8358 break;
8361 goto binop;
8363 case PLUS:
8364 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8365 low-order bits (as in an alignment operation) and FOO is already
8366 aligned to that boundary, mask C1 to that boundary as well.
8367 This may eliminate that PLUS and, later, the AND. */
8370 unsigned int width = GET_MODE_PRECISION (mode);
8371 unsigned HOST_WIDE_INT smask = mask;
8373 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8374 number, sign extend it. */
8376 if (width < HOST_BITS_PER_WIDE_INT
8377 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8378 smask |= HOST_WIDE_INT_M1U << width;
8380 if (CONST_INT_P (XEXP (x, 1))
8381 && exact_log2 (- smask) >= 0
8382 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8383 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8384 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8385 (INTVAL (XEXP (x, 1)) & smask)),
8386 mode, smask, next_select);
8389 /* ... fall through ... */
8391 case MULT:
8392 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8393 most significant bit in MASK since carries from those bits will
8394 affect the bits we are interested in. */
8395 mask = fuller_mask;
8396 goto binop;
8398 case MINUS:
8399 /* If X is (minus C Y) where C's least set bit is larger than any bit
8400 in the mask, then we may replace with (neg Y). */
8401 if (CONST_INT_P (XEXP (x, 0))
8402 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8404 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8405 GET_MODE (x));
8406 return force_to_mode (x, mode, mask, next_select);
8409 /* Similarly, if C contains every bit in the fuller_mask, then we may
8410 replace with (not Y). */
8411 if (CONST_INT_P (XEXP (x, 0))
8412 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8414 x = simplify_gen_unary (NOT, GET_MODE (x),
8415 XEXP (x, 1), GET_MODE (x));
8416 return force_to_mode (x, mode, mask, next_select);
8419 mask = fuller_mask;
8420 goto binop;
8422 case IOR:
8423 case XOR:
8424 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8425 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8426 operation which may be a bitfield extraction. Ensure that the
8427 constant we form is not wider than the mode of X. */
8429 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8430 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8431 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8432 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8433 && CONST_INT_P (XEXP (x, 1))
8434 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8435 + floor_log2 (INTVAL (XEXP (x, 1))))
8436 < GET_MODE_PRECISION (GET_MODE (x)))
8437 && (UINTVAL (XEXP (x, 1))
8438 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8440 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8441 << INTVAL (XEXP (XEXP (x, 0), 1)),
8442 GET_MODE (x));
8443 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8444 XEXP (XEXP (x, 0), 0), temp);
8445 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8446 XEXP (XEXP (x, 0), 1));
8447 return force_to_mode (x, mode, mask, next_select);
8450 binop:
8451 /* For most binary operations, just propagate into the operation and
8452 change the mode if we have an operation of that mode. */
8454 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8455 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8457 /* If we ended up truncating both operands, truncate the result of the
8458 operation instead. */
8459 if (GET_CODE (op0) == TRUNCATE
8460 && GET_CODE (op1) == TRUNCATE)
8462 op0 = XEXP (op0, 0);
8463 op1 = XEXP (op1, 0);
8466 op0 = gen_lowpart_or_truncate (op_mode, op0);
8467 op1 = gen_lowpart_or_truncate (op_mode, op1);
8469 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8470 x = simplify_gen_binary (code, op_mode, op0, op1);
8471 break;
8473 case ASHIFT:
8474 /* For left shifts, do the same, but just for the first operand.
8475 However, we cannot do anything with shifts where we cannot
8476 guarantee that the counts are smaller than the size of the mode
8477 because such a count will have a different meaning in a
8478 wider mode. */
8480 if (! (CONST_INT_P (XEXP (x, 1))
8481 && INTVAL (XEXP (x, 1)) >= 0
8482 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8483 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8484 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8485 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8486 break;
8488 /* If the shift count is a constant and we can do arithmetic in
8489 the mode of the shift, refine which bits we need. Otherwise, use the
8490 conservative form of the mask. */
8491 if (CONST_INT_P (XEXP (x, 1))
8492 && INTVAL (XEXP (x, 1)) >= 0
8493 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8494 && HWI_COMPUTABLE_MODE_P (op_mode))
8495 mask >>= INTVAL (XEXP (x, 1));
8496 else
8497 mask = fuller_mask;
8499 op0 = gen_lowpart_or_truncate (op_mode,
8500 force_to_mode (XEXP (x, 0), op_mode,
8501 mask, next_select));
8503 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8504 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8505 break;
8507 case LSHIFTRT:
8508 /* Here we can only do something if the shift count is a constant,
8509 this shift constant is valid for the host, and we can do arithmetic
8510 in OP_MODE. */
8512 if (CONST_INT_P (XEXP (x, 1))
8513 && INTVAL (XEXP (x, 1)) >= 0
8514 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8515 && HWI_COMPUTABLE_MODE_P (op_mode))
8517 rtx inner = XEXP (x, 0);
8518 unsigned HOST_WIDE_INT inner_mask;
8520 /* Select the mask of the bits we need for the shift operand. */
8521 inner_mask = mask << INTVAL (XEXP (x, 1));
8523 /* We can only change the mode of the shift if we can do arithmetic
8524 in the mode of the shift and INNER_MASK is no wider than the
8525 width of X's mode. */
8526 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8527 op_mode = GET_MODE (x);
8529 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8531 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8532 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8535 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8536 shift and AND produces only copies of the sign bit (C2 is one less
8537 than a power of two), we can do this with just a shift. */
8539 if (GET_CODE (x) == LSHIFTRT
8540 && CONST_INT_P (XEXP (x, 1))
8541 /* The shift puts one of the sign bit copies in the least significant
8542 bit. */
8543 && ((INTVAL (XEXP (x, 1))
8544 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8545 >= GET_MODE_PRECISION (GET_MODE (x)))
8546 && exact_log2 (mask + 1) >= 0
8547 /* Number of bits left after the shift must be more than the mask
8548 needs. */
8549 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8550 <= GET_MODE_PRECISION (GET_MODE (x)))
8551 /* Must be more sign bit copies than the mask needs. */
8552 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8553 >= exact_log2 (mask + 1)))
8554 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8555 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8556 - exact_log2 (mask + 1)));
8558 goto shiftrt;
8560 case ASHIFTRT:
8561 /* If we are just looking for the sign bit, we don't need this shift at
8562 all, even if it has a variable count. */
8563 if (val_signbit_p (GET_MODE (x), mask))
8564 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8566 /* If this is a shift by a constant, get a mask that contains those bits
8567 that are not copies of the sign bit. We then have two cases: If
8568 MASK only includes those bits, this can be a logical shift, which may
8569 allow simplifications. If MASK is a single-bit field not within
8570 those bits, we are requesting a copy of the sign bit and hence can
8571 shift the sign bit to the appropriate location. */
8573 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8574 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8576 int i;
8578 /* If the considered data is wider than HOST_WIDE_INT, we can't
8579 represent a mask for all its bits in a single scalar.
8580 But we only care about the lower bits, so calculate these. */
8582 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8584 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8586 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8587 is the number of bits a full-width mask would have set.
8588 We need only shift if these are fewer than nonzero can
8589 hold. If not, we must keep all bits set in nonzero. */
8591 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8592 < HOST_BITS_PER_WIDE_INT)
8593 nonzero >>= INTVAL (XEXP (x, 1))
8594 + HOST_BITS_PER_WIDE_INT
8595 - GET_MODE_PRECISION (GET_MODE (x)) ;
8597 else
8599 nonzero = GET_MODE_MASK (GET_MODE (x));
8600 nonzero >>= INTVAL (XEXP (x, 1));
8603 if ((mask & ~nonzero) == 0)
8605 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8606 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8607 if (GET_CODE (x) != ASHIFTRT)
8608 return force_to_mode (x, mode, mask, next_select);
8611 else if ((i = exact_log2 (mask)) >= 0)
8613 x = simplify_shift_const
8614 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8615 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8617 if (GET_CODE (x) != ASHIFTRT)
8618 return force_to_mode (x, mode, mask, next_select);
8622 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8623 even if the shift count isn't a constant. */
8624 if (mask == 1)
8625 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8626 XEXP (x, 0), XEXP (x, 1));
8628 shiftrt:
8630 /* If this is a zero- or sign-extension operation that just affects bits
8631 we don't care about, remove it. Be sure the call above returned
8632 something that is still a shift. */
8634 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8635 && CONST_INT_P (XEXP (x, 1))
8636 && INTVAL (XEXP (x, 1)) >= 0
8637 && (INTVAL (XEXP (x, 1))
8638 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8639 && GET_CODE (XEXP (x, 0)) == ASHIFT
8640 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8641 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8642 next_select);
8644 break;
8646 case ROTATE:
8647 case ROTATERT:
8648 /* If the shift count is constant and we can do computations
8649 in the mode of X, compute where the bits we care about are.
8650 Otherwise, we can't do anything. Don't change the mode of
8651 the shift or propagate MODE into the shift, though. */
8652 if (CONST_INT_P (XEXP (x, 1))
8653 && INTVAL (XEXP (x, 1)) >= 0)
8655 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8656 GET_MODE (x),
8657 gen_int_mode (mask, GET_MODE (x)),
8658 XEXP (x, 1));
8659 if (temp && CONST_INT_P (temp))
8660 x = simplify_gen_binary (code, GET_MODE (x),
8661 force_to_mode (XEXP (x, 0), GET_MODE (x),
8662 INTVAL (temp), next_select),
8663 XEXP (x, 1));
8665 break;
8667 case NEG:
8668 /* If we just want the low-order bit, the NEG isn't needed since it
8669 won't change the low-order bit. */
8670 if (mask == 1)
8671 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8673 /* We need any bits less significant than the most significant bit in
8674 MASK since carries from those bits will affect the bits we are
8675 interested in. */
8676 mask = fuller_mask;
8677 goto unop;
8679 case NOT:
8680 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8681 same as the XOR case above. Ensure that the constant we form is not
8682 wider than the mode of X. */
8684 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8685 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8686 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8687 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8688 < GET_MODE_PRECISION (GET_MODE (x)))
8689 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8691 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8692 GET_MODE (x));
8693 temp = simplify_gen_binary (XOR, GET_MODE (x),
8694 XEXP (XEXP (x, 0), 0), temp);
8695 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8696 temp, XEXP (XEXP (x, 0), 1));
8698 return force_to_mode (x, mode, mask, next_select);
8701 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8702 use the full mask inside the NOT. */
8703 mask = fuller_mask;
8705 unop:
8706 op0 = gen_lowpart_or_truncate (op_mode,
8707 force_to_mode (XEXP (x, 0), mode, mask,
8708 next_select));
8709 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8710 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8711 break;
8713 case NE:
8714 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8715 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8716 which is equal to STORE_FLAG_VALUE. */
8717 if ((mask & ~STORE_FLAG_VALUE) == 0
8718 && XEXP (x, 1) == const0_rtx
8719 && GET_MODE (XEXP (x, 0)) == mode
8720 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8721 && (nonzero_bits (XEXP (x, 0), mode)
8722 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8723 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8725 break;
8727 case IF_THEN_ELSE:
8728 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8729 written in a narrower mode. We play it safe and do not do so. */
8731 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8732 force_to_mode (XEXP (x, 1), mode,
8733 mask, next_select));
8734 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8735 force_to_mode (XEXP (x, 2), mode,
8736 mask, next_select));
8737 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8738 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8739 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8740 op0, op1);
8741 break;
8743 default:
8744 break;
8747 /* Ensure we return a value of the proper mode. */
8748 return gen_lowpart_or_truncate (mode, x);
8751 /* Return nonzero if X is an expression that has one of two values depending on
8752 whether some other value is zero or nonzero. In that case, we return the
8753 value that is being tested, *PTRUE is set to the value if the rtx being
8754 returned has a nonzero value, and *PFALSE is set to the other alternative.
8756 If we return zero, we set *PTRUE and *PFALSE to X. */
8758 static rtx
8759 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8761 machine_mode mode = GET_MODE (x);
8762 enum rtx_code code = GET_CODE (x);
8763 rtx cond0, cond1, true0, true1, false0, false1;
8764 unsigned HOST_WIDE_INT nz;
8766 /* If we are comparing a value against zero, we are done. */
8767 if ((code == NE || code == EQ)
8768 && XEXP (x, 1) == const0_rtx)
8770 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8771 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8772 return XEXP (x, 0);
8775 /* If this is a unary operation whose operand has one of two values, apply
8776 our opcode to compute those values. */
8777 else if (UNARY_P (x)
8778 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8780 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8781 *pfalse = simplify_gen_unary (code, mode, false0,
8782 GET_MODE (XEXP (x, 0)));
8783 return cond0;
8786 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8787 make can't possibly match and would suppress other optimizations. */
8788 else if (code == COMPARE)
8791 /* If this is a binary operation, see if either side has only one of two
8792 values. If either one does or if both do and they are conditional on
8793 the same value, compute the new true and false values. */
8794 else if (BINARY_P (x))
8796 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8797 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8799 if ((cond0 != 0 || cond1 != 0)
8800 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8802 /* If if_then_else_cond returned zero, then true/false are the
8803 same rtl. We must copy one of them to prevent invalid rtl
8804 sharing. */
8805 if (cond0 == 0)
8806 true0 = copy_rtx (true0);
8807 else if (cond1 == 0)
8808 true1 = copy_rtx (true1);
8810 if (COMPARISON_P (x))
8812 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8813 true0, true1);
8814 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8815 false0, false1);
8817 else
8819 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8820 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8823 return cond0 ? cond0 : cond1;
8826 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8827 operands is zero when the other is nonzero, and vice-versa,
8828 and STORE_FLAG_VALUE is 1 or -1. */
8830 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8831 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8832 || code == UMAX)
8833 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8835 rtx op0 = XEXP (XEXP (x, 0), 1);
8836 rtx op1 = XEXP (XEXP (x, 1), 1);
8838 cond0 = XEXP (XEXP (x, 0), 0);
8839 cond1 = XEXP (XEXP (x, 1), 0);
8841 if (COMPARISON_P (cond0)
8842 && COMPARISON_P (cond1)
8843 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8844 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8845 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8846 || ((swap_condition (GET_CODE (cond0))
8847 == reversed_comparison_code (cond1, NULL))
8848 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8849 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8850 && ! side_effects_p (x))
8852 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8853 *pfalse = simplify_gen_binary (MULT, mode,
8854 (code == MINUS
8855 ? simplify_gen_unary (NEG, mode,
8856 op1, mode)
8857 : op1),
8858 const_true_rtx);
8859 return cond0;
8863 /* Similarly for MULT, AND and UMIN, except that for these the result
8864 is always zero. */
8865 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8866 && (code == MULT || code == AND || code == UMIN)
8867 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8869 cond0 = XEXP (XEXP (x, 0), 0);
8870 cond1 = XEXP (XEXP (x, 1), 0);
8872 if (COMPARISON_P (cond0)
8873 && COMPARISON_P (cond1)
8874 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8875 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8876 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8877 || ((swap_condition (GET_CODE (cond0))
8878 == reversed_comparison_code (cond1, NULL))
8879 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8880 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8881 && ! side_effects_p (x))
8883 *ptrue = *pfalse = const0_rtx;
8884 return cond0;
8889 else if (code == IF_THEN_ELSE)
8891 /* If we have IF_THEN_ELSE already, extract the condition and
8892 canonicalize it if it is NE or EQ. */
8893 cond0 = XEXP (x, 0);
8894 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8895 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8896 return XEXP (cond0, 0);
8897 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8899 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8900 return XEXP (cond0, 0);
8902 else
8903 return cond0;
8906 /* If X is a SUBREG, we can narrow both the true and false values
8907 if the inner expression, if there is a condition. */
8908 else if (code == SUBREG
8909 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8910 &true0, &false0)))
8912 true0 = simplify_gen_subreg (mode, true0,
8913 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8914 false0 = simplify_gen_subreg (mode, false0,
8915 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8916 if (true0 && false0)
8918 *ptrue = true0;
8919 *pfalse = false0;
8920 return cond0;
8924 /* If X is a constant, this isn't special and will cause confusions
8925 if we treat it as such. Likewise if it is equivalent to a constant. */
8926 else if (CONSTANT_P (x)
8927 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8930 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8931 will be least confusing to the rest of the compiler. */
8932 else if (mode == BImode)
8934 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8935 return x;
8938 /* If X is known to be either 0 or -1, those are the true and
8939 false values when testing X. */
8940 else if (x == constm1_rtx || x == const0_rtx
8941 || (mode != VOIDmode
8942 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8944 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8945 return x;
8948 /* Likewise for 0 or a single bit. */
8949 else if (HWI_COMPUTABLE_MODE_P (mode)
8950 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8952 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8953 return x;
8956 /* Otherwise fail; show no condition with true and false values the same. */
8957 *ptrue = *pfalse = x;
8958 return 0;
8961 /* Return the value of expression X given the fact that condition COND
8962 is known to be true when applied to REG as its first operand and VAL
8963 as its second. X is known to not be shared and so can be modified in
8964 place.
8966 We only handle the simplest cases, and specifically those cases that
8967 arise with IF_THEN_ELSE expressions. */
8969 static rtx
8970 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8972 enum rtx_code code = GET_CODE (x);
8973 rtx temp;
8974 const char *fmt;
8975 int i, j;
8977 if (side_effects_p (x))
8978 return x;
8980 /* If either operand of the condition is a floating point value,
8981 then we have to avoid collapsing an EQ comparison. */
8982 if (cond == EQ
8983 && rtx_equal_p (x, reg)
8984 && ! FLOAT_MODE_P (GET_MODE (x))
8985 && ! FLOAT_MODE_P (GET_MODE (val)))
8986 return val;
8988 if (cond == UNEQ && rtx_equal_p (x, reg))
8989 return val;
8991 /* If X is (abs REG) and we know something about REG's relationship
8992 with zero, we may be able to simplify this. */
8994 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8995 switch (cond)
8997 case GE: case GT: case EQ:
8998 return XEXP (x, 0);
8999 case LT: case LE:
9000 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9001 XEXP (x, 0),
9002 GET_MODE (XEXP (x, 0)));
9003 default:
9004 break;
9007 /* The only other cases we handle are MIN, MAX, and comparisons if the
9008 operands are the same as REG and VAL. */
9010 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9012 if (rtx_equal_p (XEXP (x, 0), val))
9013 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
9015 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9017 if (COMPARISON_P (x))
9019 if (comparison_dominates_p (cond, code))
9020 return const_true_rtx;
9022 code = reversed_comparison_code (x, NULL);
9023 if (code != UNKNOWN
9024 && comparison_dominates_p (cond, code))
9025 return const0_rtx;
9026 else
9027 return x;
9029 else if (code == SMAX || code == SMIN
9030 || code == UMIN || code == UMAX)
9032 int unsignedp = (code == UMIN || code == UMAX);
9034 /* Do not reverse the condition when it is NE or EQ.
9035 This is because we cannot conclude anything about
9036 the value of 'SMAX (x, y)' when x is not equal to y,
9037 but we can when x equals y. */
9038 if ((code == SMAX || code == UMAX)
9039 && ! (cond == EQ || cond == NE))
9040 cond = reverse_condition (cond);
9042 switch (cond)
9044 case GE: case GT:
9045 return unsignedp ? x : XEXP (x, 1);
9046 case LE: case LT:
9047 return unsignedp ? x : XEXP (x, 0);
9048 case GEU: case GTU:
9049 return unsignedp ? XEXP (x, 1) : x;
9050 case LEU: case LTU:
9051 return unsignedp ? XEXP (x, 0) : x;
9052 default:
9053 break;
9058 else if (code == SUBREG)
9060 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9061 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9063 if (SUBREG_REG (x) != r)
9065 /* We must simplify subreg here, before we lose track of the
9066 original inner_mode. */
9067 new_rtx = simplify_subreg (GET_MODE (x), r,
9068 inner_mode, SUBREG_BYTE (x));
9069 if (new_rtx)
9070 return new_rtx;
9071 else
9072 SUBST (SUBREG_REG (x), r);
9075 return x;
9077 /* We don't have to handle SIGN_EXTEND here, because even in the
9078 case of replacing something with a modeless CONST_INT, a
9079 CONST_INT is already (supposed to be) a valid sign extension for
9080 its narrower mode, which implies it's already properly
9081 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9082 story is different. */
9083 else if (code == ZERO_EXTEND)
9085 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9086 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9088 if (XEXP (x, 0) != r)
9090 /* We must simplify the zero_extend here, before we lose
9091 track of the original inner_mode. */
9092 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9093 r, inner_mode);
9094 if (new_rtx)
9095 return new_rtx;
9096 else
9097 SUBST (XEXP (x, 0), r);
9100 return x;
9103 fmt = GET_RTX_FORMAT (code);
9104 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9106 if (fmt[i] == 'e')
9107 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9108 else if (fmt[i] == 'E')
9109 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9110 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9111 cond, reg, val));
9114 return x;
9117 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9118 assignment as a field assignment. */
9120 static int
9121 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9123 if (x == y || rtx_equal_p (x, y))
9124 return 1;
9126 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9127 return 0;
9129 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9130 Note that all SUBREGs of MEM are paradoxical; otherwise they
9131 would have been rewritten. */
9132 if (MEM_P (x) && GET_CODE (y) == SUBREG
9133 && MEM_P (SUBREG_REG (y))
9134 && rtx_equal_p (SUBREG_REG (y),
9135 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9136 return 1;
9138 if (MEM_P (y) && GET_CODE (x) == SUBREG
9139 && MEM_P (SUBREG_REG (x))
9140 && rtx_equal_p (SUBREG_REG (x),
9141 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9142 return 1;
9144 /* We used to see if get_last_value of X and Y were the same but that's
9145 not correct. In one direction, we'll cause the assignment to have
9146 the wrong destination and in the case, we'll import a register into this
9147 insn that might have already have been dead. So fail if none of the
9148 above cases are true. */
9149 return 0;
9152 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9153 Return that assignment if so.
9155 We only handle the most common cases. */
9157 static rtx
9158 make_field_assignment (rtx x)
9160 rtx dest = SET_DEST (x);
9161 rtx src = SET_SRC (x);
9162 rtx assign;
9163 rtx rhs, lhs;
9164 HOST_WIDE_INT c1;
9165 HOST_WIDE_INT pos;
9166 unsigned HOST_WIDE_INT len;
9167 rtx other;
9168 machine_mode mode;
9170 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9171 a clear of a one-bit field. We will have changed it to
9172 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9173 for a SUBREG. */
9175 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9176 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9177 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9178 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9180 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9181 1, 1, 1, 0);
9182 if (assign != 0)
9183 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9184 return x;
9187 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9188 && subreg_lowpart_p (XEXP (src, 0))
9189 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9190 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9191 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9192 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9193 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9194 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9196 assign = make_extraction (VOIDmode, dest, 0,
9197 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9198 1, 1, 1, 0);
9199 if (assign != 0)
9200 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9201 return x;
9204 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9205 one-bit field. */
9206 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9207 && XEXP (XEXP (src, 0), 0) == const1_rtx
9208 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9210 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9211 1, 1, 1, 0);
9212 if (assign != 0)
9213 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9214 return x;
9217 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9218 SRC is an AND with all bits of that field set, then we can discard
9219 the AND. */
9220 if (GET_CODE (dest) == ZERO_EXTRACT
9221 && CONST_INT_P (XEXP (dest, 1))
9222 && GET_CODE (src) == AND
9223 && CONST_INT_P (XEXP (src, 1)))
9225 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9226 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9227 unsigned HOST_WIDE_INT ze_mask;
9229 if (width >= HOST_BITS_PER_WIDE_INT)
9230 ze_mask = -1;
9231 else
9232 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9234 /* Complete overlap. We can remove the source AND. */
9235 if ((and_mask & ze_mask) == ze_mask)
9236 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9238 /* Partial overlap. We can reduce the source AND. */
9239 if ((and_mask & ze_mask) != and_mask)
9241 mode = GET_MODE (src);
9242 src = gen_rtx_AND (mode, XEXP (src, 0),
9243 gen_int_mode (and_mask & ze_mask, mode));
9244 return gen_rtx_SET (VOIDmode, dest, src);
9248 /* The other case we handle is assignments into a constant-position
9249 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9250 a mask that has all one bits except for a group of zero bits and
9251 OTHER is known to have zeros where C1 has ones, this is such an
9252 assignment. Compute the position and length from C1. Shift OTHER
9253 to the appropriate position, force it to the required mode, and
9254 make the extraction. Check for the AND in both operands. */
9256 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9257 return x;
9259 rhs = expand_compound_operation (XEXP (src, 0));
9260 lhs = expand_compound_operation (XEXP (src, 1));
9262 if (GET_CODE (rhs) == AND
9263 && CONST_INT_P (XEXP (rhs, 1))
9264 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9265 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9266 else if (GET_CODE (lhs) == AND
9267 && CONST_INT_P (XEXP (lhs, 1))
9268 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9269 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9270 else
9271 return x;
9273 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9274 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9275 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9276 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9277 return x;
9279 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9280 if (assign == 0)
9281 return x;
9283 /* The mode to use for the source is the mode of the assignment, or of
9284 what is inside a possible STRICT_LOW_PART. */
9285 mode = (GET_CODE (assign) == STRICT_LOW_PART
9286 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9288 /* Shift OTHER right POS places and make it the source, restricting it
9289 to the proper length and mode. */
9291 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9292 GET_MODE (src),
9293 other, pos),
9294 dest);
9295 src = force_to_mode (src, mode,
9296 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9297 ? ~(unsigned HOST_WIDE_INT) 0
9298 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9301 /* If SRC is masked by an AND that does not make a difference in
9302 the value being stored, strip it. */
9303 if (GET_CODE (assign) == ZERO_EXTRACT
9304 && CONST_INT_P (XEXP (assign, 1))
9305 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9306 && GET_CODE (src) == AND
9307 && CONST_INT_P (XEXP (src, 1))
9308 && UINTVAL (XEXP (src, 1))
9309 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9310 src = XEXP (src, 0);
9312 return gen_rtx_SET (VOIDmode, assign, src);
9315 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9316 if so. */
9318 static rtx
9319 apply_distributive_law (rtx x)
9321 enum rtx_code code = GET_CODE (x);
9322 enum rtx_code inner_code;
9323 rtx lhs, rhs, other;
9324 rtx tem;
9326 /* Distributivity is not true for floating point as it can change the
9327 value. So we don't do it unless -funsafe-math-optimizations. */
9328 if (FLOAT_MODE_P (GET_MODE (x))
9329 && ! flag_unsafe_math_optimizations)
9330 return x;
9332 /* The outer operation can only be one of the following: */
9333 if (code != IOR && code != AND && code != XOR
9334 && code != PLUS && code != MINUS)
9335 return x;
9337 lhs = XEXP (x, 0);
9338 rhs = XEXP (x, 1);
9340 /* If either operand is a primitive we can't do anything, so get out
9341 fast. */
9342 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9343 return x;
9345 lhs = expand_compound_operation (lhs);
9346 rhs = expand_compound_operation (rhs);
9347 inner_code = GET_CODE (lhs);
9348 if (inner_code != GET_CODE (rhs))
9349 return x;
9351 /* See if the inner and outer operations distribute. */
9352 switch (inner_code)
9354 case LSHIFTRT:
9355 case ASHIFTRT:
9356 case AND:
9357 case IOR:
9358 /* These all distribute except over PLUS. */
9359 if (code == PLUS || code == MINUS)
9360 return x;
9361 break;
9363 case MULT:
9364 if (code != PLUS && code != MINUS)
9365 return x;
9366 break;
9368 case ASHIFT:
9369 /* This is also a multiply, so it distributes over everything. */
9370 break;
9372 /* This used to handle SUBREG, but this turned out to be counter-
9373 productive, since (subreg (op ...)) usually is not handled by
9374 insn patterns, and this "optimization" therefore transformed
9375 recognizable patterns into unrecognizable ones. Therefore the
9376 SUBREG case was removed from here.
9378 It is possible that distributing SUBREG over arithmetic operations
9379 leads to an intermediate result than can then be optimized further,
9380 e.g. by moving the outer SUBREG to the other side of a SET as done
9381 in simplify_set. This seems to have been the original intent of
9382 handling SUBREGs here.
9384 However, with current GCC this does not appear to actually happen,
9385 at least on major platforms. If some case is found where removing
9386 the SUBREG case here prevents follow-on optimizations, distributing
9387 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9389 default:
9390 return x;
9393 /* Set LHS and RHS to the inner operands (A and B in the example
9394 above) and set OTHER to the common operand (C in the example).
9395 There is only one way to do this unless the inner operation is
9396 commutative. */
9397 if (COMMUTATIVE_ARITH_P (lhs)
9398 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9399 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9400 else if (COMMUTATIVE_ARITH_P (lhs)
9401 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9402 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9403 else if (COMMUTATIVE_ARITH_P (lhs)
9404 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9405 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9406 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9407 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9408 else
9409 return x;
9411 /* Form the new inner operation, seeing if it simplifies first. */
9412 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9414 /* There is one exception to the general way of distributing:
9415 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9416 if (code == XOR && inner_code == IOR)
9418 inner_code = AND;
9419 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9422 /* We may be able to continuing distributing the result, so call
9423 ourselves recursively on the inner operation before forming the
9424 outer operation, which we return. */
9425 return simplify_gen_binary (inner_code, GET_MODE (x),
9426 apply_distributive_law (tem), other);
9429 /* See if X is of the form (* (+ A B) C), and if so convert to
9430 (+ (* A C) (* B C)) and try to simplify.
9432 Most of the time, this results in no change. However, if some of
9433 the operands are the same or inverses of each other, simplifications
9434 will result.
9436 For example, (and (ior A B) (not B)) can occur as the result of
9437 expanding a bit field assignment. When we apply the distributive
9438 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9439 which then simplifies to (and (A (not B))).
9441 Note that no checks happen on the validity of applying the inverse
9442 distributive law. This is pointless since we can do it in the
9443 few places where this routine is called.
9445 N is the index of the term that is decomposed (the arithmetic operation,
9446 i.e. (+ A B) in the first example above). !N is the index of the term that
9447 is distributed, i.e. of C in the first example above. */
9448 static rtx
9449 distribute_and_simplify_rtx (rtx x, int n)
9451 machine_mode mode;
9452 enum rtx_code outer_code, inner_code;
9453 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9455 /* Distributivity is not true for floating point as it can change the
9456 value. So we don't do it unless -funsafe-math-optimizations. */
9457 if (FLOAT_MODE_P (GET_MODE (x))
9458 && ! flag_unsafe_math_optimizations)
9459 return NULL_RTX;
9461 decomposed = XEXP (x, n);
9462 if (!ARITHMETIC_P (decomposed))
9463 return NULL_RTX;
9465 mode = GET_MODE (x);
9466 outer_code = GET_CODE (x);
9467 distributed = XEXP (x, !n);
9469 inner_code = GET_CODE (decomposed);
9470 inner_op0 = XEXP (decomposed, 0);
9471 inner_op1 = XEXP (decomposed, 1);
9473 /* Special case (and (xor B C) (not A)), which is equivalent to
9474 (xor (ior A B) (ior A C)) */
9475 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9477 distributed = XEXP (distributed, 0);
9478 outer_code = IOR;
9481 if (n == 0)
9483 /* Distribute the second term. */
9484 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9485 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9487 else
9489 /* Distribute the first term. */
9490 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9491 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9494 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9495 new_op0, new_op1));
9496 if (GET_CODE (tmp) != outer_code
9497 && (set_src_cost (tmp, optimize_this_for_speed_p)
9498 < set_src_cost (x, optimize_this_for_speed_p)))
9499 return tmp;
9501 return NULL_RTX;
9504 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9505 in MODE. Return an equivalent form, if different from (and VAROP
9506 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9508 static rtx
9509 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9510 unsigned HOST_WIDE_INT constop)
9512 unsigned HOST_WIDE_INT nonzero;
9513 unsigned HOST_WIDE_INT orig_constop;
9514 rtx orig_varop;
9515 int i;
9517 orig_varop = varop;
9518 orig_constop = constop;
9519 if (GET_CODE (varop) == CLOBBER)
9520 return NULL_RTX;
9522 /* Simplify VAROP knowing that we will be only looking at some of the
9523 bits in it.
9525 Note by passing in CONSTOP, we guarantee that the bits not set in
9526 CONSTOP are not significant and will never be examined. We must
9527 ensure that is the case by explicitly masking out those bits
9528 before returning. */
9529 varop = force_to_mode (varop, mode, constop, 0);
9531 /* If VAROP is a CLOBBER, we will fail so return it. */
9532 if (GET_CODE (varop) == CLOBBER)
9533 return varop;
9535 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9536 to VAROP and return the new constant. */
9537 if (CONST_INT_P (varop))
9538 return gen_int_mode (INTVAL (varop) & constop, mode);
9540 /* See what bits may be nonzero in VAROP. Unlike the general case of
9541 a call to nonzero_bits, here we don't care about bits outside
9542 MODE. */
9544 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9546 /* Turn off all bits in the constant that are known to already be zero.
9547 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9548 which is tested below. */
9550 constop &= nonzero;
9552 /* If we don't have any bits left, return zero. */
9553 if (constop == 0)
9554 return const0_rtx;
9556 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9557 a power of two, we can replace this with an ASHIFT. */
9558 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9559 && (i = exact_log2 (constop)) >= 0)
9560 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9562 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9563 or XOR, then try to apply the distributive law. This may eliminate
9564 operations if either branch can be simplified because of the AND.
9565 It may also make some cases more complex, but those cases probably
9566 won't match a pattern either with or without this. */
9568 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9569 return
9570 gen_lowpart
9571 (mode,
9572 apply_distributive_law
9573 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9574 simplify_and_const_int (NULL_RTX,
9575 GET_MODE (varop),
9576 XEXP (varop, 0),
9577 constop),
9578 simplify_and_const_int (NULL_RTX,
9579 GET_MODE (varop),
9580 XEXP (varop, 1),
9581 constop))));
9583 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9584 the AND and see if one of the operands simplifies to zero. If so, we
9585 may eliminate it. */
9587 if (GET_CODE (varop) == PLUS
9588 && exact_log2 (constop + 1) >= 0)
9590 rtx o0, o1;
9592 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9593 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9594 if (o0 == const0_rtx)
9595 return o1;
9596 if (o1 == const0_rtx)
9597 return o0;
9600 /* Make a SUBREG if necessary. If we can't make it, fail. */
9601 varop = gen_lowpart (mode, varop);
9602 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9603 return NULL_RTX;
9605 /* If we are only masking insignificant bits, return VAROP. */
9606 if (constop == nonzero)
9607 return varop;
9609 if (varop == orig_varop && constop == orig_constop)
9610 return NULL_RTX;
9612 /* Otherwise, return an AND. */
9613 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9617 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9618 in MODE.
9620 Return an equivalent form, if different from X. Otherwise, return X. If
9621 X is zero, we are to always construct the equivalent form. */
9623 static rtx
9624 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9625 unsigned HOST_WIDE_INT constop)
9627 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9628 if (tem)
9629 return tem;
9631 if (!x)
9632 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9633 gen_int_mode (constop, mode));
9634 if (GET_MODE (x) != mode)
9635 x = gen_lowpart (mode, x);
9636 return x;
9639 /* Given a REG, X, compute which bits in X can be nonzero.
9640 We don't care about bits outside of those defined in MODE.
9642 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9643 a shift, AND, or zero_extract, we can do better. */
9645 static rtx
9646 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9647 const_rtx known_x ATTRIBUTE_UNUSED,
9648 machine_mode known_mode ATTRIBUTE_UNUSED,
9649 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9650 unsigned HOST_WIDE_INT *nonzero)
9652 rtx tem;
9653 reg_stat_type *rsp;
9655 /* If X is a register whose nonzero bits value is current, use it.
9656 Otherwise, if X is a register whose value we can find, use that
9657 value. Otherwise, use the previously-computed global nonzero bits
9658 for this register. */
9660 rsp = &reg_stat[REGNO (x)];
9661 if (rsp->last_set_value != 0
9662 && (rsp->last_set_mode == mode
9663 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9664 && GET_MODE_CLASS (mode) == MODE_INT))
9665 && ((rsp->last_set_label >= label_tick_ebb_start
9666 && rsp->last_set_label < label_tick)
9667 || (rsp->last_set_label == label_tick
9668 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9669 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9670 && REG_N_SETS (REGNO (x)) == 1
9671 && !REGNO_REG_SET_P
9672 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9673 REGNO (x)))))
9675 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9677 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9678 /* We don't know anything about the upper bits. */
9679 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9681 *nonzero &= mask;
9682 return NULL;
9685 tem = get_last_value (x);
9687 if (tem)
9689 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9690 /* If X is narrower than MODE and TEM is a non-negative
9691 constant that would appear negative in the mode of X,
9692 sign-extend it for use in reg_nonzero_bits because some
9693 machines (maybe most) will actually do the sign-extension
9694 and this is the conservative approach.
9696 ??? For 2.5, try to tighten up the MD files in this regard
9697 instead of this kludge. */
9699 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9700 && CONST_INT_P (tem)
9701 && INTVAL (tem) > 0
9702 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9703 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9704 #endif
9705 return tem;
9707 else if (nonzero_sign_valid && rsp->nonzero_bits)
9709 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9711 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9712 /* We don't know anything about the upper bits. */
9713 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9715 *nonzero &= mask;
9718 return NULL;
9721 /* Return the number of bits at the high-order end of X that are known to
9722 be equal to the sign bit. X will be used in mode MODE; if MODE is
9723 VOIDmode, X will be used in its own mode. The returned value will always
9724 be between 1 and the number of bits in MODE. */
9726 static rtx
9727 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9728 const_rtx known_x ATTRIBUTE_UNUSED,
9729 machine_mode known_mode
9730 ATTRIBUTE_UNUSED,
9731 unsigned int known_ret ATTRIBUTE_UNUSED,
9732 unsigned int *result)
9734 rtx tem;
9735 reg_stat_type *rsp;
9737 rsp = &reg_stat[REGNO (x)];
9738 if (rsp->last_set_value != 0
9739 && rsp->last_set_mode == mode
9740 && ((rsp->last_set_label >= label_tick_ebb_start
9741 && rsp->last_set_label < label_tick)
9742 || (rsp->last_set_label == label_tick
9743 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9744 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9745 && REG_N_SETS (REGNO (x)) == 1
9746 && !REGNO_REG_SET_P
9747 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9748 REGNO (x)))))
9750 *result = rsp->last_set_sign_bit_copies;
9751 return NULL;
9754 tem = get_last_value (x);
9755 if (tem != 0)
9756 return tem;
9758 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9759 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9760 *result = rsp->sign_bit_copies;
9762 return NULL;
9765 /* Return the number of "extended" bits there are in X, when interpreted
9766 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9767 unsigned quantities, this is the number of high-order zero bits.
9768 For signed quantities, this is the number of copies of the sign bit
9769 minus 1. In both case, this function returns the number of "spare"
9770 bits. For example, if two quantities for which this function returns
9771 at least 1 are added, the addition is known not to overflow.
9773 This function will always return 0 unless called during combine, which
9774 implies that it must be called from a define_split. */
9776 unsigned int
9777 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9779 if (nonzero_sign_valid == 0)
9780 return 0;
9782 return (unsignedp
9783 ? (HWI_COMPUTABLE_MODE_P (mode)
9784 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9785 - floor_log2 (nonzero_bits (x, mode)))
9786 : 0)
9787 : num_sign_bit_copies (x, mode) - 1);
9790 /* This function is called from `simplify_shift_const' to merge two
9791 outer operations. Specifically, we have already found that we need
9792 to perform operation *POP0 with constant *PCONST0 at the outermost
9793 position. We would now like to also perform OP1 with constant CONST1
9794 (with *POP0 being done last).
9796 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9797 the resulting operation. *PCOMP_P is set to 1 if we would need to
9798 complement the innermost operand, otherwise it is unchanged.
9800 MODE is the mode in which the operation will be done. No bits outside
9801 the width of this mode matter. It is assumed that the width of this mode
9802 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9804 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9805 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9806 result is simply *PCONST0.
9808 If the resulting operation cannot be expressed as one operation, we
9809 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9811 static int
9812 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, machine_mode mode, int *pcomp_p)
9814 enum rtx_code op0 = *pop0;
9815 HOST_WIDE_INT const0 = *pconst0;
9817 const0 &= GET_MODE_MASK (mode);
9818 const1 &= GET_MODE_MASK (mode);
9820 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9821 if (op0 == AND)
9822 const1 &= const0;
9824 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9825 if OP0 is SET. */
9827 if (op1 == UNKNOWN || op0 == SET)
9828 return 1;
9830 else if (op0 == UNKNOWN)
9831 op0 = op1, const0 = const1;
9833 else if (op0 == op1)
9835 switch (op0)
9837 case AND:
9838 const0 &= const1;
9839 break;
9840 case IOR:
9841 const0 |= const1;
9842 break;
9843 case XOR:
9844 const0 ^= const1;
9845 break;
9846 case PLUS:
9847 const0 += const1;
9848 break;
9849 case NEG:
9850 op0 = UNKNOWN;
9851 break;
9852 default:
9853 break;
9857 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9858 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9859 return 0;
9861 /* If the two constants aren't the same, we can't do anything. The
9862 remaining six cases can all be done. */
9863 else if (const0 != const1)
9864 return 0;
9866 else
9867 switch (op0)
9869 case IOR:
9870 if (op1 == AND)
9871 /* (a & b) | b == b */
9872 op0 = SET;
9873 else /* op1 == XOR */
9874 /* (a ^ b) | b == a | b */
9876 break;
9878 case XOR:
9879 if (op1 == AND)
9880 /* (a & b) ^ b == (~a) & b */
9881 op0 = AND, *pcomp_p = 1;
9882 else /* op1 == IOR */
9883 /* (a | b) ^ b == a & ~b */
9884 op0 = AND, const0 = ~const0;
9885 break;
9887 case AND:
9888 if (op1 == IOR)
9889 /* (a | b) & b == b */
9890 op0 = SET;
9891 else /* op1 == XOR */
9892 /* (a ^ b) & b) == (~a) & b */
9893 *pcomp_p = 1;
9894 break;
9895 default:
9896 break;
9899 /* Check for NO-OP cases. */
9900 const0 &= GET_MODE_MASK (mode);
9901 if (const0 == 0
9902 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9903 op0 = UNKNOWN;
9904 else if (const0 == 0 && op0 == AND)
9905 op0 = SET;
9906 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9907 && op0 == AND)
9908 op0 = UNKNOWN;
9910 *pop0 = op0;
9912 /* ??? Slightly redundant with the above mask, but not entirely.
9913 Moving this above means we'd have to sign-extend the mode mask
9914 for the final test. */
9915 if (op0 != UNKNOWN && op0 != NEG)
9916 *pconst0 = trunc_int_for_mode (const0, mode);
9918 return 1;
9921 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9922 the shift in. The original shift operation CODE is performed on OP in
9923 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9924 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9925 result of the shift is subject to operation OUTER_CODE with operand
9926 OUTER_CONST. */
9928 static machine_mode
9929 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9930 machine_mode orig_mode, machine_mode mode,
9931 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9933 if (orig_mode == mode)
9934 return mode;
9935 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9937 /* In general we can't perform in wider mode for right shift and rotate. */
9938 switch (code)
9940 case ASHIFTRT:
9941 /* We can still widen if the bits brought in from the left are identical
9942 to the sign bit of ORIG_MODE. */
9943 if (num_sign_bit_copies (op, mode)
9944 > (unsigned) (GET_MODE_PRECISION (mode)
9945 - GET_MODE_PRECISION (orig_mode)))
9946 return mode;
9947 return orig_mode;
9949 case LSHIFTRT:
9950 /* Similarly here but with zero bits. */
9951 if (HWI_COMPUTABLE_MODE_P (mode)
9952 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9953 return mode;
9955 /* We can also widen if the bits brought in will be masked off. This
9956 operation is performed in ORIG_MODE. */
9957 if (outer_code == AND)
9959 int care_bits = low_bitmask_len (orig_mode, outer_const);
9961 if (care_bits >= 0
9962 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9963 return mode;
9965 /* fall through */
9967 case ROTATE:
9968 return orig_mode;
9970 case ROTATERT:
9971 gcc_unreachable ();
9973 default:
9974 return mode;
9978 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9979 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9980 if we cannot simplify it. Otherwise, return a simplified value.
9982 The shift is normally computed in the widest mode we find in VAROP, as
9983 long as it isn't a different number of words than RESULT_MODE. Exceptions
9984 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9986 static rtx
9987 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
9988 rtx varop, int orig_count)
9990 enum rtx_code orig_code = code;
9991 rtx orig_varop = varop;
9992 int count;
9993 machine_mode mode = result_mode;
9994 machine_mode shift_mode, tmode;
9995 unsigned int mode_words
9996 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9997 /* We form (outer_op (code varop count) (outer_const)). */
9998 enum rtx_code outer_op = UNKNOWN;
9999 HOST_WIDE_INT outer_const = 0;
10000 int complement_p = 0;
10001 rtx new_rtx, x;
10003 /* Make sure and truncate the "natural" shift on the way in. We don't
10004 want to do this inside the loop as it makes it more difficult to
10005 combine shifts. */
10006 if (SHIFT_COUNT_TRUNCATED)
10007 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10009 /* If we were given an invalid count, don't do anything except exactly
10010 what was requested. */
10012 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10013 return NULL_RTX;
10015 count = orig_count;
10017 /* Unless one of the branches of the `if' in this loop does a `continue',
10018 we will `break' the loop after the `if'. */
10020 while (count != 0)
10022 /* If we have an operand of (clobber (const_int 0)), fail. */
10023 if (GET_CODE (varop) == CLOBBER)
10024 return NULL_RTX;
10026 /* Convert ROTATERT to ROTATE. */
10027 if (code == ROTATERT)
10029 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10030 code = ROTATE;
10031 if (VECTOR_MODE_P (result_mode))
10032 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10033 else
10034 count = bitsize - count;
10037 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10038 mode, outer_op, outer_const);
10040 /* Handle cases where the count is greater than the size of the mode
10041 minus 1. For ASHIFT, use the size minus one as the count (this can
10042 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10043 take the count modulo the size. For other shifts, the result is
10044 zero.
10046 Since these shifts are being produced by the compiler by combining
10047 multiple operations, each of which are defined, we know what the
10048 result is supposed to be. */
10050 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10052 if (code == ASHIFTRT)
10053 count = GET_MODE_PRECISION (shift_mode) - 1;
10054 else if (code == ROTATE || code == ROTATERT)
10055 count %= GET_MODE_PRECISION (shift_mode);
10056 else
10058 /* We can't simply return zero because there may be an
10059 outer op. */
10060 varop = const0_rtx;
10061 count = 0;
10062 break;
10066 /* If we discovered we had to complement VAROP, leave. Making a NOT
10067 here would cause an infinite loop. */
10068 if (complement_p)
10069 break;
10071 /* An arithmetic right shift of a quantity known to be -1 or 0
10072 is a no-op. */
10073 if (code == ASHIFTRT
10074 && (num_sign_bit_copies (varop, shift_mode)
10075 == GET_MODE_PRECISION (shift_mode)))
10077 count = 0;
10078 break;
10081 /* If we are doing an arithmetic right shift and discarding all but
10082 the sign bit copies, this is equivalent to doing a shift by the
10083 bitsize minus one. Convert it into that shift because it will often
10084 allow other simplifications. */
10086 if (code == ASHIFTRT
10087 && (count + num_sign_bit_copies (varop, shift_mode)
10088 >= GET_MODE_PRECISION (shift_mode)))
10089 count = GET_MODE_PRECISION (shift_mode) - 1;
10091 /* We simplify the tests below and elsewhere by converting
10092 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10093 `make_compound_operation' will convert it to an ASHIFTRT for
10094 those machines (such as VAX) that don't have an LSHIFTRT. */
10095 if (code == ASHIFTRT
10096 && val_signbit_known_clear_p (shift_mode,
10097 nonzero_bits (varop, shift_mode)))
10098 code = LSHIFTRT;
10100 if (((code == LSHIFTRT
10101 && HWI_COMPUTABLE_MODE_P (shift_mode)
10102 && !(nonzero_bits (varop, shift_mode) >> count))
10103 || (code == ASHIFT
10104 && HWI_COMPUTABLE_MODE_P (shift_mode)
10105 && !((nonzero_bits (varop, shift_mode) << count)
10106 & GET_MODE_MASK (shift_mode))))
10107 && !side_effects_p (varop))
10108 varop = const0_rtx;
10110 switch (GET_CODE (varop))
10112 case SIGN_EXTEND:
10113 case ZERO_EXTEND:
10114 case SIGN_EXTRACT:
10115 case ZERO_EXTRACT:
10116 new_rtx = expand_compound_operation (varop);
10117 if (new_rtx != varop)
10119 varop = new_rtx;
10120 continue;
10122 break;
10124 case MEM:
10125 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10126 minus the width of a smaller mode, we can do this with a
10127 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10128 if ((code == ASHIFTRT || code == LSHIFTRT)
10129 && ! mode_dependent_address_p (XEXP (varop, 0),
10130 MEM_ADDR_SPACE (varop))
10131 && ! MEM_VOLATILE_P (varop)
10132 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10133 MODE_INT, 1)) != BLKmode)
10135 new_rtx = adjust_address_nv (varop, tmode,
10136 BYTES_BIG_ENDIAN ? 0
10137 : count / BITS_PER_UNIT);
10139 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10140 : ZERO_EXTEND, mode, new_rtx);
10141 count = 0;
10142 continue;
10144 break;
10146 case SUBREG:
10147 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10148 the same number of words as what we've seen so far. Then store
10149 the widest mode in MODE. */
10150 if (subreg_lowpart_p (varop)
10151 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10152 > GET_MODE_SIZE (GET_MODE (varop)))
10153 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10154 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10155 == mode_words
10156 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10157 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10159 varop = SUBREG_REG (varop);
10160 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10161 mode = GET_MODE (varop);
10162 continue;
10164 break;
10166 case MULT:
10167 /* Some machines use MULT instead of ASHIFT because MULT
10168 is cheaper. But it is still better on those machines to
10169 merge two shifts into one. */
10170 if (CONST_INT_P (XEXP (varop, 1))
10171 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10173 varop
10174 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10175 XEXP (varop, 0),
10176 GEN_INT (exact_log2 (
10177 UINTVAL (XEXP (varop, 1)))));
10178 continue;
10180 break;
10182 case UDIV:
10183 /* Similar, for when divides are cheaper. */
10184 if (CONST_INT_P (XEXP (varop, 1))
10185 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10187 varop
10188 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10189 XEXP (varop, 0),
10190 GEN_INT (exact_log2 (
10191 UINTVAL (XEXP (varop, 1)))));
10192 continue;
10194 break;
10196 case ASHIFTRT:
10197 /* If we are extracting just the sign bit of an arithmetic
10198 right shift, that shift is not needed. However, the sign
10199 bit of a wider mode may be different from what would be
10200 interpreted as the sign bit in a narrower mode, so, if
10201 the result is narrower, don't discard the shift. */
10202 if (code == LSHIFTRT
10203 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10204 && (GET_MODE_BITSIZE (result_mode)
10205 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10207 varop = XEXP (varop, 0);
10208 continue;
10211 /* ... fall through ... */
10213 case LSHIFTRT:
10214 case ASHIFT:
10215 case ROTATE:
10216 /* Here we have two nested shifts. The result is usually the
10217 AND of a new shift with a mask. We compute the result below. */
10218 if (CONST_INT_P (XEXP (varop, 1))
10219 && INTVAL (XEXP (varop, 1)) >= 0
10220 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10221 && HWI_COMPUTABLE_MODE_P (result_mode)
10222 && HWI_COMPUTABLE_MODE_P (mode)
10223 && !VECTOR_MODE_P (result_mode))
10225 enum rtx_code first_code = GET_CODE (varop);
10226 unsigned int first_count = INTVAL (XEXP (varop, 1));
10227 unsigned HOST_WIDE_INT mask;
10228 rtx mask_rtx;
10230 /* We have one common special case. We can't do any merging if
10231 the inner code is an ASHIFTRT of a smaller mode. However, if
10232 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10233 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10234 we can convert it to
10235 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10236 This simplifies certain SIGN_EXTEND operations. */
10237 if (code == ASHIFT && first_code == ASHIFTRT
10238 && count == (GET_MODE_PRECISION (result_mode)
10239 - GET_MODE_PRECISION (GET_MODE (varop))))
10241 /* C3 has the low-order C1 bits zero. */
10243 mask = GET_MODE_MASK (mode)
10244 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10246 varop = simplify_and_const_int (NULL_RTX, result_mode,
10247 XEXP (varop, 0), mask);
10248 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10249 varop, count);
10250 count = first_count;
10251 code = ASHIFTRT;
10252 continue;
10255 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10256 than C1 high-order bits equal to the sign bit, we can convert
10257 this to either an ASHIFT or an ASHIFTRT depending on the
10258 two counts.
10260 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10262 if (code == ASHIFTRT && first_code == ASHIFT
10263 && GET_MODE (varop) == shift_mode
10264 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10265 > first_count))
10267 varop = XEXP (varop, 0);
10268 count -= first_count;
10269 if (count < 0)
10271 count = -count;
10272 code = ASHIFT;
10275 continue;
10278 /* There are some cases we can't do. If CODE is ASHIFTRT,
10279 we can only do this if FIRST_CODE is also ASHIFTRT.
10281 We can't do the case when CODE is ROTATE and FIRST_CODE is
10282 ASHIFTRT.
10284 If the mode of this shift is not the mode of the outer shift,
10285 we can't do this if either shift is a right shift or ROTATE.
10287 Finally, we can't do any of these if the mode is too wide
10288 unless the codes are the same.
10290 Handle the case where the shift codes are the same
10291 first. */
10293 if (code == first_code)
10295 if (GET_MODE (varop) != result_mode
10296 && (code == ASHIFTRT || code == LSHIFTRT
10297 || code == ROTATE))
10298 break;
10300 count += first_count;
10301 varop = XEXP (varop, 0);
10302 continue;
10305 if (code == ASHIFTRT
10306 || (code == ROTATE && first_code == ASHIFTRT)
10307 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10308 || (GET_MODE (varop) != result_mode
10309 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10310 || first_code == ROTATE
10311 || code == ROTATE)))
10312 break;
10314 /* To compute the mask to apply after the shift, shift the
10315 nonzero bits of the inner shift the same way the
10316 outer shift will. */
10318 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10319 result_mode);
10321 mask_rtx
10322 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10323 GEN_INT (count));
10325 /* Give up if we can't compute an outer operation to use. */
10326 if (mask_rtx == 0
10327 || !CONST_INT_P (mask_rtx)
10328 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10329 INTVAL (mask_rtx),
10330 result_mode, &complement_p))
10331 break;
10333 /* If the shifts are in the same direction, we add the
10334 counts. Otherwise, we subtract them. */
10335 if ((code == ASHIFTRT || code == LSHIFTRT)
10336 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10337 count += first_count;
10338 else
10339 count -= first_count;
10341 /* If COUNT is positive, the new shift is usually CODE,
10342 except for the two exceptions below, in which case it is
10343 FIRST_CODE. If the count is negative, FIRST_CODE should
10344 always be used */
10345 if (count > 0
10346 && ((first_code == ROTATE && code == ASHIFT)
10347 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10348 code = first_code;
10349 else if (count < 0)
10350 code = first_code, count = -count;
10352 varop = XEXP (varop, 0);
10353 continue;
10356 /* If we have (A << B << C) for any shift, we can convert this to
10357 (A << C << B). This wins if A is a constant. Only try this if
10358 B is not a constant. */
10360 else if (GET_CODE (varop) == code
10361 && CONST_INT_P (XEXP (varop, 0))
10362 && !CONST_INT_P (XEXP (varop, 1)))
10364 rtx new_rtx = simplify_const_binary_operation (code, mode,
10365 XEXP (varop, 0),
10366 GEN_INT (count));
10367 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10368 count = 0;
10369 continue;
10371 break;
10373 case NOT:
10374 if (VECTOR_MODE_P (mode))
10375 break;
10377 /* Make this fit the case below. */
10378 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10379 continue;
10381 case IOR:
10382 case AND:
10383 case XOR:
10384 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10385 with C the size of VAROP - 1 and the shift is logical if
10386 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10387 we have an (le X 0) operation. If we have an arithmetic shift
10388 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10389 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10391 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10392 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10393 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10394 && (code == LSHIFTRT || code == ASHIFTRT)
10395 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10396 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10398 count = 0;
10399 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10400 const0_rtx);
10402 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10403 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10405 continue;
10408 /* If we have (shift (logical)), move the logical to the outside
10409 to allow it to possibly combine with another logical and the
10410 shift to combine with another shift. This also canonicalizes to
10411 what a ZERO_EXTRACT looks like. Also, some machines have
10412 (and (shift)) insns. */
10414 if (CONST_INT_P (XEXP (varop, 1))
10415 /* We can't do this if we have (ashiftrt (xor)) and the
10416 constant has its sign bit set in shift_mode with shift_mode
10417 wider than result_mode. */
10418 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10419 && result_mode != shift_mode
10420 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10421 shift_mode))
10422 && (new_rtx = simplify_const_binary_operation
10423 (code, result_mode,
10424 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10425 GEN_INT (count))) != 0
10426 && CONST_INT_P (new_rtx)
10427 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10428 INTVAL (new_rtx), result_mode, &complement_p))
10430 varop = XEXP (varop, 0);
10431 continue;
10434 /* If we can't do that, try to simplify the shift in each arm of the
10435 logical expression, make a new logical expression, and apply
10436 the inverse distributive law. This also can't be done for
10437 (ashiftrt (xor)) where we've widened the shift and the constant
10438 changes the sign bit. */
10439 if (CONST_INT_P (XEXP (varop, 1))
10440 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10441 && result_mode != shift_mode
10442 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10443 shift_mode)))
10445 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10446 XEXP (varop, 0), count);
10447 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10448 XEXP (varop, 1), count);
10450 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10451 lhs, rhs);
10452 varop = apply_distributive_law (varop);
10454 count = 0;
10455 continue;
10457 break;
10459 case EQ:
10460 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10461 says that the sign bit can be tested, FOO has mode MODE, C is
10462 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10463 that may be nonzero. */
10464 if (code == LSHIFTRT
10465 && XEXP (varop, 1) == const0_rtx
10466 && GET_MODE (XEXP (varop, 0)) == result_mode
10467 && count == (GET_MODE_PRECISION (result_mode) - 1)
10468 && HWI_COMPUTABLE_MODE_P (result_mode)
10469 && STORE_FLAG_VALUE == -1
10470 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10471 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10472 &complement_p))
10474 varop = XEXP (varop, 0);
10475 count = 0;
10476 continue;
10478 break;
10480 case NEG:
10481 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10482 than the number of bits in the mode is equivalent to A. */
10483 if (code == LSHIFTRT
10484 && count == (GET_MODE_PRECISION (result_mode) - 1)
10485 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10487 varop = XEXP (varop, 0);
10488 count = 0;
10489 continue;
10492 /* NEG commutes with ASHIFT since it is multiplication. Move the
10493 NEG outside to allow shifts to combine. */
10494 if (code == ASHIFT
10495 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10496 &complement_p))
10498 varop = XEXP (varop, 0);
10499 continue;
10501 break;
10503 case PLUS:
10504 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10505 is one less than the number of bits in the mode is
10506 equivalent to (xor A 1). */
10507 if (code == LSHIFTRT
10508 && count == (GET_MODE_PRECISION (result_mode) - 1)
10509 && XEXP (varop, 1) == constm1_rtx
10510 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10511 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10512 &complement_p))
10514 count = 0;
10515 varop = XEXP (varop, 0);
10516 continue;
10519 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10520 that might be nonzero in BAR are those being shifted out and those
10521 bits are known zero in FOO, we can replace the PLUS with FOO.
10522 Similarly in the other operand order. This code occurs when
10523 we are computing the size of a variable-size array. */
10525 if ((code == ASHIFTRT || code == LSHIFTRT)
10526 && count < HOST_BITS_PER_WIDE_INT
10527 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10528 && (nonzero_bits (XEXP (varop, 1), result_mode)
10529 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10531 varop = XEXP (varop, 0);
10532 continue;
10534 else if ((code == ASHIFTRT || code == LSHIFTRT)
10535 && count < HOST_BITS_PER_WIDE_INT
10536 && HWI_COMPUTABLE_MODE_P (result_mode)
10537 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10538 >> count)
10539 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10540 & nonzero_bits (XEXP (varop, 1),
10541 result_mode)))
10543 varop = XEXP (varop, 1);
10544 continue;
10547 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10548 if (code == ASHIFT
10549 && CONST_INT_P (XEXP (varop, 1))
10550 && (new_rtx = simplify_const_binary_operation
10551 (ASHIFT, result_mode,
10552 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10553 GEN_INT (count))) != 0
10554 && CONST_INT_P (new_rtx)
10555 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10556 INTVAL (new_rtx), result_mode, &complement_p))
10558 varop = XEXP (varop, 0);
10559 continue;
10562 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10563 signbit', and attempt to change the PLUS to an XOR and move it to
10564 the outer operation as is done above in the AND/IOR/XOR case
10565 leg for shift(logical). See details in logical handling above
10566 for reasoning in doing so. */
10567 if (code == LSHIFTRT
10568 && CONST_INT_P (XEXP (varop, 1))
10569 && mode_signbit_p (result_mode, XEXP (varop, 1))
10570 && (new_rtx = simplify_const_binary_operation
10571 (code, result_mode,
10572 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10573 GEN_INT (count))) != 0
10574 && CONST_INT_P (new_rtx)
10575 && merge_outer_ops (&outer_op, &outer_const, XOR,
10576 INTVAL (new_rtx), result_mode, &complement_p))
10578 varop = XEXP (varop, 0);
10579 continue;
10582 break;
10584 case MINUS:
10585 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10586 with C the size of VAROP - 1 and the shift is logical if
10587 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10588 we have a (gt X 0) operation. If the shift is arithmetic with
10589 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10590 we have a (neg (gt X 0)) operation. */
10592 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10593 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10594 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10595 && (code == LSHIFTRT || code == ASHIFTRT)
10596 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10597 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10598 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10600 count = 0;
10601 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10602 const0_rtx);
10604 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10605 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10607 continue;
10609 break;
10611 case TRUNCATE:
10612 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10613 if the truncate does not affect the value. */
10614 if (code == LSHIFTRT
10615 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10616 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10617 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10618 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10619 - GET_MODE_PRECISION (GET_MODE (varop)))))
10621 rtx varop_inner = XEXP (varop, 0);
10623 varop_inner
10624 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10625 XEXP (varop_inner, 0),
10626 GEN_INT
10627 (count + INTVAL (XEXP (varop_inner, 1))));
10628 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10629 count = 0;
10630 continue;
10632 break;
10634 default:
10635 break;
10638 break;
10641 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10642 outer_op, outer_const);
10644 /* We have now finished analyzing the shift. The result should be
10645 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10646 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10647 to the result of the shift. OUTER_CONST is the relevant constant,
10648 but we must turn off all bits turned off in the shift. */
10650 if (outer_op == UNKNOWN
10651 && orig_code == code && orig_count == count
10652 && varop == orig_varop
10653 && shift_mode == GET_MODE (varop))
10654 return NULL_RTX;
10656 /* Make a SUBREG if necessary. If we can't make it, fail. */
10657 varop = gen_lowpart (shift_mode, varop);
10658 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10659 return NULL_RTX;
10661 /* If we have an outer operation and we just made a shift, it is
10662 possible that we could have simplified the shift were it not
10663 for the outer operation. So try to do the simplification
10664 recursively. */
10666 if (outer_op != UNKNOWN)
10667 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10668 else
10669 x = NULL_RTX;
10671 if (x == NULL_RTX)
10672 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10674 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10675 turn off all the bits that the shift would have turned off. */
10676 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10677 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10678 GET_MODE_MASK (result_mode) >> orig_count);
10680 /* Do the remainder of the processing in RESULT_MODE. */
10681 x = gen_lowpart_or_truncate (result_mode, x);
10683 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10684 operation. */
10685 if (complement_p)
10686 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10688 if (outer_op != UNKNOWN)
10690 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10691 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10692 outer_const = trunc_int_for_mode (outer_const, result_mode);
10694 if (outer_op == AND)
10695 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10696 else if (outer_op == SET)
10698 /* This means that we have determined that the result is
10699 equivalent to a constant. This should be rare. */
10700 if (!side_effects_p (x))
10701 x = GEN_INT (outer_const);
10703 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10704 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10705 else
10706 x = simplify_gen_binary (outer_op, result_mode, x,
10707 GEN_INT (outer_const));
10710 return x;
10713 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10714 The result of the shift is RESULT_MODE. If we cannot simplify it,
10715 return X or, if it is NULL, synthesize the expression with
10716 simplify_gen_binary. Otherwise, return a simplified value.
10718 The shift is normally computed in the widest mode we find in VAROP, as
10719 long as it isn't a different number of words than RESULT_MODE. Exceptions
10720 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10722 static rtx
10723 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10724 rtx varop, int count)
10726 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10727 if (tem)
10728 return tem;
10730 if (!x)
10731 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10732 if (GET_MODE (x) != result_mode)
10733 x = gen_lowpart (result_mode, x);
10734 return x;
10738 /* Like recog, but we receive the address of a pointer to a new pattern.
10739 We try to match the rtx that the pointer points to.
10740 If that fails, we may try to modify or replace the pattern,
10741 storing the replacement into the same pointer object.
10743 Modifications include deletion or addition of CLOBBERs.
10745 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10746 the CLOBBERs are placed.
10748 The value is the final insn code from the pattern ultimately matched,
10749 or -1. */
10751 static int
10752 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10754 rtx pat = *pnewpat;
10755 rtx pat_without_clobbers;
10756 int insn_code_number;
10757 int num_clobbers_to_add = 0;
10758 int i;
10759 rtx notes = NULL_RTX;
10760 rtx old_notes, old_pat;
10761 int old_icode;
10763 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10764 we use to indicate that something didn't match. If we find such a
10765 thing, force rejection. */
10766 if (GET_CODE (pat) == PARALLEL)
10767 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10768 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10769 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10770 return -1;
10772 old_pat = PATTERN (insn);
10773 old_notes = REG_NOTES (insn);
10774 PATTERN (insn) = pat;
10775 REG_NOTES (insn) = NULL_RTX;
10777 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10778 if (dump_file && (dump_flags & TDF_DETAILS))
10780 if (insn_code_number < 0)
10781 fputs ("Failed to match this instruction:\n", dump_file);
10782 else
10783 fputs ("Successfully matched this instruction:\n", dump_file);
10784 print_rtl_single (dump_file, pat);
10787 /* If it isn't, there is the possibility that we previously had an insn
10788 that clobbered some register as a side effect, but the combined
10789 insn doesn't need to do that. So try once more without the clobbers
10790 unless this represents an ASM insn. */
10792 if (insn_code_number < 0 && ! check_asm_operands (pat)
10793 && GET_CODE (pat) == PARALLEL)
10795 int pos;
10797 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10798 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10800 if (i != pos)
10801 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10802 pos++;
10805 SUBST_INT (XVECLEN (pat, 0), pos);
10807 if (pos == 1)
10808 pat = XVECEXP (pat, 0, 0);
10810 PATTERN (insn) = pat;
10811 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10812 if (dump_file && (dump_flags & TDF_DETAILS))
10814 if (insn_code_number < 0)
10815 fputs ("Failed to match this instruction:\n", dump_file);
10816 else
10817 fputs ("Successfully matched this instruction:\n", dump_file);
10818 print_rtl_single (dump_file, pat);
10822 pat_without_clobbers = pat;
10824 PATTERN (insn) = old_pat;
10825 REG_NOTES (insn) = old_notes;
10827 /* Recognize all noop sets, these will be killed by followup pass. */
10828 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10829 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10831 /* If we had any clobbers to add, make a new pattern than contains
10832 them. Then check to make sure that all of them are dead. */
10833 if (num_clobbers_to_add)
10835 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10836 rtvec_alloc (GET_CODE (pat) == PARALLEL
10837 ? (XVECLEN (pat, 0)
10838 + num_clobbers_to_add)
10839 : num_clobbers_to_add + 1));
10841 if (GET_CODE (pat) == PARALLEL)
10842 for (i = 0; i < XVECLEN (pat, 0); i++)
10843 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10844 else
10845 XVECEXP (newpat, 0, 0) = pat;
10847 add_clobbers (newpat, insn_code_number);
10849 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10850 i < XVECLEN (newpat, 0); i++)
10852 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10853 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10854 return -1;
10855 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10857 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10858 notes = alloc_reg_note (REG_UNUSED,
10859 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10862 pat = newpat;
10865 if (insn_code_number >= 0
10866 && insn_code_number != NOOP_MOVE_INSN_CODE)
10868 old_pat = PATTERN (insn);
10869 old_notes = REG_NOTES (insn);
10870 old_icode = INSN_CODE (insn);
10871 PATTERN (insn) = pat;
10872 REG_NOTES (insn) = notes;
10874 /* Allow targets to reject combined insn. */
10875 if (!targetm.legitimate_combined_insn (insn))
10877 if (dump_file && (dump_flags & TDF_DETAILS))
10878 fputs ("Instruction not appropriate for target.",
10879 dump_file);
10881 /* Callers expect recog_for_combine to strip
10882 clobbers from the pattern on failure. */
10883 pat = pat_without_clobbers;
10884 notes = NULL_RTX;
10886 insn_code_number = -1;
10889 PATTERN (insn) = old_pat;
10890 REG_NOTES (insn) = old_notes;
10891 INSN_CODE (insn) = old_icode;
10894 *pnewpat = pat;
10895 *pnotes = notes;
10897 return insn_code_number;
10900 /* Like gen_lowpart_general but for use by combine. In combine it
10901 is not possible to create any new pseudoregs. However, it is
10902 safe to create invalid memory addresses, because combine will
10903 try to recognize them and all they will do is make the combine
10904 attempt fail.
10906 If for some reason this cannot do its job, an rtx
10907 (clobber (const_int 0)) is returned.
10908 An insn containing that will not be recognized. */
10910 static rtx
10911 gen_lowpart_for_combine (machine_mode omode, rtx x)
10913 machine_mode imode = GET_MODE (x);
10914 unsigned int osize = GET_MODE_SIZE (omode);
10915 unsigned int isize = GET_MODE_SIZE (imode);
10916 rtx result;
10918 if (omode == imode)
10919 return x;
10921 /* We can only support MODE being wider than a word if X is a
10922 constant integer or has a mode the same size. */
10923 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10924 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10925 goto fail;
10927 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10928 won't know what to do. So we will strip off the SUBREG here and
10929 process normally. */
10930 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10932 x = SUBREG_REG (x);
10934 /* For use in case we fall down into the address adjustments
10935 further below, we need to adjust the known mode and size of
10936 x; imode and isize, since we just adjusted x. */
10937 imode = GET_MODE (x);
10939 if (imode == omode)
10940 return x;
10942 isize = GET_MODE_SIZE (imode);
10945 result = gen_lowpart_common (omode, x);
10947 if (result)
10948 return result;
10950 if (MEM_P (x))
10952 int offset = 0;
10954 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10955 address. */
10956 if (MEM_VOLATILE_P (x)
10957 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10958 goto fail;
10960 /* If we want to refer to something bigger than the original memref,
10961 generate a paradoxical subreg instead. That will force a reload
10962 of the original memref X. */
10963 if (isize < osize)
10964 return gen_rtx_SUBREG (omode, x, 0);
10966 if (WORDS_BIG_ENDIAN)
10967 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10969 /* Adjust the address so that the address-after-the-data is
10970 unchanged. */
10971 if (BYTES_BIG_ENDIAN)
10972 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10974 return adjust_address_nv (x, omode, offset);
10977 /* If X is a comparison operator, rewrite it in a new mode. This
10978 probably won't match, but may allow further simplifications. */
10979 else if (COMPARISON_P (x))
10980 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10982 /* If we couldn't simplify X any other way, just enclose it in a
10983 SUBREG. Normally, this SUBREG won't match, but some patterns may
10984 include an explicit SUBREG or we may simplify it further in combine. */
10985 else
10987 int offset = 0;
10988 rtx res;
10990 offset = subreg_lowpart_offset (omode, imode);
10991 if (imode == VOIDmode)
10993 imode = int_mode_for_mode (omode);
10994 x = gen_lowpart_common (imode, x);
10995 if (x == NULL)
10996 goto fail;
10998 res = simplify_gen_subreg (omode, x, imode, offset);
10999 if (res)
11000 return res;
11003 fail:
11004 return gen_rtx_CLOBBER (omode, const0_rtx);
11007 /* Try to simplify a comparison between OP0 and a constant OP1,
11008 where CODE is the comparison code that will be tested, into a
11009 (CODE OP0 const0_rtx) form.
11011 The result is a possibly different comparison code to use.
11012 *POP1 may be updated. */
11014 static enum rtx_code
11015 simplify_compare_const (enum rtx_code code, machine_mode mode,
11016 rtx op0, rtx *pop1)
11018 unsigned int mode_width = GET_MODE_PRECISION (mode);
11019 HOST_WIDE_INT const_op = INTVAL (*pop1);
11021 /* Get the constant we are comparing against and turn off all bits
11022 not on in our mode. */
11023 if (mode != VOIDmode)
11024 const_op = trunc_int_for_mode (const_op, mode);
11026 /* If we are comparing against a constant power of two and the value
11027 being compared can only have that single bit nonzero (e.g., it was
11028 `and'ed with that bit), we can replace this with a comparison
11029 with zero. */
11030 if (const_op
11031 && (code == EQ || code == NE || code == GE || code == GEU
11032 || code == LT || code == LTU)
11033 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11034 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11035 && (nonzero_bits (op0, mode)
11036 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11038 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11039 const_op = 0;
11042 /* Similarly, if we are comparing a value known to be either -1 or
11043 0 with -1, change it to the opposite comparison against zero. */
11044 if (const_op == -1
11045 && (code == EQ || code == NE || code == GT || code == LE
11046 || code == GEU || code == LTU)
11047 && num_sign_bit_copies (op0, mode) == mode_width)
11049 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11050 const_op = 0;
11053 /* Do some canonicalizations based on the comparison code. We prefer
11054 comparisons against zero and then prefer equality comparisons.
11055 If we can reduce the size of a constant, we will do that too. */
11056 switch (code)
11058 case LT:
11059 /* < C is equivalent to <= (C - 1) */
11060 if (const_op > 0)
11062 const_op -= 1;
11063 code = LE;
11064 /* ... fall through to LE case below. */
11066 else
11067 break;
11069 case LE:
11070 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11071 if (const_op < 0)
11073 const_op += 1;
11074 code = LT;
11077 /* If we are doing a <= 0 comparison on a value known to have
11078 a zero sign bit, we can replace this with == 0. */
11079 else if (const_op == 0
11080 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11081 && (nonzero_bits (op0, mode)
11082 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11083 == 0)
11084 code = EQ;
11085 break;
11087 case GE:
11088 /* >= C is equivalent to > (C - 1). */
11089 if (const_op > 0)
11091 const_op -= 1;
11092 code = GT;
11093 /* ... fall through to GT below. */
11095 else
11096 break;
11098 case GT:
11099 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11100 if (const_op < 0)
11102 const_op += 1;
11103 code = GE;
11106 /* If we are doing a > 0 comparison on a value known to have
11107 a zero sign bit, we can replace this with != 0. */
11108 else if (const_op == 0
11109 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11110 && (nonzero_bits (op0, mode)
11111 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11112 == 0)
11113 code = NE;
11114 break;
11116 case LTU:
11117 /* < C is equivalent to <= (C - 1). */
11118 if (const_op > 0)
11120 const_op -= 1;
11121 code = LEU;
11122 /* ... fall through ... */
11124 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11125 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11126 && (unsigned HOST_WIDE_INT) const_op
11127 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11129 const_op = 0;
11130 code = GE;
11131 break;
11133 else
11134 break;
11136 case LEU:
11137 /* unsigned <= 0 is equivalent to == 0 */
11138 if (const_op == 0)
11139 code = EQ;
11140 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11141 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11142 && (unsigned HOST_WIDE_INT) const_op
11143 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11145 const_op = 0;
11146 code = GE;
11148 break;
11150 case GEU:
11151 /* >= C is equivalent to > (C - 1). */
11152 if (const_op > 1)
11154 const_op -= 1;
11155 code = GTU;
11156 /* ... fall through ... */
11159 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11160 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11161 && (unsigned HOST_WIDE_INT) const_op
11162 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11164 const_op = 0;
11165 code = LT;
11166 break;
11168 else
11169 break;
11171 case GTU:
11172 /* unsigned > 0 is equivalent to != 0 */
11173 if (const_op == 0)
11174 code = NE;
11175 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11176 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11177 && (unsigned HOST_WIDE_INT) const_op
11178 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11180 const_op = 0;
11181 code = LT;
11183 break;
11185 default:
11186 break;
11189 *pop1 = GEN_INT (const_op);
11190 return code;
11193 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11194 comparison code that will be tested.
11196 The result is a possibly different comparison code to use. *POP0 and
11197 *POP1 may be updated.
11199 It is possible that we might detect that a comparison is either always
11200 true or always false. However, we do not perform general constant
11201 folding in combine, so this knowledge isn't useful. Such tautologies
11202 should have been detected earlier. Hence we ignore all such cases. */
11204 static enum rtx_code
11205 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11207 rtx op0 = *pop0;
11208 rtx op1 = *pop1;
11209 rtx tem, tem1;
11210 int i;
11211 machine_mode mode, tmode;
11213 /* Try a few ways of applying the same transformation to both operands. */
11214 while (1)
11216 #ifndef WORD_REGISTER_OPERATIONS
11217 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11218 so check specially. */
11219 if (code != GTU && code != GEU && code != LTU && code != LEU
11220 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11221 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11222 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11223 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11224 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11225 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11226 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11227 && CONST_INT_P (XEXP (op0, 1))
11228 && XEXP (op0, 1) == XEXP (op1, 1)
11229 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11230 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11231 && (INTVAL (XEXP (op0, 1))
11232 == (GET_MODE_PRECISION (GET_MODE (op0))
11233 - (GET_MODE_PRECISION
11234 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11236 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11237 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11239 #endif
11241 /* If both operands are the same constant shift, see if we can ignore the
11242 shift. We can if the shift is a rotate or if the bits shifted out of
11243 this shift are known to be zero for both inputs and if the type of
11244 comparison is compatible with the shift. */
11245 if (GET_CODE (op0) == GET_CODE (op1)
11246 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11247 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11248 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11249 && (code != GT && code != LT && code != GE && code != LE))
11250 || (GET_CODE (op0) == ASHIFTRT
11251 && (code != GTU && code != LTU
11252 && code != GEU && code != LEU)))
11253 && CONST_INT_P (XEXP (op0, 1))
11254 && INTVAL (XEXP (op0, 1)) >= 0
11255 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11256 && XEXP (op0, 1) == XEXP (op1, 1))
11258 machine_mode mode = GET_MODE (op0);
11259 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11260 int shift_count = INTVAL (XEXP (op0, 1));
11262 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11263 mask &= (mask >> shift_count) << shift_count;
11264 else if (GET_CODE (op0) == ASHIFT)
11265 mask = (mask & (mask << shift_count)) >> shift_count;
11267 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11268 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11269 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11270 else
11271 break;
11274 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11275 SUBREGs are of the same mode, and, in both cases, the AND would
11276 be redundant if the comparison was done in the narrower mode,
11277 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11278 and the operand's possibly nonzero bits are 0xffffff01; in that case
11279 if we only care about QImode, we don't need the AND). This case
11280 occurs if the output mode of an scc insn is not SImode and
11281 STORE_FLAG_VALUE == 1 (e.g., the 386).
11283 Similarly, check for a case where the AND's are ZERO_EXTEND
11284 operations from some narrower mode even though a SUBREG is not
11285 present. */
11287 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11288 && CONST_INT_P (XEXP (op0, 1))
11289 && CONST_INT_P (XEXP (op1, 1)))
11291 rtx inner_op0 = XEXP (op0, 0);
11292 rtx inner_op1 = XEXP (op1, 0);
11293 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11294 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11295 int changed = 0;
11297 if (paradoxical_subreg_p (inner_op0)
11298 && GET_CODE (inner_op1) == SUBREG
11299 && (GET_MODE (SUBREG_REG (inner_op0))
11300 == GET_MODE (SUBREG_REG (inner_op1)))
11301 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11302 <= HOST_BITS_PER_WIDE_INT)
11303 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11304 GET_MODE (SUBREG_REG (inner_op0)))))
11305 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11306 GET_MODE (SUBREG_REG (inner_op1))))))
11308 op0 = SUBREG_REG (inner_op0);
11309 op1 = SUBREG_REG (inner_op1);
11311 /* The resulting comparison is always unsigned since we masked
11312 off the original sign bit. */
11313 code = unsigned_condition (code);
11315 changed = 1;
11318 else if (c0 == c1)
11319 for (tmode = GET_CLASS_NARROWEST_MODE
11320 (GET_MODE_CLASS (GET_MODE (op0)));
11321 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11322 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11324 op0 = gen_lowpart (tmode, inner_op0);
11325 op1 = gen_lowpart (tmode, inner_op1);
11326 code = unsigned_condition (code);
11327 changed = 1;
11328 break;
11331 if (! changed)
11332 break;
11335 /* If both operands are NOT, we can strip off the outer operation
11336 and adjust the comparison code for swapped operands; similarly for
11337 NEG, except that this must be an equality comparison. */
11338 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11339 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11340 && (code == EQ || code == NE)))
11341 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11343 else
11344 break;
11347 /* If the first operand is a constant, swap the operands and adjust the
11348 comparison code appropriately, but don't do this if the second operand
11349 is already a constant integer. */
11350 if (swap_commutative_operands_p (op0, op1))
11352 tem = op0, op0 = op1, op1 = tem;
11353 code = swap_condition (code);
11356 /* We now enter a loop during which we will try to simplify the comparison.
11357 For the most part, we only are concerned with comparisons with zero,
11358 but some things may really be comparisons with zero but not start
11359 out looking that way. */
11361 while (CONST_INT_P (op1))
11363 machine_mode mode = GET_MODE (op0);
11364 unsigned int mode_width = GET_MODE_PRECISION (mode);
11365 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11366 int equality_comparison_p;
11367 int sign_bit_comparison_p;
11368 int unsigned_comparison_p;
11369 HOST_WIDE_INT const_op;
11371 /* We only want to handle integral modes. This catches VOIDmode,
11372 CCmode, and the floating-point modes. An exception is that we
11373 can handle VOIDmode if OP0 is a COMPARE or a comparison
11374 operation. */
11376 if (GET_MODE_CLASS (mode) != MODE_INT
11377 && ! (mode == VOIDmode
11378 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11379 break;
11381 /* Try to simplify the compare to constant, possibly changing the
11382 comparison op, and/or changing op1 to zero. */
11383 code = simplify_compare_const (code, mode, op0, &op1);
11384 const_op = INTVAL (op1);
11386 /* Compute some predicates to simplify code below. */
11388 equality_comparison_p = (code == EQ || code == NE);
11389 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11390 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11391 || code == GEU);
11393 /* If this is a sign bit comparison and we can do arithmetic in
11394 MODE, say that we will only be needing the sign bit of OP0. */
11395 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11396 op0 = force_to_mode (op0, mode,
11397 (unsigned HOST_WIDE_INT) 1
11398 << (GET_MODE_PRECISION (mode) - 1),
11401 /* Now try cases based on the opcode of OP0. If none of the cases
11402 does a "continue", we exit this loop immediately after the
11403 switch. */
11405 switch (GET_CODE (op0))
11407 case ZERO_EXTRACT:
11408 /* If we are extracting a single bit from a variable position in
11409 a constant that has only a single bit set and are comparing it
11410 with zero, we can convert this into an equality comparison
11411 between the position and the location of the single bit. */
11412 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11413 have already reduced the shift count modulo the word size. */
11414 if (!SHIFT_COUNT_TRUNCATED
11415 && CONST_INT_P (XEXP (op0, 0))
11416 && XEXP (op0, 1) == const1_rtx
11417 && equality_comparison_p && const_op == 0
11418 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11420 if (BITS_BIG_ENDIAN)
11421 i = BITS_PER_WORD - 1 - i;
11423 op0 = XEXP (op0, 2);
11424 op1 = GEN_INT (i);
11425 const_op = i;
11427 /* Result is nonzero iff shift count is equal to I. */
11428 code = reverse_condition (code);
11429 continue;
11432 /* ... fall through ... */
11434 case SIGN_EXTRACT:
11435 tem = expand_compound_operation (op0);
11436 if (tem != op0)
11438 op0 = tem;
11439 continue;
11441 break;
11443 case NOT:
11444 /* If testing for equality, we can take the NOT of the constant. */
11445 if (equality_comparison_p
11446 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11448 op0 = XEXP (op0, 0);
11449 op1 = tem;
11450 continue;
11453 /* If just looking at the sign bit, reverse the sense of the
11454 comparison. */
11455 if (sign_bit_comparison_p)
11457 op0 = XEXP (op0, 0);
11458 code = (code == GE ? LT : GE);
11459 continue;
11461 break;
11463 case NEG:
11464 /* If testing for equality, we can take the NEG of the constant. */
11465 if (equality_comparison_p
11466 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11468 op0 = XEXP (op0, 0);
11469 op1 = tem;
11470 continue;
11473 /* The remaining cases only apply to comparisons with zero. */
11474 if (const_op != 0)
11475 break;
11477 /* When X is ABS or is known positive,
11478 (neg X) is < 0 if and only if X != 0. */
11480 if (sign_bit_comparison_p
11481 && (GET_CODE (XEXP (op0, 0)) == ABS
11482 || (mode_width <= HOST_BITS_PER_WIDE_INT
11483 && (nonzero_bits (XEXP (op0, 0), mode)
11484 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11485 == 0)))
11487 op0 = XEXP (op0, 0);
11488 code = (code == LT ? NE : EQ);
11489 continue;
11492 /* If we have NEG of something whose two high-order bits are the
11493 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11494 if (num_sign_bit_copies (op0, mode) >= 2)
11496 op0 = XEXP (op0, 0);
11497 code = swap_condition (code);
11498 continue;
11500 break;
11502 case ROTATE:
11503 /* If we are testing equality and our count is a constant, we
11504 can perform the inverse operation on our RHS. */
11505 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11506 && (tem = simplify_binary_operation (ROTATERT, mode,
11507 op1, XEXP (op0, 1))) != 0)
11509 op0 = XEXP (op0, 0);
11510 op1 = tem;
11511 continue;
11514 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11515 a particular bit. Convert it to an AND of a constant of that
11516 bit. This will be converted into a ZERO_EXTRACT. */
11517 if (const_op == 0 && sign_bit_comparison_p
11518 && CONST_INT_P (XEXP (op0, 1))
11519 && mode_width <= HOST_BITS_PER_WIDE_INT)
11521 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11522 ((unsigned HOST_WIDE_INT) 1
11523 << (mode_width - 1
11524 - INTVAL (XEXP (op0, 1)))));
11525 code = (code == LT ? NE : EQ);
11526 continue;
11529 /* Fall through. */
11531 case ABS:
11532 /* ABS is ignorable inside an equality comparison with zero. */
11533 if (const_op == 0 && equality_comparison_p)
11535 op0 = XEXP (op0, 0);
11536 continue;
11538 break;
11540 case SIGN_EXTEND:
11541 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11542 (compare FOO CONST) if CONST fits in FOO's mode and we
11543 are either testing inequality or have an unsigned
11544 comparison with ZERO_EXTEND or a signed comparison with
11545 SIGN_EXTEND. But don't do it if we don't have a compare
11546 insn of the given mode, since we'd have to revert it
11547 later on, and then we wouldn't know whether to sign- or
11548 zero-extend. */
11549 mode = GET_MODE (XEXP (op0, 0));
11550 if (GET_MODE_CLASS (mode) == MODE_INT
11551 && ! unsigned_comparison_p
11552 && HWI_COMPUTABLE_MODE_P (mode)
11553 && trunc_int_for_mode (const_op, mode) == const_op
11554 && have_insn_for (COMPARE, mode))
11556 op0 = XEXP (op0, 0);
11557 continue;
11559 break;
11561 case SUBREG:
11562 /* Check for the case where we are comparing A - C1 with C2, that is
11564 (subreg:MODE (plus (A) (-C1))) op (C2)
11566 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11567 comparison in the wider mode. One of the following two conditions
11568 must be true in order for this to be valid:
11570 1. The mode extension results in the same bit pattern being added
11571 on both sides and the comparison is equality or unsigned. As
11572 C2 has been truncated to fit in MODE, the pattern can only be
11573 all 0s or all 1s.
11575 2. The mode extension results in the sign bit being copied on
11576 each side.
11578 The difficulty here is that we have predicates for A but not for
11579 (A - C1) so we need to check that C1 is within proper bounds so
11580 as to perturbate A as little as possible. */
11582 if (mode_width <= HOST_BITS_PER_WIDE_INT
11583 && subreg_lowpart_p (op0)
11584 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11585 && GET_CODE (SUBREG_REG (op0)) == PLUS
11586 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11588 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11589 rtx a = XEXP (SUBREG_REG (op0), 0);
11590 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11592 if ((c1 > 0
11593 && (unsigned HOST_WIDE_INT) c1
11594 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11595 && (equality_comparison_p || unsigned_comparison_p)
11596 /* (A - C1) zero-extends if it is positive and sign-extends
11597 if it is negative, C2 both zero- and sign-extends. */
11598 && ((0 == (nonzero_bits (a, inner_mode)
11599 & ~GET_MODE_MASK (mode))
11600 && const_op >= 0)
11601 /* (A - C1) sign-extends if it is positive and 1-extends
11602 if it is negative, C2 both sign- and 1-extends. */
11603 || (num_sign_bit_copies (a, inner_mode)
11604 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11605 - mode_width)
11606 && const_op < 0)))
11607 || ((unsigned HOST_WIDE_INT) c1
11608 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11609 /* (A - C1) always sign-extends, like C2. */
11610 && num_sign_bit_copies (a, inner_mode)
11611 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11612 - (mode_width - 1))))
11614 op0 = SUBREG_REG (op0);
11615 continue;
11619 /* If the inner mode is narrower and we are extracting the low part,
11620 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11621 if (subreg_lowpart_p (op0)
11622 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11623 /* Fall through */ ;
11624 else
11625 break;
11627 /* ... fall through ... */
11629 case ZERO_EXTEND:
11630 mode = GET_MODE (XEXP (op0, 0));
11631 if (GET_MODE_CLASS (mode) == MODE_INT
11632 && (unsigned_comparison_p || equality_comparison_p)
11633 && HWI_COMPUTABLE_MODE_P (mode)
11634 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11635 && const_op >= 0
11636 && have_insn_for (COMPARE, mode))
11638 op0 = XEXP (op0, 0);
11639 continue;
11641 break;
11643 case PLUS:
11644 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11645 this for equality comparisons due to pathological cases involving
11646 overflows. */
11647 if (equality_comparison_p
11648 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11649 op1, XEXP (op0, 1))))
11651 op0 = XEXP (op0, 0);
11652 op1 = tem;
11653 continue;
11656 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11657 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11658 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11660 op0 = XEXP (XEXP (op0, 0), 0);
11661 code = (code == LT ? EQ : NE);
11662 continue;
11664 break;
11666 case MINUS:
11667 /* We used to optimize signed comparisons against zero, but that
11668 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11669 arrive here as equality comparisons, or (GEU, LTU) are
11670 optimized away. No need to special-case them. */
11672 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11673 (eq B (minus A C)), whichever simplifies. We can only do
11674 this for equality comparisons due to pathological cases involving
11675 overflows. */
11676 if (equality_comparison_p
11677 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11678 XEXP (op0, 1), op1)))
11680 op0 = XEXP (op0, 0);
11681 op1 = tem;
11682 continue;
11685 if (equality_comparison_p
11686 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11687 XEXP (op0, 0), op1)))
11689 op0 = XEXP (op0, 1);
11690 op1 = tem;
11691 continue;
11694 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11695 of bits in X minus 1, is one iff X > 0. */
11696 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11697 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11698 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11699 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11701 op0 = XEXP (op0, 1);
11702 code = (code == GE ? LE : GT);
11703 continue;
11705 break;
11707 case XOR:
11708 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11709 if C is zero or B is a constant. */
11710 if (equality_comparison_p
11711 && 0 != (tem = simplify_binary_operation (XOR, mode,
11712 XEXP (op0, 1), op1)))
11714 op0 = XEXP (op0, 0);
11715 op1 = tem;
11716 continue;
11718 break;
11720 case EQ: case NE:
11721 case UNEQ: case LTGT:
11722 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11723 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11724 case UNORDERED: case ORDERED:
11725 /* We can't do anything if OP0 is a condition code value, rather
11726 than an actual data value. */
11727 if (const_op != 0
11728 || CC0_P (XEXP (op0, 0))
11729 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11730 break;
11732 /* Get the two operands being compared. */
11733 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11734 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11735 else
11736 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11738 /* Check for the cases where we simply want the result of the
11739 earlier test or the opposite of that result. */
11740 if (code == NE || code == EQ
11741 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11742 && (code == LT || code == GE)))
11744 enum rtx_code new_code;
11745 if (code == LT || code == NE)
11746 new_code = GET_CODE (op0);
11747 else
11748 new_code = reversed_comparison_code (op0, NULL);
11750 if (new_code != UNKNOWN)
11752 code = new_code;
11753 op0 = tem;
11754 op1 = tem1;
11755 continue;
11758 break;
11760 case IOR:
11761 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11762 iff X <= 0. */
11763 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11764 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11765 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11767 op0 = XEXP (op0, 1);
11768 code = (code == GE ? GT : LE);
11769 continue;
11771 break;
11773 case AND:
11774 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11775 will be converted to a ZERO_EXTRACT later. */
11776 if (const_op == 0 && equality_comparison_p
11777 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11778 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11780 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11781 XEXP (XEXP (op0, 0), 1));
11782 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11783 continue;
11786 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11787 zero and X is a comparison and C1 and C2 describe only bits set
11788 in STORE_FLAG_VALUE, we can compare with X. */
11789 if (const_op == 0 && equality_comparison_p
11790 && mode_width <= HOST_BITS_PER_WIDE_INT
11791 && CONST_INT_P (XEXP (op0, 1))
11792 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11793 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11794 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11795 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11797 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11798 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11799 if ((~STORE_FLAG_VALUE & mask) == 0
11800 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11801 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11802 && COMPARISON_P (tem))))
11804 op0 = XEXP (XEXP (op0, 0), 0);
11805 continue;
11809 /* If we are doing an equality comparison of an AND of a bit equal
11810 to the sign bit, replace this with a LT or GE comparison of
11811 the underlying value. */
11812 if (equality_comparison_p
11813 && const_op == 0
11814 && CONST_INT_P (XEXP (op0, 1))
11815 && mode_width <= HOST_BITS_PER_WIDE_INT
11816 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11817 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11819 op0 = XEXP (op0, 0);
11820 code = (code == EQ ? GE : LT);
11821 continue;
11824 /* If this AND operation is really a ZERO_EXTEND from a narrower
11825 mode, the constant fits within that mode, and this is either an
11826 equality or unsigned comparison, try to do this comparison in
11827 the narrower mode.
11829 Note that in:
11831 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11832 -> (ne:DI (reg:SI 4) (const_int 0))
11834 unless TRULY_NOOP_TRUNCATION allows it or the register is
11835 known to hold a value of the required mode the
11836 transformation is invalid. */
11837 if ((equality_comparison_p || unsigned_comparison_p)
11838 && CONST_INT_P (XEXP (op0, 1))
11839 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11840 & GET_MODE_MASK (mode))
11841 + 1)) >= 0
11842 && const_op >> i == 0
11843 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11844 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11845 || (REG_P (XEXP (op0, 0))
11846 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11848 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11849 continue;
11852 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11853 fits in both M1 and M2 and the SUBREG is either paradoxical
11854 or represents the low part, permute the SUBREG and the AND
11855 and try again. */
11856 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11858 unsigned HOST_WIDE_INT c1;
11859 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11860 /* Require an integral mode, to avoid creating something like
11861 (AND:SF ...). */
11862 if (SCALAR_INT_MODE_P (tmode)
11863 /* It is unsafe to commute the AND into the SUBREG if the
11864 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11865 not defined. As originally written the upper bits
11866 have a defined value due to the AND operation.
11867 However, if we commute the AND inside the SUBREG then
11868 they no longer have defined values and the meaning of
11869 the code has been changed. */
11870 && (0
11871 #ifdef WORD_REGISTER_OPERATIONS
11872 || (mode_width > GET_MODE_PRECISION (tmode)
11873 && mode_width <= BITS_PER_WORD)
11874 #endif
11875 || (mode_width <= GET_MODE_PRECISION (tmode)
11876 && subreg_lowpart_p (XEXP (op0, 0))))
11877 && CONST_INT_P (XEXP (op0, 1))
11878 && mode_width <= HOST_BITS_PER_WIDE_INT
11879 && HWI_COMPUTABLE_MODE_P (tmode)
11880 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11881 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11882 && c1 != mask
11883 && c1 != GET_MODE_MASK (tmode))
11885 op0 = simplify_gen_binary (AND, tmode,
11886 SUBREG_REG (XEXP (op0, 0)),
11887 gen_int_mode (c1, tmode));
11888 op0 = gen_lowpart (mode, op0);
11889 continue;
11893 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11894 if (const_op == 0 && equality_comparison_p
11895 && XEXP (op0, 1) == const1_rtx
11896 && GET_CODE (XEXP (op0, 0)) == NOT)
11898 op0 = simplify_and_const_int (NULL_RTX, mode,
11899 XEXP (XEXP (op0, 0), 0), 1);
11900 code = (code == NE ? EQ : NE);
11901 continue;
11904 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11905 (eq (and (lshiftrt X) 1) 0).
11906 Also handle the case where (not X) is expressed using xor. */
11907 if (const_op == 0 && equality_comparison_p
11908 && XEXP (op0, 1) == const1_rtx
11909 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11911 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11912 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11914 if (GET_CODE (shift_op) == NOT
11915 || (GET_CODE (shift_op) == XOR
11916 && CONST_INT_P (XEXP (shift_op, 1))
11917 && CONST_INT_P (shift_count)
11918 && HWI_COMPUTABLE_MODE_P (mode)
11919 && (UINTVAL (XEXP (shift_op, 1))
11920 == (unsigned HOST_WIDE_INT) 1
11921 << INTVAL (shift_count))))
11924 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11925 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11926 code = (code == NE ? EQ : NE);
11927 continue;
11930 break;
11932 case ASHIFT:
11933 /* If we have (compare (ashift FOO N) (const_int C)) and
11934 the high order N bits of FOO (N+1 if an inequality comparison)
11935 are known to be zero, we can do this by comparing FOO with C
11936 shifted right N bits so long as the low-order N bits of C are
11937 zero. */
11938 if (CONST_INT_P (XEXP (op0, 1))
11939 && INTVAL (XEXP (op0, 1)) >= 0
11940 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11941 < HOST_BITS_PER_WIDE_INT)
11942 && (((unsigned HOST_WIDE_INT) const_op
11943 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11944 - 1)) == 0)
11945 && mode_width <= HOST_BITS_PER_WIDE_INT
11946 && (nonzero_bits (XEXP (op0, 0), mode)
11947 & ~(mask >> (INTVAL (XEXP (op0, 1))
11948 + ! equality_comparison_p))) == 0)
11950 /* We must perform a logical shift, not an arithmetic one,
11951 as we want the top N bits of C to be zero. */
11952 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11954 temp >>= INTVAL (XEXP (op0, 1));
11955 op1 = gen_int_mode (temp, mode);
11956 op0 = XEXP (op0, 0);
11957 continue;
11960 /* If we are doing a sign bit comparison, it means we are testing
11961 a particular bit. Convert it to the appropriate AND. */
11962 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11963 && mode_width <= HOST_BITS_PER_WIDE_INT)
11965 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11966 ((unsigned HOST_WIDE_INT) 1
11967 << (mode_width - 1
11968 - INTVAL (XEXP (op0, 1)))));
11969 code = (code == LT ? NE : EQ);
11970 continue;
11973 /* If this an equality comparison with zero and we are shifting
11974 the low bit to the sign bit, we can convert this to an AND of the
11975 low-order bit. */
11976 if (const_op == 0 && equality_comparison_p
11977 && CONST_INT_P (XEXP (op0, 1))
11978 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11980 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11981 continue;
11983 break;
11985 case ASHIFTRT:
11986 /* If this is an equality comparison with zero, we can do this
11987 as a logical shift, which might be much simpler. */
11988 if (equality_comparison_p && const_op == 0
11989 && CONST_INT_P (XEXP (op0, 1)))
11991 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11992 XEXP (op0, 0),
11993 INTVAL (XEXP (op0, 1)));
11994 continue;
11997 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11998 do the comparison in a narrower mode. */
11999 if (! unsigned_comparison_p
12000 && CONST_INT_P (XEXP (op0, 1))
12001 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12002 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12003 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12004 MODE_INT, 1)) != BLKmode
12005 && (((unsigned HOST_WIDE_INT) const_op
12006 + (GET_MODE_MASK (tmode) >> 1) + 1)
12007 <= GET_MODE_MASK (tmode)))
12009 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12010 continue;
12013 /* Likewise if OP0 is a PLUS of a sign extension with a
12014 constant, which is usually represented with the PLUS
12015 between the shifts. */
12016 if (! unsigned_comparison_p
12017 && CONST_INT_P (XEXP (op0, 1))
12018 && GET_CODE (XEXP (op0, 0)) == PLUS
12019 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12020 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12021 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12022 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12023 MODE_INT, 1)) != BLKmode
12024 && (((unsigned HOST_WIDE_INT) const_op
12025 + (GET_MODE_MASK (tmode) >> 1) + 1)
12026 <= GET_MODE_MASK (tmode)))
12028 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12029 rtx add_const = XEXP (XEXP (op0, 0), 1);
12030 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12031 add_const, XEXP (op0, 1));
12033 op0 = simplify_gen_binary (PLUS, tmode,
12034 gen_lowpart (tmode, inner),
12035 new_const);
12036 continue;
12039 /* ... fall through ... */
12040 case LSHIFTRT:
12041 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12042 the low order N bits of FOO are known to be zero, we can do this
12043 by comparing FOO with C shifted left N bits so long as no
12044 overflow occurs. Even if the low order N bits of FOO aren't known
12045 to be zero, if the comparison is >= or < we can use the same
12046 optimization and for > or <= by setting all the low
12047 order N bits in the comparison constant. */
12048 if (CONST_INT_P (XEXP (op0, 1))
12049 && INTVAL (XEXP (op0, 1)) > 0
12050 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12051 && mode_width <= HOST_BITS_PER_WIDE_INT
12052 && (((unsigned HOST_WIDE_INT) const_op
12053 + (GET_CODE (op0) != LSHIFTRT
12054 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12055 + 1)
12056 : 0))
12057 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12059 unsigned HOST_WIDE_INT low_bits
12060 = (nonzero_bits (XEXP (op0, 0), mode)
12061 & (((unsigned HOST_WIDE_INT) 1
12062 << INTVAL (XEXP (op0, 1))) - 1));
12063 if (low_bits == 0 || !equality_comparison_p)
12065 /* If the shift was logical, then we must make the condition
12066 unsigned. */
12067 if (GET_CODE (op0) == LSHIFTRT)
12068 code = unsigned_condition (code);
12070 const_op <<= INTVAL (XEXP (op0, 1));
12071 if (low_bits != 0
12072 && (code == GT || code == GTU
12073 || code == LE || code == LEU))
12074 const_op
12075 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12076 op1 = GEN_INT (const_op);
12077 op0 = XEXP (op0, 0);
12078 continue;
12082 /* If we are using this shift to extract just the sign bit, we
12083 can replace this with an LT or GE comparison. */
12084 if (const_op == 0
12085 && (equality_comparison_p || sign_bit_comparison_p)
12086 && CONST_INT_P (XEXP (op0, 1))
12087 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12089 op0 = XEXP (op0, 0);
12090 code = (code == NE || code == GT ? LT : GE);
12091 continue;
12093 break;
12095 default:
12096 break;
12099 break;
12102 /* Now make any compound operations involved in this comparison. Then,
12103 check for an outmost SUBREG on OP0 that is not doing anything or is
12104 paradoxical. The latter transformation must only be performed when
12105 it is known that the "extra" bits will be the same in op0 and op1 or
12106 that they don't matter. There are three cases to consider:
12108 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12109 care bits and we can assume they have any convenient value. So
12110 making the transformation is safe.
12112 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12113 In this case the upper bits of op0 are undefined. We should not make
12114 the simplification in that case as we do not know the contents of
12115 those bits.
12117 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12118 UNKNOWN. In that case we know those bits are zeros or ones. We must
12119 also be sure that they are the same as the upper bits of op1.
12121 We can never remove a SUBREG for a non-equality comparison because
12122 the sign bit is in a different place in the underlying object. */
12124 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12125 op1 = make_compound_operation (op1, SET);
12127 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12128 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12129 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12130 && (code == NE || code == EQ))
12132 if (paradoxical_subreg_p (op0))
12134 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12135 implemented. */
12136 if (REG_P (SUBREG_REG (op0)))
12138 op0 = SUBREG_REG (op0);
12139 op1 = gen_lowpart (GET_MODE (op0), op1);
12142 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12143 <= HOST_BITS_PER_WIDE_INT)
12144 && (nonzero_bits (SUBREG_REG (op0),
12145 GET_MODE (SUBREG_REG (op0)))
12146 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12148 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12150 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12151 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12152 op0 = SUBREG_REG (op0), op1 = tem;
12156 /* We now do the opposite procedure: Some machines don't have compare
12157 insns in all modes. If OP0's mode is an integer mode smaller than a
12158 word and we can't do a compare in that mode, see if there is a larger
12159 mode for which we can do the compare. There are a number of cases in
12160 which we can use the wider mode. */
12162 mode = GET_MODE (op0);
12163 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12164 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12165 && ! have_insn_for (COMPARE, mode))
12166 for (tmode = GET_MODE_WIDER_MODE (mode);
12167 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12168 tmode = GET_MODE_WIDER_MODE (tmode))
12169 if (have_insn_for (COMPARE, tmode))
12171 int zero_extended;
12173 /* If this is a test for negative, we can make an explicit
12174 test of the sign bit. Test this first so we can use
12175 a paradoxical subreg to extend OP0. */
12177 if (op1 == const0_rtx && (code == LT || code == GE)
12178 && HWI_COMPUTABLE_MODE_P (mode))
12180 unsigned HOST_WIDE_INT sign
12181 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12182 op0 = simplify_gen_binary (AND, tmode,
12183 gen_lowpart (tmode, op0),
12184 gen_int_mode (sign, tmode));
12185 code = (code == LT) ? NE : EQ;
12186 break;
12189 /* If the only nonzero bits in OP0 and OP1 are those in the
12190 narrower mode and this is an equality or unsigned comparison,
12191 we can use the wider mode. Similarly for sign-extended
12192 values, in which case it is true for all comparisons. */
12193 zero_extended = ((code == EQ || code == NE
12194 || code == GEU || code == GTU
12195 || code == LEU || code == LTU)
12196 && (nonzero_bits (op0, tmode)
12197 & ~GET_MODE_MASK (mode)) == 0
12198 && ((CONST_INT_P (op1)
12199 || (nonzero_bits (op1, tmode)
12200 & ~GET_MODE_MASK (mode)) == 0)));
12202 if (zero_extended
12203 || ((num_sign_bit_copies (op0, tmode)
12204 > (unsigned int) (GET_MODE_PRECISION (tmode)
12205 - GET_MODE_PRECISION (mode)))
12206 && (num_sign_bit_copies (op1, tmode)
12207 > (unsigned int) (GET_MODE_PRECISION (tmode)
12208 - GET_MODE_PRECISION (mode)))))
12210 /* If OP0 is an AND and we don't have an AND in MODE either,
12211 make a new AND in the proper mode. */
12212 if (GET_CODE (op0) == AND
12213 && !have_insn_for (AND, mode))
12214 op0 = simplify_gen_binary (AND, tmode,
12215 gen_lowpart (tmode,
12216 XEXP (op0, 0)),
12217 gen_lowpart (tmode,
12218 XEXP (op0, 1)));
12219 else
12221 if (zero_extended)
12223 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12224 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12226 else
12228 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12229 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12231 break;
12236 /* We may have changed the comparison operands. Re-canonicalize. */
12237 if (swap_commutative_operands_p (op0, op1))
12239 tem = op0, op0 = op1, op1 = tem;
12240 code = swap_condition (code);
12243 /* If this machine only supports a subset of valid comparisons, see if we
12244 can convert an unsupported one into a supported one. */
12245 target_canonicalize_comparison (&code, &op0, &op1, 0);
12247 *pop0 = op0;
12248 *pop1 = op1;
12250 return code;
12253 /* Utility function for record_value_for_reg. Count number of
12254 rtxs in X. */
12255 static int
12256 count_rtxs (rtx x)
12258 enum rtx_code code = GET_CODE (x);
12259 const char *fmt;
12260 int i, j, ret = 1;
12262 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12263 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12265 rtx x0 = XEXP (x, 0);
12266 rtx x1 = XEXP (x, 1);
12268 if (x0 == x1)
12269 return 1 + 2 * count_rtxs (x0);
12271 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12272 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12273 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12274 return 2 + 2 * count_rtxs (x0)
12275 + count_rtxs (x == XEXP (x1, 0)
12276 ? XEXP (x1, 1) : XEXP (x1, 0));
12278 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12279 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12280 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12281 return 2 + 2 * count_rtxs (x1)
12282 + count_rtxs (x == XEXP (x0, 0)
12283 ? XEXP (x0, 1) : XEXP (x0, 0));
12286 fmt = GET_RTX_FORMAT (code);
12287 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12288 if (fmt[i] == 'e')
12289 ret += count_rtxs (XEXP (x, i));
12290 else if (fmt[i] == 'E')
12291 for (j = 0; j < XVECLEN (x, i); j++)
12292 ret += count_rtxs (XVECEXP (x, i, j));
12294 return ret;
12297 /* Utility function for following routine. Called when X is part of a value
12298 being stored into last_set_value. Sets last_set_table_tick
12299 for each register mentioned. Similar to mention_regs in cse.c */
12301 static void
12302 update_table_tick (rtx x)
12304 enum rtx_code code = GET_CODE (x);
12305 const char *fmt = GET_RTX_FORMAT (code);
12306 int i, j;
12308 if (code == REG)
12310 unsigned int regno = REGNO (x);
12311 unsigned int endregno = END_REGNO (x);
12312 unsigned int r;
12314 for (r = regno; r < endregno; r++)
12316 reg_stat_type *rsp = &reg_stat[r];
12317 rsp->last_set_table_tick = label_tick;
12320 return;
12323 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12324 if (fmt[i] == 'e')
12326 /* Check for identical subexpressions. If x contains
12327 identical subexpression we only have to traverse one of
12328 them. */
12329 if (i == 0 && ARITHMETIC_P (x))
12331 /* Note that at this point x1 has already been
12332 processed. */
12333 rtx x0 = XEXP (x, 0);
12334 rtx x1 = XEXP (x, 1);
12336 /* If x0 and x1 are identical then there is no need to
12337 process x0. */
12338 if (x0 == x1)
12339 break;
12341 /* If x0 is identical to a subexpression of x1 then while
12342 processing x1, x0 has already been processed. Thus we
12343 are done with x. */
12344 if (ARITHMETIC_P (x1)
12345 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12346 break;
12348 /* If x1 is identical to a subexpression of x0 then we
12349 still have to process the rest of x0. */
12350 if (ARITHMETIC_P (x0)
12351 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12353 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12354 break;
12358 update_table_tick (XEXP (x, i));
12360 else if (fmt[i] == 'E')
12361 for (j = 0; j < XVECLEN (x, i); j++)
12362 update_table_tick (XVECEXP (x, i, j));
12365 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12366 are saying that the register is clobbered and we no longer know its
12367 value. If INSN is zero, don't update reg_stat[].last_set; this is
12368 only permitted with VALUE also zero and is used to invalidate the
12369 register. */
12371 static void
12372 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12374 unsigned int regno = REGNO (reg);
12375 unsigned int endregno = END_REGNO (reg);
12376 unsigned int i;
12377 reg_stat_type *rsp;
12379 /* If VALUE contains REG and we have a previous value for REG, substitute
12380 the previous value. */
12381 if (value && insn && reg_overlap_mentioned_p (reg, value))
12383 rtx tem;
12385 /* Set things up so get_last_value is allowed to see anything set up to
12386 our insn. */
12387 subst_low_luid = DF_INSN_LUID (insn);
12388 tem = get_last_value (reg);
12390 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12391 it isn't going to be useful and will take a lot of time to process,
12392 so just use the CLOBBER. */
12394 if (tem)
12396 if (ARITHMETIC_P (tem)
12397 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12398 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12399 tem = XEXP (tem, 0);
12400 else if (count_occurrences (value, reg, 1) >= 2)
12402 /* If there are two or more occurrences of REG in VALUE,
12403 prevent the value from growing too much. */
12404 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12405 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12408 value = replace_rtx (copy_rtx (value), reg, tem);
12412 /* For each register modified, show we don't know its value, that
12413 we don't know about its bitwise content, that its value has been
12414 updated, and that we don't know the location of the death of the
12415 register. */
12416 for (i = regno; i < endregno; i++)
12418 rsp = &reg_stat[i];
12420 if (insn)
12421 rsp->last_set = insn;
12423 rsp->last_set_value = 0;
12424 rsp->last_set_mode = VOIDmode;
12425 rsp->last_set_nonzero_bits = 0;
12426 rsp->last_set_sign_bit_copies = 0;
12427 rsp->last_death = 0;
12428 rsp->truncated_to_mode = VOIDmode;
12431 /* Mark registers that are being referenced in this value. */
12432 if (value)
12433 update_table_tick (value);
12435 /* Now update the status of each register being set.
12436 If someone is using this register in this block, set this register
12437 to invalid since we will get confused between the two lives in this
12438 basic block. This makes using this register always invalid. In cse, we
12439 scan the table to invalidate all entries using this register, but this
12440 is too much work for us. */
12442 for (i = regno; i < endregno; i++)
12444 rsp = &reg_stat[i];
12445 rsp->last_set_label = label_tick;
12446 if (!insn
12447 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12448 rsp->last_set_invalid = 1;
12449 else
12450 rsp->last_set_invalid = 0;
12453 /* The value being assigned might refer to X (like in "x++;"). In that
12454 case, we must replace it with (clobber (const_int 0)) to prevent
12455 infinite loops. */
12456 rsp = &reg_stat[regno];
12457 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12459 value = copy_rtx (value);
12460 if (!get_last_value_validate (&value, insn, label_tick, 1))
12461 value = 0;
12464 /* For the main register being modified, update the value, the mode, the
12465 nonzero bits, and the number of sign bit copies. */
12467 rsp->last_set_value = value;
12469 if (value)
12471 machine_mode mode = GET_MODE (reg);
12472 subst_low_luid = DF_INSN_LUID (insn);
12473 rsp->last_set_mode = mode;
12474 if (GET_MODE_CLASS (mode) == MODE_INT
12475 && HWI_COMPUTABLE_MODE_P (mode))
12476 mode = nonzero_bits_mode;
12477 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12478 rsp->last_set_sign_bit_copies
12479 = num_sign_bit_copies (value, GET_MODE (reg));
12483 /* Called via note_stores from record_dead_and_set_regs to handle one
12484 SET or CLOBBER in an insn. DATA is the instruction in which the
12485 set is occurring. */
12487 static void
12488 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12490 rtx_insn *record_dead_insn = (rtx_insn *) data;
12492 if (GET_CODE (dest) == SUBREG)
12493 dest = SUBREG_REG (dest);
12495 if (!record_dead_insn)
12497 if (REG_P (dest))
12498 record_value_for_reg (dest, NULL, NULL_RTX);
12499 return;
12502 if (REG_P (dest))
12504 /* If we are setting the whole register, we know its value. Otherwise
12505 show that we don't know the value. We can handle SUBREG in
12506 some cases. */
12507 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12508 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12509 else if (GET_CODE (setter) == SET
12510 && GET_CODE (SET_DEST (setter)) == SUBREG
12511 && SUBREG_REG (SET_DEST (setter)) == dest
12512 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12513 && subreg_lowpart_p (SET_DEST (setter)))
12514 record_value_for_reg (dest, record_dead_insn,
12515 gen_lowpart (GET_MODE (dest),
12516 SET_SRC (setter)));
12517 else
12518 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12520 else if (MEM_P (dest)
12521 /* Ignore pushes, they clobber nothing. */
12522 && ! push_operand (dest, GET_MODE (dest)))
12523 mem_last_set = DF_INSN_LUID (record_dead_insn);
12526 /* Update the records of when each REG was most recently set or killed
12527 for the things done by INSN. This is the last thing done in processing
12528 INSN in the combiner loop.
12530 We update reg_stat[], in particular fields last_set, last_set_value,
12531 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12532 last_death, and also the similar information mem_last_set (which insn
12533 most recently modified memory) and last_call_luid (which insn was the
12534 most recent subroutine call). */
12536 static void
12537 record_dead_and_set_regs (rtx_insn *insn)
12539 rtx link;
12540 unsigned int i;
12542 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12544 if (REG_NOTE_KIND (link) == REG_DEAD
12545 && REG_P (XEXP (link, 0)))
12547 unsigned int regno = REGNO (XEXP (link, 0));
12548 unsigned int endregno = END_REGNO (XEXP (link, 0));
12550 for (i = regno; i < endregno; i++)
12552 reg_stat_type *rsp;
12554 rsp = &reg_stat[i];
12555 rsp->last_death = insn;
12558 else if (REG_NOTE_KIND (link) == REG_INC)
12559 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12562 if (CALL_P (insn))
12564 hard_reg_set_iterator hrsi;
12565 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12567 reg_stat_type *rsp;
12569 rsp = &reg_stat[i];
12570 rsp->last_set_invalid = 1;
12571 rsp->last_set = insn;
12572 rsp->last_set_value = 0;
12573 rsp->last_set_mode = VOIDmode;
12574 rsp->last_set_nonzero_bits = 0;
12575 rsp->last_set_sign_bit_copies = 0;
12576 rsp->last_death = 0;
12577 rsp->truncated_to_mode = VOIDmode;
12580 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12582 /* We can't combine into a call pattern. Remember, though, that
12583 the return value register is set at this LUID. We could
12584 still replace a register with the return value from the
12585 wrong subroutine call! */
12586 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12588 else
12589 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12592 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12593 register present in the SUBREG, so for each such SUBREG go back and
12594 adjust nonzero and sign bit information of the registers that are
12595 known to have some zero/sign bits set.
12597 This is needed because when combine blows the SUBREGs away, the
12598 information on zero/sign bits is lost and further combines can be
12599 missed because of that. */
12601 static void
12602 record_promoted_value (rtx_insn *insn, rtx subreg)
12604 struct insn_link *links;
12605 rtx set;
12606 unsigned int regno = REGNO (SUBREG_REG (subreg));
12607 machine_mode mode = GET_MODE (subreg);
12609 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12610 return;
12612 for (links = LOG_LINKS (insn); links;)
12614 reg_stat_type *rsp;
12616 insn = links->insn;
12617 set = single_set (insn);
12619 if (! set || !REG_P (SET_DEST (set))
12620 || REGNO (SET_DEST (set)) != regno
12621 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12623 links = links->next;
12624 continue;
12627 rsp = &reg_stat[regno];
12628 if (rsp->last_set == insn)
12630 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12631 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12634 if (REG_P (SET_SRC (set)))
12636 regno = REGNO (SET_SRC (set));
12637 links = LOG_LINKS (insn);
12639 else
12640 break;
12644 /* Check if X, a register, is known to contain a value already
12645 truncated to MODE. In this case we can use a subreg to refer to
12646 the truncated value even though in the generic case we would need
12647 an explicit truncation. */
12649 static bool
12650 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12652 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12653 machine_mode truncated = rsp->truncated_to_mode;
12655 if (truncated == 0
12656 || rsp->truncation_label < label_tick_ebb_start)
12657 return false;
12658 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12659 return true;
12660 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12661 return true;
12662 return false;
12665 /* If X is a hard reg or a subreg record the mode that the register is
12666 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12667 to turn a truncate into a subreg using this information. Return true
12668 if traversing X is complete. */
12670 static bool
12671 record_truncated_value (rtx x)
12673 machine_mode truncated_mode;
12674 reg_stat_type *rsp;
12676 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12678 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12679 truncated_mode = GET_MODE (x);
12681 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12682 return true;
12684 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12685 return true;
12687 x = SUBREG_REG (x);
12689 /* ??? For hard-regs we now record everything. We might be able to
12690 optimize this using last_set_mode. */
12691 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12692 truncated_mode = GET_MODE (x);
12693 else
12694 return false;
12696 rsp = &reg_stat[REGNO (x)];
12697 if (rsp->truncated_to_mode == 0
12698 || rsp->truncation_label < label_tick_ebb_start
12699 || (GET_MODE_SIZE (truncated_mode)
12700 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12702 rsp->truncated_to_mode = truncated_mode;
12703 rsp->truncation_label = label_tick;
12706 return true;
12709 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12710 the modes they are used in. This can help truning TRUNCATEs into
12711 SUBREGs. */
12713 static void
12714 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12716 subrtx_var_iterator::array_type array;
12717 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12718 if (record_truncated_value (*iter))
12719 iter.skip_subrtxes ();
12722 /* Scan X for promoted SUBREGs. For each one found,
12723 note what it implies to the registers used in it. */
12725 static void
12726 check_promoted_subreg (rtx_insn *insn, rtx x)
12728 if (GET_CODE (x) == SUBREG
12729 && SUBREG_PROMOTED_VAR_P (x)
12730 && REG_P (SUBREG_REG (x)))
12731 record_promoted_value (insn, x);
12732 else
12734 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12735 int i, j;
12737 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12738 switch (format[i])
12740 case 'e':
12741 check_promoted_subreg (insn, XEXP (x, i));
12742 break;
12743 case 'V':
12744 case 'E':
12745 if (XVEC (x, i) != 0)
12746 for (j = 0; j < XVECLEN (x, i); j++)
12747 check_promoted_subreg (insn, XVECEXP (x, i, j));
12748 break;
12753 /* Verify that all the registers and memory references mentioned in *LOC are
12754 still valid. *LOC was part of a value set in INSN when label_tick was
12755 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12756 the invalid references with (clobber (const_int 0)) and return 1. This
12757 replacement is useful because we often can get useful information about
12758 the form of a value (e.g., if it was produced by a shift that always
12759 produces -1 or 0) even though we don't know exactly what registers it
12760 was produced from. */
12762 static int
12763 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12765 rtx x = *loc;
12766 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12767 int len = GET_RTX_LENGTH (GET_CODE (x));
12768 int i, j;
12770 if (REG_P (x))
12772 unsigned int regno = REGNO (x);
12773 unsigned int endregno = END_REGNO (x);
12774 unsigned int j;
12776 for (j = regno; j < endregno; j++)
12778 reg_stat_type *rsp = &reg_stat[j];
12779 if (rsp->last_set_invalid
12780 /* If this is a pseudo-register that was only set once and not
12781 live at the beginning of the function, it is always valid. */
12782 || (! (regno >= FIRST_PSEUDO_REGISTER
12783 && REG_N_SETS (regno) == 1
12784 && (!REGNO_REG_SET_P
12785 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12786 regno)))
12787 && rsp->last_set_label > tick))
12789 if (replace)
12790 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12791 return replace;
12795 return 1;
12797 /* If this is a memory reference, make sure that there were no stores after
12798 it that might have clobbered the value. We don't have alias info, so we
12799 assume any store invalidates it. Moreover, we only have local UIDs, so
12800 we also assume that there were stores in the intervening basic blocks. */
12801 else if (MEM_P (x) && !MEM_READONLY_P (x)
12802 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12804 if (replace)
12805 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12806 return replace;
12809 for (i = 0; i < len; i++)
12811 if (fmt[i] == 'e')
12813 /* Check for identical subexpressions. If x contains
12814 identical subexpression we only have to traverse one of
12815 them. */
12816 if (i == 1 && ARITHMETIC_P (x))
12818 /* Note that at this point x0 has already been checked
12819 and found valid. */
12820 rtx x0 = XEXP (x, 0);
12821 rtx x1 = XEXP (x, 1);
12823 /* If x0 and x1 are identical then x is also valid. */
12824 if (x0 == x1)
12825 return 1;
12827 /* If x1 is identical to a subexpression of x0 then
12828 while checking x0, x1 has already been checked. Thus
12829 it is valid and so as x. */
12830 if (ARITHMETIC_P (x0)
12831 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12832 return 1;
12834 /* If x0 is identical to a subexpression of x1 then x is
12835 valid iff the rest of x1 is valid. */
12836 if (ARITHMETIC_P (x1)
12837 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12838 return
12839 get_last_value_validate (&XEXP (x1,
12840 x0 == XEXP (x1, 0) ? 1 : 0),
12841 insn, tick, replace);
12844 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12845 replace) == 0)
12846 return 0;
12848 else if (fmt[i] == 'E')
12849 for (j = 0; j < XVECLEN (x, i); j++)
12850 if (get_last_value_validate (&XVECEXP (x, i, j),
12851 insn, tick, replace) == 0)
12852 return 0;
12855 /* If we haven't found a reason for it to be invalid, it is valid. */
12856 return 1;
12859 /* Get the last value assigned to X, if known. Some registers
12860 in the value may be replaced with (clobber (const_int 0)) if their value
12861 is known longer known reliably. */
12863 static rtx
12864 get_last_value (const_rtx x)
12866 unsigned int regno;
12867 rtx value;
12868 reg_stat_type *rsp;
12870 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12871 then convert it to the desired mode. If this is a paradoxical SUBREG,
12872 we cannot predict what values the "extra" bits might have. */
12873 if (GET_CODE (x) == SUBREG
12874 && subreg_lowpart_p (x)
12875 && !paradoxical_subreg_p (x)
12876 && (value = get_last_value (SUBREG_REG (x))) != 0)
12877 return gen_lowpart (GET_MODE (x), value);
12879 if (!REG_P (x))
12880 return 0;
12882 regno = REGNO (x);
12883 rsp = &reg_stat[regno];
12884 value = rsp->last_set_value;
12886 /* If we don't have a value, or if it isn't for this basic block and
12887 it's either a hard register, set more than once, or it's a live
12888 at the beginning of the function, return 0.
12890 Because if it's not live at the beginning of the function then the reg
12891 is always set before being used (is never used without being set).
12892 And, if it's set only once, and it's always set before use, then all
12893 uses must have the same last value, even if it's not from this basic
12894 block. */
12896 if (value == 0
12897 || (rsp->last_set_label < label_tick_ebb_start
12898 && (regno < FIRST_PSEUDO_REGISTER
12899 || REG_N_SETS (regno) != 1
12900 || REGNO_REG_SET_P
12901 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12902 return 0;
12904 /* If the value was set in a later insn than the ones we are processing,
12905 we can't use it even if the register was only set once. */
12906 if (rsp->last_set_label == label_tick
12907 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12908 return 0;
12910 /* If the value has all its registers valid, return it. */
12911 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12912 return value;
12914 /* Otherwise, make a copy and replace any invalid register with
12915 (clobber (const_int 0)). If that fails for some reason, return 0. */
12917 value = copy_rtx (value);
12918 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12919 return value;
12921 return 0;
12924 /* Return nonzero if expression X refers to a REG or to memory
12925 that is set in an instruction more recent than FROM_LUID. */
12927 static int
12928 use_crosses_set_p (const_rtx x, int from_luid)
12930 const char *fmt;
12931 int i;
12932 enum rtx_code code = GET_CODE (x);
12934 if (code == REG)
12936 unsigned int regno = REGNO (x);
12937 unsigned endreg = END_REGNO (x);
12939 #ifdef PUSH_ROUNDING
12940 /* Don't allow uses of the stack pointer to be moved,
12941 because we don't know whether the move crosses a push insn. */
12942 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12943 return 1;
12944 #endif
12945 for (; regno < endreg; regno++)
12947 reg_stat_type *rsp = &reg_stat[regno];
12948 if (rsp->last_set
12949 && rsp->last_set_label == label_tick
12950 && DF_INSN_LUID (rsp->last_set) > from_luid)
12951 return 1;
12953 return 0;
12956 if (code == MEM && mem_last_set > from_luid)
12957 return 1;
12959 fmt = GET_RTX_FORMAT (code);
12961 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12963 if (fmt[i] == 'E')
12965 int j;
12966 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12967 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12968 return 1;
12970 else if (fmt[i] == 'e'
12971 && use_crosses_set_p (XEXP (x, i), from_luid))
12972 return 1;
12974 return 0;
12977 /* Define three variables used for communication between the following
12978 routines. */
12980 static unsigned int reg_dead_regno, reg_dead_endregno;
12981 static int reg_dead_flag;
12983 /* Function called via note_stores from reg_dead_at_p.
12985 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12986 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12988 static void
12989 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12991 unsigned int regno, endregno;
12993 if (!REG_P (dest))
12994 return;
12996 regno = REGNO (dest);
12997 endregno = END_REGNO (dest);
12998 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12999 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13002 /* Return nonzero if REG is known to be dead at INSN.
13004 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13005 referencing REG, it is dead. If we hit a SET referencing REG, it is
13006 live. Otherwise, see if it is live or dead at the start of the basic
13007 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13008 must be assumed to be always live. */
13010 static int
13011 reg_dead_at_p (rtx reg, rtx_insn *insn)
13013 basic_block block;
13014 unsigned int i;
13016 /* Set variables for reg_dead_at_p_1. */
13017 reg_dead_regno = REGNO (reg);
13018 reg_dead_endregno = END_REGNO (reg);
13020 reg_dead_flag = 0;
13022 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13023 we allow the machine description to decide whether use-and-clobber
13024 patterns are OK. */
13025 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13027 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13028 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13029 return 0;
13032 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13033 beginning of basic block. */
13034 block = BLOCK_FOR_INSN (insn);
13035 for (;;)
13037 if (INSN_P (insn))
13039 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13040 return 1;
13042 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13043 if (reg_dead_flag)
13044 return reg_dead_flag == 1 ? 1 : 0;
13046 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13047 return 1;
13050 if (insn == BB_HEAD (block))
13051 break;
13053 insn = PREV_INSN (insn);
13056 /* Look at live-in sets for the basic block that we were in. */
13057 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13058 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13059 return 0;
13061 return 1;
13064 /* Note hard registers in X that are used. */
13066 static void
13067 mark_used_regs_combine (rtx x)
13069 RTX_CODE code = GET_CODE (x);
13070 unsigned int regno;
13071 int i;
13073 switch (code)
13075 case LABEL_REF:
13076 case SYMBOL_REF:
13077 case CONST:
13078 CASE_CONST_ANY:
13079 case PC:
13080 case ADDR_VEC:
13081 case ADDR_DIFF_VEC:
13082 case ASM_INPUT:
13083 #ifdef HAVE_cc0
13084 /* CC0 must die in the insn after it is set, so we don't need to take
13085 special note of it here. */
13086 case CC0:
13087 #endif
13088 return;
13090 case CLOBBER:
13091 /* If we are clobbering a MEM, mark any hard registers inside the
13092 address as used. */
13093 if (MEM_P (XEXP (x, 0)))
13094 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13095 return;
13097 case REG:
13098 regno = REGNO (x);
13099 /* A hard reg in a wide mode may really be multiple registers.
13100 If so, mark all of them just like the first. */
13101 if (regno < FIRST_PSEUDO_REGISTER)
13103 /* None of this applies to the stack, frame or arg pointers. */
13104 if (regno == STACK_POINTER_REGNUM
13105 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13106 || regno == HARD_FRAME_POINTER_REGNUM
13107 #endif
13108 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13109 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13110 #endif
13111 || regno == FRAME_POINTER_REGNUM)
13112 return;
13114 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13116 return;
13118 case SET:
13120 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13121 the address. */
13122 rtx testreg = SET_DEST (x);
13124 while (GET_CODE (testreg) == SUBREG
13125 || GET_CODE (testreg) == ZERO_EXTRACT
13126 || GET_CODE (testreg) == STRICT_LOW_PART)
13127 testreg = XEXP (testreg, 0);
13129 if (MEM_P (testreg))
13130 mark_used_regs_combine (XEXP (testreg, 0));
13132 mark_used_regs_combine (SET_SRC (x));
13134 return;
13136 default:
13137 break;
13140 /* Recursively scan the operands of this expression. */
13143 const char *fmt = GET_RTX_FORMAT (code);
13145 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13147 if (fmt[i] == 'e')
13148 mark_used_regs_combine (XEXP (x, i));
13149 else if (fmt[i] == 'E')
13151 int j;
13153 for (j = 0; j < XVECLEN (x, i); j++)
13154 mark_used_regs_combine (XVECEXP (x, i, j));
13160 /* Remove register number REGNO from the dead registers list of INSN.
13162 Return the note used to record the death, if there was one. */
13165 remove_death (unsigned int regno, rtx_insn *insn)
13167 rtx note = find_regno_note (insn, REG_DEAD, regno);
13169 if (note)
13170 remove_note (insn, note);
13172 return note;
13175 /* For each register (hardware or pseudo) used within expression X, if its
13176 death is in an instruction with luid between FROM_LUID (inclusive) and
13177 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13178 list headed by PNOTES.
13180 That said, don't move registers killed by maybe_kill_insn.
13182 This is done when X is being merged by combination into TO_INSN. These
13183 notes will then be distributed as needed. */
13185 static void
13186 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13187 rtx *pnotes)
13189 const char *fmt;
13190 int len, i;
13191 enum rtx_code code = GET_CODE (x);
13193 if (code == REG)
13195 unsigned int regno = REGNO (x);
13196 rtx_insn *where_dead = reg_stat[regno].last_death;
13198 /* Don't move the register if it gets killed in between from and to. */
13199 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13200 && ! reg_referenced_p (x, maybe_kill_insn))
13201 return;
13203 if (where_dead
13204 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13205 && DF_INSN_LUID (where_dead) >= from_luid
13206 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13208 rtx note = remove_death (regno, where_dead);
13210 /* It is possible for the call above to return 0. This can occur
13211 when last_death points to I2 or I1 that we combined with.
13212 In that case make a new note.
13214 We must also check for the case where X is a hard register
13215 and NOTE is a death note for a range of hard registers
13216 including X. In that case, we must put REG_DEAD notes for
13217 the remaining registers in place of NOTE. */
13219 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13220 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13221 > GET_MODE_SIZE (GET_MODE (x))))
13223 unsigned int deadregno = REGNO (XEXP (note, 0));
13224 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13225 unsigned int ourend = END_HARD_REGNO (x);
13226 unsigned int i;
13228 for (i = deadregno; i < deadend; i++)
13229 if (i < regno || i >= ourend)
13230 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13233 /* If we didn't find any note, or if we found a REG_DEAD note that
13234 covers only part of the given reg, and we have a multi-reg hard
13235 register, then to be safe we must check for REG_DEAD notes
13236 for each register other than the first. They could have
13237 their own REG_DEAD notes lying around. */
13238 else if ((note == 0
13239 || (note != 0
13240 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13241 < GET_MODE_SIZE (GET_MODE (x)))))
13242 && regno < FIRST_PSEUDO_REGISTER
13243 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13245 unsigned int ourend = END_HARD_REGNO (x);
13246 unsigned int i, offset;
13247 rtx oldnotes = 0;
13249 if (note)
13250 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13251 else
13252 offset = 1;
13254 for (i = regno + offset; i < ourend; i++)
13255 move_deaths (regno_reg_rtx[i],
13256 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13259 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13261 XEXP (note, 1) = *pnotes;
13262 *pnotes = note;
13264 else
13265 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13268 return;
13271 else if (GET_CODE (x) == SET)
13273 rtx dest = SET_DEST (x);
13275 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13277 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13278 that accesses one word of a multi-word item, some
13279 piece of everything register in the expression is used by
13280 this insn, so remove any old death. */
13281 /* ??? So why do we test for equality of the sizes? */
13283 if (GET_CODE (dest) == ZERO_EXTRACT
13284 || GET_CODE (dest) == STRICT_LOW_PART
13285 || (GET_CODE (dest) == SUBREG
13286 && (((GET_MODE_SIZE (GET_MODE (dest))
13287 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13288 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13289 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13291 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13292 return;
13295 /* If this is some other SUBREG, we know it replaces the entire
13296 value, so use that as the destination. */
13297 if (GET_CODE (dest) == SUBREG)
13298 dest = SUBREG_REG (dest);
13300 /* If this is a MEM, adjust deaths of anything used in the address.
13301 For a REG (the only other possibility), the entire value is
13302 being replaced so the old value is not used in this insn. */
13304 if (MEM_P (dest))
13305 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13306 to_insn, pnotes);
13307 return;
13310 else if (GET_CODE (x) == CLOBBER)
13311 return;
13313 len = GET_RTX_LENGTH (code);
13314 fmt = GET_RTX_FORMAT (code);
13316 for (i = 0; i < len; i++)
13318 if (fmt[i] == 'E')
13320 int j;
13321 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13322 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13323 to_insn, pnotes);
13325 else if (fmt[i] == 'e')
13326 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13330 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13331 pattern of an insn. X must be a REG. */
13333 static int
13334 reg_bitfield_target_p (rtx x, rtx body)
13336 int i;
13338 if (GET_CODE (body) == SET)
13340 rtx dest = SET_DEST (body);
13341 rtx target;
13342 unsigned int regno, tregno, endregno, endtregno;
13344 if (GET_CODE (dest) == ZERO_EXTRACT)
13345 target = XEXP (dest, 0);
13346 else if (GET_CODE (dest) == STRICT_LOW_PART)
13347 target = SUBREG_REG (XEXP (dest, 0));
13348 else
13349 return 0;
13351 if (GET_CODE (target) == SUBREG)
13352 target = SUBREG_REG (target);
13354 if (!REG_P (target))
13355 return 0;
13357 tregno = REGNO (target), regno = REGNO (x);
13358 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13359 return target == x;
13361 endtregno = end_hard_regno (GET_MODE (target), tregno);
13362 endregno = end_hard_regno (GET_MODE (x), regno);
13364 return endregno > tregno && regno < endtregno;
13367 else if (GET_CODE (body) == PARALLEL)
13368 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13369 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13370 return 1;
13372 return 0;
13375 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13376 as appropriate. I3 and I2 are the insns resulting from the combination
13377 insns including FROM (I2 may be zero).
13379 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13380 not need REG_DEAD notes because they are being substituted for. This
13381 saves searching in the most common cases.
13383 Each note in the list is either ignored or placed on some insns, depending
13384 on the type of note. */
13386 static void
13387 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13388 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13390 rtx note, next_note;
13391 rtx tem_note;
13392 rtx_insn *tem_insn;
13394 for (note = notes; note; note = next_note)
13396 rtx_insn *place = 0, *place2 = 0;
13398 next_note = XEXP (note, 1);
13399 switch (REG_NOTE_KIND (note))
13401 case REG_BR_PROB:
13402 case REG_BR_PRED:
13403 /* Doesn't matter much where we put this, as long as it's somewhere.
13404 It is preferable to keep these notes on branches, which is most
13405 likely to be i3. */
13406 place = i3;
13407 break;
13409 case REG_NON_LOCAL_GOTO:
13410 if (JUMP_P (i3))
13411 place = i3;
13412 else
13414 gcc_assert (i2 && JUMP_P (i2));
13415 place = i2;
13417 break;
13419 case REG_EH_REGION:
13420 /* These notes must remain with the call or trapping instruction. */
13421 if (CALL_P (i3))
13422 place = i3;
13423 else if (i2 && CALL_P (i2))
13424 place = i2;
13425 else
13427 gcc_assert (cfun->can_throw_non_call_exceptions);
13428 if (may_trap_p (i3))
13429 place = i3;
13430 else if (i2 && may_trap_p (i2))
13431 place = i2;
13432 /* ??? Otherwise assume we've combined things such that we
13433 can now prove that the instructions can't trap. Drop the
13434 note in this case. */
13436 break;
13438 case REG_ARGS_SIZE:
13439 /* ??? How to distribute between i3-i1. Assume i3 contains the
13440 entire adjustment. Assert i3 contains at least some adjust. */
13441 if (!noop_move_p (i3))
13443 int old_size, args_size = INTVAL (XEXP (note, 0));
13444 /* fixup_args_size_notes looks at REG_NORETURN note,
13445 so ensure the note is placed there first. */
13446 if (CALL_P (i3))
13448 rtx *np;
13449 for (np = &next_note; *np; np = &XEXP (*np, 1))
13450 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13452 rtx n = *np;
13453 *np = XEXP (n, 1);
13454 XEXP (n, 1) = REG_NOTES (i3);
13455 REG_NOTES (i3) = n;
13456 break;
13459 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13460 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13461 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13462 gcc_assert (old_size != args_size
13463 || (CALL_P (i3)
13464 && !ACCUMULATE_OUTGOING_ARGS
13465 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13467 break;
13469 case REG_NORETURN:
13470 case REG_SETJMP:
13471 case REG_TM:
13472 case REG_CALL_DECL:
13473 /* These notes must remain with the call. It should not be
13474 possible for both I2 and I3 to be a call. */
13475 if (CALL_P (i3))
13476 place = i3;
13477 else
13479 gcc_assert (i2 && CALL_P (i2));
13480 place = i2;
13482 break;
13484 case REG_UNUSED:
13485 /* Any clobbers for i3 may still exist, and so we must process
13486 REG_UNUSED notes from that insn.
13488 Any clobbers from i2 or i1 can only exist if they were added by
13489 recog_for_combine. In that case, recog_for_combine created the
13490 necessary REG_UNUSED notes. Trying to keep any original
13491 REG_UNUSED notes from these insns can cause incorrect output
13492 if it is for the same register as the original i3 dest.
13493 In that case, we will notice that the register is set in i3,
13494 and then add a REG_UNUSED note for the destination of i3, which
13495 is wrong. However, it is possible to have REG_UNUSED notes from
13496 i2 or i1 for register which were both used and clobbered, so
13497 we keep notes from i2 or i1 if they will turn into REG_DEAD
13498 notes. */
13500 /* If this register is set or clobbered in I3, put the note there
13501 unless there is one already. */
13502 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13504 if (from_insn != i3)
13505 break;
13507 if (! (REG_P (XEXP (note, 0))
13508 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13509 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13510 place = i3;
13512 /* Otherwise, if this register is used by I3, then this register
13513 now dies here, so we must put a REG_DEAD note here unless there
13514 is one already. */
13515 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13516 && ! (REG_P (XEXP (note, 0))
13517 ? find_regno_note (i3, REG_DEAD,
13518 REGNO (XEXP (note, 0)))
13519 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13521 PUT_REG_NOTE_KIND (note, REG_DEAD);
13522 place = i3;
13524 break;
13526 case REG_EQUAL:
13527 case REG_EQUIV:
13528 case REG_NOALIAS:
13529 /* These notes say something about results of an insn. We can
13530 only support them if they used to be on I3 in which case they
13531 remain on I3. Otherwise they are ignored.
13533 If the note refers to an expression that is not a constant, we
13534 must also ignore the note since we cannot tell whether the
13535 equivalence is still true. It might be possible to do
13536 slightly better than this (we only have a problem if I2DEST
13537 or I1DEST is present in the expression), but it doesn't
13538 seem worth the trouble. */
13540 if (from_insn == i3
13541 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13542 place = i3;
13543 break;
13545 case REG_INC:
13546 /* These notes say something about how a register is used. They must
13547 be present on any use of the register in I2 or I3. */
13548 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13549 place = i3;
13551 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13553 if (place)
13554 place2 = i2;
13555 else
13556 place = i2;
13558 break;
13560 case REG_LABEL_TARGET:
13561 case REG_LABEL_OPERAND:
13562 /* This can show up in several ways -- either directly in the
13563 pattern, or hidden off in the constant pool with (or without?)
13564 a REG_EQUAL note. */
13565 /* ??? Ignore the without-reg_equal-note problem for now. */
13566 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13567 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13568 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13569 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13570 place = i3;
13572 if (i2
13573 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13574 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13575 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13576 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13578 if (place)
13579 place2 = i2;
13580 else
13581 place = i2;
13584 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13585 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13586 there. */
13587 if (place && JUMP_P (place)
13588 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13589 && (JUMP_LABEL (place) == NULL
13590 || JUMP_LABEL (place) == XEXP (note, 0)))
13592 rtx label = JUMP_LABEL (place);
13594 if (!label)
13595 JUMP_LABEL (place) = XEXP (note, 0);
13596 else if (LABEL_P (label))
13597 LABEL_NUSES (label)--;
13600 if (place2 && JUMP_P (place2)
13601 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13602 && (JUMP_LABEL (place2) == NULL
13603 || JUMP_LABEL (place2) == XEXP (note, 0)))
13605 rtx label = JUMP_LABEL (place2);
13607 if (!label)
13608 JUMP_LABEL (place2) = XEXP (note, 0);
13609 else if (LABEL_P (label))
13610 LABEL_NUSES (label)--;
13611 place2 = 0;
13613 break;
13615 case REG_NONNEG:
13616 /* This note says something about the value of a register prior
13617 to the execution of an insn. It is too much trouble to see
13618 if the note is still correct in all situations. It is better
13619 to simply delete it. */
13620 break;
13622 case REG_DEAD:
13623 /* If we replaced the right hand side of FROM_INSN with a
13624 REG_EQUAL note, the original use of the dying register
13625 will not have been combined into I3 and I2. In such cases,
13626 FROM_INSN is guaranteed to be the first of the combined
13627 instructions, so we simply need to search back before
13628 FROM_INSN for the previous use or set of this register,
13629 then alter the notes there appropriately.
13631 If the register is used as an input in I3, it dies there.
13632 Similarly for I2, if it is nonzero and adjacent to I3.
13634 If the register is not used as an input in either I3 or I2
13635 and it is not one of the registers we were supposed to eliminate,
13636 there are two possibilities. We might have a non-adjacent I2
13637 or we might have somehow eliminated an additional register
13638 from a computation. For example, we might have had A & B where
13639 we discover that B will always be zero. In this case we will
13640 eliminate the reference to A.
13642 In both cases, we must search to see if we can find a previous
13643 use of A and put the death note there. */
13645 if (from_insn
13646 && from_insn == i2mod
13647 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13648 tem_insn = from_insn;
13649 else
13651 if (from_insn
13652 && CALL_P (from_insn)
13653 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13654 place = from_insn;
13655 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13656 place = i3;
13657 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13658 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13659 place = i2;
13660 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13661 && !(i2mod
13662 && reg_overlap_mentioned_p (XEXP (note, 0),
13663 i2mod_old_rhs)))
13664 || rtx_equal_p (XEXP (note, 0), elim_i1)
13665 || rtx_equal_p (XEXP (note, 0), elim_i0))
13666 break;
13667 tem_insn = i3;
13670 if (place == 0)
13672 basic_block bb = this_basic_block;
13674 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13676 if (!NONDEBUG_INSN_P (tem_insn))
13678 if (tem_insn == BB_HEAD (bb))
13679 break;
13680 continue;
13683 /* If the register is being set at TEM_INSN, see if that is all
13684 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13685 into a REG_UNUSED note instead. Don't delete sets to
13686 global register vars. */
13687 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13688 || !global_regs[REGNO (XEXP (note, 0))])
13689 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13691 rtx set = single_set (tem_insn);
13692 rtx inner_dest = 0;
13693 #ifdef HAVE_cc0
13694 rtx_insn *cc0_setter = NULL;
13695 #endif
13697 if (set != 0)
13698 for (inner_dest = SET_DEST (set);
13699 (GET_CODE (inner_dest) == STRICT_LOW_PART
13700 || GET_CODE (inner_dest) == SUBREG
13701 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13702 inner_dest = XEXP (inner_dest, 0))
13705 /* Verify that it was the set, and not a clobber that
13706 modified the register.
13708 CC0 targets must be careful to maintain setter/user
13709 pairs. If we cannot delete the setter due to side
13710 effects, mark the user with an UNUSED note instead
13711 of deleting it. */
13713 if (set != 0 && ! side_effects_p (SET_SRC (set))
13714 && rtx_equal_p (XEXP (note, 0), inner_dest)
13715 #ifdef HAVE_cc0
13716 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13717 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13718 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13719 #endif
13722 /* Move the notes and links of TEM_INSN elsewhere.
13723 This might delete other dead insns recursively.
13724 First set the pattern to something that won't use
13725 any register. */
13726 rtx old_notes = REG_NOTES (tem_insn);
13728 PATTERN (tem_insn) = pc_rtx;
13729 REG_NOTES (tem_insn) = NULL;
13731 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13732 NULL_RTX, NULL_RTX, NULL_RTX);
13733 distribute_links (LOG_LINKS (tem_insn));
13735 SET_INSN_DELETED (tem_insn);
13736 if (tem_insn == i2)
13737 i2 = NULL;
13739 #ifdef HAVE_cc0
13740 /* Delete the setter too. */
13741 if (cc0_setter)
13743 PATTERN (cc0_setter) = pc_rtx;
13744 old_notes = REG_NOTES (cc0_setter);
13745 REG_NOTES (cc0_setter) = NULL;
13747 distribute_notes (old_notes, cc0_setter,
13748 cc0_setter, NULL,
13749 NULL_RTX, NULL_RTX, NULL_RTX);
13750 distribute_links (LOG_LINKS (cc0_setter));
13752 SET_INSN_DELETED (cc0_setter);
13753 if (cc0_setter == i2)
13754 i2 = NULL;
13756 #endif
13758 else
13760 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13762 /* If there isn't already a REG_UNUSED note, put one
13763 here. Do not place a REG_DEAD note, even if
13764 the register is also used here; that would not
13765 match the algorithm used in lifetime analysis
13766 and can cause the consistency check in the
13767 scheduler to fail. */
13768 if (! find_regno_note (tem_insn, REG_UNUSED,
13769 REGNO (XEXP (note, 0))))
13770 place = tem_insn;
13771 break;
13774 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13775 || (CALL_P (tem_insn)
13776 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13778 place = tem_insn;
13780 /* If we are doing a 3->2 combination, and we have a
13781 register which formerly died in i3 and was not used
13782 by i2, which now no longer dies in i3 and is used in
13783 i2 but does not die in i2, and place is between i2
13784 and i3, then we may need to move a link from place to
13785 i2. */
13786 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13787 && from_insn
13788 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13789 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13791 struct insn_link *links = LOG_LINKS (place);
13792 LOG_LINKS (place) = NULL;
13793 distribute_links (links);
13795 break;
13798 if (tem_insn == BB_HEAD (bb))
13799 break;
13804 /* If the register is set or already dead at PLACE, we needn't do
13805 anything with this note if it is still a REG_DEAD note.
13806 We check here if it is set at all, not if is it totally replaced,
13807 which is what `dead_or_set_p' checks, so also check for it being
13808 set partially. */
13810 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13812 unsigned int regno = REGNO (XEXP (note, 0));
13813 reg_stat_type *rsp = &reg_stat[regno];
13815 if (dead_or_set_p (place, XEXP (note, 0))
13816 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13818 /* Unless the register previously died in PLACE, clear
13819 last_death. [I no longer understand why this is
13820 being done.] */
13821 if (rsp->last_death != place)
13822 rsp->last_death = 0;
13823 place = 0;
13825 else
13826 rsp->last_death = place;
13828 /* If this is a death note for a hard reg that is occupying
13829 multiple registers, ensure that we are still using all
13830 parts of the object. If we find a piece of the object
13831 that is unused, we must arrange for an appropriate REG_DEAD
13832 note to be added for it. However, we can't just emit a USE
13833 and tag the note to it, since the register might actually
13834 be dead; so we recourse, and the recursive call then finds
13835 the previous insn that used this register. */
13837 if (place && regno < FIRST_PSEUDO_REGISTER
13838 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13840 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13841 bool all_used = true;
13842 unsigned int i;
13844 for (i = regno; i < endregno; i++)
13845 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13846 && ! find_regno_fusage (place, USE, i))
13847 || dead_or_set_regno_p (place, i))
13849 all_used = false;
13850 break;
13853 if (! all_used)
13855 /* Put only REG_DEAD notes for pieces that are
13856 not already dead or set. */
13858 for (i = regno; i < endregno;
13859 i += hard_regno_nregs[i][reg_raw_mode[i]])
13861 rtx piece = regno_reg_rtx[i];
13862 basic_block bb = this_basic_block;
13864 if (! dead_or_set_p (place, piece)
13865 && ! reg_bitfield_target_p (piece,
13866 PATTERN (place)))
13868 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13869 NULL_RTX);
13871 distribute_notes (new_note, place, place,
13872 NULL, NULL_RTX, NULL_RTX,
13873 NULL_RTX);
13875 else if (! refers_to_regno_p (i, i + 1,
13876 PATTERN (place), 0)
13877 && ! find_regno_fusage (place, USE, i))
13878 for (tem_insn = PREV_INSN (place); ;
13879 tem_insn = PREV_INSN (tem_insn))
13881 if (!NONDEBUG_INSN_P (tem_insn))
13883 if (tem_insn == BB_HEAD (bb))
13884 break;
13885 continue;
13887 if (dead_or_set_p (tem_insn, piece)
13888 || reg_bitfield_target_p (piece,
13889 PATTERN (tem_insn)))
13891 add_reg_note (tem_insn, REG_UNUSED, piece);
13892 break;
13897 place = 0;
13901 break;
13903 default:
13904 /* Any other notes should not be present at this point in the
13905 compilation. */
13906 gcc_unreachable ();
13909 if (place)
13911 XEXP (note, 1) = REG_NOTES (place);
13912 REG_NOTES (place) = note;
13915 if (place2)
13916 add_shallow_copy_of_reg_note (place2, note);
13920 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13921 I3, I2, and I1 to new locations. This is also called to add a link
13922 pointing at I3 when I3's destination is changed. */
13924 static void
13925 distribute_links (struct insn_link *links)
13927 struct insn_link *link, *next_link;
13929 for (link = links; link; link = next_link)
13931 rtx_insn *place = 0;
13932 rtx_insn *insn;
13933 rtx set, reg;
13935 next_link = link->next;
13937 /* If the insn that this link points to is a NOTE, ignore it. */
13938 if (NOTE_P (link->insn))
13939 continue;
13941 set = 0;
13942 rtx pat = PATTERN (link->insn);
13943 if (GET_CODE (pat) == SET)
13944 set = pat;
13945 else if (GET_CODE (pat) == PARALLEL)
13947 int i;
13948 for (i = 0; i < XVECLEN (pat, 0); i++)
13950 set = XVECEXP (pat, 0, i);
13951 if (GET_CODE (set) != SET)
13952 continue;
13954 reg = SET_DEST (set);
13955 while (GET_CODE (reg) == ZERO_EXTRACT
13956 || GET_CODE (reg) == STRICT_LOW_PART
13957 || GET_CODE (reg) == SUBREG)
13958 reg = XEXP (reg, 0);
13960 if (!REG_P (reg))
13961 continue;
13963 if (REGNO (reg) == link->regno)
13964 break;
13966 if (i == XVECLEN (pat, 0))
13967 continue;
13969 else
13970 continue;
13972 reg = SET_DEST (set);
13974 while (GET_CODE (reg) == ZERO_EXTRACT
13975 || GET_CODE (reg) == STRICT_LOW_PART
13976 || GET_CODE (reg) == SUBREG)
13977 reg = XEXP (reg, 0);
13979 /* A LOG_LINK is defined as being placed on the first insn that uses
13980 a register and points to the insn that sets the register. Start
13981 searching at the next insn after the target of the link and stop
13982 when we reach a set of the register or the end of the basic block.
13984 Note that this correctly handles the link that used to point from
13985 I3 to I2. Also note that not much searching is typically done here
13986 since most links don't point very far away. */
13988 for (insn = NEXT_INSN (link->insn);
13989 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13990 || BB_HEAD (this_basic_block->next_bb) != insn));
13991 insn = NEXT_INSN (insn))
13992 if (DEBUG_INSN_P (insn))
13993 continue;
13994 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13996 if (reg_referenced_p (reg, PATTERN (insn)))
13997 place = insn;
13998 break;
14000 else if (CALL_P (insn)
14001 && find_reg_fusage (insn, USE, reg))
14003 place = insn;
14004 break;
14006 else if (INSN_P (insn) && reg_set_p (reg, insn))
14007 break;
14009 /* If we found a place to put the link, place it there unless there
14010 is already a link to the same insn as LINK at that point. */
14012 if (place)
14014 struct insn_link *link2;
14016 FOR_EACH_LOG_LINK (link2, place)
14017 if (link2->insn == link->insn && link2->regno == link->regno)
14018 break;
14020 if (link2 == NULL)
14022 link->next = LOG_LINKS (place);
14023 LOG_LINKS (place) = link;
14025 /* Set added_links_insn to the earliest insn we added a
14026 link to. */
14027 if (added_links_insn == 0
14028 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14029 added_links_insn = place;
14035 /* Check for any register or memory mentioned in EQUIV that is not
14036 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14037 of EXPR where some registers may have been replaced by constants. */
14039 static bool
14040 unmentioned_reg_p (rtx equiv, rtx expr)
14042 subrtx_iterator::array_type array;
14043 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14045 const_rtx x = *iter;
14046 if ((REG_P (x) || MEM_P (x))
14047 && !reg_mentioned_p (x, expr))
14048 return true;
14050 return false;
14053 DEBUG_FUNCTION void
14054 dump_combine_stats (FILE *file)
14056 fprintf
14057 (file,
14058 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14059 combine_attempts, combine_merges, combine_extras, combine_successes);
14062 void
14063 dump_combine_total_stats (FILE *file)
14065 fprintf
14066 (file,
14067 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14068 total_attempts, total_merges, total_extras, total_successes);
14071 /* Try combining insns through substitution. */
14072 static unsigned int
14073 rest_of_handle_combine (void)
14075 int rebuild_jump_labels_after_combine;
14077 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14078 df_note_add_problem ();
14079 df_analyze ();
14081 regstat_init_n_sets_and_refs ();
14083 rebuild_jump_labels_after_combine
14084 = combine_instructions (get_insns (), max_reg_num ());
14086 /* Combining insns may have turned an indirect jump into a
14087 direct jump. Rebuild the JUMP_LABEL fields of jumping
14088 instructions. */
14089 if (rebuild_jump_labels_after_combine)
14091 timevar_push (TV_JUMP);
14092 rebuild_jump_labels (get_insns ());
14093 cleanup_cfg (0);
14094 timevar_pop (TV_JUMP);
14097 regstat_free_n_sets_and_refs ();
14098 return 0;
14101 namespace {
14103 const pass_data pass_data_combine =
14105 RTL_PASS, /* type */
14106 "combine", /* name */
14107 OPTGROUP_NONE, /* optinfo_flags */
14108 TV_COMBINE, /* tv_id */
14109 PROP_cfglayout, /* properties_required */
14110 0, /* properties_provided */
14111 0, /* properties_destroyed */
14112 0, /* todo_flags_start */
14113 TODO_df_finish, /* todo_flags_finish */
14116 class pass_combine : public rtl_opt_pass
14118 public:
14119 pass_combine (gcc::context *ctxt)
14120 : rtl_opt_pass (pass_data_combine, ctxt)
14123 /* opt_pass methods: */
14124 virtual bool gate (function *) { return (optimize > 0); }
14125 virtual unsigned int execute (function *)
14127 return rest_of_handle_combine ();
14130 }; // class pass_combine
14132 } // anon namespace
14134 rtl_opt_pass *
14135 make_pass_combine (gcc::context *ctxt)
14137 return new pass_combine (ctxt);