PR rtl-optimization/59278
[official-gcc.git] / gcc / combine.c
blob63c1e4f3f6031d5fd0885d3f4274707307348649
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This module is essentially the "combiner" phase of the U. of Arizona
21 Portable Optimizer, but redone to work on our list-structured
22 representation for RTL instead of their string representation.
24 The LOG_LINKS of each insn identify the most recent assignment
25 to each REG used in the insn. It is a list of previous insns,
26 each of which contains a SET for a REG that is used in this insn
27 and not used or set in between. LOG_LINKs never cross basic blocks.
28 They were set up by the preceding pass (lifetime analysis).
30 We try to combine each pair of insns joined by a logical link.
31 We also try to combine triplets of insns A, B and C when C has
32 a link back to B and B has a link back to A. Likewise for a
33 small number of quadruplets of insns A, B, C and D for which
34 there's high likelihood of of success.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "stor-layout.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "predict.h"
90 #include "vec.h"
91 #include "hashtab.h"
92 #include "hash-set.h"
93 #include "machmode.h"
94 #include "input.h"
95 #include "function.h"
96 #include "dominance.h"
97 #include "cfg.h"
98 #include "cfgrtl.h"
99 #include "cfgcleanup.h"
100 #include "basic-block.h"
101 #include "insn-config.h"
102 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
103 #include "expr.h"
104 #include "insn-attr.h"
105 #include "recog.h"
106 #include "diagnostic-core.h"
107 #include "target.h"
108 #include "insn-codes.h"
109 #include "optabs.h"
110 #include "rtlhooks-def.h"
111 #include "params.h"
112 #include "tree-pass.h"
113 #include "df.h"
114 #include "valtrack.h"
115 #include "hash-map.h"
116 #include "is-a.h"
117 #include "plugin-api.h"
118 #include "ipa-ref.h"
119 #include "cgraph.h"
120 #include "obstack.h"
121 #include "statistics.h"
122 #include "params.h"
123 #include "rtl-iter.h"
125 /* Number of attempts to combine instructions in this function. */
127 static int combine_attempts;
129 /* Number of attempts that got as far as substitution in this function. */
131 static int combine_merges;
133 /* Number of instructions combined with added SETs in this function. */
135 static int combine_extras;
137 /* Number of instructions combined in this function. */
139 static int combine_successes;
141 /* Totals over entire compilation. */
143 static int total_attempts, total_merges, total_extras, total_successes;
145 /* combine_instructions may try to replace the right hand side of the
146 second instruction with the value of an associated REG_EQUAL note
147 before throwing it at try_combine. That is problematic when there
148 is a REG_DEAD note for a register used in the old right hand side
149 and can cause distribute_notes to do wrong things. This is the
150 second instruction if it has been so modified, null otherwise. */
152 static rtx_insn *i2mod;
154 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
156 static rtx i2mod_old_rhs;
158 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
160 static rtx i2mod_new_rhs;
162 typedef struct reg_stat_struct {
163 /* Record last point of death of (hard or pseudo) register n. */
164 rtx_insn *last_death;
166 /* Record last point of modification of (hard or pseudo) register n. */
167 rtx_insn *last_set;
169 /* The next group of fields allows the recording of the last value assigned
170 to (hard or pseudo) register n. We use this information to see if an
171 operation being processed is redundant given a prior operation performed
172 on the register. For example, an `and' with a constant is redundant if
173 all the zero bits are already known to be turned off.
175 We use an approach similar to that used by cse, but change it in the
176 following ways:
178 (1) We do not want to reinitialize at each label.
179 (2) It is useful, but not critical, to know the actual value assigned
180 to a register. Often just its form is helpful.
182 Therefore, we maintain the following fields:
184 last_set_value the last value assigned
185 last_set_label records the value of label_tick when the
186 register was assigned
187 last_set_table_tick records the value of label_tick when a
188 value using the register is assigned
189 last_set_invalid set to nonzero when it is not valid
190 to use the value of this register in some
191 register's value
193 To understand the usage of these tables, it is important to understand
194 the distinction between the value in last_set_value being valid and
195 the register being validly contained in some other expression in the
196 table.
198 (The next two parameters are out of date).
200 reg_stat[i].last_set_value is valid if it is nonzero, and either
201 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
203 Register I may validly appear in any expression returned for the value
204 of another register if reg_n_sets[i] is 1. It may also appear in the
205 value for register J if reg_stat[j].last_set_invalid is zero, or
206 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
208 If an expression is found in the table containing a register which may
209 not validly appear in an expression, the register is replaced by
210 something that won't match, (clobber (const_int 0)). */
212 /* Record last value assigned to (hard or pseudo) register n. */
214 rtx last_set_value;
216 /* Record the value of label_tick when an expression involving register n
217 is placed in last_set_value. */
219 int last_set_table_tick;
221 /* Record the value of label_tick when the value for register n is placed in
222 last_set_value. */
224 int last_set_label;
226 /* These fields are maintained in parallel with last_set_value and are
227 used to store the mode in which the register was last set, the bits
228 that were known to be zero when it was last set, and the number of
229 sign bits copies it was known to have when it was last set. */
231 unsigned HOST_WIDE_INT last_set_nonzero_bits;
232 char last_set_sign_bit_copies;
233 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
235 /* Set nonzero if references to register n in expressions should not be
236 used. last_set_invalid is set nonzero when this register is being
237 assigned to and last_set_table_tick == label_tick. */
239 char last_set_invalid;
241 /* Some registers that are set more than once and used in more than one
242 basic block are nevertheless always set in similar ways. For example,
243 a QImode register may be loaded from memory in two places on a machine
244 where byte loads zero extend.
246 We record in the following fields if a register has some leading bits
247 that are always equal to the sign bit, and what we know about the
248 nonzero bits of a register, specifically which bits are known to be
249 zero.
251 If an entry is zero, it means that we don't know anything special. */
253 unsigned char sign_bit_copies;
255 unsigned HOST_WIDE_INT nonzero_bits;
257 /* Record the value of the label_tick when the last truncation
258 happened. The field truncated_to_mode is only valid if
259 truncation_label == label_tick. */
261 int truncation_label;
263 /* Record the last truncation seen for this register. If truncation
264 is not a nop to this mode we might be able to save an explicit
265 truncation if we know that value already contains a truncated
266 value. */
268 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
269 } reg_stat_type;
272 static vec<reg_stat_type> reg_stat;
274 /* Record the luid of the last insn that invalidated memory
275 (anything that writes memory, and subroutine calls, but not pushes). */
277 static int mem_last_set;
279 /* Record the luid of the last CALL_INSN
280 so we can tell whether a potential combination crosses any calls. */
282 static int last_call_luid;
284 /* When `subst' is called, this is the insn that is being modified
285 (by combining in a previous insn). The PATTERN of this insn
286 is still the old pattern partially modified and it should not be
287 looked at, but this may be used to examine the successors of the insn
288 to judge whether a simplification is valid. */
290 static rtx_insn *subst_insn;
292 /* This is the lowest LUID that `subst' is currently dealing with.
293 get_last_value will not return a value if the register was set at or
294 after this LUID. If not for this mechanism, we could get confused if
295 I2 or I1 in try_combine were an insn that used the old value of a register
296 to obtain a new value. In that case, we might erroneously get the
297 new value of the register when we wanted the old one. */
299 static int subst_low_luid;
301 /* This contains any hard registers that are used in newpat; reg_dead_at_p
302 must consider all these registers to be always live. */
304 static HARD_REG_SET newpat_used_regs;
306 /* This is an insn to which a LOG_LINKS entry has been added. If this
307 insn is the earlier than I2 or I3, combine should rescan starting at
308 that location. */
310 static rtx_insn *added_links_insn;
312 /* Basic block in which we are performing combines. */
313 static basic_block this_basic_block;
314 static bool optimize_this_for_speed_p;
317 /* Length of the currently allocated uid_insn_cost array. */
319 static int max_uid_known;
321 /* The following array records the insn_rtx_cost for every insn
322 in the instruction stream. */
324 static int *uid_insn_cost;
326 /* The following array records the LOG_LINKS for every insn in the
327 instruction stream as struct insn_link pointers. */
329 struct insn_link {
330 rtx_insn *insn;
331 unsigned int regno;
332 struct insn_link *next;
335 static struct insn_link **uid_log_links;
337 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
338 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
340 #define FOR_EACH_LOG_LINK(L, INSN) \
341 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
343 /* Links for LOG_LINKS are allocated from this obstack. */
345 static struct obstack insn_link_obstack;
347 /* Allocate a link. */
349 static inline struct insn_link *
350 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
352 struct insn_link *l
353 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
354 sizeof (struct insn_link));
355 l->insn = insn;
356 l->regno = regno;
357 l->next = next;
358 return l;
361 /* Incremented for each basic block. */
363 static int label_tick;
365 /* Reset to label_tick for each extended basic block in scanning order. */
367 static int label_tick_ebb_start;
369 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
370 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
372 static machine_mode nonzero_bits_mode;
374 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
375 be safely used. It is zero while computing them and after combine has
376 completed. This former test prevents propagating values based on
377 previously set values, which can be incorrect if a variable is modified
378 in a loop. */
380 static int nonzero_sign_valid;
383 /* Record one modification to rtl structure
384 to be undone by storing old_contents into *where. */
386 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
388 struct undo
390 struct undo *next;
391 enum undo_kind kind;
392 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
393 union { rtx *r; int *i; struct insn_link **l; } where;
396 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
397 num_undo says how many are currently recorded.
399 other_insn is nonzero if we have modified some other insn in the process
400 of working on subst_insn. It must be verified too. */
402 struct undobuf
404 struct undo *undos;
405 struct undo *frees;
406 rtx_insn *other_insn;
409 static struct undobuf undobuf;
411 /* Number of times the pseudo being substituted for
412 was found and replaced. */
414 static int n_occurrences;
416 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
417 machine_mode,
418 unsigned HOST_WIDE_INT,
419 unsigned HOST_WIDE_INT *);
420 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
421 machine_mode,
422 unsigned int, unsigned int *);
423 static void do_SUBST (rtx *, rtx);
424 static void do_SUBST_INT (int *, int);
425 static void init_reg_last (void);
426 static void setup_incoming_promotions (rtx_insn *);
427 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
428 static int cant_combine_insn_p (rtx_insn *);
429 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
430 rtx_insn *, rtx_insn *, rtx *, rtx *);
431 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
432 static int contains_muldiv (rtx);
433 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
434 int *, rtx_insn *);
435 static void undo_all (void);
436 static void undo_commit (void);
437 static rtx *find_split_point (rtx *, rtx_insn *, bool);
438 static rtx subst (rtx, rtx, rtx, int, int, int);
439 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
440 static rtx simplify_if_then_else (rtx);
441 static rtx simplify_set (rtx);
442 static rtx simplify_logical (rtx);
443 static rtx expand_compound_operation (rtx);
444 static const_rtx expand_field_assignment (const_rtx);
445 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
446 rtx, unsigned HOST_WIDE_INT, int, int, int);
447 static rtx extract_left_shift (rtx, int);
448 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
449 unsigned HOST_WIDE_INT *);
450 static rtx canon_reg_for_combine (rtx, rtx);
451 static rtx force_to_mode (rtx, machine_mode,
452 unsigned HOST_WIDE_INT, int);
453 static rtx if_then_else_cond (rtx, rtx *, rtx *);
454 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
455 static int rtx_equal_for_field_assignment_p (rtx, rtx);
456 static rtx make_field_assignment (rtx);
457 static rtx apply_distributive_law (rtx);
458 static rtx distribute_and_simplify_rtx (rtx, int);
459 static rtx simplify_and_const_int_1 (machine_mode, rtx,
460 unsigned HOST_WIDE_INT);
461 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
462 unsigned HOST_WIDE_INT);
463 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
464 HOST_WIDE_INT, machine_mode, int *);
465 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
466 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
467 int);
468 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
469 static rtx gen_lowpart_for_combine (machine_mode, rtx);
470 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
471 rtx, rtx *);
472 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
473 static void update_table_tick (rtx);
474 static void record_value_for_reg (rtx, rtx_insn *, rtx);
475 static void check_promoted_subreg (rtx_insn *, rtx);
476 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
477 static void record_dead_and_set_regs (rtx_insn *);
478 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
479 static rtx get_last_value (const_rtx);
480 static int use_crosses_set_p (const_rtx, int);
481 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
482 static int reg_dead_at_p (rtx, rtx_insn *);
483 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
484 static int reg_bitfield_target_p (rtx, rtx);
485 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
486 static void distribute_links (struct insn_link *);
487 static void mark_used_regs_combine (rtx);
488 static void record_promoted_value (rtx_insn *, rtx);
489 static bool unmentioned_reg_p (rtx, rtx);
490 static void record_truncated_values (rtx *, void *);
491 static bool reg_truncated_to_mode (machine_mode, const_rtx);
492 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
495 /* It is not safe to use ordinary gen_lowpart in combine.
496 See comments in gen_lowpart_for_combine. */
497 #undef RTL_HOOKS_GEN_LOWPART
498 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
500 /* Our implementation of gen_lowpart never emits a new pseudo. */
501 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
502 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
504 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
505 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
507 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
508 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
510 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
511 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
513 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
516 /* Convenience wrapper for the canonicalize_comparison target hook.
517 Target hooks cannot use enum rtx_code. */
518 static inline void
519 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
520 bool op0_preserve_value)
522 int code_int = (int)*code;
523 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
524 *code = (enum rtx_code)code_int;
527 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
528 PATTERN can not be split. Otherwise, it returns an insn sequence.
529 This is a wrapper around split_insns which ensures that the
530 reg_stat vector is made larger if the splitter creates a new
531 register. */
533 static rtx_insn *
534 combine_split_insns (rtx pattern, rtx insn)
536 rtx_insn *ret;
537 unsigned int nregs;
539 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
540 nregs = max_reg_num ();
541 if (nregs > reg_stat.length ())
542 reg_stat.safe_grow_cleared (nregs);
543 return ret;
546 /* This is used by find_single_use to locate an rtx in LOC that
547 contains exactly one use of DEST, which is typically either a REG
548 or CC0. It returns a pointer to the innermost rtx expression
549 containing DEST. Appearances of DEST that are being used to
550 totally replace it are not counted. */
552 static rtx *
553 find_single_use_1 (rtx dest, rtx *loc)
555 rtx x = *loc;
556 enum rtx_code code = GET_CODE (x);
557 rtx *result = NULL;
558 rtx *this_result;
559 int i;
560 const char *fmt;
562 switch (code)
564 case CONST:
565 case LABEL_REF:
566 case SYMBOL_REF:
567 CASE_CONST_ANY:
568 case CLOBBER:
569 return 0;
571 case SET:
572 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
573 of a REG that occupies all of the REG, the insn uses DEST if
574 it is mentioned in the destination or the source. Otherwise, we
575 need just check the source. */
576 if (GET_CODE (SET_DEST (x)) != CC0
577 && GET_CODE (SET_DEST (x)) != PC
578 && !REG_P (SET_DEST (x))
579 && ! (GET_CODE (SET_DEST (x)) == SUBREG
580 && REG_P (SUBREG_REG (SET_DEST (x)))
581 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
582 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
583 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
584 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
585 break;
587 return find_single_use_1 (dest, &SET_SRC (x));
589 case MEM:
590 case SUBREG:
591 return find_single_use_1 (dest, &XEXP (x, 0));
593 default:
594 break;
597 /* If it wasn't one of the common cases above, check each expression and
598 vector of this code. Look for a unique usage of DEST. */
600 fmt = GET_RTX_FORMAT (code);
601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
603 if (fmt[i] == 'e')
605 if (dest == XEXP (x, i)
606 || (REG_P (dest) && REG_P (XEXP (x, i))
607 && REGNO (dest) == REGNO (XEXP (x, i))))
608 this_result = loc;
609 else
610 this_result = find_single_use_1 (dest, &XEXP (x, i));
612 if (result == NULL)
613 result = this_result;
614 else if (this_result)
615 /* Duplicate usage. */
616 return NULL;
618 else if (fmt[i] == 'E')
620 int j;
622 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
624 if (XVECEXP (x, i, j) == dest
625 || (REG_P (dest)
626 && REG_P (XVECEXP (x, i, j))
627 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
628 this_result = loc;
629 else
630 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
632 if (result == NULL)
633 result = this_result;
634 else if (this_result)
635 return NULL;
640 return result;
644 /* See if DEST, produced in INSN, is used only a single time in the
645 sequel. If so, return a pointer to the innermost rtx expression in which
646 it is used.
648 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
650 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
651 care about REG_DEAD notes or LOG_LINKS.
653 Otherwise, we find the single use by finding an insn that has a
654 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
655 only referenced once in that insn, we know that it must be the first
656 and last insn referencing DEST. */
658 static rtx *
659 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
661 basic_block bb;
662 rtx_insn *next;
663 rtx *result;
664 struct insn_link *link;
666 #ifdef HAVE_cc0
667 if (dest == cc0_rtx)
669 next = NEXT_INSN (insn);
670 if (next == 0
671 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
672 return 0;
674 result = find_single_use_1 (dest, &PATTERN (next));
675 if (result && ploc)
676 *ploc = next;
677 return result;
679 #endif
681 if (!REG_P (dest))
682 return 0;
684 bb = BLOCK_FOR_INSN (insn);
685 for (next = NEXT_INSN (insn);
686 next && BLOCK_FOR_INSN (next) == bb;
687 next = NEXT_INSN (next))
688 if (INSN_P (next) && dead_or_set_p (next, dest))
690 FOR_EACH_LOG_LINK (link, next)
691 if (link->insn == insn && link->regno == REGNO (dest))
692 break;
694 if (link)
696 result = find_single_use_1 (dest, &PATTERN (next));
697 if (ploc)
698 *ploc = next;
699 return result;
703 return 0;
706 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
707 insn. The substitution can be undone by undo_all. If INTO is already
708 set to NEWVAL, do not record this change. Because computing NEWVAL might
709 also call SUBST, we have to compute it before we put anything into
710 the undo table. */
712 static void
713 do_SUBST (rtx *into, rtx newval)
715 struct undo *buf;
716 rtx oldval = *into;
718 if (oldval == newval)
719 return;
721 /* We'd like to catch as many invalid transformations here as
722 possible. Unfortunately, there are way too many mode changes
723 that are perfectly valid, so we'd waste too much effort for
724 little gain doing the checks here. Focus on catching invalid
725 transformations involving integer constants. */
726 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
727 && CONST_INT_P (newval))
729 /* Sanity check that we're replacing oldval with a CONST_INT
730 that is a valid sign-extension for the original mode. */
731 gcc_assert (INTVAL (newval)
732 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
734 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
735 CONST_INT is not valid, because after the replacement, the
736 original mode would be gone. Unfortunately, we can't tell
737 when do_SUBST is called to replace the operand thereof, so we
738 perform this test on oldval instead, checking whether an
739 invalid replacement took place before we got here. */
740 gcc_assert (!(GET_CODE (oldval) == SUBREG
741 && CONST_INT_P (SUBREG_REG (oldval))));
742 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
743 && CONST_INT_P (XEXP (oldval, 0))));
746 if (undobuf.frees)
747 buf = undobuf.frees, undobuf.frees = buf->next;
748 else
749 buf = XNEW (struct undo);
751 buf->kind = UNDO_RTX;
752 buf->where.r = into;
753 buf->old_contents.r = oldval;
754 *into = newval;
756 buf->next = undobuf.undos, undobuf.undos = buf;
759 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
761 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
762 for the value of a HOST_WIDE_INT value (including CONST_INT) is
763 not safe. */
765 static void
766 do_SUBST_INT (int *into, int newval)
768 struct undo *buf;
769 int oldval = *into;
771 if (oldval == newval)
772 return;
774 if (undobuf.frees)
775 buf = undobuf.frees, undobuf.frees = buf->next;
776 else
777 buf = XNEW (struct undo);
779 buf->kind = UNDO_INT;
780 buf->where.i = into;
781 buf->old_contents.i = oldval;
782 *into = newval;
784 buf->next = undobuf.undos, undobuf.undos = buf;
787 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
789 /* Similar to SUBST, but just substitute the mode. This is used when
790 changing the mode of a pseudo-register, so that any other
791 references to the entry in the regno_reg_rtx array will change as
792 well. */
794 static void
795 do_SUBST_MODE (rtx *into, machine_mode newval)
797 struct undo *buf;
798 machine_mode oldval = GET_MODE (*into);
800 if (oldval == newval)
801 return;
803 if (undobuf.frees)
804 buf = undobuf.frees, undobuf.frees = buf->next;
805 else
806 buf = XNEW (struct undo);
808 buf->kind = UNDO_MODE;
809 buf->where.r = into;
810 buf->old_contents.m = oldval;
811 adjust_reg_mode (*into, newval);
813 buf->next = undobuf.undos, undobuf.undos = buf;
816 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
818 #ifndef HAVE_cc0
819 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
821 static void
822 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
824 struct undo *buf;
825 struct insn_link * oldval = *into;
827 if (oldval == newval)
828 return;
830 if (undobuf.frees)
831 buf = undobuf.frees, undobuf.frees = buf->next;
832 else
833 buf = XNEW (struct undo);
835 buf->kind = UNDO_LINKS;
836 buf->where.l = into;
837 buf->old_contents.l = oldval;
838 *into = newval;
840 buf->next = undobuf.undos, undobuf.undos = buf;
843 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
844 #endif
846 /* Subroutine of try_combine. Determine whether the replacement patterns
847 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
848 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
849 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
850 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
851 of all the instructions can be estimated and the replacements are more
852 expensive than the original sequence. */
854 static bool
855 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
856 rtx newpat, rtx newi2pat, rtx newotherpat)
858 int i0_cost, i1_cost, i2_cost, i3_cost;
859 int new_i2_cost, new_i3_cost;
860 int old_cost, new_cost;
862 /* Lookup the original insn_rtx_costs. */
863 i2_cost = INSN_COST (i2);
864 i3_cost = INSN_COST (i3);
866 if (i1)
868 i1_cost = INSN_COST (i1);
869 if (i0)
871 i0_cost = INSN_COST (i0);
872 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
873 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
875 else
877 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
878 ? i1_cost + i2_cost + i3_cost : 0);
879 i0_cost = 0;
882 else
884 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
885 i1_cost = i0_cost = 0;
888 /* Calculate the replacement insn_rtx_costs. */
889 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
890 if (newi2pat)
892 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
893 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
894 ? new_i2_cost + new_i3_cost : 0;
896 else
898 new_cost = new_i3_cost;
899 new_i2_cost = 0;
902 if (undobuf.other_insn)
904 int old_other_cost, new_other_cost;
906 old_other_cost = INSN_COST (undobuf.other_insn);
907 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
908 if (old_other_cost > 0 && new_other_cost > 0)
910 old_cost += old_other_cost;
911 new_cost += new_other_cost;
913 else
914 old_cost = 0;
917 /* Disallow this combination if both new_cost and old_cost are greater than
918 zero, and new_cost is greater than old cost. */
919 int reject = old_cost > 0 && new_cost > old_cost;
921 if (dump_file)
923 fprintf (dump_file, "%s combination of insns ",
924 reject ? "rejecting" : "allowing");
925 if (i0)
926 fprintf (dump_file, "%d, ", INSN_UID (i0));
927 if (i1)
928 fprintf (dump_file, "%d, ", INSN_UID (i1));
929 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
931 fprintf (dump_file, "original costs ");
932 if (i0)
933 fprintf (dump_file, "%d + ", i0_cost);
934 if (i1)
935 fprintf (dump_file, "%d + ", i1_cost);
936 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
938 if (newi2pat)
939 fprintf (dump_file, "replacement costs %d + %d = %d\n",
940 new_i2_cost, new_i3_cost, new_cost);
941 else
942 fprintf (dump_file, "replacement cost %d\n", new_cost);
945 if (reject)
946 return false;
948 /* Update the uid_insn_cost array with the replacement costs. */
949 INSN_COST (i2) = new_i2_cost;
950 INSN_COST (i3) = new_i3_cost;
951 if (i1)
953 INSN_COST (i1) = 0;
954 if (i0)
955 INSN_COST (i0) = 0;
958 return true;
962 /* Delete any insns that copy a register to itself. */
964 static void
965 delete_noop_moves (void)
967 rtx_insn *insn, *next;
968 basic_block bb;
970 FOR_EACH_BB_FN (bb, cfun)
972 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
974 next = NEXT_INSN (insn);
975 if (INSN_P (insn) && noop_move_p (insn))
977 if (dump_file)
978 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
980 delete_insn_and_edges (insn);
987 /* Return false if we do not want to (or cannot) combine DEF. */
988 static bool
989 can_combine_def_p (df_ref def)
991 /* Do not consider if it is pre/post modification in MEM. */
992 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
993 return false;
995 unsigned int regno = DF_REF_REGNO (def);
997 /* Do not combine frame pointer adjustments. */
998 if ((regno == FRAME_POINTER_REGNUM
999 && (!reload_completed || frame_pointer_needed))
1000 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1001 || (regno == HARD_FRAME_POINTER_REGNUM
1002 && (!reload_completed || frame_pointer_needed))
1003 #endif
1004 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1005 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1006 #endif
1008 return false;
1010 return true;
1013 /* Return false if we do not want to (or cannot) combine USE. */
1014 static bool
1015 can_combine_use_p (df_ref use)
1017 /* Do not consider the usage of the stack pointer by function call. */
1018 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1019 return false;
1021 return true;
1024 /* Fill in log links field for all insns. */
1026 static void
1027 create_log_links (void)
1029 basic_block bb;
1030 rtx_insn **next_use;
1031 rtx_insn *insn;
1032 df_ref def, use;
1034 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1036 /* Pass through each block from the end, recording the uses of each
1037 register and establishing log links when def is encountered.
1038 Note that we do not clear next_use array in order to save time,
1039 so we have to test whether the use is in the same basic block as def.
1041 There are a few cases below when we do not consider the definition or
1042 usage -- these are taken from original flow.c did. Don't ask me why it is
1043 done this way; I don't know and if it works, I don't want to know. */
1045 FOR_EACH_BB_FN (bb, cfun)
1047 FOR_BB_INSNS_REVERSE (bb, insn)
1049 if (!NONDEBUG_INSN_P (insn))
1050 continue;
1052 /* Log links are created only once. */
1053 gcc_assert (!LOG_LINKS (insn));
1055 FOR_EACH_INSN_DEF (def, insn)
1057 unsigned int regno = DF_REF_REGNO (def);
1058 rtx_insn *use_insn;
1060 if (!next_use[regno])
1061 continue;
1063 if (!can_combine_def_p (def))
1064 continue;
1066 use_insn = next_use[regno];
1067 next_use[regno] = NULL;
1069 if (BLOCK_FOR_INSN (use_insn) != bb)
1070 continue;
1072 /* flow.c claimed:
1074 We don't build a LOG_LINK for hard registers contained
1075 in ASM_OPERANDs. If these registers get replaced,
1076 we might wind up changing the semantics of the insn,
1077 even if reload can make what appear to be valid
1078 assignments later. */
1079 if (regno < FIRST_PSEUDO_REGISTER
1080 && asm_noperands (PATTERN (use_insn)) >= 0)
1081 continue;
1083 /* Don't add duplicate links between instructions. */
1084 struct insn_link *links;
1085 FOR_EACH_LOG_LINK (links, use_insn)
1086 if (insn == links->insn && regno == links->regno)
1087 break;
1089 if (!links)
1090 LOG_LINKS (use_insn)
1091 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1094 FOR_EACH_INSN_USE (use, insn)
1095 if (can_combine_use_p (use))
1096 next_use[DF_REF_REGNO (use)] = insn;
1100 free (next_use);
1103 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1104 true if we found a LOG_LINK that proves that A feeds B. This only works
1105 if there are no instructions between A and B which could have a link
1106 depending on A, since in that case we would not record a link for B.
1107 We also check the implicit dependency created by a cc0 setter/user
1108 pair. */
1110 static bool
1111 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1113 struct insn_link *links;
1114 FOR_EACH_LOG_LINK (links, b)
1115 if (links->insn == a)
1116 return true;
1117 #ifdef HAVE_cc0
1118 if (sets_cc0_p (a))
1119 return true;
1120 #endif
1121 return false;
1124 /* Main entry point for combiner. F is the first insn of the function.
1125 NREGS is the first unused pseudo-reg number.
1127 Return nonzero if the combiner has turned an indirect jump
1128 instruction into a direct jump. */
1129 static int
1130 combine_instructions (rtx_insn *f, unsigned int nregs)
1132 rtx_insn *insn, *next;
1133 #ifdef HAVE_cc0
1134 rtx_insn *prev;
1135 #endif
1136 struct insn_link *links, *nextlinks;
1137 rtx_insn *first;
1138 basic_block last_bb;
1140 int new_direct_jump_p = 0;
1142 for (first = f; first && !INSN_P (first); )
1143 first = NEXT_INSN (first);
1144 if (!first)
1145 return 0;
1147 combine_attempts = 0;
1148 combine_merges = 0;
1149 combine_extras = 0;
1150 combine_successes = 0;
1152 rtl_hooks = combine_rtl_hooks;
1154 reg_stat.safe_grow_cleared (nregs);
1156 init_recog_no_volatile ();
1158 /* Allocate array for insn info. */
1159 max_uid_known = get_max_uid ();
1160 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1161 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1162 gcc_obstack_init (&insn_link_obstack);
1164 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1166 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1167 problems when, for example, we have j <<= 1 in a loop. */
1169 nonzero_sign_valid = 0;
1170 label_tick = label_tick_ebb_start = 1;
1172 /* Scan all SETs and see if we can deduce anything about what
1173 bits are known to be zero for some registers and how many copies
1174 of the sign bit are known to exist for those registers.
1176 Also set any known values so that we can use it while searching
1177 for what bits are known to be set. */
1179 setup_incoming_promotions (first);
1180 /* Allow the entry block and the first block to fall into the same EBB.
1181 Conceptually the incoming promotions are assigned to the entry block. */
1182 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1184 create_log_links ();
1185 FOR_EACH_BB_FN (this_basic_block, cfun)
1187 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1188 last_call_luid = 0;
1189 mem_last_set = -1;
1191 label_tick++;
1192 if (!single_pred_p (this_basic_block)
1193 || single_pred (this_basic_block) != last_bb)
1194 label_tick_ebb_start = label_tick;
1195 last_bb = this_basic_block;
1197 FOR_BB_INSNS (this_basic_block, insn)
1198 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1200 #ifdef AUTO_INC_DEC
1201 rtx links;
1202 #endif
1204 subst_low_luid = DF_INSN_LUID (insn);
1205 subst_insn = insn;
1207 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1208 insn);
1209 record_dead_and_set_regs (insn);
1211 #ifdef AUTO_INC_DEC
1212 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1213 if (REG_NOTE_KIND (links) == REG_INC)
1214 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1215 insn);
1216 #endif
1218 /* Record the current insn_rtx_cost of this instruction. */
1219 if (NONJUMP_INSN_P (insn))
1220 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1221 optimize_this_for_speed_p);
1222 if (dump_file)
1223 fprintf (dump_file, "insn_cost %d: %d\n",
1224 INSN_UID (insn), INSN_COST (insn));
1228 nonzero_sign_valid = 1;
1230 /* Now scan all the insns in forward order. */
1231 label_tick = label_tick_ebb_start = 1;
1232 init_reg_last ();
1233 setup_incoming_promotions (first);
1234 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1235 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1237 FOR_EACH_BB_FN (this_basic_block, cfun)
1239 rtx_insn *last_combined_insn = NULL;
1240 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1241 last_call_luid = 0;
1242 mem_last_set = -1;
1244 label_tick++;
1245 if (!single_pred_p (this_basic_block)
1246 || single_pred (this_basic_block) != last_bb)
1247 label_tick_ebb_start = label_tick;
1248 last_bb = this_basic_block;
1250 rtl_profile_for_bb (this_basic_block);
1251 for (insn = BB_HEAD (this_basic_block);
1252 insn != NEXT_INSN (BB_END (this_basic_block));
1253 insn = next ? next : NEXT_INSN (insn))
1255 next = 0;
1256 if (!NONDEBUG_INSN_P (insn))
1257 continue;
1259 while (last_combined_insn
1260 && last_combined_insn->deleted ())
1261 last_combined_insn = PREV_INSN (last_combined_insn);
1262 if (last_combined_insn == NULL_RTX
1263 || BARRIER_P (last_combined_insn)
1264 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1265 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1266 last_combined_insn = insn;
1268 /* See if we know about function return values before this
1269 insn based upon SUBREG flags. */
1270 check_promoted_subreg (insn, PATTERN (insn));
1272 /* See if we can find hardregs and subreg of pseudos in
1273 narrower modes. This could help turning TRUNCATEs
1274 into SUBREGs. */
1275 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1277 /* Try this insn with each insn it links back to. */
1279 FOR_EACH_LOG_LINK (links, insn)
1280 if ((next = try_combine (insn, links->insn, NULL,
1281 NULL, &new_direct_jump_p,
1282 last_combined_insn)) != 0)
1284 statistics_counter_event (cfun, "two-insn combine", 1);
1285 goto retry;
1288 /* Try each sequence of three linked insns ending with this one. */
1290 if (max_combine >= 3)
1291 FOR_EACH_LOG_LINK (links, insn)
1293 rtx_insn *link = links->insn;
1295 /* If the linked insn has been replaced by a note, then there
1296 is no point in pursuing this chain any further. */
1297 if (NOTE_P (link))
1298 continue;
1300 FOR_EACH_LOG_LINK (nextlinks, link)
1301 if ((next = try_combine (insn, link, nextlinks->insn,
1302 NULL, &new_direct_jump_p,
1303 last_combined_insn)) != 0)
1305 statistics_counter_event (cfun, "three-insn combine", 1);
1306 goto retry;
1310 #ifdef HAVE_cc0
1311 /* Try to combine a jump insn that uses CC0
1312 with a preceding insn that sets CC0, and maybe with its
1313 logical predecessor as well.
1314 This is how we make decrement-and-branch insns.
1315 We need this special code because data flow connections
1316 via CC0 do not get entered in LOG_LINKS. */
1318 if (JUMP_P (insn)
1319 && (prev = prev_nonnote_insn (insn)) != 0
1320 && NONJUMP_INSN_P (prev)
1321 && sets_cc0_p (PATTERN (prev)))
1323 if ((next = try_combine (insn, prev, NULL, NULL,
1324 &new_direct_jump_p,
1325 last_combined_insn)) != 0)
1326 goto retry;
1328 FOR_EACH_LOG_LINK (nextlinks, prev)
1329 if ((next = try_combine (insn, prev, nextlinks->insn,
1330 NULL, &new_direct_jump_p,
1331 last_combined_insn)) != 0)
1332 goto retry;
1335 /* Do the same for an insn that explicitly references CC0. */
1336 if (NONJUMP_INSN_P (insn)
1337 && (prev = prev_nonnote_insn (insn)) != 0
1338 && NONJUMP_INSN_P (prev)
1339 && sets_cc0_p (PATTERN (prev))
1340 && GET_CODE (PATTERN (insn)) == SET
1341 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1343 if ((next = try_combine (insn, prev, NULL, NULL,
1344 &new_direct_jump_p,
1345 last_combined_insn)) != 0)
1346 goto retry;
1348 FOR_EACH_LOG_LINK (nextlinks, prev)
1349 if ((next = try_combine (insn, prev, nextlinks->insn,
1350 NULL, &new_direct_jump_p,
1351 last_combined_insn)) != 0)
1352 goto retry;
1355 /* Finally, see if any of the insns that this insn links to
1356 explicitly references CC0. If so, try this insn, that insn,
1357 and its predecessor if it sets CC0. */
1358 FOR_EACH_LOG_LINK (links, insn)
1359 if (NONJUMP_INSN_P (links->insn)
1360 && GET_CODE (PATTERN (links->insn)) == SET
1361 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1362 && (prev = prev_nonnote_insn (links->insn)) != 0
1363 && NONJUMP_INSN_P (prev)
1364 && sets_cc0_p (PATTERN (prev))
1365 && (next = try_combine (insn, links->insn,
1366 prev, NULL, &new_direct_jump_p,
1367 last_combined_insn)) != 0)
1368 goto retry;
1369 #endif
1371 /* Try combining an insn with two different insns whose results it
1372 uses. */
1373 if (max_combine >= 3)
1374 FOR_EACH_LOG_LINK (links, insn)
1375 for (nextlinks = links->next; nextlinks;
1376 nextlinks = nextlinks->next)
1377 if ((next = try_combine (insn, links->insn,
1378 nextlinks->insn, NULL,
1379 &new_direct_jump_p,
1380 last_combined_insn)) != 0)
1383 statistics_counter_event (cfun, "three-insn combine", 1);
1384 goto retry;
1387 /* Try four-instruction combinations. */
1388 if (max_combine >= 4)
1389 FOR_EACH_LOG_LINK (links, insn)
1391 struct insn_link *next1;
1392 rtx_insn *link = links->insn;
1394 /* If the linked insn has been replaced by a note, then there
1395 is no point in pursuing this chain any further. */
1396 if (NOTE_P (link))
1397 continue;
1399 FOR_EACH_LOG_LINK (next1, link)
1401 rtx_insn *link1 = next1->insn;
1402 if (NOTE_P (link1))
1403 continue;
1404 /* I0 -> I1 -> I2 -> I3. */
1405 FOR_EACH_LOG_LINK (nextlinks, link1)
1406 if ((next = try_combine (insn, link, link1,
1407 nextlinks->insn,
1408 &new_direct_jump_p,
1409 last_combined_insn)) != 0)
1411 statistics_counter_event (cfun, "four-insn combine", 1);
1412 goto retry;
1414 /* I0, I1 -> I2, I2 -> I3. */
1415 for (nextlinks = next1->next; nextlinks;
1416 nextlinks = nextlinks->next)
1417 if ((next = try_combine (insn, link, link1,
1418 nextlinks->insn,
1419 &new_direct_jump_p,
1420 last_combined_insn)) != 0)
1422 statistics_counter_event (cfun, "four-insn combine", 1);
1423 goto retry;
1427 for (next1 = links->next; next1; next1 = next1->next)
1429 rtx_insn *link1 = next1->insn;
1430 if (NOTE_P (link1))
1431 continue;
1432 /* I0 -> I2; I1, I2 -> I3. */
1433 FOR_EACH_LOG_LINK (nextlinks, link)
1434 if ((next = try_combine (insn, link, link1,
1435 nextlinks->insn,
1436 &new_direct_jump_p,
1437 last_combined_insn)) != 0)
1439 statistics_counter_event (cfun, "four-insn combine", 1);
1440 goto retry;
1442 /* I0 -> I1; I1, I2 -> I3. */
1443 FOR_EACH_LOG_LINK (nextlinks, link1)
1444 if ((next = try_combine (insn, link, link1,
1445 nextlinks->insn,
1446 &new_direct_jump_p,
1447 last_combined_insn)) != 0)
1449 statistics_counter_event (cfun, "four-insn combine", 1);
1450 goto retry;
1455 /* Try this insn with each REG_EQUAL note it links back to. */
1456 FOR_EACH_LOG_LINK (links, insn)
1458 rtx set, note;
1459 rtx_insn *temp = links->insn;
1460 if ((set = single_set (temp)) != 0
1461 && (note = find_reg_equal_equiv_note (temp)) != 0
1462 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1463 /* Avoid using a register that may already been marked
1464 dead by an earlier instruction. */
1465 && ! unmentioned_reg_p (note, SET_SRC (set))
1466 && (GET_MODE (note) == VOIDmode
1467 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1468 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1470 /* Temporarily replace the set's source with the
1471 contents of the REG_EQUAL note. The insn will
1472 be deleted or recognized by try_combine. */
1473 rtx orig = SET_SRC (set);
1474 SET_SRC (set) = note;
1475 i2mod = temp;
1476 i2mod_old_rhs = copy_rtx (orig);
1477 i2mod_new_rhs = copy_rtx (note);
1478 next = try_combine (insn, i2mod, NULL, NULL,
1479 &new_direct_jump_p,
1480 last_combined_insn);
1481 i2mod = NULL;
1482 if (next)
1484 statistics_counter_event (cfun, "insn-with-note combine", 1);
1485 goto retry;
1487 SET_SRC (set) = orig;
1491 if (!NOTE_P (insn))
1492 record_dead_and_set_regs (insn);
1494 retry:
1499 default_rtl_profile ();
1500 clear_bb_flags ();
1501 new_direct_jump_p |= purge_all_dead_edges ();
1502 delete_noop_moves ();
1504 /* Clean up. */
1505 obstack_free (&insn_link_obstack, NULL);
1506 free (uid_log_links);
1507 free (uid_insn_cost);
1508 reg_stat.release ();
1511 struct undo *undo, *next;
1512 for (undo = undobuf.frees; undo; undo = next)
1514 next = undo->next;
1515 free (undo);
1517 undobuf.frees = 0;
1520 total_attempts += combine_attempts;
1521 total_merges += combine_merges;
1522 total_extras += combine_extras;
1523 total_successes += combine_successes;
1525 nonzero_sign_valid = 0;
1526 rtl_hooks = general_rtl_hooks;
1528 /* Make recognizer allow volatile MEMs again. */
1529 init_recog ();
1531 return new_direct_jump_p;
1534 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1536 static void
1537 init_reg_last (void)
1539 unsigned int i;
1540 reg_stat_type *p;
1542 FOR_EACH_VEC_ELT (reg_stat, i, p)
1543 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1546 /* Set up any promoted values for incoming argument registers. */
1548 static void
1549 setup_incoming_promotions (rtx_insn *first)
1551 tree arg;
1552 bool strictly_local = false;
1554 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1555 arg = DECL_CHAIN (arg))
1557 rtx x, reg = DECL_INCOMING_RTL (arg);
1558 int uns1, uns3;
1559 machine_mode mode1, mode2, mode3, mode4;
1561 /* Only continue if the incoming argument is in a register. */
1562 if (!REG_P (reg))
1563 continue;
1565 /* Determine, if possible, whether all call sites of the current
1566 function lie within the current compilation unit. (This does
1567 take into account the exporting of a function via taking its
1568 address, and so forth.) */
1569 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1571 /* The mode and signedness of the argument before any promotions happen
1572 (equal to the mode of the pseudo holding it at that stage). */
1573 mode1 = TYPE_MODE (TREE_TYPE (arg));
1574 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1576 /* The mode and signedness of the argument after any source language and
1577 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1578 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1579 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1581 /* The mode and signedness of the argument as it is actually passed,
1582 see assign_parm_setup_reg in function.c. */
1583 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns1,
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 /* Return whether INSN is a PARALLEL of exactly N register SETs followed
2465 by an arbitrary number of CLOBBERs. */
2466 static bool
2467 is_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2469 rtx pat = PATTERN (insn);
2471 if (GET_CODE (pat) != PARALLEL)
2472 return false;
2474 int len = XVECLEN (pat, 0);
2475 if (len < n)
2476 return false;
2478 int i;
2479 for (i = 0; i < n; i++)
2480 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2481 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2482 return false;
2483 for ( ; i < len; i++)
2484 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2485 return false;
2487 return true;
2490 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2491 CLOBBERs), can be split into individual SETs in that order, without
2492 changing semantics. */
2493 static bool
2494 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2496 if (!insn_nothrow_p (insn))
2497 return false;
2499 rtx pat = PATTERN (insn);
2501 int i, j;
2502 for (i = 0; i < n; i++)
2504 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2505 return false;
2507 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2509 for (j = i + 1; j < n; j++)
2510 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2511 return false;
2514 return true;
2517 /* Try to combine the insns I0, I1 and I2 into I3.
2518 Here I0, I1 and I2 appear earlier than I3.
2519 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2522 If we are combining more than two insns and the resulting insn is not
2523 recognized, try splitting it into two insns. If that happens, I2 and I3
2524 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2525 Otherwise, I0, I1 and I2 are pseudo-deleted.
2527 Return 0 if the combination does not work. Then nothing is changed.
2528 If we did the combination, return the insn at which combine should
2529 resume scanning.
2531 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2532 new direct jump instruction.
2534 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2535 been I3 passed to an earlier try_combine within the same basic
2536 block. */
2538 static rtx_insn *
2539 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2540 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2542 /* New patterns for I3 and I2, respectively. */
2543 rtx newpat, newi2pat = 0;
2544 rtvec newpat_vec_with_clobbers = 0;
2545 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2546 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2547 dead. */
2548 int added_sets_0, added_sets_1, added_sets_2;
2549 /* Total number of SETs to put into I3. */
2550 int total_sets;
2551 /* Nonzero if I2's or I1's body now appears in I3. */
2552 int i2_is_used = 0, i1_is_used = 0;
2553 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2554 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2555 /* Contains I3 if the destination of I3 is used in its source, which means
2556 that the old life of I3 is being killed. If that usage is placed into
2557 I2 and not in I3, a REG_DEAD note must be made. */
2558 rtx i3dest_killed = 0;
2559 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2560 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2561 /* Copy of SET_SRC of I1 and I0, if needed. */
2562 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2563 /* Set if I2DEST was reused as a scratch register. */
2564 bool i2scratch = false;
2565 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2566 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2567 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2568 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2569 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2570 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2571 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2572 /* Notes that must be added to REG_NOTES in I3 and I2. */
2573 rtx new_i3_notes, new_i2_notes;
2574 /* Notes that we substituted I3 into I2 instead of the normal case. */
2575 int i3_subst_into_i2 = 0;
2576 /* Notes that I1, I2 or I3 is a MULT operation. */
2577 int have_mult = 0;
2578 int swap_i2i3 = 0;
2579 int changed_i3_dest = 0;
2581 int maxreg;
2582 rtx_insn *temp_insn;
2583 rtx temp_expr;
2584 struct insn_link *link;
2585 rtx other_pat = 0;
2586 rtx new_other_notes;
2587 int i;
2589 /* Only try four-insn combinations when there's high likelihood of
2590 success. Look for simple insns, such as loads of constants or
2591 binary operations involving a constant. */
2592 if (i0)
2594 int i;
2595 int ngood = 0;
2596 int nshift = 0;
2598 if (!flag_expensive_optimizations)
2599 return 0;
2601 for (i = 0; i < 4; i++)
2603 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2604 rtx set = single_set (insn);
2605 rtx src;
2606 if (!set)
2607 continue;
2608 src = SET_SRC (set);
2609 if (CONSTANT_P (src))
2611 ngood += 2;
2612 break;
2614 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2615 ngood++;
2616 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2617 || GET_CODE (src) == LSHIFTRT)
2618 nshift++;
2620 if (ngood < 2 && nshift < 2)
2621 return 0;
2624 /* Exit early if one of the insns involved can't be used for
2625 combinations. */
2626 if (CALL_P (i2)
2627 || (i1 && CALL_P (i1))
2628 || (i0 && CALL_P (i0))
2629 || cant_combine_insn_p (i3)
2630 || cant_combine_insn_p (i2)
2631 || (i1 && cant_combine_insn_p (i1))
2632 || (i0 && cant_combine_insn_p (i0))
2633 || likely_spilled_retval_p (i3))
2634 return 0;
2636 combine_attempts++;
2637 undobuf.other_insn = 0;
2639 /* Reset the hard register usage information. */
2640 CLEAR_HARD_REG_SET (newpat_used_regs);
2642 if (dump_file && (dump_flags & TDF_DETAILS))
2644 if (i0)
2645 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2646 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2647 else if (i1)
2648 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2649 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2650 else
2651 fprintf (dump_file, "\nTrying %d -> %d:\n",
2652 INSN_UID (i2), INSN_UID (i3));
2655 /* If multiple insns feed into one of I2 or I3, they can be in any
2656 order. To simplify the code below, reorder them in sequence. */
2657 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2658 temp_insn = i2, i2 = i0, i0 = temp_insn;
2659 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2660 temp_insn = i1, i1 = i0, i0 = temp_insn;
2661 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2662 temp_insn = i1, i1 = i2, i2 = temp_insn;
2664 added_links_insn = 0;
2666 /* First check for one important special case that the code below will
2667 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2668 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2669 we may be able to replace that destination with the destination of I3.
2670 This occurs in the common code where we compute both a quotient and
2671 remainder into a structure, in which case we want to do the computation
2672 directly into the structure to avoid register-register copies.
2674 Note that this case handles both multiple sets in I2 and also cases
2675 where I2 has a number of CLOBBERs inside the PARALLEL.
2677 We make very conservative checks below and only try to handle the
2678 most common cases of this. For example, we only handle the case
2679 where I2 and I3 are adjacent to avoid making difficult register
2680 usage tests. */
2682 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2683 && REG_P (SET_SRC (PATTERN (i3)))
2684 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2685 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2686 && GET_CODE (PATTERN (i2)) == PARALLEL
2687 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2688 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2689 below would need to check what is inside (and reg_overlap_mentioned_p
2690 doesn't support those codes anyway). Don't allow those destinations;
2691 the resulting insn isn't likely to be recognized anyway. */
2692 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2693 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2694 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2695 SET_DEST (PATTERN (i3)))
2696 && next_active_insn (i2) == i3)
2698 rtx p2 = PATTERN (i2);
2700 /* Make sure that the destination of I3,
2701 which we are going to substitute into one output of I2,
2702 is not used within another output of I2. We must avoid making this:
2703 (parallel [(set (mem (reg 69)) ...)
2704 (set (reg 69) ...)])
2705 which is not well-defined as to order of actions.
2706 (Besides, reload can't handle output reloads for this.)
2708 The problem can also happen if the dest of I3 is a memory ref,
2709 if another dest in I2 is an indirect memory ref. */
2710 for (i = 0; i < XVECLEN (p2, 0); i++)
2711 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2712 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2713 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2714 SET_DEST (XVECEXP (p2, 0, i))))
2715 break;
2717 if (i == XVECLEN (p2, 0))
2718 for (i = 0; i < XVECLEN (p2, 0); i++)
2719 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2720 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2722 combine_merges++;
2724 subst_insn = i3;
2725 subst_low_luid = DF_INSN_LUID (i2);
2727 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2728 i2src = SET_SRC (XVECEXP (p2, 0, i));
2729 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2730 i2dest_killed = dead_or_set_p (i2, i2dest);
2732 /* Replace the dest in I2 with our dest and make the resulting
2733 insn the new pattern for I3. Then skip to where we validate
2734 the pattern. Everything was set up above. */
2735 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2736 newpat = p2;
2737 i3_subst_into_i2 = 1;
2738 goto validate_replacement;
2742 /* If I2 is setting a pseudo to a constant and I3 is setting some
2743 sub-part of it to another constant, merge them by making a new
2744 constant. */
2745 if (i1 == 0
2746 && (temp_expr = single_set (i2)) != 0
2747 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2748 && GET_CODE (PATTERN (i3)) == SET
2749 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2750 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2752 rtx dest = SET_DEST (PATTERN (i3));
2753 int offset = -1;
2754 int width = 0;
2756 if (GET_CODE (dest) == ZERO_EXTRACT)
2758 if (CONST_INT_P (XEXP (dest, 1))
2759 && CONST_INT_P (XEXP (dest, 2)))
2761 width = INTVAL (XEXP (dest, 1));
2762 offset = INTVAL (XEXP (dest, 2));
2763 dest = XEXP (dest, 0);
2764 if (BITS_BIG_ENDIAN)
2765 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2768 else
2770 if (GET_CODE (dest) == STRICT_LOW_PART)
2771 dest = XEXP (dest, 0);
2772 width = GET_MODE_PRECISION (GET_MODE (dest));
2773 offset = 0;
2776 if (offset >= 0)
2778 /* If this is the low part, we're done. */
2779 if (subreg_lowpart_p (dest))
2781 /* Handle the case where inner is twice the size of outer. */
2782 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2783 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2784 offset += GET_MODE_PRECISION (GET_MODE (dest));
2785 /* Otherwise give up for now. */
2786 else
2787 offset = -1;
2790 if (offset >= 0)
2792 rtx inner = SET_SRC (PATTERN (i3));
2793 rtx outer = SET_SRC (temp_expr);
2795 wide_int o
2796 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2797 std::make_pair (inner, GET_MODE (dest)),
2798 offset, width);
2800 combine_merges++;
2801 subst_insn = i3;
2802 subst_low_luid = DF_INSN_LUID (i2);
2803 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2804 i2dest = SET_DEST (temp_expr);
2805 i2dest_killed = dead_or_set_p (i2, i2dest);
2807 /* Replace the source in I2 with the new constant and make the
2808 resulting insn the new pattern for I3. Then skip to where we
2809 validate the pattern. Everything was set up above. */
2810 SUBST (SET_SRC (temp_expr),
2811 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2813 newpat = PATTERN (i2);
2815 /* The dest of I3 has been replaced with the dest of I2. */
2816 changed_i3_dest = 1;
2817 goto validate_replacement;
2821 #ifndef HAVE_cc0
2822 /* If we have no I1 and I2 looks like:
2823 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2824 (set Y OP)])
2825 make up a dummy I1 that is
2826 (set Y OP)
2827 and change I2 to be
2828 (set (reg:CC X) (compare:CC Y (const_int 0)))
2830 (We can ignore any trailing CLOBBERs.)
2832 This undoes a previous combination and allows us to match a branch-and-
2833 decrement insn. */
2835 if (i1 == 0
2836 && is_parallel_of_n_reg_sets (i2, 2)
2837 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2838 == MODE_CC)
2839 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2840 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2841 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2842 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2843 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2844 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2846 /* We make I1 with the same INSN_UID as I2. This gives it
2847 the same DF_INSN_LUID for value tracking. Our fake I1 will
2848 never appear in the insn stream so giving it the same INSN_UID
2849 as I2 will not cause a problem. */
2851 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2852 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2853 -1, NULL_RTX);
2854 INSN_UID (i1) = INSN_UID (i2);
2856 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2857 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2858 SET_DEST (PATTERN (i1)));
2859 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2860 SUBST_LINK (LOG_LINKS (i2),
2861 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2864 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2865 make those two SETs separate I1 and I2 insns, and make an I0 that is
2866 the original I1. */
2867 if (i0 == 0
2868 && is_parallel_of_n_reg_sets (i2, 2)
2869 && can_split_parallel_of_n_reg_sets (i2, 2)
2870 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2871 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2873 /* If there is no I1, there is no I0 either. */
2874 i0 = i1;
2876 /* We make I1 with the same INSN_UID as I2. This gives it
2877 the same DF_INSN_LUID for value tracking. Our fake I1 will
2878 never appear in the insn stream so giving it the same INSN_UID
2879 as I2 will not cause a problem. */
2881 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2882 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2883 -1, NULL_RTX);
2884 INSN_UID (i1) = INSN_UID (i2);
2886 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2888 #endif
2890 /* Verify that I2 and I1 are valid for combining. */
2891 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2892 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2893 &i1dest, &i1src))
2894 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2895 &i0dest, &i0src)))
2897 undo_all ();
2898 return 0;
2901 /* Record whether I2DEST is used in I2SRC and similarly for the other
2902 cases. Knowing this will help in register status updating below. */
2903 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2904 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2905 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2906 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2907 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2908 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2909 i2dest_killed = dead_or_set_p (i2, i2dest);
2910 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2911 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2913 /* For the earlier insns, determine which of the subsequent ones they
2914 feed. */
2915 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2916 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2917 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2918 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2919 && reg_overlap_mentioned_p (i0dest, i2src))));
2921 /* Ensure that I3's pattern can be the destination of combines. */
2922 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2923 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2924 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2925 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2926 &i3dest_killed))
2928 undo_all ();
2929 return 0;
2932 /* See if any of the insns is a MULT operation. Unless one is, we will
2933 reject a combination that is, since it must be slower. Be conservative
2934 here. */
2935 if (GET_CODE (i2src) == MULT
2936 || (i1 != 0 && GET_CODE (i1src) == MULT)
2937 || (i0 != 0 && GET_CODE (i0src) == MULT)
2938 || (GET_CODE (PATTERN (i3)) == SET
2939 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2940 have_mult = 1;
2942 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2943 We used to do this EXCEPT in one case: I3 has a post-inc in an
2944 output operand. However, that exception can give rise to insns like
2945 mov r3,(r3)+
2946 which is a famous insn on the PDP-11 where the value of r3 used as the
2947 source was model-dependent. Avoid this sort of thing. */
2949 #if 0
2950 if (!(GET_CODE (PATTERN (i3)) == SET
2951 && REG_P (SET_SRC (PATTERN (i3)))
2952 && MEM_P (SET_DEST (PATTERN (i3)))
2953 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2954 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2955 /* It's not the exception. */
2956 #endif
2957 #ifdef AUTO_INC_DEC
2959 rtx link;
2960 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2961 if (REG_NOTE_KIND (link) == REG_INC
2962 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2963 || (i1 != 0
2964 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2966 undo_all ();
2967 return 0;
2970 #endif
2972 /* See if the SETs in I1 or I2 need to be kept around in the merged
2973 instruction: whenever the value set there is still needed past I3.
2974 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2976 For the SET in I1, we have two cases: if I1 and I2 independently feed
2977 into I3, the set in I1 needs to be kept around unless I1DEST dies
2978 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2979 in I1 needs to be kept around unless I1DEST dies or is set in either
2980 I2 or I3. The same considerations apply to I0. */
2982 added_sets_2 = !dead_or_set_p (i3, i2dest);
2984 if (i1)
2985 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2986 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2987 else
2988 added_sets_1 = 0;
2990 if (i0)
2991 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2992 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
2993 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
2994 && dead_or_set_p (i2, i0dest)));
2995 else
2996 added_sets_0 = 0;
2998 /* We are about to copy insns for the case where they need to be kept
2999 around. Check that they can be copied in the merged instruction. */
3001 if (targetm.cannot_copy_insn_p
3002 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3003 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3004 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3006 undo_all ();
3007 return 0;
3010 /* If the set in I2 needs to be kept around, we must make a copy of
3011 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3012 PATTERN (I2), we are only substituting for the original I1DEST, not into
3013 an already-substituted copy. This also prevents making self-referential
3014 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3015 I2DEST. */
3017 if (added_sets_2)
3019 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3020 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3021 else
3022 i2pat = copy_rtx (PATTERN (i2));
3025 if (added_sets_1)
3027 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3028 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3029 else
3030 i1pat = copy_rtx (PATTERN (i1));
3033 if (added_sets_0)
3035 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3036 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3037 else
3038 i0pat = copy_rtx (PATTERN (i0));
3041 combine_merges++;
3043 /* Substitute in the latest insn for the regs set by the earlier ones. */
3045 maxreg = max_reg_num ();
3047 subst_insn = i3;
3049 #ifndef HAVE_cc0
3050 /* Many machines that don't use CC0 have insns that can both perform an
3051 arithmetic operation and set the condition code. These operations will
3052 be represented as a PARALLEL with the first element of the vector
3053 being a COMPARE of an arithmetic operation with the constant zero.
3054 The second element of the vector will set some pseudo to the result
3055 of the same arithmetic operation. If we simplify the COMPARE, we won't
3056 match such a pattern and so will generate an extra insn. Here we test
3057 for this case, where both the comparison and the operation result are
3058 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3059 I2SRC. Later we will make the PARALLEL that contains I2. */
3061 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3062 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3063 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3064 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3066 rtx newpat_dest;
3067 rtx *cc_use_loc = NULL;
3068 rtx_insn *cc_use_insn = NULL;
3069 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3070 machine_mode compare_mode, orig_compare_mode;
3071 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3073 newpat = PATTERN (i3);
3074 newpat_dest = SET_DEST (newpat);
3075 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3077 if (undobuf.other_insn == 0
3078 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3079 &cc_use_insn)))
3081 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3082 compare_code = simplify_compare_const (compare_code,
3083 GET_MODE (i2dest), op0, &op1);
3084 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3087 /* Do the rest only if op1 is const0_rtx, which may be the
3088 result of simplification. */
3089 if (op1 == const0_rtx)
3091 /* If a single use of the CC is found, prepare to modify it
3092 when SELECT_CC_MODE returns a new CC-class mode, or when
3093 the above simplify_compare_const() returned a new comparison
3094 operator. undobuf.other_insn is assigned the CC use insn
3095 when modifying it. */
3096 if (cc_use_loc)
3098 #ifdef SELECT_CC_MODE
3099 machine_mode new_mode
3100 = SELECT_CC_MODE (compare_code, op0, op1);
3101 if (new_mode != orig_compare_mode
3102 && can_change_dest_mode (SET_DEST (newpat),
3103 added_sets_2, new_mode))
3105 unsigned int regno = REGNO (newpat_dest);
3106 compare_mode = new_mode;
3107 if (regno < FIRST_PSEUDO_REGISTER)
3108 newpat_dest = gen_rtx_REG (compare_mode, regno);
3109 else
3111 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3112 newpat_dest = regno_reg_rtx[regno];
3115 #endif
3116 /* Cases for modifying the CC-using comparison. */
3117 if (compare_code != orig_compare_code
3118 /* ??? Do we need to verify the zero rtx? */
3119 && XEXP (*cc_use_loc, 1) == const0_rtx)
3121 /* Replace cc_use_loc with entire new RTX. */
3122 SUBST (*cc_use_loc,
3123 gen_rtx_fmt_ee (compare_code, compare_mode,
3124 newpat_dest, const0_rtx));
3125 undobuf.other_insn = cc_use_insn;
3127 else if (compare_mode != orig_compare_mode)
3129 /* Just replace the CC reg with a new mode. */
3130 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3131 undobuf.other_insn = cc_use_insn;
3135 /* Now we modify the current newpat:
3136 First, SET_DEST(newpat) is updated if the CC mode has been
3137 altered. For targets without SELECT_CC_MODE, this should be
3138 optimized away. */
3139 if (compare_mode != orig_compare_mode)
3140 SUBST (SET_DEST (newpat), newpat_dest);
3141 /* This is always done to propagate i2src into newpat. */
3142 SUBST (SET_SRC (newpat),
3143 gen_rtx_COMPARE (compare_mode, op0, op1));
3144 /* Create new version of i2pat if needed; the below PARALLEL
3145 creation needs this to work correctly. */
3146 if (! rtx_equal_p (i2src, op0))
3147 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3148 i2_is_used = 1;
3151 #endif
3153 if (i2_is_used == 0)
3155 /* It is possible that the source of I2 or I1 may be performing
3156 an unneeded operation, such as a ZERO_EXTEND of something
3157 that is known to have the high part zero. Handle that case
3158 by letting subst look at the inner insns.
3160 Another way to do this would be to have a function that tries
3161 to simplify a single insn instead of merging two or more
3162 insns. We don't do this because of the potential of infinite
3163 loops and because of the potential extra memory required.
3164 However, doing it the way we are is a bit of a kludge and
3165 doesn't catch all cases.
3167 But only do this if -fexpensive-optimizations since it slows
3168 things down and doesn't usually win.
3170 This is not done in the COMPARE case above because the
3171 unmodified I2PAT is used in the PARALLEL and so a pattern
3172 with a modified I2SRC would not match. */
3174 if (flag_expensive_optimizations)
3176 /* Pass pc_rtx so no substitutions are done, just
3177 simplifications. */
3178 if (i1)
3180 subst_low_luid = DF_INSN_LUID (i1);
3181 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3184 subst_low_luid = DF_INSN_LUID (i2);
3185 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3188 n_occurrences = 0; /* `subst' counts here */
3189 subst_low_luid = DF_INSN_LUID (i2);
3191 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3192 copy of I2SRC each time we substitute it, in order to avoid creating
3193 self-referential RTL when we will be substituting I1SRC for I1DEST
3194 later. Likewise if I0 feeds into I2, either directly or indirectly
3195 through I1, and I0DEST is in I0SRC. */
3196 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3197 (i1_feeds_i2_n && i1dest_in_i1src)
3198 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3199 && i0dest_in_i0src));
3200 substed_i2 = 1;
3202 /* Record whether I2's body now appears within I3's body. */
3203 i2_is_used = n_occurrences;
3206 /* If we already got a failure, don't try to do more. Otherwise, try to
3207 substitute I1 if we have it. */
3209 if (i1 && GET_CODE (newpat) != CLOBBER)
3211 /* Check that an autoincrement side-effect on I1 has not been lost.
3212 This happens if I1DEST is mentioned in I2 and dies there, and
3213 has disappeared from the new pattern. */
3214 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3215 && i1_feeds_i2_n
3216 && dead_or_set_p (i2, i1dest)
3217 && !reg_overlap_mentioned_p (i1dest, newpat))
3218 /* Before we can do this substitution, we must redo the test done
3219 above (see detailed comments there) that ensures I1DEST isn't
3220 mentioned in any SETs in NEWPAT that are field assignments. */
3221 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3222 0, 0, 0))
3224 undo_all ();
3225 return 0;
3228 n_occurrences = 0;
3229 subst_low_luid = DF_INSN_LUID (i1);
3231 /* If the following substitution will modify I1SRC, make a copy of it
3232 for the case where it is substituted for I1DEST in I2PAT later. */
3233 if (added_sets_2 && i1_feeds_i2_n)
3234 i1src_copy = copy_rtx (i1src);
3236 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3237 copy of I1SRC each time we substitute it, in order to avoid creating
3238 self-referential RTL when we will be substituting I0SRC for I0DEST
3239 later. */
3240 newpat = subst (newpat, i1dest, i1src, 0, 0,
3241 i0_feeds_i1_n && i0dest_in_i0src);
3242 substed_i1 = 1;
3244 /* Record whether I1's body now appears within I3's body. */
3245 i1_is_used = n_occurrences;
3248 /* Likewise for I0 if we have it. */
3250 if (i0 && GET_CODE (newpat) != CLOBBER)
3252 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3253 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3254 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3255 && !reg_overlap_mentioned_p (i0dest, newpat))
3256 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3257 0, 0, 0))
3259 undo_all ();
3260 return 0;
3263 /* If the following substitution will modify I0SRC, make a copy of it
3264 for the case where it is substituted for I0DEST in I1PAT later. */
3265 if (added_sets_1 && i0_feeds_i1_n)
3266 i0src_copy = copy_rtx (i0src);
3267 /* And a copy for I0DEST in I2PAT substitution. */
3268 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3269 || (i0_feeds_i2_n)))
3270 i0src_copy2 = copy_rtx (i0src);
3272 n_occurrences = 0;
3273 subst_low_luid = DF_INSN_LUID (i0);
3274 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3275 substed_i0 = 1;
3278 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3279 to count all the ways that I2SRC and I1SRC can be used. */
3280 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3281 && i2_is_used + added_sets_2 > 1)
3282 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3283 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3284 > 1))
3285 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3286 && (n_occurrences + added_sets_0
3287 + (added_sets_1 && i0_feeds_i1_n)
3288 + (added_sets_2 && i0_feeds_i2_n)
3289 > 1))
3290 /* Fail if we tried to make a new register. */
3291 || max_reg_num () != maxreg
3292 /* Fail if we couldn't do something and have a CLOBBER. */
3293 || GET_CODE (newpat) == CLOBBER
3294 /* Fail if this new pattern is a MULT and we didn't have one before
3295 at the outer level. */
3296 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3297 && ! have_mult))
3299 undo_all ();
3300 return 0;
3303 /* If the actions of the earlier insns must be kept
3304 in addition to substituting them into the latest one,
3305 we must make a new PARALLEL for the latest insn
3306 to hold additional the SETs. */
3308 if (added_sets_0 || added_sets_1 || added_sets_2)
3310 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3311 combine_extras++;
3313 if (GET_CODE (newpat) == PARALLEL)
3315 rtvec old = XVEC (newpat, 0);
3316 total_sets = XVECLEN (newpat, 0) + extra_sets;
3317 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3318 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3319 sizeof (old->elem[0]) * old->num_elem);
3321 else
3323 rtx old = newpat;
3324 total_sets = 1 + extra_sets;
3325 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3326 XVECEXP (newpat, 0, 0) = old;
3329 if (added_sets_0)
3330 XVECEXP (newpat, 0, --total_sets) = i0pat;
3332 if (added_sets_1)
3334 rtx t = i1pat;
3335 if (i0_feeds_i1_n)
3336 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3338 XVECEXP (newpat, 0, --total_sets) = t;
3340 if (added_sets_2)
3342 rtx t = i2pat;
3343 if (i1_feeds_i2_n)
3344 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3345 i0_feeds_i1_n && i0dest_in_i0src);
3346 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3347 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3349 XVECEXP (newpat, 0, --total_sets) = t;
3353 validate_replacement:
3355 /* Note which hard regs this insn has as inputs. */
3356 mark_used_regs_combine (newpat);
3358 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3359 consider splitting this pattern, we might need these clobbers. */
3360 if (i1 && GET_CODE (newpat) == PARALLEL
3361 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3363 int len = XVECLEN (newpat, 0);
3365 newpat_vec_with_clobbers = rtvec_alloc (len);
3366 for (i = 0; i < len; i++)
3367 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3370 /* We have recognized nothing yet. */
3371 insn_code_number = -1;
3373 /* See if this is a PARALLEL of two SETs where one SET's destination is
3374 a register that is unused and this isn't marked as an instruction that
3375 might trap in an EH region. In that case, we just need the other SET.
3376 We prefer this over the PARALLEL.
3378 This can occur when simplifying a divmod insn. We *must* test for this
3379 case here because the code below that splits two independent SETs doesn't
3380 handle this case correctly when it updates the register status.
3382 It's pointless doing this if we originally had two sets, one from
3383 i3, and one from i2. Combining then splitting the parallel results
3384 in the original i2 again plus an invalid insn (which we delete).
3385 The net effect is only to move instructions around, which makes
3386 debug info less accurate. */
3388 if (!(added_sets_2 && i1 == 0)
3389 && GET_CODE (newpat) == PARALLEL
3390 && XVECLEN (newpat, 0) == 2
3391 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3392 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3393 && asm_noperands (newpat) < 0)
3395 rtx set0 = XVECEXP (newpat, 0, 0);
3396 rtx set1 = XVECEXP (newpat, 0, 1);
3397 rtx oldpat = newpat;
3399 if (((REG_P (SET_DEST (set1))
3400 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3401 || (GET_CODE (SET_DEST (set1)) == SUBREG
3402 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3403 && insn_nothrow_p (i3)
3404 && !side_effects_p (SET_SRC (set1)))
3406 newpat = set0;
3407 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3410 else if (((REG_P (SET_DEST (set0))
3411 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3412 || (GET_CODE (SET_DEST (set0)) == SUBREG
3413 && find_reg_note (i3, REG_UNUSED,
3414 SUBREG_REG (SET_DEST (set0)))))
3415 && insn_nothrow_p (i3)
3416 && !side_effects_p (SET_SRC (set0)))
3418 newpat = set1;
3419 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3421 if (insn_code_number >= 0)
3422 changed_i3_dest = 1;
3425 if (insn_code_number < 0)
3426 newpat = oldpat;
3429 /* Is the result of combination a valid instruction? */
3430 if (insn_code_number < 0)
3431 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3433 /* If we were combining three insns and the result is a simple SET
3434 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3435 insns. There are two ways to do this. It can be split using a
3436 machine-specific method (like when you have an addition of a large
3437 constant) or by combine in the function find_split_point. */
3439 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3440 && asm_noperands (newpat) < 0)
3442 rtx parallel, *split;
3443 rtx_insn *m_split_insn;
3445 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3446 use I2DEST as a scratch register will help. In the latter case,
3447 convert I2DEST to the mode of the source of NEWPAT if we can. */
3449 m_split_insn = combine_split_insns (newpat, i3);
3451 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3452 inputs of NEWPAT. */
3454 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3455 possible to try that as a scratch reg. This would require adding
3456 more code to make it work though. */
3458 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3460 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3462 /* First try to split using the original register as a
3463 scratch register. */
3464 parallel = gen_rtx_PARALLEL (VOIDmode,
3465 gen_rtvec (2, newpat,
3466 gen_rtx_CLOBBER (VOIDmode,
3467 i2dest)));
3468 m_split_insn = combine_split_insns (parallel, i3);
3470 /* If that didn't work, try changing the mode of I2DEST if
3471 we can. */
3472 if (m_split_insn == 0
3473 && new_mode != GET_MODE (i2dest)
3474 && new_mode != VOIDmode
3475 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3477 machine_mode old_mode = GET_MODE (i2dest);
3478 rtx ni2dest;
3480 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3481 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3482 else
3484 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3485 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3488 parallel = (gen_rtx_PARALLEL
3489 (VOIDmode,
3490 gen_rtvec (2, newpat,
3491 gen_rtx_CLOBBER (VOIDmode,
3492 ni2dest))));
3493 m_split_insn = combine_split_insns (parallel, i3);
3495 if (m_split_insn == 0
3496 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3498 struct undo *buf;
3500 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3501 buf = undobuf.undos;
3502 undobuf.undos = buf->next;
3503 buf->next = undobuf.frees;
3504 undobuf.frees = buf;
3508 i2scratch = m_split_insn != 0;
3511 /* If recog_for_combine has discarded clobbers, try to use them
3512 again for the split. */
3513 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3515 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3516 m_split_insn = combine_split_insns (parallel, i3);
3519 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3521 rtx m_split_pat = PATTERN (m_split_insn);
3522 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3523 if (insn_code_number >= 0)
3524 newpat = m_split_pat;
3526 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3527 && (next_nonnote_nondebug_insn (i2) == i3
3528 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3530 rtx i2set, i3set;
3531 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3532 newi2pat = PATTERN (m_split_insn);
3534 i3set = single_set (NEXT_INSN (m_split_insn));
3535 i2set = single_set (m_split_insn);
3537 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3539 /* If I2 or I3 has multiple SETs, we won't know how to track
3540 register status, so don't use these insns. If I2's destination
3541 is used between I2 and I3, we also can't use these insns. */
3543 if (i2_code_number >= 0 && i2set && i3set
3544 && (next_nonnote_nondebug_insn (i2) == i3
3545 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3546 insn_code_number = recog_for_combine (&newi3pat, i3,
3547 &new_i3_notes);
3548 if (insn_code_number >= 0)
3549 newpat = newi3pat;
3551 /* It is possible that both insns now set the destination of I3.
3552 If so, we must show an extra use of it. */
3554 if (insn_code_number >= 0)
3556 rtx new_i3_dest = SET_DEST (i3set);
3557 rtx new_i2_dest = SET_DEST (i2set);
3559 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3560 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3561 || GET_CODE (new_i3_dest) == SUBREG)
3562 new_i3_dest = XEXP (new_i3_dest, 0);
3564 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3565 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3566 || GET_CODE (new_i2_dest) == SUBREG)
3567 new_i2_dest = XEXP (new_i2_dest, 0);
3569 if (REG_P (new_i3_dest)
3570 && REG_P (new_i2_dest)
3571 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3572 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3576 /* If we can split it and use I2DEST, go ahead and see if that
3577 helps things be recognized. Verify that none of the registers
3578 are set between I2 and I3. */
3579 if (insn_code_number < 0
3580 && (split = find_split_point (&newpat, i3, false)) != 0
3581 #ifdef HAVE_cc0
3582 && REG_P (i2dest)
3583 #endif
3584 /* We need I2DEST in the proper mode. If it is a hard register
3585 or the only use of a pseudo, we can change its mode.
3586 Make sure we don't change a hard register to have a mode that
3587 isn't valid for it, or change the number of registers. */
3588 && (GET_MODE (*split) == GET_MODE (i2dest)
3589 || GET_MODE (*split) == VOIDmode
3590 || can_change_dest_mode (i2dest, added_sets_2,
3591 GET_MODE (*split)))
3592 && (next_nonnote_nondebug_insn (i2) == i3
3593 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3594 /* We can't overwrite I2DEST if its value is still used by
3595 NEWPAT. */
3596 && ! reg_referenced_p (i2dest, newpat))
3598 rtx newdest = i2dest;
3599 enum rtx_code split_code = GET_CODE (*split);
3600 machine_mode split_mode = GET_MODE (*split);
3601 bool subst_done = false;
3602 newi2pat = NULL_RTX;
3604 i2scratch = true;
3606 /* *SPLIT may be part of I2SRC, so make sure we have the
3607 original expression around for later debug processing.
3608 We should not need I2SRC any more in other cases. */
3609 if (MAY_HAVE_DEBUG_INSNS)
3610 i2src = copy_rtx (i2src);
3611 else
3612 i2src = NULL;
3614 /* Get NEWDEST as a register in the proper mode. We have already
3615 validated that we can do this. */
3616 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3618 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3619 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3620 else
3622 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3623 newdest = regno_reg_rtx[REGNO (i2dest)];
3627 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3628 an ASHIFT. This can occur if it was inside a PLUS and hence
3629 appeared to be a memory address. This is a kludge. */
3630 if (split_code == MULT
3631 && CONST_INT_P (XEXP (*split, 1))
3632 && INTVAL (XEXP (*split, 1)) > 0
3633 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3635 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3636 XEXP (*split, 0), GEN_INT (i)));
3637 /* Update split_code because we may not have a multiply
3638 anymore. */
3639 split_code = GET_CODE (*split);
3642 #ifdef INSN_SCHEDULING
3643 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3644 be written as a ZERO_EXTEND. */
3645 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3647 #ifdef LOAD_EXTEND_OP
3648 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3649 what it really is. */
3650 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3651 == SIGN_EXTEND)
3652 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3653 SUBREG_REG (*split)));
3654 else
3655 #endif
3656 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3657 SUBREG_REG (*split)));
3659 #endif
3661 /* Attempt to split binary operators using arithmetic identities. */
3662 if (BINARY_P (SET_SRC (newpat))
3663 && split_mode == GET_MODE (SET_SRC (newpat))
3664 && ! side_effects_p (SET_SRC (newpat)))
3666 rtx setsrc = SET_SRC (newpat);
3667 machine_mode mode = GET_MODE (setsrc);
3668 enum rtx_code code = GET_CODE (setsrc);
3669 rtx src_op0 = XEXP (setsrc, 0);
3670 rtx src_op1 = XEXP (setsrc, 1);
3672 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3673 if (rtx_equal_p (src_op0, src_op1))
3675 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3676 SUBST (XEXP (setsrc, 0), newdest);
3677 SUBST (XEXP (setsrc, 1), newdest);
3678 subst_done = true;
3680 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3681 else if ((code == PLUS || code == MULT)
3682 && GET_CODE (src_op0) == code
3683 && GET_CODE (XEXP (src_op0, 0)) == code
3684 && (INTEGRAL_MODE_P (mode)
3685 || (FLOAT_MODE_P (mode)
3686 && flag_unsafe_math_optimizations)))
3688 rtx p = XEXP (XEXP (src_op0, 0), 0);
3689 rtx q = XEXP (XEXP (src_op0, 0), 1);
3690 rtx r = XEXP (src_op0, 1);
3691 rtx s = src_op1;
3693 /* Split both "((X op Y) op X) op Y" and
3694 "((X op Y) op Y) op X" as "T op T" where T is
3695 "X op Y". */
3696 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3697 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3699 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3700 XEXP (src_op0, 0));
3701 SUBST (XEXP (setsrc, 0), newdest);
3702 SUBST (XEXP (setsrc, 1), newdest);
3703 subst_done = true;
3705 /* Split "((X op X) op Y) op Y)" as "T op T" where
3706 T is "X op Y". */
3707 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3709 rtx tmp = simplify_gen_binary (code, mode, p, r);
3710 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3711 SUBST (XEXP (setsrc, 0), newdest);
3712 SUBST (XEXP (setsrc, 1), newdest);
3713 subst_done = true;
3718 if (!subst_done)
3720 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3721 SUBST (*split, newdest);
3724 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3726 /* recog_for_combine might have added CLOBBERs to newi2pat.
3727 Make sure NEWPAT does not depend on the clobbered regs. */
3728 if (GET_CODE (newi2pat) == PARALLEL)
3729 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3730 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3732 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3733 if (reg_overlap_mentioned_p (reg, newpat))
3735 undo_all ();
3736 return 0;
3740 /* If the split point was a MULT and we didn't have one before,
3741 don't use one now. */
3742 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3743 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3747 /* Check for a case where we loaded from memory in a narrow mode and
3748 then sign extended it, but we need both registers. In that case,
3749 we have a PARALLEL with both loads from the same memory location.
3750 We can split this into a load from memory followed by a register-register
3751 copy. This saves at least one insn, more if register allocation can
3752 eliminate the copy.
3754 We cannot do this if the destination of the first assignment is a
3755 condition code register or cc0. We eliminate this case by making sure
3756 the SET_DEST and SET_SRC have the same mode.
3758 We cannot do this if the destination of the second assignment is
3759 a register that we have already assumed is zero-extended. Similarly
3760 for a SUBREG of such a register. */
3762 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3763 && GET_CODE (newpat) == PARALLEL
3764 && XVECLEN (newpat, 0) == 2
3765 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3766 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3767 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3768 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3769 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3770 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3771 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3772 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3773 DF_INSN_LUID (i2))
3774 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3775 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3776 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3777 (REG_P (temp_expr)
3778 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3779 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3780 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3781 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3782 != GET_MODE_MASK (word_mode))))
3783 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3784 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3785 (REG_P (temp_expr)
3786 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3787 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3788 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3789 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3790 != GET_MODE_MASK (word_mode)))))
3791 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3792 SET_SRC (XVECEXP (newpat, 0, 1)))
3793 && ! find_reg_note (i3, REG_UNUSED,
3794 SET_DEST (XVECEXP (newpat, 0, 0))))
3796 rtx ni2dest;
3798 newi2pat = XVECEXP (newpat, 0, 0);
3799 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3800 newpat = XVECEXP (newpat, 0, 1);
3801 SUBST (SET_SRC (newpat),
3802 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3803 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3805 if (i2_code_number >= 0)
3806 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3808 if (insn_code_number >= 0)
3809 swap_i2i3 = 1;
3812 /* Similarly, check for a case where we have a PARALLEL of two independent
3813 SETs but we started with three insns. In this case, we can do the sets
3814 as two separate insns. This case occurs when some SET allows two
3815 other insns to combine, but the destination of that SET is still live. */
3817 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3818 && GET_CODE (newpat) == PARALLEL
3819 && XVECLEN (newpat, 0) == 2
3820 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3821 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3822 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3823 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3824 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3825 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3826 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3827 XVECEXP (newpat, 0, 0))
3828 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3829 XVECEXP (newpat, 0, 1))
3830 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3831 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3833 rtx set0 = XVECEXP (newpat, 0, 0);
3834 rtx set1 = XVECEXP (newpat, 0, 1);
3836 /* Normally, it doesn't matter which of the two is done first,
3837 but the one that references cc0 can't be the second, and
3838 one which uses any regs/memory set in between i2 and i3 can't
3839 be first. The PARALLEL might also have been pre-existing in i3,
3840 so we need to make sure that we won't wrongly hoist a SET to i2
3841 that would conflict with a death note present in there. */
3842 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3843 && !(REG_P (SET_DEST (set1))
3844 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3845 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3846 && find_reg_note (i2, REG_DEAD,
3847 SUBREG_REG (SET_DEST (set1))))
3848 #ifdef HAVE_cc0
3849 && !reg_referenced_p (cc0_rtx, set0)
3850 #endif
3851 /* If I3 is a jump, ensure that set0 is a jump so that
3852 we do not create invalid RTL. */
3853 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3856 newi2pat = set1;
3857 newpat = set0;
3859 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3860 && !(REG_P (SET_DEST (set0))
3861 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3862 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3863 && find_reg_note (i2, REG_DEAD,
3864 SUBREG_REG (SET_DEST (set0))))
3865 #ifdef HAVE_cc0
3866 && !reg_referenced_p (cc0_rtx, set1)
3867 #endif
3868 /* If I3 is a jump, ensure that set1 is a jump so that
3869 we do not create invalid RTL. */
3870 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3873 newi2pat = set0;
3874 newpat = set1;
3876 else
3878 undo_all ();
3879 return 0;
3882 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3884 if (i2_code_number >= 0)
3886 /* recog_for_combine might have added CLOBBERs to newi2pat.
3887 Make sure NEWPAT does not depend on the clobbered regs. */
3888 if (GET_CODE (newi2pat) == PARALLEL)
3890 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3891 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3893 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3894 if (reg_overlap_mentioned_p (reg, newpat))
3896 undo_all ();
3897 return 0;
3902 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3906 /* If it still isn't recognized, fail and change things back the way they
3907 were. */
3908 if ((insn_code_number < 0
3909 /* Is the result a reasonable ASM_OPERANDS? */
3910 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3912 undo_all ();
3913 return 0;
3916 /* If we had to change another insn, make sure it is valid also. */
3917 if (undobuf.other_insn)
3919 CLEAR_HARD_REG_SET (newpat_used_regs);
3921 other_pat = PATTERN (undobuf.other_insn);
3922 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3923 &new_other_notes);
3925 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3927 undo_all ();
3928 return 0;
3932 #ifdef HAVE_cc0
3933 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3934 they are adjacent to each other or not. */
3936 rtx_insn *p = prev_nonnote_insn (i3);
3937 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3938 && sets_cc0_p (newi2pat))
3940 undo_all ();
3941 return 0;
3944 #endif
3946 /* Only allow this combination if insn_rtx_costs reports that the
3947 replacement instructions are cheaper than the originals. */
3948 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3950 undo_all ();
3951 return 0;
3954 if (MAY_HAVE_DEBUG_INSNS)
3956 struct undo *undo;
3958 for (undo = undobuf.undos; undo; undo = undo->next)
3959 if (undo->kind == UNDO_MODE)
3961 rtx reg = *undo->where.r;
3962 machine_mode new_mode = GET_MODE (reg);
3963 machine_mode old_mode = undo->old_contents.m;
3965 /* Temporarily revert mode back. */
3966 adjust_reg_mode (reg, old_mode);
3968 if (reg == i2dest && i2scratch)
3970 /* If we used i2dest as a scratch register with a
3971 different mode, substitute it for the original
3972 i2src while its original mode is temporarily
3973 restored, and then clear i2scratch so that we don't
3974 do it again later. */
3975 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3976 this_basic_block);
3977 i2scratch = false;
3978 /* Put back the new mode. */
3979 adjust_reg_mode (reg, new_mode);
3981 else
3983 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3984 rtx_insn *first, *last;
3986 if (reg == i2dest)
3988 first = i2;
3989 last = last_combined_insn;
3991 else
3993 first = i3;
3994 last = undobuf.other_insn;
3995 gcc_assert (last);
3996 if (DF_INSN_LUID (last)
3997 < DF_INSN_LUID (last_combined_insn))
3998 last = last_combined_insn;
4001 /* We're dealing with a reg that changed mode but not
4002 meaning, so we want to turn it into a subreg for
4003 the new mode. However, because of REG sharing and
4004 because its mode had already changed, we have to do
4005 it in two steps. First, replace any debug uses of
4006 reg, with its original mode temporarily restored,
4007 with this copy we have created; then, replace the
4008 copy with the SUBREG of the original shared reg,
4009 once again changed to the new mode. */
4010 propagate_for_debug (first, last, reg, tempreg,
4011 this_basic_block);
4012 adjust_reg_mode (reg, new_mode);
4013 propagate_for_debug (first, last, tempreg,
4014 lowpart_subreg (old_mode, reg, new_mode),
4015 this_basic_block);
4020 /* If we will be able to accept this, we have made a
4021 change to the destination of I3. This requires us to
4022 do a few adjustments. */
4024 if (changed_i3_dest)
4026 PATTERN (i3) = newpat;
4027 adjust_for_new_dest (i3);
4030 /* We now know that we can do this combination. Merge the insns and
4031 update the status of registers and LOG_LINKS. */
4033 if (undobuf.other_insn)
4035 rtx note, next;
4037 PATTERN (undobuf.other_insn) = other_pat;
4039 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4040 ensure that they are still valid. Then add any non-duplicate
4041 notes added by recog_for_combine. */
4042 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4044 next = XEXP (note, 1);
4046 if ((REG_NOTE_KIND (note) == REG_DEAD
4047 && !reg_referenced_p (XEXP (note, 0),
4048 PATTERN (undobuf.other_insn)))
4049 ||(REG_NOTE_KIND (note) == REG_UNUSED
4050 && !reg_set_p (XEXP (note, 0),
4051 PATTERN (undobuf.other_insn))))
4052 remove_note (undobuf.other_insn, note);
4055 distribute_notes (new_other_notes, undobuf.other_insn,
4056 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4057 NULL_RTX);
4060 if (swap_i2i3)
4062 rtx_insn *insn;
4063 struct insn_link *link;
4064 rtx ni2dest;
4066 /* I3 now uses what used to be its destination and which is now
4067 I2's destination. This requires us to do a few adjustments. */
4068 PATTERN (i3) = newpat;
4069 adjust_for_new_dest (i3);
4071 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4072 so we still will.
4074 However, some later insn might be using I2's dest and have
4075 a LOG_LINK pointing at I3. We must remove this link.
4076 The simplest way to remove the link is to point it at I1,
4077 which we know will be a NOTE. */
4079 /* newi2pat is usually a SET here; however, recog_for_combine might
4080 have added some clobbers. */
4081 if (GET_CODE (newi2pat) == PARALLEL)
4082 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4083 else
4084 ni2dest = SET_DEST (newi2pat);
4086 for (insn = NEXT_INSN (i3);
4087 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4088 || insn != BB_HEAD (this_basic_block->next_bb));
4089 insn = NEXT_INSN (insn))
4091 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4093 FOR_EACH_LOG_LINK (link, insn)
4094 if (link->insn == i3)
4095 link->insn = i1;
4097 break;
4103 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4104 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4105 rtx midnotes = 0;
4106 int from_luid;
4107 /* Compute which registers we expect to eliminate. newi2pat may be setting
4108 either i3dest or i2dest, so we must check it. Also, i1dest may be the
4109 same as i3dest, in which case newi2pat may be setting i1dest. */
4110 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4111 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4112 || !i2dest_killed
4113 ? 0 : i2dest);
4114 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4115 || (newi2pat && reg_set_p (i1dest, newi2pat))
4116 || !i1dest_killed
4117 ? 0 : i1dest);
4118 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4119 || (newi2pat && reg_set_p (i0dest, newi2pat))
4120 || !i0dest_killed
4121 ? 0 : i0dest);
4123 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4124 clear them. */
4125 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4126 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4127 if (i1)
4128 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4129 if (i0)
4130 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4132 /* Ensure that we do not have something that should not be shared but
4133 occurs multiple times in the new insns. Check this by first
4134 resetting all the `used' flags and then copying anything is shared. */
4136 reset_used_flags (i3notes);
4137 reset_used_flags (i2notes);
4138 reset_used_flags (i1notes);
4139 reset_used_flags (i0notes);
4140 reset_used_flags (newpat);
4141 reset_used_flags (newi2pat);
4142 if (undobuf.other_insn)
4143 reset_used_flags (PATTERN (undobuf.other_insn));
4145 i3notes = copy_rtx_if_shared (i3notes);
4146 i2notes = copy_rtx_if_shared (i2notes);
4147 i1notes = copy_rtx_if_shared (i1notes);
4148 i0notes = copy_rtx_if_shared (i0notes);
4149 newpat = copy_rtx_if_shared (newpat);
4150 newi2pat = copy_rtx_if_shared (newi2pat);
4151 if (undobuf.other_insn)
4152 reset_used_flags (PATTERN (undobuf.other_insn));
4154 INSN_CODE (i3) = insn_code_number;
4155 PATTERN (i3) = newpat;
4157 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4159 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4161 reset_used_flags (call_usage);
4162 call_usage = copy_rtx (call_usage);
4164 if (substed_i2)
4166 /* I2SRC must still be meaningful at this point. Some splitting
4167 operations can invalidate I2SRC, but those operations do not
4168 apply to calls. */
4169 gcc_assert (i2src);
4170 replace_rtx (call_usage, i2dest, i2src);
4173 if (substed_i1)
4174 replace_rtx (call_usage, i1dest, i1src);
4175 if (substed_i0)
4176 replace_rtx (call_usage, i0dest, i0src);
4178 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4181 if (undobuf.other_insn)
4182 INSN_CODE (undobuf.other_insn) = other_code_number;
4184 /* We had one special case above where I2 had more than one set and
4185 we replaced a destination of one of those sets with the destination
4186 of I3. In that case, we have to update LOG_LINKS of insns later
4187 in this basic block. Note that this (expensive) case is rare.
4189 Also, in this case, we must pretend that all REG_NOTEs for I2
4190 actually came from I3, so that REG_UNUSED notes from I2 will be
4191 properly handled. */
4193 if (i3_subst_into_i2)
4195 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4196 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4197 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4198 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4199 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4200 && ! find_reg_note (i2, REG_UNUSED,
4201 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4202 for (temp_insn = NEXT_INSN (i2);
4203 temp_insn
4204 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4205 || BB_HEAD (this_basic_block) != temp_insn);
4206 temp_insn = NEXT_INSN (temp_insn))
4207 if (temp_insn != i3 && INSN_P (temp_insn))
4208 FOR_EACH_LOG_LINK (link, temp_insn)
4209 if (link->insn == i2)
4210 link->insn = i3;
4212 if (i3notes)
4214 rtx link = i3notes;
4215 while (XEXP (link, 1))
4216 link = XEXP (link, 1);
4217 XEXP (link, 1) = i2notes;
4219 else
4220 i3notes = i2notes;
4221 i2notes = 0;
4224 LOG_LINKS (i3) = NULL;
4225 REG_NOTES (i3) = 0;
4226 LOG_LINKS (i2) = NULL;
4227 REG_NOTES (i2) = 0;
4229 if (newi2pat)
4231 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4232 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4233 this_basic_block);
4234 INSN_CODE (i2) = i2_code_number;
4235 PATTERN (i2) = newi2pat;
4237 else
4239 if (MAY_HAVE_DEBUG_INSNS && i2src)
4240 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4241 this_basic_block);
4242 SET_INSN_DELETED (i2);
4245 if (i1)
4247 LOG_LINKS (i1) = NULL;
4248 REG_NOTES (i1) = 0;
4249 if (MAY_HAVE_DEBUG_INSNS)
4250 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4251 this_basic_block);
4252 SET_INSN_DELETED (i1);
4255 if (i0)
4257 LOG_LINKS (i0) = NULL;
4258 REG_NOTES (i0) = 0;
4259 if (MAY_HAVE_DEBUG_INSNS)
4260 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4261 this_basic_block);
4262 SET_INSN_DELETED (i0);
4265 /* Get death notes for everything that is now used in either I3 or
4266 I2 and used to die in a previous insn. If we built two new
4267 patterns, move from I1 to I2 then I2 to I3 so that we get the
4268 proper movement on registers that I2 modifies. */
4270 if (i0)
4271 from_luid = DF_INSN_LUID (i0);
4272 else if (i1)
4273 from_luid = DF_INSN_LUID (i1);
4274 else
4275 from_luid = DF_INSN_LUID (i2);
4276 if (newi2pat)
4277 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4278 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4280 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4281 if (i3notes)
4282 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4283 elim_i2, elim_i1, elim_i0);
4284 if (i2notes)
4285 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4286 elim_i2, elim_i1, elim_i0);
4287 if (i1notes)
4288 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4289 elim_i2, elim_i1, elim_i0);
4290 if (i0notes)
4291 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4292 elim_i2, elim_i1, elim_i0);
4293 if (midnotes)
4294 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4295 elim_i2, elim_i1, elim_i0);
4297 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4298 know these are REG_UNUSED and want them to go to the desired insn,
4299 so we always pass it as i3. */
4301 if (newi2pat && new_i2_notes)
4302 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4303 NULL_RTX);
4305 if (new_i3_notes)
4306 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4307 NULL_RTX);
4309 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4310 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4311 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4312 in that case, it might delete I2. Similarly for I2 and I1.
4313 Show an additional death due to the REG_DEAD note we make here. If
4314 we discard it in distribute_notes, we will decrement it again. */
4316 if (i3dest_killed)
4318 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4319 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4320 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4321 elim_i1, elim_i0);
4322 else
4323 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4324 elim_i2, elim_i1, elim_i0);
4327 if (i2dest_in_i2src)
4329 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4330 if (newi2pat && reg_set_p (i2dest, newi2pat))
4331 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4332 NULL_RTX, NULL_RTX);
4333 else
4334 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4335 NULL_RTX, NULL_RTX, NULL_RTX);
4338 if (i1dest_in_i1src)
4340 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4341 if (newi2pat && reg_set_p (i1dest, newi2pat))
4342 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4343 NULL_RTX, NULL_RTX);
4344 else
4345 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4346 NULL_RTX, NULL_RTX, NULL_RTX);
4349 if (i0dest_in_i0src)
4351 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4352 if (newi2pat && reg_set_p (i0dest, newi2pat))
4353 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4354 NULL_RTX, NULL_RTX);
4355 else
4356 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4357 NULL_RTX, NULL_RTX, NULL_RTX);
4360 distribute_links (i3links);
4361 distribute_links (i2links);
4362 distribute_links (i1links);
4363 distribute_links (i0links);
4365 if (REG_P (i2dest))
4367 struct insn_link *link;
4368 rtx_insn *i2_insn = 0;
4369 rtx i2_val = 0, set;
4371 /* The insn that used to set this register doesn't exist, and
4372 this life of the register may not exist either. See if one of
4373 I3's links points to an insn that sets I2DEST. If it does,
4374 that is now the last known value for I2DEST. If we don't update
4375 this and I2 set the register to a value that depended on its old
4376 contents, we will get confused. If this insn is used, thing
4377 will be set correctly in combine_instructions. */
4378 FOR_EACH_LOG_LINK (link, i3)
4379 if ((set = single_set (link->insn)) != 0
4380 && rtx_equal_p (i2dest, SET_DEST (set)))
4381 i2_insn = link->insn, i2_val = SET_SRC (set);
4383 record_value_for_reg (i2dest, i2_insn, i2_val);
4385 /* If the reg formerly set in I2 died only once and that was in I3,
4386 zero its use count so it won't make `reload' do any work. */
4387 if (! added_sets_2
4388 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4389 && ! i2dest_in_i2src)
4390 INC_REG_N_SETS (REGNO (i2dest), -1);
4393 if (i1 && REG_P (i1dest))
4395 struct insn_link *link;
4396 rtx_insn *i1_insn = 0;
4397 rtx i1_val = 0, set;
4399 FOR_EACH_LOG_LINK (link, i3)
4400 if ((set = single_set (link->insn)) != 0
4401 && rtx_equal_p (i1dest, SET_DEST (set)))
4402 i1_insn = link->insn, i1_val = SET_SRC (set);
4404 record_value_for_reg (i1dest, i1_insn, i1_val);
4406 if (! added_sets_1 && ! i1dest_in_i1src)
4407 INC_REG_N_SETS (REGNO (i1dest), -1);
4410 if (i0 && REG_P (i0dest))
4412 struct insn_link *link;
4413 rtx_insn *i0_insn = 0;
4414 rtx i0_val = 0, set;
4416 FOR_EACH_LOG_LINK (link, i3)
4417 if ((set = single_set (link->insn)) != 0
4418 && rtx_equal_p (i0dest, SET_DEST (set)))
4419 i0_insn = link->insn, i0_val = SET_SRC (set);
4421 record_value_for_reg (i0dest, i0_insn, i0_val);
4423 if (! added_sets_0 && ! i0dest_in_i0src)
4424 INC_REG_N_SETS (REGNO (i0dest), -1);
4427 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4428 been made to this insn. The order is important, because newi2pat
4429 can affect nonzero_bits of newpat. */
4430 if (newi2pat)
4431 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4432 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4435 if (undobuf.other_insn != NULL_RTX)
4437 if (dump_file)
4439 fprintf (dump_file, "modifying other_insn ");
4440 dump_insn_slim (dump_file, undobuf.other_insn);
4442 df_insn_rescan (undobuf.other_insn);
4445 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4447 if (dump_file)
4449 fprintf (dump_file, "modifying insn i0 ");
4450 dump_insn_slim (dump_file, i0);
4452 df_insn_rescan (i0);
4455 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4457 if (dump_file)
4459 fprintf (dump_file, "modifying insn i1 ");
4460 dump_insn_slim (dump_file, i1);
4462 df_insn_rescan (i1);
4465 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4467 if (dump_file)
4469 fprintf (dump_file, "modifying insn i2 ");
4470 dump_insn_slim (dump_file, i2);
4472 df_insn_rescan (i2);
4475 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4477 if (dump_file)
4479 fprintf (dump_file, "modifying insn i3 ");
4480 dump_insn_slim (dump_file, i3);
4482 df_insn_rescan (i3);
4485 /* Set new_direct_jump_p if a new return or simple jump instruction
4486 has been created. Adjust the CFG accordingly. */
4487 if (returnjump_p (i3) || any_uncondjump_p (i3))
4489 *new_direct_jump_p = 1;
4490 mark_jump_label (PATTERN (i3), i3, 0);
4491 update_cfg_for_uncondjump (i3);
4494 if (undobuf.other_insn != NULL_RTX
4495 && (returnjump_p (undobuf.other_insn)
4496 || any_uncondjump_p (undobuf.other_insn)))
4498 *new_direct_jump_p = 1;
4499 update_cfg_for_uncondjump (undobuf.other_insn);
4502 /* A noop might also need cleaning up of CFG, if it comes from the
4503 simplification of a jump. */
4504 if (JUMP_P (i3)
4505 && GET_CODE (newpat) == SET
4506 && SET_SRC (newpat) == pc_rtx
4507 && SET_DEST (newpat) == pc_rtx)
4509 *new_direct_jump_p = 1;
4510 update_cfg_for_uncondjump (i3);
4513 if (undobuf.other_insn != NULL_RTX
4514 && JUMP_P (undobuf.other_insn)
4515 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4516 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4517 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4519 *new_direct_jump_p = 1;
4520 update_cfg_for_uncondjump (undobuf.other_insn);
4523 combine_successes++;
4524 undo_commit ();
4526 if (added_links_insn
4527 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4528 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4529 return added_links_insn;
4530 else
4531 return newi2pat ? i2 : i3;
4534 /* Undo all the modifications recorded in undobuf. */
4536 static void
4537 undo_all (void)
4539 struct undo *undo, *next;
4541 for (undo = undobuf.undos; undo; undo = next)
4543 next = undo->next;
4544 switch (undo->kind)
4546 case UNDO_RTX:
4547 *undo->where.r = undo->old_contents.r;
4548 break;
4549 case UNDO_INT:
4550 *undo->where.i = undo->old_contents.i;
4551 break;
4552 case UNDO_MODE:
4553 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4554 break;
4555 case UNDO_LINKS:
4556 *undo->where.l = undo->old_contents.l;
4557 break;
4558 default:
4559 gcc_unreachable ();
4562 undo->next = undobuf.frees;
4563 undobuf.frees = undo;
4566 undobuf.undos = 0;
4569 /* We've committed to accepting the changes we made. Move all
4570 of the undos to the free list. */
4572 static void
4573 undo_commit (void)
4575 struct undo *undo, *next;
4577 for (undo = undobuf.undos; undo; undo = next)
4579 next = undo->next;
4580 undo->next = undobuf.frees;
4581 undobuf.frees = undo;
4583 undobuf.undos = 0;
4586 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4587 where we have an arithmetic expression and return that point. LOC will
4588 be inside INSN.
4590 try_combine will call this function to see if an insn can be split into
4591 two insns. */
4593 static rtx *
4594 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4596 rtx x = *loc;
4597 enum rtx_code code = GET_CODE (x);
4598 rtx *split;
4599 unsigned HOST_WIDE_INT len = 0;
4600 HOST_WIDE_INT pos = 0;
4601 int unsignedp = 0;
4602 rtx inner = NULL_RTX;
4604 /* First special-case some codes. */
4605 switch (code)
4607 case SUBREG:
4608 #ifdef INSN_SCHEDULING
4609 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4610 point. */
4611 if (MEM_P (SUBREG_REG (x)))
4612 return loc;
4613 #endif
4614 return find_split_point (&SUBREG_REG (x), insn, false);
4616 case MEM:
4617 #ifdef HAVE_lo_sum
4618 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4619 using LO_SUM and HIGH. */
4620 if (GET_CODE (XEXP (x, 0)) == CONST
4621 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4623 machine_mode address_mode = get_address_mode (x);
4625 SUBST (XEXP (x, 0),
4626 gen_rtx_LO_SUM (address_mode,
4627 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4628 XEXP (x, 0)));
4629 return &XEXP (XEXP (x, 0), 0);
4631 #endif
4633 /* If we have a PLUS whose second operand is a constant and the
4634 address is not valid, perhaps will can split it up using
4635 the machine-specific way to split large constants. We use
4636 the first pseudo-reg (one of the virtual regs) as a placeholder;
4637 it will not remain in the result. */
4638 if (GET_CODE (XEXP (x, 0)) == PLUS
4639 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4640 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4641 MEM_ADDR_SPACE (x)))
4643 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4644 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4645 XEXP (x, 0)),
4646 subst_insn);
4648 /* This should have produced two insns, each of which sets our
4649 placeholder. If the source of the second is a valid address,
4650 we can make put both sources together and make a split point
4651 in the middle. */
4653 if (seq
4654 && NEXT_INSN (seq) != NULL_RTX
4655 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4656 && NONJUMP_INSN_P (seq)
4657 && GET_CODE (PATTERN (seq)) == SET
4658 && SET_DEST (PATTERN (seq)) == reg
4659 && ! reg_mentioned_p (reg,
4660 SET_SRC (PATTERN (seq)))
4661 && NONJUMP_INSN_P (NEXT_INSN (seq))
4662 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4663 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4664 && memory_address_addr_space_p
4665 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4666 MEM_ADDR_SPACE (x)))
4668 rtx src1 = SET_SRC (PATTERN (seq));
4669 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4671 /* Replace the placeholder in SRC2 with SRC1. If we can
4672 find where in SRC2 it was placed, that can become our
4673 split point and we can replace this address with SRC2.
4674 Just try two obvious places. */
4676 src2 = replace_rtx (src2, reg, src1);
4677 split = 0;
4678 if (XEXP (src2, 0) == src1)
4679 split = &XEXP (src2, 0);
4680 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4681 && XEXP (XEXP (src2, 0), 0) == src1)
4682 split = &XEXP (XEXP (src2, 0), 0);
4684 if (split)
4686 SUBST (XEXP (x, 0), src2);
4687 return split;
4691 /* If that didn't work, perhaps the first operand is complex and
4692 needs to be computed separately, so make a split point there.
4693 This will occur on machines that just support REG + CONST
4694 and have a constant moved through some previous computation. */
4696 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4697 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4698 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4699 return &XEXP (XEXP (x, 0), 0);
4702 /* If we have a PLUS whose first operand is complex, try computing it
4703 separately by making a split there. */
4704 if (GET_CODE (XEXP (x, 0)) == PLUS
4705 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4706 MEM_ADDR_SPACE (x))
4707 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4708 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4709 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4710 return &XEXP (XEXP (x, 0), 0);
4711 break;
4713 case SET:
4714 #ifdef HAVE_cc0
4715 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4716 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4717 we need to put the operand into a register. So split at that
4718 point. */
4720 if (SET_DEST (x) == cc0_rtx
4721 && GET_CODE (SET_SRC (x)) != COMPARE
4722 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4723 && !OBJECT_P (SET_SRC (x))
4724 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4725 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4726 return &SET_SRC (x);
4727 #endif
4729 /* See if we can split SET_SRC as it stands. */
4730 split = find_split_point (&SET_SRC (x), insn, true);
4731 if (split && split != &SET_SRC (x))
4732 return split;
4734 /* See if we can split SET_DEST as it stands. */
4735 split = find_split_point (&SET_DEST (x), insn, false);
4736 if (split && split != &SET_DEST (x))
4737 return split;
4739 /* See if this is a bitfield assignment with everything constant. If
4740 so, this is an IOR of an AND, so split it into that. */
4741 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4742 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4743 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4744 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4745 && CONST_INT_P (SET_SRC (x))
4746 && ((INTVAL (XEXP (SET_DEST (x), 1))
4747 + INTVAL (XEXP (SET_DEST (x), 2)))
4748 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4749 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4751 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4752 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4753 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4754 rtx dest = XEXP (SET_DEST (x), 0);
4755 machine_mode mode = GET_MODE (dest);
4756 unsigned HOST_WIDE_INT mask
4757 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4758 rtx or_mask;
4760 if (BITS_BIG_ENDIAN)
4761 pos = GET_MODE_PRECISION (mode) - len - pos;
4763 or_mask = gen_int_mode (src << pos, mode);
4764 if (src == mask)
4765 SUBST (SET_SRC (x),
4766 simplify_gen_binary (IOR, mode, dest, or_mask));
4767 else
4769 rtx negmask = gen_int_mode (~(mask << pos), mode);
4770 SUBST (SET_SRC (x),
4771 simplify_gen_binary (IOR, mode,
4772 simplify_gen_binary (AND, mode,
4773 dest, negmask),
4774 or_mask));
4777 SUBST (SET_DEST (x), dest);
4779 split = find_split_point (&SET_SRC (x), insn, true);
4780 if (split && split != &SET_SRC (x))
4781 return split;
4784 /* Otherwise, see if this is an operation that we can split into two.
4785 If so, try to split that. */
4786 code = GET_CODE (SET_SRC (x));
4788 switch (code)
4790 case AND:
4791 /* If we are AND'ing with a large constant that is only a single
4792 bit and the result is only being used in a context where we
4793 need to know if it is zero or nonzero, replace it with a bit
4794 extraction. This will avoid the large constant, which might
4795 have taken more than one insn to make. If the constant were
4796 not a valid argument to the AND but took only one insn to make,
4797 this is no worse, but if it took more than one insn, it will
4798 be better. */
4800 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4801 && REG_P (XEXP (SET_SRC (x), 0))
4802 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4803 && REG_P (SET_DEST (x))
4804 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4805 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4806 && XEXP (*split, 0) == SET_DEST (x)
4807 && XEXP (*split, 1) == const0_rtx)
4809 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4810 XEXP (SET_SRC (x), 0),
4811 pos, NULL_RTX, 1, 1, 0, 0);
4812 if (extraction != 0)
4814 SUBST (SET_SRC (x), extraction);
4815 return find_split_point (loc, insn, false);
4818 break;
4820 case NE:
4821 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4822 is known to be on, this can be converted into a NEG of a shift. */
4823 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4824 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4825 && 1 <= (pos = exact_log2
4826 (nonzero_bits (XEXP (SET_SRC (x), 0),
4827 GET_MODE (XEXP (SET_SRC (x), 0))))))
4829 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4831 SUBST (SET_SRC (x),
4832 gen_rtx_NEG (mode,
4833 gen_rtx_LSHIFTRT (mode,
4834 XEXP (SET_SRC (x), 0),
4835 GEN_INT (pos))));
4837 split = find_split_point (&SET_SRC (x), insn, true);
4838 if (split && split != &SET_SRC (x))
4839 return split;
4841 break;
4843 case SIGN_EXTEND:
4844 inner = XEXP (SET_SRC (x), 0);
4846 /* We can't optimize if either mode is a partial integer
4847 mode as we don't know how many bits are significant
4848 in those modes. */
4849 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4850 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4851 break;
4853 pos = 0;
4854 len = GET_MODE_PRECISION (GET_MODE (inner));
4855 unsignedp = 0;
4856 break;
4858 case SIGN_EXTRACT:
4859 case ZERO_EXTRACT:
4860 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4861 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4863 inner = XEXP (SET_SRC (x), 0);
4864 len = INTVAL (XEXP (SET_SRC (x), 1));
4865 pos = INTVAL (XEXP (SET_SRC (x), 2));
4867 if (BITS_BIG_ENDIAN)
4868 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4869 unsignedp = (code == ZERO_EXTRACT);
4871 break;
4873 default:
4874 break;
4877 if (len && pos >= 0
4878 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4880 machine_mode mode = GET_MODE (SET_SRC (x));
4882 /* For unsigned, we have a choice of a shift followed by an
4883 AND or two shifts. Use two shifts for field sizes where the
4884 constant might be too large. We assume here that we can
4885 always at least get 8-bit constants in an AND insn, which is
4886 true for every current RISC. */
4888 if (unsignedp && len <= 8)
4890 unsigned HOST_WIDE_INT mask
4891 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4892 SUBST (SET_SRC (x),
4893 gen_rtx_AND (mode,
4894 gen_rtx_LSHIFTRT
4895 (mode, gen_lowpart (mode, inner),
4896 GEN_INT (pos)),
4897 gen_int_mode (mask, mode)));
4899 split = find_split_point (&SET_SRC (x), insn, true);
4900 if (split && split != &SET_SRC (x))
4901 return split;
4903 else
4905 SUBST (SET_SRC (x),
4906 gen_rtx_fmt_ee
4907 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4908 gen_rtx_ASHIFT (mode,
4909 gen_lowpart (mode, inner),
4910 GEN_INT (GET_MODE_PRECISION (mode)
4911 - len - pos)),
4912 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4914 split = find_split_point (&SET_SRC (x), insn, true);
4915 if (split && split != &SET_SRC (x))
4916 return split;
4920 /* See if this is a simple operation with a constant as the second
4921 operand. It might be that this constant is out of range and hence
4922 could be used as a split point. */
4923 if (BINARY_P (SET_SRC (x))
4924 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4925 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4926 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4927 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4928 return &XEXP (SET_SRC (x), 1);
4930 /* Finally, see if this is a simple operation with its first operand
4931 not in a register. The operation might require this operand in a
4932 register, so return it as a split point. We can always do this
4933 because if the first operand were another operation, we would have
4934 already found it as a split point. */
4935 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4936 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4937 return &XEXP (SET_SRC (x), 0);
4939 return 0;
4941 case AND:
4942 case IOR:
4943 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4944 it is better to write this as (not (ior A B)) so we can split it.
4945 Similarly for IOR. */
4946 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4948 SUBST (*loc,
4949 gen_rtx_NOT (GET_MODE (x),
4950 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4951 GET_MODE (x),
4952 XEXP (XEXP (x, 0), 0),
4953 XEXP (XEXP (x, 1), 0))));
4954 return find_split_point (loc, insn, set_src);
4957 /* Many RISC machines have a large set of logical insns. If the
4958 second operand is a NOT, put it first so we will try to split the
4959 other operand first. */
4960 if (GET_CODE (XEXP (x, 1)) == NOT)
4962 rtx tem = XEXP (x, 0);
4963 SUBST (XEXP (x, 0), XEXP (x, 1));
4964 SUBST (XEXP (x, 1), tem);
4966 break;
4968 case PLUS:
4969 case MINUS:
4970 /* Canonicalization can produce (minus A (mult B C)), where C is a
4971 constant. It may be better to try splitting (plus (mult B -C) A)
4972 instead if this isn't a multiply by a power of two. */
4973 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4974 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4975 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4977 machine_mode mode = GET_MODE (x);
4978 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4979 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4980 SUBST (*loc, gen_rtx_PLUS (mode,
4981 gen_rtx_MULT (mode,
4982 XEXP (XEXP (x, 1), 0),
4983 gen_int_mode (other_int,
4984 mode)),
4985 XEXP (x, 0)));
4986 return find_split_point (loc, insn, set_src);
4989 /* Split at a multiply-accumulate instruction. However if this is
4990 the SET_SRC, we likely do not have such an instruction and it's
4991 worthless to try this split. */
4992 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4993 return loc;
4995 default:
4996 break;
4999 /* Otherwise, select our actions depending on our rtx class. */
5000 switch (GET_RTX_CLASS (code))
5002 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5003 case RTX_TERNARY:
5004 split = find_split_point (&XEXP (x, 2), insn, false);
5005 if (split)
5006 return split;
5007 /* ... fall through ... */
5008 case RTX_BIN_ARITH:
5009 case RTX_COMM_ARITH:
5010 case RTX_COMPARE:
5011 case RTX_COMM_COMPARE:
5012 split = find_split_point (&XEXP (x, 1), insn, false);
5013 if (split)
5014 return split;
5015 /* ... fall through ... */
5016 case RTX_UNARY:
5017 /* Some machines have (and (shift ...) ...) insns. If X is not
5018 an AND, but XEXP (X, 0) is, use it as our split point. */
5019 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5020 return &XEXP (x, 0);
5022 split = find_split_point (&XEXP (x, 0), insn, false);
5023 if (split)
5024 return split;
5025 return loc;
5027 default:
5028 /* Otherwise, we don't have a split point. */
5029 return 0;
5033 /* Throughout X, replace FROM with TO, and return the result.
5034 The result is TO if X is FROM;
5035 otherwise the result is X, but its contents may have been modified.
5036 If they were modified, a record was made in undobuf so that
5037 undo_all will (among other things) return X to its original state.
5039 If the number of changes necessary is too much to record to undo,
5040 the excess changes are not made, so the result is invalid.
5041 The changes already made can still be undone.
5042 undobuf.num_undo is incremented for such changes, so by testing that
5043 the caller can tell whether the result is valid.
5045 `n_occurrences' is incremented each time FROM is replaced.
5047 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5049 IN_COND is nonzero if we are at the top level of a condition.
5051 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5052 by copying if `n_occurrences' is nonzero. */
5054 static rtx
5055 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5057 enum rtx_code code = GET_CODE (x);
5058 machine_mode op0_mode = VOIDmode;
5059 const char *fmt;
5060 int len, i;
5061 rtx new_rtx;
5063 /* Two expressions are equal if they are identical copies of a shared
5064 RTX or if they are both registers with the same register number
5065 and mode. */
5067 #define COMBINE_RTX_EQUAL_P(X,Y) \
5068 ((X) == (Y) \
5069 || (REG_P (X) && REG_P (Y) \
5070 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5072 /* Do not substitute into clobbers of regs -- this will never result in
5073 valid RTL. */
5074 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5075 return x;
5077 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5079 n_occurrences++;
5080 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5083 /* If X and FROM are the same register but different modes, they
5084 will not have been seen as equal above. However, the log links code
5085 will make a LOG_LINKS entry for that case. If we do nothing, we
5086 will try to rerecognize our original insn and, when it succeeds,
5087 we will delete the feeding insn, which is incorrect.
5089 So force this insn not to match in this (rare) case. */
5090 if (! in_dest && code == REG && REG_P (from)
5091 && reg_overlap_mentioned_p (x, from))
5092 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5094 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5095 of which may contain things that can be combined. */
5096 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5097 return x;
5099 /* It is possible to have a subexpression appear twice in the insn.
5100 Suppose that FROM is a register that appears within TO.
5101 Then, after that subexpression has been scanned once by `subst',
5102 the second time it is scanned, TO may be found. If we were
5103 to scan TO here, we would find FROM within it and create a
5104 self-referent rtl structure which is completely wrong. */
5105 if (COMBINE_RTX_EQUAL_P (x, to))
5106 return to;
5108 /* Parallel asm_operands need special attention because all of the
5109 inputs are shared across the arms. Furthermore, unsharing the
5110 rtl results in recognition failures. Failure to handle this case
5111 specially can result in circular rtl.
5113 Solve this by doing a normal pass across the first entry of the
5114 parallel, and only processing the SET_DESTs of the subsequent
5115 entries. Ug. */
5117 if (code == PARALLEL
5118 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5119 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5121 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5123 /* If this substitution failed, this whole thing fails. */
5124 if (GET_CODE (new_rtx) == CLOBBER
5125 && XEXP (new_rtx, 0) == const0_rtx)
5126 return new_rtx;
5128 SUBST (XVECEXP (x, 0, 0), new_rtx);
5130 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5132 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5134 if (!REG_P (dest)
5135 && GET_CODE (dest) != CC0
5136 && GET_CODE (dest) != PC)
5138 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5140 /* If this substitution failed, this whole thing fails. */
5141 if (GET_CODE (new_rtx) == CLOBBER
5142 && XEXP (new_rtx, 0) == const0_rtx)
5143 return new_rtx;
5145 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5149 else
5151 len = GET_RTX_LENGTH (code);
5152 fmt = GET_RTX_FORMAT (code);
5154 /* We don't need to process a SET_DEST that is a register, CC0,
5155 or PC, so set up to skip this common case. All other cases
5156 where we want to suppress replacing something inside a
5157 SET_SRC are handled via the IN_DEST operand. */
5158 if (code == SET
5159 && (REG_P (SET_DEST (x))
5160 || GET_CODE (SET_DEST (x)) == CC0
5161 || GET_CODE (SET_DEST (x)) == PC))
5162 fmt = "ie";
5164 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5165 constant. */
5166 if (fmt[0] == 'e')
5167 op0_mode = GET_MODE (XEXP (x, 0));
5169 for (i = 0; i < len; i++)
5171 if (fmt[i] == 'E')
5173 int j;
5174 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5176 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5178 new_rtx = (unique_copy && n_occurrences
5179 ? copy_rtx (to) : to);
5180 n_occurrences++;
5182 else
5184 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5185 unique_copy);
5187 /* If this substitution failed, this whole thing
5188 fails. */
5189 if (GET_CODE (new_rtx) == CLOBBER
5190 && XEXP (new_rtx, 0) == const0_rtx)
5191 return new_rtx;
5194 SUBST (XVECEXP (x, i, j), new_rtx);
5197 else if (fmt[i] == 'e')
5199 /* If this is a register being set, ignore it. */
5200 new_rtx = XEXP (x, i);
5201 if (in_dest
5202 && i == 0
5203 && (((code == SUBREG || code == ZERO_EXTRACT)
5204 && REG_P (new_rtx))
5205 || code == STRICT_LOW_PART))
5208 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5210 /* In general, don't install a subreg involving two
5211 modes not tieable. It can worsen register
5212 allocation, and can even make invalid reload
5213 insns, since the reg inside may need to be copied
5214 from in the outside mode, and that may be invalid
5215 if it is an fp reg copied in integer mode.
5217 We allow two exceptions to this: It is valid if
5218 it is inside another SUBREG and the mode of that
5219 SUBREG and the mode of the inside of TO is
5220 tieable and it is valid if X is a SET that copies
5221 FROM to CC0. */
5223 if (GET_CODE (to) == SUBREG
5224 && ! MODES_TIEABLE_P (GET_MODE (to),
5225 GET_MODE (SUBREG_REG (to)))
5226 && ! (code == SUBREG
5227 && MODES_TIEABLE_P (GET_MODE (x),
5228 GET_MODE (SUBREG_REG (to))))
5229 #ifdef HAVE_cc0
5230 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5231 #endif
5233 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5235 if (code == SUBREG
5236 && REG_P (to)
5237 && REGNO (to) < FIRST_PSEUDO_REGISTER
5238 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5239 SUBREG_BYTE (x),
5240 GET_MODE (x)) < 0)
5241 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5243 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5244 n_occurrences++;
5246 else
5247 /* If we are in a SET_DEST, suppress most cases unless we
5248 have gone inside a MEM, in which case we want to
5249 simplify the address. We assume here that things that
5250 are actually part of the destination have their inner
5251 parts in the first expression. This is true for SUBREG,
5252 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5253 things aside from REG and MEM that should appear in a
5254 SET_DEST. */
5255 new_rtx = subst (XEXP (x, i), from, to,
5256 (((in_dest
5257 && (code == SUBREG || code == STRICT_LOW_PART
5258 || code == ZERO_EXTRACT))
5259 || code == SET)
5260 && i == 0),
5261 code == IF_THEN_ELSE && i == 0,
5262 unique_copy);
5264 /* If we found that we will have to reject this combination,
5265 indicate that by returning the CLOBBER ourselves, rather than
5266 an expression containing it. This will speed things up as
5267 well as prevent accidents where two CLOBBERs are considered
5268 to be equal, thus producing an incorrect simplification. */
5270 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5271 return new_rtx;
5273 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5275 machine_mode mode = GET_MODE (x);
5277 x = simplify_subreg (GET_MODE (x), new_rtx,
5278 GET_MODE (SUBREG_REG (x)),
5279 SUBREG_BYTE (x));
5280 if (! x)
5281 x = gen_rtx_CLOBBER (mode, const0_rtx);
5283 else if (CONST_SCALAR_INT_P (new_rtx)
5284 && GET_CODE (x) == ZERO_EXTEND)
5286 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5287 new_rtx, GET_MODE (XEXP (x, 0)));
5288 gcc_assert (x);
5290 else
5291 SUBST (XEXP (x, i), new_rtx);
5296 /* Check if we are loading something from the constant pool via float
5297 extension; in this case we would undo compress_float_constant
5298 optimization and degenerate constant load to an immediate value. */
5299 if (GET_CODE (x) == FLOAT_EXTEND
5300 && MEM_P (XEXP (x, 0))
5301 && MEM_READONLY_P (XEXP (x, 0)))
5303 rtx tmp = avoid_constant_pool_reference (x);
5304 if (x != tmp)
5305 return x;
5308 /* Try to simplify X. If the simplification changed the code, it is likely
5309 that further simplification will help, so loop, but limit the number
5310 of repetitions that will be performed. */
5312 for (i = 0; i < 4; i++)
5314 /* If X is sufficiently simple, don't bother trying to do anything
5315 with it. */
5316 if (code != CONST_INT && code != REG && code != CLOBBER)
5317 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5319 if (GET_CODE (x) == code)
5320 break;
5322 code = GET_CODE (x);
5324 /* We no longer know the original mode of operand 0 since we
5325 have changed the form of X) */
5326 op0_mode = VOIDmode;
5329 return x;
5332 /* Simplify X, a piece of RTL. We just operate on the expression at the
5333 outer level; call `subst' to simplify recursively. Return the new
5334 expression.
5336 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5337 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5338 of a condition. */
5340 static rtx
5341 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5342 int in_cond)
5344 enum rtx_code code = GET_CODE (x);
5345 machine_mode mode = GET_MODE (x);
5346 rtx temp;
5347 int i;
5349 /* If this is a commutative operation, put a constant last and a complex
5350 expression first. We don't need to do this for comparisons here. */
5351 if (COMMUTATIVE_ARITH_P (x)
5352 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5354 temp = XEXP (x, 0);
5355 SUBST (XEXP (x, 0), XEXP (x, 1));
5356 SUBST (XEXP (x, 1), temp);
5359 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5360 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5361 things. Check for cases where both arms are testing the same
5362 condition.
5364 Don't do anything if all operands are very simple. */
5366 if ((BINARY_P (x)
5367 && ((!OBJECT_P (XEXP (x, 0))
5368 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5369 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5370 || (!OBJECT_P (XEXP (x, 1))
5371 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5372 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5373 || (UNARY_P (x)
5374 && (!OBJECT_P (XEXP (x, 0))
5375 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5376 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5378 rtx cond, true_rtx, false_rtx;
5380 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5381 if (cond != 0
5382 /* If everything is a comparison, what we have is highly unlikely
5383 to be simpler, so don't use it. */
5384 && ! (COMPARISON_P (x)
5385 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5387 rtx cop1 = const0_rtx;
5388 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5390 if (cond_code == NE && COMPARISON_P (cond))
5391 return x;
5393 /* Simplify the alternative arms; this may collapse the true and
5394 false arms to store-flag values. Be careful to use copy_rtx
5395 here since true_rtx or false_rtx might share RTL with x as a
5396 result of the if_then_else_cond call above. */
5397 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5398 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5400 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5401 is unlikely to be simpler. */
5402 if (general_operand (true_rtx, VOIDmode)
5403 && general_operand (false_rtx, VOIDmode))
5405 enum rtx_code reversed;
5407 /* Restarting if we generate a store-flag expression will cause
5408 us to loop. Just drop through in this case. */
5410 /* If the result values are STORE_FLAG_VALUE and zero, we can
5411 just make the comparison operation. */
5412 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5413 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5414 cond, cop1);
5415 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5416 && ((reversed = reversed_comparison_code_parts
5417 (cond_code, cond, cop1, NULL))
5418 != UNKNOWN))
5419 x = simplify_gen_relational (reversed, mode, VOIDmode,
5420 cond, cop1);
5422 /* Likewise, we can make the negate of a comparison operation
5423 if the result values are - STORE_FLAG_VALUE and zero. */
5424 else if (CONST_INT_P (true_rtx)
5425 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5426 && false_rtx == const0_rtx)
5427 x = simplify_gen_unary (NEG, mode,
5428 simplify_gen_relational (cond_code,
5429 mode, VOIDmode,
5430 cond, cop1),
5431 mode);
5432 else if (CONST_INT_P (false_rtx)
5433 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5434 && true_rtx == const0_rtx
5435 && ((reversed = reversed_comparison_code_parts
5436 (cond_code, cond, cop1, NULL))
5437 != UNKNOWN))
5438 x = simplify_gen_unary (NEG, mode,
5439 simplify_gen_relational (reversed,
5440 mode, VOIDmode,
5441 cond, cop1),
5442 mode);
5443 else
5444 return gen_rtx_IF_THEN_ELSE (mode,
5445 simplify_gen_relational (cond_code,
5446 mode,
5447 VOIDmode,
5448 cond,
5449 cop1),
5450 true_rtx, false_rtx);
5452 code = GET_CODE (x);
5453 op0_mode = VOIDmode;
5458 /* Try to fold this expression in case we have constants that weren't
5459 present before. */
5460 temp = 0;
5461 switch (GET_RTX_CLASS (code))
5463 case RTX_UNARY:
5464 if (op0_mode == VOIDmode)
5465 op0_mode = GET_MODE (XEXP (x, 0));
5466 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5467 break;
5468 case RTX_COMPARE:
5469 case RTX_COMM_COMPARE:
5471 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5472 if (cmp_mode == VOIDmode)
5474 cmp_mode = GET_MODE (XEXP (x, 1));
5475 if (cmp_mode == VOIDmode)
5476 cmp_mode = op0_mode;
5478 temp = simplify_relational_operation (code, mode, cmp_mode,
5479 XEXP (x, 0), XEXP (x, 1));
5481 break;
5482 case RTX_COMM_ARITH:
5483 case RTX_BIN_ARITH:
5484 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5485 break;
5486 case RTX_BITFIELD_OPS:
5487 case RTX_TERNARY:
5488 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5489 XEXP (x, 1), XEXP (x, 2));
5490 break;
5491 default:
5492 break;
5495 if (temp)
5497 x = temp;
5498 code = GET_CODE (temp);
5499 op0_mode = VOIDmode;
5500 mode = GET_MODE (temp);
5503 /* First see if we can apply the inverse distributive law. */
5504 if (code == PLUS || code == MINUS
5505 || code == AND || code == IOR || code == XOR)
5507 x = apply_distributive_law (x);
5508 code = GET_CODE (x);
5509 op0_mode = VOIDmode;
5512 /* If CODE is an associative operation not otherwise handled, see if we
5513 can associate some operands. This can win if they are constants or
5514 if they are logically related (i.e. (a & b) & a). */
5515 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5516 || code == AND || code == IOR || code == XOR
5517 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5518 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5519 || (flag_associative_math && FLOAT_MODE_P (mode))))
5521 if (GET_CODE (XEXP (x, 0)) == code)
5523 rtx other = XEXP (XEXP (x, 0), 0);
5524 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5525 rtx inner_op1 = XEXP (x, 1);
5526 rtx inner;
5528 /* Make sure we pass the constant operand if any as the second
5529 one if this is a commutative operation. */
5530 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5532 rtx tem = inner_op0;
5533 inner_op0 = inner_op1;
5534 inner_op1 = tem;
5536 inner = simplify_binary_operation (code == MINUS ? PLUS
5537 : code == DIV ? MULT
5538 : code,
5539 mode, inner_op0, inner_op1);
5541 /* For commutative operations, try the other pair if that one
5542 didn't simplify. */
5543 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5545 other = XEXP (XEXP (x, 0), 1);
5546 inner = simplify_binary_operation (code, mode,
5547 XEXP (XEXP (x, 0), 0),
5548 XEXP (x, 1));
5551 if (inner)
5552 return simplify_gen_binary (code, mode, other, inner);
5556 /* A little bit of algebraic simplification here. */
5557 switch (code)
5559 case MEM:
5560 /* Ensure that our address has any ASHIFTs converted to MULT in case
5561 address-recognizing predicates are called later. */
5562 temp = make_compound_operation (XEXP (x, 0), MEM);
5563 SUBST (XEXP (x, 0), temp);
5564 break;
5566 case SUBREG:
5567 if (op0_mode == VOIDmode)
5568 op0_mode = GET_MODE (SUBREG_REG (x));
5570 /* See if this can be moved to simplify_subreg. */
5571 if (CONSTANT_P (SUBREG_REG (x))
5572 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5573 /* Don't call gen_lowpart if the inner mode
5574 is VOIDmode and we cannot simplify it, as SUBREG without
5575 inner mode is invalid. */
5576 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5577 || gen_lowpart_common (mode, SUBREG_REG (x))))
5578 return gen_lowpart (mode, SUBREG_REG (x));
5580 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5581 break;
5583 rtx temp;
5584 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5585 SUBREG_BYTE (x));
5586 if (temp)
5587 return temp;
5589 /* If op is known to have all lower bits zero, the result is zero. */
5590 if (!in_dest
5591 && SCALAR_INT_MODE_P (mode)
5592 && SCALAR_INT_MODE_P (op0_mode)
5593 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5594 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5595 && HWI_COMPUTABLE_MODE_P (op0_mode)
5596 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5597 & GET_MODE_MASK (mode)) == 0)
5598 return CONST0_RTX (mode);
5601 /* Don't change the mode of the MEM if that would change the meaning
5602 of the address. */
5603 if (MEM_P (SUBREG_REG (x))
5604 && (MEM_VOLATILE_P (SUBREG_REG (x))
5605 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5606 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5607 return gen_rtx_CLOBBER (mode, const0_rtx);
5609 /* Note that we cannot do any narrowing for non-constants since
5610 we might have been counting on using the fact that some bits were
5611 zero. We now do this in the SET. */
5613 break;
5615 case NEG:
5616 temp = expand_compound_operation (XEXP (x, 0));
5618 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5619 replaced by (lshiftrt X C). This will convert
5620 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5622 if (GET_CODE (temp) == ASHIFTRT
5623 && CONST_INT_P (XEXP (temp, 1))
5624 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5625 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5626 INTVAL (XEXP (temp, 1)));
5628 /* If X has only a single bit that might be nonzero, say, bit I, convert
5629 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5630 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5631 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5632 or a SUBREG of one since we'd be making the expression more
5633 complex if it was just a register. */
5635 if (!REG_P (temp)
5636 && ! (GET_CODE (temp) == SUBREG
5637 && REG_P (SUBREG_REG (temp)))
5638 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5640 rtx temp1 = simplify_shift_const
5641 (NULL_RTX, ASHIFTRT, mode,
5642 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5643 GET_MODE_PRECISION (mode) - 1 - i),
5644 GET_MODE_PRECISION (mode) - 1 - i);
5646 /* If all we did was surround TEMP with the two shifts, we
5647 haven't improved anything, so don't use it. Otherwise,
5648 we are better off with TEMP1. */
5649 if (GET_CODE (temp1) != ASHIFTRT
5650 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5651 || XEXP (XEXP (temp1, 0), 0) != temp)
5652 return temp1;
5654 break;
5656 case TRUNCATE:
5657 /* We can't handle truncation to a partial integer mode here
5658 because we don't know the real bitsize of the partial
5659 integer mode. */
5660 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5661 break;
5663 if (HWI_COMPUTABLE_MODE_P (mode))
5664 SUBST (XEXP (x, 0),
5665 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5666 GET_MODE_MASK (mode), 0));
5668 /* We can truncate a constant value and return it. */
5669 if (CONST_INT_P (XEXP (x, 0)))
5670 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5672 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5673 whose value is a comparison can be replaced with a subreg if
5674 STORE_FLAG_VALUE permits. */
5675 if (HWI_COMPUTABLE_MODE_P (mode)
5676 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5677 && (temp = get_last_value (XEXP (x, 0)))
5678 && COMPARISON_P (temp))
5679 return gen_lowpart (mode, XEXP (x, 0));
5680 break;
5682 case CONST:
5683 /* (const (const X)) can become (const X). Do it this way rather than
5684 returning the inner CONST since CONST can be shared with a
5685 REG_EQUAL note. */
5686 if (GET_CODE (XEXP (x, 0)) == CONST)
5687 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5688 break;
5690 #ifdef HAVE_lo_sum
5691 case LO_SUM:
5692 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5693 can add in an offset. find_split_point will split this address up
5694 again if it doesn't match. */
5695 if (GET_CODE (XEXP (x, 0)) == HIGH
5696 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5697 return XEXP (x, 1);
5698 break;
5699 #endif
5701 case PLUS:
5702 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5703 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5704 bit-field and can be replaced by either a sign_extend or a
5705 sign_extract. The `and' may be a zero_extend and the two
5706 <c>, -<c> constants may be reversed. */
5707 if (GET_CODE (XEXP (x, 0)) == XOR
5708 && CONST_INT_P (XEXP (x, 1))
5709 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5710 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5711 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5712 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5713 && HWI_COMPUTABLE_MODE_P (mode)
5714 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5715 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5716 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5717 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5718 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5719 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5720 == (unsigned int) i + 1))))
5721 return simplify_shift_const
5722 (NULL_RTX, ASHIFTRT, mode,
5723 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5724 XEXP (XEXP (XEXP (x, 0), 0), 0),
5725 GET_MODE_PRECISION (mode) - (i + 1)),
5726 GET_MODE_PRECISION (mode) - (i + 1));
5728 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5729 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5730 the bitsize of the mode - 1. This allows simplification of
5731 "a = (b & 8) == 0;" */
5732 if (XEXP (x, 1) == constm1_rtx
5733 && !REG_P (XEXP (x, 0))
5734 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5735 && REG_P (SUBREG_REG (XEXP (x, 0))))
5736 && nonzero_bits (XEXP (x, 0), mode) == 1)
5737 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5738 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5739 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5740 GET_MODE_PRECISION (mode) - 1),
5741 GET_MODE_PRECISION (mode) - 1);
5743 /* If we are adding two things that have no bits in common, convert
5744 the addition into an IOR. This will often be further simplified,
5745 for example in cases like ((a & 1) + (a & 2)), which can
5746 become a & 3. */
5748 if (HWI_COMPUTABLE_MODE_P (mode)
5749 && (nonzero_bits (XEXP (x, 0), mode)
5750 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5752 /* Try to simplify the expression further. */
5753 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5754 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5756 /* If we could, great. If not, do not go ahead with the IOR
5757 replacement, since PLUS appears in many special purpose
5758 address arithmetic instructions. */
5759 if (GET_CODE (temp) != CLOBBER
5760 && (GET_CODE (temp) != IOR
5761 || ((XEXP (temp, 0) != XEXP (x, 0)
5762 || XEXP (temp, 1) != XEXP (x, 1))
5763 && (XEXP (temp, 0) != XEXP (x, 1)
5764 || XEXP (temp, 1) != XEXP (x, 0)))))
5765 return temp;
5767 break;
5769 case MINUS:
5770 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5771 (and <foo> (const_int pow2-1)) */
5772 if (GET_CODE (XEXP (x, 1)) == AND
5773 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5774 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5775 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5776 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5777 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5778 break;
5780 case MULT:
5781 /* If we have (mult (plus A B) C), apply the distributive law and then
5782 the inverse distributive law to see if things simplify. This
5783 occurs mostly in addresses, often when unrolling loops. */
5785 if (GET_CODE (XEXP (x, 0)) == PLUS)
5787 rtx result = distribute_and_simplify_rtx (x, 0);
5788 if (result)
5789 return result;
5792 /* Try simplify a*(b/c) as (a*b)/c. */
5793 if (FLOAT_MODE_P (mode) && flag_associative_math
5794 && GET_CODE (XEXP (x, 0)) == DIV)
5796 rtx tem = simplify_binary_operation (MULT, mode,
5797 XEXP (XEXP (x, 0), 0),
5798 XEXP (x, 1));
5799 if (tem)
5800 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5802 break;
5804 case UDIV:
5805 /* If this is a divide by a power of two, treat it as a shift if
5806 its first operand is a shift. */
5807 if (CONST_INT_P (XEXP (x, 1))
5808 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5809 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5810 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5811 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5812 || GET_CODE (XEXP (x, 0)) == ROTATE
5813 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5814 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5815 break;
5817 case EQ: case NE:
5818 case GT: case GTU: case GE: case GEU:
5819 case LT: case LTU: case LE: case LEU:
5820 case UNEQ: case LTGT:
5821 case UNGT: case UNGE:
5822 case UNLT: case UNLE:
5823 case UNORDERED: case ORDERED:
5824 /* If the first operand is a condition code, we can't do anything
5825 with it. */
5826 if (GET_CODE (XEXP (x, 0)) == COMPARE
5827 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5828 && ! CC0_P (XEXP (x, 0))))
5830 rtx op0 = XEXP (x, 0);
5831 rtx op1 = XEXP (x, 1);
5832 enum rtx_code new_code;
5834 if (GET_CODE (op0) == COMPARE)
5835 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5837 /* Simplify our comparison, if possible. */
5838 new_code = simplify_comparison (code, &op0, &op1);
5840 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5841 if only the low-order bit is possibly nonzero in X (such as when
5842 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5843 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5844 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5845 (plus X 1).
5847 Remove any ZERO_EXTRACT we made when thinking this was a
5848 comparison. It may now be simpler to use, e.g., an AND. If a
5849 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5850 the call to make_compound_operation in the SET case.
5852 Don't apply these optimizations if the caller would
5853 prefer a comparison rather than a value.
5854 E.g., for the condition in an IF_THEN_ELSE most targets need
5855 an explicit comparison. */
5857 if (in_cond)
5860 else if (STORE_FLAG_VALUE == 1
5861 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5862 && op1 == const0_rtx
5863 && mode == GET_MODE (op0)
5864 && nonzero_bits (op0, mode) == 1)
5865 return gen_lowpart (mode,
5866 expand_compound_operation (op0));
5868 else if (STORE_FLAG_VALUE == 1
5869 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5870 && op1 == const0_rtx
5871 && mode == GET_MODE (op0)
5872 && (num_sign_bit_copies (op0, mode)
5873 == GET_MODE_PRECISION (mode)))
5875 op0 = expand_compound_operation (op0);
5876 return simplify_gen_unary (NEG, mode,
5877 gen_lowpart (mode, op0),
5878 mode);
5881 else if (STORE_FLAG_VALUE == 1
5882 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5883 && op1 == const0_rtx
5884 && mode == GET_MODE (op0)
5885 && nonzero_bits (op0, mode) == 1)
5887 op0 = expand_compound_operation (op0);
5888 return simplify_gen_binary (XOR, mode,
5889 gen_lowpart (mode, op0),
5890 const1_rtx);
5893 else if (STORE_FLAG_VALUE == 1
5894 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5895 && op1 == const0_rtx
5896 && mode == GET_MODE (op0)
5897 && (num_sign_bit_copies (op0, mode)
5898 == GET_MODE_PRECISION (mode)))
5900 op0 = expand_compound_operation (op0);
5901 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5904 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5905 those above. */
5906 if (in_cond)
5909 else if (STORE_FLAG_VALUE == -1
5910 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5911 && op1 == const0_rtx
5912 && mode == GET_MODE (op0)
5913 && (num_sign_bit_copies (op0, mode)
5914 == GET_MODE_PRECISION (mode)))
5915 return gen_lowpart (mode,
5916 expand_compound_operation (op0));
5918 else if (STORE_FLAG_VALUE == -1
5919 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5920 && op1 == const0_rtx
5921 && mode == GET_MODE (op0)
5922 && nonzero_bits (op0, mode) == 1)
5924 op0 = expand_compound_operation (op0);
5925 return simplify_gen_unary (NEG, mode,
5926 gen_lowpart (mode, op0),
5927 mode);
5930 else if (STORE_FLAG_VALUE == -1
5931 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5932 && op1 == const0_rtx
5933 && mode == GET_MODE (op0)
5934 && (num_sign_bit_copies (op0, mode)
5935 == GET_MODE_PRECISION (mode)))
5937 op0 = expand_compound_operation (op0);
5938 return simplify_gen_unary (NOT, mode,
5939 gen_lowpart (mode, op0),
5940 mode);
5943 /* If X is 0/1, (eq X 0) is X-1. */
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 && nonzero_bits (op0, mode) == 1)
5950 op0 = expand_compound_operation (op0);
5951 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5954 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5955 one bit that might be nonzero, we can convert (ne x 0) to
5956 (ashift x c) where C puts the bit in the sign bit. Remove any
5957 AND with STORE_FLAG_VALUE when we are done, since we are only
5958 going to test the sign bit. */
5959 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5960 && HWI_COMPUTABLE_MODE_P (mode)
5961 && val_signbit_p (mode, STORE_FLAG_VALUE)
5962 && op1 == const0_rtx
5963 && mode == GET_MODE (op0)
5964 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5966 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5967 expand_compound_operation (op0),
5968 GET_MODE_PRECISION (mode) - 1 - i);
5969 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5970 return XEXP (x, 0);
5971 else
5972 return x;
5975 /* If the code changed, return a whole new comparison.
5976 We also need to avoid using SUBST in cases where
5977 simplify_comparison has widened a comparison with a CONST_INT,
5978 since in that case the wider CONST_INT may fail the sanity
5979 checks in do_SUBST. */
5980 if (new_code != code
5981 || (CONST_INT_P (op1)
5982 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5983 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5984 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5986 /* Otherwise, keep this operation, but maybe change its operands.
5987 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5988 SUBST (XEXP (x, 0), op0);
5989 SUBST (XEXP (x, 1), op1);
5991 break;
5993 case IF_THEN_ELSE:
5994 return simplify_if_then_else (x);
5996 case ZERO_EXTRACT:
5997 case SIGN_EXTRACT:
5998 case ZERO_EXTEND:
5999 case SIGN_EXTEND:
6000 /* If we are processing SET_DEST, we are done. */
6001 if (in_dest)
6002 return x;
6004 return expand_compound_operation (x);
6006 case SET:
6007 return simplify_set (x);
6009 case AND:
6010 case IOR:
6011 return simplify_logical (x);
6013 case ASHIFT:
6014 case LSHIFTRT:
6015 case ASHIFTRT:
6016 case ROTATE:
6017 case ROTATERT:
6018 /* If this is a shift by a constant amount, simplify it. */
6019 if (CONST_INT_P (XEXP (x, 1)))
6020 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6021 INTVAL (XEXP (x, 1)));
6023 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6024 SUBST (XEXP (x, 1),
6025 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6026 ((unsigned HOST_WIDE_INT) 1
6027 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6028 - 1,
6029 0));
6030 break;
6032 default:
6033 break;
6036 return x;
6039 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6041 static rtx
6042 simplify_if_then_else (rtx x)
6044 machine_mode mode = GET_MODE (x);
6045 rtx cond = XEXP (x, 0);
6046 rtx true_rtx = XEXP (x, 1);
6047 rtx false_rtx = XEXP (x, 2);
6048 enum rtx_code true_code = GET_CODE (cond);
6049 int comparison_p = COMPARISON_P (cond);
6050 rtx temp;
6051 int i;
6052 enum rtx_code false_code;
6053 rtx reversed;
6055 /* Simplify storing of the truth value. */
6056 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6057 return simplify_gen_relational (true_code, mode, VOIDmode,
6058 XEXP (cond, 0), XEXP (cond, 1));
6060 /* Also when the truth value has to be reversed. */
6061 if (comparison_p
6062 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6063 && (reversed = reversed_comparison (cond, mode)))
6064 return reversed;
6066 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6067 in it is being compared against certain values. Get the true and false
6068 comparisons and see if that says anything about the value of each arm. */
6070 if (comparison_p
6071 && ((false_code = reversed_comparison_code (cond, NULL))
6072 != UNKNOWN)
6073 && REG_P (XEXP (cond, 0)))
6075 HOST_WIDE_INT nzb;
6076 rtx from = XEXP (cond, 0);
6077 rtx true_val = XEXP (cond, 1);
6078 rtx false_val = true_val;
6079 int swapped = 0;
6081 /* If FALSE_CODE is EQ, swap the codes and arms. */
6083 if (false_code == EQ)
6085 swapped = 1, true_code = EQ, false_code = NE;
6086 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6089 /* If we are comparing against zero and the expression being tested has
6090 only a single bit that might be nonzero, that is its value when it is
6091 not equal to zero. Similarly if it is known to be -1 or 0. */
6093 if (true_code == EQ && true_val == const0_rtx
6094 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6096 false_code = EQ;
6097 false_val = gen_int_mode (nzb, GET_MODE (from));
6099 else if (true_code == EQ && true_val == const0_rtx
6100 && (num_sign_bit_copies (from, GET_MODE (from))
6101 == GET_MODE_PRECISION (GET_MODE (from))))
6103 false_code = EQ;
6104 false_val = constm1_rtx;
6107 /* Now simplify an arm if we know the value of the register in the
6108 branch and it is used in the arm. Be careful due to the potential
6109 of locally-shared RTL. */
6111 if (reg_mentioned_p (from, true_rtx))
6112 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6113 from, true_val),
6114 pc_rtx, pc_rtx, 0, 0, 0);
6115 if (reg_mentioned_p (from, false_rtx))
6116 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6117 from, false_val),
6118 pc_rtx, pc_rtx, 0, 0, 0);
6120 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6121 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6123 true_rtx = XEXP (x, 1);
6124 false_rtx = XEXP (x, 2);
6125 true_code = GET_CODE (cond);
6128 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6129 reversed, do so to avoid needing two sets of patterns for
6130 subtract-and-branch insns. Similarly if we have a constant in the true
6131 arm, the false arm is the same as the first operand of the comparison, or
6132 the false arm is more complicated than the true arm. */
6134 if (comparison_p
6135 && reversed_comparison_code (cond, NULL) != UNKNOWN
6136 && (true_rtx == pc_rtx
6137 || (CONSTANT_P (true_rtx)
6138 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6139 || true_rtx == const0_rtx
6140 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6141 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6142 && !OBJECT_P (false_rtx))
6143 || reg_mentioned_p (true_rtx, false_rtx)
6144 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6146 true_code = reversed_comparison_code (cond, NULL);
6147 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6148 SUBST (XEXP (x, 1), false_rtx);
6149 SUBST (XEXP (x, 2), true_rtx);
6151 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6152 cond = XEXP (x, 0);
6154 /* It is possible that the conditional has been simplified out. */
6155 true_code = GET_CODE (cond);
6156 comparison_p = COMPARISON_P (cond);
6159 /* If the two arms are identical, we don't need the comparison. */
6161 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6162 return true_rtx;
6164 /* Convert a == b ? b : a to "a". */
6165 if (true_code == EQ && ! side_effects_p (cond)
6166 && !HONOR_NANS (mode)
6167 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6168 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6169 return false_rtx;
6170 else if (true_code == NE && ! side_effects_p (cond)
6171 && !HONOR_NANS (mode)
6172 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6173 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6174 return true_rtx;
6176 /* Look for cases where we have (abs x) or (neg (abs X)). */
6178 if (GET_MODE_CLASS (mode) == MODE_INT
6179 && comparison_p
6180 && XEXP (cond, 1) == const0_rtx
6181 && GET_CODE (false_rtx) == NEG
6182 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6183 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6184 && ! side_effects_p (true_rtx))
6185 switch (true_code)
6187 case GT:
6188 case GE:
6189 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6190 case LT:
6191 case LE:
6192 return
6193 simplify_gen_unary (NEG, mode,
6194 simplify_gen_unary (ABS, mode, true_rtx, mode),
6195 mode);
6196 default:
6197 break;
6200 /* Look for MIN or MAX. */
6202 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6203 && comparison_p
6204 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6205 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6206 && ! side_effects_p (cond))
6207 switch (true_code)
6209 case GE:
6210 case GT:
6211 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6212 case LE:
6213 case LT:
6214 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6215 case GEU:
6216 case GTU:
6217 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6218 case LEU:
6219 case LTU:
6220 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6221 default:
6222 break;
6225 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6226 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6227 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6228 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6229 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6230 neither 1 or -1, but it isn't worth checking for. */
6232 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6233 && comparison_p
6234 && GET_MODE_CLASS (mode) == MODE_INT
6235 && ! side_effects_p (x))
6237 rtx t = make_compound_operation (true_rtx, SET);
6238 rtx f = make_compound_operation (false_rtx, SET);
6239 rtx cond_op0 = XEXP (cond, 0);
6240 rtx cond_op1 = XEXP (cond, 1);
6241 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6242 machine_mode m = mode;
6243 rtx z = 0, c1 = NULL_RTX;
6245 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6246 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6247 || GET_CODE (t) == ASHIFT
6248 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6249 && rtx_equal_p (XEXP (t, 0), f))
6250 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6252 /* If an identity-zero op is commutative, check whether there
6253 would be a match if we swapped the operands. */
6254 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6255 || GET_CODE (t) == XOR)
6256 && rtx_equal_p (XEXP (t, 1), f))
6257 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6258 else if (GET_CODE (t) == SIGN_EXTEND
6259 && (GET_CODE (XEXP (t, 0)) == PLUS
6260 || GET_CODE (XEXP (t, 0)) == MINUS
6261 || GET_CODE (XEXP (t, 0)) == IOR
6262 || GET_CODE (XEXP (t, 0)) == XOR
6263 || GET_CODE (XEXP (t, 0)) == ASHIFT
6264 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6265 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6266 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6267 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6268 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6269 && (num_sign_bit_copies (f, GET_MODE (f))
6270 > (unsigned int)
6271 (GET_MODE_PRECISION (mode)
6272 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6274 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6275 extend_op = SIGN_EXTEND;
6276 m = GET_MODE (XEXP (t, 0));
6278 else if (GET_CODE (t) == SIGN_EXTEND
6279 && (GET_CODE (XEXP (t, 0)) == PLUS
6280 || GET_CODE (XEXP (t, 0)) == IOR
6281 || GET_CODE (XEXP (t, 0)) == XOR)
6282 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6283 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6284 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6285 && (num_sign_bit_copies (f, GET_MODE (f))
6286 > (unsigned int)
6287 (GET_MODE_PRECISION (mode)
6288 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6290 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6291 extend_op = SIGN_EXTEND;
6292 m = GET_MODE (XEXP (t, 0));
6294 else if (GET_CODE (t) == ZERO_EXTEND
6295 && (GET_CODE (XEXP (t, 0)) == PLUS
6296 || GET_CODE (XEXP (t, 0)) == MINUS
6297 || GET_CODE (XEXP (t, 0)) == IOR
6298 || GET_CODE (XEXP (t, 0)) == XOR
6299 || GET_CODE (XEXP (t, 0)) == ASHIFT
6300 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6301 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6302 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6303 && HWI_COMPUTABLE_MODE_P (mode)
6304 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6305 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6306 && ((nonzero_bits (f, GET_MODE (f))
6307 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6308 == 0))
6310 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6311 extend_op = ZERO_EXTEND;
6312 m = GET_MODE (XEXP (t, 0));
6314 else if (GET_CODE (t) == ZERO_EXTEND
6315 && (GET_CODE (XEXP (t, 0)) == PLUS
6316 || GET_CODE (XEXP (t, 0)) == IOR
6317 || GET_CODE (XEXP (t, 0)) == XOR)
6318 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6319 && HWI_COMPUTABLE_MODE_P (mode)
6320 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6321 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6322 && ((nonzero_bits (f, GET_MODE (f))
6323 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6324 == 0))
6326 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6327 extend_op = ZERO_EXTEND;
6328 m = GET_MODE (XEXP (t, 0));
6331 if (z)
6333 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6334 cond_op0, cond_op1),
6335 pc_rtx, pc_rtx, 0, 0, 0);
6336 temp = simplify_gen_binary (MULT, m, temp,
6337 simplify_gen_binary (MULT, m, c1,
6338 const_true_rtx));
6339 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6340 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6342 if (extend_op != UNKNOWN)
6343 temp = simplify_gen_unary (extend_op, mode, temp, m);
6345 return temp;
6349 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6350 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6351 negation of a single bit, we can convert this operation to a shift. We
6352 can actually do this more generally, but it doesn't seem worth it. */
6354 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6355 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6356 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6357 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6358 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6359 == GET_MODE_PRECISION (mode))
6360 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6361 return
6362 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6363 gen_lowpart (mode, XEXP (cond, 0)), i);
6365 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6366 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6367 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6368 && GET_MODE (XEXP (cond, 0)) == mode
6369 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6370 == nonzero_bits (XEXP (cond, 0), mode)
6371 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6372 return XEXP (cond, 0);
6374 return x;
6377 /* Simplify X, a SET expression. Return the new expression. */
6379 static rtx
6380 simplify_set (rtx x)
6382 rtx src = SET_SRC (x);
6383 rtx dest = SET_DEST (x);
6384 machine_mode mode
6385 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6386 rtx_insn *other_insn;
6387 rtx *cc_use;
6389 /* (set (pc) (return)) gets written as (return). */
6390 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6391 return src;
6393 /* Now that we know for sure which bits of SRC we are using, see if we can
6394 simplify the expression for the object knowing that we only need the
6395 low-order bits. */
6397 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6399 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6400 SUBST (SET_SRC (x), src);
6403 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6404 the comparison result and try to simplify it unless we already have used
6405 undobuf.other_insn. */
6406 if ((GET_MODE_CLASS (mode) == MODE_CC
6407 || GET_CODE (src) == COMPARE
6408 || CC0_P (dest))
6409 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6410 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6411 && COMPARISON_P (*cc_use)
6412 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6414 enum rtx_code old_code = GET_CODE (*cc_use);
6415 enum rtx_code new_code;
6416 rtx op0, op1, tmp;
6417 int other_changed = 0;
6418 rtx inner_compare = NULL_RTX;
6419 machine_mode compare_mode = GET_MODE (dest);
6421 if (GET_CODE (src) == COMPARE)
6423 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6424 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6426 inner_compare = op0;
6427 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6430 else
6431 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6433 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6434 op0, op1);
6435 if (!tmp)
6436 new_code = old_code;
6437 else if (!CONSTANT_P (tmp))
6439 new_code = GET_CODE (tmp);
6440 op0 = XEXP (tmp, 0);
6441 op1 = XEXP (tmp, 1);
6443 else
6445 rtx pat = PATTERN (other_insn);
6446 undobuf.other_insn = other_insn;
6447 SUBST (*cc_use, tmp);
6449 /* Attempt to simplify CC user. */
6450 if (GET_CODE (pat) == SET)
6452 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6453 if (new_rtx != NULL_RTX)
6454 SUBST (SET_SRC (pat), new_rtx);
6457 /* Convert X into a no-op move. */
6458 SUBST (SET_DEST (x), pc_rtx);
6459 SUBST (SET_SRC (x), pc_rtx);
6460 return x;
6463 /* Simplify our comparison, if possible. */
6464 new_code = simplify_comparison (new_code, &op0, &op1);
6466 #ifdef SELECT_CC_MODE
6467 /* If this machine has CC modes other than CCmode, check to see if we
6468 need to use a different CC mode here. */
6469 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6470 compare_mode = GET_MODE (op0);
6471 else if (inner_compare
6472 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6473 && new_code == old_code
6474 && op0 == XEXP (inner_compare, 0)
6475 && op1 == XEXP (inner_compare, 1))
6476 compare_mode = GET_MODE (inner_compare);
6477 else
6478 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6480 #ifndef HAVE_cc0
6481 /* If the mode changed, we have to change SET_DEST, the mode in the
6482 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6483 a hard register, just build new versions with the proper mode. If it
6484 is a pseudo, we lose unless it is only time we set the pseudo, in
6485 which case we can safely change its mode. */
6486 if (compare_mode != GET_MODE (dest))
6488 if (can_change_dest_mode (dest, 0, compare_mode))
6490 unsigned int regno = REGNO (dest);
6491 rtx new_dest;
6493 if (regno < FIRST_PSEUDO_REGISTER)
6494 new_dest = gen_rtx_REG (compare_mode, regno);
6495 else
6497 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6498 new_dest = regno_reg_rtx[regno];
6501 SUBST (SET_DEST (x), new_dest);
6502 SUBST (XEXP (*cc_use, 0), new_dest);
6503 other_changed = 1;
6505 dest = new_dest;
6508 #endif /* cc0 */
6509 #endif /* SELECT_CC_MODE */
6511 /* If the code changed, we have to build a new comparison in
6512 undobuf.other_insn. */
6513 if (new_code != old_code)
6515 int other_changed_previously = other_changed;
6516 unsigned HOST_WIDE_INT mask;
6517 rtx old_cc_use = *cc_use;
6519 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6520 dest, const0_rtx));
6521 other_changed = 1;
6523 /* If the only change we made was to change an EQ into an NE or
6524 vice versa, OP0 has only one bit that might be nonzero, and OP1
6525 is zero, check if changing the user of the condition code will
6526 produce a valid insn. If it won't, we can keep the original code
6527 in that insn by surrounding our operation with an XOR. */
6529 if (((old_code == NE && new_code == EQ)
6530 || (old_code == EQ && new_code == NE))
6531 && ! other_changed_previously && op1 == const0_rtx
6532 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6533 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6535 rtx pat = PATTERN (other_insn), note = 0;
6537 if ((recog_for_combine (&pat, other_insn, &note) < 0
6538 && ! check_asm_operands (pat)))
6540 *cc_use = old_cc_use;
6541 other_changed = 0;
6543 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6544 gen_int_mode (mask,
6545 GET_MODE (op0)));
6550 if (other_changed)
6551 undobuf.other_insn = other_insn;
6553 /* Otherwise, if we didn't previously have a COMPARE in the
6554 correct mode, we need one. */
6555 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6557 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6558 src = SET_SRC (x);
6560 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6562 SUBST (SET_SRC (x), op0);
6563 src = SET_SRC (x);
6565 /* Otherwise, update the COMPARE if needed. */
6566 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6568 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6569 src = SET_SRC (x);
6572 else
6574 /* Get SET_SRC in a form where we have placed back any
6575 compound expressions. Then do the checks below. */
6576 src = make_compound_operation (src, SET);
6577 SUBST (SET_SRC (x), src);
6580 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6581 and X being a REG or (subreg (reg)), we may be able to convert this to
6582 (set (subreg:m2 x) (op)).
6584 We can always do this if M1 is narrower than M2 because that means that
6585 we only care about the low bits of the result.
6587 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6588 perform a narrower operation than requested since the high-order bits will
6589 be undefined. On machine where it is defined, this transformation is safe
6590 as long as M1 and M2 have the same number of words. */
6592 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6593 && !OBJECT_P (SUBREG_REG (src))
6594 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6595 / UNITS_PER_WORD)
6596 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6597 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6598 #ifndef WORD_REGISTER_OPERATIONS
6599 && (GET_MODE_SIZE (GET_MODE (src))
6600 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6601 #endif
6602 #ifdef CANNOT_CHANGE_MODE_CLASS
6603 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6604 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6605 GET_MODE (SUBREG_REG (src)),
6606 GET_MODE (src)))
6607 #endif
6608 && (REG_P (dest)
6609 || (GET_CODE (dest) == SUBREG
6610 && REG_P (SUBREG_REG (dest)))))
6612 SUBST (SET_DEST (x),
6613 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6614 dest));
6615 SUBST (SET_SRC (x), SUBREG_REG (src));
6617 src = SET_SRC (x), dest = SET_DEST (x);
6620 #ifdef HAVE_cc0
6621 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6622 in SRC. */
6623 if (dest == cc0_rtx
6624 && GET_CODE (src) == SUBREG
6625 && subreg_lowpart_p (src)
6626 && (GET_MODE_PRECISION (GET_MODE (src))
6627 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6629 rtx inner = SUBREG_REG (src);
6630 machine_mode inner_mode = GET_MODE (inner);
6632 /* Here we make sure that we don't have a sign bit on. */
6633 if (val_signbit_known_clear_p (GET_MODE (src),
6634 nonzero_bits (inner, inner_mode)))
6636 SUBST (SET_SRC (x), inner);
6637 src = SET_SRC (x);
6640 #endif
6642 #ifdef LOAD_EXTEND_OP
6643 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6644 would require a paradoxical subreg. Replace the subreg with a
6645 zero_extend to avoid the reload that would otherwise be required. */
6647 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6648 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6649 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6650 && SUBREG_BYTE (src) == 0
6651 && paradoxical_subreg_p (src)
6652 && MEM_P (SUBREG_REG (src)))
6654 SUBST (SET_SRC (x),
6655 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6656 GET_MODE (src), SUBREG_REG (src)));
6658 src = SET_SRC (x);
6660 #endif
6662 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6663 are comparing an item known to be 0 or -1 against 0, use a logical
6664 operation instead. Check for one of the arms being an IOR of the other
6665 arm with some value. We compute three terms to be IOR'ed together. In
6666 practice, at most two will be nonzero. Then we do the IOR's. */
6668 if (GET_CODE (dest) != PC
6669 && GET_CODE (src) == IF_THEN_ELSE
6670 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6671 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6672 && XEXP (XEXP (src, 0), 1) == const0_rtx
6673 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6674 #ifdef HAVE_conditional_move
6675 && ! can_conditionally_move_p (GET_MODE (src))
6676 #endif
6677 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6678 GET_MODE (XEXP (XEXP (src, 0), 0)))
6679 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6680 && ! side_effects_p (src))
6682 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6683 ? XEXP (src, 1) : XEXP (src, 2));
6684 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6685 ? XEXP (src, 2) : XEXP (src, 1));
6686 rtx term1 = const0_rtx, term2, term3;
6688 if (GET_CODE (true_rtx) == IOR
6689 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6690 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6691 else if (GET_CODE (true_rtx) == IOR
6692 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6693 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6694 else if (GET_CODE (false_rtx) == IOR
6695 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6696 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6697 else if (GET_CODE (false_rtx) == IOR
6698 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6699 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6701 term2 = simplify_gen_binary (AND, GET_MODE (src),
6702 XEXP (XEXP (src, 0), 0), true_rtx);
6703 term3 = simplify_gen_binary (AND, GET_MODE (src),
6704 simplify_gen_unary (NOT, GET_MODE (src),
6705 XEXP (XEXP (src, 0), 0),
6706 GET_MODE (src)),
6707 false_rtx);
6709 SUBST (SET_SRC (x),
6710 simplify_gen_binary (IOR, GET_MODE (src),
6711 simplify_gen_binary (IOR, GET_MODE (src),
6712 term1, term2),
6713 term3));
6715 src = SET_SRC (x);
6718 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6719 whole thing fail. */
6720 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6721 return src;
6722 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6723 return dest;
6724 else
6725 /* Convert this into a field assignment operation, if possible. */
6726 return make_field_assignment (x);
6729 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6730 result. */
6732 static rtx
6733 simplify_logical (rtx x)
6735 machine_mode mode = GET_MODE (x);
6736 rtx op0 = XEXP (x, 0);
6737 rtx op1 = XEXP (x, 1);
6739 switch (GET_CODE (x))
6741 case AND:
6742 /* We can call simplify_and_const_int only if we don't lose
6743 any (sign) bits when converting INTVAL (op1) to
6744 "unsigned HOST_WIDE_INT". */
6745 if (CONST_INT_P (op1)
6746 && (HWI_COMPUTABLE_MODE_P (mode)
6747 || INTVAL (op1) > 0))
6749 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6750 if (GET_CODE (x) != AND)
6751 return x;
6753 op0 = XEXP (x, 0);
6754 op1 = XEXP (x, 1);
6757 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6758 apply the distributive law and then the inverse distributive
6759 law to see if things simplify. */
6760 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6762 rtx result = distribute_and_simplify_rtx (x, 0);
6763 if (result)
6764 return result;
6766 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6768 rtx result = distribute_and_simplify_rtx (x, 1);
6769 if (result)
6770 return result;
6772 break;
6774 case IOR:
6775 /* If we have (ior (and A B) C), apply the distributive law and then
6776 the inverse distributive law to see if things simplify. */
6778 if (GET_CODE (op0) == AND)
6780 rtx result = distribute_and_simplify_rtx (x, 0);
6781 if (result)
6782 return result;
6785 if (GET_CODE (op1) == AND)
6787 rtx result = distribute_and_simplify_rtx (x, 1);
6788 if (result)
6789 return result;
6791 break;
6793 default:
6794 gcc_unreachable ();
6797 return x;
6800 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6801 operations" because they can be replaced with two more basic operations.
6802 ZERO_EXTEND is also considered "compound" because it can be replaced with
6803 an AND operation, which is simpler, though only one operation.
6805 The function expand_compound_operation is called with an rtx expression
6806 and will convert it to the appropriate shifts and AND operations,
6807 simplifying at each stage.
6809 The function make_compound_operation is called to convert an expression
6810 consisting of shifts and ANDs into the equivalent compound expression.
6811 It is the inverse of this function, loosely speaking. */
6813 static rtx
6814 expand_compound_operation (rtx x)
6816 unsigned HOST_WIDE_INT pos = 0, len;
6817 int unsignedp = 0;
6818 unsigned int modewidth;
6819 rtx tem;
6821 switch (GET_CODE (x))
6823 case ZERO_EXTEND:
6824 unsignedp = 1;
6825 case SIGN_EXTEND:
6826 /* We can't necessarily use a const_int for a multiword mode;
6827 it depends on implicitly extending the value.
6828 Since we don't know the right way to extend it,
6829 we can't tell whether the implicit way is right.
6831 Even for a mode that is no wider than a const_int,
6832 we can't win, because we need to sign extend one of its bits through
6833 the rest of it, and we don't know which bit. */
6834 if (CONST_INT_P (XEXP (x, 0)))
6835 return x;
6837 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6838 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6839 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6840 reloaded. If not for that, MEM's would very rarely be safe.
6842 Reject MODEs bigger than a word, because we might not be able
6843 to reference a two-register group starting with an arbitrary register
6844 (and currently gen_lowpart might crash for a SUBREG). */
6846 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6847 return x;
6849 /* Reject MODEs that aren't scalar integers because turning vector
6850 or complex modes into shifts causes problems. */
6852 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6853 return x;
6855 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6856 /* If the inner object has VOIDmode (the only way this can happen
6857 is if it is an ASM_OPERANDS), we can't do anything since we don't
6858 know how much masking to do. */
6859 if (len == 0)
6860 return x;
6862 break;
6864 case ZERO_EXTRACT:
6865 unsignedp = 1;
6867 /* ... fall through ... */
6869 case SIGN_EXTRACT:
6870 /* If the operand is a CLOBBER, just return it. */
6871 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6872 return XEXP (x, 0);
6874 if (!CONST_INT_P (XEXP (x, 1))
6875 || !CONST_INT_P (XEXP (x, 2))
6876 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6877 return x;
6879 /* Reject MODEs that aren't scalar integers because turning vector
6880 or complex modes into shifts causes problems. */
6882 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6883 return x;
6885 len = INTVAL (XEXP (x, 1));
6886 pos = INTVAL (XEXP (x, 2));
6888 /* This should stay within the object being extracted, fail otherwise. */
6889 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6890 return x;
6892 if (BITS_BIG_ENDIAN)
6893 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6895 break;
6897 default:
6898 return x;
6900 /* Convert sign extension to zero extension, if we know that the high
6901 bit is not set, as this is easier to optimize. It will be converted
6902 back to cheaper alternative in make_extraction. */
6903 if (GET_CODE (x) == SIGN_EXTEND
6904 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6905 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6906 & ~(((unsigned HOST_WIDE_INT)
6907 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6908 >> 1))
6909 == 0)))
6911 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6912 rtx temp2 = expand_compound_operation (temp);
6914 /* Make sure this is a profitable operation. */
6915 if (set_src_cost (x, optimize_this_for_speed_p)
6916 > set_src_cost (temp2, optimize_this_for_speed_p))
6917 return temp2;
6918 else if (set_src_cost (x, optimize_this_for_speed_p)
6919 > set_src_cost (temp, optimize_this_for_speed_p))
6920 return temp;
6921 else
6922 return x;
6925 /* We can optimize some special cases of ZERO_EXTEND. */
6926 if (GET_CODE (x) == ZERO_EXTEND)
6928 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6929 know that the last value didn't have any inappropriate bits
6930 set. */
6931 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6932 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6933 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6934 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6935 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6936 return XEXP (XEXP (x, 0), 0);
6938 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6939 if (GET_CODE (XEXP (x, 0)) == SUBREG
6940 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6941 && subreg_lowpart_p (XEXP (x, 0))
6942 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6943 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6944 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6945 return SUBREG_REG (XEXP (x, 0));
6947 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6948 is a comparison and STORE_FLAG_VALUE permits. This is like
6949 the first case, but it works even when GET_MODE (x) is larger
6950 than HOST_WIDE_INT. */
6951 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6952 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6953 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6954 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6955 <= HOST_BITS_PER_WIDE_INT)
6956 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6957 return XEXP (XEXP (x, 0), 0);
6959 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6960 if (GET_CODE (XEXP (x, 0)) == SUBREG
6961 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6962 && subreg_lowpart_p (XEXP (x, 0))
6963 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6964 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6965 <= HOST_BITS_PER_WIDE_INT)
6966 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6967 return SUBREG_REG (XEXP (x, 0));
6971 /* If we reach here, we want to return a pair of shifts. The inner
6972 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6973 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6974 logical depending on the value of UNSIGNEDP.
6976 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6977 converted into an AND of a shift.
6979 We must check for the case where the left shift would have a negative
6980 count. This can happen in a case like (x >> 31) & 255 on machines
6981 that can't shift by a constant. On those machines, we would first
6982 combine the shift with the AND to produce a variable-position
6983 extraction. Then the constant of 31 would be substituted in
6984 to produce such a position. */
6986 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6987 if (modewidth >= pos + len)
6989 machine_mode mode = GET_MODE (x);
6990 tem = gen_lowpart (mode, XEXP (x, 0));
6991 if (!tem || GET_CODE (tem) == CLOBBER)
6992 return x;
6993 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6994 tem, modewidth - pos - len);
6995 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6996 mode, tem, modewidth - len);
6998 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6999 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7000 simplify_shift_const (NULL_RTX, LSHIFTRT,
7001 GET_MODE (x),
7002 XEXP (x, 0), pos),
7003 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7004 else
7005 /* Any other cases we can't handle. */
7006 return x;
7008 /* If we couldn't do this for some reason, return the original
7009 expression. */
7010 if (GET_CODE (tem) == CLOBBER)
7011 return x;
7013 return tem;
7016 /* X is a SET which contains an assignment of one object into
7017 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7018 or certain SUBREGS). If possible, convert it into a series of
7019 logical operations.
7021 We half-heartedly support variable positions, but do not at all
7022 support variable lengths. */
7024 static const_rtx
7025 expand_field_assignment (const_rtx x)
7027 rtx inner;
7028 rtx pos; /* Always counts from low bit. */
7029 int len;
7030 rtx mask, cleared, masked;
7031 machine_mode compute_mode;
7033 /* Loop until we find something we can't simplify. */
7034 while (1)
7036 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7037 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7039 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7040 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7041 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7043 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7044 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7046 inner = XEXP (SET_DEST (x), 0);
7047 len = INTVAL (XEXP (SET_DEST (x), 1));
7048 pos = XEXP (SET_DEST (x), 2);
7050 /* A constant position should stay within the width of INNER. */
7051 if (CONST_INT_P (pos)
7052 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7053 break;
7055 if (BITS_BIG_ENDIAN)
7057 if (CONST_INT_P (pos))
7058 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7059 - INTVAL (pos));
7060 else if (GET_CODE (pos) == MINUS
7061 && CONST_INT_P (XEXP (pos, 1))
7062 && (INTVAL (XEXP (pos, 1))
7063 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7064 /* If position is ADJUST - X, new position is X. */
7065 pos = XEXP (pos, 0);
7066 else
7068 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7069 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7070 gen_int_mode (prec - len,
7071 GET_MODE (pos)),
7072 pos);
7077 /* A SUBREG between two modes that occupy the same numbers of words
7078 can be done by moving the SUBREG to the source. */
7079 else if (GET_CODE (SET_DEST (x)) == SUBREG
7080 /* We need SUBREGs to compute nonzero_bits properly. */
7081 && nonzero_sign_valid
7082 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7083 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7084 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7085 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7087 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7088 gen_lowpart
7089 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7090 SET_SRC (x)));
7091 continue;
7093 else
7094 break;
7096 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7097 inner = SUBREG_REG (inner);
7099 compute_mode = GET_MODE (inner);
7101 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7102 if (! SCALAR_INT_MODE_P (compute_mode))
7104 machine_mode imode;
7106 /* Don't do anything for vector or complex integral types. */
7107 if (! FLOAT_MODE_P (compute_mode))
7108 break;
7110 /* Try to find an integral mode to pun with. */
7111 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7112 if (imode == BLKmode)
7113 break;
7115 compute_mode = imode;
7116 inner = gen_lowpart (imode, inner);
7119 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7120 if (len >= HOST_BITS_PER_WIDE_INT)
7121 break;
7123 /* Now compute the equivalent expression. Make a copy of INNER
7124 for the SET_DEST in case it is a MEM into which we will substitute;
7125 we don't want shared RTL in that case. */
7126 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7127 compute_mode);
7128 cleared = simplify_gen_binary (AND, compute_mode,
7129 simplify_gen_unary (NOT, compute_mode,
7130 simplify_gen_binary (ASHIFT,
7131 compute_mode,
7132 mask, pos),
7133 compute_mode),
7134 inner);
7135 masked = simplify_gen_binary (ASHIFT, compute_mode,
7136 simplify_gen_binary (
7137 AND, compute_mode,
7138 gen_lowpart (compute_mode, SET_SRC (x)),
7139 mask),
7140 pos);
7142 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7143 simplify_gen_binary (IOR, compute_mode,
7144 cleared, masked));
7147 return x;
7150 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7151 it is an RTX that represents the (variable) starting position; otherwise,
7152 POS is the (constant) starting bit position. Both are counted from the LSB.
7154 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7156 IN_DEST is nonzero if this is a reference in the destination of a SET.
7157 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7158 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7159 be used.
7161 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7162 ZERO_EXTRACT should be built even for bits starting at bit 0.
7164 MODE is the desired mode of the result (if IN_DEST == 0).
7166 The result is an RTX for the extraction or NULL_RTX if the target
7167 can't handle it. */
7169 static rtx
7170 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7171 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7172 int in_dest, int in_compare)
7174 /* This mode describes the size of the storage area
7175 to fetch the overall value from. Within that, we
7176 ignore the POS lowest bits, etc. */
7177 machine_mode is_mode = GET_MODE (inner);
7178 machine_mode inner_mode;
7179 machine_mode wanted_inner_mode;
7180 machine_mode wanted_inner_reg_mode = word_mode;
7181 machine_mode pos_mode = word_mode;
7182 machine_mode extraction_mode = word_mode;
7183 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7184 rtx new_rtx = 0;
7185 rtx orig_pos_rtx = pos_rtx;
7186 HOST_WIDE_INT orig_pos;
7188 if (pos_rtx && CONST_INT_P (pos_rtx))
7189 pos = INTVAL (pos_rtx), pos_rtx = 0;
7191 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7193 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7194 consider just the QI as the memory to extract from.
7195 The subreg adds or removes high bits; its mode is
7196 irrelevant to the meaning of this extraction,
7197 since POS and LEN count from the lsb. */
7198 if (MEM_P (SUBREG_REG (inner)))
7199 is_mode = GET_MODE (SUBREG_REG (inner));
7200 inner = SUBREG_REG (inner);
7202 else if (GET_CODE (inner) == ASHIFT
7203 && CONST_INT_P (XEXP (inner, 1))
7204 && pos_rtx == 0 && pos == 0
7205 && len > UINTVAL (XEXP (inner, 1)))
7207 /* We're extracting the least significant bits of an rtx
7208 (ashift X (const_int C)), where LEN > C. Extract the
7209 least significant (LEN - C) bits of X, giving an rtx
7210 whose mode is MODE, then shift it left C times. */
7211 new_rtx = make_extraction (mode, XEXP (inner, 0),
7212 0, 0, len - INTVAL (XEXP (inner, 1)),
7213 unsignedp, in_dest, in_compare);
7214 if (new_rtx != 0)
7215 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7217 else if (GET_CODE (inner) == TRUNCATE)
7218 inner = XEXP (inner, 0);
7220 inner_mode = GET_MODE (inner);
7222 /* See if this can be done without an extraction. We never can if the
7223 width of the field is not the same as that of some integer mode. For
7224 registers, we can only avoid the extraction if the position is at the
7225 low-order bit and this is either not in the destination or we have the
7226 appropriate STRICT_LOW_PART operation available.
7228 For MEM, we can avoid an extract if the field starts on an appropriate
7229 boundary and we can change the mode of the memory reference. */
7231 if (tmode != BLKmode
7232 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7233 && !MEM_P (inner)
7234 && (inner_mode == tmode
7235 || !REG_P (inner)
7236 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7237 || reg_truncated_to_mode (tmode, inner))
7238 && (! in_dest
7239 || (REG_P (inner)
7240 && have_insn_for (STRICT_LOW_PART, tmode))))
7241 || (MEM_P (inner) && pos_rtx == 0
7242 && (pos
7243 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7244 : BITS_PER_UNIT)) == 0
7245 /* We can't do this if we are widening INNER_MODE (it
7246 may not be aligned, for one thing). */
7247 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7248 && (inner_mode == tmode
7249 || (! mode_dependent_address_p (XEXP (inner, 0),
7250 MEM_ADDR_SPACE (inner))
7251 && ! MEM_VOLATILE_P (inner))))))
7253 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7254 field. If the original and current mode are the same, we need not
7255 adjust the offset. Otherwise, we do if bytes big endian.
7257 If INNER is not a MEM, get a piece consisting of just the field
7258 of interest (in this case POS % BITS_PER_WORD must be 0). */
7260 if (MEM_P (inner))
7262 HOST_WIDE_INT offset;
7264 /* POS counts from lsb, but make OFFSET count in memory order. */
7265 if (BYTES_BIG_ENDIAN)
7266 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7267 else
7268 offset = pos / BITS_PER_UNIT;
7270 new_rtx = adjust_address_nv (inner, tmode, offset);
7272 else if (REG_P (inner))
7274 if (tmode != inner_mode)
7276 /* We can't call gen_lowpart in a DEST since we
7277 always want a SUBREG (see below) and it would sometimes
7278 return a new hard register. */
7279 if (pos || in_dest)
7281 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7283 if (WORDS_BIG_ENDIAN
7284 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7285 final_word = ((GET_MODE_SIZE (inner_mode)
7286 - GET_MODE_SIZE (tmode))
7287 / UNITS_PER_WORD) - final_word;
7289 final_word *= UNITS_PER_WORD;
7290 if (BYTES_BIG_ENDIAN &&
7291 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7292 final_word += (GET_MODE_SIZE (inner_mode)
7293 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7295 /* Avoid creating invalid subregs, for example when
7296 simplifying (x>>32)&255. */
7297 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7298 return NULL_RTX;
7300 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7302 else
7303 new_rtx = gen_lowpart (tmode, inner);
7305 else
7306 new_rtx = inner;
7308 else
7309 new_rtx = force_to_mode (inner, tmode,
7310 len >= HOST_BITS_PER_WIDE_INT
7311 ? ~(unsigned HOST_WIDE_INT) 0
7312 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7315 /* If this extraction is going into the destination of a SET,
7316 make a STRICT_LOW_PART unless we made a MEM. */
7318 if (in_dest)
7319 return (MEM_P (new_rtx) ? new_rtx
7320 : (GET_CODE (new_rtx) != SUBREG
7321 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7322 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7324 if (mode == tmode)
7325 return new_rtx;
7327 if (CONST_SCALAR_INT_P (new_rtx))
7328 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7329 mode, new_rtx, tmode);
7331 /* If we know that no extraneous bits are set, and that the high
7332 bit is not set, convert the extraction to the cheaper of
7333 sign and zero extension, that are equivalent in these cases. */
7334 if (flag_expensive_optimizations
7335 && (HWI_COMPUTABLE_MODE_P (tmode)
7336 && ((nonzero_bits (new_rtx, tmode)
7337 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7338 == 0)))
7340 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7341 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7343 /* Prefer ZERO_EXTENSION, since it gives more information to
7344 backends. */
7345 if (set_src_cost (temp, optimize_this_for_speed_p)
7346 <= set_src_cost (temp1, optimize_this_for_speed_p))
7347 return temp;
7348 return temp1;
7351 /* Otherwise, sign- or zero-extend unless we already are in the
7352 proper mode. */
7354 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7355 mode, new_rtx));
7358 /* Unless this is a COMPARE or we have a funny memory reference,
7359 don't do anything with zero-extending field extracts starting at
7360 the low-order bit since they are simple AND operations. */
7361 if (pos_rtx == 0 && pos == 0 && ! in_dest
7362 && ! in_compare && unsignedp)
7363 return 0;
7365 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7366 if the position is not a constant and the length is not 1. In all
7367 other cases, we would only be going outside our object in cases when
7368 an original shift would have been undefined. */
7369 if (MEM_P (inner)
7370 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7371 || (pos_rtx != 0 && len != 1)))
7372 return 0;
7374 enum extraction_pattern pattern = (in_dest ? EP_insv
7375 : unsignedp ? EP_extzv : EP_extv);
7377 /* If INNER is not from memory, we want it to have the mode of a register
7378 extraction pattern's structure operand, or word_mode if there is no
7379 such pattern. The same applies to extraction_mode and pos_mode
7380 and their respective operands.
7382 For memory, assume that the desired extraction_mode and pos_mode
7383 are the same as for a register operation, since at present we don't
7384 have named patterns for aligned memory structures. */
7385 struct extraction_insn insn;
7386 if (get_best_reg_extraction_insn (&insn, pattern,
7387 GET_MODE_BITSIZE (inner_mode), mode))
7389 wanted_inner_reg_mode = insn.struct_mode;
7390 pos_mode = insn.pos_mode;
7391 extraction_mode = insn.field_mode;
7394 /* Never narrow an object, since that might not be safe. */
7396 if (mode != VOIDmode
7397 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7398 extraction_mode = mode;
7400 if (!MEM_P (inner))
7401 wanted_inner_mode = wanted_inner_reg_mode;
7402 else
7404 /* Be careful not to go beyond the extracted object and maintain the
7405 natural alignment of the memory. */
7406 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7407 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7408 > GET_MODE_BITSIZE (wanted_inner_mode))
7410 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7411 gcc_assert (wanted_inner_mode != VOIDmode);
7415 orig_pos = pos;
7417 if (BITS_BIG_ENDIAN)
7419 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7420 BITS_BIG_ENDIAN style. If position is constant, compute new
7421 position. Otherwise, build subtraction.
7422 Note that POS is relative to the mode of the original argument.
7423 If it's a MEM we need to recompute POS relative to that.
7424 However, if we're extracting from (or inserting into) a register,
7425 we want to recompute POS relative to wanted_inner_mode. */
7426 int width = (MEM_P (inner)
7427 ? GET_MODE_BITSIZE (is_mode)
7428 : GET_MODE_BITSIZE (wanted_inner_mode));
7430 if (pos_rtx == 0)
7431 pos = width - len - pos;
7432 else
7433 pos_rtx
7434 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7435 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7436 pos_rtx);
7437 /* POS may be less than 0 now, but we check for that below.
7438 Note that it can only be less than 0 if !MEM_P (inner). */
7441 /* If INNER has a wider mode, and this is a constant extraction, try to
7442 make it smaller and adjust the byte to point to the byte containing
7443 the value. */
7444 if (wanted_inner_mode != VOIDmode
7445 && inner_mode != wanted_inner_mode
7446 && ! pos_rtx
7447 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7448 && MEM_P (inner)
7449 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7450 && ! MEM_VOLATILE_P (inner))
7452 int offset = 0;
7454 /* The computations below will be correct if the machine is big
7455 endian in both bits and bytes or little endian in bits and bytes.
7456 If it is mixed, we must adjust. */
7458 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7459 adjust OFFSET to compensate. */
7460 if (BYTES_BIG_ENDIAN
7461 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7462 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7464 /* We can now move to the desired byte. */
7465 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7466 * GET_MODE_SIZE (wanted_inner_mode);
7467 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7469 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7470 && is_mode != wanted_inner_mode)
7471 offset = (GET_MODE_SIZE (is_mode)
7472 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7474 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7477 /* If INNER is not memory, get it into the proper mode. If we are changing
7478 its mode, POS must be a constant and smaller than the size of the new
7479 mode. */
7480 else if (!MEM_P (inner))
7482 /* On the LHS, don't create paradoxical subregs implicitely truncating
7483 the register unless TRULY_NOOP_TRUNCATION. */
7484 if (in_dest
7485 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7486 wanted_inner_mode))
7487 return NULL_RTX;
7489 if (GET_MODE (inner) != wanted_inner_mode
7490 && (pos_rtx != 0
7491 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7492 return NULL_RTX;
7494 if (orig_pos < 0)
7495 return NULL_RTX;
7497 inner = force_to_mode (inner, wanted_inner_mode,
7498 pos_rtx
7499 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7500 ? ~(unsigned HOST_WIDE_INT) 0
7501 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7502 << orig_pos),
7506 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7507 have to zero extend. Otherwise, we can just use a SUBREG. */
7508 if (pos_rtx != 0
7509 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7511 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7512 GET_MODE (pos_rtx));
7514 /* If we know that no extraneous bits are set, and that the high
7515 bit is not set, convert extraction to cheaper one - either
7516 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7517 cases. */
7518 if (flag_expensive_optimizations
7519 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7520 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7521 & ~(((unsigned HOST_WIDE_INT)
7522 GET_MODE_MASK (GET_MODE (pos_rtx)))
7523 >> 1))
7524 == 0)))
7526 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7527 GET_MODE (pos_rtx));
7529 /* Prefer ZERO_EXTENSION, since it gives more information to
7530 backends. */
7531 if (set_src_cost (temp1, optimize_this_for_speed_p)
7532 < set_src_cost (temp, optimize_this_for_speed_p))
7533 temp = temp1;
7535 pos_rtx = temp;
7538 /* Make POS_RTX unless we already have it and it is correct. If we don't
7539 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7540 be a CONST_INT. */
7541 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7542 pos_rtx = orig_pos_rtx;
7544 else if (pos_rtx == 0)
7545 pos_rtx = GEN_INT (pos);
7547 /* Make the required operation. See if we can use existing rtx. */
7548 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7549 extraction_mode, inner, GEN_INT (len), pos_rtx);
7550 if (! in_dest)
7551 new_rtx = gen_lowpart (mode, new_rtx);
7553 return new_rtx;
7556 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7557 with any other operations in X. Return X without that shift if so. */
7559 static rtx
7560 extract_left_shift (rtx x, int count)
7562 enum rtx_code code = GET_CODE (x);
7563 machine_mode mode = GET_MODE (x);
7564 rtx tem;
7566 switch (code)
7568 case ASHIFT:
7569 /* This is the shift itself. If it is wide enough, we will return
7570 either the value being shifted if the shift count is equal to
7571 COUNT or a shift for the difference. */
7572 if (CONST_INT_P (XEXP (x, 1))
7573 && INTVAL (XEXP (x, 1)) >= count)
7574 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7575 INTVAL (XEXP (x, 1)) - count);
7576 break;
7578 case NEG: case NOT:
7579 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7580 return simplify_gen_unary (code, mode, tem, mode);
7582 break;
7584 case PLUS: case IOR: case XOR: case AND:
7585 /* If we can safely shift this constant and we find the inner shift,
7586 make a new operation. */
7587 if (CONST_INT_P (XEXP (x, 1))
7588 && (UINTVAL (XEXP (x, 1))
7589 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7590 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7592 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7593 return simplify_gen_binary (code, mode, tem,
7594 gen_int_mode (val, mode));
7596 break;
7598 default:
7599 break;
7602 return 0;
7605 /* Look at the expression rooted at X. Look for expressions
7606 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7607 Form these expressions.
7609 Return the new rtx, usually just X.
7611 Also, for machines like the VAX that don't have logical shift insns,
7612 try to convert logical to arithmetic shift operations in cases where
7613 they are equivalent. This undoes the canonicalizations to logical
7614 shifts done elsewhere.
7616 We try, as much as possible, to re-use rtl expressions to save memory.
7618 IN_CODE says what kind of expression we are processing. Normally, it is
7619 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7620 being kludges), it is MEM. When processing the arguments of a comparison
7621 or a COMPARE against zero, it is COMPARE. */
7624 make_compound_operation (rtx x, enum rtx_code in_code)
7626 enum rtx_code code = GET_CODE (x);
7627 machine_mode mode = GET_MODE (x);
7628 int mode_width = GET_MODE_PRECISION (mode);
7629 rtx rhs, lhs;
7630 enum rtx_code next_code;
7631 int i, j;
7632 rtx new_rtx = 0;
7633 rtx tem;
7634 const char *fmt;
7636 /* Select the code to be used in recursive calls. Once we are inside an
7637 address, we stay there. If we have a comparison, set to COMPARE,
7638 but once inside, go back to our default of SET. */
7640 next_code = (code == MEM ? MEM
7641 : ((code == PLUS || code == MINUS)
7642 && SCALAR_INT_MODE_P (mode)) ? MEM
7643 : ((code == COMPARE || COMPARISON_P (x))
7644 && XEXP (x, 1) == const0_rtx) ? COMPARE
7645 : in_code == COMPARE ? SET : in_code);
7647 /* Process depending on the code of this operation. If NEW is set
7648 nonzero, it will be returned. */
7650 switch (code)
7652 case ASHIFT:
7653 /* Convert shifts by constants into multiplications if inside
7654 an address. */
7655 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7656 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7657 && INTVAL (XEXP (x, 1)) >= 0
7658 && SCALAR_INT_MODE_P (mode))
7660 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7661 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7663 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7664 if (GET_CODE (new_rtx) == NEG)
7666 new_rtx = XEXP (new_rtx, 0);
7667 multval = -multval;
7669 multval = trunc_int_for_mode (multval, mode);
7670 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7672 break;
7674 case PLUS:
7675 lhs = XEXP (x, 0);
7676 rhs = XEXP (x, 1);
7677 lhs = make_compound_operation (lhs, next_code);
7678 rhs = make_compound_operation (rhs, next_code);
7679 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7680 && SCALAR_INT_MODE_P (mode))
7682 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7683 XEXP (lhs, 1));
7684 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7686 else if (GET_CODE (lhs) == MULT
7687 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7689 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7690 simplify_gen_unary (NEG, mode,
7691 XEXP (lhs, 1),
7692 mode));
7693 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7695 else
7697 SUBST (XEXP (x, 0), lhs);
7698 SUBST (XEXP (x, 1), rhs);
7699 goto maybe_swap;
7701 x = gen_lowpart (mode, new_rtx);
7702 goto maybe_swap;
7704 case MINUS:
7705 lhs = XEXP (x, 0);
7706 rhs = XEXP (x, 1);
7707 lhs = make_compound_operation (lhs, next_code);
7708 rhs = make_compound_operation (rhs, next_code);
7709 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7710 && SCALAR_INT_MODE_P (mode))
7712 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7713 XEXP (rhs, 1));
7714 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7716 else if (GET_CODE (rhs) == MULT
7717 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7719 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7720 simplify_gen_unary (NEG, mode,
7721 XEXP (rhs, 1),
7722 mode));
7723 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7725 else
7727 SUBST (XEXP (x, 0), lhs);
7728 SUBST (XEXP (x, 1), rhs);
7729 return x;
7731 return gen_lowpart (mode, new_rtx);
7733 case AND:
7734 /* If the second operand is not a constant, we can't do anything
7735 with it. */
7736 if (!CONST_INT_P (XEXP (x, 1)))
7737 break;
7739 /* If the constant is a power of two minus one and the first operand
7740 is a logical right shift, make an extraction. */
7741 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7742 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7744 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7745 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7746 0, in_code == COMPARE);
7749 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7750 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7751 && subreg_lowpart_p (XEXP (x, 0))
7752 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7753 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7755 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7756 next_code);
7757 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7758 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7759 0, in_code == COMPARE);
7761 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7762 else if ((GET_CODE (XEXP (x, 0)) == XOR
7763 || GET_CODE (XEXP (x, 0)) == IOR)
7764 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7765 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7766 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7768 /* Apply the distributive law, and then try to make extractions. */
7769 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7770 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7771 XEXP (x, 1)),
7772 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7773 XEXP (x, 1)));
7774 new_rtx = make_compound_operation (new_rtx, in_code);
7777 /* If we are have (and (rotate X C) M) and C is larger than the number
7778 of bits in M, this is an extraction. */
7780 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7781 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7782 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7783 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7785 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7786 new_rtx = make_extraction (mode, new_rtx,
7787 (GET_MODE_PRECISION (mode)
7788 - INTVAL (XEXP (XEXP (x, 0), 1))),
7789 NULL_RTX, i, 1, 0, in_code == COMPARE);
7792 /* On machines without logical shifts, if the operand of the AND is
7793 a logical shift and our mask turns off all the propagated sign
7794 bits, we can replace the logical shift with an arithmetic shift. */
7795 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7796 && !have_insn_for (LSHIFTRT, mode)
7797 && have_insn_for (ASHIFTRT, mode)
7798 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7799 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7800 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7801 && mode_width <= HOST_BITS_PER_WIDE_INT)
7803 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7805 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7806 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7807 SUBST (XEXP (x, 0),
7808 gen_rtx_ASHIFTRT (mode,
7809 make_compound_operation
7810 (XEXP (XEXP (x, 0), 0), next_code),
7811 XEXP (XEXP (x, 0), 1)));
7814 /* If the constant is one less than a power of two, this might be
7815 representable by an extraction even if no shift is present.
7816 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7817 we are in a COMPARE. */
7818 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7819 new_rtx = make_extraction (mode,
7820 make_compound_operation (XEXP (x, 0),
7821 next_code),
7822 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7824 /* If we are in a comparison and this is an AND with a power of two,
7825 convert this into the appropriate bit extract. */
7826 else if (in_code == COMPARE
7827 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7828 new_rtx = make_extraction (mode,
7829 make_compound_operation (XEXP (x, 0),
7830 next_code),
7831 i, NULL_RTX, 1, 1, 0, 1);
7833 break;
7835 case LSHIFTRT:
7836 /* If the sign bit is known to be zero, replace this with an
7837 arithmetic shift. */
7838 if (have_insn_for (ASHIFTRT, mode)
7839 && ! have_insn_for (LSHIFTRT, mode)
7840 && mode_width <= HOST_BITS_PER_WIDE_INT
7841 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7843 new_rtx = gen_rtx_ASHIFTRT (mode,
7844 make_compound_operation (XEXP (x, 0),
7845 next_code),
7846 XEXP (x, 1));
7847 break;
7850 /* ... fall through ... */
7852 case ASHIFTRT:
7853 lhs = XEXP (x, 0);
7854 rhs = XEXP (x, 1);
7856 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7857 this is a SIGN_EXTRACT. */
7858 if (CONST_INT_P (rhs)
7859 && GET_CODE (lhs) == ASHIFT
7860 && CONST_INT_P (XEXP (lhs, 1))
7861 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7862 && INTVAL (XEXP (lhs, 1)) >= 0
7863 && INTVAL (rhs) < mode_width)
7865 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7866 new_rtx = make_extraction (mode, new_rtx,
7867 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7868 NULL_RTX, mode_width - INTVAL (rhs),
7869 code == LSHIFTRT, 0, in_code == COMPARE);
7870 break;
7873 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7874 If so, try to merge the shifts into a SIGN_EXTEND. We could
7875 also do this for some cases of SIGN_EXTRACT, but it doesn't
7876 seem worth the effort; the case checked for occurs on Alpha. */
7878 if (!OBJECT_P (lhs)
7879 && ! (GET_CODE (lhs) == SUBREG
7880 && (OBJECT_P (SUBREG_REG (lhs))))
7881 && CONST_INT_P (rhs)
7882 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7883 && INTVAL (rhs) < mode_width
7884 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7885 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7886 0, NULL_RTX, mode_width - INTVAL (rhs),
7887 code == LSHIFTRT, 0, in_code == COMPARE);
7889 break;
7891 case SUBREG:
7892 /* Call ourselves recursively on the inner expression. If we are
7893 narrowing the object and it has a different RTL code from
7894 what it originally did, do this SUBREG as a force_to_mode. */
7896 rtx inner = SUBREG_REG (x), simplified;
7897 enum rtx_code subreg_code = in_code;
7899 /* If in_code is COMPARE, it isn't always safe to pass it through
7900 to the recursive make_compound_operation call. */
7901 if (subreg_code == COMPARE
7902 && (!subreg_lowpart_p (x)
7903 || GET_CODE (inner) == SUBREG
7904 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7905 is (const_int 0), rather than
7906 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7907 || (GET_CODE (inner) == AND
7908 && CONST_INT_P (XEXP (inner, 1))
7909 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7910 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7911 >= GET_MODE_BITSIZE (mode))))
7912 subreg_code = SET;
7914 tem = make_compound_operation (inner, subreg_code);
7916 simplified
7917 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7918 if (simplified)
7919 tem = simplified;
7921 if (GET_CODE (tem) != GET_CODE (inner)
7922 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7923 && subreg_lowpart_p (x))
7925 rtx newer
7926 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7928 /* If we have something other than a SUBREG, we might have
7929 done an expansion, so rerun ourselves. */
7930 if (GET_CODE (newer) != SUBREG)
7931 newer = make_compound_operation (newer, in_code);
7933 /* force_to_mode can expand compounds. If it just re-expanded the
7934 compound, use gen_lowpart to convert to the desired mode. */
7935 if (rtx_equal_p (newer, x)
7936 /* Likewise if it re-expanded the compound only partially.
7937 This happens for SUBREG of ZERO_EXTRACT if they extract
7938 the same number of bits. */
7939 || (GET_CODE (newer) == SUBREG
7940 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7941 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7942 && GET_CODE (inner) == AND
7943 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7944 return gen_lowpart (GET_MODE (x), tem);
7946 return newer;
7949 if (simplified)
7950 return tem;
7952 break;
7954 default:
7955 break;
7958 if (new_rtx)
7960 x = gen_lowpart (mode, new_rtx);
7961 code = GET_CODE (x);
7964 /* Now recursively process each operand of this operation. We need to
7965 handle ZERO_EXTEND specially so that we don't lose track of the
7966 inner mode. */
7967 if (GET_CODE (x) == ZERO_EXTEND)
7969 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7970 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7971 new_rtx, GET_MODE (XEXP (x, 0)));
7972 if (tem)
7973 return tem;
7974 SUBST (XEXP (x, 0), new_rtx);
7975 return x;
7978 fmt = GET_RTX_FORMAT (code);
7979 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7980 if (fmt[i] == 'e')
7982 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7983 SUBST (XEXP (x, i), new_rtx);
7985 else if (fmt[i] == 'E')
7986 for (j = 0; j < XVECLEN (x, i); j++)
7988 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7989 SUBST (XVECEXP (x, i, j), new_rtx);
7992 maybe_swap:
7993 /* If this is a commutative operation, the changes to the operands
7994 may have made it noncanonical. */
7995 if (COMMUTATIVE_ARITH_P (x)
7996 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7998 tem = XEXP (x, 0);
7999 SUBST (XEXP (x, 0), XEXP (x, 1));
8000 SUBST (XEXP (x, 1), tem);
8003 return x;
8006 /* Given M see if it is a value that would select a field of bits
8007 within an item, but not the entire word. Return -1 if not.
8008 Otherwise, return the starting position of the field, where 0 is the
8009 low-order bit.
8011 *PLEN is set to the length of the field. */
8013 static int
8014 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8016 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8017 int pos = m ? ctz_hwi (m) : -1;
8018 int len = 0;
8020 if (pos >= 0)
8021 /* Now shift off the low-order zero bits and see if we have a
8022 power of two minus 1. */
8023 len = exact_log2 ((m >> pos) + 1);
8025 if (len <= 0)
8026 pos = -1;
8028 *plen = len;
8029 return pos;
8032 /* If X refers to a register that equals REG in value, replace these
8033 references with REG. */
8034 static rtx
8035 canon_reg_for_combine (rtx x, rtx reg)
8037 rtx op0, op1, op2;
8038 const char *fmt;
8039 int i;
8040 bool copied;
8042 enum rtx_code code = GET_CODE (x);
8043 switch (GET_RTX_CLASS (code))
8045 case RTX_UNARY:
8046 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8047 if (op0 != XEXP (x, 0))
8048 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8049 GET_MODE (reg));
8050 break;
8052 case RTX_BIN_ARITH:
8053 case RTX_COMM_ARITH:
8054 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8055 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8056 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8057 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8058 break;
8060 case RTX_COMPARE:
8061 case RTX_COMM_COMPARE:
8062 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8063 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8064 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8065 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8066 GET_MODE (op0), op0, op1);
8067 break;
8069 case RTX_TERNARY:
8070 case RTX_BITFIELD_OPS:
8071 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8072 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8073 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8074 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8075 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8076 GET_MODE (op0), op0, op1, op2);
8078 case RTX_OBJ:
8079 if (REG_P (x))
8081 if (rtx_equal_p (get_last_value (reg), x)
8082 || rtx_equal_p (reg, get_last_value (x)))
8083 return reg;
8084 else
8085 break;
8088 /* fall through */
8090 default:
8091 fmt = GET_RTX_FORMAT (code);
8092 copied = false;
8093 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8094 if (fmt[i] == 'e')
8096 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8097 if (op != XEXP (x, i))
8099 if (!copied)
8101 copied = true;
8102 x = copy_rtx (x);
8104 XEXP (x, i) = op;
8107 else if (fmt[i] == 'E')
8109 int j;
8110 for (j = 0; j < XVECLEN (x, i); j++)
8112 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8113 if (op != XVECEXP (x, i, j))
8115 if (!copied)
8117 copied = true;
8118 x = copy_rtx (x);
8120 XVECEXP (x, i, j) = op;
8125 break;
8128 return x;
8131 /* Return X converted to MODE. If the value is already truncated to
8132 MODE we can just return a subreg even though in the general case we
8133 would need an explicit truncation. */
8135 static rtx
8136 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8138 if (!CONST_INT_P (x)
8139 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8140 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8141 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8143 /* Bit-cast X into an integer mode. */
8144 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8145 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8146 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8147 x, GET_MODE (x));
8150 return gen_lowpart (mode, x);
8153 /* See if X can be simplified knowing that we will only refer to it in
8154 MODE and will only refer to those bits that are nonzero in MASK.
8155 If other bits are being computed or if masking operations are done
8156 that select a superset of the bits in MASK, they can sometimes be
8157 ignored.
8159 Return a possibly simplified expression, but always convert X to
8160 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8162 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8163 are all off in X. This is used when X will be complemented, by either
8164 NOT, NEG, or XOR. */
8166 static rtx
8167 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8168 int just_select)
8170 enum rtx_code code = GET_CODE (x);
8171 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8172 machine_mode op_mode;
8173 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8174 rtx op0, op1, temp;
8176 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8177 code below will do the wrong thing since the mode of such an
8178 expression is VOIDmode.
8180 Also do nothing if X is a CLOBBER; this can happen if X was
8181 the return value from a call to gen_lowpart. */
8182 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8183 return x;
8185 /* We want to perform the operation in its present mode unless we know
8186 that the operation is valid in MODE, in which case we do the operation
8187 in MODE. */
8188 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8189 && have_insn_for (code, mode))
8190 ? mode : GET_MODE (x));
8192 /* It is not valid to do a right-shift in a narrower mode
8193 than the one it came in with. */
8194 if ((code == LSHIFTRT || code == ASHIFTRT)
8195 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8196 op_mode = GET_MODE (x);
8198 /* Truncate MASK to fit OP_MODE. */
8199 if (op_mode)
8200 mask &= GET_MODE_MASK (op_mode);
8202 /* When we have an arithmetic operation, or a shift whose count we
8203 do not know, we need to assume that all bits up to the highest-order
8204 bit in MASK will be needed. This is how we form such a mask. */
8205 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8206 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8207 else
8208 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8209 - 1);
8211 /* Determine what bits of X are guaranteed to be (non)zero. */
8212 nonzero = nonzero_bits (x, mode);
8214 /* If none of the bits in X are needed, return a zero. */
8215 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8216 x = const0_rtx;
8218 /* If X is a CONST_INT, return a new one. Do this here since the
8219 test below will fail. */
8220 if (CONST_INT_P (x))
8222 if (SCALAR_INT_MODE_P (mode))
8223 return gen_int_mode (INTVAL (x) & mask, mode);
8224 else
8226 x = GEN_INT (INTVAL (x) & mask);
8227 return gen_lowpart_common (mode, x);
8231 /* If X is narrower than MODE and we want all the bits in X's mode, just
8232 get X in the proper mode. */
8233 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8234 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8235 return gen_lowpart (mode, x);
8237 /* We can ignore the effect of a SUBREG if it narrows the mode or
8238 if the constant masks to zero all the bits the mode doesn't have. */
8239 if (GET_CODE (x) == SUBREG
8240 && subreg_lowpart_p (x)
8241 && ((GET_MODE_SIZE (GET_MODE (x))
8242 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8243 || (0 == (mask
8244 & GET_MODE_MASK (GET_MODE (x))
8245 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8246 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8248 /* The arithmetic simplifications here only work for scalar integer modes. */
8249 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8250 return gen_lowpart_or_truncate (mode, x);
8252 switch (code)
8254 case CLOBBER:
8255 /* If X is a (clobber (const_int)), return it since we know we are
8256 generating something that won't match. */
8257 return x;
8259 case SIGN_EXTEND:
8260 case ZERO_EXTEND:
8261 case ZERO_EXTRACT:
8262 case SIGN_EXTRACT:
8263 x = expand_compound_operation (x);
8264 if (GET_CODE (x) != code)
8265 return force_to_mode (x, mode, mask, next_select);
8266 break;
8268 case TRUNCATE:
8269 /* Similarly for a truncate. */
8270 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8272 case AND:
8273 /* If this is an AND with a constant, convert it into an AND
8274 whose constant is the AND of that constant with MASK. If it
8275 remains an AND of MASK, delete it since it is redundant. */
8277 if (CONST_INT_P (XEXP (x, 1)))
8279 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8280 mask & INTVAL (XEXP (x, 1)));
8282 /* If X is still an AND, see if it is an AND with a mask that
8283 is just some low-order bits. If so, and it is MASK, we don't
8284 need it. */
8286 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8287 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8288 == mask))
8289 x = XEXP (x, 0);
8291 /* If it remains an AND, try making another AND with the bits
8292 in the mode mask that aren't in MASK turned on. If the
8293 constant in the AND is wide enough, this might make a
8294 cheaper constant. */
8296 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8297 && GET_MODE_MASK (GET_MODE (x)) != mask
8298 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8300 unsigned HOST_WIDE_INT cval
8301 = UINTVAL (XEXP (x, 1))
8302 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8303 rtx y;
8305 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8306 gen_int_mode (cval, GET_MODE (x)));
8307 if (set_src_cost (y, optimize_this_for_speed_p)
8308 < set_src_cost (x, optimize_this_for_speed_p))
8309 x = y;
8312 break;
8315 goto binop;
8317 case PLUS:
8318 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8319 low-order bits (as in an alignment operation) and FOO is already
8320 aligned to that boundary, mask C1 to that boundary as well.
8321 This may eliminate that PLUS and, later, the AND. */
8324 unsigned int width = GET_MODE_PRECISION (mode);
8325 unsigned HOST_WIDE_INT smask = mask;
8327 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8328 number, sign extend it. */
8330 if (width < HOST_BITS_PER_WIDE_INT
8331 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8332 smask |= HOST_WIDE_INT_M1U << width;
8334 if (CONST_INT_P (XEXP (x, 1))
8335 && exact_log2 (- smask) >= 0
8336 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8337 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8338 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8339 (INTVAL (XEXP (x, 1)) & smask)),
8340 mode, smask, next_select);
8343 /* ... fall through ... */
8345 case MULT:
8346 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8347 most significant bit in MASK since carries from those bits will
8348 affect the bits we are interested in. */
8349 mask = fuller_mask;
8350 goto binop;
8352 case MINUS:
8353 /* If X is (minus C Y) where C's least set bit is larger than any bit
8354 in the mask, then we may replace with (neg Y). */
8355 if (CONST_INT_P (XEXP (x, 0))
8356 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8358 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8359 GET_MODE (x));
8360 return force_to_mode (x, mode, mask, next_select);
8363 /* Similarly, if C contains every bit in the fuller_mask, then we may
8364 replace with (not Y). */
8365 if (CONST_INT_P (XEXP (x, 0))
8366 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8368 x = simplify_gen_unary (NOT, GET_MODE (x),
8369 XEXP (x, 1), GET_MODE (x));
8370 return force_to_mode (x, mode, mask, next_select);
8373 mask = fuller_mask;
8374 goto binop;
8376 case IOR:
8377 case XOR:
8378 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8379 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8380 operation which may be a bitfield extraction. Ensure that the
8381 constant we form is not wider than the mode of X. */
8383 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8384 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8385 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8386 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8387 && CONST_INT_P (XEXP (x, 1))
8388 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8389 + floor_log2 (INTVAL (XEXP (x, 1))))
8390 < GET_MODE_PRECISION (GET_MODE (x)))
8391 && (UINTVAL (XEXP (x, 1))
8392 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8394 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8395 << INTVAL (XEXP (XEXP (x, 0), 1)),
8396 GET_MODE (x));
8397 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8398 XEXP (XEXP (x, 0), 0), temp);
8399 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8400 XEXP (XEXP (x, 0), 1));
8401 return force_to_mode (x, mode, mask, next_select);
8404 binop:
8405 /* For most binary operations, just propagate into the operation and
8406 change the mode if we have an operation of that mode. */
8408 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8409 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8411 /* If we ended up truncating both operands, truncate the result of the
8412 operation instead. */
8413 if (GET_CODE (op0) == TRUNCATE
8414 && GET_CODE (op1) == TRUNCATE)
8416 op0 = XEXP (op0, 0);
8417 op1 = XEXP (op1, 0);
8420 op0 = gen_lowpart_or_truncate (op_mode, op0);
8421 op1 = gen_lowpart_or_truncate (op_mode, op1);
8423 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8424 x = simplify_gen_binary (code, op_mode, op0, op1);
8425 break;
8427 case ASHIFT:
8428 /* For left shifts, do the same, but just for the first operand.
8429 However, we cannot do anything with shifts where we cannot
8430 guarantee that the counts are smaller than the size of the mode
8431 because such a count will have a different meaning in a
8432 wider mode. */
8434 if (! (CONST_INT_P (XEXP (x, 1))
8435 && INTVAL (XEXP (x, 1)) >= 0
8436 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8437 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8438 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8439 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8440 break;
8442 /* If the shift count is a constant and we can do arithmetic in
8443 the mode of the shift, refine which bits we need. Otherwise, use the
8444 conservative form of the mask. */
8445 if (CONST_INT_P (XEXP (x, 1))
8446 && INTVAL (XEXP (x, 1)) >= 0
8447 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8448 && HWI_COMPUTABLE_MODE_P (op_mode))
8449 mask >>= INTVAL (XEXP (x, 1));
8450 else
8451 mask = fuller_mask;
8453 op0 = gen_lowpart_or_truncate (op_mode,
8454 force_to_mode (XEXP (x, 0), op_mode,
8455 mask, next_select));
8457 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8458 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8459 break;
8461 case LSHIFTRT:
8462 /* Here we can only do something if the shift count is a constant,
8463 this shift constant is valid for the host, and we can do arithmetic
8464 in OP_MODE. */
8466 if (CONST_INT_P (XEXP (x, 1))
8467 && INTVAL (XEXP (x, 1)) >= 0
8468 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8469 && HWI_COMPUTABLE_MODE_P (op_mode))
8471 rtx inner = XEXP (x, 0);
8472 unsigned HOST_WIDE_INT inner_mask;
8474 /* Select the mask of the bits we need for the shift operand. */
8475 inner_mask = mask << INTVAL (XEXP (x, 1));
8477 /* We can only change the mode of the shift if we can do arithmetic
8478 in the mode of the shift and INNER_MASK is no wider than the
8479 width of X's mode. */
8480 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8481 op_mode = GET_MODE (x);
8483 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8485 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8486 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8489 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8490 shift and AND produces only copies of the sign bit (C2 is one less
8491 than a power of two), we can do this with just a shift. */
8493 if (GET_CODE (x) == LSHIFTRT
8494 && CONST_INT_P (XEXP (x, 1))
8495 /* The shift puts one of the sign bit copies in the least significant
8496 bit. */
8497 && ((INTVAL (XEXP (x, 1))
8498 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8499 >= GET_MODE_PRECISION (GET_MODE (x)))
8500 && exact_log2 (mask + 1) >= 0
8501 /* Number of bits left after the shift must be more than the mask
8502 needs. */
8503 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8504 <= GET_MODE_PRECISION (GET_MODE (x)))
8505 /* Must be more sign bit copies than the mask needs. */
8506 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8507 >= exact_log2 (mask + 1)))
8508 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8509 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8510 - exact_log2 (mask + 1)));
8512 goto shiftrt;
8514 case ASHIFTRT:
8515 /* If we are just looking for the sign bit, we don't need this shift at
8516 all, even if it has a variable count. */
8517 if (val_signbit_p (GET_MODE (x), mask))
8518 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8520 /* If this is a shift by a constant, get a mask that contains those bits
8521 that are not copies of the sign bit. We then have two cases: If
8522 MASK only includes those bits, this can be a logical shift, which may
8523 allow simplifications. If MASK is a single-bit field not within
8524 those bits, we are requesting a copy of the sign bit and hence can
8525 shift the sign bit to the appropriate location. */
8527 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8528 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8530 int i;
8532 /* If the considered data is wider than HOST_WIDE_INT, we can't
8533 represent a mask for all its bits in a single scalar.
8534 But we only care about the lower bits, so calculate these. */
8536 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8538 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8540 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8541 is the number of bits a full-width mask would have set.
8542 We need only shift if these are fewer than nonzero can
8543 hold. If not, we must keep all bits set in nonzero. */
8545 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8546 < HOST_BITS_PER_WIDE_INT)
8547 nonzero >>= INTVAL (XEXP (x, 1))
8548 + HOST_BITS_PER_WIDE_INT
8549 - GET_MODE_PRECISION (GET_MODE (x)) ;
8551 else
8553 nonzero = GET_MODE_MASK (GET_MODE (x));
8554 nonzero >>= INTVAL (XEXP (x, 1));
8557 if ((mask & ~nonzero) == 0)
8559 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8560 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8561 if (GET_CODE (x) != ASHIFTRT)
8562 return force_to_mode (x, mode, mask, next_select);
8565 else if ((i = exact_log2 (mask)) >= 0)
8567 x = simplify_shift_const
8568 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8569 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8571 if (GET_CODE (x) != ASHIFTRT)
8572 return force_to_mode (x, mode, mask, next_select);
8576 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8577 even if the shift count isn't a constant. */
8578 if (mask == 1)
8579 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8580 XEXP (x, 0), XEXP (x, 1));
8582 shiftrt:
8584 /* If this is a zero- or sign-extension operation that just affects bits
8585 we don't care about, remove it. Be sure the call above returned
8586 something that is still a shift. */
8588 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8589 && CONST_INT_P (XEXP (x, 1))
8590 && INTVAL (XEXP (x, 1)) >= 0
8591 && (INTVAL (XEXP (x, 1))
8592 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8593 && GET_CODE (XEXP (x, 0)) == ASHIFT
8594 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8595 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8596 next_select);
8598 break;
8600 case ROTATE:
8601 case ROTATERT:
8602 /* If the shift count is constant and we can do computations
8603 in the mode of X, compute where the bits we care about are.
8604 Otherwise, we can't do anything. Don't change the mode of
8605 the shift or propagate MODE into the shift, though. */
8606 if (CONST_INT_P (XEXP (x, 1))
8607 && INTVAL (XEXP (x, 1)) >= 0)
8609 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8610 GET_MODE (x),
8611 gen_int_mode (mask, GET_MODE (x)),
8612 XEXP (x, 1));
8613 if (temp && CONST_INT_P (temp))
8614 x = simplify_gen_binary (code, GET_MODE (x),
8615 force_to_mode (XEXP (x, 0), GET_MODE (x),
8616 INTVAL (temp), next_select),
8617 XEXP (x, 1));
8619 break;
8621 case NEG:
8622 /* If we just want the low-order bit, the NEG isn't needed since it
8623 won't change the low-order bit. */
8624 if (mask == 1)
8625 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8627 /* We need any bits less significant than the most significant bit in
8628 MASK since carries from those bits will affect the bits we are
8629 interested in. */
8630 mask = fuller_mask;
8631 goto unop;
8633 case NOT:
8634 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8635 same as the XOR case above. Ensure that the constant we form is not
8636 wider than the mode of X. */
8638 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8639 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8640 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8641 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8642 < GET_MODE_PRECISION (GET_MODE (x)))
8643 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8645 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8646 GET_MODE (x));
8647 temp = simplify_gen_binary (XOR, GET_MODE (x),
8648 XEXP (XEXP (x, 0), 0), temp);
8649 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8650 temp, XEXP (XEXP (x, 0), 1));
8652 return force_to_mode (x, mode, mask, next_select);
8655 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8656 use the full mask inside the NOT. */
8657 mask = fuller_mask;
8659 unop:
8660 op0 = gen_lowpart_or_truncate (op_mode,
8661 force_to_mode (XEXP (x, 0), mode, mask,
8662 next_select));
8663 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8664 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8665 break;
8667 case NE:
8668 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8669 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8670 which is equal to STORE_FLAG_VALUE. */
8671 if ((mask & ~STORE_FLAG_VALUE) == 0
8672 && XEXP (x, 1) == const0_rtx
8673 && GET_MODE (XEXP (x, 0)) == mode
8674 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8675 && (nonzero_bits (XEXP (x, 0), mode)
8676 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8677 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8679 break;
8681 case IF_THEN_ELSE:
8682 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8683 written in a narrower mode. We play it safe and do not do so. */
8685 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8686 force_to_mode (XEXP (x, 1), mode,
8687 mask, next_select));
8688 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8689 force_to_mode (XEXP (x, 2), mode,
8690 mask, next_select));
8691 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8692 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8693 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8694 op0, op1);
8695 break;
8697 default:
8698 break;
8701 /* Ensure we return a value of the proper mode. */
8702 return gen_lowpart_or_truncate (mode, x);
8705 /* Return nonzero if X is an expression that has one of two values depending on
8706 whether some other value is zero or nonzero. In that case, we return the
8707 value that is being tested, *PTRUE is set to the value if the rtx being
8708 returned has a nonzero value, and *PFALSE is set to the other alternative.
8710 If we return zero, we set *PTRUE and *PFALSE to X. */
8712 static rtx
8713 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8715 machine_mode mode = GET_MODE (x);
8716 enum rtx_code code = GET_CODE (x);
8717 rtx cond0, cond1, true0, true1, false0, false1;
8718 unsigned HOST_WIDE_INT nz;
8720 /* If we are comparing a value against zero, we are done. */
8721 if ((code == NE || code == EQ)
8722 && XEXP (x, 1) == const0_rtx)
8724 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8725 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8726 return XEXP (x, 0);
8729 /* If this is a unary operation whose operand has one of two values, apply
8730 our opcode to compute those values. */
8731 else if (UNARY_P (x)
8732 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8734 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8735 *pfalse = simplify_gen_unary (code, mode, false0,
8736 GET_MODE (XEXP (x, 0)));
8737 return cond0;
8740 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8741 make can't possibly match and would suppress other optimizations. */
8742 else if (code == COMPARE)
8745 /* If this is a binary operation, see if either side has only one of two
8746 values. If either one does or if both do and they are conditional on
8747 the same value, compute the new true and false values. */
8748 else if (BINARY_P (x))
8750 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8751 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8753 if ((cond0 != 0 || cond1 != 0)
8754 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8756 /* If if_then_else_cond returned zero, then true/false are the
8757 same rtl. We must copy one of them to prevent invalid rtl
8758 sharing. */
8759 if (cond0 == 0)
8760 true0 = copy_rtx (true0);
8761 else if (cond1 == 0)
8762 true1 = copy_rtx (true1);
8764 if (COMPARISON_P (x))
8766 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8767 true0, true1);
8768 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8769 false0, false1);
8771 else
8773 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8774 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8777 return cond0 ? cond0 : cond1;
8780 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8781 operands is zero when the other is nonzero, and vice-versa,
8782 and STORE_FLAG_VALUE is 1 or -1. */
8784 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8785 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8786 || code == UMAX)
8787 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8789 rtx op0 = XEXP (XEXP (x, 0), 1);
8790 rtx op1 = XEXP (XEXP (x, 1), 1);
8792 cond0 = XEXP (XEXP (x, 0), 0);
8793 cond1 = XEXP (XEXP (x, 1), 0);
8795 if (COMPARISON_P (cond0)
8796 && COMPARISON_P (cond1)
8797 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8798 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8799 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8800 || ((swap_condition (GET_CODE (cond0))
8801 == reversed_comparison_code (cond1, NULL))
8802 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8803 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8804 && ! side_effects_p (x))
8806 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8807 *pfalse = simplify_gen_binary (MULT, mode,
8808 (code == MINUS
8809 ? simplify_gen_unary (NEG, mode,
8810 op1, mode)
8811 : op1),
8812 const_true_rtx);
8813 return cond0;
8817 /* Similarly for MULT, AND and UMIN, except that for these the result
8818 is always zero. */
8819 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8820 && (code == MULT || code == AND || code == UMIN)
8821 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8823 cond0 = XEXP (XEXP (x, 0), 0);
8824 cond1 = XEXP (XEXP (x, 1), 0);
8826 if (COMPARISON_P (cond0)
8827 && COMPARISON_P (cond1)
8828 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8829 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8830 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8831 || ((swap_condition (GET_CODE (cond0))
8832 == reversed_comparison_code (cond1, NULL))
8833 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8834 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8835 && ! side_effects_p (x))
8837 *ptrue = *pfalse = const0_rtx;
8838 return cond0;
8843 else if (code == IF_THEN_ELSE)
8845 /* If we have IF_THEN_ELSE already, extract the condition and
8846 canonicalize it if it is NE or EQ. */
8847 cond0 = XEXP (x, 0);
8848 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8849 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8850 return XEXP (cond0, 0);
8851 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8853 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8854 return XEXP (cond0, 0);
8856 else
8857 return cond0;
8860 /* If X is a SUBREG, we can narrow both the true and false values
8861 if the inner expression, if there is a condition. */
8862 else if (code == SUBREG
8863 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8864 &true0, &false0)))
8866 true0 = simplify_gen_subreg (mode, true0,
8867 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8868 false0 = simplify_gen_subreg (mode, false0,
8869 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8870 if (true0 && false0)
8872 *ptrue = true0;
8873 *pfalse = false0;
8874 return cond0;
8878 /* If X is a constant, this isn't special and will cause confusions
8879 if we treat it as such. Likewise if it is equivalent to a constant. */
8880 else if (CONSTANT_P (x)
8881 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8884 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8885 will be least confusing to the rest of the compiler. */
8886 else if (mode == BImode)
8888 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8889 return x;
8892 /* If X is known to be either 0 or -1, those are the true and
8893 false values when testing X. */
8894 else if (x == constm1_rtx || x == const0_rtx
8895 || (mode != VOIDmode
8896 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8898 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8899 return x;
8902 /* Likewise for 0 or a single bit. */
8903 else if (HWI_COMPUTABLE_MODE_P (mode)
8904 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8906 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8907 return x;
8910 /* Otherwise fail; show no condition with true and false values the same. */
8911 *ptrue = *pfalse = x;
8912 return 0;
8915 /* Return the value of expression X given the fact that condition COND
8916 is known to be true when applied to REG as its first operand and VAL
8917 as its second. X is known to not be shared and so can be modified in
8918 place.
8920 We only handle the simplest cases, and specifically those cases that
8921 arise with IF_THEN_ELSE expressions. */
8923 static rtx
8924 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8926 enum rtx_code code = GET_CODE (x);
8927 rtx temp;
8928 const char *fmt;
8929 int i, j;
8931 if (side_effects_p (x))
8932 return x;
8934 /* If either operand of the condition is a floating point value,
8935 then we have to avoid collapsing an EQ comparison. */
8936 if (cond == EQ
8937 && rtx_equal_p (x, reg)
8938 && ! FLOAT_MODE_P (GET_MODE (x))
8939 && ! FLOAT_MODE_P (GET_MODE (val)))
8940 return val;
8942 if (cond == UNEQ && rtx_equal_p (x, reg))
8943 return val;
8945 /* If X is (abs REG) and we know something about REG's relationship
8946 with zero, we may be able to simplify this. */
8948 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8949 switch (cond)
8951 case GE: case GT: case EQ:
8952 return XEXP (x, 0);
8953 case LT: case LE:
8954 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8955 XEXP (x, 0),
8956 GET_MODE (XEXP (x, 0)));
8957 default:
8958 break;
8961 /* The only other cases we handle are MIN, MAX, and comparisons if the
8962 operands are the same as REG and VAL. */
8964 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8966 if (rtx_equal_p (XEXP (x, 0), val))
8967 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8969 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8971 if (COMPARISON_P (x))
8973 if (comparison_dominates_p (cond, code))
8974 return const_true_rtx;
8976 code = reversed_comparison_code (x, NULL);
8977 if (code != UNKNOWN
8978 && comparison_dominates_p (cond, code))
8979 return const0_rtx;
8980 else
8981 return x;
8983 else if (code == SMAX || code == SMIN
8984 || code == UMIN || code == UMAX)
8986 int unsignedp = (code == UMIN || code == UMAX);
8988 /* Do not reverse the condition when it is NE or EQ.
8989 This is because we cannot conclude anything about
8990 the value of 'SMAX (x, y)' when x is not equal to y,
8991 but we can when x equals y. */
8992 if ((code == SMAX || code == UMAX)
8993 && ! (cond == EQ || cond == NE))
8994 cond = reverse_condition (cond);
8996 switch (cond)
8998 case GE: case GT:
8999 return unsignedp ? x : XEXP (x, 1);
9000 case LE: case LT:
9001 return unsignedp ? x : XEXP (x, 0);
9002 case GEU: case GTU:
9003 return unsignedp ? XEXP (x, 1) : x;
9004 case LEU: case LTU:
9005 return unsignedp ? XEXP (x, 0) : x;
9006 default:
9007 break;
9012 else if (code == SUBREG)
9014 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9015 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9017 if (SUBREG_REG (x) != r)
9019 /* We must simplify subreg here, before we lose track of the
9020 original inner_mode. */
9021 new_rtx = simplify_subreg (GET_MODE (x), r,
9022 inner_mode, SUBREG_BYTE (x));
9023 if (new_rtx)
9024 return new_rtx;
9025 else
9026 SUBST (SUBREG_REG (x), r);
9029 return x;
9031 /* We don't have to handle SIGN_EXTEND here, because even in the
9032 case of replacing something with a modeless CONST_INT, a
9033 CONST_INT is already (supposed to be) a valid sign extension for
9034 its narrower mode, which implies it's already properly
9035 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9036 story is different. */
9037 else if (code == ZERO_EXTEND)
9039 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9040 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9042 if (XEXP (x, 0) != r)
9044 /* We must simplify the zero_extend here, before we lose
9045 track of the original inner_mode. */
9046 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9047 r, inner_mode);
9048 if (new_rtx)
9049 return new_rtx;
9050 else
9051 SUBST (XEXP (x, 0), r);
9054 return x;
9057 fmt = GET_RTX_FORMAT (code);
9058 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9060 if (fmt[i] == 'e')
9061 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9062 else if (fmt[i] == 'E')
9063 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9064 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9065 cond, reg, val));
9068 return x;
9071 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9072 assignment as a field assignment. */
9074 static int
9075 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9077 if (x == y || rtx_equal_p (x, y))
9078 return 1;
9080 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9081 return 0;
9083 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9084 Note that all SUBREGs of MEM are paradoxical; otherwise they
9085 would have been rewritten. */
9086 if (MEM_P (x) && GET_CODE (y) == SUBREG
9087 && MEM_P (SUBREG_REG (y))
9088 && rtx_equal_p (SUBREG_REG (y),
9089 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9090 return 1;
9092 if (MEM_P (y) && GET_CODE (x) == SUBREG
9093 && MEM_P (SUBREG_REG (x))
9094 && rtx_equal_p (SUBREG_REG (x),
9095 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9096 return 1;
9098 /* We used to see if get_last_value of X and Y were the same but that's
9099 not correct. In one direction, we'll cause the assignment to have
9100 the wrong destination and in the case, we'll import a register into this
9101 insn that might have already have been dead. So fail if none of the
9102 above cases are true. */
9103 return 0;
9106 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9107 Return that assignment if so.
9109 We only handle the most common cases. */
9111 static rtx
9112 make_field_assignment (rtx x)
9114 rtx dest = SET_DEST (x);
9115 rtx src = SET_SRC (x);
9116 rtx assign;
9117 rtx rhs, lhs;
9118 HOST_WIDE_INT c1;
9119 HOST_WIDE_INT pos;
9120 unsigned HOST_WIDE_INT len;
9121 rtx other;
9122 machine_mode mode;
9124 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9125 a clear of a one-bit field. We will have changed it to
9126 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9127 for a SUBREG. */
9129 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9130 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9131 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9132 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9134 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9135 1, 1, 1, 0);
9136 if (assign != 0)
9137 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9138 return x;
9141 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9142 && subreg_lowpart_p (XEXP (src, 0))
9143 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9144 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9145 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9146 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9147 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9148 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9150 assign = make_extraction (VOIDmode, dest, 0,
9151 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9152 1, 1, 1, 0);
9153 if (assign != 0)
9154 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9155 return x;
9158 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9159 one-bit field. */
9160 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9161 && XEXP (XEXP (src, 0), 0) == const1_rtx
9162 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9164 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9165 1, 1, 1, 0);
9166 if (assign != 0)
9167 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9168 return x;
9171 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9172 SRC is an AND with all bits of that field set, then we can discard
9173 the AND. */
9174 if (GET_CODE (dest) == ZERO_EXTRACT
9175 && CONST_INT_P (XEXP (dest, 1))
9176 && GET_CODE (src) == AND
9177 && CONST_INT_P (XEXP (src, 1)))
9179 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9180 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9181 unsigned HOST_WIDE_INT ze_mask;
9183 if (width >= HOST_BITS_PER_WIDE_INT)
9184 ze_mask = -1;
9185 else
9186 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9188 /* Complete overlap. We can remove the source AND. */
9189 if ((and_mask & ze_mask) == ze_mask)
9190 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9192 /* Partial overlap. We can reduce the source AND. */
9193 if ((and_mask & ze_mask) != and_mask)
9195 mode = GET_MODE (src);
9196 src = gen_rtx_AND (mode, XEXP (src, 0),
9197 gen_int_mode (and_mask & ze_mask, mode));
9198 return gen_rtx_SET (VOIDmode, dest, src);
9202 /* The other case we handle is assignments into a constant-position
9203 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9204 a mask that has all one bits except for a group of zero bits and
9205 OTHER is known to have zeros where C1 has ones, this is such an
9206 assignment. Compute the position and length from C1. Shift OTHER
9207 to the appropriate position, force it to the required mode, and
9208 make the extraction. Check for the AND in both operands. */
9210 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9211 return x;
9213 rhs = expand_compound_operation (XEXP (src, 0));
9214 lhs = expand_compound_operation (XEXP (src, 1));
9216 if (GET_CODE (rhs) == AND
9217 && CONST_INT_P (XEXP (rhs, 1))
9218 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9219 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9220 else if (GET_CODE (lhs) == AND
9221 && CONST_INT_P (XEXP (lhs, 1))
9222 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9223 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9224 else
9225 return x;
9227 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9228 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9229 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9230 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9231 return x;
9233 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9234 if (assign == 0)
9235 return x;
9237 /* The mode to use for the source is the mode of the assignment, or of
9238 what is inside a possible STRICT_LOW_PART. */
9239 mode = (GET_CODE (assign) == STRICT_LOW_PART
9240 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9242 /* Shift OTHER right POS places and make it the source, restricting it
9243 to the proper length and mode. */
9245 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9246 GET_MODE (src),
9247 other, pos),
9248 dest);
9249 src = force_to_mode (src, mode,
9250 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9251 ? ~(unsigned HOST_WIDE_INT) 0
9252 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9255 /* If SRC is masked by an AND that does not make a difference in
9256 the value being stored, strip it. */
9257 if (GET_CODE (assign) == ZERO_EXTRACT
9258 && CONST_INT_P (XEXP (assign, 1))
9259 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9260 && GET_CODE (src) == AND
9261 && CONST_INT_P (XEXP (src, 1))
9262 && UINTVAL (XEXP (src, 1))
9263 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9264 src = XEXP (src, 0);
9266 return gen_rtx_SET (VOIDmode, assign, src);
9269 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9270 if so. */
9272 static rtx
9273 apply_distributive_law (rtx x)
9275 enum rtx_code code = GET_CODE (x);
9276 enum rtx_code inner_code;
9277 rtx lhs, rhs, other;
9278 rtx tem;
9280 /* Distributivity is not true for floating point as it can change the
9281 value. So we don't do it unless -funsafe-math-optimizations. */
9282 if (FLOAT_MODE_P (GET_MODE (x))
9283 && ! flag_unsafe_math_optimizations)
9284 return x;
9286 /* The outer operation can only be one of the following: */
9287 if (code != IOR && code != AND && code != XOR
9288 && code != PLUS && code != MINUS)
9289 return x;
9291 lhs = XEXP (x, 0);
9292 rhs = XEXP (x, 1);
9294 /* If either operand is a primitive we can't do anything, so get out
9295 fast. */
9296 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9297 return x;
9299 lhs = expand_compound_operation (lhs);
9300 rhs = expand_compound_operation (rhs);
9301 inner_code = GET_CODE (lhs);
9302 if (inner_code != GET_CODE (rhs))
9303 return x;
9305 /* See if the inner and outer operations distribute. */
9306 switch (inner_code)
9308 case LSHIFTRT:
9309 case ASHIFTRT:
9310 case AND:
9311 case IOR:
9312 /* These all distribute except over PLUS. */
9313 if (code == PLUS || code == MINUS)
9314 return x;
9315 break;
9317 case MULT:
9318 if (code != PLUS && code != MINUS)
9319 return x;
9320 break;
9322 case ASHIFT:
9323 /* This is also a multiply, so it distributes over everything. */
9324 break;
9326 /* This used to handle SUBREG, but this turned out to be counter-
9327 productive, since (subreg (op ...)) usually is not handled by
9328 insn patterns, and this "optimization" therefore transformed
9329 recognizable patterns into unrecognizable ones. Therefore the
9330 SUBREG case was removed from here.
9332 It is possible that distributing SUBREG over arithmetic operations
9333 leads to an intermediate result than can then be optimized further,
9334 e.g. by moving the outer SUBREG to the other side of a SET as done
9335 in simplify_set. This seems to have been the original intent of
9336 handling SUBREGs here.
9338 However, with current GCC this does not appear to actually happen,
9339 at least on major platforms. If some case is found where removing
9340 the SUBREG case here prevents follow-on optimizations, distributing
9341 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9343 default:
9344 return x;
9347 /* Set LHS and RHS to the inner operands (A and B in the example
9348 above) and set OTHER to the common operand (C in the example).
9349 There is only one way to do this unless the inner operation is
9350 commutative. */
9351 if (COMMUTATIVE_ARITH_P (lhs)
9352 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9353 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9354 else if (COMMUTATIVE_ARITH_P (lhs)
9355 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9356 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9357 else if (COMMUTATIVE_ARITH_P (lhs)
9358 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9359 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9360 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9361 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9362 else
9363 return x;
9365 /* Form the new inner operation, seeing if it simplifies first. */
9366 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9368 /* There is one exception to the general way of distributing:
9369 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9370 if (code == XOR && inner_code == IOR)
9372 inner_code = AND;
9373 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9376 /* We may be able to continuing distributing the result, so call
9377 ourselves recursively on the inner operation before forming the
9378 outer operation, which we return. */
9379 return simplify_gen_binary (inner_code, GET_MODE (x),
9380 apply_distributive_law (tem), other);
9383 /* See if X is of the form (* (+ A B) C), and if so convert to
9384 (+ (* A C) (* B C)) and try to simplify.
9386 Most of the time, this results in no change. However, if some of
9387 the operands are the same or inverses of each other, simplifications
9388 will result.
9390 For example, (and (ior A B) (not B)) can occur as the result of
9391 expanding a bit field assignment. When we apply the distributive
9392 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9393 which then simplifies to (and (A (not B))).
9395 Note that no checks happen on the validity of applying the inverse
9396 distributive law. This is pointless since we can do it in the
9397 few places where this routine is called.
9399 N is the index of the term that is decomposed (the arithmetic operation,
9400 i.e. (+ A B) in the first example above). !N is the index of the term that
9401 is distributed, i.e. of C in the first example above. */
9402 static rtx
9403 distribute_and_simplify_rtx (rtx x, int n)
9405 machine_mode mode;
9406 enum rtx_code outer_code, inner_code;
9407 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9409 /* Distributivity is not true for floating point as it can change the
9410 value. So we don't do it unless -funsafe-math-optimizations. */
9411 if (FLOAT_MODE_P (GET_MODE (x))
9412 && ! flag_unsafe_math_optimizations)
9413 return NULL_RTX;
9415 decomposed = XEXP (x, n);
9416 if (!ARITHMETIC_P (decomposed))
9417 return NULL_RTX;
9419 mode = GET_MODE (x);
9420 outer_code = GET_CODE (x);
9421 distributed = XEXP (x, !n);
9423 inner_code = GET_CODE (decomposed);
9424 inner_op0 = XEXP (decomposed, 0);
9425 inner_op1 = XEXP (decomposed, 1);
9427 /* Special case (and (xor B C) (not A)), which is equivalent to
9428 (xor (ior A B) (ior A C)) */
9429 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9431 distributed = XEXP (distributed, 0);
9432 outer_code = IOR;
9435 if (n == 0)
9437 /* Distribute the second term. */
9438 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9439 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9441 else
9443 /* Distribute the first term. */
9444 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9445 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9448 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9449 new_op0, new_op1));
9450 if (GET_CODE (tmp) != outer_code
9451 && (set_src_cost (tmp, optimize_this_for_speed_p)
9452 < set_src_cost (x, optimize_this_for_speed_p)))
9453 return tmp;
9455 return NULL_RTX;
9458 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9459 in MODE. Return an equivalent form, if different from (and VAROP
9460 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9462 static rtx
9463 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9464 unsigned HOST_WIDE_INT constop)
9466 unsigned HOST_WIDE_INT nonzero;
9467 unsigned HOST_WIDE_INT orig_constop;
9468 rtx orig_varop;
9469 int i;
9471 orig_varop = varop;
9472 orig_constop = constop;
9473 if (GET_CODE (varop) == CLOBBER)
9474 return NULL_RTX;
9476 /* Simplify VAROP knowing that we will be only looking at some of the
9477 bits in it.
9479 Note by passing in CONSTOP, we guarantee that the bits not set in
9480 CONSTOP are not significant and will never be examined. We must
9481 ensure that is the case by explicitly masking out those bits
9482 before returning. */
9483 varop = force_to_mode (varop, mode, constop, 0);
9485 /* If VAROP is a CLOBBER, we will fail so return it. */
9486 if (GET_CODE (varop) == CLOBBER)
9487 return varop;
9489 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9490 to VAROP and return the new constant. */
9491 if (CONST_INT_P (varop))
9492 return gen_int_mode (INTVAL (varop) & constop, mode);
9494 /* See what bits may be nonzero in VAROP. Unlike the general case of
9495 a call to nonzero_bits, here we don't care about bits outside
9496 MODE. */
9498 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9500 /* Turn off all bits in the constant that are known to already be zero.
9501 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9502 which is tested below. */
9504 constop &= nonzero;
9506 /* If we don't have any bits left, return zero. */
9507 if (constop == 0)
9508 return const0_rtx;
9510 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9511 a power of two, we can replace this with an ASHIFT. */
9512 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9513 && (i = exact_log2 (constop)) >= 0)
9514 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9516 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9517 or XOR, then try to apply the distributive law. This may eliminate
9518 operations if either branch can be simplified because of the AND.
9519 It may also make some cases more complex, but those cases probably
9520 won't match a pattern either with or without this. */
9522 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9523 return
9524 gen_lowpart
9525 (mode,
9526 apply_distributive_law
9527 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9528 simplify_and_const_int (NULL_RTX,
9529 GET_MODE (varop),
9530 XEXP (varop, 0),
9531 constop),
9532 simplify_and_const_int (NULL_RTX,
9533 GET_MODE (varop),
9534 XEXP (varop, 1),
9535 constop))));
9537 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9538 the AND and see if one of the operands simplifies to zero. If so, we
9539 may eliminate it. */
9541 if (GET_CODE (varop) == PLUS
9542 && exact_log2 (constop + 1) >= 0)
9544 rtx o0, o1;
9546 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9547 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9548 if (o0 == const0_rtx)
9549 return o1;
9550 if (o1 == const0_rtx)
9551 return o0;
9554 /* Make a SUBREG if necessary. If we can't make it, fail. */
9555 varop = gen_lowpart (mode, varop);
9556 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9557 return NULL_RTX;
9559 /* If we are only masking insignificant bits, return VAROP. */
9560 if (constop == nonzero)
9561 return varop;
9563 if (varop == orig_varop && constop == orig_constop)
9564 return NULL_RTX;
9566 /* Otherwise, return an AND. */
9567 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9571 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9572 in MODE.
9574 Return an equivalent form, if different from X. Otherwise, return X. If
9575 X is zero, we are to always construct the equivalent form. */
9577 static rtx
9578 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9579 unsigned HOST_WIDE_INT constop)
9581 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9582 if (tem)
9583 return tem;
9585 if (!x)
9586 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9587 gen_int_mode (constop, mode));
9588 if (GET_MODE (x) != mode)
9589 x = gen_lowpart (mode, x);
9590 return x;
9593 /* Given a REG, X, compute which bits in X can be nonzero.
9594 We don't care about bits outside of those defined in MODE.
9596 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9597 a shift, AND, or zero_extract, we can do better. */
9599 static rtx
9600 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9601 const_rtx known_x ATTRIBUTE_UNUSED,
9602 machine_mode known_mode ATTRIBUTE_UNUSED,
9603 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9604 unsigned HOST_WIDE_INT *nonzero)
9606 rtx tem;
9607 reg_stat_type *rsp;
9609 /* If X is a register whose nonzero bits value is current, use it.
9610 Otherwise, if X is a register whose value we can find, use that
9611 value. Otherwise, use the previously-computed global nonzero bits
9612 for this register. */
9614 rsp = &reg_stat[REGNO (x)];
9615 if (rsp->last_set_value != 0
9616 && (rsp->last_set_mode == mode
9617 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9618 && GET_MODE_CLASS (mode) == MODE_INT))
9619 && ((rsp->last_set_label >= label_tick_ebb_start
9620 && rsp->last_set_label < label_tick)
9621 || (rsp->last_set_label == label_tick
9622 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9623 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9624 && REG_N_SETS (REGNO (x)) == 1
9625 && !REGNO_REG_SET_P
9626 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9627 REGNO (x)))))
9629 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9631 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9632 /* We don't know anything about the upper bits. */
9633 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9635 *nonzero &= mask;
9636 return NULL;
9639 tem = get_last_value (x);
9641 if (tem)
9643 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9644 /* If X is narrower than MODE and TEM is a non-negative
9645 constant that would appear negative in the mode of X,
9646 sign-extend it for use in reg_nonzero_bits because some
9647 machines (maybe most) will actually do the sign-extension
9648 and this is the conservative approach.
9650 ??? For 2.5, try to tighten up the MD files in this regard
9651 instead of this kludge. */
9653 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9654 && CONST_INT_P (tem)
9655 && INTVAL (tem) > 0
9656 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9657 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9658 #endif
9659 return tem;
9661 else if (nonzero_sign_valid && rsp->nonzero_bits)
9663 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9665 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9666 /* We don't know anything about the upper bits. */
9667 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9669 *nonzero &= mask;
9672 return NULL;
9675 /* Return the number of bits at the high-order end of X that are known to
9676 be equal to the sign bit. X will be used in mode MODE; if MODE is
9677 VOIDmode, X will be used in its own mode. The returned value will always
9678 be between 1 and the number of bits in MODE. */
9680 static rtx
9681 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9682 const_rtx known_x ATTRIBUTE_UNUSED,
9683 machine_mode known_mode
9684 ATTRIBUTE_UNUSED,
9685 unsigned int known_ret ATTRIBUTE_UNUSED,
9686 unsigned int *result)
9688 rtx tem;
9689 reg_stat_type *rsp;
9691 rsp = &reg_stat[REGNO (x)];
9692 if (rsp->last_set_value != 0
9693 && rsp->last_set_mode == mode
9694 && ((rsp->last_set_label >= label_tick_ebb_start
9695 && rsp->last_set_label < label_tick)
9696 || (rsp->last_set_label == label_tick
9697 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9698 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9699 && REG_N_SETS (REGNO (x)) == 1
9700 && !REGNO_REG_SET_P
9701 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9702 REGNO (x)))))
9704 *result = rsp->last_set_sign_bit_copies;
9705 return NULL;
9708 tem = get_last_value (x);
9709 if (tem != 0)
9710 return tem;
9712 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9713 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9714 *result = rsp->sign_bit_copies;
9716 return NULL;
9719 /* Return the number of "extended" bits there are in X, when interpreted
9720 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9721 unsigned quantities, this is the number of high-order zero bits.
9722 For signed quantities, this is the number of copies of the sign bit
9723 minus 1. In both case, this function returns the number of "spare"
9724 bits. For example, if two quantities for which this function returns
9725 at least 1 are added, the addition is known not to overflow.
9727 This function will always return 0 unless called during combine, which
9728 implies that it must be called from a define_split. */
9730 unsigned int
9731 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9733 if (nonzero_sign_valid == 0)
9734 return 0;
9736 return (unsignedp
9737 ? (HWI_COMPUTABLE_MODE_P (mode)
9738 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9739 - floor_log2 (nonzero_bits (x, mode)))
9740 : 0)
9741 : num_sign_bit_copies (x, mode) - 1);
9744 /* This function is called from `simplify_shift_const' to merge two
9745 outer operations. Specifically, we have already found that we need
9746 to perform operation *POP0 with constant *PCONST0 at the outermost
9747 position. We would now like to also perform OP1 with constant CONST1
9748 (with *POP0 being done last).
9750 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9751 the resulting operation. *PCOMP_P is set to 1 if we would need to
9752 complement the innermost operand, otherwise it is unchanged.
9754 MODE is the mode in which the operation will be done. No bits outside
9755 the width of this mode matter. It is assumed that the width of this mode
9756 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9758 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9759 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9760 result is simply *PCONST0.
9762 If the resulting operation cannot be expressed as one operation, we
9763 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9765 static int
9766 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)
9768 enum rtx_code op0 = *pop0;
9769 HOST_WIDE_INT const0 = *pconst0;
9771 const0 &= GET_MODE_MASK (mode);
9772 const1 &= GET_MODE_MASK (mode);
9774 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9775 if (op0 == AND)
9776 const1 &= const0;
9778 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9779 if OP0 is SET. */
9781 if (op1 == UNKNOWN || op0 == SET)
9782 return 1;
9784 else if (op0 == UNKNOWN)
9785 op0 = op1, const0 = const1;
9787 else if (op0 == op1)
9789 switch (op0)
9791 case AND:
9792 const0 &= const1;
9793 break;
9794 case IOR:
9795 const0 |= const1;
9796 break;
9797 case XOR:
9798 const0 ^= const1;
9799 break;
9800 case PLUS:
9801 const0 += const1;
9802 break;
9803 case NEG:
9804 op0 = UNKNOWN;
9805 break;
9806 default:
9807 break;
9811 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9812 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9813 return 0;
9815 /* If the two constants aren't the same, we can't do anything. The
9816 remaining six cases can all be done. */
9817 else if (const0 != const1)
9818 return 0;
9820 else
9821 switch (op0)
9823 case IOR:
9824 if (op1 == AND)
9825 /* (a & b) | b == b */
9826 op0 = SET;
9827 else /* op1 == XOR */
9828 /* (a ^ b) | b == a | b */
9830 break;
9832 case XOR:
9833 if (op1 == AND)
9834 /* (a & b) ^ b == (~a) & b */
9835 op0 = AND, *pcomp_p = 1;
9836 else /* op1 == IOR */
9837 /* (a | b) ^ b == a & ~b */
9838 op0 = AND, const0 = ~const0;
9839 break;
9841 case AND:
9842 if (op1 == IOR)
9843 /* (a | b) & b == b */
9844 op0 = SET;
9845 else /* op1 == XOR */
9846 /* (a ^ b) & b) == (~a) & b */
9847 *pcomp_p = 1;
9848 break;
9849 default:
9850 break;
9853 /* Check for NO-OP cases. */
9854 const0 &= GET_MODE_MASK (mode);
9855 if (const0 == 0
9856 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9857 op0 = UNKNOWN;
9858 else if (const0 == 0 && op0 == AND)
9859 op0 = SET;
9860 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9861 && op0 == AND)
9862 op0 = UNKNOWN;
9864 *pop0 = op0;
9866 /* ??? Slightly redundant with the above mask, but not entirely.
9867 Moving this above means we'd have to sign-extend the mode mask
9868 for the final test. */
9869 if (op0 != UNKNOWN && op0 != NEG)
9870 *pconst0 = trunc_int_for_mode (const0, mode);
9872 return 1;
9875 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9876 the shift in. The original shift operation CODE is performed on OP in
9877 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9878 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9879 result of the shift is subject to operation OUTER_CODE with operand
9880 OUTER_CONST. */
9882 static machine_mode
9883 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9884 machine_mode orig_mode, machine_mode mode,
9885 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9887 if (orig_mode == mode)
9888 return mode;
9889 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9891 /* In general we can't perform in wider mode for right shift and rotate. */
9892 switch (code)
9894 case ASHIFTRT:
9895 /* We can still widen if the bits brought in from the left are identical
9896 to the sign bit of ORIG_MODE. */
9897 if (num_sign_bit_copies (op, mode)
9898 > (unsigned) (GET_MODE_PRECISION (mode)
9899 - GET_MODE_PRECISION (orig_mode)))
9900 return mode;
9901 return orig_mode;
9903 case LSHIFTRT:
9904 /* Similarly here but with zero bits. */
9905 if (HWI_COMPUTABLE_MODE_P (mode)
9906 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9907 return mode;
9909 /* We can also widen if the bits brought in will be masked off. This
9910 operation is performed in ORIG_MODE. */
9911 if (outer_code == AND)
9913 int care_bits = low_bitmask_len (orig_mode, outer_const);
9915 if (care_bits >= 0
9916 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9917 return mode;
9919 /* fall through */
9921 case ROTATE:
9922 return orig_mode;
9924 case ROTATERT:
9925 gcc_unreachable ();
9927 default:
9928 return mode;
9932 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9933 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9934 if we cannot simplify it. Otherwise, return a simplified value.
9936 The shift is normally computed in the widest mode we find in VAROP, as
9937 long as it isn't a different number of words than RESULT_MODE. Exceptions
9938 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9940 static rtx
9941 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
9942 rtx varop, int orig_count)
9944 enum rtx_code orig_code = code;
9945 rtx orig_varop = varop;
9946 int count;
9947 machine_mode mode = result_mode;
9948 machine_mode shift_mode, tmode;
9949 unsigned int mode_words
9950 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9951 /* We form (outer_op (code varop count) (outer_const)). */
9952 enum rtx_code outer_op = UNKNOWN;
9953 HOST_WIDE_INT outer_const = 0;
9954 int complement_p = 0;
9955 rtx new_rtx, x;
9957 /* Make sure and truncate the "natural" shift on the way in. We don't
9958 want to do this inside the loop as it makes it more difficult to
9959 combine shifts. */
9960 if (SHIFT_COUNT_TRUNCATED)
9961 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9963 /* If we were given an invalid count, don't do anything except exactly
9964 what was requested. */
9966 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9967 return NULL_RTX;
9969 count = orig_count;
9971 /* Unless one of the branches of the `if' in this loop does a `continue',
9972 we will `break' the loop after the `if'. */
9974 while (count != 0)
9976 /* If we have an operand of (clobber (const_int 0)), fail. */
9977 if (GET_CODE (varop) == CLOBBER)
9978 return NULL_RTX;
9980 /* Convert ROTATERT to ROTATE. */
9981 if (code == ROTATERT)
9983 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9984 code = ROTATE;
9985 if (VECTOR_MODE_P (result_mode))
9986 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9987 else
9988 count = bitsize - count;
9991 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9992 mode, outer_op, outer_const);
9994 /* Handle cases where the count is greater than the size of the mode
9995 minus 1. For ASHIFT, use the size minus one as the count (this can
9996 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9997 take the count modulo the size. For other shifts, the result is
9998 zero.
10000 Since these shifts are being produced by the compiler by combining
10001 multiple operations, each of which are defined, we know what the
10002 result is supposed to be. */
10004 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10006 if (code == ASHIFTRT)
10007 count = GET_MODE_PRECISION (shift_mode) - 1;
10008 else if (code == ROTATE || code == ROTATERT)
10009 count %= GET_MODE_PRECISION (shift_mode);
10010 else
10012 /* We can't simply return zero because there may be an
10013 outer op. */
10014 varop = const0_rtx;
10015 count = 0;
10016 break;
10020 /* If we discovered we had to complement VAROP, leave. Making a NOT
10021 here would cause an infinite loop. */
10022 if (complement_p)
10023 break;
10025 /* An arithmetic right shift of a quantity known to be -1 or 0
10026 is a no-op. */
10027 if (code == ASHIFTRT
10028 && (num_sign_bit_copies (varop, shift_mode)
10029 == GET_MODE_PRECISION (shift_mode)))
10031 count = 0;
10032 break;
10035 /* If we are doing an arithmetic right shift and discarding all but
10036 the sign bit copies, this is equivalent to doing a shift by the
10037 bitsize minus one. Convert it into that shift because it will often
10038 allow other simplifications. */
10040 if (code == ASHIFTRT
10041 && (count + num_sign_bit_copies (varop, shift_mode)
10042 >= GET_MODE_PRECISION (shift_mode)))
10043 count = GET_MODE_PRECISION (shift_mode) - 1;
10045 /* We simplify the tests below and elsewhere by converting
10046 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10047 `make_compound_operation' will convert it to an ASHIFTRT for
10048 those machines (such as VAX) that don't have an LSHIFTRT. */
10049 if (code == ASHIFTRT
10050 && val_signbit_known_clear_p (shift_mode,
10051 nonzero_bits (varop, shift_mode)))
10052 code = LSHIFTRT;
10054 if (((code == LSHIFTRT
10055 && HWI_COMPUTABLE_MODE_P (shift_mode)
10056 && !(nonzero_bits (varop, shift_mode) >> count))
10057 || (code == ASHIFT
10058 && HWI_COMPUTABLE_MODE_P (shift_mode)
10059 && !((nonzero_bits (varop, shift_mode) << count)
10060 & GET_MODE_MASK (shift_mode))))
10061 && !side_effects_p (varop))
10062 varop = const0_rtx;
10064 switch (GET_CODE (varop))
10066 case SIGN_EXTEND:
10067 case ZERO_EXTEND:
10068 case SIGN_EXTRACT:
10069 case ZERO_EXTRACT:
10070 new_rtx = expand_compound_operation (varop);
10071 if (new_rtx != varop)
10073 varop = new_rtx;
10074 continue;
10076 break;
10078 case MEM:
10079 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10080 minus the width of a smaller mode, we can do this with a
10081 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10082 if ((code == ASHIFTRT || code == LSHIFTRT)
10083 && ! mode_dependent_address_p (XEXP (varop, 0),
10084 MEM_ADDR_SPACE (varop))
10085 && ! MEM_VOLATILE_P (varop)
10086 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10087 MODE_INT, 1)) != BLKmode)
10089 new_rtx = adjust_address_nv (varop, tmode,
10090 BYTES_BIG_ENDIAN ? 0
10091 : count / BITS_PER_UNIT);
10093 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10094 : ZERO_EXTEND, mode, new_rtx);
10095 count = 0;
10096 continue;
10098 break;
10100 case SUBREG:
10101 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10102 the same number of words as what we've seen so far. Then store
10103 the widest mode in MODE. */
10104 if (subreg_lowpart_p (varop)
10105 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10106 > GET_MODE_SIZE (GET_MODE (varop)))
10107 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10108 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10109 == mode_words
10110 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10111 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10113 varop = SUBREG_REG (varop);
10114 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10115 mode = GET_MODE (varop);
10116 continue;
10118 break;
10120 case MULT:
10121 /* Some machines use MULT instead of ASHIFT because MULT
10122 is cheaper. But it is still better on those machines to
10123 merge two shifts into one. */
10124 if (CONST_INT_P (XEXP (varop, 1))
10125 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10127 varop
10128 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10129 XEXP (varop, 0),
10130 GEN_INT (exact_log2 (
10131 UINTVAL (XEXP (varop, 1)))));
10132 continue;
10134 break;
10136 case UDIV:
10137 /* Similar, for when divides are cheaper. */
10138 if (CONST_INT_P (XEXP (varop, 1))
10139 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10141 varop
10142 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10143 XEXP (varop, 0),
10144 GEN_INT (exact_log2 (
10145 UINTVAL (XEXP (varop, 1)))));
10146 continue;
10148 break;
10150 case ASHIFTRT:
10151 /* If we are extracting just the sign bit of an arithmetic
10152 right shift, that shift is not needed. However, the sign
10153 bit of a wider mode may be different from what would be
10154 interpreted as the sign bit in a narrower mode, so, if
10155 the result is narrower, don't discard the shift. */
10156 if (code == LSHIFTRT
10157 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10158 && (GET_MODE_BITSIZE (result_mode)
10159 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10161 varop = XEXP (varop, 0);
10162 continue;
10165 /* ... fall through ... */
10167 case LSHIFTRT:
10168 case ASHIFT:
10169 case ROTATE:
10170 /* Here we have two nested shifts. The result is usually the
10171 AND of a new shift with a mask. We compute the result below. */
10172 if (CONST_INT_P (XEXP (varop, 1))
10173 && INTVAL (XEXP (varop, 1)) >= 0
10174 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10175 && HWI_COMPUTABLE_MODE_P (result_mode)
10176 && HWI_COMPUTABLE_MODE_P (mode)
10177 && !VECTOR_MODE_P (result_mode))
10179 enum rtx_code first_code = GET_CODE (varop);
10180 unsigned int first_count = INTVAL (XEXP (varop, 1));
10181 unsigned HOST_WIDE_INT mask;
10182 rtx mask_rtx;
10184 /* We have one common special case. We can't do any merging if
10185 the inner code is an ASHIFTRT of a smaller mode. However, if
10186 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10187 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10188 we can convert it to
10189 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10190 This simplifies certain SIGN_EXTEND operations. */
10191 if (code == ASHIFT && first_code == ASHIFTRT
10192 && count == (GET_MODE_PRECISION (result_mode)
10193 - GET_MODE_PRECISION (GET_MODE (varop))))
10195 /* C3 has the low-order C1 bits zero. */
10197 mask = GET_MODE_MASK (mode)
10198 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10200 varop = simplify_and_const_int (NULL_RTX, result_mode,
10201 XEXP (varop, 0), mask);
10202 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10203 varop, count);
10204 count = first_count;
10205 code = ASHIFTRT;
10206 continue;
10209 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10210 than C1 high-order bits equal to the sign bit, we can convert
10211 this to either an ASHIFT or an ASHIFTRT depending on the
10212 two counts.
10214 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10216 if (code == ASHIFTRT && first_code == ASHIFT
10217 && GET_MODE (varop) == shift_mode
10218 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10219 > first_count))
10221 varop = XEXP (varop, 0);
10222 count -= first_count;
10223 if (count < 0)
10225 count = -count;
10226 code = ASHIFT;
10229 continue;
10232 /* There are some cases we can't do. If CODE is ASHIFTRT,
10233 we can only do this if FIRST_CODE is also ASHIFTRT.
10235 We can't do the case when CODE is ROTATE and FIRST_CODE is
10236 ASHIFTRT.
10238 If the mode of this shift is not the mode of the outer shift,
10239 we can't do this if either shift is a right shift or ROTATE.
10241 Finally, we can't do any of these if the mode is too wide
10242 unless the codes are the same.
10244 Handle the case where the shift codes are the same
10245 first. */
10247 if (code == first_code)
10249 if (GET_MODE (varop) != result_mode
10250 && (code == ASHIFTRT || code == LSHIFTRT
10251 || code == ROTATE))
10252 break;
10254 count += first_count;
10255 varop = XEXP (varop, 0);
10256 continue;
10259 if (code == ASHIFTRT
10260 || (code == ROTATE && first_code == ASHIFTRT)
10261 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10262 || (GET_MODE (varop) != result_mode
10263 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10264 || first_code == ROTATE
10265 || code == ROTATE)))
10266 break;
10268 /* To compute the mask to apply after the shift, shift the
10269 nonzero bits of the inner shift the same way the
10270 outer shift will. */
10272 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10273 result_mode);
10275 mask_rtx
10276 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10277 GEN_INT (count));
10279 /* Give up if we can't compute an outer operation to use. */
10280 if (mask_rtx == 0
10281 || !CONST_INT_P (mask_rtx)
10282 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10283 INTVAL (mask_rtx),
10284 result_mode, &complement_p))
10285 break;
10287 /* If the shifts are in the same direction, we add the
10288 counts. Otherwise, we subtract them. */
10289 if ((code == ASHIFTRT || code == LSHIFTRT)
10290 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10291 count += first_count;
10292 else
10293 count -= first_count;
10295 /* If COUNT is positive, the new shift is usually CODE,
10296 except for the two exceptions below, in which case it is
10297 FIRST_CODE. If the count is negative, FIRST_CODE should
10298 always be used */
10299 if (count > 0
10300 && ((first_code == ROTATE && code == ASHIFT)
10301 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10302 code = first_code;
10303 else if (count < 0)
10304 code = first_code, count = -count;
10306 varop = XEXP (varop, 0);
10307 continue;
10310 /* If we have (A << B << C) for any shift, we can convert this to
10311 (A << C << B). This wins if A is a constant. Only try this if
10312 B is not a constant. */
10314 else if (GET_CODE (varop) == code
10315 && CONST_INT_P (XEXP (varop, 0))
10316 && !CONST_INT_P (XEXP (varop, 1)))
10318 rtx new_rtx = simplify_const_binary_operation (code, mode,
10319 XEXP (varop, 0),
10320 GEN_INT (count));
10321 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10322 count = 0;
10323 continue;
10325 break;
10327 case NOT:
10328 if (VECTOR_MODE_P (mode))
10329 break;
10331 /* Make this fit the case below. */
10332 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10333 continue;
10335 case IOR:
10336 case AND:
10337 case XOR:
10338 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10339 with C the size of VAROP - 1 and the shift is logical if
10340 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10341 we have an (le X 0) operation. If we have an arithmetic shift
10342 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10343 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10345 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10346 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10347 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10348 && (code == LSHIFTRT || code == ASHIFTRT)
10349 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10350 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10352 count = 0;
10353 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10354 const0_rtx);
10356 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10357 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10359 continue;
10362 /* If we have (shift (logical)), move the logical to the outside
10363 to allow it to possibly combine with another logical and the
10364 shift to combine with another shift. This also canonicalizes to
10365 what a ZERO_EXTRACT looks like. Also, some machines have
10366 (and (shift)) insns. */
10368 if (CONST_INT_P (XEXP (varop, 1))
10369 /* We can't do this if we have (ashiftrt (xor)) and the
10370 constant has its sign bit set in shift_mode with shift_mode
10371 wider than result_mode. */
10372 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10373 && result_mode != shift_mode
10374 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10375 shift_mode))
10376 && (new_rtx = simplify_const_binary_operation
10377 (code, result_mode,
10378 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10379 GEN_INT (count))) != 0
10380 && CONST_INT_P (new_rtx)
10381 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10382 INTVAL (new_rtx), result_mode, &complement_p))
10384 varop = XEXP (varop, 0);
10385 continue;
10388 /* If we can't do that, try to simplify the shift in each arm of the
10389 logical expression, make a new logical expression, and apply
10390 the inverse distributive law. This also can't be done for
10391 (ashiftrt (xor)) where we've widened the shift and the constant
10392 changes the sign bit. */
10393 if (CONST_INT_P (XEXP (varop, 1))
10394 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10395 && result_mode != shift_mode
10396 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10397 shift_mode)))
10399 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10400 XEXP (varop, 0), count);
10401 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10402 XEXP (varop, 1), count);
10404 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10405 lhs, rhs);
10406 varop = apply_distributive_law (varop);
10408 count = 0;
10409 continue;
10411 break;
10413 case EQ:
10414 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10415 says that the sign bit can be tested, FOO has mode MODE, C is
10416 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10417 that may be nonzero. */
10418 if (code == LSHIFTRT
10419 && XEXP (varop, 1) == const0_rtx
10420 && GET_MODE (XEXP (varop, 0)) == result_mode
10421 && count == (GET_MODE_PRECISION (result_mode) - 1)
10422 && HWI_COMPUTABLE_MODE_P (result_mode)
10423 && STORE_FLAG_VALUE == -1
10424 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10425 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10426 &complement_p))
10428 varop = XEXP (varop, 0);
10429 count = 0;
10430 continue;
10432 break;
10434 case NEG:
10435 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10436 than the number of bits in the mode is equivalent to A. */
10437 if (code == LSHIFTRT
10438 && count == (GET_MODE_PRECISION (result_mode) - 1)
10439 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10441 varop = XEXP (varop, 0);
10442 count = 0;
10443 continue;
10446 /* NEG commutes with ASHIFT since it is multiplication. Move the
10447 NEG outside to allow shifts to combine. */
10448 if (code == ASHIFT
10449 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10450 &complement_p))
10452 varop = XEXP (varop, 0);
10453 continue;
10455 break;
10457 case PLUS:
10458 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10459 is one less than the number of bits in the mode is
10460 equivalent to (xor A 1). */
10461 if (code == LSHIFTRT
10462 && count == (GET_MODE_PRECISION (result_mode) - 1)
10463 && XEXP (varop, 1) == constm1_rtx
10464 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10465 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10466 &complement_p))
10468 count = 0;
10469 varop = XEXP (varop, 0);
10470 continue;
10473 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10474 that might be nonzero in BAR are those being shifted out and those
10475 bits are known zero in FOO, we can replace the PLUS with FOO.
10476 Similarly in the other operand order. This code occurs when
10477 we are computing the size of a variable-size array. */
10479 if ((code == ASHIFTRT || code == LSHIFTRT)
10480 && count < HOST_BITS_PER_WIDE_INT
10481 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10482 && (nonzero_bits (XEXP (varop, 1), result_mode)
10483 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10485 varop = XEXP (varop, 0);
10486 continue;
10488 else if ((code == ASHIFTRT || code == LSHIFTRT)
10489 && count < HOST_BITS_PER_WIDE_INT
10490 && HWI_COMPUTABLE_MODE_P (result_mode)
10491 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10492 >> count)
10493 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10494 & nonzero_bits (XEXP (varop, 1),
10495 result_mode)))
10497 varop = XEXP (varop, 1);
10498 continue;
10501 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10502 if (code == ASHIFT
10503 && CONST_INT_P (XEXP (varop, 1))
10504 && (new_rtx = simplify_const_binary_operation
10505 (ASHIFT, result_mode,
10506 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10507 GEN_INT (count))) != 0
10508 && CONST_INT_P (new_rtx)
10509 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10510 INTVAL (new_rtx), result_mode, &complement_p))
10512 varop = XEXP (varop, 0);
10513 continue;
10516 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10517 signbit', and attempt to change the PLUS to an XOR and move it to
10518 the outer operation as is done above in the AND/IOR/XOR case
10519 leg for shift(logical). See details in logical handling above
10520 for reasoning in doing so. */
10521 if (code == LSHIFTRT
10522 && CONST_INT_P (XEXP (varop, 1))
10523 && mode_signbit_p (result_mode, XEXP (varop, 1))
10524 && (new_rtx = simplify_const_binary_operation
10525 (code, result_mode,
10526 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10527 GEN_INT (count))) != 0
10528 && CONST_INT_P (new_rtx)
10529 && merge_outer_ops (&outer_op, &outer_const, XOR,
10530 INTVAL (new_rtx), result_mode, &complement_p))
10532 varop = XEXP (varop, 0);
10533 continue;
10536 break;
10538 case MINUS:
10539 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10540 with C the size of VAROP - 1 and the shift is logical if
10541 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10542 we have a (gt X 0) operation. If the shift is arithmetic with
10543 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10544 we have a (neg (gt X 0)) operation. */
10546 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10547 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10548 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10549 && (code == LSHIFTRT || code == ASHIFTRT)
10550 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10551 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10552 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10554 count = 0;
10555 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10556 const0_rtx);
10558 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10559 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10561 continue;
10563 break;
10565 case TRUNCATE:
10566 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10567 if the truncate does not affect the value. */
10568 if (code == LSHIFTRT
10569 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10570 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10571 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10572 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10573 - GET_MODE_PRECISION (GET_MODE (varop)))))
10575 rtx varop_inner = XEXP (varop, 0);
10577 varop_inner
10578 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10579 XEXP (varop_inner, 0),
10580 GEN_INT
10581 (count + INTVAL (XEXP (varop_inner, 1))));
10582 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10583 count = 0;
10584 continue;
10586 break;
10588 default:
10589 break;
10592 break;
10595 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10596 outer_op, outer_const);
10598 /* We have now finished analyzing the shift. The result should be
10599 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10600 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10601 to the result of the shift. OUTER_CONST is the relevant constant,
10602 but we must turn off all bits turned off in the shift. */
10604 if (outer_op == UNKNOWN
10605 && orig_code == code && orig_count == count
10606 && varop == orig_varop
10607 && shift_mode == GET_MODE (varop))
10608 return NULL_RTX;
10610 /* Make a SUBREG if necessary. If we can't make it, fail. */
10611 varop = gen_lowpart (shift_mode, varop);
10612 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10613 return NULL_RTX;
10615 /* If we have an outer operation and we just made a shift, it is
10616 possible that we could have simplified the shift were it not
10617 for the outer operation. So try to do the simplification
10618 recursively. */
10620 if (outer_op != UNKNOWN)
10621 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10622 else
10623 x = NULL_RTX;
10625 if (x == NULL_RTX)
10626 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10628 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10629 turn off all the bits that the shift would have turned off. */
10630 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10631 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10632 GET_MODE_MASK (result_mode) >> orig_count);
10634 /* Do the remainder of the processing in RESULT_MODE. */
10635 x = gen_lowpart_or_truncate (result_mode, x);
10637 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10638 operation. */
10639 if (complement_p)
10640 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10642 if (outer_op != UNKNOWN)
10644 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10645 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10646 outer_const = trunc_int_for_mode (outer_const, result_mode);
10648 if (outer_op == AND)
10649 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10650 else if (outer_op == SET)
10652 /* This means that we have determined that the result is
10653 equivalent to a constant. This should be rare. */
10654 if (!side_effects_p (x))
10655 x = GEN_INT (outer_const);
10657 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10658 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10659 else
10660 x = simplify_gen_binary (outer_op, result_mode, x,
10661 GEN_INT (outer_const));
10664 return x;
10667 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10668 The result of the shift is RESULT_MODE. If we cannot simplify it,
10669 return X or, if it is NULL, synthesize the expression with
10670 simplify_gen_binary. Otherwise, return a simplified value.
10672 The shift is normally computed in the widest mode we find in VAROP, as
10673 long as it isn't a different number of words than RESULT_MODE. Exceptions
10674 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10676 static rtx
10677 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10678 rtx varop, int count)
10680 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10681 if (tem)
10682 return tem;
10684 if (!x)
10685 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10686 if (GET_MODE (x) != result_mode)
10687 x = gen_lowpart (result_mode, x);
10688 return x;
10692 /* Like recog, but we receive the address of a pointer to a new pattern.
10693 We try to match the rtx that the pointer points to.
10694 If that fails, we may try to modify or replace the pattern,
10695 storing the replacement into the same pointer object.
10697 Modifications include deletion or addition of CLOBBERs.
10699 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10700 the CLOBBERs are placed.
10702 The value is the final insn code from the pattern ultimately matched,
10703 or -1. */
10705 static int
10706 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10708 rtx pat = *pnewpat;
10709 rtx pat_without_clobbers;
10710 int insn_code_number;
10711 int num_clobbers_to_add = 0;
10712 int i;
10713 rtx notes = NULL_RTX;
10714 rtx old_notes, old_pat;
10715 int old_icode;
10717 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10718 we use to indicate that something didn't match. If we find such a
10719 thing, force rejection. */
10720 if (GET_CODE (pat) == PARALLEL)
10721 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10722 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10723 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10724 return -1;
10726 old_pat = PATTERN (insn);
10727 old_notes = REG_NOTES (insn);
10728 PATTERN (insn) = pat;
10729 REG_NOTES (insn) = NULL_RTX;
10731 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10732 if (dump_file && (dump_flags & TDF_DETAILS))
10734 if (insn_code_number < 0)
10735 fputs ("Failed to match this instruction:\n", dump_file);
10736 else
10737 fputs ("Successfully matched this instruction:\n", dump_file);
10738 print_rtl_single (dump_file, pat);
10741 /* If it isn't, there is the possibility that we previously had an insn
10742 that clobbered some register as a side effect, but the combined
10743 insn doesn't need to do that. So try once more without the clobbers
10744 unless this represents an ASM insn. */
10746 if (insn_code_number < 0 && ! check_asm_operands (pat)
10747 && GET_CODE (pat) == PARALLEL)
10749 int pos;
10751 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10752 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10754 if (i != pos)
10755 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10756 pos++;
10759 SUBST_INT (XVECLEN (pat, 0), pos);
10761 if (pos == 1)
10762 pat = XVECEXP (pat, 0, 0);
10764 PATTERN (insn) = pat;
10765 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10766 if (dump_file && (dump_flags & TDF_DETAILS))
10768 if (insn_code_number < 0)
10769 fputs ("Failed to match this instruction:\n", dump_file);
10770 else
10771 fputs ("Successfully matched this instruction:\n", dump_file);
10772 print_rtl_single (dump_file, pat);
10776 pat_without_clobbers = pat;
10778 PATTERN (insn) = old_pat;
10779 REG_NOTES (insn) = old_notes;
10781 /* Recognize all noop sets, these will be killed by followup pass. */
10782 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10783 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10785 /* If we had any clobbers to add, make a new pattern than contains
10786 them. Then check to make sure that all of them are dead. */
10787 if (num_clobbers_to_add)
10789 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10790 rtvec_alloc (GET_CODE (pat) == PARALLEL
10791 ? (XVECLEN (pat, 0)
10792 + num_clobbers_to_add)
10793 : num_clobbers_to_add + 1));
10795 if (GET_CODE (pat) == PARALLEL)
10796 for (i = 0; i < XVECLEN (pat, 0); i++)
10797 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10798 else
10799 XVECEXP (newpat, 0, 0) = pat;
10801 add_clobbers (newpat, insn_code_number);
10803 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10804 i < XVECLEN (newpat, 0); i++)
10806 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10807 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10808 return -1;
10809 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10811 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10812 notes = alloc_reg_note (REG_UNUSED,
10813 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10816 pat = newpat;
10819 if (insn_code_number >= 0
10820 && insn_code_number != NOOP_MOVE_INSN_CODE)
10822 old_pat = PATTERN (insn);
10823 old_notes = REG_NOTES (insn);
10824 old_icode = INSN_CODE (insn);
10825 PATTERN (insn) = pat;
10826 REG_NOTES (insn) = notes;
10828 /* Allow targets to reject combined insn. */
10829 if (!targetm.legitimate_combined_insn (insn))
10831 if (dump_file && (dump_flags & TDF_DETAILS))
10832 fputs ("Instruction not appropriate for target.",
10833 dump_file);
10835 /* Callers expect recog_for_combine to strip
10836 clobbers from the pattern on failure. */
10837 pat = pat_without_clobbers;
10838 notes = NULL_RTX;
10840 insn_code_number = -1;
10843 PATTERN (insn) = old_pat;
10844 REG_NOTES (insn) = old_notes;
10845 INSN_CODE (insn) = old_icode;
10848 *pnewpat = pat;
10849 *pnotes = notes;
10851 return insn_code_number;
10854 /* Like gen_lowpart_general but for use by combine. In combine it
10855 is not possible to create any new pseudoregs. However, it is
10856 safe to create invalid memory addresses, because combine will
10857 try to recognize them and all they will do is make the combine
10858 attempt fail.
10860 If for some reason this cannot do its job, an rtx
10861 (clobber (const_int 0)) is returned.
10862 An insn containing that will not be recognized. */
10864 static rtx
10865 gen_lowpart_for_combine (machine_mode omode, rtx x)
10867 machine_mode imode = GET_MODE (x);
10868 unsigned int osize = GET_MODE_SIZE (omode);
10869 unsigned int isize = GET_MODE_SIZE (imode);
10870 rtx result;
10872 if (omode == imode)
10873 return x;
10875 /* We can only support MODE being wider than a word if X is a
10876 constant integer or has a mode the same size. */
10877 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10878 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10879 goto fail;
10881 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10882 won't know what to do. So we will strip off the SUBREG here and
10883 process normally. */
10884 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10886 x = SUBREG_REG (x);
10888 /* For use in case we fall down into the address adjustments
10889 further below, we need to adjust the known mode and size of
10890 x; imode and isize, since we just adjusted x. */
10891 imode = GET_MODE (x);
10893 if (imode == omode)
10894 return x;
10896 isize = GET_MODE_SIZE (imode);
10899 result = gen_lowpart_common (omode, x);
10901 if (result)
10902 return result;
10904 if (MEM_P (x))
10906 int offset = 0;
10908 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10909 address. */
10910 if (MEM_VOLATILE_P (x)
10911 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10912 goto fail;
10914 /* If we want to refer to something bigger than the original memref,
10915 generate a paradoxical subreg instead. That will force a reload
10916 of the original memref X. */
10917 if (isize < osize)
10918 return gen_rtx_SUBREG (omode, x, 0);
10920 if (WORDS_BIG_ENDIAN)
10921 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10923 /* Adjust the address so that the address-after-the-data is
10924 unchanged. */
10925 if (BYTES_BIG_ENDIAN)
10926 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10928 return adjust_address_nv (x, omode, offset);
10931 /* If X is a comparison operator, rewrite it in a new mode. This
10932 probably won't match, but may allow further simplifications. */
10933 else if (COMPARISON_P (x))
10934 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10936 /* If we couldn't simplify X any other way, just enclose it in a
10937 SUBREG. Normally, this SUBREG won't match, but some patterns may
10938 include an explicit SUBREG or we may simplify it further in combine. */
10939 else
10941 int offset = 0;
10942 rtx res;
10944 offset = subreg_lowpart_offset (omode, imode);
10945 if (imode == VOIDmode)
10947 imode = int_mode_for_mode (omode);
10948 x = gen_lowpart_common (imode, x);
10949 if (x == NULL)
10950 goto fail;
10952 res = simplify_gen_subreg (omode, x, imode, offset);
10953 if (res)
10954 return res;
10957 fail:
10958 return gen_rtx_CLOBBER (omode, const0_rtx);
10961 /* Try to simplify a comparison between OP0 and a constant OP1,
10962 where CODE is the comparison code that will be tested, into a
10963 (CODE OP0 const0_rtx) form.
10965 The result is a possibly different comparison code to use.
10966 *POP1 may be updated. */
10968 static enum rtx_code
10969 simplify_compare_const (enum rtx_code code, machine_mode mode,
10970 rtx op0, rtx *pop1)
10972 unsigned int mode_width = GET_MODE_PRECISION (mode);
10973 HOST_WIDE_INT const_op = INTVAL (*pop1);
10975 /* Get the constant we are comparing against and turn off all bits
10976 not on in our mode. */
10977 if (mode != VOIDmode)
10978 const_op = trunc_int_for_mode (const_op, mode);
10980 /* If we are comparing against a constant power of two and the value
10981 being compared can only have that single bit nonzero (e.g., it was
10982 `and'ed with that bit), we can replace this with a comparison
10983 with zero. */
10984 if (const_op
10985 && (code == EQ || code == NE || code == GE || code == GEU
10986 || code == LT || code == LTU)
10987 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10988 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
10989 && (nonzero_bits (op0, mode)
10990 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
10992 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10993 const_op = 0;
10996 /* Similarly, if we are comparing a value known to be either -1 or
10997 0 with -1, change it to the opposite comparison against zero. */
10998 if (const_op == -1
10999 && (code == EQ || code == NE || code == GT || code == LE
11000 || code == GEU || code == LTU)
11001 && num_sign_bit_copies (op0, mode) == mode_width)
11003 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11004 const_op = 0;
11007 /* Do some canonicalizations based on the comparison code. We prefer
11008 comparisons against zero and then prefer equality comparisons.
11009 If we can reduce the size of a constant, we will do that too. */
11010 switch (code)
11012 case LT:
11013 /* < C is equivalent to <= (C - 1) */
11014 if (const_op > 0)
11016 const_op -= 1;
11017 code = LE;
11018 /* ... fall through to LE case below. */
11020 else
11021 break;
11023 case LE:
11024 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11025 if (const_op < 0)
11027 const_op += 1;
11028 code = LT;
11031 /* If we are doing a <= 0 comparison on a value known to have
11032 a zero sign bit, we can replace this with == 0. */
11033 else if (const_op == 0
11034 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11035 && (nonzero_bits (op0, mode)
11036 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11037 == 0)
11038 code = EQ;
11039 break;
11041 case GE:
11042 /* >= C is equivalent to > (C - 1). */
11043 if (const_op > 0)
11045 const_op -= 1;
11046 code = GT;
11047 /* ... fall through to GT below. */
11049 else
11050 break;
11052 case GT:
11053 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11054 if (const_op < 0)
11056 const_op += 1;
11057 code = GE;
11060 /* If we are doing a > 0 comparison on a value known to have
11061 a zero sign bit, we can replace this with != 0. */
11062 else if (const_op == 0
11063 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11064 && (nonzero_bits (op0, mode)
11065 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11066 == 0)
11067 code = NE;
11068 break;
11070 case LTU:
11071 /* < C is equivalent to <= (C - 1). */
11072 if (const_op > 0)
11074 const_op -= 1;
11075 code = LEU;
11076 /* ... fall through ... */
11078 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11079 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11080 && (unsigned HOST_WIDE_INT) const_op
11081 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11083 const_op = 0;
11084 code = GE;
11085 break;
11087 else
11088 break;
11090 case LEU:
11091 /* unsigned <= 0 is equivalent to == 0 */
11092 if (const_op == 0)
11093 code = EQ;
11094 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11095 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11096 && (unsigned HOST_WIDE_INT) const_op
11097 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11099 const_op = 0;
11100 code = GE;
11102 break;
11104 case GEU:
11105 /* >= C is equivalent to > (C - 1). */
11106 if (const_op > 1)
11108 const_op -= 1;
11109 code = GTU;
11110 /* ... fall through ... */
11113 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11114 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11115 && (unsigned HOST_WIDE_INT) const_op
11116 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11118 const_op = 0;
11119 code = LT;
11120 break;
11122 else
11123 break;
11125 case GTU:
11126 /* unsigned > 0 is equivalent to != 0 */
11127 if (const_op == 0)
11128 code = NE;
11129 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11130 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11131 && (unsigned HOST_WIDE_INT) const_op
11132 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11134 const_op = 0;
11135 code = LT;
11137 break;
11139 default:
11140 break;
11143 *pop1 = GEN_INT (const_op);
11144 return code;
11147 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11148 comparison code that will be tested.
11150 The result is a possibly different comparison code to use. *POP0 and
11151 *POP1 may be updated.
11153 It is possible that we might detect that a comparison is either always
11154 true or always false. However, we do not perform general constant
11155 folding in combine, so this knowledge isn't useful. Such tautologies
11156 should have been detected earlier. Hence we ignore all such cases. */
11158 static enum rtx_code
11159 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11161 rtx op0 = *pop0;
11162 rtx op1 = *pop1;
11163 rtx tem, tem1;
11164 int i;
11165 machine_mode mode, tmode;
11167 /* Try a few ways of applying the same transformation to both operands. */
11168 while (1)
11170 #ifndef WORD_REGISTER_OPERATIONS
11171 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11172 so check specially. */
11173 if (code != GTU && code != GEU && code != LTU && code != LEU
11174 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11175 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11176 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11177 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11178 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11179 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11180 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11181 && CONST_INT_P (XEXP (op0, 1))
11182 && XEXP (op0, 1) == XEXP (op1, 1)
11183 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11184 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11185 && (INTVAL (XEXP (op0, 1))
11186 == (GET_MODE_PRECISION (GET_MODE (op0))
11187 - (GET_MODE_PRECISION
11188 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11190 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11191 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11193 #endif
11195 /* If both operands are the same constant shift, see if we can ignore the
11196 shift. We can if the shift is a rotate or if the bits shifted out of
11197 this shift are known to be zero for both inputs and if the type of
11198 comparison is compatible with the shift. */
11199 if (GET_CODE (op0) == GET_CODE (op1)
11200 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11201 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11202 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11203 && (code != GT && code != LT && code != GE && code != LE))
11204 || (GET_CODE (op0) == ASHIFTRT
11205 && (code != GTU && code != LTU
11206 && code != GEU && code != LEU)))
11207 && CONST_INT_P (XEXP (op0, 1))
11208 && INTVAL (XEXP (op0, 1)) >= 0
11209 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11210 && XEXP (op0, 1) == XEXP (op1, 1))
11212 machine_mode mode = GET_MODE (op0);
11213 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11214 int shift_count = INTVAL (XEXP (op0, 1));
11216 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11217 mask &= (mask >> shift_count) << shift_count;
11218 else if (GET_CODE (op0) == ASHIFT)
11219 mask = (mask & (mask << shift_count)) >> shift_count;
11221 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11222 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11223 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11224 else
11225 break;
11228 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11229 SUBREGs are of the same mode, and, in both cases, the AND would
11230 be redundant if the comparison was done in the narrower mode,
11231 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11232 and the operand's possibly nonzero bits are 0xffffff01; in that case
11233 if we only care about QImode, we don't need the AND). This case
11234 occurs if the output mode of an scc insn is not SImode and
11235 STORE_FLAG_VALUE == 1 (e.g., the 386).
11237 Similarly, check for a case where the AND's are ZERO_EXTEND
11238 operations from some narrower mode even though a SUBREG is not
11239 present. */
11241 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11242 && CONST_INT_P (XEXP (op0, 1))
11243 && CONST_INT_P (XEXP (op1, 1)))
11245 rtx inner_op0 = XEXP (op0, 0);
11246 rtx inner_op1 = XEXP (op1, 0);
11247 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11248 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11249 int changed = 0;
11251 if (paradoxical_subreg_p (inner_op0)
11252 && GET_CODE (inner_op1) == SUBREG
11253 && (GET_MODE (SUBREG_REG (inner_op0))
11254 == GET_MODE (SUBREG_REG (inner_op1)))
11255 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11256 <= HOST_BITS_PER_WIDE_INT)
11257 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11258 GET_MODE (SUBREG_REG (inner_op0)))))
11259 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11260 GET_MODE (SUBREG_REG (inner_op1))))))
11262 op0 = SUBREG_REG (inner_op0);
11263 op1 = SUBREG_REG (inner_op1);
11265 /* The resulting comparison is always unsigned since we masked
11266 off the original sign bit. */
11267 code = unsigned_condition (code);
11269 changed = 1;
11272 else if (c0 == c1)
11273 for (tmode = GET_CLASS_NARROWEST_MODE
11274 (GET_MODE_CLASS (GET_MODE (op0)));
11275 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11276 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11278 op0 = gen_lowpart (tmode, inner_op0);
11279 op1 = gen_lowpart (tmode, inner_op1);
11280 code = unsigned_condition (code);
11281 changed = 1;
11282 break;
11285 if (! changed)
11286 break;
11289 /* If both operands are NOT, we can strip off the outer operation
11290 and adjust the comparison code for swapped operands; similarly for
11291 NEG, except that this must be an equality comparison. */
11292 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11293 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11294 && (code == EQ || code == NE)))
11295 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11297 else
11298 break;
11301 /* If the first operand is a constant, swap the operands and adjust the
11302 comparison code appropriately, but don't do this if the second operand
11303 is already a constant integer. */
11304 if (swap_commutative_operands_p (op0, op1))
11306 tem = op0, op0 = op1, op1 = tem;
11307 code = swap_condition (code);
11310 /* We now enter a loop during which we will try to simplify the comparison.
11311 For the most part, we only are concerned with comparisons with zero,
11312 but some things may really be comparisons with zero but not start
11313 out looking that way. */
11315 while (CONST_INT_P (op1))
11317 machine_mode mode = GET_MODE (op0);
11318 unsigned int mode_width = GET_MODE_PRECISION (mode);
11319 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11320 int equality_comparison_p;
11321 int sign_bit_comparison_p;
11322 int unsigned_comparison_p;
11323 HOST_WIDE_INT const_op;
11325 /* We only want to handle integral modes. This catches VOIDmode,
11326 CCmode, and the floating-point modes. An exception is that we
11327 can handle VOIDmode if OP0 is a COMPARE or a comparison
11328 operation. */
11330 if (GET_MODE_CLASS (mode) != MODE_INT
11331 && ! (mode == VOIDmode
11332 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11333 break;
11335 /* Try to simplify the compare to constant, possibly changing the
11336 comparison op, and/or changing op1 to zero. */
11337 code = simplify_compare_const (code, mode, op0, &op1);
11338 const_op = INTVAL (op1);
11340 /* Compute some predicates to simplify code below. */
11342 equality_comparison_p = (code == EQ || code == NE);
11343 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11344 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11345 || code == GEU);
11347 /* If this is a sign bit comparison and we can do arithmetic in
11348 MODE, say that we will only be needing the sign bit of OP0. */
11349 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11350 op0 = force_to_mode (op0, mode,
11351 (unsigned HOST_WIDE_INT) 1
11352 << (GET_MODE_PRECISION (mode) - 1),
11355 /* Now try cases based on the opcode of OP0. If none of the cases
11356 does a "continue", we exit this loop immediately after the
11357 switch. */
11359 switch (GET_CODE (op0))
11361 case ZERO_EXTRACT:
11362 /* If we are extracting a single bit from a variable position in
11363 a constant that has only a single bit set and are comparing it
11364 with zero, we can convert this into an equality comparison
11365 between the position and the location of the single bit. */
11366 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11367 have already reduced the shift count modulo the word size. */
11368 if (!SHIFT_COUNT_TRUNCATED
11369 && CONST_INT_P (XEXP (op0, 0))
11370 && XEXP (op0, 1) == const1_rtx
11371 && equality_comparison_p && const_op == 0
11372 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11374 if (BITS_BIG_ENDIAN)
11375 i = BITS_PER_WORD - 1 - i;
11377 op0 = XEXP (op0, 2);
11378 op1 = GEN_INT (i);
11379 const_op = i;
11381 /* Result is nonzero iff shift count is equal to I. */
11382 code = reverse_condition (code);
11383 continue;
11386 /* ... fall through ... */
11388 case SIGN_EXTRACT:
11389 tem = expand_compound_operation (op0);
11390 if (tem != op0)
11392 op0 = tem;
11393 continue;
11395 break;
11397 case NOT:
11398 /* If testing for equality, we can take the NOT of the constant. */
11399 if (equality_comparison_p
11400 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11402 op0 = XEXP (op0, 0);
11403 op1 = tem;
11404 continue;
11407 /* If just looking at the sign bit, reverse the sense of the
11408 comparison. */
11409 if (sign_bit_comparison_p)
11411 op0 = XEXP (op0, 0);
11412 code = (code == GE ? LT : GE);
11413 continue;
11415 break;
11417 case NEG:
11418 /* If testing for equality, we can take the NEG of the constant. */
11419 if (equality_comparison_p
11420 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11422 op0 = XEXP (op0, 0);
11423 op1 = tem;
11424 continue;
11427 /* The remaining cases only apply to comparisons with zero. */
11428 if (const_op != 0)
11429 break;
11431 /* When X is ABS or is known positive,
11432 (neg X) is < 0 if and only if X != 0. */
11434 if (sign_bit_comparison_p
11435 && (GET_CODE (XEXP (op0, 0)) == ABS
11436 || (mode_width <= HOST_BITS_PER_WIDE_INT
11437 && (nonzero_bits (XEXP (op0, 0), mode)
11438 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11439 == 0)))
11441 op0 = XEXP (op0, 0);
11442 code = (code == LT ? NE : EQ);
11443 continue;
11446 /* If we have NEG of something whose two high-order bits are the
11447 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11448 if (num_sign_bit_copies (op0, mode) >= 2)
11450 op0 = XEXP (op0, 0);
11451 code = swap_condition (code);
11452 continue;
11454 break;
11456 case ROTATE:
11457 /* If we are testing equality and our count is a constant, we
11458 can perform the inverse operation on our RHS. */
11459 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11460 && (tem = simplify_binary_operation (ROTATERT, mode,
11461 op1, XEXP (op0, 1))) != 0)
11463 op0 = XEXP (op0, 0);
11464 op1 = tem;
11465 continue;
11468 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11469 a particular bit. Convert it to an AND of a constant of that
11470 bit. This will be converted into a ZERO_EXTRACT. */
11471 if (const_op == 0 && sign_bit_comparison_p
11472 && CONST_INT_P (XEXP (op0, 1))
11473 && mode_width <= HOST_BITS_PER_WIDE_INT)
11475 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11476 ((unsigned HOST_WIDE_INT) 1
11477 << (mode_width - 1
11478 - INTVAL (XEXP (op0, 1)))));
11479 code = (code == LT ? NE : EQ);
11480 continue;
11483 /* Fall through. */
11485 case ABS:
11486 /* ABS is ignorable inside an equality comparison with zero. */
11487 if (const_op == 0 && equality_comparison_p)
11489 op0 = XEXP (op0, 0);
11490 continue;
11492 break;
11494 case SIGN_EXTEND:
11495 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11496 (compare FOO CONST) if CONST fits in FOO's mode and we
11497 are either testing inequality or have an unsigned
11498 comparison with ZERO_EXTEND or a signed comparison with
11499 SIGN_EXTEND. But don't do it if we don't have a compare
11500 insn of the given mode, since we'd have to revert it
11501 later on, and then we wouldn't know whether to sign- or
11502 zero-extend. */
11503 mode = GET_MODE (XEXP (op0, 0));
11504 if (GET_MODE_CLASS (mode) == MODE_INT
11505 && ! unsigned_comparison_p
11506 && HWI_COMPUTABLE_MODE_P (mode)
11507 && trunc_int_for_mode (const_op, mode) == const_op
11508 && have_insn_for (COMPARE, mode))
11510 op0 = XEXP (op0, 0);
11511 continue;
11513 break;
11515 case SUBREG:
11516 /* Check for the case where we are comparing A - C1 with C2, that is
11518 (subreg:MODE (plus (A) (-C1))) op (C2)
11520 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11521 comparison in the wider mode. One of the following two conditions
11522 must be true in order for this to be valid:
11524 1. The mode extension results in the same bit pattern being added
11525 on both sides and the comparison is equality or unsigned. As
11526 C2 has been truncated to fit in MODE, the pattern can only be
11527 all 0s or all 1s.
11529 2. The mode extension results in the sign bit being copied on
11530 each side.
11532 The difficulty here is that we have predicates for A but not for
11533 (A - C1) so we need to check that C1 is within proper bounds so
11534 as to perturbate A as little as possible. */
11536 if (mode_width <= HOST_BITS_PER_WIDE_INT
11537 && subreg_lowpart_p (op0)
11538 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11539 && GET_CODE (SUBREG_REG (op0)) == PLUS
11540 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11542 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11543 rtx a = XEXP (SUBREG_REG (op0), 0);
11544 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11546 if ((c1 > 0
11547 && (unsigned HOST_WIDE_INT) c1
11548 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11549 && (equality_comparison_p || unsigned_comparison_p)
11550 /* (A - C1) zero-extends if it is positive and sign-extends
11551 if it is negative, C2 both zero- and sign-extends. */
11552 && ((0 == (nonzero_bits (a, inner_mode)
11553 & ~GET_MODE_MASK (mode))
11554 && const_op >= 0)
11555 /* (A - C1) sign-extends if it is positive and 1-extends
11556 if it is negative, C2 both sign- and 1-extends. */
11557 || (num_sign_bit_copies (a, inner_mode)
11558 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11559 - mode_width)
11560 && const_op < 0)))
11561 || ((unsigned HOST_WIDE_INT) c1
11562 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11563 /* (A - C1) always sign-extends, like C2. */
11564 && num_sign_bit_copies (a, inner_mode)
11565 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11566 - (mode_width - 1))))
11568 op0 = SUBREG_REG (op0);
11569 continue;
11573 /* If the inner mode is narrower and we are extracting the low part,
11574 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11575 if (subreg_lowpart_p (op0)
11576 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11577 /* Fall through */ ;
11578 else
11579 break;
11581 /* ... fall through ... */
11583 case ZERO_EXTEND:
11584 mode = GET_MODE (XEXP (op0, 0));
11585 if (GET_MODE_CLASS (mode) == MODE_INT
11586 && (unsigned_comparison_p || equality_comparison_p)
11587 && HWI_COMPUTABLE_MODE_P (mode)
11588 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11589 && const_op >= 0
11590 && have_insn_for (COMPARE, mode))
11592 op0 = XEXP (op0, 0);
11593 continue;
11595 break;
11597 case PLUS:
11598 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11599 this for equality comparisons due to pathological cases involving
11600 overflows. */
11601 if (equality_comparison_p
11602 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11603 op1, XEXP (op0, 1))))
11605 op0 = XEXP (op0, 0);
11606 op1 = tem;
11607 continue;
11610 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11611 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11612 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11614 op0 = XEXP (XEXP (op0, 0), 0);
11615 code = (code == LT ? EQ : NE);
11616 continue;
11618 break;
11620 case MINUS:
11621 /* We used to optimize signed comparisons against zero, but that
11622 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11623 arrive here as equality comparisons, or (GEU, LTU) are
11624 optimized away. No need to special-case them. */
11626 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11627 (eq B (minus A C)), whichever simplifies. We can only do
11628 this for equality comparisons due to pathological cases involving
11629 overflows. */
11630 if (equality_comparison_p
11631 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11632 XEXP (op0, 1), op1)))
11634 op0 = XEXP (op0, 0);
11635 op1 = tem;
11636 continue;
11639 if (equality_comparison_p
11640 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11641 XEXP (op0, 0), op1)))
11643 op0 = XEXP (op0, 1);
11644 op1 = tem;
11645 continue;
11648 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11649 of bits in X minus 1, is one iff X > 0. */
11650 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11651 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11652 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11653 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11655 op0 = XEXP (op0, 1);
11656 code = (code == GE ? LE : GT);
11657 continue;
11659 break;
11661 case XOR:
11662 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11663 if C is zero or B is a constant. */
11664 if (equality_comparison_p
11665 && 0 != (tem = simplify_binary_operation (XOR, mode,
11666 XEXP (op0, 1), op1)))
11668 op0 = XEXP (op0, 0);
11669 op1 = tem;
11670 continue;
11672 break;
11674 case EQ: case NE:
11675 case UNEQ: case LTGT:
11676 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11677 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11678 case UNORDERED: case ORDERED:
11679 /* We can't do anything if OP0 is a condition code value, rather
11680 than an actual data value. */
11681 if (const_op != 0
11682 || CC0_P (XEXP (op0, 0))
11683 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11684 break;
11686 /* Get the two operands being compared. */
11687 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11688 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11689 else
11690 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11692 /* Check for the cases where we simply want the result of the
11693 earlier test or the opposite of that result. */
11694 if (code == NE || code == EQ
11695 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11696 && (code == LT || code == GE)))
11698 enum rtx_code new_code;
11699 if (code == LT || code == NE)
11700 new_code = GET_CODE (op0);
11701 else
11702 new_code = reversed_comparison_code (op0, NULL);
11704 if (new_code != UNKNOWN)
11706 code = new_code;
11707 op0 = tem;
11708 op1 = tem1;
11709 continue;
11712 break;
11714 case IOR:
11715 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11716 iff X <= 0. */
11717 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11718 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11719 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11721 op0 = XEXP (op0, 1);
11722 code = (code == GE ? GT : LE);
11723 continue;
11725 break;
11727 case AND:
11728 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11729 will be converted to a ZERO_EXTRACT later. */
11730 if (const_op == 0 && equality_comparison_p
11731 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11732 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11734 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11735 XEXP (XEXP (op0, 0), 1));
11736 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11737 continue;
11740 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11741 zero and X is a comparison and C1 and C2 describe only bits set
11742 in STORE_FLAG_VALUE, we can compare with X. */
11743 if (const_op == 0 && equality_comparison_p
11744 && mode_width <= HOST_BITS_PER_WIDE_INT
11745 && CONST_INT_P (XEXP (op0, 1))
11746 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11747 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11748 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11749 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11751 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11752 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11753 if ((~STORE_FLAG_VALUE & mask) == 0
11754 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11755 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11756 && COMPARISON_P (tem))))
11758 op0 = XEXP (XEXP (op0, 0), 0);
11759 continue;
11763 /* If we are doing an equality comparison of an AND of a bit equal
11764 to the sign bit, replace this with a LT or GE comparison of
11765 the underlying value. */
11766 if (equality_comparison_p
11767 && const_op == 0
11768 && CONST_INT_P (XEXP (op0, 1))
11769 && mode_width <= HOST_BITS_PER_WIDE_INT
11770 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11771 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11773 op0 = XEXP (op0, 0);
11774 code = (code == EQ ? GE : LT);
11775 continue;
11778 /* If this AND operation is really a ZERO_EXTEND from a narrower
11779 mode, the constant fits within that mode, and this is either an
11780 equality or unsigned comparison, try to do this comparison in
11781 the narrower mode.
11783 Note that in:
11785 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11786 -> (ne:DI (reg:SI 4) (const_int 0))
11788 unless TRULY_NOOP_TRUNCATION allows it or the register is
11789 known to hold a value of the required mode the
11790 transformation is invalid. */
11791 if ((equality_comparison_p || unsigned_comparison_p)
11792 && CONST_INT_P (XEXP (op0, 1))
11793 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11794 & GET_MODE_MASK (mode))
11795 + 1)) >= 0
11796 && const_op >> i == 0
11797 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11798 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11799 || (REG_P (XEXP (op0, 0))
11800 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11802 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11803 continue;
11806 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11807 fits in both M1 and M2 and the SUBREG is either paradoxical
11808 or represents the low part, permute the SUBREG and the AND
11809 and try again. */
11810 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11812 unsigned HOST_WIDE_INT c1;
11813 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11814 /* Require an integral mode, to avoid creating something like
11815 (AND:SF ...). */
11816 if (SCALAR_INT_MODE_P (tmode)
11817 /* It is unsafe to commute the AND into the SUBREG if the
11818 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11819 not defined. As originally written the upper bits
11820 have a defined value due to the AND operation.
11821 However, if we commute the AND inside the SUBREG then
11822 they no longer have defined values and the meaning of
11823 the code has been changed. */
11824 && (0
11825 #ifdef WORD_REGISTER_OPERATIONS
11826 || (mode_width > GET_MODE_PRECISION (tmode)
11827 && mode_width <= BITS_PER_WORD)
11828 #endif
11829 || (mode_width <= GET_MODE_PRECISION (tmode)
11830 && subreg_lowpart_p (XEXP (op0, 0))))
11831 && CONST_INT_P (XEXP (op0, 1))
11832 && mode_width <= HOST_BITS_PER_WIDE_INT
11833 && HWI_COMPUTABLE_MODE_P (tmode)
11834 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11835 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11836 && c1 != mask
11837 && c1 != GET_MODE_MASK (tmode))
11839 op0 = simplify_gen_binary (AND, tmode,
11840 SUBREG_REG (XEXP (op0, 0)),
11841 gen_int_mode (c1, tmode));
11842 op0 = gen_lowpart (mode, op0);
11843 continue;
11847 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11848 if (const_op == 0 && equality_comparison_p
11849 && XEXP (op0, 1) == const1_rtx
11850 && GET_CODE (XEXP (op0, 0)) == NOT)
11852 op0 = simplify_and_const_int (NULL_RTX, mode,
11853 XEXP (XEXP (op0, 0), 0), 1);
11854 code = (code == NE ? EQ : NE);
11855 continue;
11858 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11859 (eq (and (lshiftrt X) 1) 0).
11860 Also handle the case where (not X) is expressed using xor. */
11861 if (const_op == 0 && equality_comparison_p
11862 && XEXP (op0, 1) == const1_rtx
11863 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11865 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11866 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11868 if (GET_CODE (shift_op) == NOT
11869 || (GET_CODE (shift_op) == XOR
11870 && CONST_INT_P (XEXP (shift_op, 1))
11871 && CONST_INT_P (shift_count)
11872 && HWI_COMPUTABLE_MODE_P (mode)
11873 && (UINTVAL (XEXP (shift_op, 1))
11874 == (unsigned HOST_WIDE_INT) 1
11875 << INTVAL (shift_count))))
11878 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11879 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11880 code = (code == NE ? EQ : NE);
11881 continue;
11884 break;
11886 case ASHIFT:
11887 /* If we have (compare (ashift FOO N) (const_int C)) and
11888 the high order N bits of FOO (N+1 if an inequality comparison)
11889 are known to be zero, we can do this by comparing FOO with C
11890 shifted right N bits so long as the low-order N bits of C are
11891 zero. */
11892 if (CONST_INT_P (XEXP (op0, 1))
11893 && INTVAL (XEXP (op0, 1)) >= 0
11894 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11895 < HOST_BITS_PER_WIDE_INT)
11896 && (((unsigned HOST_WIDE_INT) const_op
11897 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11898 - 1)) == 0)
11899 && mode_width <= HOST_BITS_PER_WIDE_INT
11900 && (nonzero_bits (XEXP (op0, 0), mode)
11901 & ~(mask >> (INTVAL (XEXP (op0, 1))
11902 + ! equality_comparison_p))) == 0)
11904 /* We must perform a logical shift, not an arithmetic one,
11905 as we want the top N bits of C to be zero. */
11906 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11908 temp >>= INTVAL (XEXP (op0, 1));
11909 op1 = gen_int_mode (temp, mode);
11910 op0 = XEXP (op0, 0);
11911 continue;
11914 /* If we are doing a sign bit comparison, it means we are testing
11915 a particular bit. Convert it to the appropriate AND. */
11916 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11917 && mode_width <= HOST_BITS_PER_WIDE_INT)
11919 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11920 ((unsigned HOST_WIDE_INT) 1
11921 << (mode_width - 1
11922 - INTVAL (XEXP (op0, 1)))));
11923 code = (code == LT ? NE : EQ);
11924 continue;
11927 /* If this an equality comparison with zero and we are shifting
11928 the low bit to the sign bit, we can convert this to an AND of the
11929 low-order bit. */
11930 if (const_op == 0 && equality_comparison_p
11931 && CONST_INT_P (XEXP (op0, 1))
11932 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11934 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11935 continue;
11937 break;
11939 case ASHIFTRT:
11940 /* If this is an equality comparison with zero, we can do this
11941 as a logical shift, which might be much simpler. */
11942 if (equality_comparison_p && const_op == 0
11943 && CONST_INT_P (XEXP (op0, 1)))
11945 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11946 XEXP (op0, 0),
11947 INTVAL (XEXP (op0, 1)));
11948 continue;
11951 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11952 do the comparison in a narrower mode. */
11953 if (! unsigned_comparison_p
11954 && CONST_INT_P (XEXP (op0, 1))
11955 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11956 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11957 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11958 MODE_INT, 1)) != BLKmode
11959 && (((unsigned HOST_WIDE_INT) const_op
11960 + (GET_MODE_MASK (tmode) >> 1) + 1)
11961 <= GET_MODE_MASK (tmode)))
11963 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11964 continue;
11967 /* Likewise if OP0 is a PLUS of a sign extension with a
11968 constant, which is usually represented with the PLUS
11969 between the shifts. */
11970 if (! unsigned_comparison_p
11971 && CONST_INT_P (XEXP (op0, 1))
11972 && GET_CODE (XEXP (op0, 0)) == PLUS
11973 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11974 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11975 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11976 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11977 MODE_INT, 1)) != BLKmode
11978 && (((unsigned HOST_WIDE_INT) const_op
11979 + (GET_MODE_MASK (tmode) >> 1) + 1)
11980 <= GET_MODE_MASK (tmode)))
11982 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11983 rtx add_const = XEXP (XEXP (op0, 0), 1);
11984 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11985 add_const, XEXP (op0, 1));
11987 op0 = simplify_gen_binary (PLUS, tmode,
11988 gen_lowpart (tmode, inner),
11989 new_const);
11990 continue;
11993 /* ... fall through ... */
11994 case LSHIFTRT:
11995 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11996 the low order N bits of FOO are known to be zero, we can do this
11997 by comparing FOO with C shifted left N bits so long as no
11998 overflow occurs. Even if the low order N bits of FOO aren't known
11999 to be zero, if the comparison is >= or < we can use the same
12000 optimization and for > or <= by setting all the low
12001 order N bits in the comparison constant. */
12002 if (CONST_INT_P (XEXP (op0, 1))
12003 && INTVAL (XEXP (op0, 1)) > 0
12004 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12005 && mode_width <= HOST_BITS_PER_WIDE_INT
12006 && (((unsigned HOST_WIDE_INT) const_op
12007 + (GET_CODE (op0) != LSHIFTRT
12008 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12009 + 1)
12010 : 0))
12011 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12013 unsigned HOST_WIDE_INT low_bits
12014 = (nonzero_bits (XEXP (op0, 0), mode)
12015 & (((unsigned HOST_WIDE_INT) 1
12016 << INTVAL (XEXP (op0, 1))) - 1));
12017 if (low_bits == 0 || !equality_comparison_p)
12019 /* If the shift was logical, then we must make the condition
12020 unsigned. */
12021 if (GET_CODE (op0) == LSHIFTRT)
12022 code = unsigned_condition (code);
12024 const_op <<= INTVAL (XEXP (op0, 1));
12025 if (low_bits != 0
12026 && (code == GT || code == GTU
12027 || code == LE || code == LEU))
12028 const_op
12029 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12030 op1 = GEN_INT (const_op);
12031 op0 = XEXP (op0, 0);
12032 continue;
12036 /* If we are using this shift to extract just the sign bit, we
12037 can replace this with an LT or GE comparison. */
12038 if (const_op == 0
12039 && (equality_comparison_p || sign_bit_comparison_p)
12040 && CONST_INT_P (XEXP (op0, 1))
12041 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12043 op0 = XEXP (op0, 0);
12044 code = (code == NE || code == GT ? LT : GE);
12045 continue;
12047 break;
12049 default:
12050 break;
12053 break;
12056 /* Now make any compound operations involved in this comparison. Then,
12057 check for an outmost SUBREG on OP0 that is not doing anything or is
12058 paradoxical. The latter transformation must only be performed when
12059 it is known that the "extra" bits will be the same in op0 and op1 or
12060 that they don't matter. There are three cases to consider:
12062 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12063 care bits and we can assume they have any convenient value. So
12064 making the transformation is safe.
12066 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12067 In this case the upper bits of op0 are undefined. We should not make
12068 the simplification in that case as we do not know the contents of
12069 those bits.
12071 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12072 UNKNOWN. In that case we know those bits are zeros or ones. We must
12073 also be sure that they are the same as the upper bits of op1.
12075 We can never remove a SUBREG for a non-equality comparison because
12076 the sign bit is in a different place in the underlying object. */
12078 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12079 op1 = make_compound_operation (op1, SET);
12081 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12082 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12083 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12084 && (code == NE || code == EQ))
12086 if (paradoxical_subreg_p (op0))
12088 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12089 implemented. */
12090 if (REG_P (SUBREG_REG (op0)))
12092 op0 = SUBREG_REG (op0);
12093 op1 = gen_lowpart (GET_MODE (op0), op1);
12096 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12097 <= HOST_BITS_PER_WIDE_INT)
12098 && (nonzero_bits (SUBREG_REG (op0),
12099 GET_MODE (SUBREG_REG (op0)))
12100 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12102 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12104 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12105 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12106 op0 = SUBREG_REG (op0), op1 = tem;
12110 /* We now do the opposite procedure: Some machines don't have compare
12111 insns in all modes. If OP0's mode is an integer mode smaller than a
12112 word and we can't do a compare in that mode, see if there is a larger
12113 mode for which we can do the compare. There are a number of cases in
12114 which we can use the wider mode. */
12116 mode = GET_MODE (op0);
12117 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12118 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12119 && ! have_insn_for (COMPARE, mode))
12120 for (tmode = GET_MODE_WIDER_MODE (mode);
12121 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12122 tmode = GET_MODE_WIDER_MODE (tmode))
12123 if (have_insn_for (COMPARE, tmode))
12125 int zero_extended;
12127 /* If this is a test for negative, we can make an explicit
12128 test of the sign bit. Test this first so we can use
12129 a paradoxical subreg to extend OP0. */
12131 if (op1 == const0_rtx && (code == LT || code == GE)
12132 && HWI_COMPUTABLE_MODE_P (mode))
12134 unsigned HOST_WIDE_INT sign
12135 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12136 op0 = simplify_gen_binary (AND, tmode,
12137 gen_lowpart (tmode, op0),
12138 gen_int_mode (sign, tmode));
12139 code = (code == LT) ? NE : EQ;
12140 break;
12143 /* If the only nonzero bits in OP0 and OP1 are those in the
12144 narrower mode and this is an equality or unsigned comparison,
12145 we can use the wider mode. Similarly for sign-extended
12146 values, in which case it is true for all comparisons. */
12147 zero_extended = ((code == EQ || code == NE
12148 || code == GEU || code == GTU
12149 || code == LEU || code == LTU)
12150 && (nonzero_bits (op0, tmode)
12151 & ~GET_MODE_MASK (mode)) == 0
12152 && ((CONST_INT_P (op1)
12153 || (nonzero_bits (op1, tmode)
12154 & ~GET_MODE_MASK (mode)) == 0)));
12156 if (zero_extended
12157 || ((num_sign_bit_copies (op0, tmode)
12158 > (unsigned int) (GET_MODE_PRECISION (tmode)
12159 - GET_MODE_PRECISION (mode)))
12160 && (num_sign_bit_copies (op1, tmode)
12161 > (unsigned int) (GET_MODE_PRECISION (tmode)
12162 - GET_MODE_PRECISION (mode)))))
12164 /* If OP0 is an AND and we don't have an AND in MODE either,
12165 make a new AND in the proper mode. */
12166 if (GET_CODE (op0) == AND
12167 && !have_insn_for (AND, mode))
12168 op0 = simplify_gen_binary (AND, tmode,
12169 gen_lowpart (tmode,
12170 XEXP (op0, 0)),
12171 gen_lowpart (tmode,
12172 XEXP (op0, 1)));
12173 else
12175 if (zero_extended)
12177 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12178 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12180 else
12182 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12183 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12185 break;
12190 /* We may have changed the comparison operands. Re-canonicalize. */
12191 if (swap_commutative_operands_p (op0, op1))
12193 tem = op0, op0 = op1, op1 = tem;
12194 code = swap_condition (code);
12197 /* If this machine only supports a subset of valid comparisons, see if we
12198 can convert an unsupported one into a supported one. */
12199 target_canonicalize_comparison (&code, &op0, &op1, 0);
12201 *pop0 = op0;
12202 *pop1 = op1;
12204 return code;
12207 /* Utility function for record_value_for_reg. Count number of
12208 rtxs in X. */
12209 static int
12210 count_rtxs (rtx x)
12212 enum rtx_code code = GET_CODE (x);
12213 const char *fmt;
12214 int i, j, ret = 1;
12216 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12217 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12219 rtx x0 = XEXP (x, 0);
12220 rtx x1 = XEXP (x, 1);
12222 if (x0 == x1)
12223 return 1 + 2 * count_rtxs (x0);
12225 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12226 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12227 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12228 return 2 + 2 * count_rtxs (x0)
12229 + count_rtxs (x == XEXP (x1, 0)
12230 ? XEXP (x1, 1) : XEXP (x1, 0));
12232 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12233 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12234 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12235 return 2 + 2 * count_rtxs (x1)
12236 + count_rtxs (x == XEXP (x0, 0)
12237 ? XEXP (x0, 1) : XEXP (x0, 0));
12240 fmt = GET_RTX_FORMAT (code);
12241 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12242 if (fmt[i] == 'e')
12243 ret += count_rtxs (XEXP (x, i));
12244 else if (fmt[i] == 'E')
12245 for (j = 0; j < XVECLEN (x, i); j++)
12246 ret += count_rtxs (XVECEXP (x, i, j));
12248 return ret;
12251 /* Utility function for following routine. Called when X is part of a value
12252 being stored into last_set_value. Sets last_set_table_tick
12253 for each register mentioned. Similar to mention_regs in cse.c */
12255 static void
12256 update_table_tick (rtx x)
12258 enum rtx_code code = GET_CODE (x);
12259 const char *fmt = GET_RTX_FORMAT (code);
12260 int i, j;
12262 if (code == REG)
12264 unsigned int regno = REGNO (x);
12265 unsigned int endregno = END_REGNO (x);
12266 unsigned int r;
12268 for (r = regno; r < endregno; r++)
12270 reg_stat_type *rsp = &reg_stat[r];
12271 rsp->last_set_table_tick = label_tick;
12274 return;
12277 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12278 if (fmt[i] == 'e')
12280 /* Check for identical subexpressions. If x contains
12281 identical subexpression we only have to traverse one of
12282 them. */
12283 if (i == 0 && ARITHMETIC_P (x))
12285 /* Note that at this point x1 has already been
12286 processed. */
12287 rtx x0 = XEXP (x, 0);
12288 rtx x1 = XEXP (x, 1);
12290 /* If x0 and x1 are identical then there is no need to
12291 process x0. */
12292 if (x0 == x1)
12293 break;
12295 /* If x0 is identical to a subexpression of x1 then while
12296 processing x1, x0 has already been processed. Thus we
12297 are done with x. */
12298 if (ARITHMETIC_P (x1)
12299 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12300 break;
12302 /* If x1 is identical to a subexpression of x0 then we
12303 still have to process the rest of x0. */
12304 if (ARITHMETIC_P (x0)
12305 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12307 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12308 break;
12312 update_table_tick (XEXP (x, i));
12314 else if (fmt[i] == 'E')
12315 for (j = 0; j < XVECLEN (x, i); j++)
12316 update_table_tick (XVECEXP (x, i, j));
12319 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12320 are saying that the register is clobbered and we no longer know its
12321 value. If INSN is zero, don't update reg_stat[].last_set; this is
12322 only permitted with VALUE also zero and is used to invalidate the
12323 register. */
12325 static void
12326 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12328 unsigned int regno = REGNO (reg);
12329 unsigned int endregno = END_REGNO (reg);
12330 unsigned int i;
12331 reg_stat_type *rsp;
12333 /* If VALUE contains REG and we have a previous value for REG, substitute
12334 the previous value. */
12335 if (value && insn && reg_overlap_mentioned_p (reg, value))
12337 rtx tem;
12339 /* Set things up so get_last_value is allowed to see anything set up to
12340 our insn. */
12341 subst_low_luid = DF_INSN_LUID (insn);
12342 tem = get_last_value (reg);
12344 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12345 it isn't going to be useful and will take a lot of time to process,
12346 so just use the CLOBBER. */
12348 if (tem)
12350 if (ARITHMETIC_P (tem)
12351 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12352 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12353 tem = XEXP (tem, 0);
12354 else if (count_occurrences (value, reg, 1) >= 2)
12356 /* If there are two or more occurrences of REG in VALUE,
12357 prevent the value from growing too much. */
12358 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12359 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12362 value = replace_rtx (copy_rtx (value), reg, tem);
12366 /* For each register modified, show we don't know its value, that
12367 we don't know about its bitwise content, that its value has been
12368 updated, and that we don't know the location of the death of the
12369 register. */
12370 for (i = regno; i < endregno; i++)
12372 rsp = &reg_stat[i];
12374 if (insn)
12375 rsp->last_set = insn;
12377 rsp->last_set_value = 0;
12378 rsp->last_set_mode = VOIDmode;
12379 rsp->last_set_nonzero_bits = 0;
12380 rsp->last_set_sign_bit_copies = 0;
12381 rsp->last_death = 0;
12382 rsp->truncated_to_mode = VOIDmode;
12385 /* Mark registers that are being referenced in this value. */
12386 if (value)
12387 update_table_tick (value);
12389 /* Now update the status of each register being set.
12390 If someone is using this register in this block, set this register
12391 to invalid since we will get confused between the two lives in this
12392 basic block. This makes using this register always invalid. In cse, we
12393 scan the table to invalidate all entries using this register, but this
12394 is too much work for us. */
12396 for (i = regno; i < endregno; i++)
12398 rsp = &reg_stat[i];
12399 rsp->last_set_label = label_tick;
12400 if (!insn
12401 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12402 rsp->last_set_invalid = 1;
12403 else
12404 rsp->last_set_invalid = 0;
12407 /* The value being assigned might refer to X (like in "x++;"). In that
12408 case, we must replace it with (clobber (const_int 0)) to prevent
12409 infinite loops. */
12410 rsp = &reg_stat[regno];
12411 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12413 value = copy_rtx (value);
12414 if (!get_last_value_validate (&value, insn, label_tick, 1))
12415 value = 0;
12418 /* For the main register being modified, update the value, the mode, the
12419 nonzero bits, and the number of sign bit copies. */
12421 rsp->last_set_value = value;
12423 if (value)
12425 machine_mode mode = GET_MODE (reg);
12426 subst_low_luid = DF_INSN_LUID (insn);
12427 rsp->last_set_mode = mode;
12428 if (GET_MODE_CLASS (mode) == MODE_INT
12429 && HWI_COMPUTABLE_MODE_P (mode))
12430 mode = nonzero_bits_mode;
12431 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12432 rsp->last_set_sign_bit_copies
12433 = num_sign_bit_copies (value, GET_MODE (reg));
12437 /* Called via note_stores from record_dead_and_set_regs to handle one
12438 SET or CLOBBER in an insn. DATA is the instruction in which the
12439 set is occurring. */
12441 static void
12442 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12444 rtx_insn *record_dead_insn = (rtx_insn *) data;
12446 if (GET_CODE (dest) == SUBREG)
12447 dest = SUBREG_REG (dest);
12449 if (!record_dead_insn)
12451 if (REG_P (dest))
12452 record_value_for_reg (dest, NULL, NULL_RTX);
12453 return;
12456 if (REG_P (dest))
12458 /* If we are setting the whole register, we know its value. Otherwise
12459 show that we don't know the value. We can handle SUBREG in
12460 some cases. */
12461 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12462 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12463 else if (GET_CODE (setter) == SET
12464 && GET_CODE (SET_DEST (setter)) == SUBREG
12465 && SUBREG_REG (SET_DEST (setter)) == dest
12466 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12467 && subreg_lowpart_p (SET_DEST (setter)))
12468 record_value_for_reg (dest, record_dead_insn,
12469 gen_lowpart (GET_MODE (dest),
12470 SET_SRC (setter)));
12471 else
12472 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12474 else if (MEM_P (dest)
12475 /* Ignore pushes, they clobber nothing. */
12476 && ! push_operand (dest, GET_MODE (dest)))
12477 mem_last_set = DF_INSN_LUID (record_dead_insn);
12480 /* Update the records of when each REG was most recently set or killed
12481 for the things done by INSN. This is the last thing done in processing
12482 INSN in the combiner loop.
12484 We update reg_stat[], in particular fields last_set, last_set_value,
12485 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12486 last_death, and also the similar information mem_last_set (which insn
12487 most recently modified memory) and last_call_luid (which insn was the
12488 most recent subroutine call). */
12490 static void
12491 record_dead_and_set_regs (rtx_insn *insn)
12493 rtx link;
12494 unsigned int i;
12496 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12498 if (REG_NOTE_KIND (link) == REG_DEAD
12499 && REG_P (XEXP (link, 0)))
12501 unsigned int regno = REGNO (XEXP (link, 0));
12502 unsigned int endregno = END_REGNO (XEXP (link, 0));
12504 for (i = regno; i < endregno; i++)
12506 reg_stat_type *rsp;
12508 rsp = &reg_stat[i];
12509 rsp->last_death = insn;
12512 else if (REG_NOTE_KIND (link) == REG_INC)
12513 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12516 if (CALL_P (insn))
12518 hard_reg_set_iterator hrsi;
12519 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12521 reg_stat_type *rsp;
12523 rsp = &reg_stat[i];
12524 rsp->last_set_invalid = 1;
12525 rsp->last_set = insn;
12526 rsp->last_set_value = 0;
12527 rsp->last_set_mode = VOIDmode;
12528 rsp->last_set_nonzero_bits = 0;
12529 rsp->last_set_sign_bit_copies = 0;
12530 rsp->last_death = 0;
12531 rsp->truncated_to_mode = VOIDmode;
12534 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12536 /* We can't combine into a call pattern. Remember, though, that
12537 the return value register is set at this LUID. We could
12538 still replace a register with the return value from the
12539 wrong subroutine call! */
12540 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12542 else
12543 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12546 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12547 register present in the SUBREG, so for each such SUBREG go back and
12548 adjust nonzero and sign bit information of the registers that are
12549 known to have some zero/sign bits set.
12551 This is needed because when combine blows the SUBREGs away, the
12552 information on zero/sign bits is lost and further combines can be
12553 missed because of that. */
12555 static void
12556 record_promoted_value (rtx_insn *insn, rtx subreg)
12558 struct insn_link *links;
12559 rtx set;
12560 unsigned int regno = REGNO (SUBREG_REG (subreg));
12561 machine_mode mode = GET_MODE (subreg);
12563 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12564 return;
12566 for (links = LOG_LINKS (insn); links;)
12568 reg_stat_type *rsp;
12570 insn = links->insn;
12571 set = single_set (insn);
12573 if (! set || !REG_P (SET_DEST (set))
12574 || REGNO (SET_DEST (set)) != regno
12575 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12577 links = links->next;
12578 continue;
12581 rsp = &reg_stat[regno];
12582 if (rsp->last_set == insn)
12584 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12585 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12588 if (REG_P (SET_SRC (set)))
12590 regno = REGNO (SET_SRC (set));
12591 links = LOG_LINKS (insn);
12593 else
12594 break;
12598 /* Check if X, a register, is known to contain a value already
12599 truncated to MODE. In this case we can use a subreg to refer to
12600 the truncated value even though in the generic case we would need
12601 an explicit truncation. */
12603 static bool
12604 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12606 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12607 machine_mode truncated = rsp->truncated_to_mode;
12609 if (truncated == 0
12610 || rsp->truncation_label < label_tick_ebb_start)
12611 return false;
12612 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12613 return true;
12614 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12615 return true;
12616 return false;
12619 /* If X is a hard reg or a subreg record the mode that the register is
12620 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12621 to turn a truncate into a subreg using this information. Return true
12622 if traversing X is complete. */
12624 static bool
12625 record_truncated_value (rtx x)
12627 machine_mode truncated_mode;
12628 reg_stat_type *rsp;
12630 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12632 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12633 truncated_mode = GET_MODE (x);
12635 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12636 return true;
12638 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12639 return true;
12641 x = SUBREG_REG (x);
12643 /* ??? For hard-regs we now record everything. We might be able to
12644 optimize this using last_set_mode. */
12645 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12646 truncated_mode = GET_MODE (x);
12647 else
12648 return false;
12650 rsp = &reg_stat[REGNO (x)];
12651 if (rsp->truncated_to_mode == 0
12652 || rsp->truncation_label < label_tick_ebb_start
12653 || (GET_MODE_SIZE (truncated_mode)
12654 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12656 rsp->truncated_to_mode = truncated_mode;
12657 rsp->truncation_label = label_tick;
12660 return true;
12663 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12664 the modes they are used in. This can help truning TRUNCATEs into
12665 SUBREGs. */
12667 static void
12668 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12670 subrtx_var_iterator::array_type array;
12671 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12672 if (record_truncated_value (*iter))
12673 iter.skip_subrtxes ();
12676 /* Scan X for promoted SUBREGs. For each one found,
12677 note what it implies to the registers used in it. */
12679 static void
12680 check_promoted_subreg (rtx_insn *insn, rtx x)
12682 if (GET_CODE (x) == SUBREG
12683 && SUBREG_PROMOTED_VAR_P (x)
12684 && REG_P (SUBREG_REG (x)))
12685 record_promoted_value (insn, x);
12686 else
12688 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12689 int i, j;
12691 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12692 switch (format[i])
12694 case 'e':
12695 check_promoted_subreg (insn, XEXP (x, i));
12696 break;
12697 case 'V':
12698 case 'E':
12699 if (XVEC (x, i) != 0)
12700 for (j = 0; j < XVECLEN (x, i); j++)
12701 check_promoted_subreg (insn, XVECEXP (x, i, j));
12702 break;
12707 /* Verify that all the registers and memory references mentioned in *LOC are
12708 still valid. *LOC was part of a value set in INSN when label_tick was
12709 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12710 the invalid references with (clobber (const_int 0)) and return 1. This
12711 replacement is useful because we often can get useful information about
12712 the form of a value (e.g., if it was produced by a shift that always
12713 produces -1 or 0) even though we don't know exactly what registers it
12714 was produced from. */
12716 static int
12717 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12719 rtx x = *loc;
12720 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12721 int len = GET_RTX_LENGTH (GET_CODE (x));
12722 int i, j;
12724 if (REG_P (x))
12726 unsigned int regno = REGNO (x);
12727 unsigned int endregno = END_REGNO (x);
12728 unsigned int j;
12730 for (j = regno; j < endregno; j++)
12732 reg_stat_type *rsp = &reg_stat[j];
12733 if (rsp->last_set_invalid
12734 /* If this is a pseudo-register that was only set once and not
12735 live at the beginning of the function, it is always valid. */
12736 || (! (regno >= FIRST_PSEUDO_REGISTER
12737 && REG_N_SETS (regno) == 1
12738 && (!REGNO_REG_SET_P
12739 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12740 regno)))
12741 && rsp->last_set_label > tick))
12743 if (replace)
12744 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12745 return replace;
12749 return 1;
12751 /* If this is a memory reference, make sure that there were no stores after
12752 it that might have clobbered the value. We don't have alias info, so we
12753 assume any store invalidates it. Moreover, we only have local UIDs, so
12754 we also assume that there were stores in the intervening basic blocks. */
12755 else if (MEM_P (x) && !MEM_READONLY_P (x)
12756 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12758 if (replace)
12759 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12760 return replace;
12763 for (i = 0; i < len; i++)
12765 if (fmt[i] == 'e')
12767 /* Check for identical subexpressions. If x contains
12768 identical subexpression we only have to traverse one of
12769 them. */
12770 if (i == 1 && ARITHMETIC_P (x))
12772 /* Note that at this point x0 has already been checked
12773 and found valid. */
12774 rtx x0 = XEXP (x, 0);
12775 rtx x1 = XEXP (x, 1);
12777 /* If x0 and x1 are identical then x is also valid. */
12778 if (x0 == x1)
12779 return 1;
12781 /* If x1 is identical to a subexpression of x0 then
12782 while checking x0, x1 has already been checked. Thus
12783 it is valid and so as x. */
12784 if (ARITHMETIC_P (x0)
12785 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12786 return 1;
12788 /* If x0 is identical to a subexpression of x1 then x is
12789 valid iff the rest of x1 is valid. */
12790 if (ARITHMETIC_P (x1)
12791 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12792 return
12793 get_last_value_validate (&XEXP (x1,
12794 x0 == XEXP (x1, 0) ? 1 : 0),
12795 insn, tick, replace);
12798 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12799 replace) == 0)
12800 return 0;
12802 else if (fmt[i] == 'E')
12803 for (j = 0; j < XVECLEN (x, i); j++)
12804 if (get_last_value_validate (&XVECEXP (x, i, j),
12805 insn, tick, replace) == 0)
12806 return 0;
12809 /* If we haven't found a reason for it to be invalid, it is valid. */
12810 return 1;
12813 /* Get the last value assigned to X, if known. Some registers
12814 in the value may be replaced with (clobber (const_int 0)) if their value
12815 is known longer known reliably. */
12817 static rtx
12818 get_last_value (const_rtx x)
12820 unsigned int regno;
12821 rtx value;
12822 reg_stat_type *rsp;
12824 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12825 then convert it to the desired mode. If this is a paradoxical SUBREG,
12826 we cannot predict what values the "extra" bits might have. */
12827 if (GET_CODE (x) == SUBREG
12828 && subreg_lowpart_p (x)
12829 && !paradoxical_subreg_p (x)
12830 && (value = get_last_value (SUBREG_REG (x))) != 0)
12831 return gen_lowpart (GET_MODE (x), value);
12833 if (!REG_P (x))
12834 return 0;
12836 regno = REGNO (x);
12837 rsp = &reg_stat[regno];
12838 value = rsp->last_set_value;
12840 /* If we don't have a value, or if it isn't for this basic block and
12841 it's either a hard register, set more than once, or it's a live
12842 at the beginning of the function, return 0.
12844 Because if it's not live at the beginning of the function then the reg
12845 is always set before being used (is never used without being set).
12846 And, if it's set only once, and it's always set before use, then all
12847 uses must have the same last value, even if it's not from this basic
12848 block. */
12850 if (value == 0
12851 || (rsp->last_set_label < label_tick_ebb_start
12852 && (regno < FIRST_PSEUDO_REGISTER
12853 || REG_N_SETS (regno) != 1
12854 || REGNO_REG_SET_P
12855 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12856 return 0;
12858 /* If the value was set in a later insn than the ones we are processing,
12859 we can't use it even if the register was only set once. */
12860 if (rsp->last_set_label == label_tick
12861 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12862 return 0;
12864 /* If the value has all its registers valid, return it. */
12865 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12866 return value;
12868 /* Otherwise, make a copy and replace any invalid register with
12869 (clobber (const_int 0)). If that fails for some reason, return 0. */
12871 value = copy_rtx (value);
12872 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12873 return value;
12875 return 0;
12878 /* Return nonzero if expression X refers to a REG or to memory
12879 that is set in an instruction more recent than FROM_LUID. */
12881 static int
12882 use_crosses_set_p (const_rtx x, int from_luid)
12884 const char *fmt;
12885 int i;
12886 enum rtx_code code = GET_CODE (x);
12888 if (code == REG)
12890 unsigned int regno = REGNO (x);
12891 unsigned endreg = END_REGNO (x);
12893 #ifdef PUSH_ROUNDING
12894 /* Don't allow uses of the stack pointer to be moved,
12895 because we don't know whether the move crosses a push insn. */
12896 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12897 return 1;
12898 #endif
12899 for (; regno < endreg; regno++)
12901 reg_stat_type *rsp = &reg_stat[regno];
12902 if (rsp->last_set
12903 && rsp->last_set_label == label_tick
12904 && DF_INSN_LUID (rsp->last_set) > from_luid)
12905 return 1;
12907 return 0;
12910 if (code == MEM && mem_last_set > from_luid)
12911 return 1;
12913 fmt = GET_RTX_FORMAT (code);
12915 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12917 if (fmt[i] == 'E')
12919 int j;
12920 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12921 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12922 return 1;
12924 else if (fmt[i] == 'e'
12925 && use_crosses_set_p (XEXP (x, i), from_luid))
12926 return 1;
12928 return 0;
12931 /* Define three variables used for communication between the following
12932 routines. */
12934 static unsigned int reg_dead_regno, reg_dead_endregno;
12935 static int reg_dead_flag;
12937 /* Function called via note_stores from reg_dead_at_p.
12939 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12940 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12942 static void
12943 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12945 unsigned int regno, endregno;
12947 if (!REG_P (dest))
12948 return;
12950 regno = REGNO (dest);
12951 endregno = END_REGNO (dest);
12952 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12953 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12956 /* Return nonzero if REG is known to be dead at INSN.
12958 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12959 referencing REG, it is dead. If we hit a SET referencing REG, it is
12960 live. Otherwise, see if it is live or dead at the start of the basic
12961 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12962 must be assumed to be always live. */
12964 static int
12965 reg_dead_at_p (rtx reg, rtx_insn *insn)
12967 basic_block block;
12968 unsigned int i;
12970 /* Set variables for reg_dead_at_p_1. */
12971 reg_dead_regno = REGNO (reg);
12972 reg_dead_endregno = END_REGNO (reg);
12974 reg_dead_flag = 0;
12976 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12977 we allow the machine description to decide whether use-and-clobber
12978 patterns are OK. */
12979 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12981 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12982 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12983 return 0;
12986 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12987 beginning of basic block. */
12988 block = BLOCK_FOR_INSN (insn);
12989 for (;;)
12991 if (INSN_P (insn))
12993 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
12994 return 1;
12996 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12997 if (reg_dead_flag)
12998 return reg_dead_flag == 1 ? 1 : 0;
13000 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13001 return 1;
13004 if (insn == BB_HEAD (block))
13005 break;
13007 insn = PREV_INSN (insn);
13010 /* Look at live-in sets for the basic block that we were in. */
13011 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13012 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13013 return 0;
13015 return 1;
13018 /* Note hard registers in X that are used. */
13020 static void
13021 mark_used_regs_combine (rtx x)
13023 RTX_CODE code = GET_CODE (x);
13024 unsigned int regno;
13025 int i;
13027 switch (code)
13029 case LABEL_REF:
13030 case SYMBOL_REF:
13031 case CONST:
13032 CASE_CONST_ANY:
13033 case PC:
13034 case ADDR_VEC:
13035 case ADDR_DIFF_VEC:
13036 case ASM_INPUT:
13037 #ifdef HAVE_cc0
13038 /* CC0 must die in the insn after it is set, so we don't need to take
13039 special note of it here. */
13040 case CC0:
13041 #endif
13042 return;
13044 case CLOBBER:
13045 /* If we are clobbering a MEM, mark any hard registers inside the
13046 address as used. */
13047 if (MEM_P (XEXP (x, 0)))
13048 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13049 return;
13051 case REG:
13052 regno = REGNO (x);
13053 /* A hard reg in a wide mode may really be multiple registers.
13054 If so, mark all of them just like the first. */
13055 if (regno < FIRST_PSEUDO_REGISTER)
13057 /* None of this applies to the stack, frame or arg pointers. */
13058 if (regno == STACK_POINTER_REGNUM
13059 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13060 || regno == HARD_FRAME_POINTER_REGNUM
13061 #endif
13062 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13063 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13064 #endif
13065 || regno == FRAME_POINTER_REGNUM)
13066 return;
13068 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13070 return;
13072 case SET:
13074 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13075 the address. */
13076 rtx testreg = SET_DEST (x);
13078 while (GET_CODE (testreg) == SUBREG
13079 || GET_CODE (testreg) == ZERO_EXTRACT
13080 || GET_CODE (testreg) == STRICT_LOW_PART)
13081 testreg = XEXP (testreg, 0);
13083 if (MEM_P (testreg))
13084 mark_used_regs_combine (XEXP (testreg, 0));
13086 mark_used_regs_combine (SET_SRC (x));
13088 return;
13090 default:
13091 break;
13094 /* Recursively scan the operands of this expression. */
13097 const char *fmt = GET_RTX_FORMAT (code);
13099 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13101 if (fmt[i] == 'e')
13102 mark_used_regs_combine (XEXP (x, i));
13103 else if (fmt[i] == 'E')
13105 int j;
13107 for (j = 0; j < XVECLEN (x, i); j++)
13108 mark_used_regs_combine (XVECEXP (x, i, j));
13114 /* Remove register number REGNO from the dead registers list of INSN.
13116 Return the note used to record the death, if there was one. */
13119 remove_death (unsigned int regno, rtx_insn *insn)
13121 rtx note = find_regno_note (insn, REG_DEAD, regno);
13123 if (note)
13124 remove_note (insn, note);
13126 return note;
13129 /* For each register (hardware or pseudo) used within expression X, if its
13130 death is in an instruction with luid between FROM_LUID (inclusive) and
13131 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13132 list headed by PNOTES.
13134 That said, don't move registers killed by maybe_kill_insn.
13136 This is done when X is being merged by combination into TO_INSN. These
13137 notes will then be distributed as needed. */
13139 static void
13140 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13141 rtx *pnotes)
13143 const char *fmt;
13144 int len, i;
13145 enum rtx_code code = GET_CODE (x);
13147 if (code == REG)
13149 unsigned int regno = REGNO (x);
13150 rtx_insn *where_dead = reg_stat[regno].last_death;
13152 /* Don't move the register if it gets killed in between from and to. */
13153 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13154 && ! reg_referenced_p (x, maybe_kill_insn))
13155 return;
13157 if (where_dead
13158 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13159 && DF_INSN_LUID (where_dead) >= from_luid
13160 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13162 rtx note = remove_death (regno, where_dead);
13164 /* It is possible for the call above to return 0. This can occur
13165 when last_death points to I2 or I1 that we combined with.
13166 In that case make a new note.
13168 We must also check for the case where X is a hard register
13169 and NOTE is a death note for a range of hard registers
13170 including X. In that case, we must put REG_DEAD notes for
13171 the remaining registers in place of NOTE. */
13173 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13174 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13175 > GET_MODE_SIZE (GET_MODE (x))))
13177 unsigned int deadregno = REGNO (XEXP (note, 0));
13178 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13179 unsigned int ourend = END_HARD_REGNO (x);
13180 unsigned int i;
13182 for (i = deadregno; i < deadend; i++)
13183 if (i < regno || i >= ourend)
13184 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13187 /* If we didn't find any note, or if we found a REG_DEAD note that
13188 covers only part of the given reg, and we have a multi-reg hard
13189 register, then to be safe we must check for REG_DEAD notes
13190 for each register other than the first. They could have
13191 their own REG_DEAD notes lying around. */
13192 else if ((note == 0
13193 || (note != 0
13194 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13195 < GET_MODE_SIZE (GET_MODE (x)))))
13196 && regno < FIRST_PSEUDO_REGISTER
13197 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13199 unsigned int ourend = END_HARD_REGNO (x);
13200 unsigned int i, offset;
13201 rtx oldnotes = 0;
13203 if (note)
13204 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13205 else
13206 offset = 1;
13208 for (i = regno + offset; i < ourend; i++)
13209 move_deaths (regno_reg_rtx[i],
13210 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13213 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13215 XEXP (note, 1) = *pnotes;
13216 *pnotes = note;
13218 else
13219 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13222 return;
13225 else if (GET_CODE (x) == SET)
13227 rtx dest = SET_DEST (x);
13229 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13231 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13232 that accesses one word of a multi-word item, some
13233 piece of everything register in the expression is used by
13234 this insn, so remove any old death. */
13235 /* ??? So why do we test for equality of the sizes? */
13237 if (GET_CODE (dest) == ZERO_EXTRACT
13238 || GET_CODE (dest) == STRICT_LOW_PART
13239 || (GET_CODE (dest) == SUBREG
13240 && (((GET_MODE_SIZE (GET_MODE (dest))
13241 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13242 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13243 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13245 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13246 return;
13249 /* If this is some other SUBREG, we know it replaces the entire
13250 value, so use that as the destination. */
13251 if (GET_CODE (dest) == SUBREG)
13252 dest = SUBREG_REG (dest);
13254 /* If this is a MEM, adjust deaths of anything used in the address.
13255 For a REG (the only other possibility), the entire value is
13256 being replaced so the old value is not used in this insn. */
13258 if (MEM_P (dest))
13259 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13260 to_insn, pnotes);
13261 return;
13264 else if (GET_CODE (x) == CLOBBER)
13265 return;
13267 len = GET_RTX_LENGTH (code);
13268 fmt = GET_RTX_FORMAT (code);
13270 for (i = 0; i < len; i++)
13272 if (fmt[i] == 'E')
13274 int j;
13275 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13276 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13277 to_insn, pnotes);
13279 else if (fmt[i] == 'e')
13280 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13284 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13285 pattern of an insn. X must be a REG. */
13287 static int
13288 reg_bitfield_target_p (rtx x, rtx body)
13290 int i;
13292 if (GET_CODE (body) == SET)
13294 rtx dest = SET_DEST (body);
13295 rtx target;
13296 unsigned int regno, tregno, endregno, endtregno;
13298 if (GET_CODE (dest) == ZERO_EXTRACT)
13299 target = XEXP (dest, 0);
13300 else if (GET_CODE (dest) == STRICT_LOW_PART)
13301 target = SUBREG_REG (XEXP (dest, 0));
13302 else
13303 return 0;
13305 if (GET_CODE (target) == SUBREG)
13306 target = SUBREG_REG (target);
13308 if (!REG_P (target))
13309 return 0;
13311 tregno = REGNO (target), regno = REGNO (x);
13312 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13313 return target == x;
13315 endtregno = end_hard_regno (GET_MODE (target), tregno);
13316 endregno = end_hard_regno (GET_MODE (x), regno);
13318 return endregno > tregno && regno < endtregno;
13321 else if (GET_CODE (body) == PARALLEL)
13322 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13323 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13324 return 1;
13326 return 0;
13329 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13330 as appropriate. I3 and I2 are the insns resulting from the combination
13331 insns including FROM (I2 may be zero).
13333 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13334 not need REG_DEAD notes because they are being substituted for. This
13335 saves searching in the most common cases.
13337 Each note in the list is either ignored or placed on some insns, depending
13338 on the type of note. */
13340 static void
13341 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13342 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13344 rtx note, next_note;
13345 rtx tem_note;
13346 rtx_insn *tem_insn;
13348 for (note = notes; note; note = next_note)
13350 rtx_insn *place = 0, *place2 = 0;
13352 next_note = XEXP (note, 1);
13353 switch (REG_NOTE_KIND (note))
13355 case REG_BR_PROB:
13356 case REG_BR_PRED:
13357 /* Doesn't matter much where we put this, as long as it's somewhere.
13358 It is preferable to keep these notes on branches, which is most
13359 likely to be i3. */
13360 place = i3;
13361 break;
13363 case REG_NON_LOCAL_GOTO:
13364 if (JUMP_P (i3))
13365 place = i3;
13366 else
13368 gcc_assert (i2 && JUMP_P (i2));
13369 place = i2;
13371 break;
13373 case REG_EH_REGION:
13374 /* These notes must remain with the call or trapping instruction. */
13375 if (CALL_P (i3))
13376 place = i3;
13377 else if (i2 && CALL_P (i2))
13378 place = i2;
13379 else
13381 gcc_assert (cfun->can_throw_non_call_exceptions);
13382 if (may_trap_p (i3))
13383 place = i3;
13384 else if (i2 && may_trap_p (i2))
13385 place = i2;
13386 /* ??? Otherwise assume we've combined things such that we
13387 can now prove that the instructions can't trap. Drop the
13388 note in this case. */
13390 break;
13392 case REG_ARGS_SIZE:
13393 /* ??? How to distribute between i3-i1. Assume i3 contains the
13394 entire adjustment. Assert i3 contains at least some adjust. */
13395 if (!noop_move_p (i3))
13397 int old_size, args_size = INTVAL (XEXP (note, 0));
13398 /* fixup_args_size_notes looks at REG_NORETURN note,
13399 so ensure the note is placed there first. */
13400 if (CALL_P (i3))
13402 rtx *np;
13403 for (np = &next_note; *np; np = &XEXP (*np, 1))
13404 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13406 rtx n = *np;
13407 *np = XEXP (n, 1);
13408 XEXP (n, 1) = REG_NOTES (i3);
13409 REG_NOTES (i3) = n;
13410 break;
13413 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13414 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13415 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13416 gcc_assert (old_size != args_size
13417 || (CALL_P (i3)
13418 && !ACCUMULATE_OUTGOING_ARGS
13419 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13421 break;
13423 case REG_NORETURN:
13424 case REG_SETJMP:
13425 case REG_TM:
13426 case REG_CALL_DECL:
13427 /* These notes must remain with the call. It should not be
13428 possible for both I2 and I3 to be a call. */
13429 if (CALL_P (i3))
13430 place = i3;
13431 else
13433 gcc_assert (i2 && CALL_P (i2));
13434 place = i2;
13436 break;
13438 case REG_UNUSED:
13439 /* Any clobbers for i3 may still exist, and so we must process
13440 REG_UNUSED notes from that insn.
13442 Any clobbers from i2 or i1 can only exist if they were added by
13443 recog_for_combine. In that case, recog_for_combine created the
13444 necessary REG_UNUSED notes. Trying to keep any original
13445 REG_UNUSED notes from these insns can cause incorrect output
13446 if it is for the same register as the original i3 dest.
13447 In that case, we will notice that the register is set in i3,
13448 and then add a REG_UNUSED note for the destination of i3, which
13449 is wrong. However, it is possible to have REG_UNUSED notes from
13450 i2 or i1 for register which were both used and clobbered, so
13451 we keep notes from i2 or i1 if they will turn into REG_DEAD
13452 notes. */
13454 /* If this register is set or clobbered in I3, put the note there
13455 unless there is one already. */
13456 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13458 if (from_insn != i3)
13459 break;
13461 if (! (REG_P (XEXP (note, 0))
13462 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13463 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13464 place = i3;
13466 /* Otherwise, if this register is used by I3, then this register
13467 now dies here, so we must put a REG_DEAD note here unless there
13468 is one already. */
13469 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13470 && ! (REG_P (XEXP (note, 0))
13471 ? find_regno_note (i3, REG_DEAD,
13472 REGNO (XEXP (note, 0)))
13473 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13475 PUT_REG_NOTE_KIND (note, REG_DEAD);
13476 place = i3;
13478 break;
13480 case REG_EQUAL:
13481 case REG_EQUIV:
13482 case REG_NOALIAS:
13483 /* These notes say something about results of an insn. We can
13484 only support them if they used to be on I3 in which case they
13485 remain on I3. Otherwise they are ignored.
13487 If the note refers to an expression that is not a constant, we
13488 must also ignore the note since we cannot tell whether the
13489 equivalence is still true. It might be possible to do
13490 slightly better than this (we only have a problem if I2DEST
13491 or I1DEST is present in the expression), but it doesn't
13492 seem worth the trouble. */
13494 if (from_insn == i3
13495 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13496 place = i3;
13497 break;
13499 case REG_INC:
13500 /* These notes say something about how a register is used. They must
13501 be present on any use of the register in I2 or I3. */
13502 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13503 place = i3;
13505 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13507 if (place)
13508 place2 = i2;
13509 else
13510 place = i2;
13512 break;
13514 case REG_LABEL_TARGET:
13515 case REG_LABEL_OPERAND:
13516 /* This can show up in several ways -- either directly in the
13517 pattern, or hidden off in the constant pool with (or without?)
13518 a REG_EQUAL note. */
13519 /* ??? Ignore the without-reg_equal-note problem for now. */
13520 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13521 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13522 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13523 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13524 place = i3;
13526 if (i2
13527 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13528 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13529 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13530 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13532 if (place)
13533 place2 = i2;
13534 else
13535 place = i2;
13538 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13539 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13540 there. */
13541 if (place && JUMP_P (place)
13542 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13543 && (JUMP_LABEL (place) == NULL
13544 || JUMP_LABEL (place) == XEXP (note, 0)))
13546 rtx label = JUMP_LABEL (place);
13548 if (!label)
13549 JUMP_LABEL (place) = XEXP (note, 0);
13550 else if (LABEL_P (label))
13551 LABEL_NUSES (label)--;
13554 if (place2 && JUMP_P (place2)
13555 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13556 && (JUMP_LABEL (place2) == NULL
13557 || JUMP_LABEL (place2) == XEXP (note, 0)))
13559 rtx label = JUMP_LABEL (place2);
13561 if (!label)
13562 JUMP_LABEL (place2) = XEXP (note, 0);
13563 else if (LABEL_P (label))
13564 LABEL_NUSES (label)--;
13565 place2 = 0;
13567 break;
13569 case REG_NONNEG:
13570 /* This note says something about the value of a register prior
13571 to the execution of an insn. It is too much trouble to see
13572 if the note is still correct in all situations. It is better
13573 to simply delete it. */
13574 break;
13576 case REG_DEAD:
13577 /* If we replaced the right hand side of FROM_INSN with a
13578 REG_EQUAL note, the original use of the dying register
13579 will not have been combined into I3 and I2. In such cases,
13580 FROM_INSN is guaranteed to be the first of the combined
13581 instructions, so we simply need to search back before
13582 FROM_INSN for the previous use or set of this register,
13583 then alter the notes there appropriately.
13585 If the register is used as an input in I3, it dies there.
13586 Similarly for I2, if it is nonzero and adjacent to I3.
13588 If the register is not used as an input in either I3 or I2
13589 and it is not one of the registers we were supposed to eliminate,
13590 there are two possibilities. We might have a non-adjacent I2
13591 or we might have somehow eliminated an additional register
13592 from a computation. For example, we might have had A & B where
13593 we discover that B will always be zero. In this case we will
13594 eliminate the reference to A.
13596 In both cases, we must search to see if we can find a previous
13597 use of A and put the death note there. */
13599 if (from_insn
13600 && from_insn == i2mod
13601 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13602 tem_insn = from_insn;
13603 else
13605 if (from_insn
13606 && CALL_P (from_insn)
13607 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13608 place = from_insn;
13609 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13610 place = i3;
13611 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13612 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13613 place = i2;
13614 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13615 && !(i2mod
13616 && reg_overlap_mentioned_p (XEXP (note, 0),
13617 i2mod_old_rhs)))
13618 || rtx_equal_p (XEXP (note, 0), elim_i1)
13619 || rtx_equal_p (XEXP (note, 0), elim_i0))
13620 break;
13621 tem_insn = i3;
13624 if (place == 0)
13626 basic_block bb = this_basic_block;
13628 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13630 if (!NONDEBUG_INSN_P (tem_insn))
13632 if (tem_insn == BB_HEAD (bb))
13633 break;
13634 continue;
13637 /* If the register is being set at TEM_INSN, see if that is all
13638 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13639 into a REG_UNUSED note instead. Don't delete sets to
13640 global register vars. */
13641 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13642 || !global_regs[REGNO (XEXP (note, 0))])
13643 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13645 rtx set = single_set (tem_insn);
13646 rtx inner_dest = 0;
13647 #ifdef HAVE_cc0
13648 rtx_insn *cc0_setter = NULL;
13649 #endif
13651 if (set != 0)
13652 for (inner_dest = SET_DEST (set);
13653 (GET_CODE (inner_dest) == STRICT_LOW_PART
13654 || GET_CODE (inner_dest) == SUBREG
13655 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13656 inner_dest = XEXP (inner_dest, 0))
13659 /* Verify that it was the set, and not a clobber that
13660 modified the register.
13662 CC0 targets must be careful to maintain setter/user
13663 pairs. If we cannot delete the setter due to side
13664 effects, mark the user with an UNUSED note instead
13665 of deleting it. */
13667 if (set != 0 && ! side_effects_p (SET_SRC (set))
13668 && rtx_equal_p (XEXP (note, 0), inner_dest)
13669 #ifdef HAVE_cc0
13670 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13671 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13672 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13673 #endif
13676 /* Move the notes and links of TEM_INSN elsewhere.
13677 This might delete other dead insns recursively.
13678 First set the pattern to something that won't use
13679 any register. */
13680 rtx old_notes = REG_NOTES (tem_insn);
13682 PATTERN (tem_insn) = pc_rtx;
13683 REG_NOTES (tem_insn) = NULL;
13685 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13686 NULL_RTX, NULL_RTX, NULL_RTX);
13687 distribute_links (LOG_LINKS (tem_insn));
13689 SET_INSN_DELETED (tem_insn);
13690 if (tem_insn == i2)
13691 i2 = NULL;
13693 #ifdef HAVE_cc0
13694 /* Delete the setter too. */
13695 if (cc0_setter)
13697 PATTERN (cc0_setter) = pc_rtx;
13698 old_notes = REG_NOTES (cc0_setter);
13699 REG_NOTES (cc0_setter) = NULL;
13701 distribute_notes (old_notes, cc0_setter,
13702 cc0_setter, NULL,
13703 NULL_RTX, NULL_RTX, NULL_RTX);
13704 distribute_links (LOG_LINKS (cc0_setter));
13706 SET_INSN_DELETED (cc0_setter);
13707 if (cc0_setter == i2)
13708 i2 = NULL;
13710 #endif
13712 else
13714 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13716 /* If there isn't already a REG_UNUSED note, put one
13717 here. Do not place a REG_DEAD note, even if
13718 the register is also used here; that would not
13719 match the algorithm used in lifetime analysis
13720 and can cause the consistency check in the
13721 scheduler to fail. */
13722 if (! find_regno_note (tem_insn, REG_UNUSED,
13723 REGNO (XEXP (note, 0))))
13724 place = tem_insn;
13725 break;
13728 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13729 || (CALL_P (tem_insn)
13730 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13732 place = tem_insn;
13734 /* If we are doing a 3->2 combination, and we have a
13735 register which formerly died in i3 and was not used
13736 by i2, which now no longer dies in i3 and is used in
13737 i2 but does not die in i2, and place is between i2
13738 and i3, then we may need to move a link from place to
13739 i2. */
13740 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13741 && from_insn
13742 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13743 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13745 struct insn_link *links = LOG_LINKS (place);
13746 LOG_LINKS (place) = NULL;
13747 distribute_links (links);
13749 break;
13752 if (tem_insn == BB_HEAD (bb))
13753 break;
13758 /* If the register is set or already dead at PLACE, we needn't do
13759 anything with this note if it is still a REG_DEAD note.
13760 We check here if it is set at all, not if is it totally replaced,
13761 which is what `dead_or_set_p' checks, so also check for it being
13762 set partially. */
13764 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13766 unsigned int regno = REGNO (XEXP (note, 0));
13767 reg_stat_type *rsp = &reg_stat[regno];
13769 if (dead_or_set_p (place, XEXP (note, 0))
13770 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13772 /* Unless the register previously died in PLACE, clear
13773 last_death. [I no longer understand why this is
13774 being done.] */
13775 if (rsp->last_death != place)
13776 rsp->last_death = 0;
13777 place = 0;
13779 else
13780 rsp->last_death = place;
13782 /* If this is a death note for a hard reg that is occupying
13783 multiple registers, ensure that we are still using all
13784 parts of the object. If we find a piece of the object
13785 that is unused, we must arrange for an appropriate REG_DEAD
13786 note to be added for it. However, we can't just emit a USE
13787 and tag the note to it, since the register might actually
13788 be dead; so we recourse, and the recursive call then finds
13789 the previous insn that used this register. */
13791 if (place && regno < FIRST_PSEUDO_REGISTER
13792 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13794 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13795 bool all_used = true;
13796 unsigned int i;
13798 for (i = regno; i < endregno; i++)
13799 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13800 && ! find_regno_fusage (place, USE, i))
13801 || dead_or_set_regno_p (place, i))
13803 all_used = false;
13804 break;
13807 if (! all_used)
13809 /* Put only REG_DEAD notes for pieces that are
13810 not already dead or set. */
13812 for (i = regno; i < endregno;
13813 i += hard_regno_nregs[i][reg_raw_mode[i]])
13815 rtx piece = regno_reg_rtx[i];
13816 basic_block bb = this_basic_block;
13818 if (! dead_or_set_p (place, piece)
13819 && ! reg_bitfield_target_p (piece,
13820 PATTERN (place)))
13822 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13823 NULL_RTX);
13825 distribute_notes (new_note, place, place,
13826 NULL, NULL_RTX, NULL_RTX,
13827 NULL_RTX);
13829 else if (! refers_to_regno_p (i, i + 1,
13830 PATTERN (place), 0)
13831 && ! find_regno_fusage (place, USE, i))
13832 for (tem_insn = PREV_INSN (place); ;
13833 tem_insn = PREV_INSN (tem_insn))
13835 if (!NONDEBUG_INSN_P (tem_insn))
13837 if (tem_insn == BB_HEAD (bb))
13838 break;
13839 continue;
13841 if (dead_or_set_p (tem_insn, piece)
13842 || reg_bitfield_target_p (piece,
13843 PATTERN (tem_insn)))
13845 add_reg_note (tem_insn, REG_UNUSED, piece);
13846 break;
13851 place = 0;
13855 break;
13857 default:
13858 /* Any other notes should not be present at this point in the
13859 compilation. */
13860 gcc_unreachable ();
13863 if (place)
13865 XEXP (note, 1) = REG_NOTES (place);
13866 REG_NOTES (place) = note;
13869 if (place2)
13870 add_shallow_copy_of_reg_note (place2, note);
13874 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13875 I3, I2, and I1 to new locations. This is also called to add a link
13876 pointing at I3 when I3's destination is changed. */
13878 static void
13879 distribute_links (struct insn_link *links)
13881 struct insn_link *link, *next_link;
13883 for (link = links; link; link = next_link)
13885 rtx_insn *place = 0;
13886 rtx_insn *insn;
13887 rtx set, reg;
13889 next_link = link->next;
13891 /* If the insn that this link points to is a NOTE, ignore it. */
13892 if (NOTE_P (link->insn))
13893 continue;
13895 set = 0;
13896 rtx pat = PATTERN (link->insn);
13897 if (GET_CODE (pat) == SET)
13898 set = pat;
13899 else if (GET_CODE (pat) == PARALLEL)
13901 int i;
13902 for (i = 0; i < XVECLEN (pat, 0); i++)
13904 set = XVECEXP (pat, 0, i);
13905 if (GET_CODE (set) != SET)
13906 continue;
13908 reg = SET_DEST (set);
13909 while (GET_CODE (reg) == ZERO_EXTRACT
13910 || GET_CODE (reg) == STRICT_LOW_PART
13911 || GET_CODE (reg) == SUBREG)
13912 reg = XEXP (reg, 0);
13914 if (!REG_P (reg))
13915 continue;
13917 if (REGNO (reg) == link->regno)
13918 break;
13920 if (i == XVECLEN (pat, 0))
13921 continue;
13923 else
13924 continue;
13926 reg = SET_DEST (set);
13928 while (GET_CODE (reg) == ZERO_EXTRACT
13929 || GET_CODE (reg) == STRICT_LOW_PART
13930 || GET_CODE (reg) == SUBREG)
13931 reg = XEXP (reg, 0);
13933 /* A LOG_LINK is defined as being placed on the first insn that uses
13934 a register and points to the insn that sets the register. Start
13935 searching at the next insn after the target of the link and stop
13936 when we reach a set of the register or the end of the basic block.
13938 Note that this correctly handles the link that used to point from
13939 I3 to I2. Also note that not much searching is typically done here
13940 since most links don't point very far away. */
13942 for (insn = NEXT_INSN (link->insn);
13943 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13944 || BB_HEAD (this_basic_block->next_bb) != insn));
13945 insn = NEXT_INSN (insn))
13946 if (DEBUG_INSN_P (insn))
13947 continue;
13948 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13950 if (reg_referenced_p (reg, PATTERN (insn)))
13951 place = insn;
13952 break;
13954 else if (CALL_P (insn)
13955 && find_reg_fusage (insn, USE, reg))
13957 place = insn;
13958 break;
13960 else if (INSN_P (insn) && reg_set_p (reg, insn))
13961 break;
13963 /* If we found a place to put the link, place it there unless there
13964 is already a link to the same insn as LINK at that point. */
13966 if (place)
13968 struct insn_link *link2;
13970 FOR_EACH_LOG_LINK (link2, place)
13971 if (link2->insn == link->insn && link2->regno == link->regno)
13972 break;
13974 if (link2 == NULL)
13976 link->next = LOG_LINKS (place);
13977 LOG_LINKS (place) = link;
13979 /* Set added_links_insn to the earliest insn we added a
13980 link to. */
13981 if (added_links_insn == 0
13982 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13983 added_links_insn = place;
13989 /* Check for any register or memory mentioned in EQUIV that is not
13990 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13991 of EXPR where some registers may have been replaced by constants. */
13993 static bool
13994 unmentioned_reg_p (rtx equiv, rtx expr)
13996 subrtx_iterator::array_type array;
13997 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
13999 const_rtx x = *iter;
14000 if ((REG_P (x) || MEM_P (x))
14001 && !reg_mentioned_p (x, expr))
14002 return true;
14004 return false;
14007 DEBUG_FUNCTION void
14008 dump_combine_stats (FILE *file)
14010 fprintf
14011 (file,
14012 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14013 combine_attempts, combine_merges, combine_extras, combine_successes);
14016 void
14017 dump_combine_total_stats (FILE *file)
14019 fprintf
14020 (file,
14021 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14022 total_attempts, total_merges, total_extras, total_successes);
14025 /* Try combining insns through substitution. */
14026 static unsigned int
14027 rest_of_handle_combine (void)
14029 int rebuild_jump_labels_after_combine;
14031 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14032 df_note_add_problem ();
14033 df_analyze ();
14035 regstat_init_n_sets_and_refs ();
14037 rebuild_jump_labels_after_combine
14038 = combine_instructions (get_insns (), max_reg_num ());
14040 /* Combining insns may have turned an indirect jump into a
14041 direct jump. Rebuild the JUMP_LABEL fields of jumping
14042 instructions. */
14043 if (rebuild_jump_labels_after_combine)
14045 timevar_push (TV_JUMP);
14046 rebuild_jump_labels (get_insns ());
14047 cleanup_cfg (0);
14048 timevar_pop (TV_JUMP);
14051 regstat_free_n_sets_and_refs ();
14052 return 0;
14055 namespace {
14057 const pass_data pass_data_combine =
14059 RTL_PASS, /* type */
14060 "combine", /* name */
14061 OPTGROUP_NONE, /* optinfo_flags */
14062 TV_COMBINE, /* tv_id */
14063 PROP_cfglayout, /* properties_required */
14064 0, /* properties_provided */
14065 0, /* properties_destroyed */
14066 0, /* todo_flags_start */
14067 TODO_df_finish, /* todo_flags_finish */
14070 class pass_combine : public rtl_opt_pass
14072 public:
14073 pass_combine (gcc::context *ctxt)
14074 : rtl_opt_pass (pass_data_combine, ctxt)
14077 /* opt_pass methods: */
14078 virtual bool gate (function *) { return (optimize > 0); }
14079 virtual unsigned int execute (function *)
14081 return rest_of_handle_combine ();
14084 }; // class pass_combine
14086 } // anon namespace
14088 rtl_opt_pass *
14089 make_pass_combine (gcc::context *ctxt)
14091 return new pass_combine (ctxt);