* combine.c (try_combine): Do not allow combining a PARALLEL I2
[official-gcc.git] / gcc / combine.c
blobc95b493886bced6f12026306ee41f711b21e7675
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 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1583 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &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 /* Only try four-insn combinations when there's high likelihood of
2592 success. Look for simple insns, such as loads of constants or
2593 binary operations involving a constant. */
2594 if (i0)
2596 int i;
2597 int ngood = 0;
2598 int nshift = 0;
2600 if (!flag_expensive_optimizations)
2601 return 0;
2603 for (i = 0; i < 4; i++)
2605 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2606 rtx set = single_set (insn);
2607 rtx src;
2608 if (!set)
2609 continue;
2610 src = SET_SRC (set);
2611 if (CONSTANT_P (src))
2613 ngood += 2;
2614 break;
2616 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2617 ngood++;
2618 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2619 || GET_CODE (src) == LSHIFTRT)
2620 nshift++;
2622 if (ngood < 2 && nshift < 2)
2623 return 0;
2626 /* Exit early if one of the insns involved can't be used for
2627 combinations. */
2628 if (CALL_P (i2)
2629 || (i1 && CALL_P (i1))
2630 || (i0 && CALL_P (i0))
2631 || cant_combine_insn_p (i3)
2632 || cant_combine_insn_p (i2)
2633 || (i1 && cant_combine_insn_p (i1))
2634 || (i0 && cant_combine_insn_p (i0))
2635 || likely_spilled_retval_p (i3))
2636 return 0;
2638 combine_attempts++;
2639 undobuf.other_insn = 0;
2641 /* Reset the hard register usage information. */
2642 CLEAR_HARD_REG_SET (newpat_used_regs);
2644 if (dump_file && (dump_flags & TDF_DETAILS))
2646 if (i0)
2647 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2648 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2649 else if (i1)
2650 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2651 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2652 else
2653 fprintf (dump_file, "\nTrying %d -> %d:\n",
2654 INSN_UID (i2), INSN_UID (i3));
2657 /* If multiple insns feed into one of I2 or I3, they can be in any
2658 order. To simplify the code below, reorder them in sequence. */
2659 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2660 temp_insn = i2, i2 = i0, i0 = temp_insn;
2661 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2662 temp_insn = i1, i1 = i0, i0 = temp_insn;
2663 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2664 temp_insn = i1, i1 = i2, i2 = temp_insn;
2666 added_links_insn = 0;
2668 /* First check for one important special case that the code below will
2669 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2670 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2671 we may be able to replace that destination with the destination of I3.
2672 This occurs in the common code where we compute both a quotient and
2673 remainder into a structure, in which case we want to do the computation
2674 directly into the structure to avoid register-register copies.
2676 Note that this case handles both multiple sets in I2 and also cases
2677 where I2 has a number of CLOBBERs inside the PARALLEL.
2679 We make very conservative checks below and only try to handle the
2680 most common cases of this. For example, we only handle the case
2681 where I2 and I3 are adjacent to avoid making difficult register
2682 usage tests. */
2684 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2685 && REG_P (SET_SRC (PATTERN (i3)))
2686 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2687 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2688 && GET_CODE (PATTERN (i2)) == PARALLEL
2689 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2690 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2691 below would need to check what is inside (and reg_overlap_mentioned_p
2692 doesn't support those codes anyway). Don't allow those destinations;
2693 the resulting insn isn't likely to be recognized anyway. */
2694 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2695 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2696 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2697 SET_DEST (PATTERN (i3)))
2698 && next_active_insn (i2) == i3)
2700 rtx p2 = PATTERN (i2);
2702 /* Make sure that the destination of I3,
2703 which we are going to substitute into one output of I2,
2704 is not used within another output of I2. We must avoid making this:
2705 (parallel [(set (mem (reg 69)) ...)
2706 (set (reg 69) ...)])
2707 which is not well-defined as to order of actions.
2708 (Besides, reload can't handle output reloads for this.)
2710 The problem can also happen if the dest of I3 is a memory ref,
2711 if another dest in I2 is an indirect memory ref. */
2712 for (i = 0; i < XVECLEN (p2, 0); i++)
2713 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2714 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2715 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2716 SET_DEST (XVECEXP (p2, 0, i))))
2717 break;
2719 /* Make sure this PARALLEL is not an asm. We do not allow combining
2720 that usually (see can_combine_p), so do not here either. */
2721 for (i = 0; i < XVECLEN (p2, 0); i++)
2722 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2723 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2724 break;
2726 if (i == XVECLEN (p2, 0))
2727 for (i = 0; i < XVECLEN (p2, 0); i++)
2728 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2729 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2731 combine_merges++;
2733 subst_insn = i3;
2734 subst_low_luid = DF_INSN_LUID (i2);
2736 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2737 i2src = SET_SRC (XVECEXP (p2, 0, i));
2738 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2739 i2dest_killed = dead_or_set_p (i2, i2dest);
2741 /* Replace the dest in I2 with our dest and make the resulting
2742 insn the new pattern for I3. Then skip to where we validate
2743 the pattern. Everything was set up above. */
2744 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2745 newpat = p2;
2746 i3_subst_into_i2 = 1;
2747 goto validate_replacement;
2751 /* If I2 is setting a pseudo to a constant and I3 is setting some
2752 sub-part of it to another constant, merge them by making a new
2753 constant. */
2754 if (i1 == 0
2755 && (temp_expr = single_set (i2)) != 0
2756 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2757 && GET_CODE (PATTERN (i3)) == SET
2758 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2759 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2761 rtx dest = SET_DEST (PATTERN (i3));
2762 int offset = -1;
2763 int width = 0;
2765 if (GET_CODE (dest) == ZERO_EXTRACT)
2767 if (CONST_INT_P (XEXP (dest, 1))
2768 && CONST_INT_P (XEXP (dest, 2)))
2770 width = INTVAL (XEXP (dest, 1));
2771 offset = INTVAL (XEXP (dest, 2));
2772 dest = XEXP (dest, 0);
2773 if (BITS_BIG_ENDIAN)
2774 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2777 else
2779 if (GET_CODE (dest) == STRICT_LOW_PART)
2780 dest = XEXP (dest, 0);
2781 width = GET_MODE_PRECISION (GET_MODE (dest));
2782 offset = 0;
2785 if (offset >= 0)
2787 /* If this is the low part, we're done. */
2788 if (subreg_lowpart_p (dest))
2790 /* Handle the case where inner is twice the size of outer. */
2791 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2792 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2793 offset += GET_MODE_PRECISION (GET_MODE (dest));
2794 /* Otherwise give up for now. */
2795 else
2796 offset = -1;
2799 if (offset >= 0)
2801 rtx inner = SET_SRC (PATTERN (i3));
2802 rtx outer = SET_SRC (temp_expr);
2804 wide_int o
2805 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2806 std::make_pair (inner, GET_MODE (dest)),
2807 offset, width);
2809 combine_merges++;
2810 subst_insn = i3;
2811 subst_low_luid = DF_INSN_LUID (i2);
2812 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2813 i2dest = SET_DEST (temp_expr);
2814 i2dest_killed = dead_or_set_p (i2, i2dest);
2816 /* Replace the source in I2 with the new constant and make the
2817 resulting insn the new pattern for I3. Then skip to where we
2818 validate the pattern. Everything was set up above. */
2819 SUBST (SET_SRC (temp_expr),
2820 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2822 newpat = PATTERN (i2);
2824 /* The dest of I3 has been replaced with the dest of I2. */
2825 changed_i3_dest = 1;
2826 goto validate_replacement;
2830 #ifndef HAVE_cc0
2831 /* If we have no I1 and I2 looks like:
2832 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2833 (set Y OP)])
2834 make up a dummy I1 that is
2835 (set Y OP)
2836 and change I2 to be
2837 (set (reg:CC X) (compare:CC Y (const_int 0)))
2839 (We can ignore any trailing CLOBBERs.)
2841 This undoes a previous combination and allows us to match a branch-and-
2842 decrement insn. */
2844 if (i1 == 0
2845 && is_parallel_of_n_reg_sets (i2, 2)
2846 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2847 == MODE_CC)
2848 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2849 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2850 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2851 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2852 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2853 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2855 /* We make I1 with the same INSN_UID as I2. This gives it
2856 the same DF_INSN_LUID for value tracking. Our fake I1 will
2857 never appear in the insn stream so giving it the same INSN_UID
2858 as I2 will not cause a problem. */
2860 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2861 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2862 -1, NULL_RTX);
2863 INSN_UID (i1) = INSN_UID (i2);
2865 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2866 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2867 SET_DEST (PATTERN (i1)));
2868 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2869 SUBST_LINK (LOG_LINKS (i2),
2870 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2873 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2874 make those two SETs separate I1 and I2 insns, and make an I0 that is
2875 the original I1. */
2876 if (i0 == 0
2877 && is_parallel_of_n_reg_sets (i2, 2)
2878 && can_split_parallel_of_n_reg_sets (i2, 2)
2879 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2880 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2882 /* If there is no I1, there is no I0 either. */
2883 i0 = i1;
2885 /* We make I1 with the same INSN_UID as I2. This gives it
2886 the same DF_INSN_LUID for value tracking. Our fake I1 will
2887 never appear in the insn stream so giving it the same INSN_UID
2888 as I2 will not cause a problem. */
2890 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2891 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2892 -1, NULL_RTX);
2893 INSN_UID (i1) = INSN_UID (i2);
2895 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2897 #endif
2899 /* Verify that I2 and I1 are valid for combining. */
2900 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2901 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2902 &i1dest, &i1src))
2903 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2904 &i0dest, &i0src)))
2906 undo_all ();
2907 return 0;
2910 /* Record whether I2DEST is used in I2SRC and similarly for the other
2911 cases. Knowing this will help in register status updating below. */
2912 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2913 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2914 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2915 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2916 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2917 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2918 i2dest_killed = dead_or_set_p (i2, i2dest);
2919 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2920 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2922 /* For the earlier insns, determine which of the subsequent ones they
2923 feed. */
2924 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2925 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2926 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2927 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2928 && reg_overlap_mentioned_p (i0dest, i2src))));
2930 /* Ensure that I3's pattern can be the destination of combines. */
2931 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2932 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2933 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2934 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2935 &i3dest_killed))
2937 undo_all ();
2938 return 0;
2941 /* See if any of the insns is a MULT operation. Unless one is, we will
2942 reject a combination that is, since it must be slower. Be conservative
2943 here. */
2944 if (GET_CODE (i2src) == MULT
2945 || (i1 != 0 && GET_CODE (i1src) == MULT)
2946 || (i0 != 0 && GET_CODE (i0src) == MULT)
2947 || (GET_CODE (PATTERN (i3)) == SET
2948 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2949 have_mult = 1;
2951 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2952 We used to do this EXCEPT in one case: I3 has a post-inc in an
2953 output operand. However, that exception can give rise to insns like
2954 mov r3,(r3)+
2955 which is a famous insn on the PDP-11 where the value of r3 used as the
2956 source was model-dependent. Avoid this sort of thing. */
2958 #if 0
2959 if (!(GET_CODE (PATTERN (i3)) == SET
2960 && REG_P (SET_SRC (PATTERN (i3)))
2961 && MEM_P (SET_DEST (PATTERN (i3)))
2962 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2963 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2964 /* It's not the exception. */
2965 #endif
2966 #ifdef AUTO_INC_DEC
2968 rtx link;
2969 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2970 if (REG_NOTE_KIND (link) == REG_INC
2971 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2972 || (i1 != 0
2973 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2975 undo_all ();
2976 return 0;
2979 #endif
2981 /* See if the SETs in I1 or I2 need to be kept around in the merged
2982 instruction: whenever the value set there is still needed past I3.
2983 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2985 For the SET in I1, we have two cases: if I1 and I2 independently feed
2986 into I3, the set in I1 needs to be kept around unless I1DEST dies
2987 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2988 in I1 needs to be kept around unless I1DEST dies or is set in either
2989 I2 or I3. The same considerations apply to I0. */
2991 added_sets_2 = !dead_or_set_p (i3, i2dest);
2993 if (i1)
2994 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2995 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2996 else
2997 added_sets_1 = 0;
2999 if (i0)
3000 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3001 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3002 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3003 && dead_or_set_p (i2, i0dest)));
3004 else
3005 added_sets_0 = 0;
3007 /* We are about to copy insns for the case where they need to be kept
3008 around. Check that they can be copied in the merged instruction. */
3010 if (targetm.cannot_copy_insn_p
3011 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3012 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3013 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3015 undo_all ();
3016 return 0;
3019 /* If the set in I2 needs to be kept around, we must make a copy of
3020 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3021 PATTERN (I2), we are only substituting for the original I1DEST, not into
3022 an already-substituted copy. This also prevents making self-referential
3023 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3024 I2DEST. */
3026 if (added_sets_2)
3028 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3029 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3030 else
3031 i2pat = copy_rtx (PATTERN (i2));
3034 if (added_sets_1)
3036 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3037 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3038 else
3039 i1pat = copy_rtx (PATTERN (i1));
3042 if (added_sets_0)
3044 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3045 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3046 else
3047 i0pat = copy_rtx (PATTERN (i0));
3050 combine_merges++;
3052 /* Substitute in the latest insn for the regs set by the earlier ones. */
3054 maxreg = max_reg_num ();
3056 subst_insn = i3;
3058 #ifndef HAVE_cc0
3059 /* Many machines that don't use CC0 have insns that can both perform an
3060 arithmetic operation and set the condition code. These operations will
3061 be represented as a PARALLEL with the first element of the vector
3062 being a COMPARE of an arithmetic operation with the constant zero.
3063 The second element of the vector will set some pseudo to the result
3064 of the same arithmetic operation. If we simplify the COMPARE, we won't
3065 match such a pattern and so will generate an extra insn. Here we test
3066 for this case, where both the comparison and the operation result are
3067 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3068 I2SRC. Later we will make the PARALLEL that contains I2. */
3070 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3071 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3072 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3073 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3075 rtx newpat_dest;
3076 rtx *cc_use_loc = NULL;
3077 rtx_insn *cc_use_insn = NULL;
3078 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3079 machine_mode compare_mode, orig_compare_mode;
3080 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3082 newpat = PATTERN (i3);
3083 newpat_dest = SET_DEST (newpat);
3084 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3086 if (undobuf.other_insn == 0
3087 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3088 &cc_use_insn)))
3090 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3091 compare_code = simplify_compare_const (compare_code,
3092 GET_MODE (i2dest), op0, &op1);
3093 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3096 /* Do the rest only if op1 is const0_rtx, which may be the
3097 result of simplification. */
3098 if (op1 == const0_rtx)
3100 /* If a single use of the CC is found, prepare to modify it
3101 when SELECT_CC_MODE returns a new CC-class mode, or when
3102 the above simplify_compare_const() returned a new comparison
3103 operator. undobuf.other_insn is assigned the CC use insn
3104 when modifying it. */
3105 if (cc_use_loc)
3107 #ifdef SELECT_CC_MODE
3108 machine_mode new_mode
3109 = SELECT_CC_MODE (compare_code, op0, op1);
3110 if (new_mode != orig_compare_mode
3111 && can_change_dest_mode (SET_DEST (newpat),
3112 added_sets_2, new_mode))
3114 unsigned int regno = REGNO (newpat_dest);
3115 compare_mode = new_mode;
3116 if (regno < FIRST_PSEUDO_REGISTER)
3117 newpat_dest = gen_rtx_REG (compare_mode, regno);
3118 else
3120 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3121 newpat_dest = regno_reg_rtx[regno];
3124 #endif
3125 /* Cases for modifying the CC-using comparison. */
3126 if (compare_code != orig_compare_code
3127 /* ??? Do we need to verify the zero rtx? */
3128 && XEXP (*cc_use_loc, 1) == const0_rtx)
3130 /* Replace cc_use_loc with entire new RTX. */
3131 SUBST (*cc_use_loc,
3132 gen_rtx_fmt_ee (compare_code, compare_mode,
3133 newpat_dest, const0_rtx));
3134 undobuf.other_insn = cc_use_insn;
3136 else if (compare_mode != orig_compare_mode)
3138 /* Just replace the CC reg with a new mode. */
3139 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3140 undobuf.other_insn = cc_use_insn;
3144 /* Now we modify the current newpat:
3145 First, SET_DEST(newpat) is updated if the CC mode has been
3146 altered. For targets without SELECT_CC_MODE, this should be
3147 optimized away. */
3148 if (compare_mode != orig_compare_mode)
3149 SUBST (SET_DEST (newpat), newpat_dest);
3150 /* This is always done to propagate i2src into newpat. */
3151 SUBST (SET_SRC (newpat),
3152 gen_rtx_COMPARE (compare_mode, op0, op1));
3153 /* Create new version of i2pat if needed; the below PARALLEL
3154 creation needs this to work correctly. */
3155 if (! rtx_equal_p (i2src, op0))
3156 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3157 i2_is_used = 1;
3160 #endif
3162 if (i2_is_used == 0)
3164 /* It is possible that the source of I2 or I1 may be performing
3165 an unneeded operation, such as a ZERO_EXTEND of something
3166 that is known to have the high part zero. Handle that case
3167 by letting subst look at the inner insns.
3169 Another way to do this would be to have a function that tries
3170 to simplify a single insn instead of merging two or more
3171 insns. We don't do this because of the potential of infinite
3172 loops and because of the potential extra memory required.
3173 However, doing it the way we are is a bit of a kludge and
3174 doesn't catch all cases.
3176 But only do this if -fexpensive-optimizations since it slows
3177 things down and doesn't usually win.
3179 This is not done in the COMPARE case above because the
3180 unmodified I2PAT is used in the PARALLEL and so a pattern
3181 with a modified I2SRC would not match. */
3183 if (flag_expensive_optimizations)
3185 /* Pass pc_rtx so no substitutions are done, just
3186 simplifications. */
3187 if (i1)
3189 subst_low_luid = DF_INSN_LUID (i1);
3190 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3193 subst_low_luid = DF_INSN_LUID (i2);
3194 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3197 n_occurrences = 0; /* `subst' counts here */
3198 subst_low_luid = DF_INSN_LUID (i2);
3200 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3201 copy of I2SRC each time we substitute it, in order to avoid creating
3202 self-referential RTL when we will be substituting I1SRC for I1DEST
3203 later. Likewise if I0 feeds into I2, either directly or indirectly
3204 through I1, and I0DEST is in I0SRC. */
3205 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3206 (i1_feeds_i2_n && i1dest_in_i1src)
3207 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3208 && i0dest_in_i0src));
3209 substed_i2 = 1;
3211 /* Record whether I2's body now appears within I3's body. */
3212 i2_is_used = n_occurrences;
3215 /* If we already got a failure, don't try to do more. Otherwise, try to
3216 substitute I1 if we have it. */
3218 if (i1 && GET_CODE (newpat) != CLOBBER)
3220 /* Check that an autoincrement side-effect on I1 has not been lost.
3221 This happens if I1DEST is mentioned in I2 and dies there, and
3222 has disappeared from the new pattern. */
3223 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3224 && i1_feeds_i2_n
3225 && dead_or_set_p (i2, i1dest)
3226 && !reg_overlap_mentioned_p (i1dest, newpat))
3227 /* Before we can do this substitution, we must redo the test done
3228 above (see detailed comments there) that ensures I1DEST isn't
3229 mentioned in any SETs in NEWPAT that are field assignments. */
3230 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3231 0, 0, 0))
3233 undo_all ();
3234 return 0;
3237 n_occurrences = 0;
3238 subst_low_luid = DF_INSN_LUID (i1);
3240 /* If the following substitution will modify I1SRC, make a copy of it
3241 for the case where it is substituted for I1DEST in I2PAT later. */
3242 if (added_sets_2 && i1_feeds_i2_n)
3243 i1src_copy = copy_rtx (i1src);
3245 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3246 copy of I1SRC each time we substitute it, in order to avoid creating
3247 self-referential RTL when we will be substituting I0SRC for I0DEST
3248 later. */
3249 newpat = subst (newpat, i1dest, i1src, 0, 0,
3250 i0_feeds_i1_n && i0dest_in_i0src);
3251 substed_i1 = 1;
3253 /* Record whether I1's body now appears within I3's body. */
3254 i1_is_used = n_occurrences;
3257 /* Likewise for I0 if we have it. */
3259 if (i0 && GET_CODE (newpat) != CLOBBER)
3261 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3262 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3263 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3264 && !reg_overlap_mentioned_p (i0dest, newpat))
3265 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3266 0, 0, 0))
3268 undo_all ();
3269 return 0;
3272 /* If the following substitution will modify I0SRC, make a copy of it
3273 for the case where it is substituted for I0DEST in I1PAT later. */
3274 if (added_sets_1 && i0_feeds_i1_n)
3275 i0src_copy = copy_rtx (i0src);
3276 /* And a copy for I0DEST in I2PAT substitution. */
3277 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3278 || (i0_feeds_i2_n)))
3279 i0src_copy2 = copy_rtx (i0src);
3281 n_occurrences = 0;
3282 subst_low_luid = DF_INSN_LUID (i0);
3283 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3284 substed_i0 = 1;
3287 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3288 to count all the ways that I2SRC and I1SRC can be used. */
3289 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3290 && i2_is_used + added_sets_2 > 1)
3291 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3292 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3293 > 1))
3294 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3295 && (n_occurrences + added_sets_0
3296 + (added_sets_1 && i0_feeds_i1_n)
3297 + (added_sets_2 && i0_feeds_i2_n)
3298 > 1))
3299 /* Fail if we tried to make a new register. */
3300 || max_reg_num () != maxreg
3301 /* Fail if we couldn't do something and have a CLOBBER. */
3302 || GET_CODE (newpat) == CLOBBER
3303 /* Fail if this new pattern is a MULT and we didn't have one before
3304 at the outer level. */
3305 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3306 && ! have_mult))
3308 undo_all ();
3309 return 0;
3312 /* If the actions of the earlier insns must be kept
3313 in addition to substituting them into the latest one,
3314 we must make a new PARALLEL for the latest insn
3315 to hold additional the SETs. */
3317 if (added_sets_0 || added_sets_1 || added_sets_2)
3319 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3320 combine_extras++;
3322 if (GET_CODE (newpat) == PARALLEL)
3324 rtvec old = XVEC (newpat, 0);
3325 total_sets = XVECLEN (newpat, 0) + extra_sets;
3326 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3327 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3328 sizeof (old->elem[0]) * old->num_elem);
3330 else
3332 rtx old = newpat;
3333 total_sets = 1 + extra_sets;
3334 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3335 XVECEXP (newpat, 0, 0) = old;
3338 if (added_sets_0)
3339 XVECEXP (newpat, 0, --total_sets) = i0pat;
3341 if (added_sets_1)
3343 rtx t = i1pat;
3344 if (i0_feeds_i1_n)
3345 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3347 XVECEXP (newpat, 0, --total_sets) = t;
3349 if (added_sets_2)
3351 rtx t = i2pat;
3352 if (i1_feeds_i2_n)
3353 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3354 i0_feeds_i1_n && i0dest_in_i0src);
3355 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3356 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3358 XVECEXP (newpat, 0, --total_sets) = t;
3362 validate_replacement:
3364 /* Note which hard regs this insn has as inputs. */
3365 mark_used_regs_combine (newpat);
3367 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3368 consider splitting this pattern, we might need these clobbers. */
3369 if (i1 && GET_CODE (newpat) == PARALLEL
3370 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3372 int len = XVECLEN (newpat, 0);
3374 newpat_vec_with_clobbers = rtvec_alloc (len);
3375 for (i = 0; i < len; i++)
3376 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3379 /* We have recognized nothing yet. */
3380 insn_code_number = -1;
3382 /* See if this is a PARALLEL of two SETs where one SET's destination is
3383 a register that is unused and this isn't marked as an instruction that
3384 might trap in an EH region. In that case, we just need the other SET.
3385 We prefer this over the PARALLEL.
3387 This can occur when simplifying a divmod insn. We *must* test for this
3388 case here because the code below that splits two independent SETs doesn't
3389 handle this case correctly when it updates the register status.
3391 It's pointless doing this if we originally had two sets, one from
3392 i3, and one from i2. Combining then splitting the parallel results
3393 in the original i2 again plus an invalid insn (which we delete).
3394 The net effect is only to move instructions around, which makes
3395 debug info less accurate. */
3397 if (!(added_sets_2 && i1 == 0)
3398 && GET_CODE (newpat) == PARALLEL
3399 && XVECLEN (newpat, 0) == 2
3400 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3401 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3402 && asm_noperands (newpat) < 0)
3404 rtx set0 = XVECEXP (newpat, 0, 0);
3405 rtx set1 = XVECEXP (newpat, 0, 1);
3406 rtx oldpat = newpat;
3408 if (((REG_P (SET_DEST (set1))
3409 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3410 || (GET_CODE (SET_DEST (set1)) == SUBREG
3411 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3412 && insn_nothrow_p (i3)
3413 && !side_effects_p (SET_SRC (set1)))
3415 newpat = set0;
3416 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3419 else if (((REG_P (SET_DEST (set0))
3420 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3421 || (GET_CODE (SET_DEST (set0)) == SUBREG
3422 && find_reg_note (i3, REG_UNUSED,
3423 SUBREG_REG (SET_DEST (set0)))))
3424 && insn_nothrow_p (i3)
3425 && !side_effects_p (SET_SRC (set0)))
3427 newpat = set1;
3428 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3430 if (insn_code_number >= 0)
3431 changed_i3_dest = 1;
3434 if (insn_code_number < 0)
3435 newpat = oldpat;
3438 /* Is the result of combination a valid instruction? */
3439 if (insn_code_number < 0)
3440 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3442 /* If we were combining three insns and the result is a simple SET
3443 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3444 insns. There are two ways to do this. It can be split using a
3445 machine-specific method (like when you have an addition of a large
3446 constant) or by combine in the function find_split_point. */
3448 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3449 && asm_noperands (newpat) < 0)
3451 rtx parallel, *split;
3452 rtx_insn *m_split_insn;
3454 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3455 use I2DEST as a scratch register will help. In the latter case,
3456 convert I2DEST to the mode of the source of NEWPAT if we can. */
3458 m_split_insn = combine_split_insns (newpat, i3);
3460 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3461 inputs of NEWPAT. */
3463 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3464 possible to try that as a scratch reg. This would require adding
3465 more code to make it work though. */
3467 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3469 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3471 /* First try to split using the original register as a
3472 scratch register. */
3473 parallel = gen_rtx_PARALLEL (VOIDmode,
3474 gen_rtvec (2, newpat,
3475 gen_rtx_CLOBBER (VOIDmode,
3476 i2dest)));
3477 m_split_insn = combine_split_insns (parallel, i3);
3479 /* If that didn't work, try changing the mode of I2DEST if
3480 we can. */
3481 if (m_split_insn == 0
3482 && new_mode != GET_MODE (i2dest)
3483 && new_mode != VOIDmode
3484 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3486 machine_mode old_mode = GET_MODE (i2dest);
3487 rtx ni2dest;
3489 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3490 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3491 else
3493 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3494 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3497 parallel = (gen_rtx_PARALLEL
3498 (VOIDmode,
3499 gen_rtvec (2, newpat,
3500 gen_rtx_CLOBBER (VOIDmode,
3501 ni2dest))));
3502 m_split_insn = combine_split_insns (parallel, i3);
3504 if (m_split_insn == 0
3505 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3507 struct undo *buf;
3509 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3510 buf = undobuf.undos;
3511 undobuf.undos = buf->next;
3512 buf->next = undobuf.frees;
3513 undobuf.frees = buf;
3517 i2scratch = m_split_insn != 0;
3520 /* If recog_for_combine has discarded clobbers, try to use them
3521 again for the split. */
3522 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3524 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3525 m_split_insn = combine_split_insns (parallel, i3);
3528 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3530 rtx m_split_pat = PATTERN (m_split_insn);
3531 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3532 if (insn_code_number >= 0)
3533 newpat = m_split_pat;
3535 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3536 && (next_nonnote_nondebug_insn (i2) == i3
3537 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3539 rtx i2set, i3set;
3540 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3541 newi2pat = PATTERN (m_split_insn);
3543 i3set = single_set (NEXT_INSN (m_split_insn));
3544 i2set = single_set (m_split_insn);
3546 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3548 /* If I2 or I3 has multiple SETs, we won't know how to track
3549 register status, so don't use these insns. If I2's destination
3550 is used between I2 and I3, we also can't use these insns. */
3552 if (i2_code_number >= 0 && i2set && i3set
3553 && (next_nonnote_nondebug_insn (i2) == i3
3554 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3555 insn_code_number = recog_for_combine (&newi3pat, i3,
3556 &new_i3_notes);
3557 if (insn_code_number >= 0)
3558 newpat = newi3pat;
3560 /* It is possible that both insns now set the destination of I3.
3561 If so, we must show an extra use of it. */
3563 if (insn_code_number >= 0)
3565 rtx new_i3_dest = SET_DEST (i3set);
3566 rtx new_i2_dest = SET_DEST (i2set);
3568 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3569 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3570 || GET_CODE (new_i3_dest) == SUBREG)
3571 new_i3_dest = XEXP (new_i3_dest, 0);
3573 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3574 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3575 || GET_CODE (new_i2_dest) == SUBREG)
3576 new_i2_dest = XEXP (new_i2_dest, 0);
3578 if (REG_P (new_i3_dest)
3579 && REG_P (new_i2_dest)
3580 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3581 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3585 /* If we can split it and use I2DEST, go ahead and see if that
3586 helps things be recognized. Verify that none of the registers
3587 are set between I2 and I3. */
3588 if (insn_code_number < 0
3589 && (split = find_split_point (&newpat, i3, false)) != 0
3590 #ifdef HAVE_cc0
3591 && REG_P (i2dest)
3592 #endif
3593 /* We need I2DEST in the proper mode. If it is a hard register
3594 or the only use of a pseudo, we can change its mode.
3595 Make sure we don't change a hard register to have a mode that
3596 isn't valid for it, or change the number of registers. */
3597 && (GET_MODE (*split) == GET_MODE (i2dest)
3598 || GET_MODE (*split) == VOIDmode
3599 || can_change_dest_mode (i2dest, added_sets_2,
3600 GET_MODE (*split)))
3601 && (next_nonnote_nondebug_insn (i2) == i3
3602 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3603 /* We can't overwrite I2DEST if its value is still used by
3604 NEWPAT. */
3605 && ! reg_referenced_p (i2dest, newpat))
3607 rtx newdest = i2dest;
3608 enum rtx_code split_code = GET_CODE (*split);
3609 machine_mode split_mode = GET_MODE (*split);
3610 bool subst_done = false;
3611 newi2pat = NULL_RTX;
3613 i2scratch = true;
3615 /* *SPLIT may be part of I2SRC, so make sure we have the
3616 original expression around for later debug processing.
3617 We should not need I2SRC any more in other cases. */
3618 if (MAY_HAVE_DEBUG_INSNS)
3619 i2src = copy_rtx (i2src);
3620 else
3621 i2src = NULL;
3623 /* Get NEWDEST as a register in the proper mode. We have already
3624 validated that we can do this. */
3625 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3627 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3628 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3629 else
3631 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3632 newdest = regno_reg_rtx[REGNO (i2dest)];
3636 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3637 an ASHIFT. This can occur if it was inside a PLUS and hence
3638 appeared to be a memory address. This is a kludge. */
3639 if (split_code == MULT
3640 && CONST_INT_P (XEXP (*split, 1))
3641 && INTVAL (XEXP (*split, 1)) > 0
3642 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3644 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3645 XEXP (*split, 0), GEN_INT (i)));
3646 /* Update split_code because we may not have a multiply
3647 anymore. */
3648 split_code = GET_CODE (*split);
3651 #ifdef INSN_SCHEDULING
3652 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3653 be written as a ZERO_EXTEND. */
3654 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3656 #ifdef LOAD_EXTEND_OP
3657 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3658 what it really is. */
3659 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3660 == SIGN_EXTEND)
3661 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3662 SUBREG_REG (*split)));
3663 else
3664 #endif
3665 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3666 SUBREG_REG (*split)));
3668 #endif
3670 /* Attempt to split binary operators using arithmetic identities. */
3671 if (BINARY_P (SET_SRC (newpat))
3672 && split_mode == GET_MODE (SET_SRC (newpat))
3673 && ! side_effects_p (SET_SRC (newpat)))
3675 rtx setsrc = SET_SRC (newpat);
3676 machine_mode mode = GET_MODE (setsrc);
3677 enum rtx_code code = GET_CODE (setsrc);
3678 rtx src_op0 = XEXP (setsrc, 0);
3679 rtx src_op1 = XEXP (setsrc, 1);
3681 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3682 if (rtx_equal_p (src_op0, src_op1))
3684 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3685 SUBST (XEXP (setsrc, 0), newdest);
3686 SUBST (XEXP (setsrc, 1), newdest);
3687 subst_done = true;
3689 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3690 else if ((code == PLUS || code == MULT)
3691 && GET_CODE (src_op0) == code
3692 && GET_CODE (XEXP (src_op0, 0)) == code
3693 && (INTEGRAL_MODE_P (mode)
3694 || (FLOAT_MODE_P (mode)
3695 && flag_unsafe_math_optimizations)))
3697 rtx p = XEXP (XEXP (src_op0, 0), 0);
3698 rtx q = XEXP (XEXP (src_op0, 0), 1);
3699 rtx r = XEXP (src_op0, 1);
3700 rtx s = src_op1;
3702 /* Split both "((X op Y) op X) op Y" and
3703 "((X op Y) op Y) op X" as "T op T" where T is
3704 "X op Y". */
3705 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3706 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3708 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3709 XEXP (src_op0, 0));
3710 SUBST (XEXP (setsrc, 0), newdest);
3711 SUBST (XEXP (setsrc, 1), newdest);
3712 subst_done = true;
3714 /* Split "((X op X) op Y) op Y)" as "T op T" where
3715 T is "X op Y". */
3716 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3718 rtx tmp = simplify_gen_binary (code, mode, p, r);
3719 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3720 SUBST (XEXP (setsrc, 0), newdest);
3721 SUBST (XEXP (setsrc, 1), newdest);
3722 subst_done = true;
3727 if (!subst_done)
3729 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3730 SUBST (*split, newdest);
3733 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3735 /* recog_for_combine might have added CLOBBERs to newi2pat.
3736 Make sure NEWPAT does not depend on the clobbered regs. */
3737 if (GET_CODE (newi2pat) == PARALLEL)
3738 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3739 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3741 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3742 if (reg_overlap_mentioned_p (reg, newpat))
3744 undo_all ();
3745 return 0;
3749 /* If the split point was a MULT and we didn't have one before,
3750 don't use one now. */
3751 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3752 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3756 /* Check for a case where we loaded from memory in a narrow mode and
3757 then sign extended it, but we need both registers. In that case,
3758 we have a PARALLEL with both loads from the same memory location.
3759 We can split this into a load from memory followed by a register-register
3760 copy. This saves at least one insn, more if register allocation can
3761 eliminate the copy.
3763 We cannot do this if the destination of the first assignment is a
3764 condition code register or cc0. We eliminate this case by making sure
3765 the SET_DEST and SET_SRC have the same mode.
3767 We cannot do this if the destination of the second assignment is
3768 a register that we have already assumed is zero-extended. Similarly
3769 for a SUBREG of such a register. */
3771 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3772 && GET_CODE (newpat) == PARALLEL
3773 && XVECLEN (newpat, 0) == 2
3774 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3775 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3776 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3777 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3778 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3779 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3780 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3781 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3782 DF_INSN_LUID (i2))
3783 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3784 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3785 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3786 (REG_P (temp_expr)
3787 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3788 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3789 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3790 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3791 != GET_MODE_MASK (word_mode))))
3792 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3793 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3794 (REG_P (temp_expr)
3795 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3796 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3797 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3798 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3799 != GET_MODE_MASK (word_mode)))))
3800 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3801 SET_SRC (XVECEXP (newpat, 0, 1)))
3802 && ! find_reg_note (i3, REG_UNUSED,
3803 SET_DEST (XVECEXP (newpat, 0, 0))))
3805 rtx ni2dest;
3807 newi2pat = XVECEXP (newpat, 0, 0);
3808 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3809 newpat = XVECEXP (newpat, 0, 1);
3810 SUBST (SET_SRC (newpat),
3811 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3812 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3814 if (i2_code_number >= 0)
3815 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3817 if (insn_code_number >= 0)
3818 swap_i2i3 = 1;
3821 /* Similarly, check for a case where we have a PARALLEL of two independent
3822 SETs but we started with three insns. In this case, we can do the sets
3823 as two separate insns. This case occurs when some SET allows two
3824 other insns to combine, but the destination of that SET is still live.
3826 Also do this if we started with two insns and (at least) one of the
3827 resulting sets is a noop; this noop will be deleted later. */
3829 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3830 && GET_CODE (newpat) == PARALLEL
3831 && XVECLEN (newpat, 0) == 2
3832 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3833 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3834 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3835 || set_noop_p (XVECEXP (newpat, 0, 1)))
3836 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3837 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3838 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3839 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3840 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3841 XVECEXP (newpat, 0, 0))
3842 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3843 XVECEXP (newpat, 0, 1))
3844 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3845 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3847 rtx set0 = XVECEXP (newpat, 0, 0);
3848 rtx set1 = XVECEXP (newpat, 0, 1);
3850 /* Normally, it doesn't matter which of the two is done first,
3851 but the one that references cc0 can't be the second, and
3852 one which uses any regs/memory set in between i2 and i3 can't
3853 be first. The PARALLEL might also have been pre-existing in i3,
3854 so we need to make sure that we won't wrongly hoist a SET to i2
3855 that would conflict with a death note present in there. */
3856 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3857 && !(REG_P (SET_DEST (set1))
3858 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3859 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3860 && find_reg_note (i2, REG_DEAD,
3861 SUBREG_REG (SET_DEST (set1))))
3862 #ifdef HAVE_cc0
3863 && !reg_referenced_p (cc0_rtx, set0)
3864 #endif
3865 /* If I3 is a jump, ensure that set0 is a jump so that
3866 we do not create invalid RTL. */
3867 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3870 newi2pat = set1;
3871 newpat = set0;
3873 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3874 && !(REG_P (SET_DEST (set0))
3875 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3876 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3877 && find_reg_note (i2, REG_DEAD,
3878 SUBREG_REG (SET_DEST (set0))))
3879 #ifdef HAVE_cc0
3880 && !reg_referenced_p (cc0_rtx, set1)
3881 #endif
3882 /* If I3 is a jump, ensure that set1 is a jump so that
3883 we do not create invalid RTL. */
3884 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3887 newi2pat = set0;
3888 newpat = set1;
3890 else
3892 undo_all ();
3893 return 0;
3896 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3898 if (i2_code_number >= 0)
3900 /* recog_for_combine might have added CLOBBERs to newi2pat.
3901 Make sure NEWPAT does not depend on the clobbered regs. */
3902 if (GET_CODE (newi2pat) == PARALLEL)
3904 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3905 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3907 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3908 if (reg_overlap_mentioned_p (reg, newpat))
3910 undo_all ();
3911 return 0;
3916 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3920 /* If it still isn't recognized, fail and change things back the way they
3921 were. */
3922 if ((insn_code_number < 0
3923 /* Is the result a reasonable ASM_OPERANDS? */
3924 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3926 undo_all ();
3927 return 0;
3930 /* If we had to change another insn, make sure it is valid also. */
3931 if (undobuf.other_insn)
3933 CLEAR_HARD_REG_SET (newpat_used_regs);
3935 other_pat = PATTERN (undobuf.other_insn);
3936 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3937 &new_other_notes);
3939 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3941 undo_all ();
3942 return 0;
3946 #ifdef HAVE_cc0
3947 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3948 they are adjacent to each other or not. */
3950 rtx_insn *p = prev_nonnote_insn (i3);
3951 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3952 && sets_cc0_p (newi2pat))
3954 undo_all ();
3955 return 0;
3958 #endif
3960 /* Only allow this combination if insn_rtx_costs reports that the
3961 replacement instructions are cheaper than the originals. */
3962 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3964 undo_all ();
3965 return 0;
3968 if (MAY_HAVE_DEBUG_INSNS)
3970 struct undo *undo;
3972 for (undo = undobuf.undos; undo; undo = undo->next)
3973 if (undo->kind == UNDO_MODE)
3975 rtx reg = *undo->where.r;
3976 machine_mode new_mode = GET_MODE (reg);
3977 machine_mode old_mode = undo->old_contents.m;
3979 /* Temporarily revert mode back. */
3980 adjust_reg_mode (reg, old_mode);
3982 if (reg == i2dest && i2scratch)
3984 /* If we used i2dest as a scratch register with a
3985 different mode, substitute it for the original
3986 i2src while its original mode is temporarily
3987 restored, and then clear i2scratch so that we don't
3988 do it again later. */
3989 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3990 this_basic_block);
3991 i2scratch = false;
3992 /* Put back the new mode. */
3993 adjust_reg_mode (reg, new_mode);
3995 else
3997 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3998 rtx_insn *first, *last;
4000 if (reg == i2dest)
4002 first = i2;
4003 last = last_combined_insn;
4005 else
4007 first = i3;
4008 last = undobuf.other_insn;
4009 gcc_assert (last);
4010 if (DF_INSN_LUID (last)
4011 < DF_INSN_LUID (last_combined_insn))
4012 last = last_combined_insn;
4015 /* We're dealing with a reg that changed mode but not
4016 meaning, so we want to turn it into a subreg for
4017 the new mode. However, because of REG sharing and
4018 because its mode had already changed, we have to do
4019 it in two steps. First, replace any debug uses of
4020 reg, with its original mode temporarily restored,
4021 with this copy we have created; then, replace the
4022 copy with the SUBREG of the original shared reg,
4023 once again changed to the new mode. */
4024 propagate_for_debug (first, last, reg, tempreg,
4025 this_basic_block);
4026 adjust_reg_mode (reg, new_mode);
4027 propagate_for_debug (first, last, tempreg,
4028 lowpart_subreg (old_mode, reg, new_mode),
4029 this_basic_block);
4034 /* If we will be able to accept this, we have made a
4035 change to the destination of I3. This requires us to
4036 do a few adjustments. */
4038 if (changed_i3_dest)
4040 PATTERN (i3) = newpat;
4041 adjust_for_new_dest (i3);
4044 /* We now know that we can do this combination. Merge the insns and
4045 update the status of registers and LOG_LINKS. */
4047 if (undobuf.other_insn)
4049 rtx note, next;
4051 PATTERN (undobuf.other_insn) = other_pat;
4053 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4054 ensure that they are still valid. Then add any non-duplicate
4055 notes added by recog_for_combine. */
4056 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4058 next = XEXP (note, 1);
4060 if ((REG_NOTE_KIND (note) == REG_DEAD
4061 && !reg_referenced_p (XEXP (note, 0),
4062 PATTERN (undobuf.other_insn)))
4063 ||(REG_NOTE_KIND (note) == REG_UNUSED
4064 && !reg_set_p (XEXP (note, 0),
4065 PATTERN (undobuf.other_insn))))
4066 remove_note (undobuf.other_insn, note);
4069 distribute_notes (new_other_notes, undobuf.other_insn,
4070 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4071 NULL_RTX);
4074 if (swap_i2i3)
4076 rtx_insn *insn;
4077 struct insn_link *link;
4078 rtx ni2dest;
4080 /* I3 now uses what used to be its destination and which is now
4081 I2's destination. This requires us to do a few adjustments. */
4082 PATTERN (i3) = newpat;
4083 adjust_for_new_dest (i3);
4085 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4086 so we still will.
4088 However, some later insn might be using I2's dest and have
4089 a LOG_LINK pointing at I3. We must remove this link.
4090 The simplest way to remove the link is to point it at I1,
4091 which we know will be a NOTE. */
4093 /* newi2pat is usually a SET here; however, recog_for_combine might
4094 have added some clobbers. */
4095 if (GET_CODE (newi2pat) == PARALLEL)
4096 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4097 else
4098 ni2dest = SET_DEST (newi2pat);
4100 for (insn = NEXT_INSN (i3);
4101 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4102 || insn != BB_HEAD (this_basic_block->next_bb));
4103 insn = NEXT_INSN (insn))
4105 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4107 FOR_EACH_LOG_LINK (link, insn)
4108 if (link->insn == i3)
4109 link->insn = i1;
4111 break;
4117 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4118 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4119 rtx midnotes = 0;
4120 int from_luid;
4121 /* Compute which registers we expect to eliminate. newi2pat may be setting
4122 either i3dest or i2dest, so we must check it. Also, i1dest may be the
4123 same as i3dest, in which case newi2pat may be setting i1dest. */
4124 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4125 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4126 || !i2dest_killed
4127 ? 0 : i2dest);
4128 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4129 || (newi2pat && reg_set_p (i1dest, newi2pat))
4130 || !i1dest_killed
4131 ? 0 : i1dest);
4132 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4133 || (newi2pat && reg_set_p (i0dest, newi2pat))
4134 || !i0dest_killed
4135 ? 0 : i0dest);
4137 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4138 clear them. */
4139 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4140 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4141 if (i1)
4142 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4143 if (i0)
4144 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4146 /* Ensure that we do not have something that should not be shared but
4147 occurs multiple times in the new insns. Check this by first
4148 resetting all the `used' flags and then copying anything is shared. */
4150 reset_used_flags (i3notes);
4151 reset_used_flags (i2notes);
4152 reset_used_flags (i1notes);
4153 reset_used_flags (i0notes);
4154 reset_used_flags (newpat);
4155 reset_used_flags (newi2pat);
4156 if (undobuf.other_insn)
4157 reset_used_flags (PATTERN (undobuf.other_insn));
4159 i3notes = copy_rtx_if_shared (i3notes);
4160 i2notes = copy_rtx_if_shared (i2notes);
4161 i1notes = copy_rtx_if_shared (i1notes);
4162 i0notes = copy_rtx_if_shared (i0notes);
4163 newpat = copy_rtx_if_shared (newpat);
4164 newi2pat = copy_rtx_if_shared (newi2pat);
4165 if (undobuf.other_insn)
4166 reset_used_flags (PATTERN (undobuf.other_insn));
4168 INSN_CODE (i3) = insn_code_number;
4169 PATTERN (i3) = newpat;
4171 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4173 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4175 reset_used_flags (call_usage);
4176 call_usage = copy_rtx (call_usage);
4178 if (substed_i2)
4180 /* I2SRC must still be meaningful at this point. Some splitting
4181 operations can invalidate I2SRC, but those operations do not
4182 apply to calls. */
4183 gcc_assert (i2src);
4184 replace_rtx (call_usage, i2dest, i2src);
4187 if (substed_i1)
4188 replace_rtx (call_usage, i1dest, i1src);
4189 if (substed_i0)
4190 replace_rtx (call_usage, i0dest, i0src);
4192 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4195 if (undobuf.other_insn)
4196 INSN_CODE (undobuf.other_insn) = other_code_number;
4198 /* We had one special case above where I2 had more than one set and
4199 we replaced a destination of one of those sets with the destination
4200 of I3. In that case, we have to update LOG_LINKS of insns later
4201 in this basic block. Note that this (expensive) case is rare.
4203 Also, in this case, we must pretend that all REG_NOTEs for I2
4204 actually came from I3, so that REG_UNUSED notes from I2 will be
4205 properly handled. */
4207 if (i3_subst_into_i2)
4209 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4210 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4211 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4212 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4213 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4214 && ! find_reg_note (i2, REG_UNUSED,
4215 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4216 for (temp_insn = NEXT_INSN (i2);
4217 temp_insn
4218 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4219 || BB_HEAD (this_basic_block) != temp_insn);
4220 temp_insn = NEXT_INSN (temp_insn))
4221 if (temp_insn != i3 && INSN_P (temp_insn))
4222 FOR_EACH_LOG_LINK (link, temp_insn)
4223 if (link->insn == i2)
4224 link->insn = i3;
4226 if (i3notes)
4228 rtx link = i3notes;
4229 while (XEXP (link, 1))
4230 link = XEXP (link, 1);
4231 XEXP (link, 1) = i2notes;
4233 else
4234 i3notes = i2notes;
4235 i2notes = 0;
4238 LOG_LINKS (i3) = NULL;
4239 REG_NOTES (i3) = 0;
4240 LOG_LINKS (i2) = NULL;
4241 REG_NOTES (i2) = 0;
4243 if (newi2pat)
4245 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4246 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4247 this_basic_block);
4248 INSN_CODE (i2) = i2_code_number;
4249 PATTERN (i2) = newi2pat;
4251 else
4253 if (MAY_HAVE_DEBUG_INSNS && i2src)
4254 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4255 this_basic_block);
4256 SET_INSN_DELETED (i2);
4259 if (i1)
4261 LOG_LINKS (i1) = NULL;
4262 REG_NOTES (i1) = 0;
4263 if (MAY_HAVE_DEBUG_INSNS)
4264 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4265 this_basic_block);
4266 SET_INSN_DELETED (i1);
4269 if (i0)
4271 LOG_LINKS (i0) = NULL;
4272 REG_NOTES (i0) = 0;
4273 if (MAY_HAVE_DEBUG_INSNS)
4274 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4275 this_basic_block);
4276 SET_INSN_DELETED (i0);
4279 /* Get death notes for everything that is now used in either I3 or
4280 I2 and used to die in a previous insn. If we built two new
4281 patterns, move from I1 to I2 then I2 to I3 so that we get the
4282 proper movement on registers that I2 modifies. */
4284 if (i0)
4285 from_luid = DF_INSN_LUID (i0);
4286 else if (i1)
4287 from_luid = DF_INSN_LUID (i1);
4288 else
4289 from_luid = DF_INSN_LUID (i2);
4290 if (newi2pat)
4291 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4292 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4294 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4295 if (i3notes)
4296 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4297 elim_i2, elim_i1, elim_i0);
4298 if (i2notes)
4299 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4300 elim_i2, elim_i1, elim_i0);
4301 if (i1notes)
4302 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4303 elim_i2, elim_i1, elim_i0);
4304 if (i0notes)
4305 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4306 elim_i2, elim_i1, elim_i0);
4307 if (midnotes)
4308 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4309 elim_i2, elim_i1, elim_i0);
4311 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4312 know these are REG_UNUSED and want them to go to the desired insn,
4313 so we always pass it as i3. */
4315 if (newi2pat && new_i2_notes)
4316 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4317 NULL_RTX);
4319 if (new_i3_notes)
4320 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4321 NULL_RTX);
4323 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4324 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4325 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4326 in that case, it might delete I2. Similarly for I2 and I1.
4327 Show an additional death due to the REG_DEAD note we make here. If
4328 we discard it in distribute_notes, we will decrement it again. */
4330 if (i3dest_killed)
4332 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4333 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4334 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4335 elim_i1, elim_i0);
4336 else
4337 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4338 elim_i2, elim_i1, elim_i0);
4341 if (i2dest_in_i2src)
4343 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4344 if (newi2pat && reg_set_p (i2dest, newi2pat))
4345 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4346 NULL_RTX, NULL_RTX);
4347 else
4348 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4349 NULL_RTX, NULL_RTX, NULL_RTX);
4352 if (i1dest_in_i1src)
4354 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4355 if (newi2pat && reg_set_p (i1dest, newi2pat))
4356 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4357 NULL_RTX, NULL_RTX);
4358 else
4359 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4360 NULL_RTX, NULL_RTX, NULL_RTX);
4363 if (i0dest_in_i0src)
4365 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4366 if (newi2pat && reg_set_p (i0dest, newi2pat))
4367 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4368 NULL_RTX, NULL_RTX);
4369 else
4370 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4371 NULL_RTX, NULL_RTX, NULL_RTX);
4374 distribute_links (i3links);
4375 distribute_links (i2links);
4376 distribute_links (i1links);
4377 distribute_links (i0links);
4379 if (REG_P (i2dest))
4381 struct insn_link *link;
4382 rtx_insn *i2_insn = 0;
4383 rtx i2_val = 0, set;
4385 /* The insn that used to set this register doesn't exist, and
4386 this life of the register may not exist either. See if one of
4387 I3's links points to an insn that sets I2DEST. If it does,
4388 that is now the last known value for I2DEST. If we don't update
4389 this and I2 set the register to a value that depended on its old
4390 contents, we will get confused. If this insn is used, thing
4391 will be set correctly in combine_instructions. */
4392 FOR_EACH_LOG_LINK (link, i3)
4393 if ((set = single_set (link->insn)) != 0
4394 && rtx_equal_p (i2dest, SET_DEST (set)))
4395 i2_insn = link->insn, i2_val = SET_SRC (set);
4397 record_value_for_reg (i2dest, i2_insn, i2_val);
4399 /* If the reg formerly set in I2 died only once and that was in I3,
4400 zero its use count so it won't make `reload' do any work. */
4401 if (! added_sets_2
4402 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4403 && ! i2dest_in_i2src)
4404 INC_REG_N_SETS (REGNO (i2dest), -1);
4407 if (i1 && REG_P (i1dest))
4409 struct insn_link *link;
4410 rtx_insn *i1_insn = 0;
4411 rtx i1_val = 0, set;
4413 FOR_EACH_LOG_LINK (link, i3)
4414 if ((set = single_set (link->insn)) != 0
4415 && rtx_equal_p (i1dest, SET_DEST (set)))
4416 i1_insn = link->insn, i1_val = SET_SRC (set);
4418 record_value_for_reg (i1dest, i1_insn, i1_val);
4420 if (! added_sets_1 && ! i1dest_in_i1src)
4421 INC_REG_N_SETS (REGNO (i1dest), -1);
4424 if (i0 && REG_P (i0dest))
4426 struct insn_link *link;
4427 rtx_insn *i0_insn = 0;
4428 rtx i0_val = 0, set;
4430 FOR_EACH_LOG_LINK (link, i3)
4431 if ((set = single_set (link->insn)) != 0
4432 && rtx_equal_p (i0dest, SET_DEST (set)))
4433 i0_insn = link->insn, i0_val = SET_SRC (set);
4435 record_value_for_reg (i0dest, i0_insn, i0_val);
4437 if (! added_sets_0 && ! i0dest_in_i0src)
4438 INC_REG_N_SETS (REGNO (i0dest), -1);
4441 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4442 been made to this insn. The order is important, because newi2pat
4443 can affect nonzero_bits of newpat. */
4444 if (newi2pat)
4445 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4446 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4449 if (undobuf.other_insn != NULL_RTX)
4451 if (dump_file)
4453 fprintf (dump_file, "modifying other_insn ");
4454 dump_insn_slim (dump_file, undobuf.other_insn);
4456 df_insn_rescan (undobuf.other_insn);
4459 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4461 if (dump_file)
4463 fprintf (dump_file, "modifying insn i0 ");
4464 dump_insn_slim (dump_file, i0);
4466 df_insn_rescan (i0);
4469 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4471 if (dump_file)
4473 fprintf (dump_file, "modifying insn i1 ");
4474 dump_insn_slim (dump_file, i1);
4476 df_insn_rescan (i1);
4479 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4481 if (dump_file)
4483 fprintf (dump_file, "modifying insn i2 ");
4484 dump_insn_slim (dump_file, i2);
4486 df_insn_rescan (i2);
4489 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4491 if (dump_file)
4493 fprintf (dump_file, "modifying insn i3 ");
4494 dump_insn_slim (dump_file, i3);
4496 df_insn_rescan (i3);
4499 /* Set new_direct_jump_p if a new return or simple jump instruction
4500 has been created. Adjust the CFG accordingly. */
4501 if (returnjump_p (i3) || any_uncondjump_p (i3))
4503 *new_direct_jump_p = 1;
4504 mark_jump_label (PATTERN (i3), i3, 0);
4505 update_cfg_for_uncondjump (i3);
4508 if (undobuf.other_insn != NULL_RTX
4509 && (returnjump_p (undobuf.other_insn)
4510 || any_uncondjump_p (undobuf.other_insn)))
4512 *new_direct_jump_p = 1;
4513 update_cfg_for_uncondjump (undobuf.other_insn);
4516 /* A noop might also need cleaning up of CFG, if it comes from the
4517 simplification of a jump. */
4518 if (JUMP_P (i3)
4519 && GET_CODE (newpat) == SET
4520 && SET_SRC (newpat) == pc_rtx
4521 && SET_DEST (newpat) == pc_rtx)
4523 *new_direct_jump_p = 1;
4524 update_cfg_for_uncondjump (i3);
4527 if (undobuf.other_insn != NULL_RTX
4528 && JUMP_P (undobuf.other_insn)
4529 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4530 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4531 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4533 *new_direct_jump_p = 1;
4534 update_cfg_for_uncondjump (undobuf.other_insn);
4537 combine_successes++;
4538 undo_commit ();
4540 if (added_links_insn
4541 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4542 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4543 return added_links_insn;
4544 else
4545 return newi2pat ? i2 : i3;
4548 /* Undo all the modifications recorded in undobuf. */
4550 static void
4551 undo_all (void)
4553 struct undo *undo, *next;
4555 for (undo = undobuf.undos; undo; undo = next)
4557 next = undo->next;
4558 switch (undo->kind)
4560 case UNDO_RTX:
4561 *undo->where.r = undo->old_contents.r;
4562 break;
4563 case UNDO_INT:
4564 *undo->where.i = undo->old_contents.i;
4565 break;
4566 case UNDO_MODE:
4567 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4568 break;
4569 case UNDO_LINKS:
4570 *undo->where.l = undo->old_contents.l;
4571 break;
4572 default:
4573 gcc_unreachable ();
4576 undo->next = undobuf.frees;
4577 undobuf.frees = undo;
4580 undobuf.undos = 0;
4583 /* We've committed to accepting the changes we made. Move all
4584 of the undos to the free list. */
4586 static void
4587 undo_commit (void)
4589 struct undo *undo, *next;
4591 for (undo = undobuf.undos; undo; undo = next)
4593 next = undo->next;
4594 undo->next = undobuf.frees;
4595 undobuf.frees = undo;
4597 undobuf.undos = 0;
4600 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4601 where we have an arithmetic expression and return that point. LOC will
4602 be inside INSN.
4604 try_combine will call this function to see if an insn can be split into
4605 two insns. */
4607 static rtx *
4608 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4610 rtx x = *loc;
4611 enum rtx_code code = GET_CODE (x);
4612 rtx *split;
4613 unsigned HOST_WIDE_INT len = 0;
4614 HOST_WIDE_INT pos = 0;
4615 int unsignedp = 0;
4616 rtx inner = NULL_RTX;
4618 /* First special-case some codes. */
4619 switch (code)
4621 case SUBREG:
4622 #ifdef INSN_SCHEDULING
4623 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4624 point. */
4625 if (MEM_P (SUBREG_REG (x)))
4626 return loc;
4627 #endif
4628 return find_split_point (&SUBREG_REG (x), insn, false);
4630 case MEM:
4631 #ifdef HAVE_lo_sum
4632 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4633 using LO_SUM and HIGH. */
4634 if (GET_CODE (XEXP (x, 0)) == CONST
4635 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4637 machine_mode address_mode = get_address_mode (x);
4639 SUBST (XEXP (x, 0),
4640 gen_rtx_LO_SUM (address_mode,
4641 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4642 XEXP (x, 0)));
4643 return &XEXP (XEXP (x, 0), 0);
4645 #endif
4647 /* If we have a PLUS whose second operand is a constant and the
4648 address is not valid, perhaps will can split it up using
4649 the machine-specific way to split large constants. We use
4650 the first pseudo-reg (one of the virtual regs) as a placeholder;
4651 it will not remain in the result. */
4652 if (GET_CODE (XEXP (x, 0)) == PLUS
4653 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4654 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4655 MEM_ADDR_SPACE (x)))
4657 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4658 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4659 XEXP (x, 0)),
4660 subst_insn);
4662 /* This should have produced two insns, each of which sets our
4663 placeholder. If the source of the second is a valid address,
4664 we can make put both sources together and make a split point
4665 in the middle. */
4667 if (seq
4668 && NEXT_INSN (seq) != NULL_RTX
4669 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4670 && NONJUMP_INSN_P (seq)
4671 && GET_CODE (PATTERN (seq)) == SET
4672 && SET_DEST (PATTERN (seq)) == reg
4673 && ! reg_mentioned_p (reg,
4674 SET_SRC (PATTERN (seq)))
4675 && NONJUMP_INSN_P (NEXT_INSN (seq))
4676 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4677 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4678 && memory_address_addr_space_p
4679 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4680 MEM_ADDR_SPACE (x)))
4682 rtx src1 = SET_SRC (PATTERN (seq));
4683 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4685 /* Replace the placeholder in SRC2 with SRC1. If we can
4686 find where in SRC2 it was placed, that can become our
4687 split point and we can replace this address with SRC2.
4688 Just try two obvious places. */
4690 src2 = replace_rtx (src2, reg, src1);
4691 split = 0;
4692 if (XEXP (src2, 0) == src1)
4693 split = &XEXP (src2, 0);
4694 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4695 && XEXP (XEXP (src2, 0), 0) == src1)
4696 split = &XEXP (XEXP (src2, 0), 0);
4698 if (split)
4700 SUBST (XEXP (x, 0), src2);
4701 return split;
4705 /* If that didn't work, perhaps the first operand is complex and
4706 needs to be computed separately, so make a split point there.
4707 This will occur on machines that just support REG + CONST
4708 and have a constant moved through some previous computation. */
4710 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4711 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4712 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4713 return &XEXP (XEXP (x, 0), 0);
4716 /* If we have a PLUS whose first operand is complex, try computing it
4717 separately by making a split there. */
4718 if (GET_CODE (XEXP (x, 0)) == PLUS
4719 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4720 MEM_ADDR_SPACE (x))
4721 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4722 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4723 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4724 return &XEXP (XEXP (x, 0), 0);
4725 break;
4727 case SET:
4728 #ifdef HAVE_cc0
4729 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4730 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4731 we need to put the operand into a register. So split at that
4732 point. */
4734 if (SET_DEST (x) == cc0_rtx
4735 && GET_CODE (SET_SRC (x)) != COMPARE
4736 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4737 && !OBJECT_P (SET_SRC (x))
4738 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4739 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4740 return &SET_SRC (x);
4741 #endif
4743 /* See if we can split SET_SRC as it stands. */
4744 split = find_split_point (&SET_SRC (x), insn, true);
4745 if (split && split != &SET_SRC (x))
4746 return split;
4748 /* See if we can split SET_DEST as it stands. */
4749 split = find_split_point (&SET_DEST (x), insn, false);
4750 if (split && split != &SET_DEST (x))
4751 return split;
4753 /* See if this is a bitfield assignment with everything constant. If
4754 so, this is an IOR of an AND, so split it into that. */
4755 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4756 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4757 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4758 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4759 && CONST_INT_P (SET_SRC (x))
4760 && ((INTVAL (XEXP (SET_DEST (x), 1))
4761 + INTVAL (XEXP (SET_DEST (x), 2)))
4762 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4763 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4765 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4766 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4767 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4768 rtx dest = XEXP (SET_DEST (x), 0);
4769 machine_mode mode = GET_MODE (dest);
4770 unsigned HOST_WIDE_INT mask
4771 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4772 rtx or_mask;
4774 if (BITS_BIG_ENDIAN)
4775 pos = GET_MODE_PRECISION (mode) - len - pos;
4777 or_mask = gen_int_mode (src << pos, mode);
4778 if (src == mask)
4779 SUBST (SET_SRC (x),
4780 simplify_gen_binary (IOR, mode, dest, or_mask));
4781 else
4783 rtx negmask = gen_int_mode (~(mask << pos), mode);
4784 SUBST (SET_SRC (x),
4785 simplify_gen_binary (IOR, mode,
4786 simplify_gen_binary (AND, mode,
4787 dest, negmask),
4788 or_mask));
4791 SUBST (SET_DEST (x), dest);
4793 split = find_split_point (&SET_SRC (x), insn, true);
4794 if (split && split != &SET_SRC (x))
4795 return split;
4798 /* Otherwise, see if this is an operation that we can split into two.
4799 If so, try to split that. */
4800 code = GET_CODE (SET_SRC (x));
4802 switch (code)
4804 case AND:
4805 /* If we are AND'ing with a large constant that is only a single
4806 bit and the result is only being used in a context where we
4807 need to know if it is zero or nonzero, replace it with a bit
4808 extraction. This will avoid the large constant, which might
4809 have taken more than one insn to make. If the constant were
4810 not a valid argument to the AND but took only one insn to make,
4811 this is no worse, but if it took more than one insn, it will
4812 be better. */
4814 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4815 && REG_P (XEXP (SET_SRC (x), 0))
4816 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4817 && REG_P (SET_DEST (x))
4818 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4819 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4820 && XEXP (*split, 0) == SET_DEST (x)
4821 && XEXP (*split, 1) == const0_rtx)
4823 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4824 XEXP (SET_SRC (x), 0),
4825 pos, NULL_RTX, 1, 1, 0, 0);
4826 if (extraction != 0)
4828 SUBST (SET_SRC (x), extraction);
4829 return find_split_point (loc, insn, false);
4832 break;
4834 case NE:
4835 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4836 is known to be on, this can be converted into a NEG of a shift. */
4837 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4838 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4839 && 1 <= (pos = exact_log2
4840 (nonzero_bits (XEXP (SET_SRC (x), 0),
4841 GET_MODE (XEXP (SET_SRC (x), 0))))))
4843 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4845 SUBST (SET_SRC (x),
4846 gen_rtx_NEG (mode,
4847 gen_rtx_LSHIFTRT (mode,
4848 XEXP (SET_SRC (x), 0),
4849 GEN_INT (pos))));
4851 split = find_split_point (&SET_SRC (x), insn, true);
4852 if (split && split != &SET_SRC (x))
4853 return split;
4855 break;
4857 case SIGN_EXTEND:
4858 inner = XEXP (SET_SRC (x), 0);
4860 /* We can't optimize if either mode is a partial integer
4861 mode as we don't know how many bits are significant
4862 in those modes. */
4863 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4864 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4865 break;
4867 pos = 0;
4868 len = GET_MODE_PRECISION (GET_MODE (inner));
4869 unsignedp = 0;
4870 break;
4872 case SIGN_EXTRACT:
4873 case ZERO_EXTRACT:
4874 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4875 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4877 inner = XEXP (SET_SRC (x), 0);
4878 len = INTVAL (XEXP (SET_SRC (x), 1));
4879 pos = INTVAL (XEXP (SET_SRC (x), 2));
4881 if (BITS_BIG_ENDIAN)
4882 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4883 unsignedp = (code == ZERO_EXTRACT);
4885 break;
4887 default:
4888 break;
4891 if (len && pos >= 0
4892 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4894 machine_mode mode = GET_MODE (SET_SRC (x));
4896 /* For unsigned, we have a choice of a shift followed by an
4897 AND or two shifts. Use two shifts for field sizes where the
4898 constant might be too large. We assume here that we can
4899 always at least get 8-bit constants in an AND insn, which is
4900 true for every current RISC. */
4902 if (unsignedp && len <= 8)
4904 unsigned HOST_WIDE_INT mask
4905 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4906 SUBST (SET_SRC (x),
4907 gen_rtx_AND (mode,
4908 gen_rtx_LSHIFTRT
4909 (mode, gen_lowpart (mode, inner),
4910 GEN_INT (pos)),
4911 gen_int_mode (mask, mode)));
4913 split = find_split_point (&SET_SRC (x), insn, true);
4914 if (split && split != &SET_SRC (x))
4915 return split;
4917 else
4919 SUBST (SET_SRC (x),
4920 gen_rtx_fmt_ee
4921 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4922 gen_rtx_ASHIFT (mode,
4923 gen_lowpart (mode, inner),
4924 GEN_INT (GET_MODE_PRECISION (mode)
4925 - len - pos)),
4926 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4928 split = find_split_point (&SET_SRC (x), insn, true);
4929 if (split && split != &SET_SRC (x))
4930 return split;
4934 /* See if this is a simple operation with a constant as the second
4935 operand. It might be that this constant is out of range and hence
4936 could be used as a split point. */
4937 if (BINARY_P (SET_SRC (x))
4938 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4939 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4940 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4941 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4942 return &XEXP (SET_SRC (x), 1);
4944 /* Finally, see if this is a simple operation with its first operand
4945 not in a register. The operation might require this operand in a
4946 register, so return it as a split point. We can always do this
4947 because if the first operand were another operation, we would have
4948 already found it as a split point. */
4949 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4950 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4951 return &XEXP (SET_SRC (x), 0);
4953 return 0;
4955 case AND:
4956 case IOR:
4957 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4958 it is better to write this as (not (ior A B)) so we can split it.
4959 Similarly for IOR. */
4960 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4962 SUBST (*loc,
4963 gen_rtx_NOT (GET_MODE (x),
4964 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4965 GET_MODE (x),
4966 XEXP (XEXP (x, 0), 0),
4967 XEXP (XEXP (x, 1), 0))));
4968 return find_split_point (loc, insn, set_src);
4971 /* Many RISC machines have a large set of logical insns. If the
4972 second operand is a NOT, put it first so we will try to split the
4973 other operand first. */
4974 if (GET_CODE (XEXP (x, 1)) == NOT)
4976 rtx tem = XEXP (x, 0);
4977 SUBST (XEXP (x, 0), XEXP (x, 1));
4978 SUBST (XEXP (x, 1), tem);
4980 break;
4982 case PLUS:
4983 case MINUS:
4984 /* Canonicalization can produce (minus A (mult B C)), where C is a
4985 constant. It may be better to try splitting (plus (mult B -C) A)
4986 instead if this isn't a multiply by a power of two. */
4987 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4988 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4989 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4991 machine_mode mode = GET_MODE (x);
4992 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4993 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4994 SUBST (*loc, gen_rtx_PLUS (mode,
4995 gen_rtx_MULT (mode,
4996 XEXP (XEXP (x, 1), 0),
4997 gen_int_mode (other_int,
4998 mode)),
4999 XEXP (x, 0)));
5000 return find_split_point (loc, insn, set_src);
5003 /* Split at a multiply-accumulate instruction. However if this is
5004 the SET_SRC, we likely do not have such an instruction and it's
5005 worthless to try this split. */
5006 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5007 return loc;
5009 default:
5010 break;
5013 /* Otherwise, select our actions depending on our rtx class. */
5014 switch (GET_RTX_CLASS (code))
5016 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5017 case RTX_TERNARY:
5018 split = find_split_point (&XEXP (x, 2), insn, false);
5019 if (split)
5020 return split;
5021 /* ... fall through ... */
5022 case RTX_BIN_ARITH:
5023 case RTX_COMM_ARITH:
5024 case RTX_COMPARE:
5025 case RTX_COMM_COMPARE:
5026 split = find_split_point (&XEXP (x, 1), insn, false);
5027 if (split)
5028 return split;
5029 /* ... fall through ... */
5030 case RTX_UNARY:
5031 /* Some machines have (and (shift ...) ...) insns. If X is not
5032 an AND, but XEXP (X, 0) is, use it as our split point. */
5033 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5034 return &XEXP (x, 0);
5036 split = find_split_point (&XEXP (x, 0), insn, false);
5037 if (split)
5038 return split;
5039 return loc;
5041 default:
5042 /* Otherwise, we don't have a split point. */
5043 return 0;
5047 /* Throughout X, replace FROM with TO, and return the result.
5048 The result is TO if X is FROM;
5049 otherwise the result is X, but its contents may have been modified.
5050 If they were modified, a record was made in undobuf so that
5051 undo_all will (among other things) return X to its original state.
5053 If the number of changes necessary is too much to record to undo,
5054 the excess changes are not made, so the result is invalid.
5055 The changes already made can still be undone.
5056 undobuf.num_undo is incremented for such changes, so by testing that
5057 the caller can tell whether the result is valid.
5059 `n_occurrences' is incremented each time FROM is replaced.
5061 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5063 IN_COND is nonzero if we are at the top level of a condition.
5065 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5066 by copying if `n_occurrences' is nonzero. */
5068 static rtx
5069 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5071 enum rtx_code code = GET_CODE (x);
5072 machine_mode op0_mode = VOIDmode;
5073 const char *fmt;
5074 int len, i;
5075 rtx new_rtx;
5077 /* Two expressions are equal if they are identical copies of a shared
5078 RTX or if they are both registers with the same register number
5079 and mode. */
5081 #define COMBINE_RTX_EQUAL_P(X,Y) \
5082 ((X) == (Y) \
5083 || (REG_P (X) && REG_P (Y) \
5084 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5086 /* Do not substitute into clobbers of regs -- this will never result in
5087 valid RTL. */
5088 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5089 return x;
5091 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5093 n_occurrences++;
5094 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5097 /* If X and FROM are the same register but different modes, they
5098 will not have been seen as equal above. However, the log links code
5099 will make a LOG_LINKS entry for that case. If we do nothing, we
5100 will try to rerecognize our original insn and, when it succeeds,
5101 we will delete the feeding insn, which is incorrect.
5103 So force this insn not to match in this (rare) case. */
5104 if (! in_dest && code == REG && REG_P (from)
5105 && reg_overlap_mentioned_p (x, from))
5106 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5108 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5109 of which may contain things that can be combined. */
5110 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5111 return x;
5113 /* It is possible to have a subexpression appear twice in the insn.
5114 Suppose that FROM is a register that appears within TO.
5115 Then, after that subexpression has been scanned once by `subst',
5116 the second time it is scanned, TO may be found. If we were
5117 to scan TO here, we would find FROM within it and create a
5118 self-referent rtl structure which is completely wrong. */
5119 if (COMBINE_RTX_EQUAL_P (x, to))
5120 return to;
5122 /* Parallel asm_operands need special attention because all of the
5123 inputs are shared across the arms. Furthermore, unsharing the
5124 rtl results in recognition failures. Failure to handle this case
5125 specially can result in circular rtl.
5127 Solve this by doing a normal pass across the first entry of the
5128 parallel, and only processing the SET_DESTs of the subsequent
5129 entries. Ug. */
5131 if (code == PARALLEL
5132 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5133 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5135 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5137 /* If this substitution failed, this whole thing fails. */
5138 if (GET_CODE (new_rtx) == CLOBBER
5139 && XEXP (new_rtx, 0) == const0_rtx)
5140 return new_rtx;
5142 SUBST (XVECEXP (x, 0, 0), new_rtx);
5144 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5146 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5148 if (!REG_P (dest)
5149 && GET_CODE (dest) != CC0
5150 && GET_CODE (dest) != PC)
5152 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5154 /* If this substitution failed, this whole thing fails. */
5155 if (GET_CODE (new_rtx) == CLOBBER
5156 && XEXP (new_rtx, 0) == const0_rtx)
5157 return new_rtx;
5159 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5163 else
5165 len = GET_RTX_LENGTH (code);
5166 fmt = GET_RTX_FORMAT (code);
5168 /* We don't need to process a SET_DEST that is a register, CC0,
5169 or PC, so set up to skip this common case. All other cases
5170 where we want to suppress replacing something inside a
5171 SET_SRC are handled via the IN_DEST operand. */
5172 if (code == SET
5173 && (REG_P (SET_DEST (x))
5174 || GET_CODE (SET_DEST (x)) == CC0
5175 || GET_CODE (SET_DEST (x)) == PC))
5176 fmt = "ie";
5178 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5179 constant. */
5180 if (fmt[0] == 'e')
5181 op0_mode = GET_MODE (XEXP (x, 0));
5183 for (i = 0; i < len; i++)
5185 if (fmt[i] == 'E')
5187 int j;
5188 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5190 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5192 new_rtx = (unique_copy && n_occurrences
5193 ? copy_rtx (to) : to);
5194 n_occurrences++;
5196 else
5198 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5199 unique_copy);
5201 /* If this substitution failed, this whole thing
5202 fails. */
5203 if (GET_CODE (new_rtx) == CLOBBER
5204 && XEXP (new_rtx, 0) == const0_rtx)
5205 return new_rtx;
5208 SUBST (XVECEXP (x, i, j), new_rtx);
5211 else if (fmt[i] == 'e')
5213 /* If this is a register being set, ignore it. */
5214 new_rtx = XEXP (x, i);
5215 if (in_dest
5216 && i == 0
5217 && (((code == SUBREG || code == ZERO_EXTRACT)
5218 && REG_P (new_rtx))
5219 || code == STRICT_LOW_PART))
5222 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5224 /* In general, don't install a subreg involving two
5225 modes not tieable. It can worsen register
5226 allocation, and can even make invalid reload
5227 insns, since the reg inside may need to be copied
5228 from in the outside mode, and that may be invalid
5229 if it is an fp reg copied in integer mode.
5231 We allow two exceptions to this: It is valid if
5232 it is inside another SUBREG and the mode of that
5233 SUBREG and the mode of the inside of TO is
5234 tieable and it is valid if X is a SET that copies
5235 FROM to CC0. */
5237 if (GET_CODE (to) == SUBREG
5238 && ! MODES_TIEABLE_P (GET_MODE (to),
5239 GET_MODE (SUBREG_REG (to)))
5240 && ! (code == SUBREG
5241 && MODES_TIEABLE_P (GET_MODE (x),
5242 GET_MODE (SUBREG_REG (to))))
5243 #ifdef HAVE_cc0
5244 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5245 #endif
5247 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5249 if (code == SUBREG
5250 && REG_P (to)
5251 && REGNO (to) < FIRST_PSEUDO_REGISTER
5252 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5253 SUBREG_BYTE (x),
5254 GET_MODE (x)) < 0)
5255 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5257 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5258 n_occurrences++;
5260 else
5261 /* If we are in a SET_DEST, suppress most cases unless we
5262 have gone inside a MEM, in which case we want to
5263 simplify the address. We assume here that things that
5264 are actually part of the destination have their inner
5265 parts in the first expression. This is true for SUBREG,
5266 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5267 things aside from REG and MEM that should appear in a
5268 SET_DEST. */
5269 new_rtx = subst (XEXP (x, i), from, to,
5270 (((in_dest
5271 && (code == SUBREG || code == STRICT_LOW_PART
5272 || code == ZERO_EXTRACT))
5273 || code == SET)
5274 && i == 0),
5275 code == IF_THEN_ELSE && i == 0,
5276 unique_copy);
5278 /* If we found that we will have to reject this combination,
5279 indicate that by returning the CLOBBER ourselves, rather than
5280 an expression containing it. This will speed things up as
5281 well as prevent accidents where two CLOBBERs are considered
5282 to be equal, thus producing an incorrect simplification. */
5284 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5285 return new_rtx;
5287 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5289 machine_mode mode = GET_MODE (x);
5291 x = simplify_subreg (GET_MODE (x), new_rtx,
5292 GET_MODE (SUBREG_REG (x)),
5293 SUBREG_BYTE (x));
5294 if (! x)
5295 x = gen_rtx_CLOBBER (mode, const0_rtx);
5297 else if (CONST_SCALAR_INT_P (new_rtx)
5298 && GET_CODE (x) == ZERO_EXTEND)
5300 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5301 new_rtx, GET_MODE (XEXP (x, 0)));
5302 gcc_assert (x);
5304 else
5305 SUBST (XEXP (x, i), new_rtx);
5310 /* Check if we are loading something from the constant pool via float
5311 extension; in this case we would undo compress_float_constant
5312 optimization and degenerate constant load to an immediate value. */
5313 if (GET_CODE (x) == FLOAT_EXTEND
5314 && MEM_P (XEXP (x, 0))
5315 && MEM_READONLY_P (XEXP (x, 0)))
5317 rtx tmp = avoid_constant_pool_reference (x);
5318 if (x != tmp)
5319 return x;
5322 /* Try to simplify X. If the simplification changed the code, it is likely
5323 that further simplification will help, so loop, but limit the number
5324 of repetitions that will be performed. */
5326 for (i = 0; i < 4; i++)
5328 /* If X is sufficiently simple, don't bother trying to do anything
5329 with it. */
5330 if (code != CONST_INT && code != REG && code != CLOBBER)
5331 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5333 if (GET_CODE (x) == code)
5334 break;
5336 code = GET_CODE (x);
5338 /* We no longer know the original mode of operand 0 since we
5339 have changed the form of X) */
5340 op0_mode = VOIDmode;
5343 return x;
5346 /* Simplify X, a piece of RTL. We just operate on the expression at the
5347 outer level; call `subst' to simplify recursively. Return the new
5348 expression.
5350 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5351 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5352 of a condition. */
5354 static rtx
5355 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5356 int in_cond)
5358 enum rtx_code code = GET_CODE (x);
5359 machine_mode mode = GET_MODE (x);
5360 rtx temp;
5361 int i;
5363 /* If this is a commutative operation, put a constant last and a complex
5364 expression first. We don't need to do this for comparisons here. */
5365 if (COMMUTATIVE_ARITH_P (x)
5366 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5368 temp = XEXP (x, 0);
5369 SUBST (XEXP (x, 0), XEXP (x, 1));
5370 SUBST (XEXP (x, 1), temp);
5373 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5374 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5375 things. Check for cases where both arms are testing the same
5376 condition.
5378 Don't do anything if all operands are very simple. */
5380 if ((BINARY_P (x)
5381 && ((!OBJECT_P (XEXP (x, 0))
5382 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5383 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5384 || (!OBJECT_P (XEXP (x, 1))
5385 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5386 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5387 || (UNARY_P (x)
5388 && (!OBJECT_P (XEXP (x, 0))
5389 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5390 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5392 rtx cond, true_rtx, false_rtx;
5394 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5395 if (cond != 0
5396 /* If everything is a comparison, what we have is highly unlikely
5397 to be simpler, so don't use it. */
5398 && ! (COMPARISON_P (x)
5399 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5401 rtx cop1 = const0_rtx;
5402 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5404 if (cond_code == NE && COMPARISON_P (cond))
5405 return x;
5407 /* Simplify the alternative arms; this may collapse the true and
5408 false arms to store-flag values. Be careful to use copy_rtx
5409 here since true_rtx or false_rtx might share RTL with x as a
5410 result of the if_then_else_cond call above. */
5411 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5412 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5414 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5415 is unlikely to be simpler. */
5416 if (general_operand (true_rtx, VOIDmode)
5417 && general_operand (false_rtx, VOIDmode))
5419 enum rtx_code reversed;
5421 /* Restarting if we generate a store-flag expression will cause
5422 us to loop. Just drop through in this case. */
5424 /* If the result values are STORE_FLAG_VALUE and zero, we can
5425 just make the comparison operation. */
5426 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5427 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5428 cond, cop1);
5429 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5430 && ((reversed = reversed_comparison_code_parts
5431 (cond_code, cond, cop1, NULL))
5432 != UNKNOWN))
5433 x = simplify_gen_relational (reversed, mode, VOIDmode,
5434 cond, cop1);
5436 /* Likewise, we can make the negate of a comparison operation
5437 if the result values are - STORE_FLAG_VALUE and zero. */
5438 else if (CONST_INT_P (true_rtx)
5439 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5440 && false_rtx == const0_rtx)
5441 x = simplify_gen_unary (NEG, mode,
5442 simplify_gen_relational (cond_code,
5443 mode, VOIDmode,
5444 cond, cop1),
5445 mode);
5446 else if (CONST_INT_P (false_rtx)
5447 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5448 && true_rtx == const0_rtx
5449 && ((reversed = reversed_comparison_code_parts
5450 (cond_code, cond, cop1, NULL))
5451 != UNKNOWN))
5452 x = simplify_gen_unary (NEG, mode,
5453 simplify_gen_relational (reversed,
5454 mode, VOIDmode,
5455 cond, cop1),
5456 mode);
5457 else
5458 return gen_rtx_IF_THEN_ELSE (mode,
5459 simplify_gen_relational (cond_code,
5460 mode,
5461 VOIDmode,
5462 cond,
5463 cop1),
5464 true_rtx, false_rtx);
5466 code = GET_CODE (x);
5467 op0_mode = VOIDmode;
5472 /* Try to fold this expression in case we have constants that weren't
5473 present before. */
5474 temp = 0;
5475 switch (GET_RTX_CLASS (code))
5477 case RTX_UNARY:
5478 if (op0_mode == VOIDmode)
5479 op0_mode = GET_MODE (XEXP (x, 0));
5480 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5481 break;
5482 case RTX_COMPARE:
5483 case RTX_COMM_COMPARE:
5485 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5486 if (cmp_mode == VOIDmode)
5488 cmp_mode = GET_MODE (XEXP (x, 1));
5489 if (cmp_mode == VOIDmode)
5490 cmp_mode = op0_mode;
5492 temp = simplify_relational_operation (code, mode, cmp_mode,
5493 XEXP (x, 0), XEXP (x, 1));
5495 break;
5496 case RTX_COMM_ARITH:
5497 case RTX_BIN_ARITH:
5498 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5499 break;
5500 case RTX_BITFIELD_OPS:
5501 case RTX_TERNARY:
5502 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5503 XEXP (x, 1), XEXP (x, 2));
5504 break;
5505 default:
5506 break;
5509 if (temp)
5511 x = temp;
5512 code = GET_CODE (temp);
5513 op0_mode = VOIDmode;
5514 mode = GET_MODE (temp);
5517 /* First see if we can apply the inverse distributive law. */
5518 if (code == PLUS || code == MINUS
5519 || code == AND || code == IOR || code == XOR)
5521 x = apply_distributive_law (x);
5522 code = GET_CODE (x);
5523 op0_mode = VOIDmode;
5526 /* If CODE is an associative operation not otherwise handled, see if we
5527 can associate some operands. This can win if they are constants or
5528 if they are logically related (i.e. (a & b) & a). */
5529 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5530 || code == AND || code == IOR || code == XOR
5531 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5532 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5533 || (flag_associative_math && FLOAT_MODE_P (mode))))
5535 if (GET_CODE (XEXP (x, 0)) == code)
5537 rtx other = XEXP (XEXP (x, 0), 0);
5538 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5539 rtx inner_op1 = XEXP (x, 1);
5540 rtx inner;
5542 /* Make sure we pass the constant operand if any as the second
5543 one if this is a commutative operation. */
5544 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5546 rtx tem = inner_op0;
5547 inner_op0 = inner_op1;
5548 inner_op1 = tem;
5550 inner = simplify_binary_operation (code == MINUS ? PLUS
5551 : code == DIV ? MULT
5552 : code,
5553 mode, inner_op0, inner_op1);
5555 /* For commutative operations, try the other pair if that one
5556 didn't simplify. */
5557 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5559 other = XEXP (XEXP (x, 0), 1);
5560 inner = simplify_binary_operation (code, mode,
5561 XEXP (XEXP (x, 0), 0),
5562 XEXP (x, 1));
5565 if (inner)
5566 return simplify_gen_binary (code, mode, other, inner);
5570 /* A little bit of algebraic simplification here. */
5571 switch (code)
5573 case MEM:
5574 /* Ensure that our address has any ASHIFTs converted to MULT in case
5575 address-recognizing predicates are called later. */
5576 temp = make_compound_operation (XEXP (x, 0), MEM);
5577 SUBST (XEXP (x, 0), temp);
5578 break;
5580 case SUBREG:
5581 if (op0_mode == VOIDmode)
5582 op0_mode = GET_MODE (SUBREG_REG (x));
5584 /* See if this can be moved to simplify_subreg. */
5585 if (CONSTANT_P (SUBREG_REG (x))
5586 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5587 /* Don't call gen_lowpart if the inner mode
5588 is VOIDmode and we cannot simplify it, as SUBREG without
5589 inner mode is invalid. */
5590 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5591 || gen_lowpart_common (mode, SUBREG_REG (x))))
5592 return gen_lowpart (mode, SUBREG_REG (x));
5594 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5595 break;
5597 rtx temp;
5598 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5599 SUBREG_BYTE (x));
5600 if (temp)
5601 return temp;
5603 /* If op is known to have all lower bits zero, the result is zero. */
5604 if (!in_dest
5605 && SCALAR_INT_MODE_P (mode)
5606 && SCALAR_INT_MODE_P (op0_mode)
5607 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5608 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5609 && HWI_COMPUTABLE_MODE_P (op0_mode)
5610 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5611 & GET_MODE_MASK (mode)) == 0)
5612 return CONST0_RTX (mode);
5615 /* Don't change the mode of the MEM if that would change the meaning
5616 of the address. */
5617 if (MEM_P (SUBREG_REG (x))
5618 && (MEM_VOLATILE_P (SUBREG_REG (x))
5619 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5620 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5621 return gen_rtx_CLOBBER (mode, const0_rtx);
5623 /* Note that we cannot do any narrowing for non-constants since
5624 we might have been counting on using the fact that some bits were
5625 zero. We now do this in the SET. */
5627 break;
5629 case NEG:
5630 temp = expand_compound_operation (XEXP (x, 0));
5632 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5633 replaced by (lshiftrt X C). This will convert
5634 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5636 if (GET_CODE (temp) == ASHIFTRT
5637 && CONST_INT_P (XEXP (temp, 1))
5638 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5639 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5640 INTVAL (XEXP (temp, 1)));
5642 /* If X has only a single bit that might be nonzero, say, bit I, convert
5643 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5644 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5645 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5646 or a SUBREG of one since we'd be making the expression more
5647 complex if it was just a register. */
5649 if (!REG_P (temp)
5650 && ! (GET_CODE (temp) == SUBREG
5651 && REG_P (SUBREG_REG (temp)))
5652 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5654 rtx temp1 = simplify_shift_const
5655 (NULL_RTX, ASHIFTRT, mode,
5656 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5657 GET_MODE_PRECISION (mode) - 1 - i),
5658 GET_MODE_PRECISION (mode) - 1 - i);
5660 /* If all we did was surround TEMP with the two shifts, we
5661 haven't improved anything, so don't use it. Otherwise,
5662 we are better off with TEMP1. */
5663 if (GET_CODE (temp1) != ASHIFTRT
5664 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5665 || XEXP (XEXP (temp1, 0), 0) != temp)
5666 return temp1;
5668 break;
5670 case TRUNCATE:
5671 /* We can't handle truncation to a partial integer mode here
5672 because we don't know the real bitsize of the partial
5673 integer mode. */
5674 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5675 break;
5677 if (HWI_COMPUTABLE_MODE_P (mode))
5678 SUBST (XEXP (x, 0),
5679 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5680 GET_MODE_MASK (mode), 0));
5682 /* We can truncate a constant value and return it. */
5683 if (CONST_INT_P (XEXP (x, 0)))
5684 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5686 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5687 whose value is a comparison can be replaced with a subreg if
5688 STORE_FLAG_VALUE permits. */
5689 if (HWI_COMPUTABLE_MODE_P (mode)
5690 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5691 && (temp = get_last_value (XEXP (x, 0)))
5692 && COMPARISON_P (temp))
5693 return gen_lowpart (mode, XEXP (x, 0));
5694 break;
5696 case CONST:
5697 /* (const (const X)) can become (const X). Do it this way rather than
5698 returning the inner CONST since CONST can be shared with a
5699 REG_EQUAL note. */
5700 if (GET_CODE (XEXP (x, 0)) == CONST)
5701 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5702 break;
5704 #ifdef HAVE_lo_sum
5705 case LO_SUM:
5706 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5707 can add in an offset. find_split_point will split this address up
5708 again if it doesn't match. */
5709 if (GET_CODE (XEXP (x, 0)) == HIGH
5710 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5711 return XEXP (x, 1);
5712 break;
5713 #endif
5715 case PLUS:
5716 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5717 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5718 bit-field and can be replaced by either a sign_extend or a
5719 sign_extract. The `and' may be a zero_extend and the two
5720 <c>, -<c> constants may be reversed. */
5721 if (GET_CODE (XEXP (x, 0)) == XOR
5722 && CONST_INT_P (XEXP (x, 1))
5723 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5724 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5725 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5726 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5727 && HWI_COMPUTABLE_MODE_P (mode)
5728 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5729 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5730 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5731 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5732 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5733 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5734 == (unsigned int) i + 1))))
5735 return simplify_shift_const
5736 (NULL_RTX, ASHIFTRT, mode,
5737 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5738 XEXP (XEXP (XEXP (x, 0), 0), 0),
5739 GET_MODE_PRECISION (mode) - (i + 1)),
5740 GET_MODE_PRECISION (mode) - (i + 1));
5742 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5743 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5744 the bitsize of the mode - 1. This allows simplification of
5745 "a = (b & 8) == 0;" */
5746 if (XEXP (x, 1) == constm1_rtx
5747 && !REG_P (XEXP (x, 0))
5748 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5749 && REG_P (SUBREG_REG (XEXP (x, 0))))
5750 && nonzero_bits (XEXP (x, 0), mode) == 1)
5751 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5752 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5753 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5754 GET_MODE_PRECISION (mode) - 1),
5755 GET_MODE_PRECISION (mode) - 1);
5757 /* If we are adding two things that have no bits in common, convert
5758 the addition into an IOR. This will often be further simplified,
5759 for example in cases like ((a & 1) + (a & 2)), which can
5760 become a & 3. */
5762 if (HWI_COMPUTABLE_MODE_P (mode)
5763 && (nonzero_bits (XEXP (x, 0), mode)
5764 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5766 /* Try to simplify the expression further. */
5767 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5768 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5770 /* If we could, great. If not, do not go ahead with the IOR
5771 replacement, since PLUS appears in many special purpose
5772 address arithmetic instructions. */
5773 if (GET_CODE (temp) != CLOBBER
5774 && (GET_CODE (temp) != IOR
5775 || ((XEXP (temp, 0) != XEXP (x, 0)
5776 || XEXP (temp, 1) != XEXP (x, 1))
5777 && (XEXP (temp, 0) != XEXP (x, 1)
5778 || XEXP (temp, 1) != XEXP (x, 0)))))
5779 return temp;
5781 break;
5783 case MINUS:
5784 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5785 (and <foo> (const_int pow2-1)) */
5786 if (GET_CODE (XEXP (x, 1)) == AND
5787 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5788 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5789 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5790 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5791 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5792 break;
5794 case MULT:
5795 /* If we have (mult (plus A B) C), apply the distributive law and then
5796 the inverse distributive law to see if things simplify. This
5797 occurs mostly in addresses, often when unrolling loops. */
5799 if (GET_CODE (XEXP (x, 0)) == PLUS)
5801 rtx result = distribute_and_simplify_rtx (x, 0);
5802 if (result)
5803 return result;
5806 /* Try simplify a*(b/c) as (a*b)/c. */
5807 if (FLOAT_MODE_P (mode) && flag_associative_math
5808 && GET_CODE (XEXP (x, 0)) == DIV)
5810 rtx tem = simplify_binary_operation (MULT, mode,
5811 XEXP (XEXP (x, 0), 0),
5812 XEXP (x, 1));
5813 if (tem)
5814 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5816 break;
5818 case UDIV:
5819 /* If this is a divide by a power of two, treat it as a shift if
5820 its first operand is a shift. */
5821 if (CONST_INT_P (XEXP (x, 1))
5822 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5823 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5824 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5825 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5826 || GET_CODE (XEXP (x, 0)) == ROTATE
5827 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5828 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5829 break;
5831 case EQ: case NE:
5832 case GT: case GTU: case GE: case GEU:
5833 case LT: case LTU: case LE: case LEU:
5834 case UNEQ: case LTGT:
5835 case UNGT: case UNGE:
5836 case UNLT: case UNLE:
5837 case UNORDERED: case ORDERED:
5838 /* If the first operand is a condition code, we can't do anything
5839 with it. */
5840 if (GET_CODE (XEXP (x, 0)) == COMPARE
5841 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5842 && ! CC0_P (XEXP (x, 0))))
5844 rtx op0 = XEXP (x, 0);
5845 rtx op1 = XEXP (x, 1);
5846 enum rtx_code new_code;
5848 if (GET_CODE (op0) == COMPARE)
5849 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5851 /* Simplify our comparison, if possible. */
5852 new_code = simplify_comparison (code, &op0, &op1);
5854 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5855 if only the low-order bit is possibly nonzero in X (such as when
5856 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5857 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5858 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5859 (plus X 1).
5861 Remove any ZERO_EXTRACT we made when thinking this was a
5862 comparison. It may now be simpler to use, e.g., an AND. If a
5863 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5864 the call to make_compound_operation in the SET case.
5866 Don't apply these optimizations if the caller would
5867 prefer a comparison rather than a value.
5868 E.g., for the condition in an IF_THEN_ELSE most targets need
5869 an explicit comparison. */
5871 if (in_cond)
5874 else if (STORE_FLAG_VALUE == 1
5875 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5876 && op1 == const0_rtx
5877 && mode == GET_MODE (op0)
5878 && nonzero_bits (op0, mode) == 1)
5879 return gen_lowpart (mode,
5880 expand_compound_operation (op0));
5882 else if (STORE_FLAG_VALUE == 1
5883 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5884 && op1 == const0_rtx
5885 && mode == GET_MODE (op0)
5886 && (num_sign_bit_copies (op0, mode)
5887 == GET_MODE_PRECISION (mode)))
5889 op0 = expand_compound_operation (op0);
5890 return simplify_gen_unary (NEG, mode,
5891 gen_lowpart (mode, op0),
5892 mode);
5895 else if (STORE_FLAG_VALUE == 1
5896 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5897 && op1 == const0_rtx
5898 && mode == GET_MODE (op0)
5899 && nonzero_bits (op0, mode) == 1)
5901 op0 = expand_compound_operation (op0);
5902 return simplify_gen_binary (XOR, mode,
5903 gen_lowpart (mode, op0),
5904 const1_rtx);
5907 else if (STORE_FLAG_VALUE == 1
5908 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5909 && op1 == const0_rtx
5910 && mode == GET_MODE (op0)
5911 && (num_sign_bit_copies (op0, mode)
5912 == GET_MODE_PRECISION (mode)))
5914 op0 = expand_compound_operation (op0);
5915 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5918 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5919 those above. */
5920 if (in_cond)
5923 else if (STORE_FLAG_VALUE == -1
5924 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5925 && op1 == const0_rtx
5926 && mode == GET_MODE (op0)
5927 && (num_sign_bit_copies (op0, mode)
5928 == GET_MODE_PRECISION (mode)))
5929 return gen_lowpart (mode,
5930 expand_compound_operation (op0));
5932 else if (STORE_FLAG_VALUE == -1
5933 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5934 && op1 == const0_rtx
5935 && mode == GET_MODE (op0)
5936 && nonzero_bits (op0, mode) == 1)
5938 op0 = expand_compound_operation (op0);
5939 return simplify_gen_unary (NEG, mode,
5940 gen_lowpart (mode, op0),
5941 mode);
5944 else if (STORE_FLAG_VALUE == -1
5945 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5946 && op1 == const0_rtx
5947 && mode == GET_MODE (op0)
5948 && (num_sign_bit_copies (op0, mode)
5949 == GET_MODE_PRECISION (mode)))
5951 op0 = expand_compound_operation (op0);
5952 return simplify_gen_unary (NOT, mode,
5953 gen_lowpart (mode, op0),
5954 mode);
5957 /* If X is 0/1, (eq X 0) is X-1. */
5958 else if (STORE_FLAG_VALUE == -1
5959 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5960 && op1 == const0_rtx
5961 && mode == GET_MODE (op0)
5962 && nonzero_bits (op0, mode) == 1)
5964 op0 = expand_compound_operation (op0);
5965 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5968 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5969 one bit that might be nonzero, we can convert (ne x 0) to
5970 (ashift x c) where C puts the bit in the sign bit. Remove any
5971 AND with STORE_FLAG_VALUE when we are done, since we are only
5972 going to test the sign bit. */
5973 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5974 && HWI_COMPUTABLE_MODE_P (mode)
5975 && val_signbit_p (mode, STORE_FLAG_VALUE)
5976 && op1 == const0_rtx
5977 && mode == GET_MODE (op0)
5978 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5980 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5981 expand_compound_operation (op0),
5982 GET_MODE_PRECISION (mode) - 1 - i);
5983 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5984 return XEXP (x, 0);
5985 else
5986 return x;
5989 /* If the code changed, return a whole new comparison.
5990 We also need to avoid using SUBST in cases where
5991 simplify_comparison has widened a comparison with a CONST_INT,
5992 since in that case the wider CONST_INT may fail the sanity
5993 checks in do_SUBST. */
5994 if (new_code != code
5995 || (CONST_INT_P (op1)
5996 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5997 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5998 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6000 /* Otherwise, keep this operation, but maybe change its operands.
6001 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6002 SUBST (XEXP (x, 0), op0);
6003 SUBST (XEXP (x, 1), op1);
6005 break;
6007 case IF_THEN_ELSE:
6008 return simplify_if_then_else (x);
6010 case ZERO_EXTRACT:
6011 case SIGN_EXTRACT:
6012 case ZERO_EXTEND:
6013 case SIGN_EXTEND:
6014 /* If we are processing SET_DEST, we are done. */
6015 if (in_dest)
6016 return x;
6018 return expand_compound_operation (x);
6020 case SET:
6021 return simplify_set (x);
6023 case AND:
6024 case IOR:
6025 return simplify_logical (x);
6027 case ASHIFT:
6028 case LSHIFTRT:
6029 case ASHIFTRT:
6030 case ROTATE:
6031 case ROTATERT:
6032 /* If this is a shift by a constant amount, simplify it. */
6033 if (CONST_INT_P (XEXP (x, 1)))
6034 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6035 INTVAL (XEXP (x, 1)));
6037 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6038 SUBST (XEXP (x, 1),
6039 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6040 ((unsigned HOST_WIDE_INT) 1
6041 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6042 - 1,
6043 0));
6044 break;
6046 default:
6047 break;
6050 return x;
6053 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6055 static rtx
6056 simplify_if_then_else (rtx x)
6058 machine_mode mode = GET_MODE (x);
6059 rtx cond = XEXP (x, 0);
6060 rtx true_rtx = XEXP (x, 1);
6061 rtx false_rtx = XEXP (x, 2);
6062 enum rtx_code true_code = GET_CODE (cond);
6063 int comparison_p = COMPARISON_P (cond);
6064 rtx temp;
6065 int i;
6066 enum rtx_code false_code;
6067 rtx reversed;
6069 /* Simplify storing of the truth value. */
6070 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6071 return simplify_gen_relational (true_code, mode, VOIDmode,
6072 XEXP (cond, 0), XEXP (cond, 1));
6074 /* Also when the truth value has to be reversed. */
6075 if (comparison_p
6076 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6077 && (reversed = reversed_comparison (cond, mode)))
6078 return reversed;
6080 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6081 in it is being compared against certain values. Get the true and false
6082 comparisons and see if that says anything about the value of each arm. */
6084 if (comparison_p
6085 && ((false_code = reversed_comparison_code (cond, NULL))
6086 != UNKNOWN)
6087 && REG_P (XEXP (cond, 0)))
6089 HOST_WIDE_INT nzb;
6090 rtx from = XEXP (cond, 0);
6091 rtx true_val = XEXP (cond, 1);
6092 rtx false_val = true_val;
6093 int swapped = 0;
6095 /* If FALSE_CODE is EQ, swap the codes and arms. */
6097 if (false_code == EQ)
6099 swapped = 1, true_code = EQ, false_code = NE;
6100 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6103 /* If we are comparing against zero and the expression being tested has
6104 only a single bit that might be nonzero, that is its value when it is
6105 not equal to zero. Similarly if it is known to be -1 or 0. */
6107 if (true_code == EQ && true_val == const0_rtx
6108 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6110 false_code = EQ;
6111 false_val = gen_int_mode (nzb, GET_MODE (from));
6113 else if (true_code == EQ && true_val == const0_rtx
6114 && (num_sign_bit_copies (from, GET_MODE (from))
6115 == GET_MODE_PRECISION (GET_MODE (from))))
6117 false_code = EQ;
6118 false_val = constm1_rtx;
6121 /* Now simplify an arm if we know the value of the register in the
6122 branch and it is used in the arm. Be careful due to the potential
6123 of locally-shared RTL. */
6125 if (reg_mentioned_p (from, true_rtx))
6126 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6127 from, true_val),
6128 pc_rtx, pc_rtx, 0, 0, 0);
6129 if (reg_mentioned_p (from, false_rtx))
6130 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6131 from, false_val),
6132 pc_rtx, pc_rtx, 0, 0, 0);
6134 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6135 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6137 true_rtx = XEXP (x, 1);
6138 false_rtx = XEXP (x, 2);
6139 true_code = GET_CODE (cond);
6142 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6143 reversed, do so to avoid needing two sets of patterns for
6144 subtract-and-branch insns. Similarly if we have a constant in the true
6145 arm, the false arm is the same as the first operand of the comparison, or
6146 the false arm is more complicated than the true arm. */
6148 if (comparison_p
6149 && reversed_comparison_code (cond, NULL) != UNKNOWN
6150 && (true_rtx == pc_rtx
6151 || (CONSTANT_P (true_rtx)
6152 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6153 || true_rtx == const0_rtx
6154 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6155 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6156 && !OBJECT_P (false_rtx))
6157 || reg_mentioned_p (true_rtx, false_rtx)
6158 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6160 true_code = reversed_comparison_code (cond, NULL);
6161 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6162 SUBST (XEXP (x, 1), false_rtx);
6163 SUBST (XEXP (x, 2), true_rtx);
6165 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6166 cond = XEXP (x, 0);
6168 /* It is possible that the conditional has been simplified out. */
6169 true_code = GET_CODE (cond);
6170 comparison_p = COMPARISON_P (cond);
6173 /* If the two arms are identical, we don't need the comparison. */
6175 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6176 return true_rtx;
6178 /* Convert a == b ? b : a to "a". */
6179 if (true_code == EQ && ! side_effects_p (cond)
6180 && !HONOR_NANS (mode)
6181 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6182 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6183 return false_rtx;
6184 else if (true_code == NE && ! side_effects_p (cond)
6185 && !HONOR_NANS (mode)
6186 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6187 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6188 return true_rtx;
6190 /* Look for cases where we have (abs x) or (neg (abs X)). */
6192 if (GET_MODE_CLASS (mode) == MODE_INT
6193 && comparison_p
6194 && XEXP (cond, 1) == const0_rtx
6195 && GET_CODE (false_rtx) == NEG
6196 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6197 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6198 && ! side_effects_p (true_rtx))
6199 switch (true_code)
6201 case GT:
6202 case GE:
6203 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6204 case LT:
6205 case LE:
6206 return
6207 simplify_gen_unary (NEG, mode,
6208 simplify_gen_unary (ABS, mode, true_rtx, mode),
6209 mode);
6210 default:
6211 break;
6214 /* Look for MIN or MAX. */
6216 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6217 && comparison_p
6218 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6219 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6220 && ! side_effects_p (cond))
6221 switch (true_code)
6223 case GE:
6224 case GT:
6225 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6226 case LE:
6227 case LT:
6228 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6229 case GEU:
6230 case GTU:
6231 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6232 case LEU:
6233 case LTU:
6234 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6235 default:
6236 break;
6239 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6240 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6241 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6242 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6243 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6244 neither 1 or -1, but it isn't worth checking for. */
6246 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6247 && comparison_p
6248 && GET_MODE_CLASS (mode) == MODE_INT
6249 && ! side_effects_p (x))
6251 rtx t = make_compound_operation (true_rtx, SET);
6252 rtx f = make_compound_operation (false_rtx, SET);
6253 rtx cond_op0 = XEXP (cond, 0);
6254 rtx cond_op1 = XEXP (cond, 1);
6255 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6256 machine_mode m = mode;
6257 rtx z = 0, c1 = NULL_RTX;
6259 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6260 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6261 || GET_CODE (t) == ASHIFT
6262 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6263 && rtx_equal_p (XEXP (t, 0), f))
6264 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6266 /* If an identity-zero op is commutative, check whether there
6267 would be a match if we swapped the operands. */
6268 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6269 || GET_CODE (t) == XOR)
6270 && rtx_equal_p (XEXP (t, 1), f))
6271 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6272 else if (GET_CODE (t) == SIGN_EXTEND
6273 && (GET_CODE (XEXP (t, 0)) == PLUS
6274 || GET_CODE (XEXP (t, 0)) == MINUS
6275 || GET_CODE (XEXP (t, 0)) == IOR
6276 || GET_CODE (XEXP (t, 0)) == XOR
6277 || GET_CODE (XEXP (t, 0)) == ASHIFT
6278 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6279 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6280 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6281 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6282 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6283 && (num_sign_bit_copies (f, GET_MODE (f))
6284 > (unsigned int)
6285 (GET_MODE_PRECISION (mode)
6286 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6288 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6289 extend_op = SIGN_EXTEND;
6290 m = GET_MODE (XEXP (t, 0));
6292 else if (GET_CODE (t) == SIGN_EXTEND
6293 && (GET_CODE (XEXP (t, 0)) == PLUS
6294 || GET_CODE (XEXP (t, 0)) == IOR
6295 || GET_CODE (XEXP (t, 0)) == XOR)
6296 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6297 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6298 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6299 && (num_sign_bit_copies (f, GET_MODE (f))
6300 > (unsigned int)
6301 (GET_MODE_PRECISION (mode)
6302 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6304 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6305 extend_op = SIGN_EXTEND;
6306 m = GET_MODE (XEXP (t, 0));
6308 else if (GET_CODE (t) == ZERO_EXTEND
6309 && (GET_CODE (XEXP (t, 0)) == PLUS
6310 || GET_CODE (XEXP (t, 0)) == MINUS
6311 || GET_CODE (XEXP (t, 0)) == IOR
6312 || GET_CODE (XEXP (t, 0)) == XOR
6313 || GET_CODE (XEXP (t, 0)) == ASHIFT
6314 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6315 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6316 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6317 && HWI_COMPUTABLE_MODE_P (mode)
6318 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6319 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6320 && ((nonzero_bits (f, GET_MODE (f))
6321 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6322 == 0))
6324 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6325 extend_op = ZERO_EXTEND;
6326 m = GET_MODE (XEXP (t, 0));
6328 else if (GET_CODE (t) == ZERO_EXTEND
6329 && (GET_CODE (XEXP (t, 0)) == PLUS
6330 || GET_CODE (XEXP (t, 0)) == IOR
6331 || GET_CODE (XEXP (t, 0)) == XOR)
6332 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6333 && HWI_COMPUTABLE_MODE_P (mode)
6334 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6335 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6336 && ((nonzero_bits (f, GET_MODE (f))
6337 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6338 == 0))
6340 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6341 extend_op = ZERO_EXTEND;
6342 m = GET_MODE (XEXP (t, 0));
6345 if (z)
6347 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6348 cond_op0, cond_op1),
6349 pc_rtx, pc_rtx, 0, 0, 0);
6350 temp = simplify_gen_binary (MULT, m, temp,
6351 simplify_gen_binary (MULT, m, c1,
6352 const_true_rtx));
6353 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6354 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6356 if (extend_op != UNKNOWN)
6357 temp = simplify_gen_unary (extend_op, mode, temp, m);
6359 return temp;
6363 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6364 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6365 negation of a single bit, we can convert this operation to a shift. We
6366 can actually do this more generally, but it doesn't seem worth it. */
6368 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6369 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6370 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6371 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6372 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6373 == GET_MODE_PRECISION (mode))
6374 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6375 return
6376 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6377 gen_lowpart (mode, XEXP (cond, 0)), i);
6379 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6380 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6381 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6382 && GET_MODE (XEXP (cond, 0)) == mode
6383 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6384 == nonzero_bits (XEXP (cond, 0), mode)
6385 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6386 return XEXP (cond, 0);
6388 return x;
6391 /* Simplify X, a SET expression. Return the new expression. */
6393 static rtx
6394 simplify_set (rtx x)
6396 rtx src = SET_SRC (x);
6397 rtx dest = SET_DEST (x);
6398 machine_mode mode
6399 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6400 rtx_insn *other_insn;
6401 rtx *cc_use;
6403 /* (set (pc) (return)) gets written as (return). */
6404 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6405 return src;
6407 /* Now that we know for sure which bits of SRC we are using, see if we can
6408 simplify the expression for the object knowing that we only need the
6409 low-order bits. */
6411 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6413 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6414 SUBST (SET_SRC (x), src);
6417 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6418 the comparison result and try to simplify it unless we already have used
6419 undobuf.other_insn. */
6420 if ((GET_MODE_CLASS (mode) == MODE_CC
6421 || GET_CODE (src) == COMPARE
6422 || CC0_P (dest))
6423 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6424 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6425 && COMPARISON_P (*cc_use)
6426 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6428 enum rtx_code old_code = GET_CODE (*cc_use);
6429 enum rtx_code new_code;
6430 rtx op0, op1, tmp;
6431 int other_changed = 0;
6432 rtx inner_compare = NULL_RTX;
6433 machine_mode compare_mode = GET_MODE (dest);
6435 if (GET_CODE (src) == COMPARE)
6437 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6438 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6440 inner_compare = op0;
6441 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6444 else
6445 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6447 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6448 op0, op1);
6449 if (!tmp)
6450 new_code = old_code;
6451 else if (!CONSTANT_P (tmp))
6453 new_code = GET_CODE (tmp);
6454 op0 = XEXP (tmp, 0);
6455 op1 = XEXP (tmp, 1);
6457 else
6459 rtx pat = PATTERN (other_insn);
6460 undobuf.other_insn = other_insn;
6461 SUBST (*cc_use, tmp);
6463 /* Attempt to simplify CC user. */
6464 if (GET_CODE (pat) == SET)
6466 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6467 if (new_rtx != NULL_RTX)
6468 SUBST (SET_SRC (pat), new_rtx);
6471 /* Convert X into a no-op move. */
6472 SUBST (SET_DEST (x), pc_rtx);
6473 SUBST (SET_SRC (x), pc_rtx);
6474 return x;
6477 /* Simplify our comparison, if possible. */
6478 new_code = simplify_comparison (new_code, &op0, &op1);
6480 #ifdef SELECT_CC_MODE
6481 /* If this machine has CC modes other than CCmode, check to see if we
6482 need to use a different CC mode here. */
6483 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6484 compare_mode = GET_MODE (op0);
6485 else if (inner_compare
6486 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6487 && new_code == old_code
6488 && op0 == XEXP (inner_compare, 0)
6489 && op1 == XEXP (inner_compare, 1))
6490 compare_mode = GET_MODE (inner_compare);
6491 else
6492 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6494 #ifndef HAVE_cc0
6495 /* If the mode changed, we have to change SET_DEST, the mode in the
6496 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6497 a hard register, just build new versions with the proper mode. If it
6498 is a pseudo, we lose unless it is only time we set the pseudo, in
6499 which case we can safely change its mode. */
6500 if (compare_mode != GET_MODE (dest))
6502 if (can_change_dest_mode (dest, 0, compare_mode))
6504 unsigned int regno = REGNO (dest);
6505 rtx new_dest;
6507 if (regno < FIRST_PSEUDO_REGISTER)
6508 new_dest = gen_rtx_REG (compare_mode, regno);
6509 else
6511 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6512 new_dest = regno_reg_rtx[regno];
6515 SUBST (SET_DEST (x), new_dest);
6516 SUBST (XEXP (*cc_use, 0), new_dest);
6517 other_changed = 1;
6519 dest = new_dest;
6522 #endif /* cc0 */
6523 #endif /* SELECT_CC_MODE */
6525 /* If the code changed, we have to build a new comparison in
6526 undobuf.other_insn. */
6527 if (new_code != old_code)
6529 int other_changed_previously = other_changed;
6530 unsigned HOST_WIDE_INT mask;
6531 rtx old_cc_use = *cc_use;
6533 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6534 dest, const0_rtx));
6535 other_changed = 1;
6537 /* If the only change we made was to change an EQ into an NE or
6538 vice versa, OP0 has only one bit that might be nonzero, and OP1
6539 is zero, check if changing the user of the condition code will
6540 produce a valid insn. If it won't, we can keep the original code
6541 in that insn by surrounding our operation with an XOR. */
6543 if (((old_code == NE && new_code == EQ)
6544 || (old_code == EQ && new_code == NE))
6545 && ! other_changed_previously && op1 == const0_rtx
6546 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6547 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6549 rtx pat = PATTERN (other_insn), note = 0;
6551 if ((recog_for_combine (&pat, other_insn, &note) < 0
6552 && ! check_asm_operands (pat)))
6554 *cc_use = old_cc_use;
6555 other_changed = 0;
6557 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6558 gen_int_mode (mask,
6559 GET_MODE (op0)));
6564 if (other_changed)
6565 undobuf.other_insn = other_insn;
6567 /* Otherwise, if we didn't previously have a COMPARE in the
6568 correct mode, we need one. */
6569 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6571 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6572 src = SET_SRC (x);
6574 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6576 SUBST (SET_SRC (x), op0);
6577 src = SET_SRC (x);
6579 /* Otherwise, update the COMPARE if needed. */
6580 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6582 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6583 src = SET_SRC (x);
6586 else
6588 /* Get SET_SRC in a form where we have placed back any
6589 compound expressions. Then do the checks below. */
6590 src = make_compound_operation (src, SET);
6591 SUBST (SET_SRC (x), src);
6594 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6595 and X being a REG or (subreg (reg)), we may be able to convert this to
6596 (set (subreg:m2 x) (op)).
6598 We can always do this if M1 is narrower than M2 because that means that
6599 we only care about the low bits of the result.
6601 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6602 perform a narrower operation than requested since the high-order bits will
6603 be undefined. On machine where it is defined, this transformation is safe
6604 as long as M1 and M2 have the same number of words. */
6606 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6607 && !OBJECT_P (SUBREG_REG (src))
6608 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6609 / UNITS_PER_WORD)
6610 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6611 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6612 #ifndef WORD_REGISTER_OPERATIONS
6613 && (GET_MODE_SIZE (GET_MODE (src))
6614 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6615 #endif
6616 #ifdef CANNOT_CHANGE_MODE_CLASS
6617 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6618 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6619 GET_MODE (SUBREG_REG (src)),
6620 GET_MODE (src)))
6621 #endif
6622 && (REG_P (dest)
6623 || (GET_CODE (dest) == SUBREG
6624 && REG_P (SUBREG_REG (dest)))))
6626 SUBST (SET_DEST (x),
6627 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6628 dest));
6629 SUBST (SET_SRC (x), SUBREG_REG (src));
6631 src = SET_SRC (x), dest = SET_DEST (x);
6634 #ifdef HAVE_cc0
6635 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6636 in SRC. */
6637 if (dest == cc0_rtx
6638 && GET_CODE (src) == SUBREG
6639 && subreg_lowpart_p (src)
6640 && (GET_MODE_PRECISION (GET_MODE (src))
6641 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6643 rtx inner = SUBREG_REG (src);
6644 machine_mode inner_mode = GET_MODE (inner);
6646 /* Here we make sure that we don't have a sign bit on. */
6647 if (val_signbit_known_clear_p (GET_MODE (src),
6648 nonzero_bits (inner, inner_mode)))
6650 SUBST (SET_SRC (x), inner);
6651 src = SET_SRC (x);
6654 #endif
6656 #ifdef LOAD_EXTEND_OP
6657 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6658 would require a paradoxical subreg. Replace the subreg with a
6659 zero_extend to avoid the reload that would otherwise be required. */
6661 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6662 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6663 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6664 && SUBREG_BYTE (src) == 0
6665 && paradoxical_subreg_p (src)
6666 && MEM_P (SUBREG_REG (src)))
6668 SUBST (SET_SRC (x),
6669 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6670 GET_MODE (src), SUBREG_REG (src)));
6672 src = SET_SRC (x);
6674 #endif
6676 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6677 are comparing an item known to be 0 or -1 against 0, use a logical
6678 operation instead. Check for one of the arms being an IOR of the other
6679 arm with some value. We compute three terms to be IOR'ed together. In
6680 practice, at most two will be nonzero. Then we do the IOR's. */
6682 if (GET_CODE (dest) != PC
6683 && GET_CODE (src) == IF_THEN_ELSE
6684 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6685 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6686 && XEXP (XEXP (src, 0), 1) == const0_rtx
6687 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6688 #ifdef HAVE_conditional_move
6689 && ! can_conditionally_move_p (GET_MODE (src))
6690 #endif
6691 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6692 GET_MODE (XEXP (XEXP (src, 0), 0)))
6693 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6694 && ! side_effects_p (src))
6696 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6697 ? XEXP (src, 1) : XEXP (src, 2));
6698 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6699 ? XEXP (src, 2) : XEXP (src, 1));
6700 rtx term1 = const0_rtx, term2, term3;
6702 if (GET_CODE (true_rtx) == IOR
6703 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6704 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6705 else if (GET_CODE (true_rtx) == IOR
6706 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6707 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6708 else if (GET_CODE (false_rtx) == IOR
6709 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6710 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6711 else if (GET_CODE (false_rtx) == IOR
6712 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6713 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6715 term2 = simplify_gen_binary (AND, GET_MODE (src),
6716 XEXP (XEXP (src, 0), 0), true_rtx);
6717 term3 = simplify_gen_binary (AND, GET_MODE (src),
6718 simplify_gen_unary (NOT, GET_MODE (src),
6719 XEXP (XEXP (src, 0), 0),
6720 GET_MODE (src)),
6721 false_rtx);
6723 SUBST (SET_SRC (x),
6724 simplify_gen_binary (IOR, GET_MODE (src),
6725 simplify_gen_binary (IOR, GET_MODE (src),
6726 term1, term2),
6727 term3));
6729 src = SET_SRC (x);
6732 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6733 whole thing fail. */
6734 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6735 return src;
6736 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6737 return dest;
6738 else
6739 /* Convert this into a field assignment operation, if possible. */
6740 return make_field_assignment (x);
6743 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6744 result. */
6746 static rtx
6747 simplify_logical (rtx x)
6749 machine_mode mode = GET_MODE (x);
6750 rtx op0 = XEXP (x, 0);
6751 rtx op1 = XEXP (x, 1);
6753 switch (GET_CODE (x))
6755 case AND:
6756 /* We can call simplify_and_const_int only if we don't lose
6757 any (sign) bits when converting INTVAL (op1) to
6758 "unsigned HOST_WIDE_INT". */
6759 if (CONST_INT_P (op1)
6760 && (HWI_COMPUTABLE_MODE_P (mode)
6761 || INTVAL (op1) > 0))
6763 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6764 if (GET_CODE (x) != AND)
6765 return x;
6767 op0 = XEXP (x, 0);
6768 op1 = XEXP (x, 1);
6771 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6772 apply the distributive law and then the inverse distributive
6773 law to see if things simplify. */
6774 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6776 rtx result = distribute_and_simplify_rtx (x, 0);
6777 if (result)
6778 return result;
6780 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6782 rtx result = distribute_and_simplify_rtx (x, 1);
6783 if (result)
6784 return result;
6786 break;
6788 case IOR:
6789 /* If we have (ior (and A B) C), apply the distributive law and then
6790 the inverse distributive law to see if things simplify. */
6792 if (GET_CODE (op0) == AND)
6794 rtx result = distribute_and_simplify_rtx (x, 0);
6795 if (result)
6796 return result;
6799 if (GET_CODE (op1) == AND)
6801 rtx result = distribute_and_simplify_rtx (x, 1);
6802 if (result)
6803 return result;
6805 break;
6807 default:
6808 gcc_unreachable ();
6811 return x;
6814 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6815 operations" because they can be replaced with two more basic operations.
6816 ZERO_EXTEND is also considered "compound" because it can be replaced with
6817 an AND operation, which is simpler, though only one operation.
6819 The function expand_compound_operation is called with an rtx expression
6820 and will convert it to the appropriate shifts and AND operations,
6821 simplifying at each stage.
6823 The function make_compound_operation is called to convert an expression
6824 consisting of shifts and ANDs into the equivalent compound expression.
6825 It is the inverse of this function, loosely speaking. */
6827 static rtx
6828 expand_compound_operation (rtx x)
6830 unsigned HOST_WIDE_INT pos = 0, len;
6831 int unsignedp = 0;
6832 unsigned int modewidth;
6833 rtx tem;
6835 switch (GET_CODE (x))
6837 case ZERO_EXTEND:
6838 unsignedp = 1;
6839 case SIGN_EXTEND:
6840 /* We can't necessarily use a const_int for a multiword mode;
6841 it depends on implicitly extending the value.
6842 Since we don't know the right way to extend it,
6843 we can't tell whether the implicit way is right.
6845 Even for a mode that is no wider than a const_int,
6846 we can't win, because we need to sign extend one of its bits through
6847 the rest of it, and we don't know which bit. */
6848 if (CONST_INT_P (XEXP (x, 0)))
6849 return x;
6851 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6852 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6853 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6854 reloaded. If not for that, MEM's would very rarely be safe.
6856 Reject MODEs bigger than a word, because we might not be able
6857 to reference a two-register group starting with an arbitrary register
6858 (and currently gen_lowpart might crash for a SUBREG). */
6860 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6861 return x;
6863 /* Reject MODEs that aren't scalar integers because turning vector
6864 or complex modes into shifts causes problems. */
6866 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6867 return x;
6869 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6870 /* If the inner object has VOIDmode (the only way this can happen
6871 is if it is an ASM_OPERANDS), we can't do anything since we don't
6872 know how much masking to do. */
6873 if (len == 0)
6874 return x;
6876 break;
6878 case ZERO_EXTRACT:
6879 unsignedp = 1;
6881 /* ... fall through ... */
6883 case SIGN_EXTRACT:
6884 /* If the operand is a CLOBBER, just return it. */
6885 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6886 return XEXP (x, 0);
6888 if (!CONST_INT_P (XEXP (x, 1))
6889 || !CONST_INT_P (XEXP (x, 2))
6890 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6891 return x;
6893 /* Reject MODEs that aren't scalar integers because turning vector
6894 or complex modes into shifts causes problems. */
6896 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6897 return x;
6899 len = INTVAL (XEXP (x, 1));
6900 pos = INTVAL (XEXP (x, 2));
6902 /* This should stay within the object being extracted, fail otherwise. */
6903 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6904 return x;
6906 if (BITS_BIG_ENDIAN)
6907 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6909 break;
6911 default:
6912 return x;
6914 /* Convert sign extension to zero extension, if we know that the high
6915 bit is not set, as this is easier to optimize. It will be converted
6916 back to cheaper alternative in make_extraction. */
6917 if (GET_CODE (x) == SIGN_EXTEND
6918 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6919 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6920 & ~(((unsigned HOST_WIDE_INT)
6921 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6922 >> 1))
6923 == 0)))
6925 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6926 rtx temp2 = expand_compound_operation (temp);
6928 /* Make sure this is a profitable operation. */
6929 if (set_src_cost (x, optimize_this_for_speed_p)
6930 > set_src_cost (temp2, optimize_this_for_speed_p))
6931 return temp2;
6932 else if (set_src_cost (x, optimize_this_for_speed_p)
6933 > set_src_cost (temp, optimize_this_for_speed_p))
6934 return temp;
6935 else
6936 return x;
6939 /* We can optimize some special cases of ZERO_EXTEND. */
6940 if (GET_CODE (x) == ZERO_EXTEND)
6942 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6943 know that the last value didn't have any inappropriate bits
6944 set. */
6945 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6946 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6947 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6948 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6949 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6950 return XEXP (XEXP (x, 0), 0);
6952 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6953 if (GET_CODE (XEXP (x, 0)) == SUBREG
6954 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6955 && subreg_lowpart_p (XEXP (x, 0))
6956 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6957 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6958 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6959 return SUBREG_REG (XEXP (x, 0));
6961 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6962 is a comparison and STORE_FLAG_VALUE permits. This is like
6963 the first case, but it works even when GET_MODE (x) is larger
6964 than HOST_WIDE_INT. */
6965 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6966 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6967 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6968 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6969 <= HOST_BITS_PER_WIDE_INT)
6970 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6971 return XEXP (XEXP (x, 0), 0);
6973 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6974 if (GET_CODE (XEXP (x, 0)) == SUBREG
6975 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6976 && subreg_lowpart_p (XEXP (x, 0))
6977 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6978 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6979 <= HOST_BITS_PER_WIDE_INT)
6980 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6981 return SUBREG_REG (XEXP (x, 0));
6985 /* If we reach here, we want to return a pair of shifts. The inner
6986 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6987 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6988 logical depending on the value of UNSIGNEDP.
6990 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6991 converted into an AND of a shift.
6993 We must check for the case where the left shift would have a negative
6994 count. This can happen in a case like (x >> 31) & 255 on machines
6995 that can't shift by a constant. On those machines, we would first
6996 combine the shift with the AND to produce a variable-position
6997 extraction. Then the constant of 31 would be substituted in
6998 to produce such a position. */
7000 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7001 if (modewidth >= pos + len)
7003 machine_mode mode = GET_MODE (x);
7004 tem = gen_lowpart (mode, XEXP (x, 0));
7005 if (!tem || GET_CODE (tem) == CLOBBER)
7006 return x;
7007 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7008 tem, modewidth - pos - len);
7009 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7010 mode, tem, modewidth - len);
7012 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7013 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7014 simplify_shift_const (NULL_RTX, LSHIFTRT,
7015 GET_MODE (x),
7016 XEXP (x, 0), pos),
7017 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7018 else
7019 /* Any other cases we can't handle. */
7020 return x;
7022 /* If we couldn't do this for some reason, return the original
7023 expression. */
7024 if (GET_CODE (tem) == CLOBBER)
7025 return x;
7027 return tem;
7030 /* X is a SET which contains an assignment of one object into
7031 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7032 or certain SUBREGS). If possible, convert it into a series of
7033 logical operations.
7035 We half-heartedly support variable positions, but do not at all
7036 support variable lengths. */
7038 static const_rtx
7039 expand_field_assignment (const_rtx x)
7041 rtx inner;
7042 rtx pos; /* Always counts from low bit. */
7043 int len;
7044 rtx mask, cleared, masked;
7045 machine_mode compute_mode;
7047 /* Loop until we find something we can't simplify. */
7048 while (1)
7050 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7051 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7053 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7054 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7055 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7057 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7058 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7060 inner = XEXP (SET_DEST (x), 0);
7061 len = INTVAL (XEXP (SET_DEST (x), 1));
7062 pos = XEXP (SET_DEST (x), 2);
7064 /* A constant position should stay within the width of INNER. */
7065 if (CONST_INT_P (pos)
7066 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7067 break;
7069 if (BITS_BIG_ENDIAN)
7071 if (CONST_INT_P (pos))
7072 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7073 - INTVAL (pos));
7074 else if (GET_CODE (pos) == MINUS
7075 && CONST_INT_P (XEXP (pos, 1))
7076 && (INTVAL (XEXP (pos, 1))
7077 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7078 /* If position is ADJUST - X, new position is X. */
7079 pos = XEXP (pos, 0);
7080 else
7082 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7083 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7084 gen_int_mode (prec - len,
7085 GET_MODE (pos)),
7086 pos);
7091 /* A SUBREG between two modes that occupy the same numbers of words
7092 can be done by moving the SUBREG to the source. */
7093 else if (GET_CODE (SET_DEST (x)) == SUBREG
7094 /* We need SUBREGs to compute nonzero_bits properly. */
7095 && nonzero_sign_valid
7096 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7097 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7098 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7099 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7101 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7102 gen_lowpart
7103 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7104 SET_SRC (x)));
7105 continue;
7107 else
7108 break;
7110 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7111 inner = SUBREG_REG (inner);
7113 compute_mode = GET_MODE (inner);
7115 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7116 if (! SCALAR_INT_MODE_P (compute_mode))
7118 machine_mode imode;
7120 /* Don't do anything for vector or complex integral types. */
7121 if (! FLOAT_MODE_P (compute_mode))
7122 break;
7124 /* Try to find an integral mode to pun with. */
7125 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7126 if (imode == BLKmode)
7127 break;
7129 compute_mode = imode;
7130 inner = gen_lowpart (imode, inner);
7133 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7134 if (len >= HOST_BITS_PER_WIDE_INT)
7135 break;
7137 /* Now compute the equivalent expression. Make a copy of INNER
7138 for the SET_DEST in case it is a MEM into which we will substitute;
7139 we don't want shared RTL in that case. */
7140 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7141 compute_mode);
7142 cleared = simplify_gen_binary (AND, compute_mode,
7143 simplify_gen_unary (NOT, compute_mode,
7144 simplify_gen_binary (ASHIFT,
7145 compute_mode,
7146 mask, pos),
7147 compute_mode),
7148 inner);
7149 masked = simplify_gen_binary (ASHIFT, compute_mode,
7150 simplify_gen_binary (
7151 AND, compute_mode,
7152 gen_lowpart (compute_mode, SET_SRC (x)),
7153 mask),
7154 pos);
7156 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7157 simplify_gen_binary (IOR, compute_mode,
7158 cleared, masked));
7161 return x;
7164 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7165 it is an RTX that represents the (variable) starting position; otherwise,
7166 POS is the (constant) starting bit position. Both are counted from the LSB.
7168 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7170 IN_DEST is nonzero if this is a reference in the destination of a SET.
7171 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7172 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7173 be used.
7175 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7176 ZERO_EXTRACT should be built even for bits starting at bit 0.
7178 MODE is the desired mode of the result (if IN_DEST == 0).
7180 The result is an RTX for the extraction or NULL_RTX if the target
7181 can't handle it. */
7183 static rtx
7184 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7185 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7186 int in_dest, int in_compare)
7188 /* This mode describes the size of the storage area
7189 to fetch the overall value from. Within that, we
7190 ignore the POS lowest bits, etc. */
7191 machine_mode is_mode = GET_MODE (inner);
7192 machine_mode inner_mode;
7193 machine_mode wanted_inner_mode;
7194 machine_mode wanted_inner_reg_mode = word_mode;
7195 machine_mode pos_mode = word_mode;
7196 machine_mode extraction_mode = word_mode;
7197 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7198 rtx new_rtx = 0;
7199 rtx orig_pos_rtx = pos_rtx;
7200 HOST_WIDE_INT orig_pos;
7202 if (pos_rtx && CONST_INT_P (pos_rtx))
7203 pos = INTVAL (pos_rtx), pos_rtx = 0;
7205 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7207 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7208 consider just the QI as the memory to extract from.
7209 The subreg adds or removes high bits; its mode is
7210 irrelevant to the meaning of this extraction,
7211 since POS and LEN count from the lsb. */
7212 if (MEM_P (SUBREG_REG (inner)))
7213 is_mode = GET_MODE (SUBREG_REG (inner));
7214 inner = SUBREG_REG (inner);
7216 else if (GET_CODE (inner) == ASHIFT
7217 && CONST_INT_P (XEXP (inner, 1))
7218 && pos_rtx == 0 && pos == 0
7219 && len > UINTVAL (XEXP (inner, 1)))
7221 /* We're extracting the least significant bits of an rtx
7222 (ashift X (const_int C)), where LEN > C. Extract the
7223 least significant (LEN - C) bits of X, giving an rtx
7224 whose mode is MODE, then shift it left C times. */
7225 new_rtx = make_extraction (mode, XEXP (inner, 0),
7226 0, 0, len - INTVAL (XEXP (inner, 1)),
7227 unsignedp, in_dest, in_compare);
7228 if (new_rtx != 0)
7229 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7231 else if (GET_CODE (inner) == TRUNCATE)
7232 inner = XEXP (inner, 0);
7234 inner_mode = GET_MODE (inner);
7236 /* See if this can be done without an extraction. We never can if the
7237 width of the field is not the same as that of some integer mode. For
7238 registers, we can only avoid the extraction if the position is at the
7239 low-order bit and this is either not in the destination or we have the
7240 appropriate STRICT_LOW_PART operation available.
7242 For MEM, we can avoid an extract if the field starts on an appropriate
7243 boundary and we can change the mode of the memory reference. */
7245 if (tmode != BLKmode
7246 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7247 && !MEM_P (inner)
7248 && (inner_mode == tmode
7249 || !REG_P (inner)
7250 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7251 || reg_truncated_to_mode (tmode, inner))
7252 && (! in_dest
7253 || (REG_P (inner)
7254 && have_insn_for (STRICT_LOW_PART, tmode))))
7255 || (MEM_P (inner) && pos_rtx == 0
7256 && (pos
7257 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7258 : BITS_PER_UNIT)) == 0
7259 /* We can't do this if we are widening INNER_MODE (it
7260 may not be aligned, for one thing). */
7261 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7262 && (inner_mode == tmode
7263 || (! mode_dependent_address_p (XEXP (inner, 0),
7264 MEM_ADDR_SPACE (inner))
7265 && ! MEM_VOLATILE_P (inner))))))
7267 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7268 field. If the original and current mode are the same, we need not
7269 adjust the offset. Otherwise, we do if bytes big endian.
7271 If INNER is not a MEM, get a piece consisting of just the field
7272 of interest (in this case POS % BITS_PER_WORD must be 0). */
7274 if (MEM_P (inner))
7276 HOST_WIDE_INT offset;
7278 /* POS counts from lsb, but make OFFSET count in memory order. */
7279 if (BYTES_BIG_ENDIAN)
7280 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7281 else
7282 offset = pos / BITS_PER_UNIT;
7284 new_rtx = adjust_address_nv (inner, tmode, offset);
7286 else if (REG_P (inner))
7288 if (tmode != inner_mode)
7290 /* We can't call gen_lowpart in a DEST since we
7291 always want a SUBREG (see below) and it would sometimes
7292 return a new hard register. */
7293 if (pos || in_dest)
7295 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7297 if (WORDS_BIG_ENDIAN
7298 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7299 final_word = ((GET_MODE_SIZE (inner_mode)
7300 - GET_MODE_SIZE (tmode))
7301 / UNITS_PER_WORD) - final_word;
7303 final_word *= UNITS_PER_WORD;
7304 if (BYTES_BIG_ENDIAN &&
7305 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7306 final_word += (GET_MODE_SIZE (inner_mode)
7307 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7309 /* Avoid creating invalid subregs, for example when
7310 simplifying (x>>32)&255. */
7311 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7312 return NULL_RTX;
7314 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7316 else
7317 new_rtx = gen_lowpart (tmode, inner);
7319 else
7320 new_rtx = inner;
7322 else
7323 new_rtx = force_to_mode (inner, tmode,
7324 len >= HOST_BITS_PER_WIDE_INT
7325 ? ~(unsigned HOST_WIDE_INT) 0
7326 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7329 /* If this extraction is going into the destination of a SET,
7330 make a STRICT_LOW_PART unless we made a MEM. */
7332 if (in_dest)
7333 return (MEM_P (new_rtx) ? new_rtx
7334 : (GET_CODE (new_rtx) != SUBREG
7335 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7336 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7338 if (mode == tmode)
7339 return new_rtx;
7341 if (CONST_SCALAR_INT_P (new_rtx))
7342 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7343 mode, new_rtx, tmode);
7345 /* If we know that no extraneous bits are set, and that the high
7346 bit is not set, convert the extraction to the cheaper of
7347 sign and zero extension, that are equivalent in these cases. */
7348 if (flag_expensive_optimizations
7349 && (HWI_COMPUTABLE_MODE_P (tmode)
7350 && ((nonzero_bits (new_rtx, tmode)
7351 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7352 == 0)))
7354 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7355 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7357 /* Prefer ZERO_EXTENSION, since it gives more information to
7358 backends. */
7359 if (set_src_cost (temp, optimize_this_for_speed_p)
7360 <= set_src_cost (temp1, optimize_this_for_speed_p))
7361 return temp;
7362 return temp1;
7365 /* Otherwise, sign- or zero-extend unless we already are in the
7366 proper mode. */
7368 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7369 mode, new_rtx));
7372 /* Unless this is a COMPARE or we have a funny memory reference,
7373 don't do anything with zero-extending field extracts starting at
7374 the low-order bit since they are simple AND operations. */
7375 if (pos_rtx == 0 && pos == 0 && ! in_dest
7376 && ! in_compare && unsignedp)
7377 return 0;
7379 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7380 if the position is not a constant and the length is not 1. In all
7381 other cases, we would only be going outside our object in cases when
7382 an original shift would have been undefined. */
7383 if (MEM_P (inner)
7384 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7385 || (pos_rtx != 0 && len != 1)))
7386 return 0;
7388 enum extraction_pattern pattern = (in_dest ? EP_insv
7389 : unsignedp ? EP_extzv : EP_extv);
7391 /* If INNER is not from memory, we want it to have the mode of a register
7392 extraction pattern's structure operand, or word_mode if there is no
7393 such pattern. The same applies to extraction_mode and pos_mode
7394 and their respective operands.
7396 For memory, assume that the desired extraction_mode and pos_mode
7397 are the same as for a register operation, since at present we don't
7398 have named patterns for aligned memory structures. */
7399 struct extraction_insn insn;
7400 if (get_best_reg_extraction_insn (&insn, pattern,
7401 GET_MODE_BITSIZE (inner_mode), mode))
7403 wanted_inner_reg_mode = insn.struct_mode;
7404 pos_mode = insn.pos_mode;
7405 extraction_mode = insn.field_mode;
7408 /* Never narrow an object, since that might not be safe. */
7410 if (mode != VOIDmode
7411 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7412 extraction_mode = mode;
7414 if (!MEM_P (inner))
7415 wanted_inner_mode = wanted_inner_reg_mode;
7416 else
7418 /* Be careful not to go beyond the extracted object and maintain the
7419 natural alignment of the memory. */
7420 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7421 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7422 > GET_MODE_BITSIZE (wanted_inner_mode))
7424 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7425 gcc_assert (wanted_inner_mode != VOIDmode);
7429 orig_pos = pos;
7431 if (BITS_BIG_ENDIAN)
7433 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7434 BITS_BIG_ENDIAN style. If position is constant, compute new
7435 position. Otherwise, build subtraction.
7436 Note that POS is relative to the mode of the original argument.
7437 If it's a MEM we need to recompute POS relative to that.
7438 However, if we're extracting from (or inserting into) a register,
7439 we want to recompute POS relative to wanted_inner_mode. */
7440 int width = (MEM_P (inner)
7441 ? GET_MODE_BITSIZE (is_mode)
7442 : GET_MODE_BITSIZE (wanted_inner_mode));
7444 if (pos_rtx == 0)
7445 pos = width - len - pos;
7446 else
7447 pos_rtx
7448 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7449 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7450 pos_rtx);
7451 /* POS may be less than 0 now, but we check for that below.
7452 Note that it can only be less than 0 if !MEM_P (inner). */
7455 /* If INNER has a wider mode, and this is a constant extraction, try to
7456 make it smaller and adjust the byte to point to the byte containing
7457 the value. */
7458 if (wanted_inner_mode != VOIDmode
7459 && inner_mode != wanted_inner_mode
7460 && ! pos_rtx
7461 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7462 && MEM_P (inner)
7463 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7464 && ! MEM_VOLATILE_P (inner))
7466 int offset = 0;
7468 /* The computations below will be correct if the machine is big
7469 endian in both bits and bytes or little endian in bits and bytes.
7470 If it is mixed, we must adjust. */
7472 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7473 adjust OFFSET to compensate. */
7474 if (BYTES_BIG_ENDIAN
7475 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7476 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7478 /* We can now move to the desired byte. */
7479 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7480 * GET_MODE_SIZE (wanted_inner_mode);
7481 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7483 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7484 && is_mode != wanted_inner_mode)
7485 offset = (GET_MODE_SIZE (is_mode)
7486 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7488 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7491 /* If INNER is not memory, get it into the proper mode. If we are changing
7492 its mode, POS must be a constant and smaller than the size of the new
7493 mode. */
7494 else if (!MEM_P (inner))
7496 /* On the LHS, don't create paradoxical subregs implicitely truncating
7497 the register unless TRULY_NOOP_TRUNCATION. */
7498 if (in_dest
7499 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7500 wanted_inner_mode))
7501 return NULL_RTX;
7503 if (GET_MODE (inner) != wanted_inner_mode
7504 && (pos_rtx != 0
7505 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7506 return NULL_RTX;
7508 if (orig_pos < 0)
7509 return NULL_RTX;
7511 inner = force_to_mode (inner, wanted_inner_mode,
7512 pos_rtx
7513 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7514 ? ~(unsigned HOST_WIDE_INT) 0
7515 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7516 << orig_pos),
7520 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7521 have to zero extend. Otherwise, we can just use a SUBREG. */
7522 if (pos_rtx != 0
7523 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7525 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7526 GET_MODE (pos_rtx));
7528 /* If we know that no extraneous bits are set, and that the high
7529 bit is not set, convert extraction to cheaper one - either
7530 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7531 cases. */
7532 if (flag_expensive_optimizations
7533 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7534 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7535 & ~(((unsigned HOST_WIDE_INT)
7536 GET_MODE_MASK (GET_MODE (pos_rtx)))
7537 >> 1))
7538 == 0)))
7540 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7541 GET_MODE (pos_rtx));
7543 /* Prefer ZERO_EXTENSION, since it gives more information to
7544 backends. */
7545 if (set_src_cost (temp1, optimize_this_for_speed_p)
7546 < set_src_cost (temp, optimize_this_for_speed_p))
7547 temp = temp1;
7549 pos_rtx = temp;
7552 /* Make POS_RTX unless we already have it and it is correct. If we don't
7553 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7554 be a CONST_INT. */
7555 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7556 pos_rtx = orig_pos_rtx;
7558 else if (pos_rtx == 0)
7559 pos_rtx = GEN_INT (pos);
7561 /* Make the required operation. See if we can use existing rtx. */
7562 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7563 extraction_mode, inner, GEN_INT (len), pos_rtx);
7564 if (! in_dest)
7565 new_rtx = gen_lowpart (mode, new_rtx);
7567 return new_rtx;
7570 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7571 with any other operations in X. Return X without that shift if so. */
7573 static rtx
7574 extract_left_shift (rtx x, int count)
7576 enum rtx_code code = GET_CODE (x);
7577 machine_mode mode = GET_MODE (x);
7578 rtx tem;
7580 switch (code)
7582 case ASHIFT:
7583 /* This is the shift itself. If it is wide enough, we will return
7584 either the value being shifted if the shift count is equal to
7585 COUNT or a shift for the difference. */
7586 if (CONST_INT_P (XEXP (x, 1))
7587 && INTVAL (XEXP (x, 1)) >= count)
7588 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7589 INTVAL (XEXP (x, 1)) - count);
7590 break;
7592 case NEG: case NOT:
7593 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7594 return simplify_gen_unary (code, mode, tem, mode);
7596 break;
7598 case PLUS: case IOR: case XOR: case AND:
7599 /* If we can safely shift this constant and we find the inner shift,
7600 make a new operation. */
7601 if (CONST_INT_P (XEXP (x, 1))
7602 && (UINTVAL (XEXP (x, 1))
7603 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7604 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7606 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7607 return simplify_gen_binary (code, mode, tem,
7608 gen_int_mode (val, mode));
7610 break;
7612 default:
7613 break;
7616 return 0;
7619 /* Look at the expression rooted at X. Look for expressions
7620 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7621 Form these expressions.
7623 Return the new rtx, usually just X.
7625 Also, for machines like the VAX that don't have logical shift insns,
7626 try to convert logical to arithmetic shift operations in cases where
7627 they are equivalent. This undoes the canonicalizations to logical
7628 shifts done elsewhere.
7630 We try, as much as possible, to re-use rtl expressions to save memory.
7632 IN_CODE says what kind of expression we are processing. Normally, it is
7633 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7634 being kludges), it is MEM. When processing the arguments of a comparison
7635 or a COMPARE against zero, it is COMPARE. */
7638 make_compound_operation (rtx x, enum rtx_code in_code)
7640 enum rtx_code code = GET_CODE (x);
7641 machine_mode mode = GET_MODE (x);
7642 int mode_width = GET_MODE_PRECISION (mode);
7643 rtx rhs, lhs;
7644 enum rtx_code next_code;
7645 int i, j;
7646 rtx new_rtx = 0;
7647 rtx tem;
7648 const char *fmt;
7650 /* Select the code to be used in recursive calls. Once we are inside an
7651 address, we stay there. If we have a comparison, set to COMPARE,
7652 but once inside, go back to our default of SET. */
7654 next_code = (code == MEM ? MEM
7655 : ((code == PLUS || code == MINUS)
7656 && SCALAR_INT_MODE_P (mode)) ? MEM
7657 : ((code == COMPARE || COMPARISON_P (x))
7658 && XEXP (x, 1) == const0_rtx) ? COMPARE
7659 : in_code == COMPARE ? SET : in_code);
7661 /* Process depending on the code of this operation. If NEW is set
7662 nonzero, it will be returned. */
7664 switch (code)
7666 case ASHIFT:
7667 /* Convert shifts by constants into multiplications if inside
7668 an address. */
7669 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7670 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7671 && INTVAL (XEXP (x, 1)) >= 0
7672 && SCALAR_INT_MODE_P (mode))
7674 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7675 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7677 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7678 if (GET_CODE (new_rtx) == NEG)
7680 new_rtx = XEXP (new_rtx, 0);
7681 multval = -multval;
7683 multval = trunc_int_for_mode (multval, mode);
7684 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7686 break;
7688 case PLUS:
7689 lhs = XEXP (x, 0);
7690 rhs = XEXP (x, 1);
7691 lhs = make_compound_operation (lhs, next_code);
7692 rhs = make_compound_operation (rhs, next_code);
7693 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7694 && SCALAR_INT_MODE_P (mode))
7696 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7697 XEXP (lhs, 1));
7698 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7700 else if (GET_CODE (lhs) == MULT
7701 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7703 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7704 simplify_gen_unary (NEG, mode,
7705 XEXP (lhs, 1),
7706 mode));
7707 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7709 else
7711 SUBST (XEXP (x, 0), lhs);
7712 SUBST (XEXP (x, 1), rhs);
7713 goto maybe_swap;
7715 x = gen_lowpart (mode, new_rtx);
7716 goto maybe_swap;
7718 case MINUS:
7719 lhs = XEXP (x, 0);
7720 rhs = XEXP (x, 1);
7721 lhs = make_compound_operation (lhs, next_code);
7722 rhs = make_compound_operation (rhs, next_code);
7723 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7724 && SCALAR_INT_MODE_P (mode))
7726 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7727 XEXP (rhs, 1));
7728 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7730 else if (GET_CODE (rhs) == MULT
7731 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7733 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7734 simplify_gen_unary (NEG, mode,
7735 XEXP (rhs, 1),
7736 mode));
7737 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7739 else
7741 SUBST (XEXP (x, 0), lhs);
7742 SUBST (XEXP (x, 1), rhs);
7743 return x;
7745 return gen_lowpart (mode, new_rtx);
7747 case AND:
7748 /* If the second operand is not a constant, we can't do anything
7749 with it. */
7750 if (!CONST_INT_P (XEXP (x, 1)))
7751 break;
7753 /* If the constant is a power of two minus one and the first operand
7754 is a logical right shift, make an extraction. */
7755 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7756 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7758 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7759 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7760 0, in_code == COMPARE);
7763 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7764 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7765 && subreg_lowpart_p (XEXP (x, 0))
7766 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7767 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7769 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7770 next_code);
7771 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7772 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7773 0, in_code == COMPARE);
7775 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7776 else if ((GET_CODE (XEXP (x, 0)) == XOR
7777 || GET_CODE (XEXP (x, 0)) == IOR)
7778 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7779 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7780 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7782 /* Apply the distributive law, and then try to make extractions. */
7783 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7784 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7785 XEXP (x, 1)),
7786 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7787 XEXP (x, 1)));
7788 new_rtx = make_compound_operation (new_rtx, in_code);
7791 /* If we are have (and (rotate X C) M) and C is larger than the number
7792 of bits in M, this is an extraction. */
7794 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7795 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7796 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7797 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7799 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7800 new_rtx = make_extraction (mode, new_rtx,
7801 (GET_MODE_PRECISION (mode)
7802 - INTVAL (XEXP (XEXP (x, 0), 1))),
7803 NULL_RTX, i, 1, 0, in_code == COMPARE);
7806 /* On machines without logical shifts, if the operand of the AND is
7807 a logical shift and our mask turns off all the propagated sign
7808 bits, we can replace the logical shift with an arithmetic shift. */
7809 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7810 && !have_insn_for (LSHIFTRT, mode)
7811 && have_insn_for (ASHIFTRT, mode)
7812 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7813 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7814 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7815 && mode_width <= HOST_BITS_PER_WIDE_INT)
7817 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7819 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7820 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7821 SUBST (XEXP (x, 0),
7822 gen_rtx_ASHIFTRT (mode,
7823 make_compound_operation
7824 (XEXP (XEXP (x, 0), 0), next_code),
7825 XEXP (XEXP (x, 0), 1)));
7828 /* If the constant is one less than a power of two, this might be
7829 representable by an extraction even if no shift is present.
7830 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7831 we are in a COMPARE. */
7832 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7833 new_rtx = make_extraction (mode,
7834 make_compound_operation (XEXP (x, 0),
7835 next_code),
7836 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7838 /* If we are in a comparison and this is an AND with a power of two,
7839 convert this into the appropriate bit extract. */
7840 else if (in_code == COMPARE
7841 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7842 new_rtx = make_extraction (mode,
7843 make_compound_operation (XEXP (x, 0),
7844 next_code),
7845 i, NULL_RTX, 1, 1, 0, 1);
7847 break;
7849 case LSHIFTRT:
7850 /* If the sign bit is known to be zero, replace this with an
7851 arithmetic shift. */
7852 if (have_insn_for (ASHIFTRT, mode)
7853 && ! have_insn_for (LSHIFTRT, mode)
7854 && mode_width <= HOST_BITS_PER_WIDE_INT
7855 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7857 new_rtx = gen_rtx_ASHIFTRT (mode,
7858 make_compound_operation (XEXP (x, 0),
7859 next_code),
7860 XEXP (x, 1));
7861 break;
7864 /* ... fall through ... */
7866 case ASHIFTRT:
7867 lhs = XEXP (x, 0);
7868 rhs = XEXP (x, 1);
7870 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7871 this is a SIGN_EXTRACT. */
7872 if (CONST_INT_P (rhs)
7873 && GET_CODE (lhs) == ASHIFT
7874 && CONST_INT_P (XEXP (lhs, 1))
7875 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7876 && INTVAL (XEXP (lhs, 1)) >= 0
7877 && INTVAL (rhs) < mode_width)
7879 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7880 new_rtx = make_extraction (mode, new_rtx,
7881 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7882 NULL_RTX, mode_width - INTVAL (rhs),
7883 code == LSHIFTRT, 0, in_code == COMPARE);
7884 break;
7887 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7888 If so, try to merge the shifts into a SIGN_EXTEND. We could
7889 also do this for some cases of SIGN_EXTRACT, but it doesn't
7890 seem worth the effort; the case checked for occurs on Alpha. */
7892 if (!OBJECT_P (lhs)
7893 && ! (GET_CODE (lhs) == SUBREG
7894 && (OBJECT_P (SUBREG_REG (lhs))))
7895 && CONST_INT_P (rhs)
7896 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7897 && INTVAL (rhs) < mode_width
7898 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7899 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7900 0, NULL_RTX, mode_width - INTVAL (rhs),
7901 code == LSHIFTRT, 0, in_code == COMPARE);
7903 break;
7905 case SUBREG:
7906 /* Call ourselves recursively on the inner expression. If we are
7907 narrowing the object and it has a different RTL code from
7908 what it originally did, do this SUBREG as a force_to_mode. */
7910 rtx inner = SUBREG_REG (x), simplified;
7911 enum rtx_code subreg_code = in_code;
7913 /* If in_code is COMPARE, it isn't always safe to pass it through
7914 to the recursive make_compound_operation call. */
7915 if (subreg_code == COMPARE
7916 && (!subreg_lowpart_p (x)
7917 || GET_CODE (inner) == SUBREG
7918 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7919 is (const_int 0), rather than
7920 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7921 || (GET_CODE (inner) == AND
7922 && CONST_INT_P (XEXP (inner, 1))
7923 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7924 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7925 >= GET_MODE_BITSIZE (mode))))
7926 subreg_code = SET;
7928 tem = make_compound_operation (inner, subreg_code);
7930 simplified
7931 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7932 if (simplified)
7933 tem = simplified;
7935 if (GET_CODE (tem) != GET_CODE (inner)
7936 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7937 && subreg_lowpart_p (x))
7939 rtx newer
7940 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7942 /* If we have something other than a SUBREG, we might have
7943 done an expansion, so rerun ourselves. */
7944 if (GET_CODE (newer) != SUBREG)
7945 newer = make_compound_operation (newer, in_code);
7947 /* force_to_mode can expand compounds. If it just re-expanded the
7948 compound, use gen_lowpart to convert to the desired mode. */
7949 if (rtx_equal_p (newer, x)
7950 /* Likewise if it re-expanded the compound only partially.
7951 This happens for SUBREG of ZERO_EXTRACT if they extract
7952 the same number of bits. */
7953 || (GET_CODE (newer) == SUBREG
7954 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7955 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7956 && GET_CODE (inner) == AND
7957 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7958 return gen_lowpart (GET_MODE (x), tem);
7960 return newer;
7963 if (simplified)
7964 return tem;
7966 break;
7968 default:
7969 break;
7972 if (new_rtx)
7974 x = gen_lowpart (mode, new_rtx);
7975 code = GET_CODE (x);
7978 /* Now recursively process each operand of this operation. We need to
7979 handle ZERO_EXTEND specially so that we don't lose track of the
7980 inner mode. */
7981 if (GET_CODE (x) == ZERO_EXTEND)
7983 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7984 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7985 new_rtx, GET_MODE (XEXP (x, 0)));
7986 if (tem)
7987 return tem;
7988 SUBST (XEXP (x, 0), new_rtx);
7989 return x;
7992 fmt = GET_RTX_FORMAT (code);
7993 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7994 if (fmt[i] == 'e')
7996 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7997 SUBST (XEXP (x, i), new_rtx);
7999 else if (fmt[i] == 'E')
8000 for (j = 0; j < XVECLEN (x, i); j++)
8002 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8003 SUBST (XVECEXP (x, i, j), new_rtx);
8006 maybe_swap:
8007 /* If this is a commutative operation, the changes to the operands
8008 may have made it noncanonical. */
8009 if (COMMUTATIVE_ARITH_P (x)
8010 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8012 tem = XEXP (x, 0);
8013 SUBST (XEXP (x, 0), XEXP (x, 1));
8014 SUBST (XEXP (x, 1), tem);
8017 return x;
8020 /* Given M see if it is a value that would select a field of bits
8021 within an item, but not the entire word. Return -1 if not.
8022 Otherwise, return the starting position of the field, where 0 is the
8023 low-order bit.
8025 *PLEN is set to the length of the field. */
8027 static int
8028 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8030 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8031 int pos = m ? ctz_hwi (m) : -1;
8032 int len = 0;
8034 if (pos >= 0)
8035 /* Now shift off the low-order zero bits and see if we have a
8036 power of two minus 1. */
8037 len = exact_log2 ((m >> pos) + 1);
8039 if (len <= 0)
8040 pos = -1;
8042 *plen = len;
8043 return pos;
8046 /* If X refers to a register that equals REG in value, replace these
8047 references with REG. */
8048 static rtx
8049 canon_reg_for_combine (rtx x, rtx reg)
8051 rtx op0, op1, op2;
8052 const char *fmt;
8053 int i;
8054 bool copied;
8056 enum rtx_code code = GET_CODE (x);
8057 switch (GET_RTX_CLASS (code))
8059 case RTX_UNARY:
8060 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8061 if (op0 != XEXP (x, 0))
8062 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8063 GET_MODE (reg));
8064 break;
8066 case RTX_BIN_ARITH:
8067 case RTX_COMM_ARITH:
8068 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8069 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8070 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8071 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8072 break;
8074 case RTX_COMPARE:
8075 case RTX_COMM_COMPARE:
8076 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8077 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8078 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8079 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8080 GET_MODE (op0), op0, op1);
8081 break;
8083 case RTX_TERNARY:
8084 case RTX_BITFIELD_OPS:
8085 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8086 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8087 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8088 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8089 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8090 GET_MODE (op0), op0, op1, op2);
8092 case RTX_OBJ:
8093 if (REG_P (x))
8095 if (rtx_equal_p (get_last_value (reg), x)
8096 || rtx_equal_p (reg, get_last_value (x)))
8097 return reg;
8098 else
8099 break;
8102 /* fall through */
8104 default:
8105 fmt = GET_RTX_FORMAT (code);
8106 copied = false;
8107 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8108 if (fmt[i] == 'e')
8110 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8111 if (op != XEXP (x, i))
8113 if (!copied)
8115 copied = true;
8116 x = copy_rtx (x);
8118 XEXP (x, i) = op;
8121 else if (fmt[i] == 'E')
8123 int j;
8124 for (j = 0; j < XVECLEN (x, i); j++)
8126 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8127 if (op != XVECEXP (x, i, j))
8129 if (!copied)
8131 copied = true;
8132 x = copy_rtx (x);
8134 XVECEXP (x, i, j) = op;
8139 break;
8142 return x;
8145 /* Return X converted to MODE. If the value is already truncated to
8146 MODE we can just return a subreg even though in the general case we
8147 would need an explicit truncation. */
8149 static rtx
8150 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8152 if (!CONST_INT_P (x)
8153 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8154 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8155 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8157 /* Bit-cast X into an integer mode. */
8158 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8159 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8160 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8161 x, GET_MODE (x));
8164 return gen_lowpart (mode, x);
8167 /* See if X can be simplified knowing that we will only refer to it in
8168 MODE and will only refer to those bits that are nonzero in MASK.
8169 If other bits are being computed or if masking operations are done
8170 that select a superset of the bits in MASK, they can sometimes be
8171 ignored.
8173 Return a possibly simplified expression, but always convert X to
8174 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8176 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8177 are all off in X. This is used when X will be complemented, by either
8178 NOT, NEG, or XOR. */
8180 static rtx
8181 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8182 int just_select)
8184 enum rtx_code code = GET_CODE (x);
8185 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8186 machine_mode op_mode;
8187 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8188 rtx op0, op1, temp;
8190 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8191 code below will do the wrong thing since the mode of such an
8192 expression is VOIDmode.
8194 Also do nothing if X is a CLOBBER; this can happen if X was
8195 the return value from a call to gen_lowpart. */
8196 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8197 return x;
8199 /* We want to perform the operation in its present mode unless we know
8200 that the operation is valid in MODE, in which case we do the operation
8201 in MODE. */
8202 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8203 && have_insn_for (code, mode))
8204 ? mode : GET_MODE (x));
8206 /* It is not valid to do a right-shift in a narrower mode
8207 than the one it came in with. */
8208 if ((code == LSHIFTRT || code == ASHIFTRT)
8209 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8210 op_mode = GET_MODE (x);
8212 /* Truncate MASK to fit OP_MODE. */
8213 if (op_mode)
8214 mask &= GET_MODE_MASK (op_mode);
8216 /* When we have an arithmetic operation, or a shift whose count we
8217 do not know, we need to assume that all bits up to the highest-order
8218 bit in MASK will be needed. This is how we form such a mask. */
8219 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8220 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8221 else
8222 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8223 - 1);
8225 /* Determine what bits of X are guaranteed to be (non)zero. */
8226 nonzero = nonzero_bits (x, mode);
8228 /* If none of the bits in X are needed, return a zero. */
8229 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8230 x = const0_rtx;
8232 /* If X is a CONST_INT, return a new one. Do this here since the
8233 test below will fail. */
8234 if (CONST_INT_P (x))
8236 if (SCALAR_INT_MODE_P (mode))
8237 return gen_int_mode (INTVAL (x) & mask, mode);
8238 else
8240 x = GEN_INT (INTVAL (x) & mask);
8241 return gen_lowpart_common (mode, x);
8245 /* If X is narrower than MODE and we want all the bits in X's mode, just
8246 get X in the proper mode. */
8247 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8248 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8249 return gen_lowpart (mode, x);
8251 /* We can ignore the effect of a SUBREG if it narrows the mode or
8252 if the constant masks to zero all the bits the mode doesn't have. */
8253 if (GET_CODE (x) == SUBREG
8254 && subreg_lowpart_p (x)
8255 && ((GET_MODE_SIZE (GET_MODE (x))
8256 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8257 || (0 == (mask
8258 & GET_MODE_MASK (GET_MODE (x))
8259 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8260 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8262 /* The arithmetic simplifications here only work for scalar integer modes. */
8263 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8264 return gen_lowpart_or_truncate (mode, x);
8266 switch (code)
8268 case CLOBBER:
8269 /* If X is a (clobber (const_int)), return it since we know we are
8270 generating something that won't match. */
8271 return x;
8273 case SIGN_EXTEND:
8274 case ZERO_EXTEND:
8275 case ZERO_EXTRACT:
8276 case SIGN_EXTRACT:
8277 x = expand_compound_operation (x);
8278 if (GET_CODE (x) != code)
8279 return force_to_mode (x, mode, mask, next_select);
8280 break;
8282 case TRUNCATE:
8283 /* Similarly for a truncate. */
8284 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8286 case AND:
8287 /* If this is an AND with a constant, convert it into an AND
8288 whose constant is the AND of that constant with MASK. If it
8289 remains an AND of MASK, delete it since it is redundant. */
8291 if (CONST_INT_P (XEXP (x, 1)))
8293 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8294 mask & INTVAL (XEXP (x, 1)));
8296 /* If X is still an AND, see if it is an AND with a mask that
8297 is just some low-order bits. If so, and it is MASK, we don't
8298 need it. */
8300 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8301 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8302 == mask))
8303 x = XEXP (x, 0);
8305 /* If it remains an AND, try making another AND with the bits
8306 in the mode mask that aren't in MASK turned on. If the
8307 constant in the AND is wide enough, this might make a
8308 cheaper constant. */
8310 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8311 && GET_MODE_MASK (GET_MODE (x)) != mask
8312 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8314 unsigned HOST_WIDE_INT cval
8315 = UINTVAL (XEXP (x, 1))
8316 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8317 rtx y;
8319 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8320 gen_int_mode (cval, GET_MODE (x)));
8321 if (set_src_cost (y, optimize_this_for_speed_p)
8322 < set_src_cost (x, optimize_this_for_speed_p))
8323 x = y;
8326 break;
8329 goto binop;
8331 case PLUS:
8332 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8333 low-order bits (as in an alignment operation) and FOO is already
8334 aligned to that boundary, mask C1 to that boundary as well.
8335 This may eliminate that PLUS and, later, the AND. */
8338 unsigned int width = GET_MODE_PRECISION (mode);
8339 unsigned HOST_WIDE_INT smask = mask;
8341 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8342 number, sign extend it. */
8344 if (width < HOST_BITS_PER_WIDE_INT
8345 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8346 smask |= HOST_WIDE_INT_M1U << width;
8348 if (CONST_INT_P (XEXP (x, 1))
8349 && exact_log2 (- smask) >= 0
8350 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8351 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8352 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8353 (INTVAL (XEXP (x, 1)) & smask)),
8354 mode, smask, next_select);
8357 /* ... fall through ... */
8359 case MULT:
8360 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8361 most significant bit in MASK since carries from those bits will
8362 affect the bits we are interested in. */
8363 mask = fuller_mask;
8364 goto binop;
8366 case MINUS:
8367 /* If X is (minus C Y) where C's least set bit is larger than any bit
8368 in the mask, then we may replace with (neg Y). */
8369 if (CONST_INT_P (XEXP (x, 0))
8370 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8372 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8373 GET_MODE (x));
8374 return force_to_mode (x, mode, mask, next_select);
8377 /* Similarly, if C contains every bit in the fuller_mask, then we may
8378 replace with (not Y). */
8379 if (CONST_INT_P (XEXP (x, 0))
8380 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8382 x = simplify_gen_unary (NOT, GET_MODE (x),
8383 XEXP (x, 1), GET_MODE (x));
8384 return force_to_mode (x, mode, mask, next_select);
8387 mask = fuller_mask;
8388 goto binop;
8390 case IOR:
8391 case XOR:
8392 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8393 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8394 operation which may be a bitfield extraction. Ensure that the
8395 constant we form is not wider than the mode of X. */
8397 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8398 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8399 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8400 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8401 && CONST_INT_P (XEXP (x, 1))
8402 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8403 + floor_log2 (INTVAL (XEXP (x, 1))))
8404 < GET_MODE_PRECISION (GET_MODE (x)))
8405 && (UINTVAL (XEXP (x, 1))
8406 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8408 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8409 << INTVAL (XEXP (XEXP (x, 0), 1)),
8410 GET_MODE (x));
8411 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8412 XEXP (XEXP (x, 0), 0), temp);
8413 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8414 XEXP (XEXP (x, 0), 1));
8415 return force_to_mode (x, mode, mask, next_select);
8418 binop:
8419 /* For most binary operations, just propagate into the operation and
8420 change the mode if we have an operation of that mode. */
8422 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8423 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8425 /* If we ended up truncating both operands, truncate the result of the
8426 operation instead. */
8427 if (GET_CODE (op0) == TRUNCATE
8428 && GET_CODE (op1) == TRUNCATE)
8430 op0 = XEXP (op0, 0);
8431 op1 = XEXP (op1, 0);
8434 op0 = gen_lowpart_or_truncate (op_mode, op0);
8435 op1 = gen_lowpart_or_truncate (op_mode, op1);
8437 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8438 x = simplify_gen_binary (code, op_mode, op0, op1);
8439 break;
8441 case ASHIFT:
8442 /* For left shifts, do the same, but just for the first operand.
8443 However, we cannot do anything with shifts where we cannot
8444 guarantee that the counts are smaller than the size of the mode
8445 because such a count will have a different meaning in a
8446 wider mode. */
8448 if (! (CONST_INT_P (XEXP (x, 1))
8449 && INTVAL (XEXP (x, 1)) >= 0
8450 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8451 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8452 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8453 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8454 break;
8456 /* If the shift count is a constant and we can do arithmetic in
8457 the mode of the shift, refine which bits we need. Otherwise, use the
8458 conservative form of the mask. */
8459 if (CONST_INT_P (XEXP (x, 1))
8460 && INTVAL (XEXP (x, 1)) >= 0
8461 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8462 && HWI_COMPUTABLE_MODE_P (op_mode))
8463 mask >>= INTVAL (XEXP (x, 1));
8464 else
8465 mask = fuller_mask;
8467 op0 = gen_lowpart_or_truncate (op_mode,
8468 force_to_mode (XEXP (x, 0), op_mode,
8469 mask, next_select));
8471 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8472 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8473 break;
8475 case LSHIFTRT:
8476 /* Here we can only do something if the shift count is a constant,
8477 this shift constant is valid for the host, and we can do arithmetic
8478 in OP_MODE. */
8480 if (CONST_INT_P (XEXP (x, 1))
8481 && INTVAL (XEXP (x, 1)) >= 0
8482 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8483 && HWI_COMPUTABLE_MODE_P (op_mode))
8485 rtx inner = XEXP (x, 0);
8486 unsigned HOST_WIDE_INT inner_mask;
8488 /* Select the mask of the bits we need for the shift operand. */
8489 inner_mask = mask << INTVAL (XEXP (x, 1));
8491 /* We can only change the mode of the shift if we can do arithmetic
8492 in the mode of the shift and INNER_MASK is no wider than the
8493 width of X's mode. */
8494 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8495 op_mode = GET_MODE (x);
8497 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8499 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8500 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8503 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8504 shift and AND produces only copies of the sign bit (C2 is one less
8505 than a power of two), we can do this with just a shift. */
8507 if (GET_CODE (x) == LSHIFTRT
8508 && CONST_INT_P (XEXP (x, 1))
8509 /* The shift puts one of the sign bit copies in the least significant
8510 bit. */
8511 && ((INTVAL (XEXP (x, 1))
8512 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8513 >= GET_MODE_PRECISION (GET_MODE (x)))
8514 && exact_log2 (mask + 1) >= 0
8515 /* Number of bits left after the shift must be more than the mask
8516 needs. */
8517 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8518 <= GET_MODE_PRECISION (GET_MODE (x)))
8519 /* Must be more sign bit copies than the mask needs. */
8520 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8521 >= exact_log2 (mask + 1)))
8522 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8523 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8524 - exact_log2 (mask + 1)));
8526 goto shiftrt;
8528 case ASHIFTRT:
8529 /* If we are just looking for the sign bit, we don't need this shift at
8530 all, even if it has a variable count. */
8531 if (val_signbit_p (GET_MODE (x), mask))
8532 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8534 /* If this is a shift by a constant, get a mask that contains those bits
8535 that are not copies of the sign bit. We then have two cases: If
8536 MASK only includes those bits, this can be a logical shift, which may
8537 allow simplifications. If MASK is a single-bit field not within
8538 those bits, we are requesting a copy of the sign bit and hence can
8539 shift the sign bit to the appropriate location. */
8541 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8542 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8544 int i;
8546 /* If the considered data is wider than HOST_WIDE_INT, we can't
8547 represent a mask for all its bits in a single scalar.
8548 But we only care about the lower bits, so calculate these. */
8550 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8552 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8554 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8555 is the number of bits a full-width mask would have set.
8556 We need only shift if these are fewer than nonzero can
8557 hold. If not, we must keep all bits set in nonzero. */
8559 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8560 < HOST_BITS_PER_WIDE_INT)
8561 nonzero >>= INTVAL (XEXP (x, 1))
8562 + HOST_BITS_PER_WIDE_INT
8563 - GET_MODE_PRECISION (GET_MODE (x)) ;
8565 else
8567 nonzero = GET_MODE_MASK (GET_MODE (x));
8568 nonzero >>= INTVAL (XEXP (x, 1));
8571 if ((mask & ~nonzero) == 0)
8573 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8574 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8575 if (GET_CODE (x) != ASHIFTRT)
8576 return force_to_mode (x, mode, mask, next_select);
8579 else if ((i = exact_log2 (mask)) >= 0)
8581 x = simplify_shift_const
8582 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8583 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8585 if (GET_CODE (x) != ASHIFTRT)
8586 return force_to_mode (x, mode, mask, next_select);
8590 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8591 even if the shift count isn't a constant. */
8592 if (mask == 1)
8593 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8594 XEXP (x, 0), XEXP (x, 1));
8596 shiftrt:
8598 /* If this is a zero- or sign-extension operation that just affects bits
8599 we don't care about, remove it. Be sure the call above returned
8600 something that is still a shift. */
8602 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8603 && CONST_INT_P (XEXP (x, 1))
8604 && INTVAL (XEXP (x, 1)) >= 0
8605 && (INTVAL (XEXP (x, 1))
8606 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8607 && GET_CODE (XEXP (x, 0)) == ASHIFT
8608 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8609 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8610 next_select);
8612 break;
8614 case ROTATE:
8615 case ROTATERT:
8616 /* If the shift count is constant and we can do computations
8617 in the mode of X, compute where the bits we care about are.
8618 Otherwise, we can't do anything. Don't change the mode of
8619 the shift or propagate MODE into the shift, though. */
8620 if (CONST_INT_P (XEXP (x, 1))
8621 && INTVAL (XEXP (x, 1)) >= 0)
8623 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8624 GET_MODE (x),
8625 gen_int_mode (mask, GET_MODE (x)),
8626 XEXP (x, 1));
8627 if (temp && CONST_INT_P (temp))
8628 x = simplify_gen_binary (code, GET_MODE (x),
8629 force_to_mode (XEXP (x, 0), GET_MODE (x),
8630 INTVAL (temp), next_select),
8631 XEXP (x, 1));
8633 break;
8635 case NEG:
8636 /* If we just want the low-order bit, the NEG isn't needed since it
8637 won't change the low-order bit. */
8638 if (mask == 1)
8639 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8641 /* We need any bits less significant than the most significant bit in
8642 MASK since carries from those bits will affect the bits we are
8643 interested in. */
8644 mask = fuller_mask;
8645 goto unop;
8647 case NOT:
8648 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8649 same as the XOR case above. Ensure that the constant we form is not
8650 wider than the mode of X. */
8652 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8653 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8654 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8655 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8656 < GET_MODE_PRECISION (GET_MODE (x)))
8657 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8659 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8660 GET_MODE (x));
8661 temp = simplify_gen_binary (XOR, GET_MODE (x),
8662 XEXP (XEXP (x, 0), 0), temp);
8663 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8664 temp, XEXP (XEXP (x, 0), 1));
8666 return force_to_mode (x, mode, mask, next_select);
8669 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8670 use the full mask inside the NOT. */
8671 mask = fuller_mask;
8673 unop:
8674 op0 = gen_lowpart_or_truncate (op_mode,
8675 force_to_mode (XEXP (x, 0), mode, mask,
8676 next_select));
8677 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8678 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8679 break;
8681 case NE:
8682 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8683 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8684 which is equal to STORE_FLAG_VALUE. */
8685 if ((mask & ~STORE_FLAG_VALUE) == 0
8686 && XEXP (x, 1) == const0_rtx
8687 && GET_MODE (XEXP (x, 0)) == mode
8688 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8689 && (nonzero_bits (XEXP (x, 0), mode)
8690 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8691 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8693 break;
8695 case IF_THEN_ELSE:
8696 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8697 written in a narrower mode. We play it safe and do not do so. */
8699 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8700 force_to_mode (XEXP (x, 1), mode,
8701 mask, next_select));
8702 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8703 force_to_mode (XEXP (x, 2), mode,
8704 mask, next_select));
8705 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8706 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8707 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8708 op0, op1);
8709 break;
8711 default:
8712 break;
8715 /* Ensure we return a value of the proper mode. */
8716 return gen_lowpart_or_truncate (mode, x);
8719 /* Return nonzero if X is an expression that has one of two values depending on
8720 whether some other value is zero or nonzero. In that case, we return the
8721 value that is being tested, *PTRUE is set to the value if the rtx being
8722 returned has a nonzero value, and *PFALSE is set to the other alternative.
8724 If we return zero, we set *PTRUE and *PFALSE to X. */
8726 static rtx
8727 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8729 machine_mode mode = GET_MODE (x);
8730 enum rtx_code code = GET_CODE (x);
8731 rtx cond0, cond1, true0, true1, false0, false1;
8732 unsigned HOST_WIDE_INT nz;
8734 /* If we are comparing a value against zero, we are done. */
8735 if ((code == NE || code == EQ)
8736 && XEXP (x, 1) == const0_rtx)
8738 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8739 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8740 return XEXP (x, 0);
8743 /* If this is a unary operation whose operand has one of two values, apply
8744 our opcode to compute those values. */
8745 else if (UNARY_P (x)
8746 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8748 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8749 *pfalse = simplify_gen_unary (code, mode, false0,
8750 GET_MODE (XEXP (x, 0)));
8751 return cond0;
8754 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8755 make can't possibly match and would suppress other optimizations. */
8756 else if (code == COMPARE)
8759 /* If this is a binary operation, see if either side has only one of two
8760 values. If either one does or if both do and they are conditional on
8761 the same value, compute the new true and false values. */
8762 else if (BINARY_P (x))
8764 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8765 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8767 if ((cond0 != 0 || cond1 != 0)
8768 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8770 /* If if_then_else_cond returned zero, then true/false are the
8771 same rtl. We must copy one of them to prevent invalid rtl
8772 sharing. */
8773 if (cond0 == 0)
8774 true0 = copy_rtx (true0);
8775 else if (cond1 == 0)
8776 true1 = copy_rtx (true1);
8778 if (COMPARISON_P (x))
8780 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8781 true0, true1);
8782 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8783 false0, false1);
8785 else
8787 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8788 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8791 return cond0 ? cond0 : cond1;
8794 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8795 operands is zero when the other is nonzero, and vice-versa,
8796 and STORE_FLAG_VALUE is 1 or -1. */
8798 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8799 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8800 || code == UMAX)
8801 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8803 rtx op0 = XEXP (XEXP (x, 0), 1);
8804 rtx op1 = XEXP (XEXP (x, 1), 1);
8806 cond0 = XEXP (XEXP (x, 0), 0);
8807 cond1 = XEXP (XEXP (x, 1), 0);
8809 if (COMPARISON_P (cond0)
8810 && COMPARISON_P (cond1)
8811 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8812 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8813 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8814 || ((swap_condition (GET_CODE (cond0))
8815 == reversed_comparison_code (cond1, NULL))
8816 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8817 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8818 && ! side_effects_p (x))
8820 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8821 *pfalse = simplify_gen_binary (MULT, mode,
8822 (code == MINUS
8823 ? simplify_gen_unary (NEG, mode,
8824 op1, mode)
8825 : op1),
8826 const_true_rtx);
8827 return cond0;
8831 /* Similarly for MULT, AND and UMIN, except that for these the result
8832 is always zero. */
8833 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8834 && (code == MULT || code == AND || code == UMIN)
8835 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8837 cond0 = XEXP (XEXP (x, 0), 0);
8838 cond1 = XEXP (XEXP (x, 1), 0);
8840 if (COMPARISON_P (cond0)
8841 && COMPARISON_P (cond1)
8842 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8843 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8844 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8845 || ((swap_condition (GET_CODE (cond0))
8846 == reversed_comparison_code (cond1, NULL))
8847 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8848 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8849 && ! side_effects_p (x))
8851 *ptrue = *pfalse = const0_rtx;
8852 return cond0;
8857 else if (code == IF_THEN_ELSE)
8859 /* If we have IF_THEN_ELSE already, extract the condition and
8860 canonicalize it if it is NE or EQ. */
8861 cond0 = XEXP (x, 0);
8862 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8863 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8864 return XEXP (cond0, 0);
8865 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8867 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8868 return XEXP (cond0, 0);
8870 else
8871 return cond0;
8874 /* If X is a SUBREG, we can narrow both the true and false values
8875 if the inner expression, if there is a condition. */
8876 else if (code == SUBREG
8877 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8878 &true0, &false0)))
8880 true0 = simplify_gen_subreg (mode, true0,
8881 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8882 false0 = simplify_gen_subreg (mode, false0,
8883 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8884 if (true0 && false0)
8886 *ptrue = true0;
8887 *pfalse = false0;
8888 return cond0;
8892 /* If X is a constant, this isn't special and will cause confusions
8893 if we treat it as such. Likewise if it is equivalent to a constant. */
8894 else if (CONSTANT_P (x)
8895 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8898 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8899 will be least confusing to the rest of the compiler. */
8900 else if (mode == BImode)
8902 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8903 return x;
8906 /* If X is known to be either 0 or -1, those are the true and
8907 false values when testing X. */
8908 else if (x == constm1_rtx || x == const0_rtx
8909 || (mode != VOIDmode
8910 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8912 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8913 return x;
8916 /* Likewise for 0 or a single bit. */
8917 else if (HWI_COMPUTABLE_MODE_P (mode)
8918 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8920 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8921 return x;
8924 /* Otherwise fail; show no condition with true and false values the same. */
8925 *ptrue = *pfalse = x;
8926 return 0;
8929 /* Return the value of expression X given the fact that condition COND
8930 is known to be true when applied to REG as its first operand and VAL
8931 as its second. X is known to not be shared and so can be modified in
8932 place.
8934 We only handle the simplest cases, and specifically those cases that
8935 arise with IF_THEN_ELSE expressions. */
8937 static rtx
8938 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8940 enum rtx_code code = GET_CODE (x);
8941 rtx temp;
8942 const char *fmt;
8943 int i, j;
8945 if (side_effects_p (x))
8946 return x;
8948 /* If either operand of the condition is a floating point value,
8949 then we have to avoid collapsing an EQ comparison. */
8950 if (cond == EQ
8951 && rtx_equal_p (x, reg)
8952 && ! FLOAT_MODE_P (GET_MODE (x))
8953 && ! FLOAT_MODE_P (GET_MODE (val)))
8954 return val;
8956 if (cond == UNEQ && rtx_equal_p (x, reg))
8957 return val;
8959 /* If X is (abs REG) and we know something about REG's relationship
8960 with zero, we may be able to simplify this. */
8962 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8963 switch (cond)
8965 case GE: case GT: case EQ:
8966 return XEXP (x, 0);
8967 case LT: case LE:
8968 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8969 XEXP (x, 0),
8970 GET_MODE (XEXP (x, 0)));
8971 default:
8972 break;
8975 /* The only other cases we handle are MIN, MAX, and comparisons if the
8976 operands are the same as REG and VAL. */
8978 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8980 if (rtx_equal_p (XEXP (x, 0), val))
8981 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8983 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8985 if (COMPARISON_P (x))
8987 if (comparison_dominates_p (cond, code))
8988 return const_true_rtx;
8990 code = reversed_comparison_code (x, NULL);
8991 if (code != UNKNOWN
8992 && comparison_dominates_p (cond, code))
8993 return const0_rtx;
8994 else
8995 return x;
8997 else if (code == SMAX || code == SMIN
8998 || code == UMIN || code == UMAX)
9000 int unsignedp = (code == UMIN || code == UMAX);
9002 /* Do not reverse the condition when it is NE or EQ.
9003 This is because we cannot conclude anything about
9004 the value of 'SMAX (x, y)' when x is not equal to y,
9005 but we can when x equals y. */
9006 if ((code == SMAX || code == UMAX)
9007 && ! (cond == EQ || cond == NE))
9008 cond = reverse_condition (cond);
9010 switch (cond)
9012 case GE: case GT:
9013 return unsignedp ? x : XEXP (x, 1);
9014 case LE: case LT:
9015 return unsignedp ? x : XEXP (x, 0);
9016 case GEU: case GTU:
9017 return unsignedp ? XEXP (x, 1) : x;
9018 case LEU: case LTU:
9019 return unsignedp ? XEXP (x, 0) : x;
9020 default:
9021 break;
9026 else if (code == SUBREG)
9028 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9029 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9031 if (SUBREG_REG (x) != r)
9033 /* We must simplify subreg here, before we lose track of the
9034 original inner_mode. */
9035 new_rtx = simplify_subreg (GET_MODE (x), r,
9036 inner_mode, SUBREG_BYTE (x));
9037 if (new_rtx)
9038 return new_rtx;
9039 else
9040 SUBST (SUBREG_REG (x), r);
9043 return x;
9045 /* We don't have to handle SIGN_EXTEND here, because even in the
9046 case of replacing something with a modeless CONST_INT, a
9047 CONST_INT is already (supposed to be) a valid sign extension for
9048 its narrower mode, which implies it's already properly
9049 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9050 story is different. */
9051 else if (code == ZERO_EXTEND)
9053 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9054 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9056 if (XEXP (x, 0) != r)
9058 /* We must simplify the zero_extend here, before we lose
9059 track of the original inner_mode. */
9060 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9061 r, inner_mode);
9062 if (new_rtx)
9063 return new_rtx;
9064 else
9065 SUBST (XEXP (x, 0), r);
9068 return x;
9071 fmt = GET_RTX_FORMAT (code);
9072 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9074 if (fmt[i] == 'e')
9075 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9076 else if (fmt[i] == 'E')
9077 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9078 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9079 cond, reg, val));
9082 return x;
9085 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9086 assignment as a field assignment. */
9088 static int
9089 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9091 if (x == y || rtx_equal_p (x, y))
9092 return 1;
9094 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9095 return 0;
9097 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9098 Note that all SUBREGs of MEM are paradoxical; otherwise they
9099 would have been rewritten. */
9100 if (MEM_P (x) && GET_CODE (y) == SUBREG
9101 && MEM_P (SUBREG_REG (y))
9102 && rtx_equal_p (SUBREG_REG (y),
9103 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9104 return 1;
9106 if (MEM_P (y) && GET_CODE (x) == SUBREG
9107 && MEM_P (SUBREG_REG (x))
9108 && rtx_equal_p (SUBREG_REG (x),
9109 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9110 return 1;
9112 /* We used to see if get_last_value of X and Y were the same but that's
9113 not correct. In one direction, we'll cause the assignment to have
9114 the wrong destination and in the case, we'll import a register into this
9115 insn that might have already have been dead. So fail if none of the
9116 above cases are true. */
9117 return 0;
9120 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9121 Return that assignment if so.
9123 We only handle the most common cases. */
9125 static rtx
9126 make_field_assignment (rtx x)
9128 rtx dest = SET_DEST (x);
9129 rtx src = SET_SRC (x);
9130 rtx assign;
9131 rtx rhs, lhs;
9132 HOST_WIDE_INT c1;
9133 HOST_WIDE_INT pos;
9134 unsigned HOST_WIDE_INT len;
9135 rtx other;
9136 machine_mode mode;
9138 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9139 a clear of a one-bit field. We will have changed it to
9140 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9141 for a SUBREG. */
9143 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9144 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9145 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9146 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9148 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9149 1, 1, 1, 0);
9150 if (assign != 0)
9151 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9152 return x;
9155 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9156 && subreg_lowpart_p (XEXP (src, 0))
9157 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9158 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9159 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9160 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9161 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9162 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9164 assign = make_extraction (VOIDmode, dest, 0,
9165 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9166 1, 1, 1, 0);
9167 if (assign != 0)
9168 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9169 return x;
9172 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9173 one-bit field. */
9174 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9175 && XEXP (XEXP (src, 0), 0) == const1_rtx
9176 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9178 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9179 1, 1, 1, 0);
9180 if (assign != 0)
9181 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9182 return x;
9185 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9186 SRC is an AND with all bits of that field set, then we can discard
9187 the AND. */
9188 if (GET_CODE (dest) == ZERO_EXTRACT
9189 && CONST_INT_P (XEXP (dest, 1))
9190 && GET_CODE (src) == AND
9191 && CONST_INT_P (XEXP (src, 1)))
9193 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9194 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9195 unsigned HOST_WIDE_INT ze_mask;
9197 if (width >= HOST_BITS_PER_WIDE_INT)
9198 ze_mask = -1;
9199 else
9200 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9202 /* Complete overlap. We can remove the source AND. */
9203 if ((and_mask & ze_mask) == ze_mask)
9204 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9206 /* Partial overlap. We can reduce the source AND. */
9207 if ((and_mask & ze_mask) != and_mask)
9209 mode = GET_MODE (src);
9210 src = gen_rtx_AND (mode, XEXP (src, 0),
9211 gen_int_mode (and_mask & ze_mask, mode));
9212 return gen_rtx_SET (VOIDmode, dest, src);
9216 /* The other case we handle is assignments into a constant-position
9217 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9218 a mask that has all one bits except for a group of zero bits and
9219 OTHER is known to have zeros where C1 has ones, this is such an
9220 assignment. Compute the position and length from C1. Shift OTHER
9221 to the appropriate position, force it to the required mode, and
9222 make the extraction. Check for the AND in both operands. */
9224 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9225 return x;
9227 rhs = expand_compound_operation (XEXP (src, 0));
9228 lhs = expand_compound_operation (XEXP (src, 1));
9230 if (GET_CODE (rhs) == AND
9231 && CONST_INT_P (XEXP (rhs, 1))
9232 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9233 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9234 else if (GET_CODE (lhs) == AND
9235 && CONST_INT_P (XEXP (lhs, 1))
9236 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9237 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9238 else
9239 return x;
9241 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9242 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9243 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9244 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9245 return x;
9247 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9248 if (assign == 0)
9249 return x;
9251 /* The mode to use for the source is the mode of the assignment, or of
9252 what is inside a possible STRICT_LOW_PART. */
9253 mode = (GET_CODE (assign) == STRICT_LOW_PART
9254 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9256 /* Shift OTHER right POS places and make it the source, restricting it
9257 to the proper length and mode. */
9259 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9260 GET_MODE (src),
9261 other, pos),
9262 dest);
9263 src = force_to_mode (src, mode,
9264 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9265 ? ~(unsigned HOST_WIDE_INT) 0
9266 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9269 /* If SRC is masked by an AND that does not make a difference in
9270 the value being stored, strip it. */
9271 if (GET_CODE (assign) == ZERO_EXTRACT
9272 && CONST_INT_P (XEXP (assign, 1))
9273 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9274 && GET_CODE (src) == AND
9275 && CONST_INT_P (XEXP (src, 1))
9276 && UINTVAL (XEXP (src, 1))
9277 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9278 src = XEXP (src, 0);
9280 return gen_rtx_SET (VOIDmode, assign, src);
9283 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9284 if so. */
9286 static rtx
9287 apply_distributive_law (rtx x)
9289 enum rtx_code code = GET_CODE (x);
9290 enum rtx_code inner_code;
9291 rtx lhs, rhs, other;
9292 rtx tem;
9294 /* Distributivity is not true for floating point as it can change the
9295 value. So we don't do it unless -funsafe-math-optimizations. */
9296 if (FLOAT_MODE_P (GET_MODE (x))
9297 && ! flag_unsafe_math_optimizations)
9298 return x;
9300 /* The outer operation can only be one of the following: */
9301 if (code != IOR && code != AND && code != XOR
9302 && code != PLUS && code != MINUS)
9303 return x;
9305 lhs = XEXP (x, 0);
9306 rhs = XEXP (x, 1);
9308 /* If either operand is a primitive we can't do anything, so get out
9309 fast. */
9310 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9311 return x;
9313 lhs = expand_compound_operation (lhs);
9314 rhs = expand_compound_operation (rhs);
9315 inner_code = GET_CODE (lhs);
9316 if (inner_code != GET_CODE (rhs))
9317 return x;
9319 /* See if the inner and outer operations distribute. */
9320 switch (inner_code)
9322 case LSHIFTRT:
9323 case ASHIFTRT:
9324 case AND:
9325 case IOR:
9326 /* These all distribute except over PLUS. */
9327 if (code == PLUS || code == MINUS)
9328 return x;
9329 break;
9331 case MULT:
9332 if (code != PLUS && code != MINUS)
9333 return x;
9334 break;
9336 case ASHIFT:
9337 /* This is also a multiply, so it distributes over everything. */
9338 break;
9340 /* This used to handle SUBREG, but this turned out to be counter-
9341 productive, since (subreg (op ...)) usually is not handled by
9342 insn patterns, and this "optimization" therefore transformed
9343 recognizable patterns into unrecognizable ones. Therefore the
9344 SUBREG case was removed from here.
9346 It is possible that distributing SUBREG over arithmetic operations
9347 leads to an intermediate result than can then be optimized further,
9348 e.g. by moving the outer SUBREG to the other side of a SET as done
9349 in simplify_set. This seems to have been the original intent of
9350 handling SUBREGs here.
9352 However, with current GCC this does not appear to actually happen,
9353 at least on major platforms. If some case is found where removing
9354 the SUBREG case here prevents follow-on optimizations, distributing
9355 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9357 default:
9358 return x;
9361 /* Set LHS and RHS to the inner operands (A and B in the example
9362 above) and set OTHER to the common operand (C in the example).
9363 There is only one way to do this unless the inner operation is
9364 commutative. */
9365 if (COMMUTATIVE_ARITH_P (lhs)
9366 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9367 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9368 else if (COMMUTATIVE_ARITH_P (lhs)
9369 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9370 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9371 else if (COMMUTATIVE_ARITH_P (lhs)
9372 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9373 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9374 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9375 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9376 else
9377 return x;
9379 /* Form the new inner operation, seeing if it simplifies first. */
9380 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9382 /* There is one exception to the general way of distributing:
9383 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9384 if (code == XOR && inner_code == IOR)
9386 inner_code = AND;
9387 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9390 /* We may be able to continuing distributing the result, so call
9391 ourselves recursively on the inner operation before forming the
9392 outer operation, which we return. */
9393 return simplify_gen_binary (inner_code, GET_MODE (x),
9394 apply_distributive_law (tem), other);
9397 /* See if X is of the form (* (+ A B) C), and if so convert to
9398 (+ (* A C) (* B C)) and try to simplify.
9400 Most of the time, this results in no change. However, if some of
9401 the operands are the same or inverses of each other, simplifications
9402 will result.
9404 For example, (and (ior A B) (not B)) can occur as the result of
9405 expanding a bit field assignment. When we apply the distributive
9406 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9407 which then simplifies to (and (A (not B))).
9409 Note that no checks happen on the validity of applying the inverse
9410 distributive law. This is pointless since we can do it in the
9411 few places where this routine is called.
9413 N is the index of the term that is decomposed (the arithmetic operation,
9414 i.e. (+ A B) in the first example above). !N is the index of the term that
9415 is distributed, i.e. of C in the first example above. */
9416 static rtx
9417 distribute_and_simplify_rtx (rtx x, int n)
9419 machine_mode mode;
9420 enum rtx_code outer_code, inner_code;
9421 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9423 /* Distributivity is not true for floating point as it can change the
9424 value. So we don't do it unless -funsafe-math-optimizations. */
9425 if (FLOAT_MODE_P (GET_MODE (x))
9426 && ! flag_unsafe_math_optimizations)
9427 return NULL_RTX;
9429 decomposed = XEXP (x, n);
9430 if (!ARITHMETIC_P (decomposed))
9431 return NULL_RTX;
9433 mode = GET_MODE (x);
9434 outer_code = GET_CODE (x);
9435 distributed = XEXP (x, !n);
9437 inner_code = GET_CODE (decomposed);
9438 inner_op0 = XEXP (decomposed, 0);
9439 inner_op1 = XEXP (decomposed, 1);
9441 /* Special case (and (xor B C) (not A)), which is equivalent to
9442 (xor (ior A B) (ior A C)) */
9443 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9445 distributed = XEXP (distributed, 0);
9446 outer_code = IOR;
9449 if (n == 0)
9451 /* Distribute the second term. */
9452 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9453 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9455 else
9457 /* Distribute the first term. */
9458 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9459 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9462 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9463 new_op0, new_op1));
9464 if (GET_CODE (tmp) != outer_code
9465 && (set_src_cost (tmp, optimize_this_for_speed_p)
9466 < set_src_cost (x, optimize_this_for_speed_p)))
9467 return tmp;
9469 return NULL_RTX;
9472 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9473 in MODE. Return an equivalent form, if different from (and VAROP
9474 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9476 static rtx
9477 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9478 unsigned HOST_WIDE_INT constop)
9480 unsigned HOST_WIDE_INT nonzero;
9481 unsigned HOST_WIDE_INT orig_constop;
9482 rtx orig_varop;
9483 int i;
9485 orig_varop = varop;
9486 orig_constop = constop;
9487 if (GET_CODE (varop) == CLOBBER)
9488 return NULL_RTX;
9490 /* Simplify VAROP knowing that we will be only looking at some of the
9491 bits in it.
9493 Note by passing in CONSTOP, we guarantee that the bits not set in
9494 CONSTOP are not significant and will never be examined. We must
9495 ensure that is the case by explicitly masking out those bits
9496 before returning. */
9497 varop = force_to_mode (varop, mode, constop, 0);
9499 /* If VAROP is a CLOBBER, we will fail so return it. */
9500 if (GET_CODE (varop) == CLOBBER)
9501 return varop;
9503 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9504 to VAROP and return the new constant. */
9505 if (CONST_INT_P (varop))
9506 return gen_int_mode (INTVAL (varop) & constop, mode);
9508 /* See what bits may be nonzero in VAROP. Unlike the general case of
9509 a call to nonzero_bits, here we don't care about bits outside
9510 MODE. */
9512 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9514 /* Turn off all bits in the constant that are known to already be zero.
9515 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9516 which is tested below. */
9518 constop &= nonzero;
9520 /* If we don't have any bits left, return zero. */
9521 if (constop == 0)
9522 return const0_rtx;
9524 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9525 a power of two, we can replace this with an ASHIFT. */
9526 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9527 && (i = exact_log2 (constop)) >= 0)
9528 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9530 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9531 or XOR, then try to apply the distributive law. This may eliminate
9532 operations if either branch can be simplified because of the AND.
9533 It may also make some cases more complex, but those cases probably
9534 won't match a pattern either with or without this. */
9536 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9537 return
9538 gen_lowpart
9539 (mode,
9540 apply_distributive_law
9541 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9542 simplify_and_const_int (NULL_RTX,
9543 GET_MODE (varop),
9544 XEXP (varop, 0),
9545 constop),
9546 simplify_and_const_int (NULL_RTX,
9547 GET_MODE (varop),
9548 XEXP (varop, 1),
9549 constop))));
9551 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9552 the AND and see if one of the operands simplifies to zero. If so, we
9553 may eliminate it. */
9555 if (GET_CODE (varop) == PLUS
9556 && exact_log2 (constop + 1) >= 0)
9558 rtx o0, o1;
9560 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9561 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9562 if (o0 == const0_rtx)
9563 return o1;
9564 if (o1 == const0_rtx)
9565 return o0;
9568 /* Make a SUBREG if necessary. If we can't make it, fail. */
9569 varop = gen_lowpart (mode, varop);
9570 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9571 return NULL_RTX;
9573 /* If we are only masking insignificant bits, return VAROP. */
9574 if (constop == nonzero)
9575 return varop;
9577 if (varop == orig_varop && constop == orig_constop)
9578 return NULL_RTX;
9580 /* Otherwise, return an AND. */
9581 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9585 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9586 in MODE.
9588 Return an equivalent form, if different from X. Otherwise, return X. If
9589 X is zero, we are to always construct the equivalent form. */
9591 static rtx
9592 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9593 unsigned HOST_WIDE_INT constop)
9595 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9596 if (tem)
9597 return tem;
9599 if (!x)
9600 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9601 gen_int_mode (constop, mode));
9602 if (GET_MODE (x) != mode)
9603 x = gen_lowpart (mode, x);
9604 return x;
9607 /* Given a REG, X, compute which bits in X can be nonzero.
9608 We don't care about bits outside of those defined in MODE.
9610 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9611 a shift, AND, or zero_extract, we can do better. */
9613 static rtx
9614 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9615 const_rtx known_x ATTRIBUTE_UNUSED,
9616 machine_mode known_mode ATTRIBUTE_UNUSED,
9617 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9618 unsigned HOST_WIDE_INT *nonzero)
9620 rtx tem;
9621 reg_stat_type *rsp;
9623 /* If X is a register whose nonzero bits value is current, use it.
9624 Otherwise, if X is a register whose value we can find, use that
9625 value. Otherwise, use the previously-computed global nonzero bits
9626 for this register. */
9628 rsp = &reg_stat[REGNO (x)];
9629 if (rsp->last_set_value != 0
9630 && (rsp->last_set_mode == mode
9631 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9632 && GET_MODE_CLASS (mode) == MODE_INT))
9633 && ((rsp->last_set_label >= label_tick_ebb_start
9634 && rsp->last_set_label < label_tick)
9635 || (rsp->last_set_label == label_tick
9636 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9637 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9638 && REG_N_SETS (REGNO (x)) == 1
9639 && !REGNO_REG_SET_P
9640 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9641 REGNO (x)))))
9643 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9645 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9646 /* We don't know anything about the upper bits. */
9647 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9649 *nonzero &= mask;
9650 return NULL;
9653 tem = get_last_value (x);
9655 if (tem)
9657 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9658 /* If X is narrower than MODE and TEM is a non-negative
9659 constant that would appear negative in the mode of X,
9660 sign-extend it for use in reg_nonzero_bits because some
9661 machines (maybe most) will actually do the sign-extension
9662 and this is the conservative approach.
9664 ??? For 2.5, try to tighten up the MD files in this regard
9665 instead of this kludge. */
9667 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9668 && CONST_INT_P (tem)
9669 && INTVAL (tem) > 0
9670 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9671 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9672 #endif
9673 return tem;
9675 else if (nonzero_sign_valid && rsp->nonzero_bits)
9677 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9679 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9680 /* We don't know anything about the upper bits. */
9681 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9683 *nonzero &= mask;
9686 return NULL;
9689 /* Return the number of bits at the high-order end of X that are known to
9690 be equal to the sign bit. X will be used in mode MODE; if MODE is
9691 VOIDmode, X will be used in its own mode. The returned value will always
9692 be between 1 and the number of bits in MODE. */
9694 static rtx
9695 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9696 const_rtx known_x ATTRIBUTE_UNUSED,
9697 machine_mode known_mode
9698 ATTRIBUTE_UNUSED,
9699 unsigned int known_ret ATTRIBUTE_UNUSED,
9700 unsigned int *result)
9702 rtx tem;
9703 reg_stat_type *rsp;
9705 rsp = &reg_stat[REGNO (x)];
9706 if (rsp->last_set_value != 0
9707 && rsp->last_set_mode == mode
9708 && ((rsp->last_set_label >= label_tick_ebb_start
9709 && rsp->last_set_label < label_tick)
9710 || (rsp->last_set_label == label_tick
9711 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9712 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9713 && REG_N_SETS (REGNO (x)) == 1
9714 && !REGNO_REG_SET_P
9715 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9716 REGNO (x)))))
9718 *result = rsp->last_set_sign_bit_copies;
9719 return NULL;
9722 tem = get_last_value (x);
9723 if (tem != 0)
9724 return tem;
9726 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9727 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9728 *result = rsp->sign_bit_copies;
9730 return NULL;
9733 /* Return the number of "extended" bits there are in X, when interpreted
9734 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9735 unsigned quantities, this is the number of high-order zero bits.
9736 For signed quantities, this is the number of copies of the sign bit
9737 minus 1. In both case, this function returns the number of "spare"
9738 bits. For example, if two quantities for which this function returns
9739 at least 1 are added, the addition is known not to overflow.
9741 This function will always return 0 unless called during combine, which
9742 implies that it must be called from a define_split. */
9744 unsigned int
9745 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9747 if (nonzero_sign_valid == 0)
9748 return 0;
9750 return (unsignedp
9751 ? (HWI_COMPUTABLE_MODE_P (mode)
9752 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9753 - floor_log2 (nonzero_bits (x, mode)))
9754 : 0)
9755 : num_sign_bit_copies (x, mode) - 1);
9758 /* This function is called from `simplify_shift_const' to merge two
9759 outer operations. Specifically, we have already found that we need
9760 to perform operation *POP0 with constant *PCONST0 at the outermost
9761 position. We would now like to also perform OP1 with constant CONST1
9762 (with *POP0 being done last).
9764 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9765 the resulting operation. *PCOMP_P is set to 1 if we would need to
9766 complement the innermost operand, otherwise it is unchanged.
9768 MODE is the mode in which the operation will be done. No bits outside
9769 the width of this mode matter. It is assumed that the width of this mode
9770 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9772 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9773 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9774 result is simply *PCONST0.
9776 If the resulting operation cannot be expressed as one operation, we
9777 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9779 static int
9780 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)
9782 enum rtx_code op0 = *pop0;
9783 HOST_WIDE_INT const0 = *pconst0;
9785 const0 &= GET_MODE_MASK (mode);
9786 const1 &= GET_MODE_MASK (mode);
9788 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9789 if (op0 == AND)
9790 const1 &= const0;
9792 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9793 if OP0 is SET. */
9795 if (op1 == UNKNOWN || op0 == SET)
9796 return 1;
9798 else if (op0 == UNKNOWN)
9799 op0 = op1, const0 = const1;
9801 else if (op0 == op1)
9803 switch (op0)
9805 case AND:
9806 const0 &= const1;
9807 break;
9808 case IOR:
9809 const0 |= const1;
9810 break;
9811 case XOR:
9812 const0 ^= const1;
9813 break;
9814 case PLUS:
9815 const0 += const1;
9816 break;
9817 case NEG:
9818 op0 = UNKNOWN;
9819 break;
9820 default:
9821 break;
9825 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9826 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9827 return 0;
9829 /* If the two constants aren't the same, we can't do anything. The
9830 remaining six cases can all be done. */
9831 else if (const0 != const1)
9832 return 0;
9834 else
9835 switch (op0)
9837 case IOR:
9838 if (op1 == AND)
9839 /* (a & b) | b == b */
9840 op0 = SET;
9841 else /* op1 == XOR */
9842 /* (a ^ b) | b == a | b */
9844 break;
9846 case XOR:
9847 if (op1 == AND)
9848 /* (a & b) ^ b == (~a) & b */
9849 op0 = AND, *pcomp_p = 1;
9850 else /* op1 == IOR */
9851 /* (a | b) ^ b == a & ~b */
9852 op0 = AND, const0 = ~const0;
9853 break;
9855 case AND:
9856 if (op1 == IOR)
9857 /* (a | b) & b == b */
9858 op0 = SET;
9859 else /* op1 == XOR */
9860 /* (a ^ b) & b) == (~a) & b */
9861 *pcomp_p = 1;
9862 break;
9863 default:
9864 break;
9867 /* Check for NO-OP cases. */
9868 const0 &= GET_MODE_MASK (mode);
9869 if (const0 == 0
9870 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9871 op0 = UNKNOWN;
9872 else if (const0 == 0 && op0 == AND)
9873 op0 = SET;
9874 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9875 && op0 == AND)
9876 op0 = UNKNOWN;
9878 *pop0 = op0;
9880 /* ??? Slightly redundant with the above mask, but not entirely.
9881 Moving this above means we'd have to sign-extend the mode mask
9882 for the final test. */
9883 if (op0 != UNKNOWN && op0 != NEG)
9884 *pconst0 = trunc_int_for_mode (const0, mode);
9886 return 1;
9889 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9890 the shift in. The original shift operation CODE is performed on OP in
9891 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9892 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9893 result of the shift is subject to operation OUTER_CODE with operand
9894 OUTER_CONST. */
9896 static machine_mode
9897 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9898 machine_mode orig_mode, machine_mode mode,
9899 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9901 if (orig_mode == mode)
9902 return mode;
9903 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9905 /* In general we can't perform in wider mode for right shift and rotate. */
9906 switch (code)
9908 case ASHIFTRT:
9909 /* We can still widen if the bits brought in from the left are identical
9910 to the sign bit of ORIG_MODE. */
9911 if (num_sign_bit_copies (op, mode)
9912 > (unsigned) (GET_MODE_PRECISION (mode)
9913 - GET_MODE_PRECISION (orig_mode)))
9914 return mode;
9915 return orig_mode;
9917 case LSHIFTRT:
9918 /* Similarly here but with zero bits. */
9919 if (HWI_COMPUTABLE_MODE_P (mode)
9920 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9921 return mode;
9923 /* We can also widen if the bits brought in will be masked off. This
9924 operation is performed in ORIG_MODE. */
9925 if (outer_code == AND)
9927 int care_bits = low_bitmask_len (orig_mode, outer_const);
9929 if (care_bits >= 0
9930 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9931 return mode;
9933 /* fall through */
9935 case ROTATE:
9936 return orig_mode;
9938 case ROTATERT:
9939 gcc_unreachable ();
9941 default:
9942 return mode;
9946 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9947 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9948 if we cannot simplify it. Otherwise, return a simplified value.
9950 The shift is normally computed in the widest mode we find in VAROP, as
9951 long as it isn't a different number of words than RESULT_MODE. Exceptions
9952 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9954 static rtx
9955 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
9956 rtx varop, int orig_count)
9958 enum rtx_code orig_code = code;
9959 rtx orig_varop = varop;
9960 int count;
9961 machine_mode mode = result_mode;
9962 machine_mode shift_mode, tmode;
9963 unsigned int mode_words
9964 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9965 /* We form (outer_op (code varop count) (outer_const)). */
9966 enum rtx_code outer_op = UNKNOWN;
9967 HOST_WIDE_INT outer_const = 0;
9968 int complement_p = 0;
9969 rtx new_rtx, x;
9971 /* Make sure and truncate the "natural" shift on the way in. We don't
9972 want to do this inside the loop as it makes it more difficult to
9973 combine shifts. */
9974 if (SHIFT_COUNT_TRUNCATED)
9975 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9977 /* If we were given an invalid count, don't do anything except exactly
9978 what was requested. */
9980 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9981 return NULL_RTX;
9983 count = orig_count;
9985 /* Unless one of the branches of the `if' in this loop does a `continue',
9986 we will `break' the loop after the `if'. */
9988 while (count != 0)
9990 /* If we have an operand of (clobber (const_int 0)), fail. */
9991 if (GET_CODE (varop) == CLOBBER)
9992 return NULL_RTX;
9994 /* Convert ROTATERT to ROTATE. */
9995 if (code == ROTATERT)
9997 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9998 code = ROTATE;
9999 if (VECTOR_MODE_P (result_mode))
10000 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10001 else
10002 count = bitsize - count;
10005 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10006 mode, outer_op, outer_const);
10008 /* Handle cases where the count is greater than the size of the mode
10009 minus 1. For ASHIFT, use the size minus one as the count (this can
10010 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10011 take the count modulo the size. For other shifts, the result is
10012 zero.
10014 Since these shifts are being produced by the compiler by combining
10015 multiple operations, each of which are defined, we know what the
10016 result is supposed to be. */
10018 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10020 if (code == ASHIFTRT)
10021 count = GET_MODE_PRECISION (shift_mode) - 1;
10022 else if (code == ROTATE || code == ROTATERT)
10023 count %= GET_MODE_PRECISION (shift_mode);
10024 else
10026 /* We can't simply return zero because there may be an
10027 outer op. */
10028 varop = const0_rtx;
10029 count = 0;
10030 break;
10034 /* If we discovered we had to complement VAROP, leave. Making a NOT
10035 here would cause an infinite loop. */
10036 if (complement_p)
10037 break;
10039 /* An arithmetic right shift of a quantity known to be -1 or 0
10040 is a no-op. */
10041 if (code == ASHIFTRT
10042 && (num_sign_bit_copies (varop, shift_mode)
10043 == GET_MODE_PRECISION (shift_mode)))
10045 count = 0;
10046 break;
10049 /* If we are doing an arithmetic right shift and discarding all but
10050 the sign bit copies, this is equivalent to doing a shift by the
10051 bitsize minus one. Convert it into that shift because it will often
10052 allow other simplifications. */
10054 if (code == ASHIFTRT
10055 && (count + num_sign_bit_copies (varop, shift_mode)
10056 >= GET_MODE_PRECISION (shift_mode)))
10057 count = GET_MODE_PRECISION (shift_mode) - 1;
10059 /* We simplify the tests below and elsewhere by converting
10060 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10061 `make_compound_operation' will convert it to an ASHIFTRT for
10062 those machines (such as VAX) that don't have an LSHIFTRT. */
10063 if (code == ASHIFTRT
10064 && val_signbit_known_clear_p (shift_mode,
10065 nonzero_bits (varop, shift_mode)))
10066 code = LSHIFTRT;
10068 if (((code == LSHIFTRT
10069 && HWI_COMPUTABLE_MODE_P (shift_mode)
10070 && !(nonzero_bits (varop, shift_mode) >> count))
10071 || (code == ASHIFT
10072 && HWI_COMPUTABLE_MODE_P (shift_mode)
10073 && !((nonzero_bits (varop, shift_mode) << count)
10074 & GET_MODE_MASK (shift_mode))))
10075 && !side_effects_p (varop))
10076 varop = const0_rtx;
10078 switch (GET_CODE (varop))
10080 case SIGN_EXTEND:
10081 case ZERO_EXTEND:
10082 case SIGN_EXTRACT:
10083 case ZERO_EXTRACT:
10084 new_rtx = expand_compound_operation (varop);
10085 if (new_rtx != varop)
10087 varop = new_rtx;
10088 continue;
10090 break;
10092 case MEM:
10093 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10094 minus the width of a smaller mode, we can do this with a
10095 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10096 if ((code == ASHIFTRT || code == LSHIFTRT)
10097 && ! mode_dependent_address_p (XEXP (varop, 0),
10098 MEM_ADDR_SPACE (varop))
10099 && ! MEM_VOLATILE_P (varop)
10100 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10101 MODE_INT, 1)) != BLKmode)
10103 new_rtx = adjust_address_nv (varop, tmode,
10104 BYTES_BIG_ENDIAN ? 0
10105 : count / BITS_PER_UNIT);
10107 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10108 : ZERO_EXTEND, mode, new_rtx);
10109 count = 0;
10110 continue;
10112 break;
10114 case SUBREG:
10115 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10116 the same number of words as what we've seen so far. Then store
10117 the widest mode in MODE. */
10118 if (subreg_lowpart_p (varop)
10119 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10120 > GET_MODE_SIZE (GET_MODE (varop)))
10121 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10122 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10123 == mode_words
10124 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10125 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10127 varop = SUBREG_REG (varop);
10128 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10129 mode = GET_MODE (varop);
10130 continue;
10132 break;
10134 case MULT:
10135 /* Some machines use MULT instead of ASHIFT because MULT
10136 is cheaper. But it is still better on those machines to
10137 merge two shifts into one. */
10138 if (CONST_INT_P (XEXP (varop, 1))
10139 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10141 varop
10142 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10143 XEXP (varop, 0),
10144 GEN_INT (exact_log2 (
10145 UINTVAL (XEXP (varop, 1)))));
10146 continue;
10148 break;
10150 case UDIV:
10151 /* Similar, for when divides are cheaper. */
10152 if (CONST_INT_P (XEXP (varop, 1))
10153 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10155 varop
10156 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10157 XEXP (varop, 0),
10158 GEN_INT (exact_log2 (
10159 UINTVAL (XEXP (varop, 1)))));
10160 continue;
10162 break;
10164 case ASHIFTRT:
10165 /* If we are extracting just the sign bit of an arithmetic
10166 right shift, that shift is not needed. However, the sign
10167 bit of a wider mode may be different from what would be
10168 interpreted as the sign bit in a narrower mode, so, if
10169 the result is narrower, don't discard the shift. */
10170 if (code == LSHIFTRT
10171 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10172 && (GET_MODE_BITSIZE (result_mode)
10173 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10175 varop = XEXP (varop, 0);
10176 continue;
10179 /* ... fall through ... */
10181 case LSHIFTRT:
10182 case ASHIFT:
10183 case ROTATE:
10184 /* Here we have two nested shifts. The result is usually the
10185 AND of a new shift with a mask. We compute the result below. */
10186 if (CONST_INT_P (XEXP (varop, 1))
10187 && INTVAL (XEXP (varop, 1)) >= 0
10188 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10189 && HWI_COMPUTABLE_MODE_P (result_mode)
10190 && HWI_COMPUTABLE_MODE_P (mode)
10191 && !VECTOR_MODE_P (result_mode))
10193 enum rtx_code first_code = GET_CODE (varop);
10194 unsigned int first_count = INTVAL (XEXP (varop, 1));
10195 unsigned HOST_WIDE_INT mask;
10196 rtx mask_rtx;
10198 /* We have one common special case. We can't do any merging if
10199 the inner code is an ASHIFTRT of a smaller mode. However, if
10200 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10201 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10202 we can convert it to
10203 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10204 This simplifies certain SIGN_EXTEND operations. */
10205 if (code == ASHIFT && first_code == ASHIFTRT
10206 && count == (GET_MODE_PRECISION (result_mode)
10207 - GET_MODE_PRECISION (GET_MODE (varop))))
10209 /* C3 has the low-order C1 bits zero. */
10211 mask = GET_MODE_MASK (mode)
10212 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10214 varop = simplify_and_const_int (NULL_RTX, result_mode,
10215 XEXP (varop, 0), mask);
10216 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10217 varop, count);
10218 count = first_count;
10219 code = ASHIFTRT;
10220 continue;
10223 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10224 than C1 high-order bits equal to the sign bit, we can convert
10225 this to either an ASHIFT or an ASHIFTRT depending on the
10226 two counts.
10228 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10230 if (code == ASHIFTRT && first_code == ASHIFT
10231 && GET_MODE (varop) == shift_mode
10232 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10233 > first_count))
10235 varop = XEXP (varop, 0);
10236 count -= first_count;
10237 if (count < 0)
10239 count = -count;
10240 code = ASHIFT;
10243 continue;
10246 /* There are some cases we can't do. If CODE is ASHIFTRT,
10247 we can only do this if FIRST_CODE is also ASHIFTRT.
10249 We can't do the case when CODE is ROTATE and FIRST_CODE is
10250 ASHIFTRT.
10252 If the mode of this shift is not the mode of the outer shift,
10253 we can't do this if either shift is a right shift or ROTATE.
10255 Finally, we can't do any of these if the mode is too wide
10256 unless the codes are the same.
10258 Handle the case where the shift codes are the same
10259 first. */
10261 if (code == first_code)
10263 if (GET_MODE (varop) != result_mode
10264 && (code == ASHIFTRT || code == LSHIFTRT
10265 || code == ROTATE))
10266 break;
10268 count += first_count;
10269 varop = XEXP (varop, 0);
10270 continue;
10273 if (code == ASHIFTRT
10274 || (code == ROTATE && first_code == ASHIFTRT)
10275 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10276 || (GET_MODE (varop) != result_mode
10277 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10278 || first_code == ROTATE
10279 || code == ROTATE)))
10280 break;
10282 /* To compute the mask to apply after the shift, shift the
10283 nonzero bits of the inner shift the same way the
10284 outer shift will. */
10286 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10287 result_mode);
10289 mask_rtx
10290 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10291 GEN_INT (count));
10293 /* Give up if we can't compute an outer operation to use. */
10294 if (mask_rtx == 0
10295 || !CONST_INT_P (mask_rtx)
10296 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10297 INTVAL (mask_rtx),
10298 result_mode, &complement_p))
10299 break;
10301 /* If the shifts are in the same direction, we add the
10302 counts. Otherwise, we subtract them. */
10303 if ((code == ASHIFTRT || code == LSHIFTRT)
10304 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10305 count += first_count;
10306 else
10307 count -= first_count;
10309 /* If COUNT is positive, the new shift is usually CODE,
10310 except for the two exceptions below, in which case it is
10311 FIRST_CODE. If the count is negative, FIRST_CODE should
10312 always be used */
10313 if (count > 0
10314 && ((first_code == ROTATE && code == ASHIFT)
10315 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10316 code = first_code;
10317 else if (count < 0)
10318 code = first_code, count = -count;
10320 varop = XEXP (varop, 0);
10321 continue;
10324 /* If we have (A << B << C) for any shift, we can convert this to
10325 (A << C << B). This wins if A is a constant. Only try this if
10326 B is not a constant. */
10328 else if (GET_CODE (varop) == code
10329 && CONST_INT_P (XEXP (varop, 0))
10330 && !CONST_INT_P (XEXP (varop, 1)))
10332 rtx new_rtx = simplify_const_binary_operation (code, mode,
10333 XEXP (varop, 0),
10334 GEN_INT (count));
10335 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10336 count = 0;
10337 continue;
10339 break;
10341 case NOT:
10342 if (VECTOR_MODE_P (mode))
10343 break;
10345 /* Make this fit the case below. */
10346 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10347 continue;
10349 case IOR:
10350 case AND:
10351 case XOR:
10352 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10353 with C the size of VAROP - 1 and the shift is logical if
10354 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10355 we have an (le X 0) operation. If we have an arithmetic shift
10356 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10357 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10359 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10360 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10361 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10362 && (code == LSHIFTRT || code == ASHIFTRT)
10363 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10364 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10366 count = 0;
10367 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10368 const0_rtx);
10370 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10371 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10373 continue;
10376 /* If we have (shift (logical)), move the logical to the outside
10377 to allow it to possibly combine with another logical and the
10378 shift to combine with another shift. This also canonicalizes to
10379 what a ZERO_EXTRACT looks like. Also, some machines have
10380 (and (shift)) insns. */
10382 if (CONST_INT_P (XEXP (varop, 1))
10383 /* We can't do this if we have (ashiftrt (xor)) and the
10384 constant has its sign bit set in shift_mode with shift_mode
10385 wider than result_mode. */
10386 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10387 && result_mode != shift_mode
10388 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10389 shift_mode))
10390 && (new_rtx = simplify_const_binary_operation
10391 (code, result_mode,
10392 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10393 GEN_INT (count))) != 0
10394 && CONST_INT_P (new_rtx)
10395 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10396 INTVAL (new_rtx), result_mode, &complement_p))
10398 varop = XEXP (varop, 0);
10399 continue;
10402 /* If we can't do that, try to simplify the shift in each arm of the
10403 logical expression, make a new logical expression, and apply
10404 the inverse distributive law. This also can't be done for
10405 (ashiftrt (xor)) where we've widened the shift and the constant
10406 changes the sign bit. */
10407 if (CONST_INT_P (XEXP (varop, 1))
10408 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10409 && result_mode != shift_mode
10410 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10411 shift_mode)))
10413 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10414 XEXP (varop, 0), count);
10415 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10416 XEXP (varop, 1), count);
10418 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10419 lhs, rhs);
10420 varop = apply_distributive_law (varop);
10422 count = 0;
10423 continue;
10425 break;
10427 case EQ:
10428 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10429 says that the sign bit can be tested, FOO has mode MODE, C is
10430 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10431 that may be nonzero. */
10432 if (code == LSHIFTRT
10433 && XEXP (varop, 1) == const0_rtx
10434 && GET_MODE (XEXP (varop, 0)) == result_mode
10435 && count == (GET_MODE_PRECISION (result_mode) - 1)
10436 && HWI_COMPUTABLE_MODE_P (result_mode)
10437 && STORE_FLAG_VALUE == -1
10438 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10439 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10440 &complement_p))
10442 varop = XEXP (varop, 0);
10443 count = 0;
10444 continue;
10446 break;
10448 case NEG:
10449 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10450 than the number of bits in the mode is equivalent to A. */
10451 if (code == LSHIFTRT
10452 && count == (GET_MODE_PRECISION (result_mode) - 1)
10453 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10455 varop = XEXP (varop, 0);
10456 count = 0;
10457 continue;
10460 /* NEG commutes with ASHIFT since it is multiplication. Move the
10461 NEG outside to allow shifts to combine. */
10462 if (code == ASHIFT
10463 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10464 &complement_p))
10466 varop = XEXP (varop, 0);
10467 continue;
10469 break;
10471 case PLUS:
10472 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10473 is one less than the number of bits in the mode is
10474 equivalent to (xor A 1). */
10475 if (code == LSHIFTRT
10476 && count == (GET_MODE_PRECISION (result_mode) - 1)
10477 && XEXP (varop, 1) == constm1_rtx
10478 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10479 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10480 &complement_p))
10482 count = 0;
10483 varop = XEXP (varop, 0);
10484 continue;
10487 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10488 that might be nonzero in BAR are those being shifted out and those
10489 bits are known zero in FOO, we can replace the PLUS with FOO.
10490 Similarly in the other operand order. This code occurs when
10491 we are computing the size of a variable-size array. */
10493 if ((code == ASHIFTRT || code == LSHIFTRT)
10494 && count < HOST_BITS_PER_WIDE_INT
10495 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10496 && (nonzero_bits (XEXP (varop, 1), result_mode)
10497 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10499 varop = XEXP (varop, 0);
10500 continue;
10502 else if ((code == ASHIFTRT || code == LSHIFTRT)
10503 && count < HOST_BITS_PER_WIDE_INT
10504 && HWI_COMPUTABLE_MODE_P (result_mode)
10505 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10506 >> count)
10507 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10508 & nonzero_bits (XEXP (varop, 1),
10509 result_mode)))
10511 varop = XEXP (varop, 1);
10512 continue;
10515 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10516 if (code == ASHIFT
10517 && CONST_INT_P (XEXP (varop, 1))
10518 && (new_rtx = simplify_const_binary_operation
10519 (ASHIFT, result_mode,
10520 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10521 GEN_INT (count))) != 0
10522 && CONST_INT_P (new_rtx)
10523 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10524 INTVAL (new_rtx), result_mode, &complement_p))
10526 varop = XEXP (varop, 0);
10527 continue;
10530 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10531 signbit', and attempt to change the PLUS to an XOR and move it to
10532 the outer operation as is done above in the AND/IOR/XOR case
10533 leg for shift(logical). See details in logical handling above
10534 for reasoning in doing so. */
10535 if (code == LSHIFTRT
10536 && CONST_INT_P (XEXP (varop, 1))
10537 && mode_signbit_p (result_mode, XEXP (varop, 1))
10538 && (new_rtx = simplify_const_binary_operation
10539 (code, result_mode,
10540 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10541 GEN_INT (count))) != 0
10542 && CONST_INT_P (new_rtx)
10543 && merge_outer_ops (&outer_op, &outer_const, XOR,
10544 INTVAL (new_rtx), result_mode, &complement_p))
10546 varop = XEXP (varop, 0);
10547 continue;
10550 break;
10552 case MINUS:
10553 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10554 with C the size of VAROP - 1 and the shift is logical if
10555 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10556 we have a (gt X 0) operation. If the shift is arithmetic with
10557 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10558 we have a (neg (gt X 0)) operation. */
10560 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10561 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10562 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10563 && (code == LSHIFTRT || code == ASHIFTRT)
10564 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10565 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10566 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10568 count = 0;
10569 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10570 const0_rtx);
10572 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10573 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10575 continue;
10577 break;
10579 case TRUNCATE:
10580 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10581 if the truncate does not affect the value. */
10582 if (code == LSHIFTRT
10583 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10584 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10585 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10586 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10587 - GET_MODE_PRECISION (GET_MODE (varop)))))
10589 rtx varop_inner = XEXP (varop, 0);
10591 varop_inner
10592 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10593 XEXP (varop_inner, 0),
10594 GEN_INT
10595 (count + INTVAL (XEXP (varop_inner, 1))));
10596 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10597 count = 0;
10598 continue;
10600 break;
10602 default:
10603 break;
10606 break;
10609 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10610 outer_op, outer_const);
10612 /* We have now finished analyzing the shift. The result should be
10613 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10614 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10615 to the result of the shift. OUTER_CONST is the relevant constant,
10616 but we must turn off all bits turned off in the shift. */
10618 if (outer_op == UNKNOWN
10619 && orig_code == code && orig_count == count
10620 && varop == orig_varop
10621 && shift_mode == GET_MODE (varop))
10622 return NULL_RTX;
10624 /* Make a SUBREG if necessary. If we can't make it, fail. */
10625 varop = gen_lowpart (shift_mode, varop);
10626 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10627 return NULL_RTX;
10629 /* If we have an outer operation and we just made a shift, it is
10630 possible that we could have simplified the shift were it not
10631 for the outer operation. So try to do the simplification
10632 recursively. */
10634 if (outer_op != UNKNOWN)
10635 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10636 else
10637 x = NULL_RTX;
10639 if (x == NULL_RTX)
10640 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10642 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10643 turn off all the bits that the shift would have turned off. */
10644 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10645 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10646 GET_MODE_MASK (result_mode) >> orig_count);
10648 /* Do the remainder of the processing in RESULT_MODE. */
10649 x = gen_lowpart_or_truncate (result_mode, x);
10651 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10652 operation. */
10653 if (complement_p)
10654 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10656 if (outer_op != UNKNOWN)
10658 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10659 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10660 outer_const = trunc_int_for_mode (outer_const, result_mode);
10662 if (outer_op == AND)
10663 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10664 else if (outer_op == SET)
10666 /* This means that we have determined that the result is
10667 equivalent to a constant. This should be rare. */
10668 if (!side_effects_p (x))
10669 x = GEN_INT (outer_const);
10671 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10672 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10673 else
10674 x = simplify_gen_binary (outer_op, result_mode, x,
10675 GEN_INT (outer_const));
10678 return x;
10681 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10682 The result of the shift is RESULT_MODE. If we cannot simplify it,
10683 return X or, if it is NULL, synthesize the expression with
10684 simplify_gen_binary. Otherwise, return a simplified value.
10686 The shift is normally computed in the widest mode we find in VAROP, as
10687 long as it isn't a different number of words than RESULT_MODE. Exceptions
10688 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10690 static rtx
10691 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10692 rtx varop, int count)
10694 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10695 if (tem)
10696 return tem;
10698 if (!x)
10699 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10700 if (GET_MODE (x) != result_mode)
10701 x = gen_lowpart (result_mode, x);
10702 return x;
10706 /* Like recog, but we receive the address of a pointer to a new pattern.
10707 We try to match the rtx that the pointer points to.
10708 If that fails, we may try to modify or replace the pattern,
10709 storing the replacement into the same pointer object.
10711 Modifications include deletion or addition of CLOBBERs.
10713 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10714 the CLOBBERs are placed.
10716 The value is the final insn code from the pattern ultimately matched,
10717 or -1. */
10719 static int
10720 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10722 rtx pat = *pnewpat;
10723 rtx pat_without_clobbers;
10724 int insn_code_number;
10725 int num_clobbers_to_add = 0;
10726 int i;
10727 rtx notes = NULL_RTX;
10728 rtx old_notes, old_pat;
10729 int old_icode;
10731 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10732 we use to indicate that something didn't match. If we find such a
10733 thing, force rejection. */
10734 if (GET_CODE (pat) == PARALLEL)
10735 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10736 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10737 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10738 return -1;
10740 old_pat = PATTERN (insn);
10741 old_notes = REG_NOTES (insn);
10742 PATTERN (insn) = pat;
10743 REG_NOTES (insn) = NULL_RTX;
10745 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10746 if (dump_file && (dump_flags & TDF_DETAILS))
10748 if (insn_code_number < 0)
10749 fputs ("Failed to match this instruction:\n", dump_file);
10750 else
10751 fputs ("Successfully matched this instruction:\n", dump_file);
10752 print_rtl_single (dump_file, pat);
10755 /* If it isn't, there is the possibility that we previously had an insn
10756 that clobbered some register as a side effect, but the combined
10757 insn doesn't need to do that. So try once more without the clobbers
10758 unless this represents an ASM insn. */
10760 if (insn_code_number < 0 && ! check_asm_operands (pat)
10761 && GET_CODE (pat) == PARALLEL)
10763 int pos;
10765 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10766 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10768 if (i != pos)
10769 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10770 pos++;
10773 SUBST_INT (XVECLEN (pat, 0), pos);
10775 if (pos == 1)
10776 pat = XVECEXP (pat, 0, 0);
10778 PATTERN (insn) = pat;
10779 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10780 if (dump_file && (dump_flags & TDF_DETAILS))
10782 if (insn_code_number < 0)
10783 fputs ("Failed to match this instruction:\n", dump_file);
10784 else
10785 fputs ("Successfully matched this instruction:\n", dump_file);
10786 print_rtl_single (dump_file, pat);
10790 pat_without_clobbers = pat;
10792 PATTERN (insn) = old_pat;
10793 REG_NOTES (insn) = old_notes;
10795 /* Recognize all noop sets, these will be killed by followup pass. */
10796 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10797 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10799 /* If we had any clobbers to add, make a new pattern than contains
10800 them. Then check to make sure that all of them are dead. */
10801 if (num_clobbers_to_add)
10803 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10804 rtvec_alloc (GET_CODE (pat) == PARALLEL
10805 ? (XVECLEN (pat, 0)
10806 + num_clobbers_to_add)
10807 : num_clobbers_to_add + 1));
10809 if (GET_CODE (pat) == PARALLEL)
10810 for (i = 0; i < XVECLEN (pat, 0); i++)
10811 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10812 else
10813 XVECEXP (newpat, 0, 0) = pat;
10815 add_clobbers (newpat, insn_code_number);
10817 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10818 i < XVECLEN (newpat, 0); i++)
10820 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10821 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10822 return -1;
10823 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10825 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10826 notes = alloc_reg_note (REG_UNUSED,
10827 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10830 pat = newpat;
10833 if (insn_code_number >= 0
10834 && insn_code_number != NOOP_MOVE_INSN_CODE)
10836 old_pat = PATTERN (insn);
10837 old_notes = REG_NOTES (insn);
10838 old_icode = INSN_CODE (insn);
10839 PATTERN (insn) = pat;
10840 REG_NOTES (insn) = notes;
10842 /* Allow targets to reject combined insn. */
10843 if (!targetm.legitimate_combined_insn (insn))
10845 if (dump_file && (dump_flags & TDF_DETAILS))
10846 fputs ("Instruction not appropriate for target.",
10847 dump_file);
10849 /* Callers expect recog_for_combine to strip
10850 clobbers from the pattern on failure. */
10851 pat = pat_without_clobbers;
10852 notes = NULL_RTX;
10854 insn_code_number = -1;
10857 PATTERN (insn) = old_pat;
10858 REG_NOTES (insn) = old_notes;
10859 INSN_CODE (insn) = old_icode;
10862 *pnewpat = pat;
10863 *pnotes = notes;
10865 return insn_code_number;
10868 /* Like gen_lowpart_general but for use by combine. In combine it
10869 is not possible to create any new pseudoregs. However, it is
10870 safe to create invalid memory addresses, because combine will
10871 try to recognize them and all they will do is make the combine
10872 attempt fail.
10874 If for some reason this cannot do its job, an rtx
10875 (clobber (const_int 0)) is returned.
10876 An insn containing that will not be recognized. */
10878 static rtx
10879 gen_lowpart_for_combine (machine_mode omode, rtx x)
10881 machine_mode imode = GET_MODE (x);
10882 unsigned int osize = GET_MODE_SIZE (omode);
10883 unsigned int isize = GET_MODE_SIZE (imode);
10884 rtx result;
10886 if (omode == imode)
10887 return x;
10889 /* We can only support MODE being wider than a word if X is a
10890 constant integer or has a mode the same size. */
10891 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10892 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10893 goto fail;
10895 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10896 won't know what to do. So we will strip off the SUBREG here and
10897 process normally. */
10898 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10900 x = SUBREG_REG (x);
10902 /* For use in case we fall down into the address adjustments
10903 further below, we need to adjust the known mode and size of
10904 x; imode and isize, since we just adjusted x. */
10905 imode = GET_MODE (x);
10907 if (imode == omode)
10908 return x;
10910 isize = GET_MODE_SIZE (imode);
10913 result = gen_lowpart_common (omode, x);
10915 if (result)
10916 return result;
10918 if (MEM_P (x))
10920 int offset = 0;
10922 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10923 address. */
10924 if (MEM_VOLATILE_P (x)
10925 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10926 goto fail;
10928 /* If we want to refer to something bigger than the original memref,
10929 generate a paradoxical subreg instead. That will force a reload
10930 of the original memref X. */
10931 if (isize < osize)
10932 return gen_rtx_SUBREG (omode, x, 0);
10934 if (WORDS_BIG_ENDIAN)
10935 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10937 /* Adjust the address so that the address-after-the-data is
10938 unchanged. */
10939 if (BYTES_BIG_ENDIAN)
10940 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10942 return adjust_address_nv (x, omode, offset);
10945 /* If X is a comparison operator, rewrite it in a new mode. This
10946 probably won't match, but may allow further simplifications. */
10947 else if (COMPARISON_P (x))
10948 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10950 /* If we couldn't simplify X any other way, just enclose it in a
10951 SUBREG. Normally, this SUBREG won't match, but some patterns may
10952 include an explicit SUBREG or we may simplify it further in combine. */
10953 else
10955 int offset = 0;
10956 rtx res;
10958 offset = subreg_lowpart_offset (omode, imode);
10959 if (imode == VOIDmode)
10961 imode = int_mode_for_mode (omode);
10962 x = gen_lowpart_common (imode, x);
10963 if (x == NULL)
10964 goto fail;
10966 res = simplify_gen_subreg (omode, x, imode, offset);
10967 if (res)
10968 return res;
10971 fail:
10972 return gen_rtx_CLOBBER (omode, const0_rtx);
10975 /* Try to simplify a comparison between OP0 and a constant OP1,
10976 where CODE is the comparison code that will be tested, into a
10977 (CODE OP0 const0_rtx) form.
10979 The result is a possibly different comparison code to use.
10980 *POP1 may be updated. */
10982 static enum rtx_code
10983 simplify_compare_const (enum rtx_code code, machine_mode mode,
10984 rtx op0, rtx *pop1)
10986 unsigned int mode_width = GET_MODE_PRECISION (mode);
10987 HOST_WIDE_INT const_op = INTVAL (*pop1);
10989 /* Get the constant we are comparing against and turn off all bits
10990 not on in our mode. */
10991 if (mode != VOIDmode)
10992 const_op = trunc_int_for_mode (const_op, mode);
10994 /* If we are comparing against a constant power of two and the value
10995 being compared can only have that single bit nonzero (e.g., it was
10996 `and'ed with that bit), we can replace this with a comparison
10997 with zero. */
10998 if (const_op
10999 && (code == EQ || code == NE || code == GE || code == GEU
11000 || code == LT || code == LTU)
11001 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11002 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11003 && (nonzero_bits (op0, mode)
11004 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11006 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11007 const_op = 0;
11010 /* Similarly, if we are comparing a value known to be either -1 or
11011 0 with -1, change it to the opposite comparison against zero. */
11012 if (const_op == -1
11013 && (code == EQ || code == NE || code == GT || code == LE
11014 || code == GEU || code == LTU)
11015 && num_sign_bit_copies (op0, mode) == mode_width)
11017 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11018 const_op = 0;
11021 /* Do some canonicalizations based on the comparison code. We prefer
11022 comparisons against zero and then prefer equality comparisons.
11023 If we can reduce the size of a constant, we will do that too. */
11024 switch (code)
11026 case LT:
11027 /* < C is equivalent to <= (C - 1) */
11028 if (const_op > 0)
11030 const_op -= 1;
11031 code = LE;
11032 /* ... fall through to LE case below. */
11034 else
11035 break;
11037 case LE:
11038 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11039 if (const_op < 0)
11041 const_op += 1;
11042 code = LT;
11045 /* If we are doing a <= 0 comparison on a value known to have
11046 a zero sign bit, we can replace this with == 0. */
11047 else if (const_op == 0
11048 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11049 && (nonzero_bits (op0, mode)
11050 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11051 == 0)
11052 code = EQ;
11053 break;
11055 case GE:
11056 /* >= C is equivalent to > (C - 1). */
11057 if (const_op > 0)
11059 const_op -= 1;
11060 code = GT;
11061 /* ... fall through to GT below. */
11063 else
11064 break;
11066 case GT:
11067 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11068 if (const_op < 0)
11070 const_op += 1;
11071 code = GE;
11074 /* If we are doing a > 0 comparison on a value known to have
11075 a zero sign bit, we can replace this with != 0. */
11076 else if (const_op == 0
11077 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11078 && (nonzero_bits (op0, mode)
11079 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11080 == 0)
11081 code = NE;
11082 break;
11084 case LTU:
11085 /* < C is equivalent to <= (C - 1). */
11086 if (const_op > 0)
11088 const_op -= 1;
11089 code = LEU;
11090 /* ... fall through ... */
11092 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11093 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11094 && (unsigned HOST_WIDE_INT) const_op
11095 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11097 const_op = 0;
11098 code = GE;
11099 break;
11101 else
11102 break;
11104 case LEU:
11105 /* unsigned <= 0 is equivalent to == 0 */
11106 if (const_op == 0)
11107 code = EQ;
11108 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11109 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11110 && (unsigned HOST_WIDE_INT) const_op
11111 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11113 const_op = 0;
11114 code = GE;
11116 break;
11118 case GEU:
11119 /* >= C is equivalent to > (C - 1). */
11120 if (const_op > 1)
11122 const_op -= 1;
11123 code = GTU;
11124 /* ... fall through ... */
11127 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11128 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11129 && (unsigned HOST_WIDE_INT) const_op
11130 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11132 const_op = 0;
11133 code = LT;
11134 break;
11136 else
11137 break;
11139 case GTU:
11140 /* unsigned > 0 is equivalent to != 0 */
11141 if (const_op == 0)
11142 code = NE;
11143 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11144 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11145 && (unsigned HOST_WIDE_INT) const_op
11146 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11148 const_op = 0;
11149 code = LT;
11151 break;
11153 default:
11154 break;
11157 *pop1 = GEN_INT (const_op);
11158 return code;
11161 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11162 comparison code that will be tested.
11164 The result is a possibly different comparison code to use. *POP0 and
11165 *POP1 may be updated.
11167 It is possible that we might detect that a comparison is either always
11168 true or always false. However, we do not perform general constant
11169 folding in combine, so this knowledge isn't useful. Such tautologies
11170 should have been detected earlier. Hence we ignore all such cases. */
11172 static enum rtx_code
11173 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11175 rtx op0 = *pop0;
11176 rtx op1 = *pop1;
11177 rtx tem, tem1;
11178 int i;
11179 machine_mode mode, tmode;
11181 /* Try a few ways of applying the same transformation to both operands. */
11182 while (1)
11184 #ifndef WORD_REGISTER_OPERATIONS
11185 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11186 so check specially. */
11187 if (code != GTU && code != GEU && code != LTU && code != LEU
11188 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11189 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11190 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11191 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11192 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11193 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11194 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11195 && CONST_INT_P (XEXP (op0, 1))
11196 && XEXP (op0, 1) == XEXP (op1, 1)
11197 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11198 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11199 && (INTVAL (XEXP (op0, 1))
11200 == (GET_MODE_PRECISION (GET_MODE (op0))
11201 - (GET_MODE_PRECISION
11202 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11204 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11205 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11207 #endif
11209 /* If both operands are the same constant shift, see if we can ignore the
11210 shift. We can if the shift is a rotate or if the bits shifted out of
11211 this shift are known to be zero for both inputs and if the type of
11212 comparison is compatible with the shift. */
11213 if (GET_CODE (op0) == GET_CODE (op1)
11214 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11215 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11216 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11217 && (code != GT && code != LT && code != GE && code != LE))
11218 || (GET_CODE (op0) == ASHIFTRT
11219 && (code != GTU && code != LTU
11220 && code != GEU && code != LEU)))
11221 && CONST_INT_P (XEXP (op0, 1))
11222 && INTVAL (XEXP (op0, 1)) >= 0
11223 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11224 && XEXP (op0, 1) == XEXP (op1, 1))
11226 machine_mode mode = GET_MODE (op0);
11227 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11228 int shift_count = INTVAL (XEXP (op0, 1));
11230 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11231 mask &= (mask >> shift_count) << shift_count;
11232 else if (GET_CODE (op0) == ASHIFT)
11233 mask = (mask & (mask << shift_count)) >> shift_count;
11235 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11236 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11237 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11238 else
11239 break;
11242 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11243 SUBREGs are of the same mode, and, in both cases, the AND would
11244 be redundant if the comparison was done in the narrower mode,
11245 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11246 and the operand's possibly nonzero bits are 0xffffff01; in that case
11247 if we only care about QImode, we don't need the AND). This case
11248 occurs if the output mode of an scc insn is not SImode and
11249 STORE_FLAG_VALUE == 1 (e.g., the 386).
11251 Similarly, check for a case where the AND's are ZERO_EXTEND
11252 operations from some narrower mode even though a SUBREG is not
11253 present. */
11255 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11256 && CONST_INT_P (XEXP (op0, 1))
11257 && CONST_INT_P (XEXP (op1, 1)))
11259 rtx inner_op0 = XEXP (op0, 0);
11260 rtx inner_op1 = XEXP (op1, 0);
11261 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11262 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11263 int changed = 0;
11265 if (paradoxical_subreg_p (inner_op0)
11266 && GET_CODE (inner_op1) == SUBREG
11267 && (GET_MODE (SUBREG_REG (inner_op0))
11268 == GET_MODE (SUBREG_REG (inner_op1)))
11269 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11270 <= HOST_BITS_PER_WIDE_INT)
11271 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11272 GET_MODE (SUBREG_REG (inner_op0)))))
11273 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11274 GET_MODE (SUBREG_REG (inner_op1))))))
11276 op0 = SUBREG_REG (inner_op0);
11277 op1 = SUBREG_REG (inner_op1);
11279 /* The resulting comparison is always unsigned since we masked
11280 off the original sign bit. */
11281 code = unsigned_condition (code);
11283 changed = 1;
11286 else if (c0 == c1)
11287 for (tmode = GET_CLASS_NARROWEST_MODE
11288 (GET_MODE_CLASS (GET_MODE (op0)));
11289 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11290 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11292 op0 = gen_lowpart (tmode, inner_op0);
11293 op1 = gen_lowpart (tmode, inner_op1);
11294 code = unsigned_condition (code);
11295 changed = 1;
11296 break;
11299 if (! changed)
11300 break;
11303 /* If both operands are NOT, we can strip off the outer operation
11304 and adjust the comparison code for swapped operands; similarly for
11305 NEG, except that this must be an equality comparison. */
11306 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11307 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11308 && (code == EQ || code == NE)))
11309 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11311 else
11312 break;
11315 /* If the first operand is a constant, swap the operands and adjust the
11316 comparison code appropriately, but don't do this if the second operand
11317 is already a constant integer. */
11318 if (swap_commutative_operands_p (op0, op1))
11320 tem = op0, op0 = op1, op1 = tem;
11321 code = swap_condition (code);
11324 /* We now enter a loop during which we will try to simplify the comparison.
11325 For the most part, we only are concerned with comparisons with zero,
11326 but some things may really be comparisons with zero but not start
11327 out looking that way. */
11329 while (CONST_INT_P (op1))
11331 machine_mode mode = GET_MODE (op0);
11332 unsigned int mode_width = GET_MODE_PRECISION (mode);
11333 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11334 int equality_comparison_p;
11335 int sign_bit_comparison_p;
11336 int unsigned_comparison_p;
11337 HOST_WIDE_INT const_op;
11339 /* We only want to handle integral modes. This catches VOIDmode,
11340 CCmode, and the floating-point modes. An exception is that we
11341 can handle VOIDmode if OP0 is a COMPARE or a comparison
11342 operation. */
11344 if (GET_MODE_CLASS (mode) != MODE_INT
11345 && ! (mode == VOIDmode
11346 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11347 break;
11349 /* Try to simplify the compare to constant, possibly changing the
11350 comparison op, and/or changing op1 to zero. */
11351 code = simplify_compare_const (code, mode, op0, &op1);
11352 const_op = INTVAL (op1);
11354 /* Compute some predicates to simplify code below. */
11356 equality_comparison_p = (code == EQ || code == NE);
11357 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11358 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11359 || code == GEU);
11361 /* If this is a sign bit comparison and we can do arithmetic in
11362 MODE, say that we will only be needing the sign bit of OP0. */
11363 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11364 op0 = force_to_mode (op0, mode,
11365 (unsigned HOST_WIDE_INT) 1
11366 << (GET_MODE_PRECISION (mode) - 1),
11369 /* Now try cases based on the opcode of OP0. If none of the cases
11370 does a "continue", we exit this loop immediately after the
11371 switch. */
11373 switch (GET_CODE (op0))
11375 case ZERO_EXTRACT:
11376 /* If we are extracting a single bit from a variable position in
11377 a constant that has only a single bit set and are comparing it
11378 with zero, we can convert this into an equality comparison
11379 between the position and the location of the single bit. */
11380 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11381 have already reduced the shift count modulo the word size. */
11382 if (!SHIFT_COUNT_TRUNCATED
11383 && CONST_INT_P (XEXP (op0, 0))
11384 && XEXP (op0, 1) == const1_rtx
11385 && equality_comparison_p && const_op == 0
11386 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11388 if (BITS_BIG_ENDIAN)
11389 i = BITS_PER_WORD - 1 - i;
11391 op0 = XEXP (op0, 2);
11392 op1 = GEN_INT (i);
11393 const_op = i;
11395 /* Result is nonzero iff shift count is equal to I. */
11396 code = reverse_condition (code);
11397 continue;
11400 /* ... fall through ... */
11402 case SIGN_EXTRACT:
11403 tem = expand_compound_operation (op0);
11404 if (tem != op0)
11406 op0 = tem;
11407 continue;
11409 break;
11411 case NOT:
11412 /* If testing for equality, we can take the NOT of the constant. */
11413 if (equality_comparison_p
11414 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11416 op0 = XEXP (op0, 0);
11417 op1 = tem;
11418 continue;
11421 /* If just looking at the sign bit, reverse the sense of the
11422 comparison. */
11423 if (sign_bit_comparison_p)
11425 op0 = XEXP (op0, 0);
11426 code = (code == GE ? LT : GE);
11427 continue;
11429 break;
11431 case NEG:
11432 /* If testing for equality, we can take the NEG of the constant. */
11433 if (equality_comparison_p
11434 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11436 op0 = XEXP (op0, 0);
11437 op1 = tem;
11438 continue;
11441 /* The remaining cases only apply to comparisons with zero. */
11442 if (const_op != 0)
11443 break;
11445 /* When X is ABS or is known positive,
11446 (neg X) is < 0 if and only if X != 0. */
11448 if (sign_bit_comparison_p
11449 && (GET_CODE (XEXP (op0, 0)) == ABS
11450 || (mode_width <= HOST_BITS_PER_WIDE_INT
11451 && (nonzero_bits (XEXP (op0, 0), mode)
11452 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11453 == 0)))
11455 op0 = XEXP (op0, 0);
11456 code = (code == LT ? NE : EQ);
11457 continue;
11460 /* If we have NEG of something whose two high-order bits are the
11461 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11462 if (num_sign_bit_copies (op0, mode) >= 2)
11464 op0 = XEXP (op0, 0);
11465 code = swap_condition (code);
11466 continue;
11468 break;
11470 case ROTATE:
11471 /* If we are testing equality and our count is a constant, we
11472 can perform the inverse operation on our RHS. */
11473 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11474 && (tem = simplify_binary_operation (ROTATERT, mode,
11475 op1, XEXP (op0, 1))) != 0)
11477 op0 = XEXP (op0, 0);
11478 op1 = tem;
11479 continue;
11482 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11483 a particular bit. Convert it to an AND of a constant of that
11484 bit. This will be converted into a ZERO_EXTRACT. */
11485 if (const_op == 0 && sign_bit_comparison_p
11486 && CONST_INT_P (XEXP (op0, 1))
11487 && mode_width <= HOST_BITS_PER_WIDE_INT)
11489 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11490 ((unsigned HOST_WIDE_INT) 1
11491 << (mode_width - 1
11492 - INTVAL (XEXP (op0, 1)))));
11493 code = (code == LT ? NE : EQ);
11494 continue;
11497 /* Fall through. */
11499 case ABS:
11500 /* ABS is ignorable inside an equality comparison with zero. */
11501 if (const_op == 0 && equality_comparison_p)
11503 op0 = XEXP (op0, 0);
11504 continue;
11506 break;
11508 case SIGN_EXTEND:
11509 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11510 (compare FOO CONST) if CONST fits in FOO's mode and we
11511 are either testing inequality or have an unsigned
11512 comparison with ZERO_EXTEND or a signed comparison with
11513 SIGN_EXTEND. But don't do it if we don't have a compare
11514 insn of the given mode, since we'd have to revert it
11515 later on, and then we wouldn't know whether to sign- or
11516 zero-extend. */
11517 mode = GET_MODE (XEXP (op0, 0));
11518 if (GET_MODE_CLASS (mode) == MODE_INT
11519 && ! unsigned_comparison_p
11520 && HWI_COMPUTABLE_MODE_P (mode)
11521 && trunc_int_for_mode (const_op, mode) == const_op
11522 && have_insn_for (COMPARE, mode))
11524 op0 = XEXP (op0, 0);
11525 continue;
11527 break;
11529 case SUBREG:
11530 /* Check for the case where we are comparing A - C1 with C2, that is
11532 (subreg:MODE (plus (A) (-C1))) op (C2)
11534 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11535 comparison in the wider mode. One of the following two conditions
11536 must be true in order for this to be valid:
11538 1. The mode extension results in the same bit pattern being added
11539 on both sides and the comparison is equality or unsigned. As
11540 C2 has been truncated to fit in MODE, the pattern can only be
11541 all 0s or all 1s.
11543 2. The mode extension results in the sign bit being copied on
11544 each side.
11546 The difficulty here is that we have predicates for A but not for
11547 (A - C1) so we need to check that C1 is within proper bounds so
11548 as to perturbate A as little as possible. */
11550 if (mode_width <= HOST_BITS_PER_WIDE_INT
11551 && subreg_lowpart_p (op0)
11552 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11553 && GET_CODE (SUBREG_REG (op0)) == PLUS
11554 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11556 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11557 rtx a = XEXP (SUBREG_REG (op0), 0);
11558 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11560 if ((c1 > 0
11561 && (unsigned HOST_WIDE_INT) c1
11562 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11563 && (equality_comparison_p || unsigned_comparison_p)
11564 /* (A - C1) zero-extends if it is positive and sign-extends
11565 if it is negative, C2 both zero- and sign-extends. */
11566 && ((0 == (nonzero_bits (a, inner_mode)
11567 & ~GET_MODE_MASK (mode))
11568 && const_op >= 0)
11569 /* (A - C1) sign-extends if it is positive and 1-extends
11570 if it is negative, C2 both sign- and 1-extends. */
11571 || (num_sign_bit_copies (a, inner_mode)
11572 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11573 - mode_width)
11574 && const_op < 0)))
11575 || ((unsigned HOST_WIDE_INT) c1
11576 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11577 /* (A - C1) always sign-extends, like C2. */
11578 && num_sign_bit_copies (a, inner_mode)
11579 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11580 - (mode_width - 1))))
11582 op0 = SUBREG_REG (op0);
11583 continue;
11587 /* If the inner mode is narrower and we are extracting the low part,
11588 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11589 if (subreg_lowpart_p (op0)
11590 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11591 /* Fall through */ ;
11592 else
11593 break;
11595 /* ... fall through ... */
11597 case ZERO_EXTEND:
11598 mode = GET_MODE (XEXP (op0, 0));
11599 if (GET_MODE_CLASS (mode) == MODE_INT
11600 && (unsigned_comparison_p || equality_comparison_p)
11601 && HWI_COMPUTABLE_MODE_P (mode)
11602 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11603 && const_op >= 0
11604 && have_insn_for (COMPARE, mode))
11606 op0 = XEXP (op0, 0);
11607 continue;
11609 break;
11611 case PLUS:
11612 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11613 this for equality comparisons due to pathological cases involving
11614 overflows. */
11615 if (equality_comparison_p
11616 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11617 op1, XEXP (op0, 1))))
11619 op0 = XEXP (op0, 0);
11620 op1 = tem;
11621 continue;
11624 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11625 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11626 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11628 op0 = XEXP (XEXP (op0, 0), 0);
11629 code = (code == LT ? EQ : NE);
11630 continue;
11632 break;
11634 case MINUS:
11635 /* We used to optimize signed comparisons against zero, but that
11636 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11637 arrive here as equality comparisons, or (GEU, LTU) are
11638 optimized away. No need to special-case them. */
11640 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11641 (eq B (minus A C)), whichever simplifies. We can only do
11642 this for equality comparisons due to pathological cases involving
11643 overflows. */
11644 if (equality_comparison_p
11645 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11646 XEXP (op0, 1), op1)))
11648 op0 = XEXP (op0, 0);
11649 op1 = tem;
11650 continue;
11653 if (equality_comparison_p
11654 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11655 XEXP (op0, 0), op1)))
11657 op0 = XEXP (op0, 1);
11658 op1 = tem;
11659 continue;
11662 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11663 of bits in X minus 1, is one iff X > 0. */
11664 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11665 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11666 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11667 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11669 op0 = XEXP (op0, 1);
11670 code = (code == GE ? LE : GT);
11671 continue;
11673 break;
11675 case XOR:
11676 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11677 if C is zero or B is a constant. */
11678 if (equality_comparison_p
11679 && 0 != (tem = simplify_binary_operation (XOR, mode,
11680 XEXP (op0, 1), op1)))
11682 op0 = XEXP (op0, 0);
11683 op1 = tem;
11684 continue;
11686 break;
11688 case EQ: case NE:
11689 case UNEQ: case LTGT:
11690 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11691 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11692 case UNORDERED: case ORDERED:
11693 /* We can't do anything if OP0 is a condition code value, rather
11694 than an actual data value. */
11695 if (const_op != 0
11696 || CC0_P (XEXP (op0, 0))
11697 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11698 break;
11700 /* Get the two operands being compared. */
11701 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11702 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11703 else
11704 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11706 /* Check for the cases where we simply want the result of the
11707 earlier test or the opposite of that result. */
11708 if (code == NE || code == EQ
11709 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11710 && (code == LT || code == GE)))
11712 enum rtx_code new_code;
11713 if (code == LT || code == NE)
11714 new_code = GET_CODE (op0);
11715 else
11716 new_code = reversed_comparison_code (op0, NULL);
11718 if (new_code != UNKNOWN)
11720 code = new_code;
11721 op0 = tem;
11722 op1 = tem1;
11723 continue;
11726 break;
11728 case IOR:
11729 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11730 iff X <= 0. */
11731 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11732 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11733 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11735 op0 = XEXP (op0, 1);
11736 code = (code == GE ? GT : LE);
11737 continue;
11739 break;
11741 case AND:
11742 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11743 will be converted to a ZERO_EXTRACT later. */
11744 if (const_op == 0 && equality_comparison_p
11745 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11746 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11748 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11749 XEXP (XEXP (op0, 0), 1));
11750 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11751 continue;
11754 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11755 zero and X is a comparison and C1 and C2 describe only bits set
11756 in STORE_FLAG_VALUE, we can compare with X. */
11757 if (const_op == 0 && equality_comparison_p
11758 && mode_width <= HOST_BITS_PER_WIDE_INT
11759 && CONST_INT_P (XEXP (op0, 1))
11760 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11761 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11762 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11763 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11765 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11766 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11767 if ((~STORE_FLAG_VALUE & mask) == 0
11768 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11769 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11770 && COMPARISON_P (tem))))
11772 op0 = XEXP (XEXP (op0, 0), 0);
11773 continue;
11777 /* If we are doing an equality comparison of an AND of a bit equal
11778 to the sign bit, replace this with a LT or GE comparison of
11779 the underlying value. */
11780 if (equality_comparison_p
11781 && const_op == 0
11782 && CONST_INT_P (XEXP (op0, 1))
11783 && mode_width <= HOST_BITS_PER_WIDE_INT
11784 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11785 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11787 op0 = XEXP (op0, 0);
11788 code = (code == EQ ? GE : LT);
11789 continue;
11792 /* If this AND operation is really a ZERO_EXTEND from a narrower
11793 mode, the constant fits within that mode, and this is either an
11794 equality or unsigned comparison, try to do this comparison in
11795 the narrower mode.
11797 Note that in:
11799 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11800 -> (ne:DI (reg:SI 4) (const_int 0))
11802 unless TRULY_NOOP_TRUNCATION allows it or the register is
11803 known to hold a value of the required mode the
11804 transformation is invalid. */
11805 if ((equality_comparison_p || unsigned_comparison_p)
11806 && CONST_INT_P (XEXP (op0, 1))
11807 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11808 & GET_MODE_MASK (mode))
11809 + 1)) >= 0
11810 && const_op >> i == 0
11811 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11812 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11813 || (REG_P (XEXP (op0, 0))
11814 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11816 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11817 continue;
11820 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11821 fits in both M1 and M2 and the SUBREG is either paradoxical
11822 or represents the low part, permute the SUBREG and the AND
11823 and try again. */
11824 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11826 unsigned HOST_WIDE_INT c1;
11827 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11828 /* Require an integral mode, to avoid creating something like
11829 (AND:SF ...). */
11830 if (SCALAR_INT_MODE_P (tmode)
11831 /* It is unsafe to commute the AND into the SUBREG if the
11832 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11833 not defined. As originally written the upper bits
11834 have a defined value due to the AND operation.
11835 However, if we commute the AND inside the SUBREG then
11836 they no longer have defined values and the meaning of
11837 the code has been changed. */
11838 && (0
11839 #ifdef WORD_REGISTER_OPERATIONS
11840 || (mode_width > GET_MODE_PRECISION (tmode)
11841 && mode_width <= BITS_PER_WORD)
11842 #endif
11843 || (mode_width <= GET_MODE_PRECISION (tmode)
11844 && subreg_lowpart_p (XEXP (op0, 0))))
11845 && CONST_INT_P (XEXP (op0, 1))
11846 && mode_width <= HOST_BITS_PER_WIDE_INT
11847 && HWI_COMPUTABLE_MODE_P (tmode)
11848 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11849 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11850 && c1 != mask
11851 && c1 != GET_MODE_MASK (tmode))
11853 op0 = simplify_gen_binary (AND, tmode,
11854 SUBREG_REG (XEXP (op0, 0)),
11855 gen_int_mode (c1, tmode));
11856 op0 = gen_lowpart (mode, op0);
11857 continue;
11861 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11862 if (const_op == 0 && equality_comparison_p
11863 && XEXP (op0, 1) == const1_rtx
11864 && GET_CODE (XEXP (op0, 0)) == NOT)
11866 op0 = simplify_and_const_int (NULL_RTX, mode,
11867 XEXP (XEXP (op0, 0), 0), 1);
11868 code = (code == NE ? EQ : NE);
11869 continue;
11872 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11873 (eq (and (lshiftrt X) 1) 0).
11874 Also handle the case where (not X) is expressed using xor. */
11875 if (const_op == 0 && equality_comparison_p
11876 && XEXP (op0, 1) == const1_rtx
11877 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11879 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11880 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11882 if (GET_CODE (shift_op) == NOT
11883 || (GET_CODE (shift_op) == XOR
11884 && CONST_INT_P (XEXP (shift_op, 1))
11885 && CONST_INT_P (shift_count)
11886 && HWI_COMPUTABLE_MODE_P (mode)
11887 && (UINTVAL (XEXP (shift_op, 1))
11888 == (unsigned HOST_WIDE_INT) 1
11889 << INTVAL (shift_count))))
11892 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11893 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11894 code = (code == NE ? EQ : NE);
11895 continue;
11898 break;
11900 case ASHIFT:
11901 /* If we have (compare (ashift FOO N) (const_int C)) and
11902 the high order N bits of FOO (N+1 if an inequality comparison)
11903 are known to be zero, we can do this by comparing FOO with C
11904 shifted right N bits so long as the low-order N bits of C are
11905 zero. */
11906 if (CONST_INT_P (XEXP (op0, 1))
11907 && INTVAL (XEXP (op0, 1)) >= 0
11908 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11909 < HOST_BITS_PER_WIDE_INT)
11910 && (((unsigned HOST_WIDE_INT) const_op
11911 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11912 - 1)) == 0)
11913 && mode_width <= HOST_BITS_PER_WIDE_INT
11914 && (nonzero_bits (XEXP (op0, 0), mode)
11915 & ~(mask >> (INTVAL (XEXP (op0, 1))
11916 + ! equality_comparison_p))) == 0)
11918 /* We must perform a logical shift, not an arithmetic one,
11919 as we want the top N bits of C to be zero. */
11920 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11922 temp >>= INTVAL (XEXP (op0, 1));
11923 op1 = gen_int_mode (temp, mode);
11924 op0 = XEXP (op0, 0);
11925 continue;
11928 /* If we are doing a sign bit comparison, it means we are testing
11929 a particular bit. Convert it to the appropriate AND. */
11930 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11931 && mode_width <= HOST_BITS_PER_WIDE_INT)
11933 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11934 ((unsigned HOST_WIDE_INT) 1
11935 << (mode_width - 1
11936 - INTVAL (XEXP (op0, 1)))));
11937 code = (code == LT ? NE : EQ);
11938 continue;
11941 /* If this an equality comparison with zero and we are shifting
11942 the low bit to the sign bit, we can convert this to an AND of the
11943 low-order bit. */
11944 if (const_op == 0 && equality_comparison_p
11945 && CONST_INT_P (XEXP (op0, 1))
11946 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11948 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11949 continue;
11951 break;
11953 case ASHIFTRT:
11954 /* If this is an equality comparison with zero, we can do this
11955 as a logical shift, which might be much simpler. */
11956 if (equality_comparison_p && const_op == 0
11957 && CONST_INT_P (XEXP (op0, 1)))
11959 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11960 XEXP (op0, 0),
11961 INTVAL (XEXP (op0, 1)));
11962 continue;
11965 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11966 do the comparison in a narrower mode. */
11967 if (! unsigned_comparison_p
11968 && CONST_INT_P (XEXP (op0, 1))
11969 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11970 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11971 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11972 MODE_INT, 1)) != BLKmode
11973 && (((unsigned HOST_WIDE_INT) const_op
11974 + (GET_MODE_MASK (tmode) >> 1) + 1)
11975 <= GET_MODE_MASK (tmode)))
11977 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11978 continue;
11981 /* Likewise if OP0 is a PLUS of a sign extension with a
11982 constant, which is usually represented with the PLUS
11983 between the shifts. */
11984 if (! unsigned_comparison_p
11985 && CONST_INT_P (XEXP (op0, 1))
11986 && GET_CODE (XEXP (op0, 0)) == PLUS
11987 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11988 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11989 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11990 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11991 MODE_INT, 1)) != BLKmode
11992 && (((unsigned HOST_WIDE_INT) const_op
11993 + (GET_MODE_MASK (tmode) >> 1) + 1)
11994 <= GET_MODE_MASK (tmode)))
11996 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11997 rtx add_const = XEXP (XEXP (op0, 0), 1);
11998 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11999 add_const, XEXP (op0, 1));
12001 op0 = simplify_gen_binary (PLUS, tmode,
12002 gen_lowpart (tmode, inner),
12003 new_const);
12004 continue;
12007 /* ... fall through ... */
12008 case LSHIFTRT:
12009 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12010 the low order N bits of FOO are known to be zero, we can do this
12011 by comparing FOO with C shifted left N bits so long as no
12012 overflow occurs. Even if the low order N bits of FOO aren't known
12013 to be zero, if the comparison is >= or < we can use the same
12014 optimization and for > or <= by setting all the low
12015 order N bits in the comparison constant. */
12016 if (CONST_INT_P (XEXP (op0, 1))
12017 && INTVAL (XEXP (op0, 1)) > 0
12018 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12019 && mode_width <= HOST_BITS_PER_WIDE_INT
12020 && (((unsigned HOST_WIDE_INT) const_op
12021 + (GET_CODE (op0) != LSHIFTRT
12022 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12023 + 1)
12024 : 0))
12025 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12027 unsigned HOST_WIDE_INT low_bits
12028 = (nonzero_bits (XEXP (op0, 0), mode)
12029 & (((unsigned HOST_WIDE_INT) 1
12030 << INTVAL (XEXP (op0, 1))) - 1));
12031 if (low_bits == 0 || !equality_comparison_p)
12033 /* If the shift was logical, then we must make the condition
12034 unsigned. */
12035 if (GET_CODE (op0) == LSHIFTRT)
12036 code = unsigned_condition (code);
12038 const_op <<= INTVAL (XEXP (op0, 1));
12039 if (low_bits != 0
12040 && (code == GT || code == GTU
12041 || code == LE || code == LEU))
12042 const_op
12043 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12044 op1 = GEN_INT (const_op);
12045 op0 = XEXP (op0, 0);
12046 continue;
12050 /* If we are using this shift to extract just the sign bit, we
12051 can replace this with an LT or GE comparison. */
12052 if (const_op == 0
12053 && (equality_comparison_p || sign_bit_comparison_p)
12054 && CONST_INT_P (XEXP (op0, 1))
12055 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12057 op0 = XEXP (op0, 0);
12058 code = (code == NE || code == GT ? LT : GE);
12059 continue;
12061 break;
12063 default:
12064 break;
12067 break;
12070 /* Now make any compound operations involved in this comparison. Then,
12071 check for an outmost SUBREG on OP0 that is not doing anything or is
12072 paradoxical. The latter transformation must only be performed when
12073 it is known that the "extra" bits will be the same in op0 and op1 or
12074 that they don't matter. There are three cases to consider:
12076 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12077 care bits and we can assume they have any convenient value. So
12078 making the transformation is safe.
12080 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12081 In this case the upper bits of op0 are undefined. We should not make
12082 the simplification in that case as we do not know the contents of
12083 those bits.
12085 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12086 UNKNOWN. In that case we know those bits are zeros or ones. We must
12087 also be sure that they are the same as the upper bits of op1.
12089 We can never remove a SUBREG for a non-equality comparison because
12090 the sign bit is in a different place in the underlying object. */
12092 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12093 op1 = make_compound_operation (op1, SET);
12095 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12096 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12097 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12098 && (code == NE || code == EQ))
12100 if (paradoxical_subreg_p (op0))
12102 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12103 implemented. */
12104 if (REG_P (SUBREG_REG (op0)))
12106 op0 = SUBREG_REG (op0);
12107 op1 = gen_lowpart (GET_MODE (op0), op1);
12110 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12111 <= HOST_BITS_PER_WIDE_INT)
12112 && (nonzero_bits (SUBREG_REG (op0),
12113 GET_MODE (SUBREG_REG (op0)))
12114 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12116 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12118 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12119 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12120 op0 = SUBREG_REG (op0), op1 = tem;
12124 /* We now do the opposite procedure: Some machines don't have compare
12125 insns in all modes. If OP0's mode is an integer mode smaller than a
12126 word and we can't do a compare in that mode, see if there is a larger
12127 mode for which we can do the compare. There are a number of cases in
12128 which we can use the wider mode. */
12130 mode = GET_MODE (op0);
12131 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12132 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12133 && ! have_insn_for (COMPARE, mode))
12134 for (tmode = GET_MODE_WIDER_MODE (mode);
12135 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12136 tmode = GET_MODE_WIDER_MODE (tmode))
12137 if (have_insn_for (COMPARE, tmode))
12139 int zero_extended;
12141 /* If this is a test for negative, we can make an explicit
12142 test of the sign bit. Test this first so we can use
12143 a paradoxical subreg to extend OP0. */
12145 if (op1 == const0_rtx && (code == LT || code == GE)
12146 && HWI_COMPUTABLE_MODE_P (mode))
12148 unsigned HOST_WIDE_INT sign
12149 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12150 op0 = simplify_gen_binary (AND, tmode,
12151 gen_lowpart (tmode, op0),
12152 gen_int_mode (sign, tmode));
12153 code = (code == LT) ? NE : EQ;
12154 break;
12157 /* If the only nonzero bits in OP0 and OP1 are those in the
12158 narrower mode and this is an equality or unsigned comparison,
12159 we can use the wider mode. Similarly for sign-extended
12160 values, in which case it is true for all comparisons. */
12161 zero_extended = ((code == EQ || code == NE
12162 || code == GEU || code == GTU
12163 || code == LEU || code == LTU)
12164 && (nonzero_bits (op0, tmode)
12165 & ~GET_MODE_MASK (mode)) == 0
12166 && ((CONST_INT_P (op1)
12167 || (nonzero_bits (op1, tmode)
12168 & ~GET_MODE_MASK (mode)) == 0)));
12170 if (zero_extended
12171 || ((num_sign_bit_copies (op0, tmode)
12172 > (unsigned int) (GET_MODE_PRECISION (tmode)
12173 - GET_MODE_PRECISION (mode)))
12174 && (num_sign_bit_copies (op1, tmode)
12175 > (unsigned int) (GET_MODE_PRECISION (tmode)
12176 - GET_MODE_PRECISION (mode)))))
12178 /* If OP0 is an AND and we don't have an AND in MODE either,
12179 make a new AND in the proper mode. */
12180 if (GET_CODE (op0) == AND
12181 && !have_insn_for (AND, mode))
12182 op0 = simplify_gen_binary (AND, tmode,
12183 gen_lowpart (tmode,
12184 XEXP (op0, 0)),
12185 gen_lowpart (tmode,
12186 XEXP (op0, 1)));
12187 else
12189 if (zero_extended)
12191 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12192 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12194 else
12196 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12197 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12199 break;
12204 /* We may have changed the comparison operands. Re-canonicalize. */
12205 if (swap_commutative_operands_p (op0, op1))
12207 tem = op0, op0 = op1, op1 = tem;
12208 code = swap_condition (code);
12211 /* If this machine only supports a subset of valid comparisons, see if we
12212 can convert an unsupported one into a supported one. */
12213 target_canonicalize_comparison (&code, &op0, &op1, 0);
12215 *pop0 = op0;
12216 *pop1 = op1;
12218 return code;
12221 /* Utility function for record_value_for_reg. Count number of
12222 rtxs in X. */
12223 static int
12224 count_rtxs (rtx x)
12226 enum rtx_code code = GET_CODE (x);
12227 const char *fmt;
12228 int i, j, ret = 1;
12230 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12231 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12233 rtx x0 = XEXP (x, 0);
12234 rtx x1 = XEXP (x, 1);
12236 if (x0 == x1)
12237 return 1 + 2 * count_rtxs (x0);
12239 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12240 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12241 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12242 return 2 + 2 * count_rtxs (x0)
12243 + count_rtxs (x == XEXP (x1, 0)
12244 ? XEXP (x1, 1) : XEXP (x1, 0));
12246 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12247 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12248 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12249 return 2 + 2 * count_rtxs (x1)
12250 + count_rtxs (x == XEXP (x0, 0)
12251 ? XEXP (x0, 1) : XEXP (x0, 0));
12254 fmt = GET_RTX_FORMAT (code);
12255 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12256 if (fmt[i] == 'e')
12257 ret += count_rtxs (XEXP (x, i));
12258 else if (fmt[i] == 'E')
12259 for (j = 0; j < XVECLEN (x, i); j++)
12260 ret += count_rtxs (XVECEXP (x, i, j));
12262 return ret;
12265 /* Utility function for following routine. Called when X is part of a value
12266 being stored into last_set_value. Sets last_set_table_tick
12267 for each register mentioned. Similar to mention_regs in cse.c */
12269 static void
12270 update_table_tick (rtx x)
12272 enum rtx_code code = GET_CODE (x);
12273 const char *fmt = GET_RTX_FORMAT (code);
12274 int i, j;
12276 if (code == REG)
12278 unsigned int regno = REGNO (x);
12279 unsigned int endregno = END_REGNO (x);
12280 unsigned int r;
12282 for (r = regno; r < endregno; r++)
12284 reg_stat_type *rsp = &reg_stat[r];
12285 rsp->last_set_table_tick = label_tick;
12288 return;
12291 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12292 if (fmt[i] == 'e')
12294 /* Check for identical subexpressions. If x contains
12295 identical subexpression we only have to traverse one of
12296 them. */
12297 if (i == 0 && ARITHMETIC_P (x))
12299 /* Note that at this point x1 has already been
12300 processed. */
12301 rtx x0 = XEXP (x, 0);
12302 rtx x1 = XEXP (x, 1);
12304 /* If x0 and x1 are identical then there is no need to
12305 process x0. */
12306 if (x0 == x1)
12307 break;
12309 /* If x0 is identical to a subexpression of x1 then while
12310 processing x1, x0 has already been processed. Thus we
12311 are done with x. */
12312 if (ARITHMETIC_P (x1)
12313 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12314 break;
12316 /* If x1 is identical to a subexpression of x0 then we
12317 still have to process the rest of x0. */
12318 if (ARITHMETIC_P (x0)
12319 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12321 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12322 break;
12326 update_table_tick (XEXP (x, i));
12328 else if (fmt[i] == 'E')
12329 for (j = 0; j < XVECLEN (x, i); j++)
12330 update_table_tick (XVECEXP (x, i, j));
12333 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12334 are saying that the register is clobbered and we no longer know its
12335 value. If INSN is zero, don't update reg_stat[].last_set; this is
12336 only permitted with VALUE also zero and is used to invalidate the
12337 register. */
12339 static void
12340 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12342 unsigned int regno = REGNO (reg);
12343 unsigned int endregno = END_REGNO (reg);
12344 unsigned int i;
12345 reg_stat_type *rsp;
12347 /* If VALUE contains REG and we have a previous value for REG, substitute
12348 the previous value. */
12349 if (value && insn && reg_overlap_mentioned_p (reg, value))
12351 rtx tem;
12353 /* Set things up so get_last_value is allowed to see anything set up to
12354 our insn. */
12355 subst_low_luid = DF_INSN_LUID (insn);
12356 tem = get_last_value (reg);
12358 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12359 it isn't going to be useful and will take a lot of time to process,
12360 so just use the CLOBBER. */
12362 if (tem)
12364 if (ARITHMETIC_P (tem)
12365 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12366 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12367 tem = XEXP (tem, 0);
12368 else if (count_occurrences (value, reg, 1) >= 2)
12370 /* If there are two or more occurrences of REG in VALUE,
12371 prevent the value from growing too much. */
12372 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12373 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12376 value = replace_rtx (copy_rtx (value), reg, tem);
12380 /* For each register modified, show we don't know its value, that
12381 we don't know about its bitwise content, that its value has been
12382 updated, and that we don't know the location of the death of the
12383 register. */
12384 for (i = regno; i < endregno; i++)
12386 rsp = &reg_stat[i];
12388 if (insn)
12389 rsp->last_set = insn;
12391 rsp->last_set_value = 0;
12392 rsp->last_set_mode = VOIDmode;
12393 rsp->last_set_nonzero_bits = 0;
12394 rsp->last_set_sign_bit_copies = 0;
12395 rsp->last_death = 0;
12396 rsp->truncated_to_mode = VOIDmode;
12399 /* Mark registers that are being referenced in this value. */
12400 if (value)
12401 update_table_tick (value);
12403 /* Now update the status of each register being set.
12404 If someone is using this register in this block, set this register
12405 to invalid since we will get confused between the two lives in this
12406 basic block. This makes using this register always invalid. In cse, we
12407 scan the table to invalidate all entries using this register, but this
12408 is too much work for us. */
12410 for (i = regno; i < endregno; i++)
12412 rsp = &reg_stat[i];
12413 rsp->last_set_label = label_tick;
12414 if (!insn
12415 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12416 rsp->last_set_invalid = 1;
12417 else
12418 rsp->last_set_invalid = 0;
12421 /* The value being assigned might refer to X (like in "x++;"). In that
12422 case, we must replace it with (clobber (const_int 0)) to prevent
12423 infinite loops. */
12424 rsp = &reg_stat[regno];
12425 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12427 value = copy_rtx (value);
12428 if (!get_last_value_validate (&value, insn, label_tick, 1))
12429 value = 0;
12432 /* For the main register being modified, update the value, the mode, the
12433 nonzero bits, and the number of sign bit copies. */
12435 rsp->last_set_value = value;
12437 if (value)
12439 machine_mode mode = GET_MODE (reg);
12440 subst_low_luid = DF_INSN_LUID (insn);
12441 rsp->last_set_mode = mode;
12442 if (GET_MODE_CLASS (mode) == MODE_INT
12443 && HWI_COMPUTABLE_MODE_P (mode))
12444 mode = nonzero_bits_mode;
12445 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12446 rsp->last_set_sign_bit_copies
12447 = num_sign_bit_copies (value, GET_MODE (reg));
12451 /* Called via note_stores from record_dead_and_set_regs to handle one
12452 SET or CLOBBER in an insn. DATA is the instruction in which the
12453 set is occurring. */
12455 static void
12456 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12458 rtx_insn *record_dead_insn = (rtx_insn *) data;
12460 if (GET_CODE (dest) == SUBREG)
12461 dest = SUBREG_REG (dest);
12463 if (!record_dead_insn)
12465 if (REG_P (dest))
12466 record_value_for_reg (dest, NULL, NULL_RTX);
12467 return;
12470 if (REG_P (dest))
12472 /* If we are setting the whole register, we know its value. Otherwise
12473 show that we don't know the value. We can handle SUBREG in
12474 some cases. */
12475 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12476 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12477 else if (GET_CODE (setter) == SET
12478 && GET_CODE (SET_DEST (setter)) == SUBREG
12479 && SUBREG_REG (SET_DEST (setter)) == dest
12480 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12481 && subreg_lowpart_p (SET_DEST (setter)))
12482 record_value_for_reg (dest, record_dead_insn,
12483 gen_lowpart (GET_MODE (dest),
12484 SET_SRC (setter)));
12485 else
12486 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12488 else if (MEM_P (dest)
12489 /* Ignore pushes, they clobber nothing. */
12490 && ! push_operand (dest, GET_MODE (dest)))
12491 mem_last_set = DF_INSN_LUID (record_dead_insn);
12494 /* Update the records of when each REG was most recently set or killed
12495 for the things done by INSN. This is the last thing done in processing
12496 INSN in the combiner loop.
12498 We update reg_stat[], in particular fields last_set, last_set_value,
12499 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12500 last_death, and also the similar information mem_last_set (which insn
12501 most recently modified memory) and last_call_luid (which insn was the
12502 most recent subroutine call). */
12504 static void
12505 record_dead_and_set_regs (rtx_insn *insn)
12507 rtx link;
12508 unsigned int i;
12510 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12512 if (REG_NOTE_KIND (link) == REG_DEAD
12513 && REG_P (XEXP (link, 0)))
12515 unsigned int regno = REGNO (XEXP (link, 0));
12516 unsigned int endregno = END_REGNO (XEXP (link, 0));
12518 for (i = regno; i < endregno; i++)
12520 reg_stat_type *rsp;
12522 rsp = &reg_stat[i];
12523 rsp->last_death = insn;
12526 else if (REG_NOTE_KIND (link) == REG_INC)
12527 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12530 if (CALL_P (insn))
12532 hard_reg_set_iterator hrsi;
12533 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12535 reg_stat_type *rsp;
12537 rsp = &reg_stat[i];
12538 rsp->last_set_invalid = 1;
12539 rsp->last_set = insn;
12540 rsp->last_set_value = 0;
12541 rsp->last_set_mode = VOIDmode;
12542 rsp->last_set_nonzero_bits = 0;
12543 rsp->last_set_sign_bit_copies = 0;
12544 rsp->last_death = 0;
12545 rsp->truncated_to_mode = VOIDmode;
12548 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12550 /* We can't combine into a call pattern. Remember, though, that
12551 the return value register is set at this LUID. We could
12552 still replace a register with the return value from the
12553 wrong subroutine call! */
12554 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12556 else
12557 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12560 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12561 register present in the SUBREG, so for each such SUBREG go back and
12562 adjust nonzero and sign bit information of the registers that are
12563 known to have some zero/sign bits set.
12565 This is needed because when combine blows the SUBREGs away, the
12566 information on zero/sign bits is lost and further combines can be
12567 missed because of that. */
12569 static void
12570 record_promoted_value (rtx_insn *insn, rtx subreg)
12572 struct insn_link *links;
12573 rtx set;
12574 unsigned int regno = REGNO (SUBREG_REG (subreg));
12575 machine_mode mode = GET_MODE (subreg);
12577 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12578 return;
12580 for (links = LOG_LINKS (insn); links;)
12582 reg_stat_type *rsp;
12584 insn = links->insn;
12585 set = single_set (insn);
12587 if (! set || !REG_P (SET_DEST (set))
12588 || REGNO (SET_DEST (set)) != regno
12589 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12591 links = links->next;
12592 continue;
12595 rsp = &reg_stat[regno];
12596 if (rsp->last_set == insn)
12598 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12599 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12602 if (REG_P (SET_SRC (set)))
12604 regno = REGNO (SET_SRC (set));
12605 links = LOG_LINKS (insn);
12607 else
12608 break;
12612 /* Check if X, a register, is known to contain a value already
12613 truncated to MODE. In this case we can use a subreg to refer to
12614 the truncated value even though in the generic case we would need
12615 an explicit truncation. */
12617 static bool
12618 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12620 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12621 machine_mode truncated = rsp->truncated_to_mode;
12623 if (truncated == 0
12624 || rsp->truncation_label < label_tick_ebb_start)
12625 return false;
12626 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12627 return true;
12628 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12629 return true;
12630 return false;
12633 /* If X is a hard reg or a subreg record the mode that the register is
12634 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12635 to turn a truncate into a subreg using this information. Return true
12636 if traversing X is complete. */
12638 static bool
12639 record_truncated_value (rtx x)
12641 machine_mode truncated_mode;
12642 reg_stat_type *rsp;
12644 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12646 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12647 truncated_mode = GET_MODE (x);
12649 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12650 return true;
12652 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12653 return true;
12655 x = SUBREG_REG (x);
12657 /* ??? For hard-regs we now record everything. We might be able to
12658 optimize this using last_set_mode. */
12659 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12660 truncated_mode = GET_MODE (x);
12661 else
12662 return false;
12664 rsp = &reg_stat[REGNO (x)];
12665 if (rsp->truncated_to_mode == 0
12666 || rsp->truncation_label < label_tick_ebb_start
12667 || (GET_MODE_SIZE (truncated_mode)
12668 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12670 rsp->truncated_to_mode = truncated_mode;
12671 rsp->truncation_label = label_tick;
12674 return true;
12677 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12678 the modes they are used in. This can help truning TRUNCATEs into
12679 SUBREGs. */
12681 static void
12682 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12684 subrtx_var_iterator::array_type array;
12685 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12686 if (record_truncated_value (*iter))
12687 iter.skip_subrtxes ();
12690 /* Scan X for promoted SUBREGs. For each one found,
12691 note what it implies to the registers used in it. */
12693 static void
12694 check_promoted_subreg (rtx_insn *insn, rtx x)
12696 if (GET_CODE (x) == SUBREG
12697 && SUBREG_PROMOTED_VAR_P (x)
12698 && REG_P (SUBREG_REG (x)))
12699 record_promoted_value (insn, x);
12700 else
12702 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12703 int i, j;
12705 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12706 switch (format[i])
12708 case 'e':
12709 check_promoted_subreg (insn, XEXP (x, i));
12710 break;
12711 case 'V':
12712 case 'E':
12713 if (XVEC (x, i) != 0)
12714 for (j = 0; j < XVECLEN (x, i); j++)
12715 check_promoted_subreg (insn, XVECEXP (x, i, j));
12716 break;
12721 /* Verify that all the registers and memory references mentioned in *LOC are
12722 still valid. *LOC was part of a value set in INSN when label_tick was
12723 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12724 the invalid references with (clobber (const_int 0)) and return 1. This
12725 replacement is useful because we often can get useful information about
12726 the form of a value (e.g., if it was produced by a shift that always
12727 produces -1 or 0) even though we don't know exactly what registers it
12728 was produced from. */
12730 static int
12731 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12733 rtx x = *loc;
12734 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12735 int len = GET_RTX_LENGTH (GET_CODE (x));
12736 int i, j;
12738 if (REG_P (x))
12740 unsigned int regno = REGNO (x);
12741 unsigned int endregno = END_REGNO (x);
12742 unsigned int j;
12744 for (j = regno; j < endregno; j++)
12746 reg_stat_type *rsp = &reg_stat[j];
12747 if (rsp->last_set_invalid
12748 /* If this is a pseudo-register that was only set once and not
12749 live at the beginning of the function, it is always valid. */
12750 || (! (regno >= FIRST_PSEUDO_REGISTER
12751 && REG_N_SETS (regno) == 1
12752 && (!REGNO_REG_SET_P
12753 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12754 regno)))
12755 && rsp->last_set_label > tick))
12757 if (replace)
12758 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12759 return replace;
12763 return 1;
12765 /* If this is a memory reference, make sure that there were no stores after
12766 it that might have clobbered the value. We don't have alias info, so we
12767 assume any store invalidates it. Moreover, we only have local UIDs, so
12768 we also assume that there were stores in the intervening basic blocks. */
12769 else if (MEM_P (x) && !MEM_READONLY_P (x)
12770 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12772 if (replace)
12773 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12774 return replace;
12777 for (i = 0; i < len; i++)
12779 if (fmt[i] == 'e')
12781 /* Check for identical subexpressions. If x contains
12782 identical subexpression we only have to traverse one of
12783 them. */
12784 if (i == 1 && ARITHMETIC_P (x))
12786 /* Note that at this point x0 has already been checked
12787 and found valid. */
12788 rtx x0 = XEXP (x, 0);
12789 rtx x1 = XEXP (x, 1);
12791 /* If x0 and x1 are identical then x is also valid. */
12792 if (x0 == x1)
12793 return 1;
12795 /* If x1 is identical to a subexpression of x0 then
12796 while checking x0, x1 has already been checked. Thus
12797 it is valid and so as x. */
12798 if (ARITHMETIC_P (x0)
12799 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12800 return 1;
12802 /* If x0 is identical to a subexpression of x1 then x is
12803 valid iff the rest of x1 is valid. */
12804 if (ARITHMETIC_P (x1)
12805 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12806 return
12807 get_last_value_validate (&XEXP (x1,
12808 x0 == XEXP (x1, 0) ? 1 : 0),
12809 insn, tick, replace);
12812 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12813 replace) == 0)
12814 return 0;
12816 else if (fmt[i] == 'E')
12817 for (j = 0; j < XVECLEN (x, i); j++)
12818 if (get_last_value_validate (&XVECEXP (x, i, j),
12819 insn, tick, replace) == 0)
12820 return 0;
12823 /* If we haven't found a reason for it to be invalid, it is valid. */
12824 return 1;
12827 /* Get the last value assigned to X, if known. Some registers
12828 in the value may be replaced with (clobber (const_int 0)) if their value
12829 is known longer known reliably. */
12831 static rtx
12832 get_last_value (const_rtx x)
12834 unsigned int regno;
12835 rtx value;
12836 reg_stat_type *rsp;
12838 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12839 then convert it to the desired mode. If this is a paradoxical SUBREG,
12840 we cannot predict what values the "extra" bits might have. */
12841 if (GET_CODE (x) == SUBREG
12842 && subreg_lowpart_p (x)
12843 && !paradoxical_subreg_p (x)
12844 && (value = get_last_value (SUBREG_REG (x))) != 0)
12845 return gen_lowpart (GET_MODE (x), value);
12847 if (!REG_P (x))
12848 return 0;
12850 regno = REGNO (x);
12851 rsp = &reg_stat[regno];
12852 value = rsp->last_set_value;
12854 /* If we don't have a value, or if it isn't for this basic block and
12855 it's either a hard register, set more than once, or it's a live
12856 at the beginning of the function, return 0.
12858 Because if it's not live at the beginning of the function then the reg
12859 is always set before being used (is never used without being set).
12860 And, if it's set only once, and it's always set before use, then all
12861 uses must have the same last value, even if it's not from this basic
12862 block. */
12864 if (value == 0
12865 || (rsp->last_set_label < label_tick_ebb_start
12866 && (regno < FIRST_PSEUDO_REGISTER
12867 || REG_N_SETS (regno) != 1
12868 || REGNO_REG_SET_P
12869 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12870 return 0;
12872 /* If the value was set in a later insn than the ones we are processing,
12873 we can't use it even if the register was only set once. */
12874 if (rsp->last_set_label == label_tick
12875 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12876 return 0;
12878 /* If the value has all its registers valid, return it. */
12879 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12880 return value;
12882 /* Otherwise, make a copy and replace any invalid register with
12883 (clobber (const_int 0)). If that fails for some reason, return 0. */
12885 value = copy_rtx (value);
12886 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12887 return value;
12889 return 0;
12892 /* Return nonzero if expression X refers to a REG or to memory
12893 that is set in an instruction more recent than FROM_LUID. */
12895 static int
12896 use_crosses_set_p (const_rtx x, int from_luid)
12898 const char *fmt;
12899 int i;
12900 enum rtx_code code = GET_CODE (x);
12902 if (code == REG)
12904 unsigned int regno = REGNO (x);
12905 unsigned endreg = END_REGNO (x);
12907 #ifdef PUSH_ROUNDING
12908 /* Don't allow uses of the stack pointer to be moved,
12909 because we don't know whether the move crosses a push insn. */
12910 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12911 return 1;
12912 #endif
12913 for (; regno < endreg; regno++)
12915 reg_stat_type *rsp = &reg_stat[regno];
12916 if (rsp->last_set
12917 && rsp->last_set_label == label_tick
12918 && DF_INSN_LUID (rsp->last_set) > from_luid)
12919 return 1;
12921 return 0;
12924 if (code == MEM && mem_last_set > from_luid)
12925 return 1;
12927 fmt = GET_RTX_FORMAT (code);
12929 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12931 if (fmt[i] == 'E')
12933 int j;
12934 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12935 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12936 return 1;
12938 else if (fmt[i] == 'e'
12939 && use_crosses_set_p (XEXP (x, i), from_luid))
12940 return 1;
12942 return 0;
12945 /* Define three variables used for communication between the following
12946 routines. */
12948 static unsigned int reg_dead_regno, reg_dead_endregno;
12949 static int reg_dead_flag;
12951 /* Function called via note_stores from reg_dead_at_p.
12953 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12954 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12956 static void
12957 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12959 unsigned int regno, endregno;
12961 if (!REG_P (dest))
12962 return;
12964 regno = REGNO (dest);
12965 endregno = END_REGNO (dest);
12966 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12967 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12970 /* Return nonzero if REG is known to be dead at INSN.
12972 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12973 referencing REG, it is dead. If we hit a SET referencing REG, it is
12974 live. Otherwise, see if it is live or dead at the start of the basic
12975 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12976 must be assumed to be always live. */
12978 static int
12979 reg_dead_at_p (rtx reg, rtx_insn *insn)
12981 basic_block block;
12982 unsigned int i;
12984 /* Set variables for reg_dead_at_p_1. */
12985 reg_dead_regno = REGNO (reg);
12986 reg_dead_endregno = END_REGNO (reg);
12988 reg_dead_flag = 0;
12990 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12991 we allow the machine description to decide whether use-and-clobber
12992 patterns are OK. */
12993 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12995 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12996 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12997 return 0;
13000 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13001 beginning of basic block. */
13002 block = BLOCK_FOR_INSN (insn);
13003 for (;;)
13005 if (INSN_P (insn))
13007 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13008 return 1;
13010 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13011 if (reg_dead_flag)
13012 return reg_dead_flag == 1 ? 1 : 0;
13014 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13015 return 1;
13018 if (insn == BB_HEAD (block))
13019 break;
13021 insn = PREV_INSN (insn);
13024 /* Look at live-in sets for the basic block that we were in. */
13025 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13026 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13027 return 0;
13029 return 1;
13032 /* Note hard registers in X that are used. */
13034 static void
13035 mark_used_regs_combine (rtx x)
13037 RTX_CODE code = GET_CODE (x);
13038 unsigned int regno;
13039 int i;
13041 switch (code)
13043 case LABEL_REF:
13044 case SYMBOL_REF:
13045 case CONST:
13046 CASE_CONST_ANY:
13047 case PC:
13048 case ADDR_VEC:
13049 case ADDR_DIFF_VEC:
13050 case ASM_INPUT:
13051 #ifdef HAVE_cc0
13052 /* CC0 must die in the insn after it is set, so we don't need to take
13053 special note of it here. */
13054 case CC0:
13055 #endif
13056 return;
13058 case CLOBBER:
13059 /* If we are clobbering a MEM, mark any hard registers inside the
13060 address as used. */
13061 if (MEM_P (XEXP (x, 0)))
13062 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13063 return;
13065 case REG:
13066 regno = REGNO (x);
13067 /* A hard reg in a wide mode may really be multiple registers.
13068 If so, mark all of them just like the first. */
13069 if (regno < FIRST_PSEUDO_REGISTER)
13071 /* None of this applies to the stack, frame or arg pointers. */
13072 if (regno == STACK_POINTER_REGNUM
13073 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13074 || regno == HARD_FRAME_POINTER_REGNUM
13075 #endif
13076 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13077 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13078 #endif
13079 || regno == FRAME_POINTER_REGNUM)
13080 return;
13082 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13084 return;
13086 case SET:
13088 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13089 the address. */
13090 rtx testreg = SET_DEST (x);
13092 while (GET_CODE (testreg) == SUBREG
13093 || GET_CODE (testreg) == ZERO_EXTRACT
13094 || GET_CODE (testreg) == STRICT_LOW_PART)
13095 testreg = XEXP (testreg, 0);
13097 if (MEM_P (testreg))
13098 mark_used_regs_combine (XEXP (testreg, 0));
13100 mark_used_regs_combine (SET_SRC (x));
13102 return;
13104 default:
13105 break;
13108 /* Recursively scan the operands of this expression. */
13111 const char *fmt = GET_RTX_FORMAT (code);
13113 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13115 if (fmt[i] == 'e')
13116 mark_used_regs_combine (XEXP (x, i));
13117 else if (fmt[i] == 'E')
13119 int j;
13121 for (j = 0; j < XVECLEN (x, i); j++)
13122 mark_used_regs_combine (XVECEXP (x, i, j));
13128 /* Remove register number REGNO from the dead registers list of INSN.
13130 Return the note used to record the death, if there was one. */
13133 remove_death (unsigned int regno, rtx_insn *insn)
13135 rtx note = find_regno_note (insn, REG_DEAD, regno);
13137 if (note)
13138 remove_note (insn, note);
13140 return note;
13143 /* For each register (hardware or pseudo) used within expression X, if its
13144 death is in an instruction with luid between FROM_LUID (inclusive) and
13145 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13146 list headed by PNOTES.
13148 That said, don't move registers killed by maybe_kill_insn.
13150 This is done when X is being merged by combination into TO_INSN. These
13151 notes will then be distributed as needed. */
13153 static void
13154 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13155 rtx *pnotes)
13157 const char *fmt;
13158 int len, i;
13159 enum rtx_code code = GET_CODE (x);
13161 if (code == REG)
13163 unsigned int regno = REGNO (x);
13164 rtx_insn *where_dead = reg_stat[regno].last_death;
13166 /* Don't move the register if it gets killed in between from and to. */
13167 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13168 && ! reg_referenced_p (x, maybe_kill_insn))
13169 return;
13171 if (where_dead
13172 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13173 && DF_INSN_LUID (where_dead) >= from_luid
13174 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13176 rtx note = remove_death (regno, where_dead);
13178 /* It is possible for the call above to return 0. This can occur
13179 when last_death points to I2 or I1 that we combined with.
13180 In that case make a new note.
13182 We must also check for the case where X is a hard register
13183 and NOTE is a death note for a range of hard registers
13184 including X. In that case, we must put REG_DEAD notes for
13185 the remaining registers in place of NOTE. */
13187 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13188 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13189 > GET_MODE_SIZE (GET_MODE (x))))
13191 unsigned int deadregno = REGNO (XEXP (note, 0));
13192 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13193 unsigned int ourend = END_HARD_REGNO (x);
13194 unsigned int i;
13196 for (i = deadregno; i < deadend; i++)
13197 if (i < regno || i >= ourend)
13198 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13201 /* If we didn't find any note, or if we found a REG_DEAD note that
13202 covers only part of the given reg, and we have a multi-reg hard
13203 register, then to be safe we must check for REG_DEAD notes
13204 for each register other than the first. They could have
13205 their own REG_DEAD notes lying around. */
13206 else if ((note == 0
13207 || (note != 0
13208 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13209 < GET_MODE_SIZE (GET_MODE (x)))))
13210 && regno < FIRST_PSEUDO_REGISTER
13211 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13213 unsigned int ourend = END_HARD_REGNO (x);
13214 unsigned int i, offset;
13215 rtx oldnotes = 0;
13217 if (note)
13218 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13219 else
13220 offset = 1;
13222 for (i = regno + offset; i < ourend; i++)
13223 move_deaths (regno_reg_rtx[i],
13224 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13227 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13229 XEXP (note, 1) = *pnotes;
13230 *pnotes = note;
13232 else
13233 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13236 return;
13239 else if (GET_CODE (x) == SET)
13241 rtx dest = SET_DEST (x);
13243 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13245 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13246 that accesses one word of a multi-word item, some
13247 piece of everything register in the expression is used by
13248 this insn, so remove any old death. */
13249 /* ??? So why do we test for equality of the sizes? */
13251 if (GET_CODE (dest) == ZERO_EXTRACT
13252 || GET_CODE (dest) == STRICT_LOW_PART
13253 || (GET_CODE (dest) == SUBREG
13254 && (((GET_MODE_SIZE (GET_MODE (dest))
13255 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13256 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13257 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13259 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13260 return;
13263 /* If this is some other SUBREG, we know it replaces the entire
13264 value, so use that as the destination. */
13265 if (GET_CODE (dest) == SUBREG)
13266 dest = SUBREG_REG (dest);
13268 /* If this is a MEM, adjust deaths of anything used in the address.
13269 For a REG (the only other possibility), the entire value is
13270 being replaced so the old value is not used in this insn. */
13272 if (MEM_P (dest))
13273 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13274 to_insn, pnotes);
13275 return;
13278 else if (GET_CODE (x) == CLOBBER)
13279 return;
13281 len = GET_RTX_LENGTH (code);
13282 fmt = GET_RTX_FORMAT (code);
13284 for (i = 0; i < len; i++)
13286 if (fmt[i] == 'E')
13288 int j;
13289 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13290 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13291 to_insn, pnotes);
13293 else if (fmt[i] == 'e')
13294 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13298 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13299 pattern of an insn. X must be a REG. */
13301 static int
13302 reg_bitfield_target_p (rtx x, rtx body)
13304 int i;
13306 if (GET_CODE (body) == SET)
13308 rtx dest = SET_DEST (body);
13309 rtx target;
13310 unsigned int regno, tregno, endregno, endtregno;
13312 if (GET_CODE (dest) == ZERO_EXTRACT)
13313 target = XEXP (dest, 0);
13314 else if (GET_CODE (dest) == STRICT_LOW_PART)
13315 target = SUBREG_REG (XEXP (dest, 0));
13316 else
13317 return 0;
13319 if (GET_CODE (target) == SUBREG)
13320 target = SUBREG_REG (target);
13322 if (!REG_P (target))
13323 return 0;
13325 tregno = REGNO (target), regno = REGNO (x);
13326 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13327 return target == x;
13329 endtregno = end_hard_regno (GET_MODE (target), tregno);
13330 endregno = end_hard_regno (GET_MODE (x), regno);
13332 return endregno > tregno && regno < endtregno;
13335 else if (GET_CODE (body) == PARALLEL)
13336 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13337 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13338 return 1;
13340 return 0;
13343 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13344 as appropriate. I3 and I2 are the insns resulting from the combination
13345 insns including FROM (I2 may be zero).
13347 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13348 not need REG_DEAD notes because they are being substituted for. This
13349 saves searching in the most common cases.
13351 Each note in the list is either ignored or placed on some insns, depending
13352 on the type of note. */
13354 static void
13355 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13356 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13358 rtx note, next_note;
13359 rtx tem_note;
13360 rtx_insn *tem_insn;
13362 for (note = notes; note; note = next_note)
13364 rtx_insn *place = 0, *place2 = 0;
13366 next_note = XEXP (note, 1);
13367 switch (REG_NOTE_KIND (note))
13369 case REG_BR_PROB:
13370 case REG_BR_PRED:
13371 /* Doesn't matter much where we put this, as long as it's somewhere.
13372 It is preferable to keep these notes on branches, which is most
13373 likely to be i3. */
13374 place = i3;
13375 break;
13377 case REG_NON_LOCAL_GOTO:
13378 if (JUMP_P (i3))
13379 place = i3;
13380 else
13382 gcc_assert (i2 && JUMP_P (i2));
13383 place = i2;
13385 break;
13387 case REG_EH_REGION:
13388 /* These notes must remain with the call or trapping instruction. */
13389 if (CALL_P (i3))
13390 place = i3;
13391 else if (i2 && CALL_P (i2))
13392 place = i2;
13393 else
13395 gcc_assert (cfun->can_throw_non_call_exceptions);
13396 if (may_trap_p (i3))
13397 place = i3;
13398 else if (i2 && may_trap_p (i2))
13399 place = i2;
13400 /* ??? Otherwise assume we've combined things such that we
13401 can now prove that the instructions can't trap. Drop the
13402 note in this case. */
13404 break;
13406 case REG_ARGS_SIZE:
13407 /* ??? How to distribute between i3-i1. Assume i3 contains the
13408 entire adjustment. Assert i3 contains at least some adjust. */
13409 if (!noop_move_p (i3))
13411 int old_size, args_size = INTVAL (XEXP (note, 0));
13412 /* fixup_args_size_notes looks at REG_NORETURN note,
13413 so ensure the note is placed there first. */
13414 if (CALL_P (i3))
13416 rtx *np;
13417 for (np = &next_note; *np; np = &XEXP (*np, 1))
13418 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13420 rtx n = *np;
13421 *np = XEXP (n, 1);
13422 XEXP (n, 1) = REG_NOTES (i3);
13423 REG_NOTES (i3) = n;
13424 break;
13427 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13428 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13429 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13430 gcc_assert (old_size != args_size
13431 || (CALL_P (i3)
13432 && !ACCUMULATE_OUTGOING_ARGS
13433 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13435 break;
13437 case REG_NORETURN:
13438 case REG_SETJMP:
13439 case REG_TM:
13440 case REG_CALL_DECL:
13441 /* These notes must remain with the call. It should not be
13442 possible for both I2 and I3 to be a call. */
13443 if (CALL_P (i3))
13444 place = i3;
13445 else
13447 gcc_assert (i2 && CALL_P (i2));
13448 place = i2;
13450 break;
13452 case REG_UNUSED:
13453 /* Any clobbers for i3 may still exist, and so we must process
13454 REG_UNUSED notes from that insn.
13456 Any clobbers from i2 or i1 can only exist if they were added by
13457 recog_for_combine. In that case, recog_for_combine created the
13458 necessary REG_UNUSED notes. Trying to keep any original
13459 REG_UNUSED notes from these insns can cause incorrect output
13460 if it is for the same register as the original i3 dest.
13461 In that case, we will notice that the register is set in i3,
13462 and then add a REG_UNUSED note for the destination of i3, which
13463 is wrong. However, it is possible to have REG_UNUSED notes from
13464 i2 or i1 for register which were both used and clobbered, so
13465 we keep notes from i2 or i1 if they will turn into REG_DEAD
13466 notes. */
13468 /* If this register is set or clobbered in I3, put the note there
13469 unless there is one already. */
13470 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13472 if (from_insn != i3)
13473 break;
13475 if (! (REG_P (XEXP (note, 0))
13476 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13477 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13478 place = i3;
13480 /* Otherwise, if this register is used by I3, then this register
13481 now dies here, so we must put a REG_DEAD note here unless there
13482 is one already. */
13483 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13484 && ! (REG_P (XEXP (note, 0))
13485 ? find_regno_note (i3, REG_DEAD,
13486 REGNO (XEXP (note, 0)))
13487 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13489 PUT_REG_NOTE_KIND (note, REG_DEAD);
13490 place = i3;
13492 break;
13494 case REG_EQUAL:
13495 case REG_EQUIV:
13496 case REG_NOALIAS:
13497 /* These notes say something about results of an insn. We can
13498 only support them if they used to be on I3 in which case they
13499 remain on I3. Otherwise they are ignored.
13501 If the note refers to an expression that is not a constant, we
13502 must also ignore the note since we cannot tell whether the
13503 equivalence is still true. It might be possible to do
13504 slightly better than this (we only have a problem if I2DEST
13505 or I1DEST is present in the expression), but it doesn't
13506 seem worth the trouble. */
13508 if (from_insn == i3
13509 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13510 place = i3;
13511 break;
13513 case REG_INC:
13514 /* These notes say something about how a register is used. They must
13515 be present on any use of the register in I2 or I3. */
13516 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13517 place = i3;
13519 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13521 if (place)
13522 place2 = i2;
13523 else
13524 place = i2;
13526 break;
13528 case REG_LABEL_TARGET:
13529 case REG_LABEL_OPERAND:
13530 /* This can show up in several ways -- either directly in the
13531 pattern, or hidden off in the constant pool with (or without?)
13532 a REG_EQUAL note. */
13533 /* ??? Ignore the without-reg_equal-note problem for now. */
13534 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13535 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13536 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13537 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13538 place = i3;
13540 if (i2
13541 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13542 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13543 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13544 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13546 if (place)
13547 place2 = i2;
13548 else
13549 place = i2;
13552 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13553 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13554 there. */
13555 if (place && JUMP_P (place)
13556 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13557 && (JUMP_LABEL (place) == NULL
13558 || JUMP_LABEL (place) == XEXP (note, 0)))
13560 rtx label = JUMP_LABEL (place);
13562 if (!label)
13563 JUMP_LABEL (place) = XEXP (note, 0);
13564 else if (LABEL_P (label))
13565 LABEL_NUSES (label)--;
13568 if (place2 && JUMP_P (place2)
13569 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13570 && (JUMP_LABEL (place2) == NULL
13571 || JUMP_LABEL (place2) == XEXP (note, 0)))
13573 rtx label = JUMP_LABEL (place2);
13575 if (!label)
13576 JUMP_LABEL (place2) = XEXP (note, 0);
13577 else if (LABEL_P (label))
13578 LABEL_NUSES (label)--;
13579 place2 = 0;
13581 break;
13583 case REG_NONNEG:
13584 /* This note says something about the value of a register prior
13585 to the execution of an insn. It is too much trouble to see
13586 if the note is still correct in all situations. It is better
13587 to simply delete it. */
13588 break;
13590 case REG_DEAD:
13591 /* If we replaced the right hand side of FROM_INSN with a
13592 REG_EQUAL note, the original use of the dying register
13593 will not have been combined into I3 and I2. In such cases,
13594 FROM_INSN is guaranteed to be the first of the combined
13595 instructions, so we simply need to search back before
13596 FROM_INSN for the previous use or set of this register,
13597 then alter the notes there appropriately.
13599 If the register is used as an input in I3, it dies there.
13600 Similarly for I2, if it is nonzero and adjacent to I3.
13602 If the register is not used as an input in either I3 or I2
13603 and it is not one of the registers we were supposed to eliminate,
13604 there are two possibilities. We might have a non-adjacent I2
13605 or we might have somehow eliminated an additional register
13606 from a computation. For example, we might have had A & B where
13607 we discover that B will always be zero. In this case we will
13608 eliminate the reference to A.
13610 In both cases, we must search to see if we can find a previous
13611 use of A and put the death note there. */
13613 if (from_insn
13614 && from_insn == i2mod
13615 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13616 tem_insn = from_insn;
13617 else
13619 if (from_insn
13620 && CALL_P (from_insn)
13621 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13622 place = from_insn;
13623 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13624 place = i3;
13625 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13626 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13627 place = i2;
13628 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13629 && !(i2mod
13630 && reg_overlap_mentioned_p (XEXP (note, 0),
13631 i2mod_old_rhs)))
13632 || rtx_equal_p (XEXP (note, 0), elim_i1)
13633 || rtx_equal_p (XEXP (note, 0), elim_i0))
13634 break;
13635 tem_insn = i3;
13638 if (place == 0)
13640 basic_block bb = this_basic_block;
13642 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13644 if (!NONDEBUG_INSN_P (tem_insn))
13646 if (tem_insn == BB_HEAD (bb))
13647 break;
13648 continue;
13651 /* If the register is being set at TEM_INSN, see if that is all
13652 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13653 into a REG_UNUSED note instead. Don't delete sets to
13654 global register vars. */
13655 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13656 || !global_regs[REGNO (XEXP (note, 0))])
13657 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13659 rtx set = single_set (tem_insn);
13660 rtx inner_dest = 0;
13661 #ifdef HAVE_cc0
13662 rtx_insn *cc0_setter = NULL;
13663 #endif
13665 if (set != 0)
13666 for (inner_dest = SET_DEST (set);
13667 (GET_CODE (inner_dest) == STRICT_LOW_PART
13668 || GET_CODE (inner_dest) == SUBREG
13669 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13670 inner_dest = XEXP (inner_dest, 0))
13673 /* Verify that it was the set, and not a clobber that
13674 modified the register.
13676 CC0 targets must be careful to maintain setter/user
13677 pairs. If we cannot delete the setter due to side
13678 effects, mark the user with an UNUSED note instead
13679 of deleting it. */
13681 if (set != 0 && ! side_effects_p (SET_SRC (set))
13682 && rtx_equal_p (XEXP (note, 0), inner_dest)
13683 #ifdef HAVE_cc0
13684 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13685 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13686 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13687 #endif
13690 /* Move the notes and links of TEM_INSN elsewhere.
13691 This might delete other dead insns recursively.
13692 First set the pattern to something that won't use
13693 any register. */
13694 rtx old_notes = REG_NOTES (tem_insn);
13696 PATTERN (tem_insn) = pc_rtx;
13697 REG_NOTES (tem_insn) = NULL;
13699 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13700 NULL_RTX, NULL_RTX, NULL_RTX);
13701 distribute_links (LOG_LINKS (tem_insn));
13703 SET_INSN_DELETED (tem_insn);
13704 if (tem_insn == i2)
13705 i2 = NULL;
13707 #ifdef HAVE_cc0
13708 /* Delete the setter too. */
13709 if (cc0_setter)
13711 PATTERN (cc0_setter) = pc_rtx;
13712 old_notes = REG_NOTES (cc0_setter);
13713 REG_NOTES (cc0_setter) = NULL;
13715 distribute_notes (old_notes, cc0_setter,
13716 cc0_setter, NULL,
13717 NULL_RTX, NULL_RTX, NULL_RTX);
13718 distribute_links (LOG_LINKS (cc0_setter));
13720 SET_INSN_DELETED (cc0_setter);
13721 if (cc0_setter == i2)
13722 i2 = NULL;
13724 #endif
13726 else
13728 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13730 /* If there isn't already a REG_UNUSED note, put one
13731 here. Do not place a REG_DEAD note, even if
13732 the register is also used here; that would not
13733 match the algorithm used in lifetime analysis
13734 and can cause the consistency check in the
13735 scheduler to fail. */
13736 if (! find_regno_note (tem_insn, REG_UNUSED,
13737 REGNO (XEXP (note, 0))))
13738 place = tem_insn;
13739 break;
13742 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13743 || (CALL_P (tem_insn)
13744 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13746 place = tem_insn;
13748 /* If we are doing a 3->2 combination, and we have a
13749 register which formerly died in i3 and was not used
13750 by i2, which now no longer dies in i3 and is used in
13751 i2 but does not die in i2, and place is between i2
13752 and i3, then we may need to move a link from place to
13753 i2. */
13754 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13755 && from_insn
13756 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13757 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13759 struct insn_link *links = LOG_LINKS (place);
13760 LOG_LINKS (place) = NULL;
13761 distribute_links (links);
13763 break;
13766 if (tem_insn == BB_HEAD (bb))
13767 break;
13772 /* If the register is set or already dead at PLACE, we needn't do
13773 anything with this note if it is still a REG_DEAD note.
13774 We check here if it is set at all, not if is it totally replaced,
13775 which is what `dead_or_set_p' checks, so also check for it being
13776 set partially. */
13778 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13780 unsigned int regno = REGNO (XEXP (note, 0));
13781 reg_stat_type *rsp = &reg_stat[regno];
13783 if (dead_or_set_p (place, XEXP (note, 0))
13784 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13786 /* Unless the register previously died in PLACE, clear
13787 last_death. [I no longer understand why this is
13788 being done.] */
13789 if (rsp->last_death != place)
13790 rsp->last_death = 0;
13791 place = 0;
13793 else
13794 rsp->last_death = place;
13796 /* If this is a death note for a hard reg that is occupying
13797 multiple registers, ensure that we are still using all
13798 parts of the object. If we find a piece of the object
13799 that is unused, we must arrange for an appropriate REG_DEAD
13800 note to be added for it. However, we can't just emit a USE
13801 and tag the note to it, since the register might actually
13802 be dead; so we recourse, and the recursive call then finds
13803 the previous insn that used this register. */
13805 if (place && regno < FIRST_PSEUDO_REGISTER
13806 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13808 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13809 bool all_used = true;
13810 unsigned int i;
13812 for (i = regno; i < endregno; i++)
13813 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13814 && ! find_regno_fusage (place, USE, i))
13815 || dead_or_set_regno_p (place, i))
13817 all_used = false;
13818 break;
13821 if (! all_used)
13823 /* Put only REG_DEAD notes for pieces that are
13824 not already dead or set. */
13826 for (i = regno; i < endregno;
13827 i += hard_regno_nregs[i][reg_raw_mode[i]])
13829 rtx piece = regno_reg_rtx[i];
13830 basic_block bb = this_basic_block;
13832 if (! dead_or_set_p (place, piece)
13833 && ! reg_bitfield_target_p (piece,
13834 PATTERN (place)))
13836 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13837 NULL_RTX);
13839 distribute_notes (new_note, place, place,
13840 NULL, NULL_RTX, NULL_RTX,
13841 NULL_RTX);
13843 else if (! refers_to_regno_p (i, i + 1,
13844 PATTERN (place), 0)
13845 && ! find_regno_fusage (place, USE, i))
13846 for (tem_insn = PREV_INSN (place); ;
13847 tem_insn = PREV_INSN (tem_insn))
13849 if (!NONDEBUG_INSN_P (tem_insn))
13851 if (tem_insn == BB_HEAD (bb))
13852 break;
13853 continue;
13855 if (dead_or_set_p (tem_insn, piece)
13856 || reg_bitfield_target_p (piece,
13857 PATTERN (tem_insn)))
13859 add_reg_note (tem_insn, REG_UNUSED, piece);
13860 break;
13865 place = 0;
13869 break;
13871 default:
13872 /* Any other notes should not be present at this point in the
13873 compilation. */
13874 gcc_unreachable ();
13877 if (place)
13879 XEXP (note, 1) = REG_NOTES (place);
13880 REG_NOTES (place) = note;
13883 if (place2)
13884 add_shallow_copy_of_reg_note (place2, note);
13888 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13889 I3, I2, and I1 to new locations. This is also called to add a link
13890 pointing at I3 when I3's destination is changed. */
13892 static void
13893 distribute_links (struct insn_link *links)
13895 struct insn_link *link, *next_link;
13897 for (link = links; link; link = next_link)
13899 rtx_insn *place = 0;
13900 rtx_insn *insn;
13901 rtx set, reg;
13903 next_link = link->next;
13905 /* If the insn that this link points to is a NOTE, ignore it. */
13906 if (NOTE_P (link->insn))
13907 continue;
13909 set = 0;
13910 rtx pat = PATTERN (link->insn);
13911 if (GET_CODE (pat) == SET)
13912 set = pat;
13913 else if (GET_CODE (pat) == PARALLEL)
13915 int i;
13916 for (i = 0; i < XVECLEN (pat, 0); i++)
13918 set = XVECEXP (pat, 0, i);
13919 if (GET_CODE (set) != SET)
13920 continue;
13922 reg = SET_DEST (set);
13923 while (GET_CODE (reg) == ZERO_EXTRACT
13924 || GET_CODE (reg) == STRICT_LOW_PART
13925 || GET_CODE (reg) == SUBREG)
13926 reg = XEXP (reg, 0);
13928 if (!REG_P (reg))
13929 continue;
13931 if (REGNO (reg) == link->regno)
13932 break;
13934 if (i == XVECLEN (pat, 0))
13935 continue;
13937 else
13938 continue;
13940 reg = SET_DEST (set);
13942 while (GET_CODE (reg) == ZERO_EXTRACT
13943 || GET_CODE (reg) == STRICT_LOW_PART
13944 || GET_CODE (reg) == SUBREG)
13945 reg = XEXP (reg, 0);
13947 /* A LOG_LINK is defined as being placed on the first insn that uses
13948 a register and points to the insn that sets the register. Start
13949 searching at the next insn after the target of the link and stop
13950 when we reach a set of the register or the end of the basic block.
13952 Note that this correctly handles the link that used to point from
13953 I3 to I2. Also note that not much searching is typically done here
13954 since most links don't point very far away. */
13956 for (insn = NEXT_INSN (link->insn);
13957 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13958 || BB_HEAD (this_basic_block->next_bb) != insn));
13959 insn = NEXT_INSN (insn))
13960 if (DEBUG_INSN_P (insn))
13961 continue;
13962 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13964 if (reg_referenced_p (reg, PATTERN (insn)))
13965 place = insn;
13966 break;
13968 else if (CALL_P (insn)
13969 && find_reg_fusage (insn, USE, reg))
13971 place = insn;
13972 break;
13974 else if (INSN_P (insn) && reg_set_p (reg, insn))
13975 break;
13977 /* If we found a place to put the link, place it there unless there
13978 is already a link to the same insn as LINK at that point. */
13980 if (place)
13982 struct insn_link *link2;
13984 FOR_EACH_LOG_LINK (link2, place)
13985 if (link2->insn == link->insn && link2->regno == link->regno)
13986 break;
13988 if (link2 == NULL)
13990 link->next = LOG_LINKS (place);
13991 LOG_LINKS (place) = link;
13993 /* Set added_links_insn to the earliest insn we added a
13994 link to. */
13995 if (added_links_insn == 0
13996 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13997 added_links_insn = place;
14003 /* Check for any register or memory mentioned in EQUIV that is not
14004 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14005 of EXPR where some registers may have been replaced by constants. */
14007 static bool
14008 unmentioned_reg_p (rtx equiv, rtx expr)
14010 subrtx_iterator::array_type array;
14011 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14013 const_rtx x = *iter;
14014 if ((REG_P (x) || MEM_P (x))
14015 && !reg_mentioned_p (x, expr))
14016 return true;
14018 return false;
14021 DEBUG_FUNCTION void
14022 dump_combine_stats (FILE *file)
14024 fprintf
14025 (file,
14026 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14027 combine_attempts, combine_merges, combine_extras, combine_successes);
14030 void
14031 dump_combine_total_stats (FILE *file)
14033 fprintf
14034 (file,
14035 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14036 total_attempts, total_merges, total_extras, total_successes);
14039 /* Try combining insns through substitution. */
14040 static unsigned int
14041 rest_of_handle_combine (void)
14043 int rebuild_jump_labels_after_combine;
14045 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14046 df_note_add_problem ();
14047 df_analyze ();
14049 regstat_init_n_sets_and_refs ();
14051 rebuild_jump_labels_after_combine
14052 = combine_instructions (get_insns (), max_reg_num ());
14054 /* Combining insns may have turned an indirect jump into a
14055 direct jump. Rebuild the JUMP_LABEL fields of jumping
14056 instructions. */
14057 if (rebuild_jump_labels_after_combine)
14059 timevar_push (TV_JUMP);
14060 rebuild_jump_labels (get_insns ());
14061 cleanup_cfg (0);
14062 timevar_pop (TV_JUMP);
14065 regstat_free_n_sets_and_refs ();
14066 return 0;
14069 namespace {
14071 const pass_data pass_data_combine =
14073 RTL_PASS, /* type */
14074 "combine", /* name */
14075 OPTGROUP_NONE, /* optinfo_flags */
14076 TV_COMBINE, /* tv_id */
14077 PROP_cfglayout, /* properties_required */
14078 0, /* properties_provided */
14079 0, /* properties_destroyed */
14080 0, /* todo_flags_start */
14081 TODO_df_finish, /* todo_flags_finish */
14084 class pass_combine : public rtl_opt_pass
14086 public:
14087 pass_combine (gcc::context *ctxt)
14088 : rtl_opt_pass (pass_data_combine, ctxt)
14091 /* opt_pass methods: */
14092 virtual bool gate (function *) { return (optimize > 0); }
14093 virtual unsigned int execute (function *)
14095 return rest_of_handle_combine ();
14098 }; // class pass_combine
14100 } // anon namespace
14102 rtl_opt_pass *
14103 make_pass_combine (gcc::context *ctxt)
14105 return new pass_combine (ctxt);