Line data Source code
1 : /* Optimize by combining instructions for GNU compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /* This module is essentially the "combiner" phase of the U. of Arizona
21 : Portable Optimizer, but redone to work on our list-structured
22 : representation for RTL instead of their string representation.
23 :
24 : The LOG_LINKS of each insn identify the most recent assignment
25 : to each REG used in the insn. It is a list of previous insns,
26 : each of which contains a SET for a REG that is used in this insn
27 : and not used or set in between. LOG_LINKs never cross basic blocks.
28 : They were set up by the preceding pass (lifetime analysis).
29 :
30 : We try to combine each pair of insns joined by a logical link.
31 : We also try to combine triplets of insns A, B and C when C has
32 : a link back to B and B has a link back to A. Likewise for a
33 : small number of quadruplets of insns A, B, C and D for which
34 : there's high likelihood of success.
35 :
36 : We check (with modified_between_p) to avoid combining in such a way
37 : as to move a computation to a place where its value would be different.
38 :
39 : Combination is done by mathematically substituting the previous
40 : insn(s) values for the regs they set into the expressions in
41 : the later insns that refer to these regs. If the result is a valid insn
42 : for our target machine, according to the machine description,
43 : we install it, delete the earlier insns, and update the data flow
44 : information (LOG_LINKS and REG_NOTES) for what we did.
45 :
46 : There are a few exceptions where the dataflow information isn't
47 : completely updated (however this is only a local issue since it is
48 : regenerated before the next pass that uses it):
49 :
50 : - reg_live_length is not updated
51 : - reg_n_refs is not adjusted in the rare case when a register is
52 : no longer required in a computation
53 : - there are extremely rare cases (see distribute_notes) when a
54 : REG_DEAD note is lost
55 : - a LOG_LINKS entry that refers to an insn with multiple SETs may be
56 : removed because there is no way to know which register it was
57 : linking
58 :
59 : To simplify substitution, we combine only when the earlier insn(s)
60 : consist of only a single assignment. To simplify updating afterward,
61 : we never combine when a subroutine call appears in the middle. */
62 :
63 : #include "config.h"
64 : #include "system.h"
65 : #include "coretypes.h"
66 : #include "backend.h"
67 : #include "target.h"
68 : #include "rtl.h"
69 : #include "tree.h"
70 : #include "cfghooks.h"
71 : #include "predict.h"
72 : #include "df.h"
73 : #include "memmodel.h"
74 : #include "tm_p.h"
75 : #include "optabs.h"
76 : #include "regs.h"
77 : #include "emit-rtl.h"
78 : #include "recog.h"
79 : #include "cgraph.h"
80 : #include "stor-layout.h"
81 : #include "cfgrtl.h"
82 : #include "cfgcleanup.h"
83 : /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
84 : #include "explow.h"
85 : #include "insn-attr.h"
86 : #include "rtlhooks-def.h"
87 : #include "expr.h"
88 : #include "tree-pass.h"
89 : #include "valtrack.h"
90 : #include "rtl-iter.h"
91 : #include "print-rtl.h"
92 : #include "function-abi.h"
93 : #include "rtlanal.h"
94 :
95 : /* Number of attempts to combine instructions in this function. */
96 :
97 : static int combine_attempts;
98 :
99 : /* Number of attempts that got as far as substitution in this function. */
100 :
101 : static int combine_merges;
102 :
103 : /* Number of instructions combined with added SETs in this function. */
104 :
105 : static int combine_extras;
106 :
107 : /* Number of instructions combined in this function. */
108 :
109 : static int combine_successes;
110 :
111 : /* combine_instructions may try to replace the right hand side of the
112 : second instruction with the value of an associated REG_EQUAL note
113 : before throwing it at try_combine. That is problematic when there
114 : is a REG_DEAD note for a register used in the old right hand side
115 : and can cause distribute_notes to do wrong things. This is the
116 : second instruction if it has been so modified, null otherwise. */
117 :
118 : static rtx_insn *i2mod;
119 :
120 : /* When I2MOD is nonnull, this is a copy of the old right hand side. */
121 :
122 : static rtx i2mod_old_rhs;
123 :
124 : /* When I2MOD is nonnull, this is a copy of the new right hand side. */
125 :
126 : static rtx i2mod_new_rhs;
127 :
128 : struct reg_stat_type {
129 : /* Record last point of death of (hard or pseudo) register n. */
130 : rtx_insn *last_death;
131 :
132 : /* Record last point of modification of (hard or pseudo) register n. */
133 : rtx_insn *last_set;
134 :
135 : /* The next group of fields allows the recording of the last value assigned
136 : to (hard or pseudo) register n. We use this information to see if an
137 : operation being processed is redundant given a prior operation performed
138 : on the register. For example, an `and' with a constant is redundant if
139 : all the zero bits are already known to be turned off.
140 :
141 : We use an approach similar to that used by cse, but change it in the
142 : following ways:
143 :
144 : (1) We do not want to reinitialize at each label.
145 : (2) It is useful, but not critical, to know the actual value assigned
146 : to a register. Often just its form is helpful.
147 :
148 : Therefore, we maintain the following fields:
149 :
150 : last_set_value the last value assigned
151 : last_set_label records the value of label_tick when the
152 : register was assigned
153 : last_set_table_tick records the value of label_tick when a
154 : value using the register is assigned
155 : last_set_invalid set to true when it is not valid
156 : to use the value of this register in some
157 : register's value
158 :
159 : To understand the usage of these tables, it is important to understand
160 : the distinction between the value in last_set_value being valid and
161 : the register being validly contained in some other expression in the
162 : table.
163 :
164 : (The next two parameters are out of date).
165 :
166 : reg_stat[i].last_set_value is valid if it is nonzero, and either
167 : reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
168 :
169 : Register I may validly appear in any expression returned for the value
170 : of another register if reg_n_sets[i] is 1. It may also appear in the
171 : value for register J if reg_stat[j].last_set_invalid is zero, or
172 : reg_stat[i].last_set_label < reg_stat[j].last_set_label.
173 :
174 : If an expression is found in the table containing a register which may
175 : not validly appear in an expression, the register is replaced by
176 : something that won't match, (clobber (const_int 0)). */
177 :
178 : /* Record last value assigned to (hard or pseudo) register n. */
179 :
180 : rtx last_set_value;
181 :
182 : /* Record the value of label_tick when an expression involving register n
183 : is placed in last_set_value. */
184 :
185 : int last_set_table_tick;
186 :
187 : /* Record the value of label_tick when the value for register n is placed in
188 : last_set_value. */
189 :
190 : int last_set_label;
191 :
192 : /* These fields are maintained in parallel with last_set_value and are
193 : used to store the mode in which the register was last set, the bits
194 : that were known to be zero when it was last set, and the number of
195 : sign bits copies it was known to have when it was last set. */
196 :
197 : unsigned HOST_WIDE_INT last_set_nonzero_bits;
198 : unsigned short last_set_sign_bit_copies;
199 : machine_mode last_set_mode : MACHINE_MODE_BITSIZE;
200 :
201 : /* Set to true if references to register n in expressions should not be
202 : used. last_set_invalid is set nonzero when this register is being
203 : assigned to and last_set_table_tick == label_tick. */
204 :
205 : bool last_set_invalid;
206 :
207 : /* Some registers that are set more than once and used in more than one
208 : basic block are nevertheless always set in similar ways. For example,
209 : a QImode register may be loaded from memory in two places on a machine
210 : where byte loads zero extend.
211 :
212 : We record in the following fields if a register has some leading bits
213 : that are always equal to the sign bit, and what we know about the
214 : nonzero bits of a register, specifically which bits are known to be
215 : zero.
216 :
217 : If an entry is zero, it means that we don't know anything special. */
218 :
219 : unsigned short sign_bit_copies;
220 :
221 : unsigned HOST_WIDE_INT nonzero_bits;
222 :
223 : /* Record the value of the label_tick when the last truncation
224 : happened. The field truncated_to_mode is only valid if
225 : truncation_label == label_tick. */
226 :
227 : int truncation_label;
228 :
229 : /* Record the last truncation seen for this register. If truncation
230 : is not a nop to this mode we might be able to save an explicit
231 : truncation if we know that value already contains a truncated
232 : value. */
233 :
234 : machine_mode truncated_to_mode : MACHINE_MODE_BITSIZE;
235 : };
236 :
237 :
238 : static vec<reg_stat_type> reg_stat;
239 :
240 : /* One plus the highest pseudo for which we track REG_N_SETS.
241 : regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
242 : but during combine_split_insns new pseudos can be created. As we don't have
243 : updated DF information in that case, it is hard to initialize the array
244 : after growing. The combiner only cares about REG_N_SETS (regno) == 1,
245 : so instead of growing the arrays, just assume all newly created pseudos
246 : during combine might be set multiple times. */
247 :
248 : static unsigned int reg_n_sets_max;
249 :
250 : /* Record the luid of the last insn that invalidated memory
251 : (anything that writes memory, and subroutine calls, but not pushes). */
252 :
253 : static int mem_last_set;
254 :
255 : /* Record the luid of the last CALL_INSN
256 : so we can tell whether a potential combination crosses any calls. */
257 :
258 : static int last_call_luid;
259 :
260 : /* When `subst' is called, this is the insn that is being modified
261 : (by combining in a previous insn). The PATTERN of this insn
262 : is still the old pattern partially modified and it should not be
263 : looked at, but this may be used to examine the successors of the insn
264 : to judge whether a simplification is valid. */
265 :
266 : static rtx_insn *subst_insn;
267 :
268 : /* This is the lowest LUID that `subst' is currently dealing with.
269 : get_last_value will not return a value if the register was set at or
270 : after this LUID. If not for this mechanism, we could get confused if
271 : I2 or I1 in try_combine were an insn that used the old value of a register
272 : to obtain a new value. In that case, we might erroneously get the
273 : new value of the register when we wanted the old one. */
274 :
275 : static int subst_low_luid;
276 :
277 : /* This contains any hard registers that are used in newpat; reg_dead_at_p
278 : must consider all these registers to be always live. */
279 :
280 : static HARD_REG_SET newpat_used_regs;
281 :
282 : /* This is an insn to which a LOG_LINKS entry has been added. If this
283 : insn is the earlier than I2 or I3, combine should rescan starting at
284 : that location. */
285 :
286 : static rtx_insn *added_links_insn;
287 :
288 : /* And similarly, for notes. */
289 :
290 : static rtx_insn *added_notes_insn;
291 :
292 : /* Basic block in which we are performing combines. */
293 : static basic_block this_basic_block;
294 : static bool optimize_this_for_speed_p;
295 :
296 :
297 : /* Length of the currently allocated uid_insn_cost array. */
298 :
299 : static int max_uid_known;
300 :
301 : /* The following array records the insn_cost for every insn
302 : in the instruction stream. */
303 :
304 : static int *uid_insn_cost;
305 :
306 : /* The following array records the LOG_LINKS for every insn in the
307 : instruction stream as struct insn_link pointers. */
308 :
309 : struct insn_link {
310 : rtx_insn *insn;
311 : unsigned int regno;
312 : int insn_count;
313 : struct insn_link *next;
314 : };
315 :
316 : static struct insn_link **uid_log_links;
317 :
318 : static inline int
319 781922051 : insn_uid_check (const rtx_insn *insn)
320 : {
321 781922051 : int uid = INSN_UID (insn);
322 781922051 : gcc_checking_assert (uid <= max_uid_known);
323 781922051 : return uid;
324 : }
325 :
326 : #define INSN_COST(INSN) (uid_insn_cost[insn_uid_check (INSN)])
327 : #define LOG_LINKS(INSN) (uid_log_links[insn_uid_check (INSN)])
328 :
329 : #define FOR_EACH_LOG_LINK(L, INSN) \
330 : for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
331 :
332 : /* Links for LOG_LINKS are allocated from this obstack. */
333 :
334 : static struct obstack insn_link_obstack;
335 :
336 : /* Allocate a link. */
337 :
338 : static inline struct insn_link *
339 39218762 : alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
340 : {
341 39218762 : struct insn_link *l
342 39218762 : = (struct insn_link *) obstack_alloc (&insn_link_obstack,
343 : sizeof (struct insn_link));
344 39218762 : l->insn = insn;
345 39218762 : l->regno = regno;
346 39218762 : l->insn_count = 0;
347 39218762 : l->next = next;
348 39218762 : return l;
349 : }
350 :
351 : /* Incremented for each basic block. */
352 :
353 : static int label_tick;
354 :
355 : /* Reset to label_tick for each extended basic block in scanning order. */
356 :
357 : static int label_tick_ebb_start;
358 :
359 : /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
360 : largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
361 :
362 : static scalar_int_mode nonzero_bits_mode;
363 :
364 : /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
365 : be safely used. It is zero while computing them and after combine has
366 : completed. This former test prevents propagating values based on
367 : previously set values, which can be incorrect if a variable is modified
368 : in a loop. */
369 :
370 : static int nonzero_sign_valid;
371 :
372 :
373 : /* Record one modification to rtl structure
374 : to be undone by storing old_contents into *where. */
375 :
376 : enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
377 :
378 : struct undo
379 : {
380 : struct undo *next;
381 : enum undo_kind kind;
382 : union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
383 : union { rtx *r; int *i; int regno; struct insn_link **l; } where;
384 : };
385 :
386 : /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
387 : num_undo says how many are currently recorded.
388 :
389 : other_insn is nonzero if we have modified some other insn in the process
390 : of working on subst_insn. It must be verified too. */
391 :
392 : struct undobuf
393 : {
394 : struct undo *undos;
395 : struct undo *frees;
396 : rtx_insn *other_insn;
397 : };
398 :
399 : static struct undobuf undobuf;
400 :
401 : /* Number of times the pseudo being substituted for
402 : was found and replaced. */
403 :
404 : static int n_occurrences;
405 :
406 : static rtx reg_nonzero_bits_for_combine (const_rtx, scalar_int_mode,
407 : scalar_int_mode,
408 : unsigned HOST_WIDE_INT *);
409 : static rtx reg_num_sign_bit_copies_for_combine (const_rtx, scalar_int_mode,
410 : scalar_int_mode,
411 : unsigned int *);
412 : static void do_SUBST (rtx *, rtx);
413 : static void do_SUBST_INT (int *, int);
414 : static void init_reg_last (void);
415 : static void setup_incoming_promotions (rtx_insn *);
416 : static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
417 : static bool cant_combine_insn_p (rtx_insn *);
418 : static bool can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
419 : rtx_insn *, rtx_insn *, rtx *, rtx *);
420 : static bool combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx,
421 : bool, bool, rtx *);
422 : static bool contains_muldiv (rtx);
423 : static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
424 : bool *, rtx_insn *);
425 : static void undo_all (void);
426 : static void undo_commit (void);
427 : static rtx *find_split_point (rtx *, rtx_insn *, bool);
428 : static rtx subst (rtx, rtx, rtx, bool, bool, bool);
429 : static rtx combine_simplify_rtx (rtx, machine_mode, bool, bool);
430 : static rtx simplify_if_then_else (rtx);
431 : static rtx simplify_set (rtx);
432 : static rtx simplify_logical (rtx);
433 : static rtx expand_compound_operation (rtx);
434 : static const_rtx expand_field_assignment (const_rtx);
435 : static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT, rtx,
436 : unsigned HOST_WIDE_INT, bool, bool, bool);
437 : static int get_pos_from_mask (unsigned HOST_WIDE_INT,
438 : unsigned HOST_WIDE_INT *);
439 : static rtx canon_reg_for_combine (rtx, rtx);
440 : static rtx force_int_to_mode (rtx, scalar_int_mode, scalar_int_mode,
441 : scalar_int_mode, unsigned HOST_WIDE_INT, bool);
442 : static rtx force_to_mode (rtx, machine_mode,
443 : unsigned HOST_WIDE_INT, bool);
444 : static rtx if_then_else_cond (rtx, rtx *, rtx *);
445 : static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
446 : static bool rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
447 : static rtx make_field_assignment (rtx);
448 : static rtx apply_distributive_law (rtx);
449 : static rtx distribute_and_simplify_rtx (rtx, int);
450 : static rtx simplify_and_const_int_1 (scalar_int_mode, rtx,
451 : unsigned HOST_WIDE_INT);
452 : static rtx simplify_and_const_int (rtx, scalar_int_mode, rtx,
453 : unsigned HOST_WIDE_INT);
454 : static bool merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
455 : HOST_WIDE_INT, machine_mode, bool *);
456 : static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
457 : static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
458 : int);
459 : static int recog_for_combine (rtx *, rtx_insn *, rtx *, unsigned = 0, unsigned = 0);
460 : static rtx gen_lowpart_for_combine (machine_mode, rtx);
461 : static rtx gen_lowpart_for_combine_no_emit (machine_mode, rtx);
462 : static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
463 : rtx *, rtx *);
464 : static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
465 : static void update_table_tick (rtx);
466 : static void record_value_for_reg (rtx, rtx_insn *, rtx);
467 : static void check_promoted_subreg (rtx_insn *, rtx);
468 : static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
469 : static void record_dead_and_set_regs (rtx_insn *);
470 : static bool get_last_value_validate (rtx *, rtx_insn *, int, bool);
471 : static rtx get_last_value (const_rtx);
472 : static void reg_dead_at_p_1 (rtx, const_rtx, void *);
473 : static bool reg_dead_at_p (rtx, rtx_insn *);
474 : static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
475 : static bool reg_bitfield_target_p (rtx, rtx);
476 : static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *,
477 : rtx, rtx, rtx);
478 : static void distribute_links (struct insn_link *, rtx_insn * = nullptr,
479 : int limit = INT_MAX);
480 : static void mark_used_regs_combine (rtx);
481 : static void record_promoted_value (rtx_insn *, rtx);
482 : static bool unmentioned_reg_p (rtx, rtx);
483 : static void record_truncated_values (rtx *, void *);
484 : static bool reg_truncated_to_mode (machine_mode, const_rtx);
485 : static rtx gen_lowpart_or_truncate (machine_mode, rtx);
486 :
487 :
488 : /* It is not safe to use ordinary gen_lowpart in combine.
489 : See comments in gen_lowpart_for_combine. */
490 : #undef RTL_HOOKS_GEN_LOWPART
491 : #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
492 :
493 : /* Our implementation of gen_lowpart never emits a new pseudo. */
494 : #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
495 : #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine_no_emit
496 :
497 : #undef RTL_HOOKS_REG_NONZERO_REG_BITS
498 : #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
499 :
500 : #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
501 : #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
502 :
503 : #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
504 : #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
505 :
506 : static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
507 :
508 :
509 : /* Convenience wrapper for the canonicalize_comparison target hook.
510 : Target hooks cannot use enum rtx_code. */
511 : static inline void
512 25348260 : target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
513 : bool op0_preserve_value)
514 : {
515 25348260 : int code_int = (int)*code;
516 25348260 : targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
517 25348260 : *code = (enum rtx_code)code_int;
518 : }
519 :
520 : /* Try to split PATTERN found in INSN. This returns NULL_RTX if
521 : PATTERN cannot be split. Otherwise, it returns an insn sequence.
522 : Updates OLD_NREGS with the max number of regs before the split
523 : and NEW_NREGS after the split.
524 : This is a wrapper around split_insns which ensures that the
525 : reg_stat vector is made larger if the splitter creates a new
526 : register. */
527 :
528 : static rtx_insn *
529 12341385 : combine_split_insns (rtx pattern, rtx_insn *insn,
530 : unsigned int *old_nregs,
531 : unsigned int *new_regs)
532 : {
533 12341385 : rtx_insn *ret;
534 12341385 : unsigned int nregs;
535 12341385 : *old_nregs = max_reg_num ();
536 12341385 : ret = split_insns (pattern, insn);
537 12341385 : *new_regs = nregs = max_reg_num ();
538 24682770 : if (nregs > reg_stat.length ())
539 2257 : reg_stat.safe_grow_cleared (nregs, true);
540 12341385 : return ret;
541 : }
542 :
543 : /* This is used by find_single_use to locate an rtx in LOC that
544 : contains exactly one use of DEST, which is typically a REG.
545 : It returns a pointer to the innermost rtx expression
546 : containing DEST. Appearances of DEST that are being used to
547 : totally replace it are not counted. */
548 :
549 : static rtx *
550 33300440 : find_single_use_1 (rtx dest, rtx *loc)
551 : {
552 40325327 : rtx x = *loc;
553 40325327 : enum rtx_code code = GET_CODE (x);
554 40325327 : rtx *result = NULL;
555 40325327 : rtx *this_result;
556 40325327 : int i;
557 40325327 : const char *fmt;
558 :
559 40325327 : switch (code)
560 : {
561 : case CONST:
562 : case LABEL_REF:
563 : case SYMBOL_REF:
564 : CASE_CONST_ANY:
565 : case CLOBBER:
566 : return 0;
567 :
568 6979438 : case SET:
569 : /* If the destination is anything other than PC, a REG or a SUBREG
570 : of a REG that occupies all of the REG, the insn uses DEST if
571 : it is mentioned in the destination or the source. Otherwise, we
572 : need just check the source. */
573 6979438 : if (GET_CODE (SET_DEST (x)) != PC
574 6979438 : && !REG_P (SET_DEST (x))
575 6981959 : && ! (GET_CODE (SET_DEST (x)) == SUBREG
576 2521 : && REG_P (SUBREG_REG (SET_DEST (x)))
577 2521 : && !read_modify_subreg_p (SET_DEST (x))))
578 : break;
579 :
580 6978059 : return find_single_use_1 (dest, &SET_SRC (x));
581 :
582 46828 : case MEM:
583 46828 : case SUBREG:
584 46828 : return find_single_use_1 (dest, &XEXP (x, 0));
585 :
586 : default:
587 : break;
588 : }
589 :
590 : /* If it wasn't one of the common cases above, check each expression and
591 : vector of this code. Look for a unique usage of DEST. */
592 :
593 20058662 : fmt = GET_RTX_FORMAT (code);
594 53638127 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
595 : {
596 33587206 : if (fmt[i] == 'e')
597 : {
598 33197065 : if (dest == XEXP (x, i)
599 33197065 : || (REG_P (dest) && REG_P (XEXP (x, i))
600 888112 : && REGNO (dest) == REGNO (XEXP (x, i))))
601 : this_result = loc;
602 : else
603 26215107 : this_result = find_single_use_1 (dest, &XEXP (x, i));
604 :
605 33197065 : if (result == NULL)
606 : result = this_result;
607 41158 : else if (this_result)
608 : /* Duplicate usage. */
609 : return NULL;
610 : }
611 390141 : else if (fmt[i] == 'E')
612 : {
613 53350 : int j;
614 :
615 162746 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
616 : {
617 113504 : if (XVECEXP (x, i, j) == dest
618 113504 : || (REG_P (dest)
619 113504 : && REG_P (XVECEXP (x, i, j))
620 5179 : && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
621 : this_result = loc;
622 : else
623 113504 : this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
624 :
625 113504 : if (result == NULL)
626 : result = this_result;
627 18216 : else if (this_result)
628 : return NULL;
629 : }
630 : }
631 : }
632 :
633 : return result;
634 : }
635 :
636 :
637 : /* See if DEST, produced in INSN, is used only a single time in the
638 : sequel. If so, return a pointer to the innermost rtx expression in which
639 : it is used.
640 :
641 : If PLOC is nonzero, *PLOC is set to the insn containing the single use.
642 :
643 : Otherwise, we find the single use by finding an insn that has a
644 : LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
645 : only referenced once in that insn, we know that it must be the first
646 : and last insn referencing DEST. */
647 :
648 : static rtx *
649 7600813 : find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
650 : {
651 7600813 : basic_block bb;
652 7600813 : rtx_insn *next;
653 7600813 : rtx *result;
654 7600813 : struct insn_link *link;
655 :
656 7600813 : if (!REG_P (dest))
657 : return 0;
658 :
659 7600813 : bb = BLOCK_FOR_INSN (insn);
660 10577019 : for (next = NEXT_INSN (insn);
661 10577019 : next && BLOCK_FOR_INSN (next) == bb;
662 2976206 : next = NEXT_INSN (next))
663 9948035 : if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
664 : {
665 9374465 : FOR_EACH_LOG_LINK (link, next)
666 8301706 : if (link->insn == insn && link->regno == REGNO (dest))
667 : break;
668 :
669 8044588 : if (link)
670 : {
671 6971829 : result = find_single_use_1 (dest, &PATTERN (next));
672 6971829 : if (ploc)
673 6971828 : *ploc = next;
674 : return result;
675 : }
676 : }
677 :
678 : return 0;
679 : }
680 :
681 : /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
682 : insn. The substitution can be undone by undo_all. If INTO is already
683 : set to NEWVAL, do not record this change. Because computing NEWVAL might
684 : also call SUBST, we have to compute it before we put anything into
685 : the undo table. */
686 :
687 : static void
688 773544793 : do_SUBST (rtx *into, rtx newval)
689 : {
690 773544793 : struct undo *buf;
691 773544793 : rtx oldval = *into;
692 :
693 773544793 : if (oldval == newval)
694 : return;
695 :
696 : /* We'd like to catch as many invalid transformations here as
697 : possible. Unfortunately, there are way too many mode changes
698 : that are perfectly valid, so we'd waste too much effort for
699 : little gain doing the checks here. Focus on catching invalid
700 : transformations involving integer constants. */
701 98172551 : if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
702 59751579 : && CONST_INT_P (newval))
703 : {
704 : /* Sanity check that we're replacing oldval with a CONST_INT
705 : that is a valid sign-extension for the original mode. */
706 1859933 : gcc_assert (INTVAL (newval)
707 : == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
708 :
709 : /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
710 : CONST_INT is not valid, because after the replacement, the
711 : original mode would be gone. Unfortunately, we can't tell
712 : when do_SUBST is called to replace the operand thereof, so we
713 : perform this test on oldval instead, checking whether an
714 : invalid replacement took place before we got here. */
715 1859933 : gcc_assert (!(GET_CODE (oldval) == SUBREG
716 : && CONST_INT_P (SUBREG_REG (oldval))));
717 1859933 : gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
718 : && CONST_INT_P (XEXP (oldval, 0))));
719 : }
720 :
721 98172551 : if (undobuf.frees)
722 93949945 : buf = undobuf.frees, undobuf.frees = buf->next;
723 : else
724 4222606 : buf = XNEW (struct undo);
725 :
726 98172551 : buf->kind = UNDO_RTX;
727 98172551 : buf->where.r = into;
728 98172551 : buf->old_contents.r = oldval;
729 98172551 : *into = newval;
730 :
731 98172551 : buf->next = undobuf.undos, undobuf.undos = buf;
732 : }
733 :
734 : #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
735 :
736 : /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
737 : for the value of a HOST_WIDE_INT value (including CONST_INT) is
738 : not safe. */
739 :
740 : static void
741 16426204 : do_SUBST_INT (int *into, int newval)
742 : {
743 16426204 : struct undo *buf;
744 16426204 : int oldval = *into;
745 :
746 16426204 : if (oldval == newval)
747 : return;
748 :
749 7217129 : if (undobuf.frees)
750 6671851 : buf = undobuf.frees, undobuf.frees = buf->next;
751 : else
752 545278 : buf = XNEW (struct undo);
753 :
754 7217129 : buf->kind = UNDO_INT;
755 7217129 : buf->where.i = into;
756 7217129 : buf->old_contents.i = oldval;
757 7217129 : *into = newval;
758 :
759 7217129 : buf->next = undobuf.undos, undobuf.undos = buf;
760 : }
761 :
762 : #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
763 :
764 : /* Similar to SUBST, but just substitute the mode. This is used when
765 : changing the mode of a pseudo-register, so that any other
766 : references to the entry in the regno_reg_rtx array will change as
767 : well. */
768 :
769 : static void
770 1456683 : subst_mode (int regno, machine_mode newval)
771 : {
772 1456683 : struct undo *buf;
773 1456683 : rtx reg = regno_reg_rtx[regno];
774 1456683 : machine_mode oldval = GET_MODE (reg);
775 :
776 1456683 : if (oldval == newval)
777 : return;
778 :
779 1456683 : if (undobuf.frees)
780 1378817 : buf = undobuf.frees, undobuf.frees = buf->next;
781 : else
782 77866 : buf = XNEW (struct undo);
783 :
784 1456683 : buf->kind = UNDO_MODE;
785 1456683 : buf->where.regno = regno;
786 1456683 : buf->old_contents.m = oldval;
787 1456683 : adjust_reg_mode (reg, newval);
788 :
789 1456683 : buf->next = undobuf.undos, undobuf.undos = buf;
790 : }
791 :
792 : /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
793 :
794 : static void
795 73629 : do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
796 : {
797 73629 : struct undo *buf;
798 73629 : struct insn_link * oldval = *into;
799 :
800 73629 : if (oldval == newval)
801 : return;
802 :
803 73629 : if (undobuf.frees)
804 70446 : buf = undobuf.frees, undobuf.frees = buf->next;
805 : else
806 3183 : buf = XNEW (struct undo);
807 :
808 73629 : buf->kind = UNDO_LINKS;
809 73629 : buf->where.l = into;
810 73629 : buf->old_contents.l = oldval;
811 73629 : *into = newval;
812 :
813 73629 : buf->next = undobuf.undos, undobuf.undos = buf;
814 : }
815 :
816 : #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
817 :
818 : /* Subroutine of try_combine. Determine whether the replacement patterns
819 : NEWPAT, NEWI2PAT and NEWOTHERPAT are more expensive according to insn_cost
820 : than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
821 : that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
822 : undobuf.other_insn may also both be NULL_RTX. Return false if the cost
823 : of all the instructions can be estimated and the replacements are more
824 : expensive than the original sequence. */
825 :
826 : static bool
827 4339872 : combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
828 : rtx newpat, rtx newi2pat, rtx newotherpat,
829 : int insn_code, int i2_code, int other_code)
830 : {
831 4339872 : int i0_cost, i1_cost, i2_cost, i3_cost;
832 4339872 : int new_i2_cost, new_i3_cost;
833 4339872 : int old_cost, new_cost;
834 :
835 : /* Lookup the original insn_costs. */
836 4339872 : i2_cost = INSN_COST (i2);
837 4339872 : i3_cost = INSN_COST (i3);
838 :
839 4339872 : if (i1)
840 : {
841 122717 : i1_cost = INSN_COST (i1);
842 122717 : if (i0)
843 : {
844 5122 : i0_cost = INSN_COST (i0);
845 4993 : old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
846 10106 : ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
847 : }
848 : else
849 : {
850 113014 : old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
851 230607 : ? i1_cost + i2_cost + i3_cost : 0);
852 : i0_cost = 0;
853 : }
854 : }
855 : else
856 : {
857 4217155 : old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
858 : i1_cost = i0_cost = 0;
859 : }
860 :
861 : /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
862 : correct that. */
863 4339872 : if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
864 2079 : old_cost -= i1_cost;
865 :
866 :
867 : /* Calculate the replacement insn_costs. */
868 4339872 : rtx tmp = PATTERN (i3);
869 4339872 : PATTERN (i3) = newpat;
870 4339872 : int tmpi = INSN_CODE (i3);
871 4339872 : INSN_CODE (i3) = insn_code;
872 4339872 : new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
873 4339872 : PATTERN (i3) = tmp;
874 4339872 : INSN_CODE (i3) = tmpi;
875 4339872 : if (newi2pat)
876 : {
877 227851 : tmp = PATTERN (i2);
878 227851 : PATTERN (i2) = newi2pat;
879 227851 : tmpi = INSN_CODE (i2);
880 227851 : INSN_CODE (i2) = i2_code;
881 227851 : new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
882 227851 : PATTERN (i2) = tmp;
883 227851 : INSN_CODE (i2) = tmpi;
884 227851 : new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
885 227851 : ? new_i2_cost + new_i3_cost : 0;
886 : }
887 : else
888 : {
889 : new_cost = new_i3_cost;
890 : new_i2_cost = 0;
891 : }
892 :
893 4339872 : if (undobuf.other_insn)
894 : {
895 229428 : int old_other_cost, new_other_cost;
896 :
897 229428 : old_other_cost = INSN_COST (undobuf.other_insn);
898 229428 : tmp = PATTERN (undobuf.other_insn);
899 229428 : PATTERN (undobuf.other_insn) = newotherpat;
900 229428 : tmpi = INSN_CODE (undobuf.other_insn);
901 229428 : INSN_CODE (undobuf.other_insn) = other_code;
902 229428 : new_other_cost = insn_cost (undobuf.other_insn,
903 : optimize_this_for_speed_p);
904 229428 : PATTERN (undobuf.other_insn) = tmp;
905 229428 : INSN_CODE (undobuf.other_insn) = tmpi;
906 229428 : if (old_other_cost > 0 && new_other_cost > 0)
907 : {
908 229428 : old_cost += old_other_cost;
909 229428 : new_cost += new_other_cost;
910 : }
911 : else
912 : old_cost = 0;
913 : }
914 :
915 : /* Disallow this combination if both new_cost and old_cost are greater than
916 : zero, and new_cost is greater than old cost. */
917 4339872 : bool reject = old_cost > 0 && new_cost > old_cost;
918 :
919 4339872 : if (dump_file)
920 : {
921 680 : fprintf (dump_file, "%s combination of insns ",
922 : reject ? "rejecting" : "allowing");
923 342 : if (i0)
924 0 : fprintf (dump_file, "%d, ", INSN_UID (i0));
925 342 : if (i1 && INSN_UID (i1) != INSN_UID (i2))
926 1 : fprintf (dump_file, "%d, ", INSN_UID (i1));
927 342 : fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
928 :
929 342 : fprintf (dump_file, "original costs ");
930 342 : if (i0)
931 0 : fprintf (dump_file, "%d + ", i0_cost);
932 342 : if (i1 && INSN_UID (i1) != INSN_UID (i2))
933 1 : fprintf (dump_file, "%d + ", i1_cost);
934 342 : fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
935 :
936 342 : if (newi2pat)
937 23 : fprintf (dump_file, "replacement costs %d + %d = %d\n",
938 : new_i2_cost, new_i3_cost, new_cost);
939 : else
940 319 : fprintf (dump_file, "replacement cost %d\n", new_cost);
941 : }
942 :
943 4339872 : if (reject)
944 : return false;
945 :
946 : /* Update the uid_insn_cost array with the replacement costs. */
947 4118165 : INSN_COST (i2) = new_i2_cost;
948 4118165 : INSN_COST (i3) = new_i3_cost;
949 4118165 : if (i1)
950 : {
951 103531 : INSN_COST (i1) = 0;
952 103531 : if (i0)
953 4736 : INSN_COST (i0) = 0;
954 : }
955 :
956 : return true;
957 : }
958 :
959 :
960 : /* Delete any insns that copy a register to itself.
961 : Return true if the CFG was changed. */
962 :
963 : static bool
964 1025221 : delete_noop_moves (void)
965 : {
966 1025221 : rtx_insn *insn, *next;
967 1025221 : basic_block bb;
968 :
969 1025221 : bool edges_deleted = false;
970 :
971 11640906 : FOR_EACH_BB_FN (bb, cfun)
972 : {
973 142984383 : for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
974 : {
975 132368698 : next = NEXT_INSN (insn);
976 132368698 : if (INSN_P (insn) && noop_move_p (insn))
977 : {
978 6478 : if (dump_file)
979 0 : fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
980 :
981 6478 : edges_deleted |= delete_insn_and_edges (insn);
982 : }
983 : }
984 : }
985 :
986 1025221 : return edges_deleted;
987 : }
988 :
989 :
990 : /* Return false if we do not want to (or cannot) combine DEF. */
991 : static bool
992 43123922 : can_combine_def_p (df_ref def)
993 : {
994 : /* Do not consider if it is pre/post modification in MEM. */
995 43123922 : if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
996 : return false;
997 :
998 41440085 : unsigned int regno = DF_REF_REGNO (def);
999 :
1000 : /* Do not combine frame pointer adjustments. */
1001 41440085 : if ((regno == FRAME_POINTER_REGNUM
1002 0 : && (!reload_completed || frame_pointer_needed))
1003 2098 : || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1004 41440085 : && regno == HARD_FRAME_POINTER_REGNUM
1005 : && (!reload_completed || frame_pointer_needed))
1006 41437987 : || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1007 0 : && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1008 2098 : return false;
1009 :
1010 : return true;
1011 : }
1012 :
1013 : /* Return false if we do not want to (or cannot) combine USE. */
1014 : static bool
1015 80019086 : can_combine_use_p (df_ref use)
1016 : {
1017 : /* Do not consider the usage of the stack pointer by function call. */
1018 0 : if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1019 0 : return false;
1020 :
1021 : return true;
1022 : }
1023 :
1024 : /* Fill in log links field for all insns. */
1025 :
1026 : static void
1027 1025221 : create_log_links (void)
1028 : {
1029 1025221 : basic_block bb;
1030 1025221 : rtx_insn **next_use;
1031 1025221 : rtx_insn *insn;
1032 1025221 : df_ref def, use;
1033 :
1034 1025221 : next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1035 :
1036 : /* Pass through each block from the end, recording the uses of each
1037 : register and establishing log links when def is encountered.
1038 : Note that we do not clear next_use array in order to save time,
1039 : so we have to test whether the use is in the same basic block as def.
1040 :
1041 : There are a few cases below when we do not consider the definition or
1042 : usage -- these are taken from original flow.c did. Don't ask me why it is
1043 : done this way; I don't know and if it works, I don't want to know. */
1044 :
1045 11640906 : FOR_EACH_BB_FN (bb, cfun)
1046 : {
1047 142967645 : FOR_BB_INSNS_REVERSE (bb, insn)
1048 : {
1049 132351960 : if (!NONDEBUG_INSN_P (insn))
1050 68694290 : continue;
1051 :
1052 : /* Log links are created only once. */
1053 63657670 : gcc_assert (!LOG_LINKS (insn));
1054 :
1055 521511888 : FOR_EACH_INSN_DEF (def, insn)
1056 : {
1057 457854218 : unsigned int regno = DF_REF_REGNO (def);
1058 457854218 : rtx_insn *use_insn;
1059 :
1060 457854218 : if (!next_use[regno])
1061 414730296 : continue;
1062 :
1063 43123922 : if (!can_combine_def_p (def))
1064 1685935 : continue;
1065 :
1066 41437987 : use_insn = next_use[regno];
1067 41437987 : next_use[regno] = NULL;
1068 :
1069 41437987 : if (BLOCK_FOR_INSN (use_insn) != bb)
1070 2309353 : continue;
1071 :
1072 : /* flow.c claimed:
1073 :
1074 : We don't build a LOG_LINK for hard registers contained
1075 : in ASM_OPERANDs. If these registers get replaced,
1076 : we might wind up changing the semantics of the insn,
1077 : even if reload can make what appear to be valid
1078 : assignments later. */
1079 39129512 : if (regno < FIRST_PSEUDO_REGISTER
1080 39128634 : && asm_noperands (PATTERN (use_insn)) >= 0)
1081 878 : continue;
1082 :
1083 : /* Don't add duplicate links between instructions. */
1084 39127756 : struct insn_link *links;
1085 52506282 : FOR_EACH_LOG_LINK (links, use_insn)
1086 13378526 : if (insn == links->insn && regno == links->regno)
1087 : break;
1088 :
1089 39127756 : if (!links)
1090 39127756 : LOG_LINKS (use_insn)
1091 78255512 : = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1092 : }
1093 :
1094 143676756 : FOR_EACH_INSN_USE (use, insn)
1095 155226244 : if (can_combine_use_p (use))
1096 75207158 : next_use[DF_REF_REGNO (use)] = insn;
1097 : }
1098 : }
1099 :
1100 1025221 : free (next_use);
1101 1025221 : }
1102 :
1103 : /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1104 : true if we found a LOG_LINK that proves that A feeds B. This only works
1105 : if there are no instructions between A and B which could have a link
1106 : depending on A, since in that case we would not record a link for B. */
1107 :
1108 : static bool
1109 13457916 : insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1110 : {
1111 13457916 : struct insn_link *links;
1112 16986793 : FOR_EACH_LOG_LINK (links, b)
1113 14298900 : if (links->insn == a)
1114 : return true;
1115 : return false;
1116 : }
1117 :
1118 : /* Main entry point for combiner. F is the first insn of the function.
1119 : NREGS is the first unused pseudo-reg number.
1120 :
1121 : Return nonzero if the CFG was changed (e.g. if the combiner has
1122 : turned an indirect jump instruction into a direct jump). */
1123 : static bool
1124 1070227 : combine_instructions (rtx_insn *f, unsigned int nregs)
1125 : {
1126 1070227 : rtx_insn *insn, *next;
1127 1070227 : struct insn_link *links, *nextlinks;
1128 1070227 : rtx_insn *first;
1129 1070227 : basic_block last_bb;
1130 :
1131 1070227 : bool new_direct_jump_p = false;
1132 :
1133 3203092 : for (first = f; first && !NONDEBUG_INSN_P (first); )
1134 2132865 : first = NEXT_INSN (first);
1135 1070227 : if (!first)
1136 : return false;
1137 :
1138 1025221 : combine_attempts = 0;
1139 1025221 : combine_merges = 0;
1140 1025221 : combine_extras = 0;
1141 1025221 : combine_successes = 0;
1142 :
1143 1025221 : rtl_hooks = combine_rtl_hooks;
1144 :
1145 1025221 : reg_stat.safe_grow_cleared (nregs, true);
1146 :
1147 1025221 : init_recog_no_volatile ();
1148 :
1149 : /* Allocate array for insn info. */
1150 1025221 : max_uid_known = get_max_uid ();
1151 1025221 : uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1152 1025221 : uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1153 1025221 : gcc_obstack_init (&insn_link_obstack);
1154 :
1155 1025221 : nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1156 :
1157 : /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1158 : problems when, for example, we have j <<= 1 in a loop. */
1159 :
1160 1025221 : nonzero_sign_valid = 0;
1161 1025221 : label_tick = label_tick_ebb_start = 1;
1162 :
1163 : /* Scan all SETs and see if we can deduce anything about what
1164 : bits are known to be zero for some registers and how many copies
1165 : of the sign bit are known to exist for those registers.
1166 :
1167 : Also set any known values so that we can use it while searching
1168 : for what bits are known to be set. */
1169 :
1170 1025221 : setup_incoming_promotions (first);
1171 : /* Allow the entry block and the first block to fall into the same EBB.
1172 : Conceptually the incoming promotions are assigned to the entry block. */
1173 1025221 : last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1174 :
1175 1025221 : create_log_links ();
1176 11640906 : FOR_EACH_BB_FN (this_basic_block, cfun)
1177 : {
1178 10615685 : optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1179 10615685 : last_call_luid = 0;
1180 10615685 : mem_last_set = -1;
1181 :
1182 10615685 : label_tick++;
1183 10615685 : if (!single_pred_p (this_basic_block)
1184 10615685 : || single_pred (this_basic_block) != last_bb)
1185 5084011 : label_tick_ebb_start = label_tick;
1186 10615685 : last_bb = this_basic_block;
1187 :
1188 142967645 : FOR_BB_INSNS (this_basic_block, insn)
1189 132351960 : if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1190 : {
1191 115525525 : rtx links;
1192 :
1193 115525525 : subst_low_luid = DF_INSN_LUID (insn);
1194 115525525 : subst_insn = insn;
1195 :
1196 115525525 : note_stores (insn, set_nonzero_bits_and_sign_copies, insn);
1197 115525525 : record_dead_and_set_regs (insn);
1198 :
1199 115525525 : if (AUTO_INC_DEC)
1200 : for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1201 : if (REG_NOTE_KIND (links) == REG_INC)
1202 : set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1203 : insn);
1204 :
1205 : /* Record the current insn_cost of this instruction. */
1206 115525525 : INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1207 115525525 : if (dump_file)
1208 : {
1209 2227 : fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1210 2227 : dump_insn_slim (dump_file, insn);
1211 : }
1212 : }
1213 : }
1214 :
1215 1025221 : nonzero_sign_valid = 1;
1216 :
1217 : /* Now scan all the insns in forward order. */
1218 1025221 : label_tick = label_tick_ebb_start = 1;
1219 1025221 : init_reg_last ();
1220 1025221 : setup_incoming_promotions (first);
1221 1025221 : last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1222 1025221 : int max_combine = param_max_combine_insns;
1223 :
1224 11640906 : FOR_EACH_BB_FN (this_basic_block, cfun)
1225 : {
1226 10615685 : rtx_insn *last_combined_insn = NULL;
1227 :
1228 : /* Ignore instruction combination in basic blocks that are going to
1229 : be removed as unreachable anyway. See PR82386. */
1230 10615685 : if (EDGE_COUNT (this_basic_block->preds) == 0)
1231 1616 : continue;
1232 :
1233 10614069 : optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1234 10614069 : last_call_luid = 0;
1235 10614069 : mem_last_set = -1;
1236 :
1237 10614069 : label_tick++;
1238 10614069 : if (!single_pred_p (this_basic_block)
1239 10614069 : || single_pred (this_basic_block) != last_bb)
1240 5083627 : label_tick_ebb_start = label_tick;
1241 10614069 : last_bb = this_basic_block;
1242 :
1243 10614069 : rtl_profile_for_bb (this_basic_block);
1244 10614069 : for (insn = BB_HEAD (this_basic_block);
1245 147626541 : insn != NEXT_INSN (BB_END (this_basic_block));
1246 132894307 : insn = next ? next : NEXT_INSN (insn))
1247 : {
1248 137012472 : next = 0;
1249 137012472 : if (!NONDEBUG_INSN_P (insn))
1250 68939231 : continue;
1251 :
1252 : while (last_combined_insn
1253 68075069 : && (!NONDEBUG_INSN_P (last_combined_insn)
1254 57650868 : || last_combined_insn->deleted ()))
1255 1828 : last_combined_insn = PREV_INSN (last_combined_insn);
1256 68073241 : if (last_combined_insn == NULL_RTX
1257 57650273 : || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1258 125723304 : || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1259 : last_combined_insn = insn;
1260 :
1261 : /* See if we know about function return values before this
1262 : insn based upon SUBREG flags. */
1263 68073241 : check_promoted_subreg (insn, PATTERN (insn));
1264 :
1265 : /* See if we can find hardregs and subreg of pseudos in
1266 : narrower modes. This could help turning TRUNCATEs
1267 : into SUBREGs. */
1268 68073241 : note_uses (&PATTERN (insn), record_truncated_values, NULL);
1269 :
1270 : /* Try this insn with each insn it links back to. */
1271 :
1272 106264867 : FOR_EACH_LOG_LINK (links, insn)
1273 42181515 : if ((next = try_combine (insn, links->insn, NULL,
1274 : NULL, &new_direct_jump_p,
1275 : last_combined_insn)) != 0)
1276 : {
1277 3989889 : statistics_counter_event (cfun, "two-insn combine", 1);
1278 3989889 : goto retry;
1279 : }
1280 :
1281 : /* Try each sequence of three linked insns ending with this one. */
1282 :
1283 64083352 : if (max_combine >= 3)
1284 101670665 : FOR_EACH_LOG_LINK (links, insn)
1285 : {
1286 37770348 : rtx_insn *link = links->insn;
1287 :
1288 : /* If the linked insn has been replaced by a note, then there
1289 : is no point in pursuing this chain any further. */
1290 37770348 : if (NOTE_P (link))
1291 154 : continue;
1292 :
1293 56225635 : FOR_EACH_LOG_LINK (nextlinks, link)
1294 18536439 : if ((next = try_combine (insn, link, nextlinks->insn,
1295 : NULL, &new_direct_jump_p,
1296 : last_combined_insn)) != 0)
1297 : {
1298 80998 : statistics_counter_event (cfun, "three-insn combine", 1);
1299 80998 : goto retry;
1300 : }
1301 : }
1302 :
1303 : /* Try combining an insn with two different insns whose results it
1304 : uses. */
1305 63900317 : if (max_combine >= 3)
1306 101551838 : FOR_EACH_LOG_LINK (links, insn)
1307 50732332 : for (nextlinks = links->next; nextlinks;
1308 13065255 : nextlinks = nextlinks->next)
1309 13080811 : if ((next = try_combine (insn, links->insn,
1310 : nextlinks->insn, NULL,
1311 : &new_direct_jump_p,
1312 : last_combined_insn)) != 0)
1313 :
1314 : {
1315 15556 : statistics_counter_event (cfun, "three-insn combine", 1);
1316 15556 : goto retry;
1317 : }
1318 :
1319 : /* Try four-instruction combinations. */
1320 63884761 : if (max_combine >= 4)
1321 101528268 : FOR_EACH_LOG_LINK (links, insn)
1322 : {
1323 37648190 : struct insn_link *next1;
1324 37648190 : rtx_insn *link = links->insn;
1325 :
1326 : /* If the linked insn has been replaced by a note, then there
1327 : is no point in pursuing this chain any further. */
1328 37648190 : if (NOTE_P (link))
1329 154 : continue;
1330 :
1331 56079615 : FOR_EACH_LOG_LINK (next1, link)
1332 : {
1333 18433035 : rtx_insn *link1 = next1->insn;
1334 18433035 : if (NOTE_P (link1))
1335 24 : continue;
1336 : /* I0 -> I1 -> I2 -> I3. */
1337 30165227 : FOR_EACH_LOG_LINK (nextlinks, link1)
1338 11733563 : if ((next = try_combine (insn, link, link1,
1339 : nextlinks->insn,
1340 : &new_direct_jump_p,
1341 : last_combined_insn)) != 0)
1342 : {
1343 1347 : statistics_counter_event (cfun, "four-insn combine", 1);
1344 1347 : goto retry;
1345 : }
1346 : /* I0, I1 -> I2, I2 -> I3. */
1347 22441822 : for (nextlinks = next1->next; nextlinks;
1348 4010158 : nextlinks = nextlinks->next)
1349 4010267 : if ((next = try_combine (insn, link, link1,
1350 : nextlinks->insn,
1351 : &new_direct_jump_p,
1352 : last_combined_insn)) != 0)
1353 : {
1354 109 : statistics_counter_event (cfun, "four-insn combine", 1);
1355 109 : goto retry;
1356 : }
1357 : }
1358 :
1359 50708364 : for (next1 = links->next; next1; next1 = next1->next)
1360 : {
1361 13064932 : rtx_insn *link1 = next1->insn;
1362 13064932 : if (NOTE_P (link1))
1363 2 : continue;
1364 : /* I0 -> I2; I1, I2 -> I3. */
1365 16502788 : FOR_EACH_LOG_LINK (nextlinks, link)
1366 3440812 : if ((next = try_combine (insn, link, link1,
1367 : nextlinks->insn,
1368 : &new_direct_jump_p,
1369 : last_combined_insn)) != 0)
1370 : {
1371 2954 : statistics_counter_event (cfun, "four-insn combine", 1);
1372 2954 : goto retry;
1373 : }
1374 : /* I0 -> I1; I1, I2 -> I3. */
1375 16806247 : FOR_EACH_LOG_LINK (nextlinks, link1)
1376 3744465 : if ((next = try_combine (insn, link, link1,
1377 : nextlinks->insn,
1378 : &new_direct_jump_p,
1379 : last_combined_insn)) != 0)
1380 : {
1381 194 : statistics_counter_event (cfun, "four-insn combine", 1);
1382 194 : goto retry;
1383 : }
1384 : }
1385 : }
1386 :
1387 : /* Try this insn with each REG_EQUAL note it links back to. */
1388 101650539 : FOR_EACH_LOG_LINK (links, insn)
1389 : {
1390 37695463 : rtx set, note;
1391 37695463 : rtx_insn *temp = links->insn;
1392 37695463 : if ((set = single_set (temp)) != 0
1393 37293639 : && (note = find_reg_equal_equiv_note (temp)) != 0
1394 2772029 : && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1395 2772029 : && ! side_effects_p (SET_SRC (set))
1396 : /* Avoid using a register that may already been marked
1397 : dead by an earlier instruction. */
1398 2772029 : && ! unmentioned_reg_p (note, SET_SRC (set))
1399 39074237 : && (GET_MODE (note) == VOIDmode
1400 27488 : ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1401 1351286 : : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1402 1351253 : && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1403 0 : || (GET_MODE (XEXP (SET_DEST (set), 0))
1404 : == GET_MODE (note))))))
1405 : {
1406 : /* Temporarily replace the set's source with the
1407 : contents of the REG_EQUAL note. The insn will
1408 : be deleted or recognized by try_combine. */
1409 1378724 : rtx orig_src = SET_SRC (set);
1410 1378724 : rtx orig_dest = SET_DEST (set);
1411 1378724 : if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1412 0 : SET_DEST (set) = XEXP (SET_DEST (set), 0);
1413 1378724 : SET_SRC (set) = note;
1414 1378724 : i2mod = temp;
1415 1378724 : i2mod_old_rhs = copy_rtx (orig_src);
1416 1378724 : i2mod_new_rhs = copy_rtx (note);
1417 1378724 : next = try_combine (insn, i2mod, NULL, NULL,
1418 : &new_direct_jump_p,
1419 : last_combined_insn);
1420 1378724 : i2mod = NULL;
1421 1378724 : if (next)
1422 : {
1423 27118 : statistics_counter_event (cfun, "insn-with-note combine", 1);
1424 27118 : goto retry;
1425 : }
1426 1351606 : INSN_CODE (temp) = -1;
1427 1351606 : SET_SRC (set) = orig_src;
1428 1351606 : SET_DEST (set) = orig_dest;
1429 : }
1430 : }
1431 :
1432 63955076 : if (!NOTE_P (insn))
1433 63955076 : record_dead_and_set_regs (insn);
1434 :
1435 137012472 : retry:
1436 137012472 : ;
1437 : }
1438 : }
1439 :
1440 1025221 : default_rtl_profile ();
1441 1025221 : clear_bb_flags ();
1442 :
1443 1025221 : if (purge_all_dead_edges ())
1444 1286 : new_direct_jump_p = true;
1445 1025221 : if (delete_noop_moves ())
1446 0 : new_direct_jump_p = true;
1447 :
1448 : /* Clean up. */
1449 1025221 : obstack_free (&insn_link_obstack, NULL);
1450 1025221 : free (uid_log_links);
1451 1025221 : free (uid_insn_cost);
1452 1025221 : reg_stat.release ();
1453 :
1454 1025221 : {
1455 1025221 : struct undo *undo, *next;
1456 5874154 : for (undo = undobuf.frees; undo; undo = next)
1457 : {
1458 4848933 : next = undo->next;
1459 4848933 : free (undo);
1460 : }
1461 1025221 : undobuf.frees = 0;
1462 : }
1463 :
1464 1025221 : statistics_counter_event (cfun, "attempts", combine_attempts);
1465 1025221 : statistics_counter_event (cfun, "merges", combine_merges);
1466 1025221 : statistics_counter_event (cfun, "extras", combine_extras);
1467 1025221 : statistics_counter_event (cfun, "successes", combine_successes);
1468 :
1469 1025221 : nonzero_sign_valid = 0;
1470 1025221 : rtl_hooks = general_rtl_hooks;
1471 :
1472 : /* Make recognizer allow volatile MEMs again. */
1473 1025221 : init_recog ();
1474 :
1475 1025221 : return new_direct_jump_p;
1476 : }
1477 :
1478 : /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1479 :
1480 : static void
1481 1025221 : init_reg_last (void)
1482 : {
1483 1025221 : unsigned int i;
1484 1025221 : reg_stat_type *p;
1485 :
1486 148617161 : FOR_EACH_VEC_ELT (reg_stat, i, p)
1487 147591940 : memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1488 1025221 : }
1489 :
1490 : /* Set up any promoted values for incoming argument registers. */
1491 :
1492 : static void
1493 2050442 : setup_incoming_promotions (rtx_insn *first)
1494 : {
1495 2050442 : tree arg;
1496 2050442 : bool strictly_local = false;
1497 :
1498 5541668 : for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1499 3491226 : arg = DECL_CHAIN (arg))
1500 : {
1501 3491226 : rtx x, reg = DECL_INCOMING_RTL (arg);
1502 3491226 : int uns1, uns3;
1503 3491226 : machine_mode mode1, mode2, mode3, mode4;
1504 :
1505 : /* Only continue if the incoming argument is in a register. */
1506 3491226 : if (!REG_P (reg))
1507 3491126 : continue;
1508 :
1509 : /* Determine, if possible, whether all call sites of the current
1510 : function lie within the current compilation unit. (This does
1511 : take into account the exporting of a function via taking its
1512 : address, and so forth.) */
1513 2746830 : strictly_local
1514 2746830 : = cgraph_node::local_info_node (current_function_decl)->local;
1515 :
1516 : /* The mode and signedness of the argument before any promotions happen
1517 : (equal to the mode of the pseudo holding it at that stage). */
1518 2746830 : mode1 = TYPE_MODE (TREE_TYPE (arg));
1519 2746830 : uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1520 :
1521 : /* The mode and signedness of the argument after any source language and
1522 : TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1523 2746830 : mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1524 2746830 : uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1525 :
1526 : /* The mode and signedness of the argument as it is actually passed,
1527 : see assign_parm_setup_reg in function.cc. */
1528 2746830 : mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1529 2746830 : TREE_TYPE (cfun->decl), 0);
1530 :
1531 : /* The mode of the register in which the argument is being passed. */
1532 2746830 : mode4 = GET_MODE (reg);
1533 :
1534 : /* Eliminate sign extensions in the callee when:
1535 : (a) A mode promotion has occurred; */
1536 2746830 : if (mode1 == mode3)
1537 2746730 : continue;
1538 : /* (b) The mode of the register is the same as the mode of
1539 : the argument as it is passed; */
1540 100 : if (mode3 != mode4)
1541 0 : continue;
1542 : /* (c) There's no language level extension; */
1543 100 : if (mode1 == mode2)
1544 : ;
1545 : /* (c.1) All callers are from the current compilation unit. If that's
1546 : the case we don't have to rely on an ABI, we only have to know
1547 : what we're generating right now, and we know that we will do the
1548 : mode1 to mode2 promotion with the given sign. */
1549 0 : else if (!strictly_local)
1550 0 : continue;
1551 : /* (c.2) The combination of the two promotions is useful. This is
1552 : true when the signs match, or if the first promotion is unsigned.
1553 : In the later case, (sign_extend (zero_extend x)) is the same as
1554 : (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1555 0 : else if (uns1)
1556 0 : uns3 = true;
1557 0 : else if (uns3)
1558 0 : continue;
1559 :
1560 : /* Record that the value was promoted from mode1 to mode3,
1561 : so that any sign extension at the head of the current
1562 : function may be eliminated. */
1563 100 : x = gen_rtx_CLOBBER (mode1, const0_rtx);
1564 100 : x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1565 100 : record_value_for_reg (reg, first, x);
1566 : }
1567 2050442 : }
1568 :
1569 : /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1570 : that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1571 : because some machines (maybe most) will actually do the sign-extension and
1572 : this is the conservative approach.
1573 :
1574 : ??? For 2.5, try to tighten up the MD files in this regard instead of this
1575 : kludge. */
1576 :
1577 : static rtx
1578 0 : sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1579 : {
1580 0 : scalar_int_mode int_mode;
1581 0 : if (CONST_INT_P (src)
1582 0 : && is_a <scalar_int_mode> (mode, &int_mode)
1583 0 : && GET_MODE_PRECISION (int_mode) < prec
1584 0 : && INTVAL (src) > 0
1585 0 : && val_signbit_known_set_p (int_mode, INTVAL (src)))
1586 0 : src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1587 :
1588 0 : return src;
1589 : }
1590 :
1591 : /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1592 : and SET. */
1593 :
1594 : static void
1595 24143195 : update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1596 : rtx x)
1597 : {
1598 24143195 : rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1599 24143195 : unsigned HOST_WIDE_INT bits = 0;
1600 24143195 : rtx reg_equal = NULL, src = SET_SRC (set);
1601 24143195 : unsigned int num = 0;
1602 :
1603 24143195 : if (reg_equal_note)
1604 1007883 : reg_equal = XEXP (reg_equal_note, 0);
1605 :
1606 24143195 : if (SHORT_IMMEDIATES_SIGN_EXTEND)
1607 : {
1608 : src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1609 : if (reg_equal)
1610 : reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1611 : }
1612 :
1613 : /* Don't call nonzero_bits if it cannot change anything. */
1614 24143195 : if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1615 : {
1616 20889213 : machine_mode mode = GET_MODE (x);
1617 20889213 : if (GET_MODE_CLASS (mode) == MODE_INT
1618 20889213 : && HWI_COMPUTABLE_MODE_P (mode))
1619 20889081 : mode = nonzero_bits_mode;
1620 20889213 : bits = nonzero_bits (src, mode);
1621 20889213 : if (reg_equal && bits)
1622 956464 : bits &= nonzero_bits (reg_equal, mode);
1623 20889213 : rsp->nonzero_bits |= bits;
1624 : }
1625 :
1626 : /* Don't call num_sign_bit_copies if it cannot change anything. */
1627 24143195 : if (rsp->sign_bit_copies != 1)
1628 : {
1629 20735289 : num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1630 20735289 : if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1631 : {
1632 954250 : unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1633 954250 : if (num == 0 || numeq > num)
1634 20735289 : num = numeq;
1635 : }
1636 20735289 : if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1637 20006220 : rsp->sign_bit_copies = num;
1638 : }
1639 24143195 : }
1640 :
1641 : /* Called via note_stores. If X is a pseudo that is narrower than
1642 : HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1643 :
1644 : If we are setting only a portion of X and we can't figure out what
1645 : portion, assume all bits will be used since we don't know what will
1646 : be happening.
1647 :
1648 : Similarly, set how many bits of X are known to be copies of the sign bit
1649 : at all locations in the function. This is the smallest number implied
1650 : by any set of X. */
1651 :
1652 : static void
1653 74271532 : set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1654 : {
1655 74271532 : rtx_insn *insn = (rtx_insn *) data;
1656 74271532 : scalar_int_mode mode;
1657 :
1658 74271532 : if (REG_P (x)
1659 59828526 : && REGNO (x) >= FIRST_PSEUDO_REGISTER
1660 : /* If this register is undefined at the start of the file, we can't
1661 : say what its contents were. */
1662 59878006 : && ! REGNO_REG_SET_P
1663 : (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1664 29836597 : && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1665 99364560 : && HWI_COMPUTABLE_MODE_P (mode))
1666 : {
1667 24350696 : reg_stat_type *rsp = ®_stat[REGNO (x)];
1668 :
1669 24350696 : if (set == 0 || GET_CODE (set) == CLOBBER)
1670 : {
1671 22577 : rsp->nonzero_bits = GET_MODE_MASK (mode);
1672 22577 : rsp->sign_bit_copies = 1;
1673 22577 : return;
1674 : }
1675 :
1676 : /* If this register is being initialized using itself, and the
1677 : register is uninitialized in this basic block, and there are
1678 : no LOG_LINKS which set the register, then part of the
1679 : register is uninitialized. In that case we can't assume
1680 : anything about the number of nonzero bits.
1681 :
1682 : ??? We could do better if we checked this in
1683 : reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1684 : could avoid making assumptions about the insn which initially
1685 : sets the register, while still using the information in other
1686 : insns. We would have to be careful to check every insn
1687 : involved in the combination. */
1688 :
1689 24328119 : if (insn
1690 22871351 : && reg_referenced_p (x, PATTERN (insn))
1691 27012577 : && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1692 : REGNO (x)))
1693 : {
1694 269079 : struct insn_link *link;
1695 :
1696 399623 : FOR_EACH_LOG_LINK (link, insn)
1697 312458 : if (dead_or_set_p (link->insn, x))
1698 : break;
1699 269079 : if (!link)
1700 : {
1701 87165 : rsp->nonzero_bits = GET_MODE_MASK (mode);
1702 87165 : rsp->sign_bit_copies = 1;
1703 87165 : return;
1704 : }
1705 : }
1706 :
1707 : /* If this is a complex assignment, see if we can convert it into a
1708 : simple assignment. */
1709 24240954 : set = expand_field_assignment (set);
1710 :
1711 : /* If this is a simple assignment, or we have a paradoxical SUBREG,
1712 : set what we know about X. */
1713 :
1714 24240954 : if (SET_DEST (set) == x
1715 24240954 : || (paradoxical_subreg_p (SET_DEST (set))
1716 4743 : && SUBREG_REG (SET_DEST (set)) == x))
1717 24143195 : update_rsp_from_reg_equal (rsp, insn, set, x);
1718 : else
1719 : {
1720 97759 : rsp->nonzero_bits = GET_MODE_MASK (mode);
1721 97759 : rsp->sign_bit_copies = 1;
1722 : }
1723 : }
1724 : }
1725 :
1726 : /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1727 : optionally insns that were previously combined into I3 or that will be
1728 : combined into the merger of INSN and I3. The order is PRED, PRED2,
1729 : INSN, SUCC, SUCC2, I3.
1730 :
1731 : Return false if the combination is not allowed for any reason.
1732 :
1733 : If the combination is allowed, *PDEST will be set to the single
1734 : destination of INSN and *PSRC to the single source, and this function
1735 : will return true. */
1736 :
1737 : static bool
1738 62956477 : can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1739 : rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1740 : rtx *pdest, rtx *psrc)
1741 : {
1742 62956477 : int i;
1743 62956477 : const_rtx set = 0;
1744 62956477 : rtx src, dest;
1745 62956477 : rtx_insn *p;
1746 62956477 : rtx link;
1747 62956477 : bool all_adjacent = true;
1748 62956477 : bool (*is_volatile_p) (const_rtx);
1749 :
1750 62956477 : if (succ)
1751 : {
1752 14878018 : if (succ2)
1753 : {
1754 2231286 : if (next_active_insn (succ2) != i3)
1755 195912 : all_adjacent = false;
1756 2231286 : if (next_active_insn (succ) != succ2)
1757 2075742 : all_adjacent = false;
1758 : }
1759 12646732 : else if (next_active_insn (succ) != i3)
1760 2075742 : all_adjacent = false;
1761 14878018 : if (next_active_insn (insn) != succ)
1762 17577201 : all_adjacent = false;
1763 : }
1764 48078459 : else if (next_active_insn (insn) != i3)
1765 17577201 : all_adjacent = false;
1766 :
1767 : /* Can combine only if previous insn is a SET of a REG or a SUBREG,
1768 : or a PARALLEL consisting of such a SET and CLOBBERs.
1769 :
1770 : If INSN has CLOBBER parallel parts, ignore them for our processing.
1771 : By definition, these happen during the execution of the insn. When it
1772 : is merged with another insn, all bets are off. If they are, in fact,
1773 : needed and aren't also supplied in I3, they may be added by
1774 : recog_for_combine. Otherwise, it won't match.
1775 :
1776 : We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1777 : note.
1778 :
1779 : Get the source and destination of INSN. If more than one, can't
1780 : combine. */
1781 :
1782 62956477 : if (GET_CODE (PATTERN (insn)) == SET)
1783 : set = PATTERN (insn);
1784 16795926 : else if (GET_CODE (PATTERN (insn)) == PARALLEL
1785 16795926 : && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1786 : {
1787 50321857 : for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1788 : {
1789 34068838 : rtx elt = XVECEXP (PATTERN (insn), 0, i);
1790 :
1791 34068838 : switch (GET_CODE (elt))
1792 : {
1793 : /* This is important to combine floating point insns
1794 : for the SH4 port. */
1795 137695 : case USE:
1796 : /* Combining an isolated USE doesn't make sense.
1797 : We depend here on combinable_i3pat to reject them. */
1798 : /* The code below this loop only verifies that the inputs of
1799 : the SET in INSN do not change. We call reg_set_between_p
1800 : to verify that the REG in the USE does not change between
1801 : I3 and INSN.
1802 : If the USE in INSN was for a pseudo register, the matching
1803 : insn pattern will likely match any register; combining this
1804 : with any other USE would only be safe if we knew that the
1805 : used registers have identical values, or if there was
1806 : something to tell them apart, e.g. different modes. For
1807 : now, we forgo such complicated tests and simply disallow
1808 : combining of USES of pseudo registers with any other USE. */
1809 137695 : if (REG_P (XEXP (elt, 0))
1810 137695 : && GET_CODE (PATTERN (i3)) == PARALLEL)
1811 : {
1812 0 : rtx i3pat = PATTERN (i3);
1813 0 : int i = XVECLEN (i3pat, 0) - 1;
1814 0 : unsigned int regno = REGNO (XEXP (elt, 0));
1815 :
1816 0 : do
1817 : {
1818 0 : rtx i3elt = XVECEXP (i3pat, 0, i);
1819 :
1820 0 : if (GET_CODE (i3elt) == USE
1821 0 : && REG_P (XEXP (i3elt, 0))
1822 0 : && (REGNO (XEXP (i3elt, 0)) == regno
1823 0 : ? reg_set_between_p (XEXP (elt, 0),
1824 0 : PREV_INSN (insn), i3)
1825 : : regno >= FIRST_PSEUDO_REGISTER))
1826 : return false;
1827 : }
1828 0 : while (--i >= 0);
1829 : }
1830 : break;
1831 :
1832 : /* We can ignore CLOBBERs. */
1833 : case CLOBBER:
1834 : break;
1835 :
1836 17417267 : case SET:
1837 : /* Ignore SETs whose result isn't used but not those that
1838 : have side-effects. */
1839 17417267 : if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1840 203956 : && insn_nothrow_p (insn)
1841 17608182 : && !side_effects_p (elt))
1842 : break;
1843 :
1844 : /* If we have already found a SET, this is a second one and
1845 : so we cannot combine with this insn. */
1846 17312995 : if (set)
1847 : return false;
1848 :
1849 : set = elt;
1850 : break;
1851 :
1852 : default:
1853 : /* Anything else means we can't combine. */
1854 : return false;
1855 : }
1856 : }
1857 :
1858 16253019 : if (set == 0
1859 : /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1860 : so don't do anything with it. */
1861 16253019 : || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1862 : return false;
1863 : }
1864 : else
1865 : return false;
1866 :
1867 : if (set == 0)
1868 : return false;
1869 :
1870 : /* The simplification in expand_field_assignment may call back to
1871 : get_last_value, so set safe guard here. */
1872 62393121 : subst_low_luid = DF_INSN_LUID (insn);
1873 :
1874 62393121 : set = expand_field_assignment (set);
1875 62393121 : src = SET_SRC (set), dest = SET_DEST (set);
1876 :
1877 : /* Do not eliminate user-specified register if it is in an
1878 : asm input because we may break the register asm usage defined
1879 : in GCC manual if allow to do so.
1880 : Be aware that this may cover more cases than we expect but this
1881 : should be harmless. */
1882 61831046 : if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1883 62393124 : && extract_asm_operands (PATTERN (i3)))
1884 : return false;
1885 :
1886 : /* Don't eliminate a store in the stack pointer. */
1887 62393121 : if (dest == stack_pointer_rtx
1888 : /* Don't combine with an insn that sets a register to itself if it has
1889 : a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1890 60486021 : || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1891 : /* Can't merge an ASM_OPERANDS. */
1892 60486021 : || GET_CODE (src) == ASM_OPERANDS
1893 : /* Can't merge a function call. */
1894 60482403 : || GET_CODE (src) == CALL
1895 : /* Don't eliminate a function call argument. */
1896 60482403 : || (CALL_P (i3)
1897 9067227 : && (find_reg_fusage (i3, USE, dest)
1898 168567 : || (REG_P (dest)
1899 168567 : && REGNO (dest) < FIRST_PSEUDO_REGISTER
1900 283 : && global_regs[REGNO (dest)])))
1901 : /* Don't substitute into an incremented register. */
1902 : || FIND_REG_INC_NOTE (i3, dest)
1903 : || (succ && FIND_REG_INC_NOTE (succ, dest))
1904 60482403 : || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1905 : /* Don't substitute into a non-local goto, this confuses CFG. */
1906 51583740 : || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1907 : /* Make sure that DEST is not used after INSN but before SUCC, or
1908 : after SUCC and before SUCC2, or after SUCC2 but before I3. */
1909 51583007 : || (!all_adjacent
1910 12679130 : && ((succ2
1911 976455 : && (reg_used_between_p (dest, succ2, i3)
1912 952955 : || reg_used_between_p (dest, succ, succ2)))
1913 12608198 : || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1914 12322594 : || (!succ2 && !succ && reg_used_between_p (dest, insn, i3))
1915 12322594 : || (succ
1916 : /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1917 : that case SUCC is not in the insn stream, so use SUCC2
1918 : instead for this test. */
1919 10172826 : && reg_used_between_p (dest, insn,
1920 : succ2
1921 905523 : && INSN_UID (succ) == INSN_UID (succ2)
1922 : ? succ2 : succ))))
1923 : /* Make sure that the value that is to be substituted for the register
1924 : does not use any registers whose values alter in between. However,
1925 : If the insns are adjacent, a use can't cross a set even though we
1926 : think it might (this can happen for a sequence of insns each setting
1927 : the same destination; last_set of that register might point to
1928 : a NOTE). If INSN has a REG_EQUIV note, the register is always
1929 : equivalent to the memory so the substitution is valid even if there
1930 : are intervening stores. Also, don't move a volatile asm or
1931 : UNSPEC_VOLATILE across any other insns. */
1932 : || (! all_adjacent
1933 12322594 : && (((!MEM_P (src)
1934 3454135 : || ! find_reg_note (insn, REG_EQUIV, src))
1935 12208517 : && modified_between_p (src, insn, i3))
1936 11163060 : || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1937 11163060 : || GET_CODE (src) == UNSPEC_VOLATILE))
1938 : /* Don't combine across a CALL_INSN, because that would possibly
1939 : change whether the life span of some REGs crosses calls or not,
1940 : and it is a pain to update that information.
1941 : Exception: if source is a constant, moving it later can't hurt.
1942 : Accept that as a special case. */
1943 112449594 : || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1944 : return false;
1945 :
1946 : /* DEST must be a REG. */
1947 49756427 : if (REG_P (dest))
1948 : {
1949 : /* If register alignment is being enforced for multi-word items in all
1950 : cases except for parameters, it is possible to have a register copy
1951 : insn referencing a hard register that is not allowed to contain the
1952 : mode being copied and which would not be valid as an operand of most
1953 : insns. Eliminate this problem by not combining with such an insn.
1954 :
1955 : Also, on some machines we don't want to extend the life of a hard
1956 : register. */
1957 :
1958 49199223 : if (REG_P (src)
1959 49199223 : && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1960 29844 : && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
1961 : /* Don't extend the life of a hard register unless it is
1962 : user variable (if we have few registers) or it can't
1963 : fit into the desired register (meaning something special
1964 : is going on).
1965 : Also avoid substituting a return register into I3, because
1966 : reload can't handle a conflict with constraints of other
1967 : inputs. */
1968 2592534 : || (REGNO (src) < FIRST_PSEUDO_REGISTER
1969 37786 : && !targetm.hard_regno_mode_ok (REGNO (src),
1970 37786 : GET_MODE (src)))))
1971 : return false;
1972 : }
1973 : else
1974 : return false;
1975 :
1976 :
1977 49199223 : if (GET_CODE (PATTERN (i3)) == PARALLEL)
1978 37716974 : for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1979 25431952 : if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1980 : {
1981 11941683 : rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1982 :
1983 : /* If the clobber represents an earlyclobber operand, we must not
1984 : substitute an expression containing the clobbered register.
1985 : As we do not analyze the constraint strings here, we have to
1986 : make the conservative assumption. However, if the register is
1987 : a fixed hard reg, the clobber cannot represent any operand;
1988 : we leave it up to the machine description to either accept or
1989 : reject use-and-clobber patterns. */
1990 11941683 : if (!REG_P (reg)
1991 11580568 : || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1992 23473361 : || !fixed_regs[REGNO (reg)])
1993 448113 : if (reg_overlap_mentioned_p (reg, src))
1994 : return false;
1995 : }
1996 :
1997 : /* If INSN contains anything volatile, or is an `asm' (whether volatile
1998 : or not), reject, unless nothing volatile comes between it and I3 */
1999 :
2000 49198151 : if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2001 : {
2002 : /* Make sure neither succ nor succ2 contains a volatile reference. */
2003 725655 : if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2004 : return false;
2005 725543 : if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2006 : return false;
2007 : /* We'll check insns between INSN and I3 below. */
2008 : }
2009 :
2010 : /* If INSN is an asm, and DEST is a hard register, reject, since it has
2011 : to be an explicit register variable, and was chosen for a reason. */
2012 :
2013 49151253 : if (GET_CODE (src) == ASM_OPERANDS
2014 49151253 : && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2015 : return false;
2016 :
2017 : /* If INSN contains volatile references (specifically volatile MEMs),
2018 : we cannot combine across any other volatile references.
2019 : Even if INSN doesn't contain volatile references, any intervening
2020 : volatile insn might affect machine state. */
2021 :
2022 97622880 : is_volatile_p = volatile_refs_p (PATTERN (insn))
2023 49151253 : ? volatile_refs_p
2024 : : volatile_insn_p;
2025 :
2026 227600033 : for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2027 129504644 : if (NONDEBUG_INSN_P (p)
2028 64734011 : && p != succ
2029 64734011 : && p != succ2
2030 179188357 : && is_volatile_p (PATTERN (p)))
2031 : return false;
2032 :
2033 : /* If INSN contains an autoincrement or autodecrement, make sure that
2034 : register is not used between there and I3, and not already used in
2035 : I3 either. Neither must it be used in PRED or SUCC, if they exist.
2036 : Also insist that I3 not be a jump if using LRA; if it were one
2037 : and the incremented register were spilled, we would lose.
2038 : Reload handles this correctly. */
2039 :
2040 48944136 : if (AUTO_INC_DEC)
2041 : for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2042 : if (REG_NOTE_KIND (link) == REG_INC
2043 : && ((JUMP_P (i3) && targetm.lra_p ())
2044 : || reg_used_between_p (XEXP (link, 0), insn, i3)
2045 : || (pred != NULL_RTX
2046 : && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2047 : || (pred2 != NULL_RTX
2048 : && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2049 : || (succ != NULL_RTX
2050 : && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2051 : || (succ2 != NULL_RTX
2052 : && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2053 : || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2054 : return false;
2055 :
2056 : /* If we get here, we have passed all the tests and the combination is
2057 : to be allowed. */
2058 :
2059 48944136 : *pdest = dest;
2060 48944136 : *psrc = src;
2061 :
2062 48944136 : return true;
2063 : }
2064 :
2065 : /* LOC is the location within I3 that contains its pattern or the component
2066 : of a PARALLEL of the pattern. We validate that it is valid for combining.
2067 :
2068 : One problem is if I3 modifies its output, as opposed to replacing it
2069 : entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2070 : doing so would produce an insn that is not equivalent to the original insns.
2071 :
2072 : Consider:
2073 :
2074 : (set (reg:DI 101) (reg:DI 100))
2075 : (set (subreg:SI (reg:DI 101) 0) <foo>)
2076 :
2077 : This is NOT equivalent to:
2078 :
2079 : (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2080 : (set (reg:DI 101) (reg:DI 100))])
2081 :
2082 : Not only does this modify 100 (in which case it might still be valid
2083 : if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2084 :
2085 : We can also run into a problem if I2 sets a register that I1
2086 : uses and I1 gets directly substituted into I3 (not via I2). In that
2087 : case, we would be getting the wrong value of I2DEST into I3, so we
2088 : must reject the combination. This case occurs when I2 and I1 both
2089 : feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2090 : If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2091 : of a SET must prevent combination from occurring. The same situation
2092 : can occur for I0, in which case I0_NOT_IN_SRC is set.
2093 :
2094 : Before doing the above check, we first try to expand a field assignment
2095 : into a set of logical operations.
2096 :
2097 : If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2098 : we place a register that is both set and used within I3. If more than one
2099 : such register is detected, we fail.
2100 :
2101 : Return true if the combination is valid, false otherwise. */
2102 :
2103 : static bool
2104 70786741 : combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2105 : bool i1_not_in_src, bool i0_not_in_src, rtx *pi3dest_killed)
2106 : {
2107 70786741 : rtx x = *loc;
2108 :
2109 70786741 : if (GET_CODE (x) == SET)
2110 : {
2111 47439618 : rtx set = x ;
2112 47439618 : rtx dest = SET_DEST (set);
2113 47439618 : rtx src = SET_SRC (set);
2114 47439618 : rtx inner_dest = dest;
2115 47439618 : rtx subdest;
2116 :
2117 47439618 : while (GET_CODE (inner_dest) == STRICT_LOW_PART
2118 47957349 : || GET_CODE (inner_dest) == SUBREG
2119 47957349 : || GET_CODE (inner_dest) == ZERO_EXTRACT)
2120 517731 : inner_dest = XEXP (inner_dest, 0);
2121 :
2122 : /* Check for the case where I3 modifies its output, as discussed
2123 : above. We don't want to prevent pseudos from being combined
2124 : into the address of a MEM, so only prevent the combination if
2125 : i1 or i2 set the same MEM. */
2126 497115 : if ((inner_dest != dest &&
2127 : (!MEM_P (inner_dest)
2128 787 : || rtx_equal_p (i2dest, inner_dest)
2129 787 : || (i1dest && rtx_equal_p (i1dest, inner_dest))
2130 787 : || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2131 496328 : && (reg_overlap_mentioned_p (i2dest, inner_dest)
2132 366173 : || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2133 364880 : || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2134 :
2135 : /* This is the same test done in can_combine_p except we can't test
2136 : all_adjacent; we don't have to, since this instruction will stay
2137 : in place, thus we are not considering increasing the lifetime of
2138 : INNER_DEST.
2139 :
2140 : Also, if this insn sets a function argument, combining it with
2141 : something that might need a spill could clobber a previous
2142 : function argument; the all_adjacent test in can_combine_p also
2143 : checks this; here, we do a more specific test for this case. */
2144 :
2145 47308076 : || (REG_P (inner_dest)
2146 30644693 : && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2147 7450356 : && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2148 7450356 : GET_MODE (inner_dest)))
2149 47307822 : || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2150 94740257 : || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2151 : return false;
2152 :
2153 : /* If DEST is used in I3, it is being killed in this insn, so
2154 : record that for later. We have to consider paradoxical
2155 : subregs here, since they kill the whole register, but we
2156 : ignore partial subregs, STRICT_LOW_PART, etc.
2157 : Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2158 : STACK_POINTER_REGNUM, since these are always considered to be
2159 : live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2160 47269757 : subdest = dest;
2161 47269757 : if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2162 255381 : subdest = SUBREG_REG (subdest);
2163 47269757 : if (pi3dest_killed
2164 34294405 : && REG_P (subdest)
2165 21899488 : && reg_referenced_p (subdest, PATTERN (i3))
2166 1288976 : && REGNO (subdest) != FRAME_POINTER_REGNUM
2167 1288976 : && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2168 1288976 : || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2169 1288976 : && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2170 1288976 : || (REGNO (subdest) != ARG_POINTER_REGNUM
2171 0 : || ! fixed_regs [REGNO (subdest)]))
2172 48558733 : && REGNO (subdest) != STACK_POINTER_REGNUM)
2173 : {
2174 1251315 : if (*pi3dest_killed)
2175 : return false;
2176 :
2177 1226606 : *pi3dest_killed = subdest;
2178 : }
2179 : }
2180 :
2181 23347123 : else if (GET_CODE (x) == PARALLEL)
2182 : {
2183 : int i;
2184 :
2185 35550367 : for (i = 0; i < XVECLEN (x, 0); i++)
2186 23971985 : if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2187 : i1_not_in_src, i0_not_in_src, pi3dest_killed))
2188 : return false;
2189 : }
2190 :
2191 : return true;
2192 : }
2193 :
2194 : /* Return true if X is an arithmetic expression that contains a multiplication
2195 : and division. We don't count multiplications by powers of two here. */
2196 :
2197 : static bool
2198 17770028 : contains_muldiv (rtx x)
2199 : {
2200 18462918 : switch (GET_CODE (x))
2201 : {
2202 : case MOD: case DIV: case UMOD: case UDIV:
2203 : return true;
2204 :
2205 562992 : case MULT:
2206 562992 : return ! (CONST_INT_P (XEXP (x, 1))
2207 129443 : && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2208 17734825 : default:
2209 17734825 : if (BINARY_P (x))
2210 6108170 : return contains_muldiv (XEXP (x, 0))
2211 6108170 : || contains_muldiv (XEXP (x, 1));
2212 :
2213 11626655 : if (UNARY_P (x))
2214 692890 : return contains_muldiv (XEXP (x, 0));
2215 :
2216 : return false;
2217 : }
2218 : }
2219 :
2220 : /* Determine whether INSN can be used in a combination. Return true if
2221 : not. This is used in try_combine to detect early some cases where we
2222 : can't perform combinations. */
2223 :
2224 : static bool
2225 170699014 : cant_combine_insn_p (rtx_insn *insn)
2226 : {
2227 170699014 : rtx set;
2228 170699014 : rtx src, dest;
2229 :
2230 : /* If this isn't really an insn, we can't do anything.
2231 : This can occur when flow deletes an insn that it has merged into an
2232 : auto-increment address. */
2233 170699014 : if (!NONDEBUG_INSN_P (insn))
2234 : return true;
2235 :
2236 : /* Never combine loads and stores involving hard regs that are likely
2237 : to be spilled. The register allocator can usually handle such
2238 : reg-reg moves by tying. If we allow the combiner to make
2239 : substitutions of likely-spilled regs, reload might die.
2240 : As an exception, we allow combinations involving fixed regs; these are
2241 : not available to the register allocator so there's no risk involved. */
2242 :
2243 170698802 : set = single_set (insn);
2244 170698802 : if (! set)
2245 : return false;
2246 157052438 : src = SET_SRC (set);
2247 157052438 : dest = SET_DEST (set);
2248 157052438 : if (GET_CODE (src) == SUBREG)
2249 1168807 : src = SUBREG_REG (src);
2250 157052438 : if (GET_CODE (dest) == SUBREG)
2251 1743500 : dest = SUBREG_REG (dest);
2252 42410833 : if (REG_P (src) && REG_P (dest)
2253 192608098 : && ((HARD_REGISTER_P (src)
2254 6877218 : && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2255 : #ifdef LEAF_REGISTERS
2256 : && ! LEAF_REGISTERS [REGNO (src)])
2257 : #else
2258 : )
2259 : #endif
2260 29024331 : || (HARD_REGISTER_P (dest)
2261 20669943 : && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2262 20360817 : && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2263 25297494 : return true;
2264 :
2265 : return false;
2266 : }
2267 :
2268 : struct likely_spilled_retval_info
2269 : {
2270 : unsigned regno, nregs;
2271 : unsigned mask;
2272 : };
2273 :
2274 : /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2275 : hard registers that are known to be written to / clobbered in full. */
2276 : static void
2277 188311 : likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2278 : {
2279 188311 : struct likely_spilled_retval_info *const info =
2280 : (struct likely_spilled_retval_info *) data;
2281 188311 : unsigned regno, nregs;
2282 188311 : unsigned new_mask;
2283 :
2284 188311 : if (!REG_P (XEXP (set, 0)))
2285 : return;
2286 188311 : regno = REGNO (x);
2287 188311 : if (regno >= info->regno + info->nregs)
2288 : return;
2289 188311 : nregs = REG_NREGS (x);
2290 188311 : if (regno + nregs <= info->regno)
2291 : return;
2292 188311 : new_mask = (2U << (nregs - 1)) - 1;
2293 188311 : if (regno < info->regno)
2294 0 : new_mask >>= info->regno - regno;
2295 : else
2296 188311 : new_mask <<= regno - info->regno;
2297 188311 : info->mask &= ~new_mask;
2298 : }
2299 :
2300 : /* Return true iff part of the return value is live during INSN, and
2301 : it is likely spilled. This can happen when more than one insn is needed
2302 : to copy the return value, e.g. when we consider to combine into the
2303 : second copy insn for a complex value. */
2304 :
2305 : static bool
2306 48439130 : likely_spilled_retval_p (rtx_insn *insn)
2307 : {
2308 48439130 : rtx_insn *use = BB_END (this_basic_block);
2309 48439130 : rtx reg;
2310 48439130 : rtx_insn *p;
2311 48439130 : unsigned regno, nregs;
2312 : /* We assume here that no machine mode needs more than
2313 : 32 hard registers when the value overlaps with a register
2314 : for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2315 48439130 : unsigned mask;
2316 48439130 : struct likely_spilled_retval_info info;
2317 :
2318 : /* With -g, BB_END may be a DEBUG_INSN. Ignore trailing debug
2319 : instructions without changing how notes are handled. */
2320 48439130 : if (DEBUG_INSN_P (use))
2321 1122394 : use = prev_nondebug_insn (use);
2322 48439130 : if (!NONJUMP_INSN_P (use)
2323 12676453 : || GET_CODE (PATTERN (use)) != USE
2324 51869992 : || insn == use)
2325 : return false;
2326 3307347 : reg = XEXP (PATTERN (use), 0);
2327 3307347 : if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2328 : return false;
2329 3307347 : regno = REGNO (reg);
2330 3307347 : nregs = REG_NREGS (reg);
2331 3307347 : if (nregs == 1)
2332 : return false;
2333 185711 : mask = (2U << (nregs - 1)) - 1;
2334 :
2335 : /* Disregard parts of the return value that are set later. */
2336 185711 : info.regno = regno;
2337 185711 : info.nregs = nregs;
2338 185711 : info.mask = mask;
2339 623800 : for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2340 252378 : if (INSN_P (p))
2341 252378 : note_stores (p, likely_spilled_retval_1, &info);
2342 371378 : mask = info.mask;
2343 :
2344 : /* Check if any of the (probably) live return value registers is
2345 : likely spilled. */
2346 : nregs --;
2347 371378 : do
2348 : {
2349 371378 : if ((mask & 1 << nregs)
2350 371378 : && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2351 : return true;
2352 371324 : } while (nregs--);
2353 : return false;
2354 : }
2355 :
2356 : /* Adjust INSN after we made a change to its destination.
2357 :
2358 : Changing the destination can invalidate notes that say something about
2359 : the results of the insn and a LOG_LINK pointing to the insn. */
2360 :
2361 : static void
2362 17377 : adjust_for_new_dest (rtx_insn *insn)
2363 : {
2364 : /* For notes, be conservative and simply remove them. */
2365 17377 : remove_reg_equal_equiv_notes (insn, true);
2366 :
2367 : /* The new insn will have a destination that was previously the destination
2368 : of an insn just above it. Call distribute_links to make a LOG_LINK from
2369 : the next use of that destination. */
2370 :
2371 17377 : rtx set = single_set (insn);
2372 17377 : gcc_assert (set);
2373 :
2374 17377 : rtx reg = SET_DEST (set);
2375 :
2376 17377 : while (GET_CODE (reg) == ZERO_EXTRACT
2377 17377 : || GET_CODE (reg) == STRICT_LOW_PART
2378 34754 : || GET_CODE (reg) == SUBREG)
2379 0 : reg = XEXP (reg, 0);
2380 17377 : gcc_assert (REG_P (reg));
2381 :
2382 17377 : distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2383 :
2384 17377 : df_insn_rescan (insn);
2385 17377 : }
2386 :
2387 : /* Return TRUE if combine can reuse reg X in mode MODE.
2388 : ADDED_SETS is trueif the original set is still required. */
2389 : static bool
2390 2753753 : can_change_dest_mode (rtx x, bool added_sets, machine_mode mode)
2391 : {
2392 2753753 : unsigned int regno;
2393 :
2394 2753753 : if (!REG_P (x))
2395 : return false;
2396 :
2397 : /* Don't change between modes with different underlying register sizes,
2398 : since this could lead to invalid subregs. */
2399 2753753 : if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2400 2753753 : REGMODE_NATURAL_SIZE (GET_MODE (x))))
2401 : return false;
2402 :
2403 2753753 : regno = REGNO (x);
2404 : /* Allow hard registers if the new mode is legal, and occupies no more
2405 : registers than the old mode. */
2406 2753753 : if (regno < FIRST_PSEUDO_REGISTER)
2407 1232714 : return (targetm.hard_regno_mode_ok (regno, mode)
2408 1232714 : && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2409 :
2410 : /* Or a pseudo that is only used once. */
2411 1521039 : return (regno < reg_n_sets_max
2412 1521015 : && REG_N_SETS (regno) == 1
2413 1468732 : && !added_sets
2414 2989771 : && !REG_USERVAR_P (x));
2415 : }
2416 :
2417 :
2418 : /* Check whether X, the destination of a set, refers to part of
2419 : the register specified by REG. */
2420 :
2421 : static bool
2422 17651 : reg_subword_p (rtx x, rtx reg)
2423 : {
2424 : /* Check that reg is an integer mode register. */
2425 17651 : if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2426 : return false;
2427 :
2428 17114 : if (GET_CODE (x) == STRICT_LOW_PART
2429 16678 : || GET_CODE (x) == ZERO_EXTRACT)
2430 460 : x = XEXP (x, 0);
2431 :
2432 17114 : return GET_CODE (x) == SUBREG
2433 16877 : && !paradoxical_subreg_p (x)
2434 16877 : && SUBREG_REG (x) == reg
2435 33991 : && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2436 : }
2437 :
2438 : /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2439 : by an arbitrary number of CLOBBERs. */
2440 : static bool
2441 103535319 : is_parallel_of_n_reg_sets (rtx pat, int n)
2442 : {
2443 103535319 : if (GET_CODE (pat) != PARALLEL)
2444 : return false;
2445 :
2446 27948227 : int len = XVECLEN (pat, 0);
2447 27948227 : if (len < n)
2448 : return false;
2449 :
2450 : int i;
2451 55691009 : for (i = 0; i < n; i++)
2452 52802455 : if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2453 31147805 : || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2454 : return false;
2455 3303977 : for ( ; i < len; i++)
2456 1037275 : switch (GET_CODE (XVECEXP (pat, 0, i)))
2457 : {
2458 415424 : case CLOBBER:
2459 415424 : if (XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2460 : return false;
2461 415423 : break;
2462 : default:
2463 : return false;
2464 : }
2465 : return true;
2466 : }
2467 :
2468 : /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2469 : CLOBBERs), can be split into individual SETs in that order, without
2470 : changing semantics. */
2471 : static bool
2472 386761 : can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2473 : {
2474 386761 : if (!insn_nothrow_p (insn))
2475 : return false;
2476 :
2477 385278 : rtx pat = PATTERN (insn);
2478 :
2479 385278 : int i, j;
2480 1034868 : for (i = 0; i < n; i++)
2481 : {
2482 710073 : if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2483 : return false;
2484 :
2485 706983 : rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2486 :
2487 1031778 : for (j = i + 1; j < n; j++)
2488 382188 : if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2489 : return false;
2490 : }
2491 :
2492 : return true;
2493 : }
2494 :
2495 : /* Return whether X is just a single_set, with the source
2496 : a general_operand. */
2497 : static bool
2498 68132010 : is_just_move (rtx_insn *x)
2499 : {
2500 68132010 : rtx set = single_set (x);
2501 68132010 : if (!set)
2502 : return false;
2503 :
2504 67708817 : return general_operand (SET_SRC (set), VOIDmode);
2505 : }
2506 :
2507 : /* Callback function to count autoincs. */
2508 :
2509 : static int
2510 1023561 : count_auto_inc (rtx, rtx, rtx, rtx, rtx, void *arg)
2511 : {
2512 1023561 : (*((int *) arg))++;
2513 :
2514 1023561 : return 0;
2515 : }
2516 :
2517 : /* Try to combine the insns I0, I1 and I2 into I3.
2518 : Here I0, I1 and I2 appear earlier than I3.
2519 : I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2520 : I3.
2521 :
2522 : If we are combining more than two insns and the resulting insn is not
2523 : recognized, try splitting it into two insns. If that happens, I2 and I3
2524 : are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2525 : Otherwise, I0, I1 and I2 are pseudo-deleted.
2526 :
2527 : Return 0 if the combination does not work. Then nothing is changed.
2528 : If we did the combination, return the insn at which combine should
2529 : resume scanning.
2530 :
2531 : Set NEW_DIRECT_JUMP_P to true if try_combine creates a
2532 : new direct jump instruction.
2533 :
2534 : LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2535 : been I3 passed to an earlier try_combine within the same basic
2536 : block. */
2537 :
2538 : static rtx_insn *
2539 98106596 : try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2540 : bool *new_direct_jump_p, rtx_insn *last_combined_insn)
2541 : {
2542 : /* New patterns for I3 and I2, respectively. */
2543 98106596 : rtx newpat, newi2pat = 0;
2544 98106596 : rtvec newpat_vec_with_clobbers = 0;
2545 98106596 : bool substed_i2 = false, substed_i1 = false, substed_i0 = false;
2546 : /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2547 : dead. */
2548 98106596 : bool added_sets_0, added_sets_1, added_sets_2;
2549 : /* Total number of SETs to put into I3. */
2550 98106596 : int total_sets;
2551 : /* Nonzero if I2's or I1's body now appears in I3. */
2552 98106596 : int i2_is_used = 0, i1_is_used = 0;
2553 : /* INSN_CODEs for new I3, new I2, and user of condition code. */
2554 98106596 : int insn_code_number, i2_code_number = 0, other_code_number = 0;
2555 : /* Contains I3 if the destination of I3 is used in its source, which means
2556 : that the old life of I3 is being killed. If that usage is placed into
2557 : I2 and not in I3, a REG_DEAD note must be made. */
2558 98106596 : rtx i3dest_killed = 0;
2559 : /* SET_DEST and SET_SRC of I2, I1 and I0. */
2560 98106596 : rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2561 : /* Copy of SET_SRC of I1 and I0, if needed. */
2562 98106596 : rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2563 : /* Set if I2DEST was reused as a scratch register. */
2564 98106596 : bool i2scratch = false;
2565 : /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2566 98106596 : rtx i0pat = 0, i1pat = 0, i2pat = 0;
2567 : /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2568 98106596 : bool i2dest_in_i2src = false, i1dest_in_i1src = false;
2569 98106596 : bool i2dest_in_i1src = false, i0dest_in_i0src = false;
2570 98106596 : bool i1dest_in_i0src = false, i2dest_in_i0src = false;;
2571 98106596 : bool i2dest_killed = false, i1dest_killed = false, i0dest_killed = false;
2572 98106596 : bool i1_feeds_i2_n = false, i0_feeds_i2_n = false, i0_feeds_i1_n = false;
2573 : /* Notes that must be added to REG_NOTES in I3 and I2. */
2574 98106596 : rtx new_i3_notes, new_i2_notes;
2575 : /* Notes that we substituted I3 into I2 instead of the normal case. */
2576 98106596 : bool i3_subst_into_i2 = false;
2577 : /* Notes that I1, I2 or I3 is a MULT operation. */
2578 98106596 : bool have_mult = false;
2579 98106596 : bool swap_i2i3 = false;
2580 98106596 : bool split_i2i3 = false;
2581 98106596 : bool changed_i3_dest = false;
2582 98106596 : bool i2_was_move = false, i3_was_move = false;
2583 98106596 : int n_auto_inc = 0;
2584 :
2585 98106596 : int maxreg;
2586 98106596 : rtx_insn *temp_insn;
2587 98106596 : rtx temp_expr;
2588 98106596 : struct insn_link *link;
2589 98106596 : rtx other_pat = 0;
2590 98106596 : rtx new_other_notes;
2591 98106596 : int i;
2592 98106596 : scalar_int_mode dest_mode, temp_mode;
2593 98106596 : bool has_non_call_exception = false;
2594 :
2595 : /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2596 : never be). */
2597 98106596 : if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2598 : return 0;
2599 :
2600 : /* Only try four-insn combinations when there's high likelihood of
2601 : success. Look for simple insns, such as loads of constants or
2602 : binary operations involving a constant. */
2603 22526260 : if (i0)
2604 : {
2605 22526260 : int i;
2606 22526260 : int ngood = 0;
2607 22526260 : int nshift = 0;
2608 22526260 : rtx set0, set3;
2609 :
2610 22526260 : if (!flag_expensive_optimizations)
2611 : return 0;
2612 :
2613 88777989 : for (i = 0; i < 4; i++)
2614 : {
2615 72633856 : rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2616 72633856 : rtx set = single_set (insn);
2617 72633856 : rtx src;
2618 72633856 : if (!set)
2619 2306476 : continue;
2620 70327380 : src = SET_SRC (set);
2621 70327380 : if (CONSTANT_P (src))
2622 : {
2623 4889263 : ngood += 2;
2624 4889263 : break;
2625 : }
2626 65438117 : else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2627 8482880 : ngood++;
2628 56955237 : else if (GET_CODE (src) == IF_THEN_ELSE)
2629 2151197 : ngood++;
2630 54804040 : else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2631 54705739 : || GET_CODE (src) == LSHIFTRT)
2632 131364 : nshift++;
2633 : }
2634 :
2635 : /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2636 : are likely manipulating its value. Ideally we'll be able to combine
2637 : all four insns into a bitfield insertion of some kind.
2638 :
2639 : Note the source in I0 might be inside a sign/zero extension and the
2640 : memory modes in I0 and I3 might be different. So extract the address
2641 : from the destination of I3 and search for it in the source of I0.
2642 :
2643 : In the event that there's a match but the source/dest do not actually
2644 : refer to the same memory, the worst that happens is we try some
2645 : combinations that we wouldn't have otherwise. */
2646 21033396 : if ((set0 = single_set (i0))
2647 : /* Ensure the source of SET0 is a MEM, possibly buried inside
2648 : an extension. */
2649 20899949 : && (GET_CODE (SET_SRC (set0)) == MEM
2650 17634622 : || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2651 17634622 : || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2652 559778 : && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2653 3378918 : && (set3 = single_set (i3))
2654 : /* Ensure the destination of SET3 is a MEM. */
2655 2936643 : && GET_CODE (SET_DEST (set3)) == MEM
2656 : /* Would it be better to extract the base address for the MEM
2657 : in SET3 and look for that? I don't have cases where it matters
2658 : but I could envision such cases. */
2659 21335998 : && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2660 22761 : ngood += 2;
2661 :
2662 21033396 : if (ngood < 2 && nshift < 2)
2663 : return 0;
2664 : }
2665 :
2666 : /* Exit early if one of the insns involved can't be used for
2667 : combinations. */
2668 82801539 : if (CALL_P (i2)
2669 77684865 : || (i1 && CALL_P (i1))
2670 74208459 : || (i0 && CALL_P (i0))
2671 73736836 : || cant_combine_insn_p (i3)
2672 70296280 : || cant_combine_insn_p (i2)
2673 53884752 : || (i1 && cant_combine_insn_p (i1))
2674 48678246 : || (i0 && cant_combine_insn_p (i0))
2675 131240669 : || likely_spilled_retval_p (i3))
2676 : return 0;
2677 :
2678 48439076 : combine_attempts++;
2679 48439076 : undobuf.other_insn = 0;
2680 :
2681 : /* Reset the hard register usage information. */
2682 48439076 : CLEAR_HARD_REG_SET (newpat_used_regs);
2683 :
2684 48439076 : if (dump_file && (dump_flags & TDF_DETAILS))
2685 : {
2686 174 : if (i0)
2687 20 : fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2688 20 : INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2689 154 : else if (i1)
2690 26 : fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2691 26 : INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2692 : else
2693 128 : fprintf (dump_file, "\nTrying %d -> %d:\n",
2694 128 : INSN_UID (i2), INSN_UID (i3));
2695 :
2696 174 : if (i0)
2697 20 : dump_insn_slim (dump_file, i0);
2698 174 : if (i1)
2699 46 : dump_insn_slim (dump_file, i1);
2700 174 : dump_insn_slim (dump_file, i2);
2701 174 : dump_insn_slim (dump_file, i3);
2702 : }
2703 :
2704 : /* If multiple insns feed into one of I2 or I3, they can be in any
2705 : order. To simplify the code below, reorder them in sequence. */
2706 48439076 : if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2707 : std::swap (i0, i2);
2708 48439076 : if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2709 : std::swap (i0, i1);
2710 48439076 : if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2711 : std::swap (i1, i2);
2712 :
2713 48439076 : added_links_insn = 0;
2714 48439076 : added_notes_insn = 0;
2715 :
2716 : /* First check for one important special case that the code below will
2717 : not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2718 : and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2719 : we may be able to replace that destination with the destination of I3.
2720 : This occurs in the common code where we compute both a quotient and
2721 : remainder into a structure, in which case we want to do the computation
2722 : directly into the structure to avoid register-register copies.
2723 :
2724 : Note that this case handles both multiple sets in I2 and also cases
2725 : where I2 has a number of CLOBBERs inside the PARALLEL.
2726 :
2727 : We make very conservative checks below and only try to handle the
2728 : most common cases of this. For example, we only handle the case
2729 : where I2 and I3 are adjacent to avoid making difficult register
2730 : usage tests. */
2731 :
2732 30221294 : if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2733 15656854 : && REG_P (SET_SRC (PATTERN (i3)))
2734 5288271 : && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2735 5039511 : && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2736 4124163 : && GET_CODE (PATTERN (i2)) == PARALLEL
2737 1115146 : && ! side_effects_p (SET_DEST (PATTERN (i3)))
2738 : /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2739 : below would need to check what is inside (and reg_overlap_mentioned_p
2740 : doesn't support those codes anyway). Don't allow those destinations;
2741 : the resulting insn isn't likely to be recognized anyway. */
2742 619359 : && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2743 619339 : && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2744 618132 : && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2745 618132 : SET_DEST (PATTERN (i3)))
2746 49057100 : && next_active_insn (i2) == i3)
2747 : {
2748 405953 : rtx p2 = PATTERN (i2);
2749 :
2750 : /* Make sure that the destination of I3,
2751 : which we are going to substitute into one output of I2,
2752 : is not used within another output of I2. We must avoid making this:
2753 : (parallel [(set (mem (reg 69)) ...)
2754 : (set (reg 69) ...)])
2755 : which is not well-defined as to order of actions.
2756 : (Besides, reload can't handle output reloads for this.)
2757 :
2758 : The problem can also happen if the dest of I3 is a memory ref,
2759 : if another dest in I2 is an indirect memory ref.
2760 :
2761 : Neither can this PARALLEL be an asm. We do not allow combining
2762 : that usually (see can_combine_p), so do not here either. */
2763 405953 : bool ok = true;
2764 1230627 : for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2765 : {
2766 824674 : if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2767 405578 : || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2768 1647898 : && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2769 823224 : SET_DEST (XVECEXP (p2, 0, i))))
2770 : ok = false;
2771 823889 : else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2772 418313 : && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2773 824674 : ok = false;
2774 : }
2775 :
2776 405953 : if (ok)
2777 528057 : for (i = 0; i < XVECLEN (p2, 0); i++)
2778 467783 : if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2779 467783 : && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2780 : {
2781 343743 : combine_merges++;
2782 :
2783 343743 : subst_insn = i3;
2784 343743 : subst_low_luid = DF_INSN_LUID (i2);
2785 :
2786 343743 : added_sets_2 = added_sets_1 = added_sets_0 = false;
2787 343743 : i2src = SET_SRC (XVECEXP (p2, 0, i));
2788 343743 : i2dest = SET_DEST (XVECEXP (p2, 0, i));
2789 343743 : i2dest_killed = dead_or_set_p (i2, i2dest);
2790 :
2791 : /* Replace the dest in I2 with our dest and make the resulting
2792 : insn the new pattern for I3. Then skip to where we validate
2793 : the pattern. Everything was set up above. */
2794 343743 : SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2795 343743 : newpat = p2;
2796 343743 : i3_subst_into_i2 = true;
2797 343743 : goto validate_replacement;
2798 : }
2799 : }
2800 :
2801 : /* If I2 is setting a pseudo to a constant and I3 is setting some
2802 : sub-part of it to another constant, merge them by making a new
2803 : constant. */
2804 48095333 : if (i1 == 0
2805 29877551 : && (temp_expr = single_set (i2)) != 0
2806 29592154 : && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2807 19528889 : && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2808 2918887 : && GET_CODE (PATTERN (i3)) == SET
2809 1446033 : && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2810 48112984 : && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2811 : {
2812 16877 : rtx dest = SET_DEST (PATTERN (i3));
2813 16877 : rtx temp_dest = SET_DEST (temp_expr);
2814 16877 : int offset = -1;
2815 16877 : int width = 0;
2816 :
2817 16877 : if (GET_CODE (dest) == ZERO_EXTRACT)
2818 : {
2819 1 : if (CONST_INT_P (XEXP (dest, 1))
2820 1 : && CONST_INT_P (XEXP (dest, 2))
2821 2 : && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2822 : &dest_mode))
2823 : {
2824 1 : width = INTVAL (XEXP (dest, 1));
2825 1 : offset = INTVAL (XEXP (dest, 2));
2826 1 : dest = XEXP (dest, 0);
2827 1 : if (BITS_BIG_ENDIAN)
2828 : offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2829 : }
2830 : }
2831 : else
2832 : {
2833 16876 : if (GET_CODE (dest) == STRICT_LOW_PART)
2834 436 : dest = XEXP (dest, 0);
2835 16876 : if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2836 : {
2837 16876 : width = GET_MODE_PRECISION (dest_mode);
2838 16876 : offset = 0;
2839 : }
2840 : }
2841 :
2842 16877 : if (offset >= 0)
2843 : {
2844 : /* If this is the low part, we're done. */
2845 16877 : if (subreg_lowpart_p (dest))
2846 : ;
2847 : /* Handle the case where inner is twice the size of outer. */
2848 4986 : else if (GET_MODE_PRECISION (temp_mode)
2849 4986 : == 2 * GET_MODE_PRECISION (dest_mode))
2850 4983 : offset += GET_MODE_PRECISION (dest_mode);
2851 : /* Otherwise give up for now. */
2852 : else
2853 : offset = -1;
2854 : }
2855 :
2856 16874 : if (offset >= 0)
2857 : {
2858 16874 : rtx inner = SET_SRC (PATTERN (i3));
2859 16874 : rtx outer = SET_SRC (temp_expr);
2860 :
2861 33748 : wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2862 16874 : rtx_mode_t (inner, dest_mode),
2863 33748 : offset, width);
2864 :
2865 16874 : combine_merges++;
2866 16874 : subst_insn = i3;
2867 16874 : subst_low_luid = DF_INSN_LUID (i2);
2868 16874 : added_sets_2 = added_sets_1 = added_sets_0 = false;
2869 16874 : i2dest = temp_dest;
2870 16874 : i2dest_killed = dead_or_set_p (i2, i2dest);
2871 :
2872 : /* Replace the source in I2 with the new constant and make the
2873 : resulting insn the new pattern for I3. Then skip to where we
2874 : validate the pattern. Everything was set up above. */
2875 16874 : SUBST (SET_SRC (temp_expr),
2876 : immed_wide_int_const (o, temp_mode));
2877 :
2878 16874 : newpat = PATTERN (i2);
2879 :
2880 : /* The dest of I3 has been replaced with the dest of I2. */
2881 16874 : changed_i3_dest = true;
2882 16874 : goto validate_replacement;
2883 16874 : }
2884 : }
2885 :
2886 : /* If we have no I1 and I2 looks like:
2887 : (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2888 : (set Y OP)])
2889 : make up a dummy I1 that is
2890 : (set Y OP)
2891 : and change I2 to be
2892 : (set (reg:CC X) (compare:CC Y (const_int 0)))
2893 :
2894 : (We can ignore any trailing CLOBBERs.)
2895 :
2896 : This undoes a previous combination and allows us to match a branch-and-
2897 : decrement insn. */
2898 :
2899 48078459 : if (i1 == 0
2900 29860677 : && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2901 245457 : && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2902 : == MODE_CC)
2903 145953 : && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2904 119200 : && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2905 79522 : && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2906 79522 : SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2907 73851 : && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2908 48152310 : && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2909 : {
2910 : /* We make I1 with the same INSN_UID as I2. This gives it
2911 : the same DF_INSN_LUID for value tracking. Our fake I1 will
2912 : never appear in the insn stream so giving it the same INSN_UID
2913 : as I2 will not cause a problem. */
2914 :
2915 73629 : i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2916 73629 : XVECEXP (PATTERN (i2), 0, 1), i2->location (),
2917 : -1, NULL_RTX);
2918 73629 : INSN_UID (i1) = INSN_UID (i2);
2919 :
2920 73629 : SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2921 73629 : SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2922 : SET_DEST (PATTERN (i1)));
2923 73629 : unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2924 73629 : SUBST_LINK (LOG_LINKS (i2),
2925 : alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2926 : }
2927 :
2928 : /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2929 : make those two SETs separate I1 and I2 insns, and make an I0 that is
2930 : the original I1. */
2931 48078459 : if (i0 == 0
2932 45315107 : && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2933 386761 : && can_split_parallel_of_n_reg_sets (i2, 2)
2934 324795 : && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2935 292185 : && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
2936 275568 : && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2937 48354019 : && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2938 : {
2939 : /* If there is no I1, there is no I0 either. */
2940 275560 : i0 = i1;
2941 :
2942 : /* We make I1 with the same INSN_UID as I2. This gives it
2943 : the same DF_INSN_LUID for value tracking. Our fake I1 will
2944 : never appear in the insn stream so giving it the same INSN_UID
2945 : as I2 will not cause a problem. */
2946 :
2947 275560 : i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2948 275560 : XVECEXP (PATTERN (i2), 0, 0), i2->location (),
2949 : -1, NULL_RTX);
2950 275560 : INSN_UID (i1) = INSN_UID (i2);
2951 :
2952 275560 : SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2953 : }
2954 :
2955 : /* Verify that I2 and maybe I1 and I0 can be combined into I3. */
2956 48078459 : if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
2957 : {
2958 12332472 : if (dump_file && (dump_flags & TDF_DETAILS))
2959 8 : fprintf (dump_file, "Can't combine i2 into i3\n");
2960 12332472 : undo_all ();
2961 12332472 : return 0;
2962 : }
2963 35745987 : if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
2964 : {
2965 1435435 : if (dump_file && (dump_flags & TDF_DETAILS))
2966 0 : fprintf (dump_file, "Can't combine i1 into i3\n");
2967 1435435 : undo_all ();
2968 1435435 : return 0;
2969 : }
2970 34310552 : if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
2971 : {
2972 244434 : if (dump_file && (dump_flags & TDF_DETAILS))
2973 0 : fprintf (dump_file, "Can't combine i0 into i3\n");
2974 244434 : undo_all ();
2975 244434 : return 0;
2976 : }
2977 :
2978 : /* With non-call exceptions we can end up trying to combine multiple
2979 : insns with possible EH side effects. Make sure we can combine
2980 : that to a single insn which means there must be at most one insn
2981 : in the combination with an EH side effect. */
2982 34066118 : if (cfun->can_throw_non_call_exceptions)
2983 : {
2984 5988080 : if (find_reg_note (i3, REG_EH_REGION, NULL_RTX)
2985 5964518 : || find_reg_note (i2, REG_EH_REGION, NULL_RTX)
2986 5964436 : || (i1 && find_reg_note (i1, REG_EH_REGION, NULL_RTX))
2987 11952515 : || (i0 && find_reg_note (i0, REG_EH_REGION, NULL_RTX)))
2988 : {
2989 23645 : has_non_call_exception = true;
2990 23645 : if (insn_could_throw_p (i3)
2991 23645 : + insn_could_throw_p (i2)
2992 23645 : + (i1 ? insn_could_throw_p (i1) : 0)
2993 23645 : + (i0 ? insn_could_throw_p (i0) : 0) > 1)
2994 : {
2995 113 : if (dump_file && (dump_flags & TDF_DETAILS))
2996 0 : fprintf (dump_file, "Can't combine multiple insns with EH "
2997 : "side-effects\n");
2998 113 : undo_all ();
2999 113 : return 0;
3000 : }
3001 : }
3002 : }
3003 :
3004 : /* Record whether i2 and i3 are trivial moves. */
3005 34066005 : i2_was_move = is_just_move (i2);
3006 34066005 : i3_was_move = is_just_move (i3);
3007 :
3008 : /* Record whether I2DEST is used in I2SRC and similarly for the other
3009 : cases. Knowing this will help in register status updating below. */
3010 34066005 : i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3011 34066005 : i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3012 10966814 : i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3013 34066005 : i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3014 1986852 : i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3015 1986852 : i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3016 34066005 : i2dest_killed = dead_or_set_p (i2, i2dest);
3017 34066005 : i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3018 34066005 : i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3019 :
3020 : /* For the earlier insns, determine which of the subsequent ones they
3021 : feed. */
3022 34066005 : i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3023 34066005 : i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3024 3469454 : i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3025 1482602 : : (!reg_overlap_mentioned_p (i1dest, i0dest)
3026 1448796 : && reg_overlap_mentioned_p (i0dest, i2src))));
3027 :
3028 : /* Ensure that I3's pattern can be the destination of combines. */
3029 45032819 : if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3030 10966814 : i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3031 1986852 : i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3032 1957690 : || (i1dest_in_i0src && !i0_feeds_i1_n)),
3033 : &i3dest_killed))
3034 : {
3035 194486 : undo_all ();
3036 194486 : return 0;
3037 : }
3038 :
3039 : /* See if any of the insns is a MULT operation. Unless one is, we will
3040 : reject a combination that is, since it must be slower. Be conservative
3041 : here. */
3042 33871519 : if (GET_CODE (i2src) == MULT
3043 32975502 : || (i1 != 0 && GET_CODE (i1src) == MULT)
3044 32616785 : || (i0 != 0 && GET_CODE (i0src) == MULT)
3045 66438313 : || (GET_CODE (PATTERN (i3)) == SET
3046 25374535 : && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3047 : have_mult = true;
3048 :
3049 : /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3050 : We used to do this EXCEPT in one case: I3 has a post-inc in an
3051 : output operand. However, that exception can give rise to insns like
3052 : mov r3,(r3)+
3053 : which is a famous insn on the PDP-11 where the value of r3 used as the
3054 : source was model-dependent. Avoid this sort of thing. */
3055 :
3056 : #if 0
3057 : if (!(GET_CODE (PATTERN (i3)) == SET
3058 : && REG_P (SET_SRC (PATTERN (i3)))
3059 : && MEM_P (SET_DEST (PATTERN (i3)))
3060 : && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3061 : || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3062 : /* It's not the exception. */
3063 : #endif
3064 33871519 : if (AUTO_INC_DEC)
3065 : {
3066 : rtx link;
3067 : for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3068 : if (REG_NOTE_KIND (link) == REG_INC
3069 : && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3070 : || (i1 != 0
3071 : && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3072 : {
3073 : undo_all ();
3074 : return 0;
3075 : }
3076 : }
3077 :
3078 : /* See if the SETs in I1 or I2 need to be kept around in the merged
3079 : instruction: whenever the value set there is still needed past I3.
3080 : For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3081 :
3082 : For the SET in I1, we have two cases: if I1 and I2 independently feed
3083 : into I3, the set in I1 needs to be kept around unless I1DEST dies
3084 : or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3085 : in I1 needs to be kept around unless I1DEST dies or is set in either
3086 : I2 or I3. The same considerations apply to I0. */
3087 :
3088 33871519 : added_sets_2 = !dead_or_set_p (i3, i2dest);
3089 :
3090 33871519 : if (i1)
3091 10898735 : added_sets_1 = !(dead_or_set_p (i3, i1dest)
3092 8329158 : || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3093 : else
3094 : added_sets_1 = false;
3095 :
3096 33871519 : if (i0)
3097 2874641 : added_sets_0 = !(dead_or_set_p (i3, i0dest)
3098 1774082 : || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3099 358130 : || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3100 855898 : && dead_or_set_p (i2, i0dest)));
3101 : else
3102 : added_sets_0 = false;
3103 :
3104 : /* We are about to copy insns for the case where they need to be kept
3105 : around. Check that they can be copied in the merged instruction. */
3106 :
3107 33871519 : if (targetm.cannot_copy_insn_p
3108 33871519 : && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3109 0 : || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3110 0 : || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3111 : {
3112 0 : undo_all ();
3113 0 : return 0;
3114 : }
3115 :
3116 : /* We cannot safely duplicate volatile references in any case. */
3117 :
3118 7563697 : if ((added_sets_2 && volatile_refs_p (PATTERN (i2)))
3119 33835210 : || (added_sets_1 && volatile_refs_p (PATTERN (i1)))
3120 67678400 : || (added_sets_0 && volatile_refs_p (PATTERN (i0))))
3121 : {
3122 66915 : undo_all ();
3123 66915 : return 0;
3124 : }
3125 :
3126 : /* Count how many auto_inc expressions there were in the original insns;
3127 : we need to have the same number in the resulting patterns. */
3128 :
3129 33804604 : if (i0)
3130 1945738 : for_each_inc_dec (PATTERN (i0), count_auto_inc, &n_auto_inc);
3131 33804604 : if (i1)
3132 10866669 : for_each_inc_dec (PATTERN (i1), count_auto_inc, &n_auto_inc);
3133 33804604 : for_each_inc_dec (PATTERN (i2), count_auto_inc, &n_auto_inc);
3134 33804604 : for_each_inc_dec (PATTERN (i3), count_auto_inc, &n_auto_inc);
3135 :
3136 : /* If the set in I2 needs to be kept around, we must make a copy of
3137 : PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3138 : PATTERN (I2), we are only substituting for the original I1DEST, not into
3139 : an already-substituted copy. This also prevents making self-referential
3140 : rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3141 : I2DEST. */
3142 :
3143 33804604 : if (added_sets_2)
3144 : {
3145 7524453 : if (GET_CODE (PATTERN (i2)) == PARALLEL)
3146 2376811 : i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3147 : else
3148 5147642 : i2pat = copy_rtx (PATTERN (i2));
3149 : }
3150 :
3151 33804604 : if (added_sets_1)
3152 : {
3153 4055254 : if (GET_CODE (PATTERN (i1)) == PARALLEL)
3154 1312572 : i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3155 : else
3156 2742682 : i1pat = copy_rtx (PATTERN (i1));
3157 : }
3158 :
3159 33804604 : if (added_sets_0)
3160 : {
3161 534917 : if (GET_CODE (PATTERN (i0)) == PARALLEL)
3162 207229 : i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3163 : else
3164 327688 : i0pat = copy_rtx (PATTERN (i0));
3165 : }
3166 :
3167 33804604 : combine_merges++;
3168 :
3169 : /* Substitute in the latest insn for the regs set by the earlier ones. */
3170 :
3171 33804604 : maxreg = max_reg_num ();
3172 :
3173 33804604 : subst_insn = i3;
3174 :
3175 : /* Many machines have insns that can both perform an
3176 : arithmetic operation and set the condition code. These operations will
3177 : be represented as a PARALLEL with the first element of the vector
3178 : being a COMPARE of an arithmetic operation with the constant zero.
3179 : The second element of the vector will set some pseudo to the result
3180 : of the same arithmetic operation. If we simplify the COMPARE, we won't
3181 : match such a pattern and so will generate an extra insn. Here we test
3182 : for this case, where both the comparison and the operation result are
3183 : needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3184 : I2SRC. Later we will make the PARALLEL that contains I2. */
3185 :
3186 22937935 : if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3187 4296291 : && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3188 1807361 : && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3189 34705588 : && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3190 : {
3191 830533 : rtx newpat_dest;
3192 830533 : rtx *cc_use_loc = NULL;
3193 830533 : rtx_insn *cc_use_insn = NULL;
3194 830533 : rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3195 830533 : machine_mode compare_mode, orig_compare_mode;
3196 830533 : enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3197 830533 : scalar_int_mode mode;
3198 :
3199 830533 : newpat = PATTERN (i3);
3200 830533 : newpat_dest = SET_DEST (newpat);
3201 830533 : compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3202 :
3203 830533 : if (undobuf.other_insn == 0
3204 830533 : && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3205 : &cc_use_insn)))
3206 : {
3207 823835 : compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3208 823835 : if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3209 823835 : compare_code = simplify_compare_const (compare_code, mode,
3210 : &op0, &op1);
3211 823835 : target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3212 : }
3213 :
3214 : /* Do the rest only if op1 is const0_rtx, which may be the
3215 : result of simplification. */
3216 830533 : if (op1 == const0_rtx)
3217 : {
3218 : /* If a single use of the CC is found, prepare to modify it
3219 : when SELECT_CC_MODE returns a new CC-class mode, or when
3220 : the above simplify_compare_const() returned a new comparison
3221 : operator. undobuf.other_insn is assigned the CC use insn
3222 : when modifying it. */
3223 522178 : if (cc_use_loc)
3224 : {
3225 : #ifdef SELECT_CC_MODE
3226 519079 : machine_mode new_mode
3227 519079 : = SELECT_CC_MODE (compare_code, op0, op1);
3228 519079 : if (new_mode != orig_compare_mode
3229 519079 : && can_change_dest_mode (SET_DEST (newpat),
3230 : added_sets_2, new_mode))
3231 : {
3232 561 : unsigned int regno = REGNO (newpat_dest);
3233 561 : compare_mode = new_mode;
3234 561 : if (regno < FIRST_PSEUDO_REGISTER)
3235 561 : newpat_dest = gen_rtx_REG (compare_mode, regno);
3236 : else
3237 : {
3238 0 : subst_mode (regno, compare_mode);
3239 0 : newpat_dest = regno_reg_rtx[regno];
3240 : }
3241 : }
3242 : #endif
3243 : /* Cases for modifying the CC-using comparison. */
3244 519079 : if (compare_code != orig_compare_code
3245 585 : && COMPARISON_P (*cc_use_loc))
3246 : {
3247 : /* Replace cc_use_loc with entire new RTX. */
3248 585 : SUBST (*cc_use_loc,
3249 : gen_rtx_fmt_ee (compare_code, GET_MODE (*cc_use_loc),
3250 : newpat_dest, const0_rtx));
3251 585 : undobuf.other_insn = cc_use_insn;
3252 : }
3253 518494 : else if (compare_mode != orig_compare_mode)
3254 : {
3255 1 : subrtx_ptr_iterator::array_type array;
3256 :
3257 : /* Just replace the CC reg with a new mode. */
3258 4 : FOR_EACH_SUBRTX_PTR (iter, array, cc_use_loc, NONCONST)
3259 : {
3260 3 : rtx *loc = *iter;
3261 3 : if (REG_P (*loc)
3262 3 : && REGNO (*loc) == REGNO (newpat_dest))
3263 : {
3264 1 : SUBST (*loc, newpat_dest);
3265 1 : iter.skip_subrtxes ();
3266 : }
3267 : }
3268 1 : undobuf.other_insn = cc_use_insn;
3269 1 : }
3270 : }
3271 :
3272 : /* Now we modify the current newpat:
3273 : First, SET_DEST(newpat) is updated if the CC mode has been
3274 : altered. For targets without SELECT_CC_MODE, this should be
3275 : optimized away. */
3276 522178 : if (compare_mode != orig_compare_mode)
3277 561 : SUBST (SET_DEST (newpat), newpat_dest);
3278 : /* This is always done to propagate i2src into newpat. */
3279 522178 : SUBST (SET_SRC (newpat),
3280 : gen_rtx_COMPARE (compare_mode, op0, op1));
3281 : /* Create new version of i2pat if needed; the below PARALLEL
3282 : creation needs this to work correctly. */
3283 522178 : if (! rtx_equal_p (i2src, op0))
3284 27 : i2pat = gen_rtx_SET (i2dest, op0);
3285 : i2_is_used = 1;
3286 : }
3287 : }
3288 :
3289 830533 : if (i2_is_used == 0)
3290 : {
3291 : /* It is possible that the source of I2 or I1 may be performing
3292 : an unneeded operation, such as a ZERO_EXTEND of something
3293 : that is known to have the high part zero. Handle that case
3294 : by letting subst look at the inner insns.
3295 :
3296 : Another way to do this would be to have a function that tries
3297 : to simplify a single insn instead of merging two or more
3298 : insns. We don't do this because of the potential of infinite
3299 : loops and because of the potential extra memory required.
3300 : However, doing it the way we are is a bit of a kludge and
3301 : doesn't catch all cases.
3302 :
3303 : But only do this if -fexpensive-optimizations since it slows
3304 : things down and doesn't usually win.
3305 :
3306 : This is not done in the COMPARE case above because the
3307 : unmodified I2PAT is used in the PARALLEL and so a pattern
3308 : with a modified I2SRC would not match. */
3309 :
3310 33282426 : if (flag_expensive_optimizations)
3311 : {
3312 : /* Pass pc_rtx so no substitutions are done, just
3313 : simplifications. */
3314 30991092 : if (i1)
3315 : {
3316 10188128 : subst_low_luid = DF_INSN_LUID (i1);
3317 10188128 : i1src = subst (i1src, pc_rtx, pc_rtx, false, false, false);
3318 : }
3319 :
3320 30991092 : subst_low_luid = DF_INSN_LUID (i2);
3321 30991092 : i2src = subst (i2src, pc_rtx, pc_rtx, false, false, false);
3322 : }
3323 :
3324 33282426 : n_occurrences = 0; /* `subst' counts here */
3325 33282426 : subst_low_luid = DF_INSN_LUID (i2);
3326 :
3327 : /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3328 : copy of I2SRC each time we substitute it, in order to avoid creating
3329 : self-referential RTL when we will be substituting I1SRC for I1DEST
3330 : later. Likewise if I0 feeds into I2, either directly or indirectly
3331 : through I1, and I0DEST is in I0SRC. */
3332 65927488 : newpat = subst (PATTERN (i3), i2dest, i2src, false, false,
3333 33282426 : (i1_feeds_i2_n && i1dest_in_i1src)
3334 32032637 : || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3335 : && i0dest_in_i0src));
3336 33282426 : substed_i2 = true;
3337 :
3338 : /* Record whether I2's body now appears within I3's body. */
3339 33282426 : i2_is_used = n_occurrences;
3340 : }
3341 :
3342 : /* If we already got a failure, don't try to do more. Otherwise, try to
3343 : substitute I1 if we have it. */
3344 :
3345 33804604 : if (i1 && GET_CODE (newpat) != CLOBBER)
3346 : {
3347 : /* Before we can do this substitution, we must redo the test done
3348 : above (see detailed comments there) that ensures I1DEST isn't
3349 : mentioned in any SETs in NEWPAT that are field assignments. */
3350 10822225 : if (!combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3351 : false, false, 0))
3352 : {
3353 82 : undo_all ();
3354 82 : return 0;
3355 : }
3356 :
3357 10822143 : n_occurrences = 0;
3358 10822143 : subst_low_luid = DF_INSN_LUID (i1);
3359 :
3360 : /* If the following substitution will modify I1SRC, make a copy of it
3361 : for the case where it is substituted for I1DEST in I2PAT later. */
3362 10822143 : if (added_sets_2 && i1_feeds_i2_n)
3363 1526207 : i1src_copy = copy_rtx (i1src);
3364 :
3365 : /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3366 : copy of I1SRC each time we substitute it, in order to avoid creating
3367 : self-referential RTL when we will be substituting I0SRC for I0DEST
3368 : later. */
3369 10822143 : newpat = subst (newpat, i1dest, i1src, false, false,
3370 : i0_feeds_i1_n && i0dest_in_i0src);
3371 10822143 : substed_i1 = true;
3372 :
3373 : /* Record whether I1's body now appears within I3's body. */
3374 10822143 : i1_is_used = n_occurrences;
3375 : }
3376 :
3377 : /* Likewise for I0 if we have it. */
3378 :
3379 33804522 : if (i0 && GET_CODE (newpat) != CLOBBER)
3380 : {
3381 1926526 : if (!combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3382 : false, false, 0))
3383 : {
3384 2 : undo_all ();
3385 2 : return 0;
3386 : }
3387 :
3388 : /* If the following substitution will modify I0SRC, make a copy of it
3389 : for the case where it is substituted for I0DEST in I1PAT later. */
3390 1926524 : if (added_sets_1 && i0_feeds_i1_n)
3391 370496 : i0src_copy = copy_rtx (i0src);
3392 : /* And a copy for I0DEST in I2PAT substitution. */
3393 1926524 : if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3394 217197 : || (i0_feeds_i2_n)))
3395 355256 : i0src_copy2 = copy_rtx (i0src);
3396 :
3397 1926524 : n_occurrences = 0;
3398 1926524 : subst_low_luid = DF_INSN_LUID (i0);
3399 1926524 : newpat = subst (newpat, i0dest, i0src, false, false, false);
3400 1926524 : substed_i0 = true;
3401 : }
3402 :
3403 33804520 : if (n_auto_inc)
3404 : {
3405 512290 : int new_n_auto_inc = 0;
3406 512290 : for_each_inc_dec (newpat, count_auto_inc, &new_n_auto_inc);
3407 :
3408 512290 : if (n_auto_inc != new_n_auto_inc)
3409 : {
3410 1021 : if (dump_file && (dump_flags & TDF_DETAILS))
3411 0 : fprintf (dump_file, "Number of auto_inc expressions changed\n");
3412 1021 : undo_all ();
3413 1021 : return 0;
3414 : }
3415 : }
3416 :
3417 : /* Fail if an autoincrement side-effect has been duplicated. Be careful
3418 : to count all the ways that I2SRC and I1SRC can be used. */
3419 33803499 : if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3420 : && i2_is_used + added_sets_2 > 1)
3421 : || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3422 : && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n) > 1))
3423 : || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3424 : && (n_occurrences + added_sets_0
3425 : + (added_sets_1 && i0_feeds_i1_n)
3426 : + (added_sets_2 && i0_feeds_i2_n) > 1))
3427 : /* Fail if we tried to make a new register. */
3428 33803499 : || max_reg_num () != maxreg
3429 : /* Fail if we couldn't do something and have a CLOBBER. */
3430 33803499 : || GET_CODE (newpat) == CLOBBER
3431 : /* Fail if this new pattern is a MULT and we didn't have one before
3432 : at the outer level. */
3433 67246490 : || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3434 316622 : && ! have_mult))
3435 : {
3436 392604 : undo_all ();
3437 392604 : return 0;
3438 : }
3439 :
3440 : /* If the actions of the earlier insns must be kept
3441 : in addition to substituting them into the latest one,
3442 : we must make a new PARALLEL for the latest insn
3443 : to hold additional the SETs. */
3444 :
3445 33410895 : if (added_sets_0 || added_sets_1 || added_sets_2)
3446 : {
3447 11042689 : int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3448 11042689 : combine_extras++;
3449 :
3450 11042689 : if (GET_CODE (newpat) == PARALLEL)
3451 : {
3452 2309259 : rtvec old = XVEC (newpat, 0);
3453 2309259 : total_sets = XVECLEN (newpat, 0) + extra_sets;
3454 2309259 : newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3455 2309259 : memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3456 2309259 : sizeof (old->elem[0]) * old->num_elem);
3457 : }
3458 : else
3459 : {
3460 8733430 : rtx old = newpat;
3461 8733430 : total_sets = 1 + extra_sets;
3462 8733430 : newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3463 8733430 : XVECEXP (newpat, 0, 0) = old;
3464 : }
3465 :
3466 11042689 : if (added_sets_0)
3467 515507 : XVECEXP (newpat, 0, --total_sets) = i0pat;
3468 :
3469 11042689 : if (added_sets_1)
3470 : {
3471 4003856 : rtx t = i1pat;
3472 4003856 : if (i0_feeds_i1_n)
3473 366983 : t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src,
3474 : false, false, false);
3475 :
3476 4003856 : XVECEXP (newpat, 0, --total_sets) = t;
3477 : }
3478 11042689 : if (added_sets_2)
3479 : {
3480 7442747 : rtx t = i2pat;
3481 7442747 : if (i1_feeds_i2_n)
3482 1507164 : t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, false, false,
3483 : i0_feeds_i1_n && i0dest_in_i0src);
3484 7442747 : if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3485 350701 : t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src,
3486 : false, false, false);
3487 :
3488 7442747 : XVECEXP (newpat, 0, --total_sets) = t;
3489 : }
3490 : }
3491 :
3492 25968148 : validate_replacement:
3493 :
3494 : /* Note which hard regs this insn has as inputs. */
3495 33771512 : mark_used_regs_combine (newpat);
3496 :
3497 : /* If recog_for_combine fails, it strips existing clobbers. If we'll
3498 : consider splitting this pattern, we might need these clobbers. */
3499 33771512 : if (i1 && GET_CODE (newpat) == PARALLEL
3500 7478083 : && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3501 : {
3502 1778510 : int len = XVECLEN (newpat, 0);
3503 :
3504 1778510 : newpat_vec_with_clobbers = rtvec_alloc (len);
3505 7164809 : for (i = 0; i < len; i++)
3506 3607789 : RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3507 : }
3508 :
3509 : /* We have recognized nothing yet. */
3510 33771512 : insn_code_number = -1;
3511 :
3512 : /* See if this is a PARALLEL of two SETs where one SET's destination is
3513 : a register that is unused and this isn't marked as an instruction that
3514 : might trap in an EH region. In that case, we just need the other SET.
3515 : We prefer this over the PARALLEL.
3516 :
3517 : This can occur when simplifying a divmod insn. We *must* test for this
3518 : case here because the code below that splits two independent SETs doesn't
3519 : handle this case correctly when it updates the register status.
3520 :
3521 : It's pointless doing this if we originally had two sets, one from
3522 : i3, and one from i2. Combining then splitting the parallel results
3523 : in the original i2 again plus an invalid insn (which we delete).
3524 : The net effect is only to move instructions around, which makes
3525 : debug info less accurate.
3526 :
3527 : If the remaining SET came from I2 its destination should not be used
3528 : between I2 and I3. See PR82024. */
3529 :
3530 7442747 : if (!(added_sets_2 && i1 == 0)
3531 28359535 : && is_parallel_of_n_reg_sets (newpat, 2)
3532 35405996 : && asm_noperands (newpat) < 0)
3533 : {
3534 1633588 : rtx set0 = XVECEXP (newpat, 0, 0);
3535 1633588 : rtx set1 = XVECEXP (newpat, 0, 1);
3536 1633588 : rtx oldpat = newpat;
3537 :
3538 1633588 : if (((REG_P (SET_DEST (set1))
3539 1633588 : && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3540 1592046 : || (GET_CODE (SET_DEST (set1)) == SUBREG
3541 0 : && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3542 41542 : && insn_nothrow_p (i3)
3543 1673928 : && !side_effects_p (SET_SRC (set1)))
3544 : {
3545 40101 : newpat = set0;
3546 40101 : insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3547 : }
3548 :
3549 1593487 : else if (((REG_P (SET_DEST (set0))
3550 1593487 : && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3551 1566167 : || (GET_CODE (SET_DEST (set0)) == SUBREG
3552 0 : && find_reg_note (i3, REG_UNUSED,
3553 0 : SUBREG_REG (SET_DEST (set0)))))
3554 27320 : && insn_nothrow_p (i3)
3555 1620266 : && !side_effects_p (SET_SRC (set0)))
3556 : {
3557 26734 : rtx dest = SET_DEST (set1);
3558 26734 : if (GET_CODE (dest) == SUBREG)
3559 0 : dest = SUBREG_REG (dest);
3560 26734 : if (!reg_used_between_p (dest, i2, i3))
3561 : {
3562 26733 : newpat = set1;
3563 26733 : insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3564 :
3565 26733 : if (insn_code_number >= 0)
3566 : changed_i3_dest = true;
3567 : }
3568 : }
3569 :
3570 40101 : if (insn_code_number < 0)
3571 1627980 : newpat = oldpat;
3572 : }
3573 :
3574 : /* Is the result of combination a valid instruction? */
3575 1627980 : if (insn_code_number < 0)
3576 33765904 : insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3577 :
3578 : /* If we were combining three insns and the result is a simple SET
3579 : with no ASM_OPERANDS that wasn't recognized, try to split it into two
3580 : insns. There are two ways to do this. It can be split using a
3581 : machine-specific method (like when you have an addition of a large
3582 : constant) or by combine in the function find_split_point. */
3583 :
3584 10661760 : if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3585 38645172 : && asm_noperands (newpat) < 0)
3586 : {
3587 4873101 : rtx parallel, *split;
3588 4873101 : rtx_insn *m_split_insn;
3589 4873101 : unsigned int old_nregs, new_nregs;
3590 :
3591 : /* See if the MD file can split NEWPAT. If it can't, see if letting it
3592 : use I2DEST as a scratch register will help. In the latter case,
3593 : convert I2DEST to the mode of the source of NEWPAT if we can. */
3594 :
3595 4873101 : m_split_insn = combine_split_insns (newpat, i3, &old_nregs, &new_nregs);
3596 :
3597 : /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3598 : inputs of NEWPAT. */
3599 :
3600 : /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3601 : possible to try that as a scratch reg. This would require adding
3602 : more code to make it work though. */
3603 :
3604 4873101 : if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3605 : {
3606 4728152 : machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3607 :
3608 : /* ??? Reusing i2dest without resetting the reg_stat entry for it
3609 : (temporarily, until we are committed to this instruction
3610 : combination) does not work: for example, any call to nonzero_bits
3611 : on the register (from a splitter in the MD file, for example)
3612 : will get the old information, which is invalid.
3613 :
3614 : Since nowadays we can create registers during combine just fine,
3615 : we should just create a new one here, not reuse i2dest. */
3616 :
3617 : /* First try to split using the original register as a
3618 : scratch register. */
3619 4728152 : parallel = gen_rtx_PARALLEL (VOIDmode,
3620 : gen_rtvec (2, newpat,
3621 : gen_rtx_CLOBBER (VOIDmode,
3622 : i2dest)));
3623 4728152 : m_split_insn = combine_split_insns (parallel, i3, &old_nregs, &new_nregs);
3624 :
3625 : /* If that didn't work, try changing the mode of I2DEST if
3626 : we can. */
3627 4728152 : if (m_split_insn == 0
3628 4728152 : && new_mode != GET_MODE (i2dest)
3629 1802267 : && new_mode != VOIDmode
3630 5944269 : && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3631 : {
3632 895229 : machine_mode old_mode = GET_MODE (i2dest);
3633 895229 : rtx ni2dest;
3634 :
3635 895229 : if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3636 9135 : ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3637 : else
3638 : {
3639 886094 : subst_mode (REGNO (i2dest), new_mode);
3640 886094 : ni2dest = regno_reg_rtx[REGNO (i2dest)];
3641 : }
3642 :
3643 895229 : parallel = (gen_rtx_PARALLEL
3644 : (VOIDmode,
3645 : gen_rtvec (2, newpat,
3646 : gen_rtx_CLOBBER (VOIDmode,
3647 : ni2dest))));
3648 895229 : m_split_insn = combine_split_insns (parallel, i3, &old_nregs, &new_nregs);
3649 :
3650 895229 : if (m_split_insn == 0
3651 895229 : && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3652 : {
3653 886094 : struct undo *buf;
3654 :
3655 886094 : adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3656 886094 : buf = undobuf.undos;
3657 886094 : undobuf.undos = buf->next;
3658 886094 : buf->next = undobuf.frees;
3659 886094 : undobuf.frees = buf;
3660 : }
3661 : }
3662 :
3663 4728152 : i2scratch = m_split_insn != 0;
3664 : }
3665 :
3666 : /* If recog_for_combine has discarded clobbers, try to use them
3667 : again for the split. */
3668 4873101 : if (m_split_insn == 0 && newpat_vec_with_clobbers)
3669 : {
3670 1726659 : parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3671 1726659 : m_split_insn = combine_split_insns (parallel, i3, &old_nregs, &new_nregs);
3672 : }
3673 :
3674 4884913 : if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3675 : {
3676 1667 : rtx m_split_pat = PATTERN (m_split_insn);
3677 1667 : insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes,
3678 : old_nregs, new_nregs);
3679 1667 : if (insn_code_number >= 0)
3680 259 : newpat = m_split_pat;
3681 : }
3682 10145 : else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3683 4881579 : && (next_nonnote_nondebug_insn (i2) == i3
3684 6 : || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3685 : {
3686 10145 : rtx i2set, i3set;
3687 10145 : rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3688 10145 : newi2pat = PATTERN (m_split_insn);
3689 :
3690 10145 : i3set = single_set (NEXT_INSN (m_split_insn));
3691 10145 : i2set = single_set (m_split_insn);
3692 :
3693 10145 : i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3694 :
3695 : /* If I2 or I3 has multiple SETs, we won't know how to track
3696 : register status, so don't use these insns. If I2's destination
3697 : is used between I2 and I3, we also can't use these insns. */
3698 :
3699 10145 : if (i2_code_number >= 0 && i2set && i3set
3700 20290 : && (next_nonnote_nondebug_insn (i2) == i3
3701 6 : || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3702 10145 : insn_code_number = recog_for_combine (&newi3pat, i3,
3703 : &new_i3_notes,
3704 : old_nregs, new_nregs);
3705 10145 : if (insn_code_number >= 0)
3706 10145 : newpat = newi3pat;
3707 :
3708 : /* It is possible that both insns now set the destination of I3.
3709 : If so, we must show an extra use of it. */
3710 :
3711 10145 : if (insn_code_number >= 0)
3712 : {
3713 10145 : rtx new_i3_dest = SET_DEST (i3set);
3714 10145 : rtx new_i2_dest = SET_DEST (i2set);
3715 :
3716 10145 : while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3717 10185 : || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3718 20352 : || GET_CODE (new_i3_dest) == SUBREG)
3719 40 : new_i3_dest = XEXP (new_i3_dest, 0);
3720 :
3721 10145 : while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3722 10145 : || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3723 20290 : || GET_CODE (new_i2_dest) == SUBREG)
3724 0 : new_i2_dest = XEXP (new_i2_dest, 0);
3725 :
3726 10145 : if (REG_P (new_i3_dest)
3727 5804 : && REG_P (new_i2_dest)
3728 5804 : && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3729 10145 : && REGNO (new_i2_dest) < reg_n_sets_max)
3730 0 : INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3731 : }
3732 : }
3733 :
3734 : /* If we can split it and use I2DEST, go ahead and see if that
3735 : helps things be recognized. Verify that none of the registers
3736 : are set between I2 and I3. */
3737 1408 : if (insn_code_number < 0
3738 4862697 : && (split = find_split_point (&newpat, i3, false)) != 0
3739 : /* We need I2DEST in the proper mode. If it is a hard register
3740 : or the only use of a pseudo, we can change its mode.
3741 : Make sure we don't change a hard register to have a mode that
3742 : isn't valid for it, or change the number of registers. */
3743 4591095 : && (GET_MODE (*split) == GET_MODE (i2dest)
3744 1699830 : || GET_MODE (*split) == VOIDmode
3745 1320739 : || can_change_dest_mode (i2dest, added_sets_2,
3746 : GET_MODE (*split)))
3747 3844453 : && (next_nonnote_nondebug_insn (i2) == i3
3748 594390 : || !modified_between_p (*split, i2, i3))
3749 : /* We can't overwrite I2DEST if its value is still used by
3750 : NEWPAT. */
3751 3815264 : && ! reg_referenced_p (i2dest, newpat)
3752 : /* We should not split a possibly trapping part when we
3753 : care about non-call EH and have REG_EH_REGION notes
3754 : to distribute. */
3755 8609063 : && ! (cfun->can_throw_non_call_exceptions
3756 391522 : && has_non_call_exception
3757 121 : && may_trap_p (*split)))
3758 : {
3759 3737249 : rtx newdest = i2dest;
3760 3737249 : enum rtx_code split_code = GET_CODE (*split);
3761 3737249 : machine_mode split_mode = GET_MODE (*split);
3762 3737249 : bool subst_done = false;
3763 3737249 : newi2pat = NULL_RTX;
3764 :
3765 3737249 : i2scratch = true;
3766 :
3767 : /* *SPLIT may be part of I2SRC, so make sure we have the
3768 : original expression around for later debug processing.
3769 : We should not need I2SRC any more in other cases. */
3770 3737249 : if (MAY_HAVE_DEBUG_BIND_INSNS)
3771 1829066 : i2src = copy_rtx (i2src);
3772 : else
3773 : i2src = NULL;
3774 :
3775 : /* Get NEWDEST as a register in the proper mode. We have already
3776 : validated that we can do this. */
3777 3737249 : if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3778 : {
3779 570593 : if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3780 4 : newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3781 : else
3782 : {
3783 570589 : subst_mode (REGNO (i2dest), split_mode);
3784 570589 : newdest = regno_reg_rtx[REGNO (i2dest)];
3785 : }
3786 : }
3787 :
3788 : /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3789 : an ASHIFT. This can occur if it was inside a PLUS and hence
3790 : appeared to be a memory address. This is a kludge. */
3791 3737249 : if (split_code == MULT
3792 208846 : && CONST_INT_P (XEXP (*split, 1))
3793 103197 : && INTVAL (XEXP (*split, 1)) > 0
3794 3835960 : && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3795 : {
3796 68820 : rtx i_rtx = gen_int_shift_amount (split_mode, i);
3797 68820 : SUBST (*split, gen_rtx_ASHIFT (split_mode,
3798 : XEXP (*split, 0), i_rtx));
3799 : /* Update split_code because we may not have a multiply
3800 : anymore. */
3801 68820 : split_code = GET_CODE (*split);
3802 : }
3803 :
3804 : /* Similarly for (plus (mult FOO (const_int pow2))). */
3805 3737249 : if (split_code == PLUS
3806 729813 : && GET_CODE (XEXP (*split, 0)) == MULT
3807 115258 : && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3808 39737 : && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3809 3773508 : && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3810 : {
3811 6857 : rtx nsplit = XEXP (*split, 0);
3812 6857 : rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3813 6857 : SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3814 : XEXP (nsplit, 0),
3815 : i_rtx));
3816 : /* Update split_code because we may not have a multiply
3817 : anymore. */
3818 6857 : split_code = GET_CODE (*split);
3819 : }
3820 :
3821 : #ifdef INSN_SCHEDULING
3822 : /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3823 : be written as a ZERO_EXTEND. */
3824 3737249 : if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3825 : {
3826 : /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3827 : what it really is. */
3828 10267 : if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3829 : == SIGN_EXTEND)
3830 : SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3831 : SUBREG_REG (*split)));
3832 : else
3833 10267 : SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3834 : SUBREG_REG (*split)));
3835 : }
3836 : #endif
3837 :
3838 : /* Attempt to split binary operators using arithmetic identities. */
3839 3737249 : if (BINARY_P (SET_SRC (newpat))
3840 3135218 : && split_mode == GET_MODE (SET_SRC (newpat))
3841 5905175 : && ! side_effects_p (SET_SRC (newpat)))
3842 : {
3843 2152877 : rtx setsrc = SET_SRC (newpat);
3844 2152877 : machine_mode mode = GET_MODE (setsrc);
3845 2152877 : enum rtx_code code = GET_CODE (setsrc);
3846 2152877 : rtx src_op0 = XEXP (setsrc, 0);
3847 2152877 : rtx src_op1 = XEXP (setsrc, 1);
3848 :
3849 : /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3850 2152877 : if (rtx_equal_p (src_op0, src_op1))
3851 : {
3852 1625 : newi2pat = gen_rtx_SET (newdest, src_op0);
3853 1625 : SUBST (XEXP (setsrc, 0), newdest);
3854 1625 : SUBST (XEXP (setsrc, 1), newdest);
3855 1625 : subst_done = true;
3856 : }
3857 : /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3858 2151252 : else if ((code == PLUS || code == MULT)
3859 1129322 : && GET_CODE (src_op0) == code
3860 427868 : && GET_CODE (XEXP (src_op0, 0)) == code
3861 183817 : && (INTEGRAL_MODE_P (mode)
3862 : || (FLOAT_MODE_P (mode)
3863 100034 : && flag_unsafe_math_optimizations)))
3864 : {
3865 87692 : rtx p = XEXP (XEXP (src_op0, 0), 0);
3866 87692 : rtx q = XEXP (XEXP (src_op0, 0), 1);
3867 87692 : rtx r = XEXP (src_op0, 1);
3868 87692 : rtx s = src_op1;
3869 :
3870 : /* Split both "((X op Y) op X) op Y" and
3871 : "((X op Y) op Y) op X" as "T op T" where T is
3872 : "X op Y". */
3873 87938 : if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3874 87859 : || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3875 : {
3876 79 : newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3877 79 : SUBST (XEXP (setsrc, 0), newdest);
3878 79 : SUBST (XEXP (setsrc, 1), newdest);
3879 79 : subst_done = true;
3880 : }
3881 : /* Split "((X op X) op Y) op Y)" as "T op T" where
3882 : T is "X op Y". */
3883 87613 : else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3884 : {
3885 41 : rtx tmp = simplify_gen_binary (code, mode, p, r);
3886 41 : newi2pat = gen_rtx_SET (newdest, tmp);
3887 41 : SUBST (XEXP (setsrc, 0), newdest);
3888 41 : SUBST (XEXP (setsrc, 1), newdest);
3889 41 : subst_done = true;
3890 : }
3891 : }
3892 : }
3893 :
3894 1745 : if (!subst_done)
3895 : {
3896 3735504 : newi2pat = gen_rtx_SET (newdest, *split);
3897 3735504 : SUBST (*split, newdest);
3898 : }
3899 :
3900 3737249 : i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3901 :
3902 : /* recog_for_combine might have added CLOBBERs to newi2pat.
3903 : Make sure NEWPAT does not depend on the clobbered regs. */
3904 3737249 : if (GET_CODE (newi2pat) == PARALLEL)
3905 2564504 : for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3906 1722601 : if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3907 : {
3908 880698 : rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3909 880698 : if (reg_overlap_mentioned_p (reg, newpat))
3910 : {
3911 24501 : undo_all ();
3912 24501 : return 0;
3913 : }
3914 : }
3915 :
3916 : /* If the split point was a MULT and we didn't have one before,
3917 : don't use one now. */
3918 3712748 : if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3919 2214045 : insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3920 : }
3921 : }
3922 :
3923 : /* Check for a case where we loaded from memory in a narrow mode and
3924 : then sign extended it, but we need both registers. In that case,
3925 : we have a PARALLEL with both loads from the same memory location.
3926 : We can split this into a load from memory followed by a register-register
3927 : copy. This saves at least one insn, more if register allocation can
3928 : eliminate the copy.
3929 :
3930 : We cannot do this if the involved modes have more than one elements,
3931 : like for vector or complex modes.
3932 :
3933 : We cannot do this if the destination of the first assignment is a
3934 : condition code register. We eliminate this case by making sure
3935 : the SET_DEST and SET_SRC have the same mode.
3936 :
3937 : We cannot do this if the destination of the second assignment is
3938 : a register that we have already assumed is zero-extended. Similarly
3939 : for a SUBREG of such a register. */
3940 :
3941 5788659 : else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3942 5729609 : && GET_CODE (newpat) == PARALLEL
3943 5727755 : && XVECLEN (newpat, 0) == 2
3944 4757130 : && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3945 4756865 : && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3946 23635 : && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3947 23635 : == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3948 23635 : && ! VECTOR_MODE_P (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0))))
3949 : && ! COMPLEX_MODE_P (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0))))
3950 22201 : && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3951 22201 : && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3952 22201 : XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3953 6607 : && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
3954 6607 : && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3955 6607 : && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3956 6607 : && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3957 : (REG_P (temp_expr)
3958 6607 : && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3959 6697 : && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3960 : BITS_PER_WORD)
3961 6491 : && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3962 : HOST_BITS_PER_INT)
3963 1147 : && (reg_stat[REGNO (temp_expr)].nonzero_bits
3964 1147 : != GET_MODE_MASK (word_mode))))
3965 6589 : && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3966 0 : && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3967 0 : (REG_P (temp_expr)
3968 0 : && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3969 0 : && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3970 : BITS_PER_WORD)
3971 0 : && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3972 : HOST_BITS_PER_INT)
3973 0 : && (reg_stat[REGNO (temp_expr)].nonzero_bits
3974 0 : != GET_MODE_MASK (word_mode)))))
3975 6589 : && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3976 6589 : SET_SRC (XVECEXP (newpat, 0, 1)))
3977 28904958 : && ! find_reg_note (i3, REG_UNUSED,
3978 6547 : SET_DEST (XVECEXP (newpat, 0, 0))))
3979 : {
3980 6547 : rtx ni2dest;
3981 :
3982 6547 : newi2pat = XVECEXP (newpat, 0, 0);
3983 6547 : ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3984 6547 : newpat = XVECEXP (newpat, 0, 1);
3985 6547 : SUBST (SET_SRC (newpat),
3986 : gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3987 6547 : i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3988 :
3989 6547 : if (i2_code_number >= 0)
3990 0 : insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3991 :
3992 6547 : if (insn_code_number >= 0)
3993 : swap_i2i3 = 1;
3994 : }
3995 :
3996 : /* Similarly, check for a case where we have a PARALLEL of two independent
3997 : SETs but we started with three insns. In this case, we can do the sets
3998 : as two separate insns. This case occurs when some SET allows two
3999 : other insns to combine, but the destination of that SET is still live.
4000 :
4001 : Also do this if we started with two insns and (at least) one of the
4002 : resulting sets is a noop; this noop will be deleted later.
4003 :
4004 : Also do this if we started with two insns neither of which was a simple
4005 : move. */
4006 :
4007 24776911 : else if (insn_code_number < 0 && asm_noperands (newpat) < 0
4008 24758449 : && GET_CODE (newpat) == PARALLEL
4009 11254568 : && XVECLEN (newpat, 0) == 2
4010 10176920 : && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
4011 10066704 : && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
4012 10002487 : && (i1
4013 5274931 : || set_noop_p (XVECEXP (newpat, 0, 0))
4014 5274447 : || set_noop_p (XVECEXP (newpat, 0, 1))
4015 5274443 : || (!i2_was_move && !i3_was_move))
4016 6660663 : && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
4017 6659972 : && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
4018 6659829 : && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4019 6659243 : && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4020 6659229 : && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4021 : XVECEXP (newpat, 0, 0))
4022 5549121 : && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
4023 5549121 : XVECEXP (newpat, 0, 1))
4024 34774851 : && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
4025 463644 : && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
4026 : {
4027 5154894 : rtx set0 = XVECEXP (newpat, 0, 0);
4028 5154894 : rtx set1 = XVECEXP (newpat, 0, 1);
4029 :
4030 : /* Normally, it doesn't matter which of the two is done first, but
4031 : one which uses any regs/memory set or used in between i2 and i3
4032 : can't be first. The PARALLEL might also have been pre-existing
4033 : in i3, so we need to make sure that we won't wrongly hoist a SET
4034 : to i2 that would conflict with a death note present in there, or
4035 : would have its dest modified or used between i2 and i3. */
4036 5154894 : if ((set_noop_p (set1)
4037 5154894 : || (!modified_between_p (SET_SRC (set1), i2, i3)
4038 10271790 : && !(REG_P (SET_DEST (set1))
4039 5123704 : && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4040 5172169 : && !(GET_CODE (SET_DEST (set1)) == SUBREG
4041 24382 : && find_reg_note (i2, REG_DEAD,
4042 24382 : SUBREG_REG (SET_DEST (set1))))
4043 5147787 : && !modified_between_p (SET_DEST (set1), i2, i3)
4044 5147787 : && !reg_used_between_p (SET_DEST (set1), i2, i3)))
4045 : /* If I3 is a jump, ensure that set0 is a jump so that
4046 : we do not create invalid RTL. */
4047 10302675 : && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx))
4048 : {
4049 5147781 : newi2pat = set1;
4050 5147781 : newpat = set0;
4051 : }
4052 7113 : else if ((set_noop_p (set0)
4053 7107 : || (!modified_between_p (SET_SRC (set0), i2, i3)
4054 598 : && !(REG_P (SET_DEST (set0))
4055 299 : && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4056 299 : && !(GET_CODE (SET_DEST (set0)) == SUBREG
4057 0 : && find_reg_note (i2, REG_DEAD,
4058 0 : SUBREG_REG (SET_DEST (set0))))
4059 299 : && !modified_between_p (SET_DEST (set0), i2, i3)
4060 298 : && !reg_used_between_p (SET_DEST (set0), i2, i3)))
4061 : /* If I3 is a jump, ensure that set1 is a jump so that
4062 : we do not create invalid RTL. */
4063 7411 : && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx))
4064 : {
4065 304 : newi2pat = set0;
4066 304 : newpat = set1;
4067 : }
4068 : else
4069 : {
4070 6809 : undo_all ();
4071 6809 : return 0;
4072 : }
4073 :
4074 5148085 : i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4075 :
4076 5148085 : if (i2_code_number >= 0)
4077 : {
4078 : /* recog_for_combine might have added CLOBBERs to newi2pat.
4079 : Make sure NEWPAT does not depend on the clobbered regs. */
4080 3776858 : if (GET_CODE (newi2pat) == PARALLEL)
4081 : {
4082 1400316 : for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4083 937628 : if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4084 : {
4085 474940 : rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4086 474940 : if (reg_overlap_mentioned_p (reg, newpat))
4087 : {
4088 4362 : undo_all ();
4089 4362 : return 0;
4090 : }
4091 : }
4092 : }
4093 :
4094 3772496 : insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4095 :
4096 : /* Likewise, recog_for_combine might have added clobbers to NEWPAT.
4097 : Checking that the SET0's SET_DEST and SET1's SET_DEST aren't
4098 : mentioned/clobbered, ensures NEWI2PAT's SET_DEST is live. */
4099 3772496 : if (insn_code_number >= 0 && GET_CODE (newpat) == PARALLEL)
4100 : {
4101 98864 : for (i = XVECLEN (newpat, 0) - 1; i >= 0; i--)
4102 65916 : if (GET_CODE (XVECEXP (newpat, 0, i)) == CLOBBER)
4103 : {
4104 32968 : rtx reg = XEXP (XVECEXP (newpat, 0, i), 0);
4105 32968 : if (reg_overlap_mentioned_p (reg, SET_DEST (set0))
4106 32968 : || reg_overlap_mentioned_p (reg, SET_DEST (set1)))
4107 : {
4108 0 : undo_all ();
4109 0 : return 0;
4110 : }
4111 : }
4112 : }
4113 :
4114 : if (insn_code_number >= 0)
4115 : split_i2i3 = true;
4116 : }
4117 : }
4118 :
4119 : /* If it still isn't recognized, fail and change things back the way they
4120 : were. */
4121 29956797 : if ((insn_code_number < 0
4122 : /* Is the result a reasonable ASM_OPERANDS? */
4123 33560670 : && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4124 : {
4125 29388713 : undo_all ();
4126 29388713 : return 0;
4127 : }
4128 :
4129 : /* If we had to change another insn, make sure it is valid also. */
4130 4347127 : if (undobuf.other_insn)
4131 : {
4132 236683 : CLEAR_HARD_REG_SET (newpat_used_regs);
4133 :
4134 236683 : other_pat = PATTERN (undobuf.other_insn);
4135 236683 : other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4136 : &new_other_notes);
4137 :
4138 236683 : if (other_code_number < 0 && ! check_asm_operands (other_pat))
4139 : {
4140 7255 : undo_all ();
4141 7255 : return 0;
4142 : }
4143 : }
4144 :
4145 : /* Reject this combination if insn_cost reports that the replacement
4146 : instructions are more expensive than the originals. */
4147 4339872 : if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat,
4148 : insn_code_number, i2_code_number,
4149 : other_code_number))
4150 : {
4151 221707 : undo_all ();
4152 221707 : return 0;
4153 : }
4154 :
4155 4118165 : if (MAY_HAVE_DEBUG_BIND_INSNS)
4156 : {
4157 2190329 : struct undo *undo;
4158 :
4159 6582633 : for (undo = undobuf.undos; undo; undo = undo->next)
4160 4392304 : if (undo->kind == UNDO_MODE)
4161 : {
4162 2900 : rtx reg = regno_reg_rtx[undo->where.regno];
4163 2900 : machine_mode new_mode = GET_MODE (reg);
4164 2900 : machine_mode old_mode = undo->old_contents.m;
4165 :
4166 : /* Temporarily revert mode back. */
4167 2900 : adjust_reg_mode (reg, old_mode);
4168 :
4169 2900 : if (reg == i2dest && i2scratch)
4170 : {
4171 : /* If we used i2dest as a scratch register with a
4172 : different mode, substitute it for the original
4173 : i2src while its original mode is temporarily
4174 : restored, and then clear i2scratch so that we don't
4175 : do it again later. */
4176 2900 : propagate_for_debug (i2, last_combined_insn, reg, i2src,
4177 : this_basic_block);
4178 2900 : i2scratch = false;
4179 : /* Put back the new mode. */
4180 2900 : adjust_reg_mode (reg, new_mode);
4181 : }
4182 : else
4183 : {
4184 0 : rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4185 0 : rtx_insn *first, *last;
4186 :
4187 0 : if (reg == i2dest)
4188 : {
4189 : first = i2;
4190 : last = last_combined_insn;
4191 : }
4192 : else
4193 : {
4194 0 : first = i3;
4195 0 : last = undobuf.other_insn;
4196 0 : gcc_assert (last);
4197 0 : if (DF_INSN_LUID (last)
4198 0 : < DF_INSN_LUID (last_combined_insn))
4199 0 : last = last_combined_insn;
4200 : }
4201 :
4202 : /* We're dealing with a reg that changed mode but not
4203 : meaning, so we want to turn it into a subreg for
4204 : the new mode. However, because of REG sharing and
4205 : because its mode had already changed, we have to do
4206 : it in two steps. First, replace any debug uses of
4207 : reg, with its original mode temporarily restored,
4208 : with this copy we have created; then, replace the
4209 : copy with the SUBREG of the original shared reg,
4210 : once again changed to the new mode. */
4211 0 : propagate_for_debug (first, last, reg, tempreg,
4212 : this_basic_block);
4213 0 : adjust_reg_mode (reg, new_mode);
4214 0 : propagate_for_debug (first, last, tempreg,
4215 : lowpart_subreg (old_mode, reg, new_mode),
4216 : this_basic_block);
4217 : }
4218 : }
4219 : }
4220 :
4221 : /* If we will be able to accept this, we have made a
4222 : change to the destination of I3. This requires us to
4223 : do a few adjustments. */
4224 :
4225 4118165 : if (changed_i3_dest)
4226 : {
4227 17377 : PATTERN (i3) = newpat;
4228 17377 : adjust_for_new_dest (i3);
4229 : }
4230 :
4231 4118165 : bool only_i3_changed = !i0 && !i1 && rtx_equal_p (newi2pat, PATTERN (i2));
4232 :
4233 : /* If only i3 has changed, any split of the combined instruction just
4234 : restored i2 to its original state. No destinations moved from i3
4235 : to i2. */
4236 : if (only_i3_changed)
4237 : split_i2i3 = false;
4238 :
4239 : /* We now know that we can do this combination. Merge the insns and
4240 : update the status of registers and LOG_LINKS. */
4241 :
4242 4118165 : if (undobuf.other_insn)
4243 : {
4244 229261 : rtx note, next;
4245 :
4246 229261 : PATTERN (undobuf.other_insn) = other_pat;
4247 :
4248 : /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4249 : ensure that they are still valid. Then add any non-duplicate
4250 : notes added by recog_for_combine. */
4251 685444 : for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4252 : {
4253 456183 : next = XEXP (note, 1);
4254 :
4255 456183 : if ((REG_NOTE_KIND (note) == REG_DEAD
4256 232446 : && !reg_referenced_p (XEXP (note, 0),
4257 232446 : PATTERN (undobuf.other_insn)))
4258 451990 : ||(REG_NOTE_KIND (note) == REG_UNUSED
4259 10 : && !reg_set_p (XEXP (note, 0),
4260 10 : PATTERN (undobuf.other_insn)))
4261 : /* Simply drop equal note since it may be no longer valid
4262 : for other_insn. It may be possible to record that CC
4263 : register is changed and only discard those notes, but
4264 : in practice it's unnecessary complication and doesn't
4265 : give any meaningful improvement.
4266 :
4267 : See PR78559. */
4268 451985 : || REG_NOTE_KIND (note) == REG_EQUAL
4269 908008 : || REG_NOTE_KIND (note) == REG_EQUIV)
4270 4358 : remove_note (undobuf.other_insn, note);
4271 : }
4272 :
4273 229261 : distribute_notes (new_other_notes, undobuf.other_insn,
4274 : undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4275 : NULL_RTX);
4276 : }
4277 :
4278 4118165 : if (swap_i2i3)
4279 : {
4280 : /* I3 now uses what used to be its destination and which is now
4281 : I2's destination. This requires us to do a few adjustments. */
4282 0 : PATTERN (i3) = newpat;
4283 0 : adjust_for_new_dest (i3);
4284 : }
4285 :
4286 4118165 : if (swap_i2i3 || split_i2i3)
4287 : {
4288 : /* We might need a LOG_LINK from I3 to I2. But then we used to
4289 : have one, so we still will.
4290 :
4291 : However, some later insn might be using I2's dest and have
4292 : a LOG_LINK pointing at I3. We should change it to point at
4293 : I2 instead. */
4294 :
4295 : /* newi2pat is usually a SET here; however, recog_for_combine might
4296 : have added some clobbers. */
4297 27872 : rtx x = newi2pat;
4298 27872 : if (GET_CODE (x) == PARALLEL)
4299 544 : x = XVECEXP (newi2pat, 0, 0);
4300 :
4301 27872 : if (REG_P (SET_DEST (x))
4302 8 : || (GET_CODE (SET_DEST (x)) == SUBREG
4303 2 : && REG_P (SUBREG_REG (SET_DEST (x)))))
4304 : {
4305 27866 : unsigned int regno = reg_or_subregno (SET_DEST (x));
4306 :
4307 27866 : bool done = false;
4308 402168 : for (rtx_insn *insn = NEXT_INSN (i3);
4309 402168 : !done
4310 402168 : && insn
4311 400770 : && INSN_P (insn)
4312 776470 : && BLOCK_FOR_INSN (insn) == this_basic_block;
4313 374302 : insn = NEXT_INSN (insn))
4314 : {
4315 374302 : if (DEBUG_INSN_P (insn))
4316 68657 : continue;
4317 305645 : struct insn_link *link;
4318 580551 : FOR_EACH_LOG_LINK (link, insn)
4319 274916 : if (link->insn == i3 && link->regno == regno)
4320 : {
4321 10 : link->insn = i2;
4322 10 : done = true;
4323 10 : break;
4324 : }
4325 : }
4326 : }
4327 : }
4328 :
4329 4118165 : {
4330 4118165 : rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4331 4118165 : struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4332 4118165 : rtx midnotes = 0;
4333 4118165 : int from_luid;
4334 : /* Compute which registers we expect to eliminate. newi2pat may be setting
4335 : either i3dest or i2dest, so we must check it. */
4336 112077 : rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4337 4017712 : || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4338 3930678 : || !i2dest_killed
4339 8047763 : ? 0 : i2dest);
4340 : /* For i1, we need to compute both local elimination and global
4341 : elimination information with respect to newi2pat because i1dest
4342 : may be the same as i3dest, in which case newi2pat may be setting
4343 : i1dest. Global information is used when distributing REG_DEAD
4344 : note for i2 and i3, in which case it does matter if newi2pat sets
4345 : i1dest or not.
4346 :
4347 : Local information is used when distributing REG_DEAD note for i1,
4348 : in which case it doesn't matter if newi2pat sets i1dest or not.
4349 : See PR62151, if we have four insns combination:
4350 : i0: r0 <- i0src
4351 : i1: r1 <- i1src (using r0)
4352 : REG_DEAD (r0)
4353 : i2: r0 <- i2src (using r1)
4354 : i3: r3 <- i3src (using r0)
4355 : ix: using r0
4356 : From i1's point of view, r0 is eliminated, no matter if it is set
4357 : by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4358 : should be discarded.
4359 :
4360 : Note local information only affects cases in forms like "I1->I2->I3",
4361 : "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4362 : "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4363 : i0dest anyway. */
4364 103531 : rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4365 103464 : || !i1dest_killed
4366 4118165 : ? 0 : i1dest);
4367 103463 : rtx elim_i1 = (local_elim_i1 == 0
4368 103463 : || (newi2pat && reg_set_p (i1dest, newi2pat))
4369 103463 : ? 0 : i1dest);
4370 : /* Same case as i1. */
4371 4736 : rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4372 4118165 : ? 0 : i0dest);
4373 4724 : rtx elim_i0 = (local_elim_i0 == 0
4374 4724 : || (newi2pat && reg_set_p (i0dest, newi2pat))
4375 4724 : ? 0 : i0dest);
4376 :
4377 : /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4378 : clear them. */
4379 4118165 : i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4380 4118165 : i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4381 4118165 : if (i1)
4382 103531 : i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4383 4118165 : if (i0)
4384 4736 : i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4385 :
4386 : /* Ensure that we do not have something that should not be shared but
4387 : occurs multiple times in the new insns. Check this by first
4388 : resetting all the `used' flags and then copying anything is shared. */
4389 :
4390 4118165 : reset_used_flags (i3notes);
4391 4118165 : reset_used_flags (i2notes);
4392 4118165 : reset_used_flags (i1notes);
4393 4118165 : reset_used_flags (i0notes);
4394 4118165 : reset_used_flags (newpat);
4395 4118165 : reset_used_flags (newi2pat);
4396 4118165 : if (undobuf.other_insn)
4397 229261 : reset_used_flags (PATTERN (undobuf.other_insn));
4398 :
4399 4118165 : i3notes = copy_rtx_if_shared (i3notes);
4400 4118165 : i2notes = copy_rtx_if_shared (i2notes);
4401 4118165 : i1notes = copy_rtx_if_shared (i1notes);
4402 4118165 : i0notes = copy_rtx_if_shared (i0notes);
4403 4118165 : newpat = copy_rtx_if_shared (newpat);
4404 4118165 : newi2pat = copy_rtx_if_shared (newi2pat);
4405 4118165 : if (undobuf.other_insn)
4406 229261 : reset_used_flags (PATTERN (undobuf.other_insn));
4407 :
4408 4118165 : INSN_CODE (i3) = insn_code_number;
4409 4118165 : PATTERN (i3) = newpat;
4410 :
4411 4118165 : auto call = dyn_cast<rtx_call_insn *> (i3);
4412 81397 : if (call && CALL_INSN_FUNCTION_USAGE (call))
4413 : {
4414 245220 : for (rtx link = CALL_INSN_FUNCTION_USAGE (call); link;
4415 165922 : link = XEXP (link, 1))
4416 : {
4417 165922 : if (substed_i2)
4418 : {
4419 : /* I2SRC must still be meaningful at this point. Some
4420 : splitting operations can invalidate I2SRC, but those
4421 : operations do not apply to calls. */
4422 165922 : gcc_assert (i2src);
4423 165922 : XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4424 : i2dest, i2src);
4425 : }
4426 165922 : if (substed_i1)
4427 0 : XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4428 : i1dest, i1src);
4429 165922 : if (substed_i0)
4430 0 : XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4431 : i0dest, i0src);
4432 : }
4433 : }
4434 :
4435 4118165 : if (undobuf.other_insn)
4436 229261 : INSN_CODE (undobuf.other_insn) = other_code_number;
4437 :
4438 : /* We had one special case above where I2 had more than one set and
4439 : we replaced a destination of one of those sets with the destination
4440 : of I3. In that case, we have to update LOG_LINKS of insns later
4441 : in this basic block. Note that this (expensive) case is rare.
4442 :
4443 : Also, in this case, we must pretend that all REG_NOTEs for I2
4444 : actually came from I3, so that REG_UNUSED notes from I2 will be
4445 : properly handled. */
4446 :
4447 4118165 : if (i3_subst_into_i2)
4448 : {
4449 209692 : for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4450 144051 : if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4451 66896 : || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4452 143218 : && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4453 128281 : && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4454 272332 : && ! find_reg_note (i2, REG_UNUSED,
4455 128281 : SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4456 31300624 : for (temp_insn = NEXT_INSN (i2);
4457 : temp_insn
4458 31300624 : && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4459 31051303 : || BB_HEAD (this_basic_block) != temp_insn);
4460 31242574 : temp_insn = NEXT_INSN (temp_insn))
4461 31242574 : if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4462 20553740 : FOR_EACH_LOG_LINK (link, temp_insn)
4463 7553960 : if (link->insn == i2)
4464 705 : link->insn = i3;
4465 :
4466 65641 : if (i3notes)
4467 : {
4468 : rtx link = i3notes;
4469 73206 : while (XEXP (link, 1))
4470 : link = XEXP (link, 1);
4471 65641 : XEXP (link, 1) = i2notes;
4472 : }
4473 : else
4474 : i3notes = i2notes;
4475 : i2notes = 0;
4476 : }
4477 :
4478 4118165 : LOG_LINKS (i3) = NULL;
4479 4118165 : REG_NOTES (i3) = 0;
4480 4118165 : LOG_LINKS (i2) = NULL;
4481 4118165 : REG_NOTES (i2) = 0;
4482 :
4483 4118165 : if (newi2pat)
4484 : {
4485 112077 : if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4486 10370 : propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4487 : this_basic_block);
4488 112077 : INSN_CODE (i2) = i2_code_number;
4489 112077 : PATTERN (i2) = newi2pat;
4490 : }
4491 : else
4492 : {
4493 4006088 : if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4494 2123434 : propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4495 : this_basic_block);
4496 4006088 : SET_INSN_DELETED (i2);
4497 : }
4498 :
4499 4118165 : if (i1)
4500 : {
4501 103531 : LOG_LINKS (i1) = NULL;
4502 103531 : REG_NOTES (i1) = 0;
4503 103531 : if (MAY_HAVE_DEBUG_BIND_INSNS)
4504 53219 : propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4505 : this_basic_block);
4506 103531 : SET_INSN_DELETED (i1);
4507 : }
4508 :
4509 4118165 : if (i0)
4510 : {
4511 4736 : LOG_LINKS (i0) = NULL;
4512 4736 : REG_NOTES (i0) = 0;
4513 4736 : if (MAY_HAVE_DEBUG_BIND_INSNS)
4514 2997 : propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4515 : this_basic_block);
4516 4736 : SET_INSN_DELETED (i0);
4517 : }
4518 :
4519 : /* Get death notes for everything that is now used in either I3 or
4520 : I2 and used to die in a previous insn. If we built two new
4521 : patterns, move from I1 to I2 then I2 to I3 so that we get the
4522 : proper movement on registers that I2 modifies. */
4523 :
4524 4736 : if (i0)
4525 4736 : from_luid = DF_INSN_LUID (i0);
4526 4113429 : else if (i1)
4527 98795 : from_luid = DF_INSN_LUID (i1);
4528 : else
4529 4014634 : from_luid = DF_INSN_LUID (i2);
4530 4118165 : if (newi2pat)
4531 112077 : move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4532 4118165 : move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4533 :
4534 : /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4535 4118165 : if (i3notes)
4536 7427349 : distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4537 : elim_i2, elim_i1, elim_i0);
4538 4118165 : if (i2notes)
4539 5774060 : distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4540 : elim_i2, elim_i1, elim_i0);
4541 4118165 : if (i1notes)
4542 60976 : distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4543 : elim_i2, local_elim_i1, local_elim_i0);
4544 4118165 : if (i0notes)
4545 3988 : distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4546 : elim_i2, elim_i1, local_elim_i0);
4547 4118165 : if (midnotes)
4548 4940056 : distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4549 : elim_i2, elim_i1, elim_i0);
4550 :
4551 : /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4552 : know these are REG_UNUSED and want them to go to the desired insn,
4553 : so we always pass it as i3. */
4554 :
4555 4118165 : if (newi2pat && new_i2_notes)
4556 49002 : distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4557 : NULL_RTX);
4558 :
4559 4118165 : if (new_i3_notes)
4560 167987 : distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4561 : NULL_RTX);
4562 :
4563 : /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4564 : put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4565 : I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4566 : in that case, it might delete I2. Similarly for I2 and I1.
4567 : Show an additional death due to the REG_DEAD note we make here. If
4568 : we discard it in distribute_notes, we will decrement it again. */
4569 :
4570 4118165 : if (i3dest_killed)
4571 : {
4572 345972 : rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4573 345972 : if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4574 682 : distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4575 : elim_i1, elim_i0);
4576 : else
4577 688620 : distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4578 : elim_i2, elim_i1, elim_i0);
4579 : }
4580 :
4581 4118165 : if (i2dest_in_i2src)
4582 : {
4583 85821 : rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4584 85821 : if (newi2pat && reg_set_p (i2dest, newi2pat))
4585 855 : distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4586 : NULL_RTX, NULL_RTX);
4587 : else
4588 169894 : distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4589 : NULL_RTX, NULL_RTX, NULL_RTX);
4590 : }
4591 :
4592 4118165 : if (i1dest_in_i1src)
4593 : {
4594 65 : rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4595 65 : if (newi2pat && reg_set_p (i1dest, newi2pat))
4596 5 : distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4597 : NULL_RTX, NULL_RTX);
4598 : else
4599 102 : distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4600 : NULL_RTX, NULL_RTX, NULL_RTX);
4601 : }
4602 :
4603 4118165 : if (i0dest_in_i0src)
4604 : {
4605 12 : rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4606 12 : if (newi2pat && reg_set_p (i0dest, newi2pat))
4607 0 : distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4608 : NULL_RTX, NULL_RTX);
4609 : else
4610 24 : distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4611 : NULL_RTX, NULL_RTX, NULL_RTX);
4612 : }
4613 :
4614 4118165 : if (only_i3_changed)
4615 41634 : distribute_links (i3links, i3, param_max_combine_search_insns);
4616 : else
4617 : {
4618 4076531 : distribute_links (i3links);
4619 4076531 : distribute_links (i2links, i2);
4620 4076531 : distribute_links (i1links);
4621 4076531 : distribute_links (i0links);
4622 : }
4623 :
4624 4118165 : if (REG_P (i2dest))
4625 : {
4626 4118165 : struct insn_link *link;
4627 4118165 : rtx_insn *i2_insn = 0;
4628 4118165 : rtx i2_val = 0, set;
4629 :
4630 : /* The insn that used to set this register doesn't exist, and
4631 : this life of the register may not exist either. See if one of
4632 : I3's links points to an insn that sets I2DEST. If it does,
4633 : that is now the last known value for I2DEST. If we don't update
4634 : this and I2 set the register to a value that depended on its old
4635 : contents, we will get confused. If this insn is used, thing
4636 : will be set correctly in combine_instructions. */
4637 7571846 : FOR_EACH_LOG_LINK (link, i3)
4638 3453681 : if ((set = single_set (link->insn)) != 0
4639 3453681 : && rtx_equal_p (i2dest, SET_DEST (set)))
4640 48228 : i2_insn = link->insn, i2_val = SET_SRC (set);
4641 :
4642 4118165 : record_value_for_reg (i2dest, i2_insn, i2_val);
4643 :
4644 : /* If the reg formerly set in I2 died only once and that was in I3,
4645 : zero its use count so it won't make `reload' do any work. */
4646 4118165 : if (! added_sets_2
4647 3980350 : && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4648 3941303 : && ! i2dest_in_i2src
4649 7994958 : && REGNO (i2dest) < reg_n_sets_max)
4650 3876791 : INC_REG_N_SETS (REGNO (i2dest), -1);
4651 : }
4652 :
4653 4118165 : if (i1 && REG_P (i1dest))
4654 : {
4655 103531 : struct insn_link *link;
4656 103531 : rtx_insn *i1_insn = 0;
4657 103531 : rtx i1_val = 0, set;
4658 :
4659 180390 : FOR_EACH_LOG_LINK (link, i3)
4660 76859 : if ((set = single_set (link->insn)) != 0
4661 76859 : && rtx_equal_p (i1dest, SET_DEST (set)))
4662 495 : i1_insn = link->insn, i1_val = SET_SRC (set);
4663 :
4664 103531 : record_value_for_reg (i1dest, i1_insn, i1_val);
4665 :
4666 103531 : if (! added_sets_1
4667 : && ! i1dest_in_i1src
4668 103531 : && REGNO (i1dest) < reg_n_sets_max)
4669 97203 : INC_REG_N_SETS (REGNO (i1dest), -1);
4670 : }
4671 :
4672 4118165 : if (i0 && REG_P (i0dest))
4673 : {
4674 4736 : struct insn_link *link;
4675 4736 : rtx_insn *i0_insn = 0;
4676 4736 : rtx i0_val = 0, set;
4677 :
4678 7324 : FOR_EACH_LOG_LINK (link, i3)
4679 2588 : if ((set = single_set (link->insn)) != 0
4680 2588 : && rtx_equal_p (i0dest, SET_DEST (set)))
4681 0 : i0_insn = link->insn, i0_val = SET_SRC (set);
4682 :
4683 4736 : record_value_for_reg (i0dest, i0_insn, i0_val);
4684 :
4685 4736 : if (! added_sets_0
4686 : && ! i0dest_in_i0src
4687 4736 : && REGNO (i0dest) < reg_n_sets_max)
4688 4675 : INC_REG_N_SETS (REGNO (i0dest), -1);
4689 : }
4690 :
4691 : /* Update reg_stat[].nonzero_bits et al for any changes that may have
4692 : been made to this insn. The order is important, because newi2pat
4693 : can affect nonzero_bits of newpat. */
4694 4118165 : if (newi2pat)
4695 112077 : note_pattern_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4696 4118165 : note_pattern_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4697 : }
4698 :
4699 4118165 : if (undobuf.other_insn != NULL_RTX)
4700 : {
4701 229261 : if (dump_file)
4702 : {
4703 12 : fprintf (dump_file, "modifying other_insn ");
4704 12 : dump_insn_slim (dump_file, undobuf.other_insn);
4705 : }
4706 229261 : df_insn_rescan (undobuf.other_insn);
4707 : }
4708 :
4709 4118165 : if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4710 : {
4711 0 : if (dump_file)
4712 : {
4713 0 : fprintf (dump_file, "modifying insn i0 ");
4714 0 : dump_insn_slim (dump_file, i0);
4715 : }
4716 0 : df_insn_rescan (i0);
4717 : }
4718 :
4719 4118165 : if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4720 : {
4721 0 : if (dump_file)
4722 : {
4723 0 : fprintf (dump_file, "modifying insn i1 ");
4724 0 : dump_insn_slim (dump_file, i1);
4725 : }
4726 0 : df_insn_rescan (i1);
4727 : }
4728 :
4729 4118165 : if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4730 : {
4731 112077 : if (dump_file)
4732 : {
4733 19 : fprintf (dump_file, "modifying insn i2 ");
4734 19 : dump_insn_slim (dump_file, i2);
4735 : }
4736 112077 : df_insn_rescan (i2);
4737 : }
4738 :
4739 4118165 : if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4740 : {
4741 4118165 : if (dump_file)
4742 : {
4743 338 : fprintf (dump_file, "modifying insn i3 ");
4744 338 : dump_insn_slim (dump_file, i3);
4745 : }
4746 4118165 : df_insn_rescan (i3);
4747 : }
4748 :
4749 : /* Set new_direct_jump_p if a new return or simple jump instruction
4750 : has been created. Adjust the CFG accordingly. */
4751 4118165 : if (returnjump_p (i3) || any_uncondjump_p (i3))
4752 : {
4753 204 : *new_direct_jump_p = 1;
4754 204 : mark_jump_label (PATTERN (i3), i3, 0);
4755 204 : update_cfg_for_uncondjump (i3);
4756 : }
4757 :
4758 4118165 : if (undobuf.other_insn != NULL_RTX
4759 4118165 : && (returnjump_p (undobuf.other_insn)
4760 229261 : || any_uncondjump_p (undobuf.other_insn)))
4761 : {
4762 1913 : *new_direct_jump_p = 1;
4763 1913 : update_cfg_for_uncondjump (undobuf.other_insn);
4764 : }
4765 :
4766 4118165 : if (GET_CODE (PATTERN (i3)) == TRAP_IF
4767 4118165 : && XEXP (PATTERN (i3), 0) == const1_rtx)
4768 : {
4769 0 : basic_block bb = BLOCK_FOR_INSN (i3);
4770 0 : gcc_assert (bb);
4771 0 : remove_edge (split_block (bb, i3));
4772 0 : emit_barrier_after_bb (bb);
4773 0 : *new_direct_jump_p = 1;
4774 : }
4775 :
4776 4118165 : if (undobuf.other_insn
4777 229261 : && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4778 4118165 : && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4779 : {
4780 0 : basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4781 0 : gcc_assert (bb);
4782 0 : remove_edge (split_block (bb, undobuf.other_insn));
4783 0 : emit_barrier_after_bb (bb);
4784 0 : *new_direct_jump_p = 1;
4785 : }
4786 :
4787 : /* A noop might also need cleaning up of CFG, if it comes from the
4788 : simplification of a jump. */
4789 4118165 : if (JUMP_P (i3)
4790 47391 : && GET_CODE (newpat) == SET
4791 35481 : && SET_SRC (newpat) == pc_rtx
4792 391 : && SET_DEST (newpat) == pc_rtx)
4793 : {
4794 391 : *new_direct_jump_p = 1;
4795 391 : update_cfg_for_uncondjump (i3);
4796 : }
4797 :
4798 4118165 : if (undobuf.other_insn != NULL_RTX
4799 229261 : && JUMP_P (undobuf.other_insn)
4800 223638 : && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4801 223638 : && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4802 4119925 : && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4803 : {
4804 1760 : *new_direct_jump_p = 1;
4805 1760 : update_cfg_for_uncondjump (undobuf.other_insn);
4806 : }
4807 :
4808 4118165 : combine_successes++;
4809 4118165 : undo_commit ();
4810 :
4811 4118165 : if (only_i3_changed)
4812 : return i3;
4813 :
4814 4076531 : rtx_insn *ret = newi2pat ? i2 : i3;
4815 4076531 : if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4816 : ret = added_links_insn;
4817 4076531 : if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4818 : ret = added_notes_insn;
4819 :
4820 : return ret;
4821 : }
4822 :
4823 : /* Get a marker for undoing to the current state. */
4824 :
4825 : static void *
4826 38528901 : get_undo_marker (void)
4827 : {
4828 38528901 : return undobuf.undos;
4829 : }
4830 :
4831 : /* Undo the modifications up to the marker. */
4832 :
4833 : static void
4834 45468347 : undo_to_marker (void *marker)
4835 : {
4836 45468347 : struct undo *undo, *next;
4837 :
4838 143445710 : for (undo = undobuf.undos; undo != marker; undo = next)
4839 : {
4840 97977363 : gcc_assert (undo);
4841 :
4842 97977363 : next = undo->next;
4843 97977363 : switch (undo->kind)
4844 : {
4845 90498106 : case UNDO_RTX:
4846 90498106 : *undo->where.r = undo->old_contents.r;
4847 90498106 : break;
4848 6840719 : case UNDO_INT:
4849 6840719 : *undo->where.i = undo->old_contents.i;
4850 6840719 : break;
4851 565228 : case UNDO_MODE:
4852 565228 : adjust_reg_mode (regno_reg_rtx[undo->where.regno],
4853 : undo->old_contents.m);
4854 565228 : break;
4855 73310 : case UNDO_LINKS:
4856 73310 : *undo->where.l = undo->old_contents.l;
4857 73310 : break;
4858 0 : default:
4859 0 : gcc_unreachable ();
4860 : }
4861 :
4862 97977363 : undo->next = undobuf.frees;
4863 97977363 : undobuf.frees = undo;
4864 : }
4865 :
4866 45468347 : undobuf.undos = (struct undo *) marker;
4867 45468347 : }
4868 :
4869 : /* Undo all the modifications recorded in undobuf. */
4870 :
4871 : static void
4872 44320911 : undo_all (void)
4873 : {
4874 44320911 : undo_to_marker (0);
4875 0 : }
4876 :
4877 : /* We've committed to accepting the changes we made. Move all
4878 : of the undos to the free list. */
4879 :
4880 : static void
4881 4118165 : undo_commit (void)
4882 : {
4883 4118165 : struct undo *undo, *next;
4884 :
4885 12174700 : for (undo = undobuf.undos; undo; undo = next)
4886 : {
4887 8056535 : next = undo->next;
4888 8056535 : undo->next = undobuf.frees;
4889 8056535 : undobuf.frees = undo;
4890 : }
4891 4118165 : undobuf.undos = 0;
4892 4118165 : }
4893 :
4894 : /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4895 : where we have an arithmetic expression and return that point. LOC will
4896 : be inside INSN.
4897 :
4898 : try_combine will call this function to see if an insn can be split into
4899 : two insns. */
4900 :
4901 : static rtx *
4902 32073170 : find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4903 : {
4904 33131128 : rtx x = *loc;
4905 33131128 : enum rtx_code code = GET_CODE (x);
4906 33131128 : rtx *split;
4907 33131128 : unsigned HOST_WIDE_INT len = 0;
4908 33131128 : HOST_WIDE_INT pos = 0;
4909 33131128 : bool unsignedp = false;
4910 33131128 : rtx inner = NULL_RTX;
4911 33131128 : scalar_int_mode mode, inner_mode;
4912 :
4913 : /* First special-case some codes. */
4914 33131128 : switch (code)
4915 : {
4916 1062425 : case SUBREG:
4917 : #ifdef INSN_SCHEDULING
4918 : /* If we are making a paradoxical SUBREG invalid, it becomes a split
4919 : point. */
4920 1062425 : if (MEM_P (SUBREG_REG (x)))
4921 : return loc;
4922 : #endif
4923 1050561 : return find_split_point (&SUBREG_REG (x), insn, false);
4924 :
4925 1554957 : case MEM:
4926 : /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4927 : using LO_SUM and HIGH. */
4928 1554957 : if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4929 : || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4930 : {
4931 : machine_mode address_mode = get_address_mode (x);
4932 :
4933 : SUBST (XEXP (x, 0),
4934 : gen_rtx_LO_SUM (address_mode,
4935 : gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4936 : XEXP (x, 0)));
4937 : return &XEXP (XEXP (x, 0), 0);
4938 : }
4939 :
4940 : /* If we have a PLUS whose second operand is a constant and the
4941 : address is not valid, perhaps we can split it up using
4942 : the machine-specific way to split large constants. We use
4943 : the first pseudo-reg (one of the virtual regs) as a placeholder;
4944 : it will not remain in the result. */
4945 1554957 : if (GET_CODE (XEXP (x, 0)) == PLUS
4946 1035176 : && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4947 3347120 : && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4948 756987 : MEM_ADDR_SPACE (x)))
4949 : {
4950 118244 : rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4951 118244 : unsigned int old_nregs, new_nregs;
4952 118244 : rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4953 : subst_insn, &old_nregs, &new_nregs);
4954 :
4955 : /* This should have produced two insns, each of which sets our
4956 : placeholder. If the source of the second is a valid address,
4957 : we can put both sources together and make a split point
4958 : in the middle. */
4959 :
4960 118244 : if (seq
4961 56 : && NEXT_INSN (seq) != NULL_RTX
4962 0 : && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4963 0 : && NONJUMP_INSN_P (seq)
4964 0 : && GET_CODE (PATTERN (seq)) == SET
4965 0 : && SET_DEST (PATTERN (seq)) == reg
4966 0 : && ! reg_mentioned_p (reg,
4967 0 : SET_SRC (PATTERN (seq)))
4968 0 : && NONJUMP_INSN_P (NEXT_INSN (seq))
4969 0 : && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4970 0 : && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4971 118244 : && memory_address_addr_space_p
4972 118244 : (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4973 0 : MEM_ADDR_SPACE (x)))
4974 : {
4975 0 : rtx src1 = SET_SRC (PATTERN (seq));
4976 0 : rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4977 :
4978 : /* Replace the placeholder in SRC2 with SRC1. If we can
4979 : find where in SRC2 it was placed, that can become our
4980 : split point and we can replace this address with SRC2.
4981 : Just try two obvious places. */
4982 :
4983 0 : src2 = replace_rtx (src2, reg, src1);
4984 0 : split = 0;
4985 0 : if (XEXP (src2, 0) == src1)
4986 0 : split = &XEXP (src2, 0);
4987 0 : else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4988 0 : && XEXP (XEXP (src2, 0), 0) == src1)
4989 0 : split = &XEXP (XEXP (src2, 0), 0);
4990 :
4991 0 : if (split)
4992 : {
4993 0 : SUBST (XEXP (x, 0), src2);
4994 92188 : return split;
4995 : }
4996 : }
4997 :
4998 : /* If that didn't work and we have a nested plus, like:
4999 : ((REG1 * CONST1) + REG2) + CONST2 and (REG1 + REG2) + CONST2
5000 : is valid address, try to split (REG1 * CONST1). */
5001 118244 : if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5002 75894 : && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5003 61778 : && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5004 61767 : && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SUBREG
5005 10 : && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5006 : 0), 0)))))
5007 : {
5008 61767 : rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 0);
5009 61767 : XEXP (XEXP (XEXP (x, 0), 0), 0) = reg;
5010 123534 : if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5011 61767 : MEM_ADDR_SPACE (x)))
5012 : {
5013 50384 : XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5014 50384 : return &XEXP (XEXP (XEXP (x, 0), 0), 0);
5015 : }
5016 11383 : XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5017 11383 : }
5018 56477 : else if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5019 14127 : && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5020 14116 : && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5021 290 : && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SUBREG
5022 290 : && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5023 : 0), 1)))))
5024 : {
5025 0 : rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 1);
5026 0 : XEXP (XEXP (XEXP (x, 0), 0), 1) = reg;
5027 0 : if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5028 0 : MEM_ADDR_SPACE (x)))
5029 : {
5030 0 : XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5031 0 : return &XEXP (XEXP (XEXP (x, 0), 0), 1);
5032 : }
5033 0 : XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5034 : }
5035 :
5036 : /* If that didn't work, perhaps the first operand is complex and
5037 : needs to be computed separately, so make a split point there.
5038 : This will occur on machines that just support REG + CONST
5039 : and have a constant moved through some previous computation. */
5040 67860 : if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
5041 41804 : && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5042 0 : && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5043 41804 : return &XEXP (XEXP (x, 0), 0);
5044 : }
5045 :
5046 : /* If we have a PLUS whose first operand is complex, try computing it
5047 : separately by making a split there. */
5048 1462769 : if (GET_CODE (XEXP (x, 0)) == PLUS
5049 2571980 : && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5050 942988 : MEM_ADDR_SPACE (x))
5051 166223 : && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
5052 1575405 : && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5053 606 : && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5054 112632 : return &XEXP (XEXP (x, 0), 0);
5055 : break;
5056 :
5057 4862697 : case SET:
5058 : /* See if we can split SET_SRC as it stands. */
5059 4862697 : split = find_split_point (&SET_SRC (x), insn, true);
5060 4862697 : if (split && split != &SET_SRC (x))
5061 : return split;
5062 :
5063 : /* See if we can split SET_DEST as it stands. */
5064 517714 : split = find_split_point (&SET_DEST (x), insn, false);
5065 517714 : if (split && split != &SET_DEST (x))
5066 : return split;
5067 :
5068 : /* See if this is a bitfield assignment with everything constant. If
5069 : so, this is an IOR of an AND, so split it into that. */
5070 485871 : if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5071 4113 : && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5072 : &inner_mode)
5073 4113 : && HWI_COMPUTABLE_MODE_P (inner_mode)
5074 4113 : && CONST_INT_P (XEXP (SET_DEST (x), 1))
5075 4113 : && CONST_INT_P (XEXP (SET_DEST (x), 2))
5076 3942 : && CONST_INT_P (SET_SRC (x))
5077 425 : && ((INTVAL (XEXP (SET_DEST (x), 1))
5078 425 : + INTVAL (XEXP (SET_DEST (x), 2)))
5079 425 : <= GET_MODE_PRECISION (inner_mode))
5080 486296 : && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5081 : {
5082 408 : HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5083 408 : unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5084 408 : rtx dest = XEXP (SET_DEST (x), 0);
5085 408 : unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << len) - 1;
5086 408 : unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x)) & mask;
5087 408 : rtx or_mask;
5088 :
5089 408 : if (BITS_BIG_ENDIAN)
5090 : pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5091 :
5092 408 : or_mask = gen_int_mode (src << pos, inner_mode);
5093 408 : if (src == mask)
5094 0 : SUBST (SET_SRC (x),
5095 : simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5096 : else
5097 : {
5098 408 : rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5099 408 : SUBST (SET_SRC (x),
5100 : simplify_gen_binary (IOR, inner_mode,
5101 : simplify_gen_binary (AND, inner_mode,
5102 : dest, negmask),
5103 : or_mask));
5104 : }
5105 :
5106 408 : SUBST (SET_DEST (x), dest);
5107 :
5108 408 : split = find_split_point (&SET_SRC (x), insn, true);
5109 408 : if (split && split != &SET_SRC (x))
5110 : return split;
5111 : }
5112 :
5113 : /* Otherwise, see if this is an operation that we can split into two.
5114 : If so, try to split that. */
5115 485463 : code = GET_CODE (SET_SRC (x));
5116 :
5117 485463 : switch (code)
5118 : {
5119 17311 : case AND:
5120 : /* If we are AND'ing with a large constant that is only a single
5121 : bit and the result is only being used in a context where we
5122 : need to know if it is zero or nonzero, replace it with a bit
5123 : extraction. This will avoid the large constant, which might
5124 : have taken more than one insn to make. If the constant were
5125 : not a valid argument to the AND but took only one insn to make,
5126 : this is no worse, but if it took more than one insn, it will
5127 : be better. */
5128 :
5129 17311 : if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5130 11498 : && REG_P (XEXP (SET_SRC (x), 0))
5131 450 : && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5132 2 : && REG_P (SET_DEST (x))
5133 1 : && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5134 1 : && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5135 0 : && XEXP (*split, 0) == SET_DEST (x)
5136 17311 : && XEXP (*split, 1) == const0_rtx)
5137 : {
5138 0 : rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5139 0 : XEXP (SET_SRC (x), 0),
5140 : pos, NULL_RTX, 1,
5141 : true, false, false);
5142 0 : if (extraction != 0)
5143 : {
5144 0 : SUBST (SET_SRC (x), extraction);
5145 0 : return find_split_point (loc, insn, false);
5146 : }
5147 : }
5148 : break;
5149 :
5150 : case NE:
5151 : /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5152 : is known to be on, this can be converted into a NEG of a shift. */
5153 : if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5154 : && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5155 : && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5156 : GET_MODE (XEXP (SET_SRC (x),
5157 : 0))))) >= 1))
5158 : {
5159 : machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5160 : rtx pos_rtx = gen_int_shift_amount (mode, pos);
5161 : SUBST (SET_SRC (x),
5162 : gen_rtx_NEG (mode,
5163 : gen_rtx_LSHIFTRT (mode,
5164 : XEXP (SET_SRC (x), 0),
5165 : pos_rtx)));
5166 :
5167 : split = find_split_point (&SET_SRC (x), insn, true);
5168 : if (split && split != &SET_SRC (x))
5169 : return split;
5170 : }
5171 : break;
5172 :
5173 518 : case SIGN_EXTEND:
5174 518 : inner = XEXP (SET_SRC (x), 0);
5175 :
5176 : /* We can't optimize if either mode is a partial integer
5177 : mode as we don't know how many bits are significant
5178 : in those modes. */
5179 518 : if (!is_int_mode (GET_MODE (inner), &inner_mode)
5180 512 : || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5181 : break;
5182 :
5183 512 : pos = 0;
5184 512 : len = GET_MODE_PRECISION (inner_mode);
5185 512 : unsignedp = false;
5186 512 : break;
5187 :
5188 13297 : case SIGN_EXTRACT:
5189 13297 : case ZERO_EXTRACT:
5190 13297 : if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5191 : &inner_mode)
5192 12896 : && CONST_INT_P (XEXP (SET_SRC (x), 1))
5193 12896 : && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5194 : {
5195 12456 : inner = XEXP (SET_SRC (x), 0);
5196 12456 : len = INTVAL (XEXP (SET_SRC (x), 1));
5197 12456 : pos = INTVAL (XEXP (SET_SRC (x), 2));
5198 :
5199 12456 : if (BITS_BIG_ENDIAN)
5200 : pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5201 12456 : unsignedp = (code == ZERO_EXTRACT);
5202 : }
5203 : break;
5204 :
5205 : default:
5206 : break;
5207 : }
5208 :
5209 485463 : if (len
5210 12968 : && known_subrange_p (pos, len,
5211 12968 : 0, GET_MODE_PRECISION (GET_MODE (inner)))
5212 498431 : && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5213 : {
5214 : /* For unsigned, we have a choice of a shift followed by an
5215 : AND or two shifts. Use two shifts for field sizes where the
5216 : constant might be too large. We assume here that we can
5217 : always at least get 8-bit constants in an AND insn, which is
5218 : true for every current RISC. */
5219 :
5220 12968 : if (unsignedp && len <= 8)
5221 : {
5222 5514 : unsigned HOST_WIDE_INT mask
5223 5514 : = (HOST_WIDE_INT_1U << len) - 1;
5224 5514 : rtx pos_rtx = gen_int_shift_amount (mode, pos);
5225 5514 : SUBST (SET_SRC (x),
5226 : gen_rtx_AND (mode,
5227 : gen_rtx_LSHIFTRT
5228 : (mode, gen_lowpart (mode, inner), pos_rtx),
5229 : gen_int_mode (mask, mode)));
5230 :
5231 5514 : split = find_split_point (&SET_SRC (x), insn, true);
5232 5514 : if (split && split != &SET_SRC (x))
5233 32073170 : return split;
5234 : }
5235 : else
5236 : {
5237 7454 : int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5238 7454 : int right_bits = GET_MODE_PRECISION (mode) - len;
5239 14908 : SUBST (SET_SRC (x),
5240 : gen_rtx_fmt_ee
5241 : (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5242 : gen_rtx_ASHIFT (mode,
5243 : gen_lowpart (mode, inner),
5244 : gen_int_shift_amount (mode, left_bits)),
5245 : gen_int_shift_amount (mode, right_bits)));
5246 :
5247 7454 : split = find_split_point (&SET_SRC (x), insn, true);
5248 7454 : if (split && split != &SET_SRC (x))
5249 32073170 : return split;
5250 : }
5251 : }
5252 :
5253 : /* See if this is a simple operation with a constant as the second
5254 : operand. It might be that this constant is out of range and hence
5255 : could be used as a split point. */
5256 472495 : if (BINARY_P (SET_SRC (x))
5257 206859 : && CONSTANT_P (XEXP (SET_SRC (x), 1))
5258 114818 : && (OBJECT_P (XEXP (SET_SRC (x), 0))
5259 37789 : || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5260 11219 : && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5261 78804 : return &XEXP (SET_SRC (x), 1);
5262 :
5263 : /* Finally, see if this is a simple operation with its first operand
5264 : not in a register. The operation might require this operand in a
5265 : register, so return it as a split point. We can always do this
5266 : because if the first operand were another operation, we would have
5267 : already found it as a split point. */
5268 393691 : if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5269 393691 : && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5270 122089 : return &XEXP (SET_SRC (x), 0);
5271 :
5272 : return 0;
5273 :
5274 1028457 : case AND:
5275 1028457 : case IOR:
5276 : /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5277 : it is better to write this as (not (ior A B)) so we can split it.
5278 : Similarly for IOR. */
5279 1028457 : if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5280 : {
5281 1994 : SUBST (*loc,
5282 : gen_rtx_NOT (GET_MODE (x),
5283 : gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5284 : GET_MODE (x),
5285 : XEXP (XEXP (x, 0), 0),
5286 : XEXP (XEXP (x, 1), 0))));
5287 997 : return find_split_point (loc, insn, set_src);
5288 : }
5289 :
5290 : /* Many RISC machines have a large set of logical insns. If the
5291 : second operand is a NOT, put it first so we will try to split the
5292 : other operand first. */
5293 1027460 : if (GET_CODE (XEXP (x, 1)) == NOT)
5294 : {
5295 6232 : rtx tem = XEXP (x, 0);
5296 6232 : SUBST (XEXP (x, 0), XEXP (x, 1));
5297 6232 : SUBST (XEXP (x, 1), tem);
5298 : }
5299 : /* Many targets have a `(and (not X) Y)` and/or `(ior (not X) Y)` instructions.
5300 : Split at that insns. However if this is
5301 : the SET_SRC, we likely do not have such an instruction and it's
5302 : worthless to try this split. */
5303 1027460 : if (!set_src && GET_CODE (XEXP (x, 0)) == NOT)
5304 : return loc;
5305 : break;
5306 :
5307 3473411 : case PLUS:
5308 3473411 : case MINUS:
5309 : /* Canonicalization can produce (minus A (mult B C)), where C is a
5310 : constant. It may be better to try splitting (plus (mult B -C) A)
5311 : instead if this isn't a multiply by a power of two. */
5312 201773 : if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5313 22204 : && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5314 3479811 : && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5315 : {
5316 6400 : machine_mode mode = GET_MODE (x);
5317 6400 : unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5318 6400 : HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5319 6400 : SUBST (*loc, gen_rtx_PLUS (mode,
5320 : gen_rtx_MULT (mode,
5321 : XEXP (XEXP (x, 1), 0),
5322 : gen_int_mode (other_int,
5323 : mode)),
5324 : XEXP (x, 0)));
5325 6400 : return find_split_point (loc, insn, set_src);
5326 : }
5327 :
5328 : /* Split at a multiply-accumulate instruction. However if this is
5329 : the SET_SRC, we likely do not have such an instruction and it's
5330 : worthless to try this split. */
5331 3467011 : if (!set_src
5332 2063344 : && (GET_CODE (XEXP (x, 0)) == MULT
5333 1946681 : || (GET_CODE (XEXP (x, 0)) == ASHIFT
5334 141084 : && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5335 : return loc;
5336 :
5337 : default:
5338 : break;
5339 : }
5340 :
5341 : /* Otherwise, select our actions depending on our rtx class. */
5342 26729912 : switch (GET_RTX_CLASS (code))
5343 : {
5344 1521714 : case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5345 1521714 : case RTX_TERNARY:
5346 1521714 : split = find_split_point (&XEXP (x, 2), insn, false);
5347 1521714 : if (split)
5348 : return split;
5349 : /* fall through */
5350 10537423 : case RTX_BIN_ARITH:
5351 10537423 : case RTX_COMM_ARITH:
5352 10537423 : case RTX_COMPARE:
5353 10537423 : case RTX_COMM_COMPARE:
5354 10537423 : split = find_split_point (&XEXP (x, 1), insn, false);
5355 10537423 : if (split)
5356 : return split;
5357 : /* fall through */
5358 10139880 : case RTX_UNARY:
5359 : /* Some machines have (and (shift ...) ...) insns. If X is not
5360 : an AND, but XEXP (X, 0) is, use it as our split point. */
5361 10139880 : if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5362 382331 : return &XEXP (x, 0);
5363 :
5364 9757549 : split = find_split_point (&XEXP (x, 0), insn, false);
5365 9757549 : if (split)
5366 5987797 : return split;
5367 : return loc;
5368 :
5369 : default:
5370 : /* Otherwise, we don't have a split point. */
5371 : return 0;
5372 : }
5373 : }
5374 :
5375 : /* Throughout X, replace FROM with TO, and return the result.
5376 : The result is TO if X is FROM;
5377 : otherwise the result is X, but its contents may have been modified.
5378 : If they were modified, a record was made in undobuf so that
5379 : undo_all will (among other things) return X to its original state.
5380 :
5381 : If the number of changes necessary is too much to record to undo,
5382 : the excess changes are not made, so the result is invalid.
5383 : The changes already made can still be undone.
5384 : undobuf.num_undo is incremented for such changes, so by testing that
5385 : the caller can tell whether the result is valid.
5386 :
5387 : `n_occurrences' is incremented each time FROM is replaced.
5388 :
5389 : IN_DEST is true if we are processing the SET_DEST of a SET.
5390 :
5391 : IN_COND is true if we are at the top level of a condition.
5392 :
5393 : UNIQUE_COPY is true if each substitution must be unique. We do this
5394 : by copying if `n_occurrences' is nonzero. */
5395 :
5396 : static rtx
5397 428450351 : subst (rtx x, rtx from, rtx to, bool in_dest, bool in_cond, bool unique_copy)
5398 : {
5399 428450351 : enum rtx_code code = GET_CODE (x);
5400 428450351 : machine_mode op0_mode = VOIDmode;
5401 428450351 : const char *fmt;
5402 428450351 : int len, i;
5403 428450351 : rtx new_rtx;
5404 :
5405 : /* Two expressions are equal if they are identical copies of a shared
5406 : RTX or if they are both registers with the same register number
5407 : and mode. */
5408 :
5409 : #define COMBINE_RTX_EQUAL_P(X,Y) \
5410 : ((X) == (Y) \
5411 : || (REG_P (X) && REG_P (Y) \
5412 : && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5413 :
5414 : /* Do not substitute into clobbers of regs -- this will never result in
5415 : valid RTL. */
5416 428450351 : if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5417 : return x;
5418 :
5419 417493380 : if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5420 : {
5421 0 : n_occurrences++;
5422 0 : return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5423 : }
5424 :
5425 : /* If X and FROM are the same register but different modes, they
5426 : will not have been seen as equal above. However, the log links code
5427 : will make a LOG_LINKS entry for that case. If we do nothing, we
5428 : will try to rerecognize our original insn and, when it succeeds,
5429 : we will delete the feeding insn, which is incorrect.
5430 :
5431 : So force this insn not to match in this (rare) case. */
5432 93018998 : if (! in_dest && code == REG && REG_P (from)
5433 450924052 : && reg_overlap_mentioned_p (x, from))
5434 4583 : return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5435 :
5436 : /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5437 : of which may contain things that can be combined. */
5438 417488797 : if (code != MEM && code != LO_SUM && OBJECT_P (x))
5439 : return x;
5440 :
5441 : /* It is possible to have a subexpression appear twice in the insn.
5442 : Suppose that FROM is a register that appears within TO.
5443 : Then, after that subexpression has been scanned once by `subst',
5444 : the second time it is scanned, TO may be found. If we were
5445 : to scan TO here, we would find FROM within it and create a
5446 : self-referent rtl structure which is completely wrong. */
5447 223786379 : if (COMBINE_RTX_EQUAL_P (x, to))
5448 : return to;
5449 :
5450 : /* Parallel asm_operands need special attention because all of the
5451 : inputs are shared across the arms. Furthermore, unsharing the
5452 : rtl results in recognition failures. Failure to handle this case
5453 : specially can result in circular rtl.
5454 :
5455 : Solve this by doing a normal pass across the first entry of the
5456 : parallel, and only processing the SET_DESTs of the subsequent
5457 : entries. Ug. */
5458 :
5459 223639878 : if (code == PARALLEL
5460 13590095 : && GET_CODE (XVECEXP (x, 0, 0)) == SET
5461 11558941 : && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5462 : {
5463 21704 : new_rtx = subst (XVECEXP (x, 0, 0), from, to, false, false, unique_copy);
5464 :
5465 : /* If this substitution failed, this whole thing fails. */
5466 21704 : if (GET_CODE (new_rtx) == CLOBBER
5467 0 : && XEXP (new_rtx, 0) == const0_rtx)
5468 : return new_rtx;
5469 :
5470 21704 : SUBST (XVECEXP (x, 0, 0), new_rtx);
5471 :
5472 105171 : for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5473 : {
5474 83467 : rtx dest = SET_DEST (XVECEXP (x, 0, i));
5475 :
5476 83467 : if (!REG_P (dest) && GET_CODE (dest) != PC)
5477 : {
5478 3188 : new_rtx = subst (dest, from, to, false, false, unique_copy);
5479 :
5480 : /* If this substitution failed, this whole thing fails. */
5481 3188 : if (GET_CODE (new_rtx) == CLOBBER
5482 0 : && XEXP (new_rtx, 0) == const0_rtx)
5483 : return new_rtx;
5484 :
5485 3188 : SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5486 : }
5487 : }
5488 : }
5489 : else
5490 : {
5491 223618174 : len = GET_RTX_LENGTH (code);
5492 223618174 : fmt = GET_RTX_FORMAT (code);
5493 :
5494 : /* We don't need to process a SET_DEST that is a register or PC, so
5495 : set up to skip this common case. All other cases where we want
5496 : to suppress replacing something inside a SET_SRC are handled via
5497 : the IN_DEST operand. */
5498 223618174 : if (code == SET
5499 48798152 : && (REG_P (SET_DEST (x))
5500 48798152 : || GET_CODE (SET_DEST (x)) == PC))
5501 223618174 : fmt = "ie";
5502 :
5503 : /* Trying to simplify the operands of a widening MULT is not likely
5504 : to create RTL matching a machine insn. */
5505 223618174 : if (code == MULT
5506 5071462 : && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5507 5071462 : || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5508 319301 : && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5509 319301 : || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5510 240137 : && REG_P (XEXP (XEXP (x, 0), 0))
5511 113968 : && REG_P (XEXP (XEXP (x, 1), 0))
5512 101261 : && from == to)
5513 : return x;
5514 :
5515 :
5516 : /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5517 : constant. */
5518 223556436 : if (fmt[0] == 'e')
5519 164576145 : op0_mode = GET_MODE (XEXP (x, 0));
5520 :
5521 661779121 : for (i = 0; i < len; i++)
5522 : {
5523 439211707 : if (fmt[i] == 'E')
5524 : {
5525 16150375 : int j;
5526 51192575 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5527 : {
5528 35177036 : if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5529 : {
5530 1681 : new_rtx = (unique_copy && n_occurrences
5531 323294 : ? copy_rtx (to) : to);
5532 323275 : n_occurrences++;
5533 : }
5534 : else
5535 : {
5536 34853761 : new_rtx = subst (XVECEXP (x, i, j), from, to,
5537 : false, false, unique_copy);
5538 :
5539 : /* If this substitution failed, this whole thing
5540 : fails. */
5541 34853761 : if (GET_CODE (new_rtx) == CLOBBER
5542 11413728 : && XEXP (new_rtx, 0) == const0_rtx)
5543 : return new_rtx;
5544 : }
5545 :
5546 35042200 : SUBST (XVECEXP (x, i, j), new_rtx);
5547 : }
5548 : }
5549 423061332 : else if (fmt[i] == 'e')
5550 : {
5551 : /* If this is a register being set, ignore it. */
5552 345151826 : new_rtx = XEXP (x, i);
5553 345151826 : if (in_dest
5554 345151826 : && i == 0
5555 6001943 : && (((code == SUBREG || code == ZERO_EXTRACT)
5556 369153 : && REG_P (new_rtx))
5557 5635230 : || code == STRICT_LOW_PART))
5558 : ;
5559 :
5560 344774552 : else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5561 : {
5562 : /* In general, don't install a subreg involving two
5563 : modes not tieable. It can worsen register
5564 : allocation, and can even make invalid reload
5565 : insns, since the reg inside may need to be copied
5566 : from in the outside mode, and that may be invalid
5567 : if it is an fp reg copied in integer mode.
5568 :
5569 : We allow an exception to this: It is valid if
5570 : it is inside another SUBREG and the mode of that
5571 : SUBREG and the mode of the inside of TO is
5572 : tieable. */
5573 :
5574 48976912 : if (GET_CODE (to) == SUBREG
5575 616466 : && !targetm.modes_tieable_p (GET_MODE (to),
5576 616466 : GET_MODE (SUBREG_REG (to)))
5577 49313688 : && ! (code == SUBREG
5578 29970 : && (targetm.modes_tieable_p
5579 29970 : (GET_MODE (x), GET_MODE (SUBREG_REG (to))))))
5580 304543 : return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5581 :
5582 48672369 : if (code == SUBREG
5583 2612396 : && REG_P (to)
5584 98400 : && REGNO (to) < FIRST_PSEUDO_REGISTER
5585 48672374 : && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5586 5 : SUBREG_BYTE (x),
5587 5 : GET_MODE (x)) < 0)
5588 0 : return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5589 :
5590 48672369 : new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5591 48672369 : n_occurrences++;
5592 : }
5593 : else
5594 : /* If we are in a SET_DEST, suppress most cases unless we
5595 : have gone inside a MEM, in which case we want to
5596 : simplify the address. We assume here that things that
5597 : are actually part of the destination have their inner
5598 : parts in the first expression. This is true for SUBREG,
5599 : STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5600 : things aside from REG and MEM that should appear in a
5601 : SET_DEST. */
5602 340125145 : new_rtx = subst (XEXP (x, i), from, to,
5603 : (((in_dest
5604 5342875 : && (code == SUBREG || code == STRICT_LOW_PART
5605 5342875 : || code == ZERO_EXTRACT))
5606 295789783 : || code == SET)
5607 : && i == 0),
5608 295797640 : code == IF_THEN_ELSE && i == 0,
5609 : unique_copy);
5610 :
5611 : /* If we found that we will have to reject this combination,
5612 : indicate that by returning the CLOBBER ourselves, rather than
5613 : an expression containing it. This will speed things up as
5614 : well as prevent accidents where two CLOBBERs are considered
5615 : to be equal, thus producing an incorrect simplification. */
5616 :
5617 344847283 : if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5618 : return new_rtx;
5619 :
5620 344297892 : if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5621 : {
5622 31927 : machine_mode mode = GET_MODE (x);
5623 :
5624 63854 : x = simplify_subreg (GET_MODE (x), new_rtx,
5625 31927 : GET_MODE (SUBREG_REG (x)),
5626 31927 : SUBREG_BYTE (x));
5627 31927 : if (! x)
5628 2 : x = gen_rtx_CLOBBER (mode, const0_rtx);
5629 : }
5630 344265965 : else if (CONST_SCALAR_INT_P (new_rtx)
5631 : && (GET_CODE (x) == ZERO_EXTEND
5632 62431511 : || GET_CODE (x) == SIGN_EXTEND
5633 : || GET_CODE (x) == FLOAT
5634 : || GET_CODE (x) == UNSIGNED_FLOAT))
5635 : {
5636 139888 : x = simplify_unary_operation (GET_CODE (x), GET_MODE (x),
5637 : new_rtx,
5638 69944 : GET_MODE (XEXP (x, 0)));
5639 69944 : if (!x)
5640 252 : return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5641 : }
5642 : /* CONST_INTs shouldn't be substituted into PRE_DEC, PRE_MODIFY
5643 : etc. arguments, otherwise we can ICE before trying to recog
5644 : it. See PR104446. */
5645 344196021 : else if (CONST_SCALAR_INT_P (new_rtx)
5646 62361567 : && GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
5647 0 : return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5648 : else
5649 344196021 : SUBST (XEXP (x, i), new_rtx);
5650 : }
5651 : }
5652 : }
5653 :
5654 : /* Check if we are loading something from the constant pool via float
5655 : extension; in this case we would undo compress_float_constant
5656 : optimization and degenerate constant load to an immediate value. */
5657 222589118 : if (GET_CODE (x) == FLOAT_EXTEND
5658 309945 : && MEM_P (XEXP (x, 0))
5659 222653400 : && MEM_READONLY_P (XEXP (x, 0)))
5660 : {
5661 36754 : rtx tmp = avoid_constant_pool_reference (x);
5662 36754 : if (x != tmp)
5663 : return x;
5664 : }
5665 :
5666 : /* Try to simplify X. If the simplification changed the code, it is likely
5667 : that further simplification will help, so loop, but limit the number
5668 : of repetitions that will be performed. */
5669 :
5670 230887723 : for (i = 0; i < 4; i++)
5671 : {
5672 : /* If X is sufficiently simple, don't bother trying to do anything
5673 : with it. */
5674 230832023 : if (code != CONST_INT && code != REG && code != CLOBBER)
5675 230148823 : x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5676 :
5677 230832023 : if (GET_CODE (x) == code)
5678 : break;
5679 :
5680 8335229 : code = GET_CODE (x);
5681 :
5682 : /* We no longer know the original mode of operand 0 since we
5683 : have changed the form of X) */
5684 8335229 : op0_mode = VOIDmode;
5685 : }
5686 :
5687 : return x;
5688 : }
5689 :
5690 : /* If X is a commutative operation whose operands are not in the canonical
5691 : order, use substitutions to swap them. */
5692 :
5693 : static void
5694 671762748 : maybe_swap_commutative_operands (rtx x)
5695 : {
5696 671762748 : if (COMMUTATIVE_ARITH_P (x)
5697 671762748 : && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5698 : {
5699 3602195 : rtx temp = XEXP (x, 0);
5700 3602195 : SUBST (XEXP (x, 0), XEXP (x, 1));
5701 3602195 : SUBST (XEXP (x, 1), temp);
5702 : }
5703 :
5704 : /* Canonicalize (vec_merge (fma op2 op1 op3) op1 mask) to
5705 : (vec_merge (fma op1 op2 op3) op1 mask). */
5706 671762748 : if (GET_CODE (x) == VEC_MERGE
5707 1118220 : && GET_CODE (XEXP (x, 0)) == FMA)
5708 : {
5709 26537 : rtx fma_op1 = XEXP (XEXP (x, 0), 0);
5710 26537 : rtx fma_op2 = XEXP (XEXP (x, 0), 1);
5711 26537 : rtx masked_op = XEXP (x, 1);
5712 26537 : if (rtx_equal_p (masked_op, fma_op2))
5713 : {
5714 192 : if (GET_CODE (fma_op1) == NEG)
5715 : {
5716 : /* Keep the negate canonicalized to the first operand. */
5717 132 : fma_op1 = XEXP (fma_op1, 0);
5718 132 : SUBST (XEXP (XEXP (XEXP (x, 0), 0), 0), fma_op2);
5719 132 : SUBST (XEXP (XEXP (x, 0), 1), fma_op1);
5720 : }
5721 : else
5722 : {
5723 60 : SUBST (XEXP (XEXP (x, 0), 0), fma_op2);
5724 60 : SUBST (XEXP (XEXP (x, 0), 1), fma_op1);
5725 : }
5726 : }
5727 : }
5728 :
5729 671762748 : unsigned n_elts = 0;
5730 671762748 : if (GET_CODE (x) == VEC_MERGE
5731 1118220 : && CONST_INT_P (XEXP (x, 2))
5732 1470086 : && GET_MODE_NUNITS (GET_MODE (x)).is_constant (&n_elts)
5733 672497791 : && (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1))
5734 : /* Two operands have same precedence, then
5735 : first bit of mask select first operand. */
5736 701749 : || (!swap_commutative_operands_p (XEXP (x, 1), XEXP (x, 0))
5737 163833 : && !(UINTVAL (XEXP (x, 2)) & 1))))
5738 : {
5739 65909 : rtx temp = XEXP (x, 0);
5740 65909 : unsigned HOST_WIDE_INT sel = UINTVAL (XEXP (x, 2));
5741 65909 : unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_1U;
5742 65909 : if (n_elts == HOST_BITS_PER_WIDE_INT)
5743 : mask = -1;
5744 : else
5745 65768 : mask = (HOST_WIDE_INT_1U << n_elts) - 1;
5746 65909 : SUBST (XEXP (x, 0), XEXP (x, 1));
5747 65909 : SUBST (XEXP (x, 1), temp);
5748 65909 : SUBST (XEXP (x, 2), GEN_INT (~sel & mask));
5749 : }
5750 671762748 : }
5751 :
5752 : /* Simplify X, a piece of RTL. We just operate on the expression at the
5753 : outer level; call `subst' to simplify recursively. Return the new
5754 : expression.
5755 :
5756 : OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is true
5757 : if we are inside a SET_DEST. IN_COND is true if we are at the top level
5758 : of a condition. */
5759 :
5760 : static rtx
5761 231118570 : combine_simplify_rtx (rtx x, machine_mode op0_mode, bool in_dest, bool in_cond)
5762 : {
5763 231118570 : enum rtx_code code = GET_CODE (x);
5764 231118570 : machine_mode mode = GET_MODE (x);
5765 231118570 : scalar_int_mode int_mode;
5766 231118570 : rtx temp;
5767 231118570 : int i;
5768 :
5769 : /* If this is a commutative operation, put a constant last and a complex
5770 : expression first. We don't need to do this for comparisons here. */
5771 231118570 : maybe_swap_commutative_operands (x);
5772 :
5773 : /* Try to fold this expression in case we have constants that weren't
5774 : present before. */
5775 231118570 : temp = 0;
5776 231118570 : switch (GET_RTX_CLASS (code))
5777 : {
5778 7760948 : case RTX_UNARY:
5779 7760948 : if (op0_mode == VOIDmode)
5780 171062 : op0_mode = GET_MODE (XEXP (x, 0));
5781 7760948 : temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5782 7760948 : break;
5783 18543028 : case RTX_COMPARE:
5784 18543028 : case RTX_COMM_COMPARE:
5785 18543028 : {
5786 18543028 : machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5787 18543028 : if (cmp_mode == VOIDmode)
5788 : {
5789 49013 : cmp_mode = GET_MODE (XEXP (x, 1));
5790 49013 : if (cmp_mode == VOIDmode)
5791 8211 : cmp_mode = op0_mode;
5792 : }
5793 18543028 : temp = simplify_relational_operation (code, mode, cmp_mode,
5794 : XEXP (x, 0), XEXP (x, 1));
5795 : }
5796 18543028 : break;
5797 91530983 : case RTX_COMM_ARITH:
5798 91530983 : case RTX_BIN_ARITH:
5799 91530983 : temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5800 91530983 : break;
5801 14820150 : case RTX_BITFIELD_OPS:
5802 14820150 : case RTX_TERNARY:
5803 14820150 : temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5804 : XEXP (x, 1), XEXP (x, 2));
5805 14820150 : break;
5806 : default:
5807 : break;
5808 : }
5809 :
5810 132655109 : if (temp)
5811 : {
5812 17104753 : x = temp;
5813 17104753 : code = GET_CODE (temp);
5814 17104753 : op0_mode = VOIDmode;
5815 17104753 : mode = GET_MODE (temp);
5816 : }
5817 :
5818 : /* If this is a simple operation applied to an IF_THEN_ELSE, try
5819 : applying it to the arms of the IF_THEN_ELSE. This often simplifies
5820 : things. Check for cases where both arms are testing the same
5821 : condition.
5822 :
5823 : Don't do anything if all operands are very simple. */
5824 :
5825 231118570 : if ((BINARY_P (x)
5826 109804366 : && ((!OBJECT_P (XEXP (x, 0))
5827 43679900 : && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5828 5396244 : && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5829 69168864 : || (!OBJECT_P (XEXP (x, 1))
5830 5022159 : && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5831 2034190 : && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5832 187206124 : || (UNARY_P (x)
5833 7644801 : && (!OBJECT_P (XEXP (x, 0))
5834 3477030 : && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5835 917900 : && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5836 : {
5837 46564374 : rtx cond, true_rtx, false_rtx;
5838 :
5839 46564374 : cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5840 46564374 : if (cond != 0
5841 : /* If everything is a comparison, what we have is highly unlikely
5842 : to be simpler, so don't use it. */
5843 4704413 : && ! (COMPARISON_P (x)
5844 1278933 : && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5845 : /* Similarly, if we end up with one of the expressions the same
5846 : as the original, it is certainly not simpler. */
5847 4526563 : && ! rtx_equal_p (x, true_rtx)
5848 51090937 : && ! rtx_equal_p (x, false_rtx))
5849 : {
5850 4526563 : rtx cop1 = const0_rtx;
5851 4526563 : enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5852 :
5853 4526563 : if (cond_code == NE && COMPARISON_P (cond))
5854 678657 : return x;
5855 :
5856 : /* Simplify the alternative arms; this may collapse the true and
5857 : false arms to store-flag values. Be careful to use copy_rtx
5858 : here since true_rtx or false_rtx might share RTL with x as a
5859 : result of the if_then_else_cond call above. */
5860 3847906 : true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx,
5861 : false, false, false);
5862 3847906 : false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx,
5863 : false, false, false);
5864 :
5865 : /* If true_rtx and false_rtx are not general_operands, an if_then_else
5866 : is unlikely to be simpler. */
5867 3847906 : if (general_operand (true_rtx, VOIDmode)
5868 3847906 : && general_operand (false_rtx, VOIDmode))
5869 : {
5870 1428076 : enum rtx_code reversed;
5871 :
5872 : /* Restarting if we generate a store-flag expression will cause
5873 : us to loop. Just drop through in this case. */
5874 :
5875 : /* If the result values are STORE_FLAG_VALUE and zero, we can
5876 : just make the comparison operation. */
5877 1428076 : if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5878 628918 : x = simplify_gen_relational (cond_code, mode, VOIDmode,
5879 : cond, cop1);
5880 521422 : else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5881 799158 : && ((reversed = reversed_comparison_code_parts
5882 461372 : (cond_code, cond, cop1, NULL))
5883 : != UNKNOWN))
5884 461372 : x = simplify_gen_relational (reversed, mode, VOIDmode,
5885 : cond, cop1);
5886 :
5887 : /* Likewise, we can make the negate of a comparison operation
5888 : if the result values are - STORE_FLAG_VALUE and zero. */
5889 337786 : else if (CONST_INT_P (true_rtx)
5890 237048 : && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5891 46404 : && false_rtx == const0_rtx)
5892 44680 : x = simplify_gen_unary (NEG, mode,
5893 : simplify_gen_relational (cond_code,
5894 : mode, VOIDmode,
5895 : cond, cop1),
5896 : mode);
5897 293106 : else if (CONST_INT_P (false_rtx)
5898 228321 : && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5899 24500 : && true_rtx == const0_rtx
5900 293106 : && ((reversed = reversed_comparison_code_parts
5901 21614 : (cond_code, cond, cop1, NULL))
5902 : != UNKNOWN))
5903 21611 : x = simplify_gen_unary (NEG, mode,
5904 : simplify_gen_relational (reversed,
5905 : mode, VOIDmode,
5906 : cond, cop1),
5907 : mode);
5908 :
5909 1428076 : code = GET_CODE (x);
5910 1428076 : op0_mode = VOIDmode;
5911 : }
5912 : }
5913 : }
5914 :
5915 : /* First see if we can apply the inverse distributive law. */
5916 230439913 : if (code == PLUS || code == MINUS
5917 230439913 : || code == AND || code == IOR || code == XOR)
5918 : {
5919 52094112 : x = apply_distributive_law (x);
5920 52094112 : code = GET_CODE (x);
5921 52094112 : op0_mode = VOIDmode;
5922 : }
5923 :
5924 : /* If CODE is an associative operation not otherwise handled, see if we
5925 : can associate some operands. This can win if they are constants or
5926 : if they are logically related (i.e. (a & b) & a). */
5927 230439913 : if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5928 : || code == AND || code == IOR || code == XOR
5929 : || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5930 56817456 : && ((INTEGRAL_MODE_P (mode) && code != DIV)
5931 4802267 : || (flag_associative_math && FLOAT_MODE_P (mode))))
5932 : {
5933 52658395 : if (GET_CODE (XEXP (x, 0)) == code)
5934 : {
5935 4140583 : rtx other = XEXP (XEXP (x, 0), 0);
5936 4140583 : rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5937 4140583 : rtx inner_op1 = XEXP (x, 1);
5938 4140583 : rtx inner;
5939 :
5940 : /* Make sure we pass the constant operand if any as the second
5941 : one if this is a commutative operation. */
5942 4140583 : if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5943 : std::swap (inner_op0, inner_op1);
5944 4140583 : inner = simplify_binary_operation (code == MINUS ? PLUS
5945 4033723 : : code == DIV ? MULT
5946 : : code,
5947 : mode, inner_op0, inner_op1);
5948 :
5949 : /* For commutative operations, try the other pair if that one
5950 : didn't simplify. */
5951 4140583 : if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5952 : {
5953 4003799 : other = XEXP (XEXP (x, 0), 1);
5954 4003799 : inner = simplify_binary_operation (code, mode,
5955 : XEXP (XEXP (x, 0), 0),
5956 : XEXP (x, 1));
5957 : }
5958 :
5959 4106612 : if (inner)
5960 250217 : return simplify_gen_binary (code, mode, other, inner);
5961 : }
5962 : }
5963 :
5964 : /* A little bit of algebraic simplification here. */
5965 230189696 : switch (code)
5966 : {
5967 22760725 : case PREFETCH:
5968 : /* A prefetch reaches memory through an address, and targets recognize
5969 : that address with the same predicates they use for a MEM, so it
5970 : needs the same treatment. */
5971 22760725 : case MEM:
5972 : /* Ensure that our address has any ASHIFTs converted to MULT in case
5973 : address-recognizing predicates are called later. */
5974 22760725 : temp = make_compound_operation (XEXP (x, 0), MEM);
5975 22760725 : SUBST (XEXP (x, 0), temp);
5976 22760725 : break;
5977 :
5978 9901029 : case SUBREG:
5979 9901029 : if (op0_mode == VOIDmode)
5980 162592 : op0_mode = GET_MODE (SUBREG_REG (x));
5981 :
5982 : /* See if this can be moved to simplify_subreg. */
5983 9901029 : if (CONSTANT_P (SUBREG_REG (x))
5984 46402 : && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5985 : /* Don't call gen_lowpart if the inner mode
5986 : is VOIDmode and we cannot simplify it, as SUBREG without
5987 : inner mode is invalid. */
5988 9924174 : && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5989 0 : || gen_lowpart_common (mode, SUBREG_REG (x))))
5990 23145 : return gen_lowpart (mode, SUBREG_REG (x));
5991 :
5992 9877884 : if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5993 : break;
5994 9877884 : {
5995 9877884 : rtx temp;
5996 19755768 : temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5997 9877884 : SUBREG_BYTE (x));
5998 9877884 : if (temp)
5999 231118570 : return temp;
6000 :
6001 : /* If op is known to have all lower bits zero, the result is zero. */
6002 9218986 : scalar_int_mode int_mode, int_op0_mode;
6003 9218986 : if (!in_dest
6004 5655050 : && is_a <scalar_int_mode> (mode, &int_mode)
6005 5526725 : && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
6006 5526725 : && (GET_MODE_PRECISION (int_mode)
6007 5526725 : < GET_MODE_PRECISION (int_op0_mode))
6008 4970249 : && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
6009 : SUBREG_BYTE (x))
6010 4455233 : && HWI_COMPUTABLE_MODE_P (int_op0_mode)
6011 4237558 : && ((nonzero_bits (SUBREG_REG (x), int_op0_mode)
6012 4237558 : & GET_MODE_MASK (int_mode)) == 0)
6013 9219991 : && !side_effects_p (SUBREG_REG (x)))
6014 1005 : return CONST0_RTX (int_mode);
6015 : }
6016 :
6017 : /* Don't change the mode of the MEM if that would change the meaning
6018 : of the address. */
6019 9217981 : if (MEM_P (SUBREG_REG (x))
6020 9217981 : && (MEM_VOLATILE_P (SUBREG_REG (x))
6021 70370 : || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
6022 70408 : MEM_ADDR_SPACE (SUBREG_REG (x)))))
6023 45677 : return gen_rtx_CLOBBER (mode, const0_rtx);
6024 :
6025 : /* Note that we cannot do any narrowing for non-constants since
6026 : we might have been counting on using the fact that some bits were
6027 : zero. We now do this in the SET. */
6028 :
6029 : break;
6030 :
6031 397755 : case NEG:
6032 397755 : temp = expand_compound_operation (XEXP (x, 0));
6033 :
6034 : /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
6035 : replaced by (lshiftrt X C). This will convert
6036 : (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
6037 :
6038 397755 : if (GET_CODE (temp) == ASHIFTRT
6039 16472 : && CONST_INT_P (XEXP (temp, 1))
6040 430625 : && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
6041 0 : return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
6042 0 : INTVAL (XEXP (temp, 1)));
6043 :
6044 : /* If X has only a single bit that might be nonzero, say, bit I, convert
6045 : (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
6046 : MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
6047 : (sign_extract X 1 Y). But only do this if TEMP isn't a register
6048 : or a SUBREG of one since we'd be making the expression more
6049 : complex if it was just a register. */
6050 :
6051 397755 : if (!REG_P (temp)
6052 192361 : && ! (GET_CODE (temp) == SUBREG
6053 20435 : && REG_P (SUBREG_REG (temp)))
6054 231264381 : && is_a <scalar_int_mode> (mode, &int_mode)
6055 543566 : && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
6056 : {
6057 67292 : rtx temp1 = simplify_shift_const
6058 67292 : (NULL_RTX, ASHIFTRT, int_mode,
6059 : simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
6060 67292 : GET_MODE_PRECISION (int_mode) - 1 - i),
6061 67292 : GET_MODE_PRECISION (int_mode) - 1 - i);
6062 :
6063 : /* If all we did was surround TEMP with the two shifts, we
6064 : haven't improved anything, so don't use it. Otherwise,
6065 : we are better off with TEMP1. */
6066 67292 : if (GET_CODE (temp1) != ASHIFTRT
6067 66716 : || GET_CODE (XEXP (temp1, 0)) != ASHIFT
6068 66716 : || XEXP (XEXP (temp1, 0), 0) != temp)
6069 7037 : return temp1;
6070 : }
6071 : break;
6072 :
6073 9777 : case TRUNCATE:
6074 : /* We can't handle truncation to a partial integer mode here
6075 : because we don't know the real bitsize of the partial
6076 : integer mode. */
6077 9777 : if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6078 : break;
6079 :
6080 9777 : if (HWI_COMPUTABLE_MODE_P (mode))
6081 0 : SUBST (XEXP (x, 0),
6082 : force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6083 : GET_MODE_MASK (mode), false));
6084 :
6085 : /* We can truncate a constant value and return it. */
6086 9777 : {
6087 9777 : poly_int64 c;
6088 9777 : if (poly_int_rtx_p (XEXP (x, 0), &c))
6089 0 : return gen_int_mode (c, mode);
6090 : }
6091 :
6092 : /* Similarly to what we do in simplify-rtx.cc, a truncate of a register
6093 : whose value is a comparison can be replaced with a subreg if
6094 : STORE_FLAG_VALUE permits. */
6095 9777 : if (HWI_COMPUTABLE_MODE_P (mode)
6096 0 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
6097 0 : && (temp = get_last_value (XEXP (x, 0)))
6098 0 : && COMPARISON_P (temp)
6099 9777 : && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (XEXP (x, 0))))
6100 0 : return gen_lowpart (mode, XEXP (x, 0));
6101 : break;
6102 :
6103 5466 : case CONST:
6104 : /* (const (const X)) can become (const X). Do it this way rather than
6105 : returning the inner CONST since CONST can be shared with a
6106 : REG_EQUAL note. */
6107 5466 : if (GET_CODE (XEXP (x, 0)) == CONST)
6108 0 : SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
6109 : break;
6110 :
6111 : case LO_SUM:
6112 : /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
6113 : can add in an offset. find_split_point will split this address up
6114 : again if it doesn't match. */
6115 : if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6116 : && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6117 : return XEXP (x, 1);
6118 : break;
6119 :
6120 35308096 : case PLUS:
6121 : /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6122 : when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6123 : bit-field and can be replaced by either a sign_extend or a
6124 : sign_extract. The `and' may be a zero_extend and the two
6125 : <c>, -<c> constants may be reversed. */
6126 35308096 : if (GET_CODE (XEXP (x, 0)) == XOR
6127 35308096 : && is_a <scalar_int_mode> (mode, &int_mode)
6128 14967 : && CONST_INT_P (XEXP (x, 1))
6129 4557 : && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6130 3898 : && UINTVAL (XEXP (x, 1)) == -UINTVAL (XEXP (XEXP (x, 0), 1))
6131 37 : && ((INTVAL (XEXP (XEXP (x, 0), 1)) > 0
6132 36 : && (i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0)
6133 1 : || (INTVAL (XEXP (x, 1)) > 0
6134 1 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0))
6135 37 : && HWI_COMPUTABLE_MODE_P (int_mode)
6136 35308133 : && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6137 0 : && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6138 0 : && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6139 0 : == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6140 37 : || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6141 0 : && known_eq ((GET_MODE_PRECISION
6142 : (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6143 : (unsigned int) i + 1))))
6144 0 : return simplify_shift_const
6145 0 : (NULL_RTX, ASHIFTRT, int_mode,
6146 : simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6147 : XEXP (XEXP (XEXP (x, 0), 0), 0),
6148 0 : GET_MODE_PRECISION (int_mode) - (i + 1)),
6149 0 : GET_MODE_PRECISION (int_mode) - (i + 1));
6150 :
6151 : /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6152 : can become (ashiftrt (ashift (xor x 1) C) C) where C is
6153 : the bitsize of the mode - 1. This allows simplification of
6154 : "a = (b & 8) == 0;" */
6155 35308096 : if (XEXP (x, 1) == constm1_rtx
6156 749767 : && !REG_P (XEXP (x, 0))
6157 330501 : && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6158 35573 : && REG_P (SUBREG_REG (XEXP (x, 0))))
6159 35599442 : && is_a <scalar_int_mode> (mode, &int_mode)
6160 35609340 : && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6161 9898 : return simplify_shift_const
6162 9898 : (NULL_RTX, ASHIFTRT, int_mode,
6163 : simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6164 : gen_rtx_XOR (int_mode, XEXP (x, 0),
6165 : const1_rtx),
6166 9898 : GET_MODE_PRECISION (int_mode) - 1),
6167 19796 : GET_MODE_PRECISION (int_mode) - 1);
6168 :
6169 : /* If we are adding two things that have no bits in common, convert
6170 : the addition into an IOR. This will often be further simplified,
6171 : for example in cases like ((a & 1) + (a & 2)), which can
6172 : become a & 3. */
6173 :
6174 35298198 : if (HWI_COMPUTABLE_MODE_P (mode)
6175 31367057 : && (nonzero_bits (XEXP (x, 0), mode)
6176 31367057 : & nonzero_bits (XEXP (x, 1), mode)) == 0)
6177 : {
6178 : /* Try to simplify the expression further. */
6179 969747 : rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6180 969747 : temp = combine_simplify_rtx (tor, VOIDmode, in_dest, false);
6181 :
6182 : /* If we could, great. If not, do not go ahead with the IOR
6183 : replacement, since PLUS appears in many special purpose
6184 : address arithmetic instructions. */
6185 969747 : if (GET_CODE (temp) != CLOBBER
6186 969747 : && (GET_CODE (temp) != IOR
6187 964050 : || ((XEXP (temp, 0) != XEXP (x, 0)
6188 953128 : || XEXP (temp, 1) != XEXP (x, 1))
6189 10922 : && (XEXP (temp, 0) != XEXP (x, 1)
6190 77 : || XEXP (temp, 1) != XEXP (x, 0)))))
6191 : return temp;
6192 : }
6193 :
6194 : /* Canonicalize x + x into x << 1. */
6195 35281656 : if (GET_MODE_CLASS (mode) == MODE_INT
6196 31688666 : && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6197 35284960 : && !side_effects_p (XEXP (x, 0)))
6198 3275 : return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6199 :
6200 : break;
6201 :
6202 4226426 : case MINUS:
6203 : /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6204 : (and <foo> (const_int pow2-1)) */
6205 4226426 : if (is_a <scalar_int_mode> (mode, &int_mode)
6206 3570362 : && GET_CODE (XEXP (x, 1)) == AND
6207 97574 : && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6208 94809 : && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6209 48662 : && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6210 0 : return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6211 0 : -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6212 : break;
6213 :
6214 3380138 : case MULT:
6215 : /* If we have (mult (plus A B) C), apply the distributive law and then
6216 : the inverse distributive law to see if things simplify. This
6217 : occurs mostly in addresses, often when unrolling loops. */
6218 :
6219 3380138 : if (GET_CODE (XEXP (x, 0)) == PLUS)
6220 : {
6221 291424 : rtx result = distribute_and_simplify_rtx (x, 0);
6222 291424 : if (result)
6223 : return result;
6224 : }
6225 :
6226 : /* Try simplify a*(b/c) as (a*b)/c. */
6227 3379310 : if (FLOAT_MODE_P (mode) && flag_associative_math
6228 205299 : && GET_CODE (XEXP (x, 0)) == DIV)
6229 : {
6230 249 : rtx tem = simplify_binary_operation (MULT, mode,
6231 : XEXP (XEXP (x, 0), 0),
6232 : XEXP (x, 1));
6233 249 : if (tem)
6234 33 : return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6235 : }
6236 : break;
6237 :
6238 124559 : case UDIV:
6239 : /* If this is a divide by a power of two, treat it as a shift if
6240 : its first operand is a shift. */
6241 124559 : if (is_a <scalar_int_mode> (mode, &int_mode)
6242 124559 : && CONST_INT_P (XEXP (x, 1))
6243 2020 : && INTVAL (XEXP (x, 1)) > 0
6244 1064 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6245 0 : && (GET_CODE (XEXP (x, 0)) == ASHIFT
6246 0 : || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6247 0 : || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6248 0 : || GET_CODE (XEXP (x, 0)) == ROTATE
6249 0 : || GET_CODE (XEXP (x, 0)) == ROTATERT))
6250 0 : return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6251 0 : XEXP (x, 0), i);
6252 : break;
6253 :
6254 18465102 : case EQ: case NE:
6255 18465102 : case GT: case GTU: case GE: case GEU:
6256 18465102 : case LT: case LTU: case LE: case LEU:
6257 18465102 : case UNEQ: case LTGT:
6258 18465102 : case UNGT: case UNGE:
6259 18465102 : case UNLT: case UNLE:
6260 18465102 : case UNORDERED: case ORDERED:
6261 : /* If the first operand is a condition code, we can't do anything
6262 : with it. */
6263 18465102 : if (GET_CODE (XEXP (x, 0)) == COMPARE
6264 18465102 : || GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC)
6265 : {
6266 13862294 : rtx op0 = XEXP (x, 0);
6267 13862294 : rtx op1 = XEXP (x, 1);
6268 13862294 : enum rtx_code new_code;
6269 :
6270 13862294 : if (GET_CODE (op0) == COMPARE)
6271 0 : op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6272 :
6273 : /* Simplify our comparison, if possible. */
6274 13862294 : new_code = simplify_comparison (code, &op0, &op1);
6275 :
6276 : /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6277 : if only the low-order bit is possibly nonzero in X (such as when
6278 : X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6279 : (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6280 : known to be either 0 or -1, NE becomes a NEG and EQ becomes
6281 : (plus X 1).
6282 :
6283 : Remove any ZERO_EXTRACT we made when thinking this was a
6284 : comparison. It may now be simpler to use, e.g., an AND. If a
6285 : ZERO_EXTRACT is indeed appropriate, it will be placed back by
6286 : the call to make_compound_operation in the SET case.
6287 :
6288 : Don't apply these optimizations if the caller would
6289 : prefer a comparison rather than a value.
6290 : E.g., for the condition in an IF_THEN_ELSE most targets need
6291 : an explicit comparison. */
6292 :
6293 13862294 : if (in_cond)
6294 : ;
6295 :
6296 2202213 : else if (STORE_FLAG_VALUE == 1
6297 : && new_code == NE
6298 2654005 : && is_int_mode (mode, &int_mode)
6299 452008 : && op1 == const0_rtx
6300 235173 : && int_mode == GET_MODE (op0)
6301 2300398 : && nonzero_bits (op0, int_mode) == 1)
6302 216 : return gen_lowpart (int_mode,
6303 482412 : expand_compound_operation (op0));
6304 :
6305 2201997 : else if (STORE_FLAG_VALUE == 1
6306 : && new_code == NE
6307 2652762 : && is_int_mode (mode, &int_mode)
6308 451792 : && op1 == const0_rtx
6309 234957 : && int_mode == GET_MODE (op0)
6310 2299966 : && (num_sign_bit_copies (op0, int_mode)
6311 97969 : == GET_MODE_PRECISION (int_mode)))
6312 : {
6313 1027 : op0 = expand_compound_operation (op0);
6314 1027 : return simplify_gen_unary (NEG, int_mode,
6315 1027 : gen_lowpart (int_mode, op0),
6316 1027 : int_mode);
6317 : }
6318 :
6319 2200970 : else if (STORE_FLAG_VALUE == 1
6320 : && new_code == EQ
6321 2544864 : && is_int_mode (mode, &int_mode)
6322 345907 : && op1 == const0_rtx
6323 138811 : && int_mode == GET_MODE (op0)
6324 2246504 : && nonzero_bits (op0, int_mode) == 1)
6325 : {
6326 2013 : op0 = expand_compound_operation (op0);
6327 2013 : return simplify_gen_binary (XOR, int_mode,
6328 2013 : gen_lowpart (int_mode, op0),
6329 2013 : const1_rtx);
6330 : }
6331 :
6332 2198957 : else if (STORE_FLAG_VALUE == 1
6333 : && new_code == EQ
6334 14202368 : && is_int_mode (mode, &int_mode)
6335 343894 : && op1 == const0_rtx
6336 136798 : && int_mode == GET_MODE (op0)
6337 2242478 : && (num_sign_bit_copies (op0, int_mode)
6338 43521 : == GET_MODE_PRECISION (int_mode)))
6339 : {
6340 564 : op0 = expand_compound_operation (op0);
6341 564 : return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6342 : }
6343 :
6344 : /* If STORE_FLAG_VALUE is -1, we have cases similar to
6345 : those above. */
6346 13858474 : if (in_cond)
6347 : ;
6348 :
6349 13858474 : else if (STORE_FLAG_VALUE == -1
6350 : && new_code == NE
6351 : && is_int_mode (mode, &int_mode)
6352 : && op1 == const0_rtx
6353 : && int_mode == GET_MODE (op0)
6354 : && (num_sign_bit_copies (op0, int_mode)
6355 : == GET_MODE_PRECISION (int_mode)))
6356 : return gen_lowpart (int_mode, expand_compound_operation (op0));
6357 :
6358 13858474 : else if (STORE_FLAG_VALUE == -1
6359 : && new_code == NE
6360 : && is_int_mode (mode, &int_mode)
6361 : && op1 == const0_rtx
6362 : && int_mode == GET_MODE (op0)
6363 : && nonzero_bits (op0, int_mode) == 1)
6364 : {
6365 : op0 = expand_compound_operation (op0);
6366 : return simplify_gen_unary (NEG, int_mode,
6367 : gen_lowpart (int_mode, op0),
6368 : int_mode);
6369 : }
6370 :
6371 13858474 : else if (STORE_FLAG_VALUE == -1
6372 : && new_code == EQ
6373 : && is_int_mode (mode, &int_mode)
6374 : && op1 == const0_rtx
6375 : && int_mode == GET_MODE (op0)
6376 : && (num_sign_bit_copies (op0, int_mode)
6377 : == GET_MODE_PRECISION (int_mode)))
6378 : {
6379 : op0 = expand_compound_operation (op0);
6380 : return simplify_gen_unary (NOT, int_mode,
6381 : gen_lowpart (int_mode, op0),
6382 : int_mode);
6383 : }
6384 :
6385 : /* If X is 0/1, (eq X 0) is X-1. */
6386 13858474 : else if (STORE_FLAG_VALUE == -1
6387 : && new_code == EQ
6388 : && is_int_mode (mode, &int_mode)
6389 : && op1 == const0_rtx
6390 : && int_mode == GET_MODE (op0)
6391 : && nonzero_bits (op0, int_mode) == 1)
6392 : {
6393 : op0 = expand_compound_operation (op0);
6394 : return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6395 : }
6396 :
6397 : /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6398 : one bit that might be nonzero, we can convert (ne x 0) to
6399 : (ashift x c) where C puts the bit in the sign bit. Remove any
6400 : AND with STORE_FLAG_VALUE when we are done, since we are only
6401 : going to test the sign bit. */
6402 13858474 : if (new_code == NE
6403 14305000 : && is_int_mode (mode, &int_mode)
6404 450838 : && HWI_COMPUTABLE_MODE_P (int_mode)
6405 446526 : && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6406 0 : && op1 == const0_rtx
6407 0 : && int_mode == GET_MODE (op0)
6408 13858474 : && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6409 : {
6410 0 : x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6411 : expand_compound_operation (op0),
6412 0 : GET_MODE_PRECISION (int_mode) - 1 - i);
6413 0 : if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6414 0 : return XEXP (x, 0);
6415 : else
6416 : return x;
6417 : }
6418 :
6419 : /* If the code changed, return a whole new comparison.
6420 : We also need to avoid using SUBST in cases where
6421 : simplify_comparison has widened a comparison with a CONST_INT,
6422 : since in that case the wider CONST_INT may fail the sanity
6423 : checks in do_SUBST. */
6424 13858474 : if (new_code != code
6425 13387806 : || (CONST_INT_P (op1)
6426 7668890 : && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6427 9335 : && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6428 478592 : return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6429 :
6430 : /* Otherwise, keep this operation, but maybe change its operands.
6431 : This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6432 13379882 : SUBST (XEXP (x, 0), op0);
6433 13379882 : SUBST (XEXP (x, 1), op1);
6434 : }
6435 : break;
6436 :
6437 13515810 : case IF_THEN_ELSE:
6438 13515810 : return simplify_if_then_else (x);
6439 :
6440 5021655 : case ZERO_EXTRACT:
6441 5021655 : case SIGN_EXTRACT:
6442 5021655 : case ZERO_EXTEND:
6443 5021655 : case SIGN_EXTEND:
6444 : /* If we are processing SET_DEST, we are done. */
6445 5021655 : if (in_dest)
6446 : return x;
6447 :
6448 5018873 : return expand_compound_operation (x);
6449 :
6450 48436526 : case SET:
6451 48436526 : return simplify_set (x);
6452 :
6453 11626599 : case AND:
6454 11626599 : case IOR:
6455 11626599 : return simplify_logical (x);
6456 :
6457 13819632 : case ASHIFT:
6458 13819632 : case LSHIFTRT:
6459 13819632 : case ASHIFTRT:
6460 13819632 : case ROTATE:
6461 13819632 : case ROTATERT:
6462 : /* If this is a shift by a constant amount, simplify it. */
6463 13819632 : if (CONST_INT_P (XEXP (x, 1)))
6464 13309730 : return simplify_shift_const (x, code, mode, XEXP (x, 0),
6465 13309730 : INTVAL (XEXP (x, 1)));
6466 :
6467 : else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6468 : SUBST (XEXP (x, 1),
6469 : force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6470 : (HOST_WIDE_INT_1U
6471 : << exact_log2 (GET_MODE_UNIT_BITSIZE
6472 : (GET_MODE (x)))) - 1, false));
6473 : break;
6474 2055937 : case VEC_SELECT:
6475 2055937 : {
6476 2055937 : rtx trueop0 = XEXP (x, 0);
6477 2055937 : mode = GET_MODE (trueop0);
6478 2055937 : rtx trueop1 = XEXP (x, 1);
6479 : /* If we select a low-part subreg, return that. */
6480 2055937 : if (vec_series_lowpart_p (GET_MODE (x), mode, trueop1))
6481 : {
6482 1158 : rtx new_rtx = lowpart_subreg (GET_MODE (x), trueop0, mode);
6483 1158 : if (new_rtx != NULL_RTX)
6484 1158 : return new_rtx;
6485 : }
6486 : }
6487 :
6488 : default:
6489 : break;
6490 : }
6491 :
6492 : return x;
6493 : }
6494 :
6495 : /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6496 :
6497 : static rtx
6498 13515810 : simplify_if_then_else (rtx x)
6499 : {
6500 13515810 : machine_mode mode = GET_MODE (x);
6501 13515810 : rtx cond = XEXP (x, 0);
6502 13515810 : rtx true_rtx = XEXP (x, 1);
6503 13515810 : rtx false_rtx = XEXP (x, 2);
6504 13515810 : enum rtx_code true_code = GET_CODE (cond);
6505 13515810 : bool comparison_p = COMPARISON_P (cond);
6506 13515810 : rtx temp;
6507 13515810 : int i;
6508 13515810 : enum rtx_code false_code;
6509 13515810 : rtx reversed;
6510 13515810 : scalar_int_mode int_mode, inner_mode;
6511 :
6512 : /* Simplify storing of the truth value. */
6513 13515810 : if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6514 0 : return simplify_gen_relational (true_code, mode, VOIDmode,
6515 0 : XEXP (cond, 0), XEXP (cond, 1));
6516 :
6517 : /* Also when the truth value has to be reversed. */
6518 13515260 : if (comparison_p
6519 13515260 : && true_rtx == const0_rtx && false_rtx == const_true_rtx
6520 0 : && (reversed = reversed_comparison (cond, mode)))
6521 : return reversed;
6522 :
6523 : /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6524 : in it is being compared against certain values. Get the true and false
6525 : comparisons and see if that says anything about the value of each arm. */
6526 :
6527 13515810 : if (comparison_p
6528 13515260 : && ((false_code = reversed_comparison_code (cond, NULL))
6529 : != UNKNOWN)
6530 26869715 : && REG_P (XEXP (cond, 0)))
6531 : {
6532 8364790 : HOST_WIDE_INT nzb;
6533 8364790 : rtx from = XEXP (cond, 0);
6534 8364790 : rtx true_val = XEXP (cond, 1);
6535 8364790 : rtx false_val = true_val;
6536 8364790 : bool swapped = false;
6537 :
6538 : /* If FALSE_CODE is EQ, swap the codes and arms. */
6539 :
6540 8364790 : if (false_code == EQ)
6541 : {
6542 3063789 : swapped = true, true_code = EQ, false_code = NE;
6543 3063789 : std::swap (true_rtx, false_rtx);
6544 : }
6545 :
6546 8364790 : scalar_int_mode from_mode;
6547 8364790 : if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6548 : {
6549 : /* If we are comparing against zero and the expression being
6550 : tested has only a single bit that might be nonzero, that is
6551 : its value when it is not equal to zero. Similarly if it is
6552 : known to be -1 or 0. */
6553 6915688 : if (true_code == EQ
6554 5014512 : && true_val == const0_rtx
6555 9016224 : && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6556 : {
6557 240677 : false_code = EQ;
6558 240677 : false_val = gen_int_mode (nzb, from_mode);
6559 : }
6560 6675011 : else if (true_code == EQ
6561 4773835 : && true_val == const0_rtx
6562 8534870 : && (num_sign_bit_copies (from, from_mode)
6563 1859859 : == GET_MODE_PRECISION (from_mode)))
6564 : {
6565 714 : false_code = EQ;
6566 714 : false_val = constm1_rtx;
6567 : }
6568 : }
6569 :
6570 : /* Now simplify an arm if we know the value of the register in the
6571 : branch and it is used in the arm. Be careful due to the potential
6572 : of locally-shared RTL. */
6573 :
6574 8364790 : if (reg_mentioned_p (from, true_rtx))
6575 320465 : true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6576 : from, true_val),
6577 : pc_rtx, pc_rtx, false, false, false);
6578 8364790 : if (reg_mentioned_p (from, false_rtx))
6579 136555 : false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6580 : from, false_val),
6581 : pc_rtx, pc_rtx, false, false, false);
6582 :
6583 13665791 : SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6584 13665791 : SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6585 :
6586 8364790 : true_rtx = XEXP (x, 1);
6587 8364790 : false_rtx = XEXP (x, 2);
6588 8364790 : true_code = GET_CODE (cond);
6589 : }
6590 :
6591 : /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6592 : reversed, do so to avoid needing two sets of patterns for
6593 : subtract-and-branch insns. Similarly if we have a constant in the true
6594 : arm, the false arm is the same as the first operand of the comparison, or
6595 : the false arm is more complicated than the true arm. */
6596 :
6597 13515810 : if (comparison_p
6598 13515260 : && reversed_comparison_code (cond, NULL) != UNKNOWN
6599 26869715 : && (true_rtx == pc_rtx
6600 13353905 : || (CONSTANT_P (true_rtx)
6601 10920678 : && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6602 13312431 : || true_rtx == const0_rtx
6603 13309443 : || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6604 13242182 : || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6605 19895 : && !OBJECT_P (false_rtx))
6606 13238293 : || reg_mentioned_p (true_rtx, false_rtx)
6607 13238193 : || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6608 : {
6609 170049 : SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6610 170049 : SUBST (XEXP (x, 1), false_rtx);
6611 170049 : SUBST (XEXP (x, 2), true_rtx);
6612 :
6613 170049 : std::swap (true_rtx, false_rtx);
6614 170049 : cond = XEXP (x, 0);
6615 :
6616 : /* It is possible that the conditional has been simplified out. */
6617 170049 : true_code = GET_CODE (cond);
6618 170049 : comparison_p = COMPARISON_P (cond);
6619 : }
6620 :
6621 : /* If the two arms are identical, we don't need the comparison. */
6622 :
6623 13515810 : if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6624 : return true_rtx;
6625 :
6626 : /* Convert a == b ? b : a to "a". */
6627 3912945 : if (true_code == EQ && ! side_effects_p (cond)
6628 3893662 : && !HONOR_NANS (mode)
6629 3890780 : && !HONOR_SIGNED_ZEROS (mode)
6630 3890780 : && rtx_equal_p (XEXP (cond, 0), false_rtx)
6631 13516299 : && rtx_equal_p (XEXP (cond, 1), true_rtx))
6632 : return false_rtx;
6633 4930252 : else if (true_code == NE && ! side_effects_p (cond)
6634 4881048 : && !HONOR_NANS (mode)
6635 4732543 : && !HONOR_SIGNED_ZEROS (mode)
6636 4732543 : && rtx_equal_p (XEXP (cond, 0), true_rtx)
6637 13582817 : && rtx_equal_p (XEXP (cond, 1), false_rtx))
6638 : return true_rtx;
6639 :
6640 : /* Look for cases where we have (abs x) or (neg (abs X)). */
6641 :
6642 13515792 : if (GET_MODE_CLASS (mode) == MODE_INT
6643 2248749 : && comparison_p
6644 2248736 : && XEXP (cond, 1) == const0_rtx
6645 1720930 : && GET_CODE (false_rtx) == NEG
6646 141 : && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6647 26 : && rtx_equal_p (true_rtx, XEXP (cond, 0))
6648 13515818 : && ! side_effects_p (true_rtx))
6649 26 : switch (true_code)
6650 : {
6651 26 : case GT:
6652 26 : case GE:
6653 26 : return simplify_gen_unary (ABS, mode, true_rtx, mode);
6654 0 : case LT:
6655 0 : case LE:
6656 0 : return
6657 0 : simplify_gen_unary (NEG, mode,
6658 : simplify_gen_unary (ABS, mode, true_rtx, mode),
6659 0 : mode);
6660 : default:
6661 : break;
6662 : }
6663 :
6664 : /* Look for MIN or MAX. */
6665 :
6666 13515766 : if ((! FLOAT_MODE_P (mode)
6667 165198 : || (flag_unsafe_math_optimizations
6668 476 : && !HONOR_NANS (mode)
6669 476 : && !HONOR_SIGNED_ZEROS (mode)))
6670 13351044 : && comparison_p
6671 13350660 : && rtx_equal_p (XEXP (cond, 0), true_rtx)
6672 138075 : && rtx_equal_p (XEXP (cond, 1), false_rtx)
6673 13617 : && ! side_effects_p (cond))
6674 13613 : switch (true_code)
6675 : {
6676 5137 : case GE:
6677 5137 : case GT:
6678 5137 : return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6679 4768 : case LE:
6680 4768 : case LT:
6681 4768 : return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6682 2801 : case GEU:
6683 2801 : case GTU:
6684 2801 : return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6685 907 : case LEU:
6686 907 : case LTU:
6687 907 : return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6688 : default:
6689 : break;
6690 : }
6691 :
6692 : /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6693 : second operand is zero, this can be done as (OP Z (mult COND C2)) where
6694 : C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6695 : SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6696 : We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6697 : neither 1 or -1, but it isn't worth checking for. */
6698 :
6699 13502153 : if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6700 : && comparison_p
6701 15646609 : && is_int_mode (mode, &int_mode)
6702 15737272 : && ! side_effects_p (x))
6703 : {
6704 2230764 : rtx t = make_compound_operation (true_rtx, SET);
6705 2230764 : rtx f = make_compound_operation (false_rtx, SET);
6706 2230764 : rtx cond_op0 = XEXP (cond, 0);
6707 2230764 : rtx cond_op1 = XEXP (cond, 1);
6708 2230764 : enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6709 2230764 : scalar_int_mode m = int_mode;
6710 2230764 : rtx z = 0, c1 = NULL_RTX;
6711 :
6712 2230764 : if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6713 : || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6714 : || GET_CODE (t) == ASHIFT
6715 : || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6716 243892 : && rtx_equal_p (XEXP (t, 0), f))
6717 79953 : c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6718 :
6719 : /* If an identity-zero op is commutative, check whether there
6720 : would be a match if we swapped the operands. */
6721 2064234 : else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6722 2054066 : || GET_CODE (t) == XOR)
6723 2164129 : && rtx_equal_p (XEXP (t, 1), f))
6724 10710 : c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6725 2140101 : else if (GET_CODE (t) == SIGN_EXTEND
6726 4656 : && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6727 4656 : && (GET_CODE (XEXP (t, 0)) == PLUS
6728 4656 : || GET_CODE (XEXP (t, 0)) == MINUS
6729 : || GET_CODE (XEXP (t, 0)) == IOR
6730 : || GET_CODE (XEXP (t, 0)) == XOR
6731 : || GET_CODE (XEXP (t, 0)) == ASHIFT
6732 : || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6733 : || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6734 173 : && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6735 88 : && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6736 88 : && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6737 2140101 : && (num_sign_bit_copies (f, GET_MODE (f))
6738 0 : > (unsigned int)
6739 0 : (GET_MODE_PRECISION (int_mode)
6740 0 : - GET_MODE_PRECISION (inner_mode))))
6741 : {
6742 0 : c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6743 0 : extend_op = SIGN_EXTEND;
6744 0 : m = inner_mode;
6745 : }
6746 2140101 : else if (GET_CODE (t) == SIGN_EXTEND
6747 4656 : && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6748 4656 : && (GET_CODE (XEXP (t, 0)) == PLUS
6749 4513 : || GET_CODE (XEXP (t, 0)) == IOR
6750 4509 : || GET_CODE (XEXP (t, 0)) == XOR)
6751 147 : && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6752 4 : && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6753 4 : && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6754 2140105 : && (num_sign_bit_copies (f, GET_MODE (f))
6755 4 : > (unsigned int)
6756 4 : (GET_MODE_PRECISION (int_mode)
6757 4 : - GET_MODE_PRECISION (inner_mode))))
6758 : {
6759 0 : c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6760 0 : extend_op = SIGN_EXTEND;
6761 0 : m = inner_mode;
6762 : }
6763 2140101 : else if (GET_CODE (t) == ZERO_EXTEND
6764 5190 : && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6765 5190 : && (GET_CODE (XEXP (t, 0)) == PLUS
6766 5190 : || GET_CODE (XEXP (t, 0)) == MINUS
6767 : || GET_CODE (XEXP (t, 0)) == IOR
6768 : || GET_CODE (XEXP (t, 0)) == XOR
6769 : || GET_CODE (XEXP (t, 0)) == ASHIFT
6770 : || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6771 : || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6772 1398 : && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6773 55 : && HWI_COMPUTABLE_MODE_P (int_mode)
6774 55 : && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6775 55 : && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6776 2140101 : && ((nonzero_bits (f, GET_MODE (f))
6777 0 : & ~GET_MODE_MASK (inner_mode))
6778 : == 0))
6779 : {
6780 0 : c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6781 0 : extend_op = ZERO_EXTEND;
6782 0 : m = inner_mode;
6783 : }
6784 2140101 : else if (GET_CODE (t) == ZERO_EXTEND
6785 5190 : && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6786 5190 : && (GET_CODE (XEXP (t, 0)) == PLUS
6787 4311 : || GET_CODE (XEXP (t, 0)) == IOR
6788 4309 : || GET_CODE (XEXP (t, 0)) == XOR)
6789 881 : && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6790 20 : && HWI_COMPUTABLE_MODE_P (int_mode)
6791 20 : && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6792 20 : && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6793 2140101 : && ((nonzero_bits (f, GET_MODE (f))
6794 0 : & ~GET_MODE_MASK (inner_mode))
6795 : == 0))
6796 : {
6797 0 : c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6798 0 : extend_op = ZERO_EXTEND;
6799 0 : m = inner_mode;
6800 : }
6801 :
6802 90663 : if (z)
6803 : {
6804 90663 : machine_mode cm = m;
6805 90663 : if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6806 2481 : && GET_MODE (c1) != VOIDmode)
6807 1855 : cm = GET_MODE (c1);
6808 90663 : temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6809 : cond_op0, cond_op1),
6810 : pc_rtx, pc_rtx, false, false, false);
6811 90663 : temp = simplify_gen_binary (MULT, cm, temp,
6812 : simplify_gen_binary (MULT, cm, c1,
6813 : const_true_rtx));
6814 90663 : temp = subst (temp, pc_rtx, pc_rtx, false, false, false);
6815 90663 : temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6816 :
6817 90663 : if (extend_op != UNKNOWN)
6818 0 : temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6819 :
6820 13515810 : return temp;
6821 : }
6822 : }
6823 :
6824 : /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6825 : 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6826 : negation of a single bit, we can convert this operation to a shift. We
6827 : can actually do this more generally, but it doesn't seem worth it. */
6828 :
6829 13411490 : if (true_code == NE
6830 13411490 : && is_a <scalar_int_mode> (mode, &int_mode)
6831 456327 : && XEXP (cond, 1) == const0_rtx
6832 340765 : && false_rtx == const0_rtx
6833 52211 : && CONST_INT_P (true_rtx)
6834 13413545 : && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6835 0 : && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6836 2055 : || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6837 2055 : == GET_MODE_PRECISION (int_mode))
6838 0 : && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6839 0 : return
6840 0 : simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6841 0 : gen_lowpart (int_mode, XEXP (cond, 0)), i);
6842 :
6843 : /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6844 : non-zero bit in A is C1. */
6845 4912732 : if (true_code == NE && XEXP (cond, 1) == const0_rtx
6846 2310330 : && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6847 13517865 : && is_a <scalar_int_mode> (mode, &int_mode)
6848 2055 : && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6849 7 : && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6850 7 : == nonzero_bits (XEXP (cond, 0), inner_mode)
6851 13411490 : && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6852 : {
6853 0 : rtx val = XEXP (cond, 0);
6854 0 : if (inner_mode == int_mode)
6855 : return val;
6856 0 : else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6857 0 : return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6858 : }
6859 :
6860 : return x;
6861 : }
6862 :
6863 : /* Simplify X, a SET expression. Return the new expression. */
6864 :
6865 : static rtx
6866 48436526 : simplify_set (rtx x)
6867 : {
6868 48436526 : rtx src = SET_SRC (x);
6869 48436526 : rtx dest = SET_DEST (x);
6870 108294504 : machine_mode mode
6871 48436526 : = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6872 48436526 : rtx_insn *other_insn;
6873 48436526 : rtx *cc_use;
6874 48436526 : scalar_int_mode int_mode;
6875 :
6876 : /* (set (pc) (return)) gets written as (return). */
6877 48436526 : if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6878 : return src;
6879 :
6880 : /* Now that we know for sure which bits of SRC we are using, see if we can
6881 : simplify the expression for the object knowing that we only need the
6882 : low-order bits. */
6883 :
6884 48436526 : if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6885 : {
6886 21799864 : src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, false);
6887 21799864 : SUBST (SET_SRC (x), src);
6888 : }
6889 :
6890 : /* If the source is a COMPARE, look for the use of the comparison result
6891 : and try to simplify it unless we already have used undobuf.other_insn. */
6892 41666247 : if ((GET_MODE_CLASS (mode) == MODE_CC || GET_CODE (src) == COMPARE)
6893 6770279 : && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6894 6142640 : && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6895 6142640 : && COMPARISON_P (*cc_use)
6896 54578654 : && rtx_equal_p (XEXP (*cc_use, 0), dest))
6897 : {
6898 6140307 : enum rtx_code old_code = GET_CODE (*cc_use);
6899 6140307 : enum rtx_code new_code;
6900 6140307 : rtx op0, op1, tmp;
6901 6140307 : bool other_changed = false;
6902 6140307 : rtx inner_compare = NULL_RTX;
6903 6140307 : machine_mode compare_mode = GET_MODE (dest);
6904 :
6905 6140307 : if (GET_CODE (src) == COMPARE)
6906 : {
6907 5688726 : op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6908 5688726 : if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6909 : {
6910 0 : inner_compare = op0;
6911 0 : op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6912 : }
6913 : }
6914 : else
6915 451581 : op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6916 :
6917 6140307 : tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6918 : op0, op1);
6919 6140307 : if (!tmp)
6920 : new_code = old_code;
6921 496549 : else if (!CONSTANT_P (tmp))
6922 : {
6923 491810 : new_code = GET_CODE (tmp);
6924 491810 : op0 = XEXP (tmp, 0);
6925 491810 : op1 = XEXP (tmp, 1);
6926 : }
6927 : else
6928 : {
6929 4739 : rtx pat = PATTERN (other_insn);
6930 4739 : undobuf.other_insn = other_insn;
6931 :
6932 : /* Substitute the constant into the CC user and simplify it. Use
6933 : subst, since the comparison may be the operand of a ZERO_EXTEND,
6934 : which must not be given a CONST_INT. Preserve n_occurrences,
6935 : which the caller of the outer subst may be counting. */
6936 4739 : int saved_n_occurrences = n_occurrences;
6937 4739 : rtx new_pat = subst (pat, *cc_use, tmp, false, false, false);
6938 4739 : n_occurrences = saved_n_occurrences;
6939 4739 : if (new_pat != pat)
6940 0 : SUBST (PATTERN (other_insn), new_pat);
6941 :
6942 : /* Convert X into a no-op move. */
6943 4739 : SUBST (SET_DEST (x), pc_rtx);
6944 4739 : SUBST (SET_SRC (x), pc_rtx);
6945 4739 : return x;
6946 : }
6947 :
6948 : /* Simplify our comparison, if possible. */
6949 6135568 : new_code = simplify_comparison (new_code, &op0, &op1);
6950 :
6951 : #ifdef SELECT_CC_MODE
6952 : /* If this machine has CC modes other than CCmode, check to see if we
6953 : need to use a different CC mode here. */
6954 6135568 : if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6955 695425 : compare_mode = GET_MODE (op0);
6956 5440143 : else if (inner_compare
6957 0 : && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6958 0 : && new_code == old_code
6959 0 : && op0 == XEXP (inner_compare, 0)
6960 0 : && op1 == XEXP (inner_compare, 1))
6961 0 : compare_mode = GET_MODE (inner_compare);
6962 : else
6963 5440143 : compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6964 :
6965 : /* If the mode changed, we have to change SET_DEST, the mode in the
6966 : compare, and the mode in the place SET_DEST is used. If SET_DEST is
6967 : a hard register, just build new versions with the proper mode. If it
6968 : is a pseudo, we lose unless it is only time we set the pseudo, in
6969 : which case we can safely change its mode. */
6970 6135568 : if (compare_mode != GET_MODE (dest))
6971 : {
6972 216263 : if (can_change_dest_mode (dest, 0, compare_mode))
6973 : {
6974 216263 : unsigned int regno = REGNO (dest);
6975 216263 : rtx new_dest;
6976 :
6977 216263 : if (regno < FIRST_PSEUDO_REGISTER)
6978 216263 : new_dest = gen_rtx_REG (compare_mode, regno);
6979 : else
6980 : {
6981 0 : subst_mode (regno, compare_mode);
6982 0 : new_dest = regno_reg_rtx[regno];
6983 : }
6984 :
6985 216263 : SUBST (SET_DEST (x), new_dest);
6986 216263 : SUBST (XEXP (*cc_use, 0), new_dest);
6987 216263 : other_changed = true;
6988 :
6989 216263 : dest = new_dest;
6990 : }
6991 : }
6992 : #endif /* SELECT_CC_MODE */
6993 :
6994 : /* If the code changed, we have to build a new comparison in
6995 : undobuf.other_insn. */
6996 6135568 : if (new_code != old_code)
6997 : {
6998 635452 : bool other_changed_previously = other_changed;
6999 635452 : unsigned HOST_WIDE_INT mask;
7000 635452 : rtx old_cc_use = *cc_use;
7001 :
7002 635452 : SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
7003 : dest, const0_rtx));
7004 635452 : other_changed = true;
7005 :
7006 : /* If the only change we made was to change an EQ into an NE or
7007 : vice versa, OP0 has only one bit that might be nonzero, and OP1
7008 : is zero, check if changing the user of the condition code will
7009 : produce a valid insn. If it won't, we can keep the original code
7010 : in that insn by surrounding our operation with an XOR. */
7011 :
7012 635452 : if (((old_code == NE && new_code == EQ)
7013 593303 : || (old_code == EQ && new_code == NE))
7014 93883 : && ! other_changed_previously && op1 == const0_rtx
7015 91164 : && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
7016 645818 : && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
7017 : {
7018 10355 : rtx pat = PATTERN (other_insn), note = 0;
7019 :
7020 10355 : if ((recog_for_combine (&pat, other_insn, ¬e) < 0
7021 10355 : && ! check_asm_operands (pat)))
7022 : {
7023 6 : *cc_use = old_cc_use;
7024 6 : other_changed = false;
7025 :
7026 6 : op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
7027 6 : gen_int_mode (mask,
7028 6 : GET_MODE (op0)));
7029 : }
7030 : }
7031 : }
7032 :
7033 5510471 : if (other_changed)
7034 652732 : undobuf.other_insn = other_insn;
7035 :
7036 : /* Don't generate a compare of a CC with 0, just use that CC. */
7037 6135568 : if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
7038 : {
7039 695425 : SUBST (SET_SRC (x), op0);
7040 695425 : src = SET_SRC (x);
7041 : }
7042 : /* Otherwise, if we didn't previously have the same COMPARE we
7043 : want, create it from scratch. */
7044 5440143 : else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
7045 5316213 : || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
7046 : {
7047 1394933 : SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
7048 1394933 : src = SET_SRC (x);
7049 : }
7050 : }
7051 : else
7052 : {
7053 : /* Get SET_SRC in a form where we have placed back any
7054 : compound expressions. Then do the checks below. */
7055 42296219 : src = make_compound_operation (src, SET);
7056 42296219 : SUBST (SET_SRC (x), src);
7057 : }
7058 :
7059 : /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
7060 : and X being a REG or (subreg (reg)), we may be able to convert this to
7061 : (set (subreg:m2 x) (op)).
7062 :
7063 : We can always do this if M1 is narrower than M2 because that means that
7064 : we only care about the low bits of the result.
7065 :
7066 : However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
7067 : perform a narrower operation than requested since the high-order bits will
7068 : be undefined. On machine where it is defined, this transformation is safe
7069 : as long as M1 and M2 have the same number of words. */
7070 :
7071 436372 : if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
7072 420441 : && !OBJECT_P (SUBREG_REG (src))
7073 : && (known_equal_after_align_up
7074 272309 : (GET_MODE_SIZE (GET_MODE (src)),
7075 544618 : GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
7076 272309 : UNITS_PER_WORD))
7077 241460 : && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
7078 227745 : && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
7079 236 : && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
7080 : GET_MODE (SUBREG_REG (src)),
7081 : GET_MODE (src)))
7082 48659296 : && (REG_P (dest)
7083 125109 : || (GET_CODE (dest) == SUBREG
7084 322 : && REG_P (SUBREG_REG (dest)))))
7085 : {
7086 102722 : SUBST (SET_DEST (x),
7087 : gen_lowpart (GET_MODE (SUBREG_REG (src)),
7088 : dest));
7089 102722 : SUBST (SET_SRC (x), SUBREG_REG (src));
7090 :
7091 102722 : src = SET_SRC (x), dest = SET_DEST (x);
7092 : }
7093 :
7094 : /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
7095 : would require a paradoxical subreg. Replace the subreg with a
7096 : zero_extend to avoid the reload that would otherwise be required.
7097 : Don't do this unless we have a scalar integer mode, otherwise the
7098 : transformation is incorrect. */
7099 :
7100 48431787 : enum rtx_code extend_op;
7101 48431787 : if (paradoxical_subreg_p (src)
7102 : && MEM_P (SUBREG_REG (src))
7103 : && SCALAR_INT_MODE_P (GET_MODE (src))
7104 : && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
7105 : {
7106 : SUBST (SET_SRC (x),
7107 : gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
7108 :
7109 : src = SET_SRC (x);
7110 : }
7111 :
7112 : /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7113 : are comparing an item known to be 0 or -1 against 0, use a logical
7114 : operation instead. Check for one of the arms being an IOR of the other
7115 : arm with some value. We compute three terms to be IOR'ed together. In
7116 : practice, at most two will be nonzero. Then we do the IOR's. */
7117 :
7118 48431787 : if (GET_CODE (dest) != PC
7119 37445682 : && GET_CODE (src) == IF_THEN_ELSE
7120 1358351 : && is_int_mode (GET_MODE (src), &int_mode)
7121 1231005 : && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7122 488889 : && XEXP (XEXP (src, 0), 1) == const0_rtx
7123 362288 : && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7124 120876 : && (!HAVE_conditional_move
7125 120876 : || ! can_conditionally_move_p (int_mode))
7126 0 : && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7127 0 : == GET_MODE_PRECISION (int_mode))
7128 48431787 : && ! side_effects_p (src))
7129 : {
7130 0 : rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7131 0 : ? XEXP (src, 1) : XEXP (src, 2));
7132 0 : rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7133 0 : ? XEXP (src, 2) : XEXP (src, 1));
7134 0 : rtx term1 = const0_rtx, term2, term3;
7135 :
7136 0 : if (GET_CODE (true_rtx) == IOR
7137 0 : && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7138 0 : term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7139 0 : else if (GET_CODE (true_rtx) == IOR
7140 0 : && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7141 0 : term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7142 0 : else if (GET_CODE (false_rtx) == IOR
7143 0 : && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7144 0 : term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7145 0 : else if (GET_CODE (false_rtx) == IOR
7146 0 : && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7147 0 : term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7148 :
7149 0 : term2 = simplify_gen_binary (AND, int_mode,
7150 0 : XEXP (XEXP (src, 0), 0), true_rtx);
7151 0 : term3 = simplify_gen_binary (AND, int_mode,
7152 : simplify_gen_unary (NOT, int_mode,
7153 0 : XEXP (XEXP (src, 0), 0),
7154 : int_mode),
7155 : false_rtx);
7156 :
7157 0 : SUBST (SET_SRC (x),
7158 : simplify_gen_binary (IOR, int_mode,
7159 : simplify_gen_binary (IOR, int_mode,
7160 : term1, term2),
7161 : term3));
7162 :
7163 0 : src = SET_SRC (x);
7164 : }
7165 :
7166 : /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7167 : whole thing fail. */
7168 48431787 : if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7169 : return src;
7170 48431769 : else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7171 : return dest;
7172 : else
7173 : /* Convert this into a field assignment operation, if possible. */
7174 48431727 : return make_field_assignment (x);
7175 : }
7176 :
7177 : /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7178 : result. */
7179 :
7180 : static rtx
7181 11626599 : simplify_logical (rtx x)
7182 : {
7183 11626599 : rtx op0 = XEXP (x, 0);
7184 11626599 : rtx op1 = XEXP (x, 1);
7185 11626599 : scalar_int_mode mode;
7186 :
7187 11626599 : switch (GET_CODE (x))
7188 : {
7189 7406769 : case AND:
7190 : /* We can call simplify_and_const_int only if we don't lose
7191 : any (sign) bits when converting INTVAL (op1) to
7192 : "unsigned HOST_WIDE_INT". */
7193 7406769 : if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7194 6864048 : && CONST_INT_P (op1)
7195 5368600 : && (HWI_COMPUTABLE_MODE_P (mode)
7196 6885 : || INTVAL (op1) > 0))
7197 : {
7198 5365232 : x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7199 5365232 : if (GET_CODE (x) != AND)
7200 : return x;
7201 :
7202 5339131 : op0 = XEXP (x, 0);
7203 5339131 : op1 = XEXP (x, 1);
7204 : }
7205 :
7206 : /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7207 : apply the distributive law and then the inverse distributive
7208 : law to see if things simplify. */
7209 7380668 : if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7210 : {
7211 122001 : rtx result = distribute_and_simplify_rtx (x, 0);
7212 122001 : if (result)
7213 : return result;
7214 : }
7215 7367090 : if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7216 : {
7217 1970 : rtx result = distribute_and_simplify_rtx (x, 1);
7218 1970 : if (result)
7219 0 : return result;
7220 : }
7221 : break;
7222 :
7223 4219830 : case IOR:
7224 : /* If we have (ior (and A B) C), apply the distributive law and then
7225 : the inverse distributive law to see if things simplify. */
7226 :
7227 4219830 : if (GET_CODE (op0) == AND)
7228 : {
7229 1271561 : rtx result = distribute_and_simplify_rtx (x, 0);
7230 1271561 : if (result)
7231 : return result;
7232 : }
7233 :
7234 4217076 : if (GET_CODE (op1) == AND)
7235 : {
7236 59067 : rtx result = distribute_and_simplify_rtx (x, 1);
7237 59067 : if (result)
7238 4 : return result;
7239 : }
7240 : break;
7241 :
7242 0 : default:
7243 0 : gcc_unreachable ();
7244 : }
7245 :
7246 : return x;
7247 : }
7248 :
7249 : /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7250 : operations" because they can be replaced with two more basic operations.
7251 : ZERO_EXTEND is also considered "compound" because it can be replaced with
7252 : an AND operation, which is simpler, though only one operation.
7253 :
7254 : The function expand_compound_operation is called with an rtx expression
7255 : and will convert it to the appropriate shifts and AND operations,
7256 : simplifying at each stage.
7257 :
7258 : The function make_compound_operation is called to convert an expression
7259 : consisting of shifts and ANDs into the equivalent compound expression.
7260 : It is the inverse of this function, loosely speaking. */
7261 :
7262 : static rtx
7263 30383606 : expand_compound_operation (rtx x)
7264 : {
7265 30383606 : unsigned HOST_WIDE_INT pos = 0, len;
7266 30383606 : bool unsignedp = false;
7267 30383606 : unsigned int modewidth;
7268 30383606 : rtx tem;
7269 30383606 : scalar_int_mode inner_mode;
7270 :
7271 30383606 : switch (GET_CODE (x))
7272 : {
7273 5087927 : case ZERO_EXTEND:
7274 5087927 : unsignedp = true;
7275 : /* FALLTHRU */
7276 6553824 : case SIGN_EXTEND:
7277 : /* We can't necessarily use a const_int for a multiword mode;
7278 : it depends on implicitly extending the value.
7279 : Since we don't know the right way to extend it,
7280 : we can't tell whether the implicit way is right.
7281 :
7282 : Even for a mode that is no wider than a const_int,
7283 : we can't win, because we need to sign extend one of its bits through
7284 : the rest of it, and we don't know which bit. */
7285 6553824 : if (CONST_INT_P (XEXP (x, 0)))
7286 : return x;
7287 :
7288 : /* Reject modes that aren't scalar integers because turning vector
7289 : or complex modes into shifts causes problems. */
7290 6553824 : if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7291 : return x;
7292 :
7293 : /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7294 : (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7295 : because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7296 : reloaded. If not for that, MEM's would very rarely be safe.
7297 :
7298 : Reject modes bigger than a word, because we might not be able
7299 : to reference a two-register group starting with an arbitrary register
7300 : (and currently gen_lowpart might crash for a SUBREG). */
7301 :
7302 13289205 : if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7303 : return x;
7304 :
7305 6190314 : len = GET_MODE_PRECISION (inner_mode);
7306 : /* If the inner object has VOIDmode (the only way this can happen
7307 : is if it is an ASM_OPERANDS), we can't do anything since we don't
7308 : know how much masking to do. */
7309 6190314 : if (len == 0)
7310 : return x;
7311 :
7312 : break;
7313 :
7314 969830 : case ZERO_EXTRACT:
7315 969830 : unsignedp = true;
7316 :
7317 : /* fall through */
7318 :
7319 1002395 : case SIGN_EXTRACT:
7320 : /* If the operand is a CLOBBER, just return it. */
7321 1002395 : if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7322 : return XEXP (x, 0);
7323 :
7324 1002389 : if (!CONST_INT_P (XEXP (x, 1))
7325 1002266 : || !CONST_INT_P (XEXP (x, 2)))
7326 : return x;
7327 :
7328 : /* Reject modes that aren't scalar integers because turning vector
7329 : or complex modes into shifts causes problems. */
7330 920749 : if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7331 : return x;
7332 :
7333 920747 : len = INTVAL (XEXP (x, 1));
7334 920747 : pos = INTVAL (XEXP (x, 2));
7335 :
7336 : /* This should stay within the object being extracted, fail otherwise. */
7337 920747 : if (len + pos > GET_MODE_PRECISION (inner_mode))
7338 : return x;
7339 :
7340 : if (BITS_BIG_ENDIAN)
7341 : pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7342 :
7343 : break;
7344 :
7345 : default:
7346 : return x;
7347 : }
7348 :
7349 : /* We've rejected non-scalar operations by now. */
7350 7111012 : scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7351 :
7352 : /* Convert sign extension to zero extension, if we know that the high
7353 : bit is not set, as this is easier to optimize. It will be converted
7354 : back to cheaper alternative in make_extraction. */
7355 7111012 : if (GET_CODE (x) == SIGN_EXTEND
7356 1309514 : && HWI_COMPUTABLE_MODE_P (mode)
7357 8305832 : && ((nonzero_bits (XEXP (x, 0), inner_mode)
7358 1194820 : & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7359 : == 0))
7360 : {
7361 396 : rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7362 396 : rtx temp2 = expand_compound_operation (temp);
7363 :
7364 : /* Make sure this is a profitable operation. */
7365 396 : if (set_src_cost (x, mode, optimize_this_for_speed_p)
7366 396 : > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7367 : return temp2;
7368 352 : else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7369 352 : > set_src_cost (temp, mode, optimize_this_for_speed_p))
7370 : return temp;
7371 : else
7372 18 : return x;
7373 : }
7374 :
7375 : /* We can optimize some special cases of ZERO_EXTEND. */
7376 7110616 : if (GET_CODE (x) == ZERO_EXTEND)
7377 : {
7378 : /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7379 : know that the last value didn't have any inappropriate bits
7380 : set. */
7381 4880800 : if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7382 256 : && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7383 256 : && HWI_COMPUTABLE_MODE_P (mode)
7384 4881056 : && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7385 256 : & ~GET_MODE_MASK (inner_mode)) == 0)
7386 57 : return XEXP (XEXP (x, 0), 0);
7387 :
7388 : /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7389 4880743 : if (GET_CODE (XEXP (x, 0)) == SUBREG
7390 783875 : && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7391 708082 : && subreg_lowpart_p (XEXP (x, 0))
7392 345609 : && HWI_COMPUTABLE_MODE_P (mode)
7393 5195368 : && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7394 314625 : & ~GET_MODE_MASK (inner_mode)) == 0)
7395 65 : return SUBREG_REG (XEXP (x, 0));
7396 :
7397 : /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7398 : is a comparison and STORE_FLAG_VALUE permits. This is like
7399 : the first case, but it works even when MODE is larger
7400 : than HOST_WIDE_INT. */
7401 4880678 : if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7402 199 : && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7403 199 : && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7404 0 : && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7405 4880678 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7406 : return XEXP (XEXP (x, 0), 0);
7407 :
7408 : /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7409 4880678 : if (GET_CODE (XEXP (x, 0)) == SUBREG
7410 783810 : && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7411 708017 : && subreg_lowpart_p (XEXP (x, 0))
7412 345544 : && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7413 0 : && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7414 4880678 : && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7415 : return SUBREG_REG (XEXP (x, 0));
7416 :
7417 : }
7418 :
7419 : /* If we reach here, we want to return a pair of shifts. The inner
7420 : shift is a left shift of BITSIZE - POS - LEN bits. The outer
7421 : shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7422 : logical depending on the value of UNSIGNEDP.
7423 :
7424 : If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7425 : converted into an AND of a shift.
7426 :
7427 : We must check for the case where the left shift would have a negative
7428 : count. This can happen in a case like (x >> 31) & 255 on machines
7429 : that can't shift by a constant. On those machines, we would first
7430 : combine the shift with the AND to produce a variable-position
7431 : extraction. Then the constant of 31 would be substituted in
7432 : to produce such a position. */
7433 :
7434 7110494 : modewidth = GET_MODE_PRECISION (mode);
7435 7110494 : if (modewidth >= pos + len)
7436 : {
7437 7110493 : tem = gen_lowpart (mode, XEXP (x, 0));
7438 7110493 : if (!tem || GET_CODE (tem) == CLOBBER)
7439 : return x;
7440 7675492 : tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7441 3837746 : tem, modewidth - pos - len);
7442 3837746 : tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7443 3837746 : mode, tem, modewidth - len);
7444 : }
7445 1 : else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7446 : {
7447 1 : tem = simplify_shift_const (NULL_RTX, LSHIFTRT, inner_mode,
7448 : XEXP (x, 0), pos);
7449 1 : tem = gen_lowpart (mode, tem);
7450 1 : if (!tem || GET_CODE (tem) == CLOBBER)
7451 : return x;
7452 1 : tem = simplify_and_const_int (NULL_RTX, mode, tem,
7453 1 : (HOST_WIDE_INT_1U << len) - 1);
7454 : }
7455 : else
7456 : /* Any other cases we can't handle. */
7457 : return x;
7458 :
7459 : /* If we couldn't do this for some reason, return the original
7460 : expression. */
7461 3837747 : if (GET_CODE (tem) == CLOBBER)
7462 25 : return x;
7463 :
7464 : return tem;
7465 : }
7466 :
7467 : /* X is a SET which contains an assignment of one object into
7468 : a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7469 : or certain SUBREGS). If possible, convert it into a series of
7470 : logical operations.
7471 :
7472 : We half-heartedly support variable positions, but do not at all
7473 : support variable lengths. */
7474 :
7475 : static const_rtx
7476 86634075 : expand_field_assignment (const_rtx x)
7477 : {
7478 86634075 : rtx inner;
7479 86634075 : rtx pos; /* Always counts from low bit. */
7480 86634075 : int len, inner_len;
7481 86634075 : rtx mask, cleared, masked;
7482 86634075 : scalar_int_mode compute_mode;
7483 :
7484 : /* Loop until we find something we can't simplify. */
7485 86908461 : while (1)
7486 : {
7487 86908461 : if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7488 14308 : && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7489 : {
7490 14308 : rtx x0 = XEXP (SET_DEST (x), 0);
7491 14308 : if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7492 : break;
7493 14308 : inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7494 14308 : pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7495 : MAX_MODE_INT);
7496 14308 : }
7497 86894153 : else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7498 4573 : && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7499 : {
7500 4573 : inner = XEXP (SET_DEST (x), 0);
7501 4573 : if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7502 : break;
7503 :
7504 4573 : len = INTVAL (XEXP (SET_DEST (x), 1));
7505 4573 : pos = XEXP (SET_DEST (x), 2);
7506 :
7507 : /* A constant position should stay within the width of INNER. */
7508 4573 : if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7509 : break;
7510 :
7511 : if (BITS_BIG_ENDIAN)
7512 : {
7513 : if (CONST_INT_P (pos))
7514 : pos = GEN_INT (inner_len - len - INTVAL (pos));
7515 : else if (GET_CODE (pos) == MINUS
7516 : && CONST_INT_P (XEXP (pos, 1))
7517 : && INTVAL (XEXP (pos, 1)) == inner_len - len)
7518 : /* If position is ADJUST - X, new position is X. */
7519 : pos = XEXP (pos, 0);
7520 : else
7521 : pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7522 : gen_int_mode (inner_len - len,
7523 : GET_MODE (pos)),
7524 : pos);
7525 : }
7526 : }
7527 :
7528 : /* If the destination is a subreg that overwrites the whole of the inner
7529 : register, we can move the subreg to the source. */
7530 87151906 : else if (GET_CODE (SET_DEST (x)) == SUBREG
7531 : /* We need SUBREGs to compute nonzero_bits properly. */
7532 919605 : && nonzero_sign_valid
7533 87720878 : && !read_modify_subreg_p (SET_DEST (x)))
7534 : {
7535 262326 : x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7536 : gen_lowpart
7537 : (GET_MODE (SUBREG_REG (SET_DEST (x))),
7538 : SET_SRC (x)));
7539 262326 : continue;
7540 : }
7541 : else
7542 : break;
7543 :
7544 21166 : while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7545 2285 : inner = SUBREG_REG (inner);
7546 :
7547 : /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7548 18881 : if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7549 : {
7550 : /* Don't do anything for vector or complex integral types. */
7551 : if (! FLOAT_MODE_P (GET_MODE (inner)))
7552 : break;
7553 :
7554 : /* Try to find an integral mode to pun with. */
7555 38 : if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7556 0 : .exists (&compute_mode))
7557 : break;
7558 :
7559 19 : inner = gen_lowpart (compute_mode, inner);
7560 : }
7561 :
7562 : /* Compute a mask of LEN bits, if we can do this on the host machine. */
7563 14151 : if (len >= HOST_BITS_PER_WIDE_INT)
7564 : break;
7565 :
7566 : /* Don't try to compute in too wide unsupported modes. */
7567 14151 : if (!targetm.scalar_mode_supported_p (compute_mode))
7568 : break;
7569 :
7570 : /* gen_lowpart_for_combine returns CLOBBER on failure. */
7571 14151 : rtx lowpart = gen_lowpart (compute_mode, SET_SRC (x));
7572 14151 : if (GET_CODE (lowpart) == CLOBBER)
7573 : break;
7574 :
7575 : /* Now compute the equivalent expression. Make a copy of INNER
7576 : for the SET_DEST in case it is a MEM into which we will substitute;
7577 : we don't want shared RTL in that case. */
7578 12060 : mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7579 : compute_mode);
7580 12060 : cleared = simplify_gen_binary (AND, compute_mode,
7581 : simplify_gen_unary (NOT, compute_mode,
7582 : simplify_gen_binary (ASHIFT,
7583 : compute_mode,
7584 : mask, pos),
7585 : compute_mode),
7586 : inner);
7587 12060 : masked = simplify_gen_binary (ASHIFT, compute_mode,
7588 : simplify_gen_binary (
7589 : AND, compute_mode, lowpart, mask),
7590 : pos);
7591 :
7592 12060 : x = gen_rtx_SET (copy_rtx (inner),
7593 : simplify_gen_binary (IOR, compute_mode,
7594 : cleared, masked));
7595 : }
7596 :
7597 86634075 : return x;
7598 : }
7599 :
7600 : /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7601 : it is an RTX that represents the (variable) starting position; otherwise,
7602 : POS is the (constant) starting bit position. Both are counted from the LSB.
7603 :
7604 : UNSIGNEDP is true for an unsigned reference and zero for a signed one.
7605 :
7606 : IN_DEST is true if this is a reference in the destination of a SET.
7607 : This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7608 : a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7609 : be used.
7610 :
7611 : IN_COMPARE is true if we are in a COMPARE. This means that a
7612 : ZERO_EXTRACT should be built even for bits starting at bit 0.
7613 :
7614 : MODE is the desired mode of the result (if IN_DEST == 0).
7615 :
7616 : The result is an RTX for the extraction or NULL_RTX if the target
7617 : can't handle it. */
7618 :
7619 : static rtx
7620 5351384 : make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7621 : rtx pos_rtx, unsigned HOST_WIDE_INT len, bool unsignedp,
7622 : bool in_dest, bool in_compare)
7623 : {
7624 : /* This mode describes the size of the storage area
7625 : to fetch the overall value from. Within that, we
7626 : ignore the POS lowest bits, etc. */
7627 5351384 : machine_mode is_mode = GET_MODE (inner);
7628 5351384 : machine_mode inner_mode;
7629 5351384 : scalar_int_mode wanted_inner_mode;
7630 5351384 : scalar_int_mode wanted_inner_reg_mode = word_mode;
7631 5351384 : scalar_int_mode pos_mode = word_mode;
7632 5351384 : machine_mode extraction_mode = word_mode;
7633 5351384 : rtx new_rtx = 0;
7634 5351384 : rtx orig_pos_rtx = pos_rtx;
7635 5351384 : HOST_WIDE_INT orig_pos;
7636 :
7637 5351384 : if (pos_rtx && CONST_INT_P (pos_rtx))
7638 958975 : pos = INTVAL (pos_rtx), pos_rtx = 0;
7639 :
7640 5351384 : if (GET_CODE (inner) == SUBREG
7641 2726179 : && subreg_lowpart_p (inner)
7642 8073840 : && (paradoxical_subreg_p (inner)
7643 : /* If trying or potentially trying to extract
7644 : bits outside of is_mode, don't look through
7645 : non-paradoxical SUBREGs. See PR82192. */
7646 190257 : || (pos_rtx == NULL_RTX
7647 190233 : && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7648 : {
7649 : /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7650 : consider just the QI as the memory to extract from.
7651 : The subreg adds or removes high bits; its mode is
7652 : irrelevant to the meaning of this extraction,
7653 : since POS and LEN count from the lsb. */
7654 2722432 : if (MEM_P (SUBREG_REG (inner)))
7655 505711 : is_mode = GET_MODE (SUBREG_REG (inner));
7656 : inner = SUBREG_REG (inner);
7657 : }
7658 2628952 : else if (GET_CODE (inner) == ASHIFT
7659 145275 : && CONST_INT_P (XEXP (inner, 1))
7660 143937 : && pos_rtx == 0 && pos == 0
7661 143913 : && len > UINTVAL (XEXP (inner, 1)))
7662 : {
7663 : /* We're extracting the least significant bits of an rtx
7664 : (ashift X (const_int C)), where LEN > C. Extract the
7665 : least significant (LEN - C) bits of X, giving an rtx
7666 : whose mode is MODE, then shift it left C times. */
7667 143913 : new_rtx = make_extraction (mode, XEXP (inner, 0),
7668 : 0, 0, len - INTVAL (XEXP (inner, 1)),
7669 : unsignedp, in_dest, in_compare);
7670 143913 : if (new_rtx != 0)
7671 142267 : return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7672 : }
7673 2485039 : else if (GET_CODE (inner) == MULT
7674 179257 : && CONST_INT_P (XEXP (inner, 1))
7675 136157 : && pos_rtx == 0 && pos == 0)
7676 : {
7677 : /* We're extracting the least significant bits of an rtx
7678 : (mult X (const_int 2^C)), where LEN > C. Extract the
7679 : least significant (LEN - C) bits of X, giving an rtx
7680 : whose mode is MODE, then multiply it by 2^C. */
7681 115499 : const HOST_WIDE_INT shift_amt = exact_log2 (INTVAL (XEXP (inner, 1)));
7682 115499 : if (len > 1 && IN_RANGE (shift_amt, 1, len - 1))
7683 : {
7684 110845 : new_rtx = make_extraction (mode, XEXP (inner, 0),
7685 : 0, 0, len - shift_amt,
7686 : unsignedp, in_dest, in_compare);
7687 110845 : if (new_rtx)
7688 110845 : return gen_rtx_MULT (mode, new_rtx, XEXP (inner, 1));
7689 : }
7690 : }
7691 2369540 : else if (GET_CODE (inner) == TRUNCATE
7692 : /* If trying or potentially trying to extract
7693 : bits outside of is_mode, don't look through
7694 : TRUNCATE. See PR82192. */
7695 0 : && pos_rtx == NULL_RTX
7696 2369540 : && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7697 0 : inner = XEXP (inner, 0);
7698 :
7699 5098272 : inner_mode = GET_MODE (inner);
7700 :
7701 : /* See if this can be done without an extraction. We never can if the
7702 : width of the field is not the same as that of some integer mode. For
7703 : registers, we can only avoid the extraction if the position is at the
7704 : low-order bit and this is either not in the destination or we have the
7705 : appropriate STRICT_LOW_PART operation available.
7706 :
7707 : For MEM, we can avoid an extract if the field starts on an appropriate
7708 : boundary and we can change the mode of the memory reference. */
7709 :
7710 5098272 : scalar_int_mode tmode;
7711 5098272 : if (int_mode_for_size (len, 1).exists (&tmode)
7712 2469908 : && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7713 2146419 : && !MEM_P (inner)
7714 1751603 : && (pos == 0 || REG_P (inner))
7715 1751603 : && (inner_mode == tmode
7716 265716 : || !REG_P (inner)
7717 2373415 : || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7718 0 : || reg_truncated_to_mode (tmode, inner))
7719 1751603 : && (! in_dest
7720 29 : || (REG_P (inner)
7721 29 : && have_insn_for (STRICT_LOW_PART, tmode))))
7722 578957 : || (MEM_P (inner) && pos_rtx == 0
7723 396192 : && (pos
7724 : % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7725 : : BITS_PER_UNIT)) == 0
7726 : /* We can't do this if we are widening INNER_MODE (it
7727 : may not be aligned, for one thing). */
7728 395176 : && !paradoxical_subreg_p (tmode, inner_mode)
7729 395176 : && known_le (pos + len, GET_MODE_PRECISION (is_mode))
7730 395176 : && (inner_mode == tmode
7731 581 : || (! mode_dependent_address_p (XEXP (inner, 0),
7732 581 : MEM_ADDR_SPACE (inner))
7733 581 : && ! MEM_VOLATILE_P (inner))))))
7734 : {
7735 : /* If INNER is a MEM, make a new MEM that encompasses just the desired
7736 : field. If the original and current mode are the same, we need not
7737 : adjust the offset. Otherwise, we do if bytes big endian.
7738 :
7739 : If INNER is not a MEM, get a piece consisting of just the field
7740 : of interest (in this case POS % BITS_PER_WORD must be 0). */
7741 :
7742 2146750 : if (MEM_P (inner))
7743 : {
7744 395163 : poly_int64 offset;
7745 :
7746 : /* POS counts from lsb, but make OFFSET count in memory order. */
7747 395163 : if (BYTES_BIG_ENDIAN)
7748 : offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7749 : - len - pos);
7750 : else
7751 395163 : offset = pos / BITS_PER_UNIT;
7752 :
7753 395163 : new_rtx = adjust_address_nv (inner, tmode, offset);
7754 : }
7755 1751587 : else if (REG_P (inner))
7756 : {
7757 1096380 : if (tmode != inner_mode)
7758 : {
7759 : /* We can't call gen_lowpart in a DEST since we
7760 : always want a SUBREG (see below) and it would sometimes
7761 : return a new hard register. */
7762 226649 : if (pos || in_dest)
7763 : {
7764 16 : poly_uint64 offset
7765 16 : = subreg_offset_from_lsb (tmode, inner_mode, pos);
7766 :
7767 : /* Avoid creating invalid subregs, for example when
7768 : simplifying (x>>32)&255. */
7769 16 : if (!validate_subreg (tmode, inner_mode, inner, offset))
7770 0 : return NULL_RTX;
7771 :
7772 16 : new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7773 16 : }
7774 : else
7775 226633 : new_rtx = gen_lowpart (tmode, inner);
7776 : }
7777 : else
7778 : new_rtx = inner;
7779 : }
7780 : else
7781 1310414 : new_rtx = force_to_mode (inner, tmode,
7782 : len >= HOST_BITS_PER_WIDE_INT
7783 : ? HOST_WIDE_INT_M1U
7784 655207 : : (HOST_WIDE_INT_1U << len) - 1, false);
7785 :
7786 : /* If this extraction is going into the destination of a SET,
7787 : make a STRICT_LOW_PART unless we made a MEM. */
7788 :
7789 2146750 : if (in_dest)
7790 55 : return (MEM_P (new_rtx) ? new_rtx
7791 : : (GET_CODE (new_rtx) != SUBREG
7792 13 : ? gen_rtx_CLOBBER (tmode, const0_rtx)
7793 13 : : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7794 :
7795 2146695 : if (mode == tmode)
7796 : return new_rtx;
7797 :
7798 2146666 : if (CONST_SCALAR_INT_P (new_rtx))
7799 5 : return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7800 5 : mode, new_rtx, tmode);
7801 :
7802 : /* If we know that no extraneous bits are set, and that the high
7803 : bit is not set, convert the extraction to the cheaper of
7804 : sign and zero extension, that are equivalent in these cases. */
7805 2146661 : if (flag_expensive_optimizations
7806 2146661 : && (HWI_COMPUTABLE_MODE_P (tmode)
7807 1991904 : && ((nonzero_bits (new_rtx, tmode)
7808 1991904 : & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7809 : == 0)))
7810 : {
7811 7305 : rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7812 7305 : rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7813 :
7814 : /* Prefer ZERO_EXTENSION, since it gives more information to
7815 : backends. */
7816 7305 : if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7817 7305 : <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7818 : return temp;
7819 0 : return temp1;
7820 : }
7821 :
7822 : /* Otherwise, sign- or zero-extend unless we already are in the
7823 : proper mode. */
7824 :
7825 2139356 : return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7826 2139356 : mode, new_rtx));
7827 : }
7828 :
7829 : /* Unless this is a COMPARE or we have a funny memory reference,
7830 : don't do anything with zero-extending field extracts starting at
7831 : the low-order bit since they are simple AND operations. */
7832 2951522 : if (pos_rtx == 0 && pos == 0 && ! in_dest
7833 1860454 : && ! in_compare && unsignedp)
7834 : return 0;
7835 :
7836 : /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7837 : if the position is not a constant and the length is not 1. In all
7838 : other cases, we would only be going outside our object in cases when
7839 : an original shift would have been undefined. */
7840 1509743 : if (MEM_P (inner)
7841 1509743 : && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7842 2867 : || (pos_rtx != 0 && len != 1)))
7843 : return 0;
7844 :
7845 1632423 : enum extraction_pattern pattern = (in_dest ? EP_insv
7846 1503049 : : unsignedp ? EP_extzv : EP_extv);
7847 :
7848 : /* If INNER is not from memory, we want it to have the mode of a register
7849 : extraction pattern's structure operand, or word_mode if there is no
7850 : such pattern. The same applies to extraction_mode and pos_mode
7851 : and their respective operands.
7852 :
7853 : For memory, assume that the desired extraction_mode and pos_mode
7854 : are the same as for a register operation, since at present we don't
7855 : have named patterns for aligned memory structures. */
7856 1509703 : class extraction_insn insn;
7857 1509703 : unsigned int inner_size;
7858 3019406 : if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7859 1509703 : && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7860 : {
7861 1404920 : wanted_inner_reg_mode = insn.struct_mode.require ();
7862 1404920 : pos_mode = insn.pos_mode;
7863 1404920 : extraction_mode = insn.field_mode;
7864 : }
7865 :
7866 : /* Never narrow an object, since that might not be safe. */
7867 :
7868 1509703 : if (mode != VOIDmode
7869 1509703 : && partial_subreg_p (extraction_mode, mode))
7870 : extraction_mode = mode;
7871 :
7872 : /* Punt if len is too large for extraction_mode. */
7873 1509703 : if (maybe_gt (len, GET_MODE_PRECISION (extraction_mode)))
7874 : return NULL_RTX;
7875 :
7876 1509691 : if (!MEM_P (inner))
7877 1328846 : wanted_inner_mode = wanted_inner_reg_mode;
7878 : else
7879 : {
7880 : /* Be careful not to go beyond the extracted object and maintain the
7881 : natural alignment of the memory. */
7882 180845 : wanted_inner_mode = smallest_int_mode_for_size (len).require ();
7883 364914 : while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7884 368138 : > GET_MODE_BITSIZE (wanted_inner_mode))
7885 3224 : wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7886 : }
7887 :
7888 1509691 : orig_pos = pos;
7889 :
7890 1509691 : if (BITS_BIG_ENDIAN)
7891 : {
7892 : /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7893 : BITS_BIG_ENDIAN style. If position is constant, compute new
7894 : position. Otherwise, build subtraction.
7895 : Note that POS is relative to the mode of the original argument.
7896 : If it's a MEM we need to recompute POS relative to that.
7897 : However, if we're extracting from (or inserting into) a register,
7898 : we want to recompute POS relative to wanted_inner_mode. */
7899 : int width;
7900 : if (!MEM_P (inner))
7901 : width = GET_MODE_BITSIZE (wanted_inner_mode);
7902 : else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7903 : return NULL_RTX;
7904 :
7905 : if (pos_rtx == 0)
7906 : pos = width - len - pos;
7907 : else
7908 : pos_rtx
7909 : = gen_rtx_MINUS (GET_MODE (pos_rtx),
7910 : gen_int_mode (width - len, GET_MODE (pos_rtx)),
7911 : pos_rtx);
7912 : /* POS may be less than 0 now, but we check for that below.
7913 : Note that it can only be less than 0 if !MEM_P (inner). */
7914 : }
7915 :
7916 : /* If INNER has a wider mode, and this is a constant extraction, try to
7917 : make it smaller and adjust the byte to point to the byte containing
7918 : the value. */
7919 1509691 : if (wanted_inner_mode != VOIDmode
7920 1509691 : && inner_mode != wanted_inner_mode
7921 227881 : && ! pos_rtx
7922 219408 : && partial_subreg_p (wanted_inner_mode, is_mode)
7923 113030 : && MEM_P (inner)
7924 27028 : && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7925 1536719 : && ! MEM_VOLATILE_P (inner))
7926 : {
7927 25285 : poly_int64 offset = 0;
7928 :
7929 : /* The computations below will be correct if the machine is big
7930 : endian in both bits and bytes or little endian in bits and bytes.
7931 : If it is mixed, we must adjust. */
7932 :
7933 : /* If bytes are big endian and we had a paradoxical SUBREG, we must
7934 : adjust OFFSET to compensate. */
7935 25285 : if (BYTES_BIG_ENDIAN
7936 : && paradoxical_subreg_p (is_mode, inner_mode))
7937 : offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7938 :
7939 : /* We can now move to the desired byte. */
7940 50570 : offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7941 25285 : * GET_MODE_SIZE (wanted_inner_mode);
7942 25285 : pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7943 :
7944 25285 : if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7945 : && is_mode != wanted_inner_mode)
7946 : offset = (GET_MODE_SIZE (is_mode)
7947 : - GET_MODE_SIZE (wanted_inner_mode) - offset);
7948 :
7949 25285 : inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7950 : }
7951 :
7952 : /* If INNER is not memory, get it into the proper mode. If we are changing
7953 : its mode, POS must be a constant and smaller than the size of the new
7954 : mode. */
7955 1484406 : else if (!MEM_P (inner))
7956 : {
7957 : /* On the LHS, don't create paradoxical subregs implicitly truncating
7958 : the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7959 1328846 : if (in_dest
7960 1328846 : && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7961 : wanted_inner_mode))
7962 0 : return NULL_RTX;
7963 :
7964 1328846 : if (GET_MODE (inner) != wanted_inner_mode
7965 1328846 : && (pos_rtx != 0
7966 384760 : || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7967 : return NULL_RTX;
7968 :
7969 1265114 : if (orig_pos < 0)
7970 : return NULL_RTX;
7971 :
7972 2510275 : inner = force_to_mode (inner, wanted_inner_mode,
7973 : pos_rtx
7974 1245161 : || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7975 : ? HOST_WIDE_INT_M1U
7976 1072628 : : (((HOST_WIDE_INT_1U << len) - 1)
7977 1072628 : << orig_pos), false);
7978 : }
7979 :
7980 : /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7981 : have to zero extend. Otherwise, we can just use a SUBREG.
7982 :
7983 : We dealt with constant rtxes earlier, so pos_rtx cannot
7984 : have VOIDmode at this point. */
7985 1445959 : if (pos_rtx != 0
7986 1445959 : && (GET_MODE_SIZE (pos_mode)
7987 1468739 : > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7988 : {
7989 66 : rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7990 : GET_MODE (pos_rtx));
7991 :
7992 : /* If we know that no extraneous bits are set, and that the high
7993 : bit is not set, convert extraction to cheaper one - either
7994 : SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7995 : cases. */
7996 66 : if (flag_expensive_optimizations
7997 66 : && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7998 66 : && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7999 66 : & ~(((unsigned HOST_WIDE_INT)
8000 66 : GET_MODE_MASK (GET_MODE (pos_rtx)))
8001 66 : >> 1))
8002 : == 0)))
8003 : {
8004 56 : rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
8005 : GET_MODE (pos_rtx));
8006 :
8007 : /* Prefer ZERO_EXTENSION, since it gives more information to
8008 : backends. */
8009 56 : if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
8010 56 : < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
8011 1445959 : temp = temp1;
8012 : }
8013 : pos_rtx = temp;
8014 : }
8015 :
8016 : /* Make POS_RTX unless we already have it and it is correct. If we don't
8017 : have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
8018 : be a CONST_INT. */
8019 1445959 : if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
8020 : pos_rtx = orig_pos_rtx;
8021 :
8022 511412 : else if (pos_rtx == 0)
8023 488632 : pos_rtx = GEN_INT (pos);
8024 :
8025 : /* Make the required operation. See if we can use existing rtx. */
8026 1445959 : new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
8027 : extraction_mode, inner, GEN_INT (len), pos_rtx);
8028 1445959 : if (! in_dest)
8029 1439349 : new_rtx = gen_lowpart (mode, new_rtx);
8030 :
8031 : return new_rtx;
8032 : }
8033 :
8034 : /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
8035 : can be commuted with any other operations in X. Return X without
8036 : that shift if so. */
8037 :
8038 : static rtx
8039 1674912 : extract_left_shift (scalar_int_mode mode, rtx x, int count)
8040 : {
8041 1674912 : enum rtx_code code = GET_CODE (x);
8042 1674912 : rtx tem;
8043 :
8044 1674912 : switch (code)
8045 : {
8046 270848 : case ASHIFT:
8047 : /* This is the shift itself. If it is wide enough, we will return
8048 : either the value being shifted if the shift count is equal to
8049 : COUNT or a shift for the difference. */
8050 270848 : if (CONST_INT_P (XEXP (x, 1))
8051 265504 : && INTVAL (XEXP (x, 1)) >= count)
8052 264209 : return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
8053 264209 : INTVAL (XEXP (x, 1)) - count);
8054 : break;
8055 :
8056 5463 : case NEG: case NOT:
8057 5463 : if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8058 2603 : return simplify_gen_unary (code, mode, tem, mode);
8059 :
8060 : break;
8061 :
8062 597981 : case PLUS: case IOR: case XOR: case AND:
8063 : /* If we can safely shift this constant and we find the inner shift,
8064 : make a new operation. */
8065 597981 : if (CONST_INT_P (XEXP (x, 1))
8066 323990 : && (UINTVAL (XEXP (x, 1))
8067 323990 : & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
8068 760462 : && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8069 : {
8070 6884 : HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
8071 6884 : return simplify_gen_binary (code, mode, tem,
8072 6884 : gen_int_mode (val, mode));
8073 : }
8074 : break;
8075 :
8076 : default:
8077 : break;
8078 : }
8079 :
8080 : return 0;
8081 : }
8082 :
8083 : /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
8084 : level of the expression and MODE is its mode. IN_CODE is as for
8085 : make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
8086 : that should be used when recursing on operands of *X_PTR.
8087 :
8088 : There are two possible actions:
8089 :
8090 : - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
8091 : equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
8092 :
8093 : - Return a new rtx, which the caller returns directly. */
8094 :
8095 : static rtx
8096 286863551 : make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
8097 : enum rtx_code in_code,
8098 : enum rtx_code *next_code_ptr)
8099 : {
8100 286863551 : rtx x = *x_ptr;
8101 286863551 : enum rtx_code next_code = *next_code_ptr;
8102 286863551 : enum rtx_code code = GET_CODE (x);
8103 286863551 : int mode_width = GET_MODE_PRECISION (mode);
8104 286863551 : rtx rhs, lhs;
8105 286863551 : rtx new_rtx = 0;
8106 286863551 : int i;
8107 286863551 : rtx tem;
8108 286863551 : scalar_int_mode inner_mode;
8109 286863551 : bool equality_comparison = false;
8110 :
8111 286863551 : if (in_code == EQ)
8112 : {
8113 9279681 : equality_comparison = true;
8114 9279681 : in_code = COMPARE;
8115 : }
8116 :
8117 : /* Process depending on the code of this operation. If NEW is set
8118 : nonzero, it will be returned. */
8119 :
8120 286863551 : switch (code)
8121 : {
8122 6645191 : case ASHIFT:
8123 : /* Convert shifts by constants into multiplications if inside
8124 : an address. */
8125 6645191 : if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
8126 1987144 : && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8127 1987144 : && INTVAL (XEXP (x, 1)) >= 0)
8128 : {
8129 1987144 : HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
8130 1987144 : HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
8131 :
8132 1987144 : new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8133 1987144 : if (GET_CODE (new_rtx) == NEG)
8134 : {
8135 9 : new_rtx = XEXP (new_rtx, 0);
8136 9 : multval = -multval;
8137 : }
8138 1987144 : multval = trunc_int_for_mode (multval, mode);
8139 1987144 : new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8140 : }
8141 : break;
8142 :
8143 56316893 : case PLUS:
8144 56316893 : case MINUS:
8145 56316893 : lhs = make_compound_operation (XEXP (x, 0), next_code);
8146 56316893 : rhs = make_compound_operation (XEXP (x, 1), next_code);
8147 56316893 : if (lhs != XEXP (x, 0) || rhs != XEXP (x, 1))
8148 3277874 : return simplify_gen_binary (code, mode, lhs, rhs);
8149 : return x;
8150 :
8151 7732018 : case AND:
8152 : /* If the second operand is not a constant, we can't do anything
8153 : with it. */
8154 7732018 : if (!CONST_INT_P (XEXP (x, 1)))
8155 : break;
8156 :
8157 : /* If the constant is a power of two minus one and the first operand
8158 : is a logical right shift, make an extraction. */
8159 6104235 : if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8160 6104235 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8161 : {
8162 657272 : new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8163 657272 : new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8164 : i, true, false, in_code == COMPARE);
8165 : }
8166 :
8167 : /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8168 5446963 : else if (GET_CODE (XEXP (x, 0)) == SUBREG
8169 1432880 : && subreg_lowpart_p (XEXP (x, 0))
8170 6834495 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8171 : &inner_mode)
8172 1417915 : && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8173 5479448 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8174 : {
8175 30383 : rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8176 30383 : new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8177 30383 : new_rtx = make_extraction (inner_mode, new_rtx, 0,
8178 : XEXP (inner_x0, 1),
8179 : i, true, false, in_code == COMPARE);
8180 :
8181 : /* If we narrowed the mode when dropping the subreg, then we lose. */
8182 91149 : if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8183 30383 : new_rtx = NULL;
8184 :
8185 : /* If that didn't give anything, see if the AND simplifies on
8186 : its own. */
8187 30383 : if (!new_rtx && i >= 0)
8188 : {
8189 3350 : new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8190 3350 : new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i,
8191 : true, false, in_code == COMPARE);
8192 : }
8193 : }
8194 : /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8195 5416580 : else if ((GET_CODE (XEXP (x, 0)) == XOR
8196 5416580 : || GET_CODE (XEXP (x, 0)) == IOR)
8197 31865 : && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8198 2816 : && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8199 5416590 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8200 : {
8201 : /* Apply the distributive law, and then try to make extractions. */
8202 10 : new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8203 : gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8204 : XEXP (x, 1)),
8205 : gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8206 : XEXP (x, 1)));
8207 10 : new_rtx = make_compound_operation (new_rtx, in_code);
8208 : }
8209 :
8210 : /* If we are have (and (rotate X C) M) and C is larger than the number
8211 : of bits in M, this is an extraction. */
8212 :
8213 5416570 : else if (GET_CODE (XEXP (x, 0)) == ROTATE
8214 935 : && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8215 925 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8216 5416607 : && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8217 : {
8218 0 : new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8219 0 : new_rtx = make_extraction (mode, new_rtx,
8220 0 : (GET_MODE_PRECISION (mode)
8221 0 : - INTVAL (XEXP (XEXP (x, 0), 1))),
8222 : NULL_RTX, i, true, false,
8223 : in_code == COMPARE);
8224 : }
8225 :
8226 : /* On machines without logical shifts, if the operand of the AND is
8227 : a logical shift and our mask turns off all the propagated sign
8228 : bits, we can replace the logical shift with an arithmetic shift. */
8229 5416570 : else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8230 89682 : && !have_insn_for (LSHIFTRT, mode)
8231 0 : && have_insn_for (ASHIFTRT, mode)
8232 0 : && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8233 0 : && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8234 0 : && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8235 5416570 : && mode_width <= HOST_BITS_PER_WIDE_INT)
8236 : {
8237 0 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8238 :
8239 0 : mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8240 0 : if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8241 0 : SUBST (XEXP (x, 0),
8242 : gen_rtx_ASHIFTRT (mode,
8243 : make_compound_operation (XEXP (XEXP (x,
8244 : 0),
8245 : 0),
8246 : next_code),
8247 : XEXP (XEXP (x, 0), 1)));
8248 : }
8249 :
8250 : /* If the constant is one less than a power of two, this might be
8251 : representable by an extraction even if no shift is present.
8252 : If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8253 : we are in a COMPARE. */
8254 5416570 : else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8255 2824831 : new_rtx = make_extraction (mode,
8256 : make_compound_operation (XEXP (x, 0),
8257 : next_code),
8258 : 0, NULL_RTX, i,
8259 : true, false, in_code == COMPARE);
8260 :
8261 : /* If we are in a comparison and this is an AND with a power of two,
8262 : convert this into the appropriate bit extract. */
8263 2591739 : else if (in_code == COMPARE
8264 475361 : && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8265 2672061 : && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8266 80322 : new_rtx = make_extraction (mode,
8267 : make_compound_operation (XEXP (x, 0),
8268 : next_code),
8269 : i, NULL_RTX, 1, true, false, true);
8270 :
8271 : /* If the one operand is a paradoxical subreg of a register or memory and
8272 : the constant (limited to the smaller mode) has only zero bits where
8273 : the sub expression has known zero bits, this can be expressed as
8274 : a zero_extend. */
8275 2511417 : else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8276 : {
8277 79362 : rtx sub;
8278 :
8279 79362 : sub = XEXP (XEXP (x, 0), 0);
8280 79362 : machine_mode sub_mode = GET_MODE (sub);
8281 79362 : int sub_width;
8282 30079 : if ((REG_P (sub) || MEM_P (sub))
8283 49826 : && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8284 49826 : && sub_width < mode_width
8285 79362 : && (!WORD_REGISTER_OPERATIONS
8286 : || sub_width >= BITS_PER_WORD
8287 : /* On WORD_REGISTER_OPERATIONS targets the bits
8288 : beyond sub_mode aren't considered undefined,
8289 : so optimize only if it is a MEM load when MEM loads
8290 : zero extend, because then the upper bits are all zero. */
8291 : || (MEM_P (sub)
8292 : && load_extend_op (sub_mode) == ZERO_EXTEND)))
8293 : {
8294 25399 : unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8295 25399 : unsigned HOST_WIDE_INT mask;
8296 :
8297 : /* Original AND constant with all the known zero bits set. */
8298 25399 : mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8299 25399 : if ((mask & mode_mask) == mode_mask)
8300 : {
8301 18694 : new_rtx = make_compound_operation (sub, next_code);
8302 18694 : new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8303 : true, false, in_code == COMPARE);
8304 : }
8305 : }
8306 : }
8307 :
8308 : break;
8309 :
8310 2075179 : case LSHIFTRT:
8311 : /* If the sign bit is known to be zero, replace this with an
8312 : arithmetic shift. */
8313 2075179 : if (have_insn_for (ASHIFTRT, mode)
8314 2075179 : && ! have_insn_for (LSHIFTRT, mode)
8315 0 : && mode_width <= HOST_BITS_PER_WIDE_INT
8316 2075179 : && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8317 : {
8318 0 : new_rtx = gen_rtx_ASHIFTRT (mode,
8319 : make_compound_operation (XEXP (x, 0),
8320 : next_code),
8321 : XEXP (x, 1));
8322 0 : break;
8323 : }
8324 :
8325 : /* fall through */
8326 :
8327 5009231 : case ASHIFTRT:
8328 5009231 : lhs = XEXP (x, 0);
8329 5009231 : rhs = XEXP (x, 1);
8330 :
8331 : /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8332 : this is a SIGN_EXTRACT. */
8333 5009231 : if (CONST_INT_P (rhs)
8334 4823997 : && GET_CODE (lhs) == ASHIFT
8335 1171262 : && CONST_INT_P (XEXP (lhs, 1))
8336 1165922 : && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8337 911134 : && INTVAL (XEXP (lhs, 1)) >= 0
8338 911130 : && INTVAL (rhs) < mode_width)
8339 : {
8340 911129 : new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8341 911129 : new_rtx = make_extraction (mode, new_rtx,
8342 911129 : INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8343 911129 : NULL_RTX, mode_width - INTVAL (rhs),
8344 : code == LSHIFTRT, false,
8345 : in_code == COMPARE);
8346 911129 : break;
8347 : }
8348 :
8349 : /* See if we have operations between an ASHIFTRT and an ASHIFT.
8350 : If so, try to merge the shifts into a SIGN_EXTEND. We could
8351 : also do this for some cases of SIGN_EXTRACT, but it doesn't
8352 : seem worth the effort; the case checked for occurs on Alpha. */
8353 :
8354 4098102 : if (!OBJECT_P (lhs)
8355 1590189 : && ! (GET_CODE (lhs) == SUBREG
8356 66620 : && (OBJECT_P (SUBREG_REG (lhs))))
8357 1538430 : && CONST_INT_P (rhs)
8358 1512991 : && INTVAL (rhs) >= 0
8359 1512991 : && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8360 1506969 : && INTVAL (rhs) < mode_width
8361 5605070 : && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8362 264209 : new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8363 : next_code),
8364 264209 : 0, NULL_RTX, mode_width - INTVAL (rhs),
8365 : code == LSHIFTRT, false, in_code == COMPARE);
8366 :
8367 : break;
8368 :
8369 9971863 : case SUBREG:
8370 : /* Call ourselves recursively on the inner expression. If we are
8371 : narrowing the object and it has a different RTL code from
8372 : what it originally did, do this SUBREG as a force_to_mode. */
8373 9971863 : {
8374 9971863 : rtx inner = SUBREG_REG (x), simplified;
8375 9971863 : enum rtx_code subreg_code = in_code;
8376 :
8377 : /* If the SUBREG is masking of a logical right shift,
8378 : make an extraction. */
8379 9971863 : if (GET_CODE (inner) == LSHIFTRT
8380 9982823 : && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8381 621374 : && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8382 306669 : && CONST_INT_P (XEXP (inner, 1))
8383 301119 : && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8384 10272982 : && subreg_lowpart_p (x))
8385 : {
8386 299727 : new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8387 299727 : int width = GET_MODE_PRECISION (inner_mode)
8388 299727 : - INTVAL (XEXP (inner, 1));
8389 299727 : if (width > mode_width)
8390 : width = mode_width;
8391 299727 : new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8392 : width, true, false, in_code == COMPARE);
8393 299727 : break;
8394 : }
8395 :
8396 : /* If in_code is COMPARE, it isn't always safe to pass it through
8397 : to the recursive make_compound_operation call. */
8398 9672136 : if (subreg_code == COMPARE
8399 9672136 : && (!subreg_lowpart_p (x)
8400 246420 : || GET_CODE (inner) == SUBREG
8401 : /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8402 : is (const_int 0), rather than
8403 : (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8404 : Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8405 : for non-equality comparisons against 0 is not equivalent
8406 : to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8407 246420 : || (GET_CODE (inner) == AND
8408 1267 : && CONST_INT_P (XEXP (inner, 1))
8409 208 : && partial_subreg_p (x)
8410 416 : && exact_log2 (UINTVAL (XEXP (inner, 1)))
8411 208 : >= GET_MODE_BITSIZE (mode) - 1)))
8412 : subreg_code = SET;
8413 :
8414 9672136 : tem = make_compound_operation (inner, subreg_code);
8415 :
8416 : /* TEM's code might be CLOBBER if combine_simplify_rtx
8417 : could not transform a subexpression, e.g. a volatile MEM.
8418 : simplify_subreg cannot be called with clobber, so bail out. */
8419 9672136 : if (GET_CODE (tem) == CLOBBER)
8420 : return NULL_RTX;
8421 :
8422 9672117 : simplified
8423 9672117 : = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8424 9672117 : if (simplified)
8425 16576 : tem = simplified;
8426 :
8427 9672117 : if (GET_CODE (tem) != GET_CODE (inner)
8428 21941 : && partial_subreg_p (x)
8429 9690669 : && subreg_lowpart_p (x))
8430 : {
8431 18536 : rtx newer
8432 18536 : = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, false);
8433 :
8434 : /* If we have something other than a SUBREG, we might have
8435 : done an expansion, so rerun ourselves. */
8436 18536 : if (GET_CODE (newer) != SUBREG)
8437 17111 : newer = make_compound_operation (newer, in_code);
8438 :
8439 : /* force_to_mode can expand compounds. If it just re-expanded
8440 : the compound, use gen_lowpart to convert to the desired
8441 : mode. */
8442 18536 : if (rtx_equal_p (newer, x)
8443 : /* Likewise if it re-expanded the compound only partially.
8444 : This happens for SUBREG of ZERO_EXTRACT if they extract
8445 : the same number of bits. */
8446 18536 : || (GET_CODE (newer) == SUBREG
8447 2226 : && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8448 2226 : || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8449 78 : && GET_CODE (inner) == AND
8450 60 : && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8451 782 : return gen_lowpart (GET_MODE (x), tem);
8452 :
8453 : return newer;
8454 : }
8455 :
8456 9653581 : if (simplified)
8457 : return tem;
8458 : }
8459 : break;
8460 :
8461 : default:
8462 : break;
8463 : }
8464 :
8465 10907614 : if (new_rtx)
8466 5572965 : *x_ptr = gen_lowpart (mode, new_rtx);
8467 230527732 : *next_code_ptr = next_code;
8468 230527732 : return NULL_RTX;
8469 : }
8470 :
8471 : /* Look at the expression rooted at X. Look for expressions
8472 : equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8473 : Form these expressions.
8474 :
8475 : Return the new rtx, usually just X.
8476 :
8477 : Also, for machines like the VAX that don't have logical shift insns,
8478 : try to convert logical to arithmetic shift operations in cases where
8479 : they are equivalent. This undoes the canonicalizations to logical
8480 : shifts done elsewhere.
8481 :
8482 : We try, as much as possible, to re-use rtl expressions to save memory.
8483 :
8484 : IN_CODE says what kind of expression we are processing. Normally, it is
8485 : SET. In a memory address it is MEM. When processing the arguments of
8486 : a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8487 : precisely it is an equality comparison against zero. */
8488 :
8489 : rtx
8490 491462459 : make_compound_operation (rtx x, enum rtx_code in_code)
8491 : {
8492 491462459 : enum rtx_code code = GET_CODE (x);
8493 491462459 : const char *fmt;
8494 491462459 : int i, j;
8495 491462459 : enum rtx_code next_code;
8496 491462459 : rtx new_rtx, tem;
8497 :
8498 : /* Select the code to be used in recursive calls. Once we are inside an
8499 : address, we stay there. If we have a comparison, set to COMPARE,
8500 : but once inside, go back to our default of SET. */
8501 :
8502 491462459 : next_code = (code == MEM ? MEM
8503 462892240 : : ((code == COMPARE || COMPARISON_P (x))
8504 483791296 : && XEXP (x, 1) == const0_rtx) ? COMPARE
8505 454571599 : : in_code == COMPARE || in_code == EQ ? SET : in_code);
8506 :
8507 491462459 : scalar_int_mode mode;
8508 491462459 : if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8509 : {
8510 286863551 : rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8511 : &next_code);
8512 286863551 : if (new_rtx)
8513 : return new_rtx;
8514 230527751 : code = GET_CODE (x);
8515 : }
8516 :
8517 : /* Now recursively process each operand of this operation. We need to
8518 : handle ZERO_EXTEND specially so that we don't lose track of the
8519 : inner mode. */
8520 435126659 : if (code == ZERO_EXTEND)
8521 : {
8522 3501001 : new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8523 7002002 : tem = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8524 3501001 : new_rtx, GET_MODE (XEXP (x, 0)));
8525 3501001 : if (tem)
8526 : return tem;
8527 3490550 : SUBST (XEXP (x, 0), new_rtx);
8528 3490550 : return x;
8529 : }
8530 :
8531 431625658 : fmt = GET_RTX_FORMAT (code);
8532 1004274590 : for (i = 0; i < GET_RTX_LENGTH (code); i++)
8533 572648932 : if (fmt[i] == 'e')
8534 : {
8535 219408393 : new_rtx = make_compound_operation (XEXP (x, i), next_code);
8536 219408393 : SUBST (XEXP (x, i), new_rtx);
8537 : }
8538 353240539 : else if (fmt[i] == 'E')
8539 28024549 : for (j = 0; j < XVECLEN (x, i); j++)
8540 : {
8541 20394946 : new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8542 20394946 : SUBST (XVECEXP (x, i, j), new_rtx);
8543 : }
8544 :
8545 431625658 : maybe_swap_commutative_operands (x);
8546 431625658 : return x;
8547 : }
8548 :
8549 : /* Given M see if it is a value that would select a field of bits
8550 : within an item, but not the entire word. Return -1 if not.
8551 : Otherwise, return the starting position of the field, where 0 is the
8552 : low-order bit.
8553 :
8554 : *PLEN is set to the length of the field. */
8555 :
8556 : static int
8557 22362 : get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8558 : {
8559 : /* Get the bit number of the first 1 bit from the right, -1 if none. */
8560 22362 : int pos = m ? ctz_hwi (m) : -1;
8561 22362 : int len = 0;
8562 :
8563 22362 : if (pos >= 0)
8564 : /* Now shift off the low-order zero bits and see if we have a
8565 : power of two minus 1. */
8566 22362 : len = exact_log2 ((m >> pos) + 1);
8567 :
8568 10852 : if (len <= 0)
8569 : pos = -1;
8570 :
8571 22362 : *plen = len;
8572 22362 : return pos;
8573 : }
8574 :
8575 : /* If X refers to a register that equals REG in value, replace these
8576 : references with REG. */
8577 : static rtx
8578 9304 : canon_reg_for_combine (rtx x, rtx reg)
8579 : {
8580 9304 : rtx op0, op1, op2;
8581 9304 : const char *fmt;
8582 9304 : int i;
8583 9304 : bool copied;
8584 :
8585 9304 : enum rtx_code code = GET_CODE (x);
8586 9304 : switch (GET_RTX_CLASS (code))
8587 : {
8588 0 : case RTX_UNARY:
8589 0 : op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8590 0 : if (op0 != XEXP (x, 0))
8591 0 : return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8592 0 : GET_MODE (reg));
8593 : break;
8594 :
8595 1622 : case RTX_BIN_ARITH:
8596 1622 : case RTX_COMM_ARITH:
8597 1622 : op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8598 1622 : op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8599 1622 : if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8600 0 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8601 : break;
8602 :
8603 17 : case RTX_COMPARE:
8604 17 : case RTX_COMM_COMPARE:
8605 17 : op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8606 17 : op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8607 17 : if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8608 0 : return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8609 0 : GET_MODE (op0), op0, op1);
8610 : break;
8611 :
8612 2 : case RTX_TERNARY:
8613 2 : case RTX_BITFIELD_OPS:
8614 2 : op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8615 2 : op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8616 2 : op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8617 2 : if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8618 0 : return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8619 0 : GET_MODE (op0), op0, op1, op2);
8620 : /* FALLTHRU */
8621 :
8622 5412 : case RTX_OBJ:
8623 5412 : if (REG_P (x))
8624 : {
8625 5404 : if (rtx_equal_p (get_last_value (reg), x)
8626 5404 : || rtx_equal_p (reg, get_last_value (x)))
8627 0 : return reg;
8628 : else
8629 : break;
8630 : }
8631 :
8632 : /* fall through */
8633 :
8634 2261 : default:
8635 2261 : fmt = GET_RTX_FORMAT (code);
8636 2261 : copied = false;
8637 4585 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8638 2324 : if (fmt[i] == 'e')
8639 : {
8640 65 : rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8641 65 : if (op != XEXP (x, i))
8642 : {
8643 0 : if (!copied)
8644 : {
8645 0 : copied = true;
8646 0 : x = copy_rtx (x);
8647 : }
8648 0 : XEXP (x, i) = op;
8649 : }
8650 : }
8651 2259 : else if (fmt[i] == 'E')
8652 : {
8653 : int j;
8654 0 : for (j = 0; j < XVECLEN (x, i); j++)
8655 : {
8656 0 : rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8657 0 : if (op != XVECEXP (x, i, j))
8658 : {
8659 0 : if (!copied)
8660 : {
8661 0 : copied = true;
8662 0 : x = copy_rtx (x);
8663 : }
8664 0 : XVECEXP (x, i, j) = op;
8665 : }
8666 : }
8667 : }
8668 :
8669 : break;
8670 : }
8671 :
8672 : return x;
8673 : }
8674 :
8675 : /* Return X converted to MODE. If the value is already truncated to
8676 : MODE we can just return a subreg even though in the general case we
8677 : would need an explicit truncation.
8678 :
8679 : If an explicit truncation is needed but either mode has no
8680 : equivalent integer mode (e.g. a wide vector mode), return
8681 : (clobber (const_int 0)) so that the combination fails. */
8682 :
8683 : static rtx
8684 122840649 : gen_lowpart_or_truncate (machine_mode mode, rtx x)
8685 : {
8686 122840649 : if (!CONST_INT_P (x)
8687 117056642 : && partial_subreg_p (mode, GET_MODE (x))
8688 122840649 : && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8689 122840649 : && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8690 : {
8691 0 : scalar_int_mode imode, omode;
8692 0 : if (!int_mode_for_mode (GET_MODE (x)).exists (&imode)
8693 0 : || !int_mode_for_mode (mode).exists (&omode))
8694 0 : return gen_rtx_CLOBBER (mode, const0_rtx);
8695 :
8696 : /* Bit-cast X into an integer mode. */
8697 0 : if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8698 0 : x = gen_lowpart (imode, x);
8699 0 : x = simplify_gen_unary (TRUNCATE, omode, x, GET_MODE (x));
8700 : }
8701 :
8702 122840649 : return gen_lowpart (mode, x);
8703 : }
8704 :
8705 : /* See if X can be simplified knowing that we will only refer to it in
8706 : MODE and will only refer to those bits that are nonzero in MASK.
8707 : If other bits are being computed or if masking operations are done
8708 : that select a superset of the bits in MASK, they can sometimes be
8709 : ignored.
8710 :
8711 : Return a possibly simplified expression, but always convert X to
8712 : MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8713 :
8714 : If JUST_SELECT is true, don't optimize by noticing that bits in MASK
8715 : are all off in X. This is used when X will be complemented, by either
8716 : NOT, NEG, or XOR. */
8717 :
8718 : static rtx
8719 84787524 : force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8720 : bool just_select)
8721 : {
8722 92161202 : enum rtx_code code = GET_CODE (x);
8723 92161202 : bool next_select = just_select || code == XOR || code == NOT || code == NEG;
8724 92161202 : machine_mode op_mode;
8725 92161202 : unsigned HOST_WIDE_INT nonzero;
8726 :
8727 : /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8728 : code below will do the wrong thing since the mode of such an
8729 : expression is VOIDmode.
8730 :
8731 : Also do nothing if X is a CLOBBER; this can happen if X was
8732 : the return value from a call to gen_lowpart. */
8733 92161202 : if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8734 : return x;
8735 :
8736 : /* We want to perform the operation in its present mode unless we know
8737 : that the operation is valid in MODE, in which case we do the operation
8738 : in MODE. */
8739 150780841 : op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8740 85799125 : && have_insn_for (code, mode))
8741 144498372 : ? mode : GET_MODE (x));
8742 :
8743 : /* It is not valid to do a right-shift in a narrower mode
8744 : than the one it came in with. */
8745 92081594 : if ((code == LSHIFTRT || code == ASHIFTRT)
8746 92081594 : && partial_subreg_p (mode, GET_MODE (x)))
8747 404225 : op_mode = GET_MODE (x);
8748 :
8749 : /* Truncate MASK to fit OP_MODE. */
8750 92081594 : if (op_mode)
8751 85878701 : mask &= GET_MODE_MASK (op_mode);
8752 :
8753 : /* Determine what bits of X are guaranteed to be (non)zero. */
8754 92081594 : nonzero = nonzero_bits (x, mode);
8755 :
8756 : /* If none of the bits in X are needed, return a zero. */
8757 92081594 : if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8758 683627 : x = const0_rtx;
8759 :
8760 : /* If X is a CONST_INT, return a new one. Do this here since the
8761 : test below will fail. */
8762 92081594 : if (CONST_INT_P (x))
8763 : {
8764 6418227 : if (SCALAR_INT_MODE_P (mode))
8765 6418227 : return gen_int_mode (INTVAL (x) & mask, mode);
8766 : else
8767 : {
8768 0 : x = GEN_INT (INTVAL (x) & mask);
8769 0 : return gen_lowpart_common (mode, x);
8770 : }
8771 : }
8772 :
8773 : /* If X is narrower than MODE and we want all the bits in X's mode, just
8774 : get X in the proper mode. */
8775 85663367 : if (paradoxical_subreg_p (mode, GET_MODE (x))
8776 85663367 : && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8777 3692733 : return gen_lowpart (mode, x);
8778 :
8779 : /* We can ignore the effect of a SUBREG if it narrows the mode or
8780 : if the constant masks to zero all the bits the mode doesn't have. */
8781 81970634 : if (GET_CODE (x) == SUBREG
8782 7579963 : && subreg_lowpart_p (x)
8783 89381039 : && (partial_subreg_p (x)
8784 5553963 : || (mask
8785 5553963 : & GET_MODE_MASK (GET_MODE (x))
8786 5553963 : & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8787 7373678 : return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8788 :
8789 74596956 : scalar_int_mode int_mode, xmode;
8790 74596956 : if (is_a <scalar_int_mode> (mode, &int_mode)
8791 74596956 : && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8792 : /* OP_MODE is either MODE or XMODE, so it must be a scalar
8793 : integer too. */
8794 74558615 : return force_int_to_mode (x, int_mode, xmode,
8795 : as_a <scalar_int_mode> (op_mode),
8796 74558615 : mask, just_select);
8797 :
8798 38341 : return gen_lowpart_or_truncate (mode, x);
8799 : }
8800 :
8801 : /* Subroutine of force_to_mode that handles cases in which both X and
8802 : the result are scalar integers. MODE is the mode of the result,
8803 : XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8804 : is preferred for simplified versions of X. The other arguments
8805 : are as for force_to_mode. */
8806 :
8807 : static rtx
8808 74558615 : force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8809 : scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8810 : bool just_select)
8811 : {
8812 74558615 : enum rtx_code code = GET_CODE (x);
8813 74558615 : bool next_select = just_select || code == XOR || code == NOT || code == NEG;
8814 74558615 : unsigned HOST_WIDE_INT fuller_mask;
8815 74558615 : rtx op0, op1, temp;
8816 74558615 : poly_int64 const_op0;
8817 :
8818 : /* When we have an arithmetic operation, or a shift whose count we
8819 : do not know, we need to assume that all bits up to the highest-order
8820 : bit in MASK will be needed. This is how we form such a mask. */
8821 74558615 : if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8822 : fuller_mask = HOST_WIDE_INT_M1U;
8823 : else
8824 80295903 : fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1)) - 1);
8825 :
8826 74558615 : switch (code)
8827 : {
8828 : case CLOBBER:
8829 : /* If X is a (clobber (const_int)), return it since we know we are
8830 : generating something that won't match. */
8831 : return x;
8832 :
8833 328559 : case SIGN_EXTEND:
8834 328559 : case ZERO_EXTEND:
8835 328559 : case ZERO_EXTRACT:
8836 328559 : case SIGN_EXTRACT:
8837 328559 : x = expand_compound_operation (x);
8838 328559 : if (GET_CODE (x) != code)
8839 196442 : return force_to_mode (x, mode, mask, next_select);
8840 : break;
8841 :
8842 180 : case TRUNCATE:
8843 : /* Similarly for a truncate. */
8844 180 : return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8845 :
8846 3713035 : case AND:
8847 : /* If this is an AND with a constant, convert it into an AND
8848 : whose constant is the AND of that constant with MASK. If it
8849 : remains an AND of MASK, delete it since it is redundant. */
8850 :
8851 3713035 : if (CONST_INT_P (XEXP (x, 1)))
8852 : {
8853 5919582 : x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8854 2959791 : mask & INTVAL (XEXP (x, 1)));
8855 2959791 : xmode = op_mode;
8856 :
8857 : /* If X is still an AND, see if it is an AND with a mask that
8858 : is just some low-order bits. If so, and it is MASK, we don't
8859 : need it. */
8860 :
8861 2933638 : if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8862 5893429 : && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8863 38883 : x = XEXP (x, 0);
8864 :
8865 : /* If it remains an AND, try making another AND with the bits
8866 : in the mode mask that aren't in MASK turned on. If the
8867 : constant in the AND is wide enough, this might make a
8868 : cheaper constant. */
8869 :
8870 2894845 : if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8871 2894755 : && GET_MODE_MASK (xmode) != mask
8872 3069133 : && HWI_COMPUTABLE_MODE_P (xmode))
8873 : {
8874 109342 : unsigned HOST_WIDE_INT cval
8875 109342 : = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8876 109342 : rtx y;
8877 :
8878 109342 : y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8879 109342 : gen_int_mode (cval, xmode));
8880 109342 : if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8881 109342 : < set_src_cost (x, xmode, optimize_this_for_speed_p))
8882 74251570 : x = y;
8883 : }
8884 :
8885 : break;
8886 : }
8887 :
8888 753244 : goto binop;
8889 :
8890 10778285 : case PLUS:
8891 : /* In (and (plus FOO C1) M), if M is a mask that just turns off
8892 : low-order bits (as in an alignment operation) and FOO is already
8893 : aligned to that boundary, mask C1 to that boundary as well.
8894 : This may eliminate that PLUS and, later, the AND. */
8895 :
8896 10778285 : {
8897 10778285 : unsigned int width = GET_MODE_PRECISION (mode);
8898 10778285 : unsigned HOST_WIDE_INT smask = mask;
8899 :
8900 : /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8901 : number, sign extend it. */
8902 :
8903 10778285 : if (width < HOST_BITS_PER_WIDE_INT
8904 3341055 : && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8905 2994618 : smask |= HOST_WIDE_INT_M1U << width;
8906 :
8907 10778285 : if (CONST_INT_P (XEXP (x, 1))
8908 3820764 : && pow2p_hwi (- smask)
8909 3278493 : && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8910 13658281 : && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8911 10939 : return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8912 10939 : (INTVAL (XEXP (x, 1)) & smask)),
8913 : mode, smask, next_select);
8914 : }
8915 :
8916 : /* fall through */
8917 :
8918 12983644 : case MULT:
8919 : /* Substituting into the operands of a widening MULT is not likely to
8920 : create RTL matching a machine insn. */
8921 12983644 : if (code == MULT
8922 2216298 : && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8923 2216298 : || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8924 88573 : && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8925 88573 : || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8926 49328 : && REG_P (XEXP (XEXP (x, 0), 0))
8927 42425 : && REG_P (XEXP (XEXP (x, 1), 0)))
8928 35018 : return gen_lowpart_or_truncate (mode, x);
8929 :
8930 : /* For PLUS, MINUS and MULT, we need any bits less significant than the
8931 : most significant bit in MASK since carries from those bits will
8932 : affect the bits we are interested in. */
8933 12948626 : mask = fuller_mask;
8934 12948626 : goto binop;
8935 :
8936 2469483 : case MINUS:
8937 : /* If X is (minus C Y) where C's least set bit is larger than any bit
8938 : in the mask, then we may replace with (neg Y). */
8939 2469483 : if (poly_int_rtx_p (XEXP (x, 0), &const_op0)
8940 154593 : && known_alignment (poly_uint64 (const_op0)) > mask)
8941 : {
8942 30 : x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8943 30 : return force_to_mode (x, mode, mask, next_select);
8944 : }
8945 :
8946 : /* Similarly, if C contains every bit in the fuller_mask, then we may
8947 : replace with (not Y). */
8948 2469453 : if (CONST_INT_P (XEXP (x, 0))
8949 154563 : && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8950 : {
8951 342 : x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8952 342 : return force_to_mode (x, mode, mask, next_select);
8953 : }
8954 :
8955 2469111 : mask = fuller_mask;
8956 2469111 : goto binop;
8957 :
8958 1936729 : case IOR:
8959 1936729 : case XOR:
8960 : /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8961 : LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8962 : operation which may be a bitfield extraction. Ensure that the
8963 : constant we form is not wider than the mode of X. */
8964 :
8965 1936729 : if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8966 64469 : && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8967 52594 : && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8968 52594 : && CONST_INT_P (XEXP (x, 1))
8969 8551 : && ((INTVAL (XEXP (XEXP (x, 0), 1))
8970 17102 : + floor_log2 (INTVAL (XEXP (x, 1))))
8971 8551 : < MIN (GET_MODE_PRECISION (xmode), HOST_BITS_PER_WIDE_INT))
8972 1936729 : && (UINTVAL (XEXP (x, 1))
8973 4358 : & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8974 : {
8975 8230 : temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8976 4115 : << INTVAL (XEXP (XEXP (x, 0), 1)),
8977 : xmode);
8978 8230 : temp = simplify_gen_binary (GET_CODE (x), xmode,
8979 4115 : XEXP (XEXP (x, 0), 0), temp);
8980 8230 : x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8981 4115 : XEXP (XEXP (x, 0), 1));
8982 4115 : return force_to_mode (x, mode, mask, next_select);
8983 : }
8984 :
8985 18103595 : binop:
8986 : /* For most binary operations, just propagate into the operation and
8987 : change the mode if we have an operation of that mode. */
8988 :
8989 18103595 : op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8990 18103595 : op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8991 :
8992 : /* If we ended up truncating both operands, truncate the result of the
8993 : operation instead. */
8994 18103595 : if (GET_CODE (op0) == TRUNCATE
8995 0 : && GET_CODE (op1) == TRUNCATE)
8996 : {
8997 0 : op0 = XEXP (op0, 0);
8998 0 : op1 = XEXP (op1, 0);
8999 : }
9000 :
9001 18103595 : op0 = gen_lowpart_or_truncate (op_mode, op0);
9002 18103595 : op1 = gen_lowpart_or_truncate (op_mode, op1);
9003 :
9004 18103595 : if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
9005 : {
9006 2287158 : x = simplify_gen_binary (code, op_mode, op0, op1);
9007 2287158 : xmode = op_mode;
9008 : }
9009 : break;
9010 :
9011 4325088 : case ASHIFT:
9012 : /* For left shifts, do the same, but just for the first operand.
9013 : However, we cannot do anything with shifts where we cannot
9014 : guarantee that the counts are smaller than the size of the mode
9015 : because such a count will have a different meaning in a
9016 : wider mode. */
9017 :
9018 4114975 : if (! (CONST_INT_P (XEXP (x, 1))
9019 4114979 : && INTVAL (XEXP (x, 1)) >= 0
9020 4114975 : && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
9021 4327628 : && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
9022 210109 : && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
9023 210109 : < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
9024 : break;
9025 :
9026 : /* If the shift count is a constant and we can do arithmetic in
9027 : the mode of the shift, refine which bits we need. Otherwise, use the
9028 : conservative form of the mask. */
9029 4180865 : if (CONST_INT_P (XEXP (x, 1))
9030 4112439 : && INTVAL (XEXP (x, 1)) >= 0
9031 4112439 : && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
9032 8293304 : && HWI_COMPUTABLE_MODE_P (op_mode))
9033 4111697 : mask >>= INTVAL (XEXP (x, 1));
9034 : else
9035 : mask = fuller_mask;
9036 :
9037 4180865 : op0 = gen_lowpart_or_truncate (op_mode,
9038 : force_to_mode (XEXP (x, 0), mode,
9039 : mask, next_select));
9040 :
9041 4180865 : if (op_mode != xmode || op0 != XEXP (x, 0))
9042 : {
9043 995903 : x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
9044 995903 : xmode = op_mode;
9045 : }
9046 : break;
9047 :
9048 3150695 : case LSHIFTRT:
9049 : /* Here we can only do something if the shift count is a constant,
9050 : this shift constant is valid for the host, and we can do arithmetic
9051 : in OP_MODE. */
9052 :
9053 3150695 : if (CONST_INT_P (XEXP (x, 1))
9054 3034375 : && INTVAL (XEXP (x, 1)) >= 0
9055 3034374 : && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
9056 6184971 : && HWI_COMPUTABLE_MODE_P (op_mode))
9057 : {
9058 3030753 : rtx inner = XEXP (x, 0);
9059 3030753 : unsigned HOST_WIDE_INT inner_mask;
9060 :
9061 : /* Select the mask of the bits we need for the shift operand. */
9062 3030753 : inner_mask = mask << INTVAL (XEXP (x, 1));
9063 :
9064 : /* We can only change the mode of the shift if we can do arithmetic
9065 : in the mode of the shift and INNER_MASK is no wider than the
9066 : width of X's mode. */
9067 3030753 : if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
9068 263268 : op_mode = xmode;
9069 :
9070 3030753 : inner = force_to_mode (inner, op_mode, inner_mask, next_select);
9071 :
9072 3030753 : if (xmode != op_mode || inner != XEXP (x, 0))
9073 : {
9074 803894 : x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
9075 803894 : xmode = op_mode;
9076 : }
9077 : }
9078 :
9079 : /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
9080 : shift and AND produces only copies of the sign bit (C2 is one less
9081 : than a power of two), we can do this with just a shift. */
9082 :
9083 3150695 : if (GET_CODE (x) == LSHIFTRT
9084 3150638 : && CONST_INT_P (XEXP (x, 1))
9085 : /* The shift puts one of the sign bit copies in the least significant
9086 : bit. */
9087 6068636 : && ((INTVAL (XEXP (x, 1))
9088 3034318 : + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
9089 3034318 : >= GET_MODE_PRECISION (xmode))
9090 243151 : && pow2p_hwi (mask + 1)
9091 : /* Number of bits left after the shift must be more than the mask
9092 : needs. */
9093 79836 : && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
9094 79836 : <= GET_MODE_PRECISION (xmode))
9095 : /* Must be more sign bit copies than the mask needs. */
9096 3179820 : && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
9097 29125 : >= exact_log2 (mask + 1)))
9098 : {
9099 29125 : int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
9100 29125 : x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
9101 29125 : gen_int_shift_amount (xmode, nbits));
9102 : }
9103 3150695 : goto shiftrt;
9104 :
9105 2090551 : case ASHIFTRT:
9106 : /* If we are just looking for the sign bit, we don't need this shift at
9107 : all, even if it has a variable count. */
9108 2090551 : if (val_signbit_p (xmode, mask))
9109 1188 : return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9110 :
9111 : /* If this is a shift by a constant, get a mask that contains those bits
9112 : that are not copies of the sign bit. We then have two cases: If
9113 : MASK only includes those bits, this can be a logical shift, which may
9114 : allow simplifications. If MASK is a single-bit field not within
9115 : those bits, we are requesting a copy of the sign bit and hence can
9116 : shift the sign bit to the appropriate location. */
9117 :
9118 2089363 : if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9119 2043522 : && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9120 : {
9121 2043411 : unsigned HOST_WIDE_INT nonzero;
9122 2043411 : int i;
9123 :
9124 : /* If the considered data is wider than HOST_WIDE_INT, we can't
9125 : represent a mask for all its bits in a single scalar.
9126 : But we only care about the lower bits, so calculate these. */
9127 :
9128 2043411 : if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9129 : {
9130 459 : nonzero = HOST_WIDE_INT_M1U;
9131 :
9132 : /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9133 : is the number of bits a full-width mask would have set.
9134 : We need only shift if these are fewer than nonzero can
9135 : hold. If not, we must keep all bits set in nonzero. */
9136 :
9137 459 : if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9138 : < HOST_BITS_PER_WIDE_INT)
9139 0 : nonzero >>= INTVAL (XEXP (x, 1))
9140 0 : + HOST_BITS_PER_WIDE_INT
9141 0 : - GET_MODE_PRECISION (xmode);
9142 : }
9143 : else
9144 : {
9145 2042952 : nonzero = GET_MODE_MASK (xmode);
9146 2042952 : nonzero >>= INTVAL (XEXP (x, 1));
9147 : }
9148 :
9149 2043411 : if ((mask & ~nonzero) == 0)
9150 : {
9151 50178 : x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9152 : XEXP (x, 0), INTVAL (XEXP (x, 1)));
9153 50178 : if (GET_CODE (x) != ASHIFTRT)
9154 50178 : return force_to_mode (x, mode, mask, next_select);
9155 : }
9156 :
9157 1993233 : else if ((i = exact_log2 (mask)) >= 0)
9158 : {
9159 186 : x = simplify_shift_const
9160 372 : (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9161 186 : GET_MODE_PRECISION (xmode) - 1 - i);
9162 :
9163 186 : if (GET_CODE (x) != ASHIFTRT)
9164 186 : return force_to_mode (x, mode, mask, next_select);
9165 : }
9166 : }
9167 :
9168 : /* If MASK is 1, convert this to an LSHIFTRT. This can be done
9169 : even if the shift count isn't a constant. */
9170 2038999 : if (mask == 1)
9171 3109 : x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9172 :
9173 2035890 : shiftrt:
9174 :
9175 : /* If this is a zero- or sign-extension operation that just affects bits
9176 : we don't care about, remove it. Be sure the call above returned
9177 : something that is still a shift. */
9178 :
9179 5189694 : if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9180 5189637 : && CONST_INT_P (XEXP (x, 1))
9181 5027476 : && INTVAL (XEXP (x, 1)) >= 0
9182 5027475 : && (INTVAL (XEXP (x, 1))
9183 10054950 : <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9184 1801146 : && GET_CODE (XEXP (x, 0)) == ASHIFT
9185 5190838 : && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9186 922 : return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask, next_select);
9187 :
9188 : break;
9189 :
9190 36760 : case ROTATE:
9191 36760 : case ROTATERT:
9192 : /* If the shift count is constant and we can do computations
9193 : in the mode of X, compute where the bits we care about are.
9194 : Otherwise, we can't do anything. Don't change the mode of
9195 : the shift or propagate MODE into the shift, though. */
9196 36760 : if (CONST_INT_P (XEXP (x, 1))
9197 26500 : && INTVAL (XEXP (x, 1)) >= 0)
9198 : {
9199 26498 : temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9200 26498 : xmode, gen_int_mode (mask, xmode),
9201 : XEXP (x, 1));
9202 26498 : if (temp && CONST_INT_P (temp))
9203 26498 : x = simplify_gen_binary (code, xmode,
9204 : force_to_mode (XEXP (x, 0), xmode,
9205 26498 : INTVAL (temp), next_select),
9206 : XEXP (x, 1));
9207 : }
9208 : break;
9209 :
9210 160501 : case NEG:
9211 : /* If we just want the low-order bit, the NEG isn't needed since it
9212 : won't change the low-order bit. */
9213 160501 : if (mask == 1)
9214 390 : return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9215 :
9216 : /* We need any bits less significant than the most significant bit in
9217 : MASK since carries from those bits will affect the bits we are
9218 : interested in. */
9219 160111 : mask = fuller_mask;
9220 160111 : goto unop;
9221 :
9222 478905 : case NOT:
9223 : /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9224 : same as the XOR case above. Ensure that the constant we form is not
9225 : wider than the mode of X. */
9226 :
9227 478905 : if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9228 21362 : && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9229 20789 : && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9230 520483 : && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9231 20789 : < MIN (GET_MODE_PRECISION (xmode), HOST_BITS_PER_WIDE_INT)))
9232 : {
9233 7115 : temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9234 7115 : temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9235 14230 : x = simplify_gen_binary (LSHIFTRT, xmode,
9236 7115 : temp, XEXP (XEXP (x, 0), 1));
9237 :
9238 7115 : return force_to_mode (x, mode, mask, next_select);
9239 : }
9240 :
9241 : /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9242 : use the full mask inside the NOT. */
9243 : mask = fuller_mask;
9244 :
9245 631901 : unop:
9246 631901 : op0 = gen_lowpart_or_truncate (op_mode,
9247 : force_to_mode (XEXP (x, 0), mode, mask,
9248 : next_select));
9249 631901 : if (op_mode != xmode || op0 != XEXP (x, 0))
9250 : {
9251 75265 : x = simplify_gen_unary (code, op_mode, op0, op_mode);
9252 75265 : xmode = op_mode;
9253 : }
9254 : break;
9255 :
9256 594121 : case NE:
9257 : /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9258 : in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9259 : which is equal to STORE_FLAG_VALUE. */
9260 594121 : if ((mask & ~STORE_FLAG_VALUE) == 0
9261 2555 : && XEXP (x, 1) == const0_rtx
9262 2524 : && GET_MODE (XEXP (x, 0)) == mode
9263 21 : && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9264 594133 : && (nonzero_bits (XEXP (x, 0), mode)
9265 : == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9266 0 : return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9267 :
9268 : break;
9269 :
9270 1657373 : case IF_THEN_ELSE:
9271 : /* We have no way of knowing if the IF_THEN_ELSE can itself be
9272 : written in a narrower mode. We play it safe and do not do so. */
9273 :
9274 1657373 : op0 = gen_lowpart_or_truncate (xmode,
9275 : force_to_mode (XEXP (x, 1), mode,
9276 : mask, next_select));
9277 1657373 : op1 = gen_lowpart_or_truncate (xmode,
9278 : force_to_mode (XEXP (x, 2), mode,
9279 : mask, next_select));
9280 1657373 : if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9281 270081 : x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9282 270081 : GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9283 : op0, op1);
9284 : break;
9285 :
9286 : default:
9287 : break;
9288 : }
9289 :
9290 : /* Ensure we return a value of the proper mode. */
9291 74251570 : return gen_lowpart_or_truncate (mode, x);
9292 : }
9293 :
9294 : /* Return nonzero if X is an expression that has one of two values depending on
9295 : whether some other value is zero or nonzero. In that case, we return the
9296 : value that is being tested, *PTRUE is set to the value if the rtx being
9297 : returned has a nonzero value, and *PFALSE is set to the other alternative.
9298 :
9299 : If we return zero, we set *PTRUE and *PFALSE to X. */
9300 :
9301 : static rtx
9302 250568595 : if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9303 : {
9304 250568595 : machine_mode mode = GET_MODE (x);
9305 250568595 : enum rtx_code code = GET_CODE (x);
9306 250568595 : rtx cond0, cond1, true0, true1, false0, false1;
9307 250568595 : unsigned HOST_WIDE_INT nz;
9308 250568595 : scalar_int_mode int_mode;
9309 :
9310 : /* If we are comparing a value against zero, we are done. */
9311 250568595 : if ((code == NE || code == EQ)
9312 2713404 : && XEXP (x, 1) == const0_rtx)
9313 : {
9314 1658150 : *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9315 1658150 : *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9316 1658150 : return XEXP (x, 0);
9317 : }
9318 :
9319 : /* If this is a unary operation whose operand has one of two values, apply
9320 : our opcode to compute those values. */
9321 248910445 : else if (UNARY_P (x)
9322 248910445 : && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9323 : {
9324 532111 : *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9325 1064222 : *pfalse = simplify_gen_unary (code, mode, false0,
9326 532111 : GET_MODE (XEXP (x, 0)));
9327 532111 : return cond0;
9328 : }
9329 :
9330 : /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9331 : make can't possibly match and would suppress other optimizations. */
9332 248378334 : else if (code == COMPARE)
9333 : ;
9334 :
9335 : /* If this is a binary operation, see if either side has only one of two
9336 : values. If either one does or if both do and they are conditional on
9337 : the same value, compute the new true and false values. */
9338 243937587 : else if (BINARY_P (x))
9339 : {
9340 90198099 : rtx op0 = XEXP (x, 0);
9341 90198099 : rtx op1 = XEXP (x, 1);
9342 90198099 : cond0 = if_then_else_cond (op0, &true0, &false0);
9343 90198099 : cond1 = if_then_else_cond (op1, &true1, &false1);
9344 :
9345 491849 : if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9346 90634175 : && (REG_P (op0) || REG_P (op1)))
9347 : {
9348 : /* Try to enable a simplification by undoing work done by
9349 : if_then_else_cond if it converted a REG into something more
9350 : complex. */
9351 363119 : if (REG_P (op0))
9352 : {
9353 74183 : cond0 = 0;
9354 74183 : true0 = false0 = op0;
9355 : }
9356 : else
9357 : {
9358 288936 : cond1 = 0;
9359 288936 : true1 = false1 = op1;
9360 : }
9361 : }
9362 :
9363 90198099 : if ((cond0 != 0 || cond1 != 0)
9364 90198099 : && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9365 : {
9366 : /* If if_then_else_cond returned zero, then true/false are the
9367 : same rtl. We must copy one of them to prevent invalid rtl
9368 : sharing. */
9369 5081387 : if (cond0 == 0)
9370 1401612 : true0 = copy_rtx (true0);
9371 3679775 : else if (cond1 == 0)
9372 3624002 : true1 = copy_rtx (true1);
9373 :
9374 5081387 : if (COMPARISON_P (x))
9375 : {
9376 272766 : *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9377 : true0, true1);
9378 272766 : *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9379 : false0, false1);
9380 : }
9381 : else
9382 : {
9383 4808621 : *ptrue = simplify_gen_binary (code, mode, true0, true1);
9384 4808621 : *pfalse = simplify_gen_binary (code, mode, false0, false1);
9385 : }
9386 :
9387 5081387 : return cond0 ? cond0 : cond1;
9388 : }
9389 :
9390 : /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9391 : operands is zero when the other is nonzero, and vice-versa,
9392 : and STORE_FLAG_VALUE is 1 or -1. */
9393 :
9394 85116712 : if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9395 85116712 : && (code == PLUS || code == IOR || code == XOR || code == MINUS
9396 : || code == UMAX)
9397 35505709 : && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9398 : {
9399 40068 : rtx op0 = XEXP (XEXP (x, 0), 1);
9400 40068 : rtx op1 = XEXP (XEXP (x, 1), 1);
9401 :
9402 40068 : cond0 = XEXP (XEXP (x, 0), 0);
9403 40068 : cond1 = XEXP (XEXP (x, 1), 0);
9404 :
9405 40068 : if (COMPARISON_P (cond0)
9406 11 : && COMPARISON_P (cond1)
9407 0 : && SCALAR_INT_MODE_P (mode)
9408 0 : && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9409 0 : && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9410 0 : && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9411 0 : || ((swap_condition (GET_CODE (cond0))
9412 0 : == reversed_comparison_code (cond1, NULL))
9413 0 : && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9414 0 : && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9415 40068 : && ! side_effects_p (x))
9416 : {
9417 0 : *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9418 0 : *pfalse = simplify_gen_binary (MULT, mode,
9419 : (code == MINUS
9420 0 : ? simplify_gen_unary (NEG, mode,
9421 : op1, mode)
9422 : : op1),
9423 : const_true_rtx);
9424 0 : return cond0;
9425 : }
9426 : }
9427 :
9428 : /* Similarly for MULT, AND and UMIN, except that for these the result
9429 : is always zero. */
9430 85116712 : if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9431 85116712 : && (code == MULT || code == AND || code == UMIN)
9432 12506157 : && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9433 : {
9434 965 : cond0 = XEXP (XEXP (x, 0), 0);
9435 965 : cond1 = XEXP (XEXP (x, 1), 0);
9436 :
9437 965 : if (COMPARISON_P (cond0)
9438 0 : && COMPARISON_P (cond1)
9439 0 : && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9440 0 : && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9441 0 : && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9442 0 : || ((swap_condition (GET_CODE (cond0))
9443 0 : == reversed_comparison_code (cond1, NULL))
9444 0 : && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9445 0 : && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9446 965 : && ! side_effects_p (x))
9447 : {
9448 0 : *ptrue = *pfalse = const0_rtx;
9449 0 : return cond0;
9450 : }
9451 : }
9452 : }
9453 :
9454 153739488 : else if (code == IF_THEN_ELSE)
9455 : {
9456 : /* If we have IF_THEN_ELSE already, extract the condition and
9457 : canonicalize it if it is NE or EQ. */
9458 763181 : cond0 = XEXP (x, 0);
9459 763181 : *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9460 763181 : if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9461 266117 : return XEXP (cond0, 0);
9462 497064 : else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9463 : {
9464 35852 : *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9465 35852 : return XEXP (cond0, 0);
9466 : }
9467 : else
9468 : return cond0;
9469 : }
9470 :
9471 : /* If X is a SUBREG, we can narrow both the true and false values
9472 : if the inner expression, if there is a condition. */
9473 152976307 : else if (code == SUBREG
9474 152976307 : && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9475 : &false0)) != 0)
9476 : {
9477 1312562 : true0 = simplify_gen_subreg (mode, true0,
9478 656281 : GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9479 1312562 : false0 = simplify_gen_subreg (mode, false0,
9480 656281 : GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9481 656281 : if (true0 && false0)
9482 : {
9483 656281 : *ptrue = true0;
9484 656281 : *pfalse = false0;
9485 656281 : return cond0;
9486 : }
9487 : }
9488 :
9489 : /* If X is a constant, this isn't special and will cause confusions
9490 : if we treat it as such. Likewise if it is equivalent to a constant. */
9491 152320026 : else if (CONSTANT_P (x)
9492 152320026 : || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9493 : ;
9494 :
9495 : /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9496 : will be least confusing to the rest of the compiler. */
9497 103120828 : else if (mode == BImode)
9498 : {
9499 0 : *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9500 0 : return x;
9501 : }
9502 :
9503 : /* If X is known to be either 0 or -1, those are the true and
9504 : false values when testing X. */
9505 103120828 : else if (x == constm1_rtx || x == const0_rtx
9506 103120828 : || (is_a <scalar_int_mode> (mode, &int_mode)
9507 73944795 : && (num_sign_bit_copies (x, int_mode)
9508 73944795 : == GET_MODE_PRECISION (int_mode))))
9509 : {
9510 1032311 : *ptrue = constm1_rtx, *pfalse = const0_rtx;
9511 1032311 : return x;
9512 : }
9513 :
9514 : /* Likewise for 0 or a single bit. */
9515 102088517 : else if (HWI_COMPUTABLE_MODE_P (mode)
9516 69316864 : && pow2p_hwi (nz = nonzero_bits (x, mode)))
9517 : {
9518 1815577 : *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9519 1815577 : return x;
9520 : }
9521 :
9522 : /* Otherwise fail; show no condition with true and false values the same. */
9523 239029597 : *ptrue = *pfalse = x;
9524 239029597 : return 0;
9525 : }
9526 :
9527 : /* Return the value of expression X given the fact that condition COND
9528 : is known to be true when applied to REG as its first operand and VAL
9529 : as its second. X is known to not be shared and so can be modified in
9530 : place.
9531 :
9532 : We only handle the simplest cases, and specifically those cases that
9533 : arise with IF_THEN_ELSE expressions. */
9534 :
9535 : static rtx
9536 680125 : known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9537 : {
9538 680125 : enum rtx_code code = GET_CODE (x);
9539 680125 : const char *fmt;
9540 680125 : int i, j;
9541 :
9542 680125 : if (side_effects_p (x))
9543 : return x;
9544 :
9545 : /* If either operand of the condition is a floating point value,
9546 : then we have to avoid collapsing an EQ comparison. */
9547 680125 : if (cond == EQ
9548 125537 : && rtx_equal_p (x, reg)
9549 83060 : && ! FLOAT_MODE_P (GET_MODE (x))
9550 763185 : && ! FLOAT_MODE_P (GET_MODE (val)))
9551 : return val;
9552 :
9553 : /* If X is (abs REG) and we know something about REG's relationship
9554 : with zero, we may be able to simplify this. */
9555 :
9556 597065 : if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9557 3 : switch (cond)
9558 : {
9559 1 : case GE: case GT: case EQ:
9560 1 : return XEXP (x, 0);
9561 2 : case LT: case LE:
9562 4 : return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9563 : XEXP (x, 0),
9564 2 : GET_MODE (XEXP (x, 0)));
9565 : default:
9566 : break;
9567 : }
9568 :
9569 : /* The only other cases we handle are MIN, MAX, and comparisons if the
9570 : operands are the same as REG and VAL. */
9571 :
9572 597062 : else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9573 : {
9574 245061 : if (rtx_equal_p (XEXP (x, 0), val))
9575 : {
9576 0 : std::swap (val, reg);
9577 0 : cond = swap_condition (cond);
9578 : }
9579 :
9580 245061 : if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9581 : {
9582 222517 : if (COMPARISON_P (x))
9583 : {
9584 222262 : if (comparison_dominates_p (cond, code))
9585 1299 : return VECTOR_MODE_P (GET_MODE (x)) ? x : const_true_rtx;
9586 :
9587 220963 : code = reversed_comparison_code (x, NULL);
9588 220963 : if (code != UNKNOWN
9589 220963 : && comparison_dominates_p (cond, code))
9590 81 : return CONST0_RTX (GET_MODE (x));
9591 : else
9592 : return x;
9593 : }
9594 255 : else if (code == SMAX || code == SMIN
9595 255 : || code == UMIN || code == UMAX)
9596 : {
9597 47 : int unsignedp = (code == UMIN || code == UMAX);
9598 :
9599 : /* Do not reverse the condition when it is NE or EQ.
9600 : This is because we cannot conclude anything about
9601 : the value of 'SMAX (x, y)' when x is not equal to y,
9602 : but we can when x equals y. */
9603 47 : if ((code == SMAX || code == UMAX)
9604 44 : && ! (cond == EQ || cond == NE))
9605 11 : cond = reverse_condition (cond);
9606 :
9607 14 : switch (cond)
9608 : {
9609 4 : case GE: case GT:
9610 4 : return unsignedp ? x : XEXP (x, 1);
9611 10 : case LE: case LT:
9612 10 : return unsignedp ? x : XEXP (x, 0);
9613 0 : case GEU: case GTU:
9614 0 : return unsignedp ? XEXP (x, 1) : x;
9615 0 : case LEU: case LTU:
9616 0 : return unsignedp ? XEXP (x, 0) : x;
9617 : default:
9618 : break;
9619 : }
9620 : }
9621 : }
9622 : }
9623 352001 : else if (code == SUBREG)
9624 : {
9625 9323 : machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9626 9323 : rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9627 :
9628 9323 : if (SUBREG_REG (x) != r)
9629 : {
9630 : /* We must simplify subreg here, before we lose track of the
9631 : original inner_mode. */
9632 334 : new_rtx = simplify_subreg (GET_MODE (x), r,
9633 167 : inner_mode, SUBREG_BYTE (x));
9634 167 : if (new_rtx)
9635 : return new_rtx;
9636 : else
9637 167 : SUBST (SUBREG_REG (x), r);
9638 : }
9639 :
9640 : return x;
9641 : }
9642 : /* We don't have to handle SIGN_EXTEND here, because even in the
9643 : case of replacing something with a modeless CONST_INT, a
9644 : CONST_INT is already (supposed to be) a valid sign extension for
9645 : its narrower mode, which implies it's already properly
9646 : sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9647 : story is different. */
9648 342678 : else if (code == ZERO_EXTEND)
9649 : {
9650 1334 : machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9651 1334 : rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9652 :
9653 1334 : if (XEXP (x, 0) != r)
9654 : {
9655 : /* We must simplify the zero_extend here, before we lose
9656 : track of the original inner_mode. */
9657 0 : new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9658 : r, inner_mode);
9659 0 : if (new_rtx)
9660 : return new_rtx;
9661 : else
9662 0 : SUBST (XEXP (x, 0), r);
9663 : }
9664 :
9665 : return x;
9666 : }
9667 :
9668 364129 : fmt = GET_RTX_FORMAT (code);
9669 825637 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9670 : {
9671 461508 : if (fmt[i] == 'e')
9672 200238 : SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9673 261270 : else if (fmt[i] == 'E')
9674 15144 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9675 12210 : SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9676 : cond, reg, val));
9677 : }
9678 :
9679 : return x;
9680 : }
9681 :
9682 : /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9683 : assignment as a field assignment. */
9684 :
9685 : static bool
9686 986359 : rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9687 : {
9688 986359 : if (widen_x && GET_MODE (x) != GET_MODE (y))
9689 : {
9690 73909 : if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9691 : return false;
9692 73909 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9693 : return false;
9694 73909 : x = adjust_address_nv (x, GET_MODE (y),
9695 : byte_lowpart_offset (GET_MODE (y),
9696 : GET_MODE (x)));
9697 : }
9698 :
9699 986359 : if (x == y || rtx_equal_p (x, y))
9700 : return true;
9701 :
9702 963313 : if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9703 : return false;
9704 :
9705 : /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9706 : Note that all SUBREGs of MEM are paradoxical; otherwise they
9707 : would have been rewritten. */
9708 114494 : if (MEM_P (x) && GET_CODE (y) == SUBREG
9709 6334 : && MEM_P (SUBREG_REG (y))
9710 963059 : && rtx_equal_p (SUBREG_REG (y),
9711 0 : gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9712 : return true;
9713 :
9714 71796 : if (MEM_P (y) && GET_CODE (x) == SUBREG
9715 10430 : && MEM_P (SUBREG_REG (x))
9716 963856 : && rtx_equal_p (SUBREG_REG (x),
9717 797 : gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9718 : return true;
9719 :
9720 : /* We used to see if get_last_value of X and Y were the same but that's
9721 : not correct. In one direction, we'll cause the assignment to have
9722 : the wrong destination and in the case, we'll import a register into this
9723 : insn that might have already have been dead. So fail if none of the
9724 : above cases are true. */
9725 : return false;
9726 : }
9727 :
9728 : /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9729 : Return that assignment if so.
9730 :
9731 : We only handle the most common cases. */
9732 :
9733 : static rtx
9734 48431727 : make_field_assignment (rtx x)
9735 : {
9736 48431727 : rtx dest = SET_DEST (x);
9737 48431727 : rtx src = SET_SRC (x);
9738 48431727 : rtx assign;
9739 48431727 : rtx rhs, lhs;
9740 48431727 : HOST_WIDE_INT c1;
9741 48431727 : HOST_WIDE_INT pos;
9742 48431727 : unsigned HOST_WIDE_INT len;
9743 48431727 : rtx other;
9744 :
9745 : /* All the rules in this function are specific to scalar integers. */
9746 48431727 : scalar_int_mode mode;
9747 48431727 : if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9748 : return x;
9749 :
9750 : /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9751 : a clear of a one-bit field. We will have changed it to
9752 : (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9753 : for a SUBREG. */
9754 :
9755 1307310 : if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9756 1613 : && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9757 549 : && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9758 22471143 : && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9759 : {
9760 157 : assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9761 : 1, true, true, false);
9762 157 : if (assign != 0)
9763 154 : return gen_rtx_SET (assign, const0_rtx);
9764 : return x;
9765 : }
9766 :
9767 1307153 : if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9768 89360 : && subreg_lowpart_p (XEXP (src, 0))
9769 89327 : && partial_subreg_p (XEXP (src, 0))
9770 20647 : && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9771 125 : && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9772 57 : && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9773 22470494 : && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9774 : {
9775 14 : assign = make_extraction (VOIDmode, dest, 0,
9776 7 : XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9777 : 1, true, true, false);
9778 7 : if (assign != 0)
9779 7 : return gen_rtx_SET (assign, const0_rtx);
9780 : return x;
9781 : }
9782 :
9783 : /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9784 : one-bit field. */
9785 1385691 : if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9786 346447 : && XEXP (XEXP (src, 0), 0) == const1_rtx
9787 22472637 : && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9788 : {
9789 578 : assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9790 : 1, true, true, false);
9791 578 : if (assign != 0)
9792 549 : return gen_rtx_SET (assign, const1_rtx);
9793 : return x;
9794 : }
9795 :
9796 : /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9797 : SRC is an AND with all bits of that field set, then we can discard
9798 : the AND. */
9799 22469852 : if (GET_CODE (dest) == ZERO_EXTRACT
9800 2781 : && CONST_INT_P (XEXP (dest, 1))
9801 2781 : && GET_CODE (src) == AND
9802 845 : && CONST_INT_P (XEXP (src, 1)))
9803 : {
9804 845 : HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9805 845 : unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9806 845 : unsigned HOST_WIDE_INT ze_mask;
9807 :
9808 845 : if (width >= HOST_BITS_PER_WIDE_INT)
9809 : ze_mask = -1;
9810 : else
9811 845 : ze_mask = (HOST_WIDE_INT_1U << width) - 1;
9812 :
9813 : /* Complete overlap. We can remove the source AND. */
9814 845 : if ((and_mask & ze_mask) == ze_mask)
9815 791 : return gen_rtx_SET (dest, XEXP (src, 0));
9816 :
9817 : /* Partial overlap. We can reduce the source AND. */
9818 54 : if ((and_mask & ze_mask) != and_mask)
9819 : {
9820 0 : src = gen_rtx_AND (mode, XEXP (src, 0),
9821 : gen_int_mode (and_mask & ze_mask, mode));
9822 0 : return gen_rtx_SET (dest, src);
9823 : }
9824 : }
9825 :
9826 : /* The other case we handle is assignments into a constant-position
9827 : field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9828 : a mask that has all one bits except for a group of zero bits and
9829 : OTHER is known to have zeros where C1 has ones, this is such an
9830 : assignment. Compute the position and length from C1. Shift OTHER
9831 : to the appropriate position, force it to the required mode, and
9832 : make the extraction. Check for the AND in both operands. */
9833 :
9834 : /* One or more SUBREGs might obscure the constant-position field
9835 : assignment. The first one we are likely to encounter is an outer
9836 : narrowing SUBREG, which we can just strip for the purposes of
9837 : identifying the constant-field assignment. */
9838 22469061 : scalar_int_mode src_mode = mode;
9839 22469061 : if (GET_CODE (src) == SUBREG
9840 212478 : && subreg_lowpart_p (src)
9841 22665590 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9842 22469061 : src = SUBREG_REG (src);
9843 :
9844 22469061 : if (GET_CODE (src) != IOR
9845 22469061 : && GET_CODE (src) != XOR
9846 20866796 : && GET_CODE (src) != PLUS)
9847 : return x;
9848 :
9849 8155180 : rhs = expand_compound_operation (XEXP (src, 0));
9850 8155180 : lhs = expand_compound_operation (XEXP (src, 1));
9851 :
9852 8155180 : if (GET_CODE (rhs) == AND
9853 1222856 : && CONST_INT_P (XEXP (rhs, 1))
9854 8962974 : && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9855 22332 : c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9856 : /* The second SUBREG that might get in the way is a paradoxical
9857 : SUBREG around the first operand of the AND. We want to
9858 : pretend the operand is as wide as the destination here. We
9859 : do this by adjusting the MEM to wider mode for the sole
9860 : purpose of the call to rtx_equal_for_field_assignment_p. Also
9861 : note this trick only works for MEMs. */
9862 8132848 : else if (GET_CODE (rhs) == AND
9863 1200524 : && paradoxical_subreg_p (XEXP (rhs, 0))
9864 175441 : && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9865 45969 : && CONST_INT_P (XEXP (rhs, 1))
9866 8178817 : && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9867 : dest, true))
9868 0 : c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9869 8132848 : else if (GET_CODE (lhs) == AND
9870 112629 : && CONST_INT_P (XEXP (lhs, 1))
9871 8234691 : && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9872 30 : c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9873 : /* The second SUBREG that might get in the way is a paradoxical
9874 : SUBREG around the first operand of the AND. We want to
9875 : pretend the operand is as wide as the destination here. We
9876 : do this by adjusting the MEM to wider mode for the sole
9877 : purpose of the call to rtx_equal_for_field_assignment_p. Also
9878 : note this trick only works for MEMs. */
9879 8132818 : else if (GET_CODE (lhs) == AND
9880 112599 : && paradoxical_subreg_p (XEXP (lhs, 0))
9881 58620 : && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9882 27940 : && CONST_INT_P (XEXP (lhs, 1))
9883 8160758 : && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9884 : dest, true))
9885 0 : c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9886 : else
9887 : return x;
9888 :
9889 22362 : pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9890 22362 : if (pos < 0
9891 10852 : || pos + len > GET_MODE_PRECISION (mode)
9892 10852 : || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9893 33202 : || (c1 & nonzero_bits (other, mode)) != 0)
9894 : return x;
9895 :
9896 5967 : assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len,
9897 : true, true, false);
9898 5967 : if (assign == 0)
9899 : return x;
9900 :
9901 : /* The mode to use for the source is the mode of the assignment, or of
9902 : what is inside a possible STRICT_LOW_PART. */
9903 11910 : machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9904 5955 : ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9905 :
9906 : /* Shift OTHER right POS places and make it the source, restricting it
9907 : to the proper length and mode. */
9908 :
9909 5955 : src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9910 : src_mode, other, pos),
9911 : dest);
9912 11910 : src = force_to_mode (src, new_mode,
9913 : len >= HOST_BITS_PER_WIDE_INT
9914 : ? HOST_WIDE_INT_M1U
9915 5955 : : (HOST_WIDE_INT_1U << len) - 1, false);
9916 :
9917 : /* If SRC is masked by an AND that does not make a difference in
9918 : the value being stored, strip it. */
9919 5955 : if (GET_CODE (assign) == ZERO_EXTRACT
9920 5900 : && CONST_INT_P (XEXP (assign, 1))
9921 5900 : && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9922 5900 : && GET_CODE (src) == AND
9923 0 : && CONST_INT_P (XEXP (src, 1))
9924 0 : && UINTVAL (XEXP (src, 1))
9925 0 : == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9926 0 : src = XEXP (src, 0);
9927 :
9928 5955 : return gen_rtx_SET (assign, src);
9929 : }
9930 :
9931 : /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9932 : if so. */
9933 :
9934 : static rtx
9935 53861836 : apply_distributive_law (rtx x)
9936 : {
9937 53861836 : enum rtx_code code = GET_CODE (x);
9938 53861836 : enum rtx_code inner_code;
9939 53861836 : rtx lhs, rhs, other;
9940 53861836 : rtx tem;
9941 :
9942 : /* Distributivity is not true for floating point as it can change the
9943 : value. So we don't do it unless -funsafe-math-optimizations. */
9944 53861836 : if (FLOAT_MODE_P (GET_MODE (x))
9945 3654948 : && ! flag_unsafe_math_optimizations)
9946 : return x;
9947 :
9948 : /* The outer operation can only be one of the following: */
9949 50641104 : if (code != IOR && code != AND && code != XOR
9950 50641104 : && code != PLUS && code != MINUS)
9951 : return x;
9952 :
9953 50627080 : lhs = XEXP (x, 0);
9954 50627080 : rhs = XEXP (x, 1);
9955 :
9956 : /* If either operand is a primitive we can't do anything, so get out
9957 : fast. */
9958 50627080 : if (OBJECT_P (lhs) || OBJECT_P (rhs))
9959 : return x;
9960 :
9961 3702516 : lhs = expand_compound_operation (lhs);
9962 3702516 : rhs = expand_compound_operation (rhs);
9963 3702516 : inner_code = GET_CODE (lhs);
9964 3702516 : if (inner_code != GET_CODE (rhs))
9965 : return x;
9966 :
9967 : /* See if the inner and outer operations distribute. */
9968 1069357 : switch (inner_code)
9969 : {
9970 279834 : case LSHIFTRT:
9971 279834 : case ASHIFTRT:
9972 279834 : case AND:
9973 279834 : case IOR:
9974 : /* These all distribute except over PLUS. */
9975 279834 : if (code == PLUS || code == MINUS)
9976 : return x;
9977 : break;
9978 :
9979 107230 : case MULT:
9980 107230 : if (code != PLUS && code != MINUS)
9981 : return x;
9982 : break;
9983 :
9984 : case ASHIFT:
9985 : /* This is also a multiply, so it distributes over everything. */
9986 : break;
9987 :
9988 : /* This used to handle SUBREG, but this turned out to be counter-
9989 : productive, since (subreg (op ...)) usually is not handled by
9990 : insn patterns, and this "optimization" therefore transformed
9991 : recognizable patterns into unrecognizable ones. Therefore the
9992 : SUBREG case was removed from here.
9993 :
9994 : It is possible that distributing SUBREG over arithmetic operations
9995 : leads to an intermediate result than can then be optimized further,
9996 : e.g. by moving the outer SUBREG to the other side of a SET as done
9997 : in simplify_set. This seems to have been the original intent of
9998 : handling SUBREGs here.
9999 :
10000 : However, with current GCC this does not appear to actually happen,
10001 : at least on major platforms. If some case is found where removing
10002 : the SUBREG case here prevents follow-on optimizations, distributing
10003 : SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
10004 :
10005 : default:
10006 : return x;
10007 : }
10008 :
10009 : /* Set LHS and RHS to the inner operands (A and B in the example
10010 : above) and set OTHER to the common operand (C in the example).
10011 : There is only one way to do this unless the inner operation is
10012 : commutative. */
10013 296626 : if (COMMUTATIVE_ARITH_P (lhs)
10014 296626 : && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
10015 2217 : other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
10016 294409 : else if (COMMUTATIVE_ARITH_P (lhs)
10017 294409 : && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
10018 23 : other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
10019 294386 : else if (COMMUTATIVE_ARITH_P (lhs)
10020 294386 : && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
10021 11522 : other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
10022 282864 : else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
10023 74220 : other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
10024 : else
10025 : return x;
10026 :
10027 : /* Form the new inner operation, seeing if it simplifies first. */
10028 87982 : tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
10029 :
10030 : /* There is one exception to the general way of distributing:
10031 : (a | c) ^ (b | c) -> (a ^ b) & ~c */
10032 87982 : if (code == XOR && inner_code == IOR)
10033 : {
10034 1269 : inner_code = AND;
10035 1269 : other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
10036 : }
10037 :
10038 : /* We may be able to continuing distributing the result, so call
10039 : ourselves recursively on the inner operation before forming the
10040 : outer operation, which we return. */
10041 87982 : return simplify_gen_binary (inner_code, GET_MODE (x),
10042 87982 : apply_distributive_law (tem), other);
10043 : }
10044 :
10045 : /* See if X is of the form (* (+ A B) C), and if so convert to
10046 : (+ (* A C) (* B C)) and try to simplify.
10047 :
10048 : Most of the time, this results in no change. However, if some of
10049 : the operands are the same or inverses of each other, simplifications
10050 : will result.
10051 :
10052 : For example, (and (ior A B) (not B)) can occur as the result of
10053 : expanding a bit field assignment. When we apply the distributive
10054 : law to this, we get (ior (and (A (not B))) (and (B (not B)))),
10055 : which then simplifies to (and (A (not B))).
10056 :
10057 : Note that no checks happen on the validity of applying the inverse
10058 : distributive law. This is pointless since we can do it in the
10059 : few places where this routine is called.
10060 :
10061 : N is the index of the term that is decomposed (the arithmetic operation,
10062 : i.e. (+ A B) in the first example above). !N is the index of the term that
10063 : is distributed, i.e. of C in the first example above. */
10064 : static rtx
10065 1746023 : distribute_and_simplify_rtx (rtx x, int n)
10066 : {
10067 1746023 : machine_mode mode;
10068 1746023 : enum rtx_code outer_code, inner_code;
10069 1746023 : rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
10070 :
10071 : /* Distributivity is not true for floating point as it can change the
10072 : value. So we don't do it unless -funsafe-math-optimizations. */
10073 1746023 : if (FLOAT_MODE_P (GET_MODE (x))
10074 160704 : && ! flag_unsafe_math_optimizations)
10075 : return NULL_RTX;
10076 :
10077 1588212 : decomposed = XEXP (x, n);
10078 1588212 : if (!ARITHMETIC_P (decomposed))
10079 : return NULL_RTX;
10080 :
10081 1588212 : mode = GET_MODE (x);
10082 1588212 : outer_code = GET_CODE (x);
10083 1588212 : distributed = XEXP (x, !n);
10084 :
10085 1588212 : inner_code = GET_CODE (decomposed);
10086 1588212 : inner_op0 = XEXP (decomposed, 0);
10087 1588212 : inner_op1 = XEXP (decomposed, 1);
10088 :
10089 : /* Special case (and (xor B C) (not A)), which is equivalent to
10090 : (xor (ior A B) (ior A C)) */
10091 43483 : if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT
10092 : /* But not when A is a paradoxical subreg since the masking effect
10093 : of the outer AND is lost and the duplicated use can result in
10094 : undefined bits to be exposed when spilling. */
10095 1589476 : && !contains_paradoxical_subreg_p (XEXP (distributed, 0)))
10096 : {
10097 1264 : distributed = XEXP (distributed, 0);
10098 1264 : outer_code = IOR;
10099 : }
10100 :
10101 1588212 : if (n == 0)
10102 : {
10103 : /* Distribute the second term. */
10104 1535115 : new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
10105 1535115 : new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
10106 : }
10107 : else
10108 : {
10109 : /* Distribute the first term. */
10110 53097 : new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
10111 53097 : new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
10112 : }
10113 :
10114 1588212 : tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
10115 : new_op0, new_op1));
10116 1588212 : if (GET_CODE (tmp) != outer_code
10117 1588212 : && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
10118 273681 : < set_src_cost (x, mode, optimize_this_for_speed_p)))
10119 17164 : return tmp;
10120 :
10121 : return NULL_RTX;
10122 : }
10123 :
10124 : /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
10125 : in MODE. Return an equivalent form, if different from (and VAROP
10126 : (const_int CONSTOP)). Otherwise, return NULL_RTX. */
10127 :
10128 : static rtx
10129 12915121 : simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10130 : unsigned HOST_WIDE_INT constop)
10131 : {
10132 12915121 : unsigned HOST_WIDE_INT nonzero;
10133 12915121 : unsigned HOST_WIDE_INT orig_constop;
10134 12915121 : rtx orig_varop;
10135 12915121 : int i;
10136 :
10137 12915121 : orig_varop = varop;
10138 12915121 : orig_constop = constop;
10139 12915121 : if (GET_CODE (varop) == CLOBBER)
10140 : return NULL_RTX;
10141 :
10142 : /* Simplify VAROP knowing that we will be only looking at some of the
10143 : bits in it.
10144 :
10145 : Note by passing in CONSTOP, we guarantee that the bits not set in
10146 : CONSTOP are not significant and will never be examined. We must
10147 : ensure that is the case by explicitly masking out those bits
10148 : before returning. */
10149 12915106 : varop = force_to_mode (varop, mode, constop, false);
10150 :
10151 : /* If VAROP is a CLOBBER, we will fail so return it. */
10152 12915106 : if (GET_CODE (varop) == CLOBBER)
10153 : return varop;
10154 :
10155 : /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
10156 : to VAROP and return the new constant. */
10157 12915060 : if (CONST_INT_P (varop))
10158 304671 : return gen_int_mode (INTVAL (varop) & constop, mode);
10159 :
10160 : /* See what bits may be nonzero in VAROP. Unlike the general case of
10161 : a call to nonzero_bits, here we don't care about bits outside
10162 : MODE unless WORD_REGISTER_OPERATIONS is true. */
10163 :
10164 12610389 : scalar_int_mode tmode = mode;
10165 12610389 : if (WORD_REGISTER_OPERATIONS && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
10166 : tmode = word_mode;
10167 12610389 : nonzero = nonzero_bits (varop, tmode) & GET_MODE_MASK (tmode);
10168 :
10169 : /* Turn off all bits in the constant that are known to already be zero.
10170 : Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
10171 : which is tested below. */
10172 :
10173 12610389 : constop &= nonzero;
10174 :
10175 : /* If we don't have any bits left, return zero. */
10176 12610389 : if (constop == 0 && !side_effects_p (varop))
10177 0 : return const0_rtx;
10178 :
10179 : /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10180 : a power of two, we can replace this with an ASHIFT. */
10181 35126 : if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), tmode) == 1
10182 12616510 : && (i = exact_log2 (constop)) >= 0)
10183 163 : return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10184 :
10185 : /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10186 : or XOR, then try to apply the distributive law. This may eliminate
10187 : operations if either branch can be simplified because of the AND.
10188 : It may also make some cases more complex, but those cases probably
10189 : won't match a pattern either with or without this. */
10190 :
10191 12610226 : if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10192 : {
10193 90156 : scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10194 90156 : return
10195 90156 : gen_lowpart
10196 90156 : (mode,
10197 : apply_distributive_law
10198 90156 : (simplify_gen_binary (GET_CODE (varop), varop_mode,
10199 : simplify_and_const_int (NULL_RTX, varop_mode,
10200 : XEXP (varop, 0),
10201 : constop),
10202 : simplify_and_const_int (NULL_RTX, varop_mode,
10203 : XEXP (varop, 1),
10204 : constop))));
10205 : }
10206 :
10207 : /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10208 : the AND and see if one of the operands simplifies to zero. If so, we
10209 : may eliminate it. */
10210 :
10211 12520070 : if (GET_CODE (varop) == PLUS
10212 12520070 : && pow2p_hwi (constop + 1))
10213 : {
10214 448576 : rtx o0, o1;
10215 :
10216 448576 : o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10217 448576 : o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10218 448576 : if (o0 == const0_rtx)
10219 : return o1;
10220 448576 : if (o1 == const0_rtx)
10221 : return o0;
10222 : }
10223 :
10224 : /* Make a SUBREG if necessary. If we can't make it, fail. */
10225 12519994 : varop = gen_lowpart (mode, varop);
10226 12519994 : if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10227 : return NULL_RTX;
10228 :
10229 : /* If we are only masking insignificant bits, return VAROP. */
10230 12519994 : if (constop == nonzero)
10231 : return varop;
10232 :
10233 12048016 : if (varop == orig_varop && constop == orig_constop)
10234 : return NULL_RTX;
10235 :
10236 : /* Otherwise, return an AND. */
10237 6505079 : return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10238 : }
10239 :
10240 :
10241 : /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10242 : in MODE.
10243 :
10244 : Return an equivalent form, if different from X. Otherwise, return X. If
10245 : X is zero, we are to always construct the equivalent form. */
10246 :
10247 : static rtx
10248 12915121 : simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10249 : unsigned HOST_WIDE_INT constop)
10250 : {
10251 12915121 : rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10252 12915121 : if (tem)
10253 : return tem;
10254 :
10255 5542952 : if (!x)
10256 1419452 : x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10257 1419452 : gen_int_mode (constop, mode));
10258 5542952 : if (GET_MODE (x) != mode)
10259 2 : x = gen_lowpart (mode, x);
10260 : return x;
10261 : }
10262 :
10263 : /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10264 : We don't care about bits outside of those defined in MODE.
10265 : We DO care about all the bits in MODE, even if XMODE is smaller than MODE.
10266 :
10267 : For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10268 : a shift, AND, or zero_extract, we can do better. */
10269 :
10270 : static rtx
10271 473267497 : reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10272 : scalar_int_mode mode,
10273 : unsigned HOST_WIDE_INT *nonzero)
10274 : {
10275 473267497 : rtx tem;
10276 473267497 : reg_stat_type *rsp;
10277 :
10278 : /* If X is a register whose nonzero bits value is current, use it.
10279 : Otherwise, if X is a register whose value we can find, use that
10280 : value. Otherwise, use the previously-computed global nonzero bits
10281 : for this register. */
10282 :
10283 473267497 : rsp = ®_stat[REGNO (x)];
10284 473267497 : if (rsp->last_set_value != 0
10285 437599605 : && (rsp->last_set_mode == mode
10286 1278 : || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10287 0 : && GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10288 0 : && GET_MODE_CLASS (mode) == MODE_INT))
10289 910865824 : && ((rsp->last_set_label >= label_tick_ebb_start
10290 332800910 : && rsp->last_set_label < label_tick)
10291 413949310 : || (rsp->last_set_label == label_tick
10292 309151893 : && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10293 135066963 : || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10294 135013642 : && REGNO (x) < reg_n_sets_max
10295 135013512 : && REG_N_SETS (REGNO (x)) == 1
10296 156045606 : && !REGNO_REG_SET_P
10297 : (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10298 : REGNO (x)))))
10299 : {
10300 : /* Note that, even if the precision of last_set_mode is lower than that
10301 : of mode, record_value_for_reg invoked nonzero_bits on the register
10302 : with nonzero_bits_mode (because last_set_mode is necessarily integral
10303 : and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10304 : are all valid, hence in mode too since nonzero_bits_mode is defined
10305 : to the largest HWI_COMPUTABLE_MODE_P mode. */
10306 380445864 : *nonzero &= rsp->last_set_nonzero_bits;
10307 380445864 : return NULL;
10308 : }
10309 :
10310 92821633 : tem = get_last_value (x);
10311 92821633 : if (tem)
10312 : {
10313 : if (SHORT_IMMEDIATES_SIGN_EXTEND)
10314 : tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10315 :
10316 : return tem;
10317 : }
10318 :
10319 92821627 : if (nonzero_sign_valid && rsp->nonzero_bits)
10320 : {
10321 59011933 : unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10322 :
10323 59011933 : if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10324 : /* We don't know anything about the upper bits. */
10325 0 : mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10326 :
10327 59011933 : *nonzero &= mask;
10328 : }
10329 :
10330 : return NULL;
10331 : }
10332 :
10333 : /* Given a reg X of mode XMODE, return the number of bits at the high-order
10334 : end of X that are known to be equal to the sign bit. X will be used
10335 : in mode MODE; the returned value will always be between 1 and the
10336 : number of bits in MODE. */
10337 :
10338 : static rtx
10339 136691351 : reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10340 : scalar_int_mode mode,
10341 : unsigned int *result)
10342 : {
10343 136691351 : rtx tem;
10344 136691351 : reg_stat_type *rsp;
10345 :
10346 136691351 : rsp = ®_stat[REGNO (x)];
10347 136691351 : if (rsp->last_set_value != 0
10348 124327701 : && rsp->last_set_mode == mode
10349 261018883 : && ((rsp->last_set_label >= label_tick_ebb_start
10350 94561356 : && rsp->last_set_label < label_tick)
10351 118115176 : || (rsp->last_set_label == label_tick
10352 88349000 : && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10353 38109376 : || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10354 38099432 : && REGNO (x) < reg_n_sets_max
10355 38099346 : && REG_N_SETS (REGNO (x)) == 1
10356 44079882 : && !REGNO_REG_SET_P
10357 : (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10358 : REGNO (x)))))
10359 : {
10360 108230519 : *result = rsp->last_set_sign_bit_copies;
10361 108230519 : return NULL;
10362 : }
10363 :
10364 28460832 : tem = get_last_value (x);
10365 28460832 : if (tem != 0)
10366 : return tem;
10367 :
10368 18958025 : if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10369 43249131 : && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10370 14788304 : *result = rsp->sign_bit_copies;
10371 :
10372 : return NULL;
10373 : }
10374 :
10375 : /* Return the number of "extended" bits there are in X, when interpreted
10376 : as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10377 : unsigned quantities, this is the number of high-order zero bits.
10378 : For signed quantities, this is the number of copies of the sign bit
10379 : minus 1. In both case, this function returns the number of "spare"
10380 : bits. For example, if two quantities for which this function returns
10381 : at least 1 are added, the addition is known not to overflow.
10382 :
10383 : This function will always return 0 unless called during combine, which
10384 : implies that it must be called from a define_split. */
10385 :
10386 : unsigned int
10387 0 : extended_count (const_rtx x, machine_mode mode, bool unsignedp)
10388 : {
10389 0 : if (nonzero_sign_valid == 0)
10390 : return 0;
10391 :
10392 0 : scalar_int_mode int_mode;
10393 0 : return (unsignedp
10394 0 : ? (is_a <scalar_int_mode> (mode, &int_mode)
10395 0 : && HWI_COMPUTABLE_MODE_P (int_mode)
10396 0 : ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10397 0 : - floor_log2 (nonzero_bits (x, int_mode)))
10398 : : 0)
10399 0 : : num_sign_bit_copies (x, mode) - 1);
10400 : }
10401 :
10402 : /* This function is called from `simplify_shift_const' to merge two
10403 : outer operations. Specifically, we have already found that we need
10404 : to perform operation *POP0 with constant *PCONST0 at the outermost
10405 : position. We would now like to also perform OP1 with constant CONST1
10406 : (with *POP0 being done last).
10407 :
10408 : Return true if we can do the operation and update *POP0 and *PCONST0 with
10409 : the resulting operation. *PCOMP_P is set to true if we would need to
10410 : complement the innermost operand, otherwise it is unchanged.
10411 :
10412 : MODE is the mode in which the operation will be done. No bits outside
10413 : the width of this mode matter. It is assumed that the width of this mode
10414 : is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10415 :
10416 : If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10417 : IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10418 : result is simply *PCONST0.
10419 :
10420 : If the resulting operation cannot be expressed as one operation, we
10421 : return false and do not change *POP0, *PCONST0, and *PCOMP_P. */
10422 :
10423 : static bool
10424 4037377 : merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0,
10425 : enum rtx_code op1, HOST_WIDE_INT const1,
10426 : machine_mode mode, bool *pcomp_p)
10427 : {
10428 4037377 : enum rtx_code op0 = *pop0;
10429 4037377 : HOST_WIDE_INT const0 = *pconst0;
10430 :
10431 4037377 : const0 &= GET_MODE_MASK (mode);
10432 4037377 : const1 &= GET_MODE_MASK (mode);
10433 :
10434 : /* If OP0 is an AND, clear unimportant bits in CONST1. */
10435 4037377 : if (op0 == AND)
10436 26499 : const1 &= const0;
10437 :
10438 : /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10439 : if OP0 is SET. */
10440 :
10441 4037377 : if (op1 == UNKNOWN || op0 == SET)
10442 : return true;
10443 :
10444 4037377 : else if (op0 == UNKNOWN)
10445 : op0 = op1, const0 = const1;
10446 :
10447 78996 : else if (op0 == op1)
10448 : {
10449 26207 : switch (op0)
10450 : {
10451 26207 : case AND:
10452 26207 : const0 &= const1;
10453 26207 : break;
10454 0 : case IOR:
10455 0 : const0 |= const1;
10456 0 : break;
10457 0 : case XOR:
10458 0 : const0 ^= const1;
10459 0 : break;
10460 0 : case PLUS:
10461 0 : const0 += const1;
10462 0 : break;
10463 : case NEG:
10464 4009523 : op0 = UNKNOWN;
10465 : break;
10466 : default:
10467 : break;
10468 : }
10469 : }
10470 :
10471 : /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10472 52789 : else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10473 : return false;
10474 :
10475 : /* If the two constants aren't the same, we can't do anything. The
10476 : remaining six cases can all be done. */
10477 26127 : else if (const0 != const1)
10478 : return false;
10479 :
10480 : else
10481 24935 : switch (op0)
10482 : {
10483 8 : case IOR:
10484 8 : if (op1 == AND)
10485 : /* (a & b) | b == b */
10486 0 : op0 = SET;
10487 : else /* op1 == XOR */
10488 : /* (a ^ b) | b == a | b */
10489 : {;}
10490 : break;
10491 :
10492 24639 : case XOR:
10493 24639 : if (op1 == AND)
10494 : /* (a & b) ^ b == (~a) & b */
10495 24639 : op0 = AND, *pcomp_p = true;
10496 : else /* op1 == IOR */
10497 : /* (a | b) ^ b == a & ~b */
10498 0 : op0 = AND, const0 = ~const0;
10499 : break;
10500 :
10501 288 : case AND:
10502 288 : if (op1 == IOR)
10503 : /* (a | b) & b == b */
10504 : op0 = SET;
10505 : else /* op1 == XOR */
10506 : /* (a ^ b) & b) == (~a) & b */
10507 288 : *pcomp_p = true;
10508 : break;
10509 : default:
10510 : break;
10511 : }
10512 :
10513 : /* Check for NO-OP cases. */
10514 4009523 : const0 &= GET_MODE_MASK (mode);
10515 4009523 : if (const0 == 0
10516 19943 : && (op0 == IOR || op0 == XOR || op0 == PLUS))
10517 : op0 = UNKNOWN;
10518 4008107 : else if (const0 == 0 && op0 == AND)
10519 : op0 = SET;
10520 4008107 : else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10521 25136 : && op0 == AND)
10522 4009523 : op0 = UNKNOWN;
10523 :
10524 4009523 : *pop0 = op0;
10525 :
10526 : /* ??? Slightly redundant with the above mask, but not entirely.
10527 : Moving this above means we'd have to sign-extend the mode mask
10528 : for the final test. */
10529 4009523 : if (op0 != UNKNOWN && op0 != NEG)
10530 3972957 : *pconst0 = trunc_int_for_mode (const0, mode);
10531 :
10532 : return true;
10533 : }
10534 :
10535 : /* A helper to simplify_shift_const_1 to determine the mode we can perform
10536 : the shift in. The original shift operation CODE is performed on OP in
10537 : ORIG_MODE. Return the wider mode MODE if we can perform the operation
10538 : in that mode. Return ORIG_MODE otherwise. We can also assume that the
10539 : result of the shift is subject to operation OUTER_CODE with operand
10540 : OUTER_CONST. */
10541 :
10542 : static scalar_int_mode
10543 413174 : try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10544 : scalar_int_mode orig_mode, scalar_int_mode mode,
10545 : enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10546 : {
10547 413174 : gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10548 :
10549 : /* In general we can't perform in wider mode for right shift and rotate. */
10550 413174 : switch (code)
10551 : {
10552 30117 : case ASHIFTRT:
10553 : /* We can still widen if the bits brought in from the left are identical
10554 : to the sign bit of ORIG_MODE. */
10555 30117 : if (num_sign_bit_copies (op, mode)
10556 30117 : > (unsigned) (GET_MODE_PRECISION (mode)
10557 30117 : - GET_MODE_PRECISION (orig_mode)))
10558 336 : return mode;
10559 29781 : return orig_mode;
10560 :
10561 75878 : case LSHIFTRT:
10562 : /* Similarly here but with zero bits. */
10563 75878 : if (HWI_COMPUTABLE_MODE_P (mode)
10564 75878 : && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10565 6463 : return mode;
10566 :
10567 : /* We can also widen if the bits brought in will be masked off. This
10568 : operation is performed in ORIG_MODE. */
10569 69415 : if (outer_code == AND)
10570 : {
10571 27855 : int care_bits = low_bitmask_len (orig_mode, outer_const);
10572 :
10573 27855 : if (care_bits >= 0
10574 27855 : && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10575 27835 : return mode;
10576 : }
10577 : /* fall through */
10578 :
10579 42066 : case ROTATE:
10580 42066 : return orig_mode;
10581 :
10582 0 : case ROTATERT:
10583 0 : gcc_unreachable ();
10584 :
10585 306693 : default:
10586 306693 : return mode;
10587 : }
10588 : }
10589 :
10590 : /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10591 : of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10592 : if we cannot simplify it. Otherwise, return a simplified value.
10593 :
10594 : The shift is normally computed in the widest mode we find in VAROP, as
10595 : long as it isn't a different number of words than RESULT_MODE. Exceptions
10596 : are ASHIFTRT and ROTATE, which are always done in their original mode. */
10597 :
10598 : static rtx
10599 25429967 : simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10600 : rtx varop, int orig_count)
10601 : {
10602 25429967 : enum rtx_code orig_code = code;
10603 25429967 : rtx orig_varop = varop;
10604 25429967 : int count, log2;
10605 25429967 : machine_mode mode = result_mode;
10606 25429967 : machine_mode shift_mode;
10607 25429967 : scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10608 : /* We form (outer_op (code varop count) (outer_const)). */
10609 25429967 : enum rtx_code outer_op = UNKNOWN;
10610 25429967 : HOST_WIDE_INT outer_const = 0;
10611 25429967 : bool complement_p = false;
10612 25429967 : rtx new_rtx, x;
10613 :
10614 : /* Make sure and truncate the "natural" shift on the way in. We don't
10615 : want to do this inside the loop as it makes it more difficult to
10616 : combine shifts. */
10617 25429967 : if (SHIFT_COUNT_TRUNCATED)
10618 : orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10619 :
10620 : /* If we were given an invalid count, don't do anything except exactly
10621 : what was requested. */
10622 :
10623 50859874 : if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10624 : return NULL_RTX;
10625 :
10626 : count = orig_count;
10627 :
10628 : /* Unless one of the branches of the `if' in this loop does a `continue',
10629 : we will `break' the loop after the `if'. */
10630 :
10631 29813766 : while (count != 0)
10632 : {
10633 : /* If we have an operand of (clobber (const_int 0)), fail. */
10634 25499089 : if (GET_CODE (varop) == CLOBBER)
10635 25429967 : return NULL_RTX;
10636 :
10637 : /* Convert ROTATERT to ROTATE. */
10638 25499089 : if (code == ROTATERT)
10639 : {
10640 11602 : unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10641 11602 : code = ROTATE;
10642 11602 : count = bitsize - count;
10643 : }
10644 :
10645 25499089 : shift_mode = result_mode;
10646 25499089 : if (shift_mode != mode)
10647 : {
10648 : /* We only change the modes of scalar shifts. */
10649 212609 : int_mode = as_a <scalar_int_mode> (mode);
10650 212609 : int_result_mode = as_a <scalar_int_mode> (result_mode);
10651 212609 : shift_mode = try_widen_shift_mode (code, varop, count,
10652 : int_result_mode, int_mode,
10653 : outer_op, outer_const);
10654 : }
10655 :
10656 25499089 : scalar_int_mode shift_unit_mode;
10657 50998178 : if (!is_a <scalar_int_mode> (GET_MODE_INNER (shift_mode),
10658 : &shift_unit_mode))
10659 : return NULL_RTX;
10660 :
10661 : /* Handle cases where the count is greater than the size of the mode
10662 : minus 1. For ASHIFT, use the size minus one as the count (this can
10663 : occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10664 : take the count modulo the size. For other shifts, the result is
10665 : zero.
10666 :
10667 : Since these shifts are being produced by the compiler by combining
10668 : multiple operations, each of which are defined, we know what the
10669 : result is supposed to be. */
10670 :
10671 25499089 : if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10672 : {
10673 12739 : if (code == ASHIFTRT)
10674 12733 : count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10675 6 : else if (code == ROTATE || code == ROTATERT)
10676 6 : count %= GET_MODE_PRECISION (shift_unit_mode);
10677 : else
10678 : {
10679 : /* We can't simply return zero because there may be an
10680 : outer op. */
10681 0 : varop = const0_rtx;
10682 0 : count = 0;
10683 0 : break;
10684 : }
10685 : }
10686 :
10687 : /* If we discovered we had to complement VAROP, leave. Making a NOT
10688 : here would cause an infinite loop. */
10689 25499089 : if (complement_p)
10690 : break;
10691 :
10692 25485426 : if (shift_mode == shift_unit_mode)
10693 : {
10694 : /* An arithmetic right shift of a quantity known to be -1 or 0
10695 : is a no-op. */
10696 24795022 : if (code == ASHIFTRT
10697 24795022 : && (num_sign_bit_copies (varop, shift_unit_mode)
10698 4638050 : == GET_MODE_PRECISION (shift_unit_mode)))
10699 : {
10700 : count = 0;
10701 : break;
10702 : }
10703 :
10704 : /* If we are doing an arithmetic right shift and discarding all but
10705 : the sign bit copies, this is equivalent to doing a shift by the
10706 : bitsize minus one. Convert it into that shift because it will
10707 : often allow other simplifications. */
10708 :
10709 24794949 : if (code == ASHIFTRT
10710 24794949 : && (count + num_sign_bit_copies (varop, shift_unit_mode)
10711 4637977 : >= GET_MODE_PRECISION (shift_unit_mode)))
10712 346480 : count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10713 :
10714 : /* We simplify the tests below and elsewhere by converting
10715 : ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10716 : `make_compound_operation' will convert it to an ASHIFTRT for
10717 : those machines (such as VAX) that don't have an LSHIFTRT. */
10718 24794949 : if (code == ASHIFTRT
10719 4637977 : && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10720 29408780 : && val_signbit_known_clear_p (shift_unit_mode,
10721 : nonzero_bits (varop,
10722 : shift_unit_mode)))
10723 : code = LSHIFTRT;
10724 :
10725 24765819 : if (((code == LSHIFTRT
10726 6283331 : && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10727 6261013 : && !(nonzero_bits (varop, shift_unit_mode) >> count))
10728 24793049 : || (code == ASHIFT
10729 13873955 : && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10730 13385793 : && !((nonzero_bits (varop, shift_unit_mode) << count)
10731 13385793 : & GET_MODE_MASK (shift_unit_mode))))
10732 24770010 : && !side_effects_p (varop))
10733 4191 : varop = const0_rtx;
10734 : }
10735 :
10736 25485353 : switch (GET_CODE (varop))
10737 : {
10738 527198 : case SIGN_EXTEND:
10739 527198 : case ZERO_EXTEND:
10740 527198 : case SIGN_EXTRACT:
10741 527198 : case ZERO_EXTRACT:
10742 527198 : new_rtx = expand_compound_operation (varop);
10743 527198 : if (new_rtx != varop)
10744 : {
10745 68775 : varop = new_rtx;
10746 4384071 : continue;
10747 : }
10748 : break;
10749 :
10750 344016 : case MEM:
10751 : /* The following rules apply only to scalars. */
10752 344016 : if (shift_mode != shift_unit_mode)
10753 : break;
10754 330500 : int_mode = as_a <scalar_int_mode> (mode);
10755 :
10756 : /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10757 : minus the width of a smaller mode, we can do this with a
10758 : SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10759 334036 : if ((code == ASHIFTRT || code == LSHIFTRT)
10760 125212 : && ! mode_dependent_address_p (XEXP (varop, 0),
10761 125212 : MEM_ADDR_SPACE (varop))
10762 125212 : && ! MEM_VOLATILE_P (varop)
10763 453788 : && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10764 326964 : .exists (&tmode)))
10765 : {
10766 3536 : new_rtx = adjust_address_nv (varop, tmode,
10767 : BYTES_BIG_ENDIAN ? 0
10768 : : count / BITS_PER_UNIT);
10769 :
10770 3536 : varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10771 : : ZERO_EXTEND, int_mode, new_rtx);
10772 3536 : count = 0;
10773 3536 : continue;
10774 : }
10775 : break;
10776 :
10777 5345709 : case SUBREG:
10778 : /* The following rules apply only to scalars. */
10779 5345709 : if (shift_mode != shift_unit_mode)
10780 : break;
10781 4892827 : int_mode = as_a <scalar_int_mode> (mode);
10782 4892827 : int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10783 :
10784 : /* If VAROP is a SUBREG, strip it as long as the inner operand has
10785 : the same number of words as what we've seen so far. Then store
10786 : the widest mode in MODE. */
10787 4892827 : if (subreg_lowpart_p (varop)
10788 30275411 : && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10789 9691432 : && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10790 232053 : && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10791 214072 : == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10792 5093978 : && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10793 : {
10794 201151 : varop = SUBREG_REG (varop);
10795 603453 : if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10796 201151 : mode = inner_mode;
10797 201151 : continue;
10798 : }
10799 : break;
10800 :
10801 428085 : case MULT:
10802 : /* Some machines use MULT instead of ASHIFT because MULT
10803 : is cheaper. But it is still better on those machines to
10804 : merge two shifts into one. */
10805 428085 : if (CONST_INT_P (XEXP (varop, 1))
10806 245809 : && INTVAL (XEXP (varop, 1)) > 0
10807 664444 : && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10808 : {
10809 0 : rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10810 0 : varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10811 : XEXP (varop, 0), log2_rtx);
10812 0 : continue;
10813 0 : }
10814 : break;
10815 :
10816 8813 : case UDIV:
10817 : /* Similar, for when divides are cheaper. */
10818 8813 : if (CONST_INT_P (XEXP (varop, 1))
10819 257 : && INTVAL (XEXP (varop, 1)) > 0
10820 8833 : && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10821 : {
10822 9 : rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10823 9 : varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10824 : XEXP (varop, 0), log2_rtx);
10825 9 : continue;
10826 9 : }
10827 : break;
10828 :
10829 388410 : case ASHIFTRT:
10830 : /* If we are extracting just the sign bit of an arithmetic
10831 : right shift, that shift is not needed. However, the sign
10832 : bit of a wider mode may be different from what would be
10833 : interpreted as the sign bit in a narrower mode, so, if
10834 : the result is narrower, don't discard the shift. */
10835 390342 : if (code == LSHIFTRT
10836 15971 : && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10837 388410 : && (GET_MODE_UNIT_BITSIZE (result_mode)
10838 3890 : >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10839 : {
10840 1932 : varop = XEXP (varop, 0);
10841 1932 : continue;
10842 : }
10843 :
10844 : /* fall through */
10845 :
10846 6398924 : case LSHIFTRT:
10847 6398924 : case ASHIFT:
10848 6398924 : case ROTATE:
10849 : /* The following rules apply only to scalars. */
10850 6398924 : if (shift_mode != shift_unit_mode)
10851 : break;
10852 6390891 : int_mode = as_a <scalar_int_mode> (mode);
10853 6390891 : int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10854 6390891 : int_result_mode = as_a <scalar_int_mode> (result_mode);
10855 :
10856 : /* Here we have two nested shifts. The result is usually the
10857 : AND of a new shift with a mask. We compute the result below. */
10858 6390891 : if (CONST_INT_P (XEXP (varop, 1))
10859 6370491 : && INTVAL (XEXP (varop, 1)) >= 0
10860 6370488 : && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10861 6370488 : && HWI_COMPUTABLE_MODE_P (int_result_mode)
10862 12730484 : && HWI_COMPUTABLE_MODE_P (int_mode))
10863 : {
10864 6339593 : enum rtx_code first_code = GET_CODE (varop);
10865 6339593 : unsigned int first_count = INTVAL (XEXP (varop, 1));
10866 6339593 : unsigned HOST_WIDE_INT mask;
10867 6339593 : rtx mask_rtx;
10868 :
10869 : /* We have one common special case. We can't do any merging if
10870 : the inner code is an ASHIFTRT of a smaller mode. However, if
10871 : we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10872 : with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10873 : we can convert it to
10874 : (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10875 : This simplifies certain SIGN_EXTEND operations. */
10876 6339593 : if (code == ASHIFT && first_code == ASHIFTRT
10877 6339593 : && count == (GET_MODE_PRECISION (int_result_mode)
10878 349994 : - GET_MODE_PRECISION (int_varop_mode)))
10879 : {
10880 : /* C3 has the low-order C1 bits zero. */
10881 :
10882 0 : mask = GET_MODE_MASK (int_mode)
10883 0 : & ~((HOST_WIDE_INT_1U << first_count) - 1);
10884 :
10885 0 : varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10886 : XEXP (varop, 0), mask);
10887 0 : varop = simplify_shift_const (NULL_RTX, ASHIFT,
10888 : int_result_mode, varop, count);
10889 0 : count = first_count;
10890 0 : code = ASHIFTRT;
10891 0 : continue;
10892 : }
10893 :
10894 : /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10895 : than C1 high-order bits equal to the sign bit, we can convert
10896 : this to either an ASHIFT or an ASHIFTRT depending on the
10897 : two counts.
10898 :
10899 : We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10900 :
10901 6341120 : if (code == ASHIFTRT && first_code == ASHIFT
10902 3068081 : && int_varop_mode == shift_unit_mode
10903 9400736 : && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10904 : > first_count))
10905 : {
10906 1527 : varop = XEXP (varop, 0);
10907 1527 : count -= first_count;
10908 1527 : if (count < 0)
10909 : {
10910 4 : count = -count;
10911 4 : code = ASHIFT;
10912 : }
10913 :
10914 1527 : continue;
10915 : }
10916 :
10917 : /* There are some cases we can't do. If CODE is ASHIFTRT,
10918 : we can only do this if FIRST_CODE is also ASHIFTRT.
10919 :
10920 : We can't do the case when CODE is ROTATE and FIRST_CODE is
10921 : ASHIFTRT.
10922 :
10923 : If the mode of this shift is not the mode of the outer shift,
10924 : we can't do this if either shift is a right shift or ROTATE.
10925 :
10926 : Finally, we can't do any of these if the mode is too wide
10927 : unless the codes are the same.
10928 :
10929 : Handle the case where the shift codes are the same
10930 : first. */
10931 :
10932 6338066 : if (code == first_code)
10933 : {
10934 31733 : if (int_varop_mode != int_result_mode
10935 31733 : && (code == ASHIFTRT || code == LSHIFTRT
10936 715 : || code == ROTATE))
10937 : break;
10938 :
10939 31072 : count += first_count;
10940 31072 : varop = XEXP (varop, 0);
10941 31072 : continue;
10942 : }
10943 :
10944 6306333 : if (code == ASHIFTRT
10945 3239731 : || (code == ROTATE && first_code == ASHIFTRT)
10946 3239701 : || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10947 9546034 : || (int_varop_mode != int_result_mode
10948 82888 : && (first_code == ASHIFTRT || first_code == LSHIFTRT
10949 82888 : || first_code == ROTATE
10950 27280 : || code == ROTATE)))
10951 : break;
10952 :
10953 : /* To compute the mask to apply after the shift, shift the
10954 : nonzero bits of the inner shift the same way the
10955 : outer shift will. */
10956 :
10957 3184093 : mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10958 : int_result_mode);
10959 3184093 : rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10960 3184093 : mask_rtx
10961 3184093 : = simplify_const_binary_operation (code, int_result_mode,
10962 : mask_rtx, count_rtx);
10963 :
10964 : /* Give up if we can't compute an outer operation to use. */
10965 3184093 : if (mask_rtx == 0
10966 3184093 : || !CONST_INT_P (mask_rtx)
10967 6368186 : || ! merge_outer_ops (&outer_op, &outer_const, AND,
10968 : INTVAL (mask_rtx),
10969 : int_result_mode, &complement_p))
10970 : break;
10971 :
10972 : /* If the shifts are in the same direction, we add the
10973 : counts. Otherwise, we subtract them. */
10974 3157786 : if ((code == ASHIFTRT || code == LSHIFTRT)
10975 3157786 : == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10976 12034 : count += first_count;
10977 : else
10978 3145752 : count -= first_count;
10979 :
10980 : /* If COUNT is positive, the new shift is usually CODE,
10981 : except for the two exceptions below, in which case it is
10982 : FIRST_CODE. If the count is negative, FIRST_CODE should
10983 : always be used */
10984 3157786 : if (count > 0
10985 716897 : && ((first_code == ROTATE && code == ASHIFT)
10986 716391 : || (first_code == ASHIFTRT && code == LSHIFTRT)))
10987 : code = first_code;
10988 3145758 : else if (count < 0)
10989 328849 : code = first_code, count = -count;
10990 :
10991 3157786 : varop = XEXP (varop, 0);
10992 3157786 : continue;
10993 3157786 : }
10994 :
10995 : /* If we have (A << B << C) for any shift, we can convert this to
10996 : (A << C << B). This wins if A is a constant. Only try this if
10997 : B is not a constant. */
10998 :
10999 51298 : else if (GET_CODE (varop) == code
11000 5291 : && CONST_INT_P (XEXP (varop, 0))
11001 1047 : && !CONST_INT_P (XEXP (varop, 1)))
11002 : {
11003 : /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
11004 : sure the result will be masked. See PR70222. */
11005 1047 : if (code == LSHIFTRT
11006 7 : && int_mode != int_result_mode
11007 1054 : && !merge_outer_ops (&outer_op, &outer_const, AND,
11008 7 : GET_MODE_MASK (int_result_mode)
11009 7 : >> orig_count, int_result_mode,
11010 : &complement_p))
11011 : break;
11012 : /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
11013 : up outer sign extension (often left and right shift) is
11014 : hardly more efficient than the original. See PR70429.
11015 : Similarly punt for rotates with different modes.
11016 : See PR97386. */
11017 1047 : if ((code == ASHIFTRT || code == ROTATE)
11018 1047 : && int_mode != int_result_mode)
11019 : break;
11020 :
11021 1033 : rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
11022 1033 : rtx new_rtx = simplify_const_binary_operation (code, int_mode,
11023 : XEXP (varop, 0),
11024 : count_rtx);
11025 1033 : varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
11026 1033 : count = 0;
11027 1033 : continue;
11028 1033 : }
11029 : break;
11030 :
11031 60213 : case NOT:
11032 : /* The following rules apply only to scalars. */
11033 60213 : if (shift_mode != shift_unit_mode)
11034 : break;
11035 :
11036 : /* Make this fit the case below. */
11037 60196 : varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
11038 60196 : continue;
11039 :
11040 737236 : case IOR:
11041 737236 : case AND:
11042 737236 : case XOR:
11043 : /* The following rules apply only to scalars. */
11044 737236 : if (shift_mode != shift_unit_mode)
11045 : break;
11046 733119 : int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11047 733119 : int_result_mode = as_a <scalar_int_mode> (result_mode);
11048 :
11049 : /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
11050 : with C the size of VAROP - 1 and the shift is logical if
11051 : STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11052 : we have an (le X 0) operation. If we have an arithmetic shift
11053 : and STORE_FLAG_VALUE is 1 or we have a logical shift with
11054 : STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
11055 :
11056 188891 : if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
11057 1560 : && XEXP (XEXP (varop, 0), 1) == constm1_rtx
11058 : && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11059 140 : && (code == LSHIFTRT || code == ASHIFTRT)
11060 140 : && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11061 733259 : && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11062 : {
11063 56 : count = 0;
11064 56 : varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
11065 : const0_rtx);
11066 :
11067 56 : if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11068 56 : varop = gen_rtx_NEG (int_varop_mode, varop);
11069 :
11070 56 : continue;
11071 : }
11072 :
11073 : /* If we have (shift (logical)), move the logical to the outside
11074 : to allow it to possibly combine with another logical and the
11075 : shift to combine with another shift. This also canonicalizes to
11076 : what a ZERO_EXTRACT looks like. Also, some machines have
11077 : (and (shift)) insns. */
11078 :
11079 1169624 : if (CONST_INT_P (XEXP (varop, 1))
11080 : /* We can't do this if we have (ashiftrt (xor)) and the
11081 : constant has its sign bit set in shift_unit_mode with
11082 : shift_unit_mode wider than result_mode. */
11083 437935 : && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11084 8748 : && int_result_mode != shift_unit_mode
11085 0 : && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11086 : shift_unit_mode) < 0)
11087 437935 : && (new_rtx = simplify_const_binary_operation
11088 437935 : (code, int_result_mode,
11089 437935 : gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11090 437935 : gen_int_shift_amount (int_result_mode, count))) != 0
11091 437935 : && CONST_INT_P (new_rtx)
11092 1170994 : && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
11093 : INTVAL (new_rtx), int_result_mode,
11094 : &complement_p))
11095 : {
11096 436561 : varop = XEXP (varop, 0);
11097 436561 : continue;
11098 : }
11099 :
11100 : /* If we can't do that, try to simplify the shift in each arm of the
11101 : logical expression, make a new logical expression, and apply
11102 : the inverse distributive law. This also can't be done for
11103 : (ashiftrt (xor)) where we've widened the shift and the constant
11104 : changes the sign bit. */
11105 296502 : if (CONST_INT_P (XEXP (varop, 1))
11106 296502 : && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11107 48 : && int_result_mode != shift_unit_mode
11108 0 : && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11109 : shift_unit_mode) < 0))
11110 : {
11111 1374 : rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11112 : XEXP (varop, 0), count);
11113 1374 : rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11114 : XEXP (varop, 1), count);
11115 :
11116 1374 : varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
11117 : lhs, rhs);
11118 1374 : varop = apply_distributive_law (varop);
11119 :
11120 1374 : count = 0;
11121 1374 : continue;
11122 1374 : }
11123 : break;
11124 :
11125 34594 : case EQ:
11126 : /* The following rules apply only to scalars. */
11127 34594 : if (shift_mode != shift_unit_mode)
11128 : break;
11129 34594 : int_result_mode = as_a <scalar_int_mode> (result_mode);
11130 :
11131 : /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
11132 : says that the sign bit can be tested, FOO has mode MODE, C is
11133 : GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11134 : that may be nonzero. */
11135 34594 : if (code == LSHIFTRT
11136 0 : && XEXP (varop, 1) == const0_rtx
11137 0 : && GET_MODE (XEXP (varop, 0)) == int_result_mode
11138 0 : && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11139 34594 : && HWI_COMPUTABLE_MODE_P (int_result_mode)
11140 : && STORE_FLAG_VALUE == -1
11141 : && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11142 : && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11143 : int_result_mode, &complement_p))
11144 : {
11145 : varop = XEXP (varop, 0);
11146 : count = 0;
11147 : continue;
11148 : }
11149 : break;
11150 :
11151 27981 : case NEG:
11152 : /* The following rules apply only to scalars. */
11153 27981 : if (shift_mode != shift_unit_mode)
11154 : break;
11155 27851 : int_result_mode = as_a <scalar_int_mode> (result_mode);
11156 :
11157 : /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11158 : than the number of bits in the mode is equivalent to A. */
11159 27856 : if (code == LSHIFTRT
11160 5856 : && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11161 30586 : && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11162 : {
11163 5 : varop = XEXP (varop, 0);
11164 5 : count = 0;
11165 5 : continue;
11166 : }
11167 :
11168 : /* NEG commutes with ASHIFT since it is multiplication. Move the
11169 : NEG outside to allow shifts to combine. */
11170 46373 : if (code == ASHIFT
11171 27846 : && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11172 : int_result_mode, &complement_p))
11173 : {
11174 18527 : varop = XEXP (varop, 0);
11175 18527 : continue;
11176 : }
11177 : break;
11178 :
11179 2075174 : case PLUS:
11180 : /* The following rules apply only to scalars. */
11181 2075174 : if (shift_mode != shift_unit_mode)
11182 : break;
11183 2015437 : int_result_mode = as_a <scalar_int_mode> (result_mode);
11184 :
11185 : /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11186 : is one less than the number of bits in the mode is
11187 : equivalent to (xor A 1). */
11188 2015437 : if (code == LSHIFTRT
11189 422367 : && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11190 34813 : && XEXP (varop, 1) == constm1_rtx
11191 16752 : && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11192 2015437 : && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11193 : int_result_mode, &complement_p))
11194 : {
11195 0 : count = 0;
11196 0 : varop = XEXP (varop, 0);
11197 0 : continue;
11198 : }
11199 :
11200 : /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11201 : that might be nonzero in BAR are those being shifted out and those
11202 : bits are known zero in FOO, we can replace the PLUS with FOO.
11203 : Similarly in the other operand order. This code occurs when
11204 : we are computing the size of a variable-size array. */
11205 :
11206 2018805 : if ((code == ASHIFTRT || code == LSHIFTRT)
11207 594997 : && count < HOST_BITS_PER_WIDE_INT
11208 593828 : && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11209 2196581 : && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11210 181144 : & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11211 : {
11212 3368 : varop = XEXP (varop, 0);
11213 3368 : continue;
11214 : }
11215 2012101 : else if ((code == ASHIFTRT || code == LSHIFTRT)
11216 591629 : && count < HOST_BITS_PER_WIDE_INT
11217 590460 : && HWI_COMPUTABLE_MODE_P (int_result_mode)
11218 589222 : && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11219 589222 : >> count) == 0
11220 2107634 : && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11221 95565 : & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11222 : {
11223 32 : varop = XEXP (varop, 1);
11224 32 : continue;
11225 : }
11226 :
11227 : /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11228 2399616 : if (code == ASHIFT
11229 1411610 : && CONST_INT_P (XEXP (varop, 1))
11230 387752 : && (new_rtx = simplify_const_binary_operation
11231 387752 : (ASHIFT, int_result_mode,
11232 387752 : gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11233 387752 : gen_int_shift_amount (int_result_mode, count))) != 0
11234 387752 : && CONST_INT_P (new_rtx)
11235 2399789 : && merge_outer_ops (&outer_op, &outer_const, PLUS,
11236 : INTVAL (new_rtx), int_result_mode,
11237 : &complement_p))
11238 : {
11239 387579 : varop = XEXP (varop, 0);
11240 387579 : continue;
11241 : }
11242 :
11243 : /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11244 : signbit', and attempt to change the PLUS to an XOR and move it to
11245 : the outer operation as is done above in the AND/IOR/XOR case
11246 : leg for shift(logical). See details in logical handling above
11247 : for reasoning in doing so. */
11248 1633521 : if (code == LSHIFTRT
11249 419069 : && CONST_INT_P (XEXP (varop, 1))
11250 294599 : && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11251 9063 : && (new_rtx = simplify_const_binary_operation
11252 1624458 : (code, int_result_mode,
11253 9063 : gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11254 9063 : gen_int_shift_amount (int_result_mode, count))) != 0
11255 9063 : && CONST_INT_P (new_rtx)
11256 1633521 : && merge_outer_ops (&outer_op, &outer_const, XOR,
11257 : INTVAL (new_rtx), int_result_mode,
11258 : &complement_p))
11259 : {
11260 9063 : varop = XEXP (varop, 0);
11261 9063 : continue;
11262 : }
11263 :
11264 : break;
11265 :
11266 649491 : case MINUS:
11267 : /* The following rules apply only to scalars. */
11268 649491 : if (shift_mode != shift_unit_mode)
11269 : break;
11270 629350 : int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11271 :
11272 : /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11273 : with C the size of VAROP - 1 and the shift is logical if
11274 : STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11275 : we have a (gt X 0) operation. If the shift is arithmetic with
11276 : STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11277 : we have a (neg (gt X 0)) operation. */
11278 :
11279 629350 : if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11280 629350 : && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11281 12395 : && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11282 49 : && (code == LSHIFTRT || code == ASHIFTRT)
11283 13 : && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11284 13 : && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11285 629350 : && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11286 : {
11287 0 : count = 0;
11288 0 : varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11289 : const0_rtx);
11290 :
11291 0 : if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11292 0 : varop = gen_rtx_NEG (int_varop_mode, varop);
11293 :
11294 0 : continue;
11295 : }
11296 : break;
11297 :
11298 647 : case TRUNCATE:
11299 : /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11300 : if the truncate does not affect the value. */
11301 647 : if (code == LSHIFTRT
11302 489 : && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11303 489 : && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11304 647 : && (INTVAL (XEXP (XEXP (varop, 0), 1))
11305 489 : >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11306 978 : - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11307 : {
11308 489 : rtx varop_inner = XEXP (varop, 0);
11309 489 : int new_count = count + INTVAL (XEXP (varop_inner, 1));
11310 489 : rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11311 489 : new_count);
11312 489 : varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11313 : XEXP (varop_inner, 0),
11314 : new_count_rtx);
11315 489 : varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11316 489 : count = 0;
11317 489 : continue;
11318 489 : }
11319 : break;
11320 :
11321 : default:
11322 : break;
11323 60196 : }
11324 :
11325 : break;
11326 : }
11327 :
11328 25429695 : shift_mode = result_mode;
11329 25429695 : if (shift_mode != mode)
11330 : {
11331 : /* We only change the modes of scalar shifts. */
11332 200565 : int_mode = as_a <scalar_int_mode> (mode);
11333 200565 : int_result_mode = as_a <scalar_int_mode> (result_mode);
11334 200565 : shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11335 : int_mode, outer_op, outer_const);
11336 : }
11337 :
11338 : /* We have now finished analyzing the shift. The result should be
11339 : a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11340 : OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11341 : to the result of the shift. OUTER_CONST is the relevant constant,
11342 : but we must turn off all bits turned off in the shift. */
11343 :
11344 25429695 : if (outer_op == UNKNOWN
11345 21489353 : && orig_code == code && orig_count == count
11346 21441077 : && varop == orig_varop
11347 21267478 : && shift_mode == GET_MODE (varop))
11348 : return NULL_RTX;
11349 :
11350 : /* Make a SUBREG if necessary. If we can't make it, fail. */
11351 4164913 : varop = gen_lowpart (shift_mode, varop);
11352 4164913 : if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11353 : return NULL_RTX;
11354 :
11355 : /* If we have an outer operation and we just made a shift, it is
11356 : possible that we could have simplified the shift were it not
11357 : for the outer operation. So try to do the simplification
11358 : recursively. */
11359 :
11360 4164913 : if (outer_op != UNKNOWN)
11361 3940342 : x = simplify_shift_const_1 (code, shift_mode, varop, count);
11362 : else
11363 : x = NULL_RTX;
11364 :
11365 3940342 : if (x == NULL_RTX)
11366 4129168 : x = simplify_gen_binary (code, shift_mode, varop,
11367 4129168 : gen_int_shift_amount (shift_mode, count));
11368 :
11369 : /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11370 : turn off all the bits that the shift would have turned off. */
11371 4164913 : if (orig_code == LSHIFTRT && result_mode != shift_mode)
11372 : /* We only change the modes of scalar shifts. */
11373 28112 : x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11374 28112 : x, GET_MODE_MASK (result_mode) >> orig_count);
11375 :
11376 : /* Do the remainder of the processing in RESULT_MODE. */
11377 4164913 : x = gen_lowpart_or_truncate (result_mode, x);
11378 :
11379 : /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11380 : operation. */
11381 4164913 : if (complement_p)
11382 24927 : x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11383 :
11384 4164913 : if (outer_op != UNKNOWN)
11385 : {
11386 3940342 : int_result_mode = as_a <scalar_int_mode> (result_mode);
11387 :
11388 3940342 : if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11389 3940342 : && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11390 1380361 : outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11391 :
11392 3940342 : if (outer_op == AND)
11393 3466110 : x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11394 474232 : else if (outer_op == SET)
11395 : {
11396 : /* This means that we have determined that the result is
11397 : equivalent to a constant. This should be rare. */
11398 0 : if (!side_effects_p (x))
11399 0 : x = GEN_INT (outer_const);
11400 : }
11401 474232 : else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11402 18527 : x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11403 : else
11404 455705 : x = simplify_gen_binary (outer_op, int_result_mode, x,
11405 : GEN_INT (outer_const));
11406 : }
11407 :
11408 : return x;
11409 : }
11410 :
11411 : /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11412 : The result of the shift is RESULT_MODE. If we cannot simplify it,
11413 : return X or, if it is NULL, synthesize the expression with
11414 : simplify_gen_binary. Otherwise, return a simplified value.
11415 :
11416 : The shift is normally computed in the widest mode we find in VAROP, as
11417 : long as it isn't a different number of words than RESULT_MODE. Exceptions
11418 : are ASHIFTRT and ROTATE, which are always done in their original mode. */
11419 :
11420 : static rtx
11421 21489625 : simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11422 : rtx varop, int count)
11423 : {
11424 21489625 : rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11425 21489625 : if (tem)
11426 : return tem;
11427 :
11428 17360457 : if (!x)
11429 5441642 : x = simplify_gen_binary (code, GET_MODE (varop), varop,
11430 5441642 : gen_int_shift_amount (GET_MODE (varop), count));
11431 17360457 : if (GET_MODE (x) != result_mode)
11432 0 : x = gen_lowpart (result_mode, x);
11433 : return x;
11434 : }
11435 :
11436 :
11437 : /* A subroutine of recog_for_combine. See there for arguments and
11438 : return value. */
11439 :
11440 : static int
11441 50295268 : recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes,
11442 : unsigned old_nregs, unsigned new_nregs)
11443 : {
11444 50295268 : rtx pat = *pnewpat;
11445 50295268 : rtx pat_without_clobbers;
11446 50295268 : int insn_code_number;
11447 50295268 : int num_clobbers_to_add = 0;
11448 50295268 : int i;
11449 50295268 : rtx notes = NULL_RTX;
11450 50295268 : rtx old_notes, old_pat;
11451 50295268 : int old_icode;
11452 :
11453 : /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11454 : we use to indicate that something didn't match. If we find such a
11455 : thing, force rejection. */
11456 50295268 : if (GET_CODE (pat) == PARALLEL)
11457 54532517 : for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11458 37619230 : if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11459 7714703 : && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11460 : return -1;
11461 :
11462 50293274 : old_pat = PATTERN (insn);
11463 50293274 : old_notes = REG_NOTES (insn);
11464 50293274 : PATTERN (insn) = pat;
11465 50293274 : REG_NOTES (insn) = NULL_RTX;
11466 :
11467 50293274 : insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11468 50293274 : if (dump_file && (dump_flags & TDF_DETAILS))
11469 : {
11470 277 : if (insn_code_number < 0)
11471 177 : fputs ("Failed to match this instruction:\n", dump_file);
11472 : else
11473 100 : fputs ("Successfully matched this instruction:\n", dump_file);
11474 277 : print_rtl_single (dump_file, pat);
11475 : }
11476 :
11477 : /* If it isn't, there is the possibility that we previously had an insn
11478 : that clobbered some register as a side effect, but the combined
11479 : insn doesn't need to do that. So try once more without the clobbers
11480 : unless this represents an ASM insn. */
11481 :
11482 40099886 : if (insn_code_number < 0 && ! check_asm_operands (pat)
11483 90390783 : && GET_CODE (pat) == PARALLEL)
11484 : {
11485 : int pos;
11486 :
11487 53005506 : for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11488 36579302 : if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11489 : {
11490 29282832 : if (i != pos)
11491 2504761 : SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11492 29282832 : pos++;
11493 : }
11494 :
11495 16426204 : SUBST_INT (XVECLEN (pat, 0), pos);
11496 :
11497 16426204 : if (pos == 1)
11498 4873770 : pat = XVECEXP (pat, 0, 0);
11499 :
11500 16426204 : PATTERN (insn) = pat;
11501 16426204 : insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11502 16426204 : if (dump_file && (dump_flags & TDF_DETAILS))
11503 : {
11504 82 : if (insn_code_number < 0)
11505 81 : fputs ("Failed to match this instruction:\n", dump_file);
11506 : else
11507 1 : fputs ("Successfully matched this instruction:\n", dump_file);
11508 82 : print_rtl_single (dump_file, pat);
11509 : }
11510 : }
11511 :
11512 50293274 : pat_without_clobbers = pat;
11513 :
11514 50293274 : PATTERN (insn) = old_pat;
11515 50293274 : REG_NOTES (insn) = old_notes;
11516 :
11517 : /* Recognize all noop sets, these will be killed by followup pass. */
11518 50293274 : if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11519 249177 : insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11520 :
11521 : /* If we had any clobbers to add, make a new pattern than contains
11522 : them. Then check to make sure that all of them are dead. */
11523 50293274 : if (num_clobbers_to_add)
11524 : {
11525 1693071 : rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11526 : rtvec_alloc (GET_CODE (pat) == PARALLEL
11527 : ? (XVECLEN (pat, 0)
11528 : + num_clobbers_to_add)
11529 : : num_clobbers_to_add + 1));
11530 :
11531 1693071 : if (GET_CODE (pat) == PARALLEL)
11532 1383 : for (i = 0; i < XVECLEN (pat, 0); i++)
11533 922 : XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11534 : else
11535 1692610 : XVECEXP (newpat, 0, 0) = pat;
11536 :
11537 1693071 : add_clobbers (newpat, insn_code_number);
11538 :
11539 3258838 : for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11540 3258838 : i < XVECLEN (newpat, 0); i++)
11541 : {
11542 1715454 : if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11543 1715454 : && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11544 : return -1;
11545 1565767 : if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11546 : {
11547 1515040 : gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11548 1515040 : notes = alloc_reg_note (REG_UNUSED,
11549 : XEXP (XVECEXP (newpat, 0, i), 0), notes);
11550 : }
11551 : }
11552 : pat = newpat;
11553 : }
11554 :
11555 50143587 : if (insn_code_number >= 0
11556 50143587 : && insn_code_number != NOOP_MOVE_INSN_CODE)
11557 : {
11558 : /* Create the reg dead notes if needed for the regs that were created via split. */
11559 10373307 : for (; old_nregs < new_nregs; old_nregs++)
11560 2257 : notes = alloc_reg_note (REG_DEAD, regno_reg_rtx[old_nregs], notes);
11561 10371050 : old_pat = PATTERN (insn);
11562 10371050 : old_notes = REG_NOTES (insn);
11563 10371050 : old_icode = INSN_CODE (insn);
11564 10371050 : PATTERN (insn) = pat;
11565 10371050 : REG_NOTES (insn) = notes;
11566 10371050 : INSN_CODE (insn) = insn_code_number;
11567 :
11568 : /* Do not accept an insn if hard register constraints are used. For
11569 : example, assume that the first insn is combined into the last one:
11570 :
11571 : r100=...
11572 : %5=...
11573 : r101=exp(r100)
11574 :
11575 : If the resulting insn has an operand which is constrained to hard
11576 : register %5, then this introduces a conflict since register %5 is live
11577 : at this point. Therefore, skip for now. This is a sledge hammer
11578 : approach. Ideally we would skip based on the fact whether a
11579 : combination crosses a hard register assignment and the corresponding
11580 : hard register is also referred by a single register constraint of the
11581 : resulting insn. */
11582 10371050 : bool has_hard_reg_cstr = false;
11583 10371050 : extract_insn (insn);
11584 35386346 : for (int nop = recog_data.n_operands - 1; nop >= 0; --nop)
11585 25015296 : if (strchr (recog_data.constraints[nop], '{'))
11586 : {
11587 : has_hard_reg_cstr = true;
11588 : break;
11589 : }
11590 :
11591 : /* Don't accept hard register constraints. Allow targets to reject
11592 : combined insn. */
11593 10371050 : if (has_hard_reg_cstr || !targetm.legitimate_combined_insn (insn))
11594 : {
11595 3673 : if (dump_file && (dump_flags & TDF_DETAILS))
11596 : {
11597 0 : if (has_hard_reg_cstr)
11598 0 : fputs ("Instruction makes use of hard register constraints.",
11599 : dump_file);
11600 : else
11601 0 : fputs ("Instruction not appropriate for target.",
11602 : dump_file);
11603 : }
11604 :
11605 : /* Callers expect recog_for_combine to strip
11606 : clobbers from the pattern on failure. */
11607 : pat = pat_without_clobbers;
11608 : notes = NULL_RTX;
11609 :
11610 : insn_code_number = -1;
11611 : }
11612 :
11613 10371050 : PATTERN (insn) = old_pat;
11614 10371050 : REG_NOTES (insn) = old_notes;
11615 10371050 : INSN_CODE (insn) = old_icode;
11616 : }
11617 :
11618 50143587 : *pnewpat = pat;
11619 50143587 : *pnotes = notes;
11620 :
11621 50143587 : return insn_code_number;
11622 : }
11623 :
11624 : /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11625 : expressed as an AND and maybe an LSHIFTRT, to that formulation.
11626 : Return whether anything was so changed. */
11627 :
11628 : static bool
11629 50412652 : change_zero_ext (rtx pat)
11630 : {
11631 50412652 : bool changed = false;
11632 50412652 : rtx *src = &SET_SRC (pat);
11633 :
11634 50412652 : subrtx_ptr_iterator::array_type array;
11635 351772843 : FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11636 : {
11637 301360191 : rtx x = **iter;
11638 301360191 : scalar_int_mode mode, inner_mode;
11639 301360191 : if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11640 301360191 : continue;
11641 158830278 : int size;
11642 :
11643 158830278 : if (GET_CODE (x) == ZERO_EXTRACT
11644 826856 : && CONST_INT_P (XEXP (x, 1))
11645 826842 : && CONST_INT_P (XEXP (x, 2))
11646 781224 : && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11647 159611498 : && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11648 : {
11649 781202 : size = INTVAL (XEXP (x, 1));
11650 :
11651 781202 : int start = INTVAL (XEXP (x, 2));
11652 781202 : if (BITS_BIG_ENDIAN)
11653 : start = GET_MODE_PRECISION (inner_mode) - size - start;
11654 :
11655 781202 : if (start != 0)
11656 658650 : x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11657 : gen_int_shift_amount (inner_mode, start));
11658 : else
11659 : x = XEXP (x, 0);
11660 :
11661 781202 : if (mode != inner_mode)
11662 : {
11663 162 : if (REG_P (x) && HARD_REGISTER_P (x)
11664 210825 : && !can_change_dest_mode (x, 0, mode))
11665 0 : continue;
11666 :
11667 210825 : x = gen_lowpart_SUBREG (mode, x);
11668 : }
11669 : }
11670 158049076 : else if (GET_CODE (x) == ZERO_EXTEND
11671 2382118 : && GET_CODE (XEXP (x, 0)) == SUBREG
11672 446565 : && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11673 438688 : && !paradoxical_subreg_p (XEXP (x, 0))
11674 158487764 : && subreg_lowpart_p (XEXP (x, 0)))
11675 : {
11676 311552 : inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11677 311552 : size = GET_MODE_PRECISION (inner_mode);
11678 311552 : x = SUBREG_REG (XEXP (x, 0));
11679 311552 : if (GET_MODE (x) != mode)
11680 : {
11681 19257 : if (REG_P (x) && HARD_REGISTER_P (x)
11682 22166 : && !can_change_dest_mode (x, 0, mode))
11683 0 : continue;
11684 :
11685 22166 : x = gen_lowpart_SUBREG (mode, x);
11686 : }
11687 : }
11688 315474975 : else if (GET_CODE (x) == ZERO_EXTEND
11689 2070566 : && REG_P (XEXP (x, 0))
11690 1059869 : && HARD_REGISTER_P (XEXP (x, 0))
11691 157737597 : && can_change_dest_mode (XEXP (x, 0), 0, mode))
11692 : {
11693 73 : inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11694 73 : size = GET_MODE_PRECISION (inner_mode);
11695 73 : x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11696 : }
11697 : else
11698 157737451 : continue;
11699 :
11700 1559439 : if (!(GET_CODE (x) == LSHIFTRT
11701 466612 : && CONST_INT_P (XEXP (x, 1))
11702 466612 : && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11703 : {
11704 895476 : wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11705 895476 : x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11706 895476 : }
11707 :
11708 1092827 : SUBST (**iter, x);
11709 1092827 : changed = true;
11710 : }
11711 :
11712 50412652 : if (changed)
11713 10091114 : FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11714 9018520 : maybe_swap_commutative_operands (**iter);
11715 :
11716 50412652 : rtx *dst = &SET_DEST (pat);
11717 50412652 : scalar_int_mode mode;
11718 50412652 : if (GET_CODE (*dst) == ZERO_EXTRACT
11719 9038 : && REG_P (XEXP (*dst, 0))
11720 395 : && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11721 395 : && CONST_INT_P (XEXP (*dst, 1))
11722 50413047 : && CONST_INT_P (XEXP (*dst, 2)))
11723 : {
11724 288 : rtx reg = XEXP (*dst, 0);
11725 288 : int width = INTVAL (XEXP (*dst, 1));
11726 288 : int offset = INTVAL (XEXP (*dst, 2));
11727 288 : int reg_width = GET_MODE_PRECISION (mode);
11728 288 : if (BITS_BIG_ENDIAN)
11729 : offset = reg_width - width - offset;
11730 :
11731 288 : rtx x, y, z, w;
11732 288 : wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11733 288 : wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11734 288 : x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11735 288 : if (offset)
11736 235 : y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11737 : else
11738 53 : y = SET_SRC (pat);
11739 288 : z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11740 288 : w = gen_rtx_IOR (mode, x, z);
11741 288 : SUBST (SET_DEST (pat), reg);
11742 288 : SUBST (SET_SRC (pat), w);
11743 :
11744 288 : changed = true;
11745 288 : }
11746 :
11747 50412652 : return changed;
11748 50412652 : }
11749 :
11750 : /* Like recog, but we receive the address of a pointer to a new pattern.
11751 : We try to match the rtx that the pointer points to.
11752 : If that fails, we may try to modify or replace the pattern,
11753 : storing the replacement into the same pointer object.
11754 :
11755 : Modifications include deletion or addition of CLOBBERs. If the
11756 : instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11757 : to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11758 : (and undo if that fails).
11759 :
11760 : PNOTES is a pointer to a location where any REG_UNUSED notes added for
11761 : the CLOBBERs are placed.
11762 : If OLD_NREGS != NEW_NREGS, then PNOTES also includes REG_DEAD notes added.
11763 :
11764 : The value is the final insn code from the pattern ultimately matched,
11765 : or -1. */
11766 :
11767 : static int
11768 48980155 : recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes,
11769 : unsigned int old_nregs, unsigned int new_nregs)
11770 : {
11771 48980155 : rtx pat = *pnewpat;
11772 48980155 : int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes,
11773 : old_nregs, new_nregs);
11774 48980155 : if (insn_code_number >= 0 || check_asm_operands (pat))
11775 : return insn_code_number;
11776 :
11777 38528901 : void *marker = get_undo_marker ();
11778 38528901 : bool changed = false;
11779 :
11780 38528901 : if (GET_CODE (pat) == SET)
11781 : {
11782 : /* For an unrecognized single set of a constant, try placing it in
11783 : the constant pool, if this function already uses one. */
11784 22696214 : rtx src = SET_SRC (pat);
11785 22696214 : if (CONSTANT_P (src)
11786 476220 : && !CONST_INT_P (src)
11787 425481 : && crtl->uses_const_pool
11788 373076 : && SET_DEST (pat) != pc_rtx)
11789 : {
11790 373074 : machine_mode mode = GET_MODE (src);
11791 373074 : if (mode == VOIDmode)
11792 1346 : mode = GET_MODE (SET_DEST (pat));
11793 373074 : src = force_const_mem (mode, src);
11794 373074 : if (src)
11795 : {
11796 373064 : SUBST (SET_SRC (pat), src);
11797 373064 : changed = true;
11798 : }
11799 : }
11800 : else
11801 22323140 : changed = change_zero_ext (pat);
11802 : }
11803 15832687 : else if (GET_CODE (pat) == PARALLEL)
11804 : {
11805 : int i;
11806 44167069 : for (i = 0; i < XVECLEN (pat, 0); i++)
11807 : {
11808 28350055 : rtx set = XVECEXP (pat, 0, i);
11809 28350055 : if (GET_CODE (set) == SET)
11810 28089512 : changed |= change_zero_ext (set);
11811 : }
11812 : }
11813 :
11814 38513218 : if (changed)
11815 : {
11816 1315113 : insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes,
11817 : old_nregs, new_nregs);
11818 :
11819 1315113 : if (insn_code_number < 0)
11820 1147436 : undo_to_marker (marker);
11821 : }
11822 :
11823 : return insn_code_number;
11824 : }
11825 :
11826 : /* Like gen_lowpart_general but for use by combine. In combine it
11827 : is not possible to create any new pseudoregs. However, it is
11828 : safe to create invalid memory addresses, because combine will
11829 : try to recognize them and all they will do is make the combine
11830 : attempt fail.
11831 :
11832 : If for some reason this cannot do its job, an rtx
11833 : (clobber (const_int 0)) is returned.
11834 : An insn containing that will not be recognized. */
11835 :
11836 : static rtx
11837 164516193 : gen_lowpart_for_combine (machine_mode omode, rtx x)
11838 : {
11839 164516193 : machine_mode imode = GET_MODE (x);
11840 164516193 : rtx result;
11841 :
11842 164516193 : if (omode == imode)
11843 : return x;
11844 :
11845 : /* We can only support MODE being wider than a word if X is a
11846 : constant integer or has a mode the same size. */
11847 58383412 : if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11848 27696420 : && ! (CONST_SCALAR_INT_P (x)
11849 10515378 : || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11850 3254640 : goto fail;
11851 :
11852 : /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11853 : won't know what to do. So we will strip off the SUBREG here and
11854 : process normally. */
11855 24441780 : if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11856 : {
11857 13514 : x = SUBREG_REG (x);
11858 :
11859 : /* For use in case we fall down into the address adjustments
11860 : further below, we need to adjust the known mode and size of
11861 : x; imode and isize, since we just adjusted x. */
11862 13514 : imode = GET_MODE (x);
11863 :
11864 13514 : if (imode == omode)
11865 : return x;
11866 : }
11867 :
11868 24432964 : result = gen_lowpart_common (omode, x);
11869 :
11870 24432964 : if (result)
11871 : return result;
11872 :
11873 10293959 : if (MEM_P (x))
11874 : {
11875 : /* Refuse to work on a volatile memory ref or one with a mode-dependent
11876 : address. */
11877 2019156 : if (MEM_VOLATILE_P (x)
11878 3986012 : || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11879 52334 : goto fail;
11880 :
11881 : /* If we want to refer to something bigger than the original memref,
11882 : generate a paradoxical subreg instead. That will force a reload
11883 : of the original memref X. */
11884 1966822 : if (paradoxical_subreg_p (omode, imode)
11885 1966822 : && validate_subreg (omode, GET_MODE (x), x, 0))
11886 1752655 : return gen_rtx_SUBREG (omode, x, 0);
11887 :
11888 214167 : poly_int64 offset = byte_lowpart_offset (omode, imode);
11889 214167 : return adjust_address_nv (x, omode, offset);
11890 : }
11891 :
11892 : /* If X is a comparison operator, rewrite it in a new mode. This
11893 : probably won't match, but may allow further simplifications. */
11894 8274803 : else if (COMPARISON_P (x)
11895 152072 : && SCALAR_INT_MODE_P (imode)
11896 54801 : && SCALAR_INT_MODE_P (omode))
11897 54790 : return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11898 :
11899 : /* If we couldn't simplify X any other way, just enclose it in a
11900 : SUBREG. Normally, this SUBREG won't match, but some patterns may
11901 : include an explicit SUBREG or we may simplify it further in combine. */
11902 : else
11903 : {
11904 8220013 : rtx res;
11905 :
11906 8220013 : if (imode == VOIDmode)
11907 : {
11908 8 : imode = int_mode_for_mode (omode).require ();
11909 8 : x = gen_lowpart_common (imode, x);
11910 8 : if (x == NULL)
11911 0 : goto fail;
11912 : }
11913 8220013 : res = lowpart_subreg (omode, x, imode);
11914 8220013 : if (res)
11915 : return res;
11916 : }
11917 :
11918 17027 : fail:
11919 3324001 : return gen_rtx_CLOBBER (omode, const0_rtx);
11920 : }
11921 :
11922 : /* Like gen_lowpart_for_combine but returns NULL_RTX
11923 : for an error instead of CLOBBER.
11924 : Note no_emit is not called directly from combine but rather from
11925 : simplify_rtx and is expecting a NULL on failure rather than
11926 : a CLOBBER. */
11927 :
11928 : static rtx
11929 1613484 : gen_lowpart_for_combine_no_emit (machine_mode omode, rtx x)
11930 : {
11931 1613484 : rtx tem = gen_lowpart_for_combine (omode, x);
11932 1613484 : if (!tem || GET_CODE (tem) == CLOBBER)
11933 18543 : return NULL_RTX;
11934 : return tem;
11935 : }
11936 :
11937 :
11938 : /* Try to simplify a comparison between OP0 and a constant OP1,
11939 : where CODE is the comparison code that will be tested, into a
11940 : (CODE OP0 const0_rtx) form.
11941 :
11942 : The result is a possibly different comparison code to use.
11943 : *POP0 and *POP1 may be updated. */
11944 :
11945 : static enum rtx_code
11946 16309186 : simplify_compare_const (enum rtx_code code, machine_mode mode,
11947 : rtx *pop0, rtx *pop1)
11948 : {
11949 16309186 : scalar_int_mode int_mode;
11950 16309186 : rtx op0 = *pop0;
11951 16309186 : HOST_WIDE_INT const_op = INTVAL (*pop1);
11952 :
11953 : /* Get the constant we are comparing against and turn off all bits
11954 : not on in our mode. */
11955 16309186 : if (mode != VOIDmode)
11956 15904441 : const_op = trunc_int_for_mode (const_op, mode);
11957 :
11958 : /* If we are comparing against a constant power of two and the value
11959 : being compared can only have that single bit nonzero (e.g., it was
11960 : `and'ed with that bit), we can replace this with a comparison
11961 : with zero. */
11962 16309186 : if (const_op
11963 4305563 : && (code == EQ || code == NE || code == GEU || code == LTU
11964 : /* This optimization is incorrect for signed >= INT_MIN or
11965 : < INT_MIN, those are always true or always false. */
11966 26404 : || ((code == GE || code == LT) && const_op > 0))
11967 2903274 : && is_a <scalar_int_mode> (mode, &int_mode)
11968 2903274 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11969 2883901 : && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11970 17238159 : && (nonzero_bits (op0, int_mode)
11971 928973 : == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11972 : {
11973 5160 : code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11974 : const_op = 0;
11975 : }
11976 :
11977 : /* Similarly, if we are comparing a value known to be either -1 or
11978 : 0 with -1, change it to the opposite comparison against zero. */
11979 2460 : if (const_op == -1
11980 263883 : && (code == EQ || code == NE || code == GT || code == LE
11981 : || code == GEU || code == LTU)
11982 16558074 : && is_a <scalar_int_mode> (mode, &int_mode)
11983 16565192 : && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11984 : {
11985 12278 : code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11986 : const_op = 0;
11987 : }
11988 :
11989 : /* Do some canonicalizations based on the comparison code. We prefer
11990 : comparisons against zero and then prefer equality comparisons.
11991 : If we can reduce the size of a constant, we will do that too. */
11992 16296908 : switch (code)
11993 : {
11994 275504 : case LT:
11995 : /* < C is equivalent to <= (C - 1) */
11996 275504 : if (const_op > 0)
11997 : {
11998 5397 : const_op -= 1;
11999 5397 : code = LE;
12000 : /* ... fall through to LE case below. */
12001 471523 : gcc_fallthrough ();
12002 : }
12003 : else
12004 : break;
12005 :
12006 471523 : case LE:
12007 : /* <= C is equivalent to < (C + 1); we do this for C < 0 */
12008 471523 : if (const_op < 0)
12009 : {
12010 52 : const_op += 1;
12011 52 : code = LT;
12012 : }
12013 :
12014 : /* If we are doing a <= 0 comparison on a value known to have
12015 : a zero sign bit, we can replace this with == 0. */
12016 471471 : else if (const_op == 0
12017 327925 : && is_a <scalar_int_mode> (mode, &int_mode)
12018 327925 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12019 799396 : && (nonzero_bits (op0, int_mode)
12020 327925 : & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12021 327925 : == 0)
12022 : code = EQ;
12023 : break;
12024 :
12025 241831 : case GE:
12026 : /* >= C is equivalent to > (C - 1). */
12027 241831 : if (const_op > 0)
12028 : {
12029 1201 : const_op -= 1;
12030 1201 : code = GT;
12031 : /* ... fall through to GT below. */
12032 268639 : gcc_fallthrough ();
12033 : }
12034 : else
12035 : break;
12036 :
12037 268639 : case GT:
12038 : /* > C is equivalent to >= (C + 1); we do this for C < 0. */
12039 268639 : if (const_op < 0)
12040 : {
12041 451 : const_op += 1;
12042 451 : code = GE;
12043 : }
12044 :
12045 : /* If we are doing a > 0 comparison on a value known to have
12046 : a zero sign bit, we can replace this with != 0. */
12047 268188 : else if (const_op == 0
12048 135524 : && is_a <scalar_int_mode> (mode, &int_mode)
12049 135524 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12050 403712 : && (nonzero_bits (op0, int_mode)
12051 135524 : & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12052 135524 : == 0)
12053 : code = NE;
12054 : break;
12055 :
12056 98389 : case LTU:
12057 : /* < C is equivalent to <= (C - 1). */
12058 98389 : if (const_op > 0)
12059 : {
12060 88577 : const_op -= 1;
12061 88577 : code = LEU;
12062 : /* ... fall through ... */
12063 88577 : gcc_fallthrough ();
12064 : }
12065 : /* (unsigned) < 0x80000000 is equivalent to >= 0. */
12066 9812 : else if (is_a <scalar_int_mode> (mode, &int_mode)
12067 9812 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12068 9039 : && (((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode))
12069 9039 : == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12070 : {
12071 : const_op = 0;
12072 : code = GE;
12073 : break;
12074 : }
12075 : else
12076 : break;
12077 :
12078 699679 : case LEU:
12079 : /* unsigned <= 0 is equivalent to == 0 */
12080 699679 : if (const_op == 0)
12081 : code = EQ;
12082 : /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
12083 699190 : else if (is_a <scalar_int_mode> (mode, &int_mode)
12084 699190 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12085 697195 : && ((unsigned HOST_WIDE_INT) const_op
12086 : == ((HOST_WIDE_INT_1U
12087 697195 : << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
12088 : {
12089 : const_op = 0;
12090 : code = GE;
12091 : }
12092 : break;
12093 :
12094 31630 : case GEU:
12095 : /* >= C is equivalent to > (C - 1). */
12096 31630 : if (const_op > 1)
12097 : {
12098 22854 : const_op -= 1;
12099 22854 : code = GTU;
12100 : /* ... fall through ... */
12101 22854 : gcc_fallthrough ();
12102 : }
12103 :
12104 : /* (unsigned) >= 0x80000000 is equivalent to < 0. */
12105 8776 : else if (is_a <scalar_int_mode> (mode, &int_mode)
12106 8776 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12107 7516 : && (((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode))
12108 7516 : == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12109 : {
12110 : const_op = 0;
12111 : code = LT;
12112 : break;
12113 : }
12114 : else
12115 : break;
12116 :
12117 524121 : case GTU:
12118 : /* unsigned > 0 is equivalent to != 0 */
12119 524121 : if (const_op == 0)
12120 : code = NE;
12121 : /* (unsigned) > 0x7fffffff is equivalent to < 0. */
12122 524120 : else if (is_a <scalar_int_mode> (mode, &int_mode)
12123 524120 : && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12124 522803 : && ((unsigned HOST_WIDE_INT) const_op
12125 : == (HOST_WIDE_INT_1U
12126 522803 : << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
12127 : {
12128 : const_op = 0;
12129 : code = LT;
12130 : }
12131 : break;
12132 :
12133 : default:
12134 : break;
12135 : }
12136 :
12137 : /* Narrow non-symmetric comparison of memory and constant as e.g.
12138 : x0...x7 <= 0x3fffffffffffffff into x0 <= 0x3f where x0 is the most
12139 : significant byte. Likewise, transform x0...x7 >= 0x4000000000000000 into
12140 : x0 >= 0x40. */
12141 15592210 : if ((code == LEU || code == LTU || code == GEU || code == GTU)
12142 1238643 : && is_a <scalar_int_mode> (GET_MODE (op0), &int_mode)
12143 1238611 : && HWI_COMPUTABLE_MODE_P (int_mode)
12144 1233266 : && MEM_P (op0)
12145 82510 : && !MEM_VOLATILE_P (op0)
12146 : /* The optimization makes only sense for constants which are big enough
12147 : so that we have a chance to chop off something at all. */
12148 81633 : && ((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode)) > 0xff
12149 : /* Ensure that we do not overflow during normalization. */
12150 22373 : && (code != GTU
12151 4440 : || ((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode))
12152 : < HOST_WIDE_INT_M1U)
12153 16331559 : && trunc_int_for_mode (const_op, int_mode) == const_op)
12154 : {
12155 22373 : unsigned HOST_WIDE_INT n
12156 22373 : = (unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode);
12157 22373 : enum rtx_code adjusted_code;
12158 :
12159 : /* Normalize code to either LEU or GEU. */
12160 22373 : if (code == LTU)
12161 : {
12162 116 : --n;
12163 116 : adjusted_code = LEU;
12164 : }
12165 22257 : else if (code == GTU)
12166 : {
12167 4440 : ++n;
12168 4440 : adjusted_code = GEU;
12169 : }
12170 : else
12171 : adjusted_code = code;
12172 :
12173 22373 : scalar_int_mode narrow_mode_iter;
12174 69402 : FOR_EACH_MODE_UNTIL (narrow_mode_iter, int_mode)
12175 : {
12176 47696 : unsigned nbits = GET_MODE_PRECISION (int_mode)
12177 47696 : - GET_MODE_PRECISION (narrow_mode_iter);
12178 47696 : unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << nbits) - 1;
12179 47696 : unsigned HOST_WIDE_INT lower_bits = n & mask;
12180 47696 : if ((adjusted_code == LEU && lower_bits == mask)
12181 47436 : || (adjusted_code == GEU && lower_bits == 0))
12182 : {
12183 667 : n >>= nbits;
12184 667 : break;
12185 : }
12186 : }
12187 :
12188 22373 : if (narrow_mode_iter < int_mode)
12189 : {
12190 667 : if (dump_file && (dump_flags & TDF_DETAILS))
12191 : {
12192 12 : fprintf (
12193 : dump_file, "narrow comparison from mode %s to %s: (MEM %s "
12194 : HOST_WIDE_INT_PRINT_HEX ") to (MEM %s "
12195 12 : HOST_WIDE_INT_PRINT_HEX ").\n", GET_MODE_NAME (int_mode),
12196 12 : GET_MODE_NAME (narrow_mode_iter), GET_RTX_NAME (code),
12197 12 : (unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode),
12198 12 : GET_RTX_NAME (adjusted_code), n);
12199 : }
12200 667 : poly_int64 offset = (BYTES_BIG_ENDIAN
12201 667 : ? 0
12202 667 : : (GET_MODE_SIZE (int_mode)
12203 667 : - GET_MODE_SIZE (narrow_mode_iter)));
12204 667 : *pop0 = adjust_address_nv (op0, narrow_mode_iter, offset);
12205 667 : *pop1 = gen_int_mode (n, narrow_mode_iter);
12206 667 : return adjusted_code;
12207 : }
12208 : }
12209 :
12210 16308519 : *pop1 = GEN_INT (const_op);
12211 16308519 : return code;
12212 : }
12213 :
12214 : /* Simplify a comparison between *POP0 and *POP1 where CODE is the
12215 : comparison code that will be tested.
12216 :
12217 : The result is a possibly different comparison code to use. *POP0 and
12218 : *POP1 may be updated.
12219 :
12220 : It is possible that we might detect that a comparison is either always
12221 : true or always false. However, we do not perform general constant
12222 : folding in combine, so this knowledge isn't useful. Such tautologies
12223 : should have been detected earlier. Hence we ignore all such cases. */
12224 :
12225 : static enum rtx_code
12226 24524425 : simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
12227 : {
12228 24524425 : rtx op0 = *pop0;
12229 24524425 : rtx op1 = *pop1;
12230 24524425 : rtx tem, tem1;
12231 24524425 : int i;
12232 24524425 : scalar_int_mode mode, inner_mode, tmode;
12233 24524425 : opt_scalar_int_mode tmode_iter;
12234 :
12235 : /* Try a few ways of applying the same transformation to both operands. */
12236 24524762 : while (1)
12237 : {
12238 : /* The test below this one won't handle SIGN_EXTENDs on these machines,
12239 : so check specially. */
12240 24524762 : if (!WORD_REGISTER_OPERATIONS
12241 24524762 : && code != GTU && code != GEU && code != LTU && code != LEU
12242 21281391 : && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
12243 1486 : && GET_CODE (XEXP (op0, 0)) == ASHIFT
12244 1092 : && GET_CODE (XEXP (op1, 0)) == ASHIFT
12245 722 : && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
12246 721 : && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
12247 721 : && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
12248 : && (is_a <scalar_int_mode>
12249 721 : (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
12250 721 : && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
12251 721 : && CONST_INT_P (XEXP (op0, 1))
12252 721 : && XEXP (op0, 1) == XEXP (op1, 1)
12253 88 : && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12254 88 : && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
12255 88 : && (INTVAL (XEXP (op0, 1))
12256 88 : == (GET_MODE_PRECISION (mode)
12257 88 : - GET_MODE_PRECISION (inner_mode))))
12258 : {
12259 88 : op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
12260 88 : op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
12261 : }
12262 :
12263 : /* If both operands are the same constant shift, see if we can ignore the
12264 : shift. We can if the shift is a rotate or if the bits shifted out of
12265 : this shift are known to be zero for both inputs and if the type of
12266 : comparison is compatible with the shift. */
12267 24524762 : if (GET_CODE (op0) == GET_CODE (op1)
12268 3465312 : && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
12269 3102708 : && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
12270 3102708 : || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
12271 858 : && (code != GT && code != LT && code != GE && code != LE))
12272 3101902 : || (GET_CODE (op0) == ASHIFTRT
12273 1440 : && (code != GTU && code != LTU
12274 1432 : && code != GEU && code != LEU)))
12275 2201 : && CONST_INT_P (XEXP (op0, 1))
12276 2175 : && INTVAL (XEXP (op0, 1)) >= 0
12277 2175 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12278 24526937 : && XEXP (op0, 1) == XEXP (op1, 1))
12279 : {
12280 1101 : machine_mode mode = GET_MODE (op0);
12281 1101 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12282 1101 : int shift_count = INTVAL (XEXP (op0, 1));
12283 :
12284 1101 : if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
12285 510 : mask &= (mask >> shift_count) << shift_count;
12286 591 : else if (GET_CODE (op0) == ASHIFT)
12287 591 : mask = (mask & (mask << shift_count)) >> shift_count;
12288 :
12289 1101 : if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12290 1101 : && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12291 172 : op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12292 : else
12293 : break;
12294 : }
12295 :
12296 : /* If both operands are AND's of a paradoxical SUBREG by constant, the
12297 : SUBREGs are of the same mode, and, in both cases, the AND would
12298 : be redundant if the comparison was done in the narrower mode,
12299 : do the comparison in the narrower mode (e.g., we are AND'ing with 1
12300 : and the operand's possibly nonzero bits are 0xffffff01; in that case
12301 : if we only care about QImode, we don't need the AND). This case
12302 : occurs if the output mode of an scc insn is not SImode and
12303 : STORE_FLAG_VALUE == 1 (e.g., the 386).
12304 :
12305 : Similarly, check for a case where the AND's are ZERO_EXTEND
12306 : operations from some narrower mode even though a SUBREG is not
12307 : present. */
12308 :
12309 24523661 : else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12310 2713 : && CONST_INT_P (XEXP (op0, 1))
12311 2631 : && CONST_INT_P (XEXP (op1, 1)))
12312 : {
12313 2589 : rtx inner_op0 = XEXP (op0, 0);
12314 2589 : rtx inner_op1 = XEXP (op1, 0);
12315 2589 : HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12316 2589 : HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12317 2589 : bool changed = false;
12318 :
12319 2589 : if (paradoxical_subreg_p (inner_op0)
12320 1026 : && GET_CODE (inner_op1) == SUBREG
12321 484 : && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12322 484 : && (GET_MODE (SUBREG_REG (inner_op0))
12323 484 : == GET_MODE (SUBREG_REG (inner_op1)))
12324 200 : && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12325 : GET_MODE (SUBREG_REG (inner_op0)))) == 0
12326 1712 : && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12327 127 : GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12328 : {
12329 117 : op0 = SUBREG_REG (inner_op0);
12330 117 : op1 = SUBREG_REG (inner_op1);
12331 :
12332 : /* The resulting comparison is always unsigned since we masked
12333 : off the original sign bit. */
12334 117 : code = unsigned_condition (code);
12335 :
12336 117 : changed = true;
12337 : }
12338 :
12339 2472 : else if (c0 == c1)
12340 5197 : FOR_EACH_MODE_UNTIL (tmode,
12341 : as_a <scalar_int_mode> (GET_MODE (op0)))
12342 3109 : if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12343 : {
12344 37 : op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12345 37 : op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12346 37 : code = unsigned_condition (code);
12347 37 : changed = true;
12348 37 : break;
12349 : }
12350 :
12351 2242 : if (! changed)
12352 : break;
12353 : }
12354 :
12355 : /* If both operands are NOT, we can strip off the outer operation
12356 : and adjust the comparison code for swapped operands; similarly for
12357 : NEG, except that this must be an equality comparison. */
12358 24521072 : else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12359 24521072 : || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12360 11 : && (code == EQ || code == NE)))
12361 11 : op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12362 :
12363 : else
12364 : break;
12365 : }
12366 :
12367 : /* If the first operand is a constant, swap the operands and adjust the
12368 : comparison code appropriately, but don't do this if the second operand
12369 : is already a constant integer. */
12370 24524425 : if (swap_commutative_operands_p (op0, op1))
12371 : {
12372 1543903 : std::swap (op0, op1);
12373 1543903 : code = swap_condition (code);
12374 : }
12375 :
12376 : /* We now enter a loop during which we will try to simplify the comparison.
12377 : For the most part, we only are concerned with comparisons with zero,
12378 : but some things may really be comparisons with zero but not start
12379 : out looking that way. */
12380 :
12381 25654402 : while (CONST_INT_P (op1))
12382 : {
12383 16827892 : machine_mode raw_mode = GET_MODE (op0);
12384 16827892 : scalar_int_mode int_mode;
12385 16827892 : int equality_comparison_p;
12386 16827892 : int sign_bit_comparison_p;
12387 16827892 : int unsigned_comparison_p;
12388 16827892 : HOST_WIDE_INT const_op;
12389 :
12390 : /* We only want to handle integral modes. This catches VOIDmode,
12391 : CCmode, and the floating-point modes. An exception is that we
12392 : can handle VOIDmode if OP0 is a COMPARE or a comparison
12393 : operation. */
12394 :
12395 16827892 : if (GET_MODE_CLASS (raw_mode) != MODE_INT
12396 1747286 : && ! (raw_mode == VOIDmode
12397 404775 : && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12398 : break;
12399 :
12400 : /* Try to simplify the compare to constant, possibly changing the
12401 : comparison op, and/or changing op1 to zero. */
12402 15485351 : code = simplify_compare_const (code, raw_mode, &op0, &op1);
12403 15485351 : const_op = INTVAL (op1);
12404 :
12405 : /* Compute some predicates to simplify code below. */
12406 :
12407 15485351 : equality_comparison_p = (code == EQ || code == NE);
12408 15485351 : sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12409 15485351 : unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12410 15485351 : || code == GEU);
12411 :
12412 : /* If this is a sign bit comparison and we can do arithmetic in
12413 : MODE, say that we will only be needing the sign bit of OP0. */
12414 15485351 : if (sign_bit_comparison_p
12415 464154 : && is_a <scalar_int_mode> (raw_mode, &int_mode)
12416 15949505 : && HWI_COMPUTABLE_MODE_P (int_mode))
12417 463762 : op0 = force_to_mode (op0, int_mode,
12418 : HOST_WIDE_INT_1U
12419 463762 : << (GET_MODE_PRECISION (int_mode) - 1), false);
12420 :
12421 15485351 : if (COMPARISON_P (op0))
12422 : {
12423 : /* We can't do anything if OP0 is a condition code value, rather
12424 : than an actual data value. */
12425 729949 : if (const_op != 0
12426 729949 : || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12427 : break;
12428 :
12429 : /* Get the two operands being compared. */
12430 137097 : if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12431 0 : tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12432 : else
12433 137097 : tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12434 :
12435 : /* Check for the cases where we simply want the result of the
12436 : earlier test or the opposite of that result. */
12437 137097 : if (code == NE || code == EQ
12438 137097 : || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12439 0 : && (code == LT || code == GE)))
12440 : {
12441 137097 : enum rtx_code new_code;
12442 137097 : if (code == LT || code == NE)
12443 137097 : new_code = GET_CODE (op0);
12444 : else
12445 0 : new_code = reversed_comparison_code (op0, NULL);
12446 :
12447 137097 : if (new_code != UNKNOWN)
12448 : {
12449 137097 : code = new_code;
12450 137097 : op0 = tem;
12451 137097 : op1 = tem1;
12452 1129977 : continue;
12453 : }
12454 : }
12455 : break;
12456 : }
12457 :
12458 14755402 : if (raw_mode == VOIDmode)
12459 : break;
12460 14755402 : scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12461 :
12462 : /* Now try cases based on the opcode of OP0. If none of the cases
12463 : does a "continue", we exit this loop immediately after the
12464 : switch. */
12465 :
12466 14755402 : unsigned int mode_width = GET_MODE_PRECISION (mode);
12467 14755402 : unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12468 14755402 : switch (GET_CODE (op0))
12469 : {
12470 391609 : case ZERO_EXTRACT:
12471 : /* If we are extracting a single bit from a variable position in
12472 : a constant that has only a single bit set and are comparing it
12473 : with zero, we can convert this into an equality comparison
12474 : between the position and the location of the single bit. */
12475 : /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12476 : have already reduced the shift count modulo the word size. */
12477 391609 : if (!SHIFT_COUNT_TRUNCATED
12478 391609 : && CONST_INT_P (XEXP (op0, 0))
12479 11153 : && XEXP (op0, 1) == const1_rtx
12480 11135 : && equality_comparison_p && const_op == 0
12481 402744 : && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12482 : {
12483 0 : if (BITS_BIG_ENDIAN)
12484 : i = BITS_PER_WORD - 1 - i;
12485 :
12486 0 : op0 = XEXP (op0, 2);
12487 0 : op1 = GEN_INT (i);
12488 0 : const_op = i;
12489 :
12490 : /* Result is nonzero iff shift count is equal to I. */
12491 0 : code = reverse_condition (code);
12492 0 : continue;
12493 : }
12494 :
12495 : /* fall through */
12496 :
12497 391613 : case SIGN_EXTRACT:
12498 391613 : tem = expand_compound_operation (op0);
12499 391613 : if (tem != op0)
12500 : {
12501 355565 : op0 = tem;
12502 355565 : continue;
12503 : }
12504 : break;
12505 :
12506 28816 : case NOT:
12507 : /* If testing for equality, we can take the NOT of the constant. */
12508 41133 : if (equality_comparison_p
12509 28816 : && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12510 : {
12511 12317 : op0 = XEXP (op0, 0);
12512 12317 : op1 = tem;
12513 12317 : continue;
12514 : }
12515 :
12516 : /* If just looking at the sign bit, reverse the sense of the
12517 : comparison. */
12518 16499 : if (sign_bit_comparison_p)
12519 : {
12520 16165 : op0 = XEXP (op0, 0);
12521 16165 : code = (code == GE ? LT : GE);
12522 16165 : continue;
12523 : }
12524 : break;
12525 :
12526 246065 : case NEG:
12527 : /* If testing for equality, we can take the NEG of the constant. */
12528 488480 : if (equality_comparison_p
12529 246065 : && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12530 : {
12531 242415 : op0 = XEXP (op0, 0);
12532 242415 : op1 = tem;
12533 242415 : continue;
12534 : }
12535 :
12536 : /* The remaining cases only apply to comparisons with zero. */
12537 3650 : if (const_op != 0)
12538 : break;
12539 :
12540 : /* When X is ABS or is known positive,
12541 : (neg X) is < 0 if and only if X != 0. */
12542 :
12543 3045 : if (sign_bit_comparison_p
12544 3007 : && (GET_CODE (XEXP (op0, 0)) == ABS
12545 3000 : || (mode_width <= HOST_BITS_PER_WIDE_INT
12546 3000 : && (nonzero_bits (XEXP (op0, 0), mode)
12547 3000 : & (HOST_WIDE_INT_1U << (mode_width - 1)))
12548 3000 : == 0)))
12549 : {
12550 38 : op0 = XEXP (op0, 0);
12551 38 : code = (code == LT ? NE : EQ);
12552 38 : continue;
12553 : }
12554 :
12555 : /* If we have NEG of something whose two high-order bits are the
12556 : same, we know that "(-a) < 0" is equivalent to "a > 0". */
12557 2969 : if (num_sign_bit_copies (op0, mode) >= 2)
12558 : {
12559 22 : op0 = XEXP (op0, 0);
12560 22 : code = swap_condition (code);
12561 22 : continue;
12562 : }
12563 : break;
12564 :
12565 146 : case ROTATE:
12566 : /* If we are testing equality and our count is a constant, we
12567 : can perform the inverse operation on our RHS. */
12568 146 : if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12569 146 : && (tem = simplify_binary_operation (ROTATERT, mode,
12570 : op1, XEXP (op0, 1))) != 0)
12571 : {
12572 0 : op0 = XEXP (op0, 0);
12573 0 : op1 = tem;
12574 0 : continue;
12575 : }
12576 :
12577 : /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12578 : a particular bit. Convert it to an AND of a constant of that
12579 : bit. This will be converted into a ZERO_EXTRACT. */
12580 146 : if (const_op == 0 && sign_bit_comparison_p
12581 0 : && CONST_INT_P (XEXP (op0, 1))
12582 0 : && mode_width <= HOST_BITS_PER_WIDE_INT
12583 0 : && UINTVAL (XEXP (op0, 1)) < mode_width)
12584 : {
12585 0 : op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12586 : (HOST_WIDE_INT_1U
12587 : << (mode_width - 1
12588 0 : - INTVAL (XEXP (op0, 1)))));
12589 0 : code = (code == LT ? NE : EQ);
12590 0 : continue;
12591 : }
12592 :
12593 : /* Fall through. */
12594 :
12595 1554 : case ABS:
12596 : /* ABS is ignorable inside an equality comparison with zero. */
12597 1554 : if (const_op == 0 && equality_comparison_p)
12598 : {
12599 1 : op0 = XEXP (op0, 0);
12600 1 : continue;
12601 : }
12602 : break;
12603 :
12604 1774 : case SIGN_EXTEND:
12605 : /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12606 : (compare FOO CONST) if CONST fits in FOO's mode and we
12607 : are either testing inequality or have an unsigned
12608 : comparison with ZERO_EXTEND or a signed comparison with
12609 : SIGN_EXTEND. But don't do it if we don't have a compare
12610 : insn of the given mode, since we'd have to revert it
12611 : later on, and then we wouldn't know whether to sign- or
12612 : zero-extend. */
12613 1774 : if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12614 1774 : && ! unsigned_comparison_p
12615 1007 : && HWI_COMPUTABLE_MODE_P (mode)
12616 1007 : && trunc_int_for_mode (const_op, mode) == const_op
12617 1007 : && have_insn_for (COMPARE, mode))
12618 : {
12619 1007 : op0 = XEXP (op0, 0);
12620 1007 : continue;
12621 : }
12622 : break;
12623 :
12624 497661 : case SUBREG:
12625 : /* Check for the case where we are comparing A - C1 with C2, that is
12626 :
12627 : (subreg:MODE (plus (A) (-C1))) op (C2)
12628 :
12629 : with C1 a constant, and try to lift the SUBREG, i.e. to do the
12630 : comparison in the wider mode. One of the following two conditions
12631 : must be true in order for this to be valid:
12632 :
12633 : 1. The mode extension results in the same bit pattern being added
12634 : on both sides and the comparison is equality or unsigned. As
12635 : C2 has been truncated to fit in MODE, the pattern can only be
12636 : all 0s or all 1s.
12637 :
12638 : 2. The mode extension results in the sign bit being copied on
12639 : each side.
12640 :
12641 : The difficulty here is that we have predicates for A but not for
12642 : (A - C1) so we need to check that C1 is within proper bounds so
12643 : as to perturb A as little as possible. */
12644 :
12645 497661 : if (mode_width <= HOST_BITS_PER_WIDE_INT
12646 497585 : && subreg_lowpart_p (op0)
12647 466547 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12648 : &inner_mode)
12649 464432 : && GET_MODE_PRECISION (inner_mode) > mode_width
12650 464432 : && GET_CODE (SUBREG_REG (op0)) == PLUS
12651 497661 : && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12652 : {
12653 0 : rtx a = XEXP (SUBREG_REG (op0), 0);
12654 0 : HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12655 :
12656 0 : if ((c1 > 0
12657 0 : && (unsigned HOST_WIDE_INT) c1
12658 0 : < HOST_WIDE_INT_1U << (mode_width - 1)
12659 0 : && (equality_comparison_p || unsigned_comparison_p)
12660 : /* (A - C1) zero-extends if it is positive and sign-extends
12661 : if it is negative, C2 both zero- and sign-extends. */
12662 0 : && (((nonzero_bits (a, inner_mode)
12663 0 : & ~GET_MODE_MASK (mode)) == 0
12664 0 : && const_op >= 0)
12665 : /* (A - C1) sign-extends if it is positive and 1-extends
12666 : if it is negative, C2 both sign- and 1-extends. */
12667 0 : || (num_sign_bit_copies (a, inner_mode)
12668 0 : > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12669 0 : - mode_width)
12670 0 : && const_op < 0)))
12671 0 : || ((unsigned HOST_WIDE_INT) c1
12672 0 : < HOST_WIDE_INT_1U << (mode_width - 2)
12673 : /* (A - C1) always sign-extends, like C2. */
12674 0 : && num_sign_bit_copies (a, inner_mode)
12675 0 : > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12676 0 : - (mode_width - 1))))
12677 : {
12678 0 : op0 = SUBREG_REG (op0);
12679 0 : continue;
12680 : }
12681 : }
12682 :
12683 : /* If the inner mode is narrower and we are extracting the low part,
12684 : we can treat the SUBREG as if it were a ZERO_EXTEND ... */
12685 497661 : if (paradoxical_subreg_p (op0))
12686 : {
12687 : if (WORD_REGISTER_OPERATIONS
12688 : && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12689 : &inner_mode)
12690 : && GET_MODE_PRECISION (inner_mode) < BITS_PER_WORD
12691 : /* On WORD_REGISTER_OPERATIONS targets the bits
12692 : beyond sub_mode aren't considered undefined,
12693 : so optimize only if it is a MEM load when MEM loads
12694 : zero extend, because then the upper bits are all zero. */
12695 : && !(MEM_P (SUBREG_REG (op0))
12696 : && load_extend_op (inner_mode) == ZERO_EXTEND))
12697 : break;
12698 : /* FALLTHROUGH to case ZERO_EXTEND */
12699 : }
12700 497661 : else if (subreg_lowpart_p (op0)
12701 466623 : && GET_MODE_CLASS (mode) == MODE_INT
12702 466623 : && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12703 464432 : && (code == NE || code == EQ)
12704 329873 : && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12705 323594 : && !paradoxical_subreg_p (op0)
12706 821255 : && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12707 323594 : & ~GET_MODE_MASK (mode)) == 0)
12708 : {
12709 : /* Remove outer subregs that don't do anything. */
12710 65105 : tem = gen_lowpart (inner_mode, op1);
12711 :
12712 65105 : if ((nonzero_bits (tem, inner_mode)
12713 65105 : & ~GET_MODE_MASK (mode)) == 0)
12714 : {
12715 64385 : op0 = SUBREG_REG (op0);
12716 64385 : op1 = tem;
12717 64385 : continue;
12718 : }
12719 : break;
12720 : }
12721 : else
12722 : break;
12723 :
12724 : /* FALLTHROUGH */
12725 :
12726 42107 : case ZERO_EXTEND:
12727 42107 : if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12728 42107 : && (unsigned_comparison_p || equality_comparison_p)
12729 42069 : && HWI_COMPUTABLE_MODE_P (mode)
12730 42069 : && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12731 42069 : && const_op >= 0
12732 42060 : && have_insn_for (COMPARE, mode))
12733 : {
12734 42060 : op0 = XEXP (op0, 0);
12735 42060 : continue;
12736 : }
12737 : break;
12738 :
12739 469789 : case PLUS:
12740 : /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12741 : this for equality comparisons due to pathological cases involving
12742 : overflows. */
12743 526918 : if (equality_comparison_p
12744 469789 : && (tem = simplify_binary_operation (MINUS, mode,
12745 : op1, XEXP (op0, 1))) != 0)
12746 : {
12747 57129 : op0 = XEXP (op0, 0);
12748 57129 : op1 = tem;
12749 57129 : continue;
12750 : }
12751 :
12752 : /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12753 412660 : if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12754 15285 : && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12755 : {
12756 0 : op0 = XEXP (XEXP (op0, 0), 0);
12757 0 : code = (code == LT ? EQ : NE);
12758 0 : continue;
12759 : }
12760 : break;
12761 :
12762 183384 : case MINUS:
12763 : /* We used to optimize signed comparisons against zero, but that
12764 : was incorrect. Unsigned comparisons against zero (GTU, LEU)
12765 : arrive here as equality comparisons, or (GEU, LTU) are
12766 : optimized away. No need to special-case them. */
12767 :
12768 : /* (eq (minus A B) C) -> (eq A (plus B C)) or
12769 : (eq B (minus A C)), whichever simplifies. We can only do
12770 : this for equality comparisons due to pathological cases involving
12771 : overflows. */
12772 216171 : if (equality_comparison_p
12773 183384 : && (tem = simplify_binary_operation (PLUS, mode,
12774 : XEXP (op0, 1), op1)) != 0)
12775 : {
12776 32787 : op0 = XEXP (op0, 0);
12777 32787 : op1 = tem;
12778 32787 : continue;
12779 : }
12780 :
12781 183467 : if (equality_comparison_p
12782 150597 : && (tem = simplify_binary_operation (MINUS, mode,
12783 : XEXP (op0, 0), op1)) != 0)
12784 : {
12785 32870 : op0 = XEXP (op0, 1);
12786 32870 : op1 = tem;
12787 32870 : continue;
12788 : }
12789 :
12790 : /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12791 : of bits in X minus 1, is one iff X > 0. */
12792 16334 : if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12793 502 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12794 502 : && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12795 117751 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12796 : {
12797 0 : op0 = XEXP (op0, 1);
12798 0 : code = (code == GE ? LE : GT);
12799 0 : continue;
12800 : }
12801 : break;
12802 :
12803 9013 : case XOR:
12804 : /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12805 : if C is zero or B is a constant. */
12806 9023 : if (equality_comparison_p
12807 9013 : && (tem = simplify_binary_operation (XOR, mode,
12808 : XEXP (op0, 1), op1)) != 0)
12809 : {
12810 10 : op0 = XEXP (op0, 0);
12811 10 : op1 = tem;
12812 10 : continue;
12813 : }
12814 : break;
12815 :
12816 :
12817 397763 : case IOR:
12818 : /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12819 : iff X <= 0. */
12820 7539 : if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12821 1246 : && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12822 397811 : && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12823 : {
12824 48 : op0 = XEXP (op0, 1);
12825 48 : code = (code == GE ? GT : LE);
12826 48 : continue;
12827 : }
12828 : break;
12829 :
12830 1690308 : case AND:
12831 : /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12832 : will be converted to a ZERO_EXTRACT later. */
12833 1690308 : if (const_op == 0 && equality_comparison_p
12834 1573004 : && GET_CODE (XEXP (op0, 0)) == ASHIFT
12835 60900 : && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12836 : {
12837 6827 : op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12838 : XEXP (XEXP (op0, 0), 1));
12839 6827 : op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12840 6827 : continue;
12841 : }
12842 :
12843 : /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12844 : zero and X is a comparison and C1 and C2 describe only bits set
12845 : in STORE_FLAG_VALUE, we can compare with X. */
12846 1683481 : if (const_op == 0 && equality_comparison_p
12847 1566177 : && mode_width <= HOST_BITS_PER_WIDE_INT
12848 1562286 : && CONST_INT_P (XEXP (op0, 1))
12849 1209318 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12850 510035 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12851 495977 : && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12852 495977 : && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12853 : {
12854 495977 : mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12855 495977 : << INTVAL (XEXP (XEXP (op0, 0), 1)));
12856 495977 : if ((~STORE_FLAG_VALUE & mask) == 0
12857 495977 : && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12858 0 : || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12859 0 : && COMPARISON_P (tem))))
12860 : {
12861 0 : op0 = XEXP (XEXP (op0, 0), 0);
12862 0 : continue;
12863 : }
12864 : }
12865 :
12866 : /* If we are doing an equality comparison of an AND of a bit equal
12867 : to the sign bit, replace this with a LT or GE comparison of
12868 : the underlying value. */
12869 1684064 : if (equality_comparison_p
12870 : && const_op == 0
12871 1566177 : && CONST_INT_P (XEXP (op0, 1))
12872 1209629 : && mode_width <= HOST_BITS_PER_WIDE_INT
12873 1683481 : && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12874 1209318 : == HOST_WIDE_INT_1U << (mode_width - 1)))
12875 : {
12876 583 : op0 = XEXP (op0, 0);
12877 583 : code = (code == EQ ? GE : LT);
12878 583 : continue;
12879 : }
12880 :
12881 : /* If this AND operation is really a ZERO_EXTEND from a narrower
12882 : mode, the constant fits within that mode, and this is either an
12883 : equality or unsigned comparison, try to do this comparison in
12884 : the narrower mode.
12885 :
12886 : Note that in:
12887 :
12888 : (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12889 : -> (ne:DI (reg:SI 4) (const_int 0))
12890 :
12891 : unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12892 : known to hold a value of the required mode the
12893 : transformation is invalid. */
12894 1698929 : if ((equality_comparison_p || unsigned_comparison_p)
12895 1667185 : && CONST_INT_P (XEXP (op0, 1))
12896 3877201 : && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12897 1303514 : & GET_MODE_MASK (mode))
12898 : + 1)) >= 0
12899 906820 : && const_op >> i == 0
12900 2589718 : && int_mode_for_size (i, 1).exists (&tmode))
12901 : {
12902 16031 : op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12903 16031 : continue;
12904 : }
12905 :
12906 : /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12907 1666867 : if (const_op == 0 && equality_comparison_p
12908 1554820 : && XEXP (op0, 1) == const1_rtx
12909 681455 : && GET_CODE (XEXP (op0, 0)) == NOT)
12910 : {
12911 6455 : op0 = simplify_and_const_int (NULL_RTX, mode,
12912 : XEXP (XEXP (op0, 0), 0), 1);
12913 6455 : code = (code == NE ? EQ : NE);
12914 6455 : continue;
12915 : }
12916 :
12917 : /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12918 : (eq (and (lshiftrt X) 1) 0).
12919 : Also handle the case where (not X) is expressed using xor. */
12920 1660412 : if (const_op == 0 && equality_comparison_p
12921 1548365 : && XEXP (op0, 1) == const1_rtx
12922 675000 : && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12923 : {
12924 504148 : rtx shift_op = XEXP (XEXP (op0, 0), 0);
12925 504148 : rtx shift_count = XEXP (XEXP (op0, 0), 1);
12926 :
12927 507234 : if (GET_CODE (shift_op) == NOT
12928 504148 : || (GET_CODE (shift_op) == XOR
12929 5191 : && CONST_INT_P (XEXP (shift_op, 1))
12930 3086 : && CONST_INT_P (shift_count)
12931 3086 : && HWI_COMPUTABLE_MODE_P (mode)
12932 3086 : && (UINTVAL (XEXP (shift_op, 1))
12933 : == HOST_WIDE_INT_1U
12934 3086 : << INTVAL (shift_count))))
12935 : {
12936 3086 : op0
12937 3086 : = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12938 3086 : op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12939 3086 : code = (code == NE ? EQ : NE);
12940 3086 : continue;
12941 : }
12942 : }
12943 : break;
12944 :
12945 48916 : case ASHIFT:
12946 : /* If we have (compare (ashift FOO N) (const_int C)) and
12947 : the high order N bits of FOO (N+1 if an inequality comparison)
12948 : are known to be zero, we can do this by comparing FOO with C
12949 : shifted right N bits so long as the low-order N bits of C are
12950 : zero. */
12951 48916 : if (CONST_INT_P (XEXP (op0, 1))
12952 45210 : && INTVAL (XEXP (op0, 1)) >= 0
12953 45210 : && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12954 : < HOST_BITS_PER_WIDE_INT)
12955 45210 : && (((unsigned HOST_WIDE_INT) const_op
12956 45210 : & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12957 : - 1)) == 0)
12958 38115 : && mode_width <= HOST_BITS_PER_WIDE_INT
12959 87011 : && (nonzero_bits (XEXP (op0, 0), mode)
12960 38095 : & ~(mask >> (INTVAL (XEXP (op0, 1))
12961 38095 : + ! equality_comparison_p))) == 0)
12962 : {
12963 : /* We must perform a logical shift, not an arithmetic one,
12964 : as we want the top N bits of C to be zero. */
12965 848 : unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12966 :
12967 848 : temp >>= INTVAL (XEXP (op0, 1));
12968 848 : op1 = gen_int_mode (temp, mode);
12969 848 : op0 = XEXP (op0, 0);
12970 848 : continue;
12971 848 : }
12972 :
12973 : /* If we are doing a sign bit comparison, it means we are testing
12974 : a particular bit. Convert it to the appropriate AND. */
12975 48068 : if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12976 1640 : && mode_width <= HOST_BITS_PER_WIDE_INT)
12977 : {
12978 3280 : op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12979 : (HOST_WIDE_INT_1U
12980 : << (mode_width - 1
12981 1640 : - INTVAL (XEXP (op0, 1)))));
12982 1640 : code = (code == LT ? NE : EQ);
12983 1640 : continue;
12984 : }
12985 :
12986 : /* If this an equality comparison with zero and we are shifting
12987 : the low bit to the sign bit, we can convert this to an AND of the
12988 : low-order bit. */
12989 46428 : if (const_op == 0 && equality_comparison_p
12990 15880 : && CONST_INT_P (XEXP (op0, 1))
12991 13344 : && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12992 : {
12993 403 : op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12994 403 : continue;
12995 : }
12996 : break;
12997 :
12998 44605 : case ASHIFTRT:
12999 : /* If this is an equality comparison with zero, we can do this
13000 : as a logical shift, which might be much simpler. */
13001 44605 : if (equality_comparison_p && const_op == 0
13002 27344 : && CONST_INT_P (XEXP (op0, 1)))
13003 : {
13004 53166 : op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
13005 : XEXP (op0, 0),
13006 26583 : INTVAL (XEXP (op0, 1)));
13007 26583 : continue;
13008 : }
13009 :
13010 : /* If OP0 is a sign extension and CODE is not an unsigned comparison,
13011 : do the comparison in a narrower mode. */
13012 23012 : if (! unsigned_comparison_p
13013 16774 : && CONST_INT_P (XEXP (op0, 1))
13014 15921 : && GET_CODE (XEXP (op0, 0)) == ASHIFT
13015 5580 : && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
13016 5478 : && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
13017 18022 : .exists (&tmode))
13018 18022 : && (((unsigned HOST_WIDE_INT) const_op
13019 4990 : + (GET_MODE_MASK (tmode) >> 1) + 1)
13020 4990 : <= GET_MODE_MASK (tmode)))
13021 : {
13022 4990 : op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
13023 4990 : continue;
13024 : }
13025 :
13026 : /* Likewise if OP0 is a PLUS of a sign extension with a
13027 : constant, which is usually represented with the PLUS
13028 : between the shifts. */
13029 13032 : if (! unsigned_comparison_p
13030 11784 : && CONST_INT_P (XEXP (op0, 1))
13031 10931 : && GET_CODE (XEXP (op0, 0)) == PLUS
13032 54 : && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
13033 22 : && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
13034 2 : && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
13035 0 : && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
13036 13032 : .exists (&tmode))
13037 13032 : && (((unsigned HOST_WIDE_INT) const_op
13038 0 : + (GET_MODE_MASK (tmode) >> 1) + 1)
13039 0 : <= GET_MODE_MASK (tmode)))
13040 : {
13041 0 : rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
13042 0 : rtx add_const = XEXP (XEXP (op0, 0), 1);
13043 0 : rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
13044 : add_const, XEXP (op0, 1));
13045 :
13046 0 : op0 = simplify_gen_binary (PLUS, tmode,
13047 0 : gen_lowpart (tmode, inner),
13048 : new_const);
13049 0 : continue;
13050 0 : }
13051 :
13052 : /* FALLTHROUGH */
13053 134217 : case LSHIFTRT:
13054 : /* If we have (compare (xshiftrt FOO N) (const_int C)) and
13055 : the low order N bits of FOO are known to be zero, we can do this
13056 : by comparing FOO with C shifted left N bits so long as no
13057 : overflow occurs. Even if the low order N bits of FOO aren't known
13058 : to be zero, if the comparison is >= or < we can use the same
13059 : optimization and for > or <= by setting all the low
13060 : order N bits in the comparison constant. */
13061 134217 : if (CONST_INT_P (XEXP (op0, 1))
13062 129439 : && INTVAL (XEXP (op0, 1)) > 0
13063 129439 : && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
13064 129079 : && mode_width <= HOST_BITS_PER_WIDE_INT
13065 134217 : && (((unsigned HOST_WIDE_INT) const_op
13066 256644 : + (GET_CODE (op0) != LSHIFTRT
13067 128322 : ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
13068 : + 1)
13069 : : 0))
13070 128322 : <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
13071 : {
13072 128144 : unsigned HOST_WIDE_INT low_bits
13073 128144 : = (nonzero_bits (XEXP (op0, 0), mode)
13074 128144 : & ((HOST_WIDE_INT_1U
13075 128144 : << INTVAL (XEXP (op0, 1))) - 1));
13076 128144 : if (low_bits == 0 || !equality_comparison_p)
13077 : {
13078 : /* If the shift was logical, then we must make the condition
13079 : unsigned. */
13080 19062 : if (GET_CODE (op0) == LSHIFTRT)
13081 16380 : code = unsigned_condition (code);
13082 :
13083 19062 : const_op = (unsigned HOST_WIDE_INT) const_op
13084 19062 : << INTVAL (XEXP (op0, 1));
13085 19062 : if (low_bits != 0
13086 3019 : && (code == GT || code == GTU
13087 1029 : || code == LE || code == LEU))
13088 2951 : const_op
13089 2951 : |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
13090 19062 : op1 = GEN_INT (const_op);
13091 19062 : op0 = XEXP (op0, 0);
13092 19062 : continue;
13093 : }
13094 : }
13095 :
13096 : /* If we are using this shift to extract just the sign bit, we
13097 : can replace this with an LT or GE comparison. */
13098 115155 : if (const_op == 0
13099 99667 : && (equality_comparison_p || sign_bit_comparison_p)
13100 99631 : && CONST_INT_P (XEXP (op0, 1))
13101 95180 : && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
13102 : {
13103 49553 : op0 = XEXP (op0, 0);
13104 49553 : code = (code == NE || code == GT ? LT : GE);
13105 49553 : continue;
13106 : }
13107 : break;
13108 :
13109 : default:
13110 : break;
13111 : }
13112 :
13113 : break;
13114 : }
13115 :
13116 : /* Now make any compound operations involved in this comparison. Then,
13117 : check for an outermost SUBREG on OP0 that is not doing anything or is
13118 : paradoxical. The latter transformation must only be performed when
13119 : it is known that the "extra" bits will be the same in op0 and op1 or
13120 : that they don't matter. There are three cases to consider:
13121 :
13122 : 1. SUBREG_REG (op0) is a register. In this case the bits are don't
13123 : care bits and we can assume they have any convenient value. So
13124 : making the transformation is safe.
13125 :
13126 : 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
13127 : In this case the upper bits of op0 are undefined. We should not make
13128 : the simplification in that case as we do not know the contents of
13129 : those bits.
13130 :
13131 : 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
13132 : In that case we know those bits are zeros or ones. We must also be
13133 : sure that they are the same as the upper bits of op1.
13134 :
13135 : We can never remove a SUBREG for a non-equality comparison because
13136 : the sign bit is in a different place in the underlying object. */
13137 :
13138 24524425 : rtx_code op0_mco_code = SET;
13139 24524425 : if (op1 == const0_rtx)
13140 11765996 : op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
13141 :
13142 24524425 : op0 = make_compound_operation (op0, op0_mco_code);
13143 24524425 : op1 = make_compound_operation (op1, SET);
13144 :
13145 624966 : if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
13146 592132 : && is_int_mode (GET_MODE (op0), &mode)
13147 560596 : && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
13148 25081259 : && (code == NE || code == EQ))
13149 : {
13150 292544 : if (paradoxical_subreg_p (op0))
13151 : {
13152 : /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
13153 : implemented. */
13154 0 : if (REG_P (SUBREG_REG (op0)))
13155 : {
13156 0 : op0 = SUBREG_REG (op0);
13157 0 : op1 = gen_lowpart (inner_mode, op1);
13158 : }
13159 : }
13160 292544 : else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
13161 292544 : && (nonzero_bits (SUBREG_REG (op0), inner_mode)
13162 284836 : & ~GET_MODE_MASK (mode)) == 0)
13163 : {
13164 13983 : tem = gen_lowpart (inner_mode, op1);
13165 :
13166 13983 : if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
13167 4593 : op0 = SUBREG_REG (op0), op1 = tem;
13168 : }
13169 : }
13170 :
13171 : /* We now do the opposite procedure: Some machines don't have compare
13172 : insns in all modes. If OP0's mode is an integer mode smaller than a
13173 : word and we can't do a compare in that mode, see if there is a larger
13174 : mode for which we can do the compare. There are a number of cases in
13175 : which we can use the wider mode. */
13176 :
13177 24524425 : if (is_int_mode (GET_MODE (op0), &mode)
13178 25115050 : && GET_MODE_SIZE (mode) < UNITS_PER_WORD
13179 9102533 : && ! have_insn_for (COMPARE, mode))
13180 0 : FOR_EACH_WIDER_MODE (tmode_iter, mode)
13181 : {
13182 0 : tmode = tmode_iter.require ();
13183 0 : if (!HWI_COMPUTABLE_MODE_P (tmode))
13184 : break;
13185 0 : if (have_insn_for (COMPARE, tmode))
13186 : {
13187 0 : int zero_extended;
13188 :
13189 : /* If this is a test for negative, we can make an explicit
13190 : test of the sign bit. Test this first so we can use
13191 : a paradoxical subreg to extend OP0. */
13192 :
13193 0 : if (op1 == const0_rtx && (code == LT || code == GE)
13194 0 : && HWI_COMPUTABLE_MODE_P (mode))
13195 : {
13196 0 : unsigned HOST_WIDE_INT sign
13197 0 : = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
13198 0 : op0 = simplify_gen_binary (AND, tmode,
13199 0 : gen_lowpart (tmode, op0),
13200 0 : gen_int_mode (sign, tmode));
13201 0 : code = (code == LT) ? NE : EQ;
13202 : break;
13203 : }
13204 :
13205 : /* If the only nonzero bits in OP0 and OP1 are those in the
13206 : narrower mode and this is an equality or unsigned comparison,
13207 : we can use the wider mode. Similarly for sign-extended
13208 : values, in which case it is true for all comparisons. */
13209 0 : zero_extended = ((code == EQ || code == NE
13210 0 : || code == GEU || code == GTU
13211 0 : || code == LEU || code == LTU)
13212 0 : && (nonzero_bits (op0, tmode)
13213 0 : & ~GET_MODE_MASK (mode)) == 0
13214 0 : && ((CONST_INT_P (op1)
13215 0 : || (nonzero_bits (op1, tmode)
13216 0 : & ~GET_MODE_MASK (mode)) == 0)));
13217 :
13218 0 : if (zero_extended
13219 0 : || ((num_sign_bit_copies (op0, tmode)
13220 0 : > (unsigned int) (GET_MODE_PRECISION (tmode)
13221 0 : - GET_MODE_PRECISION (mode)))
13222 0 : && (num_sign_bit_copies (op1, tmode)
13223 0 : > (unsigned int) (GET_MODE_PRECISION (tmode)
13224 0 : - GET_MODE_PRECISION (mode)))))
13225 : {
13226 : /* If OP0 is an AND and we don't have an AND in MODE either,
13227 : make a new AND in the proper mode. */
13228 0 : if (GET_CODE (op0) == AND
13229 0 : && !have_insn_for (AND, mode))
13230 0 : op0 = simplify_gen_binary (AND, tmode,
13231 0 : gen_lowpart (tmode,
13232 : XEXP (op0, 0)),
13233 0 : gen_lowpart (tmode,
13234 : XEXP (op0, 1)));
13235 : else
13236 : {
13237 0 : if (zero_extended)
13238 : {
13239 0 : op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
13240 : op0, mode);
13241 0 : op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
13242 : op1, mode);
13243 : }
13244 : else
13245 : {
13246 0 : op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
13247 : op0, mode);
13248 0 : op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
13249 : op1, mode);
13250 : }
13251 : break;
13252 : }
13253 : }
13254 : }
13255 : }
13256 :
13257 : /* We may have changed the comparison operands. Re-canonicalize. */
13258 24524425 : if (swap_commutative_operands_p (op0, op1))
13259 : {
13260 63318 : std::swap (op0, op1);
13261 63318 : code = swap_condition (code);
13262 : }
13263 :
13264 : /* If this machine only supports a subset of valid comparisons, see if we
13265 : can convert an unsupported one into a supported one. */
13266 24524425 : target_canonicalize_comparison (&code, &op0, &op1, 0);
13267 :
13268 24524425 : *pop0 = op0;
13269 24524425 : *pop1 = op1;
13270 :
13271 24524425 : return code;
13272 : }
13273 :
13274 : /* Utility function for record_value_for_reg. Count number of
13275 : rtxs in X. */
13276 : static int
13277 2715 : count_rtxs (rtx x)
13278 : {
13279 2715 : enum rtx_code code = GET_CODE (x);
13280 2715 : const char *fmt;
13281 2715 : int i, j, ret = 1;
13282 :
13283 2715 : if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13284 2715 : || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13285 : {
13286 83 : rtx x0 = XEXP (x, 0);
13287 83 : rtx x1 = XEXP (x, 1);
13288 :
13289 83 : if (x0 == x1)
13290 0 : return 1 + 2 * count_rtxs (x0);
13291 :
13292 83 : if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13293 83 : || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13294 0 : && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13295 0 : return 2 + 2 * count_rtxs (x0)
13296 0 : + count_rtxs (x0 == XEXP (x1, 0)
13297 0 : ? XEXP (x1, 1) : XEXP (x1, 0));
13298 :
13299 83 : if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13300 83 : || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13301 0 : && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13302 0 : return 2 + 2 * count_rtxs (x1)
13303 0 : + count_rtxs (x1 == XEXP (x0, 0)
13304 0 : ? XEXP (x0, 1) : XEXP (x0, 0));
13305 : }
13306 :
13307 2715 : fmt = GET_RTX_FORMAT (code);
13308 6111 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13309 3396 : if (fmt[i] == 'e')
13310 1592 : ret += count_rtxs (XEXP (x, i));
13311 1804 : else if (fmt[i] == 'E')
13312 208 : for (j = 0; j < XVECLEN (x, i); j++)
13313 156 : ret += count_rtxs (XVECEXP (x, i, j));
13314 :
13315 : return ret;
13316 : }
13317 :
13318 : /* Utility function for following routine. Called when X is part of a value
13319 : being stored into last_set_value. Sets last_set_table_tick
13320 : for each register mentioned. Similar to mention_regs in cse.cc */
13321 :
13322 : static void
13323 236072516 : update_table_tick (rtx x)
13324 : {
13325 236773417 : enum rtx_code code = GET_CODE (x);
13326 236773417 : const char *fmt = GET_RTX_FORMAT (code);
13327 236773417 : int i, j;
13328 :
13329 236773417 : if (code == REG)
13330 : {
13331 84336195 : unsigned int regno = REGNO (x);
13332 84336195 : unsigned int endregno = END_REGNO (x);
13333 84336195 : unsigned int r;
13334 :
13335 168784195 : for (r = regno; r < endregno; r++)
13336 : {
13337 84448000 : reg_stat_type *rsp = ®_stat[r];
13338 84448000 : rsp->last_set_table_tick = label_tick;
13339 : }
13340 :
13341 : return;
13342 : }
13343 :
13344 393920565 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13345 242310570 : if (fmt[i] == 'e')
13346 : {
13347 : /* Check for identical subexpressions. If x contains
13348 : identical subexpression we only have to traverse one of
13349 : them. */
13350 140472099 : if (i == 0 && ARITHMETIC_P (x))
13351 : {
13352 : /* Note that at this point x1 has already been
13353 : processed. */
13354 53096551 : rtx x0 = XEXP (x, 0);
13355 53096551 : rtx x1 = XEXP (x, 1);
13356 :
13357 : /* If x0 and x1 are identical then there is no need to
13358 : process x0. */
13359 53096551 : if (x0 == x1)
13360 : break;
13361 :
13362 : /* If x0 is identical to a subexpression of x1 then while
13363 : processing x1, x0 has already been processed. Thus we
13364 : are done with x. */
13365 52970380 : if (ARITHMETIC_P (x1)
13366 239041 : && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13367 : break;
13368 :
13369 : /* If x1 is identical to a subexpression of x0 then we
13370 : still have to process the rest of x0. */
13371 52970225 : if (ARITHMETIC_P (x0)
13372 9524000 : && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13373 : {
13374 700901 : update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13375 700901 : break;
13376 : }
13377 : }
13378 :
13379 139644872 : update_table_tick (XEXP (x, i));
13380 : }
13381 101838471 : else if (fmt[i] == 'E')
13382 10468050 : for (j = 0; j < XVECLEN (x, i); j++)
13383 7739308 : update_table_tick (XVECEXP (x, i, j));
13384 : }
13385 :
13386 : /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13387 : are saying that the register is clobbered and we no longer know its
13388 : value. If INSN is zero, don't update reg_stat[].last_set; this is
13389 : only permitted with VALUE also zero and is used to invalidate the
13390 : register. */
13391 :
13392 : static void
13393 116177140 : record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13394 : {
13395 116177140 : unsigned int regno = REGNO (reg);
13396 116177140 : unsigned int endregno = END_REGNO (reg);
13397 116177140 : unsigned int i;
13398 116177140 : reg_stat_type *rsp;
13399 :
13400 : /* If VALUE contains REG and we have a previous value for REG, substitute
13401 : the previous value. */
13402 116177140 : if (value && insn && reg_overlap_mentioned_p (reg, value))
13403 : {
13404 6356294 : rtx tem;
13405 :
13406 : /* Set things up so get_last_value is allowed to see anything set up to
13407 : our insn. */
13408 6356294 : subst_low_luid = DF_INSN_LUID (insn);
13409 6356294 : tem = get_last_value (reg);
13410 :
13411 : /* If TEM is simply a binary operation with two CLOBBERs as operands,
13412 : it isn't going to be useful and will take a lot of time to process,
13413 : so just use the CLOBBER. */
13414 :
13415 6356294 : if (tem)
13416 : {
13417 2508040 : if (ARITHMETIC_P (tem)
13418 2273577 : && GET_CODE (XEXP (tem, 0)) == CLOBBER
13419 1105863 : && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13420 : tem = XEXP (tem, 0);
13421 2506554 : else if (count_occurrences (value, reg, 1) >= 2)
13422 : {
13423 : /* If there are two or more occurrences of REG in VALUE,
13424 : prevent the value from growing too much. */
13425 967 : if (count_rtxs (tem) > param_max_last_value_rtl)
13426 0 : tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13427 : }
13428 :
13429 2508040 : value = replace_rtx (copy_rtx (value), reg, tem);
13430 : }
13431 : }
13432 :
13433 : /* For each register modified, show we don't know its value, that
13434 : we don't know about its bitwise content, that its value has been
13435 : updated, and that we don't know the location of the death of the
13436 : register. */
13437 232711005 : for (i = regno; i < endregno; i++)
13438 : {
13439 116533865 : rsp = ®_stat[i];
13440 :
13441 116533865 : if (insn)
13442 107035321 : rsp->last_set = insn;
13443 :
13444 116533865 : rsp->last_set_value = 0;
13445 116533865 : rsp->last_set_mode = VOIDmode;
13446 116533865 : rsp->last_set_nonzero_bits = 0;
13447 116533865 : rsp->last_set_sign_bit_copies = 0;
13448 116533865 : rsp->last_death = 0;
13449 116533865 : rsp->truncated_to_mode = VOIDmode;
13450 : }
13451 :
13452 : /* Mark registers that are being referenced in this value. */
13453 116177140 : if (value)
13454 88688336 : update_table_tick (value);
13455 :
13456 : /* Now update the status of each register being set.
13457 : If someone is using this register in this block, set this register
13458 : to invalid since we will get confused between the two lives in this
13459 : basic block. This makes using this register always invalid. In cse, we
13460 : scan the table to invalidate all entries using this register, but this
13461 : is too much work for us. */
13462 :
13463 232711005 : for (i = regno; i < endregno; i++)
13464 : {
13465 116533865 : rsp = ®_stat[i];
13466 116533865 : rsp->last_set_label = label_tick;
13467 116533865 : if (!insn
13468 107035321 : || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13469 20263782 : rsp->last_set_invalid = true;
13470 : else
13471 96270083 : rsp->last_set_invalid = false;
13472 : }
13473 :
13474 : /* The value being assigned might refer to X (like in "x++;"). In that
13475 : case, we must replace it with (clobber (const_int 0)) to prevent
13476 : infinite loops. */
13477 116177140 : rsp = ®_stat[regno];
13478 116177140 : if (value && !get_last_value_validate (&value, insn, label_tick, false))
13479 : {
13480 11466813 : value = copy_rtx (value);
13481 11466813 : if (!get_last_value_validate (&value, insn, label_tick, true))
13482 0 : value = 0;
13483 : }
13484 :
13485 : /* For the main register being modified, update the value, the mode, the
13486 : nonzero bits, and the number of sign bit copies. */
13487 :
13488 116177140 : rsp->last_set_value = value;
13489 :
13490 116177140 : if (value)
13491 : {
13492 88688336 : machine_mode mode = GET_MODE (reg);
13493 88688336 : subst_low_luid = DF_INSN_LUID (insn);
13494 88688336 : rsp->last_set_mode = mode;
13495 88688336 : if (GET_MODE_CLASS (mode) == MODE_INT
13496 88688336 : && HWI_COMPUTABLE_MODE_P (mode))
13497 66804571 : mode = nonzero_bits_mode;
13498 88688336 : rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13499 88688336 : rsp->last_set_sign_bit_copies
13500 88688336 : = num_sign_bit_copies (value, GET_MODE (reg));
13501 : }
13502 116177140 : }
13503 :
13504 : /* Called via note_stores from record_dead_and_set_regs to handle one
13505 : SET or CLOBBER in an insn. DATA is the instruction in which the
13506 : set is occurring. */
13507 :
13508 : static void
13509 138982658 : record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13510 : {
13511 138982658 : rtx_insn *record_dead_insn = (rtx_insn *) data;
13512 :
13513 138982658 : if (GET_CODE (dest) == SUBREG)
13514 5 : dest = SUBREG_REG (dest);
13515 :
13516 138982658 : if (!record_dead_insn)
13517 : {
13518 4303044 : if (REG_P (dest))
13519 4303044 : record_value_for_reg (dest, NULL, NULL_RTX);
13520 : return;
13521 : }
13522 :
13523 134679614 : if (REG_P (dest))
13524 : {
13525 : /* If we are setting the whole register, we know its value. */
13526 106859654 : if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13527 88501570 : record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13528 : /* We can handle a SUBREG if it's the low part, but we must be
13529 : careful with paradoxical SUBREGs on RISC architectures because
13530 : we cannot strip e.g. an extension around a load and record the
13531 : naked load since the RTL middle-end considers that the upper bits
13532 : are defined according to LOAD_EXTEND_OP. */
13533 18358084 : else if (GET_CODE (setter) == SET
13534 634797 : && GET_CODE (SET_DEST (setter)) == SUBREG
13535 622299 : && SUBREG_REG (SET_DEST (setter)) == dest
13536 989373 : && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13537 : BITS_PER_WORD)
13538 18496027 : && subreg_lowpart_p (SET_DEST (setter)))
13539 : {
13540 137943 : if (WORD_REGISTER_OPERATIONS
13541 : && word_register_operation_p (SET_SRC (setter))
13542 : && paradoxical_subreg_p (SET_DEST (setter)))
13543 : record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13544 137943 : else if (!partial_subreg_p (SET_DEST (setter)))
13545 125811 : record_value_for_reg (dest, record_dead_insn,
13546 125811 : gen_lowpart (GET_MODE (dest),
13547 125811 : SET_SRC (setter)));
13548 : else
13549 : {
13550 12132 : record_value_for_reg (dest, record_dead_insn,
13551 12132 : gen_lowpart (GET_MODE (dest),
13552 12132 : SET_SRC (setter)));
13553 :
13554 12132 : unsigned HOST_WIDE_INT mask;
13555 12132 : reg_stat_type *rsp = ®_stat[REGNO (dest)];
13556 12132 : mask = GET_MODE_MASK (GET_MODE (SET_DEST (setter)));
13557 12132 : rsp->last_set_nonzero_bits |= ~mask;
13558 12132 : rsp->last_set_sign_bit_copies = 1;
13559 : }
13560 : }
13561 : /* Otherwise show that we don't know the value. */
13562 : else
13563 18220141 : record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13564 : }
13565 27819960 : else if (MEM_P (dest)
13566 : /* Ignore pushes, they clobber nothing. */
13567 27819960 : && ! push_operand (dest, GET_MODE (dest)))
13568 14402299 : mem_last_set = DF_INSN_LUID (record_dead_insn);
13569 : }
13570 :
13571 : /* Update the records of when each REG was most recently set or killed
13572 : for the things done by INSN. This is the last thing done in processing
13573 : INSN in the combiner loop.
13574 :
13575 : We update reg_stat[], in particular fields last_set, last_set_value,
13576 : last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13577 : last_death, and also the similar information mem_last_set (which insn
13578 : most recently modified memory) and last_call_luid (which insn was the
13579 : most recent subroutine call). */
13580 :
13581 : static void
13582 179480601 : record_dead_and_set_regs (rtx_insn *insn)
13583 : {
13584 179480601 : rtx link;
13585 179480601 : unsigned int i;
13586 :
13587 319843714 : for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13588 : {
13589 140363113 : if (REG_NOTE_KIND (link) == REG_DEAD
13590 80131238 : && REG_P (XEXP (link, 0)))
13591 : {
13592 80131238 : unsigned int regno = REGNO (XEXP (link, 0));
13593 80131238 : unsigned int endregno = END_REGNO (XEXP (link, 0));
13594 :
13595 160468566 : for (i = regno; i < endregno; i++)
13596 : {
13597 80337328 : reg_stat_type *rsp;
13598 :
13599 80337328 : rsp = ®_stat[i];
13600 80337328 : rsp->last_death = insn;
13601 : }
13602 : }
13603 60231875 : else if (REG_NOTE_KIND (link) == REG_INC)
13604 0 : record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13605 : }
13606 :
13607 179480601 : if (auto call = dyn_cast<rtx_call_insn *> (insn))
13608 : {
13609 9624393 : HARD_REG_SET callee_clobbers
13610 9624393 : = insn_callee_abi (call).full_and_partial_reg_clobbers ();
13611 9624393 : hard_reg_set_iterator hrsi;
13612 814873652 : EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, i, hrsi)
13613 : {
13614 805249259 : reg_stat_type *rsp;
13615 :
13616 : /* ??? We could try to preserve some information from the last
13617 : set of register I if the call doesn't actually clobber
13618 : (reg:last_set_mode I), which might be true for ABIs with
13619 : partial clobbers. However, it would be difficult to
13620 : update last_set_nonzero_bits and last_sign_bit_copies
13621 : to account for the part of I that actually was clobbered.
13622 : It wouldn't help much anyway, since we rarely see this
13623 : situation before RA. */
13624 805249259 : rsp = ®_stat[i];
13625 805249259 : rsp->last_set_invalid = true;
13626 805249259 : rsp->last_set = call;
13627 805249259 : rsp->last_set_value = 0;
13628 805249259 : rsp->last_set_mode = VOIDmode;
13629 805249259 : rsp->last_set_nonzero_bits = 0;
13630 805249259 : rsp->last_set_sign_bit_copies = 0;
13631 805249259 : rsp->last_death = 0;
13632 805249259 : rsp->truncated_to_mode = VOIDmode;
13633 : }
13634 :
13635 9624393 : last_call_luid = mem_last_set = DF_INSN_LUID (call);
13636 :
13637 : /* We can't combine into a call pattern. Remember, though, that
13638 : the return value register is set at this LUID. We could
13639 : still replace a register with the return value from the
13640 : wrong subroutine call! */
13641 9624393 : note_stores (call, record_dead_and_set_regs_1, NULL_RTX);
13642 : }
13643 : else
13644 169856208 : note_stores (insn, record_dead_and_set_regs_1, insn);
13645 179480601 : }
13646 :
13647 : /* If a SUBREG has the promoted bit set, it is in fact a property of the
13648 : register present in the SUBREG, so for each such SUBREG go back and
13649 : adjust nonzero and sign bit information of the registers that are
13650 : known to have some zero/sign bits set.
13651 :
13652 : This is needed because when combine blows the SUBREGs away, the
13653 : information on zero/sign bits is lost and further combines can be
13654 : missed because of that. */
13655 :
13656 : static void
13657 7379 : record_promoted_value (rtx_insn *insn, rtx subreg)
13658 : {
13659 7379 : struct insn_link *links;
13660 7379 : rtx set;
13661 7379 : unsigned int regno = REGNO (SUBREG_REG (subreg));
13662 7379 : machine_mode mode = GET_MODE (subreg);
13663 :
13664 7379 : if (!HWI_COMPUTABLE_MODE_P (mode))
13665 : return;
13666 :
13667 8693 : for (links = LOG_LINKS (insn); links;)
13668 : {
13669 7799 : reg_stat_type *rsp;
13670 :
13671 7799 : insn = links->insn;
13672 7799 : set = single_set (insn);
13673 :
13674 7799 : if (! set || !REG_P (SET_DEST (set))
13675 7795 : || REGNO (SET_DEST (set)) != regno
13676 14424 : || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13677 : {
13678 1174 : links = links->next;
13679 1174 : continue;
13680 : }
13681 :
13682 6625 : rsp = ®_stat[regno];
13683 6625 : if (rsp->last_set == insn)
13684 : {
13685 6625 : if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13686 6625 : rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13687 : }
13688 :
13689 6625 : if (REG_P (SET_SRC (set)))
13690 : {
13691 140 : regno = REGNO (SET_SRC (set));
13692 140 : links = LOG_LINKS (insn);
13693 : }
13694 : else
13695 : break;
13696 : }
13697 : }
13698 :
13699 : /* Check if X, a register, is known to contain a value already
13700 : truncated to MODE. In this case we can use a subreg to refer to
13701 : the truncated value even though in the generic case we would need
13702 : an explicit truncation. */
13703 :
13704 : static bool
13705 0 : reg_truncated_to_mode (machine_mode mode, const_rtx x)
13706 : {
13707 0 : reg_stat_type *rsp = ®_stat[REGNO (x)];
13708 0 : machine_mode truncated = rsp->truncated_to_mode;
13709 :
13710 0 : if (truncated == 0
13711 0 : || rsp->truncation_label < label_tick_ebb_start)
13712 : return false;
13713 0 : if (!partial_subreg_p (mode, truncated))
13714 : return true;
13715 0 : if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13716 : return true;
13717 : return false;
13718 : }
13719 :
13720 : /* If X is a hard reg or a subreg record the mode that the register is
13721 : accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13722 : able to turn a truncate into a subreg using this information. Return true
13723 : if traversing X is complete. */
13724 :
13725 : static bool
13726 203612815 : record_truncated_value (rtx x)
13727 : {
13728 203612815 : machine_mode truncated_mode;
13729 203612815 : reg_stat_type *rsp;
13730 :
13731 203612815 : if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13732 : {
13733 1906779 : machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13734 1906779 : truncated_mode = GET_MODE (x);
13735 :
13736 1906779 : if (!partial_subreg_p (truncated_mode, original_mode))
13737 : return true;
13738 :
13739 1143949 : truncated_mode = GET_MODE (x);
13740 1143949 : if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13741 : return true;
13742 :
13743 0 : x = SUBREG_REG (x);
13744 0 : }
13745 : /* ??? For hard-regs we now record everything. We might be able to
13746 : optimize this using last_set_mode. */
13747 201706036 : else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13748 21357545 : truncated_mode = GET_MODE (x);
13749 : else
13750 : return false;
13751 :
13752 21357545 : rsp = ®_stat[REGNO (x)];
13753 21357545 : if (rsp->truncated_to_mode == 0
13754 9915746 : || rsp->truncation_label < label_tick_ebb_start
13755 30005324 : || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13756 : {
13757 12710408 : rsp->truncated_to_mode = truncated_mode;
13758 12710408 : rsp->truncation_label = label_tick;
13759 : }
13760 :
13761 : return true;
13762 : }
13763 :
13764 : /* Callback for note_uses. Find hardregs and subregs of pseudos and
13765 : the modes they are used in. This can help turning TRUNCATEs into
13766 : SUBREGs. */
13767 :
13768 : static void
13769 78027500 : record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13770 : {
13771 78027500 : subrtx_var_iterator::array_type array;
13772 281640315 : FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13773 203612815 : if (record_truncated_value (*iter))
13774 23264324 : iter.skip_subrtxes ();
13775 78027500 : }
13776 :
13777 : /* Scan X for promoted SUBREGs. For each one found,
13778 : note what it implies to the registers used in it. */
13779 :
13780 : static void
13781 371339327 : check_promoted_subreg (rtx_insn *insn, rtx x)
13782 : {
13783 371339327 : if (GET_CODE (x) == SUBREG
13784 2301175 : && SUBREG_PROMOTED_VAR_P (x)
13785 371346706 : && REG_P (SUBREG_REG (x)))
13786 7379 : record_promoted_value (insn, x);
13787 : else
13788 : {
13789 371331948 : const char *format = GET_RTX_FORMAT (GET_CODE (x));
13790 371331948 : int i, j;
13791 :
13792 893629080 : for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13793 522297132 : switch (format[i])
13794 : {
13795 277609042 : case 'e':
13796 277609042 : check_promoted_subreg (insn, XEXP (x, i));
13797 277609042 : break;
13798 12161627 : case 'V':
13799 12161627 : case 'E':
13800 12161627 : if (XVEC (x, i) != 0)
13801 37818671 : for (j = 0; j < XVECLEN (x, i); j++)
13802 25657044 : check_promoted_subreg (insn, XVECEXP (x, i, j));
13803 : break;
13804 : }
13805 : }
13806 371339327 : }
13807 :
13808 : /* Verify that all the registers and memory references mentioned in *LOC are
13809 : still valid. *LOC was part of a value set in INSN when label_tick was
13810 : equal to TICK. Return false if some are not. If REPLACE is true, replace
13811 : the invalid references with (clobber (const_int 0)) and return true. This
13812 : replacement is useful because we often can get useful information about
13813 : the form of a value (e.g., if it was produced by a shift that always
13814 : produces -1 or 0) even though we don't know exactly what registers it
13815 : was produced from. */
13816 :
13817 : static bool
13818 489729989 : get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, bool replace)
13819 : {
13820 489730556 : rtx x = *loc;
13821 489730556 : const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13822 489730556 : int len = GET_RTX_LENGTH (GET_CODE (x));
13823 489730556 : int i, j;
13824 :
13825 489730556 : if (REG_P (x))
13826 : {
13827 164448665 : unsigned int regno = REGNO (x);
13828 164448665 : unsigned int endregno = END_REGNO (x);
13829 164448665 : unsigned int j;
13830 :
13831 304685825 : for (j = regno; j < endregno; j++)
13832 : {
13833 164474118 : reg_stat_type *rsp = ®_stat[j];
13834 164474118 : if (rsp->last_set_invalid
13835 : /* If this is a pseudo-register that was only set once and not
13836 : live at the beginning of the function, it is always valid. */
13837 273414266 : || (! (regno >= FIRST_PSEUDO_REGISTER
13838 125303733 : && regno < reg_n_sets_max
13839 125282821 : && REG_N_SETS (regno) == 1
13840 217880296 : && (!REGNO_REG_SET_P
13841 : (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13842 : regno)))
13843 31543500 : && rsp->last_set_label > tick))
13844 : {
13845 24236958 : if (replace)
13846 12253806 : *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13847 : return replace;
13848 : }
13849 : }
13850 :
13851 : return true;
13852 : }
13853 : /* If this is a memory reference, make sure that there were no stores after
13854 : it that might have clobbered the value. We don't have alias info, so we
13855 : assume any store invalidates it. Moreover, we only have local UIDs, so
13856 : we also assume that there were stores in the intervening basic blocks. */
13857 36141272 : else if (MEM_P (x) && !MEM_READONLY_P (x)
13858 359260663 : && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13859 : {
13860 8226624 : if (replace)
13861 4116969 : *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13862 : return replace;
13863 : }
13864 :
13865 791457729 : for (i = 0; i < len; i++)
13866 : {
13867 486750329 : if (fmt[i] == 'e')
13868 : {
13869 : /* Check for identical subexpressions. If x contains
13870 : identical subexpression we only have to traverse one of
13871 : them. */
13872 298120597 : if (i == 1 && ARITHMETIC_P (x))
13873 : {
13874 : /* Note that at this point x0 has already been checked
13875 : and found valid. */
13876 106841059 : rtx x0 = XEXP (x, 0);
13877 106841059 : rtx x1 = XEXP (x, 1);
13878 :
13879 : /* If x0 and x1 are identical then x is also valid. */
13880 106841059 : if (x0 == x1)
13881 : return true;
13882 :
13883 : /* If x1 is identical to a subexpression of x0 then
13884 : while checking x0, x1 has already been checked. Thus
13885 : it is valid and so as x. */
13886 106438597 : if (ARITHMETIC_P (x0)
13887 19851340 : && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13888 : return true;
13889 :
13890 : /* If x0 is identical to a subexpression of x1 then x is
13891 : valid iff the rest of x1 is valid. */
13892 104344274 : if (ARITHMETIC_P (x1)
13893 387143 : && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13894 567 : return
13895 567 : get_last_value_validate (&XEXP (x1,
13896 : x0 == XEXP (x1, 0) ? 1 : 0),
13897 567 : insn, tick, replace);
13898 : }
13899 :
13900 295623245 : if (!get_last_value_validate (&XEXP (x, i), insn, tick, replace))
13901 : return false;
13902 : }
13903 188629732 : else if (fmt[i] == 'E')
13904 33670130 : for (j = 0; j < XVECLEN (x, i); j++)
13905 26678207 : if (!get_last_value_validate (&XVECEXP (x, i, j),
13906 : insn, tick, replace))
13907 : return false;
13908 : }
13909 :
13910 : /* If we haven't found a reason for it to be invalid, it is valid. */
13911 : return true;
13912 : }
13913 :
13914 : /* Get the last value assigned to X, if known. Some registers
13915 : in the value may be replaced with (clobber (const_int 0)) if their value
13916 : is known longer known reliably. */
13917 :
13918 : static rtx
13919 242136404 : get_last_value (const_rtx x)
13920 : {
13921 242136404 : unsigned int regno;
13922 242136404 : rtx value;
13923 242136404 : reg_stat_type *rsp;
13924 :
13925 : /* If this is a non-paradoxical SUBREG, get the value of its operand and
13926 : then convert it to the desired mode. If this is a paradoxical SUBREG,
13927 : we cannot predict what values the "extra" bits might have. */
13928 242136404 : if (GET_CODE (x) == SUBREG
13929 14447131 : && subreg_lowpart_p (x)
13930 13889183 : && !paradoxical_subreg_p (x)
13931 250964060 : && (value = get_last_value (SUBREG_REG (x))) != 0)
13932 4504836 : return gen_lowpart (GET_MODE (x), value);
13933 :
13934 237631568 : if (!REG_P (x))
13935 : return 0;
13936 :
13937 205904421 : regno = REGNO (x);
13938 205904421 : rsp = ®_stat[regno];
13939 205904421 : value = rsp->last_set_value;
13940 :
13941 : /* If we don't have a value, or if it isn't for this basic block and
13942 : it's either a hard register, set more than once, or it's a live
13943 : at the beginning of the function, return 0.
13944 :
13945 : Because if it's not live at the beginning of the function then the reg
13946 : is always set before being used (is never used without being set).
13947 : And, if it's set only once, and it's always set before use, then all
13948 : uses must have the same last value, even if it's not from this basic
13949 : block. */
13950 :
13951 205904421 : if (value == 0
13952 205904421 : || (rsp->last_set_label < label_tick_ebb_start
13953 78188373 : && (regno < FIRST_PSEUDO_REGISTER
13954 77326318 : || regno >= reg_n_sets_max
13955 77326318 : || REG_N_SETS (regno) != 1
13956 17186972 : || REGNO_REG_SET_P
13957 : (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13958 : return 0;
13959 :
13960 : /* If the value was set in a later insn than the ones we are processing,
13961 : we can't use it even if the register was only set once. */
13962 84024237 : if (rsp->last_set_label == label_tick
13963 84024237 : && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13964 : return 0;
13965 :
13966 : /* If fewer bits were set than what we are asked for now, we cannot use
13967 : the value. */
13968 62648816 : if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13969 62648816 : GET_MODE_PRECISION (GET_MODE (x))))
13970 : return 0;
13971 :
13972 : /* If the value has all its registers valid, return it. */
13973 62647394 : if (get_last_value_validate (&value, rsp->last_set,
13974 : rsp->last_set_label, false))
13975 58021400 : return value;
13976 :
13977 : /* Otherwise, make a copy and replace any invalid register with
13978 : (clobber (const_int 0)). If that fails for some reason, return 0. */
13979 :
13980 4625994 : value = copy_rtx (value);
13981 4625994 : if (get_last_value_validate (&value, rsp->last_set,
13982 : rsp->last_set_label, true))
13983 4625994 : return value;
13984 :
13985 : return 0;
13986 : }
13987 :
13988 : /* Define three variables used for communication between the following
13989 : routines. */
13990 :
13991 : static unsigned int reg_dead_regno, reg_dead_endregno;
13992 : static int reg_dead_flag;
13993 : rtx reg_dead_reg;
13994 :
13995 : /* Function called via note_stores from reg_dead_at_p.
13996 :
13997 : If DEST is within [reg_dead_regno, reg_dead_endregno), set
13998 : reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13999 :
14000 : static void
14001 659153 : reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
14002 : {
14003 659153 : unsigned int regno, endregno;
14004 :
14005 659153 : if (!REG_P (dest))
14006 : return;
14007 :
14008 606664 : regno = REGNO (dest);
14009 606664 : endregno = END_REGNO (dest);
14010 606664 : if (reg_dead_endregno > regno && reg_dead_regno < endregno)
14011 299374 : reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
14012 : }
14013 :
14014 : /* Return true if REG is known to be dead at INSN.
14015 :
14016 : We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
14017 : referencing REG, it is dead. If we hit a SET referencing REG, it is
14018 : live. Otherwise, see if it is live or dead at the start of the basic
14019 : block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
14020 : must be assumed to be always live. */
14021 :
14022 : static bool
14023 1664727 : reg_dead_at_p (rtx reg, rtx_insn *insn)
14024 : {
14025 1664727 : basic_block block;
14026 1664727 : unsigned int i;
14027 :
14028 : /* Set variables for reg_dead_at_p_1. */
14029 1664727 : reg_dead_regno = REGNO (reg);
14030 1664727 : reg_dead_endregno = END_REGNO (reg);
14031 1664727 : reg_dead_reg = reg;
14032 :
14033 1664727 : reg_dead_flag = 0;
14034 :
14035 : /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
14036 : we allow the machine description to decide whether use-and-clobber
14037 : patterns are OK. */
14038 1664727 : if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
14039 : {
14040 3329454 : for (i = reg_dead_regno; i < reg_dead_endregno; i++)
14041 1664727 : if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
14042 : return false;
14043 : }
14044 :
14045 : /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
14046 : beginning of basic block. */
14047 1664727 : block = BLOCK_FOR_INSN (insn);
14048 807231 : for (;;)
14049 : {
14050 2471958 : if (INSN_P (insn))
14051 : {
14052 2318765 : if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
14053 : return true;
14054 :
14055 863831 : note_stores (insn, reg_dead_at_p_1, NULL);
14056 863831 : if (reg_dead_flag)
14057 149687 : return reg_dead_flag == 1 ? 1 : 0;
14058 :
14059 714144 : if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
14060 : return true;
14061 : }
14062 :
14063 837383 : if (insn == BB_HEAD (block))
14064 : break;
14065 :
14066 807231 : insn = PREV_INSN (insn);
14067 : }
14068 :
14069 : /* Look at live-in sets for the basic block that we were in. */
14070 60304 : for (i = reg_dead_regno; i < reg_dead_endregno; i++)
14071 30152 : if (REGNO_REG_SET_P (df_get_live_in (block), i))
14072 : return false;
14073 :
14074 : return true;
14075 : }
14076 :
14077 : /* Note hard registers in X that are used. */
14078 :
14079 : static void
14080 297150991 : mark_used_regs_combine (rtx x)
14081 : {
14082 343267786 : RTX_CODE code = GET_CODE (x);
14083 343267786 : unsigned int regno;
14084 343267786 : int i;
14085 :
14086 343267786 : switch (code)
14087 : {
14088 : case LABEL_REF:
14089 : case SYMBOL_REF:
14090 : case CONST:
14091 : CASE_CONST_ANY:
14092 : case PC:
14093 : case ADDR_VEC:
14094 : case ADDR_DIFF_VEC:
14095 : case ASM_INPUT:
14096 : return;
14097 :
14098 7710961 : case CLOBBER:
14099 : /* If we are clobbering a MEM, mark any hard registers inside the
14100 : address as used. */
14101 7710961 : if (MEM_P (XEXP (x, 0)))
14102 5683 : mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
14103 : return;
14104 :
14105 78209461 : case REG:
14106 78209461 : regno = REGNO (x);
14107 : /* A hard reg in a wide mode may really be multiple registers.
14108 : If so, mark all of them just like the first. */
14109 78209461 : if (regno < FIRST_PSEUDO_REGISTER)
14110 : {
14111 : /* None of this applies to the stack, frame or arg pointers. */
14112 9286435 : if (regno == STACK_POINTER_REGNUM
14113 9286435 : || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
14114 : && regno == HARD_FRAME_POINTER_REGNUM)
14115 8366060 : || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
14116 1119415 : && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
14117 7246645 : || regno == FRAME_POINTER_REGNUM)
14118 : return;
14119 :
14120 1813570 : add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
14121 : }
14122 : return;
14123 :
14124 46111112 : case SET:
14125 46111112 : {
14126 : /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
14127 : the address. */
14128 46111112 : rtx testreg = SET_DEST (x);
14129 :
14130 46111112 : while (GET_CODE (testreg) == SUBREG
14131 46126516 : || GET_CODE (testreg) == ZERO_EXTRACT
14132 92572791 : || GET_CODE (testreg) == STRICT_LOW_PART)
14133 342946 : testreg = XEXP (testreg, 0);
14134 :
14135 46111112 : if (MEM_P (testreg))
14136 4893256 : mark_used_regs_combine (XEXP (testreg, 0));
14137 :
14138 46111112 : mark_used_regs_combine (SET_SRC (x));
14139 : }
14140 46111112 : return;
14141 :
14142 137103826 : default:
14143 137103826 : break;
14144 : }
14145 :
14146 : /* Recursively scan the operands of this expression. */
14147 :
14148 137103826 : {
14149 137103826 : const char *fmt = GET_RTX_FORMAT (code);
14150 :
14151 397631862 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
14152 : {
14153 260528036 : if (fmt[i] == 'e')
14154 211281378 : mark_used_regs_combine (XEXP (x, i));
14155 49246658 : else if (fmt[i] == 'E')
14156 : {
14157 : int j;
14158 :
14159 68006632 : for (j = 0; j < XVECLEN (x, i); j++)
14160 47204845 : mark_used_regs_combine (XVECEXP (x, i, j));
14161 : }
14162 : }
14163 : }
14164 : }
14165 :
14166 : /* Remove register number REGNO from the dead registers list of INSN.
14167 :
14168 : Return the note used to record the death, if there was one. */
14169 :
14170 : rtx
14171 3234625 : remove_death (unsigned int regno, rtx_insn *insn)
14172 : {
14173 3234625 : rtx note = find_regno_note (insn, REG_DEAD, regno);
14174 :
14175 3234625 : if (note)
14176 523944 : remove_note (insn, note);
14177 :
14178 3234625 : return note;
14179 : }
14180 :
14181 : /* For each register (hardware or pseudo) used within expression X, if its
14182 : death is in an instruction with luid between FROM_LUID (inclusive) and
14183 : TO_INSN (exclusive), put a REG_DEAD note for that register in the
14184 : list headed by PNOTES.
14185 :
14186 : That said, don't move registers killed by maybe_kill_insn.
14187 :
14188 : This is done when X is being merged by combination into TO_INSN. These
14189 : notes will then be distributed as needed. */
14190 :
14191 : static void
14192 24833322 : move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
14193 : rtx *pnotes)
14194 : {
14195 25371832 : const char *fmt;
14196 25371832 : int len, i;
14197 25371832 : enum rtx_code code = GET_CODE (x);
14198 :
14199 25371832 : if (code == REG)
14200 : {
14201 6261357 : unsigned int regno = REGNO (x);
14202 6261357 : rtx_insn *where_dead = reg_stat[regno].last_death;
14203 :
14204 : /* If we do not know where the register died, it may still die between
14205 : FROM_LUID and TO_INSN. If so, find it. This is PR83304. */
14206 6261357 : if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
14207 : {
14208 3381903 : rtx_insn *insn = prev_real_nondebug_insn (to_insn);
14209 3381903 : while (insn
14210 5100718 : && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
14211 9475900 : && DF_INSN_LUID (insn) >= from_luid)
14212 : {
14213 2315584 : if (dead_or_set_regno_p (insn, regno))
14214 : {
14215 565236 : if (find_regno_note (insn, REG_DEAD, regno))
14216 6261357 : where_dead = insn;
14217 : break;
14218 : }
14219 :
14220 1750348 : insn = prev_real_nondebug_insn (insn);
14221 : }
14222 : }
14223 :
14224 : /* Don't move the register if it gets killed in between from and to. */
14225 151763 : if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
14226 6303949 : && ! reg_referenced_p (x, maybe_kill_insn))
14227 : return;
14228 :
14229 6218765 : if (where_dead
14230 3238427 : && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
14231 3081183 : && DF_INSN_LUID (where_dead) >= from_luid
14232 9299719 : && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
14233 : {
14234 2786857 : rtx note = remove_death (regno, where_dead);
14235 :
14236 : /* It is possible for the call above to return 0. This can occur
14237 : when last_death points to I2 or I1 that we combined with.
14238 : In that case make a new note.
14239 :
14240 : We must also check for the case where X is a hard register
14241 : and NOTE is a death note for a range of hard registers
14242 : including X. In that case, we must put REG_DEAD notes for
14243 : the remaining registers in place of NOTE. */
14244 :
14245 2786857 : if (note != 0 && regno < FIRST_PSEUDO_REGISTER
14246 2786857 : && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
14247 : {
14248 0 : unsigned int deadregno = REGNO (XEXP (note, 0));
14249 0 : unsigned int deadend = END_REGNO (XEXP (note, 0));
14250 0 : unsigned int ourend = END_REGNO (x);
14251 0 : unsigned int i;
14252 :
14253 0 : for (i = deadregno; i < deadend; i++)
14254 0 : if (i < regno || i >= ourend)
14255 0 : add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
14256 : }
14257 :
14258 : /* If we didn't find any note, or if we found a REG_DEAD note that
14259 : covers only part of the given reg, and we have a multi-reg hard
14260 : register, then to be safe we must check for REG_DEAD notes
14261 : for each register other than the first. They could have
14262 : their own REG_DEAD notes lying around. */
14263 2786857 : else if ((note == 0
14264 : || (note != 0
14265 76221 : && partial_subreg_p (GET_MODE (XEXP (note, 0)),
14266 76221 : GET_MODE (x))))
14267 2710636 : && regno < FIRST_PSEUDO_REGISTER
14268 3132152 : && REG_NREGS (x) > 1)
14269 : {
14270 0 : unsigned int ourend = END_REGNO (x);
14271 0 : unsigned int i, offset;
14272 0 : rtx oldnotes = 0;
14273 :
14274 0 : if (note)
14275 0 : offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
14276 : else
14277 : offset = 1;
14278 :
14279 0 : for (i = regno + offset; i < ourend; i++)
14280 0 : move_deaths (regno_reg_rtx[i],
14281 : maybe_kill_insn, from_luid, to_insn, &oldnotes);
14282 : }
14283 :
14284 2786857 : if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14285 : {
14286 76197 : XEXP (note, 1) = *pnotes;
14287 76197 : *pnotes = note;
14288 : }
14289 : else
14290 2710660 : *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14291 : }
14292 :
14293 : return;
14294 : }
14295 :
14296 19110475 : else if (GET_CODE (x) == SET)
14297 : {
14298 4307475 : rtx dest = SET_DEST (x);
14299 :
14300 4307475 : move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14301 :
14302 : /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14303 : that accesses one word of a multi-word item, some
14304 : piece of everything register in the expression is used by
14305 : this insn, so remove any old death. */
14306 : /* ??? So why do we test for equality of the sizes? */
14307 :
14308 4307475 : if (GET_CODE (dest) == ZERO_EXTRACT
14309 4307038 : || GET_CODE (dest) == STRICT_LOW_PART
14310 8612760 : || (GET_CODE (dest) == SUBREG
14311 79339 : && !read_modify_subreg_p (dest)))
14312 : {
14313 : move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14314 : return;
14315 : }
14316 :
14317 : /* If this is some other SUBREG, we know it replaces the entire
14318 : value, so use that as the destination. */
14319 4243663 : if (GET_CODE (dest) == SUBREG)
14320 17717 : dest = SUBREG_REG (dest);
14321 :
14322 : /* If this is a MEM, adjust deaths of anything used in the address.
14323 : For a REG (the only other possibility), the entire value is
14324 : being replaced so the old value is not used in this insn. */
14325 :
14326 4243663 : if (MEM_P (dest))
14327 474698 : move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14328 : to_insn, pnotes);
14329 : return;
14330 : }
14331 :
14332 14803000 : else if (GET_CODE (x) == CLOBBER)
14333 : return;
14334 :
14335 14152112 : len = GET_RTX_LENGTH (code);
14336 14152112 : fmt = GET_RTX_FORMAT (code);
14337 :
14338 36855258 : for (i = 0; i < len; i++)
14339 : {
14340 22703146 : if (fmt[i] == 'E')
14341 : {
14342 1047675 : int j;
14343 3709393 : for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14344 2661718 : move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14345 : to_insn, pnotes);
14346 : }
14347 21655471 : else if (fmt[i] == 'e')
14348 13633887 : move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14349 : }
14350 : }
14351 :
14352 : /* Return true if X is the target of a bit-field assignment in BODY, the
14353 : pattern of an insn. X must be a REG. */
14354 :
14355 : static bool
14356 4849313 : reg_bitfield_target_p (rtx x, rtx body)
14357 : {
14358 4849313 : int i;
14359 :
14360 4849313 : if (GET_CODE (body) == SET)
14361 : {
14362 3524909 : rtx dest = SET_DEST (body);
14363 3524909 : rtx target;
14364 3524909 : unsigned int regno, tregno, endregno, endtregno;
14365 :
14366 3524909 : if (GET_CODE (dest) == ZERO_EXTRACT)
14367 426 : target = XEXP (dest, 0);
14368 3524483 : else if (GET_CODE (dest) == STRICT_LOW_PART)
14369 2008 : target = SUBREG_REG (XEXP (dest, 0));
14370 : else
14371 : return false;
14372 :
14373 2434 : if (GET_CODE (target) == SUBREG)
14374 219 : target = SUBREG_REG (target);
14375 :
14376 2434 : if (!REG_P (target))
14377 : return false;
14378 :
14379 2355 : tregno = REGNO (target), regno = REGNO (x);
14380 2355 : if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14381 2345 : return target == x;
14382 :
14383 10 : endtregno = end_hard_regno (GET_MODE (target), tregno);
14384 10 : endregno = end_hard_regno (GET_MODE (x), regno);
14385 :
14386 10 : return endregno > tregno && regno < endtregno;
14387 : }
14388 :
14389 1324404 : else if (GET_CODE (body) == PARALLEL)
14390 1992400 : for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14391 1343254 : if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14392 : return true;
14393 :
14394 : return false;
14395 : }
14396 :
14397 : /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14398 : as appropriate. I3 and I2 are the insns resulting from the combination
14399 : insns including FROM (I2 may be zero).
14400 :
14401 : ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14402 : not need REG_DEAD notes because they are being substituted for. This
14403 : saves searching in the most common cases.
14404 :
14405 : Each note in the list is either ignored or placed on some insns, depending
14406 : on the type of note. */
14407 :
14408 : static void
14409 10123261 : distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14410 : rtx elim_i2, rtx elim_i1, rtx elim_i0)
14411 : {
14412 10123261 : rtx note, next_note;
14413 10123261 : rtx tem_note;
14414 10123261 : rtx_insn *tem_insn;
14415 :
14416 23458607 : for (note = notes; note; note = next_note)
14417 : {
14418 13335346 : rtx_insn *place = 0, *place2 = 0;
14419 :
14420 13335346 : next_note = XEXP (note, 1);
14421 13335346 : switch (REG_NOTE_KIND (note))
14422 : {
14423 : case REG_BR_PROB:
14424 : case REG_BR_PRED:
14425 : /* Doesn't matter much where we put this, as long as it's somewhere.
14426 : It is preferable to keep these notes on branches, which is most
14427 : likely to be i3. */
14428 : place = i3;
14429 : break;
14430 :
14431 0 : case REG_NON_LOCAL_GOTO:
14432 0 : if (JUMP_P (i3))
14433 : place = i3;
14434 : else
14435 : {
14436 0 : gcc_assert (i2 && JUMP_P (i2));
14437 : place = i2;
14438 : }
14439 : break;
14440 :
14441 21231 : case REG_EH_REGION:
14442 21231 : {
14443 : /* The landing pad handling needs to be kept in sync with the
14444 : prerequisite checking in try_combine. */
14445 21231 : int lp_nr = INTVAL (XEXP (note, 0));
14446 : /* A REG_EH_REGION note transferring control can only ever come
14447 : from i3. */
14448 21231 : if (lp_nr > 0)
14449 11594 : gcc_assert (from_insn == i3);
14450 : /* We are making sure there is a single effective REG_EH_REGION
14451 : note and it's valid to put it on i3. */
14452 21231 : if (!insn_could_throw_p (from_insn)
14453 21231 : && !(lp_nr == INT_MIN && can_nonlocal_goto (from_insn)))
14454 : /* Throw away stray notes on insns that can never throw or
14455 : make a nonlocal goto. */
14456 : ;
14457 : else
14458 : {
14459 21154 : if (CALL_P (i3))
14460 : place = i3;
14461 : else
14462 : {
14463 2103 : gcc_assert (cfun->can_throw_non_call_exceptions);
14464 : /* If i3 can still trap preserve the note, otherwise we've
14465 : combined things such that we can now prove that the
14466 : instructions can't trap. Drop the note in this case. */
14467 2103 : if (may_trap_p (i3))
14468 : place = i3;
14469 : }
14470 : }
14471 : break;
14472 : }
14473 :
14474 125286 : case REG_ARGS_SIZE:
14475 : /* ??? How to distribute between i3-i1. Assume i3 contains the
14476 : entire adjustment. Assert i3 contains at least some adjust. */
14477 125286 : if (!noop_move_p (i3))
14478 : {
14479 125285 : poly_int64 old_size, args_size = get_args_size (note);
14480 : /* fixup_args_size_notes looks at REG_NORETURN note,
14481 : so ensure the note is placed there first. */
14482 125285 : if (CALL_P (i3))
14483 : {
14484 : rtx *np;
14485 1661 : for (np = &next_note; *np; np = &XEXP (*np, 1))
14486 20 : if (REG_NOTE_KIND (*np) == REG_NORETURN)
14487 : {
14488 9 : rtx n = *np;
14489 9 : *np = XEXP (n, 1);
14490 9 : XEXP (n, 1) = REG_NOTES (i3);
14491 9 : REG_NOTES (i3) = n;
14492 9 : break;
14493 : }
14494 : }
14495 125285 : old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14496 : /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14497 : REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14498 125285 : gcc_assert (maybe_ne (old_size, args_size)
14499 : || (CALL_P (i3)
14500 : && !ACCUMULATE_OUTGOING_ARGS
14501 : && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14502 : }
14503 : break;
14504 :
14505 81404 : case REG_NORETURN:
14506 81404 : case REG_SETJMP:
14507 81404 : case REG_TM:
14508 81404 : case REG_CALL_DECL:
14509 81404 : case REG_UNTYPED_CALL:
14510 81404 : case REG_CALL_NOCF_CHECK:
14511 : /* These notes must remain with the call. It should not be
14512 : possible for both I2 and I3 to be a call. */
14513 81404 : if (CALL_P (i3))
14514 : place = i3;
14515 : else
14516 : {
14517 0 : gcc_assert (i2 && CALL_P (i2));
14518 : place = i2;
14519 : }
14520 : break;
14521 :
14522 2029896 : case REG_UNUSED:
14523 : /* Any clobbers for i3 may still exist, and so we must process
14524 : REG_UNUSED notes from that insn.
14525 :
14526 : Any clobbers from i2 or i1 can only exist if they were added by
14527 : recog_for_combine. In that case, recog_for_combine created the
14528 : necessary REG_UNUSED notes. Trying to keep any original
14529 : REG_UNUSED notes from these insns can cause incorrect output
14530 : if it is for the same register as the original i3 dest.
14531 : In that case, we will notice that the register is set in i3,
14532 : and then add a REG_UNUSED note for the destination of i3, which
14533 : is wrong. However, it is possible to have REG_UNUSED notes from
14534 : i2 or i1 for register which were both used and clobbered, so
14535 : we keep notes from i2 or i1 if they will turn into REG_DEAD
14536 : notes. */
14537 :
14538 : /* If this register is set or clobbered between FROM_INSN and I3,
14539 : we should not create a note for it. */
14540 2029896 : if (reg_set_between_p (XEXP (note, 0), from_insn, i3))
14541 : break;
14542 :
14543 : /* If this register is set or clobbered in I3, put the note there
14544 : unless there is one already. */
14545 1938623 : if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14546 : {
14547 1153185 : if (from_insn != i3)
14548 : break;
14549 :
14550 676492 : if (! (REG_P (XEXP (note, 0))
14551 676492 : ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14552 0 : : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14553 : place = i3;
14554 : }
14555 : /* Otherwise, if this register is used by I3, then this register
14556 : now dies here, so we must put a REG_DEAD note here unless there
14557 : is one already. */
14558 785438 : else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14559 : {
14560 7773 : if (! (REG_P (XEXP (note, 0))
14561 7773 : ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
14562 0 : : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14563 : {
14564 7522 : PUT_REG_NOTE_KIND (note, REG_DEAD);
14565 7522 : place = i3;
14566 : }
14567 : }
14568 :
14569 : /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14570 : but we can't tell which at this point. We must reset any
14571 : expectations we had about the value that was previously
14572 : stored in the reg. ??? Ideally, we'd adjust REG_N_SETS
14573 : and, if appropriate, restore its previous value, but we
14574 : don't have enough information for that at this point. */
14575 : else
14576 : {
14577 777665 : record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14578 :
14579 : /* Otherwise, if this register is now referenced in i2
14580 : then the register used to be modified in one of the
14581 : original insns. If it was i3 (say, in an unused
14582 : parallel), it's now completely gone, so the note can
14583 : be discarded. But if it was modified in i2, i1 or i0
14584 : and we still reference it in i2, then we're
14585 : referencing the previous value, and since the
14586 : register was modified and REG_UNUSED, we know that
14587 : the previous value is now dead. So, if we only
14588 : reference the register in i2, we change the note to
14589 : REG_DEAD, to reflect the previous value. However, if
14590 : we're also setting or clobbering the register as
14591 : scratch, we know (because the register was not
14592 : referenced in i3) that it's unused, just as it was
14593 : unused before, and we place the note in i2. */
14594 17258 : if (from_insn != i3 && i2 && INSN_P (i2)
14595 794923 : && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14596 : {
14597 24 : if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14598 24 : PUT_REG_NOTE_KIND (note, REG_DEAD);
14599 24 : if (! (REG_P (XEXP (note, 0))
14600 24 : ? find_regno_note (i2, REG_NOTE_KIND (note),
14601 24 : REGNO (XEXP (note, 0)))
14602 0 : : find_reg_note (i2, REG_NOTE_KIND (note),
14603 : XEXP (note, 0))))
14604 : place = i2;
14605 : }
14606 : }
14607 :
14608 : break;
14609 :
14610 399815 : case REG_EQUAL:
14611 399815 : case REG_EQUIV:
14612 399815 : case REG_NOALIAS:
14613 : /* These notes say something about results of an insn. We can
14614 : only support them if they used to be on I3 in which case they
14615 : remain on I3. Otherwise they are ignored.
14616 :
14617 : If the note refers to an expression that is not a constant, we
14618 : must also ignore the note since we cannot tell whether the
14619 : equivalence is still true. It might be possible to do
14620 : slightly better than this (we only have a problem if I2DEST
14621 : or I1DEST is present in the expression), but it doesn't
14622 : seem worth the trouble. */
14623 :
14624 399815 : if (from_insn == i3
14625 198255 : && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14626 : place = i3;
14627 : break;
14628 :
14629 0 : case REG_INC:
14630 : /* These notes say something about how a register is used. They must
14631 : be present on any use of the register in I2 or I3. */
14632 0 : if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14633 0 : place = i3;
14634 :
14635 0 : if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14636 : {
14637 0 : if (place)
14638 : place2 = i2;
14639 : else
14640 : place = i2;
14641 : }
14642 : break;
14643 :
14644 7383 : case REG_LABEL_TARGET:
14645 7383 : case REG_LABEL_OPERAND:
14646 : /* This can show up in several ways -- either directly in the
14647 : pattern, or hidden off in the constant pool with (or without?)
14648 : a REG_EQUAL note. */
14649 : /* ??? Ignore the without-reg_equal-note problem for now. */
14650 7383 : if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14651 7383 : || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14652 0 : && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14653 0 : && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14654 : place = i3;
14655 :
14656 7383 : if (i2
14657 7383 : && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14658 0 : || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14659 0 : && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14660 0 : && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14661 : {
14662 0 : if (place)
14663 : place2 = i2;
14664 : else
14665 : place = i2;
14666 : }
14667 :
14668 : /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14669 : as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14670 : there. */
14671 7383 : if (place && JUMP_P (place)
14672 6107 : && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14673 0 : && (JUMP_LABEL (place) == NULL
14674 0 : || JUMP_LABEL (place) == XEXP (note, 0)))
14675 : {
14676 0 : rtx label = JUMP_LABEL (place);
14677 :
14678 0 : if (!label)
14679 0 : JUMP_LABEL (place) = XEXP (note, 0);
14680 0 : else if (LABEL_P (label))
14681 0 : LABEL_NUSES (label)--;
14682 : }
14683 :
14684 7383 : if (place2 && JUMP_P (place2)
14685 0 : && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14686 0 : && (JUMP_LABEL (place2) == NULL
14687 0 : || JUMP_LABEL (place2) == XEXP (note, 0)))
14688 : {
14689 0 : rtx label = JUMP_LABEL (place2);
14690 :
14691 0 : if (!label)
14692 0 : JUMP_LABEL (place2) = XEXP (note, 0);
14693 0 : else if (LABEL_P (label))
14694 0 : LABEL_NUSES (label)--;
14695 : place2 = 0;
14696 : }
14697 : break;
14698 :
14699 : case REG_NONNEG:
14700 : /* This note says something about the value of a register prior
14701 : to the execution of an insn. It is too much trouble to see
14702 : if the note is still correct in all situations. It is better
14703 : to simply delete it. */
14704 : break;
14705 :
14706 10629488 : case REG_DEAD:
14707 : /* If we replaced the right hand side of FROM_INSN with a
14708 : REG_EQUAL note, the original use of the dying register
14709 : will not have been combined into I3 and I2. In such cases,
14710 : FROM_INSN is guaranteed to be the first of the combined
14711 : instructions, so we simply need to search back before
14712 : FROM_INSN for the previous use or set of this register,
14713 : then alter the notes there appropriately.
14714 :
14715 : If the register is used as an input in I3, it dies there.
14716 : Similarly for I2, if it is nonzero and adjacent to I3.
14717 :
14718 : If the register is not used as an input in either I3 or I2
14719 : and it is not one of the registers we were supposed to eliminate,
14720 : there are two possibilities. We might have a non-adjacent I2
14721 : or we might have somehow eliminated an additional register
14722 : from a computation. For example, we might have had A & B where
14723 : we discover that B will always be zero. In this case we will
14724 : eliminate the reference to A.
14725 :
14726 : In both cases, we must search to see if we can find a previous
14727 : use of A and put the death note there. */
14728 :
14729 10629488 : if (from_insn
14730 7410761 : && from_insn == i2mod
14731 10631138 : && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14732 : tem_insn = from_insn;
14733 : else
14734 : {
14735 10628129 : if (from_insn
14736 7409402 : && CALL_P (from_insn)
14737 10861721 : && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14738 : place = from_insn;
14739 10475934 : else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14740 : {
14741 : /* If the new I2 sets the same register that is marked
14742 : dead in the note, we do not in general know where to
14743 : put the note. One important case we _can_ handle is
14744 : when the note comes from I3. */
14745 40154 : if (from_insn == i3)
14746 : place = i3;
14747 : else
14748 : break;
14749 : }
14750 10435780 : else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14751 : place = i3;
14752 106081 : else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14753 4152792 : && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14754 : place = i2;
14755 4002825 : else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14756 3882889 : && !(i2mod
14757 26784 : && reg_overlap_mentioned_p (XEXP (note, 0),
14758 : i2mod_old_rhs)))
14759 146730 : || rtx_equal_p (XEXP (note, 0), elim_i1)
14760 4056907 : || rtx_equal_p (XEXP (note, 0), elim_i0))
14761 : break;
14762 : tem_insn = i3;
14763 : }
14764 :
14765 238846 : if (place == 0)
14766 : {
14767 50767 : basic_block bb = this_basic_block;
14768 :
14769 1161139 : for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14770 : {
14771 1161139 : if (!NONDEBUG_INSN_P (tem_insn))
14772 : {
14773 819050 : if (tem_insn == BB_HEAD (bb))
14774 : break;
14775 784745 : continue;
14776 : }
14777 :
14778 : /* If the register is being set at TEM_INSN, see if that is all
14779 : TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14780 : into a REG_UNUSED note instead. Don't delete sets to
14781 : global register vars. */
14782 342089 : if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14783 1485 : || !global_regs[REGNO (XEXP (note, 0))])
14784 343574 : && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14785 : {
14786 15778 : rtx set = single_set (tem_insn);
14787 15778 : rtx inner_dest = 0;
14788 :
14789 15778 : if (set != 0)
14790 12150 : for (inner_dest = SET_DEST (set);
14791 12391 : (GET_CODE (inner_dest) == STRICT_LOW_PART
14792 12391 : || GET_CODE (inner_dest) == SUBREG
14793 12391 : || GET_CODE (inner_dest) == ZERO_EXTRACT);
14794 241 : inner_dest = XEXP (inner_dest, 0))
14795 : ;
14796 :
14797 : /* Verify that it was the set, and not a clobber that
14798 : modified the register.
14799 :
14800 : If we cannot delete the setter due to side
14801 : effects, mark the user with an UNUSED note instead
14802 : of deleting it. */
14803 :
14804 12150 : if (set != 0 && ! side_effects_p (SET_SRC (set))
14805 11777 : && rtx_equal_p (XEXP (note, 0), inner_dest))
14806 : {
14807 : /* Move the notes and links of TEM_INSN elsewhere.
14808 : This might delete other dead insns recursively.
14809 : First set the pattern to something that won't use
14810 : any register. */
14811 11648 : rtx old_notes = REG_NOTES (tem_insn);
14812 :
14813 11648 : PATTERN (tem_insn) = pc_rtx;
14814 11648 : REG_NOTES (tem_insn) = NULL;
14815 :
14816 11648 : distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14817 : NULL_RTX, NULL_RTX, NULL_RTX);
14818 11648 : distribute_links (LOG_LINKS (tem_insn));
14819 :
14820 11648 : unsigned int regno = REGNO (XEXP (note, 0));
14821 11648 : reg_stat_type *rsp = ®_stat[regno];
14822 11648 : if (rsp->last_set == tem_insn)
14823 10245 : record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14824 :
14825 11648 : SET_INSN_DELETED (tem_insn);
14826 11648 : if (tem_insn == i2)
14827 325627 : i2 = NULL;
14828 : }
14829 : else
14830 : {
14831 4130 : PUT_REG_NOTE_KIND (note, REG_UNUSED);
14832 :
14833 : /* If there isn't already a REG_UNUSED note, put one
14834 : here. Do not place a REG_DEAD note, even if
14835 : the register is also used here; that would not
14836 : match the algorithm used in lifetime analysis
14837 : and can cause the consistency check in the
14838 : scheduler to fail. */
14839 4130 : if (! find_regno_note (tem_insn, REG_UNUSED,
14840 4130 : REGNO (XEXP (note, 0))))
14841 2264 : place = tem_insn;
14842 : break;
14843 : }
14844 : }
14845 326311 : else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14846 326311 : || (CALL_P (tem_insn)
14847 14354 : && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14848 : {
14849 12332 : place = tem_insn;
14850 :
14851 : /* If we are doing a 3->2 combination, and we have a
14852 : register which formerly died in i3 and was not used
14853 : by i2, which now no longer dies in i3 and is used in
14854 : i2 but does not die in i2, and place is between i2
14855 : and i3, then we may need to move a link from place to
14856 : i2. */
14857 3799 : if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14858 90 : && from_insn
14859 90 : && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14860 12422 : && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14861 : {
14862 90 : struct insn_link *links = LOG_LINKS (place);
14863 90 : LOG_LINKS (place) = NULL;
14864 90 : distribute_links (links);
14865 : }
14866 : break;
14867 : }
14868 :
14869 325627 : if (tem_insn == BB_HEAD (bb))
14870 : break;
14871 : }
14872 :
14873 : }
14874 :
14875 : /* If the register is set or already dead at PLACE, we needn't do
14876 : anything with this note if it is still a REG_DEAD note.
14877 : We check here if it is set at all, not if is it totally replaced,
14878 : which is what `dead_or_set_p' checks, so also check for it being
14879 : set partially. */
14880 :
14881 6672353 : if (place && REG_NOTE_KIND (note) == REG_DEAD)
14882 : {
14883 6633918 : unsigned int regno = REGNO (XEXP (note, 0));
14884 6633918 : reg_stat_type *rsp = ®_stat[regno];
14885 :
14886 6633918 : if (dead_or_set_p (place, XEXP (note, 0))
14887 6633918 : || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14888 : {
14889 : /* Unless the register previously died in PLACE, clear
14890 : last_death. [I no longer understand why this is
14891 : being done.] */
14892 3127885 : if (rsp->last_death != place)
14893 652223 : rsp->last_death = 0;
14894 : place = 0;
14895 : }
14896 : else
14897 3506033 : rsp->last_death = place;
14898 :
14899 : /* If this is a death note for a hard reg that is occupying
14900 : multiple registers, ensure that we are still using all
14901 : parts of the object. If we find a piece of the object
14902 : that is unused, we must arrange for an appropriate REG_DEAD
14903 : note to be added for it. However, we can't just emit a USE
14904 : and tag the note to it, since the register might actually
14905 : be dead; so we recurse, and the recursive call then finds
14906 : the previous insn that used this register. */
14907 :
14908 4158256 : if (place && REG_NREGS (XEXP (note, 0)) > 1)
14909 : {
14910 773 : unsigned int endregno = END_REGNO (XEXP (note, 0));
14911 773 : bool all_used = true;
14912 773 : unsigned int i;
14913 :
14914 2319 : for (i = regno; i < endregno; i++)
14915 1546 : if ((! refers_to_regno_p (i, PATTERN (place))
14916 1546 : && ! find_regno_fusage (place, USE, i))
14917 3092 : || dead_or_set_regno_p (place, i))
14918 : {
14919 : all_used = false;
14920 : break;
14921 : }
14922 :
14923 773 : if (! all_used)
14924 : {
14925 : /* Put only REG_DEAD notes for pieces that are
14926 : not already dead or set. */
14927 :
14928 0 : for (i = regno; i < endregno;
14929 0 : i += hard_regno_nregs (i, reg_raw_mode[i]))
14930 : {
14931 0 : rtx piece = regno_reg_rtx[i];
14932 0 : basic_block bb = this_basic_block;
14933 :
14934 0 : if (! dead_or_set_p (place, piece)
14935 0 : && ! reg_bitfield_target_p (piece,
14936 0 : PATTERN (place)))
14937 : {
14938 0 : rtx new_note = alloc_reg_note (REG_DEAD, piece,
14939 : NULL_RTX);
14940 :
14941 0 : distribute_notes (new_note, place, place,
14942 : NULL, NULL_RTX, NULL_RTX,
14943 : NULL_RTX);
14944 : }
14945 0 : else if (! refers_to_regno_p (i, PATTERN (place))
14946 0 : && ! find_regno_fusage (place, USE, i))
14947 0 : for (tem_insn = PREV_INSN (place); ;
14948 0 : tem_insn = PREV_INSN (tem_insn))
14949 : {
14950 0 : if (!NONDEBUG_INSN_P (tem_insn))
14951 : {
14952 0 : if (tem_insn == BB_HEAD (bb))
14953 : break;
14954 0 : continue;
14955 : }
14956 0 : if (dead_or_set_p (tem_insn, piece)
14957 0 : || reg_bitfield_target_p (piece,
14958 0 : PATTERN (tem_insn)))
14959 : {
14960 0 : add_reg_note (tem_insn, REG_UNUSED, piece);
14961 0 : break;
14962 : }
14963 : }
14964 : }
14965 :
14966 : place = 0;
14967 : }
14968 : }
14969 : }
14970 : break;
14971 :
14972 0 : default:
14973 : /* Any other notes should not be present at this point in the
14974 : compilation. */
14975 0 : gcc_unreachable ();
14976 : }
14977 :
14978 4353603 : if (place)
14979 : {
14980 4328773 : XEXP (note, 1) = REG_NOTES (place);
14981 4328773 : REG_NOTES (place) = note;
14982 :
14983 : /* Set added_notes_insn to the earliest insn we added a note to. */
14984 4328773 : if (added_notes_insn == 0
14985 4328773 : || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14986 2865004 : added_notes_insn = place;
14987 : }
14988 :
14989 13335346 : if (place2)
14990 : {
14991 0 : add_shallow_copy_of_reg_note (place2, note);
14992 :
14993 : /* Set added_notes_insn to the earliest insn we added a note to. */
14994 0 : if (added_notes_insn == 0
14995 0 : || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14996 0 : added_notes_insn = place2;
14997 : }
14998 : }
14999 10123261 : }
15000 :
15001 : /* Similarly to above, distribute the LOG_LINKS that used to be present on
15002 : I3, I2, and I1 to new locations. This is also called to add a link
15003 : pointing at I3 when I3's destination is changed.
15004 :
15005 : If START is nonnull and an insn, we know that the next location for each
15006 : link is no earlier than START. LIMIT is the maximum number of nondebug
15007 : instructions that can be scanned when looking for the next use of a
15008 : definition. */
15009 :
15010 : static void
15011 16376873 : distribute_links (struct insn_link *links, rtx_insn *start, int limit)
15012 : {
15013 16376873 : struct insn_link *link, *next_link;
15014 :
15015 24133085 : for (link = links; link; link = next_link)
15016 : {
15017 7756212 : rtx_insn *place = 0;
15018 7756212 : rtx_insn *insn;
15019 7756212 : rtx set, reg;
15020 :
15021 7756212 : next_link = link->next;
15022 :
15023 : /* If the insn that this link points to is a NOTE, ignore it. */
15024 7756212 : if (NOTE_P (link->insn))
15025 4124004 : continue;
15026 :
15027 3632208 : set = 0;
15028 3632208 : rtx pat = PATTERN (link->insn);
15029 3632208 : if (GET_CODE (pat) == SET)
15030 : set = pat;
15031 642688 : else if (GET_CODE (pat) == PARALLEL)
15032 : {
15033 : int i;
15034 758975 : for (i = 0; i < XVECLEN (pat, 0); i++)
15035 : {
15036 755309 : set = XVECEXP (pat, 0, i);
15037 755309 : if (GET_CODE (set) != SET)
15038 3675 : continue;
15039 :
15040 751634 : reg = SET_DEST (set);
15041 751634 : while (GET_CODE (reg) == ZERO_EXTRACT
15042 760323 : || GET_CODE (reg) == STRICT_LOW_PART
15043 1520602 : || GET_CODE (reg) == SUBREG)
15044 8696 : reg = XEXP (reg, 0);
15045 :
15046 751634 : if (!REG_P (reg))
15047 45235 : continue;
15048 :
15049 706399 : if (REGNO (reg) == link->regno)
15050 : break;
15051 : }
15052 640433 : if (i == XVECLEN (pat, 0))
15053 3666 : continue;
15054 : }
15055 : else
15056 2255 : continue;
15057 :
15058 3626287 : reg = SET_DEST (set);
15059 :
15060 3626287 : while (GET_CODE (reg) == ZERO_EXTRACT
15061 3649626 : || GET_CODE (reg) == STRICT_LOW_PART
15062 7299365 : || GET_CODE (reg) == SUBREG)
15063 23854 : reg = XEXP (reg, 0);
15064 :
15065 3626287 : if (reg == pc_rtx)
15066 402 : continue;
15067 :
15068 : /* A LOG_LINK is defined as being placed on the first insn that uses
15069 : a register and points to the insn that sets the register. Start
15070 : searching at the next insn after the target of the link and stop
15071 : when we reach a set of the register or the end of the basic block.
15072 :
15073 : Note that this correctly handles the link that used to point from
15074 : I3 to I2. Also note that not much searching is typically done here
15075 : since most links don't point very far away. */
15076 :
15077 3625885 : int count = 0;
15078 3625885 : insn = start;
15079 3625885 : if (!insn || NOTE_P (insn))
15080 3565328 : insn = NEXT_INSN (link->insn);
15081 : else
15082 60557 : count = link->insn_count;
15083 11968433 : for (;
15084 15594318 : (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
15085 10786513 : || BB_HEAD (this_basic_block->next_bb) != insn));
15086 11968433 : insn = NEXT_INSN (insn))
15087 15547431 : if (DEBUG_INSN_P (insn))
15088 3260845 : continue;
15089 12286586 : else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
15090 : {
15091 3424135 : if (reg_referenced_p (reg, PATTERN (insn)))
15092 3424135 : place = insn;
15093 : break;
15094 : }
15095 8862451 : else if (CALL_P (insn)
15096 8862451 : && find_reg_fusage (insn, USE, reg))
15097 : {
15098 : place = insn;
15099 : break;
15100 : }
15101 8707769 : else if (INSN_P (insn) && reg_set_p (reg, insn))
15102 : break;
15103 8707588 : else if (count >= limit)
15104 : break;
15105 : else
15106 8707588 : count += 1;
15107 3625885 : link->insn_count = count;
15108 :
15109 : /* If we found a place to put the link, place it there unless there
15110 : is already a link to the same insn as LINK at that point. */
15111 :
15112 3625885 : if (place)
15113 : {
15114 3578817 : struct insn_link *link2;
15115 :
15116 4610121 : FOR_EACH_LOG_LINK (link2, place)
15117 1048474 : if (link2->insn == link->insn && link2->regno == link->regno)
15118 : break;
15119 :
15120 3578817 : if (link2 == NULL)
15121 : {
15122 3561647 : link->next = LOG_LINKS (place);
15123 3561647 : LOG_LINKS (place) = link;
15124 :
15125 : /* Set added_links_insn to the earliest insn we added a
15126 : link to. */
15127 3561647 : if (added_links_insn == 0
15128 3561647 : || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
15129 2826016 : added_links_insn = place;
15130 : }
15131 : }
15132 : }
15133 16376873 : }
15134 :
15135 : /* Check for any register or memory mentioned in EQUIV that is not
15136 : mentioned in EXPR. This is used to restrict EQUIV to "specializations"
15137 : of EXPR where some registers may have been replaced by constants. */
15138 :
15139 : static bool
15140 2772029 : unmentioned_reg_p (rtx equiv, rtx expr)
15141 : {
15142 2772029 : subrtx_iterator::array_type array;
15143 7282021 : FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
15144 : {
15145 5903247 : const_rtx x = *iter;
15146 4050378 : if ((REG_P (x) || MEM_P (x))
15147 6286781 : && !reg_mentioned_p (x, expr))
15148 1393255 : return true;
15149 : }
15150 1378774 : return false;
15151 2772029 : }
15152 :
15153 : /* Make pseudo-to-pseudo copies after every hard-reg-to-pseudo-copy, because
15154 : the reg-to-reg copy can usefully combine with later instructions, but we
15155 : do not want to combine the hard reg into later instructions, for that
15156 : restricts register allocation. */
15157 : static void
15158 1070227 : make_more_copies (void)
15159 : {
15160 1070227 : basic_block bb;
15161 :
15162 11731695 : FOR_EACH_BB_FN (bb, cfun)
15163 : {
15164 10661468 : rtx_insn *insn;
15165 :
15166 140577648 : FOR_BB_INSNS (bb, insn)
15167 : {
15168 129916180 : if (!NONDEBUG_INSN_P (insn))
15169 69102390 : continue;
15170 :
15171 60813790 : rtx set = single_set (insn);
15172 60813790 : if (!set)
15173 4108766 : continue;
15174 :
15175 56705024 : rtx dest = SET_DEST (set);
15176 56705024 : if (!(REG_P (dest) && !HARD_REGISTER_P (dest)))
15177 32414883 : continue;
15178 :
15179 24290141 : rtx src = SET_SRC (set);
15180 24290141 : if (!(REG_P (src) && HARD_REGISTER_P (src)))
15181 21241998 : continue;
15182 3048143 : if (TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src)))
15183 10071 : continue;
15184 :
15185 3038072 : rtx new_reg = gen_reg_rtx (GET_MODE (dest));
15186 :
15187 : /* The "original" pseudo copies have important attributes
15188 : attached, like pointerness. We want that for these copies
15189 : too, for use by insn recognition and later passes. */
15190 3038072 : set_reg_attrs_from_value (new_reg, dest);
15191 :
15192 3038072 : rtx_insn *new_insn = gen_move_insn (new_reg, src);
15193 3038072 : SET_SRC (set) = new_reg;
15194 3038072 : emit_insn_before (new_insn, insn);
15195 3038072 : df_insn_rescan (insn);
15196 : }
15197 : }
15198 1070227 : }
15199 :
15200 : /* Try combining insns through substitution. */
15201 : static void
15202 1070227 : rest_of_handle_combine (void)
15203 : {
15204 1070227 : make_more_copies ();
15205 :
15206 1070227 : df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
15207 1070227 : df_note_add_problem ();
15208 1070227 : df_analyze ();
15209 :
15210 1070227 : regstat_init_n_sets_and_refs ();
15211 1070227 : reg_n_sets_max = max_reg_num ();
15212 :
15213 1070227 : bool rebuild_jump_labels_after_combine
15214 1070227 : = combine_instructions (get_insns (), max_reg_num ());
15215 :
15216 : /* Combining insns may have turned an indirect jump into a
15217 : direct jump. Rebuild the JUMP_LABEL fields of jumping
15218 : instructions. */
15219 1070227 : if (rebuild_jump_labels_after_combine)
15220 : {
15221 2398 : if (dom_info_available_p (CDI_DOMINATORS))
15222 0 : free_dominance_info (CDI_DOMINATORS);
15223 2398 : timevar_push (TV_JUMP);
15224 2398 : rebuild_jump_labels (get_insns ());
15225 2398 : cleanup_cfg (0);
15226 2398 : timevar_pop (TV_JUMP);
15227 : }
15228 :
15229 1070227 : regstat_free_n_sets_and_refs ();
15230 1070227 : }
15231 :
15232 : namespace {
15233 :
15234 : const pass_data pass_data_combine =
15235 : {
15236 : RTL_PASS, /* type */
15237 : "combine", /* name */
15238 : OPTGROUP_NONE, /* optinfo_flags */
15239 : TV_COMBINE, /* tv_id */
15240 : PROP_cfglayout, /* properties_required */
15241 : 0, /* properties_provided */
15242 : 0, /* properties_destroyed */
15243 : 0, /* todo_flags_start */
15244 : TODO_df_finish, /* todo_flags_finish */
15245 : };
15246 :
15247 : class pass_combine : public rtl_opt_pass
15248 : {
15249 : public:
15250 302985 : pass_combine (gcc::context *ctxt)
15251 605970 : : rtl_opt_pass (pass_data_combine, ctxt)
15252 : {}
15253 :
15254 : /* opt_pass methods: */
15255 1531131 : bool gate (function *) final override { return (optimize > 0); }
15256 1070227 : unsigned int execute (function *) final override
15257 : {
15258 1070227 : rest_of_handle_combine ();
15259 1070227 : return 0;
15260 : }
15261 :
15262 : }; // class pass_combine
15263 :
15264 : } // anon namespace
15265 :
15266 : rtl_opt_pass *
15267 302985 : make_pass_combine (gcc::context *ctxt)
15268 : {
15269 302985 : return new pass_combine (ctxt);
15270 : }
|