1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* This module is essentially the "combiner" phase of the U. of Arizona
24 Portable Optimizer, but redone to work on our list-structured
25 representation for RTL instead of their string representation.
27 The LOG_LINKS of each insn identify the most recent assignment
28 to each REG used in the insn. It is a list of previous insns,
29 each of which contains a SET for a REG that is used in this insn
30 and not used or set in between. LOG_LINKs never cross basic blocks.
31 They were set up by the preceding pass (lifetime analysis).
33 We try to combine each pair of insns joined by a logical link.
34 We also try to combine triples of insns A, B and C when
35 C has a link back to B and B has a link back to A.
37 LOG_LINKS does not have links for use of the CC0. They don't
38 need to, because the insn that sets the CC0 is always immediately
39 before the insn that tests it. So we always regard a branch
40 insn as having a logical link to the preceding insn. The same is true
41 for an insn explicitly using CC0.
43 We check (with use_crosses_set_p) to avoid combining in such a way
44 as to move a computation to a place where its value would be different.
46 Combination is done by mathematically substituting the previous
47 insn(s) values for the regs they set into the expressions in
48 the later insns that refer to these regs. If the result is a valid insn
49 for our target machine, according to the machine description,
50 we install it, delete the earlier insns, and update the data flow
51 information (LOG_LINKS and REG_NOTES) for what we did.
53 There are a few exceptions where the dataflow information isn't
54 completely updated (however this is only a local issue since it is
55 regenerated before the next pass that uses it):
57 - reg_live_length is not updated
58 - reg_n_refs is not adjusted in the rare case when a register is
59 no longer required in a computation
60 - there are extremely rare cases (see distribute_notes) when a
62 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
63 removed because there is no way to know which register it was
66 To simplify substitution, we combine only when the earlier insn(s)
67 consist of only a single assignment. To simplify updating afterward,
68 we never combine when a subroutine call appears in the middle.
70 Since we do not represent assignments to CC0 explicitly except when that
71 is all an insn does, there is no LOG_LINKS entry in an insn that uses
72 the condition code for the insn that set the condition code.
73 Fortunately, these two insns must be consecutive.
74 Therefore, every JUMP_INSN is taken to have an implicit logical link
75 to the preceding insn. This is not quite right, since non-jumps can
76 also use the condition code; but in practice such insns would not
81 #include "coretypes.h"
88 #include "hard-reg-set.h"
89 #include "basic-block.h"
90 #include "insn-config.h"
92 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
94 #include "insn-attr.h"
100 #include "insn-codes.h"
101 #include "rtlhooks-def.h"
102 /* Include output.h for dump_file. */
106 #include "tree-pass.h"
109 /* Number of attempts to combine instructions in this function. */
111 static int combine_attempts
;
113 /* Number of attempts that got as far as substitution in this function. */
115 static int combine_merges
;
117 /* Number of instructions combined with added SETs in this function. */
119 static int combine_extras
;
121 /* Number of instructions combined in this function. */
123 static int combine_successes
;
125 /* Totals over entire compilation. */
127 static int total_attempts
, total_merges
, total_extras
, total_successes
;
129 /* combine_instructions may try to replace the right hand side of the
130 second instruction with the value of an associated REG_EQUAL note
131 before throwing it at try_combine. That is problematic when there
132 is a REG_DEAD note for a register used in the old right hand side
133 and can cause distribute_notes to do wrong things. This is the
134 second instruction if it has been so modified, null otherwise. */
138 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
140 static rtx i2mod_old_rhs
;
142 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
144 static rtx i2mod_new_rhs
;
146 /* Maximum register number, which is the size of the tables below. */
148 static unsigned int combine_max_regno
;
151 /* Record last point of death of (hard or pseudo) register n. */
154 /* Record last point of modification of (hard or pseudo) register n. */
157 /* The next group of fields allows the recording of the last value assigned
158 to (hard or pseudo) register n. We use this information to see if an
159 operation being processed is redundant given a prior operation performed
160 on the register. For example, an `and' with a constant is redundant if
161 all the zero bits are already known to be turned off.
163 We use an approach similar to that used by cse, but change it in the
166 (1) We do not want to reinitialize at each label.
167 (2) It is useful, but not critical, to know the actual value assigned
168 to a register. Often just its form is helpful.
170 Therefore, we maintain the following fields:
172 last_set_value the last value assigned
173 last_set_label records the value of label_tick when the
174 register was assigned
175 last_set_table_tick records the value of label_tick when a
176 value using the register is assigned
177 last_set_invalid set to nonzero when it is not valid
178 to use the value of this register in some
181 To understand the usage of these tables, it is important to understand
182 the distinction between the value in last_set_value being valid and
183 the register being validly contained in some other expression in the
186 (The next two parameters are out of date).
188 reg_stat[i].last_set_value is valid if it is nonzero, and either
189 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
191 Register I may validly appear in any expression returned for the value
192 of another register if reg_n_sets[i] is 1. It may also appear in the
193 value for register J if reg_stat[j].last_set_invalid is zero, or
194 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
196 If an expression is found in the table containing a register which may
197 not validly appear in an expression, the register is replaced by
198 something that won't match, (clobber (const_int 0)). */
200 /* Record last value assigned to (hard or pseudo) register n. */
204 /* Record the value of label_tick when an expression involving register n
205 is placed in last_set_value. */
207 int last_set_table_tick
;
209 /* Record the value of label_tick when the value for register n is placed in
214 /* These fields are maintained in parallel with last_set_value and are
215 used to store the mode in which the register was last set, the bits
216 that were known to be zero when it was last set, and the number of
217 sign bits copies it was known to have when it was last set. */
219 unsigned HOST_WIDE_INT last_set_nonzero_bits
;
220 char last_set_sign_bit_copies
;
221 ENUM_BITFIELD(machine_mode
) last_set_mode
: 8;
223 /* Set nonzero if references to register n in expressions should not be
224 used. last_set_invalid is set nonzero when this register is being
225 assigned to and last_set_table_tick == label_tick. */
227 char last_set_invalid
;
229 /* Some registers that are set more than once and used in more than one
230 basic block are nevertheless always set in similar ways. For example,
231 a QImode register may be loaded from memory in two places on a machine
232 where byte loads zero extend.
234 We record in the following fields if a register has some leading bits
235 that are always equal to the sign bit, and what we know about the
236 nonzero bits of a register, specifically which bits are known to be
239 If an entry is zero, it means that we don't know anything special. */
241 unsigned char sign_bit_copies
;
243 unsigned HOST_WIDE_INT nonzero_bits
;
245 /* Record the value of the label_tick when the last truncation
246 happened. The field truncated_to_mode is only valid if
247 truncation_label == label_tick. */
249 int truncation_label
;
251 /* Record the last truncation seen for this register. If truncation
252 is not a nop to this mode we might be able to save an explicit
253 truncation if we know that value already contains a truncated
256 ENUM_BITFIELD(machine_mode
) truncated_to_mode
: 8;
259 static struct reg_stat
*reg_stat
;
261 /* Record the luid of the last insn that invalidated memory
262 (anything that writes memory, and subroutine calls, but not pushes). */
264 static int mem_last_set
;
266 /* Record the luid of the last CALL_INSN
267 so we can tell whether a potential combination crosses any calls. */
269 static int last_call_luid
;
271 /* When `subst' is called, this is the insn that is being modified
272 (by combining in a previous insn). The PATTERN of this insn
273 is still the old pattern partially modified and it should not be
274 looked at, but this may be used to examine the successors of the insn
275 to judge whether a simplification is valid. */
277 static rtx subst_insn
;
279 /* This is the lowest LUID that `subst' is currently dealing with.
280 get_last_value will not return a value if the register was set at or
281 after this LUID. If not for this mechanism, we could get confused if
282 I2 or I1 in try_combine were an insn that used the old value of a register
283 to obtain a new value. In that case, we might erroneously get the
284 new value of the register when we wanted the old one. */
286 static int subst_low_luid
;
288 /* This contains any hard registers that are used in newpat; reg_dead_at_p
289 must consider all these registers to be always live. */
291 static HARD_REG_SET newpat_used_regs
;
293 /* This is an insn to which a LOG_LINKS entry has been added. If this
294 insn is the earlier than I2 or I3, combine should rescan starting at
297 static rtx added_links_insn
;
299 /* Basic block in which we are performing combines. */
300 static basic_block this_basic_block
;
303 /* Length of the currently allocated uid_insn_cost array. */
305 static int max_uid_known
;
307 /* The following array records the insn_rtx_cost for every insn
308 in the instruction stream. */
310 static int *uid_insn_cost
;
312 /* The following array records the LOG_LINKS for every insn in the
313 instruction stream as an INSN_LIST rtx. */
315 static rtx
*uid_log_links
;
317 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
318 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
320 /* Incremented for each basic block. */
322 static int label_tick
;
324 /* Reset to label_tick for each label. */
326 static int label_tick_ebb_start
;
328 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
329 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
331 static enum machine_mode nonzero_bits_mode
;
333 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
334 be safely used. It is zero while computing them and after combine has
335 completed. This former test prevents propagating values based on
336 previously set values, which can be incorrect if a variable is modified
339 static int nonzero_sign_valid
;
342 /* Record one modification to rtl structure
343 to be undone by storing old_contents into *where. */
348 enum { UNDO_RTX
, UNDO_INT
, UNDO_MODE
} kind
;
349 union { rtx r
; int i
; enum machine_mode m
; } old_contents
;
350 union { rtx
*r
; int *i
; } where
;
353 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
354 num_undo says how many are currently recorded.
356 other_insn is nonzero if we have modified some other insn in the process
357 of working on subst_insn. It must be verified too. */
366 static struct undobuf undobuf
;
368 /* Number of times the pseudo being substituted for
369 was found and replaced. */
371 static int n_occurrences
;
373 static rtx
reg_nonzero_bits_for_combine (rtx
, enum machine_mode
, rtx
,
375 unsigned HOST_WIDE_INT
,
376 unsigned HOST_WIDE_INT
*);
377 static rtx
reg_num_sign_bit_copies_for_combine (rtx
, enum machine_mode
, rtx
,
379 unsigned int, unsigned int *);
380 static void do_SUBST (rtx
*, rtx
);
381 static void do_SUBST_INT (int *, int);
382 static void init_reg_last (void);
383 static void setup_incoming_promotions (rtx
);
384 static void set_nonzero_bits_and_sign_copies (rtx
, rtx
, void *);
385 static int cant_combine_insn_p (rtx
);
386 static int can_combine_p (rtx
, rtx
, rtx
, rtx
, rtx
*, rtx
*);
387 static int combinable_i3pat (rtx
, rtx
*, rtx
, rtx
, int, rtx
*);
388 static int contains_muldiv (rtx
);
389 static rtx
try_combine (rtx
, rtx
, rtx
, int *);
390 static void undo_all (void);
391 static void undo_commit (void);
392 static rtx
*find_split_point (rtx
*, rtx
);
393 static rtx
subst (rtx
, rtx
, rtx
, int, int);
394 static rtx
combine_simplify_rtx (rtx
, enum machine_mode
, int);
395 static rtx
simplify_if_then_else (rtx
);
396 static rtx
simplify_set (rtx
);
397 static rtx
simplify_logical (rtx
);
398 static rtx
expand_compound_operation (rtx
);
399 static rtx
expand_field_assignment (rtx
);
400 static rtx
make_extraction (enum machine_mode
, rtx
, HOST_WIDE_INT
,
401 rtx
, unsigned HOST_WIDE_INT
, int, int, int);
402 static rtx
extract_left_shift (rtx
, int);
403 static rtx
make_compound_operation (rtx
, enum rtx_code
);
404 static int get_pos_from_mask (unsigned HOST_WIDE_INT
,
405 unsigned HOST_WIDE_INT
*);
406 static rtx
canon_reg_for_combine (rtx
, rtx
);
407 static rtx
force_to_mode (rtx
, enum machine_mode
,
408 unsigned HOST_WIDE_INT
, int);
409 static rtx
if_then_else_cond (rtx
, rtx
*, rtx
*);
410 static rtx
known_cond (rtx
, enum rtx_code
, rtx
, rtx
);
411 static int rtx_equal_for_field_assignment_p (rtx
, rtx
);
412 static rtx
make_field_assignment (rtx
);
413 static rtx
apply_distributive_law (rtx
);
414 static rtx
distribute_and_simplify_rtx (rtx
, int);
415 static rtx
simplify_and_const_int_1 (enum machine_mode
, rtx
,
416 unsigned HOST_WIDE_INT
);
417 static rtx
simplify_and_const_int (rtx
, enum machine_mode
, rtx
,
418 unsigned HOST_WIDE_INT
);
419 static int merge_outer_ops (enum rtx_code
*, HOST_WIDE_INT
*, enum rtx_code
,
420 HOST_WIDE_INT
, enum machine_mode
, int *);
421 static rtx
simplify_shift_const_1 (enum rtx_code
, enum machine_mode
, rtx
, int);
422 static rtx
simplify_shift_const (rtx
, enum rtx_code
, enum machine_mode
, rtx
,
424 static int recog_for_combine (rtx
*, rtx
, rtx
*);
425 static rtx
gen_lowpart_for_combine (enum machine_mode
, rtx
);
426 static enum rtx_code
simplify_comparison (enum rtx_code
, rtx
*, rtx
*);
427 static void update_table_tick (rtx
);
428 static void record_value_for_reg (rtx
, rtx
, rtx
);
429 static void check_conversions (rtx
, rtx
);
430 static void record_dead_and_set_regs_1 (rtx
, rtx
, void *);
431 static void record_dead_and_set_regs (rtx
);
432 static int get_last_value_validate (rtx
*, rtx
, int, int);
433 static rtx
get_last_value (rtx
);
434 static int use_crosses_set_p (rtx
, int);
435 static void reg_dead_at_p_1 (rtx
, rtx
, void *);
436 static int reg_dead_at_p (rtx
, rtx
);
437 static void move_deaths (rtx
, rtx
, int, rtx
, rtx
*);
438 static int reg_bitfield_target_p (rtx
, rtx
);
439 static void distribute_notes (rtx
, rtx
, rtx
, rtx
, rtx
, rtx
);
440 static void distribute_links (rtx
);
441 static void mark_used_regs_combine (rtx
);
442 static void record_promoted_value (rtx
, rtx
);
443 static int unmentioned_reg_p_1 (rtx
*, void *);
444 static bool unmentioned_reg_p (rtx
, rtx
);
445 static void record_truncated_value (rtx
);
446 static bool reg_truncated_to_mode (enum machine_mode
, rtx
);
447 static rtx
gen_lowpart_or_truncate (enum machine_mode
, rtx
);
450 /* It is not safe to use ordinary gen_lowpart in combine.
451 See comments in gen_lowpart_for_combine. */
452 #undef RTL_HOOKS_GEN_LOWPART
453 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
455 /* Our implementation of gen_lowpart never emits a new pseudo. */
456 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
457 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
459 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
460 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
462 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
463 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
465 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
466 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
468 static const struct rtl_hooks combine_rtl_hooks
= RTL_HOOKS_INITIALIZER
;
471 /* This is used by find_single_use to locate an rtx in LOC that
472 contains exactly one use of DEST, which is typically either a REG
473 or CC0. It returns a pointer to the innermost rtx expression
474 containing DEST. Appearances of DEST that are being used to
475 totally replace it are not counted. */
478 find_single_use_1 (rtx dest
, rtx
*loc
)
481 enum rtx_code code
= GET_CODE (x
);
499 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
500 of a REG that occupies all of the REG, the insn uses DEST if
501 it is mentioned in the destination or the source. Otherwise, we
502 need just check the source. */
503 if (GET_CODE (SET_DEST (x
)) != CC0
504 && GET_CODE (SET_DEST (x
)) != PC
505 && !REG_P (SET_DEST (x
))
506 && ! (GET_CODE (SET_DEST (x
)) == SUBREG
507 && REG_P (SUBREG_REG (SET_DEST (x
)))
508 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x
))))
509 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
)
510 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x
)))
511 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
))))
514 return find_single_use_1 (dest
, &SET_SRC (x
));
518 return find_single_use_1 (dest
, &XEXP (x
, 0));
524 /* If it wasn't one of the common cases above, check each expression and
525 vector of this code. Look for a unique usage of DEST. */
527 fmt
= GET_RTX_FORMAT (code
);
528 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
532 if (dest
== XEXP (x
, i
)
533 || (REG_P (dest
) && REG_P (XEXP (x
, i
))
534 && REGNO (dest
) == REGNO (XEXP (x
, i
))))
537 this_result
= find_single_use_1 (dest
, &XEXP (x
, i
));
540 result
= this_result
;
541 else if (this_result
)
542 /* Duplicate usage. */
545 else if (fmt
[i
] == 'E')
549 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
551 if (XVECEXP (x
, i
, j
) == dest
553 && REG_P (XVECEXP (x
, i
, j
))
554 && REGNO (XVECEXP (x
, i
, j
)) == REGNO (dest
)))
557 this_result
= find_single_use_1 (dest
, &XVECEXP (x
, i
, j
));
560 result
= this_result
;
561 else if (this_result
)
571 /* See if DEST, produced in INSN, is used only a single time in the
572 sequel. If so, return a pointer to the innermost rtx expression in which
575 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
577 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
578 care about REG_DEAD notes or LOG_LINKS.
580 Otherwise, we find the single use by finding an insn that has a
581 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
582 only referenced once in that insn, we know that it must be the first
583 and last insn referencing DEST. */
586 find_single_use (rtx dest
, rtx insn
, rtx
*ploc
)
595 next
= NEXT_INSN (insn
);
597 || (!NONJUMP_INSN_P (next
) && !JUMP_P (next
)))
600 result
= find_single_use_1 (dest
, &PATTERN (next
));
610 for (next
= next_nonnote_insn (insn
);
611 next
!= 0 && !LABEL_P (next
);
612 next
= next_nonnote_insn (next
))
613 if (INSN_P (next
) && dead_or_set_p (next
, dest
))
615 for (link
= LOG_LINKS (next
); link
; link
= XEXP (link
, 1))
616 if (XEXP (link
, 0) == insn
)
621 result
= find_single_use_1 (dest
, &PATTERN (next
));
631 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
632 insn. The substitution can be undone by undo_all. If INTO is already
633 set to NEWVAL, do not record this change. Because computing NEWVAL might
634 also call SUBST, we have to compute it before we put anything into
638 do_SUBST (rtx
*into
, rtx newval
)
643 if (oldval
== newval
)
646 /* We'd like to catch as many invalid transformations here as
647 possible. Unfortunately, there are way too many mode changes
648 that are perfectly valid, so we'd waste too much effort for
649 little gain doing the checks here. Focus on catching invalid
650 transformations involving integer constants. */
651 if (GET_MODE_CLASS (GET_MODE (oldval
)) == MODE_INT
652 && GET_CODE (newval
) == CONST_INT
)
654 /* Sanity check that we're replacing oldval with a CONST_INT
655 that is a valid sign-extension for the original mode. */
656 gcc_assert (INTVAL (newval
)
657 == trunc_int_for_mode (INTVAL (newval
), GET_MODE (oldval
)));
659 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
660 CONST_INT is not valid, because after the replacement, the
661 original mode would be gone. Unfortunately, we can't tell
662 when do_SUBST is called to replace the operand thereof, so we
663 perform this test on oldval instead, checking whether an
664 invalid replacement took place before we got here. */
665 gcc_assert (!(GET_CODE (oldval
) == SUBREG
666 && GET_CODE (SUBREG_REG (oldval
)) == CONST_INT
));
667 gcc_assert (!(GET_CODE (oldval
) == ZERO_EXTEND
668 && GET_CODE (XEXP (oldval
, 0)) == CONST_INT
));
672 buf
= undobuf
.frees
, undobuf
.frees
= buf
->next
;
674 buf
= XNEW (struct undo
);
676 buf
->kind
= UNDO_RTX
;
678 buf
->old_contents
.r
= oldval
;
681 buf
->next
= undobuf
.undos
, undobuf
.undos
= buf
;
684 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
686 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
687 for the value of a HOST_WIDE_INT value (including CONST_INT) is
691 do_SUBST_INT (int *into
, int newval
)
696 if (oldval
== newval
)
700 buf
= undobuf
.frees
, undobuf
.frees
= buf
->next
;
702 buf
= XNEW (struct undo
);
704 buf
->kind
= UNDO_INT
;
706 buf
->old_contents
.i
= oldval
;
709 buf
->next
= undobuf
.undos
, undobuf
.undos
= buf
;
712 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
714 /* Similar to SUBST, but just substitute the mode. This is used when
715 changing the mode of a pseudo-register, so that any other
716 references to the entry in the regno_reg_rtx array will change as
720 do_SUBST_MODE (rtx
*into
, enum machine_mode newval
)
723 enum machine_mode oldval
= GET_MODE (*into
);
725 if (oldval
== newval
)
729 buf
= undobuf
.frees
, undobuf
.frees
= buf
->next
;
731 buf
= XNEW (struct undo
);
733 buf
->kind
= UNDO_MODE
;
735 buf
->old_contents
.m
= oldval
;
736 PUT_MODE (*into
, newval
);
738 buf
->next
= undobuf
.undos
, undobuf
.undos
= buf
;
741 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
743 /* Subroutine of try_combine. Determine whether the combine replacement
744 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
745 insn_rtx_cost that the original instruction sequence I1, I2, I3 and
746 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
747 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
748 function returns false, if the costs of all instructions can be
749 estimated, and the replacements are more expensive than the original
753 combine_validate_cost (rtx i1
, rtx i2
, rtx i3
, rtx newpat
, rtx newi2pat
,
756 int i1_cost
, i2_cost
, i3_cost
;
757 int new_i2_cost
, new_i3_cost
;
758 int old_cost
, new_cost
;
760 /* Lookup the original insn_rtx_costs. */
761 i2_cost
= INSN_COST (i2
);
762 i3_cost
= INSN_COST (i3
);
766 i1_cost
= INSN_COST (i1
);
767 old_cost
= (i1_cost
> 0 && i2_cost
> 0 && i3_cost
> 0)
768 ? i1_cost
+ i2_cost
+ i3_cost
: 0;
772 old_cost
= (i2_cost
> 0 && i3_cost
> 0) ? i2_cost
+ i3_cost
: 0;
776 /* Calculate the replacement insn_rtx_costs. */
777 new_i3_cost
= insn_rtx_cost (newpat
);
780 new_i2_cost
= insn_rtx_cost (newi2pat
);
781 new_cost
= (new_i2_cost
> 0 && new_i3_cost
> 0)
782 ? new_i2_cost
+ new_i3_cost
: 0;
786 new_cost
= new_i3_cost
;
790 if (undobuf
.other_insn
)
792 int old_other_cost
, new_other_cost
;
794 old_other_cost
= INSN_COST (undobuf
.other_insn
);
795 new_other_cost
= insn_rtx_cost (newotherpat
);
796 if (old_other_cost
> 0 && new_other_cost
> 0)
798 old_cost
+= old_other_cost
;
799 new_cost
+= new_other_cost
;
805 /* Disallow this recombination if both new_cost and old_cost are
806 greater than zero, and new_cost is greater than old cost. */
808 && new_cost
> old_cost
)
815 "rejecting combination of insns %d, %d and %d\n",
816 INSN_UID (i1
), INSN_UID (i2
), INSN_UID (i3
));
817 fprintf (dump_file
, "original costs %d + %d + %d = %d\n",
818 i1_cost
, i2_cost
, i3_cost
, old_cost
);
823 "rejecting combination of insns %d and %d\n",
824 INSN_UID (i2
), INSN_UID (i3
));
825 fprintf (dump_file
, "original costs %d + %d = %d\n",
826 i2_cost
, i3_cost
, old_cost
);
831 fprintf (dump_file
, "replacement costs %d + %d = %d\n",
832 new_i2_cost
, new_i3_cost
, new_cost
);
835 fprintf (dump_file
, "replacement cost %d\n", new_cost
);
841 /* Update the uid_insn_cost array with the replacement costs. */
842 INSN_COST (i2
) = new_i2_cost
;
843 INSN_COST (i3
) = new_i3_cost
;
851 /* Delete any insns that copy a register to itself. */
854 delete_noop_moves (void)
861 for (insn
= BB_HEAD (bb
); insn
!= NEXT_INSN (BB_END (bb
)); insn
= next
)
863 next
= NEXT_INSN (insn
);
864 if (INSN_P (insn
) && noop_move_p (insn
))
868 /* If we're about to remove the first insn of a libcall
869 then move the libcall note to the next real insn and
870 update the retval note. */
871 if ((note
= find_reg_note (insn
, REG_LIBCALL
, NULL_RTX
))
872 && XEXP (note
, 0) != insn
)
874 rtx new_libcall_insn
= next_real_insn (insn
);
875 rtx retval_note
= find_reg_note (XEXP (note
, 0),
876 REG_RETVAL
, NULL_RTX
);
877 REG_NOTES (new_libcall_insn
)
878 = gen_rtx_INSN_LIST (REG_LIBCALL
, XEXP (note
, 0),
879 REG_NOTES (new_libcall_insn
));
880 XEXP (retval_note
, 0) = new_libcall_insn
;
884 fprintf (dump_file
, "deleting noop move %d\n", INSN_UID (insn
));
886 delete_insn_and_edges (insn
);
893 /* Fill in log links field for all insns. */
896 create_log_links (void)
900 struct df_ref
**def_vec
, **use_vec
;
902 next_use
= XCNEWVEC (rtx
, max_reg_num ());
904 /* Pass through each block from the end, recording the uses of each
905 register and establishing log links when def is encountered.
906 Note that we do not clear next_use array in order to save time,
907 so we have to test whether the use is in the same basic block as def.
909 There are a few cases below when we do not consider the definition or
910 usage -- these are taken from original flow.c did. Don't ask me why it is
911 done this way; I don't know and if it works, I don't want to know. */
915 FOR_BB_INSNS_REVERSE (bb
, insn
)
920 /* Log links are created only once. */
921 gcc_assert (!LOG_LINKS (insn
));
923 for (def_vec
= DF_INSN_DEFS (insn
); *def_vec
; def_vec
++)
925 struct df_ref
*def
= *def_vec
;
926 int regno
= DF_REF_REGNO (def
);
929 if (!next_use
[regno
])
932 /* Do not consider if it is pre/post modification in MEM. */
933 if (DF_REF_FLAGS (def
) & DF_REF_PRE_POST_MODIFY
)
936 /* Do not make the log link for frame pointer. */
937 if ((regno
== FRAME_POINTER_REGNUM
938 && (! reload_completed
|| frame_pointer_needed
))
939 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
940 || (regno
== HARD_FRAME_POINTER_REGNUM
941 && (! reload_completed
|| frame_pointer_needed
))
943 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
944 || (regno
== ARG_POINTER_REGNUM
&& fixed_regs
[regno
])
949 use_insn
= next_use
[regno
];
950 if (BLOCK_FOR_INSN (use_insn
) == bb
)
954 We don't build a LOG_LINK for hard registers contained
955 in ASM_OPERANDs. If these registers get replaced,
956 we might wind up changing the semantics of the insn,
957 even if reload can make what appear to be valid
958 assignments later. */
959 if (regno
>= FIRST_PSEUDO_REGISTER
960 || asm_noperands (PATTERN (use_insn
)) < 0)
961 LOG_LINKS (use_insn
) =
962 alloc_INSN_LIST (insn
, LOG_LINKS (use_insn
));
964 next_use
[regno
] = NULL_RTX
;
967 for (use_vec
= DF_INSN_USES (insn
); *use_vec
; use_vec
++)
969 struct df_ref
*use
= *use_vec
;
970 int regno
= DF_REF_REGNO (use
);
972 /* Do not consider the usage of the stack pointer
974 if (DF_REF_FLAGS (use
) & DF_REF_CALL_STACK_USAGE
)
977 next_use
[regno
] = insn
;
985 /* Clear LOG_LINKS fields of insns. */
988 clear_log_links (void)
992 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
994 free_INSN_LIST_list (&LOG_LINKS (insn
));
1000 /* Main entry point for combiner. F is the first insn of the function.
1001 NREGS is the first unused pseudo-reg number.
1003 Return nonzero if the combiner has turned an indirect jump
1004 instruction into a direct jump. */
1006 combine_instructions (rtx f
, unsigned int nregs
)
1012 rtx links
, nextlinks
;
1015 int new_direct_jump_p
= 0;
1017 for (first
= f
; first
&& !INSN_P (first
); )
1018 first
= NEXT_INSN (first
);
1022 combine_attempts
= 0;
1025 combine_successes
= 0;
1027 combine_max_regno
= nregs
;
1029 rtl_hooks
= combine_rtl_hooks
;
1031 reg_stat
= XCNEWVEC (struct reg_stat
, nregs
);
1033 init_recog_no_volatile ();
1035 /* Allocate array for insn info. */
1036 max_uid_known
= get_max_uid ();
1037 uid_log_links
= XCNEWVEC (rtx
, max_uid_known
+ 1);
1038 uid_insn_cost
= XCNEWVEC (int, max_uid_known
+ 1);
1040 nonzero_bits_mode
= mode_for_size (HOST_BITS_PER_WIDE_INT
, MODE_INT
, 0);
1042 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1043 problems when, for example, we have j <<= 1 in a loop. */
1045 nonzero_sign_valid
= 0;
1047 /* Scan all SETs and see if we can deduce anything about what
1048 bits are known to be zero for some registers and how many copies
1049 of the sign bit are known to exist for those registers.
1051 Also set any known values so that we can use it while searching
1052 for what bits are known to be set. */
1054 label_tick
= label_tick_ebb_start
= 1;
1056 setup_incoming_promotions (first
);
1058 create_log_links ();
1059 FOR_EACH_BB (this_basic_block
)
1064 FOR_BB_INSNS (this_basic_block
, insn
)
1065 if (INSN_P (insn
) && BLOCK_FOR_INSN (insn
))
1067 subst_low_luid
= DF_INSN_LUID (insn
);
1070 note_stores (PATTERN (insn
), set_nonzero_bits_and_sign_copies
,
1072 record_dead_and_set_regs (insn
);
1075 for (links
= REG_NOTES (insn
); links
; links
= XEXP (links
, 1))
1076 if (REG_NOTE_KIND (links
) == REG_INC
)
1077 set_nonzero_bits_and_sign_copies (XEXP (links
, 0), NULL_RTX
,
1081 /* Record the current insn_rtx_cost of this instruction. */
1082 if (NONJUMP_INSN_P (insn
))
1083 INSN_COST (insn
) = insn_rtx_cost (PATTERN (insn
));
1085 fprintf(dump_file
, "insn_cost %d: %d\n",
1086 INSN_UID (insn
), INSN_COST (insn
));
1088 else if (LABEL_P (insn
))
1089 label_tick_ebb_start
= label_tick
;
1092 nonzero_sign_valid
= 1;
1094 /* Now scan all the insns in forward order. */
1096 label_tick
= label_tick_ebb_start
= 1;
1098 setup_incoming_promotions (first
);
1100 FOR_EACH_BB (this_basic_block
)
1105 for (insn
= BB_HEAD (this_basic_block
);
1106 insn
!= NEXT_INSN (BB_END (this_basic_block
));
1107 insn
= next
? next
: NEXT_INSN (insn
))
1112 /* See if we know about function return values before this
1113 insn based upon SUBREG flags. */
1114 check_conversions (insn
, PATTERN (insn
));
1116 /* Try this insn with each insn it links back to. */
1118 for (links
= LOG_LINKS (insn
); links
; links
= XEXP (links
, 1))
1119 if ((next
= try_combine (insn
, XEXP (links
, 0),
1120 NULL_RTX
, &new_direct_jump_p
)) != 0)
1123 /* Try each sequence of three linked insns ending with this one. */
1125 for (links
= LOG_LINKS (insn
); links
; links
= XEXP (links
, 1))
1127 rtx link
= XEXP (links
, 0);
1129 /* If the linked insn has been replaced by a note, then there
1130 is no point in pursuing this chain any further. */
1134 for (nextlinks
= LOG_LINKS (link
);
1136 nextlinks
= XEXP (nextlinks
, 1))
1137 if ((next
= try_combine (insn
, link
,
1138 XEXP (nextlinks
, 0),
1139 &new_direct_jump_p
)) != 0)
1144 /* Try to combine a jump insn that uses CC0
1145 with a preceding insn that sets CC0, and maybe with its
1146 logical predecessor as well.
1147 This is how we make decrement-and-branch insns.
1148 We need this special code because data flow connections
1149 via CC0 do not get entered in LOG_LINKS. */
1152 && (prev
= prev_nonnote_insn (insn
)) != 0
1153 && NONJUMP_INSN_P (prev
)
1154 && sets_cc0_p (PATTERN (prev
)))
1156 if ((next
= try_combine (insn
, prev
,
1157 NULL_RTX
, &new_direct_jump_p
)) != 0)
1160 for (nextlinks
= LOG_LINKS (prev
); nextlinks
;
1161 nextlinks
= XEXP (nextlinks
, 1))
1162 if ((next
= try_combine (insn
, prev
,
1163 XEXP (nextlinks
, 0),
1164 &new_direct_jump_p
)) != 0)
1168 /* Do the same for an insn that explicitly references CC0. */
1169 if (NONJUMP_INSN_P (insn
)
1170 && (prev
= prev_nonnote_insn (insn
)) != 0
1171 && NONJUMP_INSN_P (prev
)
1172 && sets_cc0_p (PATTERN (prev
))
1173 && GET_CODE (PATTERN (insn
)) == SET
1174 && reg_mentioned_p (cc0_rtx
, SET_SRC (PATTERN (insn
))))
1176 if ((next
= try_combine (insn
, prev
,
1177 NULL_RTX
, &new_direct_jump_p
)) != 0)
1180 for (nextlinks
= LOG_LINKS (prev
); nextlinks
;
1181 nextlinks
= XEXP (nextlinks
, 1))
1182 if ((next
= try_combine (insn
, prev
,
1183 XEXP (nextlinks
, 0),
1184 &new_direct_jump_p
)) != 0)
1188 /* Finally, see if any of the insns that this insn links to
1189 explicitly references CC0. If so, try this insn, that insn,
1190 and its predecessor if it sets CC0. */
1191 for (links
= LOG_LINKS (insn
); links
; links
= XEXP (links
, 1))
1192 if (NONJUMP_INSN_P (XEXP (links
, 0))
1193 && GET_CODE (PATTERN (XEXP (links
, 0))) == SET
1194 && reg_mentioned_p (cc0_rtx
, SET_SRC (PATTERN (XEXP (links
, 0))))
1195 && (prev
= prev_nonnote_insn (XEXP (links
, 0))) != 0
1196 && NONJUMP_INSN_P (prev
)
1197 && sets_cc0_p (PATTERN (prev
))
1198 && (next
= try_combine (insn
, XEXP (links
, 0),
1199 prev
, &new_direct_jump_p
)) != 0)
1203 /* Try combining an insn with two different insns whose results it
1205 for (links
= LOG_LINKS (insn
); links
; links
= XEXP (links
, 1))
1206 for (nextlinks
= XEXP (links
, 1); nextlinks
;
1207 nextlinks
= XEXP (nextlinks
, 1))
1208 if ((next
= try_combine (insn
, XEXP (links
, 0),
1209 XEXP (nextlinks
, 0),
1210 &new_direct_jump_p
)) != 0)
1213 /* Try this insn with each REG_EQUAL note it links back to. */
1214 for (links
= LOG_LINKS (insn
); links
; links
= XEXP (links
, 1))
1217 rtx temp
= XEXP (links
, 0);
1218 if ((set
= single_set (temp
)) != 0
1219 && (note
= find_reg_equal_equiv_note (temp
)) != 0
1220 && (note
= XEXP (note
, 0), GET_CODE (note
)) != EXPR_LIST
1221 /* Avoid using a register that may already been marked
1222 dead by an earlier instruction. */
1223 && ! unmentioned_reg_p (note
, SET_SRC (set
))
1224 && (GET_MODE (note
) == VOIDmode
1225 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set
)))
1226 : GET_MODE (SET_DEST (set
)) == GET_MODE (note
)))
1228 /* Temporarily replace the set's source with the
1229 contents of the REG_EQUAL note. The insn will
1230 be deleted or recognized by try_combine. */
1231 rtx orig
= SET_SRC (set
);
1232 SET_SRC (set
) = note
;
1234 i2mod_old_rhs
= copy_rtx (orig
);
1235 i2mod_new_rhs
= copy_rtx (note
);
1236 next
= try_combine (insn
, i2mod
, NULL_RTX
,
1237 &new_direct_jump_p
);
1241 SET_SRC (set
) = orig
;
1246 record_dead_and_set_regs (insn
);
1251 else if (LABEL_P (insn
))
1252 label_tick_ebb_start
= label_tick
;
1258 new_direct_jump_p
|= purge_all_dead_edges ();
1259 delete_noop_moves ();
1262 free (uid_log_links
);
1263 free (uid_insn_cost
);
1267 struct undo
*undo
, *next
;
1268 for (undo
= undobuf
.frees
; undo
; undo
= next
)
1276 total_attempts
+= combine_attempts
;
1277 total_merges
+= combine_merges
;
1278 total_extras
+= combine_extras
;
1279 total_successes
+= combine_successes
;
1281 nonzero_sign_valid
= 0;
1282 rtl_hooks
= general_rtl_hooks
;
1284 /* Make recognizer allow volatile MEMs again. */
1287 return new_direct_jump_p
;
1290 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1293 init_reg_last (void)
1296 for (i
= 0; i
< combine_max_regno
; i
++)
1297 memset (reg_stat
+ i
, 0, offsetof (struct reg_stat
, sign_bit_copies
));
1300 /* Set up any promoted values for incoming argument registers. */
1303 setup_incoming_promotions (rtx first
)
1307 if (!targetm
.calls
.promote_function_args (TREE_TYPE (cfun
->decl
)))
1310 for (arg
= DECL_ARGUMENTS (current_function_decl
); arg
;
1311 arg
= TREE_CHAIN (arg
))
1313 rtx reg
= DECL_INCOMING_RTL (arg
);
1318 if (TYPE_MODE (DECL_ARG_TYPE (arg
)) == TYPE_MODE (TREE_TYPE (arg
)))
1320 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (arg
));
1321 int uns
= TYPE_UNSIGNED (TREE_TYPE (arg
));
1323 mode
= promote_mode (TREE_TYPE (arg
), mode
, &uns
, 1);
1324 if (mode
== GET_MODE (reg
) && mode
!= DECL_MODE (arg
))
1327 x
= gen_rtx_CLOBBER (DECL_MODE (arg
), const0_rtx
);
1328 x
= gen_rtx_fmt_e ((uns
? ZERO_EXTEND
: SIGN_EXTEND
), mode
, x
);
1329 record_value_for_reg (reg
, first
, x
);
1335 /* Called via note_stores. If X is a pseudo that is narrower than
1336 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1338 If we are setting only a portion of X and we can't figure out what
1339 portion, assume all bits will be used since we don't know what will
1342 Similarly, set how many bits of X are known to be copies of the sign bit
1343 at all locations in the function. This is the smallest number implied
1347 set_nonzero_bits_and_sign_copies (rtx x
, rtx set
, void *data
)
1349 rtx insn
= (rtx
) data
;
1353 && REGNO (x
) >= FIRST_PSEUDO_REGISTER
1354 /* If this register is undefined at the start of the file, we can't
1355 say what its contents were. */
1356 && ! REGNO_REG_SET_P
1357 (DF_LR_IN (ENTRY_BLOCK_PTR
->next_bb
), REGNO (x
))
1358 && GET_MODE_BITSIZE (GET_MODE (x
)) <= HOST_BITS_PER_WIDE_INT
)
1360 if (set
== 0 || GET_CODE (set
) == CLOBBER
)
1362 reg_stat
[REGNO (x
)].nonzero_bits
= GET_MODE_MASK (GET_MODE (x
));
1363 reg_stat
[REGNO (x
)].sign_bit_copies
= 1;
1367 /* If this register is being initialized using itself, and the
1368 register is uninitialized in this basic block, and there are
1369 no LOG_LINKS which set the register, then part of the
1370 register is uninitialized. In that case we can't assume
1371 anything about the number of nonzero bits.
1373 ??? We could do better if we checked this in
1374 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1375 could avoid making assumptions about the insn which initially
1376 sets the register, while still using the information in other
1377 insns. We would have to be careful to check every insn
1378 involved in the combination. */
1381 && reg_referenced_p (x
, PATTERN (insn
))
1382 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn
)),
1387 for (link
= LOG_LINKS (insn
); link
; link
= XEXP (link
, 1))
1389 if (dead_or_set_p (XEXP (link
, 0), x
))
1394 reg_stat
[REGNO (x
)].nonzero_bits
= GET_MODE_MASK (GET_MODE (x
));
1395 reg_stat
[REGNO (x
)].sign_bit_copies
= 1;
1400 /* If this is a complex assignment, see if we can convert it into a
1401 simple assignment. */
1402 set
= expand_field_assignment (set
);
1404 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1405 set what we know about X. */
1407 if (SET_DEST (set
) == x
1408 || (GET_CODE (SET_DEST (set
)) == SUBREG
1409 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set
)))
1410 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set
)))))
1411 && SUBREG_REG (SET_DEST (set
)) == x
))
1413 rtx src
= SET_SRC (set
);
1415 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1416 /* If X is narrower than a word and SRC is a non-negative
1417 constant that would appear negative in the mode of X,
1418 sign-extend it for use in reg_stat[].nonzero_bits because some
1419 machines (maybe most) will actually do the sign-extension
1420 and this is the conservative approach.
1422 ??? For 2.5, try to tighten up the MD files in this regard
1423 instead of this kludge. */
1425 if (GET_MODE_BITSIZE (GET_MODE (x
)) < BITS_PER_WORD
1426 && GET_CODE (src
) == CONST_INT
1428 && 0 != (INTVAL (src
)
1429 & ((HOST_WIDE_INT
) 1
1430 << (GET_MODE_BITSIZE (GET_MODE (x
)) - 1))))
1431 src
= GEN_INT (INTVAL (src
)
1432 | ((HOST_WIDE_INT
) (-1)
1433 << GET_MODE_BITSIZE (GET_MODE (x
))));
1436 /* Don't call nonzero_bits if it cannot change anything. */
1437 if (reg_stat
[REGNO (x
)].nonzero_bits
!= ~(unsigned HOST_WIDE_INT
) 0)
1438 reg_stat
[REGNO (x
)].nonzero_bits
1439 |= nonzero_bits (src
, nonzero_bits_mode
);
1440 num
= num_sign_bit_copies (SET_SRC (set
), GET_MODE (x
));
1441 if (reg_stat
[REGNO (x
)].sign_bit_copies
== 0
1442 || reg_stat
[REGNO (x
)].sign_bit_copies
> num
)
1443 reg_stat
[REGNO (x
)].sign_bit_copies
= num
;
1447 reg_stat
[REGNO (x
)].nonzero_bits
= GET_MODE_MASK (GET_MODE (x
));
1448 reg_stat
[REGNO (x
)].sign_bit_copies
= 1;
1453 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1454 insns that were previously combined into I3 or that will be combined
1455 into the merger of INSN and I3.
1457 Return 0 if the combination is not allowed for any reason.
1459 If the combination is allowed, *PDEST will be set to the single
1460 destination of INSN and *PSRC to the single source, and this function
1464 can_combine_p (rtx insn
, rtx i3
, rtx pred ATTRIBUTE_UNUSED
, rtx succ
,
1465 rtx
*pdest
, rtx
*psrc
)
1468 rtx set
= 0, src
, dest
;
1473 int all_adjacent
= (succ
? (next_active_insn (insn
) == succ
1474 && next_active_insn (succ
) == i3
)
1475 : next_active_insn (insn
) == i3
);
1477 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1478 or a PARALLEL consisting of such a SET and CLOBBERs.
1480 If INSN has CLOBBER parallel parts, ignore them for our processing.
1481 By definition, these happen during the execution of the insn. When it
1482 is merged with another insn, all bets are off. If they are, in fact,
1483 needed and aren't also supplied in I3, they may be added by
1484 recog_for_combine. Otherwise, it won't match.
1486 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1489 Get the source and destination of INSN. If more than one, can't
1492 if (GET_CODE (PATTERN (insn
)) == SET
)
1493 set
= PATTERN (insn
);
1494 else if (GET_CODE (PATTERN (insn
)) == PARALLEL
1495 && GET_CODE (XVECEXP (PATTERN (insn
), 0, 0)) == SET
)
1497 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
1499 rtx elt
= XVECEXP (PATTERN (insn
), 0, i
);
1502 switch (GET_CODE (elt
))
1504 /* This is important to combine floating point insns
1505 for the SH4 port. */
1507 /* Combining an isolated USE doesn't make sense.
1508 We depend here on combinable_i3pat to reject them. */
1509 /* The code below this loop only verifies that the inputs of
1510 the SET in INSN do not change. We call reg_set_between_p
1511 to verify that the REG in the USE does not change between
1513 If the USE in INSN was for a pseudo register, the matching
1514 insn pattern will likely match any register; combining this
1515 with any other USE would only be safe if we knew that the
1516 used registers have identical values, or if there was
1517 something to tell them apart, e.g. different modes. For
1518 now, we forgo such complicated tests and simply disallow
1519 combining of USES of pseudo registers with any other USE. */
1520 if (REG_P (XEXP (elt
, 0))
1521 && GET_CODE (PATTERN (i3
)) == PARALLEL
)
1523 rtx i3pat
= PATTERN (i3
);
1524 int i
= XVECLEN (i3pat
, 0) - 1;
1525 unsigned int regno
= REGNO (XEXP (elt
, 0));
1529 rtx i3elt
= XVECEXP (i3pat
, 0, i
);
1531 if (GET_CODE (i3elt
) == USE
1532 && REG_P (XEXP (i3elt
, 0))
1533 && (REGNO (XEXP (i3elt
, 0)) == regno
1534 ? reg_set_between_p (XEXP (elt
, 0),
1535 PREV_INSN (insn
), i3
)
1536 : regno
>= FIRST_PSEUDO_REGISTER
))
1543 /* We can ignore CLOBBERs. */
1548 /* Ignore SETs whose result isn't used but not those that
1549 have side-effects. */
1550 if (find_reg_note (insn
, REG_UNUSED
, SET_DEST (elt
))
1551 && (!(note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
))
1552 || INTVAL (XEXP (note
, 0)) <= 0)
1553 && ! side_effects_p (elt
))
1556 /* If we have already found a SET, this is a second one and
1557 so we cannot combine with this insn. */
1565 /* Anything else means we can't combine. */
1571 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1572 so don't do anything with it. */
1573 || GET_CODE (SET_SRC (set
)) == ASM_OPERANDS
)
1582 set
= expand_field_assignment (set
);
1583 src
= SET_SRC (set
), dest
= SET_DEST (set
);
1585 /* Don't eliminate a store in the stack pointer. */
1586 if (dest
== stack_pointer_rtx
1587 /* Don't combine with an insn that sets a register to itself if it has
1588 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1589 || (rtx_equal_p (src
, dest
) && find_reg_note (insn
, REG_EQUAL
, NULL_RTX
))
1590 /* Can't merge an ASM_OPERANDS. */
1591 || GET_CODE (src
) == ASM_OPERANDS
1592 /* Can't merge a function call. */
1593 || GET_CODE (src
) == CALL
1594 /* Don't eliminate a function call argument. */
1596 && (find_reg_fusage (i3
, USE
, dest
)
1598 && REGNO (dest
) < FIRST_PSEUDO_REGISTER
1599 && global_regs
[REGNO (dest
)])))
1600 /* Don't substitute into an incremented register. */
1601 || FIND_REG_INC_NOTE (i3
, dest
)
1602 || (succ
&& FIND_REG_INC_NOTE (succ
, dest
))
1603 /* Don't substitute into a non-local goto, this confuses CFG. */
1604 || (JUMP_P (i3
) && find_reg_note (i3
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
1606 /* Don't combine the end of a libcall into anything. */
1607 /* ??? This gives worse code, and appears to be unnecessary, since no
1608 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1609 use REG_RETVAL notes for noconflict blocks, but other code here
1610 makes sure that those insns don't disappear. */
1611 || find_reg_note (insn
, REG_RETVAL
, NULL_RTX
)
1613 /* Make sure that DEST is not used after SUCC but before I3. */
1614 || (succ
&& ! all_adjacent
1615 && reg_used_between_p (dest
, succ
, i3
))
1616 /* Make sure that the value that is to be substituted for the register
1617 does not use any registers whose values alter in between. However,
1618 If the insns are adjacent, a use can't cross a set even though we
1619 think it might (this can happen for a sequence of insns each setting
1620 the same destination; last_set of that register might point to
1621 a NOTE). If INSN has a REG_EQUIV note, the register is always
1622 equivalent to the memory so the substitution is valid even if there
1623 are intervening stores. Also, don't move a volatile asm or
1624 UNSPEC_VOLATILE across any other insns. */
1627 || ! find_reg_note (insn
, REG_EQUIV
, src
))
1628 && use_crosses_set_p (src
, DF_INSN_LUID (insn
)))
1629 || (GET_CODE (src
) == ASM_OPERANDS
&& MEM_VOLATILE_P (src
))
1630 || GET_CODE (src
) == UNSPEC_VOLATILE
))
1631 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1632 better register allocation by not doing the combine. */
1633 || find_reg_note (i3
, REG_NO_CONFLICT
, dest
)
1634 || (succ
&& find_reg_note (succ
, REG_NO_CONFLICT
, dest
))
1635 /* Don't combine across a CALL_INSN, because that would possibly
1636 change whether the life span of some REGs crosses calls or not,
1637 and it is a pain to update that information.
1638 Exception: if source is a constant, moving it later can't hurt.
1639 Accept that special case, because it helps -fforce-addr a lot. */
1640 || (DF_INSN_LUID (insn
) < last_call_luid
&& ! CONSTANT_P (src
)))
1643 /* DEST must either be a REG or CC0. */
1646 /* If register alignment is being enforced for multi-word items in all
1647 cases except for parameters, it is possible to have a register copy
1648 insn referencing a hard register that is not allowed to contain the
1649 mode being copied and which would not be valid as an operand of most
1650 insns. Eliminate this problem by not combining with such an insn.
1652 Also, on some machines we don't want to extend the life of a hard
1656 && ((REGNO (dest
) < FIRST_PSEUDO_REGISTER
1657 && ! HARD_REGNO_MODE_OK (REGNO (dest
), GET_MODE (dest
)))
1658 /* Don't extend the life of a hard register unless it is
1659 user variable (if we have few registers) or it can't
1660 fit into the desired register (meaning something special
1662 Also avoid substituting a return register into I3, because
1663 reload can't handle a conflict with constraints of other
1665 || (REGNO (src
) < FIRST_PSEUDO_REGISTER
1666 && ! HARD_REGNO_MODE_OK (REGNO (src
), GET_MODE (src
)))))
1669 else if (GET_CODE (dest
) != CC0
)
1673 if (GET_CODE (PATTERN (i3
)) == PARALLEL
)
1674 for (i
= XVECLEN (PATTERN (i3
), 0) - 1; i
>= 0; i
--)
1675 if (GET_CODE (XVECEXP (PATTERN (i3
), 0, i
)) == CLOBBER
)
1677 /* Don't substitute for a register intended as a clobberable
1679 rtx reg
= XEXP (XVECEXP (PATTERN (i3
), 0, i
), 0);
1680 if (rtx_equal_p (reg
, dest
))
1683 /* If the clobber represents an earlyclobber operand, we must not
1684 substitute an expression containing the clobbered register.
1685 As we do not analyze the constraint strings here, we have to
1686 make the conservative assumption. However, if the register is
1687 a fixed hard reg, the clobber cannot represent any operand;
1688 we leave it up to the machine description to either accept or
1689 reject use-and-clobber patterns. */
1691 || REGNO (reg
) >= FIRST_PSEUDO_REGISTER
1692 || !fixed_regs
[REGNO (reg
)])
1693 if (reg_overlap_mentioned_p (reg
, src
))
1697 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1698 or not), reject, unless nothing volatile comes between it and I3 */
1700 if (GET_CODE (src
) == ASM_OPERANDS
|| volatile_refs_p (src
))
1702 /* Make sure succ doesn't contain a volatile reference. */
1703 if (succ
!= 0 && volatile_refs_p (PATTERN (succ
)))
1706 for (p
= NEXT_INSN (insn
); p
!= i3
; p
= NEXT_INSN (p
))
1707 if (INSN_P (p
) && p
!= succ
&& volatile_refs_p (PATTERN (p
)))
1711 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1712 to be an explicit register variable, and was chosen for a reason. */
1714 if (GET_CODE (src
) == ASM_OPERANDS
1715 && REG_P (dest
) && REGNO (dest
) < FIRST_PSEUDO_REGISTER
)
1718 /* If there are any volatile insns between INSN and I3, reject, because
1719 they might affect machine state. */
1721 for (p
= NEXT_INSN (insn
); p
!= i3
; p
= NEXT_INSN (p
))
1722 if (INSN_P (p
) && p
!= succ
&& volatile_insn_p (PATTERN (p
)))
1725 /* If INSN contains an autoincrement or autodecrement, make sure that
1726 register is not used between there and I3, and not already used in
1727 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1728 Also insist that I3 not be a jump; if it were one
1729 and the incremented register were spilled, we would lose. */
1732 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
1733 if (REG_NOTE_KIND (link
) == REG_INC
1735 || reg_used_between_p (XEXP (link
, 0), insn
, i3
)
1736 || (pred
!= NULL_RTX
1737 && reg_overlap_mentioned_p (XEXP (link
, 0), PATTERN (pred
)))
1738 || (succ
!= NULL_RTX
1739 && reg_overlap_mentioned_p (XEXP (link
, 0), PATTERN (succ
)))
1740 || reg_overlap_mentioned_p (XEXP (link
, 0), PATTERN (i3
))))
1745 /* Don't combine an insn that follows a CC0-setting insn.
1746 An insn that uses CC0 must not be separated from the one that sets it.
1747 We do, however, allow I2 to follow a CC0-setting insn if that insn
1748 is passed as I1; in that case it will be deleted also.
1749 We also allow combining in this case if all the insns are adjacent
1750 because that would leave the two CC0 insns adjacent as well.
1751 It would be more logical to test whether CC0 occurs inside I1 or I2,
1752 but that would be much slower, and this ought to be equivalent. */
1754 p
= prev_nonnote_insn (insn
);
1755 if (p
&& p
!= pred
&& NONJUMP_INSN_P (p
) && sets_cc0_p (PATTERN (p
))
1760 /* If we get here, we have passed all the tests and the combination is
1769 /* LOC is the location within I3 that contains its pattern or the component
1770 of a PARALLEL of the pattern. We validate that it is valid for combining.
1772 One problem is if I3 modifies its output, as opposed to replacing it
1773 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1774 so would produce an insn that is not equivalent to the original insns.
1778 (set (reg:DI 101) (reg:DI 100))
1779 (set (subreg:SI (reg:DI 101) 0) <foo>)
1781 This is NOT equivalent to:
1783 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1784 (set (reg:DI 101) (reg:DI 100))])
1786 Not only does this modify 100 (in which case it might still be valid
1787 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1789 We can also run into a problem if I2 sets a register that I1
1790 uses and I1 gets directly substituted into I3 (not via I2). In that
1791 case, we would be getting the wrong value of I2DEST into I3, so we
1792 must reject the combination. This case occurs when I2 and I1 both
1793 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1794 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1795 of a SET must prevent combination from occurring.
1797 Before doing the above check, we first try to expand a field assignment
1798 into a set of logical operations.
1800 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1801 we place a register that is both set and used within I3. If more than one
1802 such register is detected, we fail.
1804 Return 1 if the combination is valid, zero otherwise. */
1807 combinable_i3pat (rtx i3
, rtx
*loc
, rtx i2dest
, rtx i1dest
,
1808 int i1_not_in_src
, rtx
*pi3dest_killed
)
1812 if (GET_CODE (x
) == SET
)
1815 rtx dest
= SET_DEST (set
);
1816 rtx src
= SET_SRC (set
);
1817 rtx inner_dest
= dest
;
1820 while (GET_CODE (inner_dest
) == STRICT_LOW_PART
1821 || GET_CODE (inner_dest
) == SUBREG
1822 || GET_CODE (inner_dest
) == ZERO_EXTRACT
)
1823 inner_dest
= XEXP (inner_dest
, 0);
1825 /* Check for the case where I3 modifies its output, as discussed
1826 above. We don't want to prevent pseudos from being combined
1827 into the address of a MEM, so only prevent the combination if
1828 i1 or i2 set the same MEM. */
1829 if ((inner_dest
!= dest
&&
1830 (!MEM_P (inner_dest
)
1831 || rtx_equal_p (i2dest
, inner_dest
)
1832 || (i1dest
&& rtx_equal_p (i1dest
, inner_dest
)))
1833 && (reg_overlap_mentioned_p (i2dest
, inner_dest
)
1834 || (i1dest
&& reg_overlap_mentioned_p (i1dest
, inner_dest
))))
1836 /* This is the same test done in can_combine_p except we can't test
1837 all_adjacent; we don't have to, since this instruction will stay
1838 in place, thus we are not considering increasing the lifetime of
1841 Also, if this insn sets a function argument, combining it with
1842 something that might need a spill could clobber a previous
1843 function argument; the all_adjacent test in can_combine_p also
1844 checks this; here, we do a more specific test for this case. */
1846 || (REG_P (inner_dest
)
1847 && REGNO (inner_dest
) < FIRST_PSEUDO_REGISTER
1848 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest
),
1849 GET_MODE (inner_dest
))))
1850 || (i1_not_in_src
&& reg_overlap_mentioned_p (i1dest
, src
)))
1853 /* If DEST is used in I3, it is being killed in this insn, so
1854 record that for later. We have to consider paradoxical
1855 subregs here, since they kill the whole register, but we
1856 ignore partial subregs, STRICT_LOW_PART, etc.
1857 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1858 STACK_POINTER_REGNUM, since these are always considered to be
1859 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1861 if (GET_CODE (subdest
) == SUBREG
1862 && (GET_MODE_SIZE (GET_MODE (subdest
))
1863 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest
)))))
1864 subdest
= SUBREG_REG (subdest
);
1867 && reg_referenced_p (subdest
, PATTERN (i3
))
1868 && REGNO (subdest
) != FRAME_POINTER_REGNUM
1869 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1870 && REGNO (subdest
) != HARD_FRAME_POINTER_REGNUM
1872 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1873 && (REGNO (subdest
) != ARG_POINTER_REGNUM
1874 || ! fixed_regs
[REGNO (subdest
)])
1876 && REGNO (subdest
) != STACK_POINTER_REGNUM
)
1878 if (*pi3dest_killed
)
1881 *pi3dest_killed
= subdest
;
1885 else if (GET_CODE (x
) == PARALLEL
)
1889 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
1890 if (! combinable_i3pat (i3
, &XVECEXP (x
, 0, i
), i2dest
, i1dest
,
1891 i1_not_in_src
, pi3dest_killed
))
1898 /* Return 1 if X is an arithmetic expression that contains a multiplication
1899 and division. We don't count multiplications by powers of two here. */
1902 contains_muldiv (rtx x
)
1904 switch (GET_CODE (x
))
1906 case MOD
: case DIV
: case UMOD
: case UDIV
:
1910 return ! (GET_CODE (XEXP (x
, 1)) == CONST_INT
1911 && exact_log2 (INTVAL (XEXP (x
, 1))) >= 0);
1914 return contains_muldiv (XEXP (x
, 0))
1915 || contains_muldiv (XEXP (x
, 1));
1918 return contains_muldiv (XEXP (x
, 0));
1924 /* Determine whether INSN can be used in a combination. Return nonzero if
1925 not. This is used in try_combine to detect early some cases where we
1926 can't perform combinations. */
1929 cant_combine_insn_p (rtx insn
)
1934 /* If this isn't really an insn, we can't do anything.
1935 This can occur when flow deletes an insn that it has merged into an
1936 auto-increment address. */
1937 if (! INSN_P (insn
))
1940 /* Never combine loads and stores involving hard regs that are likely
1941 to be spilled. The register allocator can usually handle such
1942 reg-reg moves by tying. If we allow the combiner to make
1943 substitutions of likely-spilled regs, reload might die.
1944 As an exception, we allow combinations involving fixed regs; these are
1945 not available to the register allocator so there's no risk involved. */
1947 set
= single_set (insn
);
1950 src
= SET_SRC (set
);
1951 dest
= SET_DEST (set
);
1952 if (GET_CODE (src
) == SUBREG
)
1953 src
= SUBREG_REG (src
);
1954 if (GET_CODE (dest
) == SUBREG
)
1955 dest
= SUBREG_REG (dest
);
1956 if (REG_P (src
) && REG_P (dest
)
1957 && ((REGNO (src
) < FIRST_PSEUDO_REGISTER
1958 && ! fixed_regs
[REGNO (src
)]
1959 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src
))))
1960 || (REGNO (dest
) < FIRST_PSEUDO_REGISTER
1961 && ! fixed_regs
[REGNO (dest
)]
1962 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest
))))))
1968 struct likely_spilled_retval_info
1970 unsigned regno
, nregs
;
1974 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
1975 hard registers that are known to be written to / clobbered in full. */
1977 likely_spilled_retval_1 (rtx x
, rtx set
, void *data
)
1979 struct likely_spilled_retval_info
*info
= data
;
1980 unsigned regno
, nregs
;
1983 if (!REG_P (XEXP (set
, 0)))
1986 if (regno
>= info
->regno
+ info
->nregs
)
1988 nregs
= hard_regno_nregs
[regno
][GET_MODE (x
)];
1989 if (regno
+ nregs
<= info
->regno
)
1991 new_mask
= (2U << (nregs
- 1)) - 1;
1992 if (regno
< info
->regno
)
1993 new_mask
>>= info
->regno
- regno
;
1995 new_mask
<<= regno
- info
->regno
;
1996 info
->mask
&= ~new_mask
;
1999 /* Return nonzero iff part of the return value is live during INSN, and
2000 it is likely spilled. This can happen when more than one insn is needed
2001 to copy the return value, e.g. when we consider to combine into the
2002 second copy insn for a complex value. */
2005 likely_spilled_retval_p (rtx insn
)
2007 rtx use
= BB_END (this_basic_block
);
2009 unsigned regno
, nregs
;
2010 /* We assume here that no machine mode needs more than
2011 32 hard registers when the value overlaps with a register
2012 for which FUNCTION_VALUE_REGNO_P is true. */
2014 struct likely_spilled_retval_info info
;
2016 if (!NONJUMP_INSN_P (use
) || GET_CODE (PATTERN (use
)) != USE
|| insn
== use
)
2018 reg
= XEXP (PATTERN (use
), 0);
2019 if (!REG_P (reg
) || !FUNCTION_VALUE_REGNO_P (REGNO (reg
)))
2021 regno
= REGNO (reg
);
2022 nregs
= hard_regno_nregs
[regno
][GET_MODE (reg
)];
2025 mask
= (2U << (nregs
- 1)) - 1;
2027 /* Disregard parts of the return value that are set later. */
2031 for (p
= PREV_INSN (use
); info
.mask
&& p
!= insn
; p
= PREV_INSN (p
))
2033 note_stores (PATTERN (p
), likely_spilled_retval_1
, &info
);
2036 /* Check if any of the (probably) live return value registers is
2041 if ((mask
& 1 << nregs
)
2042 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno
+ nregs
)))
2048 /* Adjust INSN after we made a change to its destination.
2050 Changing the destination can invalidate notes that say something about
2051 the results of the insn and a LOG_LINK pointing to the insn. */
2054 adjust_for_new_dest (rtx insn
)
2056 /* For notes, be conservative and simply remove them. */
2057 remove_reg_equal_equiv_notes (insn
);
2059 /* The new insn will have a destination that was previously the destination
2060 of an insn just above it. Call distribute_links to make a LOG_LINK from
2061 the next use of that destination. */
2062 distribute_links (gen_rtx_INSN_LIST (VOIDmode
, insn
, NULL_RTX
));
2064 df_insn_rescan (insn
);
2067 /* Return TRUE if combine can reuse reg X in mode MODE.
2068 ADDED_SETS is nonzero if the original set is still required. */
2070 can_change_dest_mode (rtx x
, int added_sets
, enum machine_mode mode
)
2078 /* Allow hard registers if the new mode is legal, and occupies no more
2079 registers than the old mode. */
2080 if (regno
< FIRST_PSEUDO_REGISTER
)
2081 return (HARD_REGNO_MODE_OK (regno
, mode
)
2082 && (hard_regno_nregs
[regno
][GET_MODE (x
)]
2083 >= hard_regno_nregs
[regno
][mode
]));
2085 /* Or a pseudo that is only used once. */
2086 return (REG_N_SETS (regno
) == 1 && !added_sets
2087 && !REG_USERVAR_P (x
));
2091 /* Check whether X, the destination of a set, refers to part of
2092 the register specified by REG. */
2095 reg_subword_p (rtx x
, rtx reg
)
2097 /* Check that reg is an integer mode register. */
2098 if (!REG_P (reg
) || GET_MODE_CLASS (GET_MODE (reg
)) != MODE_INT
)
2101 if (GET_CODE (x
) == STRICT_LOW_PART
2102 || GET_CODE (x
) == ZERO_EXTRACT
)
2105 return GET_CODE (x
) == SUBREG
2106 && SUBREG_REG (x
) == reg
2107 && GET_MODE_CLASS (GET_MODE (x
)) == MODE_INT
;
2111 /* Try to combine the insns I1 and I2 into I3.
2112 Here I1 and I2 appear earlier than I3.
2113 I1 can be zero; then we combine just I2 into I3.
2115 If we are combining three insns and the resulting insn is not recognized,
2116 try splitting it into two insns. If that happens, I2 and I3 are retained
2117 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
2120 Return 0 if the combination does not work. Then nothing is changed.
2121 If we did the combination, return the insn at which combine should
2124 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2125 new direct jump instruction. */
2128 try_combine (rtx i3
, rtx i2
, rtx i1
, int *new_direct_jump_p
)
2130 /* New patterns for I3 and I2, respectively. */
2131 rtx newpat
, newi2pat
= 0;
2132 rtvec newpat_vec_with_clobbers
= 0;
2133 int substed_i2
= 0, substed_i1
= 0;
2134 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
2135 int added_sets_1
, added_sets_2
;
2136 /* Total number of SETs to put into I3. */
2138 /* Nonzero if I2's body now appears in I3. */
2140 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2141 int insn_code_number
, i2_code_number
= 0, other_code_number
= 0;
2142 /* Contains I3 if the destination of I3 is used in its source, which means
2143 that the old life of I3 is being killed. If that usage is placed into
2144 I2 and not in I3, a REG_DEAD note must be made. */
2145 rtx i3dest_killed
= 0;
2146 /* SET_DEST and SET_SRC of I2 and I1. */
2147 rtx i2dest
, i2src
, i1dest
= 0, i1src
= 0;
2148 /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases. */
2149 rtx i1pat
= 0, i2pat
= 0;
2150 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2151 int i2dest_in_i2src
= 0, i1dest_in_i1src
= 0, i2dest_in_i1src
= 0;
2152 int i2dest_killed
= 0, i1dest_killed
= 0;
2153 int i1_feeds_i3
= 0;
2154 /* Notes that must be added to REG_NOTES in I3 and I2. */
2155 rtx new_i3_notes
, new_i2_notes
;
2156 /* Notes that we substituted I3 into I2 instead of the normal case. */
2157 int i3_subst_into_i2
= 0;
2158 /* Notes that I1, I2 or I3 is a MULT operation. */
2166 rtx new_other_notes
;
2169 /* Exit early if one of the insns involved can't be used for
2171 if (cant_combine_insn_p (i3
)
2172 || cant_combine_insn_p (i2
)
2173 || (i1
&& cant_combine_insn_p (i1
))
2174 || likely_spilled_retval_p (i3
)
2175 /* We also can't do anything if I3 has a
2176 REG_LIBCALL note since we don't want to disrupt the contiguity of a
2179 /* ??? This gives worse code, and appears to be unnecessary, since no
2180 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
2181 || find_reg_note (i3
, REG_LIBCALL
, NULL_RTX
)
2187 undobuf
.other_insn
= 0;
2189 /* Reset the hard register usage information. */
2190 CLEAR_HARD_REG_SET (newpat_used_regs
);
2192 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
2193 code below, set I1 to be the earlier of the two insns. */
2194 if (i1
&& DF_INSN_LUID (i1
) > DF_INSN_LUID (i2
))
2195 temp
= i1
, i1
= i2
, i2
= temp
;
2197 added_links_insn
= 0;
2199 /* First check for one important special-case that the code below will
2200 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2201 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2202 we may be able to replace that destination with the destination of I3.
2203 This occurs in the common code where we compute both a quotient and
2204 remainder into a structure, in which case we want to do the computation
2205 directly into the structure to avoid register-register copies.
2207 Note that this case handles both multiple sets in I2 and also
2208 cases where I2 has a number of CLOBBER or PARALLELs.
2210 We make very conservative checks below and only try to handle the
2211 most common cases of this. For example, we only handle the case
2212 where I2 and I3 are adjacent to avoid making difficult register
2215 if (i1
== 0 && NONJUMP_INSN_P (i3
) && GET_CODE (PATTERN (i3
)) == SET
2216 && REG_P (SET_SRC (PATTERN (i3
)))
2217 && REGNO (SET_SRC (PATTERN (i3
))) >= FIRST_PSEUDO_REGISTER
2218 && find_reg_note (i3
, REG_DEAD
, SET_SRC (PATTERN (i3
)))
2219 && GET_CODE (PATTERN (i2
)) == PARALLEL
2220 && ! side_effects_p (SET_DEST (PATTERN (i3
)))
2221 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2222 below would need to check what is inside (and reg_overlap_mentioned_p
2223 doesn't support those codes anyway). Don't allow those destinations;
2224 the resulting insn isn't likely to be recognized anyway. */
2225 && GET_CODE (SET_DEST (PATTERN (i3
))) != ZERO_EXTRACT
2226 && GET_CODE (SET_DEST (PATTERN (i3
))) != STRICT_LOW_PART
2227 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3
)),
2228 SET_DEST (PATTERN (i3
)))
2229 && next_real_insn (i2
) == i3
)
2231 rtx p2
= PATTERN (i2
);
2233 /* Make sure that the destination of I3,
2234 which we are going to substitute into one output of I2,
2235 is not used within another output of I2. We must avoid making this:
2236 (parallel [(set (mem (reg 69)) ...)
2237 (set (reg 69) ...)])
2238 which is not well-defined as to order of actions.
2239 (Besides, reload can't handle output reloads for this.)
2241 The problem can also happen if the dest of I3 is a memory ref,
2242 if another dest in I2 is an indirect memory ref. */
2243 for (i
= 0; i
< XVECLEN (p2
, 0); i
++)
2244 if ((GET_CODE (XVECEXP (p2
, 0, i
)) == SET
2245 || GET_CODE (XVECEXP (p2
, 0, i
)) == CLOBBER
)
2246 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3
)),
2247 SET_DEST (XVECEXP (p2
, 0, i
))))
2250 if (i
== XVECLEN (p2
, 0))
2251 for (i
= 0; i
< XVECLEN (p2
, 0); i
++)
2252 if ((GET_CODE (XVECEXP (p2
, 0, i
)) == SET
2253 || GET_CODE (XVECEXP (p2
, 0, i
)) == CLOBBER
)
2254 && SET_DEST (XVECEXP (p2
, 0, i
)) == SET_SRC (PATTERN (i3
)))
2259 subst_low_luid
= DF_INSN_LUID (i2
);
2261 added_sets_2
= added_sets_1
= 0;
2262 i2dest
= SET_SRC (PATTERN (i3
));
2263 i2dest_killed
= dead_or_set_p (i2
, i2dest
);
2265 /* Replace the dest in I2 with our dest and make the resulting
2266 insn the new pattern for I3. Then skip to where we
2267 validate the pattern. Everything was set up above. */
2268 SUBST (SET_DEST (XVECEXP (p2
, 0, i
)),
2269 SET_DEST (PATTERN (i3
)));
2272 i3_subst_into_i2
= 1;
2273 goto validate_replacement
;
2277 /* If I2 is setting a pseudo to a constant and I3 is setting some
2278 sub-part of it to another constant, merge them by making a new
2281 && (temp
= single_set (i2
)) != 0
2282 && (GET_CODE (SET_SRC (temp
)) == CONST_INT
2283 || GET_CODE (SET_SRC (temp
)) == CONST_DOUBLE
)
2284 && GET_CODE (PATTERN (i3
)) == SET
2285 && (GET_CODE (SET_SRC (PATTERN (i3
))) == CONST_INT
2286 || GET_CODE (SET_SRC (PATTERN (i3
))) == CONST_DOUBLE
)
2287 && reg_subword_p (SET_DEST (PATTERN (i3
)), SET_DEST (temp
)))
2289 rtx dest
= SET_DEST (PATTERN (i3
));
2293 if (GET_CODE (dest
) == ZERO_EXTRACT
)
2295 if (GET_CODE (XEXP (dest
, 1)) == CONST_INT
2296 && GET_CODE (XEXP (dest
, 2)) == CONST_INT
)
2298 width
= INTVAL (XEXP (dest
, 1));
2299 offset
= INTVAL (XEXP (dest
, 2));
2300 dest
= XEXP (dest
, 0);
2301 if (BITS_BIG_ENDIAN
)
2302 offset
= GET_MODE_BITSIZE (GET_MODE (dest
)) - width
- offset
;
2307 if (GET_CODE (dest
) == STRICT_LOW_PART
)
2308 dest
= XEXP (dest
, 0);
2309 width
= GET_MODE_BITSIZE (GET_MODE (dest
));
2315 /* If this is the low part, we're done. */
2316 if (subreg_lowpart_p (dest
))
2318 /* Handle the case where inner is twice the size of outer. */
2319 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp
)))
2320 == 2 * GET_MODE_BITSIZE (GET_MODE (dest
)))
2321 offset
+= GET_MODE_BITSIZE (GET_MODE (dest
));
2322 /* Otherwise give up for now. */
2328 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp
)))
2329 <= HOST_BITS_PER_WIDE_INT
* 2))
2331 HOST_WIDE_INT mhi
, ohi
, ihi
;
2332 HOST_WIDE_INT mlo
, olo
, ilo
;
2333 rtx inner
= SET_SRC (PATTERN (i3
));
2334 rtx outer
= SET_SRC (temp
);
2336 if (GET_CODE (outer
) == CONST_INT
)
2338 olo
= INTVAL (outer
);
2339 ohi
= olo
< 0 ? -1 : 0;
2343 olo
= CONST_DOUBLE_LOW (outer
);
2344 ohi
= CONST_DOUBLE_HIGH (outer
);
2347 if (GET_CODE (inner
) == CONST_INT
)
2349 ilo
= INTVAL (inner
);
2350 ihi
= ilo
< 0 ? -1 : 0;
2354 ilo
= CONST_DOUBLE_LOW (inner
);
2355 ihi
= CONST_DOUBLE_HIGH (inner
);
2358 if (width
< HOST_BITS_PER_WIDE_INT
)
2360 mlo
= ((unsigned HOST_WIDE_INT
) 1 << width
) - 1;
2363 else if (width
< HOST_BITS_PER_WIDE_INT
* 2)
2365 mhi
= ((unsigned HOST_WIDE_INT
) 1
2366 << (width
- HOST_BITS_PER_WIDE_INT
)) - 1;
2378 if (offset
>= HOST_BITS_PER_WIDE_INT
)
2380 mhi
= mlo
<< (offset
- HOST_BITS_PER_WIDE_INT
);
2382 ihi
= ilo
<< (offset
- HOST_BITS_PER_WIDE_INT
);
2385 else if (offset
> 0)
2387 mhi
= (mhi
<< offset
) | ((unsigned HOST_WIDE_INT
) mlo
2388 >> (HOST_BITS_PER_WIDE_INT
- offset
));
2389 mlo
= mlo
<< offset
;
2390 ihi
= (ihi
<< offset
) | ((unsigned HOST_WIDE_INT
) ilo
2391 >> (HOST_BITS_PER_WIDE_INT
- offset
));
2392 ilo
= ilo
<< offset
;
2395 olo
= (olo
& ~mlo
) | ilo
;
2396 ohi
= (ohi
& ~mhi
) | ihi
;
2400 subst_low_luid
= DF_INSN_LUID (i2
);
2401 added_sets_2
= added_sets_1
= 0;
2402 i2dest
= SET_DEST (temp
);
2403 i2dest_killed
= dead_or_set_p (i2
, i2dest
);
2405 SUBST (SET_SRC (temp
),
2406 immed_double_const (olo
, ohi
, GET_MODE (SET_DEST (temp
))));
2408 newpat
= PATTERN (i2
);
2409 goto validate_replacement
;
2414 /* If we have no I1 and I2 looks like:
2415 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2417 make up a dummy I1 that is
2420 (set (reg:CC X) (compare:CC Y (const_int 0)))
2422 (We can ignore any trailing CLOBBERs.)
2424 This undoes a previous combination and allows us to match a branch-and-
2427 if (i1
== 0 && GET_CODE (PATTERN (i2
)) == PARALLEL
2428 && XVECLEN (PATTERN (i2
), 0) >= 2
2429 && GET_CODE (XVECEXP (PATTERN (i2
), 0, 0)) == SET
2430 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2
), 0, 0))))
2432 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2
), 0, 0))) == COMPARE
2433 && XEXP (SET_SRC (XVECEXP (PATTERN (i2
), 0, 0)), 1) == const0_rtx
2434 && GET_CODE (XVECEXP (PATTERN (i2
), 0, 1)) == SET
2435 && REG_P (SET_DEST (XVECEXP (PATTERN (i2
), 0, 1)))
2436 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2
), 0, 0)), 0),
2437 SET_SRC (XVECEXP (PATTERN (i2
), 0, 1))))
2439 for (i
= XVECLEN (PATTERN (i2
), 0) - 1; i
>= 2; i
--)
2440 if (GET_CODE (XVECEXP (PATTERN (i2
), 0, i
)) != CLOBBER
)
2445 /* We make I1 with the same INSN_UID as I2. This gives it
2446 the same DF_INSN_LUID for value tracking. Our fake I1 will
2447 never appear in the insn stream so giving it the same INSN_UID
2448 as I2 will not cause a problem. */
2450 i1
= gen_rtx_INSN (VOIDmode
, INSN_UID (i2
), NULL_RTX
, i2
,
2451 BLOCK_FOR_INSN (i2
), INSN_LOCATOR (i2
),
2452 XVECEXP (PATTERN (i2
), 0, 1), -1, NULL_RTX
);
2454 SUBST (PATTERN (i2
), XVECEXP (PATTERN (i2
), 0, 0));
2455 SUBST (XEXP (SET_SRC (PATTERN (i2
)), 0),
2456 SET_DEST (PATTERN (i1
)));
2461 /* Verify that I2 and I1 are valid for combining. */
2462 if (! can_combine_p (i2
, i3
, i1
, NULL_RTX
, &i2dest
, &i2src
)
2463 || (i1
&& ! can_combine_p (i1
, i3
, NULL_RTX
, i2
, &i1dest
, &i1src
)))
2469 /* Record whether I2DEST is used in I2SRC and similarly for the other
2470 cases. Knowing this will help in register status updating below. */
2471 i2dest_in_i2src
= reg_overlap_mentioned_p (i2dest
, i2src
);
2472 i1dest_in_i1src
= i1
&& reg_overlap_mentioned_p (i1dest
, i1src
);
2473 i2dest_in_i1src
= i1
&& reg_overlap_mentioned_p (i2dest
, i1src
);
2474 i2dest_killed
= dead_or_set_p (i2
, i2dest
);
2475 i1dest_killed
= i1
&& dead_or_set_p (i1
, i1dest
);
2477 /* See if I1 directly feeds into I3. It does if I1DEST is not used
2479 i1_feeds_i3
= i1
&& ! reg_overlap_mentioned_p (i1dest
, i2src
);
2481 /* Ensure that I3's pattern can be the destination of combines. */
2482 if (! combinable_i3pat (i3
, &PATTERN (i3
), i2dest
, i1dest
,
2483 i1
&& i2dest_in_i1src
&& i1_feeds_i3
,
2490 /* See if any of the insns is a MULT operation. Unless one is, we will
2491 reject a combination that is, since it must be slower. Be conservative
2493 if (GET_CODE (i2src
) == MULT
2494 || (i1
!= 0 && GET_CODE (i1src
) == MULT
)
2495 || (GET_CODE (PATTERN (i3
)) == SET
2496 && GET_CODE (SET_SRC (PATTERN (i3
))) == MULT
))
2499 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2500 We used to do this EXCEPT in one case: I3 has a post-inc in an
2501 output operand. However, that exception can give rise to insns like
2503 which is a famous insn on the PDP-11 where the value of r3 used as the
2504 source was model-dependent. Avoid this sort of thing. */
2507 if (!(GET_CODE (PATTERN (i3
)) == SET
2508 && REG_P (SET_SRC (PATTERN (i3
)))
2509 && MEM_P (SET_DEST (PATTERN (i3
)))
2510 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3
)), 0)) == POST_INC
2511 || GET_CODE (XEXP (SET_DEST (PATTERN (i3
)), 0)) == POST_DEC
)))
2512 /* It's not the exception. */
2515 for (link
= REG_NOTES (i3
); link
; link
= XEXP (link
, 1))
2516 if (REG_NOTE_KIND (link
) == REG_INC
2517 && (reg_overlap_mentioned_p (XEXP (link
, 0), PATTERN (i2
))
2519 && reg_overlap_mentioned_p (XEXP (link
, 0), PATTERN (i1
)))))
2526 /* See if the SETs in I1 or I2 need to be kept around in the merged
2527 instruction: whenever the value set there is still needed past I3.
2528 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2530 For the SET in I1, we have two cases: If I1 and I2 independently
2531 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2532 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2533 in I1 needs to be kept around unless I1DEST dies or is set in either
2534 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2535 I1DEST. If so, we know I1 feeds into I2. */
2537 added_sets_2
= ! dead_or_set_p (i3
, i2dest
);
2540 = i1
&& ! (i1_feeds_i3
? dead_or_set_p (i3
, i1dest
)
2541 : (dead_or_set_p (i3
, i1dest
) || dead_or_set_p (i2
, i1dest
)));
2543 /* If the set in I2 needs to be kept around, we must make a copy of
2544 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2545 PATTERN (I2), we are only substituting for the original I1DEST, not into
2546 an already-substituted copy. This also prevents making self-referential
2547 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2552 if (GET_CODE (PATTERN (i2
)) == PARALLEL
)
2553 i2pat
= gen_rtx_SET (VOIDmode
, i2dest
, copy_rtx (i2src
));
2555 i2pat
= copy_rtx (PATTERN (i2
));
2560 if (GET_CODE (PATTERN (i1
)) == PARALLEL
)
2561 i1pat
= gen_rtx_SET (VOIDmode
, i1dest
, copy_rtx (i1src
));
2563 i1pat
= copy_rtx (PATTERN (i1
));
2568 /* Substitute in the latest insn for the regs set by the earlier ones. */
2570 maxreg
= max_reg_num ();
2575 /* Many machines that don't use CC0 have insns that can both perform an
2576 arithmetic operation and set the condition code. These operations will
2577 be represented as a PARALLEL with the first element of the vector
2578 being a COMPARE of an arithmetic operation with the constant zero.
2579 The second element of the vector will set some pseudo to the result
2580 of the same arithmetic operation. If we simplify the COMPARE, we won't
2581 match such a pattern and so will generate an extra insn. Here we test
2582 for this case, where both the comparison and the operation result are
2583 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2584 I2SRC. Later we will make the PARALLEL that contains I2. */
2586 if (i1
== 0 && added_sets_2
&& GET_CODE (PATTERN (i3
)) == SET
2587 && GET_CODE (SET_SRC (PATTERN (i3
))) == COMPARE
2588 && XEXP (SET_SRC (PATTERN (i3
)), 1) == const0_rtx
2589 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3
)), 0), i2dest
))
2591 #ifdef SELECT_CC_MODE
2593 enum machine_mode compare_mode
;
2596 newpat
= PATTERN (i3
);
2597 SUBST (XEXP (SET_SRC (newpat
), 0), i2src
);
2601 #ifdef SELECT_CC_MODE
2602 /* See if a COMPARE with the operand we substituted in should be done
2603 with the mode that is currently being used. If not, do the same
2604 processing we do in `subst' for a SET; namely, if the destination
2605 is used only once, try to replace it with a register of the proper
2606 mode and also replace the COMPARE. */
2607 if (undobuf
.other_insn
== 0
2608 && (cc_use
= find_single_use (SET_DEST (newpat
), i3
,
2609 &undobuf
.other_insn
))
2610 && ((compare_mode
= SELECT_CC_MODE (GET_CODE (*cc_use
),
2612 != GET_MODE (SET_DEST (newpat
))))
2614 if (can_change_dest_mode(SET_DEST (newpat
), added_sets_2
,
2617 unsigned int regno
= REGNO (SET_DEST (newpat
));
2620 if (regno
< FIRST_PSEUDO_REGISTER
)
2621 new_dest
= gen_rtx_REG (compare_mode
, regno
);
2624 SUBST_MODE (regno_reg_rtx
[regno
], compare_mode
);
2625 new_dest
= regno_reg_rtx
[regno
];
2628 SUBST (SET_DEST (newpat
), new_dest
);
2629 SUBST (XEXP (*cc_use
, 0), new_dest
);
2630 SUBST (SET_SRC (newpat
),
2631 gen_rtx_COMPARE (compare_mode
, i2src
, const0_rtx
));
2634 undobuf
.other_insn
= 0;
2641 /* It is possible that the source of I2 or I1 may be performing
2642 an unneeded operation, such as a ZERO_EXTEND of something
2643 that is known to have the high part zero. Handle that case
2644 by letting subst look at the innermost one of them.
2646 Another way to do this would be to have a function that tries
2647 to simplify a single insn instead of merging two or more
2648 insns. We don't do this because of the potential of infinite
2649 loops and because of the potential extra memory required.
2650 However, doing it the way we are is a bit of a kludge and
2651 doesn't catch all cases.
2653 But only do this if -fexpensive-optimizations since it slows
2654 things down and doesn't usually win.
2656 This is not done in the COMPARE case above because the
2657 unmodified I2PAT is used in the PARALLEL and so a pattern
2658 with a modified I2SRC would not match. */
2660 if (flag_expensive_optimizations
)
2662 /* Pass pc_rtx so no substitutions are done, just
2666 subst_low_luid
= DF_INSN_LUID (i1
);
2667 i1src
= subst (i1src
, pc_rtx
, pc_rtx
, 0, 0);
2671 subst_low_luid
= DF_INSN_LUID (i2
);
2672 i2src
= subst (i2src
, pc_rtx
, pc_rtx
, 0, 0);
2676 n_occurrences
= 0; /* `subst' counts here */
2678 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2679 need to make a unique copy of I2SRC each time we substitute it
2680 to avoid self-referential rtl. */
2682 subst_low_luid
= DF_INSN_LUID (i2
);
2683 newpat
= subst (PATTERN (i3
), i2dest
, i2src
, 0,
2684 ! i1_feeds_i3
&& i1dest_in_i1src
);
2687 /* Record whether i2's body now appears within i3's body. */
2688 i2_is_used
= n_occurrences
;
2691 /* If we already got a failure, don't try to do more. Otherwise,
2692 try to substitute in I1 if we have it. */
2694 if (i1
&& GET_CODE (newpat
) != CLOBBER
)
2696 /* Before we can do this substitution, we must redo the test done
2697 above (see detailed comments there) that ensures that I1DEST
2698 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2700 if (! combinable_i3pat (NULL_RTX
, &newpat
, i1dest
, NULL_RTX
,
2708 subst_low_luid
= DF_INSN_LUID (i1
);
2709 newpat
= subst (newpat
, i1dest
, i1src
, 0, 0);
2713 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2714 to count all the ways that I2SRC and I1SRC can be used. */
2715 if ((FIND_REG_INC_NOTE (i2
, NULL_RTX
) != 0
2716 && i2_is_used
+ added_sets_2
> 1)
2717 || (i1
!= 0 && FIND_REG_INC_NOTE (i1
, NULL_RTX
) != 0
2718 && (n_occurrences
+ added_sets_1
+ (added_sets_2
&& ! i1_feeds_i3
)
2720 /* Fail if we tried to make a new register. */
2721 || max_reg_num () != maxreg
2722 /* Fail if we couldn't do something and have a CLOBBER. */
2723 || GET_CODE (newpat
) == CLOBBER
2724 /* Fail if this new pattern is a MULT and we didn't have one before
2725 at the outer level. */
2726 || (GET_CODE (newpat
) == SET
&& GET_CODE (SET_SRC (newpat
)) == MULT
2733 /* If the actions of the earlier insns must be kept
2734 in addition to substituting them into the latest one,
2735 we must make a new PARALLEL for the latest insn
2736 to hold additional the SETs. */
2738 if (added_sets_1
|| added_sets_2
)
2742 if (GET_CODE (newpat
) == PARALLEL
)
2744 rtvec old
= XVEC (newpat
, 0);
2745 total_sets
= XVECLEN (newpat
, 0) + added_sets_1
+ added_sets_2
;
2746 newpat
= gen_rtx_PARALLEL (VOIDmode
, rtvec_alloc (total_sets
));
2747 memcpy (XVEC (newpat
, 0)->elem
, &old
->elem
[0],
2748 sizeof (old
->elem
[0]) * old
->num_elem
);
2753 total_sets
= 1 + added_sets_1
+ added_sets_2
;
2754 newpat
= gen_rtx_PARALLEL (VOIDmode
, rtvec_alloc (total_sets
));
2755 XVECEXP (newpat
, 0, 0) = old
;
2759 XVECEXP (newpat
, 0, --total_sets
) = i1pat
;
2763 /* If there is no I1, use I2's body as is. We used to also not do
2764 the subst call below if I2 was substituted into I3,
2765 but that could lose a simplification. */
2767 XVECEXP (newpat
, 0, --total_sets
) = i2pat
;
2769 /* See comment where i2pat is assigned. */
2770 XVECEXP (newpat
, 0, --total_sets
)
2771 = subst (i2pat
, i1dest
, i1src
, 0, 0);
2775 /* We come here when we are replacing a destination in I2 with the
2776 destination of I3. */
2777 validate_replacement
:
2779 /* Note which hard regs this insn has as inputs. */
2780 mark_used_regs_combine (newpat
);
2782 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2783 consider splitting this pattern, we might need these clobbers. */
2784 if (i1
&& GET_CODE (newpat
) == PARALLEL
2785 && GET_CODE (XVECEXP (newpat
, 0, XVECLEN (newpat
, 0) - 1)) == CLOBBER
)
2787 int len
= XVECLEN (newpat
, 0);
2789 newpat_vec_with_clobbers
= rtvec_alloc (len
);
2790 for (i
= 0; i
< len
; i
++)
2791 RTVEC_ELT (newpat_vec_with_clobbers
, i
) = XVECEXP (newpat
, 0, i
);
2794 /* Is the result of combination a valid instruction? */
2795 insn_code_number
= recog_for_combine (&newpat
, i3
, &new_i3_notes
);
2797 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2798 the second SET's destination is a register that is unused and isn't
2799 marked as an instruction that might trap in an EH region. In that case,
2800 we just need the first SET. This can occur when simplifying a divmod
2801 insn. We *must* test for this case here because the code below that
2802 splits two independent SETs doesn't handle this case correctly when it
2803 updates the register status.
2805 It's pointless doing this if we originally had two sets, one from
2806 i3, and one from i2. Combining then splitting the parallel results
2807 in the original i2 again plus an invalid insn (which we delete).
2808 The net effect is only to move instructions around, which makes
2809 debug info less accurate.
2811 Also check the case where the first SET's destination is unused.
2812 That would not cause incorrect code, but does cause an unneeded
2815 if (insn_code_number
< 0
2816 && !(added_sets_2
&& i1
== 0)
2817 && GET_CODE (newpat
) == PARALLEL
2818 && XVECLEN (newpat
, 0) == 2
2819 && GET_CODE (XVECEXP (newpat
, 0, 0)) == SET
2820 && GET_CODE (XVECEXP (newpat
, 0, 1)) == SET
2821 && asm_noperands (newpat
) < 0)
2823 rtx set0
= XVECEXP (newpat
, 0, 0);
2824 rtx set1
= XVECEXP (newpat
, 0, 1);
2827 if (((REG_P (SET_DEST (set1
))
2828 && find_reg_note (i3
, REG_UNUSED
, SET_DEST (set1
)))
2829 || (GET_CODE (SET_DEST (set1
)) == SUBREG
2830 && find_reg_note (i3
, REG_UNUSED
, SUBREG_REG (SET_DEST (set1
)))))
2831 && (!(note
= find_reg_note (i3
, REG_EH_REGION
, NULL_RTX
))
2832 || INTVAL (XEXP (note
, 0)) <= 0)
2833 && ! side_effects_p (SET_SRC (set1
)))
2836 insn_code_number
= recog_for_combine (&newpat
, i3
, &new_i3_notes
);
2839 else if (((REG_P (SET_DEST (set0
))
2840 && find_reg_note (i3
, REG_UNUSED
, SET_DEST (set0
)))
2841 || (GET_CODE (SET_DEST (set0
)) == SUBREG
2842 && find_reg_note (i3
, REG_UNUSED
,
2843 SUBREG_REG (SET_DEST (set0
)))))
2844 && (!(note
= find_reg_note (i3
, REG_EH_REGION
, NULL_RTX
))
2845 || INTVAL (XEXP (note
, 0)) <= 0)
2846 && ! side_effects_p (SET_SRC (set0
)))
2849 insn_code_number
= recog_for_combine (&newpat
, i3
, &new_i3_notes
);
2851 if (insn_code_number
>= 0)
2853 /* If we will be able to accept this, we have made a
2854 change to the destination of I3. This requires us to
2855 do a few adjustments. */
2857 PATTERN (i3
) = newpat
;
2858 adjust_for_new_dest (i3
);
2863 /* If we were combining three insns and the result is a simple SET
2864 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2865 insns. There are two ways to do this. It can be split using a
2866 machine-specific method (like when you have an addition of a large
2867 constant) or by combine in the function find_split_point. */
2869 if (i1
&& insn_code_number
< 0 && GET_CODE (newpat
) == SET
2870 && asm_noperands (newpat
) < 0)
2872 rtx m_split
, *split
;
2874 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2875 use I2DEST as a scratch register will help. In the latter case,
2876 convert I2DEST to the mode of the source of NEWPAT if we can. */
2878 m_split
= split_insns (newpat
, i3
);
2880 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2881 inputs of NEWPAT. */
2883 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2884 possible to try that as a scratch reg. This would require adding
2885 more code to make it work though. */
2887 if (m_split
== 0 && ! reg_overlap_mentioned_p (i2dest
, newpat
))
2889 enum machine_mode new_mode
= GET_MODE (SET_DEST (newpat
));
2891 /* First try to split using the original register as a
2892 scratch register. */
2893 m_split
= split_insns (gen_rtx_PARALLEL
2895 gen_rtvec (2, newpat
,
2896 gen_rtx_CLOBBER (VOIDmode
,
2900 /* If that didn't work, try changing the mode of I2DEST if
2903 && new_mode
!= GET_MODE (i2dest
)
2904 && new_mode
!= VOIDmode
2905 && can_change_dest_mode (i2dest
, added_sets_2
, new_mode
))
2907 enum machine_mode old_mode
= GET_MODE (i2dest
);
2910 if (REGNO (i2dest
) < FIRST_PSEUDO_REGISTER
)
2911 ni2dest
= gen_rtx_REG (new_mode
, REGNO (i2dest
));
2914 SUBST_MODE (regno_reg_rtx
[REGNO (i2dest
)], new_mode
);
2915 ni2dest
= regno_reg_rtx
[REGNO (i2dest
)];
2918 m_split
= split_insns (gen_rtx_PARALLEL
2920 gen_rtvec (2, newpat
,
2921 gen_rtx_CLOBBER (VOIDmode
,
2926 && REGNO (i2dest
) >= FIRST_PSEUDO_REGISTER
)
2930 PUT_MODE (regno_reg_rtx
[REGNO (i2dest
)], old_mode
);
2931 buf
= undobuf
.undos
;
2932 undobuf
.undos
= buf
->next
;
2933 buf
->next
= undobuf
.frees
;
2934 undobuf
.frees
= buf
;
2939 /* If recog_for_combine has discarded clobbers, try to use them
2940 again for the split. */
2941 if (m_split
== 0 && newpat_vec_with_clobbers
)
2943 = split_insns (gen_rtx_PARALLEL (VOIDmode
,
2944 newpat_vec_with_clobbers
), i3
);
2946 if (m_split
&& NEXT_INSN (m_split
) == NULL_RTX
)
2948 m_split
= PATTERN (m_split
);
2949 insn_code_number
= recog_for_combine (&m_split
, i3
, &new_i3_notes
);
2950 if (insn_code_number
>= 0)
2953 else if (m_split
&& NEXT_INSN (NEXT_INSN (m_split
)) == NULL_RTX
2954 && (next_real_insn (i2
) == i3
2955 || ! use_crosses_set_p (PATTERN (m_split
), DF_INSN_LUID (i2
))))
2958 rtx newi3pat
= PATTERN (NEXT_INSN (m_split
));
2959 newi2pat
= PATTERN (m_split
);
2961 i3set
= single_set (NEXT_INSN (m_split
));
2962 i2set
= single_set (m_split
);
2964 i2_code_number
= recog_for_combine (&newi2pat
, i2
, &new_i2_notes
);
2966 /* If I2 or I3 has multiple SETs, we won't know how to track
2967 register status, so don't use these insns. If I2's destination
2968 is used between I2 and I3, we also can't use these insns. */
2970 if (i2_code_number
>= 0 && i2set
&& i3set
2971 && (next_real_insn (i2
) == i3
2972 || ! reg_used_between_p (SET_DEST (i2set
), i2
, i3
)))
2973 insn_code_number
= recog_for_combine (&newi3pat
, i3
,
2975 if (insn_code_number
>= 0)
2978 /* It is possible that both insns now set the destination of I3.
2979 If so, we must show an extra use of it. */
2981 if (insn_code_number
>= 0)
2983 rtx new_i3_dest
= SET_DEST (i3set
);
2984 rtx new_i2_dest
= SET_DEST (i2set
);
2986 while (GET_CODE (new_i3_dest
) == ZERO_EXTRACT
2987 || GET_CODE (new_i3_dest
) == STRICT_LOW_PART
2988 || GET_CODE (new_i3_dest
) == SUBREG
)
2989 new_i3_dest
= XEXP (new_i3_dest
, 0);
2991 while (GET_CODE (new_i2_dest
) == ZERO_EXTRACT
2992 || GET_CODE (new_i2_dest
) == STRICT_LOW_PART
2993 || GET_CODE (new_i2_dest
) == SUBREG
)
2994 new_i2_dest
= XEXP (new_i2_dest
, 0);
2996 if (REG_P (new_i3_dest
)
2997 && REG_P (new_i2_dest
)
2998 && REGNO (new_i3_dest
) == REGNO (new_i2_dest
))
2999 INC_REG_N_SETS (REGNO (new_i2_dest
), 1);
3003 /* If we can split it and use I2DEST, go ahead and see if that
3004 helps things be recognized. Verify that none of the registers
3005 are set between I2 and I3. */
3006 if (insn_code_number
< 0 && (split
= find_split_point (&newpat
, i3
)) != 0
3010 /* We need I2DEST in the proper mode. If it is a hard register
3011 or the only use of a pseudo, we can change its mode.
3012 Make sure we don't change a hard register to have a mode that
3013 isn't valid for it, or change the number of registers. */
3014 && (GET_MODE (*split
) == GET_MODE (i2dest
)
3015 || GET_MODE (*split
) == VOIDmode
3016 || can_change_dest_mode (i2dest
, added_sets_2
,
3018 && (next_real_insn (i2
) == i3
3019 || ! use_crosses_set_p (*split
, DF_INSN_LUID (i2
)))
3020 /* We can't overwrite I2DEST if its value is still used by
3022 && ! reg_referenced_p (i2dest
, newpat
))
3024 rtx newdest
= i2dest
;
3025 enum rtx_code split_code
= GET_CODE (*split
);
3026 enum machine_mode split_mode
= GET_MODE (*split
);
3027 bool subst_done
= false;
3028 newi2pat
= NULL_RTX
;
3030 /* Get NEWDEST as a register in the proper mode. We have already
3031 validated that we can do this. */
3032 if (GET_MODE (i2dest
) != split_mode
&& split_mode
!= VOIDmode
)
3034 if (REGNO (i2dest
) < FIRST_PSEUDO_REGISTER
)
3035 newdest
= gen_rtx_REG (split_mode
, REGNO (i2dest
));
3038 SUBST_MODE (regno_reg_rtx
[REGNO (i2dest
)], split_mode
);
3039 newdest
= regno_reg_rtx
[REGNO (i2dest
)];
3043 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3044 an ASHIFT. This can occur if it was inside a PLUS and hence
3045 appeared to be a memory address. This is a kludge. */
3046 if (split_code
== MULT
3047 && GET_CODE (XEXP (*split
, 1)) == CONST_INT
3048 && INTVAL (XEXP (*split
, 1)) > 0
3049 && (i
= exact_log2 (INTVAL (XEXP (*split
, 1)))) >= 0)
3051 SUBST (*split
, gen_rtx_ASHIFT (split_mode
,
3052 XEXP (*split
, 0), GEN_INT (i
)));
3053 /* Update split_code because we may not have a multiply
3055 split_code
= GET_CODE (*split
);
3058 #ifdef INSN_SCHEDULING
3059 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3060 be written as a ZERO_EXTEND. */
3061 if (split_code
== SUBREG
&& MEM_P (SUBREG_REG (*split
)))
3063 #ifdef LOAD_EXTEND_OP
3064 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3065 what it really is. */
3066 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split
)))
3068 SUBST (*split
, gen_rtx_SIGN_EXTEND (split_mode
,
3069 SUBREG_REG (*split
)));
3072 SUBST (*split
, gen_rtx_ZERO_EXTEND (split_mode
,
3073 SUBREG_REG (*split
)));
3077 /* Attempt to split binary operators using arithmetic identities. */
3078 if (BINARY_P (SET_SRC (newpat
))
3079 && split_mode
== GET_MODE (SET_SRC (newpat
))
3080 && ! side_effects_p (SET_SRC (newpat
)))
3082 rtx setsrc
= SET_SRC (newpat
);
3083 enum machine_mode mode
= GET_MODE (setsrc
);
3084 enum rtx_code code
= GET_CODE (setsrc
);
3085 rtx src_op0
= XEXP (setsrc
, 0);
3086 rtx src_op1
= XEXP (setsrc
, 1);
3088 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3089 if (rtx_equal_p (src_op0
, src_op1
))
3091 newi2pat
= gen_rtx_SET (VOIDmode
, newdest
, src_op0
);
3092 SUBST (XEXP (setsrc
, 0), newdest
);
3093 SUBST (XEXP (setsrc
, 1), newdest
);
3096 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3097 else if ((code
== PLUS
|| code
== MULT
)
3098 && GET_CODE (src_op0
) == code
3099 && GET_CODE (XEXP (src_op0
, 0)) == code
3100 && (INTEGRAL_MODE_P (mode
)
3101 || (FLOAT_MODE_P (mode
)
3102 && flag_unsafe_math_optimizations
)))
3104 rtx p
= XEXP (XEXP (src_op0
, 0), 0);
3105 rtx q
= XEXP (XEXP (src_op0
, 0), 1);
3106 rtx r
= XEXP (src_op0
, 1);
3109 /* Split both "((X op Y) op X) op Y" and
3110 "((X op Y) op Y) op X" as "T op T" where T is
3112 if ((rtx_equal_p (p
,r
) && rtx_equal_p (q
,s
))
3113 || (rtx_equal_p (p
,s
) && rtx_equal_p (q
,r
)))
3115 newi2pat
= gen_rtx_SET (VOIDmode
, newdest
,
3117 SUBST (XEXP (setsrc
, 0), newdest
);
3118 SUBST (XEXP (setsrc
, 1), newdest
);
3121 /* Split "((X op X) op Y) op Y)" as "T op T" where
3123 else if (rtx_equal_p (p
,q
) && rtx_equal_p (r
,s
))
3125 rtx tmp
= simplify_gen_binary (code
, mode
, p
, r
);
3126 newi2pat
= gen_rtx_SET (VOIDmode
, newdest
, tmp
);
3127 SUBST (XEXP (setsrc
, 0), newdest
);
3128 SUBST (XEXP (setsrc
, 1), newdest
);
3136 newi2pat
= gen_rtx_SET (VOIDmode
, newdest
, *split
);
3137 SUBST (*split
, newdest
);
3140 i2_code_number
= recog_for_combine (&newi2pat
, i2
, &new_i2_notes
);
3142 /* recog_for_combine might have added CLOBBERs to newi2pat.
3143 Make sure NEWPAT does not depend on the clobbered regs. */
3144 if (GET_CODE (newi2pat
) == PARALLEL
)
3145 for (i
= XVECLEN (newi2pat
, 0) - 1; i
>= 0; i
--)
3146 if (GET_CODE (XVECEXP (newi2pat
, 0, i
)) == CLOBBER
)
3148 rtx reg
= XEXP (XVECEXP (newi2pat
, 0, i
), 0);
3149 if (reg_overlap_mentioned_p (reg
, newpat
))
3156 /* If the split point was a MULT and we didn't have one before,
3157 don't use one now. */
3158 if (i2_code_number
>= 0 && ! (split_code
== MULT
&& ! have_mult
))
3159 insn_code_number
= recog_for_combine (&newpat
, i3
, &new_i3_notes
);
3163 /* Check for a case where we loaded from memory in a narrow mode and
3164 then sign extended it, but we need both registers. In that case,
3165 we have a PARALLEL with both loads from the same memory location.
3166 We can split this into a load from memory followed by a register-register
3167 copy. This saves at least one insn, more if register allocation can
3170 We cannot do this if the destination of the first assignment is a
3171 condition code register or cc0. We eliminate this case by making sure
3172 the SET_DEST and SET_SRC have the same mode.
3174 We cannot do this if the destination of the second assignment is
3175 a register that we have already assumed is zero-extended. Similarly
3176 for a SUBREG of such a register. */
3178 else if (i1
&& insn_code_number
< 0 && asm_noperands (newpat
) < 0
3179 && GET_CODE (newpat
) == PARALLEL
3180 && XVECLEN (newpat
, 0) == 2
3181 && GET_CODE (XVECEXP (newpat
, 0, 0)) == SET
3182 && GET_CODE (SET_SRC (XVECEXP (newpat
, 0, 0))) == SIGN_EXTEND
3183 && (GET_MODE (SET_DEST (XVECEXP (newpat
, 0, 0)))
3184 == GET_MODE (SET_SRC (XVECEXP (newpat
, 0, 0))))
3185 && GET_CODE (XVECEXP (newpat
, 0, 1)) == SET
3186 && rtx_equal_p (SET_SRC (XVECEXP (newpat
, 0, 1)),
3187 XEXP (SET_SRC (XVECEXP (newpat
, 0, 0)), 0))
3188 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat
, 0, 1)),
3190 && GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 1))) != ZERO_EXTRACT
3191 && GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 1))) != STRICT_LOW_PART
3192 && ! (temp
= SET_DEST (XVECEXP (newpat
, 0, 1)),
3194 && reg_stat
[REGNO (temp
)].nonzero_bits
!= 0
3195 && GET_MODE_BITSIZE (GET_MODE (temp
)) < BITS_PER_WORD
3196 && GET_MODE_BITSIZE (GET_MODE (temp
)) < HOST_BITS_PER_INT
3197 && (reg_stat
[REGNO (temp
)].nonzero_bits
3198 != GET_MODE_MASK (word_mode
))))
3199 && ! (GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 1))) == SUBREG
3200 && (temp
= SUBREG_REG (SET_DEST (XVECEXP (newpat
, 0, 1))),
3202 && reg_stat
[REGNO (temp
)].nonzero_bits
!= 0
3203 && GET_MODE_BITSIZE (GET_MODE (temp
)) < BITS_PER_WORD
3204 && GET_MODE_BITSIZE (GET_MODE (temp
)) < HOST_BITS_PER_INT
3205 && (reg_stat
[REGNO (temp
)].nonzero_bits
3206 != GET_MODE_MASK (word_mode
)))))
3207 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat
, 0, 1)),
3208 SET_SRC (XVECEXP (newpat
, 0, 1)))
3209 && ! find_reg_note (i3
, REG_UNUSED
,
3210 SET_DEST (XVECEXP (newpat
, 0, 0))))
3214 newi2pat
= XVECEXP (newpat
, 0, 0);
3215 ni2dest
= SET_DEST (XVECEXP (newpat
, 0, 0));
3216 newpat
= XVECEXP (newpat
, 0, 1);
3217 SUBST (SET_SRC (newpat
),
3218 gen_lowpart (GET_MODE (SET_SRC (newpat
)), ni2dest
));
3219 i2_code_number
= recog_for_combine (&newi2pat
, i2
, &new_i2_notes
);
3221 if (i2_code_number
>= 0)
3222 insn_code_number
= recog_for_combine (&newpat
, i3
, &new_i3_notes
);
3224 if (insn_code_number
>= 0)
3228 /* Similarly, check for a case where we have a PARALLEL of two independent
3229 SETs but we started with three insns. In this case, we can do the sets
3230 as two separate insns. This case occurs when some SET allows two
3231 other insns to combine, but the destination of that SET is still live. */
3233 else if (i1
&& insn_code_number
< 0 && asm_noperands (newpat
) < 0
3234 && GET_CODE (newpat
) == PARALLEL
3235 && XVECLEN (newpat
, 0) == 2
3236 && GET_CODE (XVECEXP (newpat
, 0, 0)) == SET
3237 && GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 0))) != ZERO_EXTRACT
3238 && GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 0))) != STRICT_LOW_PART
3239 && GET_CODE (XVECEXP (newpat
, 0, 1)) == SET
3240 && GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 1))) != ZERO_EXTRACT
3241 && GET_CODE (SET_DEST (XVECEXP (newpat
, 0, 1))) != STRICT_LOW_PART
3242 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat
, 0, 1)),
3244 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat
, 0, 1)),
3245 XVECEXP (newpat
, 0, 0))
3246 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat
, 0, 0)),
3247 XVECEXP (newpat
, 0, 1))
3248 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat
, 0, 0)))
3249 && contains_muldiv (SET_SRC (XVECEXP (newpat
, 0, 1))))
3251 /* We cannot split the parallel into two sets if both sets
3253 && ! (reg_referenced_p (cc0_rtx
, XVECEXP (newpat
, 0, 0))
3254 && reg_referenced_p (cc0_rtx
, XVECEXP (newpat
, 0, 1)))
3258 /* Normally, it doesn't matter which of the two is done first,
3259 but it does if one references cc0. In that case, it has to
3262 if (reg_referenced_p (cc0_rtx
, XVECEXP (newpat
, 0, 0)))
3264 newi2pat
= XVECEXP (newpat
, 0, 0);
3265 newpat
= XVECEXP (newpat
, 0, 1);
3270 newi2pat
= XVECEXP (newpat
, 0, 1);
3271 newpat
= XVECEXP (newpat
, 0, 0);
3274 i2_code_number
= recog_for_combine (&newi2pat
, i2
, &new_i2_notes
);
3276 if (i2_code_number
>= 0)
3277 insn_code_number
= recog_for_combine (&newpat
, i3
, &new_i3_notes
);
3280 /* If it still isn't recognized, fail and change things back the way they
3282 if ((insn_code_number
< 0
3283 /* Is the result a reasonable ASM_OPERANDS? */
3284 && (! check_asm_operands (newpat
) || added_sets_1
|| added_sets_2
)))
3290 /* If we had to change another insn, make sure it is valid also. */
3291 if (undobuf
.other_insn
)
3293 CLEAR_HARD_REG_SET (newpat_used_regs
);
3295 other_pat
= PATTERN (undobuf
.other_insn
);
3296 other_code_number
= recog_for_combine (&other_pat
, undobuf
.other_insn
,
3299 if (other_code_number
< 0 && ! check_asm_operands (other_pat
))
3307 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3308 they are adjacent to each other or not. */
3310 rtx p
= prev_nonnote_insn (i3
);
3311 if (p
&& p
!= i2
&& NONJUMP_INSN_P (p
) && newi2pat
3312 && sets_cc0_p (newi2pat
))
3320 /* Only allow this combination if insn_rtx_costs reports that the
3321 replacement instructions are cheaper than the originals. */
3322 if (!combine_validate_cost (i1
, i2
, i3
, newpat
, newi2pat
, other_pat
))
3328 /* We now know that we can do this combination. Merge the insns and
3329 update the status of registers and LOG_LINKS. */
3331 if (undobuf
.other_insn
)
3335 PATTERN (undobuf
.other_insn
) = other_pat
;
3337 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3338 are still valid. Then add any non-duplicate notes added by
3339 recog_for_combine. */
3340 for (note
= REG_NOTES (undobuf
.other_insn
); note
; note
= next
)
3342 next
= XEXP (note
, 1);
3344 if (REG_NOTE_KIND (note
) == REG_UNUSED
3345 && ! reg_set_p (XEXP (note
, 0), PATTERN (undobuf
.other_insn
)))
3346 remove_note (undobuf
.other_insn
, note
);
3349 distribute_notes (new_other_notes
, undobuf
.other_insn
,
3350 undobuf
.other_insn
, NULL_RTX
, NULL_RTX
, NULL_RTX
);
3359 /* I3 now uses what used to be its destination and which is now
3360 I2's destination. This requires us to do a few adjustments. */
3361 PATTERN (i3
) = newpat
;
3362 adjust_for_new_dest (i3
);
3364 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3367 However, some later insn might be using I2's dest and have
3368 a LOG_LINK pointing at I3. We must remove this link.
3369 The simplest way to remove the link is to point it at I1,
3370 which we know will be a NOTE. */
3372 /* newi2pat is usually a SET here; however, recog_for_combine might
3373 have added some clobbers. */
3374 if (GET_CODE (newi2pat
) == PARALLEL
)
3375 ni2dest
= SET_DEST (XVECEXP (newi2pat
, 0, 0));
3377 ni2dest
= SET_DEST (newi2pat
);
3379 for (insn
= NEXT_INSN (i3
);
3380 insn
&& (this_basic_block
->next_bb
== EXIT_BLOCK_PTR
3381 || insn
!= BB_HEAD (this_basic_block
->next_bb
));
3382 insn
= NEXT_INSN (insn
))
3384 if (INSN_P (insn
) && reg_referenced_p (ni2dest
, PATTERN (insn
)))
3386 for (link
= LOG_LINKS (insn
); link
;
3387 link
= XEXP (link
, 1))
3388 if (XEXP (link
, 0) == i3
)
3389 XEXP (link
, 0) = i1
;
3397 rtx i3notes
, i2notes
, i1notes
= 0;
3398 rtx i3links
, i2links
, i1links
= 0;
3401 /* Compute which registers we expect to eliminate. newi2pat may be setting
3402 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3403 same as i3dest, in which case newi2pat may be setting i1dest. */
3404 rtx elim_i2
= ((newi2pat
&& reg_set_p (i2dest
, newi2pat
))
3405 || i2dest_in_i2src
|| i2dest_in_i1src
3408 rtx elim_i1
= (i1
== 0 || i1dest_in_i1src
3409 || (newi2pat
&& reg_set_p (i1dest
, newi2pat
))
3413 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3415 i3notes
= REG_NOTES (i3
), i3links
= LOG_LINKS (i3
);
3416 i2notes
= REG_NOTES (i2
), i2links
= LOG_LINKS (i2
);
3418 i1notes
= REG_NOTES (i1
), i1links
= LOG_LINKS (i1
);
3420 /* Ensure that we do not have something that should not be shared but
3421 occurs multiple times in the new insns. Check this by first
3422 resetting all the `used' flags and then copying anything is shared. */
3424 reset_used_flags (i3notes
);
3425 reset_used_flags (i2notes
);
3426 reset_used_flags (i1notes
);
3427 reset_used_flags (newpat
);
3428 reset_used_flags (newi2pat
);
3429 if (undobuf
.other_insn
)
3430 reset_used_flags (PATTERN (undobuf
.other_insn
));
3432 i3notes
= copy_rtx_if_shared (i3notes
);
3433 i2notes
= copy_rtx_if_shared (i2notes
);
3434 i1notes
= copy_rtx_if_shared (i1notes
);
3435 newpat
= copy_rtx_if_shared (newpat
);
3436 newi2pat
= copy_rtx_if_shared (newi2pat
);
3437 if (undobuf
.other_insn
)
3438 reset_used_flags (PATTERN (undobuf
.other_insn
));
3440 INSN_CODE (i3
) = insn_code_number
;
3441 PATTERN (i3
) = newpat
;
3443 if (CALL_P (i3
) && CALL_INSN_FUNCTION_USAGE (i3
))
3445 rtx call_usage
= CALL_INSN_FUNCTION_USAGE (i3
);
3447 reset_used_flags (call_usage
);
3448 call_usage
= copy_rtx (call_usage
);
3451 replace_rtx (call_usage
, i2dest
, i2src
);
3454 replace_rtx (call_usage
, i1dest
, i1src
);
3456 CALL_INSN_FUNCTION_USAGE (i3
) = call_usage
;
3459 if (undobuf
.other_insn
)
3460 INSN_CODE (undobuf
.other_insn
) = other_code_number
;
3462 /* We had one special case above where I2 had more than one set and
3463 we replaced a destination of one of those sets with the destination
3464 of I3. In that case, we have to update LOG_LINKS of insns later
3465 in this basic block. Note that this (expensive) case is rare.
3467 Also, in this case, we must pretend that all REG_NOTEs for I2
3468 actually came from I3, so that REG_UNUSED notes from I2 will be
3469 properly handled. */
3471 if (i3_subst_into_i2
)
3473 for (i
= 0; i
< XVECLEN (PATTERN (i2
), 0); i
++)
3474 if ((GET_CODE (XVECEXP (PATTERN (i2
), 0, i
)) == SET
3475 || GET_CODE (XVECEXP (PATTERN (i2
), 0, i
)) == CLOBBER
)
3476 && REG_P (SET_DEST (XVECEXP (PATTERN (i2
), 0, i
)))
3477 && SET_DEST (XVECEXP (PATTERN (i2
), 0, i
)) != i2dest
3478 && ! find_reg_note (i2
, REG_UNUSED
,
3479 SET_DEST (XVECEXP (PATTERN (i2
), 0, i
))))
3480 for (temp
= NEXT_INSN (i2
);
3481 temp
&& (this_basic_block
->next_bb
== EXIT_BLOCK_PTR
3482 || BB_HEAD (this_basic_block
) != temp
);
3483 temp
= NEXT_INSN (temp
))
3484 if (temp
!= i3
&& INSN_P (temp
))
3485 for (link
= LOG_LINKS (temp
); link
; link
= XEXP (link
, 1))
3486 if (XEXP (link
, 0) == i2
)
3487 XEXP (link
, 0) = i3
;
3492 while (XEXP (link
, 1))
3493 link
= XEXP (link
, 1);
3494 XEXP (link
, 1) = i2notes
;
3508 INSN_CODE (i2
) = i2_code_number
;
3509 PATTERN (i2
) = newi2pat
;
3512 SET_INSN_DELETED (i2
);
3518 SET_INSN_DELETED (i1
);
3521 /* Get death notes for everything that is now used in either I3 or
3522 I2 and used to die in a previous insn. If we built two new
3523 patterns, move from I1 to I2 then I2 to I3 so that we get the
3524 proper movement on registers that I2 modifies. */
3528 move_deaths (newi2pat
, NULL_RTX
, DF_INSN_LUID (i1
), i2
, &midnotes
);
3529 move_deaths (newpat
, newi2pat
, DF_INSN_LUID (i1
), i3
, &midnotes
);
3532 move_deaths (newpat
, NULL_RTX
, i1
? DF_INSN_LUID (i1
) : DF_INSN_LUID (i2
),
3535 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
3537 distribute_notes (i3notes
, i3
, i3
, newi2pat
? i2
: NULL_RTX
,
3540 distribute_notes (i2notes
, i2
, i3
, newi2pat
? i2
: NULL_RTX
,
3543 distribute_notes (i1notes
, i1
, i3
, newi2pat
? i2
: NULL_RTX
,
3546 distribute_notes (midnotes
, NULL_RTX
, i3
, newi2pat
? i2
: NULL_RTX
,
3549 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
3550 know these are REG_UNUSED and want them to go to the desired insn,
3551 so we always pass it as i3. */
3553 if (newi2pat
&& new_i2_notes
)
3554 distribute_notes (new_i2_notes
, i2
, i2
, NULL_RTX
, NULL_RTX
, NULL_RTX
);
3557 distribute_notes (new_i3_notes
, i3
, i3
, NULL_RTX
, NULL_RTX
, NULL_RTX
);
3559 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
3560 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
3561 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
3562 in that case, it might delete I2. Similarly for I2 and I1.
3563 Show an additional death due to the REG_DEAD note we make here. If
3564 we discard it in distribute_notes, we will decrement it again. */
3568 if (newi2pat
&& reg_set_p (i3dest_killed
, newi2pat
))
3569 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD
, i3dest_killed
,
3571 NULL_RTX
, i2
, NULL_RTX
, elim_i2
, elim_i1
);
3573 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD
, i3dest_killed
,
3575 NULL_RTX
, i3
, newi2pat
? i2
: NULL_RTX
,
3579 if (i2dest_in_i2src
)
3581 if (newi2pat
&& reg_set_p (i2dest
, newi2pat
))
3582 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD
, i2dest
, NULL_RTX
),
3583 NULL_RTX
, i2
, NULL_RTX
, NULL_RTX
, NULL_RTX
);
3585 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD
, i2dest
, NULL_RTX
),
3586 NULL_RTX
, i3
, newi2pat
? i2
: NULL_RTX
,
3587 NULL_RTX
, NULL_RTX
);
3590 if (i1dest_in_i1src
)
3592 if (newi2pat
&& reg_set_p (i1dest
, newi2pat
))
3593 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD
, i1dest
, NULL_RTX
),
3594 NULL_RTX
, i2
, NULL_RTX
, NULL_RTX
, NULL_RTX
);
3596 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD
, i1dest
, NULL_RTX
),
3597 NULL_RTX
, i3
, newi2pat
? i2
: NULL_RTX
,
3598 NULL_RTX
, NULL_RTX
);
3601 distribute_links (i3links
);
3602 distribute_links (i2links
);
3603 distribute_links (i1links
);
3608 rtx i2_insn
= 0, i2_val
= 0, set
;
3610 /* The insn that used to set this register doesn't exist, and
3611 this life of the register may not exist either. See if one of
3612 I3's links points to an insn that sets I2DEST. If it does,
3613 that is now the last known value for I2DEST. If we don't update
3614 this and I2 set the register to a value that depended on its old
3615 contents, we will get confused. If this insn is used, thing
3616 will be set correctly in combine_instructions. */
3618 for (link
= LOG_LINKS (i3
); link
; link
= XEXP (link
, 1))
3619 if ((set
= single_set (XEXP (link
, 0))) != 0
3620 && rtx_equal_p (i2dest
, SET_DEST (set
)))
3621 i2_insn
= XEXP (link
, 0), i2_val
= SET_SRC (set
);
3623 record_value_for_reg (i2dest
, i2_insn
, i2_val
);
3625 /* If the reg formerly set in I2 died only once and that was in I3,
3626 zero its use count so it won't make `reload' do any work. */
3628 && (newi2pat
== 0 || ! reg_mentioned_p (i2dest
, newi2pat
))
3629 && ! i2dest_in_i2src
)
3631 regno
= REGNO (i2dest
);
3632 INC_REG_N_SETS (regno
, -1);
3636 if (i1
&& REG_P (i1dest
))
3639 rtx i1_insn
= 0, i1_val
= 0, set
;
3641 for (link
= LOG_LINKS (i3
); link
; link
= XEXP (link
, 1))
3642 if ((set
= single_set (XEXP (link
, 0))) != 0
3643 && rtx_equal_p (i1dest
, SET_DEST (set
)))
3644 i1_insn
= XEXP (link
, 0), i1_val
= SET_SRC (set
);
3646 record_value_for_reg (i1dest
, i1_insn
, i1_val
);
3648 regno
= REGNO (i1dest
);
3649 if (! added_sets_1
&& ! i1dest_in_i1src
)
3650 INC_REG_N_SETS (regno
, -1);
3653 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3654 been made to this insn. The order of
3655 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3656 can affect nonzero_bits of newpat */
3658 note_stores (newi2pat
, set_nonzero_bits_and_sign_copies
, NULL
);
3659 note_stores (newpat
, set_nonzero_bits_and_sign_copies
, NULL
);
3661 /* Set new_direct_jump_p if a new return or simple jump instruction
3664 If I3 is now an unconditional jump, ensure that it has a
3665 BARRIER following it since it may have initially been a
3666 conditional jump. It may also be the last nonnote insn. */
3668 if (returnjump_p (i3
) || any_uncondjump_p (i3
))
3670 *new_direct_jump_p
= 1;
3671 mark_jump_label (PATTERN (i3
), i3
, 0);
3673 if ((temp
= next_nonnote_insn (i3
)) == NULL_RTX
3674 || !BARRIER_P (temp
))
3675 emit_barrier_after (i3
);
3678 if (undobuf
.other_insn
!= NULL_RTX
3679 && (returnjump_p (undobuf
.other_insn
)
3680 || any_uncondjump_p (undobuf
.other_insn
)))
3682 *new_direct_jump_p
= 1;
3684 if ((temp
= next_nonnote_insn (undobuf
.other_insn
)) == NULL_RTX
3685 || !BARRIER_P (temp
))
3686 emit_barrier_after (undobuf
.other_insn
);
3689 /* An NOOP jump does not need barrier, but it does need cleaning up
3691 if (GET_CODE (newpat
) == SET
3692 && SET_SRC (newpat
) == pc_rtx
3693 && SET_DEST (newpat
) == pc_rtx
)
3694 *new_direct_jump_p
= 1;
3697 if (undobuf
.other_insn
!= NULL_RTX
)
3701 fprintf (dump_file
, "modifying other_insn ");
3702 dump_insn_slim (dump_file
, undobuf
.other_insn
);
3704 df_insn_rescan (undobuf
.other_insn
);
3707 if (i1
&& !(NOTE_P(i1
) && (NOTE_KIND (i1
) == NOTE_INSN_DELETED
)))
3711 fprintf (dump_file
, "modifying insn i1 ");
3712 dump_insn_slim (dump_file
, i1
);
3714 df_insn_rescan (i1
);
3717 if (i2
&& !(NOTE_P(i2
) && (NOTE_KIND (i2
) == NOTE_INSN_DELETED
)))
3721 fprintf (dump_file
, "modifying insn i2 ");
3722 dump_insn_slim (dump_file
, i2
);
3724 df_insn_rescan (i2
);
3727 if (i3
&& !(NOTE_P(i3
) && (NOTE_KIND (i3
) == NOTE_INSN_DELETED
)))
3731 fprintf (dump_file
, "modifying insn i3 ");
3732 dump_insn_slim (dump_file
, i3
);
3734 df_insn_rescan (i3
);
3737 combine_successes
++;
3740 if (added_links_insn
3741 && (newi2pat
== 0 || DF_INSN_LUID (added_links_insn
) < DF_INSN_LUID (i2
))
3742 && DF_INSN_LUID (added_links_insn
) < DF_INSN_LUID (i3
))
3743 return added_links_insn
;
3745 return newi2pat
? i2
: i3
;
3748 /* Undo all the modifications recorded in undobuf. */
3753 struct undo
*undo
, *next
;
3755 for (undo
= undobuf
.undos
; undo
; undo
= next
)
3761 *undo
->where
.r
= undo
->old_contents
.r
;
3764 *undo
->where
.i
= undo
->old_contents
.i
;
3767 PUT_MODE (*undo
->where
.r
, undo
->old_contents
.m
);
3773 undo
->next
= undobuf
.frees
;
3774 undobuf
.frees
= undo
;
3780 /* We've committed to accepting the changes we made. Move all
3781 of the undos to the free list. */
3786 struct undo
*undo
, *next
;
3788 for (undo
= undobuf
.undos
; undo
; undo
= next
)
3791 undo
->next
= undobuf
.frees
;
3792 undobuf
.frees
= undo
;
3797 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3798 where we have an arithmetic expression and return that point. LOC will
3801 try_combine will call this function to see if an insn can be split into
3805 find_split_point (rtx
*loc
, rtx insn
)
3808 enum rtx_code code
= GET_CODE (x
);
3810 unsigned HOST_WIDE_INT len
= 0;
3811 HOST_WIDE_INT pos
= 0;
3813 rtx inner
= NULL_RTX
;
3815 /* First special-case some codes. */
3819 #ifdef INSN_SCHEDULING
3820 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3822 if (MEM_P (SUBREG_REG (x
)))
3825 return find_split_point (&SUBREG_REG (x
), insn
);
3829 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3830 using LO_SUM and HIGH. */
3831 if (GET_CODE (XEXP (x
, 0)) == CONST
3832 || GET_CODE (XEXP (x
, 0)) == SYMBOL_REF
)
3835 gen_rtx_LO_SUM (Pmode
,
3836 gen_rtx_HIGH (Pmode
, XEXP (x
, 0)),
3838 return &XEXP (XEXP (x
, 0), 0);
3842 /* If we have a PLUS whose second operand is a constant and the
3843 address is not valid, perhaps will can split it up using
3844 the machine-specific way to split large constants. We use
3845 the first pseudo-reg (one of the virtual regs) as a placeholder;
3846 it will not remain in the result. */
3847 if (GET_CODE (XEXP (x
, 0)) == PLUS
3848 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
3849 && ! memory_address_p (GET_MODE (x
), XEXP (x
, 0)))
3851 rtx reg
= regno_reg_rtx
[FIRST_PSEUDO_REGISTER
];
3852 rtx seq
= split_insns (gen_rtx_SET (VOIDmode
, reg
, XEXP (x
, 0)),
3855 /* This should have produced two insns, each of which sets our
3856 placeholder. If the source of the second is a valid address,
3857 we can make put both sources together and make a split point
3861 && NEXT_INSN (seq
) != NULL_RTX
3862 && NEXT_INSN (NEXT_INSN (seq
)) == NULL_RTX
3863 && NONJUMP_INSN_P (seq
)
3864 && GET_CODE (PATTERN (seq
)) == SET
3865 && SET_DEST (PATTERN (seq
)) == reg
3866 && ! reg_mentioned_p (reg
,
3867 SET_SRC (PATTERN (seq
)))
3868 && NONJUMP_INSN_P (NEXT_INSN (seq
))
3869 && GET_CODE (PATTERN (NEXT_INSN (seq
))) == SET
3870 && SET_DEST (PATTERN (NEXT_INSN (seq
))) == reg
3871 && memory_address_p (GET_MODE (x
),
3872 SET_SRC (PATTERN (NEXT_INSN (seq
)))))
3874 rtx src1
= SET_SRC (PATTERN (seq
));
3875 rtx src2
= SET_SRC (PATTERN (NEXT_INSN (seq
)));
3877 /* Replace the placeholder in SRC2 with SRC1. If we can
3878 find where in SRC2 it was placed, that can become our
3879 split point and we can replace this address with SRC2.
3880 Just try two obvious places. */
3882 src2
= replace_rtx (src2
, reg
, src1
);
3884 if (XEXP (src2
, 0) == src1
)
3885 split
= &XEXP (src2
, 0);
3886 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2
, 0)))[0] == 'e'
3887 && XEXP (XEXP (src2
, 0), 0) == src1
)
3888 split
= &XEXP (XEXP (src2
, 0), 0);
3892 SUBST (XEXP (x
, 0), src2
);
3897 /* If that didn't work, perhaps the first operand is complex and
3898 needs to be computed separately, so make a split point there.
3899 This will occur on machines that just support REG + CONST
3900 and have a constant moved through some previous computation. */
3902 else if (!OBJECT_P (XEXP (XEXP (x
, 0), 0))
3903 && ! (GET_CODE (XEXP (XEXP (x
, 0), 0)) == SUBREG
3904 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x
, 0), 0)))))
3905 return &XEXP (XEXP (x
, 0), 0);
3911 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3912 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3913 we need to put the operand into a register. So split at that
3916 if (SET_DEST (x
) == cc0_rtx
3917 && GET_CODE (SET_SRC (x
)) != COMPARE
3918 && GET_CODE (SET_SRC (x
)) != ZERO_EXTRACT
3919 && !OBJECT_P (SET_SRC (x
))
3920 && ! (GET_CODE (SET_SRC (x
)) == SUBREG
3921 && OBJECT_P (SUBREG_REG (SET_SRC (x
)))))
3922 return &SET_SRC (x
);
3925 /* See if we can split SET_SRC as it stands. */
3926 split
= find_split_point (&SET_SRC (x
), insn
);
3927 if (split
&& split
!= &SET_SRC (x
))
3930 /* See if we can split SET_DEST as it stands. */
3931 split
= find_split_point (&SET_DEST (x
), insn
);
3932 if (split
&& split
!= &SET_DEST (x
))
3935 /* See if this is a bitfield assignment with everything constant. If
3936 so, this is an IOR of an AND, so split it into that. */
3937 if (GET_CODE (SET_DEST (x
)) == ZERO_EXTRACT
3938 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x
), 0)))
3939 <= HOST_BITS_PER_WIDE_INT
)
3940 && GET_CODE (XEXP (SET_DEST (x
), 1)) == CONST_INT
3941 && GET_CODE (XEXP (SET_DEST (x
), 2)) == CONST_INT
3942 && GET_CODE (SET_SRC (x
)) == CONST_INT
3943 && ((INTVAL (XEXP (SET_DEST (x
), 1))
3944 + INTVAL (XEXP (SET_DEST (x
), 2)))
3945 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x
), 0))))
3946 && ! side_effects_p (XEXP (SET_DEST (x
), 0)))
3948 HOST_WIDE_INT pos
= INTVAL (XEXP (SET_DEST (x
), 2));
3949 unsigned HOST_WIDE_INT len
= INTVAL (XEXP (SET_DEST (x
), 1));
3950 unsigned HOST_WIDE_INT src
= INTVAL (SET_SRC (x
));
3951 rtx dest
= XEXP (SET_DEST (x
), 0);
3952 enum machine_mode mode
= GET_MODE (dest
);
3953 unsigned HOST_WIDE_INT mask
= ((HOST_WIDE_INT
) 1 << len
) - 1;
3956 if (BITS_BIG_ENDIAN
)
3957 pos
= GET_MODE_BITSIZE (mode
) - len
- pos
;
3959 or_mask
= gen_int_mode (src
<< pos
, mode
);
3962 simplify_gen_binary (IOR
, mode
, dest
, or_mask
));
3965 rtx negmask
= gen_int_mode (~(mask
<< pos
), mode
);
3967 simplify_gen_binary (IOR
, mode
,
3968 simplify_gen_binary (AND
, mode
,
3973 SUBST (SET_DEST (x
), dest
);
3975 split
= find_split_point (&SET_SRC (x
), insn
);
3976 if (split
&& split
!= &SET_SRC (x
))
3980 /* Otherwise, see if this is an operation that we can split into two.
3981 If so, try to split that. */
3982 code
= GET_CODE (SET_SRC (x
));
3987 /* If we are AND'ing with a large constant that is only a single
3988 bit and the result is only being used in a context where we
3989 need to know if it is zero or nonzero, replace it with a bit
3990 extraction. This will avoid the large constant, which might
3991 have taken more than one insn to make. If the constant were
3992 not a valid argument to the AND but took only one insn to make,
3993 this is no worse, but if it took more than one insn, it will
3996 if (GET_CODE (XEXP (SET_SRC (x
), 1)) == CONST_INT
3997 && REG_P (XEXP (SET_SRC (x
), 0))
3998 && (pos
= exact_log2 (INTVAL (XEXP (SET_SRC (x
), 1)))) >= 7
3999 && REG_P (SET_DEST (x
))
4000 && (split
= find_single_use (SET_DEST (x
), insn
, (rtx
*) 0)) != 0
4001 && (GET_CODE (*split
) == EQ
|| GET_CODE (*split
) == NE
)
4002 && XEXP (*split
, 0) == SET_DEST (x
)
4003 && XEXP (*split
, 1) == const0_rtx
)
4005 rtx extraction
= make_extraction (GET_MODE (SET_DEST (x
)),
4006 XEXP (SET_SRC (x
), 0),
4007 pos
, NULL_RTX
, 1, 1, 0, 0);
4008 if (extraction
!= 0)
4010 SUBST (SET_SRC (x
), extraction
);
4011 return find_split_point (loc
, insn
);
4017 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4018 is known to be on, this can be converted into a NEG of a shift. */
4019 if (STORE_FLAG_VALUE
== -1 && XEXP (SET_SRC (x
), 1) == const0_rtx
4020 && GET_MODE (SET_SRC (x
)) == GET_MODE (XEXP (SET_SRC (x
), 0))
4021 && 1 <= (pos
= exact_log2
4022 (nonzero_bits (XEXP (SET_SRC (x
), 0),
4023 GET_MODE (XEXP (SET_SRC (x
), 0))))))
4025 enum machine_mode mode
= GET_MODE (XEXP (SET_SRC (x
), 0));
4029 gen_rtx_LSHIFTRT (mode
,
4030 XEXP (SET_SRC (x
), 0),
4033 split
= find_split_point (&SET_SRC (x
), insn
);
4034 if (split
&& split
!= &SET_SRC (x
))
4040 inner
= XEXP (SET_SRC (x
), 0);
4042 /* We can't optimize if either mode is a partial integer
4043 mode as we don't know how many bits are significant
4045 if (GET_MODE_CLASS (GET_MODE (inner
)) == MODE_PARTIAL_INT
4046 || GET_MODE_CLASS (GET_MODE (SET_SRC (x
))) == MODE_PARTIAL_INT
)
4050 len
= GET_MODE_BITSIZE (GET_MODE (inner
));
4056 if (GET_CODE (XEXP (SET_SRC (x
), 1)) == CONST_INT
4057 && GET_CODE (XEXP (SET_SRC (x
), 2)) == CONST_INT
)
4059 inner
= XEXP (SET_SRC (x
), 0);
4060 len
= INTVAL (XEXP (SET_SRC (x
), 1));
4061 pos
= INTVAL (XEXP (SET_SRC (x
), 2));
4063 if (BITS_BIG_ENDIAN
)
4064 pos
= GET_MODE_BITSIZE (GET_MODE (inner
)) - len
- pos
;
4065 unsignedp
= (code
== ZERO_EXTRACT
);
4073 if (len
&& pos
>= 0 && pos
+ len
<= GET_MODE_BITSIZE (GET_MODE (inner
)))
4075 enum machine_mode mode
= GET_MODE (SET_SRC (x
));
4077 /* For unsigned, we have a choice of a shift followed by an
4078 AND or two shifts. Use two shifts for field sizes where the
4079 constant might be too large. We assume here that we can
4080 always at least get 8-bit constants in an AND insn, which is
4081 true for every current RISC. */
4083 if (unsignedp
&& len
<= 8)
4088 (mode
, gen_lowpart (mode
, inner
),
4090 GEN_INT (((HOST_WIDE_INT
) 1 << len
) - 1)));
4092 split
= find_split_point (&SET_SRC (x
), insn
);
4093 if (split
&& split
!= &SET_SRC (x
))
4100 (unsignedp
? LSHIFTRT
: ASHIFTRT
, mode
,
4101 gen_rtx_ASHIFT (mode
,
4102 gen_lowpart (mode
, inner
),
4103 GEN_INT (GET_MODE_BITSIZE (mode
)
4105 GEN_INT (GET_MODE_BITSIZE (mode
) - len
)));
4107 split
= find_split_point (&SET_SRC (x
), insn
);
4108 if (split
&& split
!= &SET_SRC (x
))
4113 /* See if this is a simple operation with a constant as the second
4114 operand. It might be that this constant is out of range and hence
4115 could be used as a split point. */
4116 if (BINARY_P (SET_SRC (x
))
4117 && CONSTANT_P (XEXP (SET_SRC (x
), 1))
4118 && (OBJECT_P (XEXP (SET_SRC (x
), 0))
4119 || (GET_CODE (XEXP (SET_SRC (x
), 0)) == SUBREG
4120 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x
), 0))))))
4121 return &XEXP (SET_SRC (x
), 1);
4123 /* Finally, see if this is a simple operation with its first operand
4124 not in a register. The operation might require this operand in a
4125 register, so return it as a split point. We can always do this
4126 because if the first operand were another operation, we would have
4127 already found it as a split point. */
4128 if ((BINARY_P (SET_SRC (x
)) || UNARY_P (SET_SRC (x
)))
4129 && ! register_operand (XEXP (SET_SRC (x
), 0), VOIDmode
))
4130 return &XEXP (SET_SRC (x
), 0);
4136 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4137 it is better to write this as (not (ior A B)) so we can split it.
4138 Similarly for IOR. */
4139 if (GET_CODE (XEXP (x
, 0)) == NOT
&& GET_CODE (XEXP (x
, 1)) == NOT
)
4142 gen_rtx_NOT (GET_MODE (x
),
4143 gen_rtx_fmt_ee (code
== IOR
? AND
: IOR
,
4145 XEXP (XEXP (x
, 0), 0),
4146 XEXP (XEXP (x
, 1), 0))));
4147 return find_split_point (loc
, insn
);
4150 /* Many RISC machines have a large set of logical insns. If the
4151 second operand is a NOT, put it first so we will try to split the
4152 other operand first. */
4153 if (GET_CODE (XEXP (x
, 1)) == NOT
)
4155 rtx tem
= XEXP (x
, 0);
4156 SUBST (XEXP (x
, 0), XEXP (x
, 1));
4157 SUBST (XEXP (x
, 1), tem
);
4165 /* Otherwise, select our actions depending on our rtx class. */
4166 switch (GET_RTX_CLASS (code
))
4168 case RTX_BITFIELD_OPS
: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4170 split
= find_split_point (&XEXP (x
, 2), insn
);
4173 /* ... fall through ... */
4175 case RTX_COMM_ARITH
:
4177 case RTX_COMM_COMPARE
:
4178 split
= find_split_point (&XEXP (x
, 1), insn
);
4181 /* ... fall through ... */
4183 /* Some machines have (and (shift ...) ...) insns. If X is not
4184 an AND, but XEXP (X, 0) is, use it as our split point. */
4185 if (GET_CODE (x
) != AND
&& GET_CODE (XEXP (x
, 0)) == AND
)
4186 return &XEXP (x
, 0);
4188 split
= find_split_point (&XEXP (x
, 0), insn
);
4194 /* Otherwise, we don't have a split point. */
4199 /* Throughout X, replace FROM with TO, and return the result.
4200 The result is TO if X is FROM;
4201 otherwise the result is X, but its contents may have been modified.
4202 If they were modified, a record was made in undobuf so that
4203 undo_all will (among other things) return X to its original state.
4205 If the number of changes necessary is too much to record to undo,
4206 the excess changes are not made, so the result is invalid.
4207 The changes already made can still be undone.
4208 undobuf.num_undo is incremented for such changes, so by testing that
4209 the caller can tell whether the result is valid.
4211 `n_occurrences' is incremented each time FROM is replaced.
4213 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4215 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4216 by copying if `n_occurrences' is nonzero. */
4219 subst (rtx x
, rtx from
, rtx to
, int in_dest
, int unique_copy
)
4221 enum rtx_code code
= GET_CODE (x
);
4222 enum machine_mode op0_mode
= VOIDmode
;
4227 /* Two expressions are equal if they are identical copies of a shared
4228 RTX or if they are both registers with the same register number
4231 #define COMBINE_RTX_EQUAL_P(X,Y) \
4233 || (REG_P (X) && REG_P (Y) \
4234 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4236 if (! in_dest
&& COMBINE_RTX_EQUAL_P (x
, from
))
4239 return (unique_copy
&& n_occurrences
> 1 ? copy_rtx (to
) : to
);
4242 /* If X and FROM are the same register but different modes, they
4243 will not have been seen as equal above. However, the log links code
4244 will make a LOG_LINKS entry for that case. If we do nothing, we
4245 will try to rerecognize our original insn and, when it succeeds,
4246 we will delete the feeding insn, which is incorrect.
4248 So force this insn not to match in this (rare) case. */
4249 if (! in_dest
&& code
== REG
&& REG_P (from
)
4250 && reg_overlap_mentioned_p (x
, from
))
4251 return gen_rtx_CLOBBER (GET_MODE (x
), const0_rtx
);
4253 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4254 of which may contain things that can be combined. */
4255 if (code
!= MEM
&& code
!= LO_SUM
&& OBJECT_P (x
))
4258 /* It is possible to have a subexpression appear twice in the insn.
4259 Suppose that FROM is a register that appears within TO.
4260 Then, after that subexpression has been scanned once by `subst',
4261 the second time it is scanned, TO may be found. If we were
4262 to scan TO here, we would find FROM within it and create a
4263 self-referent rtl structure which is completely wrong. */
4264 if (COMBINE_RTX_EQUAL_P (x
, to
))
4267 /* Parallel asm_operands need special attention because all of the
4268 inputs are shared across the arms. Furthermore, unsharing the
4269 rtl results in recognition failures. Failure to handle this case
4270 specially can result in circular rtl.
4272 Solve this by doing a normal pass across the first entry of the
4273 parallel, and only processing the SET_DESTs of the subsequent
4276 if (code
== PARALLEL
4277 && GET_CODE (XVECEXP (x
, 0, 0)) == SET
4278 && GET_CODE (SET_SRC (XVECEXP (x
, 0, 0))) == ASM_OPERANDS
)
4280 new = subst (XVECEXP (x
, 0, 0), from
, to
, 0, unique_copy
);
4282 /* If this substitution failed, this whole thing fails. */
4283 if (GET_CODE (new) == CLOBBER
4284 && XEXP (new, 0) == const0_rtx
)
4287 SUBST (XVECEXP (x
, 0, 0), new);
4289 for (i
= XVECLEN (x
, 0) - 1; i
>= 1; i
--)
4291 rtx dest
= SET_DEST (XVECEXP (x
, 0, i
));
4294 && GET_CODE (dest
) != CC0
4295 && GET_CODE (dest
) != PC
)
4297 new = subst (dest
, from
, to
, 0, unique_copy
);
4299 /* If this substitution failed, this whole thing fails. */
4300 if (GET_CODE (new) == CLOBBER
4301 && XEXP (new, 0) == const0_rtx
)
4304 SUBST (SET_DEST (XVECEXP (x
, 0, i
)), new);
4310 len
= GET_RTX_LENGTH (code
);
4311 fmt
= GET_RTX_FORMAT (code
);
4313 /* We don't need to process a SET_DEST that is a register, CC0,
4314 or PC, so set up to skip this common case. All other cases
4315 where we want to suppress replacing something inside a
4316 SET_SRC are handled via the IN_DEST operand. */
4318 && (REG_P (SET_DEST (x
))
4319 || GET_CODE (SET_DEST (x
)) == CC0
4320 || GET_CODE (SET_DEST (x
)) == PC
))
4323 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4326 op0_mode
= GET_MODE (XEXP (x
, 0));
4328 for (i
= 0; i
< len
; i
++)
4333 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
4335 if (COMBINE_RTX_EQUAL_P (XVECEXP (x
, i
, j
), from
))
4337 new = (unique_copy
&& n_occurrences
4338 ? copy_rtx (to
) : to
);
4343 new = subst (XVECEXP (x
, i
, j
), from
, to
, 0,
4346 /* If this substitution failed, this whole thing
4348 if (GET_CODE (new) == CLOBBER
4349 && XEXP (new, 0) == const0_rtx
)
4353 SUBST (XVECEXP (x
, i
, j
), new);
4356 else if (fmt
[i
] == 'e')
4358 /* If this is a register being set, ignore it. */
4362 && (((code
== SUBREG
|| code
== ZERO_EXTRACT
)
4364 || code
== STRICT_LOW_PART
))
4367 else if (COMBINE_RTX_EQUAL_P (XEXP (x
, i
), from
))
4369 /* In general, don't install a subreg involving two
4370 modes not tieable. It can worsen register
4371 allocation, and can even make invalid reload
4372 insns, since the reg inside may need to be copied
4373 from in the outside mode, and that may be invalid
4374 if it is an fp reg copied in integer mode.
4376 We allow two exceptions to this: It is valid if
4377 it is inside another SUBREG and the mode of that
4378 SUBREG and the mode of the inside of TO is
4379 tieable and it is valid if X is a SET that copies
4382 if (GET_CODE (to
) == SUBREG
4383 && ! MODES_TIEABLE_P (GET_MODE (to
),
4384 GET_MODE (SUBREG_REG (to
)))
4385 && ! (code
== SUBREG
4386 && MODES_TIEABLE_P (GET_MODE (x
),
4387 GET_MODE (SUBREG_REG (to
))))
4389 && ! (code
== SET
&& i
== 1 && XEXP (x
, 0) == cc0_rtx
)
4392 return gen_rtx_CLOBBER (VOIDmode
, const0_rtx
);
4394 #ifdef CANNOT_CHANGE_MODE_CLASS
4397 && REGNO (to
) < FIRST_PSEUDO_REGISTER
4398 && REG_CANNOT_CHANGE_MODE_P (REGNO (to
),
4401 return gen_rtx_CLOBBER (VOIDmode
, const0_rtx
);
4404 new = (unique_copy
&& n_occurrences
? copy_rtx (to
) : to
);
4408 /* If we are in a SET_DEST, suppress most cases unless we
4409 have gone inside a MEM, in which case we want to
4410 simplify the address. We assume here that things that
4411 are actually part of the destination have their inner
4412 parts in the first expression. This is true for SUBREG,
4413 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4414 things aside from REG and MEM that should appear in a
4416 new = subst (XEXP (x
, i
), from
, to
,
4418 && (code
== SUBREG
|| code
== STRICT_LOW_PART
4419 || code
== ZERO_EXTRACT
))
4421 && i
== 0), unique_copy
);
4423 /* If we found that we will have to reject this combination,
4424 indicate that by returning the CLOBBER ourselves, rather than
4425 an expression containing it. This will speed things up as
4426 well as prevent accidents where two CLOBBERs are considered
4427 to be equal, thus producing an incorrect simplification. */
4429 if (GET_CODE (new) == CLOBBER
&& XEXP (new, 0) == const0_rtx
)
4432 if (GET_CODE (x
) == SUBREG
4433 && (GET_CODE (new) == CONST_INT
4434 || GET_CODE (new) == CONST_DOUBLE
))
4436 enum machine_mode mode
= GET_MODE (x
);
4438 x
= simplify_subreg (GET_MODE (x
), new,
4439 GET_MODE (SUBREG_REG (x
)),
4442 x
= gen_rtx_CLOBBER (mode
, const0_rtx
);
4444 else if (GET_CODE (new) == CONST_INT
4445 && GET_CODE (x
) == ZERO_EXTEND
)
4447 x
= simplify_unary_operation (ZERO_EXTEND
, GET_MODE (x
),
4448 new, GET_MODE (XEXP (x
, 0)));
4452 SUBST (XEXP (x
, i
), new);
4457 /* Try to simplify X. If the simplification changed the code, it is likely
4458 that further simplification will help, so loop, but limit the number
4459 of repetitions that will be performed. */
4461 for (i
= 0; i
< 4; i
++)
4463 /* If X is sufficiently simple, don't bother trying to do anything
4465 if (code
!= CONST_INT
&& code
!= REG
&& code
!= CLOBBER
)
4466 x
= combine_simplify_rtx (x
, op0_mode
, in_dest
);
4468 if (GET_CODE (x
) == code
)
4471 code
= GET_CODE (x
);
4473 /* We no longer know the original mode of operand 0 since we
4474 have changed the form of X) */
4475 op0_mode
= VOIDmode
;
4481 /* Simplify X, a piece of RTL. We just operate on the expression at the
4482 outer level; call `subst' to simplify recursively. Return the new
4485 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4486 if we are inside a SET_DEST. */
4489 combine_simplify_rtx (rtx x
, enum machine_mode op0_mode
, int in_dest
)
4491 enum rtx_code code
= GET_CODE (x
);
4492 enum machine_mode mode
= GET_MODE (x
);
4496 /* If this is a commutative operation, put a constant last and a complex
4497 expression first. We don't need to do this for comparisons here. */
4498 if (COMMUTATIVE_ARITH_P (x
)
4499 && swap_commutative_operands_p (XEXP (x
, 0), XEXP (x
, 1)))
4502 SUBST (XEXP (x
, 0), XEXP (x
, 1));
4503 SUBST (XEXP (x
, 1), temp
);
4506 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4507 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4508 things. Check for cases where both arms are testing the same
4511 Don't do anything if all operands are very simple. */
4514 && ((!OBJECT_P (XEXP (x
, 0))
4515 && ! (GET_CODE (XEXP (x
, 0)) == SUBREG
4516 && OBJECT_P (SUBREG_REG (XEXP (x
, 0)))))
4517 || (!OBJECT_P (XEXP (x
, 1))
4518 && ! (GET_CODE (XEXP (x
, 1)) == SUBREG
4519 && OBJECT_P (SUBREG_REG (XEXP (x
, 1)))))))
4521 && (!OBJECT_P (XEXP (x
, 0))
4522 && ! (GET_CODE (XEXP (x
, 0)) == SUBREG
4523 && OBJECT_P (SUBREG_REG (XEXP (x
, 0)))))))
4525 rtx cond
, true_rtx
, false_rtx
;
4527 cond
= if_then_else_cond (x
, &true_rtx
, &false_rtx
);
4529 /* If everything is a comparison, what we have is highly unlikely
4530 to be simpler, so don't use it. */
4531 && ! (COMPARISON_P (x
)
4532 && (COMPARISON_P (true_rtx
) || COMPARISON_P (false_rtx
))))
4534 rtx cop1
= const0_rtx
;
4535 enum rtx_code cond_code
= simplify_comparison (NE
, &cond
, &cop1
);
4537 if (cond_code
== NE
&& COMPARISON_P (cond
))
4540 /* Simplify the alternative arms; this may collapse the true and
4541 false arms to store-flag values. Be careful to use copy_rtx
4542 here since true_rtx or false_rtx might share RTL with x as a
4543 result of the if_then_else_cond call above. */
4544 true_rtx
= subst (copy_rtx (true_rtx
), pc_rtx
, pc_rtx
, 0, 0);
4545 false_rtx
= subst (copy_rtx (false_rtx
), pc_rtx
, pc_rtx
, 0, 0);
4547 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4548 is unlikely to be simpler. */
4549 if (general_operand (true_rtx
, VOIDmode
)
4550 && general_operand (false_rtx
, VOIDmode
))
4552 enum rtx_code reversed
;
4554 /* Restarting if we generate a store-flag expression will cause
4555 us to loop. Just drop through in this case. */
4557 /* If the result values are STORE_FLAG_VALUE and zero, we can
4558 just make the comparison operation. */
4559 if (true_rtx
== const_true_rtx
&& false_rtx
== const0_rtx
)
4560 x
= simplify_gen_relational (cond_code
, mode
, VOIDmode
,
4562 else if (true_rtx
== const0_rtx
&& false_rtx
== const_true_rtx
4563 && ((reversed
= reversed_comparison_code_parts
4564 (cond_code
, cond
, cop1
, NULL
))
4566 x
= simplify_gen_relational (reversed
, mode
, VOIDmode
,
4569 /* Likewise, we can make the negate of a comparison operation
4570 if the result values are - STORE_FLAG_VALUE and zero. */
4571 else if (GET_CODE (true_rtx
) == CONST_INT
4572 && INTVAL (true_rtx
) == - STORE_FLAG_VALUE
4573 && false_rtx
== const0_rtx
)
4574 x
= simplify_gen_unary (NEG
, mode
,
4575 simplify_gen_relational (cond_code
,
4579 else if (GET_CODE (false_rtx
) == CONST_INT
4580 && INTVAL (false_rtx
) == - STORE_FLAG_VALUE
4581 && true_rtx
== const0_rtx
4582 && ((reversed
= reversed_comparison_code_parts
4583 (cond_code
, cond
, cop1
, NULL
))
4585 x
= simplify_gen_unary (NEG
, mode
,
4586 simplify_gen_relational (reversed
,
4591 return gen_rtx_IF_THEN_ELSE (mode
,
4592 simplify_gen_relational (cond_code
,
4597 true_rtx
, false_rtx
);
4599 code
= GET_CODE (x
);
4600 op0_mode
= VOIDmode
;
4605 /* Try to fold this expression in case we have constants that weren't
4608 switch (GET_RTX_CLASS (code
))
4611 if (op0_mode
== VOIDmode
)
4612 op0_mode
= GET_MODE (XEXP (x
, 0));
4613 temp
= simplify_unary_operation (code
, mode
, XEXP (x
, 0), op0_mode
);
4616 case RTX_COMM_COMPARE
:
4618 enum machine_mode cmp_mode
= GET_MODE (XEXP (x
, 0));
4619 if (cmp_mode
== VOIDmode
)
4621 cmp_mode
= GET_MODE (XEXP (x
, 1));
4622 if (cmp_mode
== VOIDmode
)
4623 cmp_mode
= op0_mode
;
4625 temp
= simplify_relational_operation (code
, mode
, cmp_mode
,
4626 XEXP (x
, 0), XEXP (x
, 1));
4629 case RTX_COMM_ARITH
:
4631 temp
= simplify_binary_operation (code
, mode
, XEXP (x
, 0), XEXP (x
, 1));
4633 case RTX_BITFIELD_OPS
:
4635 temp
= simplify_ternary_operation (code
, mode
, op0_mode
, XEXP (x
, 0),
4636 XEXP (x
, 1), XEXP (x
, 2));
4645 code
= GET_CODE (temp
);
4646 op0_mode
= VOIDmode
;
4647 mode
= GET_MODE (temp
);
4650 /* First see if we can apply the inverse distributive law. */
4651 if (code
== PLUS
|| code
== MINUS
4652 || code
== AND
|| code
== IOR
|| code
== XOR
)
4654 x
= apply_distributive_law (x
);
4655 code
= GET_CODE (x
);
4656 op0_mode
= VOIDmode
;
4659 /* If CODE is an associative operation not otherwise handled, see if we
4660 can associate some operands. This can win if they are constants or
4661 if they are logically related (i.e. (a & b) & a). */
4662 if ((code
== PLUS
|| code
== MINUS
|| code
== MULT
|| code
== DIV
4663 || code
== AND
|| code
== IOR
|| code
== XOR
4664 || code
== SMAX
|| code
== SMIN
|| code
== UMAX
|| code
== UMIN
)
4665 && ((INTEGRAL_MODE_P (mode
) && code
!= DIV
)
4666 || (flag_unsafe_math_optimizations
&& FLOAT_MODE_P (mode
))))
4668 if (GET_CODE (XEXP (x
, 0)) == code
)
4670 rtx other
= XEXP (XEXP (x
, 0), 0);
4671 rtx inner_op0
= XEXP (XEXP (x
, 0), 1);
4672 rtx inner_op1
= XEXP (x
, 1);
4675 /* Make sure we pass the constant operand if any as the second
4676 one if this is a commutative operation. */
4677 if (CONSTANT_P (inner_op0
) && COMMUTATIVE_ARITH_P (x
))
4679 rtx tem
= inner_op0
;
4680 inner_op0
= inner_op1
;
4683 inner
= simplify_binary_operation (code
== MINUS
? PLUS
4684 : code
== DIV
? MULT
4686 mode
, inner_op0
, inner_op1
);
4688 /* For commutative operations, try the other pair if that one
4690 if (inner
== 0 && COMMUTATIVE_ARITH_P (x
))
4692 other
= XEXP (XEXP (x
, 0), 1);
4693 inner
= simplify_binary_operation (code
, mode
,
4694 XEXP (XEXP (x
, 0), 0),
4699 return simplify_gen_binary (code
, mode
, other
, inner
);
4703 /* A little bit of algebraic simplification here. */
4707 /* Ensure that our address has any ASHIFTs converted to MULT in case
4708 address-recognizing predicates are called later. */
4709 temp
= make_compound_operation (XEXP (x
, 0), MEM
);
4710 SUBST (XEXP (x
, 0), temp
);
4714 if (op0_mode
== VOIDmode
)
4715 op0_mode
= GET_MODE (SUBREG_REG (x
));
4717 /* See if this can be moved to simplify_subreg. */
4718 if (CONSTANT_P (SUBREG_REG (x
))
4719 && subreg_lowpart_offset (mode
, op0_mode
) == SUBREG_BYTE (x
)
4720 /* Don't call gen_lowpart if the inner mode
4721 is VOIDmode and we cannot simplify it, as SUBREG without
4722 inner mode is invalid. */
4723 && (GET_MODE (SUBREG_REG (x
)) != VOIDmode
4724 || gen_lowpart_common (mode
, SUBREG_REG (x
))))
4725 return gen_lowpart (mode
, SUBREG_REG (x
));
4727 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x
))) == MODE_CC
)
4731 temp
= simplify_subreg (mode
, SUBREG_REG (x
), op0_mode
,
4737 /* Don't change the mode of the MEM if that would change the meaning
4739 if (MEM_P (SUBREG_REG (x
))
4740 && (MEM_VOLATILE_P (SUBREG_REG (x
))
4741 || mode_dependent_address_p (XEXP (SUBREG_REG (x
), 0))))
4742 return gen_rtx_CLOBBER (mode
, const0_rtx
);
4744 /* Note that we cannot do any narrowing for non-constants since
4745 we might have been counting on using the fact that some bits were
4746 zero. We now do this in the SET. */
4751 temp
= expand_compound_operation (XEXP (x
, 0));
4753 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4754 replaced by (lshiftrt X C). This will convert
4755 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4757 if (GET_CODE (temp
) == ASHIFTRT
4758 && GET_CODE (XEXP (temp
, 1)) == CONST_INT
4759 && INTVAL (XEXP (temp
, 1)) == GET_MODE_BITSIZE (mode
) - 1)
4760 return simplify_shift_const (NULL_RTX
, LSHIFTRT
, mode
, XEXP (temp
, 0),
4761 INTVAL (XEXP (temp
, 1)));
4763 /* If X has only a single bit that might be nonzero, say, bit I, convert
4764 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4765 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4766 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4767 or a SUBREG of one since we'd be making the expression more
4768 complex if it was just a register. */
4771 && ! (GET_CODE (temp
) == SUBREG
4772 && REG_P (SUBREG_REG (temp
)))
4773 && (i
= exact_log2 (nonzero_bits (temp
, mode
))) >= 0)
4775 rtx temp1
= simplify_shift_const
4776 (NULL_RTX
, ASHIFTRT
, mode
,
4777 simplify_shift_const (NULL_RTX
, ASHIFT
, mode
, temp
,
4778 GET_MODE_BITSIZE (mode
) - 1 - i
),
4779 GET_MODE_BITSIZE (mode
) - 1 - i
);
4781 /* If all we did was surround TEMP with the two shifts, we
4782 haven't improved anything, so don't use it. Otherwise,
4783 we are better off with TEMP1. */
4784 if (GET_CODE (temp1
) != ASHIFTRT
4785 || GET_CODE (XEXP (temp1
, 0)) != ASHIFT
4786 || XEXP (XEXP (temp1
, 0), 0) != temp
)
4792 /* We can't handle truncation to a partial integer mode here
4793 because we don't know the real bitsize of the partial
4795 if (GET_MODE_CLASS (mode
) == MODE_PARTIAL_INT
)
4798 if (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
4799 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode
),
4800 GET_MODE_BITSIZE (GET_MODE (XEXP (x
, 0)))))
4802 force_to_mode (XEXP (x
, 0), GET_MODE (XEXP (x
, 0)),
4803 GET_MODE_MASK (mode
), 0));
4805 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4806 whose value is a comparison can be replaced with a subreg if
4807 STORE_FLAG_VALUE permits. */
4808 if (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
4809 && ((HOST_WIDE_INT
) STORE_FLAG_VALUE
& ~GET_MODE_MASK (mode
)) == 0
4810 && (temp
= get_last_value (XEXP (x
, 0)))
4811 && COMPARISON_P (temp
))
4812 return gen_lowpart (mode
, XEXP (x
, 0));
4817 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4818 using cc0, in which case we want to leave it as a COMPARE
4819 so we can distinguish it from a register-register-copy. */
4820 if (XEXP (x
, 1) == const0_rtx
)
4823 /* x - 0 is the same as x unless x's mode has signed zeros and
4824 allows rounding towards -infinity. Under those conditions,
4826 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x
, 0)))
4827 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x
, 0))))
4828 && XEXP (x
, 1) == CONST0_RTX (GET_MODE (XEXP (x
, 0))))
4834 /* (const (const X)) can become (const X). Do it this way rather than
4835 returning the inner CONST since CONST can be shared with a
4837 if (GET_CODE (XEXP (x
, 0)) == CONST
)
4838 SUBST (XEXP (x
, 0), XEXP (XEXP (x
, 0), 0));
4843 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4844 can add in an offset. find_split_point will split this address up
4845 again if it doesn't match. */
4846 if (GET_CODE (XEXP (x
, 0)) == HIGH
4847 && rtx_equal_p (XEXP (XEXP (x
, 0), 0), XEXP (x
, 1)))
4853 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4854 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4855 bit-field and can be replaced by either a sign_extend or a
4856 sign_extract. The `and' may be a zero_extend and the two
4857 <c>, -<c> constants may be reversed. */
4858 if (GET_CODE (XEXP (x
, 0)) == XOR
4859 && GET_CODE (XEXP (x
, 1)) == CONST_INT
4860 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
4861 && INTVAL (XEXP (x
, 1)) == -INTVAL (XEXP (XEXP (x
, 0), 1))
4862 && ((i
= exact_log2 (INTVAL (XEXP (XEXP (x
, 0), 1)))) >= 0
4863 || (i
= exact_log2 (INTVAL (XEXP (x
, 1)))) >= 0)
4864 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
4865 && ((GET_CODE (XEXP (XEXP (x
, 0), 0)) == AND
4866 && GET_CODE (XEXP (XEXP (XEXP (x
, 0), 0), 1)) == CONST_INT
4867 && (INTVAL (XEXP (XEXP (XEXP (x
, 0), 0), 1))
4868 == ((HOST_WIDE_INT
) 1 << (i
+ 1)) - 1))
4869 || (GET_CODE (XEXP (XEXP (x
, 0), 0)) == ZERO_EXTEND
4870 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x
, 0), 0), 0)))
4871 == (unsigned int) i
+ 1))))
4872 return simplify_shift_const
4873 (NULL_RTX
, ASHIFTRT
, mode
,
4874 simplify_shift_const (NULL_RTX
, ASHIFT
, mode
,
4875 XEXP (XEXP (XEXP (x
, 0), 0), 0),
4876 GET_MODE_BITSIZE (mode
) - (i
+ 1)),
4877 GET_MODE_BITSIZE (mode
) - (i
+ 1));
4879 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4880 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4881 the bitsize of the mode - 1. This allows simplification of
4882 "a = (b & 8) == 0;" */
4883 if (XEXP (x
, 1) == constm1_rtx
4884 && !REG_P (XEXP (x
, 0))
4885 && ! (GET_CODE (XEXP (x
, 0)) == SUBREG
4886 && REG_P (SUBREG_REG (XEXP (x
, 0))))
4887 && nonzero_bits (XEXP (x
, 0), mode
) == 1)
4888 return simplify_shift_const (NULL_RTX
, ASHIFTRT
, mode
,
4889 simplify_shift_const (NULL_RTX
, ASHIFT
, mode
,
4890 gen_rtx_XOR (mode
, XEXP (x
, 0), const1_rtx
),
4891 GET_MODE_BITSIZE (mode
) - 1),
4892 GET_MODE_BITSIZE (mode
) - 1);
4894 /* If we are adding two things that have no bits in common, convert
4895 the addition into an IOR. This will often be further simplified,
4896 for example in cases like ((a & 1) + (a & 2)), which can
4899 if (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
4900 && (nonzero_bits (XEXP (x
, 0), mode
)
4901 & nonzero_bits (XEXP (x
, 1), mode
)) == 0)
4903 /* Try to simplify the expression further. */
4904 rtx tor
= simplify_gen_binary (IOR
, mode
, XEXP (x
, 0), XEXP (x
, 1));
4905 temp
= combine_simplify_rtx (tor
, mode
, in_dest
);
4907 /* If we could, great. If not, do not go ahead with the IOR
4908 replacement, since PLUS appears in many special purpose
4909 address arithmetic instructions. */
4910 if (GET_CODE (temp
) != CLOBBER
&& temp
!= tor
)
4916 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4917 (and <foo> (const_int pow2-1)) */
4918 if (GET_CODE (XEXP (x
, 1)) == AND
4919 && GET_CODE (XEXP (XEXP (x
, 1), 1)) == CONST_INT
4920 && exact_log2 (-INTVAL (XEXP (XEXP (x
, 1), 1))) >= 0
4921 && rtx_equal_p (XEXP (XEXP (x
, 1), 0), XEXP (x
, 0)))
4922 return simplify_and_const_int (NULL_RTX
, mode
, XEXP (x
, 0),
4923 -INTVAL (XEXP (XEXP (x
, 1), 1)) - 1);
4927 /* If we have (mult (plus A B) C), apply the distributive law and then
4928 the inverse distributive law to see if things simplify. This
4929 occurs mostly in addresses, often when unrolling loops. */
4931 if (GET_CODE (XEXP (x
, 0)) == PLUS
)
4933 rtx result
= distribute_and_simplify_rtx (x
, 0);
4938 /* Try simplify a*(b/c) as (a*b)/c. */
4939 if (FLOAT_MODE_P (mode
) && flag_unsafe_math_optimizations
4940 && GET_CODE (XEXP (x
, 0)) == DIV
)
4942 rtx tem
= simplify_binary_operation (MULT
, mode
,
4943 XEXP (XEXP (x
, 0), 0),
4946 return simplify_gen_binary (DIV
, mode
, tem
, XEXP (XEXP (x
, 0), 1));
4951 /* If this is a divide by a power of two, treat it as a shift if
4952 its first operand is a shift. */
4953 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
4954 && (i
= exact_log2 (INTVAL (XEXP (x
, 1)))) >= 0
4955 && (GET_CODE (XEXP (x
, 0)) == ASHIFT
4956 || GET_CODE (XEXP (x
, 0)) == LSHIFTRT
4957 || GET_CODE (XEXP (x
, 0)) == ASHIFTRT
4958 || GET_CODE (XEXP (x
, 0)) == ROTATE
4959 || GET_CODE (XEXP (x
, 0)) == ROTATERT
))
4960 return simplify_shift_const (NULL_RTX
, LSHIFTRT
, mode
, XEXP (x
, 0), i
);
4964 case GT
: case GTU
: case GE
: case GEU
:
4965 case LT
: case LTU
: case LE
: case LEU
:
4966 case UNEQ
: case LTGT
:
4967 case UNGT
: case UNGE
:
4968 case UNLT
: case UNLE
:
4969 case UNORDERED
: case ORDERED
:
4970 /* If the first operand is a condition code, we can't do anything
4972 if (GET_CODE (XEXP (x
, 0)) == COMPARE
4973 || (GET_MODE_CLASS (GET_MODE (XEXP (x
, 0))) != MODE_CC
4974 && ! CC0_P (XEXP (x
, 0))))
4976 rtx op0
= XEXP (x
, 0);
4977 rtx op1
= XEXP (x
, 1);
4978 enum rtx_code new_code
;
4980 if (GET_CODE (op0
) == COMPARE
)
4981 op1
= XEXP (op0
, 1), op0
= XEXP (op0
, 0);
4983 /* Simplify our comparison, if possible. */
4984 new_code
= simplify_comparison (code
, &op0
, &op1
);
4986 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4987 if only the low-order bit is possibly nonzero in X (such as when
4988 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4989 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4990 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4993 Remove any ZERO_EXTRACT we made when thinking this was a
4994 comparison. It may now be simpler to use, e.g., an AND. If a
4995 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4996 the call to make_compound_operation in the SET case. */
4998 if (STORE_FLAG_VALUE
== 1
4999 && new_code
== NE
&& GET_MODE_CLASS (mode
) == MODE_INT
5000 && op1
== const0_rtx
5001 && mode
== GET_MODE (op0
)
5002 && nonzero_bits (op0
, mode
) == 1)
5003 return gen_lowpart (mode
,
5004 expand_compound_operation (op0
));
5006 else if (STORE_FLAG_VALUE
== 1
5007 && new_code
== NE
&& GET_MODE_CLASS (mode
) == MODE_INT
5008 && op1
== const0_rtx
5009 && mode
== GET_MODE (op0
)
5010 && (num_sign_bit_copies (op0
, mode
)
5011 == GET_MODE_BITSIZE (mode
)))
5013 op0
= expand_compound_operation (op0
);
5014 return simplify_gen_unary (NEG
, mode
,
5015 gen_lowpart (mode
, op0
),
5019 else if (STORE_FLAG_VALUE
== 1
5020 && new_code
== EQ
&& GET_MODE_CLASS (mode
) == MODE_INT
5021 && op1
== const0_rtx
5022 && mode
== GET_MODE (op0
)
5023 && nonzero_bits (op0
, mode
) == 1)
5025 op0
= expand_compound_operation (op0
);
5026 return simplify_gen_binary (XOR
, mode
,
5027 gen_lowpart (mode
, op0
),
5031 else if (STORE_FLAG_VALUE
== 1
5032 && new_code
== EQ
&& GET_MODE_CLASS (mode
) == MODE_INT
5033 && op1
== const0_rtx
5034 && mode
== GET_MODE (op0
)
5035 && (num_sign_bit_copies (op0
, mode
)
5036 == GET_MODE_BITSIZE (mode
)))
5038 op0
= expand_compound_operation (op0
);
5039 return plus_constant (gen_lowpart (mode
, op0
), 1);
5042 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5044 if (STORE_FLAG_VALUE
== -1
5045 && new_code
== NE
&& GET_MODE_CLASS (mode
) == MODE_INT
5046 && op1
== const0_rtx
5047 && (num_sign_bit_copies (op0
, mode
)
5048 == GET_MODE_BITSIZE (mode
)))
5049 return gen_lowpart (mode
,
5050 expand_compound_operation (op0
));
5052 else if (STORE_FLAG_VALUE
== -1
5053 && new_code
== NE
&& GET_MODE_CLASS (mode
) == MODE_INT
5054 && op1
== const0_rtx
5055 && mode
== GET_MODE (op0
)
5056 && nonzero_bits (op0
, mode
) == 1)
5058 op0
= expand_compound_operation (op0
);
5059 return simplify_gen_unary (NEG
, mode
,
5060 gen_lowpart (mode
, op0
),
5064 else if (STORE_FLAG_VALUE
== -1
5065 && new_code
== EQ
&& GET_MODE_CLASS (mode
) == MODE_INT
5066 && op1
== const0_rtx
5067 && mode
== GET_MODE (op0
)
5068 && (num_sign_bit_copies (op0
, mode
)
5069 == GET_MODE_BITSIZE (mode
)))
5071 op0
= expand_compound_operation (op0
);
5072 return simplify_gen_unary (NOT
, mode
,
5073 gen_lowpart (mode
, op0
),
5077 /* If X is 0/1, (eq X 0) is X-1. */
5078 else if (STORE_FLAG_VALUE
== -1
5079 && new_code
== EQ
&& GET_MODE_CLASS (mode
) == MODE_INT
5080 && op1
== const0_rtx
5081 && mode
== GET_MODE (op0
)
5082 && nonzero_bits (op0
, mode
) == 1)
5084 op0
= expand_compound_operation (op0
);
5085 return plus_constant (gen_lowpart (mode
, op0
), -1);
5088 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5089 one bit that might be nonzero, we can convert (ne x 0) to
5090 (ashift x c) where C puts the bit in the sign bit. Remove any
5091 AND with STORE_FLAG_VALUE when we are done, since we are only
5092 going to test the sign bit. */
5093 if (new_code
== NE
&& GET_MODE_CLASS (mode
) == MODE_INT
5094 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
5095 && ((STORE_FLAG_VALUE
& GET_MODE_MASK (mode
))
5096 == (unsigned HOST_WIDE_INT
) 1 << (GET_MODE_BITSIZE (mode
) - 1))
5097 && op1
== const0_rtx
5098 && mode
== GET_MODE (op0
)
5099 && (i
= exact_log2 (nonzero_bits (op0
, mode
))) >= 0)
5101 x
= simplify_shift_const (NULL_RTX
, ASHIFT
, mode
,
5102 expand_compound_operation (op0
),
5103 GET_MODE_BITSIZE (mode
) - 1 - i
);
5104 if (GET_CODE (x
) == AND
&& XEXP (x
, 1) == const_true_rtx
)
5110 /* If the code changed, return a whole new comparison. */
5111 if (new_code
!= code
)
5112 return gen_rtx_fmt_ee (new_code
, mode
, op0
, op1
);
5114 /* Otherwise, keep this operation, but maybe change its operands.
5115 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5116 SUBST (XEXP (x
, 0), op0
);
5117 SUBST (XEXP (x
, 1), op1
);
5122 return simplify_if_then_else (x
);
5128 /* If we are processing SET_DEST, we are done. */
5132 return expand_compound_operation (x
);
5135 return simplify_set (x
);
5139 return simplify_logical (x
);
5146 /* If this is a shift by a constant amount, simplify it. */
5147 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
)
5148 return simplify_shift_const (x
, code
, mode
, XEXP (x
, 0),
5149 INTVAL (XEXP (x
, 1)));
5151 else if (SHIFT_COUNT_TRUNCATED
&& !REG_P (XEXP (x
, 1)))
5153 force_to_mode (XEXP (x
, 1), GET_MODE (XEXP (x
, 1)),
5155 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x
))))
5167 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5170 simplify_if_then_else (rtx x
)
5172 enum machine_mode mode
= GET_MODE (x
);
5173 rtx cond
= XEXP (x
, 0);
5174 rtx true_rtx
= XEXP (x
, 1);
5175 rtx false_rtx
= XEXP (x
, 2);
5176 enum rtx_code true_code
= GET_CODE (cond
);
5177 int comparison_p
= COMPARISON_P (cond
);
5180 enum rtx_code false_code
;
5183 /* Simplify storing of the truth value. */
5184 if (comparison_p
&& true_rtx
== const_true_rtx
&& false_rtx
== const0_rtx
)
5185 return simplify_gen_relational (true_code
, mode
, VOIDmode
,
5186 XEXP (cond
, 0), XEXP (cond
, 1));
5188 /* Also when the truth value has to be reversed. */
5190 && true_rtx
== const0_rtx
&& false_rtx
== const_true_rtx
5191 && (reversed
= reversed_comparison (cond
, mode
)))
5194 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5195 in it is being compared against certain values. Get the true and false
5196 comparisons and see if that says anything about the value of each arm. */
5199 && ((false_code
= reversed_comparison_code (cond
, NULL
))
5201 && REG_P (XEXP (cond
, 0)))
5204 rtx from
= XEXP (cond
, 0);
5205 rtx true_val
= XEXP (cond
, 1);
5206 rtx false_val
= true_val
;
5209 /* If FALSE_CODE is EQ, swap the codes and arms. */
5211 if (false_code
== EQ
)
5213 swapped
= 1, true_code
= EQ
, false_code
= NE
;
5214 temp
= true_rtx
, true_rtx
= false_rtx
, false_rtx
= temp
;
5217 /* If we are comparing against zero and the expression being tested has
5218 only a single bit that might be nonzero, that is its value when it is
5219 not equal to zero. Similarly if it is known to be -1 or 0. */
5221 if (true_code
== EQ
&& true_val
== const0_rtx
5222 && exact_log2 (nzb
= nonzero_bits (from
, GET_MODE (from
))) >= 0)
5225 false_val
= GEN_INT (trunc_int_for_mode (nzb
, GET_MODE (from
)));
5227 else if (true_code
== EQ
&& true_val
== const0_rtx
5228 && (num_sign_bit_copies (from
, GET_MODE (from
))
5229 == GET_MODE_BITSIZE (GET_MODE (from
))))
5232 false_val
= constm1_rtx
;
5235 /* Now simplify an arm if we know the value of the register in the
5236 branch and it is used in the arm. Be careful due to the potential
5237 of locally-shared RTL. */
5239 if (reg_mentioned_p (from
, true_rtx
))
5240 true_rtx
= subst (known_cond (copy_rtx (true_rtx
), true_code
,
5242 pc_rtx
, pc_rtx
, 0, 0);
5243 if (reg_mentioned_p (from
, false_rtx
))
5244 false_rtx
= subst (known_cond (copy_rtx (false_rtx
), false_code
,
5246 pc_rtx
, pc_rtx
, 0, 0);
5248 SUBST (XEXP (x
, 1), swapped
? false_rtx
: true_rtx
);
5249 SUBST (XEXP (x
, 2), swapped
? true_rtx
: false_rtx
);
5251 true_rtx
= XEXP (x
, 1);
5252 false_rtx
= XEXP (x
, 2);
5253 true_code
= GET_CODE (cond
);
5256 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5257 reversed, do so to avoid needing two sets of patterns for
5258 subtract-and-branch insns. Similarly if we have a constant in the true
5259 arm, the false arm is the same as the first operand of the comparison, or
5260 the false arm is more complicated than the true arm. */
5263 && reversed_comparison_code (cond
, NULL
) != UNKNOWN
5264 && (true_rtx
== pc_rtx
5265 || (CONSTANT_P (true_rtx
)
5266 && GET_CODE (false_rtx
) != CONST_INT
&& false_rtx
!= pc_rtx
)
5267 || true_rtx
== const0_rtx
5268 || (OBJECT_P (true_rtx
) && !OBJECT_P (false_rtx
))
5269 || (GET_CODE (true_rtx
) == SUBREG
&& OBJECT_P (SUBREG_REG (true_rtx
))
5270 && !OBJECT_P (false_rtx
))
5271 || reg_mentioned_p (true_rtx
, false_rtx
)
5272 || rtx_equal_p (false_rtx
, XEXP (cond
, 0))))
5274 true_code
= reversed_comparison_code (cond
, NULL
);
5275 SUBST (XEXP (x
, 0), reversed_comparison (cond
, GET_MODE (cond
)));
5276 SUBST (XEXP (x
, 1), false_rtx
);
5277 SUBST (XEXP (x
, 2), true_rtx
);
5279 temp
= true_rtx
, true_rtx
= false_rtx
, false_rtx
= temp
;
5282 /* It is possible that the conditional has been simplified out. */
5283 true_code
= GET_CODE (cond
);
5284 comparison_p
= COMPARISON_P (cond
);
5287 /* If the two arms are identical, we don't need the comparison. */
5289 if (rtx_equal_p (true_rtx
, false_rtx
) && ! side_effects_p (cond
))
5292 /* Convert a == b ? b : a to "a". */
5293 if (true_code
== EQ
&& ! side_effects_p (cond
)
5294 && !HONOR_NANS (mode
)
5295 && rtx_equal_p (XEXP (cond
, 0), false_rtx
)
5296 && rtx_equal_p (XEXP (cond
, 1), true_rtx
))
5298 else if (true_code
== NE
&& ! side_effects_p (cond
)
5299 && !HONOR_NANS (mode
)
5300 && rtx_equal_p (XEXP (cond
, 0), true_rtx
)
5301 && rtx_equal_p (XEXP (cond
, 1), false_rtx
))
5304 /* Look for cases where we have (abs x) or (neg (abs X)). */
5306 if (GET_MODE_CLASS (mode
) == MODE_INT
5307 && GET_CODE (false_rtx
) == NEG
5308 && rtx_equal_p (true_rtx
, XEXP (false_rtx
, 0))
5310 && rtx_equal_p (true_rtx
, XEXP (cond
, 0))
5311 && ! side_effects_p (true_rtx
))
5316 return simplify_gen_unary (ABS
, mode
, true_rtx
, mode
);
5320 simplify_gen_unary (NEG
, mode
,
5321 simplify_gen_unary (ABS
, mode
, true_rtx
, mode
),
5327 /* Look for MIN or MAX. */
5329 if ((! FLOAT_MODE_P (mode
) || flag_unsafe_math_optimizations
)
5331 && rtx_equal_p (XEXP (cond
, 0), true_rtx
)
5332 && rtx_equal_p (XEXP (cond
, 1), false_rtx
)
5333 && ! side_effects_p (cond
))
5338 return simplify_gen_binary (SMAX
, mode
, true_rtx
, false_rtx
);
5341 return simplify_gen_binary (SMIN
, mode
, true_rtx
, false_rtx
);
5344 return simplify_gen_binary (UMAX
, mode
, true_rtx
, false_rtx
);
5347 return simplify_gen_binary (UMIN
, mode
, true_rtx
, false_rtx
);
5352 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5353 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5354 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5355 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5356 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5357 neither 1 or -1, but it isn't worth checking for. */
5359 if ((STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
5361 && GET_MODE_CLASS (mode
) == MODE_INT
5362 && ! side_effects_p (x
))
5364 rtx t
= make_compound_operation (true_rtx
, SET
);
5365 rtx f
= make_compound_operation (false_rtx
, SET
);
5366 rtx cond_op0
= XEXP (cond
, 0);
5367 rtx cond_op1
= XEXP (cond
, 1);
5368 enum rtx_code op
= UNKNOWN
, extend_op
= UNKNOWN
;
5369 enum machine_mode m
= mode
;
5370 rtx z
= 0, c1
= NULL_RTX
;
5372 if ((GET_CODE (t
) == PLUS
|| GET_CODE (t
) == MINUS
5373 || GET_CODE (t
) == IOR
|| GET_CODE (t
) == XOR
5374 || GET_CODE (t
) == ASHIFT
5375 || GET_CODE (t
) == LSHIFTRT
|| GET_CODE (t
) == ASHIFTRT
)
5376 && rtx_equal_p (XEXP (t
, 0), f
))
5377 c1
= XEXP (t
, 1), op
= GET_CODE (t
), z
= f
;
5379 /* If an identity-zero op is commutative, check whether there
5380 would be a match if we swapped the operands. */
5381 else if ((GET_CODE (t
) == PLUS
|| GET_CODE (t
) == IOR
5382 || GET_CODE (t
) == XOR
)
5383 && rtx_equal_p (XEXP (t
, 1), f
))
5384 c1
= XEXP (t
, 0), op
= GET_CODE (t
), z
= f
;
5385 else if (GET_CODE (t
) == SIGN_EXTEND
5386 && (GET_CODE (XEXP (t
, 0)) == PLUS
5387 || GET_CODE (XEXP (t
, 0)) == MINUS
5388 || GET_CODE (XEXP (t
, 0)) == IOR
5389 || GET_CODE (XEXP (t
, 0)) == XOR
5390 || GET_CODE (XEXP (t
, 0)) == ASHIFT
5391 || GET_CODE (XEXP (t
, 0)) == LSHIFTRT
5392 || GET_CODE (XEXP (t
, 0)) == ASHIFTRT
)
5393 && GET_CODE (XEXP (XEXP (t
, 0), 0)) == SUBREG
5394 && subreg_lowpart_p (XEXP (XEXP (t
, 0), 0))
5395 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t
, 0), 0)), f
)
5396 && (num_sign_bit_copies (f
, GET_MODE (f
))
5398 (GET_MODE_BITSIZE (mode
)
5399 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t
, 0), 0))))))
5401 c1
= XEXP (XEXP (t
, 0), 1); z
= f
; op
= GET_CODE (XEXP (t
, 0));
5402 extend_op
= SIGN_EXTEND
;
5403 m
= GET_MODE (XEXP (t
, 0));
5405 else if (GET_CODE (t
) == SIGN_EXTEND
5406 && (GET_CODE (XEXP (t
, 0)) == PLUS
5407 || GET_CODE (XEXP (t
, 0)) == IOR
5408 || GET_CODE (XEXP (t
, 0)) == XOR
)
5409 && GET_CODE (XEXP (XEXP (t
, 0), 1)) == SUBREG
5410 && subreg_lowpart_p (XEXP (XEXP (t
, 0), 1))
5411 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t
, 0), 1)), f
)
5412 && (num_sign_bit_copies (f
, GET_MODE (f
))
5414 (GET_MODE_BITSIZE (mode
)
5415 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t
, 0), 1))))))
5417 c1
= XEXP (XEXP (t
, 0), 0); z
= f
; op
= GET_CODE (XEXP (t
, 0));
5418 extend_op
= SIGN_EXTEND
;
5419 m
= GET_MODE (XEXP (t
, 0));
5421 else if (GET_CODE (t
) == ZERO_EXTEND
5422 && (GET_CODE (XEXP (t
, 0)) == PLUS
5423 || GET_CODE (XEXP (t
, 0)) == MINUS
5424 || GET_CODE (XEXP (t
, 0)) == IOR
5425 || GET_CODE (XEXP (t
, 0)) == XOR
5426 || GET_CODE (XEXP (t
, 0)) == ASHIFT
5427 || GET_CODE (XEXP (t
, 0)) == LSHIFTRT
5428 || GET_CODE (XEXP (t
, 0)) == ASHIFTRT
)
5429 && GET_CODE (XEXP (XEXP (t
, 0), 0)) == SUBREG
5430 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
5431 && subreg_lowpart_p (XEXP (XEXP (t
, 0), 0))
5432 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t
, 0), 0)), f
)
5433 && ((nonzero_bits (f
, GET_MODE (f
))
5434 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t
, 0), 0))))
5437 c1
= XEXP (XEXP (t
, 0), 1); z
= f
; op
= GET_CODE (XEXP (t
, 0));
5438 extend_op
= ZERO_EXTEND
;
5439 m
= GET_MODE (XEXP (t
, 0));
5441 else if (GET_CODE (t
) == ZERO_EXTEND
5442 && (GET_CODE (XEXP (t
, 0)) == PLUS
5443 || GET_CODE (XEXP (t
, 0)) == IOR
5444 || GET_CODE (XEXP (t
, 0)) == XOR
)
5445 && GET_CODE (XEXP (XEXP (t
, 0), 1)) == SUBREG
5446 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
5447 && subreg_lowpart_p (XEXP (XEXP (t
, 0), 1))
5448 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t
, 0), 1)), f
)
5449 && ((nonzero_bits (f
, GET_MODE (f
))
5450 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t
, 0), 1))))
5453 c1
= XEXP (XEXP (t
, 0), 0); z
= f
; op
= GET_CODE (XEXP (t
, 0));
5454 extend_op
= ZERO_EXTEND
;
5455 m
= GET_MODE (XEXP (t
, 0));
5460 temp
= subst (simplify_gen_relational (true_code
, m
, VOIDmode
,
5461 cond_op0
, cond_op1
),
5462 pc_rtx
, pc_rtx
, 0, 0);
5463 temp
= simplify_gen_binary (MULT
, m
, temp
,
5464 simplify_gen_binary (MULT
, m
, c1
,
5466 temp
= subst (temp
, pc_rtx
, pc_rtx
, 0, 0);
5467 temp
= simplify_gen_binary (op
, m
, gen_lowpart (m
, z
), temp
);
5469 if (extend_op
!= UNKNOWN
)
5470 temp
= simplify_gen_unary (extend_op
, mode
, temp
, m
);
5476 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5477 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5478 negation of a single bit, we can convert this operation to a shift. We
5479 can actually do this more generally, but it doesn't seem worth it. */
5481 if (true_code
== NE
&& XEXP (cond
, 1) == const0_rtx
5482 && false_rtx
== const0_rtx
&& GET_CODE (true_rtx
) == CONST_INT
5483 && ((1 == nonzero_bits (XEXP (cond
, 0), mode
)
5484 && (i
= exact_log2 (INTVAL (true_rtx
))) >= 0)
5485 || ((num_sign_bit_copies (XEXP (cond
, 0), mode
)
5486 == GET_MODE_BITSIZE (mode
))
5487 && (i
= exact_log2 (-INTVAL (true_rtx
))) >= 0)))
5489 simplify_shift_const (NULL_RTX
, ASHIFT
, mode
,
5490 gen_lowpart (mode
, XEXP (cond
, 0)), i
);
5492 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5493 if (true_code
== NE
&& XEXP (cond
, 1) == const0_rtx
5494 && false_rtx
== const0_rtx
&& GET_CODE (true_rtx
) == CONST_INT
5495 && GET_MODE (XEXP (cond
, 0)) == mode
5496 && (INTVAL (true_rtx
) & GET_MODE_MASK (mode
))
5497 == nonzero_bits (XEXP (cond
, 0), mode
)
5498 && (i
= exact_log2 (INTVAL (true_rtx
) & GET_MODE_MASK (mode
))) >= 0)
5499 return XEXP (cond
, 0);
5504 /* Simplify X, a SET expression. Return the new expression. */
5507 simplify_set (rtx x
)
5509 rtx src
= SET_SRC (x
);
5510 rtx dest
= SET_DEST (x
);
5511 enum machine_mode mode
5512 = GET_MODE (src
) != VOIDmode
? GET_MODE (src
) : GET_MODE (dest
);
5516 /* (set (pc) (return)) gets written as (return). */
5517 if (GET_CODE (dest
) == PC
&& GET_CODE (src
) == RETURN
)
5520 /* Now that we know for sure which bits of SRC we are using, see if we can
5521 simplify the expression for the object knowing that we only need the
5524 if (GET_MODE_CLASS (mode
) == MODE_INT
5525 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
5527 src
= force_to_mode (src
, mode
, ~(HOST_WIDE_INT
) 0, 0);
5528 SUBST (SET_SRC (x
), src
);
5531 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5532 the comparison result and try to simplify it unless we already have used
5533 undobuf.other_insn. */
5534 if ((GET_MODE_CLASS (mode
) == MODE_CC
5535 || GET_CODE (src
) == COMPARE
5537 && (cc_use
= find_single_use (dest
, subst_insn
, &other_insn
)) != 0
5538 && (undobuf
.other_insn
== 0 || other_insn
== undobuf
.other_insn
)
5539 && COMPARISON_P (*cc_use
)
5540 && rtx_equal_p (XEXP (*cc_use
, 0), dest
))
5542 enum rtx_code old_code
= GET_CODE (*cc_use
);
5543 enum rtx_code new_code
;
5545 int other_changed
= 0;
5546 enum machine_mode compare_mode
= GET_MODE (dest
);
5548 if (GET_CODE (src
) == COMPARE
)
5549 op0
= XEXP (src
, 0), op1
= XEXP (src
, 1);
5551 op0
= src
, op1
= CONST0_RTX (GET_MODE (src
));
5553 tmp
= simplify_relational_operation (old_code
, compare_mode
, VOIDmode
,
5556 new_code
= old_code
;
5557 else if (!CONSTANT_P (tmp
))
5559 new_code
= GET_CODE (tmp
);
5560 op0
= XEXP (tmp
, 0);
5561 op1
= XEXP (tmp
, 1);
5565 rtx pat
= PATTERN (other_insn
);
5566 undobuf
.other_insn
= other_insn
;
5567 SUBST (*cc_use
, tmp
);
5569 /* Attempt to simplify CC user. */
5570 if (GET_CODE (pat
) == SET
)
5572 rtx
new = simplify_rtx (SET_SRC (pat
));
5573 if (new != NULL_RTX
)
5574 SUBST (SET_SRC (pat
), new);
5577 /* Convert X into a no-op move. */
5578 SUBST (SET_DEST (x
), pc_rtx
);
5579 SUBST (SET_SRC (x
), pc_rtx
);
5583 /* Simplify our comparison, if possible. */
5584 new_code
= simplify_comparison (new_code
, &op0
, &op1
);
5586 #ifdef SELECT_CC_MODE
5587 /* If this machine has CC modes other than CCmode, check to see if we
5588 need to use a different CC mode here. */
5589 if (GET_MODE_CLASS (GET_MODE (op0
)) == MODE_CC
)
5590 compare_mode
= GET_MODE (op0
);
5592 compare_mode
= SELECT_CC_MODE (new_code
, op0
, op1
);
5595 /* If the mode changed, we have to change SET_DEST, the mode in the
5596 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5597 a hard register, just build new versions with the proper mode. If it
5598 is a pseudo, we lose unless it is only time we set the pseudo, in
5599 which case we can safely change its mode. */
5600 if (compare_mode
!= GET_MODE (dest
))
5602 if (can_change_dest_mode (dest
, 0, compare_mode
))
5604 unsigned int regno
= REGNO (dest
);
5607 if (regno
< FIRST_PSEUDO_REGISTER
)
5608 new_dest
= gen_rtx_REG (compare_mode
, regno
);
5611 SUBST_MODE (regno_reg_rtx
[regno
], compare_mode
);
5612 new_dest
= regno_reg_rtx
[regno
];
5615 SUBST (SET_DEST (x
), new_dest
);
5616 SUBST (XEXP (*cc_use
, 0), new_dest
);
5623 #endif /* SELECT_CC_MODE */
5625 /* If the code changed, we have to build a new comparison in
5626 undobuf.other_insn. */
5627 if (new_code
!= old_code
)
5629 int other_changed_previously
= other_changed
;
5630 unsigned HOST_WIDE_INT mask
;
5632 SUBST (*cc_use
, gen_rtx_fmt_ee (new_code
, GET_MODE (*cc_use
),
5636 /* If the only change we made was to change an EQ into an NE or
5637 vice versa, OP0 has only one bit that might be nonzero, and OP1
5638 is zero, check if changing the user of the condition code will
5639 produce a valid insn. If it won't, we can keep the original code
5640 in that insn by surrounding our operation with an XOR. */
5642 if (((old_code
== NE
&& new_code
== EQ
)
5643 || (old_code
== EQ
&& new_code
== NE
))
5644 && ! other_changed_previously
&& op1
== const0_rtx
5645 && GET_MODE_BITSIZE (GET_MODE (op0
)) <= HOST_BITS_PER_WIDE_INT
5646 && exact_log2 (mask
= nonzero_bits (op0
, GET_MODE (op0
))) >= 0)
5648 rtx pat
= PATTERN (other_insn
), note
= 0;
5650 if ((recog_for_combine (&pat
, other_insn
, ¬e
) < 0
5651 && ! check_asm_operands (pat
)))
5653 PUT_CODE (*cc_use
, old_code
);
5656 op0
= simplify_gen_binary (XOR
, GET_MODE (op0
),
5657 op0
, GEN_INT (mask
));
5663 undobuf
.other_insn
= other_insn
;
5666 /* If we are now comparing against zero, change our source if
5667 needed. If we do not use cc0, we always have a COMPARE. */
5668 if (op1
== const0_rtx
&& dest
== cc0_rtx
)
5670 SUBST (SET_SRC (x
), op0
);
5676 /* Otherwise, if we didn't previously have a COMPARE in the
5677 correct mode, we need one. */
5678 if (GET_CODE (src
) != COMPARE
|| GET_MODE (src
) != compare_mode
)
5680 SUBST (SET_SRC (x
), gen_rtx_COMPARE (compare_mode
, op0
, op1
));
5683 else if (GET_MODE (op0
) == compare_mode
&& op1
== const0_rtx
)
5685 SUBST (SET_SRC (x
), op0
);
5688 /* Otherwise, update the COMPARE if needed. */
5689 else if (XEXP (src
, 0) != op0
|| XEXP (src
, 1) != op1
)
5691 SUBST (SET_SRC (x
), gen_rtx_COMPARE (compare_mode
, op0
, op1
));
5697 /* Get SET_SRC in a form where we have placed back any
5698 compound expressions. Then do the checks below. */
5699 src
= make_compound_operation (src
, SET
);
5700 SUBST (SET_SRC (x
), src
);
5703 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5704 and X being a REG or (subreg (reg)), we may be able to convert this to
5705 (set (subreg:m2 x) (op)).
5707 We can always do this if M1 is narrower than M2 because that means that
5708 we only care about the low bits of the result.
5710 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5711 perform a narrower operation than requested since the high-order bits will
5712 be undefined. On machine where it is defined, this transformation is safe
5713 as long as M1 and M2 have the same number of words. */
5715 if (GET_CODE (src
) == SUBREG
&& subreg_lowpart_p (src
)
5716 && !OBJECT_P (SUBREG_REG (src
))
5717 && (((GET_MODE_SIZE (GET_MODE (src
)) + (UNITS_PER_WORD
- 1))
5719 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src
)))
5720 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
))
5721 #ifndef WORD_REGISTER_OPERATIONS
5722 && (GET_MODE_SIZE (GET_MODE (src
))
5723 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src
))))
5725 #ifdef CANNOT_CHANGE_MODE_CLASS
5726 && ! (REG_P (dest
) && REGNO (dest
) < FIRST_PSEUDO_REGISTER
5727 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest
),
5728 GET_MODE (SUBREG_REG (src
)),
5732 || (GET_CODE (dest
) == SUBREG
5733 && REG_P (SUBREG_REG (dest
)))))
5735 SUBST (SET_DEST (x
),
5736 gen_lowpart (GET_MODE (SUBREG_REG (src
)),
5738 SUBST (SET_SRC (x
), SUBREG_REG (src
));
5740 src
= SET_SRC (x
), dest
= SET_DEST (x
);
5744 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5747 && GET_CODE (src
) == SUBREG
5748 && subreg_lowpart_p (src
)
5749 && (GET_MODE_BITSIZE (GET_MODE (src
))
5750 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src
)))))
5752 rtx inner
= SUBREG_REG (src
);
5753 enum machine_mode inner_mode
= GET_MODE (inner
);
5755 /* Here we make sure that we don't have a sign bit on. */
5756 if (GET_MODE_BITSIZE (inner_mode
) <= HOST_BITS_PER_WIDE_INT
5757 && (nonzero_bits (inner
, inner_mode
)
5758 < ((unsigned HOST_WIDE_INT
) 1
5759 << (GET_MODE_BITSIZE (GET_MODE (src
)) - 1))))
5761 SUBST (SET_SRC (x
), inner
);
5767 #ifdef LOAD_EXTEND_OP
5768 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5769 would require a paradoxical subreg. Replace the subreg with a
5770 zero_extend to avoid the reload that would otherwise be required. */
5772 if (GET_CODE (src
) == SUBREG
&& subreg_lowpart_p (src
)
5773 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src
))) != UNKNOWN
5774 && SUBREG_BYTE (src
) == 0
5775 && (GET_MODE_SIZE (GET_MODE (src
))
5776 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src
))))
5777 && MEM_P (SUBREG_REG (src
)))
5780 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src
))),
5781 GET_MODE (src
), SUBREG_REG (src
)));
5787 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5788 are comparing an item known to be 0 or -1 against 0, use a logical
5789 operation instead. Check for one of the arms being an IOR of the other
5790 arm with some value. We compute three terms to be IOR'ed together. In
5791 practice, at most two will be nonzero. Then we do the IOR's. */
5793 if (GET_CODE (dest
) != PC
5794 && GET_CODE (src
) == IF_THEN_ELSE
5795 && GET_MODE_CLASS (GET_MODE (src
)) == MODE_INT
5796 && (GET_CODE (XEXP (src
, 0)) == EQ
|| GET_CODE (XEXP (src
, 0)) == NE
)
5797 && XEXP (XEXP (src
, 0), 1) == const0_rtx
5798 && GET_MODE (src
) == GET_MODE (XEXP (XEXP (src
, 0), 0))
5799 #ifdef HAVE_conditional_move
5800 && ! can_conditionally_move_p (GET_MODE (src
))
5802 && (num_sign_bit_copies (XEXP (XEXP (src
, 0), 0),
5803 GET_MODE (XEXP (XEXP (src
, 0), 0)))
5804 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src
, 0), 0))))
5805 && ! side_effects_p (src
))
5807 rtx true_rtx
= (GET_CODE (XEXP (src
, 0)) == NE
5808 ? XEXP (src
, 1) : XEXP (src
, 2));
5809 rtx false_rtx
= (GET_CODE (XEXP (src
, 0)) == NE
5810 ? XEXP (src
, 2) : XEXP (src
, 1));
5811 rtx term1
= const0_rtx
, term2
, term3
;
5813 if (GET_CODE (true_rtx
) == IOR
5814 && rtx_equal_p (XEXP (true_rtx
, 0), false_rtx
))
5815 term1
= false_rtx
, true_rtx
= XEXP (true_rtx
, 1), false_rtx
= const0_rtx
;
5816 else if (GET_CODE (true_rtx
) == IOR
5817 && rtx_equal_p (XEXP (true_rtx
, 1), false_rtx
))
5818 term1
= false_rtx
, true_rtx
= XEXP (true_rtx
, 0), false_rtx
= const0_rtx
;
5819 else if (GET_CODE (false_rtx
) == IOR
5820 && rtx_equal_p (XEXP (false_rtx
, 0), true_rtx
))
5821 term1
= true_rtx
, false_rtx
= XEXP (false_rtx
, 1), true_rtx
= const0_rtx
;
5822 else if (GET_CODE (false_rtx
) == IOR
5823 && rtx_equal_p (XEXP (false_rtx
, 1), true_rtx
))
5824 term1
= true_rtx
, false_rtx
= XEXP (false_rtx
, 0), true_rtx
= const0_rtx
;
5826 term2
= simplify_gen_binary (AND
, GET_MODE (src
),
5827 XEXP (XEXP (src
, 0), 0), true_rtx
);
5828 term3
= simplify_gen_binary (AND
, GET_MODE (src
),
5829 simplify_gen_unary (NOT
, GET_MODE (src
),
5830 XEXP (XEXP (src
, 0), 0),
5835 simplify_gen_binary (IOR
, GET_MODE (src
),
5836 simplify_gen_binary (IOR
, GET_MODE (src
),
5843 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5844 whole thing fail. */
5845 if (GET_CODE (src
) == CLOBBER
&& XEXP (src
, 0) == const0_rtx
)
5847 else if (GET_CODE (dest
) == CLOBBER
&& XEXP (dest
, 0) == const0_rtx
)
5850 /* Convert this into a field assignment operation, if possible. */
5851 return make_field_assignment (x
);
5854 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5858 simplify_logical (rtx x
)
5860 enum machine_mode mode
= GET_MODE (x
);
5861 rtx op0
= XEXP (x
, 0);
5862 rtx op1
= XEXP (x
, 1);
5864 switch (GET_CODE (x
))
5867 /* We can call simplify_and_const_int only if we don't lose
5868 any (sign) bits when converting INTVAL (op1) to
5869 "unsigned HOST_WIDE_INT". */
5870 if (GET_CODE (op1
) == CONST_INT
5871 && (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
5872 || INTVAL (op1
) > 0))
5874 x
= simplify_and_const_int (x
, mode
, op0
, INTVAL (op1
));
5875 if (GET_CODE (x
) != AND
)
5882 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5883 apply the distributive law and then the inverse distributive
5884 law to see if things simplify. */
5885 if (GET_CODE (op0
) == IOR
|| GET_CODE (op0
) == XOR
)
5887 rtx result
= distribute_and_simplify_rtx (x
, 0);
5891 if (GET_CODE (op1
) == IOR
|| GET_CODE (op1
) == XOR
)
5893 rtx result
= distribute_and_simplify_rtx (x
, 1);
5900 /* If we have (ior (and A B) C), apply the distributive law and then
5901 the inverse distributive law to see if things simplify. */
5903 if (GET_CODE (op0
) == AND
)
5905 rtx result
= distribute_and_simplify_rtx (x
, 0);
5910 if (GET_CODE (op1
) == AND
)
5912 rtx result
= distribute_and_simplify_rtx (x
, 1);
5925 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5926 operations" because they can be replaced with two more basic operations.
5927 ZERO_EXTEND is also considered "compound" because it can be replaced with
5928 an AND operation, which is simpler, though only one operation.
5930 The function expand_compound_operation is called with an rtx expression
5931 and will convert it to the appropriate shifts and AND operations,
5932 simplifying at each stage.
5934 The function make_compound_operation is called to convert an expression
5935 consisting of shifts and ANDs into the equivalent compound expression.
5936 It is the inverse of this function, loosely speaking. */
5939 expand_compound_operation (rtx x
)
5941 unsigned HOST_WIDE_INT pos
= 0, len
;
5943 unsigned int modewidth
;
5946 switch (GET_CODE (x
))
5951 /* We can't necessarily use a const_int for a multiword mode;
5952 it depends on implicitly extending the value.
5953 Since we don't know the right way to extend it,
5954 we can't tell whether the implicit way is right.
5956 Even for a mode that is no wider than a const_int,
5957 we can't win, because we need to sign extend one of its bits through
5958 the rest of it, and we don't know which bit. */
5959 if (GET_CODE (XEXP (x
, 0)) == CONST_INT
)
5962 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5963 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5964 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5965 reloaded. If not for that, MEM's would very rarely be safe.
5967 Reject MODEs bigger than a word, because we might not be able
5968 to reference a two-register group starting with an arbitrary register
5969 (and currently gen_lowpart might crash for a SUBREG). */
5971 if (GET_MODE_SIZE (GET_MODE (XEXP (x
, 0))) > UNITS_PER_WORD
)
5974 /* Reject MODEs that aren't scalar integers because turning vector
5975 or complex modes into shifts causes problems. */
5977 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x
, 0))))
5980 len
= GET_MODE_BITSIZE (GET_MODE (XEXP (x
, 0)));
5981 /* If the inner object has VOIDmode (the only way this can happen
5982 is if it is an ASM_OPERANDS), we can't do anything since we don't
5983 know how much masking to do. */
5992 /* ... fall through ... */
5995 /* If the operand is a CLOBBER, just return it. */
5996 if (GET_CODE (XEXP (x
, 0)) == CLOBBER
)
5999 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
6000 || GET_CODE (XEXP (x
, 2)) != CONST_INT
6001 || GET_MODE (XEXP (x
, 0)) == VOIDmode
)
6004 /* Reject MODEs that aren't scalar integers because turning vector
6005 or complex modes into shifts causes problems. */
6007 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x
, 0))))
6010 len
= INTVAL (XEXP (x
, 1));
6011 pos
= INTVAL (XEXP (x
, 2));
6013 /* This should stay within the object being extracted, fail otherwise. */
6014 if (len
+ pos
> GET_MODE_BITSIZE (GET_MODE (XEXP (x
, 0))))
6017 if (BITS_BIG_ENDIAN
)
6018 pos
= GET_MODE_BITSIZE (GET_MODE (XEXP (x
, 0))) - len
- pos
;
6025 /* Convert sign extension to zero extension, if we know that the high
6026 bit is not set, as this is easier to optimize. It will be converted
6027 back to cheaper alternative in make_extraction. */
6028 if (GET_CODE (x
) == SIGN_EXTEND
6029 && (GET_MODE_BITSIZE (GET_MODE (x
)) <= HOST_BITS_PER_WIDE_INT
6030 && ((nonzero_bits (XEXP (x
, 0), GET_MODE (XEXP (x
, 0)))
6031 & ~(((unsigned HOST_WIDE_INT
)
6032 GET_MODE_MASK (GET_MODE (XEXP (x
, 0))))
6036 rtx temp
= gen_rtx_ZERO_EXTEND (GET_MODE (x
), XEXP (x
, 0));
6037 rtx temp2
= expand_compound_operation (temp
);
6039 /* Make sure this is a profitable operation. */
6040 if (rtx_cost (x
, SET
) > rtx_cost (temp2
, SET
))
6042 else if (rtx_cost (x
, SET
) > rtx_cost (temp
, SET
))
6048 /* We can optimize some special cases of ZERO_EXTEND. */
6049 if (GET_CODE (x
) == ZERO_EXTEND
)
6051 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6052 know that the last value didn't have any inappropriate bits
6054 if (GET_CODE (XEXP (x
, 0)) == TRUNCATE
6055 && GET_MODE (XEXP (XEXP (x
, 0), 0)) == GET_MODE (x
)
6056 && GET_MODE_BITSIZE (GET_MODE (x
)) <= HOST_BITS_PER_WIDE_INT
6057 && (nonzero_bits (XEXP (XEXP (x
, 0), 0), GET_MODE (x
))
6058 & ~GET_MODE_MASK (GET_MODE (XEXP (x
, 0)))) == 0)
6059 return XEXP (XEXP (x
, 0), 0);
6061 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6062 if (GET_CODE (XEXP (x
, 0)) == SUBREG
6063 && GET_MODE (SUBREG_REG (XEXP (x
, 0))) == GET_MODE (x
)
6064 && subreg_lowpart_p (XEXP (x
, 0))
6065 && GET_MODE_BITSIZE (GET_MODE (x
)) <= HOST_BITS_PER_WIDE_INT
6066 && (nonzero_bits (SUBREG_REG (XEXP (x
, 0)), GET_MODE (x
))
6067 & ~GET_MODE_MASK (GET_MODE (XEXP (x
, 0)))) == 0)
6068 return SUBREG_REG (XEXP (x
, 0));
6070 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6071 is a comparison and STORE_FLAG_VALUE permits. This is like
6072 the first case, but it works even when GET_MODE (x) is larger
6073 than HOST_WIDE_INT. */
6074 if (GET_CODE (XEXP (x
, 0)) == TRUNCATE
6075 && GET_MODE (XEXP (XEXP (x
, 0), 0)) == GET_MODE (x
)
6076 && COMPARISON_P (XEXP (XEXP (x
, 0), 0))
6077 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x
, 0)))
6078 <= HOST_BITS_PER_WIDE_INT
)
6079 && ((HOST_WIDE_INT
) STORE_FLAG_VALUE
6080 & ~GET_MODE_MASK (GET_MODE (XEXP (x
, 0)))) == 0)
6081 return XEXP (XEXP (x
, 0), 0);
6083 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6084 if (GET_CODE (XEXP (x
, 0)) == SUBREG
6085 && GET_MODE (SUBREG_REG (XEXP (x
, 0))) == GET_MODE (x
)
6086 && subreg_lowpart_p (XEXP (x
, 0))
6087 && COMPARISON_P (SUBREG_REG (XEXP (x
, 0)))
6088 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x
, 0)))
6089 <= HOST_BITS_PER_WIDE_INT
)
6090 && ((HOST_WIDE_INT
) STORE_FLAG_VALUE
6091 & ~GET_MODE_MASK (GET_MODE (XEXP (x
, 0)))) == 0)
6092 return SUBREG_REG (XEXP (x
, 0));
6096 /* If we reach here, we want to return a pair of shifts. The inner
6097 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6098 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6099 logical depending on the value of UNSIGNEDP.
6101 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6102 converted into an AND of a shift.
6104 We must check for the case where the left shift would have a negative
6105 count. This can happen in a case like (x >> 31) & 255 on machines
6106 that can't shift by a constant. On those machines, we would first
6107 combine the shift with the AND to produce a variable-position
6108 extraction. Then the constant of 31 would be substituted in to produce
6109 a such a position. */
6111 modewidth
= GET_MODE_BITSIZE (GET_MODE (x
));
6112 if (modewidth
+ len
>= pos
)
6114 enum machine_mode mode
= GET_MODE (x
);
6115 tem
= gen_lowpart (mode
, XEXP (x
, 0));
6116 if (!tem
|| GET_CODE (tem
) == CLOBBER
)
6118 tem
= simplify_shift_const (NULL_RTX
, ASHIFT
, mode
,
6119 tem
, modewidth
- pos
- len
);
6120 tem
= simplify_shift_const (NULL_RTX
, unsignedp
? LSHIFTRT
: ASHIFTRT
,
6121 mode
, tem
, modewidth
- len
);
6123 else if (unsignedp
&& len
< HOST_BITS_PER_WIDE_INT
)
6124 tem
= simplify_and_const_int (NULL_RTX
, GET_MODE (x
),
6125 simplify_shift_const (NULL_RTX
, LSHIFTRT
,
6128 ((HOST_WIDE_INT
) 1 << len
) - 1);
6130 /* Any other cases we can't handle. */
6133 /* If we couldn't do this for some reason, return the original
6135 if (GET_CODE (tem
) == CLOBBER
)
6141 /* X is a SET which contains an assignment of one object into
6142 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6143 or certain SUBREGS). If possible, convert it into a series of
6146 We half-heartedly support variable positions, but do not at all
6147 support variable lengths. */
6150 expand_field_assignment (rtx x
)
6153 rtx pos
; /* Always counts from low bit. */
6155 rtx mask
, cleared
, masked
;
6156 enum machine_mode compute_mode
;
6158 /* Loop until we find something we can't simplify. */
6161 if (GET_CODE (SET_DEST (x
)) == STRICT_LOW_PART
6162 && GET_CODE (XEXP (SET_DEST (x
), 0)) == SUBREG
)
6164 inner
= SUBREG_REG (XEXP (SET_DEST (x
), 0));
6165 len
= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x
), 0)));
6166 pos
= GEN_INT (subreg_lsb (XEXP (SET_DEST (x
), 0)));
6168 else if (GET_CODE (SET_DEST (x
)) == ZERO_EXTRACT
6169 && GET_CODE (XEXP (SET_DEST (x
), 1)) == CONST_INT
)
6171 inner
= XEXP (SET_DEST (x
), 0);
6172 len
= INTVAL (XEXP (SET_DEST (x
), 1));
6173 pos
= XEXP (SET_DEST (x
), 2);
6175 /* A constant position should stay within the width of INNER. */
6176 if (GET_CODE (pos
) == CONST_INT
6177 && INTVAL (pos
) + len
> GET_MODE_BITSIZE (GET_MODE (inner
)))
6180 if (BITS_BIG_ENDIAN
)
6182 if (GET_CODE (pos
) == CONST_INT
)
6183 pos
= GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner
)) - len
6185 else if (GET_CODE (pos
) == MINUS
6186 && GET_CODE (XEXP (pos
, 1)) == CONST_INT
6187 && (INTVAL (XEXP (pos
, 1))
6188 == GET_MODE_BITSIZE (GET_MODE (inner
)) - len
))
6189 /* If position is ADJUST - X, new position is X. */
6190 pos
= XEXP (pos
, 0);
6192 pos
= simplify_gen_binary (MINUS
, GET_MODE (pos
),
6193 GEN_INT (GET_MODE_BITSIZE (
6200 /* A SUBREG between two modes that occupy the same numbers of words
6201 can be done by moving the SUBREG to the source. */
6202 else if (GET_CODE (SET_DEST (x
)) == SUBREG
6203 /* We need SUBREGs to compute nonzero_bits properly. */
6204 && nonzero_sign_valid
6205 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x
)))
6206 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
)
6207 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x
))))
6208 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
)))
6210 x
= gen_rtx_SET (VOIDmode
, SUBREG_REG (SET_DEST (x
)),
6212 (GET_MODE (SUBREG_REG (SET_DEST (x
))),
6219 while (GET_CODE (inner
) == SUBREG
&& subreg_lowpart_p (inner
))
6220 inner
= SUBREG_REG (inner
);
6222 compute_mode
= GET_MODE (inner
);
6224 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6225 if (! SCALAR_INT_MODE_P (compute_mode
))
6227 enum machine_mode imode
;
6229 /* Don't do anything for vector or complex integral types. */
6230 if (! FLOAT_MODE_P (compute_mode
))
6233 /* Try to find an integral mode to pun with. */
6234 imode
= mode_for_size (GET_MODE_BITSIZE (compute_mode
), MODE_INT
, 0);
6235 if (imode
== BLKmode
)
6238 compute_mode
= imode
;
6239 inner
= gen_lowpart (imode
, inner
);
6242 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6243 if (len
>= HOST_BITS_PER_WIDE_INT
)
6246 /* Now compute the equivalent expression. Make a copy of INNER
6247 for the SET_DEST in case it is a MEM into which we will substitute;
6248 we don't want shared RTL in that case. */
6249 mask
= GEN_INT (((HOST_WIDE_INT
) 1 << len
) - 1);
6250 cleared
= simplify_gen_binary (AND
, compute_mode
,
6251 simplify_gen_unary (NOT
, compute_mode
,
6252 simplify_gen_binary (ASHIFT
,
6257 masked
= simplify_gen_binary (ASHIFT
, compute_mode
,
6258 simplify_gen_binary (
6260 gen_lowpart (compute_mode
, SET_SRC (x
)),
6264 x
= gen_rtx_SET (VOIDmode
, copy_rtx (inner
),
6265 simplify_gen_binary (IOR
, compute_mode
,
6272 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6273 it is an RTX that represents a variable starting position; otherwise,
6274 POS is the (constant) starting bit position (counted from the LSB).
6276 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6279 IN_DEST is nonzero if this is a reference in the destination of a
6280 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6281 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6284 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6285 ZERO_EXTRACT should be built even for bits starting at bit 0.
6287 MODE is the desired mode of the result (if IN_DEST == 0).
6289 The result is an RTX for the extraction or NULL_RTX if the target
6293 make_extraction (enum machine_mode mode
, rtx inner
, HOST_WIDE_INT pos
,
6294 rtx pos_rtx
, unsigned HOST_WIDE_INT len
, int unsignedp
,
6295 int in_dest
, int in_compare
)
6297 /* This mode describes the size of the storage area
6298 to fetch the overall value from. Within that, we
6299 ignore the POS lowest bits, etc. */
6300 enum machine_mode is_mode
= GET_MODE (inner
);
6301 enum machine_mode inner_mode
;
6302 enum machine_mode wanted_inner_mode
;
6303 enum machine_mode wanted_inner_reg_mode
= word_mode
;
6304 enum machine_mode pos_mode
= word_mode
;
6305 enum machine_mode extraction_mode
= word_mode
;
6306 enum machine_mode tmode
= mode_for_size (len
, MODE_INT
, 1);
6308 rtx orig_pos_rtx
= pos_rtx
;
6309 HOST_WIDE_INT orig_pos
;
6311 if (GET_CODE (inner
) == SUBREG
&& subreg_lowpart_p (inner
))
6313 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6314 consider just the QI as the memory to extract from.
6315 The subreg adds or removes high bits; its mode is
6316 irrelevant to the meaning of this extraction,
6317 since POS and LEN count from the lsb. */
6318 if (MEM_P (SUBREG_REG (inner
)))
6319 is_mode
= GET_MODE (SUBREG_REG (inner
));
6320 inner
= SUBREG_REG (inner
);
6322 else if (GET_CODE (inner
) == ASHIFT
6323 && GET_CODE (XEXP (inner
, 1)) == CONST_INT
6324 && pos_rtx
== 0 && pos
== 0
6325 && len
> (unsigned HOST_WIDE_INT
) INTVAL (XEXP (inner
, 1)))
6327 /* We're extracting the least significant bits of an rtx
6328 (ashift X (const_int C)), where LEN > C. Extract the
6329 least significant (LEN - C) bits of X, giving an rtx
6330 whose mode is MODE, then shift it left C times. */
6331 new = make_extraction (mode
, XEXP (inner
, 0),
6332 0, 0, len
- INTVAL (XEXP (inner
, 1)),
6333 unsignedp
, in_dest
, in_compare
);
6335 return gen_rtx_ASHIFT (mode
, new, XEXP (inner
, 1));
6338 inner_mode
= GET_MODE (inner
);
6340 if (pos_rtx
&& GET_CODE (pos_rtx
) == CONST_INT
)
6341 pos
= INTVAL (pos_rtx
), pos_rtx
= 0;
6343 /* See if this can be done without an extraction. We never can if the
6344 width of the field is not the same as that of some integer mode. For
6345 registers, we can only avoid the extraction if the position is at the
6346 low-order bit and this is either not in the destination or we have the
6347 appropriate STRICT_LOW_PART operation available.
6349 For MEM, we can avoid an extract if the field starts on an appropriate
6350 boundary and we can change the mode of the memory reference. */
6352 if (tmode
!= BLKmode
6353 && ((pos_rtx
== 0 && (pos
% BITS_PER_WORD
) == 0
6355 && (inner_mode
== tmode
6357 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode
),
6358 GET_MODE_BITSIZE (inner_mode
))
6359 || reg_truncated_to_mode (tmode
, inner
))
6362 && have_insn_for (STRICT_LOW_PART
, tmode
))))
6363 || (MEM_P (inner
) && pos_rtx
== 0
6365 % (STRICT_ALIGNMENT
? GET_MODE_ALIGNMENT (tmode
)
6366 : BITS_PER_UNIT
)) == 0
6367 /* We can't do this if we are widening INNER_MODE (it
6368 may not be aligned, for one thing). */
6369 && GET_MODE_BITSIZE (inner_mode
) >= GET_MODE_BITSIZE (tmode
)
6370 && (inner_mode
== tmode
6371 || (! mode_dependent_address_p (XEXP (inner
, 0))
6372 && ! MEM_VOLATILE_P (inner
))))))
6374 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6375 field. If the original and current mode are the same, we need not
6376 adjust the offset. Otherwise, we do if bytes big endian.
6378 If INNER is not a MEM, get a piece consisting of just the field
6379 of interest (in this case POS % BITS_PER_WORD must be 0). */
6383 HOST_WIDE_INT offset
;
6385 /* POS counts from lsb, but make OFFSET count in memory order. */
6386 if (BYTES_BIG_ENDIAN
)
6387 offset
= (GET_MODE_BITSIZE (is_mode
) - len
- pos
) / BITS_PER_UNIT
;
6389 offset
= pos
/ BITS_PER_UNIT
;
6391 new = adjust_address_nv (inner
, tmode
, offset
);
6393 else if (REG_P (inner
))
6395 if (tmode
!= inner_mode
)
6397 /* We can't call gen_lowpart in a DEST since we
6398 always want a SUBREG (see below) and it would sometimes
6399 return a new hard register. */
6402 HOST_WIDE_INT final_word
= pos
/ BITS_PER_WORD
;
6404 if (WORDS_BIG_ENDIAN
6405 && GET_MODE_SIZE (inner_mode
) > UNITS_PER_WORD
)
6406 final_word
= ((GET_MODE_SIZE (inner_mode
)
6407 - GET_MODE_SIZE (tmode
))
6408 / UNITS_PER_WORD
) - final_word
;
6410 final_word
*= UNITS_PER_WORD
;
6411 if (BYTES_BIG_ENDIAN
&&
6412 GET_MODE_SIZE (inner_mode
) > GET_MODE_SIZE (tmode
))
6413 final_word
+= (GET_MODE_SIZE (inner_mode
)
6414 - GET_MODE_SIZE (tmode
)) % UNITS_PER_WORD
;
6416 /* Avoid creating invalid subregs, for example when
6417 simplifying (x>>32)&255. */
6418 if (!validate_subreg (tmode
, inner_mode
, inner
, final_word
))
6421 new = gen_rtx_SUBREG (tmode
, inner
, final_word
);
6424 new = gen_lowpart (tmode
, inner
);
6430 new = force_to_mode (inner
, tmode
,
6431 len
>= HOST_BITS_PER_WIDE_INT
6432 ? ~(unsigned HOST_WIDE_INT
) 0
6433 : ((unsigned HOST_WIDE_INT
) 1 << len
) - 1,
6436 /* If this extraction is going into the destination of a SET,
6437 make a STRICT_LOW_PART unless we made a MEM. */
6440 return (MEM_P (new) ? new
6441 : (GET_CODE (new) != SUBREG
6442 ? gen_rtx_CLOBBER (tmode
, const0_rtx
)
6443 : gen_rtx_STRICT_LOW_PART (VOIDmode
, new)));
6448 if (GET_CODE (new) == CONST_INT
)
6449 return gen_int_mode (INTVAL (new), mode
);
6451 /* If we know that no extraneous bits are set, and that the high
6452 bit is not set, convert the extraction to the cheaper of
6453 sign and zero extension, that are equivalent in these cases. */
6454 if (flag_expensive_optimizations
6455 && (GET_MODE_BITSIZE (tmode
) <= HOST_BITS_PER_WIDE_INT
6456 && ((nonzero_bits (new, tmode
)
6457 & ~(((unsigned HOST_WIDE_INT
)
6458 GET_MODE_MASK (tmode
))
6462 rtx temp
= gen_rtx_ZERO_EXTEND (mode
, new);
6463 rtx temp1
= gen_rtx_SIGN_EXTEND (mode
, new);
6465 /* Prefer ZERO_EXTENSION, since it gives more information to
6467 if (rtx_cost (temp
, SET
) <= rtx_cost (temp1
, SET
))
6472 /* Otherwise, sign- or zero-extend unless we already are in the
6475 return (gen_rtx_fmt_e (unsignedp
? ZERO_EXTEND
: SIGN_EXTEND
,
6479 /* Unless this is a COMPARE or we have a funny memory reference,
6480 don't do anything with zero-extending field extracts starting at
6481 the low-order bit since they are simple AND operations. */
6482 if (pos_rtx
== 0 && pos
== 0 && ! in_dest
6483 && ! in_compare
&& unsignedp
)
6486 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6487 if the position is not a constant and the length is not 1. In all
6488 other cases, we would only be going outside our object in cases when
6489 an original shift would have been undefined. */
6491 && ((pos_rtx
== 0 && pos
+ len
> GET_MODE_BITSIZE (is_mode
))
6492 || (pos_rtx
!= 0 && len
!= 1)))
6495 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6496 and the mode for the result. */
6497 if (in_dest
&& mode_for_extraction (EP_insv
, -1) != MAX_MACHINE_MODE
)
6499 wanted_inner_reg_mode
= mode_for_extraction (EP_insv
, 0);
6500 pos_mode
= mode_for_extraction (EP_insv
, 2);
6501 extraction_mode
= mode_for_extraction (EP_insv
, 3);
6504 if (! in_dest
&& unsignedp
6505 && mode_for_extraction (EP_extzv
, -1) != MAX_MACHINE_MODE
)
6507 wanted_inner_reg_mode
= mode_for_extraction (EP_extzv
, 1);
6508 pos_mode
= mode_for_extraction (EP_extzv
, 3);
6509 extraction_mode
= mode_for_extraction (EP_extzv
, 0);
6512 if (! in_dest
&& ! unsignedp
6513 && mode_for_extraction (EP_extv
, -1) != MAX_MACHINE_MODE
)
6515 wanted_inner_reg_mode
= mode_for_extraction (EP_extv
, 1);
6516 pos_mode
= mode_for_extraction (EP_extv
, 3);
6517 extraction_mode
= mode_for_extraction (EP_extv
, 0);
6520 /* Never narrow an object, since that might not be safe. */
6522 if (mode
!= VOIDmode
6523 && GET_MODE_SIZE (extraction_mode
) < GET_MODE_SIZE (mode
))
6524 extraction_mode
= mode
;
6526 if (pos_rtx
&& GET_MODE (pos_rtx
) != VOIDmode
6527 && GET_MODE_SIZE (pos_mode
) < GET_MODE_SIZE (GET_MODE (pos_rtx
)))
6528 pos_mode
= GET_MODE (pos_rtx
);
6530 /* If this is not from memory, the desired mode is the preferred mode
6531 for an extraction pattern's first input operand, or word_mode if there
6534 wanted_inner_mode
= wanted_inner_reg_mode
;
6537 /* Be careful not to go beyond the extracted object and maintain the
6538 natural alignment of the memory. */
6539 wanted_inner_mode
= smallest_mode_for_size (len
, MODE_INT
);
6540 while (pos
% GET_MODE_BITSIZE (wanted_inner_mode
) + len
6541 > GET_MODE_BITSIZE (wanted_inner_mode
))
6543 wanted_inner_mode
= GET_MODE_WIDER_MODE (wanted_inner_mode
);
6544 gcc_assert (wanted_inner_mode
!= VOIDmode
);
6547 /* If we have to change the mode of memory and cannot, the desired mode
6548 is EXTRACTION_MODE. */
6549 if (inner_mode
!= wanted_inner_mode
6550 && (mode_dependent_address_p (XEXP (inner
, 0))
6551 || MEM_VOLATILE_P (inner
)
6553 wanted_inner_mode
= extraction_mode
;
6558 if (BITS_BIG_ENDIAN
)
6560 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6561 BITS_BIG_ENDIAN style. If position is constant, compute new
6562 position. Otherwise, build subtraction.
6563 Note that POS is relative to the mode of the original argument.
6564 If it's a MEM we need to recompute POS relative to that.
6565 However, if we're extracting from (or inserting into) a register,
6566 we want to recompute POS relative to wanted_inner_mode. */
6567 int width
= (MEM_P (inner
)
6568 ? GET_MODE_BITSIZE (is_mode
)
6569 : GET_MODE_BITSIZE (wanted_inner_mode
));
6572 pos
= width
- len
- pos
;
6575 = gen_rtx_MINUS (GET_MODE (pos_rtx
), GEN_INT (width
- len
), pos_rtx
);
6576 /* POS may be less than 0 now, but we check for that below.
6577 Note that it can only be less than 0 if !MEM_P (inner). */
6580 /* If INNER has a wider mode, and this is a constant extraction, try to
6581 make it smaller and adjust the byte to point to the byte containing
6583 if (wanted_inner_mode
!= VOIDmode
6584 && inner_mode
!= wanted_inner_mode
6586 && GET_MODE_SIZE (wanted_inner_mode
) < GET_MODE_SIZE (is_mode
)
6588 && ! mode_dependent_address_p (XEXP (inner
, 0))
6589 && ! MEM_VOLATILE_P (inner
))
6593 /* The computations below will be correct if the machine is big
6594 endian in both bits and bytes or little endian in bits and bytes.
6595 If it is mixed, we must adjust. */
6597 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6598 adjust OFFSET to compensate. */
6599 if (BYTES_BIG_ENDIAN
6600 && GET_MODE_SIZE (inner_mode
) < GET_MODE_SIZE (is_mode
))
6601 offset
-= GET_MODE_SIZE (is_mode
) - GET_MODE_SIZE (inner_mode
);
6603 /* We can now move to the desired byte. */
6604 offset
+= (pos
/ GET_MODE_BITSIZE (wanted_inner_mode
))
6605 * GET_MODE_SIZE (wanted_inner_mode
);
6606 pos
%= GET_MODE_BITSIZE (wanted_inner_mode
);
6608 if (BYTES_BIG_ENDIAN
!= BITS_BIG_ENDIAN
6609 && is_mode
!= wanted_inner_mode
)
6610 offset
= (GET_MODE_SIZE (is_mode
)
6611 - GET_MODE_SIZE (wanted_inner_mode
) - offset
);
6613 inner
= adjust_address_nv (inner
, wanted_inner_mode
, offset
);
6616 /* If INNER is not memory, we can always get it into the proper mode. If we
6617 are changing its mode, POS must be a constant and smaller than the size
6619 else if (!MEM_P (inner
))
6621 if (GET_MODE (inner
) != wanted_inner_mode
6623 || orig_pos
+ len
> GET_MODE_BITSIZE (wanted_inner_mode
)))
6629 inner
= force_to_mode (inner
, wanted_inner_mode
,
6631 || len
+ orig_pos
>= HOST_BITS_PER_WIDE_INT
6632 ? ~(unsigned HOST_WIDE_INT
) 0
6633 : ((((unsigned HOST_WIDE_INT
) 1 << len
) - 1)
6638 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6639 have to zero extend. Otherwise, we can just use a SUBREG. */
6641 && GET_MODE_SIZE (pos_mode
) > GET_MODE_SIZE (GET_MODE (pos_rtx
)))
6643 rtx temp
= gen_rtx_ZERO_EXTEND (pos_mode
, pos_rtx
);
6645 /* If we know that no extraneous bits are set, and that the high
6646 bit is not set, convert extraction to cheaper one - either
6647 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6649 if (flag_expensive_optimizations
6650 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx
)) <= HOST_BITS_PER_WIDE_INT
6651 && ((nonzero_bits (pos_rtx
, GET_MODE (pos_rtx
))
6652 & ~(((unsigned HOST_WIDE_INT
)
6653 GET_MODE_MASK (GET_MODE (pos_rtx
)))
6657 rtx temp1
= gen_rtx_SIGN_EXTEND (pos_mode
, pos_rtx
);
6659 /* Prefer ZERO_EXTENSION, since it gives more information to
6661 if (rtx_cost (temp1
, SET
) < rtx_cost (temp
, SET
))
6666 else if (pos_rtx
!= 0
6667 && GET_MODE_SIZE (pos_mode
) < GET_MODE_SIZE (GET_MODE (pos_rtx
)))
6668 pos_rtx
= gen_lowpart (pos_mode
, pos_rtx
);
6670 /* Make POS_RTX unless we already have it and it is correct. If we don't
6671 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6673 if (pos_rtx
== 0 && orig_pos_rtx
!= 0 && INTVAL (orig_pos_rtx
) == pos
)
6674 pos_rtx
= orig_pos_rtx
;
6676 else if (pos_rtx
== 0)
6677 pos_rtx
= GEN_INT (pos
);
6679 /* Make the required operation. See if we can use existing rtx. */
6680 new = gen_rtx_fmt_eee (unsignedp
? ZERO_EXTRACT
: SIGN_EXTRACT
,
6681 extraction_mode
, inner
, GEN_INT (len
), pos_rtx
);
6683 new = gen_lowpart (mode
, new);
6688 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6689 with any other operations in X. Return X without that shift if so. */
6692 extract_left_shift (rtx x
, int count
)
6694 enum rtx_code code
= GET_CODE (x
);
6695 enum machine_mode mode
= GET_MODE (x
);
6701 /* This is the shift itself. If it is wide enough, we will return
6702 either the value being shifted if the shift count is equal to
6703 COUNT or a shift for the difference. */
6704 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
6705 && INTVAL (XEXP (x
, 1)) >= count
)
6706 return simplify_shift_const (NULL_RTX
, ASHIFT
, mode
, XEXP (x
, 0),
6707 INTVAL (XEXP (x
, 1)) - count
);
6711 if ((tem
= extract_left_shift (XEXP (x
, 0), count
)) != 0)
6712 return simplify_gen_unary (code
, mode
, tem
, mode
);
6716 case PLUS
: case IOR
: case XOR
: case AND
:
6717 /* If we can safely shift this constant and we find the inner shift,
6718 make a new operation. */
6719 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
6720 && (INTVAL (XEXP (x
, 1)) & ((((HOST_WIDE_INT
) 1 << count
)) - 1)) == 0
6721 && (tem
= extract_left_shift (XEXP (x
, 0), count
)) != 0)
6722 return simplify_gen_binary (code
, mode
, tem
,
6723 GEN_INT (INTVAL (XEXP (x
, 1)) >> count
));
6734 /* Look at the expression rooted at X. Look for expressions
6735 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6736 Form these expressions.
6738 Return the new rtx, usually just X.
6740 Also, for machines like the VAX that don't have logical shift insns,
6741 try to convert logical to arithmetic shift operations in cases where
6742 they are equivalent. This undoes the canonicalizations to logical
6743 shifts done elsewhere.
6745 We try, as much as possible, to re-use rtl expressions to save memory.
6747 IN_CODE says what kind of expression we are processing. Normally, it is
6748 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6749 being kludges), it is MEM. When processing the arguments of a comparison
6750 or a COMPARE against zero, it is COMPARE. */
6753 make_compound_operation (rtx x
, enum rtx_code in_code
)
6755 enum rtx_code code
= GET_CODE (x
);
6756 enum machine_mode mode
= GET_MODE (x
);
6757 int mode_width
= GET_MODE_BITSIZE (mode
);
6759 enum rtx_code next_code
;
6765 /* Select the code to be used in recursive calls. Once we are inside an
6766 address, we stay there. If we have a comparison, set to COMPARE,
6767 but once inside, go back to our default of SET. */
6769 next_code
= (code
== MEM
|| code
== PLUS
|| code
== MINUS
? MEM
6770 : ((code
== COMPARE
|| COMPARISON_P (x
))
6771 && XEXP (x
, 1) == const0_rtx
) ? COMPARE
6772 : in_code
== COMPARE
? SET
: in_code
);
6774 /* Process depending on the code of this operation. If NEW is set
6775 nonzero, it will be returned. */
6780 /* Convert shifts by constants into multiplications if inside
6782 if (in_code
== MEM
&& GET_CODE (XEXP (x
, 1)) == CONST_INT
6783 && INTVAL (XEXP (x
, 1)) < HOST_BITS_PER_WIDE_INT
6784 && INTVAL (XEXP (x
, 1)) >= 0)
6786 new = make_compound_operation (XEXP (x
, 0), next_code
);
6787 new = gen_rtx_MULT (mode
, new,
6788 GEN_INT ((HOST_WIDE_INT
) 1
6789 << INTVAL (XEXP (x
, 1))));
6794 /* If the second operand is not a constant, we can't do anything
6796 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
6799 /* If the constant is a power of two minus one and the first operand
6800 is a logical right shift, make an extraction. */
6801 if (GET_CODE (XEXP (x
, 0)) == LSHIFTRT
6802 && (i
= exact_log2 (INTVAL (XEXP (x
, 1)) + 1)) >= 0)
6804 new = make_compound_operation (XEXP (XEXP (x
, 0), 0), next_code
);
6805 new = make_extraction (mode
, new, 0, XEXP (XEXP (x
, 0), 1), i
, 1,
6806 0, in_code
== COMPARE
);
6809 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6810 else if (GET_CODE (XEXP (x
, 0)) == SUBREG
6811 && subreg_lowpart_p (XEXP (x
, 0))
6812 && GET_CODE (SUBREG_REG (XEXP (x
, 0))) == LSHIFTRT
6813 && (i
= exact_log2 (INTVAL (XEXP (x
, 1)) + 1)) >= 0)
6815 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x
, 0)), 0),
6817 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x
, 0))), new, 0,
6818 XEXP (SUBREG_REG (XEXP (x
, 0)), 1), i
, 1,
6819 0, in_code
== COMPARE
);
6821 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6822 else if ((GET_CODE (XEXP (x
, 0)) == XOR
6823 || GET_CODE (XEXP (x
, 0)) == IOR
)
6824 && GET_CODE (XEXP (XEXP (x
, 0), 0)) == LSHIFTRT
6825 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == LSHIFTRT
6826 && (i
= exact_log2 (INTVAL (XEXP (x
, 1)) + 1)) >= 0)
6828 /* Apply the distributive law, and then try to make extractions. */
6829 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x
, 0)), mode
,
6830 gen_rtx_AND (mode
, XEXP (XEXP (x
, 0), 0),
6832 gen_rtx_AND (mode
, XEXP (XEXP (x
, 0), 1),
6834 new = make_compound_operation (new, in_code
);
6837 /* If we are have (and (rotate X C) M) and C is larger than the number
6838 of bits in M, this is an extraction. */
6840 else if (GET_CODE (XEXP (x
, 0)) == ROTATE
6841 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
6842 && (i
= exact_log2 (INTVAL (XEXP (x
, 1)) + 1)) >= 0
6843 && i
<= INTVAL (XEXP (XEXP (x
, 0), 1)))
6845 new = make_compound_operation (XEXP (XEXP (x
, 0), 0), next_code
);
6846 new = make_extraction (mode
, new,
6847 (GET_MODE_BITSIZE (mode
)
6848 - INTVAL (XEXP (XEXP (x
, 0), 1))),
6849 NULL_RTX
, i
, 1, 0, in_code
== COMPARE
);
6852 /* On machines without logical shifts, if the operand of the AND is
6853 a logical shift and our mask turns off all the propagated sign
6854 bits, we can replace the logical shift with an arithmetic shift. */
6855 else if (GET_CODE (XEXP (x
, 0)) == LSHIFTRT
6856 && !have_insn_for (LSHIFTRT
, mode
)
6857 && have_insn_for (ASHIFTRT
, mode
)
6858 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
6859 && INTVAL (XEXP (XEXP (x
, 0), 1)) >= 0
6860 && INTVAL (XEXP (XEXP (x
, 0), 1)) < HOST_BITS_PER_WIDE_INT
6861 && mode_width
<= HOST_BITS_PER_WIDE_INT
)
6863 unsigned HOST_WIDE_INT mask
= GET_MODE_MASK (mode
);
6865 mask
>>= INTVAL (XEXP (XEXP (x
, 0), 1));
6866 if ((INTVAL (XEXP (x
, 1)) & ~mask
) == 0)
6868 gen_rtx_ASHIFTRT (mode
,
6869 make_compound_operation
6870 (XEXP (XEXP (x
, 0), 0), next_code
),
6871 XEXP (XEXP (x
, 0), 1)));
6874 /* If the constant is one less than a power of two, this might be
6875 representable by an extraction even if no shift is present.
6876 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6877 we are in a COMPARE. */
6878 else if ((i
= exact_log2 (INTVAL (XEXP (x
, 1)) + 1)) >= 0)
6879 new = make_extraction (mode
,
6880 make_compound_operation (XEXP (x
, 0),
6882 0, NULL_RTX
, i
, 1, 0, in_code
== COMPARE
);
6884 /* If we are in a comparison and this is an AND with a power of two,
6885 convert this into the appropriate bit extract. */
6886 else if (in_code
== COMPARE
6887 && (i
= exact_log2 (INTVAL (XEXP (x
, 1)))) >= 0)
6888 new = make_extraction (mode
,
6889 make_compound_operation (XEXP (x
, 0),
6891 i
, NULL_RTX
, 1, 1, 0, 1);
6896 /* If the sign bit is known to be zero, replace this with an
6897 arithmetic shift. */
6898 if (have_insn_for (ASHIFTRT
, mode
)
6899 && ! have_insn_for (LSHIFTRT
, mode
)
6900 && mode_width
<= HOST_BITS_PER_WIDE_INT
6901 && (nonzero_bits (XEXP (x
, 0), mode
) & (1 << (mode_width
- 1))) == 0)
6903 new = gen_rtx_ASHIFTRT (mode
,
6904 make_compound_operation (XEXP (x
, 0),
6910 /* ... fall through ... */
6916 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6917 this is a SIGN_EXTRACT. */
6918 if (GET_CODE (rhs
) == CONST_INT
6919 && GET_CODE (lhs
) == ASHIFT
6920 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
6921 && INTVAL (rhs
) >= INTVAL (XEXP (lhs
, 1)))
6923 new = make_compound_operation (XEXP (lhs
, 0), next_code
);
6924 new = make_extraction (mode
, new,
6925 INTVAL (rhs
) - INTVAL (XEXP (lhs
, 1)),
6926 NULL_RTX
, mode_width
- INTVAL (rhs
),
6927 code
== LSHIFTRT
, 0, in_code
== COMPARE
);
6931 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6932 If so, try to merge the shifts into a SIGN_EXTEND. We could
6933 also do this for some cases of SIGN_EXTRACT, but it doesn't
6934 seem worth the effort; the case checked for occurs on Alpha. */
6937 && ! (GET_CODE (lhs
) == SUBREG
6938 && (OBJECT_P (SUBREG_REG (lhs
))))
6939 && GET_CODE (rhs
) == CONST_INT
6940 && INTVAL (rhs
) < HOST_BITS_PER_WIDE_INT
6941 && (new = extract_left_shift (lhs
, INTVAL (rhs
))) != 0)
6942 new = make_extraction (mode
, make_compound_operation (new, next_code
),
6943 0, NULL_RTX
, mode_width
- INTVAL (rhs
),
6944 code
== LSHIFTRT
, 0, in_code
== COMPARE
);
6949 /* Call ourselves recursively on the inner expression. If we are
6950 narrowing the object and it has a different RTL code from
6951 what it originally did, do this SUBREG as a force_to_mode. */
6953 tem
= make_compound_operation (SUBREG_REG (x
), in_code
);
6957 simplified
= simplify_subreg (GET_MODE (x
), tem
, GET_MODE (tem
),
6963 if (GET_CODE (tem
) != GET_CODE (SUBREG_REG (x
))
6964 && GET_MODE_SIZE (mode
) < GET_MODE_SIZE (GET_MODE (tem
))
6965 && subreg_lowpart_p (x
))
6967 rtx newer
= force_to_mode (tem
, mode
, ~(HOST_WIDE_INT
) 0,
6970 /* If we have something other than a SUBREG, we might have
6971 done an expansion, so rerun ourselves. */
6972 if (GET_CODE (newer
) != SUBREG
)
6973 newer
= make_compound_operation (newer
, in_code
);
6989 x
= gen_lowpart (mode
, new);
6990 code
= GET_CODE (x
);
6993 /* Now recursively process each operand of this operation. */
6994 fmt
= GET_RTX_FORMAT (code
);
6995 for (i
= 0; i
< GET_RTX_LENGTH (code
); i
++)
6998 new = make_compound_operation (XEXP (x
, i
), next_code
);
6999 SUBST (XEXP (x
, i
), new);
7002 /* If this is a commutative operation, the changes to the operands
7003 may have made it noncanonical. */
7004 if (COMMUTATIVE_ARITH_P (x
)
7005 && swap_commutative_operands_p (XEXP (x
, 0), XEXP (x
, 1)))
7008 SUBST (XEXP (x
, 0), XEXP (x
, 1));
7009 SUBST (XEXP (x
, 1), tem
);
7015 /* Given M see if it is a value that would select a field of bits
7016 within an item, but not the entire word. Return -1 if not.
7017 Otherwise, return the starting position of the field, where 0 is the
7020 *PLEN is set to the length of the field. */
7023 get_pos_from_mask (unsigned HOST_WIDE_INT m
, unsigned HOST_WIDE_INT
*plen
)
7025 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7026 int pos
= exact_log2 (m
& -m
);
7030 /* Now shift off the low-order zero bits and see if we have a
7031 power of two minus 1. */
7032 len
= exact_log2 ((m
>> pos
) + 1);
7041 /* If X refers to a register that equals REG in value, replace these
7042 references with REG. */
7044 canon_reg_for_combine (rtx x
, rtx reg
)
7051 enum rtx_code code
= GET_CODE (x
);
7052 switch (GET_RTX_CLASS (code
))
7055 op0
= canon_reg_for_combine (XEXP (x
, 0), reg
);
7056 if (op0
!= XEXP (x
, 0))
7057 return simplify_gen_unary (GET_CODE (x
), GET_MODE (x
), op0
,
7062 case RTX_COMM_ARITH
:
7063 op0
= canon_reg_for_combine (XEXP (x
, 0), reg
);
7064 op1
= canon_reg_for_combine (XEXP (x
, 1), reg
);
7065 if (op0
!= XEXP (x
, 0) || op1
!= XEXP (x
, 1))
7066 return simplify_gen_binary (GET_CODE (x
), GET_MODE (x
), op0
, op1
);
7070 case RTX_COMM_COMPARE
:
7071 op0
= canon_reg_for_combine (XEXP (x
, 0), reg
);
7072 op1
= canon_reg_for_combine (XEXP (x
, 1), reg
);
7073 if (op0
!= XEXP (x
, 0) || op1
!= XEXP (x
, 1))
7074 return simplify_gen_relational (GET_CODE (x
), GET_MODE (x
),
7075 GET_MODE (op0
), op0
, op1
);
7079 case RTX_BITFIELD_OPS
:
7080 op0
= canon_reg_for_combine (XEXP (x
, 0), reg
);
7081 op1
= canon_reg_for_combine (XEXP (x
, 1), reg
);
7082 op2
= canon_reg_for_combine (XEXP (x
, 2), reg
);
7083 if (op0
!= XEXP (x
, 0) || op1
!= XEXP (x
, 1) || op2
!= XEXP (x
, 2))
7084 return simplify_gen_ternary (GET_CODE (x
), GET_MODE (x
),
7085 GET_MODE (op0
), op0
, op1
, op2
);
7090 if (rtx_equal_p (get_last_value (reg
), x
)
7091 || rtx_equal_p (reg
, get_last_value (x
)))
7100 fmt
= GET_RTX_FORMAT (code
);
7102 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
7105 rtx op
= canon_reg_for_combine (XEXP (x
, i
), reg
);
7106 if (op
!= XEXP (x
, i
))
7116 else if (fmt
[i
] == 'E')
7119 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
7121 rtx op
= canon_reg_for_combine (XVECEXP (x
, i
, j
), reg
);
7122 if (op
!= XVECEXP (x
, i
, j
))
7129 XVECEXP (x
, i
, j
) = op
;
7140 /* Return X converted to MODE. If the value is already truncated to
7141 MODE we can just return a subreg even though in the general case we
7142 would need an explicit truncation. */
7145 gen_lowpart_or_truncate (enum machine_mode mode
, rtx x
)
7147 if (GET_MODE_SIZE (GET_MODE (x
)) <= GET_MODE_SIZE (mode
)
7148 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode
),
7149 GET_MODE_BITSIZE (GET_MODE (x
)))
7150 || (REG_P (x
) && reg_truncated_to_mode (mode
, x
)))
7151 return gen_lowpart (mode
, x
);
7153 return simplify_gen_unary (TRUNCATE
, mode
, x
, GET_MODE (x
));
7156 /* See if X can be simplified knowing that we will only refer to it in
7157 MODE and will only refer to those bits that are nonzero in MASK.
7158 If other bits are being computed or if masking operations are done
7159 that select a superset of the bits in MASK, they can sometimes be
7162 Return a possibly simplified expression, but always convert X to
7163 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7165 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7166 are all off in X. This is used when X will be complemented, by either
7167 NOT, NEG, or XOR. */
7170 force_to_mode (rtx x
, enum machine_mode mode
, unsigned HOST_WIDE_INT mask
,
7173 enum rtx_code code
= GET_CODE (x
);
7174 int next_select
= just_select
|| code
== XOR
|| code
== NOT
|| code
== NEG
;
7175 enum machine_mode op_mode
;
7176 unsigned HOST_WIDE_INT fuller_mask
, nonzero
;
7179 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7180 code below will do the wrong thing since the mode of such an
7181 expression is VOIDmode.
7183 Also do nothing if X is a CLOBBER; this can happen if X was
7184 the return value from a call to gen_lowpart. */
7185 if (code
== CALL
|| code
== ASM_OPERANDS
|| code
== CLOBBER
)
7188 /* We want to perform the operation is its present mode unless we know
7189 that the operation is valid in MODE, in which case we do the operation
7191 op_mode
= ((GET_MODE_CLASS (mode
) == GET_MODE_CLASS (GET_MODE (x
))
7192 && have_insn_for (code
, mode
))
7193 ? mode
: GET_MODE (x
));
7195 /* It is not valid to do a right-shift in a narrower mode
7196 than the one it came in with. */
7197 if ((code
== LSHIFTRT
|| code
== ASHIFTRT
)
7198 && GET_MODE_BITSIZE (mode
) < GET_MODE_BITSIZE (GET_MODE (x
)))
7199 op_mode
= GET_MODE (x
);
7201 /* Truncate MASK to fit OP_MODE. */
7203 mask
&= GET_MODE_MASK (op_mode
);
7205 /* When we have an arithmetic operation, or a shift whose count we
7206 do not know, we need to assume that all bits up to the highest-order
7207 bit in MASK will be needed. This is how we form such a mask. */
7208 if (mask
& ((unsigned HOST_WIDE_INT
) 1 << (HOST_BITS_PER_WIDE_INT
- 1)))
7209 fuller_mask
= ~(unsigned HOST_WIDE_INT
) 0;
7211 fuller_mask
= (((unsigned HOST_WIDE_INT
) 1 << (floor_log2 (mask
) + 1))
7214 /* Determine what bits of X are guaranteed to be (non)zero. */
7215 nonzero
= nonzero_bits (x
, mode
);
7217 /* If none of the bits in X are needed, return a zero. */
7218 if (!just_select
&& (nonzero
& mask
) == 0 && !side_effects_p (x
))
7221 /* If X is a CONST_INT, return a new one. Do this here since the
7222 test below will fail. */
7223 if (GET_CODE (x
) == CONST_INT
)
7225 if (SCALAR_INT_MODE_P (mode
))
7226 return gen_int_mode (INTVAL (x
) & mask
, mode
);
7229 x
= GEN_INT (INTVAL (x
) & mask
);
7230 return gen_lowpart_common (mode
, x
);
7234 /* If X is narrower than MODE and we want all the bits in X's mode, just
7235 get X in the proper mode. */
7236 if (GET_MODE_SIZE (GET_MODE (x
)) < GET_MODE_SIZE (mode
)
7237 && (GET_MODE_MASK (GET_MODE (x
)) & ~mask
) == 0)
7238 return gen_lowpart (mode
, x
);
7243 /* If X is a (clobber (const_int)), return it since we know we are
7244 generating something that won't match. */
7251 x
= expand_compound_operation (x
);
7252 if (GET_CODE (x
) != code
)
7253 return force_to_mode (x
, mode
, mask
, next_select
);
7257 if (subreg_lowpart_p (x
)
7258 /* We can ignore the effect of this SUBREG if it narrows the mode or
7259 if the constant masks to zero all the bits the mode doesn't
7261 && ((GET_MODE_SIZE (GET_MODE (x
))
7262 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x
))))
7264 & GET_MODE_MASK (GET_MODE (x
))
7265 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x
)))))))
7266 return force_to_mode (SUBREG_REG (x
), mode
, mask
, next_select
);
7270 /* If this is an AND with a constant, convert it into an AND
7271 whose constant is the AND of that constant with MASK. If it
7272 remains an AND of MASK, delete it since it is redundant. */
7274 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
)
7276 x
= simplify_and_const_int (x
, op_mode
, XEXP (x
, 0),
7277 mask
& INTVAL (XEXP (x
, 1)));
7279 /* If X is still an AND, see if it is an AND with a mask that
7280 is just some low-order bits. If so, and it is MASK, we don't
7283 if (GET_CODE (x
) == AND
&& GET_CODE (XEXP (x
, 1)) == CONST_INT
7284 && ((INTVAL (XEXP (x
, 1)) & GET_MODE_MASK (GET_MODE (x
)))
7288 /* If it remains an AND, try making another AND with the bits
7289 in the mode mask that aren't in MASK turned on. If the
7290 constant in the AND is wide enough, this might make a
7291 cheaper constant. */
7293 if (GET_CODE (x
) == AND
&& GET_CODE (XEXP (x
, 1)) == CONST_INT
7294 && GET_MODE_MASK (GET_MODE (x
)) != mask
7295 && GET_MODE_BITSIZE (GET_MODE (x
)) <= HOST_BITS_PER_WIDE_INT
)
7297 HOST_WIDE_INT cval
= (INTVAL (XEXP (x
, 1))
7298 | (GET_MODE_MASK (GET_MODE (x
)) & ~mask
));
7299 int width
= GET_MODE_BITSIZE (GET_MODE (x
));
7302 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7303 number, sign extend it. */
7304 if (width
> 0 && width
< HOST_BITS_PER_WIDE_INT
7305 && (cval
& ((HOST_WIDE_INT
) 1 << (width
- 1))) != 0)
7306 cval
|= (HOST_WIDE_INT
) -1 << width
;
7308 y
= simplify_gen_binary (AND
, GET_MODE (x
),
7309 XEXP (x
, 0), GEN_INT (cval
));
7310 if (rtx_cost (y
, SET
) < rtx_cost (x
, SET
))
7320 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7321 low-order bits (as in an alignment operation) and FOO is already
7322 aligned to that boundary, mask C1 to that boundary as well.
7323 This may eliminate that PLUS and, later, the AND. */
7326 unsigned int width
= GET_MODE_BITSIZE (mode
);
7327 unsigned HOST_WIDE_INT smask
= mask
;
7329 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7330 number, sign extend it. */
7332 if (width
< HOST_BITS_PER_WIDE_INT
7333 && (smask
& ((HOST_WIDE_INT
) 1 << (width
- 1))) != 0)
7334 smask
|= (HOST_WIDE_INT
) -1 << width
;
7336 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
7337 && exact_log2 (- smask
) >= 0
7338 && (nonzero_bits (XEXP (x
, 0), mode
) & ~smask
) == 0
7339 && (INTVAL (XEXP (x
, 1)) & ~smask
) != 0)
7340 return force_to_mode (plus_constant (XEXP (x
, 0),
7341 (INTVAL (XEXP (x
, 1)) & smask
)),
7342 mode
, smask
, next_select
);
7345 /* ... fall through ... */
7348 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7349 most significant bit in MASK since carries from those bits will
7350 affect the bits we are interested in. */
7355 /* If X is (minus C Y) where C's least set bit is larger than any bit
7356 in the mask, then we may replace with (neg Y). */
7357 if (GET_CODE (XEXP (x
, 0)) == CONST_INT
7358 && (((unsigned HOST_WIDE_INT
) (INTVAL (XEXP (x
, 0))
7359 & -INTVAL (XEXP (x
, 0))))
7362 x
= simplify_gen_unary (NEG
, GET_MODE (x
), XEXP (x
, 1),
7364 return force_to_mode (x
, mode
, mask
, next_select
);
7367 /* Similarly, if C contains every bit in the fuller_mask, then we may
7368 replace with (not Y). */
7369 if (GET_CODE (XEXP (x
, 0)) == CONST_INT
7370 && ((INTVAL (XEXP (x
, 0)) | (HOST_WIDE_INT
) fuller_mask
)
7371 == INTVAL (XEXP (x
, 0))))
7373 x
= simplify_gen_unary (NOT
, GET_MODE (x
),
7374 XEXP (x
, 1), GET_MODE (x
));
7375 return force_to_mode (x
, mode
, mask
, next_select
);
7383 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7384 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7385 operation which may be a bitfield extraction. Ensure that the
7386 constant we form is not wider than the mode of X. */
7388 if (GET_CODE (XEXP (x
, 0)) == LSHIFTRT
7389 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
7390 && INTVAL (XEXP (XEXP (x
, 0), 1)) >= 0
7391 && INTVAL (XEXP (XEXP (x
, 0), 1)) < HOST_BITS_PER_WIDE_INT
7392 && GET_CODE (XEXP (x
, 1)) == CONST_INT
7393 && ((INTVAL (XEXP (XEXP (x
, 0), 1))
7394 + floor_log2 (INTVAL (XEXP (x
, 1))))
7395 < GET_MODE_BITSIZE (GET_MODE (x
)))
7396 && (INTVAL (XEXP (x
, 1))
7397 & ~nonzero_bits (XEXP (x
, 0), GET_MODE (x
))) == 0)
7399 temp
= GEN_INT ((INTVAL (XEXP (x
, 1)) & mask
)
7400 << INTVAL (XEXP (XEXP (x
, 0), 1)));
7401 temp
= simplify_gen_binary (GET_CODE (x
), GET_MODE (x
),
7402 XEXP (XEXP (x
, 0), 0), temp
);
7403 x
= simplify_gen_binary (LSHIFTRT
, GET_MODE (x
), temp
,
7404 XEXP (XEXP (x
, 0), 1));
7405 return force_to_mode (x
, mode
, mask
, next_select
);
7409 /* For most binary operations, just propagate into the operation and
7410 change the mode if we have an operation of that mode. */
7412 op0
= gen_lowpart_or_truncate (op_mode
,
7413 force_to_mode (XEXP (x
, 0), mode
, mask
,
7415 op1
= gen_lowpart_or_truncate (op_mode
,
7416 force_to_mode (XEXP (x
, 1), mode
, mask
,
7419 if (op_mode
!= GET_MODE (x
) || op0
!= XEXP (x
, 0) || op1
!= XEXP (x
, 1))
7420 x
= simplify_gen_binary (code
, op_mode
, op0
, op1
);
7424 /* For left shifts, do the same, but just for the first operand.
7425 However, we cannot do anything with shifts where we cannot
7426 guarantee that the counts are smaller than the size of the mode
7427 because such a count will have a different meaning in a
7430 if (! (GET_CODE (XEXP (x
, 1)) == CONST_INT
7431 && INTVAL (XEXP (x
, 1)) >= 0
7432 && INTVAL (XEXP (x
, 1)) < GET_MODE_BITSIZE (mode
))
7433 && ! (GET_MODE (XEXP (x
, 1)) != VOIDmode
7434 && (nonzero_bits (XEXP (x
, 1), GET_MODE (XEXP (x
, 1)))
7435 < (unsigned HOST_WIDE_INT
) GET_MODE_BITSIZE (mode
))))
7438 /* If the shift count is a constant and we can do arithmetic in
7439 the mode of the shift, refine which bits we need. Otherwise, use the
7440 conservative form of the mask. */
7441 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
7442 && INTVAL (XEXP (x
, 1)) >= 0
7443 && INTVAL (XEXP (x
, 1)) < GET_MODE_BITSIZE (op_mode
)
7444 && GET_MODE_BITSIZE (op_mode
) <= HOST_BITS_PER_WIDE_INT
)
7445 mask
>>= INTVAL (XEXP (x
, 1));
7449 op0
= gen_lowpart_or_truncate (op_mode
,
7450 force_to_mode (XEXP (x
, 0), op_mode
,
7451 mask
, next_select
));
7453 if (op_mode
!= GET_MODE (x
) || op0
!= XEXP (x
, 0))
7454 x
= simplify_gen_binary (code
, op_mode
, op0
, XEXP (x
, 1));
7458 /* Here we can only do something if the shift count is a constant,
7459 this shift constant is valid for the host, and we can do arithmetic
7462 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
7463 && INTVAL (XEXP (x
, 1)) < HOST_BITS_PER_WIDE_INT
7464 && GET_MODE_BITSIZE (op_mode
) <= HOST_BITS_PER_WIDE_INT
)
7466 rtx inner
= XEXP (x
, 0);
7467 unsigned HOST_WIDE_INT inner_mask
;
7469 /* Select the mask of the bits we need for the shift operand. */
7470 inner_mask
= mask
<< INTVAL (XEXP (x
, 1));
7472 /* We can only change the mode of the shift if we can do arithmetic
7473 in the mode of the shift and INNER_MASK is no wider than the
7474 width of X's mode. */
7475 if ((inner_mask
& ~GET_MODE_MASK (GET_MODE (x
))) != 0)
7476 op_mode
= GET_MODE (x
);
7478 inner
= force_to_mode (inner
, op_mode
, inner_mask
, next_select
);
7480 if (GET_MODE (x
) != op_mode
|| inner
!= XEXP (x
, 0))
7481 x
= simplify_gen_binary (LSHIFTRT
, op_mode
, inner
, XEXP (x
, 1));
7484 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7485 shift and AND produces only copies of the sign bit (C2 is one less
7486 than a power of two), we can do this with just a shift. */
7488 if (GET_CODE (x
) == LSHIFTRT
7489 && GET_CODE (XEXP (x
, 1)) == CONST_INT
7490 /* The shift puts one of the sign bit copies in the least significant
7492 && ((INTVAL (XEXP (x
, 1))
7493 + num_sign_bit_copies (XEXP (x
, 0), GET_MODE (XEXP (x
, 0))))
7494 >= GET_MODE_BITSIZE (GET_MODE (x
)))
7495 && exact_log2 (mask
+ 1) >= 0
7496 /* Number of bits left after the shift must be more than the mask
7498 && ((INTVAL (XEXP (x
, 1)) + exact_log2 (mask
+ 1))
7499 <= GET_MODE_BITSIZE (GET_MODE (x
)))
7500 /* Must be more sign bit copies than the mask needs. */
7501 && ((int) num_sign_bit_copies (XEXP (x
, 0), GET_MODE (XEXP (x
, 0)))
7502 >= exact_log2 (mask
+ 1)))
7503 x
= simplify_gen_binary (LSHIFTRT
, GET_MODE (x
), XEXP (x
, 0),
7504 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x
))
7505 - exact_log2 (mask
+ 1)));
7510 /* If we are just looking for the sign bit, we don't need this shift at
7511 all, even if it has a variable count. */
7512 if (GET_MODE_BITSIZE (GET_MODE (x
)) <= HOST_BITS_PER_WIDE_INT
7513 && (mask
== ((unsigned HOST_WIDE_INT
) 1
7514 << (GET_MODE_BITSIZE (GET_MODE (x
)) - 1))))
7515 return force_to_mode (XEXP (x
, 0), mode
, mask
, next_select
);
7517 /* If this is a shift by a constant, get a mask that contains those bits
7518 that are not copies of the sign bit. We then have two cases: If
7519 MASK only includes those bits, this can be a logical shift, which may
7520 allow simplifications. If MASK is a single-bit field not within
7521 those bits, we are requesting a copy of the sign bit and hence can
7522 shift the sign bit to the appropriate location. */
7524 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
&& INTVAL (XEXP (x
, 1)) >= 0
7525 && INTVAL (XEXP (x
, 1)) < HOST_BITS_PER_WIDE_INT
)
7529 /* If the considered data is wider than HOST_WIDE_INT, we can't
7530 represent a mask for all its bits in a single scalar.
7531 But we only care about the lower bits, so calculate these. */
7533 if (GET_MODE_BITSIZE (GET_MODE (x
)) > HOST_BITS_PER_WIDE_INT
)
7535 nonzero
= ~(HOST_WIDE_INT
) 0;
7537 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7538 is the number of bits a full-width mask would have set.
7539 We need only shift if these are fewer than nonzero can
7540 hold. If not, we must keep all bits set in nonzero. */
7542 if (GET_MODE_BITSIZE (GET_MODE (x
)) - INTVAL (XEXP (x
, 1))
7543 < HOST_BITS_PER_WIDE_INT
)
7544 nonzero
>>= INTVAL (XEXP (x
, 1))
7545 + HOST_BITS_PER_WIDE_INT
7546 - GET_MODE_BITSIZE (GET_MODE (x
)) ;
7550 nonzero
= GET_MODE_MASK (GET_MODE (x
));
7551 nonzero
>>= INTVAL (XEXP (x
, 1));
7554 if ((mask
& ~nonzero
) == 0)
7556 x
= simplify_shift_const (NULL_RTX
, LSHIFTRT
, GET_MODE (x
),
7557 XEXP (x
, 0), INTVAL (XEXP (x
, 1)));
7558 if (GET_CODE (x
) != ASHIFTRT
)
7559 return force_to_mode (x
, mode
, mask
, next_select
);
7562 else if ((i
= exact_log2 (mask
)) >= 0)
7564 x
= simplify_shift_const
7565 (NULL_RTX
, LSHIFTRT
, GET_MODE (x
), XEXP (x
, 0),
7566 GET_MODE_BITSIZE (GET_MODE (x
)) - 1 - i
);
7568 if (GET_CODE (x
) != ASHIFTRT
)
7569 return force_to_mode (x
, mode
, mask
, next_select
);
7573 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7574 even if the shift count isn't a constant. */
7576 x
= simplify_gen_binary (LSHIFTRT
, GET_MODE (x
),
7577 XEXP (x
, 0), XEXP (x
, 1));
7581 /* If this is a zero- or sign-extension operation that just affects bits
7582 we don't care about, remove it. Be sure the call above returned
7583 something that is still a shift. */
7585 if ((GET_CODE (x
) == LSHIFTRT
|| GET_CODE (x
) == ASHIFTRT
)
7586 && GET_CODE (XEXP (x
, 1)) == CONST_INT
7587 && INTVAL (XEXP (x
, 1)) >= 0
7588 && (INTVAL (XEXP (x
, 1))
7589 <= GET_MODE_BITSIZE (GET_MODE (x
)) - (floor_log2 (mask
) + 1))
7590 && GET_CODE (XEXP (x
, 0)) == ASHIFT
7591 && XEXP (XEXP (x
, 0), 1) == XEXP (x
, 1))
7592 return force_to_mode (XEXP (XEXP (x
, 0), 0), mode
, mask
,
7599 /* If the shift count is constant and we can do computations
7600 in the mode of X, compute where the bits we care about are.
7601 Otherwise, we can't do anything. Don't change the mode of
7602 the shift or propagate MODE into the shift, though. */
7603 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
7604 && INTVAL (XEXP (x
, 1)) >= 0)
7606 temp
= simplify_binary_operation (code
== ROTATE
? ROTATERT
: ROTATE
,
7607 GET_MODE (x
), GEN_INT (mask
),
7609 if (temp
&& GET_CODE (temp
) == CONST_INT
)
7611 force_to_mode (XEXP (x
, 0), GET_MODE (x
),
7612 INTVAL (temp
), next_select
));
7617 /* If we just want the low-order bit, the NEG isn't needed since it
7618 won't change the low-order bit. */
7620 return force_to_mode (XEXP (x
, 0), mode
, mask
, just_select
);
7622 /* We need any bits less significant than the most significant bit in
7623 MASK since carries from those bits will affect the bits we are
7629 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7630 same as the XOR case above. Ensure that the constant we form is not
7631 wider than the mode of X. */
7633 if (GET_CODE (XEXP (x
, 0)) == LSHIFTRT
7634 && GET_CODE (XEXP (XEXP (x
, 0), 1)) == CONST_INT
7635 && INTVAL (XEXP (XEXP (x
, 0), 1)) >= 0
7636 && (INTVAL (XEXP (XEXP (x
, 0), 1)) + floor_log2 (mask
)
7637 < GET_MODE_BITSIZE (GET_MODE (x
)))
7638 && INTVAL (XEXP (XEXP (x
, 0), 1)) < HOST_BITS_PER_WIDE_INT
)
7640 temp
= gen_int_mode (mask
<< INTVAL (XEXP (XEXP (x
, 0), 1)),
7642 temp
= simplify_gen_binary (XOR
, GET_MODE (x
),
7643 XEXP (XEXP (x
, 0), 0), temp
);
7644 x
= simplify_gen_binary (LSHIFTRT
, GET_MODE (x
),
7645 temp
, XEXP (XEXP (x
, 0), 1));
7647 return force_to_mode (x
, mode
, mask
, next_select
);
7650 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7651 use the full mask inside the NOT. */
7655 op0
= gen_lowpart_or_truncate (op_mode
,
7656 force_to_mode (XEXP (x
, 0), mode
, mask
,
7658 if (op_mode
!= GET_MODE (x
) || op0
!= XEXP (x
, 0))
7659 x
= simplify_gen_unary (code
, op_mode
, op0
, op_mode
);
7663 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7664 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7665 which is equal to STORE_FLAG_VALUE. */
7666 if ((mask
& ~STORE_FLAG_VALUE
) == 0 && XEXP (x
, 1) == const0_rtx
7667 && GET_MODE (XEXP (x
, 0)) == mode
7668 && exact_log2 (nonzero_bits (XEXP (x
, 0), mode
)) >= 0
7669 && (nonzero_bits (XEXP (x
, 0), mode
)
7670 == (unsigned HOST_WIDE_INT
) STORE_FLAG_VALUE
))
7671 return force_to_mode (XEXP (x
, 0), mode
, mask
, next_select
);
7676 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7677 written in a narrower mode. We play it safe and do not do so. */
7680 gen_lowpart_or_truncate (GET_MODE (x
),
7681 force_to_mode (XEXP (x
, 1), mode
,
7682 mask
, next_select
)));
7684 gen_lowpart_or_truncate (GET_MODE (x
),
7685 force_to_mode (XEXP (x
, 2), mode
,
7686 mask
, next_select
)));
7693 /* Ensure we return a value of the proper mode. */
7694 return gen_lowpart_or_truncate (mode
, x
);
7697 /* Return nonzero if X is an expression that has one of two values depending on
7698 whether some other value is zero or nonzero. In that case, we return the
7699 value that is being tested, *PTRUE is set to the value if the rtx being
7700 returned has a nonzero value, and *PFALSE is set to the other alternative.
7702 If we return zero, we set *PTRUE and *PFALSE to X. */
7705 if_then_else_cond (rtx x
, rtx
*ptrue
, rtx
*pfalse
)
7707 enum machine_mode mode
= GET_MODE (x
);
7708 enum rtx_code code
= GET_CODE (x
);
7709 rtx cond0
, cond1
, true0
, true1
, false0
, false1
;
7710 unsigned HOST_WIDE_INT nz
;
7712 /* If we are comparing a value against zero, we are done. */
7713 if ((code
== NE
|| code
== EQ
)
7714 && XEXP (x
, 1) == const0_rtx
)
7716 *ptrue
= (code
== NE
) ? const_true_rtx
: const0_rtx
;
7717 *pfalse
= (code
== NE
) ? const0_rtx
: const_true_rtx
;
7721 /* If this is a unary operation whose operand has one of two values, apply
7722 our opcode to compute those values. */
7723 else if (UNARY_P (x
)
7724 && (cond0
= if_then_else_cond (XEXP (x
, 0), &true0
, &false0
)) != 0)
7726 *ptrue
= simplify_gen_unary (code
, mode
, true0
, GET_MODE (XEXP (x
, 0)));
7727 *pfalse
= simplify_gen_unary (code
, mode
, false0
,
7728 GET_MODE (XEXP (x
, 0)));
7732 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7733 make can't possibly match and would suppress other optimizations. */
7734 else if (code
== COMPARE
)
7737 /* If this is a binary operation, see if either side has only one of two
7738 values. If either one does or if both do and they are conditional on
7739 the same value, compute the new true and false values. */
7740 else if (BINARY_P (x
))
7742 cond0
= if_then_else_cond (XEXP (x
, 0), &true0
, &false0
);
7743 cond1
= if_then_else_cond (XEXP (x
, 1), &true1
, &false1
);
7745 if ((cond0
!= 0 || cond1
!= 0)
7746 && ! (cond0
!= 0 && cond1
!= 0 && ! rtx_equal_p (cond0
, cond1
)))
7748 /* If if_then_else_cond returned zero, then true/false are the
7749 same rtl. We must copy one of them to prevent invalid rtl
7752 true0
= copy_rtx (true0
);
7753 else if (cond1
== 0)
7754 true1
= copy_rtx (true1
);
7756 if (COMPARISON_P (x
))
7758 *ptrue
= simplify_gen_relational (code
, mode
, VOIDmode
,
7760 *pfalse
= simplify_gen_relational (code
, mode
, VOIDmode
,
7765 *ptrue
= simplify_gen_binary (code
, mode
, true0
, true1
);
7766 *pfalse
= simplify_gen_binary (code
, mode
, false0
, false1
);
7769 return cond0
? cond0
: cond1
;
7772 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7773 operands is zero when the other is nonzero, and vice-versa,
7774 and STORE_FLAG_VALUE is 1 or -1. */
7776 if ((STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
7777 && (code
== PLUS
|| code
== IOR
|| code
== XOR
|| code
== MINUS
7779 && GET_CODE (XEXP (x
, 0)) == MULT
&& GET_CODE (XEXP (x
, 1)) == MULT
)
7781 rtx op0
= XEXP (XEXP (x
, 0), 1);
7782 rtx op1
= XEXP (XEXP (x
, 1), 1);
7784 cond0
= XEXP (XEXP (x
, 0), 0);
7785 cond1
= XEXP (XEXP (x
, 1), 0);
7787 if (COMPARISON_P (cond0
)
7788 && COMPARISON_P (cond1
)
7789 && ((GET_CODE (cond0
) == reversed_comparison_code (cond1
, NULL
)
7790 && rtx_equal_p (XEXP (cond0
, 0), XEXP (cond1
, 0))
7791 && rtx_equal_p (XEXP (cond0
, 1), XEXP (cond1
, 1)))
7792 || ((swap_condition (GET_CODE (cond0
))
7793 == reversed_comparison_code (cond1
, NULL
))
7794 && rtx_equal_p (XEXP (cond0
, 0), XEXP (cond1
, 1))
7795 && rtx_equal_p (XEXP (cond0
, 1), XEXP (cond1
, 0))))
7796 && ! side_effects_p (x
))
7798 *ptrue
= simplify_gen_binary (MULT
, mode
, op0
, const_true_rtx
);
7799 *pfalse
= simplify_gen_binary (MULT
, mode
,
7801 ? simplify_gen_unary (NEG
, mode
,
7809 /* Similarly for MULT, AND and UMIN, except that for these the result
7811 if ((STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
7812 && (code
== MULT
|| code
== AND
|| code
== UMIN
)
7813 && GET_CODE (XEXP (x
, 0)) == MULT
&& GET_CODE (XEXP (x
, 1)) == MULT
)
7815 cond0
= XEXP (XEXP (x
, 0), 0);
7816 cond1
= XEXP (XEXP (x
, 1), 0);
7818 if (COMPARISON_P (cond0
)
7819 && COMPARISON_P (cond1
)
7820 && ((GET_CODE (cond0
) == reversed_comparison_code (cond1
, NULL
)
7821 && rtx_equal_p (XEXP (cond0
, 0), XEXP (cond1
, 0))
7822 && rtx_equal_p (XEXP (cond0
, 1), XEXP (cond1
, 1)))
7823 || ((swap_condition (GET_CODE (cond0
))
7824 == reversed_comparison_code (cond1
, NULL
))
7825 && rtx_equal_p (XEXP (cond0
, 0), XEXP (cond1
, 1))
7826 && rtx_equal_p (XEXP (cond0
, 1), XEXP (cond1
, 0))))
7827 && ! side_effects_p (x
))
7829 *ptrue
= *pfalse
= const0_rtx
;
7835 else if (code
== IF_THEN_ELSE
)
7837 /* If we have IF_THEN_ELSE already, extract the condition and
7838 canonicalize it if it is NE or EQ. */
7839 cond0
= XEXP (x
, 0);
7840 *ptrue
= XEXP (x
, 1), *pfalse
= XEXP (x
, 2);
7841 if (GET_CODE (cond0
) == NE
&& XEXP (cond0
, 1) == const0_rtx
)
7842 return XEXP (cond0
, 0);
7843 else if (GET_CODE (cond0
) == EQ
&& XEXP (cond0
, 1) == const0_rtx
)
7845 *ptrue
= XEXP (x
, 2), *pfalse
= XEXP (x
, 1);
7846 return XEXP (cond0
, 0);
7852 /* If X is a SUBREG, we can narrow both the true and false values
7853 if the inner expression, if there is a condition. */
7854 else if (code
== SUBREG
7855 && 0 != (cond0
= if_then_else_cond (SUBREG_REG (x
),
7858 true0
= simplify_gen_subreg (mode
, true0
,
7859 GET_MODE (SUBREG_REG (x
)), SUBREG_BYTE (x
));
7860 false0
= simplify_gen_subreg (mode
, false0
,
7861 GET_MODE (SUBREG_REG (x
)), SUBREG_BYTE (x
));
7862 if (true0
&& false0
)
7870 /* If X is a constant, this isn't special and will cause confusions
7871 if we treat it as such. Likewise if it is equivalent to a constant. */
7872 else if (CONSTANT_P (x
)
7873 || ((cond0
= get_last_value (x
)) != 0 && CONSTANT_P (cond0
)))
7876 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7877 will be least confusing to the rest of the compiler. */
7878 else if (mode
== BImode
)
7880 *ptrue
= GEN_INT (STORE_FLAG_VALUE
), *pfalse
= const0_rtx
;
7884 /* If X is known to be either 0 or -1, those are the true and
7885 false values when testing X. */
7886 else if (x
== constm1_rtx
|| x
== const0_rtx
7887 || (mode
!= VOIDmode
7888 && num_sign_bit_copies (x
, mode
) == GET_MODE_BITSIZE (mode
)))
7890 *ptrue
= constm1_rtx
, *pfalse
= const0_rtx
;
7894 /* Likewise for 0 or a single bit. */
7895 else if (SCALAR_INT_MODE_P (mode
)
7896 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
7897 && exact_log2 (nz
= nonzero_bits (x
, mode
)) >= 0)
7899 *ptrue
= gen_int_mode (nz
, mode
), *pfalse
= const0_rtx
;
7903 /* Otherwise fail; show no condition with true and false values the same. */
7904 *ptrue
= *pfalse
= x
;
7908 /* Return the value of expression X given the fact that condition COND
7909 is known to be true when applied to REG as its first operand and VAL
7910 as its second. X is known to not be shared and so can be modified in
7913 We only handle the simplest cases, and specifically those cases that
7914 arise with IF_THEN_ELSE expressions. */
7917 known_cond (rtx x
, enum rtx_code cond
, rtx reg
, rtx val
)
7919 enum rtx_code code
= GET_CODE (x
);
7924 if (side_effects_p (x
))
7927 /* If either operand of the condition is a floating point value,
7928 then we have to avoid collapsing an EQ comparison. */
7930 && rtx_equal_p (x
, reg
)
7931 && ! FLOAT_MODE_P (GET_MODE (x
))
7932 && ! FLOAT_MODE_P (GET_MODE (val
)))
7935 if (cond
== UNEQ
&& rtx_equal_p (x
, reg
))
7938 /* If X is (abs REG) and we know something about REG's relationship
7939 with zero, we may be able to simplify this. */
7941 if (code
== ABS
&& rtx_equal_p (XEXP (x
, 0), reg
) && val
== const0_rtx
)
7944 case GE
: case GT
: case EQ
:
7947 return simplify_gen_unary (NEG
, GET_MODE (XEXP (x
, 0)),
7949 GET_MODE (XEXP (x
, 0)));
7954 /* The only other cases we handle are MIN, MAX, and comparisons if the
7955 operands are the same as REG and VAL. */
7957 else if (COMPARISON_P (x
) || COMMUTATIVE_ARITH_P (x
))
7959 if (rtx_equal_p (XEXP (x
, 0), val
))
7960 cond
= swap_condition (cond
), temp
= val
, val
= reg
, reg
= temp
;
7962 if (rtx_equal_p (XEXP (x
, 0), reg
) && rtx_equal_p (XEXP (x
, 1), val
))
7964 if (COMPARISON_P (x
))
7966 if (comparison_dominates_p (cond
, code
))
7967 return const_true_rtx
;
7969 code
= reversed_comparison_code (x
, NULL
);
7971 && comparison_dominates_p (cond
, code
))
7976 else if (code
== SMAX
|| code
== SMIN
7977 || code
== UMIN
|| code
== UMAX
)
7979 int unsignedp
= (code
== UMIN
|| code
== UMAX
);
7981 /* Do not reverse the condition when it is NE or EQ.
7982 This is because we cannot conclude anything about
7983 the value of 'SMAX (x, y)' when x is not equal to y,
7984 but we can when x equals y. */
7985 if ((code
== SMAX
|| code
== UMAX
)
7986 && ! (cond
== EQ
|| cond
== NE
))
7987 cond
= reverse_condition (cond
);
7992 return unsignedp
? x
: XEXP (x
, 1);
7994 return unsignedp
? x
: XEXP (x
, 0);
7996 return unsignedp
? XEXP (x
, 1) : x
;
7998 return unsignedp
? XEXP (x
, 0) : x
;
8005 else if (code
== SUBREG
)
8007 enum machine_mode inner_mode
= GET_MODE (SUBREG_REG (x
));
8008 rtx
new, r
= known_cond (SUBREG_REG (x
), cond
, reg
, val
);
8010 if (SUBREG_REG (x
) != r
)
8012 /* We must simplify subreg here, before we lose track of the
8013 original inner_mode. */
8014 new = simplify_subreg (GET_MODE (x
), r
,
8015 inner_mode
, SUBREG_BYTE (x
));
8019 SUBST (SUBREG_REG (x
), r
);
8024 /* We don't have to handle SIGN_EXTEND here, because even in the
8025 case of replacing something with a modeless CONST_INT, a
8026 CONST_INT is already (supposed to be) a valid sign extension for
8027 its narrower mode, which implies it's already properly
8028 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8029 story is different. */
8030 else if (code
== ZERO_EXTEND
)
8032 enum machine_mode inner_mode
= GET_MODE (XEXP (x
, 0));
8033 rtx
new, r
= known_cond (XEXP (x
, 0), cond
, reg
, val
);
8035 if (XEXP (x
, 0) != r
)
8037 /* We must simplify the zero_extend here, before we lose
8038 track of the original inner_mode. */
8039 new = simplify_unary_operation (ZERO_EXTEND
, GET_MODE (x
),
8044 SUBST (XEXP (x
, 0), r
);
8050 fmt
= GET_RTX_FORMAT (code
);
8051 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
8054 SUBST (XEXP (x
, i
), known_cond (XEXP (x
, i
), cond
, reg
, val
));
8055 else if (fmt
[i
] == 'E')
8056 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
8057 SUBST (XVECEXP (x
, i
, j
), known_cond (XVECEXP (x
, i
, j
),
8064 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8065 assignment as a field assignment. */
8068 rtx_equal_for_field_assignment_p (rtx x
, rtx y
)
8070 if (x
== y
|| rtx_equal_p (x
, y
))
8073 if (x
== 0 || y
== 0 || GET_MODE (x
) != GET_MODE (y
))
8076 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8077 Note that all SUBREGs of MEM are paradoxical; otherwise they
8078 would have been rewritten. */
8079 if (MEM_P (x
) && GET_CODE (y
) == SUBREG
8080 && MEM_P (SUBREG_REG (y
))
8081 && rtx_equal_p (SUBREG_REG (y
),
8082 gen_lowpart (GET_MODE (SUBREG_REG (y
)), x
)))
8085 if (MEM_P (y
) && GET_CODE (x
) == SUBREG
8086 && MEM_P (SUBREG_REG (x
))
8087 && rtx_equal_p (SUBREG_REG (x
),
8088 gen_lowpart (GET_MODE (SUBREG_REG (x
)), y
)))
8091 /* We used to see if get_last_value of X and Y were the same but that's
8092 not correct. In one direction, we'll cause the assignment to have
8093 the wrong destination and in the case, we'll import a register into this
8094 insn that might have already have been dead. So fail if none of the
8095 above cases are true. */
8099 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8100 Return that assignment if so.
8102 We only handle the most common cases. */
8105 make_field_assignment (rtx x
)
8107 rtx dest
= SET_DEST (x
);
8108 rtx src
= SET_SRC (x
);
8113 unsigned HOST_WIDE_INT len
;
8115 enum machine_mode mode
;
8117 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8118 a clear of a one-bit field. We will have changed it to
8119 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8122 if (GET_CODE (src
) == AND
&& GET_CODE (XEXP (src
, 0)) == ROTATE
8123 && GET_CODE (XEXP (XEXP (src
, 0), 0)) == CONST_INT
8124 && INTVAL (XEXP (XEXP (src
, 0), 0)) == -2
8125 && rtx_equal_for_field_assignment_p (dest
, XEXP (src
, 1)))
8127 assign
= make_extraction (VOIDmode
, dest
, 0, XEXP (XEXP (src
, 0), 1),
8130 return gen_rtx_SET (VOIDmode
, assign
, const0_rtx
);
8134 if (GET_CODE (src
) == AND
&& GET_CODE (XEXP (src
, 0)) == SUBREG
8135 && subreg_lowpart_p (XEXP (src
, 0))
8136 && (GET_MODE_SIZE (GET_MODE (XEXP (src
, 0)))
8137 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src
, 0)))))
8138 && GET_CODE (SUBREG_REG (XEXP (src
, 0))) == ROTATE
8139 && GET_CODE (XEXP (SUBREG_REG (XEXP (src
, 0)), 0)) == CONST_INT
8140 && INTVAL (XEXP (SUBREG_REG (XEXP (src
, 0)), 0)) == -2
8141 && rtx_equal_for_field_assignment_p (dest
, XEXP (src
, 1)))
8143 assign
= make_extraction (VOIDmode
, dest
, 0,
8144 XEXP (SUBREG_REG (XEXP (src
, 0)), 1),
8147 return gen_rtx_SET (VOIDmode
, assign
, const0_rtx
);
8151 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8153 if (GET_CODE (src
) == IOR
&& GET_CODE (XEXP (src
, 0)) == ASHIFT
8154 && XEXP (XEXP (src
, 0), 0) == const1_rtx
8155 && rtx_equal_for_field_assignment_p (dest
, XEXP (src
, 1)))
8157 assign
= make_extraction (VOIDmode
, dest
, 0, XEXP (XEXP (src
, 0), 1),
8160 return gen_rtx_SET (VOIDmode
, assign
, const1_rtx
);
8164 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8165 SRC is an AND with all bits of that field set, then we can discard
8167 if (GET_CODE (dest
) == ZERO_EXTRACT
8168 && GET_CODE (XEXP (dest
, 1)) == CONST_INT
8169 && GET_CODE (src
) == AND
8170 && GET_CODE (XEXP (src
, 1)) == CONST_INT
)
8172 HOST_WIDE_INT width
= INTVAL (XEXP (dest
, 1));
8173 unsigned HOST_WIDE_INT and_mask
= INTVAL (XEXP (src
, 1));
8174 unsigned HOST_WIDE_INT ze_mask
;
8176 if (width
>= HOST_BITS_PER_WIDE_INT
)
8179 ze_mask
= ((unsigned HOST_WIDE_INT
)1 << width
) - 1;
8181 /* Complete overlap. We can remove the source AND. */
8182 if ((and_mask
& ze_mask
) == ze_mask
)
8183 return gen_rtx_SET (VOIDmode
, dest
, XEXP (src
, 0));
8185 /* Partial overlap. We can reduce the source AND. */
8186 if ((and_mask
& ze_mask
) != and_mask
)
8188 mode
= GET_MODE (src
);
8189 src
= gen_rtx_AND (mode
, XEXP (src
, 0),
8190 gen_int_mode (and_mask
& ze_mask
, mode
));
8191 return gen_rtx_SET (VOIDmode
, dest
, src
);
8195 /* The other case we handle is assignments into a constant-position
8196 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8197 a mask that has all one bits except for a group of zero bits and
8198 OTHER is known to have zeros where C1 has ones, this is such an
8199 assignment. Compute the position and length from C1. Shift OTHER
8200 to the appropriate position, force it to the required mode, and
8201 make the extraction. Check for the AND in both operands. */
8203 if (GET_CODE (src
) != IOR
&& GET_CODE (src
) != XOR
)
8206 rhs
= expand_compound_operation (XEXP (src
, 0));
8207 lhs
= expand_compound_operation (XEXP (src
, 1));
8209 if (GET_CODE (rhs
) == AND
8210 && GET_CODE (XEXP (rhs
, 1)) == CONST_INT
8211 && rtx_equal_for_field_assignment_p (XEXP (rhs
, 0), dest
))
8212 c1
= INTVAL (XEXP (rhs
, 1)), other
= lhs
;
8213 else if (GET_CODE (lhs
) == AND
8214 && GET_CODE (XEXP (lhs
, 1)) == CONST_INT
8215 && rtx_equal_for_field_assignment_p (XEXP (lhs
, 0), dest
))
8216 c1
= INTVAL (XEXP (lhs
, 1)), other
= rhs
;
8220 pos
= get_pos_from_mask ((~c1
) & GET_MODE_MASK (GET_MODE (dest
)), &len
);
8221 if (pos
< 0 || pos
+ len
> GET_MODE_BITSIZE (GET_MODE (dest
))
8222 || GET_MODE_BITSIZE (GET_MODE (dest
)) > HOST_BITS_PER_WIDE_INT
8223 || (c1
& nonzero_bits (other
, GET_MODE (dest
))) != 0)
8226 assign
= make_extraction (VOIDmode
, dest
, pos
, NULL_RTX
, len
, 1, 1, 0);
8230 /* The mode to use for the source is the mode of the assignment, or of
8231 what is inside a possible STRICT_LOW_PART. */
8232 mode
= (GET_CODE (assign
) == STRICT_LOW_PART
8233 ? GET_MODE (XEXP (assign
, 0)) : GET_MODE (assign
));
8235 /* Shift OTHER right POS places and make it the source, restricting it
8236 to the proper length and mode. */
8238 src
= canon_reg_for_combine (simplify_shift_const (NULL_RTX
, LSHIFTRT
,
8242 src
= force_to_mode (src
, mode
,
8243 GET_MODE_BITSIZE (mode
) >= HOST_BITS_PER_WIDE_INT
8244 ? ~(unsigned HOST_WIDE_INT
) 0
8245 : ((unsigned HOST_WIDE_INT
) 1 << len
) - 1,
8248 /* If SRC is masked by an AND that does not make a difference in
8249 the value being stored, strip it. */
8250 if (GET_CODE (assign
) == ZERO_EXTRACT
8251 && GET_CODE (XEXP (assign
, 1)) == CONST_INT
8252 && INTVAL (XEXP (assign
, 1)) < HOST_BITS_PER_WIDE_INT
8253 && GET_CODE (src
) == AND
8254 && GET_CODE (XEXP (src
, 1)) == CONST_INT
8255 && ((unsigned HOST_WIDE_INT
) INTVAL (XEXP (src
, 1))
8256 == ((unsigned HOST_WIDE_INT
) 1 << INTVAL (XEXP (assign
, 1))) - 1))
8257 src
= XEXP (src
, 0);
8259 return gen_rtx_SET (VOIDmode
, assign
, src
);
8262 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8266 apply_distributive_law (rtx x
)
8268 enum rtx_code code
= GET_CODE (x
);
8269 enum rtx_code inner_code
;
8270 rtx lhs
, rhs
, other
;
8273 /* Distributivity is not true for floating point as it can change the
8274 value. So we don't do it unless -funsafe-math-optimizations. */
8275 if (FLOAT_MODE_P (GET_MODE (x
))
8276 && ! flag_unsafe_math_optimizations
)
8279 /* The outer operation can only be one of the following: */
8280 if (code
!= IOR
&& code
!= AND
&& code
!= XOR
8281 && code
!= PLUS
&& code
!= MINUS
)
8287 /* If either operand is a primitive we can't do anything, so get out
8289 if (OBJECT_P (lhs
) || OBJECT_P (rhs
))
8292 lhs
= expand_compound_operation (lhs
);
8293 rhs
= expand_compound_operation (rhs
);
8294 inner_code
= GET_CODE (lhs
);
8295 if (inner_code
!= GET_CODE (rhs
))
8298 /* See if the inner and outer operations distribute. */
8305 /* These all distribute except over PLUS. */
8306 if (code
== PLUS
|| code
== MINUS
)
8311 if (code
!= PLUS
&& code
!= MINUS
)
8316 /* This is also a multiply, so it distributes over everything. */
8320 /* Non-paradoxical SUBREGs distributes over all operations,
8321 provided the inner modes and byte offsets are the same, this
8322 is an extraction of a low-order part, we don't convert an fp
8323 operation to int or vice versa, this is not a vector mode,
8324 and we would not be converting a single-word operation into a
8325 multi-word operation. The latter test is not required, but
8326 it prevents generating unneeded multi-word operations. Some
8327 of the previous tests are redundant given the latter test,
8328 but are retained because they are required for correctness.
8330 We produce the result slightly differently in this case. */
8332 if (GET_MODE (SUBREG_REG (lhs
)) != GET_MODE (SUBREG_REG (rhs
))
8333 || SUBREG_BYTE (lhs
) != SUBREG_BYTE (rhs
)
8334 || ! subreg_lowpart_p (lhs
)
8335 || (GET_MODE_CLASS (GET_MODE (lhs
))
8336 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs
))))
8337 || (GET_MODE_SIZE (GET_MODE (lhs
))
8338 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs
))))
8339 || VECTOR_MODE_P (GET_MODE (lhs
))
8340 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs
))) > UNITS_PER_WORD
8341 /* Result might need to be truncated. Don't change mode if
8342 explicit truncation is needed. */
8343 || !TRULY_NOOP_TRUNCATION
8344 (GET_MODE_BITSIZE (GET_MODE (x
)),
8345 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs
)))))
8348 tem
= simplify_gen_binary (code
, GET_MODE (SUBREG_REG (lhs
)),
8349 SUBREG_REG (lhs
), SUBREG_REG (rhs
));
8350 return gen_lowpart (GET_MODE (x
), tem
);
8356 /* Set LHS and RHS to the inner operands (A and B in the example
8357 above) and set OTHER to the common operand (C in the example).
8358 There is only one way to do this unless the inner operation is
8360 if (COMMUTATIVE_ARITH_P (lhs
)
8361 && rtx_equal_p (XEXP (lhs
, 0), XEXP (rhs
, 0)))
8362 other
= XEXP (lhs
, 0), lhs
= XEXP (lhs
, 1), rhs
= XEXP (rhs
, 1);
8363 else if (COMMUTATIVE_ARITH_P (lhs
)
8364 && rtx_equal_p (XEXP (lhs
, 0), XEXP (rhs
, 1)))
8365 other
= XEXP (lhs
, 0), lhs
= XEXP (lhs
, 1), rhs
= XEXP (rhs
, 0);
8366 else if (COMMUTATIVE_ARITH_P (lhs
)
8367 && rtx_equal_p (XEXP (lhs
, 1), XEXP (rhs
, 0)))
8368 other
= XEXP (lhs
, 1), lhs
= XEXP (lhs
, 0), rhs
= XEXP (rhs
, 1);
8369 else if (rtx_equal_p (XEXP (lhs
, 1), XEXP (rhs
, 1)))
8370 other
= XEXP (lhs
, 1), lhs
= XEXP (lhs
, 0), rhs
= XEXP (rhs
, 0);
8374 /* Form the new inner operation, seeing if it simplifies first. */
8375 tem
= simplify_gen_binary (code
, GET_MODE (x
), lhs
, rhs
);
8377 /* There is one exception to the general way of distributing:
8378 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8379 if (code
== XOR
&& inner_code
== IOR
)
8382 other
= simplify_gen_unary (NOT
, GET_MODE (x
), other
, GET_MODE (x
));
8385 /* We may be able to continuing distributing the result, so call
8386 ourselves recursively on the inner operation before forming the
8387 outer operation, which we return. */
8388 return simplify_gen_binary (inner_code
, GET_MODE (x
),
8389 apply_distributive_law (tem
), other
);
8392 /* See if X is of the form (* (+ A B) C), and if so convert to
8393 (+ (* A C) (* B C)) and try to simplify.
8395 Most of the time, this results in no change. However, if some of
8396 the operands are the same or inverses of each other, simplifications
8399 For example, (and (ior A B) (not B)) can occur as the result of
8400 expanding a bit field assignment. When we apply the distributive
8401 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8402 which then simplifies to (and (A (not B))).
8404 Note that no checks happen on the validity of applying the inverse
8405 distributive law. This is pointless since we can do it in the
8406 few places where this routine is called.
8408 N is the index of the term that is decomposed (the arithmetic operation,
8409 i.e. (+ A B) in the first example above). !N is the index of the term that
8410 is distributed, i.e. of C in the first example above. */
8412 distribute_and_simplify_rtx (rtx x
, int n
)
8414 enum machine_mode mode
;
8415 enum rtx_code outer_code
, inner_code
;
8416 rtx decomposed
, distributed
, inner_op0
, inner_op1
, new_op0
, new_op1
, tmp
;
8418 decomposed
= XEXP (x
, n
);
8419 if (!ARITHMETIC_P (decomposed
))
8422 mode
= GET_MODE (x
);
8423 outer_code
= GET_CODE (x
);
8424 distributed
= XEXP (x
, !n
);
8426 inner_code
= GET_CODE (decomposed
);
8427 inner_op0
= XEXP (decomposed
, 0);
8428 inner_op1
= XEXP (decomposed
, 1);
8430 /* Special case (and (xor B C) (not A)), which is equivalent to
8431 (xor (ior A B) (ior A C)) */
8432 if (outer_code
== AND
&& inner_code
== XOR
&& GET_CODE (distributed
) == NOT
)
8434 distributed
= XEXP (distributed
, 0);
8440 /* Distribute the second term. */
8441 new_op0
= simplify_gen_binary (outer_code
, mode
, inner_op0
, distributed
);
8442 new_op1
= simplify_gen_binary (outer_code
, mode
, inner_op1
, distributed
);
8446 /* Distribute the first term. */
8447 new_op0
= simplify_gen_binary (outer_code
, mode
, distributed
, inner_op0
);
8448 new_op1
= simplify_gen_binary (outer_code
, mode
, distributed
, inner_op1
);
8451 tmp
= apply_distributive_law (simplify_gen_binary (inner_code
, mode
,
8453 if (GET_CODE (tmp
) != outer_code
8454 && rtx_cost (tmp
, SET
) < rtx_cost (x
, SET
))
8460 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8461 in MODE. Return an equivalent form, if different from (and VAROP
8462 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8465 simplify_and_const_int_1 (enum machine_mode mode
, rtx varop
,
8466 unsigned HOST_WIDE_INT constop
)
8468 unsigned HOST_WIDE_INT nonzero
;
8469 unsigned HOST_WIDE_INT orig_constop
;
8474 orig_constop
= constop
;
8475 if (GET_CODE (varop
) == CLOBBER
)
8478 /* Simplify VAROP knowing that we will be only looking at some of the
8481 Note by passing in CONSTOP, we guarantee that the bits not set in
8482 CONSTOP are not significant and will never be examined. We must
8483 ensure that is the case by explicitly masking out those bits
8484 before returning. */
8485 varop
= force_to_mode (varop
, mode
, constop
, 0);
8487 /* If VAROP is a CLOBBER, we will fail so return it. */
8488 if (GET_CODE (varop
) == CLOBBER
)
8491 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8492 to VAROP and return the new constant. */
8493 if (GET_CODE (varop
) == CONST_INT
)
8494 return gen_int_mode (INTVAL (varop
) & constop
, mode
);
8496 /* See what bits may be nonzero in VAROP. Unlike the general case of
8497 a call to nonzero_bits, here we don't care about bits outside
8500 nonzero
= nonzero_bits (varop
, mode
) & GET_MODE_MASK (mode
);
8502 /* Turn off all bits in the constant that are known to already be zero.
8503 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8504 which is tested below. */
8508 /* If we don't have any bits left, return zero. */
8512 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8513 a power of two, we can replace this with an ASHIFT. */
8514 if (GET_CODE (varop
) == NEG
&& nonzero_bits (XEXP (varop
, 0), mode
) == 1
8515 && (i
= exact_log2 (constop
)) >= 0)
8516 return simplify_shift_const (NULL_RTX
, ASHIFT
, mode
, XEXP (varop
, 0), i
);
8518 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8519 or XOR, then try to apply the distributive law. This may eliminate
8520 operations if either branch can be simplified because of the AND.
8521 It may also make some cases more complex, but those cases probably
8522 won't match a pattern either with or without this. */
8524 if (GET_CODE (varop
) == IOR
|| GET_CODE (varop
) == XOR
)
8528 apply_distributive_law
8529 (simplify_gen_binary (GET_CODE (varop
), GET_MODE (varop
),
8530 simplify_and_const_int (NULL_RTX
,
8534 simplify_and_const_int (NULL_RTX
,
8539 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8540 the AND and see if one of the operands simplifies to zero. If so, we
8541 may eliminate it. */
8543 if (GET_CODE (varop
) == PLUS
8544 && exact_log2 (constop
+ 1) >= 0)
8548 o0
= simplify_and_const_int (NULL_RTX
, mode
, XEXP (varop
, 0), constop
);
8549 o1
= simplify_and_const_int (NULL_RTX
, mode
, XEXP (varop
, 1), constop
);
8550 if (o0
== const0_rtx
)
8552 if (o1
== const0_rtx
)
8556 /* Make a SUBREG if necessary. If we can't make it, fail. */
8557 varop
= gen_lowpart (mode
, varop
);
8558 if (varop
== NULL_RTX
|| GET_CODE (varop
) == CLOBBER
)
8561 /* If we are only masking insignificant bits, return VAROP. */
8562 if (constop
== nonzero
)
8565 if (varop
== orig_varop
&& constop
== orig_constop
)
8568 /* Otherwise, return an AND. */
8569 return simplify_gen_binary (AND
, mode
, varop
, gen_int_mode (constop
, mode
));
8573 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8576 Return an equivalent form, if different from X. Otherwise, return X. If
8577 X is zero, we are to always construct the equivalent form. */
8580 simplify_and_const_int (rtx x
, enum machine_mode mode
, rtx varop
,
8581 unsigned HOST_WIDE_INT constop
)
8583 rtx tem
= simplify_and_const_int_1 (mode
, varop
, constop
);
8588 x
= simplify_gen_binary (AND
, GET_MODE (varop
), varop
,
8589 gen_int_mode (constop
, mode
));
8590 if (GET_MODE (x
) != mode
)
8591 x
= gen_lowpart (mode
, x
);
8595 /* Given a REG, X, compute which bits in X can be nonzero.
8596 We don't care about bits outside of those defined in MODE.
8598 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8599 a shift, AND, or zero_extract, we can do better. */
8602 reg_nonzero_bits_for_combine (rtx x
, enum machine_mode mode
,
8603 rtx known_x ATTRIBUTE_UNUSED
,
8604 enum machine_mode known_mode ATTRIBUTE_UNUSED
,
8605 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED
,
8606 unsigned HOST_WIDE_INT
*nonzero
)
8610 /* If X is a register whose nonzero bits value is current, use it.
8611 Otherwise, if X is a register whose value we can find, use that
8612 value. Otherwise, use the previously-computed global nonzero bits
8613 for this register. */
8615 if (reg_stat
[REGNO (x
)].last_set_value
!= 0
8616 && (reg_stat
[REGNO (x
)].last_set_mode
== mode
8617 || (GET_MODE_CLASS (reg_stat
[REGNO (x
)].last_set_mode
) == MODE_INT
8618 && GET_MODE_CLASS (mode
) == MODE_INT
))
8619 && ((reg_stat
[REGNO (x
)].last_set_label
>= label_tick_ebb_start
8620 && reg_stat
[REGNO (x
)].last_set_label
< label_tick
)
8621 || (reg_stat
[REGNO (x
)].last_set_label
== label_tick
8622 && DF_INSN_LUID (reg_stat
[REGNO (x
)].last_set
) < subst_low_luid
)
8623 || (REGNO (x
) >= FIRST_PSEUDO_REGISTER
8624 && REG_N_SETS (REGNO (x
)) == 1
8626 (DF_LR_IN (ENTRY_BLOCK_PTR
->next_bb
), REGNO (x
)))))
8628 *nonzero
&= reg_stat
[REGNO (x
)].last_set_nonzero_bits
;
8632 tem
= get_last_value (x
);
8636 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8637 /* If X is narrower than MODE and TEM is a non-negative
8638 constant that would appear negative in the mode of X,
8639 sign-extend it for use in reg_nonzero_bits because some
8640 machines (maybe most) will actually do the sign-extension
8641 and this is the conservative approach.
8643 ??? For 2.5, try to tighten up the MD files in this regard
8644 instead of this kludge. */
8646 if (GET_MODE_BITSIZE (GET_MODE (x
)) < GET_MODE_BITSIZE (mode
)
8647 && GET_CODE (tem
) == CONST_INT
8649 && 0 != (INTVAL (tem
)
8650 & ((HOST_WIDE_INT
) 1
8651 << (GET_MODE_BITSIZE (GET_MODE (x
)) - 1))))
8652 tem
= GEN_INT (INTVAL (tem
)
8653 | ((HOST_WIDE_INT
) (-1)
8654 << GET_MODE_BITSIZE (GET_MODE (x
))));
8658 else if (nonzero_sign_valid
&& reg_stat
[REGNO (x
)].nonzero_bits
)
8660 unsigned HOST_WIDE_INT mask
= reg_stat
[REGNO (x
)].nonzero_bits
;
8662 if (GET_MODE_BITSIZE (GET_MODE (x
)) < GET_MODE_BITSIZE (mode
))
8663 /* We don't know anything about the upper bits. */
8664 mask
|= GET_MODE_MASK (mode
) ^ GET_MODE_MASK (GET_MODE (x
));
8671 /* Return the number of bits at the high-order end of X that are known to
8672 be equal to the sign bit. X will be used in mode MODE; if MODE is
8673 VOIDmode, X will be used in its own mode. The returned value will always
8674 be between 1 and the number of bits in MODE. */
8677 reg_num_sign_bit_copies_for_combine (rtx x
, enum machine_mode mode
,
8678 rtx known_x ATTRIBUTE_UNUSED
,
8679 enum machine_mode known_mode
8681 unsigned int known_ret ATTRIBUTE_UNUSED
,
8682 unsigned int *result
)
8686 if (reg_stat
[REGNO (x
)].last_set_value
!= 0
8687 && reg_stat
[REGNO (x
)].last_set_mode
== mode
8688 && ((reg_stat
[REGNO (x
)].last_set_label
>= label_tick_ebb_start
8689 && reg_stat
[REGNO (x
)].last_set_label
< label_tick
)
8690 || (reg_stat
[REGNO (x
)].last_set_label
== label_tick
8691 && DF_INSN_LUID (reg_stat
[REGNO (x
)].last_set
) < subst_low_luid
)
8692 || (REGNO (x
) >= FIRST_PSEUDO_REGISTER
8693 && REG_N_SETS (REGNO (x
)) == 1
8695 (DF_LR_IN (ENTRY_BLOCK_PTR
->next_bb
), REGNO (x
)))))
8697 *result
= reg_stat
[REGNO (x
)].last_set_sign_bit_copies
;
8701 tem
= get_last_value (x
);
8705 if (nonzero_sign_valid
&& reg_stat
[REGNO (x
)].sign_bit_copies
!= 0
8706 && GET_MODE_BITSIZE (GET_MODE (x
)) == GET_MODE_BITSIZE (mode
))
8707 *result
= reg_stat
[REGNO (x
)].sign_bit_copies
;
8712 /* Return the number of "extended" bits there are in X, when interpreted
8713 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8714 unsigned quantities, this is the number of high-order zero bits.
8715 For signed quantities, this is the number of copies of the sign bit
8716 minus 1. In both case, this function returns the number of "spare"
8717 bits. For example, if two quantities for which this function returns
8718 at least 1 are added, the addition is known not to overflow.
8720 This function will always return 0 unless called during combine, which
8721 implies that it must be called from a define_split. */
8724 extended_count (rtx x
, enum machine_mode mode
, int unsignedp
)
8726 if (nonzero_sign_valid
== 0)
8730 ? (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
8731 ? (unsigned int) (GET_MODE_BITSIZE (mode
) - 1
8732 - floor_log2 (nonzero_bits (x
, mode
)))
8734 : num_sign_bit_copies (x
, mode
) - 1);
8737 /* This function is called from `simplify_shift_const' to merge two
8738 outer operations. Specifically, we have already found that we need
8739 to perform operation *POP0 with constant *PCONST0 at the outermost
8740 position. We would now like to also perform OP1 with constant CONST1
8741 (with *POP0 being done last).
8743 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8744 the resulting operation. *PCOMP_P is set to 1 if we would need to
8745 complement the innermost operand, otherwise it is unchanged.
8747 MODE is the mode in which the operation will be done. No bits outside
8748 the width of this mode matter. It is assumed that the width of this mode
8749 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8751 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8752 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8753 result is simply *PCONST0.
8755 If the resulting operation cannot be expressed as one operation, we
8756 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8759 merge_outer_ops (enum rtx_code
*pop0
, HOST_WIDE_INT
*pconst0
, enum rtx_code op1
, HOST_WIDE_INT const1
, enum machine_mode mode
, int *pcomp_p
)
8761 enum rtx_code op0
= *pop0
;
8762 HOST_WIDE_INT const0
= *pconst0
;
8764 const0
&= GET_MODE_MASK (mode
);
8765 const1
&= GET_MODE_MASK (mode
);
8767 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8771 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8774 if (op1
== UNKNOWN
|| op0
== SET
)
8777 else if (op0
== UNKNOWN
)
8778 op0
= op1
, const0
= const1
;
8780 else if (op0
== op1
)
8804 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8805 else if (op0
== PLUS
|| op1
== PLUS
|| op0
== NEG
|| op1
== NEG
)
8808 /* If the two constants aren't the same, we can't do anything. The
8809 remaining six cases can all be done. */
8810 else if (const0
!= const1
)
8818 /* (a & b) | b == b */
8820 else /* op1 == XOR */
8821 /* (a ^ b) | b == a | b */
8827 /* (a & b) ^ b == (~a) & b */
8828 op0
= AND
, *pcomp_p
= 1;
8829 else /* op1 == IOR */
8830 /* (a | b) ^ b == a & ~b */
8831 op0
= AND
, const0
= ~const0
;
8836 /* (a | b) & b == b */
8838 else /* op1 == XOR */
8839 /* (a ^ b) & b) == (~a) & b */
8846 /* Check for NO-OP cases. */
8847 const0
&= GET_MODE_MASK (mode
);
8849 && (op0
== IOR
|| op0
== XOR
|| op0
== PLUS
))
8851 else if (const0
== 0 && op0
== AND
)
8853 else if ((unsigned HOST_WIDE_INT
) const0
== GET_MODE_MASK (mode
)
8857 /* ??? Slightly redundant with the above mask, but not entirely.
8858 Moving this above means we'd have to sign-extend the mode mask
8859 for the final test. */
8860 const0
= trunc_int_for_mode (const0
, mode
);
8868 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8869 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
8870 simplify it. Otherwise, return a simplified value.
8872 The shift is normally computed in the widest mode we find in VAROP, as
8873 long as it isn't a different number of words than RESULT_MODE. Exceptions
8874 are ASHIFTRT and ROTATE, which are always done in their original mode. */
8877 simplify_shift_const_1 (enum rtx_code code
, enum machine_mode result_mode
,
8878 rtx varop
, int orig_count
)
8880 enum rtx_code orig_code
= code
;
8881 rtx orig_varop
= varop
;
8883 enum machine_mode mode
= result_mode
;
8884 enum machine_mode shift_mode
, tmode
;
8885 unsigned int mode_words
8886 = (GET_MODE_SIZE (mode
) + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
;
8887 /* We form (outer_op (code varop count) (outer_const)). */
8888 enum rtx_code outer_op
= UNKNOWN
;
8889 HOST_WIDE_INT outer_const
= 0;
8890 int complement_p
= 0;
8893 /* Make sure and truncate the "natural" shift on the way in. We don't
8894 want to do this inside the loop as it makes it more difficult to
8896 if (SHIFT_COUNT_TRUNCATED
)
8897 orig_count
&= GET_MODE_BITSIZE (mode
) - 1;
8899 /* If we were given an invalid count, don't do anything except exactly
8900 what was requested. */
8902 if (orig_count
< 0 || orig_count
>= (int) GET_MODE_BITSIZE (mode
))
8907 /* Unless one of the branches of the `if' in this loop does a `continue',
8908 we will `break' the loop after the `if'. */
8912 /* If we have an operand of (clobber (const_int 0)), fail. */
8913 if (GET_CODE (varop
) == CLOBBER
)
8916 /* If we discovered we had to complement VAROP, leave. Making a NOT
8917 here would cause an infinite loop. */
8921 /* Convert ROTATERT to ROTATE. */
8922 if (code
== ROTATERT
)
8924 unsigned int bitsize
= GET_MODE_BITSIZE (result_mode
);;
8926 if (VECTOR_MODE_P (result_mode
))
8927 count
= bitsize
/ GET_MODE_NUNITS (result_mode
) - count
;
8929 count
= bitsize
- count
;
8932 /* We need to determine what mode we will do the shift in. If the
8933 shift is a right shift or a ROTATE, we must always do it in the mode
8934 it was originally done in. Otherwise, we can do it in MODE, the
8935 widest mode encountered. */
8937 = (code
== ASHIFTRT
|| code
== LSHIFTRT
|| code
== ROTATE
8938 ? result_mode
: mode
);
8940 /* Handle cases where the count is greater than the size of the mode
8941 minus 1. For ASHIFT, use the size minus one as the count (this can
8942 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8943 take the count modulo the size. For other shifts, the result is
8946 Since these shifts are being produced by the compiler by combining
8947 multiple operations, each of which are defined, we know what the
8948 result is supposed to be. */
8950 if (count
> (GET_MODE_BITSIZE (shift_mode
) - 1))
8952 if (code
== ASHIFTRT
)
8953 count
= GET_MODE_BITSIZE (shift_mode
) - 1;
8954 else if (code
== ROTATE
|| code
== ROTATERT
)
8955 count
%= GET_MODE_BITSIZE (shift_mode
);
8958 /* We can't simply return zero because there may be an
8966 /* An arithmetic right shift of a quantity known to be -1 or 0
8968 if (code
== ASHIFTRT
8969 && (num_sign_bit_copies (varop
, shift_mode
)
8970 == GET_MODE_BITSIZE (shift_mode
)))
8976 /* If we are doing an arithmetic right shift and discarding all but
8977 the sign bit copies, this is equivalent to doing a shift by the
8978 bitsize minus one. Convert it into that shift because it will often
8979 allow other simplifications. */
8981 if (code
== ASHIFTRT
8982 && (count
+ num_sign_bit_copies (varop
, shift_mode
)
8983 >= GET_MODE_BITSIZE (shift_mode
)))
8984 count
= GET_MODE_BITSIZE (shift_mode
) - 1;
8986 /* We simplify the tests below and elsewhere by converting
8987 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8988 `make_compound_operation' will convert it to an ASHIFTRT for
8989 those machines (such as VAX) that don't have an LSHIFTRT. */
8990 if (GET_MODE_BITSIZE (shift_mode
) <= HOST_BITS_PER_WIDE_INT
8992 && ((nonzero_bits (varop
, shift_mode
)
8993 & ((HOST_WIDE_INT
) 1 << (GET_MODE_BITSIZE (shift_mode
) - 1)))
8997 if (((code
== LSHIFTRT
8998 && GET_MODE_BITSIZE (shift_mode
) <= HOST_BITS_PER_WIDE_INT
8999 && !(nonzero_bits (varop
, shift_mode
) >> count
))
9001 && GET_MODE_BITSIZE (shift_mode
) <= HOST_BITS_PER_WIDE_INT
9002 && !((nonzero_bits (varop
, shift_mode
) << count
)
9003 & GET_MODE_MASK (shift_mode
))))
9004 && !side_effects_p (varop
))
9007 switch (GET_CODE (varop
))
9013 new = expand_compound_operation (varop
);
9022 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9023 minus the width of a smaller mode, we can do this with a
9024 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9025 if ((code
== ASHIFTRT
|| code
== LSHIFTRT
)
9026 && ! mode_dependent_address_p (XEXP (varop
, 0))
9027 && ! MEM_VOLATILE_P (varop
)
9028 && (tmode
= mode_for_size (GET_MODE_BITSIZE (mode
) - count
,
9029 MODE_INT
, 1)) != BLKmode
)
9031 new = adjust_address_nv (varop
, tmode
,
9032 BYTES_BIG_ENDIAN
? 0
9033 : count
/ BITS_PER_UNIT
);
9035 varop
= gen_rtx_fmt_e (code
== ASHIFTRT
? SIGN_EXTEND
9036 : ZERO_EXTEND
, mode
, new);
9043 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9044 the same number of words as what we've seen so far. Then store
9045 the widest mode in MODE. */
9046 if (subreg_lowpart_p (varop
)
9047 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop
)))
9048 > GET_MODE_SIZE (GET_MODE (varop
)))
9049 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop
)))
9050 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
)
9053 varop
= SUBREG_REG (varop
);
9054 if (GET_MODE_SIZE (GET_MODE (varop
)) > GET_MODE_SIZE (mode
))
9055 mode
= GET_MODE (varop
);
9061 /* Some machines use MULT instead of ASHIFT because MULT
9062 is cheaper. But it is still better on those machines to
9063 merge two shifts into one. */
9064 if (GET_CODE (XEXP (varop
, 1)) == CONST_INT
9065 && exact_log2 (INTVAL (XEXP (varop
, 1))) >= 0)
9068 = simplify_gen_binary (ASHIFT
, GET_MODE (varop
),
9070 GEN_INT (exact_log2 (
9071 INTVAL (XEXP (varop
, 1)))));
9077 /* Similar, for when divides are cheaper. */
9078 if (GET_CODE (XEXP (varop
, 1)) == CONST_INT
9079 && exact_log2 (INTVAL (XEXP (varop
, 1))) >= 0)
9082 = simplify_gen_binary (LSHIFTRT
, GET_MODE (varop
),
9084 GEN_INT (exact_log2 (
9085 INTVAL (XEXP (varop
, 1)))));
9091 /* If we are extracting just the sign bit of an arithmetic
9092 right shift, that shift is not needed. However, the sign
9093 bit of a wider mode may be different from what would be
9094 interpreted as the sign bit in a narrower mode, so, if
9095 the result is narrower, don't discard the shift. */
9096 if (code
== LSHIFTRT
9097 && count
== (GET_MODE_BITSIZE (result_mode
) - 1)
9098 && (GET_MODE_BITSIZE (result_mode
)
9099 >= GET_MODE_BITSIZE (GET_MODE (varop
))))
9101 varop
= XEXP (varop
, 0);
9105 /* ... fall through ... */
9110 /* Here we have two nested shifts. The result is usually the
9111 AND of a new shift with a mask. We compute the result below. */
9112 if (GET_CODE (XEXP (varop
, 1)) == CONST_INT
9113 && INTVAL (XEXP (varop
, 1)) >= 0
9114 && INTVAL (XEXP (varop
, 1)) < GET_MODE_BITSIZE (GET_MODE (varop
))
9115 && GET_MODE_BITSIZE (result_mode
) <= HOST_BITS_PER_WIDE_INT
9116 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
9117 && !VECTOR_MODE_P (result_mode
))
9119 enum rtx_code first_code
= GET_CODE (varop
);
9120 unsigned int first_count
= INTVAL (XEXP (varop
, 1));
9121 unsigned HOST_WIDE_INT mask
;
9124 /* We have one common special case. We can't do any merging if
9125 the inner code is an ASHIFTRT of a smaller mode. However, if
9126 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9127 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9128 we can convert it to
9129 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9130 This simplifies certain SIGN_EXTEND operations. */
9131 if (code
== ASHIFT
&& first_code
== ASHIFTRT
9132 && count
== (GET_MODE_BITSIZE (result_mode
)
9133 - GET_MODE_BITSIZE (GET_MODE (varop
))))
9135 /* C3 has the low-order C1 bits zero. */
9137 mask
= (GET_MODE_MASK (mode
)
9138 & ~(((HOST_WIDE_INT
) 1 << first_count
) - 1));
9140 varop
= simplify_and_const_int (NULL_RTX
, result_mode
,
9141 XEXP (varop
, 0), mask
);
9142 varop
= simplify_shift_const (NULL_RTX
, ASHIFT
, result_mode
,
9144 count
= first_count
;
9149 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9150 than C1 high-order bits equal to the sign bit, we can convert
9151 this to either an ASHIFT or an ASHIFTRT depending on the
9154 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9156 if (code
== ASHIFTRT
&& first_code
== ASHIFT
9157 && GET_MODE (varop
) == shift_mode
9158 && (num_sign_bit_copies (XEXP (varop
, 0), shift_mode
)
9161 varop
= XEXP (varop
, 0);
9162 count
-= first_count
;
9172 /* There are some cases we can't do. If CODE is ASHIFTRT,
9173 we can only do this if FIRST_CODE is also ASHIFTRT.
9175 We can't do the case when CODE is ROTATE and FIRST_CODE is
9178 If the mode of this shift is not the mode of the outer shift,
9179 we can't do this if either shift is a right shift or ROTATE.
9181 Finally, we can't do any of these if the mode is too wide
9182 unless the codes are the same.
9184 Handle the case where the shift codes are the same
9187 if (code
== first_code
)
9189 if (GET_MODE (varop
) != result_mode
9190 && (code
== ASHIFTRT
|| code
== LSHIFTRT
9194 count
+= first_count
;
9195 varop
= XEXP (varop
, 0);
9199 if (code
== ASHIFTRT
9200 || (code
== ROTATE
&& first_code
== ASHIFTRT
)
9201 || GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
9202 || (GET_MODE (varop
) != result_mode
9203 && (first_code
== ASHIFTRT
|| first_code
== LSHIFTRT
9204 || first_code
== ROTATE
9205 || code
== ROTATE
)))
9208 /* To compute the mask to apply after the shift, shift the
9209 nonzero bits of the inner shift the same way the
9210 outer shift will. */
9212 mask_rtx
= GEN_INT (nonzero_bits (varop
, GET_MODE (varop
)));
9215 = simplify_const_binary_operation (code
, result_mode
, mask_rtx
,
9218 /* Give up if we can't compute an outer operation to use. */
9220 || GET_CODE (mask_rtx
) != CONST_INT
9221 || ! merge_outer_ops (&outer_op
, &outer_const
, AND
,
9223 result_mode
, &complement_p
))
9226 /* If the shifts are in the same direction, we add the
9227 counts. Otherwise, we subtract them. */
9228 if ((code
== ASHIFTRT
|| code
== LSHIFTRT
)
9229 == (first_code
== ASHIFTRT
|| first_code
== LSHIFTRT
))
9230 count
+= first_count
;
9232 count
-= first_count
;
9234 /* If COUNT is positive, the new shift is usually CODE,
9235 except for the two exceptions below, in which case it is
9236 FIRST_CODE. If the count is negative, FIRST_CODE should
9239 && ((first_code
== ROTATE
&& code
== ASHIFT
)
9240 || (first_code
== ASHIFTRT
&& code
== LSHIFTRT
)))
9243 code
= first_code
, count
= -count
;
9245 varop
= XEXP (varop
, 0);
9249 /* If we have (A << B << C) for any shift, we can convert this to
9250 (A << C << B). This wins if A is a constant. Only try this if
9251 B is not a constant. */
9253 else if (GET_CODE (varop
) == code
9254 && GET_CODE (XEXP (varop
, 0)) == CONST_INT
9255 && GET_CODE (XEXP (varop
, 1)) != CONST_INT
)
9257 rtx
new = simplify_const_binary_operation (code
, mode
,
9260 varop
= gen_rtx_fmt_ee (code
, mode
, new, XEXP (varop
, 1));
9267 /* Make this fit the case below. */
9268 varop
= gen_rtx_XOR (mode
, XEXP (varop
, 0),
9269 GEN_INT (GET_MODE_MASK (mode
)));
9275 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9276 with C the size of VAROP - 1 and the shift is logical if
9277 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9278 we have an (le X 0) operation. If we have an arithmetic shift
9279 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9280 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9282 if (GET_CODE (varop
) == IOR
&& GET_CODE (XEXP (varop
, 0)) == PLUS
9283 && XEXP (XEXP (varop
, 0), 1) == constm1_rtx
9284 && (STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
9285 && (code
== LSHIFTRT
|| code
== ASHIFTRT
)
9286 && count
== (GET_MODE_BITSIZE (GET_MODE (varop
)) - 1)
9287 && rtx_equal_p (XEXP (XEXP (varop
, 0), 0), XEXP (varop
, 1)))
9290 varop
= gen_rtx_LE (GET_MODE (varop
), XEXP (varop
, 1),
9293 if (STORE_FLAG_VALUE
== 1 ? code
== ASHIFTRT
: code
== LSHIFTRT
)
9294 varop
= gen_rtx_NEG (GET_MODE (varop
), varop
);
9299 /* If we have (shift (logical)), move the logical to the outside
9300 to allow it to possibly combine with another logical and the
9301 shift to combine with another shift. This also canonicalizes to
9302 what a ZERO_EXTRACT looks like. Also, some machines have
9303 (and (shift)) insns. */
9305 if (GET_CODE (XEXP (varop
, 1)) == CONST_INT
9306 /* We can't do this if we have (ashiftrt (xor)) and the
9307 constant has its sign bit set in shift_mode. */
9308 && !(code
== ASHIFTRT
&& GET_CODE (varop
) == XOR
9309 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop
, 1)),
9311 && (new = simplify_const_binary_operation (code
, result_mode
,
9313 GEN_INT (count
))) != 0
9314 && GET_CODE (new) == CONST_INT
9315 && merge_outer_ops (&outer_op
, &outer_const
, GET_CODE (varop
),
9316 INTVAL (new), result_mode
, &complement_p
))
9318 varop
= XEXP (varop
, 0);
9322 /* If we can't do that, try to simplify the shift in each arm of the
9323 logical expression, make a new logical expression, and apply
9324 the inverse distributive law. This also can't be done
9325 for some (ashiftrt (xor)). */
9326 if (GET_CODE (XEXP (varop
, 1)) == CONST_INT
9327 && !(code
== ASHIFTRT
&& GET_CODE (varop
) == XOR
9328 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop
, 1)),
9331 rtx lhs
= simplify_shift_const (NULL_RTX
, code
, shift_mode
,
9332 XEXP (varop
, 0), count
);
9333 rtx rhs
= simplify_shift_const (NULL_RTX
, code
, shift_mode
,
9334 XEXP (varop
, 1), count
);
9336 varop
= simplify_gen_binary (GET_CODE (varop
), shift_mode
,
9338 varop
= apply_distributive_law (varop
);
9346 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9347 says that the sign bit can be tested, FOO has mode MODE, C is
9348 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9349 that may be nonzero. */
9350 if (code
== LSHIFTRT
9351 && XEXP (varop
, 1) == const0_rtx
9352 && GET_MODE (XEXP (varop
, 0)) == result_mode
9353 && count
== (GET_MODE_BITSIZE (result_mode
) - 1)
9354 && GET_MODE_BITSIZE (result_mode
) <= HOST_BITS_PER_WIDE_INT
9355 && STORE_FLAG_VALUE
== -1
9356 && nonzero_bits (XEXP (varop
, 0), result_mode
) == 1
9357 && merge_outer_ops (&outer_op
, &outer_const
, XOR
,
9358 (HOST_WIDE_INT
) 1, result_mode
,
9361 varop
= XEXP (varop
, 0);
9368 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9369 than the number of bits in the mode is equivalent to A. */
9370 if (code
== LSHIFTRT
9371 && count
== (GET_MODE_BITSIZE (result_mode
) - 1)
9372 && nonzero_bits (XEXP (varop
, 0), result_mode
) == 1)
9374 varop
= XEXP (varop
, 0);
9379 /* NEG commutes with ASHIFT since it is multiplication. Move the
9380 NEG outside to allow shifts to combine. */
9382 && merge_outer_ops (&outer_op
, &outer_const
, NEG
,
9383 (HOST_WIDE_INT
) 0, result_mode
,
9386 varop
= XEXP (varop
, 0);
9392 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9393 is one less than the number of bits in the mode is
9394 equivalent to (xor A 1). */
9395 if (code
== LSHIFTRT
9396 && count
== (GET_MODE_BITSIZE (result_mode
) - 1)
9397 && XEXP (varop
, 1) == constm1_rtx
9398 && nonzero_bits (XEXP (varop
, 0), result_mode
) == 1
9399 && merge_outer_ops (&outer_op
, &outer_const
, XOR
,
9400 (HOST_WIDE_INT
) 1, result_mode
,
9404 varop
= XEXP (varop
, 0);
9408 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9409 that might be nonzero in BAR are those being shifted out and those
9410 bits are known zero in FOO, we can replace the PLUS with FOO.
9411 Similarly in the other operand order. This code occurs when
9412 we are computing the size of a variable-size array. */
9414 if ((code
== ASHIFTRT
|| code
== LSHIFTRT
)
9415 && count
< HOST_BITS_PER_WIDE_INT
9416 && nonzero_bits (XEXP (varop
, 1), result_mode
) >> count
== 0
9417 && (nonzero_bits (XEXP (varop
, 1), result_mode
)
9418 & nonzero_bits (XEXP (varop
, 0), result_mode
)) == 0)
9420 varop
= XEXP (varop
, 0);
9423 else if ((code
== ASHIFTRT
|| code
== LSHIFTRT
)
9424 && count
< HOST_BITS_PER_WIDE_INT
9425 && GET_MODE_BITSIZE (result_mode
) <= HOST_BITS_PER_WIDE_INT
9426 && 0 == (nonzero_bits (XEXP (varop
, 0), result_mode
)
9428 && 0 == (nonzero_bits (XEXP (varop
, 0), result_mode
)
9429 & nonzero_bits (XEXP (varop
, 1),
9432 varop
= XEXP (varop
, 1);
9436 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9438 && GET_CODE (XEXP (varop
, 1)) == CONST_INT
9439 && (new = simplify_const_binary_operation (ASHIFT
, result_mode
,
9441 GEN_INT (count
))) != 0
9442 && GET_CODE (new) == CONST_INT
9443 && merge_outer_ops (&outer_op
, &outer_const
, PLUS
,
9444 INTVAL (new), result_mode
, &complement_p
))
9446 varop
= XEXP (varop
, 0);
9450 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9451 signbit', and attempt to change the PLUS to an XOR and move it to
9452 the outer operation as is done above in the AND/IOR/XOR case
9453 leg for shift(logical). See details in logical handling above
9454 for reasoning in doing so. */
9455 if (code
== LSHIFTRT
9456 && GET_CODE (XEXP (varop
, 1)) == CONST_INT
9457 && mode_signbit_p (result_mode
, XEXP (varop
, 1))
9458 && (new = simplify_const_binary_operation (code
, result_mode
,
9460 GEN_INT (count
))) != 0
9461 && GET_CODE (new) == CONST_INT
9462 && merge_outer_ops (&outer_op
, &outer_const
, XOR
,
9463 INTVAL (new), result_mode
, &complement_p
))
9465 varop
= XEXP (varop
, 0);
9472 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9473 with C the size of VAROP - 1 and the shift is logical if
9474 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9475 we have a (gt X 0) operation. If the shift is arithmetic with
9476 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9477 we have a (neg (gt X 0)) operation. */
9479 if ((STORE_FLAG_VALUE
== 1 || STORE_FLAG_VALUE
== -1)
9480 && GET_CODE (XEXP (varop
, 0)) == ASHIFTRT
9481 && count
== (GET_MODE_BITSIZE (GET_MODE (varop
)) - 1)
9482 && (code
== LSHIFTRT
|| code
== ASHIFTRT
)
9483 && GET_CODE (XEXP (XEXP (varop
, 0), 1)) == CONST_INT
9484 && INTVAL (XEXP (XEXP (varop
, 0), 1)) == count
9485 && rtx_equal_p (XEXP (XEXP (varop
, 0), 0), XEXP (varop
, 1)))
9488 varop
= gen_rtx_GT (GET_MODE (varop
), XEXP (varop
, 1),
9491 if (STORE_FLAG_VALUE
== 1 ? code
== ASHIFTRT
: code
== LSHIFTRT
)
9492 varop
= gen_rtx_NEG (GET_MODE (varop
), varop
);
9499 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9500 if the truncate does not affect the value. */
9501 if (code
== LSHIFTRT
9502 && GET_CODE (XEXP (varop
, 0)) == LSHIFTRT
9503 && GET_CODE (XEXP (XEXP (varop
, 0), 1)) == CONST_INT
9504 && (INTVAL (XEXP (XEXP (varop
, 0), 1))
9505 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop
, 0)))
9506 - GET_MODE_BITSIZE (GET_MODE (varop
)))))
9508 rtx varop_inner
= XEXP (varop
, 0);
9511 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner
),
9512 XEXP (varop_inner
, 0),
9514 (count
+ INTVAL (XEXP (varop_inner
, 1))));
9515 varop
= gen_rtx_TRUNCATE (GET_MODE (varop
), varop_inner
);
9528 /* We need to determine what mode to do the shift in. If the shift is
9529 a right shift or ROTATE, we must always do it in the mode it was
9530 originally done in. Otherwise, we can do it in MODE, the widest mode
9531 encountered. The code we care about is that of the shift that will
9532 actually be done, not the shift that was originally requested. */
9534 = (code
== ASHIFTRT
|| code
== LSHIFTRT
|| code
== ROTATE
9535 ? result_mode
: mode
);
9537 /* We have now finished analyzing the shift. The result should be
9538 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9539 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9540 to the result of the shift. OUTER_CONST is the relevant constant,
9541 but we must turn off all bits turned off in the shift. */
9543 if (outer_op
== UNKNOWN
9544 && orig_code
== code
&& orig_count
== count
9545 && varop
== orig_varop
9546 && shift_mode
== GET_MODE (varop
))
9549 /* Make a SUBREG if necessary. If we can't make it, fail. */
9550 varop
= gen_lowpart (shift_mode
, varop
);
9551 if (varop
== NULL_RTX
|| GET_CODE (varop
) == CLOBBER
)
9554 /* If we have an outer operation and we just made a shift, it is
9555 possible that we could have simplified the shift were it not
9556 for the outer operation. So try to do the simplification
9559 if (outer_op
!= UNKNOWN
)
9560 x
= simplify_shift_const_1 (code
, shift_mode
, varop
, count
);
9565 x
= simplify_gen_binary (code
, shift_mode
, varop
, GEN_INT (count
));
9567 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9568 turn off all the bits that the shift would have turned off. */
9569 if (orig_code
== LSHIFTRT
&& result_mode
!= shift_mode
)
9570 x
= simplify_and_const_int (NULL_RTX
, shift_mode
, x
,
9571 GET_MODE_MASK (result_mode
) >> orig_count
);
9573 /* Do the remainder of the processing in RESULT_MODE. */
9574 x
= gen_lowpart_or_truncate (result_mode
, x
);
9576 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9579 x
= simplify_gen_unary (NOT
, result_mode
, x
, result_mode
);
9581 if (outer_op
!= UNKNOWN
)
9583 if (GET_MODE_BITSIZE (result_mode
) < HOST_BITS_PER_WIDE_INT
)
9584 outer_const
= trunc_int_for_mode (outer_const
, result_mode
);
9586 if (outer_op
== AND
)
9587 x
= simplify_and_const_int (NULL_RTX
, result_mode
, x
, outer_const
);
9588 else if (outer_op
== SET
)
9590 /* This means that we have determined that the result is
9591 equivalent to a constant. This should be rare. */
9592 if (!side_effects_p (x
))
9593 x
= GEN_INT (outer_const
);
9595 else if (GET_RTX_CLASS (outer_op
) == RTX_UNARY
)
9596 x
= simplify_gen_unary (outer_op
, result_mode
, x
, result_mode
);
9598 x
= simplify_gen_binary (outer_op
, result_mode
, x
,
9599 GEN_INT (outer_const
));
9605 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9606 The result of the shift is RESULT_MODE. If we cannot simplify it,
9607 return X or, if it is NULL, synthesize the expression with
9608 simplify_gen_binary. Otherwise, return a simplified value.
9610 The shift is normally computed in the widest mode we find in VAROP, as
9611 long as it isn't a different number of words than RESULT_MODE. Exceptions
9612 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9615 simplify_shift_const (rtx x
, enum rtx_code code
, enum machine_mode result_mode
,
9616 rtx varop
, int count
)
9618 rtx tem
= simplify_shift_const_1 (code
, result_mode
, varop
, count
);
9623 x
= simplify_gen_binary (code
, GET_MODE (varop
), varop
, GEN_INT (count
));
9624 if (GET_MODE (x
) != result_mode
)
9625 x
= gen_lowpart (result_mode
, x
);
9630 /* Like recog, but we receive the address of a pointer to a new pattern.
9631 We try to match the rtx that the pointer points to.
9632 If that fails, we may try to modify or replace the pattern,
9633 storing the replacement into the same pointer object.
9635 Modifications include deletion or addition of CLOBBERs.
9637 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9638 the CLOBBERs are placed.
9640 The value is the final insn code from the pattern ultimately matched,
9644 recog_for_combine (rtx
*pnewpat
, rtx insn
, rtx
*pnotes
)
9647 int insn_code_number
;
9648 int num_clobbers_to_add
= 0;
9651 rtx old_notes
, old_pat
;
9653 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9654 we use to indicate that something didn't match. If we find such a
9655 thing, force rejection. */
9656 if (GET_CODE (pat
) == PARALLEL
)
9657 for (i
= XVECLEN (pat
, 0) - 1; i
>= 0; i
--)
9658 if (GET_CODE (XVECEXP (pat
, 0, i
)) == CLOBBER
9659 && XEXP (XVECEXP (pat
, 0, i
), 0) == const0_rtx
)
9662 old_pat
= PATTERN (insn
);
9663 old_notes
= REG_NOTES (insn
);
9664 PATTERN (insn
) = pat
;
9665 REG_NOTES (insn
) = 0;
9667 insn_code_number
= recog (pat
, insn
, &num_clobbers_to_add
);
9669 /* If it isn't, there is the possibility that we previously had an insn
9670 that clobbered some register as a side effect, but the combined
9671 insn doesn't need to do that. So try once more without the clobbers
9672 unless this represents an ASM insn. */
9674 if (insn_code_number
< 0 && ! check_asm_operands (pat
)
9675 && GET_CODE (pat
) == PARALLEL
)
9679 for (pos
= 0, i
= 0; i
< XVECLEN (pat
, 0); i
++)
9680 if (GET_CODE (XVECEXP (pat
, 0, i
)) != CLOBBER
)
9683 SUBST (XVECEXP (pat
, 0, pos
), XVECEXP (pat
, 0, i
));
9687 SUBST_INT (XVECLEN (pat
, 0), pos
);
9690 pat
= XVECEXP (pat
, 0, 0);
9692 PATTERN (insn
) = pat
;
9693 insn_code_number
= recog (pat
, insn
, &num_clobbers_to_add
);
9695 PATTERN (insn
) = old_pat
;
9696 REG_NOTES (insn
) = old_notes
;
9698 /* Recognize all noop sets, these will be killed by followup pass. */
9699 if (insn_code_number
< 0 && GET_CODE (pat
) == SET
&& set_noop_p (pat
))
9700 insn_code_number
= NOOP_MOVE_INSN_CODE
, num_clobbers_to_add
= 0;
9702 /* If we had any clobbers to add, make a new pattern than contains
9703 them. Then check to make sure that all of them are dead. */
9704 if (num_clobbers_to_add
)
9706 rtx newpat
= gen_rtx_PARALLEL (VOIDmode
,
9707 rtvec_alloc (GET_CODE (pat
) == PARALLEL
9709 + num_clobbers_to_add
)
9710 : num_clobbers_to_add
+ 1));
9712 if (GET_CODE (pat
) == PARALLEL
)
9713 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
9714 XVECEXP (newpat
, 0, i
) = XVECEXP (pat
, 0, i
);
9716 XVECEXP (newpat
, 0, 0) = pat
;
9718 add_clobbers (newpat
, insn_code_number
);
9720 for (i
= XVECLEN (newpat
, 0) - num_clobbers_to_add
;
9721 i
< XVECLEN (newpat
, 0); i
++)
9723 if (REG_P (XEXP (XVECEXP (newpat
, 0, i
), 0))
9724 && ! reg_dead_at_p (XEXP (XVECEXP (newpat
, 0, i
), 0), insn
))
9726 if (GET_CODE (XEXP (XVECEXP (newpat
, 0, i
), 0)) != SCRATCH
)
9728 gcc_assert (REG_P (XEXP (XVECEXP (newpat
, 0, i
), 0)));
9729 notes
= gen_rtx_EXPR_LIST (REG_UNUSED
,
9730 XEXP (XVECEXP (newpat
, 0, i
), 0), notes
);
9739 return insn_code_number
;
9742 /* Like gen_lowpart_general but for use by combine. In combine it
9743 is not possible to create any new pseudoregs. However, it is
9744 safe to create invalid memory addresses, because combine will
9745 try to recognize them and all they will do is make the combine
9748 If for some reason this cannot do its job, an rtx
9749 (clobber (const_int 0)) is returned.
9750 An insn containing that will not be recognized. */
9753 gen_lowpart_for_combine (enum machine_mode omode
, rtx x
)
9755 enum machine_mode imode
= GET_MODE (x
);
9756 unsigned int osize
= GET_MODE_SIZE (omode
);
9757 unsigned int isize
= GET_MODE_SIZE (imode
);
9763 /* Return identity if this is a CONST or symbolic reference. */
9765 && (GET_CODE (x
) == CONST
9766 || GET_CODE (x
) == SYMBOL_REF
9767 || GET_CODE (x
) == LABEL_REF
))
9770 /* We can only support MODE being wider than a word if X is a
9771 constant integer or has a mode the same size. */
9772 if (GET_MODE_SIZE (omode
) > UNITS_PER_WORD
9773 && ! ((imode
== VOIDmode
9774 && (GET_CODE (x
) == CONST_INT
9775 || GET_CODE (x
) == CONST_DOUBLE
))
9779 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9780 won't know what to do. So we will strip off the SUBREG here and
9781 process normally. */
9782 if (GET_CODE (x
) == SUBREG
&& MEM_P (SUBREG_REG (x
)))
9786 /* For use in case we fall down into the address adjustments
9787 further below, we need to adjust the known mode and size of
9788 x; imode and isize, since we just adjusted x. */
9789 imode
= GET_MODE (x
);
9794 isize
= GET_MODE_SIZE (imode
);
9797 result
= gen_lowpart_common (omode
, x
);
9806 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9808 if (MEM_VOLATILE_P (x
) || mode_dependent_address_p (XEXP (x
, 0)))
9811 /* If we want to refer to something bigger than the original memref,
9812 generate a paradoxical subreg instead. That will force a reload
9813 of the original memref X. */
9815 return gen_rtx_SUBREG (omode
, x
, 0);
9817 if (WORDS_BIG_ENDIAN
)
9818 offset
= MAX (isize
, UNITS_PER_WORD
) - MAX (osize
, UNITS_PER_WORD
);
9820 /* Adjust the address so that the address-after-the-data is
9822 if (BYTES_BIG_ENDIAN
)
9823 offset
-= MIN (UNITS_PER_WORD
, osize
) - MIN (UNITS_PER_WORD
, isize
);
9825 return adjust_address_nv (x
, omode
, offset
);
9828 /* If X is a comparison operator, rewrite it in a new mode. This
9829 probably won't match, but may allow further simplifications. */
9830 else if (COMPARISON_P (x
))
9831 return gen_rtx_fmt_ee (GET_CODE (x
), omode
, XEXP (x
, 0), XEXP (x
, 1));
9833 /* If we couldn't simplify X any other way, just enclose it in a
9834 SUBREG. Normally, this SUBREG won't match, but some patterns may
9835 include an explicit SUBREG or we may simplify it further in combine. */
9841 offset
= subreg_lowpart_offset (omode
, imode
);
9842 if (imode
== VOIDmode
)
9844 imode
= int_mode_for_mode (omode
);
9845 x
= gen_lowpart_common (imode
, x
);
9849 res
= simplify_gen_subreg (omode
, x
, imode
, offset
);
9855 return gen_rtx_CLOBBER (imode
, const0_rtx
);
9858 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9859 comparison code that will be tested.
9861 The result is a possibly different comparison code to use. *POP0 and
9862 *POP1 may be updated.
9864 It is possible that we might detect that a comparison is either always
9865 true or always false. However, we do not perform general constant
9866 folding in combine, so this knowledge isn't useful. Such tautologies
9867 should have been detected earlier. Hence we ignore all such cases. */
9869 static enum rtx_code
9870 simplify_comparison (enum rtx_code code
, rtx
*pop0
, rtx
*pop1
)
9876 enum machine_mode mode
, tmode
;
9878 /* Try a few ways of applying the same transformation to both operands. */
9881 #ifndef WORD_REGISTER_OPERATIONS
9882 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9883 so check specially. */
9884 if (code
!= GTU
&& code
!= GEU
&& code
!= LTU
&& code
!= LEU
9885 && GET_CODE (op0
) == ASHIFTRT
&& GET_CODE (op1
) == ASHIFTRT
9886 && GET_CODE (XEXP (op0
, 0)) == ASHIFT
9887 && GET_CODE (XEXP (op1
, 0)) == ASHIFT
9888 && GET_CODE (XEXP (XEXP (op0
, 0), 0)) == SUBREG
9889 && GET_CODE (XEXP (XEXP (op1
, 0), 0)) == SUBREG
9890 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0
, 0), 0)))
9891 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1
, 0), 0))))
9892 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
9893 && XEXP (op0
, 1) == XEXP (op1
, 1)
9894 && XEXP (op0
, 1) == XEXP (XEXP (op0
, 0), 1)
9895 && XEXP (op0
, 1) == XEXP (XEXP (op1
, 0), 1)
9896 && (INTVAL (XEXP (op0
, 1))
9897 == (GET_MODE_BITSIZE (GET_MODE (op0
))
9899 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0
, 0), 0))))))))
9901 op0
= SUBREG_REG (XEXP (XEXP (op0
, 0), 0));
9902 op1
= SUBREG_REG (XEXP (XEXP (op1
, 0), 0));
9906 /* If both operands are the same constant shift, see if we can ignore the
9907 shift. We can if the shift is a rotate or if the bits shifted out of
9908 this shift are known to be zero for both inputs and if the type of
9909 comparison is compatible with the shift. */
9910 if (GET_CODE (op0
) == GET_CODE (op1
)
9911 && GET_MODE_BITSIZE (GET_MODE (op0
)) <= HOST_BITS_PER_WIDE_INT
9912 && ((GET_CODE (op0
) == ROTATE
&& (code
== NE
|| code
== EQ
))
9913 || ((GET_CODE (op0
) == LSHIFTRT
|| GET_CODE (op0
) == ASHIFT
)
9914 && (code
!= GT
&& code
!= LT
&& code
!= GE
&& code
!= LE
))
9915 || (GET_CODE (op0
) == ASHIFTRT
9916 && (code
!= GTU
&& code
!= LTU
9917 && code
!= GEU
&& code
!= LEU
)))
9918 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
9919 && INTVAL (XEXP (op0
, 1)) >= 0
9920 && INTVAL (XEXP (op0
, 1)) < HOST_BITS_PER_WIDE_INT
9921 && XEXP (op0
, 1) == XEXP (op1
, 1))
9923 enum machine_mode mode
= GET_MODE (op0
);
9924 unsigned HOST_WIDE_INT mask
= GET_MODE_MASK (mode
);
9925 int shift_count
= INTVAL (XEXP (op0
, 1));
9927 if (GET_CODE (op0
) == LSHIFTRT
|| GET_CODE (op0
) == ASHIFTRT
)
9928 mask
&= (mask
>> shift_count
) << shift_count
;
9929 else if (GET_CODE (op0
) == ASHIFT
)
9930 mask
= (mask
& (mask
<< shift_count
)) >> shift_count
;
9932 if ((nonzero_bits (XEXP (op0
, 0), mode
) & ~mask
) == 0
9933 && (nonzero_bits (XEXP (op1
, 0), mode
) & ~mask
) == 0)
9934 op0
= XEXP (op0
, 0), op1
= XEXP (op1
, 0);
9939 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9940 SUBREGs are of the same mode, and, in both cases, the AND would
9941 be redundant if the comparison was done in the narrower mode,
9942 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9943 and the operand's possibly nonzero bits are 0xffffff01; in that case
9944 if we only care about QImode, we don't need the AND). This case
9945 occurs if the output mode of an scc insn is not SImode and
9946 STORE_FLAG_VALUE == 1 (e.g., the 386).
9948 Similarly, check for a case where the AND's are ZERO_EXTEND
9949 operations from some narrower mode even though a SUBREG is not
9952 else if (GET_CODE (op0
) == AND
&& GET_CODE (op1
) == AND
9953 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
9954 && GET_CODE (XEXP (op1
, 1)) == CONST_INT
)
9956 rtx inner_op0
= XEXP (op0
, 0);
9957 rtx inner_op1
= XEXP (op1
, 0);
9958 HOST_WIDE_INT c0
= INTVAL (XEXP (op0
, 1));
9959 HOST_WIDE_INT c1
= INTVAL (XEXP (op1
, 1));
9962 if (GET_CODE (inner_op0
) == SUBREG
&& GET_CODE (inner_op1
) == SUBREG
9963 && (GET_MODE_SIZE (GET_MODE (inner_op0
))
9964 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0
))))
9965 && (GET_MODE (SUBREG_REG (inner_op0
))
9966 == GET_MODE (SUBREG_REG (inner_op1
)))
9967 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0
)))
9968 <= HOST_BITS_PER_WIDE_INT
)
9969 && (0 == ((~c0
) & nonzero_bits (SUBREG_REG (inner_op0
),
9970 GET_MODE (SUBREG_REG (inner_op0
)))))
9971 && (0 == ((~c1
) & nonzero_bits (SUBREG_REG (inner_op1
),
9972 GET_MODE (SUBREG_REG (inner_op1
))))))
9974 op0
= SUBREG_REG (inner_op0
);
9975 op1
= SUBREG_REG (inner_op1
);
9977 /* The resulting comparison is always unsigned since we masked
9978 off the original sign bit. */
9979 code
= unsigned_condition (code
);
9985 for (tmode
= GET_CLASS_NARROWEST_MODE
9986 (GET_MODE_CLASS (GET_MODE (op0
)));
9987 tmode
!= GET_MODE (op0
); tmode
= GET_MODE_WIDER_MODE (tmode
))
9988 if ((unsigned HOST_WIDE_INT
) c0
== GET_MODE_MASK (tmode
))
9990 op0
= gen_lowpart (tmode
, inner_op0
);
9991 op1
= gen_lowpart (tmode
, inner_op1
);
9992 code
= unsigned_condition (code
);
10001 /* If both operands are NOT, we can strip off the outer operation
10002 and adjust the comparison code for swapped operands; similarly for
10003 NEG, except that this must be an equality comparison. */
10004 else if ((GET_CODE (op0
) == NOT
&& GET_CODE (op1
) == NOT
)
10005 || (GET_CODE (op0
) == NEG
&& GET_CODE (op1
) == NEG
10006 && (code
== EQ
|| code
== NE
)))
10007 op0
= XEXP (op0
, 0), op1
= XEXP (op1
, 0), code
= swap_condition (code
);
10013 /* If the first operand is a constant, swap the operands and adjust the
10014 comparison code appropriately, but don't do this if the second operand
10015 is already a constant integer. */
10016 if (swap_commutative_operands_p (op0
, op1
))
10018 tem
= op0
, op0
= op1
, op1
= tem
;
10019 code
= swap_condition (code
);
10022 /* We now enter a loop during which we will try to simplify the comparison.
10023 For the most part, we only are concerned with comparisons with zero,
10024 but some things may really be comparisons with zero but not start
10025 out looking that way. */
10027 while (GET_CODE (op1
) == CONST_INT
)
10029 enum machine_mode mode
= GET_MODE (op0
);
10030 unsigned int mode_width
= GET_MODE_BITSIZE (mode
);
10031 unsigned HOST_WIDE_INT mask
= GET_MODE_MASK (mode
);
10032 int equality_comparison_p
;
10033 int sign_bit_comparison_p
;
10034 int unsigned_comparison_p
;
10035 HOST_WIDE_INT const_op
;
10037 /* We only want to handle integral modes. This catches VOIDmode,
10038 CCmode, and the floating-point modes. An exception is that we
10039 can handle VOIDmode if OP0 is a COMPARE or a comparison
10042 if (GET_MODE_CLASS (mode
) != MODE_INT
10043 && ! (mode
== VOIDmode
10044 && (GET_CODE (op0
) == COMPARE
|| COMPARISON_P (op0
))))
10047 /* Get the constant we are comparing against and turn off all bits
10048 not on in our mode. */
10049 const_op
= INTVAL (op1
);
10050 if (mode
!= VOIDmode
)
10051 const_op
= trunc_int_for_mode (const_op
, mode
);
10052 op1
= GEN_INT (const_op
);
10054 /* If we are comparing against a constant power of two and the value
10055 being compared can only have that single bit nonzero (e.g., it was
10056 `and'ed with that bit), we can replace this with a comparison
10059 && (code
== EQ
|| code
== NE
|| code
== GE
|| code
== GEU
10060 || code
== LT
|| code
== LTU
)
10061 && mode_width
<= HOST_BITS_PER_WIDE_INT
10062 && exact_log2 (const_op
) >= 0
10063 && nonzero_bits (op0
, mode
) == (unsigned HOST_WIDE_INT
) const_op
)
10065 code
= (code
== EQ
|| code
== GE
|| code
== GEU
? NE
: EQ
);
10066 op1
= const0_rtx
, const_op
= 0;
10069 /* Similarly, if we are comparing a value known to be either -1 or
10070 0 with -1, change it to the opposite comparison against zero. */
10073 && (code
== EQ
|| code
== NE
|| code
== GT
|| code
== LE
10074 || code
== GEU
|| code
== LTU
)
10075 && num_sign_bit_copies (op0
, mode
) == mode_width
)
10077 code
= (code
== EQ
|| code
== LE
|| code
== GEU
? NE
: EQ
);
10078 op1
= const0_rtx
, const_op
= 0;
10081 /* Do some canonicalizations based on the comparison code. We prefer
10082 comparisons against zero and then prefer equality comparisons.
10083 If we can reduce the size of a constant, we will do that too. */
10088 /* < C is equivalent to <= (C - 1) */
10092 op1
= GEN_INT (const_op
);
10094 /* ... fall through to LE case below. */
10100 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10104 op1
= GEN_INT (const_op
);
10108 /* If we are doing a <= 0 comparison on a value known to have
10109 a zero sign bit, we can replace this with == 0. */
10110 else if (const_op
== 0
10111 && mode_width
<= HOST_BITS_PER_WIDE_INT
10112 && (nonzero_bits (op0
, mode
)
10113 & ((HOST_WIDE_INT
) 1 << (mode_width
- 1))) == 0)
10118 /* >= C is equivalent to > (C - 1). */
10122 op1
= GEN_INT (const_op
);
10124 /* ... fall through to GT below. */
10130 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10134 op1
= GEN_INT (const_op
);
10138 /* If we are doing a > 0 comparison on a value known to have
10139 a zero sign bit, we can replace this with != 0. */
10140 else if (const_op
== 0
10141 && mode_width
<= HOST_BITS_PER_WIDE_INT
10142 && (nonzero_bits (op0
, mode
)
10143 & ((HOST_WIDE_INT
) 1 << (mode_width
- 1))) == 0)
10148 /* < C is equivalent to <= (C - 1). */
10152 op1
= GEN_INT (const_op
);
10154 /* ... fall through ... */
10157 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10158 else if ((mode_width
<= HOST_BITS_PER_WIDE_INT
)
10159 && (const_op
== (HOST_WIDE_INT
) 1 << (mode_width
- 1)))
10161 const_op
= 0, op1
= const0_rtx
;
10169 /* unsigned <= 0 is equivalent to == 0 */
10173 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10174 else if ((mode_width
<= HOST_BITS_PER_WIDE_INT
)
10175 && (const_op
== ((HOST_WIDE_INT
) 1 << (mode_width
- 1)) - 1))
10177 const_op
= 0, op1
= const0_rtx
;
10183 /* >= C is equivalent to > (C - 1). */
10187 op1
= GEN_INT (const_op
);
10189 /* ... fall through ... */
10192 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10193 else if ((mode_width
<= HOST_BITS_PER_WIDE_INT
)
10194 && (const_op
== (HOST_WIDE_INT
) 1 << (mode_width
- 1)))
10196 const_op
= 0, op1
= const0_rtx
;
10204 /* unsigned > 0 is equivalent to != 0 */
10208 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10209 else if ((mode_width
<= HOST_BITS_PER_WIDE_INT
)
10210 && (const_op
== ((HOST_WIDE_INT
) 1 << (mode_width
- 1)) - 1))
10212 const_op
= 0, op1
= const0_rtx
;
10221 /* Compute some predicates to simplify code below. */
10223 equality_comparison_p
= (code
== EQ
|| code
== NE
);
10224 sign_bit_comparison_p
= ((code
== LT
|| code
== GE
) && const_op
== 0);
10225 unsigned_comparison_p
= (code
== LTU
|| code
== LEU
|| code
== GTU
10228 /* If this is a sign bit comparison and we can do arithmetic in
10229 MODE, say that we will only be needing the sign bit of OP0. */
10230 if (sign_bit_comparison_p
10231 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
10232 op0
= force_to_mode (op0
, mode
,
10234 << (GET_MODE_BITSIZE (mode
) - 1)),
10237 /* Now try cases based on the opcode of OP0. If none of the cases
10238 does a "continue", we exit this loop immediately after the
10241 switch (GET_CODE (op0
))
10244 /* If we are extracting a single bit from a variable position in
10245 a constant that has only a single bit set and are comparing it
10246 with zero, we can convert this into an equality comparison
10247 between the position and the location of the single bit. */
10248 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10249 have already reduced the shift count modulo the word size. */
10250 if (!SHIFT_COUNT_TRUNCATED
10251 && GET_CODE (XEXP (op0
, 0)) == CONST_INT
10252 && XEXP (op0
, 1) == const1_rtx
10253 && equality_comparison_p
&& const_op
== 0
10254 && (i
= exact_log2 (INTVAL (XEXP (op0
, 0)))) >= 0)
10256 if (BITS_BIG_ENDIAN
)
10258 enum machine_mode new_mode
10259 = mode_for_extraction (EP_extzv
, 1);
10260 if (new_mode
== MAX_MACHINE_MODE
)
10261 i
= BITS_PER_WORD
- 1 - i
;
10265 i
= (GET_MODE_BITSIZE (mode
) - 1 - i
);
10269 op0
= XEXP (op0
, 2);
10273 /* Result is nonzero iff shift count is equal to I. */
10274 code
= reverse_condition (code
);
10278 /* ... fall through ... */
10281 tem
= expand_compound_operation (op0
);
10290 /* If testing for equality, we can take the NOT of the constant. */
10291 if (equality_comparison_p
10292 && (tem
= simplify_unary_operation (NOT
, mode
, op1
, mode
)) != 0)
10294 op0
= XEXP (op0
, 0);
10299 /* If just looking at the sign bit, reverse the sense of the
10301 if (sign_bit_comparison_p
)
10303 op0
= XEXP (op0
, 0);
10304 code
= (code
== GE
? LT
: GE
);
10310 /* If testing for equality, we can take the NEG of the constant. */
10311 if (equality_comparison_p
10312 && (tem
= simplify_unary_operation (NEG
, mode
, op1
, mode
)) != 0)
10314 op0
= XEXP (op0
, 0);
10319 /* The remaining cases only apply to comparisons with zero. */
10323 /* When X is ABS or is known positive,
10324 (neg X) is < 0 if and only if X != 0. */
10326 if (sign_bit_comparison_p
10327 && (GET_CODE (XEXP (op0
, 0)) == ABS
10328 || (mode_width
<= HOST_BITS_PER_WIDE_INT
10329 && (nonzero_bits (XEXP (op0
, 0), mode
)
10330 & ((HOST_WIDE_INT
) 1 << (mode_width
- 1))) == 0)))
10332 op0
= XEXP (op0
, 0);
10333 code
= (code
== LT
? NE
: EQ
);
10337 /* If we have NEG of something whose two high-order bits are the
10338 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10339 if (num_sign_bit_copies (op0
, mode
) >= 2)
10341 op0
= XEXP (op0
, 0);
10342 code
= swap_condition (code
);
10348 /* If we are testing equality and our count is a constant, we
10349 can perform the inverse operation on our RHS. */
10350 if (equality_comparison_p
&& GET_CODE (XEXP (op0
, 1)) == CONST_INT
10351 && (tem
= simplify_binary_operation (ROTATERT
, mode
,
10352 op1
, XEXP (op0
, 1))) != 0)
10354 op0
= XEXP (op0
, 0);
10359 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10360 a particular bit. Convert it to an AND of a constant of that
10361 bit. This will be converted into a ZERO_EXTRACT. */
10362 if (const_op
== 0 && sign_bit_comparison_p
10363 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10364 && mode_width
<= HOST_BITS_PER_WIDE_INT
)
10366 op0
= simplify_and_const_int (NULL_RTX
, mode
, XEXP (op0
, 0),
10369 - INTVAL (XEXP (op0
, 1)))));
10370 code
= (code
== LT
? NE
: EQ
);
10374 /* Fall through. */
10377 /* ABS is ignorable inside an equality comparison with zero. */
10378 if (const_op
== 0 && equality_comparison_p
)
10380 op0
= XEXP (op0
, 0);
10386 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10387 (compare FOO CONST) if CONST fits in FOO's mode and we
10388 are either testing inequality or have an unsigned
10389 comparison with ZERO_EXTEND or a signed comparison with
10390 SIGN_EXTEND. But don't do it if we don't have a compare
10391 insn of the given mode, since we'd have to revert it
10392 later on, and then we wouldn't know whether to sign- or
10394 mode
= GET_MODE (XEXP (op0
, 0));
10395 if (mode
!= VOIDmode
&& GET_MODE_CLASS (mode
) == MODE_INT
10396 && ! unsigned_comparison_p
10397 && (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
10398 && ((unsigned HOST_WIDE_INT
) const_op
10399 < (((unsigned HOST_WIDE_INT
) 1
10400 << (GET_MODE_BITSIZE (mode
) - 1))))
10401 && cmp_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
10403 op0
= XEXP (op0
, 0);
10409 /* Check for the case where we are comparing A - C1 with C2, that is
10411 (subreg:MODE (plus (A) (-C1))) op (C2)
10413 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10414 comparison in the wider mode. One of the following two conditions
10415 must be true in order for this to be valid:
10417 1. The mode extension results in the same bit pattern being added
10418 on both sides and the comparison is equality or unsigned. As
10419 C2 has been truncated to fit in MODE, the pattern can only be
10422 2. The mode extension results in the sign bit being copied on
10425 The difficulty here is that we have predicates for A but not for
10426 (A - C1) so we need to check that C1 is within proper bounds so
10427 as to perturbate A as little as possible. */
10429 if (mode_width
<= HOST_BITS_PER_WIDE_INT
10430 && subreg_lowpart_p (op0
)
10431 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0
))) > mode_width
10432 && GET_CODE (SUBREG_REG (op0
)) == PLUS
10433 && GET_CODE (XEXP (SUBREG_REG (op0
), 1)) == CONST_INT
)
10435 enum machine_mode inner_mode
= GET_MODE (SUBREG_REG (op0
));
10436 rtx a
= XEXP (SUBREG_REG (op0
), 0);
10437 HOST_WIDE_INT c1
= -INTVAL (XEXP (SUBREG_REG (op0
), 1));
10440 && (unsigned HOST_WIDE_INT
) c1
10441 < (unsigned HOST_WIDE_INT
) 1 << (mode_width
- 1)
10442 && (equality_comparison_p
|| unsigned_comparison_p
)
10443 /* (A - C1) zero-extends if it is positive and sign-extends
10444 if it is negative, C2 both zero- and sign-extends. */
10445 && ((0 == (nonzero_bits (a
, inner_mode
)
10446 & ~GET_MODE_MASK (mode
))
10448 /* (A - C1) sign-extends if it is positive and 1-extends
10449 if it is negative, C2 both sign- and 1-extends. */
10450 || (num_sign_bit_copies (a
, inner_mode
)
10451 > (unsigned int) (GET_MODE_BITSIZE (inner_mode
)
10454 || ((unsigned HOST_WIDE_INT
) c1
10455 < (unsigned HOST_WIDE_INT
) 1 << (mode_width
- 2)
10456 /* (A - C1) always sign-extends, like C2. */
10457 && num_sign_bit_copies (a
, inner_mode
)
10458 > (unsigned int) (GET_MODE_BITSIZE (inner_mode
)
10459 - (mode_width
- 1))))
10461 op0
= SUBREG_REG (op0
);
10466 /* If the inner mode is narrower and we are extracting the low part,
10467 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10468 if (subreg_lowpart_p (op0
)
10469 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0
))) < mode_width
)
10470 /* Fall through */ ;
10474 /* ... fall through ... */
10477 mode
= GET_MODE (XEXP (op0
, 0));
10478 if (mode
!= VOIDmode
&& GET_MODE_CLASS (mode
) == MODE_INT
10479 && (unsigned_comparison_p
|| equality_comparison_p
)
10480 && (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
10481 && ((unsigned HOST_WIDE_INT
) const_op
< GET_MODE_MASK (mode
))
10482 && cmp_optab
->handlers
[(int) mode
].insn_code
!= CODE_FOR_nothing
)
10484 op0
= XEXP (op0
, 0);
10490 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10491 this for equality comparisons due to pathological cases involving
10493 if (equality_comparison_p
10494 && 0 != (tem
= simplify_binary_operation (MINUS
, mode
,
10495 op1
, XEXP (op0
, 1))))
10497 op0
= XEXP (op0
, 0);
10502 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10503 if (const_op
== 0 && XEXP (op0
, 1) == constm1_rtx
10504 && GET_CODE (XEXP (op0
, 0)) == ABS
&& sign_bit_comparison_p
)
10506 op0
= XEXP (XEXP (op0
, 0), 0);
10507 code
= (code
== LT
? EQ
: NE
);
10513 /* We used to optimize signed comparisons against zero, but that
10514 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10515 arrive here as equality comparisons, or (GEU, LTU) are
10516 optimized away. No need to special-case them. */
10518 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10519 (eq B (minus A C)), whichever simplifies. We can only do
10520 this for equality comparisons due to pathological cases involving
10522 if (equality_comparison_p
10523 && 0 != (tem
= simplify_binary_operation (PLUS
, mode
,
10524 XEXP (op0
, 1), op1
)))
10526 op0
= XEXP (op0
, 0);
10531 if (equality_comparison_p
10532 && 0 != (tem
= simplify_binary_operation (MINUS
, mode
,
10533 XEXP (op0
, 0), op1
)))
10535 op0
= XEXP (op0
, 1);
10540 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10541 of bits in X minus 1, is one iff X > 0. */
10542 if (sign_bit_comparison_p
&& GET_CODE (XEXP (op0
, 0)) == ASHIFTRT
10543 && GET_CODE (XEXP (XEXP (op0
, 0), 1)) == CONST_INT
10544 && (unsigned HOST_WIDE_INT
) INTVAL (XEXP (XEXP (op0
, 0), 1))
10546 && rtx_equal_p (XEXP (XEXP (op0
, 0), 0), XEXP (op0
, 1)))
10548 op0
= XEXP (op0
, 1);
10549 code
= (code
== GE
? LE
: GT
);
10555 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10556 if C is zero or B is a constant. */
10557 if (equality_comparison_p
10558 && 0 != (tem
= simplify_binary_operation (XOR
, mode
,
10559 XEXP (op0
, 1), op1
)))
10561 op0
= XEXP (op0
, 0);
10568 case UNEQ
: case LTGT
:
10569 case LT
: case LTU
: case UNLT
: case LE
: case LEU
: case UNLE
:
10570 case GT
: case GTU
: case UNGT
: case GE
: case GEU
: case UNGE
:
10571 case UNORDERED
: case ORDERED
:
10572 /* We can't do anything if OP0 is a condition code value, rather
10573 than an actual data value. */
10575 || CC0_P (XEXP (op0
, 0))
10576 || GET_MODE_CLASS (GET_MODE (XEXP (op0
, 0))) == MODE_CC
)
10579 /* Get the two operands being compared. */
10580 if (GET_CODE (XEXP (op0
, 0)) == COMPARE
)
10581 tem
= XEXP (XEXP (op0
, 0), 0), tem1
= XEXP (XEXP (op0
, 0), 1);
10583 tem
= XEXP (op0
, 0), tem1
= XEXP (op0
, 1);
10585 /* Check for the cases where we simply want the result of the
10586 earlier test or the opposite of that result. */
10587 if (code
== NE
|| code
== EQ
10588 || (GET_MODE_BITSIZE (GET_MODE (op0
)) <= HOST_BITS_PER_WIDE_INT
10589 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_INT
10590 && (STORE_FLAG_VALUE
10591 & (((HOST_WIDE_INT
) 1
10592 << (GET_MODE_BITSIZE (GET_MODE (op0
)) - 1))))
10593 && (code
== LT
|| code
== GE
)))
10595 enum rtx_code new_code
;
10596 if (code
== LT
|| code
== NE
)
10597 new_code
= GET_CODE (op0
);
10599 new_code
= reversed_comparison_code (op0
, NULL
);
10601 if (new_code
!= UNKNOWN
)
10612 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10614 if (sign_bit_comparison_p
&& GET_CODE (XEXP (op0
, 0)) == PLUS
10615 && XEXP (XEXP (op0
, 0), 1) == constm1_rtx
10616 && rtx_equal_p (XEXP (XEXP (op0
, 0), 0), XEXP (op0
, 1)))
10618 op0
= XEXP (op0
, 1);
10619 code
= (code
== GE
? GT
: LE
);
10625 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10626 will be converted to a ZERO_EXTRACT later. */
10627 if (const_op
== 0 && equality_comparison_p
10628 && GET_CODE (XEXP (op0
, 0)) == ASHIFT
10629 && XEXP (XEXP (op0
, 0), 0) == const1_rtx
)
10631 op0
= simplify_and_const_int
10632 (NULL_RTX
, mode
, gen_rtx_LSHIFTRT (mode
,
10634 XEXP (XEXP (op0
, 0), 1)),
10635 (HOST_WIDE_INT
) 1);
10639 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10640 zero and X is a comparison and C1 and C2 describe only bits set
10641 in STORE_FLAG_VALUE, we can compare with X. */
10642 if (const_op
== 0 && equality_comparison_p
10643 && mode_width
<= HOST_BITS_PER_WIDE_INT
10644 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10645 && GET_CODE (XEXP (op0
, 0)) == LSHIFTRT
10646 && GET_CODE (XEXP (XEXP (op0
, 0), 1)) == CONST_INT
10647 && INTVAL (XEXP (XEXP (op0
, 0), 1)) >= 0
10648 && INTVAL (XEXP (XEXP (op0
, 0), 1)) < HOST_BITS_PER_WIDE_INT
)
10650 mask
= ((INTVAL (XEXP (op0
, 1)) & GET_MODE_MASK (mode
))
10651 << INTVAL (XEXP (XEXP (op0
, 0), 1)));
10652 if ((~STORE_FLAG_VALUE
& mask
) == 0
10653 && (COMPARISON_P (XEXP (XEXP (op0
, 0), 0))
10654 || ((tem
= get_last_value (XEXP (XEXP (op0
, 0), 0))) != 0
10655 && COMPARISON_P (tem
))))
10657 op0
= XEXP (XEXP (op0
, 0), 0);
10662 /* If we are doing an equality comparison of an AND of a bit equal
10663 to the sign bit, replace this with a LT or GE comparison of
10664 the underlying value. */
10665 if (equality_comparison_p
10667 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10668 && mode_width
<= HOST_BITS_PER_WIDE_INT
10669 && ((INTVAL (XEXP (op0
, 1)) & GET_MODE_MASK (mode
))
10670 == (unsigned HOST_WIDE_INT
) 1 << (mode_width
- 1)))
10672 op0
= XEXP (op0
, 0);
10673 code
= (code
== EQ
? GE
: LT
);
10677 /* If this AND operation is really a ZERO_EXTEND from a narrower
10678 mode, the constant fits within that mode, and this is either an
10679 equality or unsigned comparison, try to do this comparison in
10684 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10685 -> (ne:DI (reg:SI 4) (const_int 0))
10687 unless TRULY_NOOP_TRUNCATION allows it or the register is
10688 known to hold a value of the required mode the
10689 transformation is invalid. */
10690 if ((equality_comparison_p
|| unsigned_comparison_p
)
10691 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10692 && (i
= exact_log2 ((INTVAL (XEXP (op0
, 1))
10693 & GET_MODE_MASK (mode
))
10695 && const_op
>> i
== 0
10696 && (tmode
= mode_for_size (i
, MODE_INT
, 1)) != BLKmode
10697 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode
),
10698 GET_MODE_BITSIZE (GET_MODE (op0
)))
10699 || (REG_P (XEXP (op0
, 0))
10700 && reg_truncated_to_mode (tmode
, XEXP (op0
, 0)))))
10702 op0
= gen_lowpart (tmode
, XEXP (op0
, 0));
10706 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10707 fits in both M1 and M2 and the SUBREG is either paradoxical
10708 or represents the low part, permute the SUBREG and the AND
10710 if (GET_CODE (XEXP (op0
, 0)) == SUBREG
)
10712 unsigned HOST_WIDE_INT c1
;
10713 tmode
= GET_MODE (SUBREG_REG (XEXP (op0
, 0)));
10714 /* Require an integral mode, to avoid creating something like
10716 if (SCALAR_INT_MODE_P (tmode
)
10717 /* It is unsafe to commute the AND into the SUBREG if the
10718 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10719 not defined. As originally written the upper bits
10720 have a defined value due to the AND operation.
10721 However, if we commute the AND inside the SUBREG then
10722 they no longer have defined values and the meaning of
10723 the code has been changed. */
10725 #ifdef WORD_REGISTER_OPERATIONS
10726 || (mode_width
> GET_MODE_BITSIZE (tmode
)
10727 && mode_width
<= BITS_PER_WORD
)
10729 || (mode_width
<= GET_MODE_BITSIZE (tmode
)
10730 && subreg_lowpart_p (XEXP (op0
, 0))))
10731 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10732 && mode_width
<= HOST_BITS_PER_WIDE_INT
10733 && GET_MODE_BITSIZE (tmode
) <= HOST_BITS_PER_WIDE_INT
10734 && ((c1
= INTVAL (XEXP (op0
, 1))) & ~mask
) == 0
10735 && (c1
& ~GET_MODE_MASK (tmode
)) == 0
10737 && c1
!= GET_MODE_MASK (tmode
))
10739 op0
= simplify_gen_binary (AND
, tmode
,
10740 SUBREG_REG (XEXP (op0
, 0)),
10741 gen_int_mode (c1
, tmode
));
10742 op0
= gen_lowpart (mode
, op0
);
10747 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10748 if (const_op
== 0 && equality_comparison_p
10749 && XEXP (op0
, 1) == const1_rtx
10750 && GET_CODE (XEXP (op0
, 0)) == NOT
)
10752 op0
= simplify_and_const_int
10753 (NULL_RTX
, mode
, XEXP (XEXP (op0
, 0), 0), (HOST_WIDE_INT
) 1);
10754 code
= (code
== NE
? EQ
: NE
);
10758 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10759 (eq (and (lshiftrt X) 1) 0).
10760 Also handle the case where (not X) is expressed using xor. */
10761 if (const_op
== 0 && equality_comparison_p
10762 && XEXP (op0
, 1) == const1_rtx
10763 && GET_CODE (XEXP (op0
, 0)) == LSHIFTRT
)
10765 rtx shift_op
= XEXP (XEXP (op0
, 0), 0);
10766 rtx shift_count
= XEXP (XEXP (op0
, 0), 1);
10768 if (GET_CODE (shift_op
) == NOT
10769 || (GET_CODE (shift_op
) == XOR
10770 && GET_CODE (XEXP (shift_op
, 1)) == CONST_INT
10771 && GET_CODE (shift_count
) == CONST_INT
10772 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
10773 && (INTVAL (XEXP (shift_op
, 1))
10774 == (HOST_WIDE_INT
) 1 << INTVAL (shift_count
))))
10776 op0
= simplify_and_const_int
10778 gen_rtx_LSHIFTRT (mode
, XEXP (shift_op
, 0), shift_count
),
10779 (HOST_WIDE_INT
) 1);
10780 code
= (code
== NE
? EQ
: NE
);
10787 /* If we have (compare (ashift FOO N) (const_int C)) and
10788 the high order N bits of FOO (N+1 if an inequality comparison)
10789 are known to be zero, we can do this by comparing FOO with C
10790 shifted right N bits so long as the low-order N bits of C are
10792 if (GET_CODE (XEXP (op0
, 1)) == CONST_INT
10793 && INTVAL (XEXP (op0
, 1)) >= 0
10794 && ((INTVAL (XEXP (op0
, 1)) + ! equality_comparison_p
)
10795 < HOST_BITS_PER_WIDE_INT
)
10797 & (((HOST_WIDE_INT
) 1 << INTVAL (XEXP (op0
, 1))) - 1)) == 0)
10798 && mode_width
<= HOST_BITS_PER_WIDE_INT
10799 && (nonzero_bits (XEXP (op0
, 0), mode
)
10800 & ~(mask
>> (INTVAL (XEXP (op0
, 1))
10801 + ! equality_comparison_p
))) == 0)
10803 /* We must perform a logical shift, not an arithmetic one,
10804 as we want the top N bits of C to be zero. */
10805 unsigned HOST_WIDE_INT temp
= const_op
& GET_MODE_MASK (mode
);
10807 temp
>>= INTVAL (XEXP (op0
, 1));
10808 op1
= gen_int_mode (temp
, mode
);
10809 op0
= XEXP (op0
, 0);
10813 /* If we are doing a sign bit comparison, it means we are testing
10814 a particular bit. Convert it to the appropriate AND. */
10815 if (sign_bit_comparison_p
&& GET_CODE (XEXP (op0
, 1)) == CONST_INT
10816 && mode_width
<= HOST_BITS_PER_WIDE_INT
)
10818 op0
= simplify_and_const_int (NULL_RTX
, mode
, XEXP (op0
, 0),
10821 - INTVAL (XEXP (op0
, 1)))));
10822 code
= (code
== LT
? NE
: EQ
);
10826 /* If this an equality comparison with zero and we are shifting
10827 the low bit to the sign bit, we can convert this to an AND of the
10829 if (const_op
== 0 && equality_comparison_p
10830 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10831 && (unsigned HOST_WIDE_INT
) INTVAL (XEXP (op0
, 1))
10834 op0
= simplify_and_const_int (NULL_RTX
, mode
, XEXP (op0
, 0),
10835 (HOST_WIDE_INT
) 1);
10841 /* If this is an equality comparison with zero, we can do this
10842 as a logical shift, which might be much simpler. */
10843 if (equality_comparison_p
&& const_op
== 0
10844 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
)
10846 op0
= simplify_shift_const (NULL_RTX
, LSHIFTRT
, mode
,
10848 INTVAL (XEXP (op0
, 1)));
10852 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10853 do the comparison in a narrower mode. */
10854 if (! unsigned_comparison_p
10855 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10856 && GET_CODE (XEXP (op0
, 0)) == ASHIFT
10857 && XEXP (op0
, 1) == XEXP (XEXP (op0
, 0), 1)
10858 && (tmode
= mode_for_size (mode_width
- INTVAL (XEXP (op0
, 1)),
10859 MODE_INT
, 1)) != BLKmode
10860 && (((unsigned HOST_WIDE_INT
) const_op
10861 + (GET_MODE_MASK (tmode
) >> 1) + 1)
10862 <= GET_MODE_MASK (tmode
)))
10864 op0
= gen_lowpart (tmode
, XEXP (XEXP (op0
, 0), 0));
10868 /* Likewise if OP0 is a PLUS of a sign extension with a
10869 constant, which is usually represented with the PLUS
10870 between the shifts. */
10871 if (! unsigned_comparison_p
10872 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10873 && GET_CODE (XEXP (op0
, 0)) == PLUS
10874 && GET_CODE (XEXP (XEXP (op0
, 0), 1)) == CONST_INT
10875 && GET_CODE (XEXP (XEXP (op0
, 0), 0)) == ASHIFT
10876 && XEXP (op0
, 1) == XEXP (XEXP (XEXP (op0
, 0), 0), 1)
10877 && (tmode
= mode_for_size (mode_width
- INTVAL (XEXP (op0
, 1)),
10878 MODE_INT
, 1)) != BLKmode
10879 && (((unsigned HOST_WIDE_INT
) const_op
10880 + (GET_MODE_MASK (tmode
) >> 1) + 1)
10881 <= GET_MODE_MASK (tmode
)))
10883 rtx inner
= XEXP (XEXP (XEXP (op0
, 0), 0), 0);
10884 rtx add_const
= XEXP (XEXP (op0
, 0), 1);
10885 rtx new_const
= simplify_gen_binary (ASHIFTRT
, GET_MODE (op0
),
10886 add_const
, XEXP (op0
, 1));
10888 op0
= simplify_gen_binary (PLUS
, tmode
,
10889 gen_lowpart (tmode
, inner
),
10894 /* ... fall through ... */
10896 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10897 the low order N bits of FOO are known to be zero, we can do this
10898 by comparing FOO with C shifted left N bits so long as no
10899 overflow occurs. */
10900 if (GET_CODE (XEXP (op0
, 1)) == CONST_INT
10901 && INTVAL (XEXP (op0
, 1)) >= 0
10902 && INTVAL (XEXP (op0
, 1)) < HOST_BITS_PER_WIDE_INT
10903 && mode_width
<= HOST_BITS_PER_WIDE_INT
10904 && (nonzero_bits (XEXP (op0
, 0), mode
)
10905 & (((HOST_WIDE_INT
) 1 << INTVAL (XEXP (op0
, 1))) - 1)) == 0
10906 && (((unsigned HOST_WIDE_INT
) const_op
10907 + (GET_CODE (op0
) != LSHIFTRT
10908 ? ((GET_MODE_MASK (mode
) >> INTVAL (XEXP (op0
, 1)) >> 1)
10911 <= GET_MODE_MASK (mode
) >> INTVAL (XEXP (op0
, 1))))
10913 /* If the shift was logical, then we must make the condition
10915 if (GET_CODE (op0
) == LSHIFTRT
)
10916 code
= unsigned_condition (code
);
10918 const_op
<<= INTVAL (XEXP (op0
, 1));
10919 op1
= GEN_INT (const_op
);
10920 op0
= XEXP (op0
, 0);
10924 /* If we are using this shift to extract just the sign bit, we
10925 can replace this with an LT or GE comparison. */
10927 && (equality_comparison_p
|| sign_bit_comparison_p
)
10928 && GET_CODE (XEXP (op0
, 1)) == CONST_INT
10929 && (unsigned HOST_WIDE_INT
) INTVAL (XEXP (op0
, 1))
10932 op0
= XEXP (op0
, 0);
10933 code
= (code
== NE
|| code
== GT
? LT
: GE
);
10945 /* Now make any compound operations involved in this comparison. Then,
10946 check for an outmost SUBREG on OP0 that is not doing anything or is
10947 paradoxical. The latter transformation must only be performed when
10948 it is known that the "extra" bits will be the same in op0 and op1 or
10949 that they don't matter. There are three cases to consider:
10951 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10952 care bits and we can assume they have any convenient value. So
10953 making the transformation is safe.
10955 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10956 In this case the upper bits of op0 are undefined. We should not make
10957 the simplification in that case as we do not know the contents of
10960 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10961 UNKNOWN. In that case we know those bits are zeros or ones. We must
10962 also be sure that they are the same as the upper bits of op1.
10964 We can never remove a SUBREG for a non-equality comparison because
10965 the sign bit is in a different place in the underlying object. */
10967 op0
= make_compound_operation (op0
, op1
== const0_rtx
? COMPARE
: SET
);
10968 op1
= make_compound_operation (op1
, SET
);
10970 if (GET_CODE (op0
) == SUBREG
&& subreg_lowpart_p (op0
)
10971 && GET_MODE_CLASS (GET_MODE (op0
)) == MODE_INT
10972 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0
))) == MODE_INT
10973 && (code
== NE
|| code
== EQ
))
10975 if (GET_MODE_SIZE (GET_MODE (op0
))
10976 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0
))))
10978 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10980 if (REG_P (SUBREG_REG (op0
)))
10982 op0
= SUBREG_REG (op0
);
10983 op1
= gen_lowpart (GET_MODE (op0
), op1
);
10986 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0
)))
10987 <= HOST_BITS_PER_WIDE_INT
)
10988 && (nonzero_bits (SUBREG_REG (op0
),
10989 GET_MODE (SUBREG_REG (op0
)))
10990 & ~GET_MODE_MASK (GET_MODE (op0
))) == 0)
10992 tem
= gen_lowpart (GET_MODE (SUBREG_REG (op0
)), op1
);
10994 if ((nonzero_bits (tem
, GET_MODE (SUBREG_REG (op0
)))
10995 & ~GET_MODE_MASK (GET_MODE (op0
))) == 0)
10996 op0
= SUBREG_REG (op0
), op1
= tem
;
11000 /* We now do the opposite procedure: Some machines don't have compare
11001 insns in all modes. If OP0's mode is an integer mode smaller than a
11002 word and we can't do a compare in that mode, see if there is a larger
11003 mode for which we can do the compare. There are a number of cases in
11004 which we can use the wider mode. */
11006 mode
= GET_MODE (op0
);
11007 if (mode
!= VOIDmode
&& GET_MODE_CLASS (mode
) == MODE_INT
11008 && GET_MODE_SIZE (mode
) < UNITS_PER_WORD
11009 && ! have_insn_for (COMPARE
, mode
))
11010 for (tmode
= GET_MODE_WIDER_MODE (mode
);
11012 && GET_MODE_BITSIZE (tmode
) <= HOST_BITS_PER_WIDE_INT
);
11013 tmode
= GET_MODE_WIDER_MODE (tmode
))
11014 if (have_insn_for (COMPARE
, tmode
))
11018 /* If the only nonzero bits in OP0 and OP1 are those in the
11019 narrower mode and this is an equality or unsigned comparison,
11020 we can use the wider mode. Similarly for sign-extended
11021 values, in which case it is true for all comparisons. */
11022 zero_extended
= ((code
== EQ
|| code
== NE
11023 || code
== GEU
|| code
== GTU
11024 || code
== LEU
|| code
== LTU
)
11025 && (nonzero_bits (op0
, tmode
)
11026 & ~GET_MODE_MASK (mode
)) == 0
11027 && ((GET_CODE (op1
) == CONST_INT
11028 || (nonzero_bits (op1
, tmode
)
11029 & ~GET_MODE_MASK (mode
)) == 0)));
11032 || ((num_sign_bit_copies (op0
, tmode
)
11033 > (unsigned int) (GET_MODE_BITSIZE (tmode
)
11034 - GET_MODE_BITSIZE (mode
)))
11035 && (num_sign_bit_copies (op1
, tmode
)
11036 > (unsigned int) (GET_MODE_BITSIZE (tmode
)
11037 - GET_MODE_BITSIZE (mode
)))))
11039 /* If OP0 is an AND and we don't have an AND in MODE either,
11040 make a new AND in the proper mode. */
11041 if (GET_CODE (op0
) == AND
11042 && !have_insn_for (AND
, mode
))
11043 op0
= simplify_gen_binary (AND
, tmode
,
11044 gen_lowpart (tmode
,
11046 gen_lowpart (tmode
,
11049 op0
= gen_lowpart (tmode
, op0
);
11050 if (zero_extended
&& GET_CODE (op1
) == CONST_INT
)
11051 op1
= GEN_INT (INTVAL (op1
) & GET_MODE_MASK (mode
));
11052 op1
= gen_lowpart (tmode
, op1
);
11056 /* If this is a test for negative, we can make an explicit
11057 test of the sign bit. */
11059 if (op1
== const0_rtx
&& (code
== LT
|| code
== GE
)
11060 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
11062 op0
= simplify_gen_binary (AND
, tmode
,
11063 gen_lowpart (tmode
, op0
),
11064 GEN_INT ((HOST_WIDE_INT
) 1
11065 << (GET_MODE_BITSIZE (mode
)
11067 code
= (code
== LT
) ? NE
: EQ
;
11072 #ifdef CANONICALIZE_COMPARISON
11073 /* If this machine only supports a subset of valid comparisons, see if we
11074 can convert an unsupported one into a supported one. */
11075 CANONICALIZE_COMPARISON (code
, op0
, op1
);
11084 /* Utility function for record_value_for_reg. Count number of
11089 enum rtx_code code
= GET_CODE (x
);
11093 if (GET_RTX_CLASS (code
) == '2'
11094 || GET_RTX_CLASS (code
) == 'c')
11096 rtx x0
= XEXP (x
, 0);
11097 rtx x1
= XEXP (x
, 1);
11100 return 1 + 2 * count_rtxs (x0
);
11102 if ((GET_RTX_CLASS (GET_CODE (x1
)) == '2'
11103 || GET_RTX_CLASS (GET_CODE (x1
)) == 'c')
11104 && (x0
== XEXP (x1
, 0) || x0
== XEXP (x1
, 1)))
11105 return 2 + 2 * count_rtxs (x0
)
11106 + count_rtxs (x
== XEXP (x1
, 0)
11107 ? XEXP (x1
, 1) : XEXP (x1
, 0));
11109 if ((GET_RTX_CLASS (GET_CODE (x0
)) == '2'
11110 || GET_RTX_CLASS (GET_CODE (x0
)) == 'c')
11111 && (x1
== XEXP (x0
, 0) || x1
== XEXP (x0
, 1)))
11112 return 2 + 2 * count_rtxs (x1
)
11113 + count_rtxs (x
== XEXP (x0
, 0)
11114 ? XEXP (x0
, 1) : XEXP (x0
, 0));
11117 fmt
= GET_RTX_FORMAT (code
);
11118 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
11120 ret
+= count_rtxs (XEXP (x
, i
));
11125 /* Utility function for following routine. Called when X is part of a value
11126 being stored into last_set_value. Sets last_set_table_tick
11127 for each register mentioned. Similar to mention_regs in cse.c */
11130 update_table_tick (rtx x
)
11132 enum rtx_code code
= GET_CODE (x
);
11133 const char *fmt
= GET_RTX_FORMAT (code
);
11138 unsigned int regno
= REGNO (x
);
11139 unsigned int endregno
= END_REGNO (x
);
11142 for (r
= regno
; r
< endregno
; r
++)
11143 reg_stat
[r
].last_set_table_tick
= label_tick
;
11148 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
11149 /* Note that we can't have an "E" in values stored; see
11150 get_last_value_validate. */
11153 /* Check for identical subexpressions. If x contains
11154 identical subexpression we only have to traverse one of
11156 if (i
== 0 && ARITHMETIC_P (x
))
11158 /* Note that at this point x1 has already been
11160 rtx x0
= XEXP (x
, 0);
11161 rtx x1
= XEXP (x
, 1);
11163 /* If x0 and x1 are identical then there is no need to
11168 /* If x0 is identical to a subexpression of x1 then while
11169 processing x1, x0 has already been processed. Thus we
11170 are done with x. */
11171 if (ARITHMETIC_P (x1
)
11172 && (x0
== XEXP (x1
, 0) || x0
== XEXP (x1
, 1)))
11175 /* If x1 is identical to a subexpression of x0 then we
11176 still have to process the rest of x0. */
11177 if (ARITHMETIC_P (x0
)
11178 && (x1
== XEXP (x0
, 0) || x1
== XEXP (x0
, 1)))
11180 update_table_tick (XEXP (x0
, x1
== XEXP (x0
, 0) ? 1 : 0));
11185 update_table_tick (XEXP (x
, i
));
11189 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11190 are saying that the register is clobbered and we no longer know its
11191 value. If INSN is zero, don't update reg_stat[].last_set; this is
11192 only permitted with VALUE also zero and is used to invalidate the
11196 record_value_for_reg (rtx reg
, rtx insn
, rtx value
)
11198 unsigned int regno
= REGNO (reg
);
11199 unsigned int endregno
= END_REGNO (reg
);
11202 /* If VALUE contains REG and we have a previous value for REG, substitute
11203 the previous value. */
11204 if (value
&& insn
&& reg_overlap_mentioned_p (reg
, value
))
11208 /* Set things up so get_last_value is allowed to see anything set up to
11210 subst_low_luid
= DF_INSN_LUID (insn
);
11211 tem
= get_last_value (reg
);
11213 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11214 it isn't going to be useful and will take a lot of time to process,
11215 so just use the CLOBBER. */
11219 if (ARITHMETIC_P (tem
)
11220 && GET_CODE (XEXP (tem
, 0)) == CLOBBER
11221 && GET_CODE (XEXP (tem
, 1)) == CLOBBER
)
11222 tem
= XEXP (tem
, 0);
11223 else if (count_occurrences (value
, reg
, 1) >= 2)
11225 /* If there are two or more occurrences of REG in VALUE,
11226 prevent the value from growing too much. */
11227 if (count_rtxs (tem
) > MAX_LAST_VALUE_RTL
)
11228 tem
= gen_rtx_CLOBBER (GET_MODE (tem
), const0_rtx
);
11231 value
= replace_rtx (copy_rtx (value
), reg
, tem
);
11235 /* For each register modified, show we don't know its value, that
11236 we don't know about its bitwise content, that its value has been
11237 updated, and that we don't know the location of the death of the
11239 for (i
= regno
; i
< endregno
; i
++)
11242 reg_stat
[i
].last_set
= insn
;
11244 reg_stat
[i
].last_set_value
= 0;
11245 reg_stat
[i
].last_set_mode
= 0;
11246 reg_stat
[i
].last_set_nonzero_bits
= 0;
11247 reg_stat
[i
].last_set_sign_bit_copies
= 0;
11248 reg_stat
[i
].last_death
= 0;
11249 reg_stat
[i
].truncated_to_mode
= 0;
11252 /* Mark registers that are being referenced in this value. */
11254 update_table_tick (value
);
11256 /* Now update the status of each register being set.
11257 If someone is using this register in this block, set this register
11258 to invalid since we will get confused between the two lives in this
11259 basic block. This makes using this register always invalid. In cse, we
11260 scan the table to invalidate all entries using this register, but this
11261 is too much work for us. */
11263 for (i
= regno
; i
< endregno
; i
++)
11265 reg_stat
[i
].last_set_label
= label_tick
;
11267 || (value
&& reg_stat
[i
].last_set_table_tick
>= label_tick_ebb_start
))
11268 reg_stat
[i
].last_set_invalid
= 1;
11270 reg_stat
[i
].last_set_invalid
= 0;
11273 /* The value being assigned might refer to X (like in "x++;"). In that
11274 case, we must replace it with (clobber (const_int 0)) to prevent
11276 if (value
&& ! get_last_value_validate (&value
, insn
,
11277 reg_stat
[regno
].last_set_label
, 0))
11279 value
= copy_rtx (value
);
11280 if (! get_last_value_validate (&value
, insn
,
11281 reg_stat
[regno
].last_set_label
, 1))
11285 /* For the main register being modified, update the value, the mode, the
11286 nonzero bits, and the number of sign bit copies. */
11288 reg_stat
[regno
].last_set_value
= value
;
11292 enum machine_mode mode
= GET_MODE (reg
);
11293 subst_low_luid
= DF_INSN_LUID (insn
);
11294 reg_stat
[regno
].last_set_mode
= mode
;
11295 if (GET_MODE_CLASS (mode
) == MODE_INT
11296 && GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_WIDE_INT
)
11297 mode
= nonzero_bits_mode
;
11298 reg_stat
[regno
].last_set_nonzero_bits
= nonzero_bits (value
, mode
);
11299 reg_stat
[regno
].last_set_sign_bit_copies
11300 = num_sign_bit_copies (value
, GET_MODE (reg
));
11304 /* Called via note_stores from record_dead_and_set_regs to handle one
11305 SET or CLOBBER in an insn. DATA is the instruction in which the
11306 set is occurring. */
11309 record_dead_and_set_regs_1 (rtx dest
, rtx setter
, void *data
)
11311 rtx record_dead_insn
= (rtx
) data
;
11313 if (GET_CODE (dest
) == SUBREG
)
11314 dest
= SUBREG_REG (dest
);
11316 if (!record_dead_insn
)
11319 record_value_for_reg (dest
, NULL_RTX
, NULL_RTX
);
11325 /* If we are setting the whole register, we know its value. Otherwise
11326 show that we don't know the value. We can handle SUBREG in
11328 if (GET_CODE (setter
) == SET
&& dest
== SET_DEST (setter
))
11329 record_value_for_reg (dest
, record_dead_insn
, SET_SRC (setter
));
11330 else if (GET_CODE (setter
) == SET
11331 && GET_CODE (SET_DEST (setter
)) == SUBREG
11332 && SUBREG_REG (SET_DEST (setter
)) == dest
11333 && GET_MODE_BITSIZE (GET_MODE (dest
)) <= BITS_PER_WORD
11334 && subreg_lowpart_p (SET_DEST (setter
)))
11335 record_value_for_reg (dest
, record_dead_insn
,
11336 gen_lowpart (GET_MODE (dest
),
11337 SET_SRC (setter
)));
11339 record_value_for_reg (dest
, record_dead_insn
, NULL_RTX
);
11341 else if (MEM_P (dest
)
11342 /* Ignore pushes, they clobber nothing. */
11343 && ! push_operand (dest
, GET_MODE (dest
)))
11344 mem_last_set
= DF_INSN_LUID (record_dead_insn
);
11347 /* Update the records of when each REG was most recently set or killed
11348 for the things done by INSN. This is the last thing done in processing
11349 INSN in the combiner loop.
11351 We update reg_stat[], in particular fields last_set, last_set_value,
11352 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11353 last_death, and also the similar information mem_last_set (which insn
11354 most recently modified memory) and last_call_luid (which insn was the
11355 most recent subroutine call). */
11358 record_dead_and_set_regs (rtx insn
)
11363 for (link
= REG_NOTES (insn
); link
; link
= XEXP (link
, 1))
11365 if (REG_NOTE_KIND (link
) == REG_DEAD
11366 && REG_P (XEXP (link
, 0)))
11368 unsigned int regno
= REGNO (XEXP (link
, 0));
11369 unsigned int endregno
= END_REGNO (XEXP (link
, 0));
11371 for (i
= regno
; i
< endregno
; i
++)
11372 reg_stat
[i
].last_death
= insn
;
11374 else if (REG_NOTE_KIND (link
) == REG_INC
)
11375 record_value_for_reg (XEXP (link
, 0), insn
, NULL_RTX
);
11380 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
11381 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
11383 reg_stat
[i
].last_set_invalid
= 1;
11384 reg_stat
[i
].last_set
= insn
;
11385 reg_stat
[i
].last_set_value
= 0;
11386 reg_stat
[i
].last_set_mode
= 0;
11387 reg_stat
[i
].last_set_nonzero_bits
= 0;
11388 reg_stat
[i
].last_set_sign_bit_copies
= 0;
11389 reg_stat
[i
].last_death
= 0;
11390 reg_stat
[i
].truncated_to_mode
= 0;
11393 last_call_luid
= mem_last_set
= DF_INSN_LUID (insn
);
11395 /* We can't combine into a call pattern. Remember, though, that
11396 the return value register is set at this LUID. We could
11397 still replace a register with the return value from the
11398 wrong subroutine call! */
11399 note_stores (PATTERN (insn
), record_dead_and_set_regs_1
, NULL_RTX
);
11402 note_stores (PATTERN (insn
), record_dead_and_set_regs_1
, insn
);
11405 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11406 register present in the SUBREG, so for each such SUBREG go back and
11407 adjust nonzero and sign bit information of the registers that are
11408 known to have some zero/sign bits set.
11410 This is needed because when combine blows the SUBREGs away, the
11411 information on zero/sign bits is lost and further combines can be
11412 missed because of that. */
11415 record_promoted_value (rtx insn
, rtx subreg
)
11418 unsigned int regno
= REGNO (SUBREG_REG (subreg
));
11419 enum machine_mode mode
= GET_MODE (subreg
);
11421 if (GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
)
11424 for (links
= LOG_LINKS (insn
); links
;)
11426 insn
= XEXP (links
, 0);
11427 set
= single_set (insn
);
11429 if (! set
|| !REG_P (SET_DEST (set
))
11430 || REGNO (SET_DEST (set
)) != regno
11431 || GET_MODE (SET_DEST (set
)) != GET_MODE (SUBREG_REG (subreg
)))
11433 links
= XEXP (links
, 1);
11437 if (reg_stat
[regno
].last_set
== insn
)
11439 if (SUBREG_PROMOTED_UNSIGNED_P (subreg
) > 0)
11440 reg_stat
[regno
].last_set_nonzero_bits
&= GET_MODE_MASK (mode
);
11443 if (REG_P (SET_SRC (set
)))
11445 regno
= REGNO (SET_SRC (set
));
11446 links
= LOG_LINKS (insn
);
11453 /* Check if X, a register, is known to contain a value already
11454 truncated to MODE. In this case we can use a subreg to refer to
11455 the truncated value even though in the generic case we would need
11456 an explicit truncation. */
11459 reg_truncated_to_mode (enum machine_mode mode
, rtx x
)
11461 enum machine_mode truncated
= reg_stat
[REGNO (x
)].truncated_to_mode
;
11464 || reg_stat
[REGNO (x
)].truncation_label
< label_tick_ebb_start
)
11466 if (GET_MODE_SIZE (truncated
) <= GET_MODE_SIZE (mode
))
11468 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode
),
11469 GET_MODE_BITSIZE (truncated
)))
11474 /* X is a REG or a SUBREG. If X is some sort of a truncation record
11475 it. For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11476 a truncate into a subreg using this information. */
11479 record_truncated_value (rtx x
)
11481 enum machine_mode truncated_mode
;
11483 if (GET_CODE (x
) == SUBREG
&& REG_P (SUBREG_REG (x
)))
11485 enum machine_mode original_mode
= GET_MODE (SUBREG_REG (x
));
11486 truncated_mode
= GET_MODE (x
);
11488 if (GET_MODE_SIZE (original_mode
) <= GET_MODE_SIZE (truncated_mode
))
11491 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode
),
11492 GET_MODE_BITSIZE (original_mode
)))
11495 x
= SUBREG_REG (x
);
11497 /* ??? For hard-regs we now record everything. We might be able to
11498 optimize this using last_set_mode. */
11499 else if (REG_P (x
) && REGNO (x
) < FIRST_PSEUDO_REGISTER
)
11500 truncated_mode
= GET_MODE (x
);
11504 if (reg_stat
[REGNO (x
)].truncated_to_mode
== 0
11505 || reg_stat
[REGNO (x
)].truncation_label
< label_tick_ebb_start
11506 || (GET_MODE_SIZE (truncated_mode
)
11507 < GET_MODE_SIZE (reg_stat
[REGNO (x
)].truncated_to_mode
)))
11509 reg_stat
[REGNO (x
)].truncated_to_mode
= truncated_mode
;
11510 reg_stat
[REGNO (x
)].truncation_label
= label_tick
;
11514 /* Scan X for promoted SUBREGs and truncated REGs. For each one
11515 found, note what it implies to the registers used in it. */
11518 check_conversions (rtx insn
, rtx x
)
11520 if (GET_CODE (x
) == SUBREG
|| REG_P (x
))
11522 if (GET_CODE (x
) == SUBREG
11523 && SUBREG_PROMOTED_VAR_P (x
)
11524 && REG_P (SUBREG_REG (x
)))
11525 record_promoted_value (insn
, x
);
11527 record_truncated_value (x
);
11531 const char *format
= GET_RTX_FORMAT (GET_CODE (x
));
11534 for (i
= 0; i
< GET_RTX_LENGTH (GET_CODE (x
)); i
++)
11538 check_conversions (insn
, XEXP (x
, i
));
11542 if (XVEC (x
, i
) != 0)
11543 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
11544 check_conversions (insn
, XVECEXP (x
, i
, j
));
11550 /* Utility routine for the following function. Verify that all the registers
11551 mentioned in *LOC are valid when *LOC was part of a value set when
11552 label_tick == TICK. Return 0 if some are not.
11554 If REPLACE is nonzero, replace the invalid reference with
11555 (clobber (const_int 0)) and return 1. This replacement is useful because
11556 we often can get useful information about the form of a value (e.g., if
11557 it was produced by a shift that always produces -1 or 0) even though
11558 we don't know exactly what registers it was produced from. */
11561 get_last_value_validate (rtx
*loc
, rtx insn
, int tick
, int replace
)
11564 const char *fmt
= GET_RTX_FORMAT (GET_CODE (x
));
11565 int len
= GET_RTX_LENGTH (GET_CODE (x
));
11570 unsigned int regno
= REGNO (x
);
11571 unsigned int endregno
= END_REGNO (x
);
11574 for (j
= regno
; j
< endregno
; j
++)
11575 if (reg_stat
[j
].last_set_invalid
11576 /* If this is a pseudo-register that was only set once and not
11577 live at the beginning of the function, it is always valid. */
11578 || (! (regno
>= FIRST_PSEUDO_REGISTER
11579 && REG_N_SETS (regno
) == 1
11580 && !REGNO_REG_SET_P
11581 (DF_LR_IN (ENTRY_BLOCK_PTR
->next_bb
), regno
))
11582 && reg_stat
[j
].last_set_label
> tick
))
11585 *loc
= gen_rtx_CLOBBER (GET_MODE (x
), const0_rtx
);
11591 /* If this is a memory reference, make sure that there were
11592 no stores after it that might have clobbered the value. We don't
11593 have alias info, so we assume any store invalidates it. */
11594 else if (MEM_P (x
) && !MEM_READONLY_P (x
)
11595 && DF_INSN_LUID (insn
) <= mem_last_set
)
11598 *loc
= gen_rtx_CLOBBER (GET_MODE (x
), const0_rtx
);
11602 for (i
= 0; i
< len
; i
++)
11606 /* Check for identical subexpressions. If x contains
11607 identical subexpression we only have to traverse one of
11609 if (i
== 1 && ARITHMETIC_P (x
))
11611 /* Note that at this point x0 has already been checked
11612 and found valid. */
11613 rtx x0
= XEXP (x
, 0);
11614 rtx x1
= XEXP (x
, 1);
11616 /* If x0 and x1 are identical then x is also valid. */
11620 /* If x1 is identical to a subexpression of x0 then
11621 while checking x0, x1 has already been checked. Thus
11622 it is valid and so as x. */
11623 if (ARITHMETIC_P (x0
)
11624 && (x1
== XEXP (x0
, 0) || x1
== XEXP (x0
, 1)))
11627 /* If x0 is identical to a subexpression of x1 then x is
11628 valid iff the rest of x1 is valid. */
11629 if (ARITHMETIC_P (x1
)
11630 && (x0
== XEXP (x1
, 0) || x0
== XEXP (x1
, 1)))
11632 get_last_value_validate (&XEXP (x1
,
11633 x0
== XEXP (x1
, 0) ? 1 : 0),
11634 insn
, tick
, replace
);
11637 if (get_last_value_validate (&XEXP (x
, i
), insn
, tick
,
11641 /* Don't bother with these. They shouldn't occur anyway. */
11642 else if (fmt
[i
] == 'E')
11646 /* If we haven't found a reason for it to be invalid, it is valid. */
11650 /* Get the last value assigned to X, if known. Some registers
11651 in the value may be replaced with (clobber (const_int 0)) if their value
11652 is known longer known reliably. */
11655 get_last_value (rtx x
)
11657 unsigned int regno
;
11660 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11661 then convert it to the desired mode. If this is a paradoxical SUBREG,
11662 we cannot predict what values the "extra" bits might have. */
11663 if (GET_CODE (x
) == SUBREG
11664 && subreg_lowpart_p (x
)
11665 && (GET_MODE_SIZE (GET_MODE (x
))
11666 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x
))))
11667 && (value
= get_last_value (SUBREG_REG (x
))) != 0)
11668 return gen_lowpart (GET_MODE (x
), value
);
11674 value
= reg_stat
[regno
].last_set_value
;
11676 /* If we don't have a value, or if it isn't for this basic block and
11677 it's either a hard register, set more than once, or it's a live
11678 at the beginning of the function, return 0.
11680 Because if it's not live at the beginning of the function then the reg
11681 is always set before being used (is never used without being set).
11682 And, if it's set only once, and it's always set before use, then all
11683 uses must have the same last value, even if it's not from this basic
11687 || (reg_stat
[regno
].last_set_label
< label_tick_ebb_start
11688 && (regno
< FIRST_PSEUDO_REGISTER
11689 || REG_N_SETS (regno
) != 1
11691 (DF_LR_IN (ENTRY_BLOCK_PTR
->next_bb
), regno
))))
11694 /* If the value was set in a later insn than the ones we are processing,
11695 we can't use it even if the register was only set once. */
11696 if (reg_stat
[regno
].last_set_label
== label_tick
11697 && DF_INSN_LUID (reg_stat
[regno
].last_set
) >= subst_low_luid
)
11700 /* If the value has all its registers valid, return it. */
11701 if (get_last_value_validate (&value
, reg_stat
[regno
].last_set
,
11702 reg_stat
[regno
].last_set_label
, 0))
11705 /* Otherwise, make a copy and replace any invalid register with
11706 (clobber (const_int 0)). If that fails for some reason, return 0. */
11708 value
= copy_rtx (value
);
11709 if (get_last_value_validate (&value
, reg_stat
[regno
].last_set
,
11710 reg_stat
[regno
].last_set_label
, 1))
11716 /* Return nonzero if expression X refers to a REG or to memory
11717 that is set in an instruction more recent than FROM_LUID. */
11720 use_crosses_set_p (rtx x
, int from_luid
)
11724 enum rtx_code code
= GET_CODE (x
);
11728 unsigned int regno
= REGNO (x
);
11729 unsigned endreg
= END_REGNO (x
);
11731 #ifdef PUSH_ROUNDING
11732 /* Don't allow uses of the stack pointer to be moved,
11733 because we don't know whether the move crosses a push insn. */
11734 if (regno
== STACK_POINTER_REGNUM
&& PUSH_ARGS
)
11737 for (; regno
< endreg
; regno
++)
11738 if (reg_stat
[regno
].last_set
11739 && reg_stat
[regno
].last_set_label
== label_tick
11740 && DF_INSN_LUID (reg_stat
[regno
].last_set
) > from_luid
)
11745 if (code
== MEM
&& mem_last_set
> from_luid
)
11748 fmt
= GET_RTX_FORMAT (code
);
11750 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
11755 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
11756 if (use_crosses_set_p (XVECEXP (x
, i
, j
), from_luid
))
11759 else if (fmt
[i
] == 'e'
11760 && use_crosses_set_p (XEXP (x
, i
), from_luid
))
11766 /* Define three variables used for communication between the following
11769 static unsigned int reg_dead_regno
, reg_dead_endregno
;
11770 static int reg_dead_flag
;
11772 /* Function called via note_stores from reg_dead_at_p.
11774 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11775 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11778 reg_dead_at_p_1 (rtx dest
, rtx x
, void *data ATTRIBUTE_UNUSED
)
11780 unsigned int regno
, endregno
;
11785 regno
= REGNO (dest
);
11786 endregno
= END_REGNO (dest
);
11787 if (reg_dead_endregno
> regno
&& reg_dead_regno
< endregno
)
11788 reg_dead_flag
= (GET_CODE (x
) == CLOBBER
) ? 1 : -1;
11791 /* Return nonzero if REG is known to be dead at INSN.
11793 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11794 referencing REG, it is dead. If we hit a SET referencing REG, it is
11795 live. Otherwise, see if it is live or dead at the start of the basic
11796 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11797 must be assumed to be always live. */
11800 reg_dead_at_p (rtx reg
, rtx insn
)
11805 /* Set variables for reg_dead_at_p_1. */
11806 reg_dead_regno
= REGNO (reg
);
11807 reg_dead_endregno
= END_REGNO (reg
);
11811 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11812 we allow the machine description to decide whether use-and-clobber
11813 patterns are OK. */
11814 if (reg_dead_regno
< FIRST_PSEUDO_REGISTER
)
11816 for (i
= reg_dead_regno
; i
< reg_dead_endregno
; i
++)
11817 if (!fixed_regs
[i
] && TEST_HARD_REG_BIT (newpat_used_regs
, i
))
11821 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11822 beginning of function. */
11823 for (; insn
&& !LABEL_P (insn
) && !BARRIER_P (insn
);
11824 insn
= prev_nonnote_insn (insn
))
11826 note_stores (PATTERN (insn
), reg_dead_at_p_1
, NULL
);
11828 return reg_dead_flag
== 1 ? 1 : 0;
11830 if (find_regno_note (insn
, REG_DEAD
, reg_dead_regno
))
11834 /* Get the basic block that we were in. */
11836 block
= ENTRY_BLOCK_PTR
->next_bb
;
11839 FOR_EACH_BB (block
)
11840 if (insn
== BB_HEAD (block
))
11843 if (block
== EXIT_BLOCK_PTR
)
11847 for (i
= reg_dead_regno
; i
< reg_dead_endregno
; i
++)
11848 if (REGNO_REG_SET_P (df_get_live_in (block
), i
))
11854 /* Note hard registers in X that are used. */
11857 mark_used_regs_combine (rtx x
)
11859 RTX_CODE code
= GET_CODE (x
);
11860 unsigned int regno
;
11873 case ADDR_DIFF_VEC
:
11876 /* CC0 must die in the insn after it is set, so we don't need to take
11877 special note of it here. */
11883 /* If we are clobbering a MEM, mark any hard registers inside the
11884 address as used. */
11885 if (MEM_P (XEXP (x
, 0)))
11886 mark_used_regs_combine (XEXP (XEXP (x
, 0), 0));
11891 /* A hard reg in a wide mode may really be multiple registers.
11892 If so, mark all of them just like the first. */
11893 if (regno
< FIRST_PSEUDO_REGISTER
)
11895 /* None of this applies to the stack, frame or arg pointers. */
11896 if (regno
== STACK_POINTER_REGNUM
11897 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11898 || regno
== HARD_FRAME_POINTER_REGNUM
11900 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11901 || (regno
== ARG_POINTER_REGNUM
&& fixed_regs
[regno
])
11903 || regno
== FRAME_POINTER_REGNUM
)
11906 add_to_hard_reg_set (&newpat_used_regs
, GET_MODE (x
), regno
);
11912 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11914 rtx testreg
= SET_DEST (x
);
11916 while (GET_CODE (testreg
) == SUBREG
11917 || GET_CODE (testreg
) == ZERO_EXTRACT
11918 || GET_CODE (testreg
) == STRICT_LOW_PART
)
11919 testreg
= XEXP (testreg
, 0);
11921 if (MEM_P (testreg
))
11922 mark_used_regs_combine (XEXP (testreg
, 0));
11924 mark_used_regs_combine (SET_SRC (x
));
11932 /* Recursively scan the operands of this expression. */
11935 const char *fmt
= GET_RTX_FORMAT (code
);
11937 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
11940 mark_used_regs_combine (XEXP (x
, i
));
11941 else if (fmt
[i
] == 'E')
11945 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
11946 mark_used_regs_combine (XVECEXP (x
, i
, j
));
11952 /* Remove register number REGNO from the dead registers list of INSN.
11954 Return the note used to record the death, if there was one. */
11957 remove_death (unsigned int regno
, rtx insn
)
11959 rtx note
= find_regno_note (insn
, REG_DEAD
, regno
);
11962 remove_note (insn
, note
);
11967 /* For each register (hardware or pseudo) used within expression X, if its
11968 death is in an instruction with luid between FROM_LUID (inclusive) and
11969 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11970 list headed by PNOTES.
11972 That said, don't move registers killed by maybe_kill_insn.
11974 This is done when X is being merged by combination into TO_INSN. These
11975 notes will then be distributed as needed. */
11978 move_deaths (rtx x
, rtx maybe_kill_insn
, int from_luid
, rtx to_insn
,
11983 enum rtx_code code
= GET_CODE (x
);
11987 unsigned int regno
= REGNO (x
);
11988 rtx where_dead
= reg_stat
[regno
].last_death
;
11990 /* Don't move the register if it gets killed in between from and to. */
11991 if (maybe_kill_insn
&& reg_set_p (x
, maybe_kill_insn
)
11992 && ! reg_referenced_p (x
, maybe_kill_insn
))
11996 && DF_INSN_LUID (where_dead
) >= from_luid
11997 && DF_INSN_LUID (where_dead
) < DF_INSN_LUID (to_insn
))
11999 rtx note
= remove_death (regno
, where_dead
);
12001 /* It is possible for the call above to return 0. This can occur
12002 when last_death points to I2 or I1 that we combined with.
12003 In that case make a new note.
12005 We must also check for the case where X is a hard register
12006 and NOTE is a death note for a range of hard registers
12007 including X. In that case, we must put REG_DEAD notes for
12008 the remaining registers in place of NOTE. */
12010 if (note
!= 0 && regno
< FIRST_PSEUDO_REGISTER
12011 && (GET_MODE_SIZE (GET_MODE (XEXP (note
, 0)))
12012 > GET_MODE_SIZE (GET_MODE (x
))))
12014 unsigned int deadregno
= REGNO (XEXP (note
, 0));
12015 unsigned int deadend
= END_HARD_REGNO (XEXP (note
, 0));
12016 unsigned int ourend
= END_HARD_REGNO (x
);
12019 for (i
= deadregno
; i
< deadend
; i
++)
12020 if (i
< regno
|| i
>= ourend
)
12021 REG_NOTES (where_dead
)
12022 = gen_rtx_EXPR_LIST (REG_DEAD
,
12024 REG_NOTES (where_dead
));
12027 /* If we didn't find any note, or if we found a REG_DEAD note that
12028 covers only part of the given reg, and we have a multi-reg hard
12029 register, then to be safe we must check for REG_DEAD notes
12030 for each register other than the first. They could have
12031 their own REG_DEAD notes lying around. */
12032 else if ((note
== 0
12034 && (GET_MODE_SIZE (GET_MODE (XEXP (note
, 0)))
12035 < GET_MODE_SIZE (GET_MODE (x
)))))
12036 && regno
< FIRST_PSEUDO_REGISTER
12037 && hard_regno_nregs
[regno
][GET_MODE (x
)] > 1)
12039 unsigned int ourend
= END_HARD_REGNO (x
);
12040 unsigned int i
, offset
;
12044 offset
= hard_regno_nregs
[regno
][GET_MODE (XEXP (note
, 0))];
12048 for (i
= regno
+ offset
; i
< ourend
; i
++)
12049 move_deaths (regno_reg_rtx
[i
],
12050 maybe_kill_insn
, from_luid
, to_insn
, &oldnotes
);
12053 if (note
!= 0 && GET_MODE (XEXP (note
, 0)) == GET_MODE (x
))
12055 XEXP (note
, 1) = *pnotes
;
12059 *pnotes
= gen_rtx_EXPR_LIST (REG_DEAD
, x
, *pnotes
);
12065 else if (GET_CODE (x
) == SET
)
12067 rtx dest
= SET_DEST (x
);
12069 move_deaths (SET_SRC (x
), maybe_kill_insn
, from_luid
, to_insn
, pnotes
);
12071 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12072 that accesses one word of a multi-word item, some
12073 piece of everything register in the expression is used by
12074 this insn, so remove any old death. */
12075 /* ??? So why do we test for equality of the sizes? */
12077 if (GET_CODE (dest
) == ZERO_EXTRACT
12078 || GET_CODE (dest
) == STRICT_LOW_PART
12079 || (GET_CODE (dest
) == SUBREG
12080 && (((GET_MODE_SIZE (GET_MODE (dest
))
12081 + UNITS_PER_WORD
- 1) / UNITS_PER_WORD
)
12082 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest
)))
12083 + UNITS_PER_WORD
- 1) / UNITS_PER_WORD
))))
12085 move_deaths (dest
, maybe_kill_insn
, from_luid
, to_insn
, pnotes
);
12089 /* If this is some other SUBREG, we know it replaces the entire
12090 value, so use that as the destination. */
12091 if (GET_CODE (dest
) == SUBREG
)
12092 dest
= SUBREG_REG (dest
);
12094 /* If this is a MEM, adjust deaths of anything used in the address.
12095 For a REG (the only other possibility), the entire value is
12096 being replaced so the old value is not used in this insn. */
12099 move_deaths (XEXP (dest
, 0), maybe_kill_insn
, from_luid
,
12104 else if (GET_CODE (x
) == CLOBBER
)
12107 len
= GET_RTX_LENGTH (code
);
12108 fmt
= GET_RTX_FORMAT (code
);
12110 for (i
= 0; i
< len
; i
++)
12115 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
12116 move_deaths (XVECEXP (x
, i
, j
), maybe_kill_insn
, from_luid
,
12119 else if (fmt
[i
] == 'e')
12120 move_deaths (XEXP (x
, i
), maybe_kill_insn
, from_luid
, to_insn
, pnotes
);
12124 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12125 pattern of an insn. X must be a REG. */
12128 reg_bitfield_target_p (rtx x
, rtx body
)
12132 if (GET_CODE (body
) == SET
)
12134 rtx dest
= SET_DEST (body
);
12136 unsigned int regno
, tregno
, endregno
, endtregno
;
12138 if (GET_CODE (dest
) == ZERO_EXTRACT
)
12139 target
= XEXP (dest
, 0);
12140 else if (GET_CODE (dest
) == STRICT_LOW_PART
)
12141 target
= SUBREG_REG (XEXP (dest
, 0));
12145 if (GET_CODE (target
) == SUBREG
)
12146 target
= SUBREG_REG (target
);
12148 if (!REG_P (target
))
12151 tregno
= REGNO (target
), regno
= REGNO (x
);
12152 if (tregno
>= FIRST_PSEUDO_REGISTER
|| regno
>= FIRST_PSEUDO_REGISTER
)
12153 return target
== x
;
12155 endtregno
= end_hard_regno (GET_MODE (target
), tregno
);
12156 endregno
= end_hard_regno (GET_MODE (x
), regno
);
12158 return endregno
> tregno
&& regno
< endtregno
;
12161 else if (GET_CODE (body
) == PARALLEL
)
12162 for (i
= XVECLEN (body
, 0) - 1; i
>= 0; i
--)
12163 if (reg_bitfield_target_p (x
, XVECEXP (body
, 0, i
)))
12169 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12170 as appropriate. I3 and I2 are the insns resulting from the combination
12171 insns including FROM (I2 may be zero).
12173 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12174 not need REG_DEAD notes because they are being substituted for. This
12175 saves searching in the most common cases.
12177 Each note in the list is either ignored or placed on some insns, depending
12178 on the type of note. */
12181 distribute_notes (rtx notes
, rtx from_insn
, rtx i3
, rtx i2
, rtx elim_i2
,
12184 rtx note
, next_note
;
12187 for (note
= notes
; note
; note
= next_note
)
12189 rtx place
= 0, place2
= 0;
12191 next_note
= XEXP (note
, 1);
12192 switch (REG_NOTE_KIND (note
))
12196 /* Doesn't matter much where we put this, as long as it's somewhere.
12197 It is preferable to keep these notes on branches, which is most
12198 likely to be i3. */
12202 case REG_VALUE_PROFILE
:
12203 /* Just get rid of this note, as it is unused later anyway. */
12206 case REG_NON_LOCAL_GOTO
:
12211 gcc_assert (i2
&& JUMP_P (i2
));
12216 case REG_EH_REGION
:
12217 /* These notes must remain with the call or trapping instruction. */
12220 else if (i2
&& CALL_P (i2
))
12224 gcc_assert (flag_non_call_exceptions
);
12225 if (may_trap_p (i3
))
12227 else if (i2
&& may_trap_p (i2
))
12229 /* ??? Otherwise assume we've combined things such that we
12230 can now prove that the instructions can't trap. Drop the
12231 note in this case. */
12237 /* These notes must remain with the call. It should not be
12238 possible for both I2 and I3 to be a call. */
12243 gcc_assert (i2
&& CALL_P (i2
));
12249 /* Any clobbers for i3 may still exist, and so we must process
12250 REG_UNUSED notes from that insn.
12252 Any clobbers from i2 or i1 can only exist if they were added by
12253 recog_for_combine. In that case, recog_for_combine created the
12254 necessary REG_UNUSED notes. Trying to keep any original
12255 REG_UNUSED notes from these insns can cause incorrect output
12256 if it is for the same register as the original i3 dest.
12257 In that case, we will notice that the register is set in i3,
12258 and then add a REG_UNUSED note for the destination of i3, which
12259 is wrong. However, it is possible to have REG_UNUSED notes from
12260 i2 or i1 for register which were both used and clobbered, so
12261 we keep notes from i2 or i1 if they will turn into REG_DEAD
12264 /* If this register is set or clobbered in I3, put the note there
12265 unless there is one already. */
12266 if (reg_set_p (XEXP (note
, 0), PATTERN (i3
)))
12268 if (from_insn
!= i3
)
12271 if (! (REG_P (XEXP (note
, 0))
12272 ? find_regno_note (i3
, REG_UNUSED
, REGNO (XEXP (note
, 0)))
12273 : find_reg_note (i3
, REG_UNUSED
, XEXP (note
, 0))))
12276 /* Otherwise, if this register is used by I3, then this register
12277 now dies here, so we must put a REG_DEAD note here unless there
12279 else if (reg_referenced_p (XEXP (note
, 0), PATTERN (i3
))
12280 && ! (REG_P (XEXP (note
, 0))
12281 ? find_regno_note (i3
, REG_DEAD
,
12282 REGNO (XEXP (note
, 0)))
12283 : find_reg_note (i3
, REG_DEAD
, XEXP (note
, 0))))
12285 PUT_REG_NOTE_KIND (note
, REG_DEAD
);
12293 /* These notes say something about results of an insn. We can
12294 only support them if they used to be on I3 in which case they
12295 remain on I3. Otherwise they are ignored.
12297 If the note refers to an expression that is not a constant, we
12298 must also ignore the note since we cannot tell whether the
12299 equivalence is still true. It might be possible to do
12300 slightly better than this (we only have a problem if I2DEST
12301 or I1DEST is present in the expression), but it doesn't
12302 seem worth the trouble. */
12304 if (from_insn
== i3
12305 && (XEXP (note
, 0) == 0 || CONSTANT_P (XEXP (note
, 0))))
12310 case REG_NO_CONFLICT
:
12311 /* These notes say something about how a register is used. They must
12312 be present on any use of the register in I2 or I3. */
12313 if (reg_mentioned_p (XEXP (note
, 0), PATTERN (i3
)))
12316 if (i2
&& reg_mentioned_p (XEXP (note
, 0), PATTERN (i2
)))
12326 /* This can show up in several ways -- either directly in the
12327 pattern, or hidden off in the constant pool with (or without?)
12328 a REG_EQUAL note. */
12329 /* ??? Ignore the without-reg_equal-note problem for now. */
12330 if (reg_mentioned_p (XEXP (note
, 0), PATTERN (i3
))
12331 || ((tem
= find_reg_note (i3
, REG_EQUAL
, NULL_RTX
))
12332 && GET_CODE (XEXP (tem
, 0)) == LABEL_REF
12333 && XEXP (XEXP (tem
, 0), 0) == XEXP (note
, 0)))
12337 && (reg_mentioned_p (XEXP (note
, 0), PATTERN (i2
))
12338 || ((tem
= find_reg_note (i2
, REG_EQUAL
, NULL_RTX
))
12339 && GET_CODE (XEXP (tem
, 0)) == LABEL_REF
12340 && XEXP (XEXP (tem
, 0), 0) == XEXP (note
, 0))))
12348 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
12349 a JUMP_LABEL instead or decrement LABEL_NUSES. */
12350 if (place
&& JUMP_P (place
))
12352 rtx label
= JUMP_LABEL (place
);
12355 JUMP_LABEL (place
) = XEXP (note
, 0);
12358 gcc_assert (label
== XEXP (note
, 0));
12359 if (LABEL_P (label
))
12360 LABEL_NUSES (label
)--;
12364 if (place2
&& JUMP_P (place2
))
12366 rtx label
= JUMP_LABEL (place2
);
12369 JUMP_LABEL (place2
) = XEXP (note
, 0);
12372 gcc_assert (label
== XEXP (note
, 0));
12373 if (LABEL_P (label
))
12374 LABEL_NUSES (label
)--;
12381 /* This note says something about the value of a register prior
12382 to the execution of an insn. It is too much trouble to see
12383 if the note is still correct in all situations. It is better
12384 to simply delete it. */
12387 case REG_LIBCALL_ID
:
12388 /* If the insn previously containing this note still exists,
12389 put it back where it was. Otherwise move it to the previous
12391 if (!NOTE_P (from_insn
))
12394 place
= prev_real_insn (from_insn
);
12397 /* If the insn previously containing this note still exists,
12398 put it back where it was. Otherwise move it to the previous
12399 insn. Adjust the corresponding REG_LIBCALL note. */
12400 if (!NOTE_P (from_insn
))
12404 tem
= find_reg_note (XEXP (note
, 0), REG_LIBCALL
, NULL_RTX
);
12405 place
= prev_real_insn (from_insn
);
12407 XEXP (tem
, 0) = place
;
12408 /* If we're deleting the last remaining instruction of a
12409 libcall sequence, don't add the notes. */
12410 else if (XEXP (note
, 0) == from_insn
)
12412 /* Don't add the dangling REG_RETVAL note. */
12419 /* This is handled similarly to REG_RETVAL. */
12420 if (!NOTE_P (from_insn
))
12424 tem
= find_reg_note (XEXP (note
, 0), REG_RETVAL
, NULL_RTX
);
12425 place
= next_real_insn (from_insn
);
12427 XEXP (tem
, 0) = place
;
12428 /* If we're deleting the last remaining instruction of a
12429 libcall sequence, don't add the notes. */
12430 else if (XEXP (note
, 0) == from_insn
)
12432 /* Don't add the dangling REG_LIBCALL note. */
12439 /* If we replaced the right hand side of FROM_INSN with a
12440 REG_EQUAL note, the original use of the dying register
12441 will not have been combined into I3 and I2. In such cases,
12442 FROM_INSN is guaranteed to be the first of the combined
12443 instructions, so we simply need to search back before
12444 FROM_INSN for the previous use or set of this register,
12445 then alter the notes there appropriately.
12447 If the register is used as an input in I3, it dies there.
12448 Similarly for I2, if it is nonzero and adjacent to I3.
12450 If the register is not used as an input in either I3 or I2
12451 and it is not one of the registers we were supposed to eliminate,
12452 there are two possibilities. We might have a non-adjacent I2
12453 or we might have somehow eliminated an additional register
12454 from a computation. For example, we might have had A & B where
12455 we discover that B will always be zero. In this case we will
12456 eliminate the reference to A.
12458 In both cases, we must search to see if we can find a previous
12459 use of A and put the death note there. */
12462 && from_insn
== i2mod
12463 && !reg_overlap_mentioned_p (XEXP (note
, 0), i2mod_new_rhs
))
12468 && CALL_P (from_insn
)
12469 && find_reg_fusage (from_insn
, USE
, XEXP (note
, 0)))
12471 else if (reg_referenced_p (XEXP (note
, 0), PATTERN (i3
)))
12473 else if (i2
!= 0 && next_nonnote_insn (i2
) == i3
12474 && reg_referenced_p (XEXP (note
, 0), PATTERN (i2
)))
12476 else if ((rtx_equal_p (XEXP (note
, 0), elim_i2
)
12478 && reg_overlap_mentioned_p (XEXP (note
, 0),
12480 || rtx_equal_p (XEXP (note
, 0), elim_i1
))
12487 basic_block bb
= this_basic_block
;
12489 for (tem
= PREV_INSN (tem
); place
== 0; tem
= PREV_INSN (tem
))
12491 if (! INSN_P (tem
))
12493 if (tem
== BB_HEAD (bb
))
12498 /* If the register is being set at TEM, see if that is all
12499 TEM is doing. If so, delete TEM. Otherwise, make this
12500 into a REG_UNUSED note instead. Don't delete sets to
12501 global register vars. */
12502 if ((REGNO (XEXP (note
, 0)) >= FIRST_PSEUDO_REGISTER
12503 || !global_regs
[REGNO (XEXP (note
, 0))])
12504 && reg_set_p (XEXP (note
, 0), PATTERN (tem
)))
12506 rtx set
= single_set (tem
);
12507 rtx inner_dest
= 0;
12509 rtx cc0_setter
= NULL_RTX
;
12513 for (inner_dest
= SET_DEST (set
);
12514 (GET_CODE (inner_dest
) == STRICT_LOW_PART
12515 || GET_CODE (inner_dest
) == SUBREG
12516 || GET_CODE (inner_dest
) == ZERO_EXTRACT
);
12517 inner_dest
= XEXP (inner_dest
, 0))
12520 /* Verify that it was the set, and not a clobber that
12521 modified the register.
12523 CC0 targets must be careful to maintain setter/user
12524 pairs. If we cannot delete the setter due to side
12525 effects, mark the user with an UNUSED note instead
12528 if (set
!= 0 && ! side_effects_p (SET_SRC (set
))
12529 && rtx_equal_p (XEXP (note
, 0), inner_dest
)
12531 && (! reg_mentioned_p (cc0_rtx
, SET_SRC (set
))
12532 || ((cc0_setter
= prev_cc0_setter (tem
)) != NULL
12533 && sets_cc0_p (PATTERN (cc0_setter
)) > 0))
12537 /* Move the notes and links of TEM elsewhere.
12538 This might delete other dead insns recursively.
12539 First set the pattern to something that won't use
12541 rtx old_notes
= REG_NOTES (tem
);
12543 PATTERN (tem
) = pc_rtx
;
12544 REG_NOTES (tem
) = NULL
;
12546 distribute_notes (old_notes
, tem
, tem
, NULL_RTX
,
12547 NULL_RTX
, NULL_RTX
);
12548 distribute_links (LOG_LINKS (tem
));
12550 SET_INSN_DELETED (tem
);
12553 /* Delete the setter too. */
12556 PATTERN (cc0_setter
) = pc_rtx
;
12557 old_notes
= REG_NOTES (cc0_setter
);
12558 REG_NOTES (cc0_setter
) = NULL
;
12560 distribute_notes (old_notes
, cc0_setter
,
12561 cc0_setter
, NULL_RTX
,
12562 NULL_RTX
, NULL_RTX
);
12563 distribute_links (LOG_LINKS (cc0_setter
));
12565 SET_INSN_DELETED (cc0_setter
);
12571 PUT_REG_NOTE_KIND (note
, REG_UNUSED
);
12573 /* If there isn't already a REG_UNUSED note, put one
12574 here. Do not place a REG_DEAD note, even if
12575 the register is also used here; that would not
12576 match the algorithm used in lifetime analysis
12577 and can cause the consistency check in the
12578 scheduler to fail. */
12579 if (! find_regno_note (tem
, REG_UNUSED
,
12580 REGNO (XEXP (note
, 0))))
12585 else if (reg_referenced_p (XEXP (note
, 0), PATTERN (tem
))
12587 && find_reg_fusage (tem
, USE
, XEXP (note
, 0))))
12591 /* If we are doing a 3->2 combination, and we have a
12592 register which formerly died in i3 and was not used
12593 by i2, which now no longer dies in i3 and is used in
12594 i2 but does not die in i2, and place is between i2
12595 and i3, then we may need to move a link from place to
12597 if (i2
&& DF_INSN_LUID (place
) > DF_INSN_LUID (i2
)
12599 && DF_INSN_LUID (from_insn
) > DF_INSN_LUID (i2
)
12600 && reg_referenced_p (XEXP (note
, 0), PATTERN (i2
)))
12602 rtx links
= LOG_LINKS (place
);
12603 LOG_LINKS (place
) = 0;
12604 distribute_links (links
);
12609 if (tem
== BB_HEAD (bb
))
12615 /* If the register is set or already dead at PLACE, we needn't do
12616 anything with this note if it is still a REG_DEAD note.
12617 We check here if it is set at all, not if is it totally replaced,
12618 which is what `dead_or_set_p' checks, so also check for it being
12621 if (place
&& REG_NOTE_KIND (note
) == REG_DEAD
)
12623 unsigned int regno
= REGNO (XEXP (note
, 0));
12626 if (dead_or_set_p (place
, XEXP (note
, 0))
12627 || reg_bitfield_target_p (XEXP (note
, 0), PATTERN (place
)))
12629 /* Unless the register previously died in PLACE, clear
12630 last_death. [I no longer understand why this is
12632 if (reg_stat
[regno
].last_death
!= place
)
12633 reg_stat
[regno
].last_death
= 0;
12637 reg_stat
[regno
].last_death
= place
;
12639 /* If this is a death note for a hard reg that is occupying
12640 multiple registers, ensure that we are still using all
12641 parts of the object. If we find a piece of the object
12642 that is unused, we must arrange for an appropriate REG_DEAD
12643 note to be added for it. However, we can't just emit a USE
12644 and tag the note to it, since the register might actually
12645 be dead; so we recourse, and the recursive call then finds
12646 the previous insn that used this register. */
12648 if (place
&& regno
< FIRST_PSEUDO_REGISTER
12649 && hard_regno_nregs
[regno
][GET_MODE (XEXP (note
, 0))] > 1)
12651 unsigned int endregno
= END_HARD_REGNO (XEXP (note
, 0));
12655 for (i
= regno
; i
< endregno
; i
++)
12656 if ((! refers_to_regno_p (i
, i
+ 1, PATTERN (place
), 0)
12657 && ! find_regno_fusage (place
, USE
, i
))
12658 || dead_or_set_regno_p (place
, i
))
12663 /* Put only REG_DEAD notes for pieces that are
12664 not already dead or set. */
12666 for (i
= regno
; i
< endregno
;
12667 i
+= hard_regno_nregs
[i
][reg_raw_mode
[i
]])
12669 rtx piece
= regno_reg_rtx
[i
];
12670 basic_block bb
= this_basic_block
;
12672 if (! dead_or_set_p (place
, piece
)
12673 && ! reg_bitfield_target_p (piece
,
12677 = gen_rtx_EXPR_LIST (REG_DEAD
, piece
, NULL_RTX
);
12679 distribute_notes (new_note
, place
, place
,
12680 NULL_RTX
, NULL_RTX
, NULL_RTX
);
12682 else if (! refers_to_regno_p (i
, i
+ 1,
12683 PATTERN (place
), 0)
12684 && ! find_regno_fusage (place
, USE
, i
))
12685 for (tem
= PREV_INSN (place
); ;
12686 tem
= PREV_INSN (tem
))
12688 if (! INSN_P (tem
))
12690 if (tem
== BB_HEAD (bb
))
12694 if (dead_or_set_p (tem
, piece
)
12695 || reg_bitfield_target_p (piece
,
12699 = gen_rtx_EXPR_LIST (REG_UNUSED
, piece
,
12714 /* Any other notes should not be present at this point in the
12716 gcc_unreachable ();
12721 XEXP (note
, 1) = REG_NOTES (place
);
12722 REG_NOTES (place
) = note
;
12727 = gen_rtx_fmt_ee (GET_CODE (note
), REG_NOTE_KIND (note
),
12728 XEXP (note
, 0), REG_NOTES (place2
));
12732 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12733 I3, I2, and I1 to new locations. This is also called to add a link
12734 pointing at I3 when I3's destination is changed. */
12737 distribute_links (rtx links
)
12739 rtx link
, next_link
;
12741 for (link
= links
; link
; link
= next_link
)
12747 next_link
= XEXP (link
, 1);
12749 /* If the insn that this link points to is a NOTE or isn't a single
12750 set, ignore it. In the latter case, it isn't clear what we
12751 can do other than ignore the link, since we can't tell which
12752 register it was for. Such links wouldn't be used by combine
12755 It is not possible for the destination of the target of the link to
12756 have been changed by combine. The only potential of this is if we
12757 replace I3, I2, and I1 by I3 and I2. But in that case the
12758 destination of I2 also remains unchanged. */
12760 if (NOTE_P (XEXP (link
, 0))
12761 || (set
= single_set (XEXP (link
, 0))) == 0)
12764 reg
= SET_DEST (set
);
12765 while (GET_CODE (reg
) == SUBREG
|| GET_CODE (reg
) == ZERO_EXTRACT
12766 || GET_CODE (reg
) == STRICT_LOW_PART
)
12767 reg
= XEXP (reg
, 0);
12769 /* A LOG_LINK is defined as being placed on the first insn that uses
12770 a register and points to the insn that sets the register. Start
12771 searching at the next insn after the target of the link and stop
12772 when we reach a set of the register or the end of the basic block.
12774 Note that this correctly handles the link that used to point from
12775 I3 to I2. Also note that not much searching is typically done here
12776 since most links don't point very far away. */
12778 for (insn
= NEXT_INSN (XEXP (link
, 0));
12779 (insn
&& (this_basic_block
->next_bb
== EXIT_BLOCK_PTR
12780 || BB_HEAD (this_basic_block
->next_bb
) != insn
));
12781 insn
= NEXT_INSN (insn
))
12782 if (INSN_P (insn
) && reg_overlap_mentioned_p (reg
, PATTERN (insn
)))
12784 if (reg_referenced_p (reg
, PATTERN (insn
)))
12788 else if (CALL_P (insn
)
12789 && find_reg_fusage (insn
, USE
, reg
))
12794 else if (INSN_P (insn
) && reg_set_p (reg
, insn
))
12797 /* If we found a place to put the link, place it there unless there
12798 is already a link to the same insn as LINK at that point. */
12804 for (link2
= LOG_LINKS (place
); link2
; link2
= XEXP (link2
, 1))
12805 if (XEXP (link2
, 0) == XEXP (link
, 0))
12810 XEXP (link
, 1) = LOG_LINKS (place
);
12811 LOG_LINKS (place
) = link
;
12813 /* Set added_links_insn to the earliest insn we added a
12815 if (added_links_insn
== 0
12816 || DF_INSN_LUID (added_links_insn
) > DF_INSN_LUID (place
))
12817 added_links_insn
= place
;
12823 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12824 Check whether the expression pointer to by LOC is a register or
12825 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12826 Otherwise return zero. */
12829 unmentioned_reg_p_1 (rtx
*loc
, void *expr
)
12834 && (REG_P (x
) || MEM_P (x
))
12835 && ! reg_mentioned_p (x
, (rtx
) expr
))
12840 /* Check for any register or memory mentioned in EQUIV that is not
12841 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12842 of EXPR where some registers may have been replaced by constants. */
12845 unmentioned_reg_p (rtx equiv
, rtx expr
)
12847 return for_each_rtx (&equiv
, unmentioned_reg_p_1
, expr
);
12851 dump_combine_stats (FILE *file
)
12855 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12856 combine_attempts
, combine_merges
, combine_extras
, combine_successes
);
12860 dump_combine_total_stats (FILE *file
)
12864 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12865 total_attempts
, total_merges
, total_extras
, total_successes
);
12869 gate_handle_combine (void)
12871 return (optimize
> 0);
12874 /* Try combining insns through substitution. */
12875 static unsigned int
12876 rest_of_handle_combine (void)
12878 int rebuild_jump_labels_after_combine
;
12880 df_set_flags (DF_LR_RUN_DCE
+ DF_DEFER_INSN_RESCAN
);
12881 df_note_add_problem ();
12884 regstat_init_n_sets_and_refs ();
12886 rebuild_jump_labels_after_combine
12887 = combine_instructions (get_insns (), max_reg_num ());
12889 /* Combining insns may have turned an indirect jump into a
12890 direct jump. Rebuild the JUMP_LABEL fields of jumping
12892 if (rebuild_jump_labels_after_combine
)
12894 timevar_push (TV_JUMP
);
12895 rebuild_jump_labels (get_insns ());
12897 timevar_pop (TV_JUMP
);
12900 regstat_free_n_sets_and_refs ();
12904 struct tree_opt_pass pass_combine
=
12906 "combine", /* name */
12907 gate_handle_combine
, /* gate */
12908 rest_of_handle_combine
, /* execute */
12911 0, /* static_pass_number */
12912 TV_COMBINE
, /* tv_id */
12913 0, /* properties_required */
12914 0, /* properties_provided */
12915 0, /* properties_destroyed */
12916 0, /* todo_flags_start */
12919 TODO_ggc_collect
, /* todo_flags_finish */