Debian Codegraph @

gcc/combine.cc

raw
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2025 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 ENUM_BITFIELD(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 ENUM_BITFIELD(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 insn_uid_check (const_rtx insn)
320 {
321 int uid = INSN_UID (insn);
322 gcc_checking_assert (uid <= max_uid_known);
323 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 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
340 {
341 struct insn_link *l
342 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
343 sizeof (struct insn_link));
344 l->insn = insn;
345 l->regno = regno;
346 l->insn_count = 0;
347 l->next = next;
348 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 enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
462 rtx *, rtx *);
463 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
464 static void update_table_tick (rtx);
465 static void record_value_for_reg (rtx, rtx_insn *, rtx);
466 static void check_promoted_subreg (rtx_insn *, rtx);
467 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
468 static void record_dead_and_set_regs (rtx_insn *);
469 static bool get_last_value_validate (rtx *, rtx_insn *, int, bool);
470 static rtx get_last_value (const_rtx);
471 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
472 static bool reg_dead_at_p (rtx, rtx_insn *);
473 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
474 static bool reg_bitfield_target_p (rtx, rtx);
475 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *,
476 rtx, rtx, rtx);
477 static void distribute_links (struct insn_link *, rtx_insn * = nullptr,
478 int limit = INT_MAX);
479 static void mark_used_regs_combine (rtx);
480 static void record_promoted_value (rtx_insn *, rtx);
481 static bool unmentioned_reg_p (rtx, rtx);
482 static void record_truncated_values (rtx *, void *);
483 static bool reg_truncated_to_mode (machine_mode, const_rtx);
484 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
485
486
487 /* It is not safe to use ordinary gen_lowpart in combine.
488 See comments in gen_lowpart_for_combine. */
489 #undef RTL_HOOKS_GEN_LOWPART
490 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
491
492 /* Our implementation of gen_lowpart never emits a new pseudo. */
493 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
494 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
495
496 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
497 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
498
499 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
500 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
501
502 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
503 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
504
505 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
506
507
508 /* Convenience wrapper for the canonicalize_comparison target hook.
509 Target hooks cannot use enum rtx_code. */
510 static inline void
511 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
512 bool op0_preserve_value)
513 {
514 int code_int = (int)*code;
515 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
516 *code = (enum rtx_code)code_int;
517 }
518
519 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
520 PATTERN cannot be split. Otherwise, it returns an insn sequence.
521 Updates OLD_NREGS with the max number of regs before the split
522 and NEW_NREGS after the split.
523 This is a wrapper around split_insns which ensures that the
524 reg_stat vector is made larger if the splitter creates a new
525 register. */
526
527 static rtx_insn *
528 combine_split_insns (rtx pattern, rtx_insn *insn,
529 unsigned int *old_nregs,
530 unsigned int *new_regs)
531 {
532 rtx_insn *ret;
533 unsigned int nregs;
534 *old_nregs = max_reg_num ();
535 ret = split_insns (pattern, insn);
536 *new_regs = nregs = max_reg_num ();
537 if (nregs > reg_stat.length ())
538 reg_stat.safe_grow_cleared (nregs, true);
539 return ret;
540 }
541
542 /* This is used by find_single_use to locate an rtx in LOC that
543 contains exactly one use of DEST, which is typically a REG.
544 It returns a pointer to the innermost rtx expression
545 containing DEST. Appearances of DEST that are being used to
546 totally replace it are not counted. */
547
548 static rtx *
549 find_single_use_1 (rtx dest, rtx *loc)
550 {
551 rtx x = *loc;
552 enum rtx_code code = GET_CODE (x);
553 rtx *result = NULL;
554 rtx *this_result;
555 int i;
556 const char *fmt;
557
558 switch (code)
559 {
560 case CONST:
561 case LABEL_REF:
562 case SYMBOL_REF:
563 CASE_CONST_ANY:
564 case CLOBBER:
565 return 0;
566
567 case SET:
568 /* If the destination is anything other than PC, a REG or a SUBREG
569 of a REG that occupies all of the REG, the insn uses DEST if
570 it is mentioned in the destination or the source. Otherwise, we
571 need just check the source. */
572 if (GET_CODE (SET_DEST (x)) != PC
573 && !REG_P (SET_DEST (x))
574 && ! (GET_CODE (SET_DEST (x)) == SUBREG
575 && REG_P (SUBREG_REG (SET_DEST (x)))
576 && !read_modify_subreg_p (SET_DEST (x))))
577 break;
578
579 return find_single_use_1 (dest, &SET_SRC (x));
580
581 case MEM:
582 case SUBREG:
583 return find_single_use_1 (dest, &XEXP (x, 0));
584
585 default:
586 break;
587 }
588
589 /* If it wasn't one of the common cases above, check each expression and
590 vector of this code. Look for a unique usage of DEST. */
591
592 fmt = GET_RTX_FORMAT (code);
593 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
594 {
595 if (fmt[i] == 'e')
596 {
597 if (dest == XEXP (x, i)
598 || (REG_P (dest) && REG_P (XEXP (x, i))
599 && REGNO (dest) == REGNO (XEXP (x, i))))
600 this_result = loc;
601 else
602 this_result = find_single_use_1 (dest, &XEXP (x, i));
603
604 if (result == NULL)
605 result = this_result;
606 else if (this_result)
607 /* Duplicate usage. */
608 return NULL;
609 }
610 else if (fmt[i] == 'E')
611 {
612 int j;
613
614 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
615 {
616 if (XVECEXP (x, i, j) == dest
617 || (REG_P (dest)
618 && REG_P (XVECEXP (x, i, j))
619 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
620 this_result = loc;
621 else
622 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
623
624 if (result == NULL)
625 result = this_result;
626 else if (this_result)
627 return NULL;
628 }
629 }
630 }
631
632 return result;
633 }
634
635
636 /* See if DEST, produced in INSN, is used only a single time in the
637 sequel. If so, return a pointer to the innermost rtx expression in which
638 it is used.
639
640 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
641
642 Otherwise, we find the single use by finding an insn that has a
643 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
644 only referenced once in that insn, we know that it must be the first
645 and last insn referencing DEST. */
646
647 static rtx *
648 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
649 {
650 basic_block bb;
651 rtx_insn *next;
652 rtx *result;
653 struct insn_link *link;
654
655 if (!REG_P (dest))
656 return 0;
657
658 bb = BLOCK_FOR_INSN (insn);
659 for (next = NEXT_INSN (insn);
660 next && BLOCK_FOR_INSN (next) == bb;
661 next = NEXT_INSN (next))
662 if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
663 {
664 FOR_EACH_LOG_LINK (link, next)
665 if (link->insn == insn && link->regno == REGNO (dest))
666 break;
667
668 if (link)
669 {
670 result = find_single_use_1 (dest, &PATTERN (next));
671 if (ploc)
672 *ploc = next;
673 return result;
674 }
675 }
676
677 return 0;
678 }
679
680 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
681 insn. The substitution can be undone by undo_all. If INTO is already
682 set to NEWVAL, do not record this change. Because computing NEWVAL might
683 also call SUBST, we have to compute it before we put anything into
684 the undo table. */
685
686 static void
687 do_SUBST (rtx *into, rtx newval)
688 {
689 struct undo *buf;
690 rtx oldval = *into;
691
692 if (oldval == newval)
693 return;
694
695 /* We'd like to catch as many invalid transformations here as
696 possible. Unfortunately, there are way too many mode changes
697 that are perfectly valid, so we'd waste too much effort for
698 little gain doing the checks here. Focus on catching invalid
699 transformations involving integer constants. */
700 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
701 && CONST_INT_P (newval))
702 {
703 /* Sanity check that we're replacing oldval with a CONST_INT
704 that is a valid sign-extension for the original mode. */
705 gcc_assert (INTVAL (newval)
706 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
707
708 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
709 CONST_INT is not valid, because after the replacement, the
710 original mode would be gone. Unfortunately, we can't tell
711 when do_SUBST is called to replace the operand thereof, so we
712 perform this test on oldval instead, checking whether an
713 invalid replacement took place before we got here. */
714 gcc_assert (!(GET_CODE (oldval) == SUBREG
715 && CONST_INT_P (SUBREG_REG (oldval))));
716 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
717 && CONST_INT_P (XEXP (oldval, 0))));
718 }
719
720 if (undobuf.frees)
721 buf = undobuf.frees, undobuf.frees = buf->next;
722 else
723 buf = XNEW (struct undo);
724
725 buf->kind = UNDO_RTX;
726 buf->where.r = into;
727 buf->old_contents.r = oldval;
728 *into = newval;
729
730 buf->next = undobuf.undos, undobuf.undos = buf;
731 }
732
733 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
734
735 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
736 for the value of a HOST_WIDE_INT value (including CONST_INT) is
737 not safe. */
738
739 static void
740 do_SUBST_INT (int *into, int newval)
741 {
742 struct undo *buf;
743 int oldval = *into;
744
745 if (oldval == newval)
746 return;
747
748 if (undobuf.frees)
749 buf = undobuf.frees, undobuf.frees = buf->next;
750 else
751 buf = XNEW (struct undo);
752
753 buf->kind = UNDO_INT;
754 buf->where.i = into;
755 buf->old_contents.i = oldval;
756 *into = newval;
757
758 buf->next = undobuf.undos, undobuf.undos = buf;
759 }
760
761 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
762
763 /* Similar to SUBST, but just substitute the mode. This is used when
764 changing the mode of a pseudo-register, so that any other
765 references to the entry in the regno_reg_rtx array will change as
766 well. */
767
768 static void
769 subst_mode (int regno, machine_mode newval)
770 {
771 struct undo *buf;
772 rtx reg = regno_reg_rtx[regno];
773 machine_mode oldval = GET_MODE (reg);
774
775 if (oldval == newval)
776 return;
777
778 if (undobuf.frees)
779 buf = undobuf.frees, undobuf.frees = buf->next;
780 else
781 buf = XNEW (struct undo);
782
783 buf->kind = UNDO_MODE;
784 buf->where.regno = regno;
785 buf->old_contents.m = oldval;
786 adjust_reg_mode (reg, newval);
787
788 buf->next = undobuf.undos, undobuf.undos = buf;
789 }
790
791 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
792
793 static void
794 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
795 {
796 struct undo *buf;
797 struct insn_link * oldval = *into;
798
799 if (oldval == newval)
800 return;
801
802 if (undobuf.frees)
803 buf = undobuf.frees, undobuf.frees = buf->next;
804 else
805 buf = XNEW (struct undo);
806
807 buf->kind = UNDO_LINKS;
808 buf->where.l = into;
809 buf->old_contents.l = oldval;
810 *into = newval;
811
812 buf->next = undobuf.undos, undobuf.undos = buf;
813 }
814
815 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
816
817 /* Subroutine of try_combine. Determine whether the replacement patterns
818 NEWPAT, NEWI2PAT and NEWOTHERPAT are more expensive according to insn_cost
819 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
820 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
821 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
822 of all the instructions can be estimated and the replacements are more
823 expensive than the original sequence. */
824
825 static bool
826 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
827 rtx newpat, rtx newi2pat, rtx newotherpat)
828 {
829 int i0_cost, i1_cost, i2_cost, i3_cost;
830 int new_i2_cost, new_i3_cost;
831 int old_cost, new_cost;
832
833 /* Lookup the original insn_costs. */
834 i2_cost = INSN_COST (i2);
835 i3_cost = INSN_COST (i3);
836
837 if (i1)
838 {
839 i1_cost = INSN_COST (i1);
840 if (i0)
841 {
842 i0_cost = INSN_COST (i0);
843 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
844 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
845 }
846 else
847 {
848 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
849 ? i1_cost + i2_cost + i3_cost : 0);
850 i0_cost = 0;
851 }
852 }
853 else
854 {
855 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
856 i1_cost = i0_cost = 0;
857 }
858
859 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
860 correct that. */
861 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
862 old_cost -= i1_cost;
863
864
865 /* Calculate the replacement insn_costs. */
866 rtx tmp = PATTERN (i3);
867 PATTERN (i3) = newpat;
868 int tmpi = INSN_CODE (i3);
869 INSN_CODE (i3) = -1;
870 new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
871 PATTERN (i3) = tmp;
872 INSN_CODE (i3) = tmpi;
873 if (newi2pat)
874 {
875 tmp = PATTERN (i2);
876 PATTERN (i2) = newi2pat;
877 tmpi = INSN_CODE (i2);
878 INSN_CODE (i2) = -1;
879 new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
880 PATTERN (i2) = tmp;
881 INSN_CODE (i2) = tmpi;
882 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
883 ? new_i2_cost + new_i3_cost : 0;
884 }
885 else
886 {
887 new_cost = new_i3_cost;
888 new_i2_cost = 0;
889 }
890
891 if (undobuf.other_insn)
892 {
893 int old_other_cost, new_other_cost;
894
895 old_other_cost = INSN_COST (undobuf.other_insn);
896 tmp = PATTERN (undobuf.other_insn);
897 PATTERN (undobuf.other_insn) = newotherpat;
898 tmpi = INSN_CODE (undobuf.other_insn);
899 INSN_CODE (undobuf.other_insn) = -1;
900 new_other_cost = insn_cost (undobuf.other_insn,
901 optimize_this_for_speed_p);
902 PATTERN (undobuf.other_insn) = tmp;
903 INSN_CODE (undobuf.other_insn) = tmpi;
904 if (old_other_cost > 0 && new_other_cost > 0)
905 {
906 old_cost += old_other_cost;
907 new_cost += new_other_cost;
908 }
909 else
910 old_cost = 0;
911 }
912
913 /* Disallow this combination if both new_cost and old_cost are greater than
914 zero, and new_cost is greater than old cost. */
915 bool reject = old_cost > 0 && new_cost > old_cost;
916
917 if (dump_file)
918 {
919 fprintf (dump_file, "%s combination of insns ",
920 reject ? "rejecting" : "allowing");
921 if (i0)
922 fprintf (dump_file, "%d, ", INSN_UID (i0));
923 if (i1 && INSN_UID (i1) != INSN_UID (i2))
924 fprintf (dump_file, "%d, ", INSN_UID (i1));
925 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
926
927 fprintf (dump_file, "original costs ");
928 if (i0)
929 fprintf (dump_file, "%d + ", i0_cost);
930 if (i1 && INSN_UID (i1) != INSN_UID (i2))
931 fprintf (dump_file, "%d + ", i1_cost);
932 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
933
934 if (newi2pat)
935 fprintf (dump_file, "replacement costs %d + %d = %d\n",
936 new_i2_cost, new_i3_cost, new_cost);
937 else
938 fprintf (dump_file, "replacement cost %d\n", new_cost);
939 }
940
941 if (reject)
942 return false;
943
944 /* Update the uid_insn_cost array with the replacement costs. */
945 INSN_COST (i2) = new_i2_cost;
946 INSN_COST (i3) = new_i3_cost;
947 if (i1)
948 {
949 INSN_COST (i1) = 0;
950 if (i0)
951 INSN_COST (i0) = 0;
952 }
953
954 return true;
955 }
956
957
958 /* Delete any insns that copy a register to itself.
959 Return true if the CFG was changed. */
960
961 static bool
962 delete_noop_moves (void)
963 {
964 rtx_insn *insn, *next;
965 basic_block bb;
966
967 bool edges_deleted = false;
968
969 FOR_EACH_BB_FN (bb, cfun)
970 {
971 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
972 {
973 next = NEXT_INSN (insn);
974 if (INSN_P (insn) && noop_move_p (insn))
975 {
976 if (dump_file)
977 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
978
979 edges_deleted |= delete_insn_and_edges (insn);
980 }
981 }
982 }
983
984 return edges_deleted;
985 }
986
987
988 /* Return false if we do not want to (or cannot) combine DEF. */
989 static bool
990 can_combine_def_p (df_ref def)
991 {
992 /* Do not consider if it is pre/post modification in MEM. */
993 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
994 return false;
995
996 unsigned int regno = DF_REF_REGNO (def);
997
998 /* Do not combine frame pointer adjustments. */
999 if ((regno == FRAME_POINTER_REGNUM
1000 && (!reload_completed || frame_pointer_needed))
1001 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1002 && regno == HARD_FRAME_POINTER_REGNUM
1003 && (!reload_completed || frame_pointer_needed))
1004 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1005 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1006 return false;
1007
1008 return true;
1009 }
1010
1011 /* Return false if we do not want to (or cannot) combine USE. */
1012 static bool
1013 can_combine_use_p (df_ref use)
1014 {
1015 /* Do not consider the usage of the stack pointer by function call. */
1016 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1017 return false;
1018
1019 return true;
1020 }
1021
1022 /* Fill in log links field for all insns. */
1023
1024 static void
1025 create_log_links (void)
1026 {
1027 basic_block bb;
1028 rtx_insn **next_use;
1029 rtx_insn *insn;
1030 df_ref def, use;
1031
1032 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1033
1034 /* Pass through each block from the end, recording the uses of each
1035 register and establishing log links when def is encountered.
1036 Note that we do not clear next_use array in order to save time,
1037 so we have to test whether the use is in the same basic block as def.
1038
1039 There are a few cases below when we do not consider the definition or
1040 usage -- these are taken from original flow.c did. Don't ask me why it is
1041 done this way; I don't know and if it works, I don't want to know. */
1042
1043 FOR_EACH_BB_FN (bb, cfun)
1044 {
1045 FOR_BB_INSNS_REVERSE (bb, insn)
1046 {
1047 if (!NONDEBUG_INSN_P (insn))
1048 continue;
1049
1050 /* Log links are created only once. */
1051 gcc_assert (!LOG_LINKS (insn));
1052
1053 FOR_EACH_INSN_DEF (def, insn)
1054 {
1055 unsigned int regno = DF_REF_REGNO (def);
1056 rtx_insn *use_insn;
1057
1058 if (!next_use[regno])
1059 continue;
1060
1061 if (!can_combine_def_p (def))
1062 continue;
1063
1064 use_insn = next_use[regno];
1065 next_use[regno] = NULL;
1066
1067 if (BLOCK_FOR_INSN (use_insn) != bb)
1068 continue;
1069
1070 /* flow.c claimed:
1071
1072 We don't build a LOG_LINK for hard registers contained
1073 in ASM_OPERANDs. If these registers get replaced,
1074 we might wind up changing the semantics of the insn,
1075 even if reload can make what appear to be valid
1076 assignments later. */
1077 if (regno < FIRST_PSEUDO_REGISTER
1078 && asm_noperands (PATTERN (use_insn)) >= 0)
1079 continue;
1080
1081 /* Don't add duplicate links between instructions. */
1082 struct insn_link *links;
1083 FOR_EACH_LOG_LINK (links, use_insn)
1084 if (insn == links->insn && regno == links->regno)
1085 break;
1086
1087 if (!links)
1088 LOG_LINKS (use_insn)
1089 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1090 }
1091
1092 FOR_EACH_INSN_USE (use, insn)
1093 if (can_combine_use_p (use))
1094 next_use[DF_REF_REGNO (use)] = insn;
1095 }
1096 }
1097
1098 free (next_use);
1099 }
1100
1101 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1102 true if we found a LOG_LINK that proves that A feeds B. This only works
1103 if there are no instructions between A and B which could have a link
1104 depending on A, since in that case we would not record a link for B. */
1105
1106 static bool
1107 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1108 {
1109 struct insn_link *links;
1110 FOR_EACH_LOG_LINK (links, b)
1111 if (links->insn == a)
1112 return true;
1113 return false;
1114 }
1115
1116 /* Main entry point for combiner. F is the first insn of the function.
1117 NREGS is the first unused pseudo-reg number.
1118
1119 Return nonzero if the CFG was changed (e.g. if the combiner has
1120 turned an indirect jump instruction into a direct jump). */
1121 static bool
1122 combine_instructions (rtx_insn *f, unsigned int nregs)
1123 {
1124 rtx_insn *insn, *next;
1125 struct insn_link *links, *nextlinks;
1126 rtx_insn *first;
1127 basic_block last_bb;
1128
1129 bool new_direct_jump_p = false;
1130
1131 for (first = f; first && !NONDEBUG_INSN_P (first); )
1132 first = NEXT_INSN (first);
1133 if (!first)
1134 return false;
1135
1136 combine_attempts = 0;
1137 combine_merges = 0;
1138 combine_extras = 0;
1139 combine_successes = 0;
1140
1141 rtl_hooks = combine_rtl_hooks;
1142
1143 reg_stat.safe_grow_cleared (nregs, true);
1144
1145 init_recog_no_volatile ();
1146
1147 /* Allocate array for insn info. */
1148 max_uid_known = get_max_uid ();
1149 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1150 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1151 gcc_obstack_init (&insn_link_obstack);
1152
1153 nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1154
1155 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1156 problems when, for example, we have j <<= 1 in a loop. */
1157
1158 nonzero_sign_valid = 0;
1159 label_tick = label_tick_ebb_start = 1;
1160
1161 /* Scan all SETs and see if we can deduce anything about what
1162 bits are known to be zero for some registers and how many copies
1163 of the sign bit are known to exist for those registers.
1164
1165 Also set any known values so that we can use it while searching
1166 for what bits are known to be set. */
1167
1168 setup_incoming_promotions (first);
1169 /* Allow the entry block and the first block to fall into the same EBB.
1170 Conceptually the incoming promotions are assigned to the entry block. */
1171 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1172
1173 create_log_links ();
1174 FOR_EACH_BB_FN (this_basic_block, cfun)
1175 {
1176 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1177 last_call_luid = 0;
1178 mem_last_set = -1;
1179
1180 label_tick++;
1181 if (!single_pred_p (this_basic_block)
1182 || single_pred (this_basic_block) != last_bb)
1183 label_tick_ebb_start = label_tick;
1184 last_bb = this_basic_block;
1185
1186 FOR_BB_INSNS (this_basic_block, insn)
1187 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1188 {
1189 rtx links;
1190
1191 subst_low_luid = DF_INSN_LUID (insn);
1192 subst_insn = insn;
1193
1194 note_stores (insn, set_nonzero_bits_and_sign_copies, insn);
1195 record_dead_and_set_regs (insn);
1196
1197 if (AUTO_INC_DEC)
1198 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1199 if (REG_NOTE_KIND (links) == REG_INC)
1200 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1201 insn);
1202
1203 /* Record the current insn_cost of this instruction. */
1204 INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1205 if (dump_file)
1206 {
1207 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1208 dump_insn_slim (dump_file, insn);
1209 }
1210 }
1211 }
1212
1213 nonzero_sign_valid = 1;
1214
1215 /* Now scan all the insns in forward order. */
1216 label_tick = label_tick_ebb_start = 1;
1217 init_reg_last ();
1218 setup_incoming_promotions (first);
1219 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1220 int max_combine = param_max_combine_insns;
1221
1222 FOR_EACH_BB_FN (this_basic_block, cfun)
1223 {
1224 rtx_insn *last_combined_insn = NULL;
1225
1226 /* Ignore instruction combination in basic blocks that are going to
1227 be removed as unreachable anyway. See PR82386. */
1228 if (EDGE_COUNT (this_basic_block->preds) == 0)
1229 continue;
1230
1231 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1232 last_call_luid = 0;
1233 mem_last_set = -1;
1234
1235 label_tick++;
1236 if (!single_pred_p (this_basic_block)
1237 || single_pred (this_basic_block) != last_bb)
1238 label_tick_ebb_start = label_tick;
1239 last_bb = this_basic_block;
1240
1241 rtl_profile_for_bb (this_basic_block);
1242 for (insn = BB_HEAD (this_basic_block);
1243 insn != NEXT_INSN (BB_END (this_basic_block));
1244 insn = next ? next : NEXT_INSN (insn))
1245 {
1246 next = 0;
1247 if (!NONDEBUG_INSN_P (insn))
1248 continue;
1249
1250 while (last_combined_insn
1251 && (!NONDEBUG_INSN_P (last_combined_insn)
1252 || last_combined_insn->deleted ()))
1253 last_combined_insn = PREV_INSN (last_combined_insn);
1254 if (last_combined_insn == NULL_RTX
1255 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1256 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1257 last_combined_insn = insn;
1258
1259 /* See if we know about function return values before this
1260 insn based upon SUBREG flags. */
1261 check_promoted_subreg (insn, PATTERN (insn));
1262
1263 /* See if we can find hardregs and subreg of pseudos in
1264 narrower modes. This could help turning TRUNCATEs
1265 into SUBREGs. */
1266 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1267
1268 /* Try this insn with each insn it links back to. */
1269
1270 FOR_EACH_LOG_LINK (links, insn)
1271 if ((next = try_combine (insn, links->insn, NULL,
1272 NULL, &new_direct_jump_p,
1273 last_combined_insn)) != 0)
1274 {
1275 statistics_counter_event (cfun, "two-insn combine", 1);
1276 goto retry;
1277 }
1278
1279 /* Try each sequence of three linked insns ending with this one. */
1280
1281 if (max_combine >= 3)
1282 FOR_EACH_LOG_LINK (links, insn)
1283 {
1284 rtx_insn *link = links->insn;
1285
1286 /* If the linked insn has been replaced by a note, then there
1287 is no point in pursuing this chain any further. */
1288 if (NOTE_P (link))
1289 continue;
1290
1291 FOR_EACH_LOG_LINK (nextlinks, link)
1292 if ((next = try_combine (insn, link, nextlinks->insn,
1293 NULL, &new_direct_jump_p,
1294 last_combined_insn)) != 0)
1295 {
1296 statistics_counter_event (cfun, "three-insn combine", 1);
1297 goto retry;
1298 }
1299 }
1300
1301 /* Try combining an insn with two different insns whose results it
1302 uses. */
1303 if (max_combine >= 3)
1304 FOR_EACH_LOG_LINK (links, insn)
1305 for (nextlinks = links->next; nextlinks;
1306 nextlinks = nextlinks->next)
1307 if ((next = try_combine (insn, links->insn,
1308 nextlinks->insn, NULL,
1309 &new_direct_jump_p,
1310 last_combined_insn)) != 0)
1311
1312 {
1313 statistics_counter_event (cfun, "three-insn combine", 1);
1314 goto retry;
1315 }
1316
1317 /* Try four-instruction combinations. */
1318 if (max_combine >= 4)
1319 FOR_EACH_LOG_LINK (links, insn)
1320 {
1321 struct insn_link *next1;
1322 rtx_insn *link = links->insn;
1323
1324 /* If the linked insn has been replaced by a note, then there
1325 is no point in pursuing this chain any further. */
1326 if (NOTE_P (link))
1327 continue;
1328
1329 FOR_EACH_LOG_LINK (next1, link)
1330 {
1331 rtx_insn *link1 = next1->insn;
1332 if (NOTE_P (link1))
1333 continue;
1334 /* I0 -> I1 -> I2 -> I3. */
1335 FOR_EACH_LOG_LINK (nextlinks, link1)
1336 if ((next = try_combine (insn, link, link1,
1337 nextlinks->insn,
1338 &new_direct_jump_p,
1339 last_combined_insn)) != 0)
1340 {
1341 statistics_counter_event (cfun, "four-insn combine", 1);
1342 goto retry;
1343 }
1344 /* I0, I1 -> I2, I2 -> I3. */
1345 for (nextlinks = next1->next; nextlinks;
1346 nextlinks = nextlinks->next)
1347 if ((next = try_combine (insn, link, link1,
1348 nextlinks->insn,
1349 &new_direct_jump_p,
1350 last_combined_insn)) != 0)
1351 {
1352 statistics_counter_event (cfun, "four-insn combine", 1);
1353 goto retry;
1354 }
1355 }
1356
1357 for (next1 = links->next; next1; next1 = next1->next)
1358 {
1359 rtx_insn *link1 = next1->insn;
1360 if (NOTE_P (link1))
1361 continue;
1362 /* I0 -> I2; I1, I2 -> I3. */
1363 FOR_EACH_LOG_LINK (nextlinks, link)
1364 if ((next = try_combine (insn, link, link1,
1365 nextlinks->insn,
1366 &new_direct_jump_p,
1367 last_combined_insn)) != 0)
1368 {
1369 statistics_counter_event (cfun, "four-insn combine", 1);
1370 goto retry;
1371 }
1372 /* I0 -> I1; I1, I2 -> I3. */
1373 FOR_EACH_LOG_LINK (nextlinks, link1)
1374 if ((next = try_combine (insn, link, link1,
1375 nextlinks->insn,
1376 &new_direct_jump_p,
1377 last_combined_insn)) != 0)
1378 {
1379 statistics_counter_event (cfun, "four-insn combine", 1);
1380 goto retry;
1381 }
1382 }
1383 }
1384
1385 /* Try this insn with each REG_EQUAL note it links back to. */
1386 FOR_EACH_LOG_LINK (links, insn)
1387 {
1388 rtx set, note;
1389 rtx_insn *temp = links->insn;
1390 if ((set = single_set (temp)) != 0
1391 && (note = find_reg_equal_equiv_note (temp)) != 0
1392 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1393 && ! side_effects_p (SET_SRC (set))
1394 /* Avoid using a register that may already been marked
1395 dead by an earlier instruction. */
1396 && ! unmentioned_reg_p (note, SET_SRC (set))
1397 && (GET_MODE (note) == VOIDmode
1398 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1399 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1400 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1401 || (GET_MODE (XEXP (SET_DEST (set), 0))
1402 == GET_MODE (note))))))
1403 {
1404 /* Temporarily replace the set's source with the
1405 contents of the REG_EQUAL note. The insn will
1406 be deleted or recognized by try_combine. */
1407 rtx orig_src = SET_SRC (set);
1408 rtx orig_dest = SET_DEST (set);
1409 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1410 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1411 SET_SRC (set) = note;
1412 i2mod = temp;
1413 i2mod_old_rhs = copy_rtx (orig_src);
1414 i2mod_new_rhs = copy_rtx (note);
1415 next = try_combine (insn, i2mod, NULL, NULL,
1416 &new_direct_jump_p,
1417 last_combined_insn);
1418 i2mod = NULL;
1419 if (next)
1420 {
1421 statistics_counter_event (cfun, "insn-with-note combine", 1);
1422 goto retry;
1423 }
1424 INSN_CODE (temp) = -1;
1425 SET_SRC (set) = orig_src;
1426 SET_DEST (set) = orig_dest;
1427 }
1428 }
1429
1430 if (!NOTE_P (insn))
1431 record_dead_and_set_regs (insn);
1432
1433 retry:
1434 ;
1435 }
1436 }
1437
1438 default_rtl_profile ();
1439 clear_bb_flags ();
1440
1441 if (purge_all_dead_edges ())
1442 new_direct_jump_p = true;
1443 if (delete_noop_moves ())
1444 new_direct_jump_p = true;
1445
1446 /* Clean up. */
1447 obstack_free (&insn_link_obstack, NULL);
1448 free (uid_log_links);
1449 free (uid_insn_cost);
1450 reg_stat.release ();
1451
1452 {
1453 struct undo *undo, *next;
1454 for (undo = undobuf.frees; undo; undo = next)
1455 {
1456 next = undo->next;
1457 free (undo);
1458 }
1459 undobuf.frees = 0;
1460 }
1461
1462 statistics_counter_event (cfun, "attempts", combine_attempts);
1463 statistics_counter_event (cfun, "merges", combine_merges);
1464 statistics_counter_event (cfun, "extras", combine_extras);
1465 statistics_counter_event (cfun, "successes", combine_successes);
1466
1467 nonzero_sign_valid = 0;
1468 rtl_hooks = general_rtl_hooks;
1469
1470 /* Make recognizer allow volatile MEMs again. */
1471 init_recog ();
1472
1473 return new_direct_jump_p;
1474 }
1475
1476 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1477
1478 static void
1479 init_reg_last (void)
1480 {
1481 unsigned int i;
1482 reg_stat_type *p;
1483
1484 FOR_EACH_VEC_ELT (reg_stat, i, p)
1485 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1486 }
1487
1488 /* Set up any promoted values for incoming argument registers. */
1489
1490 static void
1491 setup_incoming_promotions (rtx_insn *first)
1492 {
1493 tree arg;
1494 bool strictly_local = false;
1495
1496 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1497 arg = DECL_CHAIN (arg))
1498 {
1499 rtx x, reg = DECL_INCOMING_RTL (arg);
1500 int uns1, uns3;
1501 machine_mode mode1, mode2, mode3, mode4;
1502
1503 /* Only continue if the incoming argument is in a register. */
1504 if (!REG_P (reg))
1505 continue;
1506
1507 /* Determine, if possible, whether all call sites of the current
1508 function lie within the current compilation unit. (This does
1509 take into account the exporting of a function via taking its
1510 address, and so forth.) */
1511 strictly_local
1512 = cgraph_node::local_info_node (current_function_decl)->local;
1513
1514 /* The mode and signedness of the argument before any promotions happen
1515 (equal to the mode of the pseudo holding it at that stage). */
1516 mode1 = TYPE_MODE (TREE_TYPE (arg));
1517 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1518
1519 /* The mode and signedness of the argument after any source language and
1520 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1521 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1522 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1523
1524 /* The mode and signedness of the argument as it is actually passed,
1525 see assign_parm_setup_reg in function.cc. */
1526 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1527 TREE_TYPE (cfun->decl), 0);
1528
1529 /* The mode of the register in which the argument is being passed. */
1530 mode4 = GET_MODE (reg);
1531
1532 /* Eliminate sign extensions in the callee when:
1533 (a) A mode promotion has occurred; */
1534 if (mode1 == mode3)
1535 continue;
1536 /* (b) The mode of the register is the same as the mode of
1537 the argument as it is passed; */
1538 if (mode3 != mode4)
1539 continue;
1540 /* (c) There's no language level extension; */
1541 if (mode1 == mode2)
1542 ;
1543 /* (c.1) All callers are from the current compilation unit. If that's
1544 the case we don't have to rely on an ABI, we only have to know
1545 what we're generating right now, and we know that we will do the
1546 mode1 to mode2 promotion with the given sign. */
1547 else if (!strictly_local)
1548 continue;
1549 /* (c.2) The combination of the two promotions is useful. This is
1550 true when the signs match, or if the first promotion is unsigned.
1551 In the later case, (sign_extend (zero_extend x)) is the same as
1552 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1553 else if (uns1)
1554 uns3 = true;
1555 else if (uns3)
1556 continue;
1557
1558 /* Record that the value was promoted from mode1 to mode3,
1559 so that any sign extension at the head of the current
1560 function may be eliminated. */
1561 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1562 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1563 record_value_for_reg (reg, first, x);
1564 }
1565 }
1566
1567 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1568 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1569 because some machines (maybe most) will actually do the sign-extension and
1570 this is the conservative approach.
1571
1572 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1573 kludge. */
1574
1575 static rtx
1576 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1577 {
1578 scalar_int_mode int_mode;
1579 if (CONST_INT_P (src)
1580 && is_a <scalar_int_mode> (mode, &int_mode)
1581 && GET_MODE_PRECISION (int_mode) < prec
1582 && INTVAL (src) > 0
1583 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1584 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1585
1586 return src;
1587 }
1588
1589 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1590 and SET. */
1591
1592 static void
1593 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1594 rtx x)
1595 {
1596 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1597 unsigned HOST_WIDE_INT bits = 0;
1598 rtx reg_equal = NULL, src = SET_SRC (set);
1599 unsigned int num = 0;
1600
1601 if (reg_equal_note)
1602 reg_equal = XEXP (reg_equal_note, 0);
1603
1604 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1605 {
1606 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1607 if (reg_equal)
1608 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1609 }
1610
1611 /* Don't call nonzero_bits if it cannot change anything. */
1612 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1613 {
1614 machine_mode mode = GET_MODE (x);
1615 if (GET_MODE_CLASS (mode) == MODE_INT
1616 && HWI_COMPUTABLE_MODE_P (mode))
1617 mode = nonzero_bits_mode;
1618 bits = nonzero_bits (src, mode);
1619 if (reg_equal && bits)
1620 bits &= nonzero_bits (reg_equal, mode);
1621 rsp->nonzero_bits |= bits;
1622 }
1623
1624 /* Don't call num_sign_bit_copies if it cannot change anything. */
1625 if (rsp->sign_bit_copies != 1)
1626 {
1627 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1628 if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1629 {
1630 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1631 if (num == 0 || numeq > num)
1632 num = numeq;
1633 }
1634 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1635 rsp->sign_bit_copies = num;
1636 }
1637 }
1638
1639 /* Called via note_stores. If X is a pseudo that is narrower than
1640 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1641
1642 If we are setting only a portion of X and we can't figure out what
1643 portion, assume all bits will be used since we don't know what will
1644 be happening.
1645
1646 Similarly, set how many bits of X are known to be copies of the sign bit
1647 at all locations in the function. This is the smallest number implied
1648 by any set of X. */
1649
1650 static void
1651 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1652 {
1653 rtx_insn *insn = (rtx_insn *) data;
1654 scalar_int_mode mode;
1655
1656 if (REG_P (x)
1657 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1658 /* If this register is undefined at the start of the file, we can't
1659 say what its contents were. */
1660 && ! REGNO_REG_SET_P
1661 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1662 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1663 && HWI_COMPUTABLE_MODE_P (mode))
1664 {
1665 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1666
1667 if (set == 0 || GET_CODE (set) == CLOBBER)
1668 {
1669 rsp->nonzero_bits = GET_MODE_MASK (mode);
1670 rsp->sign_bit_copies = 1;
1671 return;
1672 }
1673
1674 /* If this register is being initialized using itself, and the
1675 register is uninitialized in this basic block, and there are
1676 no LOG_LINKS which set the register, then part of the
1677 register is uninitialized. In that case we can't assume
1678 anything about the number of nonzero bits.
1679
1680 ??? We could do better if we checked this in
1681 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1682 could avoid making assumptions about the insn which initially
1683 sets the register, while still using the information in other
1684 insns. We would have to be careful to check every insn
1685 involved in the combination. */
1686
1687 if (insn
1688 && reg_referenced_p (x, PATTERN (insn))
1689 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1690 REGNO (x)))
1691 {
1692 struct insn_link *link;
1693
1694 FOR_EACH_LOG_LINK (link, insn)
1695 if (dead_or_set_p (link->insn, x))
1696 break;
1697 if (!link)
1698 {
1699 rsp->nonzero_bits = GET_MODE_MASK (mode);
1700 rsp->sign_bit_copies = 1;
1701 return;
1702 }
1703 }
1704
1705 /* If this is a complex assignment, see if we can convert it into a
1706 simple assignment. */
1707 set = expand_field_assignment (set);
1708
1709 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1710 set what we know about X. */
1711
1712 if (SET_DEST (set) == x
1713 || (paradoxical_subreg_p (SET_DEST (set))
1714 && SUBREG_REG (SET_DEST (set)) == x))
1715 update_rsp_from_reg_equal (rsp, insn, set, x);
1716 else
1717 {
1718 rsp->nonzero_bits = GET_MODE_MASK (mode);
1719 rsp->sign_bit_copies = 1;
1720 }
1721 }
1722 }
1723
1724 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1725 optionally insns that were previously combined into I3 or that will be
1726 combined into the merger of INSN and I3. The order is PRED, PRED2,
1727 INSN, SUCC, SUCC2, I3.
1728
1729 Return false if the combination is not allowed for any reason.
1730
1731 If the combination is allowed, *PDEST will be set to the single
1732 destination of INSN and *PSRC to the single source, and this function
1733 will return true. */
1734
1735 static bool
1736 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1737 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1738 rtx *pdest, rtx *psrc)
1739 {
1740 int i;
1741 const_rtx set = 0;
1742 rtx src, dest;
1743 rtx_insn *p;
1744 rtx link;
1745 bool all_adjacent = true;
1746 bool (*is_volatile_p) (const_rtx);
1747
1748 if (succ)
1749 {
1750 if (succ2)
1751 {
1752 if (next_active_insn (succ2) != i3)
1753 all_adjacent = false;
1754 if (next_active_insn (succ) != succ2)
1755 all_adjacent = false;
1756 }
1757 else if (next_active_insn (succ) != i3)
1758 all_adjacent = false;
1759 if (next_active_insn (insn) != succ)
1760 all_adjacent = false;
1761 }
1762 else if (next_active_insn (insn) != i3)
1763 all_adjacent = false;
1764
1765 /* Can combine only if previous insn is a SET of a REG or a SUBREG,
1766 or a PARALLEL consisting of such a SET and CLOBBERs.
1767
1768 If INSN has CLOBBER parallel parts, ignore them for our processing.
1769 By definition, these happen during the execution of the insn. When it
1770 is merged with another insn, all bets are off. If they are, in fact,
1771 needed and aren't also supplied in I3, they may be added by
1772 recog_for_combine. Otherwise, it won't match.
1773
1774 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1775 note.
1776
1777 Get the source and destination of INSN. If more than one, can't
1778 combine. */
1779
1780 if (GET_CODE (PATTERN (insn)) == SET)
1781 set = PATTERN (insn);
1782 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1783 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1784 {
1785 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1786 {
1787 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1788
1789 switch (GET_CODE (elt))
1790 {
1791 /* This is important to combine floating point insns
1792 for the SH4 port. */
1793 case USE:
1794 /* Combining an isolated USE doesn't make sense.
1795 We depend here on combinable_i3pat to reject them. */
1796 /* The code below this loop only verifies that the inputs of
1797 the SET in INSN do not change. We call reg_set_between_p
1798 to verify that the REG in the USE does not change between
1799 I3 and INSN.
1800 If the USE in INSN was for a pseudo register, the matching
1801 insn pattern will likely match any register; combining this
1802 with any other USE would only be safe if we knew that the
1803 used registers have identical values, or if there was
1804 something to tell them apart, e.g. different modes. For
1805 now, we forgo such complicated tests and simply disallow
1806 combining of USES of pseudo registers with any other USE. */
1807 if (REG_P (XEXP (elt, 0))
1808 && GET_CODE (PATTERN (i3)) == PARALLEL)
1809 {
1810 rtx i3pat = PATTERN (i3);
1811 int i = XVECLEN (i3pat, 0) - 1;
1812 unsigned int regno = REGNO (XEXP (elt, 0));
1813
1814 do
1815 {
1816 rtx i3elt = XVECEXP (i3pat, 0, i);
1817
1818 if (GET_CODE (i3elt) == USE
1819 && REG_P (XEXP (i3elt, 0))
1820 && (REGNO (XEXP (i3elt, 0)) == regno
1821 ? reg_set_between_p (XEXP (elt, 0),
1822 PREV_INSN (insn), i3)
1823 : regno >= FIRST_PSEUDO_REGISTER))
1824 return false;
1825 }
1826 while (--i >= 0);
1827 }
1828 break;
1829
1830 /* We can ignore CLOBBERs. */
1831 case CLOBBER:
1832 break;
1833
1834 case SET:
1835 /* Ignore SETs whose result isn't used but not those that
1836 have side-effects. */
1837 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1838 && insn_nothrow_p (insn)
1839 && !side_effects_p (elt))
1840 break;
1841
1842 /* If we have already found a SET, this is a second one and
1843 so we cannot combine with this insn. */
1844 if (set)
1845 return false;
1846
1847 set = elt;
1848 break;
1849
1850 default:
1851 /* Anything else means we can't combine. */
1852 return false;
1853 }
1854 }
1855
1856 if (set == 0
1857 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1858 so don't do anything with it. */
1859 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1860 return false;
1861 }
1862 else
1863 return false;
1864
1865 if (set == 0)
1866 return false;
1867
1868 /* The simplification in expand_field_assignment may call back to
1869 get_last_value, so set safe guard here. */
1870 subst_low_luid = DF_INSN_LUID (insn);
1871
1872 set = expand_field_assignment (set);
1873 src = SET_SRC (set), dest = SET_DEST (set);
1874
1875 /* Do not eliminate user-specified register if it is in an
1876 asm input because we may break the register asm usage defined
1877 in GCC manual if allow to do so.
1878 Be aware that this may cover more cases than we expect but this
1879 should be harmless. */
1880 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1881 && extract_asm_operands (PATTERN (i3)))
1882 return false;
1883
1884 /* Don't eliminate a store in the stack pointer. */
1885 if (dest == stack_pointer_rtx
1886 /* Don't combine with an insn that sets a register to itself if it has
1887 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1888 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1889 /* Can't merge an ASM_OPERANDS. */
1890 || GET_CODE (src) == ASM_OPERANDS
1891 /* Can't merge a function call. */
1892 || GET_CODE (src) == CALL
1893 /* Don't eliminate a function call argument. */
1894 || (CALL_P (i3)
1895 && (find_reg_fusage (i3, USE, dest)
1896 || (REG_P (dest)
1897 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1898 && global_regs[REGNO (dest)])))
1899 /* Don't substitute into an incremented register. */
1900 || FIND_REG_INC_NOTE (i3, dest)
1901 || (succ && FIND_REG_INC_NOTE (succ, dest))
1902 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1903 /* Don't substitute into a non-local goto, this confuses CFG. */
1904 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1905 /* Make sure that DEST is not used after INSN but before SUCC, or
1906 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1907 || (!all_adjacent
1908 && ((succ2
1909 && (reg_used_between_p (dest, succ2, i3)
1910 || reg_used_between_p (dest, succ, succ2)))
1911 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1912 || (!succ2 && !succ && reg_used_between_p (dest, insn, i3))
1913 || (succ
1914 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1915 that case SUCC is not in the insn stream, so use SUCC2
1916 instead for this test. */
1917 && reg_used_between_p (dest, insn,
1918 succ2
1919 && INSN_UID (succ) == INSN_UID (succ2)
1920 ? succ2 : succ))))
1921 /* Make sure that the value that is to be substituted for the register
1922 does not use any registers whose values alter in between. However,
1923 If the insns are adjacent, a use can't cross a set even though we
1924 think it might (this can happen for a sequence of insns each setting
1925 the same destination; last_set of that register might point to
1926 a NOTE). If INSN has a REG_EQUIV note, the register is always
1927 equivalent to the memory so the substitution is valid even if there
1928 are intervening stores. Also, don't move a volatile asm or
1929 UNSPEC_VOLATILE across any other insns. */
1930 || (! all_adjacent
1931 && (((!MEM_P (src)
1932 || ! find_reg_note (insn, REG_EQUIV, src))
1933 && modified_between_p (src, insn, i3))
1934 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1935 || GET_CODE (src) == UNSPEC_VOLATILE))
1936 /* Don't combine across a CALL_INSN, because that would possibly
1937 change whether the life span of some REGs crosses calls or not,
1938 and it is a pain to update that information.
1939 Exception: if source is a constant, moving it later can't hurt.
1940 Accept that as a special case. */
1941 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1942 return false;
1943
1944 /* DEST must be a REG. */
1945 if (REG_P (dest))
1946 {
1947 /* If register alignment is being enforced for multi-word items in all
1948 cases except for parameters, it is possible to have a register copy
1949 insn referencing a hard register that is not allowed to contain the
1950 mode being copied and which would not be valid as an operand of most
1951 insns. Eliminate this problem by not combining with such an insn.
1952
1953 Also, on some machines we don't want to extend the life of a hard
1954 register. */
1955
1956 if (REG_P (src)
1957 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1958 && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
1959 /* Don't extend the life of a hard register unless it is
1960 user variable (if we have few registers) or it can't
1961 fit into the desired register (meaning something special
1962 is going on).
1963 Also avoid substituting a return register into I3, because
1964 reload can't handle a conflict with constraints of other
1965 inputs. */
1966 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1967 && !targetm.hard_regno_mode_ok (REGNO (src),
1968 GET_MODE (src)))))
1969 return false;
1970 }
1971 else
1972 return false;
1973
1974
1975 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1976 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1977 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1978 {
1979 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1980
1981 /* If the clobber represents an earlyclobber operand, we must not
1982 substitute an expression containing the clobbered register.
1983 As we do not analyze the constraint strings here, we have to
1984 make the conservative assumption. However, if the register is
1985 a fixed hard reg, the clobber cannot represent any operand;
1986 we leave it up to the machine description to either accept or
1987 reject use-and-clobber patterns. */
1988 if (!REG_P (reg)
1989 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1990 || !fixed_regs[REGNO (reg)])
1991 if (reg_overlap_mentioned_p (reg, src))
1992 return false;
1993 }
1994
1995 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1996 or not), reject, unless nothing volatile comes between it and I3 */
1997
1998 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1999 {
2000 /* Make sure neither succ nor succ2 contains a volatile reference. */
2001 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2002 return false;
2003 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2004 return false;
2005 /* We'll check insns between INSN and I3 below. */
2006 }
2007
2008 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2009 to be an explicit register variable, and was chosen for a reason. */
2010
2011 if (GET_CODE (src) == ASM_OPERANDS
2012 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2013 return false;
2014
2015 /* If INSN contains volatile references (specifically volatile MEMs),
2016 we cannot combine across any other volatile references.
2017 Even if INSN doesn't contain volatile references, any intervening
2018 volatile insn might affect machine state. */
2019
2020 is_volatile_p = volatile_refs_p (PATTERN (insn))
2021 ? volatile_refs_p
2022 : volatile_insn_p;
2023
2024 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2025 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2026 return false;
2027
2028 /* If INSN contains an autoincrement or autodecrement, make sure that
2029 register is not used between there and I3, and not already used in
2030 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2031 Also insist that I3 not be a jump if using LRA; if it were one
2032 and the incremented register were spilled, we would lose.
2033 Reload handles this correctly. */
2034
2035 if (AUTO_INC_DEC)
2036 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2037 if (REG_NOTE_KIND (link) == REG_INC
2038 && ((JUMP_P (i3) && targetm.lra_p ())
2039 || reg_used_between_p (XEXP (link, 0), insn, i3)
2040 || (pred != NULL_RTX
2041 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2042 || (pred2 != NULL_RTX
2043 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2044 || (succ != NULL_RTX
2045 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2046 || (succ2 != NULL_RTX
2047 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2048 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2049 return false;
2050
2051 /* If we get here, we have passed all the tests and the combination is
2052 to be allowed. */
2053
2054 *pdest = dest;
2055 *psrc = src;
2056
2057 return true;
2058 }
2059
2060 /* LOC is the location within I3 that contains its pattern or the component
2061 of a PARALLEL of the pattern. We validate that it is valid for combining.
2062
2063 One problem is if I3 modifies its output, as opposed to replacing it
2064 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2065 doing so would produce an insn that is not equivalent to the original insns.
2066
2067 Consider:
2068
2069 (set (reg:DI 101) (reg:DI 100))
2070 (set (subreg:SI (reg:DI 101) 0) <foo>)
2071
2072 This is NOT equivalent to:
2073
2074 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2075 (set (reg:DI 101) (reg:DI 100))])
2076
2077 Not only does this modify 100 (in which case it might still be valid
2078 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2079
2080 We can also run into a problem if I2 sets a register that I1
2081 uses and I1 gets directly substituted into I3 (not via I2). In that
2082 case, we would be getting the wrong value of I2DEST into I3, so we
2083 must reject the combination. This case occurs when I2 and I1 both
2084 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2085 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2086 of a SET must prevent combination from occurring. The same situation
2087 can occur for I0, in which case I0_NOT_IN_SRC is set.
2088
2089 Before doing the above check, we first try to expand a field assignment
2090 into a set of logical operations.
2091
2092 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2093 we place a register that is both set and used within I3. If more than one
2094 such register is detected, we fail.
2095
2096 Return true if the combination is valid, false otherwise. */
2097
2098 static bool
2099 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2100 bool i1_not_in_src, bool i0_not_in_src, rtx *pi3dest_killed)
2101 {
2102 rtx x = *loc;
2103
2104 if (GET_CODE (x) == SET)
2105 {
2106 rtx set = x ;
2107 rtx dest = SET_DEST (set);
2108 rtx src = SET_SRC (set);
2109 rtx inner_dest = dest;
2110 rtx subdest;
2111
2112 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2113 || GET_CODE (inner_dest) == SUBREG
2114 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2115 inner_dest = XEXP (inner_dest, 0);
2116
2117 /* Check for the case where I3 modifies its output, as discussed
2118 above. We don't want to prevent pseudos from being combined
2119 into the address of a MEM, so only prevent the combination if
2120 i1 or i2 set the same MEM. */
2121 if ((inner_dest != dest &&
2122 (!MEM_P (inner_dest)
2123 || rtx_equal_p (i2dest, inner_dest)
2124 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2125 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2126 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2127 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2128 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2129
2130 /* This is the same test done in can_combine_p except we can't test
2131 all_adjacent; we don't have to, since this instruction will stay
2132 in place, thus we are not considering increasing the lifetime of
2133 INNER_DEST.
2134
2135 Also, if this insn sets a function argument, combining it with
2136 something that might need a spill could clobber a previous
2137 function argument; the all_adjacent test in can_combine_p also
2138 checks this; here, we do a more specific test for this case. */
2139
2140 || (REG_P (inner_dest)
2141 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2142 && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2143 GET_MODE (inner_dest)))
2144 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2145 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2146 return false;
2147
2148 /* If DEST is used in I3, it is being killed in this insn, so
2149 record that for later. We have to consider paradoxical
2150 subregs here, since they kill the whole register, but we
2151 ignore partial subregs, STRICT_LOW_PART, etc.
2152 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2153 STACK_POINTER_REGNUM, since these are always considered to be
2154 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2155 subdest = dest;
2156 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2157 subdest = SUBREG_REG (subdest);
2158 if (pi3dest_killed
2159 && REG_P (subdest)
2160 && reg_referenced_p (subdest, PATTERN (i3))
2161 && REGNO (subdest) != FRAME_POINTER_REGNUM
2162 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2163 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2164 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2165 || (REGNO (subdest) != ARG_POINTER_REGNUM
2166 || ! fixed_regs [REGNO (subdest)]))
2167 && REGNO (subdest) != STACK_POINTER_REGNUM)
2168 {
2169 if (*pi3dest_killed)
2170 return false;
2171
2172 *pi3dest_killed = subdest;
2173 }
2174 }
2175
2176 else if (GET_CODE (x) == PARALLEL)
2177 {
2178 int i;
2179
2180 for (i = 0; i < XVECLEN (x, 0); i++)
2181 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2182 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2183 return false;
2184 }
2185
2186 return true;
2187 }
2188
2189 /* Return true if X is an arithmetic expression that contains a multiplication
2190 and division. We don't count multiplications by powers of two here. */
2191
2192 static bool
2193 contains_muldiv (rtx x)
2194 {
2195 switch (GET_CODE (x))
2196 {
2197 case MOD: case DIV: case UMOD: case UDIV:
2198 return true;
2199
2200 case MULT:
2201 return ! (CONST_INT_P (XEXP (x, 1))
2202 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2203 default:
2204 if (BINARY_P (x))
2205 return contains_muldiv (XEXP (x, 0))
2206 || contains_muldiv (XEXP (x, 1));
2207
2208 if (UNARY_P (x))
2209 return contains_muldiv (XEXP (x, 0));
2210
2211 return false;
2212 }
2213 }
2214
2215 /* Determine whether INSN can be used in a combination. Return true if
2216 not. This is used in try_combine to detect early some cases where we
2217 can't perform combinations. */
2218
2219 static bool
2220 cant_combine_insn_p (rtx_insn *insn)
2221 {
2222 rtx set;
2223 rtx src, dest;
2224
2225 /* If this isn't really an insn, we can't do anything.
2226 This can occur when flow deletes an insn that it has merged into an
2227 auto-increment address. */
2228 if (!NONDEBUG_INSN_P (insn))
2229 return true;
2230
2231 /* Never combine loads and stores involving hard regs that are likely
2232 to be spilled. The register allocator can usually handle such
2233 reg-reg moves by tying. If we allow the combiner to make
2234 substitutions of likely-spilled regs, reload might die.
2235 As an exception, we allow combinations involving fixed regs; these are
2236 not available to the register allocator so there's no risk involved. */
2237
2238 set = single_set (insn);
2239 if (! set)
2240 return false;
2241 src = SET_SRC (set);
2242 dest = SET_DEST (set);
2243 if (GET_CODE (src) == SUBREG)
2244 src = SUBREG_REG (src);
2245 if (GET_CODE (dest) == SUBREG)
2246 dest = SUBREG_REG (dest);
2247 if (REG_P (src) && REG_P (dest)
2248 && ((HARD_REGISTER_P (src)
2249 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2250 #ifdef LEAF_REGISTERS
2251 && ! LEAF_REGISTERS [REGNO (src)])
2252 #else
2253 )
2254 #endif
2255 || (HARD_REGISTER_P (dest)
2256 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2257 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2258 return true;
2259
2260 return false;
2261 }
2262
2263 struct likely_spilled_retval_info
2264 {
2265 unsigned regno, nregs;
2266 unsigned mask;
2267 };
2268
2269 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2270 hard registers that are known to be written to / clobbered in full. */
2271 static void
2272 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2273 {
2274 struct likely_spilled_retval_info *const info =
2275 (struct likely_spilled_retval_info *) data;
2276 unsigned regno, nregs;
2277 unsigned new_mask;
2278
2279 if (!REG_P (XEXP (set, 0)))
2280 return;
2281 regno = REGNO (x);
2282 if (regno >= info->regno + info->nregs)
2283 return;
2284 nregs = REG_NREGS (x);
2285 if (regno + nregs <= info->regno)
2286 return;
2287 new_mask = (2U << (nregs - 1)) - 1;
2288 if (regno < info->regno)
2289 new_mask >>= info->regno - regno;
2290 else
2291 new_mask <<= regno - info->regno;
2292 info->mask &= ~new_mask;
2293 }
2294
2295 /* Return true iff part of the return value is live during INSN, and
2296 it is likely spilled. This can happen when more than one insn is needed
2297 to copy the return value, e.g. when we consider to combine into the
2298 second copy insn for a complex value. */
2299
2300 static bool
2301 likely_spilled_retval_p (rtx_insn *insn)
2302 {
2303 rtx_insn *use = BB_END (this_basic_block);
2304 rtx reg;
2305 rtx_insn *p;
2306 unsigned regno, nregs;
2307 /* We assume here that no machine mode needs more than
2308 32 hard registers when the value overlaps with a register
2309 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2310 unsigned mask;
2311 struct likely_spilled_retval_info info;
2312
2313 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2314 return false;
2315 reg = XEXP (PATTERN (use), 0);
2316 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2317 return false;
2318 regno = REGNO (reg);
2319 nregs = REG_NREGS (reg);
2320 if (nregs == 1)
2321 return false;
2322 mask = (2U << (nregs - 1)) - 1;
2323
2324 /* Disregard parts of the return value that are set later. */
2325 info.regno = regno;
2326 info.nregs = nregs;
2327 info.mask = mask;
2328 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2329 if (INSN_P (p))
2330 note_stores (p, likely_spilled_retval_1, &info);
2331 mask = info.mask;
2332
2333 /* Check if any of the (probably) live return value registers is
2334 likely spilled. */
2335 nregs --;
2336 do
2337 {
2338 if ((mask & 1 << nregs)
2339 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2340 return true;
2341 } while (nregs--);
2342 return false;
2343 }
2344
2345 /* Adjust INSN after we made a change to its destination.
2346
2347 Changing the destination can invalidate notes that say something about
2348 the results of the insn and a LOG_LINK pointing to the insn. */
2349
2350 static void
2351 adjust_for_new_dest (rtx_insn *insn)
2352 {
2353 /* For notes, be conservative and simply remove them. */
2354 remove_reg_equal_equiv_notes (insn, true);
2355
2356 /* The new insn will have a destination that was previously the destination
2357 of an insn just above it. Call distribute_links to make a LOG_LINK from
2358 the next use of that destination. */
2359
2360 rtx set = single_set (insn);
2361 gcc_assert (set);
2362
2363 rtx reg = SET_DEST (set);
2364
2365 while (GET_CODE (reg) == ZERO_EXTRACT
2366 || GET_CODE (reg) == STRICT_LOW_PART
2367 || GET_CODE (reg) == SUBREG)
2368 reg = XEXP (reg, 0);
2369 gcc_assert (REG_P (reg));
2370
2371 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2372
2373 df_insn_rescan (insn);
2374 }
2375
2376 /* Return TRUE if combine can reuse reg X in mode MODE.
2377 ADDED_SETS is trueif the original set is still required. */
2378 static bool
2379 can_change_dest_mode (rtx x, bool added_sets, machine_mode mode)
2380 {
2381 unsigned int regno;
2382
2383 if (!REG_P (x))
2384 return false;
2385
2386 /* Don't change between modes with different underlying register sizes,
2387 since this could lead to invalid subregs. */
2388 if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2389 REGMODE_NATURAL_SIZE (GET_MODE (x))))
2390 return false;
2391
2392 regno = REGNO (x);
2393 /* Allow hard registers if the new mode is legal, and occupies no more
2394 registers than the old mode. */
2395 if (regno < FIRST_PSEUDO_REGISTER)
2396 return (targetm.hard_regno_mode_ok (regno, mode)
2397 && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2398
2399 /* Or a pseudo that is only used once. */
2400 return (regno < reg_n_sets_max
2401 && REG_N_SETS (regno) == 1
2402 && !added_sets
2403 && !REG_USERVAR_P (x));
2404 }
2405
2406
2407 /* Check whether X, the destination of a set, refers to part of
2408 the register specified by REG. */
2409
2410 static bool
2411 reg_subword_p (rtx x, rtx reg)
2412 {
2413 /* Check that reg is an integer mode register. */
2414 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2415 return false;
2416
2417 if (GET_CODE (x) == STRICT_LOW_PART
2418 || GET_CODE (x) == ZERO_EXTRACT)
2419 x = XEXP (x, 0);
2420
2421 return GET_CODE (x) == SUBREG
2422 && !paradoxical_subreg_p (x)
2423 && SUBREG_REG (x) == reg
2424 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2425 }
2426
2427 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2428 by an arbitrary number of CLOBBERs. */
2429 static bool
2430 is_parallel_of_n_reg_sets (rtx pat, int n)
2431 {
2432 if (GET_CODE (pat) != PARALLEL)
2433 return false;
2434
2435 int len = XVECLEN (pat, 0);
2436 if (len < n)
2437 return false;
2438
2439 int i;
2440 for (i = 0; i < n; i++)
2441 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2442 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2443 return false;
2444 for ( ; i < len; i++)
2445 switch (GET_CODE (XVECEXP (pat, 0, i)))
2446 {
2447 case CLOBBER:
2448 if (XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2449 return false;
2450 break;
2451 default:
2452 return false;
2453 }
2454 return true;
2455 }
2456
2457 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2458 CLOBBERs), can be split into individual SETs in that order, without
2459 changing semantics. */
2460 static bool
2461 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2462 {
2463 if (!insn_nothrow_p (insn))
2464 return false;
2465
2466 rtx pat = PATTERN (insn);
2467
2468 int i, j;
2469 for (i = 0; i < n; i++)
2470 {
2471 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2472 return false;
2473
2474 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2475
2476 for (j = i + 1; j < n; j++)
2477 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2478 return false;
2479 }
2480
2481 return true;
2482 }
2483
2484 /* Return whether X is just a single_set, with the source
2485 a general_operand. */
2486 static bool
2487 is_just_move (rtx_insn *x)
2488 {
2489 rtx set = single_set (x);
2490 if (!set)
2491 return false;
2492
2493 return general_operand (SET_SRC (set), VOIDmode);
2494 }
2495
2496 /* Callback function to count autoincs. */
2497
2498 static int
2499 count_auto_inc (rtx, rtx, rtx, rtx, rtx, void *arg)
2500 {
2501 (*((int *) arg))++;
2502
2503 return 0;
2504 }
2505
2506 /* Try to combine the insns I0, I1 and I2 into I3.
2507 Here I0, I1 and I2 appear earlier than I3.
2508 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2509 I3.
2510
2511 If we are combining more than two insns and the resulting insn is not
2512 recognized, try splitting it into two insns. If that happens, I2 and I3
2513 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2514 Otherwise, I0, I1 and I2 are pseudo-deleted.
2515
2516 Return 0 if the combination does not work. Then nothing is changed.
2517 If we did the combination, return the insn at which combine should
2518 resume scanning.
2519
2520 Set NEW_DIRECT_JUMP_P to true if try_combine creates a
2521 new direct jump instruction.
2522
2523 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2524 been I3 passed to an earlier try_combine within the same basic
2525 block. */
2526
2527 static rtx_insn *
2528 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2529 bool *new_direct_jump_p, rtx_insn *last_combined_insn)
2530 {
2531 /* New patterns for I3 and I2, respectively. */
2532 rtx newpat, newi2pat = 0;
2533 rtvec newpat_vec_with_clobbers = 0;
2534 bool substed_i2 = false, substed_i1 = false, substed_i0 = false;
2535 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2536 dead. */
2537 bool added_sets_0, added_sets_1, added_sets_2;
2538 /* Total number of SETs to put into I3. */
2539 int total_sets;
2540 /* Nonzero if I2's or I1's body now appears in I3. */
2541 int i2_is_used = 0, i1_is_used = 0;
2542 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2543 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2544 /* Contains I3 if the destination of I3 is used in its source, which means
2545 that the old life of I3 is being killed. If that usage is placed into
2546 I2 and not in I3, a REG_DEAD note must be made. */
2547 rtx i3dest_killed = 0;
2548 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2549 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2550 /* Copy of SET_SRC of I1 and I0, if needed. */
2551 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2552 /* Set if I2DEST was reused as a scratch register. */
2553 bool i2scratch = false;
2554 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2555 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2556 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2557 bool i2dest_in_i2src = false, i1dest_in_i1src = false;
2558 bool i2dest_in_i1src = false, i0dest_in_i0src = false;
2559 bool i1dest_in_i0src = false, i2dest_in_i0src = false;;
2560 bool i2dest_killed = false, i1dest_killed = false, i0dest_killed = false;
2561 bool i1_feeds_i2_n = false, i0_feeds_i2_n = false, i0_feeds_i1_n = false;
2562 /* Notes that must be added to REG_NOTES in I3 and I2. */
2563 rtx new_i3_notes, new_i2_notes;
2564 /* Notes that we substituted I3 into I2 instead of the normal case. */
2565 bool i3_subst_into_i2 = false;
2566 /* Notes that I1, I2 or I3 is a MULT operation. */
2567 bool have_mult = false;
2568 bool swap_i2i3 = false;
2569 bool split_i2i3 = false;
2570 bool changed_i3_dest = false;
2571 bool i2_was_move = false, i3_was_move = false;
2572 int n_auto_inc = 0;
2573
2574 int maxreg;
2575 rtx_insn *temp_insn;
2576 rtx temp_expr;
2577 struct insn_link *link;
2578 rtx other_pat = 0;
2579 rtx new_other_notes;
2580 int i;
2581 scalar_int_mode dest_mode, temp_mode;
2582 bool has_non_call_exception = false;
2583
2584 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2585 never be). */
2586 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2587 return 0;
2588
2589 /* Only try four-insn combinations when there's high likelihood of
2590 success. Look for simple insns, such as loads of constants or
2591 binary operations involving a constant. */
2592 if (i0)
2593 {
2594 int i;
2595 int ngood = 0;
2596 int nshift = 0;
2597 rtx set0, set3;
2598
2599 if (!flag_expensive_optimizations)
2600 return 0;
2601
2602 for (i = 0; i < 4; i++)
2603 {
2604 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2605 rtx set = single_set (insn);
2606 rtx src;
2607 if (!set)
2608 continue;
2609 src = SET_SRC (set);
2610 if (CONSTANT_P (src))
2611 {
2612 ngood += 2;
2613 break;
2614 }
2615 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2616 ngood++;
2617 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2618 || GET_CODE (src) == LSHIFTRT)
2619 nshift++;
2620 }
2621
2622 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2623 are likely manipulating its value. Ideally we'll be able to combine
2624 all four insns into a bitfield insertion of some kind.
2625
2626 Note the source in I0 might be inside a sign/zero extension and the
2627 memory modes in I0 and I3 might be different. So extract the address
2628 from the destination of I3 and search for it in the source of I0.
2629
2630 In the event that there's a match but the source/dest do not actually
2631 refer to the same memory, the worst that happens is we try some
2632 combinations that we wouldn't have otherwise. */
2633 if ((set0 = single_set (i0))
2634 /* Ensure the source of SET0 is a MEM, possibly buried inside
2635 an extension. */
2636 && (GET_CODE (SET_SRC (set0)) == MEM
2637 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2638 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2639 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2640 && (set3 = single_set (i3))
2641 /* Ensure the destination of SET3 is a MEM. */
2642 && GET_CODE (SET_DEST (set3)) == MEM
2643 /* Would it be better to extract the base address for the MEM
2644 in SET3 and look for that? I don't have cases where it matters
2645 but I could envision such cases. */
2646 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2647 ngood += 2;
2648
2649 if (ngood < 2 && nshift < 2)
2650 return 0;
2651 }
2652
2653 /* Exit early if one of the insns involved can't be used for
2654 combinations. */
2655 if (CALL_P (i2)
2656 || (i1 && CALL_P (i1))
2657 || (i0 && CALL_P (i0))
2658 || cant_combine_insn_p (i3)
2659 || cant_combine_insn_p (i2)
2660 || (i1 && cant_combine_insn_p (i1))
2661 || (i0 && cant_combine_insn_p (i0))
2662 || likely_spilled_retval_p (i3))
2663 return 0;
2664
2665 combine_attempts++;
2666 undobuf.other_insn = 0;
2667
2668 /* Reset the hard register usage information. */
2669 CLEAR_HARD_REG_SET (newpat_used_regs);
2670
2671 if (dump_file && (dump_flags & TDF_DETAILS))
2672 {
2673 if (i0)
2674 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2675 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2676 else if (i1)
2677 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2678 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2679 else
2680 fprintf (dump_file, "\nTrying %d -> %d:\n",
2681 INSN_UID (i2), INSN_UID (i3));
2682
2683 if (i0)
2684 dump_insn_slim (dump_file, i0);
2685 if (i1)
2686 dump_insn_slim (dump_file, i1);
2687 dump_insn_slim (dump_file, i2);
2688 dump_insn_slim (dump_file, i3);
2689 }
2690
2691 /* If multiple insns feed into one of I2 or I3, they can be in any
2692 order. To simplify the code below, reorder them in sequence. */
2693 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2694 std::swap (i0, i2);
2695 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2696 std::swap (i0, i1);
2697 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2698 std::swap (i1, i2);
2699
2700 added_links_insn = 0;
2701 added_notes_insn = 0;
2702
2703 /* First check for one important special case that the code below will
2704 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2705 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2706 we may be able to replace that destination with the destination of I3.
2707 This occurs in the common code where we compute both a quotient and
2708 remainder into a structure, in which case we want to do the computation
2709 directly into the structure to avoid register-register copies.
2710
2711 Note that this case handles both multiple sets in I2 and also cases
2712 where I2 has a number of CLOBBERs inside the PARALLEL.
2713
2714 We make very conservative checks below and only try to handle the
2715 most common cases of this. For example, we only handle the case
2716 where I2 and I3 are adjacent to avoid making difficult register
2717 usage tests. */
2718
2719 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2720 && REG_P (SET_SRC (PATTERN (i3)))
2721 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2722 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2723 && GET_CODE (PATTERN (i2)) == PARALLEL
2724 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2725 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2726 below would need to check what is inside (and reg_overlap_mentioned_p
2727 doesn't support those codes anyway). Don't allow those destinations;
2728 the resulting insn isn't likely to be recognized anyway. */
2729 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2730 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2731 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2732 SET_DEST (PATTERN (i3)))
2733 && next_active_insn (i2) == i3)
2734 {
2735 rtx p2 = PATTERN (i2);
2736
2737 /* Make sure that the destination of I3,
2738 which we are going to substitute into one output of I2,
2739 is not used within another output of I2. We must avoid making this:
2740 (parallel [(set (mem (reg 69)) ...)
2741 (set (reg 69) ...)])
2742 which is not well-defined as to order of actions.
2743 (Besides, reload can't handle output reloads for this.)
2744
2745 The problem can also happen if the dest of I3 is a memory ref,
2746 if another dest in I2 is an indirect memory ref.
2747
2748 Neither can this PARALLEL be an asm. We do not allow combining
2749 that usually (see can_combine_p), so do not here either. */
2750 bool ok = true;
2751 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2752 {
2753 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2754 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2755 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2756 SET_DEST (XVECEXP (p2, 0, i))))
2757 ok = false;
2758 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2759 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2760 ok = false;
2761 }
2762
2763 if (ok)
2764 for (i = 0; i < XVECLEN (p2, 0); i++)
2765 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2766 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2767 {
2768 combine_merges++;
2769
2770 subst_insn = i3;
2771 subst_low_luid = DF_INSN_LUID (i2);
2772
2773 added_sets_2 = added_sets_1 = added_sets_0 = false;
2774 i2src = SET_SRC (XVECEXP (p2, 0, i));
2775 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2776 i2dest_killed = dead_or_set_p (i2, i2dest);
2777
2778 /* Replace the dest in I2 with our dest and make the resulting
2779 insn the new pattern for I3. Then skip to where we validate
2780 the pattern. Everything was set up above. */
2781 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2782 newpat = p2;
2783 i3_subst_into_i2 = true;
2784 goto validate_replacement;
2785 }
2786 }
2787
2788 /* If I2 is setting a pseudo to a constant and I3 is setting some
2789 sub-part of it to another constant, merge them by making a new
2790 constant. */
2791 if (i1 == 0
2792 && (temp_expr = single_set (i2)) != 0
2793 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2794 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2795 && GET_CODE (PATTERN (i3)) == SET
2796 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2797 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2798 {
2799 rtx dest = SET_DEST (PATTERN (i3));
2800 rtx temp_dest = SET_DEST (temp_expr);
2801 int offset = -1;
2802 int width = 0;
2803
2804 if (GET_CODE (dest) == ZERO_EXTRACT)
2805 {
2806 if (CONST_INT_P (XEXP (dest, 1))
2807 && CONST_INT_P (XEXP (dest, 2))
2808 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2809 &dest_mode))
2810 {
2811 width = INTVAL (XEXP (dest, 1));
2812 offset = INTVAL (XEXP (dest, 2));
2813 dest = XEXP (dest, 0);
2814 if (BITS_BIG_ENDIAN)
2815 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2816 }
2817 }
2818 else
2819 {
2820 if (GET_CODE (dest) == STRICT_LOW_PART)
2821 dest = XEXP (dest, 0);
2822 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2823 {
2824 width = GET_MODE_PRECISION (dest_mode);
2825 offset = 0;
2826 }
2827 }
2828
2829 if (offset >= 0)
2830 {
2831 /* If this is the low part, we're done. */
2832 if (subreg_lowpart_p (dest))
2833 ;
2834 /* Handle the case where inner is twice the size of outer. */
2835 else if (GET_MODE_PRECISION (temp_mode)
2836 == 2 * GET_MODE_PRECISION (dest_mode))
2837 offset += GET_MODE_PRECISION (dest_mode);
2838 /* Otherwise give up for now. */
2839 else
2840 offset = -1;
2841 }
2842
2843 if (offset >= 0)
2844 {
2845 rtx inner = SET_SRC (PATTERN (i3));
2846 rtx outer = SET_SRC (temp_expr);
2847
2848 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2849 rtx_mode_t (inner, dest_mode),
2850 offset, width);
2851
2852 combine_merges++;
2853 subst_insn = i3;
2854 subst_low_luid = DF_INSN_LUID (i2);
2855 added_sets_2 = added_sets_1 = added_sets_0 = false;
2856 i2dest = temp_dest;
2857 i2dest_killed = dead_or_set_p (i2, i2dest);
2858
2859 /* Replace the source in I2 with the new constant and make the
2860 resulting insn the new pattern for I3. Then skip to where we
2861 validate the pattern. Everything was set up above. */
2862 SUBST (SET_SRC (temp_expr),
2863 immed_wide_int_const (o, temp_mode));
2864
2865 newpat = PATTERN (i2);
2866
2867 /* The dest of I3 has been replaced with the dest of I2. */
2868 changed_i3_dest = true;
2869 goto validate_replacement;
2870 }
2871 }
2872
2873 /* If we have no I1 and I2 looks like:
2874 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2875 (set Y OP)])
2876 make up a dummy I1 that is
2877 (set Y OP)
2878 and change I2 to be
2879 (set (reg:CC X) (compare:CC Y (const_int 0)))
2880
2881 (We can ignore any trailing CLOBBERs.)
2882
2883 This undoes a previous combination and allows us to match a branch-and-
2884 decrement insn. */
2885
2886 if (i1 == 0
2887 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2888 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2889 == MODE_CC)
2890 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2891 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2892 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2893 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2894 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2895 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2896 {
2897 /* We make I1 with the same INSN_UID as I2. This gives it
2898 the same DF_INSN_LUID for value tracking. Our fake I1 will
2899 never appear in the insn stream so giving it the same INSN_UID
2900 as I2 will not cause a problem. */
2901
2902 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2903 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2904 -1, NULL_RTX);
2905 INSN_UID (i1) = INSN_UID (i2);
2906
2907 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2908 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2909 SET_DEST (PATTERN (i1)));
2910 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2911 SUBST_LINK (LOG_LINKS (i2),
2912 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2913 }
2914
2915 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2916 make those two SETs separate I1 and I2 insns, and make an I0 that is
2917 the original I1. */
2918 if (i0 == 0
2919 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2920 && can_split_parallel_of_n_reg_sets (i2, 2)
2921 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2922 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
2923 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2924 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2925 {
2926 /* If there is no I1, there is no I0 either. */
2927 i0 = i1;
2928
2929 /* We make I1 with the same INSN_UID as I2. This gives it
2930 the same DF_INSN_LUID for value tracking. Our fake I1 will
2931 never appear in the insn stream so giving it the same INSN_UID
2932 as I2 will not cause a problem. */
2933
2934 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2935 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2936 -1, NULL_RTX);
2937 INSN_UID (i1) = INSN_UID (i2);
2938
2939 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2940 }
2941
2942 /* Verify that I2 and maybe I1 and I0 can be combined into I3. */
2943 if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
2944 {
2945 if (dump_file && (dump_flags & TDF_DETAILS))
2946 fprintf (dump_file, "Can't combine i2 into i3\n");
2947 undo_all ();
2948 return 0;
2949 }
2950 if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
2951 {
2952 if (dump_file && (dump_flags & TDF_DETAILS))
2953 fprintf (dump_file, "Can't combine i1 into i3\n");
2954 undo_all ();
2955 return 0;
2956 }
2957 if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
2958 {
2959 if (dump_file && (dump_flags & TDF_DETAILS))
2960 fprintf (dump_file, "Can't combine i0 into i3\n");
2961 undo_all ();
2962 return 0;
2963 }
2964
2965 /* With non-call exceptions we can end up trying to combine multiple
2966 insns with possible EH side effects. Make sure we can combine
2967 that to a single insn which means there must be at most one insn
2968 in the combination with an EH side effect. */
2969 if (cfun->can_throw_non_call_exceptions)
2970 {
2971 if (find_reg_note (i3, REG_EH_REGION, NULL_RTX)
2972 || find_reg_note (i2, REG_EH_REGION, NULL_RTX)
2973 || (i1 && find_reg_note (i1, REG_EH_REGION, NULL_RTX))
2974 || (i0 && find_reg_note (i0, REG_EH_REGION, NULL_RTX)))
2975 {
2976 has_non_call_exception = true;
2977 if (insn_could_throw_p (i3)
2978 + insn_could_throw_p (i2)
2979 + (i1 ? insn_could_throw_p (i1) : 0)
2980 + (i0 ? insn_could_throw_p (i0) : 0) > 1)
2981 {
2982 if (dump_file && (dump_flags & TDF_DETAILS))
2983 fprintf (dump_file, "Can't combine multiple insns with EH "
2984 "side-effects\n");
2985 undo_all ();
2986 return 0;
2987 }
2988 }
2989 }
2990
2991 /* Record whether i2 and i3 are trivial moves. */
2992 i2_was_move = is_just_move (i2);
2993 i3_was_move = is_just_move (i3);
2994
2995 /* Record whether I2DEST is used in I2SRC and similarly for the other
2996 cases. Knowing this will help in register status updating below. */
2997 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2998 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2999 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3000 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3001 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3002 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3003 i2dest_killed = dead_or_set_p (i2, i2dest);
3004 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3005 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3006
3007 /* For the earlier insns, determine which of the subsequent ones they
3008 feed. */
3009 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3010 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3011 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3012 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3013 && reg_overlap_mentioned_p (i0dest, i2src))));
3014
3015 /* Ensure that I3's pattern can be the destination of combines. */
3016 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3017 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3018 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3019 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3020 &i3dest_killed))
3021 {
3022 undo_all ();
3023 return 0;
3024 }
3025
3026 /* See if any of the insns is a MULT operation. Unless one is, we will
3027 reject a combination that is, since it must be slower. Be conservative
3028 here. */
3029 if (GET_CODE (i2src) == MULT
3030 || (i1 != 0 && GET_CODE (i1src) == MULT)
3031 || (i0 != 0 && GET_CODE (i0src) == MULT)
3032 || (GET_CODE (PATTERN (i3)) == SET
3033 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3034 have_mult = true;
3035
3036 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3037 We used to do this EXCEPT in one case: I3 has a post-inc in an
3038 output operand. However, that exception can give rise to insns like
3039 mov r3,(r3)+
3040 which is a famous insn on the PDP-11 where the value of r3 used as the
3041 source was model-dependent. Avoid this sort of thing. */
3042
3043 #if 0
3044 if (!(GET_CODE (PATTERN (i3)) == SET
3045 && REG_P (SET_SRC (PATTERN (i3)))
3046 && MEM_P (SET_DEST (PATTERN (i3)))
3047 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3048 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3049 /* It's not the exception. */
3050 #endif
3051 if (AUTO_INC_DEC)
3052 {
3053 rtx link;
3054 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3055 if (REG_NOTE_KIND (link) == REG_INC
3056 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3057 || (i1 != 0
3058 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3059 {
3060 undo_all ();
3061 return 0;
3062 }
3063 }
3064
3065 /* See if the SETs in I1 or I2 need to be kept around in the merged
3066 instruction: whenever the value set there is still needed past I3.
3067 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3068
3069 For the SET in I1, we have two cases: if I1 and I2 independently feed
3070 into I3, the set in I1 needs to be kept around unless I1DEST dies
3071 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3072 in I1 needs to be kept around unless I1DEST dies or is set in either
3073 I2 or I3. The same considerations apply to I0. */
3074
3075 added_sets_2 = !dead_or_set_p (i3, i2dest);
3076
3077 if (i1)
3078 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3079 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3080 else
3081 added_sets_1 = false;
3082
3083 if (i0)
3084 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3085 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3086 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3087 && dead_or_set_p (i2, i0dest)));
3088 else
3089 added_sets_0 = false;
3090
3091 /* We are about to copy insns for the case where they need to be kept
3092 around. Check that they can be copied in the merged instruction. */
3093
3094 if (targetm.cannot_copy_insn_p
3095 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3096 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3097 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3098 {
3099 undo_all ();
3100 return 0;
3101 }
3102
3103 /* We cannot safely duplicate volatile references in any case. */
3104
3105 if ((added_sets_2 && volatile_refs_p (PATTERN (i2)))
3106 || (added_sets_1 && volatile_refs_p (PATTERN (i1)))
3107 || (added_sets_0 && volatile_refs_p (PATTERN (i0))))
3108 {
3109 undo_all ();
3110 return 0;
3111 }
3112
3113 /* Count how many auto_inc expressions there were in the original insns;
3114 we need to have the same number in the resulting patterns. */
3115
3116 if (i0)
3117 for_each_inc_dec (PATTERN (i0), count_auto_inc, &n_auto_inc);
3118 if (i1)
3119 for_each_inc_dec (PATTERN (i1), count_auto_inc, &n_auto_inc);
3120 for_each_inc_dec (PATTERN (i2), count_auto_inc, &n_auto_inc);
3121 for_each_inc_dec (PATTERN (i3), count_auto_inc, &n_auto_inc);
3122
3123 /* If the set in I2 needs to be kept around, we must make a copy of
3124 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3125 PATTERN (I2), we are only substituting for the original I1DEST, not into
3126 an already-substituted copy. This also prevents making self-referential
3127 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3128 I2DEST. */
3129
3130 if (added_sets_2)
3131 {
3132 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3133 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3134 else
3135 i2pat = copy_rtx (PATTERN (i2));
3136 }
3137
3138 if (added_sets_1)
3139 {
3140 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3141 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3142 else
3143 i1pat = copy_rtx (PATTERN (i1));
3144 }
3145
3146 if (added_sets_0)
3147 {
3148 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3149 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3150 else
3151 i0pat = copy_rtx (PATTERN (i0));
3152 }
3153
3154 combine_merges++;
3155
3156 /* Substitute in the latest insn for the regs set by the earlier ones. */
3157
3158 maxreg = max_reg_num ();
3159
3160 subst_insn = i3;
3161
3162 /* Many machines have insns that can both perform an
3163 arithmetic operation and set the condition code. These operations will
3164 be represented as a PARALLEL with the first element of the vector
3165 being a COMPARE of an arithmetic operation with the constant zero.
3166 The second element of the vector will set some pseudo to the result
3167 of the same arithmetic operation. If we simplify the COMPARE, we won't
3168 match such a pattern and so will generate an extra insn. Here we test
3169 for this case, where both the comparison and the operation result are
3170 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3171 I2SRC. Later we will make the PARALLEL that contains I2. */
3172
3173 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3174 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3175 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3176 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3177 {
3178 rtx newpat_dest;
3179 rtx *cc_use_loc = NULL;
3180 rtx_insn *cc_use_insn = NULL;
3181 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3182 machine_mode compare_mode, orig_compare_mode;
3183 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3184 scalar_int_mode mode;
3185
3186 newpat = PATTERN (i3);
3187 newpat_dest = SET_DEST (newpat);
3188 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3189
3190 if (undobuf.other_insn == 0
3191 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3192 &cc_use_insn)))
3193 {
3194 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3195 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3196 compare_code = simplify_compare_const (compare_code, mode,
3197 &op0, &op1);
3198 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3199 }
3200
3201 /* Do the rest only if op1 is const0_rtx, which may be the
3202 result of simplification. */
3203 if (op1 == const0_rtx)
3204 {
3205 /* If a single use of the CC is found, prepare to modify it
3206 when SELECT_CC_MODE returns a new CC-class mode, or when
3207 the above simplify_compare_const() returned a new comparison
3208 operator. undobuf.other_insn is assigned the CC use insn
3209 when modifying it. */
3210 if (cc_use_loc)
3211 {
3212 #ifdef SELECT_CC_MODE
3213 machine_mode new_mode
3214 = SELECT_CC_MODE (compare_code, op0, op1);
3215 if (new_mode != orig_compare_mode
3216 && can_change_dest_mode (SET_DEST (newpat),
3217 added_sets_2, new_mode))
3218 {
3219 unsigned int regno = REGNO (newpat_dest);
3220 compare_mode = new_mode;
3221 if (regno < FIRST_PSEUDO_REGISTER)
3222 newpat_dest = gen_rtx_REG (compare_mode, regno);
3223 else
3224 {
3225 subst_mode (regno, compare_mode);
3226 newpat_dest = regno_reg_rtx[regno];
3227 }
3228 }
3229 #endif
3230 /* Cases for modifying the CC-using comparison. */
3231 if (compare_code != orig_compare_code
3232 && COMPARISON_P (*cc_use_loc))
3233 {
3234 /* Replace cc_use_loc with entire new RTX. */
3235 SUBST (*cc_use_loc,
3236 gen_rtx_fmt_ee (compare_code, GET_MODE (*cc_use_loc),
3237 newpat_dest, const0_rtx));
3238 undobuf.other_insn = cc_use_insn;
3239 }
3240 else if (compare_mode != orig_compare_mode)
3241 {
3242 subrtx_ptr_iterator::array_type array;
3243
3244 /* Just replace the CC reg with a new mode. */
3245 FOR_EACH_SUBRTX_PTR (iter, array, cc_use_loc, NONCONST)
3246 {
3247 rtx *loc = *iter;
3248 if (REG_P (*loc)
3249 && REGNO (*loc) == REGNO (newpat_dest))
3250 {
3251 SUBST (*loc, newpat_dest);
3252 iter.skip_subrtxes ();
3253 }
3254 }
3255 undobuf.other_insn = cc_use_insn;
3256 }
3257 }
3258
3259 /* Now we modify the current newpat:
3260 First, SET_DEST(newpat) is updated if the CC mode has been
3261 altered. For targets without SELECT_CC_MODE, this should be
3262 optimized away. */
3263 if (compare_mode != orig_compare_mode)
3264 SUBST (SET_DEST (newpat), newpat_dest);
3265 /* This is always done to propagate i2src into newpat. */
3266 SUBST (SET_SRC (newpat),
3267 gen_rtx_COMPARE (compare_mode, op0, op1));
3268 /* Create new version of i2pat if needed; the below PARALLEL
3269 creation needs this to work correctly. */
3270 if (! rtx_equal_p (i2src, op0))
3271 i2pat = gen_rtx_SET (i2dest, op0);
3272 i2_is_used = 1;
3273 }
3274 }
3275
3276 if (i2_is_used == 0)
3277 {
3278 /* It is possible that the source of I2 or I1 may be performing
3279 an unneeded operation, such as a ZERO_EXTEND of something
3280 that is known to have the high part zero. Handle that case
3281 by letting subst look at the inner insns.
3282
3283 Another way to do this would be to have a function that tries
3284 to simplify a single insn instead of merging two or more
3285 insns. We don't do this because of the potential of infinite
3286 loops and because of the potential extra memory required.
3287 However, doing it the way we are is a bit of a kludge and
3288 doesn't catch all cases.
3289
3290 But only do this if -fexpensive-optimizations since it slows
3291 things down and doesn't usually win.
3292
3293 This is not done in the COMPARE case above because the
3294 unmodified I2PAT is used in the PARALLEL and so a pattern
3295 with a modified I2SRC would not match. */
3296
3297 if (flag_expensive_optimizations)
3298 {
3299 /* Pass pc_rtx so no substitutions are done, just
3300 simplifications. */
3301 if (i1)
3302 {
3303 subst_low_luid = DF_INSN_LUID (i1);
3304 i1src = subst (i1src, pc_rtx, pc_rtx, false, false, false);
3305 }
3306
3307 subst_low_luid = DF_INSN_LUID (i2);
3308 i2src = subst (i2src, pc_rtx, pc_rtx, false, false, false);
3309 }
3310
3311 n_occurrences = 0; /* `subst' counts here */
3312 subst_low_luid = DF_INSN_LUID (i2);
3313
3314 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3315 copy of I2SRC each time we substitute it, in order to avoid creating
3316 self-referential RTL when we will be substituting I1SRC for I1DEST
3317 later. Likewise if I0 feeds into I2, either directly or indirectly
3318 through I1, and I0DEST is in I0SRC. */
3319 newpat = subst (PATTERN (i3), i2dest, i2src, false, false,
3320 (i1_feeds_i2_n && i1dest_in_i1src)
3321 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3322 && i0dest_in_i0src));
3323 substed_i2 = true;
3324
3325 /* Record whether I2's body now appears within I3's body. */
3326 i2_is_used = n_occurrences;
3327 }
3328
3329 /* If we already got a failure, don't try to do more. Otherwise, try to
3330 substitute I1 if we have it. */
3331
3332 if (i1 && GET_CODE (newpat) != CLOBBER)
3333 {
3334 /* Before we can do this substitution, we must redo the test done
3335 above (see detailed comments there) that ensures I1DEST isn't
3336 mentioned in any SETs in NEWPAT that are field assignments. */
3337 if (!combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3338 false, false, 0))
3339 {
3340 undo_all ();
3341 return 0;
3342 }
3343
3344 n_occurrences = 0;
3345 subst_low_luid = DF_INSN_LUID (i1);
3346
3347 /* If the following substitution will modify I1SRC, make a copy of it
3348 for the case where it is substituted for I1DEST in I2PAT later. */
3349 if (added_sets_2 && i1_feeds_i2_n)
3350 i1src_copy = copy_rtx (i1src);
3351
3352 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3353 copy of I1SRC each time we substitute it, in order to avoid creating
3354 self-referential RTL when we will be substituting I0SRC for I0DEST
3355 later. */
3356 newpat = subst (newpat, i1dest, i1src, false, false,
3357 i0_feeds_i1_n && i0dest_in_i0src);
3358 substed_i1 = true;
3359
3360 /* Record whether I1's body now appears within I3's body. */
3361 i1_is_used = n_occurrences;
3362 }
3363
3364 /* Likewise for I0 if we have it. */
3365
3366 if (i0 && GET_CODE (newpat) != CLOBBER)
3367 {
3368 if (!combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3369 false, false, 0))
3370 {
3371 undo_all ();
3372 return 0;
3373 }
3374
3375 /* If the following substitution will modify I0SRC, make a copy of it
3376 for the case where it is substituted for I0DEST in I1PAT later. */
3377 if (added_sets_1 && i0_feeds_i1_n)
3378 i0src_copy = copy_rtx (i0src);
3379 /* And a copy for I0DEST in I2PAT substitution. */
3380 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3381 || (i0_feeds_i2_n)))
3382 i0src_copy2 = copy_rtx (i0src);
3383
3384 n_occurrences = 0;
3385 subst_low_luid = DF_INSN_LUID (i0);
3386 newpat = subst (newpat, i0dest, i0src, false, false, false);
3387 substed_i0 = true;
3388 }
3389
3390 if (n_auto_inc)
3391 {
3392 int new_n_auto_inc = 0;
3393 for_each_inc_dec (newpat, count_auto_inc, &new_n_auto_inc);
3394
3395 if (n_auto_inc != new_n_auto_inc)
3396 {
3397 if (dump_file && (dump_flags & TDF_DETAILS))
3398 fprintf (dump_file, "Number of auto_inc expressions changed\n");
3399 undo_all ();
3400 return 0;
3401 }
3402 }
3403
3404 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3405 to count all the ways that I2SRC and I1SRC can be used. */
3406 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3407 && i2_is_used + added_sets_2 > 1)
3408 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3409 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n) > 1))
3410 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3411 && (n_occurrences + added_sets_0
3412 + (added_sets_1 && i0_feeds_i1_n)
3413 + (added_sets_2 && i0_feeds_i2_n) > 1))
3414 /* Fail if we tried to make a new register. */
3415 || max_reg_num () != maxreg
3416 /* Fail if we couldn't do something and have a CLOBBER. */
3417 || GET_CODE (newpat) == CLOBBER
3418 /* Fail if this new pattern is a MULT and we didn't have one before
3419 at the outer level. */
3420 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3421 && ! have_mult))
3422 {
3423 undo_all ();
3424 return 0;
3425 }
3426
3427 /* If the actions of the earlier insns must be kept
3428 in addition to substituting them into the latest one,
3429 we must make a new PARALLEL for the latest insn
3430 to hold additional the SETs. */
3431
3432 if (added_sets_0 || added_sets_1 || added_sets_2)
3433 {
3434 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3435 combine_extras++;
3436
3437 if (GET_CODE (newpat) == PARALLEL)
3438 {
3439 rtvec old = XVEC (newpat, 0);
3440 total_sets = XVECLEN (newpat, 0) + extra_sets;
3441 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3442 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3443 sizeof (old->elem[0]) * old->num_elem);
3444 }
3445 else
3446 {
3447 rtx old = newpat;
3448 total_sets = 1 + extra_sets;
3449 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3450 XVECEXP (newpat, 0, 0) = old;
3451 }
3452
3453 if (added_sets_0)
3454 XVECEXP (newpat, 0, --total_sets) = i0pat;
3455
3456 if (added_sets_1)
3457 {
3458 rtx t = i1pat;
3459 if (i0_feeds_i1_n)
3460 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src,
3461 false, false, false);
3462
3463 XVECEXP (newpat, 0, --total_sets) = t;
3464 }
3465 if (added_sets_2)
3466 {
3467 rtx t = i2pat;
3468 if (i1_feeds_i2_n)
3469 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, false, false,
3470 i0_feeds_i1_n && i0dest_in_i0src);
3471 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3472 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src,
3473 false, false, false);
3474
3475 XVECEXP (newpat, 0, --total_sets) = t;
3476 }
3477 }
3478
3479 validate_replacement:
3480
3481 /* Note which hard regs this insn has as inputs. */
3482 mark_used_regs_combine (newpat);
3483
3484 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3485 consider splitting this pattern, we might need these clobbers. */
3486 if (i1 && GET_CODE (newpat) == PARALLEL
3487 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3488 {
3489 int len = XVECLEN (newpat, 0);
3490
3491 newpat_vec_with_clobbers = rtvec_alloc (len);
3492 for (i = 0; i < len; i++)
3493 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3494 }
3495
3496 /* We have recognized nothing yet. */
3497 insn_code_number = -1;
3498
3499 /* See if this is a PARALLEL of two SETs where one SET's destination is
3500 a register that is unused and this isn't marked as an instruction that
3501 might trap in an EH region. In that case, we just need the other SET.
3502 We prefer this over the PARALLEL.
3503
3504 This can occur when simplifying a divmod insn. We *must* test for this
3505 case here because the code below that splits two independent SETs doesn't
3506 handle this case correctly when it updates the register status.
3507
3508 It's pointless doing this if we originally had two sets, one from
3509 i3, and one from i2. Combining then splitting the parallel results
3510 in the original i2 again plus an invalid insn (which we delete).
3511 The net effect is only to move instructions around, which makes
3512 debug info less accurate.
3513
3514 If the remaining SET came from I2 its destination should not be used
3515 between I2 and I3. See PR82024. */
3516
3517 if (!(added_sets_2 && i1 == 0)
3518 && is_parallel_of_n_reg_sets (newpat, 2)
3519 && asm_noperands (newpat) < 0)
3520 {
3521 rtx set0 = XVECEXP (newpat, 0, 0);
3522 rtx set1 = XVECEXP (newpat, 0, 1);
3523 rtx oldpat = newpat;
3524
3525 if (((REG_P (SET_DEST (set1))
3526 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3527 || (GET_CODE (SET_DEST (set1)) == SUBREG
3528 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3529 && insn_nothrow_p (i3)
3530 && !side_effects_p (SET_SRC (set1)))
3531 {
3532 newpat = set0;
3533 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3534 }
3535
3536 else if (((REG_P (SET_DEST (set0))
3537 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3538 || (GET_CODE (SET_DEST (set0)) == SUBREG
3539 && find_reg_note (i3, REG_UNUSED,
3540 SUBREG_REG (SET_DEST (set0)))))
3541 && insn_nothrow_p (i3)
3542 && !side_effects_p (SET_SRC (set0)))
3543 {
3544 rtx dest = SET_DEST (set1);
3545 if (GET_CODE (dest) == SUBREG)
3546 dest = SUBREG_REG (dest);
3547 if (!reg_used_between_p (dest, i2, i3))
3548 {
3549 newpat = set1;
3550 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3551
3552 if (insn_code_number >= 0)
3553 changed_i3_dest = true;
3554 }
3555 }
3556
3557 if (insn_code_number < 0)
3558 newpat = oldpat;
3559 }
3560
3561 /* Is the result of combination a valid instruction? */
3562 if (insn_code_number < 0)
3563 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3564
3565 /* If we were combining three insns and the result is a simple SET
3566 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3567 insns. There are two ways to do this. It can be split using a
3568 machine-specific method (like when you have an addition of a large
3569 constant) or by combine in the function find_split_point. */
3570
3571 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3572 && asm_noperands (newpat) < 0)
3573 {
3574 rtx parallel, *split;
3575 rtx_insn *m_split_insn;
3576 unsigned int old_nregs, new_nregs;
3577
3578 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3579 use I2DEST as a scratch register will help. In the latter case,
3580 convert I2DEST to the mode of the source of NEWPAT if we can. */
3581
3582 m_split_insn = combine_split_insns (newpat, i3, &old_nregs, &new_nregs);
3583
3584 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3585 inputs of NEWPAT. */
3586
3587 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3588 possible to try that as a scratch reg. This would require adding
3589 more code to make it work though. */
3590
3591 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3592 {
3593 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3594
3595 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3596 (temporarily, until we are committed to this instruction
3597 combination) does not work: for example, any call to nonzero_bits
3598 on the register (from a splitter in the MD file, for example)
3599 will get the old information, which is invalid.
3600
3601 Since nowadays we can create registers during combine just fine,
3602 we should just create a new one here, not reuse i2dest. */
3603
3604 /* First try to split using the original register as a
3605 scratch register. */
3606 parallel = gen_rtx_PARALLEL (VOIDmode,
3607 gen_rtvec (2, newpat,
3608 gen_rtx_CLOBBER (VOIDmode,
3609 i2dest)));
3610 m_split_insn = combine_split_insns (parallel, i3, &old_nregs, &new_nregs);
3611
3612 /* If that didn't work, try changing the mode of I2DEST if
3613 we can. */
3614 if (m_split_insn == 0
3615 && new_mode != GET_MODE (i2dest)
3616 && new_mode != VOIDmode
3617 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3618 {
3619 machine_mode old_mode = GET_MODE (i2dest);
3620 rtx ni2dest;
3621
3622 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3623 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3624 else
3625 {
3626 subst_mode (REGNO (i2dest), new_mode);
3627 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3628 }
3629
3630 parallel = (gen_rtx_PARALLEL
3631 (VOIDmode,
3632 gen_rtvec (2, newpat,
3633 gen_rtx_CLOBBER (VOIDmode,
3634 ni2dest))));
3635 m_split_insn = combine_split_insns (parallel, i3, &old_nregs, &new_nregs);
3636
3637 if (m_split_insn == 0
3638 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3639 {
3640 struct undo *buf;
3641
3642 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3643 buf = undobuf.undos;
3644 undobuf.undos = buf->next;
3645 buf->next = undobuf.frees;
3646 undobuf.frees = buf;
3647 }
3648 }
3649
3650 i2scratch = m_split_insn != 0;
3651 }
3652
3653 /* If recog_for_combine has discarded clobbers, try to use them
3654 again for the split. */
3655 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3656 {
3657 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3658 m_split_insn = combine_split_insns (parallel, i3, &old_nregs, &new_nregs);
3659 }
3660
3661 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3662 {
3663 rtx m_split_pat = PATTERN (m_split_insn);
3664 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes,
3665 old_nregs, new_nregs);
3666 if (insn_code_number >= 0)
3667 newpat = m_split_pat;
3668 }
3669 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3670 && (next_nonnote_nondebug_insn (i2) == i3
3671 || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3672 {
3673 rtx i2set, i3set;
3674 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3675 newi2pat = PATTERN (m_split_insn);
3676
3677 i3set = single_set (NEXT_INSN (m_split_insn));
3678 i2set = single_set (m_split_insn);
3679
3680 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3681
3682 /* If I2 or I3 has multiple SETs, we won't know how to track
3683 register status, so don't use these insns. If I2's destination
3684 is used between I2 and I3, we also can't use these insns. */
3685
3686 if (i2_code_number >= 0 && i2set && i3set
3687 && (next_nonnote_nondebug_insn (i2) == i3
3688 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3689 insn_code_number = recog_for_combine (&newi3pat, i3,
3690 &new_i3_notes,
3691 old_nregs, new_nregs);
3692 if (insn_code_number >= 0)
3693 newpat = newi3pat;
3694
3695 /* It is possible that both insns now set the destination of I3.
3696 If so, we must show an extra use of it. */
3697
3698 if (insn_code_number >= 0)
3699 {
3700 rtx new_i3_dest = SET_DEST (i3set);
3701 rtx new_i2_dest = SET_DEST (i2set);
3702
3703 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3704 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3705 || GET_CODE (new_i3_dest) == SUBREG)
3706 new_i3_dest = XEXP (new_i3_dest, 0);
3707
3708 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3709 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3710 || GET_CODE (new_i2_dest) == SUBREG)
3711 new_i2_dest = XEXP (new_i2_dest, 0);
3712
3713 if (REG_P (new_i3_dest)
3714 && REG_P (new_i2_dest)
3715 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3716 && REGNO (new_i2_dest) < reg_n_sets_max)
3717 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3718 }
3719 }
3720
3721 /* If we can split it and use I2DEST, go ahead and see if that
3722 helps things be recognized. Verify that none of the registers
3723 are set between I2 and I3. */
3724 if (insn_code_number < 0
3725 && (split = find_split_point (&newpat, i3, false)) != 0
3726 /* We need I2DEST in the proper mode. If it is a hard register
3727 or the only use of a pseudo, we can change its mode.
3728 Make sure we don't change a hard register to have a mode that
3729 isn't valid for it, or change the number of registers. */
3730 && (GET_MODE (*split) == GET_MODE (i2dest)
3731 || GET_MODE (*split) == VOIDmode
3732 || can_change_dest_mode (i2dest, added_sets_2,
3733 GET_MODE (*split)))
3734 && (next_nonnote_nondebug_insn (i2) == i3
3735 || !modified_between_p (*split, i2, i3))
3736 /* We can't overwrite I2DEST if its value is still used by
3737 NEWPAT. */
3738 && ! reg_referenced_p (i2dest, newpat)
3739 /* We should not split a possibly trapping part when we
3740 care about non-call EH and have REG_EH_REGION notes
3741 to distribute. */
3742 && ! (cfun->can_throw_non_call_exceptions
3743 && has_non_call_exception
3744 && may_trap_p (*split)))
3745 {
3746 rtx newdest = i2dest;
3747 enum rtx_code split_code = GET_CODE (*split);
3748 machine_mode split_mode = GET_MODE (*split);
3749 bool subst_done = false;
3750 newi2pat = NULL_RTX;
3751
3752 i2scratch = true;
3753
3754 /* *SPLIT may be part of I2SRC, so make sure we have the
3755 original expression around for later debug processing.
3756 We should not need I2SRC any more in other cases. */
3757 if (MAY_HAVE_DEBUG_BIND_INSNS)
3758 i2src = copy_rtx (i2src);
3759 else
3760 i2src = NULL;
3761
3762 /* Get NEWDEST as a register in the proper mode. We have already
3763 validated that we can do this. */
3764 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3765 {
3766 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3767 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3768 else
3769 {
3770 subst_mode (REGNO (i2dest), split_mode);
3771 newdest = regno_reg_rtx[REGNO (i2dest)];
3772 }
3773 }
3774
3775 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3776 an ASHIFT. This can occur if it was inside a PLUS and hence
3777 appeared to be a memory address. This is a kludge. */
3778 if (split_code == MULT
3779 && CONST_INT_P (XEXP (*split, 1))
3780 && INTVAL (XEXP (*split, 1)) > 0
3781 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3782 {
3783 rtx i_rtx = gen_int_shift_amount (split_mode, i);
3784 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3785 XEXP (*split, 0), i_rtx));
3786 /* Update split_code because we may not have a multiply
3787 anymore. */
3788 split_code = GET_CODE (*split);
3789 }
3790
3791 /* Similarly for (plus (mult FOO (const_int pow2))). */
3792 if (split_code == PLUS
3793 && GET_CODE (XEXP (*split, 0)) == MULT
3794 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3795 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3796 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3797 {
3798 rtx nsplit = XEXP (*split, 0);
3799 rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3800 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3801 XEXP (nsplit, 0),
3802 i_rtx));
3803 /* Update split_code because we may not have a multiply
3804 anymore. */
3805 split_code = GET_CODE (*split);
3806 }
3807
3808 #ifdef INSN_SCHEDULING
3809 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3810 be written as a ZERO_EXTEND. */
3811 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3812 {
3813 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3814 what it really is. */
3815 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3816 == SIGN_EXTEND)
3817 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3818 SUBREG_REG (*split)));
3819 else
3820 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3821 SUBREG_REG (*split)));
3822 }
3823 #endif
3824
3825 /* Attempt to split binary operators using arithmetic identities. */
3826 if (BINARY_P (SET_SRC (newpat))
3827 && split_mode == GET_MODE (SET_SRC (newpat))
3828 && ! side_effects_p (SET_SRC (newpat)))
3829 {
3830 rtx setsrc = SET_SRC (newpat);
3831 machine_mode mode = GET_MODE (setsrc);
3832 enum rtx_code code = GET_CODE (setsrc);
3833 rtx src_op0 = XEXP (setsrc, 0);
3834 rtx src_op1 = XEXP (setsrc, 1);
3835
3836 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3837 if (rtx_equal_p (src_op0, src_op1))
3838 {
3839 newi2pat = gen_rtx_SET (newdest, src_op0);
3840 SUBST (XEXP (setsrc, 0), newdest);
3841 SUBST (XEXP (setsrc, 1), newdest);
3842 subst_done = true;
3843 }
3844 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3845 else if ((code == PLUS || code == MULT)
3846 && GET_CODE (src_op0) == code
3847 && GET_CODE (XEXP (src_op0, 0)) == code
3848 && (INTEGRAL_MODE_P (mode)
3849 || (FLOAT_MODE_P (mode)
3850 && flag_unsafe_math_optimizations)))
3851 {
3852 rtx p = XEXP (XEXP (src_op0, 0), 0);
3853 rtx q = XEXP (XEXP (src_op0, 0), 1);
3854 rtx r = XEXP (src_op0, 1);
3855 rtx s = src_op1;
3856
3857 /* Split both "((X op Y) op X) op Y" and
3858 "((X op Y) op Y) op X" as "T op T" where T is
3859 "X op Y". */
3860 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3861 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3862 {
3863 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3864 SUBST (XEXP (setsrc, 0), newdest);
3865 SUBST (XEXP (setsrc, 1), newdest);
3866 subst_done = true;
3867 }
3868 /* Split "((X op X) op Y) op Y)" as "T op T" where
3869 T is "X op Y". */
3870 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3871 {
3872 rtx tmp = simplify_gen_binary (code, mode, p, r);
3873 newi2pat = gen_rtx_SET (newdest, tmp);
3874 SUBST (XEXP (setsrc, 0), newdest);
3875 SUBST (XEXP (setsrc, 1), newdest);
3876 subst_done = true;
3877 }
3878 }
3879 }
3880
3881 if (!subst_done)
3882 {
3883 newi2pat = gen_rtx_SET (newdest, *split);
3884 SUBST (*split, newdest);
3885 }
3886
3887 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3888
3889 /* recog_for_combine might have added CLOBBERs to newi2pat.
3890 Make sure NEWPAT does not depend on the clobbered regs. */
3891 if (GET_CODE (newi2pat) == PARALLEL)
3892 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3893 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3894 {
3895 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3896 if (reg_overlap_mentioned_p (reg, newpat))
3897 {
3898 undo_all ();
3899 return 0;
3900 }
3901 }
3902
3903 /* If the split point was a MULT and we didn't have one before,
3904 don't use one now. */
3905 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3906 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3907 }
3908 }
3909
3910 /* Check for a case where we loaded from memory in a narrow mode and
3911 then sign extended it, but we need both registers. In that case,
3912 we have a PARALLEL with both loads from the same memory location.
3913 We can split this into a load from memory followed by a register-register
3914 copy. This saves at least one insn, more if register allocation can
3915 eliminate the copy.
3916
3917 We cannot do this if the involved modes have more than one elements,
3918 like for vector or complex modes.
3919
3920 We cannot do this if the destination of the first assignment is a
3921 condition code register. We eliminate this case by making sure
3922 the SET_DEST and SET_SRC have the same mode.
3923
3924 We cannot do this if the destination of the second assignment is
3925 a register that we have already assumed is zero-extended. Similarly
3926 for a SUBREG of such a register. */
3927
3928 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3929 && GET_CODE (newpat) == PARALLEL
3930 && XVECLEN (newpat, 0) == 2
3931 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3932 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3933 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3934 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3935 && ! VECTOR_MODE_P (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0))))
3936 && ! COMPLEX_MODE_P (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0))))
3937 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3938 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3939 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3940 && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
3941 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3942 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3943 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3944 (REG_P (temp_expr)
3945 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3946 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3947 BITS_PER_WORD)
3948 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3949 HOST_BITS_PER_INT)
3950 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3951 != GET_MODE_MASK (word_mode))))
3952 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3953 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3954 (REG_P (temp_expr)
3955 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3956 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3957 BITS_PER_WORD)
3958 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3959 HOST_BITS_PER_INT)
3960 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3961 != GET_MODE_MASK (word_mode)))))
3962 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3963 SET_SRC (XVECEXP (newpat, 0, 1)))
3964 && ! find_reg_note (i3, REG_UNUSED,
3965 SET_DEST (XVECEXP (newpat, 0, 0))))
3966 {
3967 rtx ni2dest;
3968
3969 newi2pat = XVECEXP (newpat, 0, 0);
3970 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3971 newpat = XVECEXP (newpat, 0, 1);
3972 SUBST (SET_SRC (newpat),
3973 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3974 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3975
3976 if (i2_code_number >= 0)
3977 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3978
3979 if (insn_code_number >= 0)
3980 swap_i2i3 = 1;
3981 }
3982
3983 /* Similarly, check for a case where we have a PARALLEL of two independent
3984 SETs but we started with three insns. In this case, we can do the sets
3985 as two separate insns. This case occurs when some SET allows two
3986 other insns to combine, but the destination of that SET is still live.
3987
3988 Also do this if we started with two insns and (at least) one of the
3989 resulting sets is a noop; this noop will be deleted later.
3990
3991 Also do this if we started with two insns neither of which was a simple
3992 move. */
3993
3994 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3995 && GET_CODE (newpat) == PARALLEL
3996 && XVECLEN (newpat, 0) == 2
3997 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3998 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3999 && (i1
4000 || set_noop_p (XVECEXP (newpat, 0, 0))
4001 || set_noop_p (XVECEXP (newpat, 0, 1))
4002 || (!i2_was_move && !i3_was_move))
4003 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
4004 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
4005 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4006 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4007 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4008 XVECEXP (newpat, 0, 0))
4009 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
4010 XVECEXP (newpat, 0, 1))
4011 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
4012 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
4013 {
4014 rtx set0 = XVECEXP (newpat, 0, 0);
4015 rtx set1 = XVECEXP (newpat, 0, 1);
4016
4017 /* Normally, it doesn't matter which of the two is done first, but
4018 one which uses any regs/memory set or used in between i2 and i3
4019 can't be first. The PARALLEL might also have been pre-existing
4020 in i3, so we need to make sure that we won't wrongly hoist a SET
4021 to i2 that would conflict with a death note present in there, or
4022 would have its dest modified or used between i2 and i3. */
4023 if (!modified_between_p (SET_SRC (set1), i2, i3)
4024 && !(REG_P (SET_DEST (set1))
4025 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4026 && !(GET_CODE (SET_DEST (set1)) == SUBREG
4027 && find_reg_note (i2, REG_DEAD,
4028 SUBREG_REG (SET_DEST (set1))))
4029 && !modified_between_p (SET_DEST (set1), i2, i3)
4030 && !reg_used_between_p (SET_DEST (set1), i2, i3)
4031 /* If I3 is a jump, ensure that set0 is a jump so that
4032 we do not create invalid RTL. */
4033 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4034 )
4035 {
4036 newi2pat = set1;
4037 newpat = set0;
4038 }
4039 else if (!modified_between_p (SET_SRC (set0), i2, i3)
4040 && !(REG_P (SET_DEST (set0))
4041 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4042 && !(GET_CODE (SET_DEST (set0)) == SUBREG
4043 && find_reg_note (i2, REG_DEAD,
4044 SUBREG_REG (SET_DEST (set0))))
4045 && !modified_between_p (SET_DEST (set0), i2, i3)
4046 && !reg_used_between_p (SET_DEST (set0), i2, i3)
4047 /* If I3 is a jump, ensure that set1 is a jump so that
4048 we do not create invalid RTL. */
4049 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4050 )
4051 {
4052 newi2pat = set0;
4053 newpat = set1;
4054 }
4055 else
4056 {
4057 undo_all ();
4058 return 0;
4059 }
4060
4061 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4062
4063 if (i2_code_number >= 0)
4064 {
4065 /* recog_for_combine might have added CLOBBERs to newi2pat.
4066 Make sure NEWPAT does not depend on the clobbered regs. */
4067 if (GET_CODE (newi2pat) == PARALLEL)
4068 {
4069 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4070 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4071 {
4072 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4073 if (reg_overlap_mentioned_p (reg, newpat))
4074 {
4075 undo_all ();
4076 return 0;
4077 }
4078 }
4079 }
4080
4081 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4082
4083 /* Likewise, recog_for_combine might have added clobbers to NEWPAT.
4084 Checking that the SET0's SET_DEST and SET1's SET_DEST aren't
4085 mentioned/clobbered, ensures NEWI2PAT's SET_DEST is live. */
4086 if (insn_code_number >= 0 && GET_CODE (newpat) == PARALLEL)
4087 {
4088 for (i = XVECLEN (newpat, 0) - 1; i >= 0; i--)
4089 if (GET_CODE (XVECEXP (newpat, 0, i)) == CLOBBER)
4090 {
4091 rtx reg = XEXP (XVECEXP (newpat, 0, i), 0);
4092 if (reg_overlap_mentioned_p (reg, SET_DEST (set0))
4093 || reg_overlap_mentioned_p (reg, SET_DEST (set1)))
4094 {
4095 undo_all ();
4096 return 0;
4097 }
4098 }
4099 }
4100
4101 if (insn_code_number >= 0)
4102 split_i2i3 = true;
4103 }
4104 }
4105
4106 /* If it still isn't recognized, fail and change things back the way they
4107 were. */
4108 if ((insn_code_number < 0
4109 /* Is the result a reasonable ASM_OPERANDS? */
4110 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4111 {
4112 undo_all ();
4113 return 0;
4114 }
4115
4116 /* If we had to change another insn, make sure it is valid also. */
4117 if (undobuf.other_insn)
4118 {
4119 CLEAR_HARD_REG_SET (newpat_used_regs);
4120
4121 other_pat = PATTERN (undobuf.other_insn);
4122 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4123 &new_other_notes);
4124
4125 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4126 {
4127 undo_all ();
4128 return 0;
4129 }
4130 }
4131
4132 /* Reject this combination if insn_cost reports that the replacement
4133 instructions are more expensive than the originals. */
4134 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4135 {
4136 undo_all ();
4137 return 0;
4138 }
4139
4140 if (MAY_HAVE_DEBUG_BIND_INSNS)
4141 {
4142 struct undo *undo;
4143
4144 for (undo = undobuf.undos; undo; undo = undo->next)
4145 if (undo->kind == UNDO_MODE)
4146 {
4147 rtx reg = regno_reg_rtx[undo->where.regno];
4148 machine_mode new_mode = GET_MODE (reg);
4149 machine_mode old_mode = undo->old_contents.m;
4150
4151 /* Temporarily revert mode back. */
4152 adjust_reg_mode (reg, old_mode);
4153
4154 if (reg == i2dest && i2scratch)
4155 {
4156 /* If we used i2dest as a scratch register with a
4157 different mode, substitute it for the original
4158 i2src while its original mode is temporarily
4159 restored, and then clear i2scratch so that we don't
4160 do it again later. */
4161 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4162 this_basic_block);
4163 i2scratch = false;
4164 /* Put back the new mode. */
4165 adjust_reg_mode (reg, new_mode);
4166 }
4167 else
4168 {
4169 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4170 rtx_insn *first, *last;
4171
4172 if (reg == i2dest)
4173 {
4174 first = i2;
4175 last = last_combined_insn;
4176 }
4177 else
4178 {
4179 first = i3;
4180 last = undobuf.other_insn;
4181 gcc_assert (last);
4182 if (DF_INSN_LUID (last)
4183 < DF_INSN_LUID (last_combined_insn))
4184 last = last_combined_insn;
4185 }
4186
4187 /* We're dealing with a reg that changed mode but not
4188 meaning, so we want to turn it into a subreg for
4189 the new mode. However, because of REG sharing and
4190 because its mode had already changed, we have to do
4191 it in two steps. First, replace any debug uses of
4192 reg, with its original mode temporarily restored,
4193 with this copy we have created; then, replace the
4194 copy with the SUBREG of the original shared reg,
4195 once again changed to the new mode. */
4196 propagate_for_debug (first, last, reg, tempreg,
4197 this_basic_block);
4198 adjust_reg_mode (reg, new_mode);
4199 propagate_for_debug (first, last, tempreg,
4200 lowpart_subreg (old_mode, reg, new_mode),
4201 this_basic_block);
4202 }
4203 }
4204 }
4205
4206 /* If we will be able to accept this, we have made a
4207 change to the destination of I3. This requires us to
4208 do a few adjustments. */
4209
4210 if (changed_i3_dest)
4211 {
4212 PATTERN (i3) = newpat;
4213 adjust_for_new_dest (i3);
4214 }
4215
4216 bool only_i3_changed = !i0 && !i1 && rtx_equal_p (newi2pat, PATTERN (i2));
4217
4218 /* If only i3 has changed, any split of the combined instruction just
4219 restored i2 to its original state. No destinations moved from i3
4220 to i2. */
4221 if (only_i3_changed)
4222 split_i2i3 = false;
4223
4224 /* We now know that we can do this combination. Merge the insns and
4225 update the status of registers and LOG_LINKS. */
4226
4227 if (undobuf.other_insn)
4228 {
4229 rtx note, next;
4230
4231 PATTERN (undobuf.other_insn) = other_pat;
4232
4233 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4234 ensure that they are still valid. Then add any non-duplicate
4235 notes added by recog_for_combine. */
4236 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4237 {
4238 next = XEXP (note, 1);
4239
4240 if ((REG_NOTE_KIND (note) == REG_DEAD
4241 && !reg_referenced_p (XEXP (note, 0),
4242 PATTERN (undobuf.other_insn)))
4243 ||(REG_NOTE_KIND (note) == REG_UNUSED
4244 && !reg_set_p (XEXP (note, 0),
4245 PATTERN (undobuf.other_insn)))
4246 /* Simply drop equal note since it may be no longer valid
4247 for other_insn. It may be possible to record that CC
4248 register is changed and only discard those notes, but
4249 in practice it's unnecessary complication and doesn't
4250 give any meaningful improvement.
4251
4252 See PR78559. */
4253 || REG_NOTE_KIND (note) == REG_EQUAL
4254 || REG_NOTE_KIND (note) == REG_EQUIV)
4255 remove_note (undobuf.other_insn, note);
4256 }
4257
4258 distribute_notes (new_other_notes, undobuf.other_insn,
4259 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4260 NULL_RTX);
4261 }
4262
4263 if (swap_i2i3)
4264 {
4265 /* I3 now uses what used to be its destination and which is now
4266 I2's destination. This requires us to do a few adjustments. */
4267 PATTERN (i3) = newpat;
4268 adjust_for_new_dest (i3);
4269 }
4270
4271 if (swap_i2i3 || split_i2i3)
4272 {
4273 /* We might need a LOG_LINK from I3 to I2. But then we used to
4274 have one, so we still will.
4275
4276 However, some later insn might be using I2's dest and have
4277 a LOG_LINK pointing at I3. We should change it to point at
4278 I2 instead. */
4279
4280 /* newi2pat is usually a SET here; however, recog_for_combine might
4281 have added some clobbers. */
4282 rtx x = newi2pat;
4283 if (GET_CODE (x) == PARALLEL)
4284 x = XVECEXP (newi2pat, 0, 0);
4285
4286 if (REG_P (SET_DEST (x))
4287 || (GET_CODE (SET_DEST (x)) == SUBREG
4288 && REG_P (SUBREG_REG (SET_DEST (x)))))
4289 {
4290 unsigned int regno = reg_or_subregno (SET_DEST (x));
4291
4292 bool done = false;
4293 for (rtx_insn *insn = NEXT_INSN (i3);
4294 !done
4295 && insn
4296 && INSN_P (insn)
4297 && BLOCK_FOR_INSN (insn) == this_basic_block;
4298 insn = NEXT_INSN (insn))
4299 {
4300 if (DEBUG_INSN_P (insn))
4301 continue;
4302 struct insn_link *link;
4303 FOR_EACH_LOG_LINK (link, insn)
4304 if (link->insn == i3 && link->regno == regno)
4305 {
4306 link->insn = i2;
4307 done = true;
4308 break;
4309 }
4310 }
4311 }
4312 }
4313
4314 {
4315 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4316 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4317 rtx midnotes = 0;
4318 int from_luid;
4319 /* Compute which registers we expect to eliminate. newi2pat may be setting
4320 either i3dest or i2dest, so we must check it. */
4321 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4322 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4323 || !i2dest_killed
4324 ? 0 : i2dest);
4325 /* For i1, we need to compute both local elimination and global
4326 elimination information with respect to newi2pat because i1dest
4327 may be the same as i3dest, in which case newi2pat may be setting
4328 i1dest. Global information is used when distributing REG_DEAD
4329 note for i2 and i3, in which case it does matter if newi2pat sets
4330 i1dest or not.
4331
4332 Local information is used when distributing REG_DEAD note for i1,
4333 in which case it doesn't matter if newi2pat sets i1dest or not.
4334 See PR62151, if we have four insns combination:
4335 i0: r0 <- i0src
4336 i1: r1 <- i1src (using r0)
4337 REG_DEAD (r0)
4338 i2: r0 <- i2src (using r1)
4339 i3: r3 <- i3src (using r0)
4340 ix: using r0
4341 From i1's point of view, r0 is eliminated, no matter if it is set
4342 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4343 should be discarded.
4344
4345 Note local information only affects cases in forms like "I1->I2->I3",
4346 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4347 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4348 i0dest anyway. */
4349 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4350 || !i1dest_killed
4351 ? 0 : i1dest);
4352 rtx elim_i1 = (local_elim_i1 == 0
4353 || (newi2pat && reg_set_p (i1dest, newi2pat))
4354 ? 0 : i1dest);
4355 /* Same case as i1. */
4356 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4357 ? 0 : i0dest);
4358 rtx elim_i0 = (local_elim_i0 == 0
4359 || (newi2pat && reg_set_p (i0dest, newi2pat))
4360 ? 0 : i0dest);
4361
4362 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4363 clear them. */
4364 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4365 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4366 if (i1)
4367 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4368 if (i0)
4369 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4370
4371 /* Ensure that we do not have something that should not be shared but
4372 occurs multiple times in the new insns. Check this by first
4373 resetting all the `used' flags and then copying anything is shared. */
4374
4375 reset_used_flags (i3notes);
4376 reset_used_flags (i2notes);
4377 reset_used_flags (i1notes);
4378 reset_used_flags (i0notes);
4379 reset_used_flags (newpat);
4380 reset_used_flags (newi2pat);
4381 if (undobuf.other_insn)
4382 reset_used_flags (PATTERN (undobuf.other_insn));
4383
4384 i3notes = copy_rtx_if_shared (i3notes);
4385 i2notes = copy_rtx_if_shared (i2notes);
4386 i1notes = copy_rtx_if_shared (i1notes);
4387 i0notes = copy_rtx_if_shared (i0notes);
4388 newpat = copy_rtx_if_shared (newpat);
4389 newi2pat = copy_rtx_if_shared (newi2pat);
4390 if (undobuf.other_insn)
4391 reset_used_flags (PATTERN (undobuf.other_insn));
4392
4393 INSN_CODE (i3) = insn_code_number;
4394 PATTERN (i3) = newpat;
4395
4396 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4397 {
4398 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4399 link = XEXP (link, 1))
4400 {
4401 if (substed_i2)
4402 {
4403 /* I2SRC must still be meaningful at this point. Some
4404 splitting operations can invalidate I2SRC, but those
4405 operations do not apply to calls. */
4406 gcc_assert (i2src);
4407 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4408 i2dest, i2src);
4409 }
4410 if (substed_i1)
4411 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4412 i1dest, i1src);
4413 if (substed_i0)
4414 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4415 i0dest, i0src);
4416 }
4417 }
4418
4419 if (undobuf.other_insn)
4420 INSN_CODE (undobuf.other_insn) = other_code_number;
4421
4422 /* We had one special case above where I2 had more than one set and
4423 we replaced a destination of one of those sets with the destination
4424 of I3. In that case, we have to update LOG_LINKS of insns later
4425 in this basic block. Note that this (expensive) case is rare.
4426
4427 Also, in this case, we must pretend that all REG_NOTEs for I2
4428 actually came from I3, so that REG_UNUSED notes from I2 will be
4429 properly handled. */
4430
4431 if (i3_subst_into_i2)
4432 {
4433 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4434 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4435 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4436 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4437 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4438 && ! find_reg_note (i2, REG_UNUSED,
4439 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4440 for (temp_insn = NEXT_INSN (i2);
4441 temp_insn
4442 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4443 || BB_HEAD (this_basic_block) != temp_insn);
4444 temp_insn = NEXT_INSN (temp_insn))
4445 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4446 FOR_EACH_LOG_LINK (link, temp_insn)
4447 if (link->insn == i2)
4448 link->insn = i3;
4449
4450 if (i3notes)
4451 {
4452 rtx link = i3notes;
4453 while (XEXP (link, 1))
4454 link = XEXP (link, 1);
4455 XEXP (link, 1) = i2notes;
4456 }
4457 else
4458 i3notes = i2notes;
4459 i2notes = 0;
4460 }
4461
4462 LOG_LINKS (i3) = NULL;
4463 REG_NOTES (i3) = 0;
4464 LOG_LINKS (i2) = NULL;
4465 REG_NOTES (i2) = 0;
4466
4467 if (newi2pat)
4468 {
4469 if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4470 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4471 this_basic_block);
4472 INSN_CODE (i2) = i2_code_number;
4473 PATTERN (i2) = newi2pat;
4474 }
4475 else
4476 {
4477 if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4478 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4479 this_basic_block);
4480 SET_INSN_DELETED (i2);
4481 }
4482
4483 if (i1)
4484 {
4485 LOG_LINKS (i1) = NULL;
4486 REG_NOTES (i1) = 0;
4487 if (MAY_HAVE_DEBUG_BIND_INSNS)
4488 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4489 this_basic_block);
4490 SET_INSN_DELETED (i1);
4491 }
4492
4493 if (i0)
4494 {
4495 LOG_LINKS (i0) = NULL;
4496 REG_NOTES (i0) = 0;
4497 if (MAY_HAVE_DEBUG_BIND_INSNS)
4498 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4499 this_basic_block);
4500 SET_INSN_DELETED (i0);
4501 }
4502
4503 /* Get death notes for everything that is now used in either I3 or
4504 I2 and used to die in a previous insn. If we built two new
4505 patterns, move from I1 to I2 then I2 to I3 so that we get the
4506 proper movement on registers that I2 modifies. */
4507
4508 if (i0)
4509 from_luid = DF_INSN_LUID (i0);
4510 else if (i1)
4511 from_luid = DF_INSN_LUID (i1);
4512 else
4513 from_luid = DF_INSN_LUID (i2);
4514 if (newi2pat)
4515 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4516 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4517
4518 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4519 if (i3notes)
4520 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4521 elim_i2, elim_i1, elim_i0);
4522 if (i2notes)
4523 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4524 elim_i2, elim_i1, elim_i0);
4525 if (i1notes)
4526 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4527 elim_i2, local_elim_i1, local_elim_i0);
4528 if (i0notes)
4529 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4530 elim_i2, elim_i1, local_elim_i0);
4531 if (midnotes)
4532 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4533 elim_i2, elim_i1, elim_i0);
4534
4535 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4536 know these are REG_UNUSED and want them to go to the desired insn,
4537 so we always pass it as i3. */
4538
4539 if (newi2pat && new_i2_notes)
4540 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4541 NULL_RTX);
4542
4543 if (new_i3_notes)
4544 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4545 NULL_RTX);
4546
4547 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4548 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4549 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4550 in that case, it might delete I2. Similarly for I2 and I1.
4551 Show an additional death due to the REG_DEAD note we make here. If
4552 we discard it in distribute_notes, we will decrement it again. */
4553
4554 if (i3dest_killed)
4555 {
4556 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4557 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4558 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4559 elim_i1, elim_i0);
4560 else
4561 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4562 elim_i2, elim_i1, elim_i0);
4563 }
4564
4565 if (i2dest_in_i2src)
4566 {
4567 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4568 if (newi2pat && reg_set_p (i2dest, newi2pat))
4569 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4570 NULL_RTX, NULL_RTX);
4571 else
4572 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4573 NULL_RTX, NULL_RTX, NULL_RTX);
4574 }
4575
4576 if (i1dest_in_i1src)
4577 {
4578 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4579 if (newi2pat && reg_set_p (i1dest, newi2pat))
4580 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4581 NULL_RTX, NULL_RTX);
4582 else
4583 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4584 NULL_RTX, NULL_RTX, NULL_RTX);
4585 }
4586
4587 if (i0dest_in_i0src)
4588 {
4589 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4590 if (newi2pat && reg_set_p (i0dest, newi2pat))
4591 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4592 NULL_RTX, NULL_RTX);
4593 else
4594 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4595 NULL_RTX, NULL_RTX, NULL_RTX);
4596 }
4597
4598 if (only_i3_changed)
4599 distribute_links (i3links, i3, param_max_combine_search_insns);
4600 else
4601 {
4602 distribute_links (i3links);
4603 distribute_links (i2links, i2);
4604 distribute_links (i1links);
4605 distribute_links (i0links);
4606 }
4607
4608 if (REG_P (i2dest))
4609 {
4610 struct insn_link *link;
4611 rtx_insn *i2_insn = 0;
4612 rtx i2_val = 0, set;
4613
4614 /* The insn that used to set this register doesn't exist, and
4615 this life of the register may not exist either. See if one of
4616 I3's links points to an insn that sets I2DEST. If it does,
4617 that is now the last known value for I2DEST. If we don't update
4618 this and I2 set the register to a value that depended on its old
4619 contents, we will get confused. If this insn is used, thing
4620 will be set correctly in combine_instructions. */
4621 FOR_EACH_LOG_LINK (link, i3)
4622 if ((set = single_set (link->insn)) != 0
4623 && rtx_equal_p (i2dest, SET_DEST (set)))
4624 i2_insn = link->insn, i2_val = SET_SRC (set);
4625
4626 record_value_for_reg (i2dest, i2_insn, i2_val);
4627
4628 /* If the reg formerly set in I2 died only once and that was in I3,
4629 zero its use count so it won't make `reload' do any work. */
4630 if (! added_sets_2
4631 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4632 && ! i2dest_in_i2src
4633 && REGNO (i2dest) < reg_n_sets_max)
4634 INC_REG_N_SETS (REGNO (i2dest), -1);
4635 }
4636
4637 if (i1 && REG_P (i1dest))
4638 {
4639 struct insn_link *link;
4640 rtx_insn *i1_insn = 0;
4641 rtx i1_val = 0, set;
4642
4643 FOR_EACH_LOG_LINK (link, i3)
4644 if ((set = single_set (link->insn)) != 0
4645 && rtx_equal_p (i1dest, SET_DEST (set)))
4646 i1_insn = link->insn, i1_val = SET_SRC (set);
4647
4648 record_value_for_reg (i1dest, i1_insn, i1_val);
4649
4650 if (! added_sets_1
4651 && ! i1dest_in_i1src
4652 && REGNO (i1dest) < reg_n_sets_max)
4653 INC_REG_N_SETS (REGNO (i1dest), -1);
4654 }
4655
4656 if (i0 && REG_P (i0dest))
4657 {
4658 struct insn_link *link;
4659 rtx_insn *i0_insn = 0;
4660 rtx i0_val = 0, set;
4661
4662 FOR_EACH_LOG_LINK (link, i3)
4663 if ((set = single_set (link->insn)) != 0
4664 && rtx_equal_p (i0dest, SET_DEST (set)))
4665 i0_insn = link->insn, i0_val = SET_SRC (set);
4666
4667 record_value_for_reg (i0dest, i0_insn, i0_val);
4668
4669 if (! added_sets_0
4670 && ! i0dest_in_i0src
4671 && REGNO (i0dest) < reg_n_sets_max)
4672 INC_REG_N_SETS (REGNO (i0dest), -1);
4673 }
4674
4675 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4676 been made to this insn. The order is important, because newi2pat
4677 can affect nonzero_bits of newpat. */
4678 if (newi2pat)
4679 note_pattern_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4680 note_pattern_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4681 }
4682
4683 if (undobuf.other_insn != NULL_RTX)
4684 {
4685 if (dump_file)
4686 {
4687 fprintf (dump_file, "modifying other_insn ");
4688 dump_insn_slim (dump_file, undobuf.other_insn);
4689 }
4690 df_insn_rescan (undobuf.other_insn);
4691 }
4692
4693 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4694 {
4695 if (dump_file)
4696 {
4697 fprintf (dump_file, "modifying insn i0 ");
4698 dump_insn_slim (dump_file, i0);
4699 }
4700 df_insn_rescan (i0);
4701 }
4702
4703 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4704 {
4705 if (dump_file)
4706 {
4707 fprintf (dump_file, "modifying insn i1 ");
4708 dump_insn_slim (dump_file, i1);
4709 }
4710 df_insn_rescan (i1);
4711 }
4712
4713 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4714 {
4715 if (dump_file)
4716 {
4717 fprintf (dump_file, "modifying insn i2 ");
4718 dump_insn_slim (dump_file, i2);
4719 }
4720 df_insn_rescan (i2);
4721 }
4722
4723 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4724 {
4725 if (dump_file)
4726 {
4727 fprintf (dump_file, "modifying insn i3 ");
4728 dump_insn_slim (dump_file, i3);
4729 }
4730 df_insn_rescan (i3);
4731 }
4732
4733 /* Set new_direct_jump_p if a new return or simple jump instruction
4734 has been created. Adjust the CFG accordingly. */
4735 if (returnjump_p (i3) || any_uncondjump_p (i3))
4736 {
4737 *new_direct_jump_p = 1;
4738 mark_jump_label (PATTERN (i3), i3, 0);
4739 update_cfg_for_uncondjump (i3);
4740 }
4741
4742 if (undobuf.other_insn != NULL_RTX
4743 && (returnjump_p (undobuf.other_insn)
4744 || any_uncondjump_p (undobuf.other_insn)))
4745 {
4746 *new_direct_jump_p = 1;
4747 update_cfg_for_uncondjump (undobuf.other_insn);
4748 }
4749
4750 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4751 && XEXP (PATTERN (i3), 0) == const1_rtx)
4752 {
4753 basic_block bb = BLOCK_FOR_INSN (i3);
4754 gcc_assert (bb);
4755 remove_edge (split_block (bb, i3));
4756 emit_barrier_after_bb (bb);
4757 *new_direct_jump_p = 1;
4758 }
4759
4760 if (undobuf.other_insn
4761 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4762 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4763 {
4764 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4765 gcc_assert (bb);
4766 remove_edge (split_block (bb, undobuf.other_insn));
4767 emit_barrier_after_bb (bb);
4768 *new_direct_jump_p = 1;
4769 }
4770
4771 /* A noop might also need cleaning up of CFG, if it comes from the
4772 simplification of a jump. */
4773 if (JUMP_P (i3)
4774 && GET_CODE (newpat) == SET
4775 && SET_SRC (newpat) == pc_rtx
4776 && SET_DEST (newpat) == pc_rtx)
4777 {
4778 *new_direct_jump_p = 1;
4779 update_cfg_for_uncondjump (i3);
4780 }
4781
4782 if (undobuf.other_insn != NULL_RTX
4783 && JUMP_P (undobuf.other_insn)
4784 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4785 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4786 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4787 {
4788 *new_direct_jump_p = 1;
4789 update_cfg_for_uncondjump (undobuf.other_insn);
4790 }
4791
4792 combine_successes++;
4793 undo_commit ();
4794
4795 if (only_i3_changed)
4796 return i3;
4797
4798 rtx_insn *ret = newi2pat ? i2 : i3;
4799 if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4800 ret = added_links_insn;
4801 if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4802 ret = added_notes_insn;
4803
4804 return ret;
4805 }
4806
4807 /* Get a marker for undoing to the current state. */
4808
4809 static void *
4810 get_undo_marker (void)
4811 {
4812 return undobuf.undos;
4813 }
4814
4815 /* Undo the modifications up to the marker. */
4816
4817 static void
4818 undo_to_marker (void *marker)
4819 {
4820 struct undo *undo, *next;
4821
4822 for (undo = undobuf.undos; undo != marker; undo = next)
4823 {
4824 gcc_assert (undo);
4825
4826 next = undo->next;
4827 switch (undo->kind)
4828 {
4829 case UNDO_RTX:
4830 *undo->where.r = undo->old_contents.r;
4831 break;
4832 case UNDO_INT:
4833 *undo->where.i = undo->old_contents.i;
4834 break;
4835 case UNDO_MODE:
4836 adjust_reg_mode (regno_reg_rtx[undo->where.regno],
4837 undo->old_contents.m);
4838 break;
4839 case UNDO_LINKS:
4840 *undo->where.l = undo->old_contents.l;
4841 break;
4842 default:
4843 gcc_unreachable ();
4844 }
4845
4846 undo->next = undobuf.frees;
4847 undobuf.frees = undo;
4848 }
4849
4850 undobuf.undos = (struct undo *) marker;
4851 }
4852
4853 /* Undo all the modifications recorded in undobuf. */
4854
4855 static void
4856 undo_all (void)
4857 {
4858 undo_to_marker (0);
4859 }
4860
4861 /* We've committed to accepting the changes we made. Move all
4862 of the undos to the free list. */
4863
4864 static void
4865 undo_commit (void)
4866 {
4867 struct undo *undo, *next;
4868
4869 for (undo = undobuf.undos; undo; undo = next)
4870 {
4871 next = undo->next;
4872 undo->next = undobuf.frees;
4873 undobuf.frees = undo;
4874 }
4875 undobuf.undos = 0;
4876 }
4877
4878 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4879 where we have an arithmetic expression and return that point. LOC will
4880 be inside INSN.
4881
4882 try_combine will call this function to see if an insn can be split into
4883 two insns. */
4884
4885 static rtx *
4886 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4887 {
4888 rtx x = *loc;
4889 enum rtx_code code = GET_CODE (x);
4890 rtx *split;
4891 unsigned HOST_WIDE_INT len = 0;
4892 HOST_WIDE_INT pos = 0;
4893 bool unsignedp = false;
4894 rtx inner = NULL_RTX;
4895 scalar_int_mode mode, inner_mode;
4896
4897 /* First special-case some codes. */
4898 switch (code)
4899 {
4900 case SUBREG:
4901 #ifdef INSN_SCHEDULING
4902 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4903 point. */
4904 if (MEM_P (SUBREG_REG (x)))
4905 return loc;
4906 #endif
4907 return find_split_point (&SUBREG_REG (x), insn, false);
4908
4909 case MEM:
4910 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4911 using LO_SUM and HIGH. */
4912 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4913 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4914 {
4915 machine_mode address_mode = get_address_mode (x);
4916
4917 SUBST (XEXP (x, 0),
4918 gen_rtx_LO_SUM (address_mode,
4919 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4920 XEXP (x, 0)));
4921 return &XEXP (XEXP (x, 0), 0);
4922 }
4923
4924 /* If we have a PLUS whose second operand is a constant and the
4925 address is not valid, perhaps we can split it up using
4926 the machine-specific way to split large constants. We use
4927 the first pseudo-reg (one of the virtual regs) as a placeholder;
4928 it will not remain in the result. */
4929 if (GET_CODE (XEXP (x, 0)) == PLUS
4930 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4931 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4932 MEM_ADDR_SPACE (x)))
4933 {
4934 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4935 unsigned int old_nregs, new_nregs;
4936 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4937 subst_insn, &old_nregs, &new_nregs);
4938
4939 /* This should have produced two insns, each of which sets our
4940 placeholder. If the source of the second is a valid address,
4941 we can put both sources together and make a split point
4942 in the middle. */
4943
4944 if (seq
4945 && NEXT_INSN (seq) != NULL_RTX
4946 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4947 && NONJUMP_INSN_P (seq)
4948 && GET_CODE (PATTERN (seq)) == SET
4949 && SET_DEST (PATTERN (seq)) == reg
4950 && ! reg_mentioned_p (reg,
4951 SET_SRC (PATTERN (seq)))
4952 && NONJUMP_INSN_P (NEXT_INSN (seq))
4953 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4954 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4955 && memory_address_addr_space_p
4956 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4957 MEM_ADDR_SPACE (x)))
4958 {
4959 rtx src1 = SET_SRC (PATTERN (seq));
4960 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4961
4962 /* Replace the placeholder in SRC2 with SRC1. If we can
4963 find where in SRC2 it was placed, that can become our
4964 split point and we can replace this address with SRC2.
4965 Just try two obvious places. */
4966
4967 src2 = replace_rtx (src2, reg, src1);
4968 split = 0;
4969 if (XEXP (src2, 0) == src1)
4970 split = &XEXP (src2, 0);
4971 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4972 && XEXP (XEXP (src2, 0), 0) == src1)
4973 split = &XEXP (XEXP (src2, 0), 0);
4974
4975 if (split)
4976 {
4977 SUBST (XEXP (x, 0), src2);
4978 return split;
4979 }
4980 }
4981
4982 /* If that didn't work and we have a nested plus, like:
4983 ((REG1 * CONST1) + REG2) + CONST2 and (REG1 + REG2) + CONST2
4984 is valid address, try to split (REG1 * CONST1). */
4985 if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
4986 && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
4987 && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
4988 && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SUBREG
4989 && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
4990 0), 0)))))
4991 {
4992 rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 0);
4993 XEXP (XEXP (XEXP (x, 0), 0), 0) = reg;
4994 if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4995 MEM_ADDR_SPACE (x)))
4996 {
4997 XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
4998 return &XEXP (XEXP (XEXP (x, 0), 0), 0);
4999 }
5000 XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5001 }
5002 else if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5003 && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5004 && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5005 && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SUBREG
5006 && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5007 0), 1)))))
5008 {
5009 rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 1);
5010 XEXP (XEXP (XEXP (x, 0), 0), 1) = reg;
5011 if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5012 MEM_ADDR_SPACE (x)))
5013 {
5014 XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5015 return &XEXP (XEXP (XEXP (x, 0), 0), 1);
5016 }
5017 XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5018 }
5019
5020 /* If that didn't work, perhaps the first operand is complex and
5021 needs to be computed separately, so make a split point there.
5022 This will occur on machines that just support REG + CONST
5023 and have a constant moved through some previous computation. */
5024 if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
5025 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5026 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5027 return &XEXP (XEXP (x, 0), 0);
5028 }
5029
5030 /* If we have a PLUS whose first operand is complex, try computing it
5031 separately by making a split there. */
5032 if (GET_CODE (XEXP (x, 0)) == PLUS
5033 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5034 MEM_ADDR_SPACE (x))
5035 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
5036 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5037 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5038 return &XEXP (XEXP (x, 0), 0);
5039 break;
5040
5041 case SET:
5042 /* See if we can split SET_SRC as it stands. */
5043 split = find_split_point (&SET_SRC (x), insn, true);
5044 if (split && split != &SET_SRC (x))
5045 return split;
5046
5047 /* See if we can split SET_DEST as it stands. */
5048 split = find_split_point (&SET_DEST (x), insn, false);
5049 if (split && split != &SET_DEST (x))
5050 return split;
5051
5052 /* See if this is a bitfield assignment with everything constant. If
5053 so, this is an IOR of an AND, so split it into that. */
5054 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5055 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5056 &inner_mode)
5057 && HWI_COMPUTABLE_MODE_P (inner_mode)
5058 && CONST_INT_P (XEXP (SET_DEST (x), 1))
5059 && CONST_INT_P (XEXP (SET_DEST (x), 2))
5060 && CONST_INT_P (SET_SRC (x))
5061 && ((INTVAL (XEXP (SET_DEST (x), 1))
5062 + INTVAL (XEXP (SET_DEST (x), 2)))
5063 <= GET_MODE_PRECISION (inner_mode))
5064 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5065 {
5066 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5067 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5068 rtx dest = XEXP (SET_DEST (x), 0);
5069 unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << len) - 1;
5070 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x)) & mask;
5071 rtx or_mask;
5072
5073 if (BITS_BIG_ENDIAN)
5074 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5075
5076 or_mask = gen_int_mode (src << pos, inner_mode);
5077 if (src == mask)
5078 SUBST (SET_SRC (x),
5079 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5080 else
5081 {
5082 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5083 SUBST (SET_SRC (x),
5084 simplify_gen_binary (IOR, inner_mode,
5085 simplify_gen_binary (AND, inner_mode,
5086 dest, negmask),
5087 or_mask));
5088 }
5089
5090 SUBST (SET_DEST (x), dest);
5091
5092 split = find_split_point (&SET_SRC (x), insn, true);
5093 if (split && split != &SET_SRC (x))
5094 return split;
5095 }
5096
5097 /* Otherwise, see if this is an operation that we can split into two.
5098 If so, try to split that. */
5099 code = GET_CODE (SET_SRC (x));
5100
5101 switch (code)
5102 {
5103 case AND:
5104 /* If we are AND'ing with a large constant that is only a single
5105 bit and the result is only being used in a context where we
5106 need to know if it is zero or nonzero, replace it with a bit
5107 extraction. This will avoid the large constant, which might
5108 have taken more than one insn to make. If the constant were
5109 not a valid argument to the AND but took only one insn to make,
5110 this is no worse, but if it took more than one insn, it will
5111 be better. */
5112
5113 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5114 && REG_P (XEXP (SET_SRC (x), 0))
5115 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5116 && REG_P (SET_DEST (x))
5117 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5118 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5119 && XEXP (*split, 0) == SET_DEST (x)
5120 && XEXP (*split, 1) == const0_rtx)
5121 {
5122 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5123 XEXP (SET_SRC (x), 0),
5124 pos, NULL_RTX, 1,
5125 true, false, false);
5126 if (extraction != 0)
5127 {
5128 SUBST (SET_SRC (x), extraction);
5129 return find_split_point (loc, insn, false);
5130 }
5131 }
5132 break;
5133
5134 case NE:
5135 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5136 is known to be on, this can be converted into a NEG of a shift. */
5137 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5138 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5139 && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5140 GET_MODE (XEXP (SET_SRC (x),
5141 0))))) >= 1))
5142 {
5143 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5144 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5145 SUBST (SET_SRC (x),
5146 gen_rtx_NEG (mode,
5147 gen_rtx_LSHIFTRT (mode,
5148 XEXP (SET_SRC (x), 0),
5149 pos_rtx)));
5150
5151 split = find_split_point (&SET_SRC (x), insn, true);
5152 if (split && split != &SET_SRC (x))
5153 return split;
5154 }
5155 break;
5156
5157 case SIGN_EXTEND:
5158 inner = XEXP (SET_SRC (x), 0);
5159
5160 /* We can't optimize if either mode is a partial integer
5161 mode as we don't know how many bits are significant
5162 in those modes. */
5163 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5164 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5165 break;
5166
5167 pos = 0;
5168 len = GET_MODE_PRECISION (inner_mode);
5169 unsignedp = false;
5170 break;
5171
5172 case SIGN_EXTRACT:
5173 case ZERO_EXTRACT:
5174 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5175 &inner_mode)
5176 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5177 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5178 {
5179 inner = XEXP (SET_SRC (x), 0);
5180 len = INTVAL (XEXP (SET_SRC (x), 1));
5181 pos = INTVAL (XEXP (SET_SRC (x), 2));
5182
5183 if (BITS_BIG_ENDIAN)
5184 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5185 unsignedp = (code == ZERO_EXTRACT);
5186 }
5187 break;
5188
5189 default:
5190 break;
5191 }
5192
5193 if (len
5194 && known_subrange_p (pos, len,
5195 0, GET_MODE_PRECISION (GET_MODE (inner)))
5196 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5197 {
5198 /* For unsigned, we have a choice of a shift followed by an
5199 AND or two shifts. Use two shifts for field sizes where the
5200 constant might be too large. We assume here that we can
5201 always at least get 8-bit constants in an AND insn, which is
5202 true for every current RISC. */
5203
5204 if (unsignedp && len <= 8)
5205 {
5206 unsigned HOST_WIDE_INT mask
5207 = (HOST_WIDE_INT_1U << len) - 1;
5208 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5209 SUBST (SET_SRC (x),
5210 gen_rtx_AND (mode,
5211 gen_rtx_LSHIFTRT
5212 (mode, gen_lowpart (mode, inner), pos_rtx),
5213 gen_int_mode (mask, mode)));
5214
5215 split = find_split_point (&SET_SRC (x), insn, true);
5216 if (split && split != &SET_SRC (x))
5217 return split;
5218 }
5219 else
5220 {
5221 int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5222 int right_bits = GET_MODE_PRECISION (mode) - len;
5223 SUBST (SET_SRC (x),
5224 gen_rtx_fmt_ee
5225 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5226 gen_rtx_ASHIFT (mode,
5227 gen_lowpart (mode, inner),
5228 gen_int_shift_amount (mode, left_bits)),
5229 gen_int_shift_amount (mode, right_bits)));
5230
5231 split = find_split_point (&SET_SRC (x), insn, true);
5232 if (split && split != &SET_SRC (x))
5233 return split;
5234 }
5235 }
5236
5237 /* See if this is a simple operation with a constant as the second
5238 operand. It might be that this constant is out of range and hence
5239 could be used as a split point. */
5240 if (BINARY_P (SET_SRC (x))
5241 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5242 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5243 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5244 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5245 return &XEXP (SET_SRC (x), 1);
5246
5247 /* Finally, see if this is a simple operation with its first operand
5248 not in a register. The operation might require this operand in a
5249 register, so return it as a split point. We can always do this
5250 because if the first operand were another operation, we would have
5251 already found it as a split point. */
5252 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5253 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5254 return &XEXP (SET_SRC (x), 0);
5255
5256 return 0;
5257
5258 case AND:
5259 case IOR:
5260 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5261 it is better to write this as (not (ior A B)) so we can split it.
5262 Similarly for IOR. */
5263 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5264 {
5265 SUBST (*loc,
5266 gen_rtx_NOT (GET_MODE (x),
5267 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5268 GET_MODE (x),
5269 XEXP (XEXP (x, 0), 0),
5270 XEXP (XEXP (x, 1), 0))));
5271 return find_split_point (loc, insn, set_src);
5272 }
5273
5274 /* Many RISC machines have a large set of logical insns. If the
5275 second operand is a NOT, put it first so we will try to split the
5276 other operand first. */
5277 if (GET_CODE (XEXP (x, 1)) == NOT)
5278 {
5279 rtx tem = XEXP (x, 0);
5280 SUBST (XEXP (x, 0), XEXP (x, 1));
5281 SUBST (XEXP (x, 1), tem);
5282 }
5283 break;
5284
5285 case PLUS:
5286 case MINUS:
5287 /* Canonicalization can produce (minus A (mult B C)), where C is a
5288 constant. It may be better to try splitting (plus (mult B -C) A)
5289 instead if this isn't a multiply by a power of two. */
5290 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5291 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5292 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5293 {
5294 machine_mode mode = GET_MODE (x);
5295 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5296 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5297 SUBST (*loc, gen_rtx_PLUS (mode,
5298 gen_rtx_MULT (mode,
5299 XEXP (XEXP (x, 1), 0),
5300 gen_int_mode (other_int,
5301 mode)),
5302 XEXP (x, 0)));
5303 return find_split_point (loc, insn, set_src);
5304 }
5305
5306 /* Split at a multiply-accumulate instruction. However if this is
5307 the SET_SRC, we likely do not have such an instruction and it's
5308 worthless to try this split. */
5309 if (!set_src
5310 && (GET_CODE (XEXP (x, 0)) == MULT
5311 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5312 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5313 return loc;
5314
5315 default:
5316 break;
5317 }
5318
5319 /* Otherwise, select our actions depending on our rtx class. */
5320 switch (GET_RTX_CLASS (code))
5321 {
5322 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5323 case RTX_TERNARY:
5324 split = find_split_point (&XEXP (x, 2), insn, false);
5325 if (split)
5326 return split;
5327 /* fall through */
5328 case RTX_BIN_ARITH:
5329 case RTX_COMM_ARITH:
5330 case RTX_COMPARE:
5331 case RTX_COMM_COMPARE:
5332 split = find_split_point (&XEXP (x, 1), insn, false);
5333 if (split)
5334 return split;
5335 /* fall through */
5336 case RTX_UNARY:
5337 /* Some machines have (and (shift ...) ...) insns. If X is not
5338 an AND, but XEXP (X, 0) is, use it as our split point. */
5339 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5340 return &XEXP (x, 0);
5341
5342 split = find_split_point (&XEXP (x, 0), insn, false);
5343 if (split)
5344 return split;
5345 return loc;
5346
5347 default:
5348 /* Otherwise, we don't have a split point. */
5349 return 0;
5350 }
5351 }
5352
5353 /* Throughout X, replace FROM with TO, and return the result.
5354 The result is TO if X is FROM;
5355 otherwise the result is X, but its contents may have been modified.
5356 If they were modified, a record was made in undobuf so that
5357 undo_all will (among other things) return X to its original state.
5358
5359 If the number of changes necessary is too much to record to undo,
5360 the excess changes are not made, so the result is invalid.
5361 The changes already made can still be undone.
5362 undobuf.num_undo is incremented for such changes, so by testing that
5363 the caller can tell whether the result is valid.
5364
5365 `n_occurrences' is incremented each time FROM is replaced.
5366
5367 IN_DEST is true if we are processing the SET_DEST of a SET.
5368
5369 IN_COND is true if we are at the top level of a condition.
5370
5371 UNIQUE_COPY is true if each substitution must be unique. We do this
5372 by copying if `n_occurrences' is nonzero. */
5373
5374 static rtx
5375 subst (rtx x, rtx from, rtx to, bool in_dest, bool in_cond, bool unique_copy)
5376 {
5377 enum rtx_code code = GET_CODE (x);
5378 machine_mode op0_mode = VOIDmode;
5379 const char *fmt;
5380 int len, i;
5381 rtx new_rtx;
5382
5383 /* Two expressions are equal if they are identical copies of a shared
5384 RTX or if they are both registers with the same register number
5385 and mode. */
5386
5387 #define COMBINE_RTX_EQUAL_P(X,Y) \
5388 ((X) == (Y) \
5389 || (REG_P (X) && REG_P (Y) \
5390 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5391
5392 /* Do not substitute into clobbers of regs -- this will never result in
5393 valid RTL. */
5394 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5395 return x;
5396
5397 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5398 {
5399 n_occurrences++;
5400 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5401 }
5402
5403 /* If X and FROM are the same register but different modes, they
5404 will not have been seen as equal above. However, the log links code
5405 will make a LOG_LINKS entry for that case. If we do nothing, we
5406 will try to rerecognize our original insn and, when it succeeds,
5407 we will delete the feeding insn, which is incorrect.
5408
5409 So force this insn not to match in this (rare) case. */
5410 if (! in_dest && code == REG && REG_P (from)
5411 && reg_overlap_mentioned_p (x, from))
5412 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5413
5414 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5415 of which may contain things that can be combined. */
5416 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5417 return x;
5418
5419 /* It is possible to have a subexpression appear twice in the insn.
5420 Suppose that FROM is a register that appears within TO.
5421 Then, after that subexpression has been scanned once by `subst',
5422 the second time it is scanned, TO may be found. If we were
5423 to scan TO here, we would find FROM within it and create a
5424 self-referent rtl structure which is completely wrong. */
5425 if (COMBINE_RTX_EQUAL_P (x, to))
5426 return to;
5427
5428 /* Parallel asm_operands need special attention because all of the
5429 inputs are shared across the arms. Furthermore, unsharing the
5430 rtl results in recognition failures. Failure to handle this case
5431 specially can result in circular rtl.
5432
5433 Solve this by doing a normal pass across the first entry of the
5434 parallel, and only processing the SET_DESTs of the subsequent
5435 entries. Ug. */
5436
5437 if (code == PARALLEL
5438 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5439 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5440 {
5441 new_rtx = subst (XVECEXP (x, 0, 0), from, to, false, false, unique_copy);
5442
5443 /* If this substitution failed, this whole thing fails. */
5444 if (GET_CODE (new_rtx) == CLOBBER
5445 && XEXP (new_rtx, 0) == const0_rtx)
5446 return new_rtx;
5447
5448 SUBST (XVECEXP (x, 0, 0), new_rtx);
5449
5450 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5451 {
5452 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5453
5454 if (!REG_P (dest) && GET_CODE (dest) != PC)
5455 {
5456 new_rtx = subst (dest, from, to, false, false, unique_copy);
5457
5458 /* If this substitution failed, this whole thing fails. */
5459 if (GET_CODE (new_rtx) == CLOBBER
5460 && XEXP (new_rtx, 0) == const0_rtx)
5461 return new_rtx;
5462
5463 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5464 }
5465 }
5466 }
5467 else
5468 {
5469 len = GET_RTX_LENGTH (code);
5470 fmt = GET_RTX_FORMAT (code);
5471
5472 /* We don't need to process a SET_DEST that is a register or PC, so
5473 set up to skip this common case. All other cases where we want
5474 to suppress replacing something inside a SET_SRC are handled via
5475 the IN_DEST operand. */
5476 if (code == SET
5477 && (REG_P (SET_DEST (x))
5478 || GET_CODE (SET_DEST (x)) == PC))
5479 fmt = "ie";
5480
5481 /* Trying to simplify the operands of a widening MULT is not likely
5482 to create RTL matching a machine insn. */
5483 if (code == MULT
5484 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5485 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5486 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5487 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5488 && REG_P (XEXP (XEXP (x, 0), 0))
5489 && REG_P (XEXP (XEXP (x, 1), 0))
5490 && from == to)
5491 return x;
5492
5493
5494 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5495 constant. */
5496 if (fmt[0] == 'e')
5497 op0_mode = GET_MODE (XEXP (x, 0));
5498
5499 for (i = 0; i < len; i++)
5500 {
5501 if (fmt[i] == 'E')
5502 {
5503 int j;
5504 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5505 {
5506 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5507 {
5508 new_rtx = (unique_copy && n_occurrences
5509 ? copy_rtx (to) : to);
5510 n_occurrences++;
5511 }
5512 else
5513 {
5514 new_rtx = subst (XVECEXP (x, i, j), from, to,
5515 false, false, unique_copy);
5516
5517 /* If this substitution failed, this whole thing
5518 fails. */
5519 if (GET_CODE (new_rtx) == CLOBBER
5520 && XEXP (new_rtx, 0) == const0_rtx)
5521 return new_rtx;
5522 }
5523
5524 SUBST (XVECEXP (x, i, j), new_rtx);
5525 }
5526 }
5527 else if (fmt[i] == 'e')
5528 {
5529 /* If this is a register being set, ignore it. */
5530 new_rtx = XEXP (x, i);
5531 if (in_dest
5532 && i == 0
5533 && (((code == SUBREG || code == ZERO_EXTRACT)
5534 && REG_P (new_rtx))
5535 || code == STRICT_LOW_PART))
5536 ;
5537
5538 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5539 {
5540 /* In general, don't install a subreg involving two
5541 modes not tieable. It can worsen register
5542 allocation, and can even make invalid reload
5543 insns, since the reg inside may need to be copied
5544 from in the outside mode, and that may be invalid
5545 if it is an fp reg copied in integer mode.
5546
5547 We allow an exception to this: It is valid if
5548 it is inside another SUBREG and the mode of that
5549 SUBREG and the mode of the inside of TO is
5550 tieable. */
5551
5552 if (GET_CODE (to) == SUBREG
5553 && !targetm.modes_tieable_p (GET_MODE (to),
5554 GET_MODE (SUBREG_REG (to)))
5555 && ! (code == SUBREG
5556 && (targetm.modes_tieable_p
5557 (GET_MODE (x), GET_MODE (SUBREG_REG (to))))))
5558 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5559
5560 if (code == SUBREG
5561 && REG_P (to)
5562 && REGNO (to) < FIRST_PSEUDO_REGISTER
5563 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5564 SUBREG_BYTE (x),
5565 GET_MODE (x)) < 0)
5566 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5567
5568 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5569 n_occurrences++;
5570 }
5571 else
5572 /* If we are in a SET_DEST, suppress most cases unless we
5573 have gone inside a MEM, in which case we want to
5574 simplify the address. We assume here that things that
5575 are actually part of the destination have their inner
5576 parts in the first expression. This is true for SUBREG,
5577 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5578 things aside from REG and MEM that should appear in a
5579 SET_DEST. */
5580 new_rtx = subst (XEXP (x, i), from, to,
5581 (((in_dest
5582 && (code == SUBREG || code == STRICT_LOW_PART
5583 || code == ZERO_EXTRACT))
5584 || code == SET)
5585 && i == 0),
5586 code == IF_THEN_ELSE && i == 0,
5587 unique_copy);
5588
5589 /* If we found that we will have to reject this combination,
5590 indicate that by returning the CLOBBER ourselves, rather than
5591 an expression containing it. This will speed things up as
5592 well as prevent accidents where two CLOBBERs are considered
5593 to be equal, thus producing an incorrect simplification. */
5594
5595 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5596 return new_rtx;
5597
5598 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5599 {
5600 machine_mode mode = GET_MODE (x);
5601
5602 x = simplify_subreg (GET_MODE (x), new_rtx,
5603 GET_MODE (SUBREG_REG (x)),
5604 SUBREG_BYTE (x));
5605 if (! x)
5606 x = gen_rtx_CLOBBER (mode, const0_rtx);
5607 }
5608 else if (CONST_SCALAR_INT_P (new_rtx)
5609 && (GET_CODE (x) == ZERO_EXTEND
5610 || GET_CODE (x) == SIGN_EXTEND
5611 || GET_CODE (x) == FLOAT
5612 || GET_CODE (x) == UNSIGNED_FLOAT))
5613 {
5614 x = simplify_unary_operation (GET_CODE (x), GET_MODE (x),
5615 new_rtx,
5616 GET_MODE (XEXP (x, 0)));
5617 if (!x)
5618 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5619 }
5620 /* CONST_INTs shouldn't be substituted into PRE_DEC, PRE_MODIFY
5621 etc. arguments, otherwise we can ICE before trying to recog
5622 it. See PR104446. */
5623 else if (CONST_SCALAR_INT_P (new_rtx)
5624 && GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
5625 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5626 else
5627 SUBST (XEXP (x, i), new_rtx);
5628 }
5629 }
5630 }
5631
5632 /* Check if we are loading something from the constant pool via float
5633 extension; in this case we would undo compress_float_constant
5634 optimization and degenerate constant load to an immediate value. */
5635 if (GET_CODE (x) == FLOAT_EXTEND
5636 && MEM_P (XEXP (x, 0))
5637 && MEM_READONLY_P (XEXP (x, 0)))
5638 {
5639 rtx tmp = avoid_constant_pool_reference (x);
5640 if (x != tmp)
5641 return x;
5642 }
5643
5644 /* Try to simplify X. If the simplification changed the code, it is likely
5645 that further simplification will help, so loop, but limit the number
5646 of repetitions that will be performed. */
5647
5648 for (i = 0; i < 4; i++)
5649 {
5650 /* If X is sufficiently simple, don't bother trying to do anything
5651 with it. */
5652 if (code != CONST_INT && code != REG && code != CLOBBER)
5653 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5654
5655 if (GET_CODE (x) == code)
5656 break;
5657
5658 code = GET_CODE (x);
5659
5660 /* We no longer know the original mode of operand 0 since we
5661 have changed the form of X) */
5662 op0_mode = VOIDmode;
5663 }
5664
5665 return x;
5666 }
5667
5668 /* If X is a commutative operation whose operands are not in the canonical
5669 order, use substitutions to swap them. */
5670
5671 static void
5672 maybe_swap_commutative_operands (rtx x)
5673 {
5674 if (COMMUTATIVE_ARITH_P (x)
5675 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5676 {
5677 rtx temp = XEXP (x, 0);
5678 SUBST (XEXP (x, 0), XEXP (x, 1));
5679 SUBST (XEXP (x, 1), temp);
5680 }
5681
5682 /* Canonicalize (vec_merge (fma op2 op1 op3) op1 mask) to
5683 (vec_merge (fma op1 op2 op3) op1 mask). */
5684 if (GET_CODE (x) == VEC_MERGE
5685 && GET_CODE (XEXP (x, 0)) == FMA)
5686 {
5687 rtx fma_op1 = XEXP (XEXP (x, 0), 0);
5688 rtx fma_op2 = XEXP (XEXP (x, 0), 1);
5689 rtx masked_op = XEXP (x, 1);
5690 if (rtx_equal_p (masked_op, fma_op2))
5691 {
5692 if (GET_CODE (fma_op1) == NEG)
5693 {
5694 /* Keep the negate canonicalized to the first operand. */
5695 fma_op1 = XEXP (fma_op1, 0);
5696 SUBST (XEXP (XEXP (XEXP (x, 0), 0), 0), fma_op2);
5697 SUBST (XEXP (XEXP (x, 0), 1), fma_op1);
5698 }
5699 else
5700 {
5701 SUBST (XEXP (XEXP (x, 0), 0), fma_op2);
5702 SUBST (XEXP (XEXP (x, 0), 1), fma_op1);
5703 }
5704 }
5705 }
5706
5707 unsigned n_elts = 0;
5708 if (GET_CODE (x) == VEC_MERGE
5709 && CONST_INT_P (XEXP (x, 2))
5710 && GET_MODE_NUNITS (GET_MODE (x)).is_constant (&n_elts)
5711 && (swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1))
5712 /* Two operands have same precedence, then
5713 first bit of mask select first operand. */
5714 || (!swap_commutative_operands_p (XEXP (x, 1), XEXP (x, 0))
5715 && !(UINTVAL (XEXP (x, 2)) & 1))))
5716 {
5717 rtx temp = XEXP (x, 0);
5718 unsigned HOST_WIDE_INT sel = UINTVAL (XEXP (x, 2));
5719 unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_1U;
5720 if (n_elts == HOST_BITS_PER_WIDE_INT)
5721 mask = -1;
5722 else
5723 mask = (HOST_WIDE_INT_1U << n_elts) - 1;
5724 SUBST (XEXP (x, 0), XEXP (x, 1));
5725 SUBST (XEXP (x, 1), temp);
5726 SUBST (XEXP (x, 2), GEN_INT (~sel & mask));
5727 }
5728 }
5729
5730 /* Simplify X, a piece of RTL. We just operate on the expression at the
5731 outer level; call `subst' to simplify recursively. Return the new
5732 expression.
5733
5734 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is true
5735 if we are inside a SET_DEST. IN_COND is true if we are at the top level
5736 of a condition. */
5737
5738 static rtx
5739 combine_simplify_rtx (rtx x, machine_mode op0_mode, bool in_dest, bool in_cond)
5740 {
5741 enum rtx_code code = GET_CODE (x);
5742 machine_mode mode = GET_MODE (x);
5743 scalar_int_mode int_mode;
5744 rtx temp;
5745 int i;
5746
5747 /* If this is a commutative operation, put a constant last and a complex
5748 expression first. We don't need to do this for comparisons here. */
5749 maybe_swap_commutative_operands (x);
5750
5751 /* Try to fold this expression in case we have constants that weren't
5752 present before. */
5753 temp = 0;
5754 switch (GET_RTX_CLASS (code))
5755 {
5756 case RTX_UNARY:
5757 if (op0_mode == VOIDmode)
5758 op0_mode = GET_MODE (XEXP (x, 0));
5759 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5760 break;
5761 case RTX_COMPARE:
5762 case RTX_COMM_COMPARE:
5763 {
5764 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5765 if (cmp_mode == VOIDmode)
5766 {
5767 cmp_mode = GET_MODE (XEXP (x, 1));
5768 if (cmp_mode == VOIDmode)
5769 cmp_mode = op0_mode;
5770 }
5771 temp = simplify_relational_operation (code, mode, cmp_mode,
5772 XEXP (x, 0), XEXP (x, 1));
5773 }
5774 break;
5775 case RTX_COMM_ARITH:
5776 case RTX_BIN_ARITH:
5777 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5778 break;
5779 case RTX_BITFIELD_OPS:
5780 case RTX_TERNARY:
5781 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5782 XEXP (x, 1), XEXP (x, 2));
5783 break;
5784 default:
5785 break;
5786 }
5787
5788 if (temp)
5789 {
5790 x = temp;
5791 code = GET_CODE (temp);
5792 op0_mode = VOIDmode;
5793 mode = GET_MODE (temp);
5794 }
5795
5796 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5797 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5798 things. Check for cases where both arms are testing the same
5799 condition.
5800
5801 Don't do anything if all operands are very simple. */
5802
5803 if ((BINARY_P (x)
5804 && ((!OBJECT_P (XEXP (x, 0))
5805 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5806 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5807 || (!OBJECT_P (XEXP (x, 1))
5808 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5809 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5810 || (UNARY_P (x)
5811 && (!OBJECT_P (XEXP (x, 0))
5812 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5813 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5814 {
5815 rtx cond, true_rtx, false_rtx;
5816
5817 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5818 if (cond != 0
5819 /* If everything is a comparison, what we have is highly unlikely
5820 to be simpler, so don't use it. */
5821 && ! (COMPARISON_P (x)
5822 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5823 /* Similarly, if we end up with one of the expressions the same
5824 as the original, it is certainly not simpler. */
5825 && ! rtx_equal_p (x, true_rtx)
5826 && ! rtx_equal_p (x, false_rtx))
5827 {
5828 rtx cop1 = const0_rtx;
5829 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5830
5831 if (cond_code == NE && COMPARISON_P (cond))
5832 return x;
5833
5834 /* Simplify the alternative arms; this may collapse the true and
5835 false arms to store-flag values. Be careful to use copy_rtx
5836 here since true_rtx or false_rtx might share RTL with x as a
5837 result of the if_then_else_cond call above. */
5838 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx,
5839 false, false, false);
5840 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx,
5841 false, false, false);
5842
5843 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5844 is unlikely to be simpler. */
5845 if (general_operand (true_rtx, VOIDmode)
5846 && general_operand (false_rtx, VOIDmode))
5847 {
5848 enum rtx_code reversed;
5849
5850 /* Restarting if we generate a store-flag expression will cause
5851 us to loop. Just drop through in this case. */
5852
5853 /* If the result values are STORE_FLAG_VALUE and zero, we can
5854 just make the comparison operation. */
5855 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5856 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5857 cond, cop1);
5858 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5859 && ((reversed = reversed_comparison_code_parts
5860 (cond_code, cond, cop1, NULL))
5861 != UNKNOWN))
5862 x = simplify_gen_relational (reversed, mode, VOIDmode,
5863 cond, cop1);
5864
5865 /* Likewise, we can make the negate of a comparison operation
5866 if the result values are - STORE_FLAG_VALUE and zero. */
5867 else if (CONST_INT_P (true_rtx)
5868 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5869 && false_rtx == const0_rtx)
5870 x = simplify_gen_unary (NEG, mode,
5871 simplify_gen_relational (cond_code,
5872 mode, VOIDmode,
5873 cond, cop1),
5874 mode);
5875 else if (CONST_INT_P (false_rtx)
5876 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5877 && true_rtx == const0_rtx
5878 && ((reversed = reversed_comparison_code_parts
5879 (cond_code, cond, cop1, NULL))
5880 != UNKNOWN))
5881 x = simplify_gen_unary (NEG, mode,
5882 simplify_gen_relational (reversed,
5883 mode, VOIDmode,
5884 cond, cop1),
5885 mode);
5886
5887 code = GET_CODE (x);
5888 op0_mode = VOIDmode;
5889 }
5890 }
5891 }
5892
5893 /* First see if we can apply the inverse distributive law. */
5894 if (code == PLUS || code == MINUS
5895 || code == AND || code == IOR || code == XOR)
5896 {
5897 x = apply_distributive_law (x);
5898 code = GET_CODE (x);
5899 op0_mode = VOIDmode;
5900 }
5901
5902 /* If CODE is an associative operation not otherwise handled, see if we
5903 can associate some operands. This can win if they are constants or
5904 if they are logically related (i.e. (a & b) & a). */
5905 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5906 || code == AND || code == IOR || code == XOR
5907 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5908 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5909 || (flag_associative_math && FLOAT_MODE_P (mode))))
5910 {
5911 if (GET_CODE (XEXP (x, 0)) == code)
5912 {
5913 rtx other = XEXP (XEXP (x, 0), 0);
5914 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5915 rtx inner_op1 = XEXP (x, 1);
5916 rtx inner;
5917
5918 /* Make sure we pass the constant operand if any as the second
5919 one if this is a commutative operation. */
5920 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5921 std::swap (inner_op0, inner_op1);
5922 inner = simplify_binary_operation (code == MINUS ? PLUS
5923 : code == DIV ? MULT
5924 : code,
5925 mode, inner_op0, inner_op1);
5926
5927 /* For commutative operations, try the other pair if that one
5928 didn't simplify. */
5929 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5930 {
5931 other = XEXP (XEXP (x, 0), 1);
5932 inner = simplify_binary_operation (code, mode,
5933 XEXP (XEXP (x, 0), 0),
5934 XEXP (x, 1));
5935 }
5936
5937 if (inner)
5938 return simplify_gen_binary (code, mode, other, inner);
5939 }
5940 }
5941
5942 /* A little bit of algebraic simplification here. */
5943 switch (code)
5944 {
5945 case MEM:
5946 /* Ensure that our address has any ASHIFTs converted to MULT in case
5947 address-recognizing predicates are called later. */
5948 temp = make_compound_operation (XEXP (x, 0), MEM);
5949 SUBST (XEXP (x, 0), temp);
5950 break;
5951
5952 case SUBREG:
5953 if (op0_mode == VOIDmode)
5954 op0_mode = GET_MODE (SUBREG_REG (x));
5955
5956 /* See if this can be moved to simplify_subreg. */
5957 if (CONSTANT_P (SUBREG_REG (x))
5958 && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5959 /* Don't call gen_lowpart if the inner mode
5960 is VOIDmode and we cannot simplify it, as SUBREG without
5961 inner mode is invalid. */
5962 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5963 || gen_lowpart_common (mode, SUBREG_REG (x))))
5964 return gen_lowpart (mode, SUBREG_REG (x));
5965
5966 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5967 break;
5968 {
5969 rtx temp;
5970 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5971 SUBREG_BYTE (x));
5972 if (temp)
5973 return temp;
5974
5975 /* If op is known to have all lower bits zero, the result is zero. */
5976 scalar_int_mode int_mode, int_op0_mode;
5977 if (!in_dest
5978 && is_a <scalar_int_mode> (mode, &int_mode)
5979 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5980 && (GET_MODE_PRECISION (int_mode)
5981 < GET_MODE_PRECISION (int_op0_mode))
5982 && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
5983 SUBREG_BYTE (x))
5984 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5985 && ((nonzero_bits (SUBREG_REG (x), int_op0_mode)
5986 & GET_MODE_MASK (int_mode)) == 0)
5987 && !side_effects_p (SUBREG_REG (x)))
5988 return CONST0_RTX (int_mode);
5989 }
5990
5991 /* Don't change the mode of the MEM if that would change the meaning
5992 of the address. */
5993 if (MEM_P (SUBREG_REG (x))
5994 && (MEM_VOLATILE_P (SUBREG_REG (x))
5995 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5996 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5997 return gen_rtx_CLOBBER (mode, const0_rtx);
5998
5999 /* Note that we cannot do any narrowing for non-constants since
6000 we might have been counting on using the fact that some bits were
6001 zero. We now do this in the SET. */
6002
6003 break;
6004
6005 case NEG:
6006 temp = expand_compound_operation (XEXP (x, 0));
6007
6008 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
6009 replaced by (lshiftrt X C). This will convert
6010 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
6011
6012 if (GET_CODE (temp) == ASHIFTRT
6013 && CONST_INT_P (XEXP (temp, 1))
6014 && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
6015 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
6016 INTVAL (XEXP (temp, 1)));
6017
6018 /* If X has only a single bit that might be nonzero, say, bit I, convert
6019 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
6020 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
6021 (sign_extract X 1 Y). But only do this if TEMP isn't a register
6022 or a SUBREG of one since we'd be making the expression more
6023 complex if it was just a register. */
6024
6025 if (!REG_P (temp)
6026 && ! (GET_CODE (temp) == SUBREG
6027 && REG_P (SUBREG_REG (temp)))
6028 && is_a <scalar_int_mode> (mode, &int_mode)
6029 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
6030 {
6031 rtx temp1 = simplify_shift_const
6032 (NULL_RTX, ASHIFTRT, int_mode,
6033 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
6034 GET_MODE_PRECISION (int_mode) - 1 - i),
6035 GET_MODE_PRECISION (int_mode) - 1 - i);
6036
6037 /* If all we did was surround TEMP with the two shifts, we
6038 haven't improved anything, so don't use it. Otherwise,
6039 we are better off with TEMP1. */
6040 if (GET_CODE (temp1) != ASHIFTRT
6041 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
6042 || XEXP (XEXP (temp1, 0), 0) != temp)
6043 return temp1;
6044 }
6045 break;
6046
6047 case TRUNCATE:
6048 /* We can't handle truncation to a partial integer mode here
6049 because we don't know the real bitsize of the partial
6050 integer mode. */
6051 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6052 break;
6053
6054 if (HWI_COMPUTABLE_MODE_P (mode))
6055 SUBST (XEXP (x, 0),
6056 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6057 GET_MODE_MASK (mode), false));
6058
6059 /* We can truncate a constant value and return it. */
6060 {
6061 poly_int64 c;
6062 if (poly_int_rtx_p (XEXP (x, 0), &c))
6063 return gen_int_mode (c, mode);
6064 }
6065
6066 /* Similarly to what we do in simplify-rtx.cc, a truncate of a register
6067 whose value is a comparison can be replaced with a subreg if
6068 STORE_FLAG_VALUE permits. */
6069 if (HWI_COMPUTABLE_MODE_P (mode)
6070 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
6071 && (temp = get_last_value (XEXP (x, 0)))
6072 && COMPARISON_P (temp)
6073 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (XEXP (x, 0))))
6074 return gen_lowpart (mode, XEXP (x, 0));
6075 break;
6076
6077 case CONST:
6078 /* (const (const X)) can become (const X). Do it this way rather than
6079 returning the inner CONST since CONST can be shared with a
6080 REG_EQUAL note. */
6081 if (GET_CODE (XEXP (x, 0)) == CONST)
6082 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
6083 break;
6084
6085 case LO_SUM:
6086 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
6087 can add in an offset. find_split_point will split this address up
6088 again if it doesn't match. */
6089 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6090 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6091 return XEXP (x, 1);
6092 break;
6093
6094 case PLUS:
6095 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6096 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6097 bit-field and can be replaced by either a sign_extend or a
6098 sign_extract. The `and' may be a zero_extend and the two
6099 <c>, -<c> constants may be reversed. */
6100 if (GET_CODE (XEXP (x, 0)) == XOR
6101 && is_a <scalar_int_mode> (mode, &int_mode)
6102 && CONST_INT_P (XEXP (x, 1))
6103 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6104 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
6105 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
6106 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
6107 && HWI_COMPUTABLE_MODE_P (int_mode)
6108 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6109 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6110 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6111 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6112 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6113 && known_eq ((GET_MODE_PRECISION
6114 (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6115 (unsigned int) i + 1))))
6116 return simplify_shift_const
6117 (NULL_RTX, ASHIFTRT, int_mode,
6118 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6119 XEXP (XEXP (XEXP (x, 0), 0), 0),
6120 GET_MODE_PRECISION (int_mode) - (i + 1)),
6121 GET_MODE_PRECISION (int_mode) - (i + 1));
6122
6123 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6124 can become (ashiftrt (ashift (xor x 1) C) C) where C is
6125 the bitsize of the mode - 1. This allows simplification of
6126 "a = (b & 8) == 0;" */
6127 if (XEXP (x, 1) == constm1_rtx
6128 && !REG_P (XEXP (x, 0))
6129 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6130 && REG_P (SUBREG_REG (XEXP (x, 0))))
6131 && is_a <scalar_int_mode> (mode, &int_mode)
6132 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6133 return simplify_shift_const
6134 (NULL_RTX, ASHIFTRT, int_mode,
6135 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6136 gen_rtx_XOR (int_mode, XEXP (x, 0),
6137 const1_rtx),
6138 GET_MODE_PRECISION (int_mode) - 1),
6139 GET_MODE_PRECISION (int_mode) - 1);
6140
6141 /* If we are adding two things that have no bits in common, convert
6142 the addition into an IOR. This will often be further simplified,
6143 for example in cases like ((a & 1) + (a & 2)), which can
6144 become a & 3. */
6145
6146 if (HWI_COMPUTABLE_MODE_P (mode)
6147 && (nonzero_bits (XEXP (x, 0), mode)
6148 & nonzero_bits (XEXP (x, 1), mode)) == 0)
6149 {
6150 /* Try to simplify the expression further. */
6151 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6152 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, false);
6153
6154 /* If we could, great. If not, do not go ahead with the IOR
6155 replacement, since PLUS appears in many special purpose
6156 address arithmetic instructions. */
6157 if (GET_CODE (temp) != CLOBBER
6158 && (GET_CODE (temp) != IOR
6159 || ((XEXP (temp, 0) != XEXP (x, 0)
6160 || XEXP (temp, 1) != XEXP (x, 1))
6161 && (XEXP (temp, 0) != XEXP (x, 1)
6162 || XEXP (temp, 1) != XEXP (x, 0)))))
6163 return temp;
6164 }
6165
6166 /* Canonicalize x + x into x << 1. */
6167 if (GET_MODE_CLASS (mode) == MODE_INT
6168 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6169 && !side_effects_p (XEXP (x, 0)))
6170 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6171
6172 break;
6173
6174 case MINUS:
6175 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6176 (and <foo> (const_int pow2-1)) */
6177 if (is_a <scalar_int_mode> (mode, &int_mode)
6178 && GET_CODE (XEXP (x, 1)) == AND
6179 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6180 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6181 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6182 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6183 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6184 break;
6185
6186 case MULT:
6187 /* If we have (mult (plus A B) C), apply the distributive law and then
6188 the inverse distributive law to see if things simplify. This
6189 occurs mostly in addresses, often when unrolling loops. */
6190
6191 if (GET_CODE (XEXP (x, 0)) == PLUS)
6192 {
6193 rtx result = distribute_and_simplify_rtx (x, 0);
6194 if (result)
6195 return result;
6196 }
6197
6198 /* Try simplify a*(b/c) as (a*b)/c. */
6199 if (FLOAT_MODE_P (mode) && flag_associative_math
6200 && GET_CODE (XEXP (x, 0)) == DIV)
6201 {
6202 rtx tem = simplify_binary_operation (MULT, mode,
6203 XEXP (XEXP (x, 0), 0),
6204 XEXP (x, 1));
6205 if (tem)
6206 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6207 }
6208 break;
6209
6210 case UDIV:
6211 /* If this is a divide by a power of two, treat it as a shift if
6212 its first operand is a shift. */
6213 if (is_a <scalar_int_mode> (mode, &int_mode)
6214 && CONST_INT_P (XEXP (x, 1))
6215 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6216 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6217 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6218 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6219 || GET_CODE (XEXP (x, 0)) == ROTATE
6220 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6221 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6222 XEXP (x, 0), i);
6223 break;
6224
6225 case EQ: case NE:
6226 case GT: case GTU: case GE: case GEU:
6227 case LT: case LTU: case LE: case LEU:
6228 case UNEQ: case LTGT:
6229 case UNGT: case UNGE:
6230 case UNLT: case UNLE:
6231 case UNORDERED: case ORDERED:
6232 /* If the first operand is a condition code, we can't do anything
6233 with it. */
6234 if (GET_CODE (XEXP (x, 0)) == COMPARE
6235 || GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC)
6236 {
6237 rtx op0 = XEXP (x, 0);
6238 rtx op1 = XEXP (x, 1);
6239 enum rtx_code new_code;
6240
6241 if (GET_CODE (op0) == COMPARE)
6242 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6243
6244 /* Simplify our comparison, if possible. */
6245 new_code = simplify_comparison (code, &op0, &op1);
6246
6247 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6248 if only the low-order bit is possibly nonzero in X (such as when
6249 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6250 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6251 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6252 (plus X 1).
6253
6254 Remove any ZERO_EXTRACT we made when thinking this was a
6255 comparison. It may now be simpler to use, e.g., an AND. If a
6256 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6257 the call to make_compound_operation in the SET case.
6258
6259 Don't apply these optimizations if the caller would
6260 prefer a comparison rather than a value.
6261 E.g., for the condition in an IF_THEN_ELSE most targets need
6262 an explicit comparison. */
6263
6264 if (in_cond)
6265 ;
6266
6267 else if (STORE_FLAG_VALUE == 1
6268 && new_code == NE
6269 && is_int_mode (mode, &int_mode)
6270 && op1 == const0_rtx
6271 && int_mode == GET_MODE (op0)
6272 && nonzero_bits (op0, int_mode) == 1)
6273 return gen_lowpart (int_mode,
6274 expand_compound_operation (op0));
6275
6276 else if (STORE_FLAG_VALUE == 1
6277 && new_code == NE
6278 && is_int_mode (mode, &int_mode)
6279 && op1 == const0_rtx
6280 && int_mode == GET_MODE (op0)
6281 && (num_sign_bit_copies (op0, int_mode)
6282 == GET_MODE_PRECISION (int_mode)))
6283 {
6284 op0 = expand_compound_operation (op0);
6285 return simplify_gen_unary (NEG, int_mode,
6286 gen_lowpart (int_mode, op0),
6287 int_mode);
6288 }
6289
6290 else if (STORE_FLAG_VALUE == 1
6291 && new_code == EQ
6292 && is_int_mode (mode, &int_mode)
6293 && op1 == const0_rtx
6294 && int_mode == GET_MODE (op0)
6295 && nonzero_bits (op0, int_mode) == 1)
6296 {
6297 op0 = expand_compound_operation (op0);
6298 return simplify_gen_binary (XOR, int_mode,
6299 gen_lowpart (int_mode, op0),
6300 const1_rtx);
6301 }
6302
6303 else if (STORE_FLAG_VALUE == 1
6304 && new_code == EQ
6305 && is_int_mode (mode, &int_mode)
6306 && op1 == const0_rtx
6307 && int_mode == GET_MODE (op0)
6308 && (num_sign_bit_copies (op0, int_mode)
6309 == GET_MODE_PRECISION (int_mode)))
6310 {
6311 op0 = expand_compound_operation (op0);
6312 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6313 }
6314
6315 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6316 those above. */
6317 if (in_cond)
6318 ;
6319
6320 else if (STORE_FLAG_VALUE == -1
6321 && new_code == NE
6322 && is_int_mode (mode, &int_mode)
6323 && op1 == const0_rtx
6324 && int_mode == GET_MODE (op0)
6325 && (num_sign_bit_copies (op0, int_mode)
6326 == GET_MODE_PRECISION (int_mode)))
6327 return gen_lowpart (int_mode, expand_compound_operation (op0));
6328
6329 else if (STORE_FLAG_VALUE == -1
6330 && new_code == NE
6331 && is_int_mode (mode, &int_mode)
6332 && op1 == const0_rtx
6333 && int_mode == GET_MODE (op0)
6334 && nonzero_bits (op0, int_mode) == 1)
6335 {
6336 op0 = expand_compound_operation (op0);
6337 return simplify_gen_unary (NEG, int_mode,
6338 gen_lowpart (int_mode, op0),
6339 int_mode);
6340 }
6341
6342 else if (STORE_FLAG_VALUE == -1
6343 && new_code == EQ
6344 && is_int_mode (mode, &int_mode)
6345 && op1 == const0_rtx
6346 && int_mode == GET_MODE (op0)
6347 && (num_sign_bit_copies (op0, int_mode)
6348 == GET_MODE_PRECISION (int_mode)))
6349 {
6350 op0 = expand_compound_operation (op0);
6351 return simplify_gen_unary (NOT, int_mode,
6352 gen_lowpart (int_mode, op0),
6353 int_mode);
6354 }
6355
6356 /* If X is 0/1, (eq X 0) is X-1. */
6357 else if (STORE_FLAG_VALUE == -1
6358 && new_code == EQ
6359 && is_int_mode (mode, &int_mode)
6360 && op1 == const0_rtx
6361 && int_mode == GET_MODE (op0)
6362 && nonzero_bits (op0, int_mode) == 1)
6363 {
6364 op0 = expand_compound_operation (op0);
6365 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6366 }
6367
6368 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6369 one bit that might be nonzero, we can convert (ne x 0) to
6370 (ashift x c) where C puts the bit in the sign bit. Remove any
6371 AND with STORE_FLAG_VALUE when we are done, since we are only
6372 going to test the sign bit. */
6373 if (new_code == NE
6374 && is_int_mode (mode, &int_mode)
6375 && HWI_COMPUTABLE_MODE_P (int_mode)
6376 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6377 && op1 == const0_rtx
6378 && int_mode == GET_MODE (op0)
6379 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6380 {
6381 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6382 expand_compound_operation (op0),
6383 GET_MODE_PRECISION (int_mode) - 1 - i);
6384 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6385 return XEXP (x, 0);
6386 else
6387 return x;
6388 }
6389
6390 /* If the code changed, return a whole new comparison.
6391 We also need to avoid using SUBST in cases where
6392 simplify_comparison has widened a comparison with a CONST_INT,
6393 since in that case the wider CONST_INT may fail the sanity
6394 checks in do_SUBST. */
6395 if (new_code != code
6396 || (CONST_INT_P (op1)
6397 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6398 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6399 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6400
6401 /* Otherwise, keep this operation, but maybe change its operands.
6402 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6403 SUBST (XEXP (x, 0), op0);
6404 SUBST (XEXP (x, 1), op1);
6405 }
6406 break;
6407
6408 case IF_THEN_ELSE:
6409 return simplify_if_then_else (x);
6410
6411 case ZERO_EXTRACT:
6412 case SIGN_EXTRACT:
6413 case ZERO_EXTEND:
6414 case SIGN_EXTEND:
6415 /* If we are processing SET_DEST, we are done. */
6416 if (in_dest)
6417 return x;
6418
6419 return expand_compound_operation (x);
6420
6421 case SET:
6422 return simplify_set (x);
6423
6424 case AND:
6425 case IOR:
6426 return simplify_logical (x);
6427
6428 case ASHIFT:
6429 case LSHIFTRT:
6430 case ASHIFTRT:
6431 case ROTATE:
6432 case ROTATERT:
6433 /* If this is a shift by a constant amount, simplify it. */
6434 if (CONST_INT_P (XEXP (x, 1)))
6435 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6436 INTVAL (XEXP (x, 1)));
6437
6438 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6439 SUBST (XEXP (x, 1),
6440 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6441 (HOST_WIDE_INT_1U
6442 << exact_log2 (GET_MODE_UNIT_BITSIZE
6443 (GET_MODE (x)))) - 1, false));
6444 break;
6445 case VEC_SELECT:
6446 {
6447 rtx trueop0 = XEXP (x, 0);
6448 mode = GET_MODE (trueop0);
6449 rtx trueop1 = XEXP (x, 1);
6450 /* If we select a low-part subreg, return that. */
6451 if (vec_series_lowpart_p (GET_MODE (x), mode, trueop1))
6452 {
6453 rtx new_rtx = lowpart_subreg (GET_MODE (x), trueop0, mode);
6454 if (new_rtx != NULL_RTX)
6455 return new_rtx;
6456 }
6457 }
6458
6459 default:
6460 break;
6461 }
6462
6463 return x;
6464 }
6465
6466 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6467
6468 static rtx
6469 simplify_if_then_else (rtx x)
6470 {
6471 machine_mode mode = GET_MODE (x);
6472 rtx cond = XEXP (x, 0);
6473 rtx true_rtx = XEXP (x, 1);
6474 rtx false_rtx = XEXP (x, 2);
6475 enum rtx_code true_code = GET_CODE (cond);
6476 bool comparison_p = COMPARISON_P (cond);
6477 rtx temp;
6478 int i;
6479 enum rtx_code false_code;
6480 rtx reversed;
6481 scalar_int_mode int_mode, inner_mode;
6482
6483 /* Simplify storing of the truth value. */
6484 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6485 return simplify_gen_relational (true_code, mode, VOIDmode,
6486 XEXP (cond, 0), XEXP (cond, 1));
6487
6488 /* Also when the truth value has to be reversed. */
6489 if (comparison_p
6490 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6491 && (reversed = reversed_comparison (cond, mode)))
6492 return reversed;
6493
6494 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6495 in it is being compared against certain values. Get the true and false
6496 comparisons and see if that says anything about the value of each arm. */
6497
6498 if (comparison_p
6499 && ((false_code = reversed_comparison_code (cond, NULL))
6500 != UNKNOWN)
6501 && REG_P (XEXP (cond, 0)))
6502 {
6503 HOST_WIDE_INT nzb;
6504 rtx from = XEXP (cond, 0);
6505 rtx true_val = XEXP (cond, 1);
6506 rtx false_val = true_val;
6507 bool swapped = false;
6508
6509 /* If FALSE_CODE is EQ, swap the codes and arms. */
6510
6511 if (false_code == EQ)
6512 {
6513 swapped = true, true_code = EQ, false_code = NE;
6514 std::swap (true_rtx, false_rtx);
6515 }
6516
6517 scalar_int_mode from_mode;
6518 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6519 {
6520 /* If we are comparing against zero and the expression being
6521 tested has only a single bit that might be nonzero, that is
6522 its value when it is not equal to zero. Similarly if it is
6523 known to be -1 or 0. */
6524 if (true_code == EQ
6525 && true_val == const0_rtx
6526 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6527 {
6528 false_code = EQ;
6529 false_val = gen_int_mode (nzb, from_mode);
6530 }
6531 else if (true_code == EQ
6532 && true_val == const0_rtx
6533 && (num_sign_bit_copies (from, from_mode)
6534 == GET_MODE_PRECISION (from_mode)))
6535 {
6536 false_code = EQ;
6537 false_val = constm1_rtx;
6538 }
6539 }
6540
6541 /* Now simplify an arm if we know the value of the register in the
6542 branch and it is used in the arm. Be careful due to the potential
6543 of locally-shared RTL. */
6544
6545 if (reg_mentioned_p (from, true_rtx))
6546 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6547 from, true_val),
6548 pc_rtx, pc_rtx, false, false, false);
6549 if (reg_mentioned_p (from, false_rtx))
6550 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6551 from, false_val),
6552 pc_rtx, pc_rtx, false, false, false);
6553
6554 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6555 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6556
6557 true_rtx = XEXP (x, 1);
6558 false_rtx = XEXP (x, 2);
6559 true_code = GET_CODE (cond);
6560 }
6561
6562 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6563 reversed, do so to avoid needing two sets of patterns for
6564 subtract-and-branch insns. Similarly if we have a constant in the true
6565 arm, the false arm is the same as the first operand of the comparison, or
6566 the false arm is more complicated than the true arm. */
6567
6568 if (comparison_p
6569 && reversed_comparison_code (cond, NULL) != UNKNOWN
6570 && (true_rtx == pc_rtx
6571 || (CONSTANT_P (true_rtx)
6572 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6573 || true_rtx == const0_rtx
6574 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6575 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6576 && !OBJECT_P (false_rtx))
6577 || reg_mentioned_p (true_rtx, false_rtx)
6578 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6579 {
6580 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6581 SUBST (XEXP (x, 1), false_rtx);
6582 SUBST (XEXP (x, 2), true_rtx);
6583
6584 std::swap (true_rtx, false_rtx);
6585 cond = XEXP (x, 0);
6586
6587 /* It is possible that the conditional has been simplified out. */
6588 true_code = GET_CODE (cond);
6589 comparison_p = COMPARISON_P (cond);
6590 }
6591
6592 /* If the two arms are identical, we don't need the comparison. */
6593
6594 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6595 return true_rtx;
6596
6597 /* Convert a == b ? b : a to "a". */
6598 if (true_code == EQ && ! side_effects_p (cond)
6599 && !HONOR_NANS (mode)
6600 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6601 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6602 return false_rtx;
6603 else if (true_code == NE && ! side_effects_p (cond)
6604 && !HONOR_NANS (mode)
6605 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6606 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6607 return true_rtx;
6608
6609 /* Look for cases where we have (abs x) or (neg (abs X)). */
6610
6611 if (GET_MODE_CLASS (mode) == MODE_INT
6612 && comparison_p
6613 && XEXP (cond, 1) == const0_rtx
6614 && GET_CODE (false_rtx) == NEG
6615 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6616 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6617 && ! side_effects_p (true_rtx))
6618 switch (true_code)
6619 {
6620 case GT:
6621 case GE:
6622 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6623 case LT:
6624 case LE:
6625 return
6626 simplify_gen_unary (NEG, mode,
6627 simplify_gen_unary (ABS, mode, true_rtx, mode),
6628 mode);
6629 default:
6630 break;
6631 }
6632
6633 /* Look for MIN or MAX. */
6634
6635 if ((! FLOAT_MODE_P (mode)
6636 || (flag_unsafe_math_optimizations
6637 && !HONOR_NANS (mode)
6638 && !HONOR_SIGNED_ZEROS (mode)))
6639 && comparison_p
6640 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6641 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6642 && ! side_effects_p (cond))
6643 switch (true_code)
6644 {
6645 case GE:
6646 case GT:
6647 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6648 case LE:
6649 case LT:
6650 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6651 case GEU:
6652 case GTU:
6653 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6654 case LEU:
6655 case LTU:
6656 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6657 default:
6658 break;
6659 }
6660
6661 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6662 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6663 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6664 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6665 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6666 neither 1 or -1, but it isn't worth checking for. */
6667
6668 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6669 && comparison_p
6670 && is_int_mode (mode, &int_mode)
6671 && ! side_effects_p (x))
6672 {
6673 rtx t = make_compound_operation (true_rtx, SET);
6674 rtx f = make_compound_operation (false_rtx, SET);
6675 rtx cond_op0 = XEXP (cond, 0);
6676 rtx cond_op1 = XEXP (cond, 1);
6677 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6678 scalar_int_mode m = int_mode;
6679 rtx z = 0, c1 = NULL_RTX;
6680
6681 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6682 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6683 || GET_CODE (t) == ASHIFT
6684 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6685 && rtx_equal_p (XEXP (t, 0), f))
6686 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6687
6688 /* If an identity-zero op is commutative, check whether there
6689 would be a match if we swapped the operands. */
6690 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6691 || GET_CODE (t) == XOR)
6692 && rtx_equal_p (XEXP (t, 1), f))
6693 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6694 else if (GET_CODE (t) == SIGN_EXTEND
6695 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6696 && (GET_CODE (XEXP (t, 0)) == PLUS
6697 || GET_CODE (XEXP (t, 0)) == MINUS
6698 || GET_CODE (XEXP (t, 0)) == IOR
6699 || GET_CODE (XEXP (t, 0)) == XOR
6700 || GET_CODE (XEXP (t, 0)) == ASHIFT
6701 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6702 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6703 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6704 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6705 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6706 && (num_sign_bit_copies (f, GET_MODE (f))
6707 > (unsigned int)
6708 (GET_MODE_PRECISION (int_mode)
6709 - GET_MODE_PRECISION (inner_mode))))
6710 {
6711 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6712 extend_op = SIGN_EXTEND;
6713 m = inner_mode;
6714 }
6715 else if (GET_CODE (t) == SIGN_EXTEND
6716 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6717 && (GET_CODE (XEXP (t, 0)) == PLUS
6718 || GET_CODE (XEXP (t, 0)) == IOR
6719 || GET_CODE (XEXP (t, 0)) == XOR)
6720 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6721 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6722 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6723 && (num_sign_bit_copies (f, GET_MODE (f))
6724 > (unsigned int)
6725 (GET_MODE_PRECISION (int_mode)
6726 - GET_MODE_PRECISION (inner_mode))))
6727 {
6728 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6729 extend_op = SIGN_EXTEND;
6730 m = inner_mode;
6731 }
6732 else if (GET_CODE (t) == ZERO_EXTEND
6733 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6734 && (GET_CODE (XEXP (t, 0)) == PLUS
6735 || GET_CODE (XEXP (t, 0)) == MINUS
6736 || GET_CODE (XEXP (t, 0)) == IOR
6737 || GET_CODE (XEXP (t, 0)) == XOR
6738 || GET_CODE (XEXP (t, 0)) == ASHIFT
6739 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6740 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6741 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6742 && HWI_COMPUTABLE_MODE_P (int_mode)
6743 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6744 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6745 && ((nonzero_bits (f, GET_MODE (f))
6746 & ~GET_MODE_MASK (inner_mode))
6747 == 0))
6748 {
6749 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6750 extend_op = ZERO_EXTEND;
6751 m = inner_mode;
6752 }
6753 else if (GET_CODE (t) == ZERO_EXTEND
6754 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6755 && (GET_CODE (XEXP (t, 0)) == PLUS
6756 || GET_CODE (XEXP (t, 0)) == IOR
6757 || GET_CODE (XEXP (t, 0)) == XOR)
6758 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6759 && HWI_COMPUTABLE_MODE_P (int_mode)
6760 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6761 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6762 && ((nonzero_bits (f, GET_MODE (f))
6763 & ~GET_MODE_MASK (inner_mode))
6764 == 0))
6765 {
6766 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6767 extend_op = ZERO_EXTEND;
6768 m = inner_mode;
6769 }
6770
6771 if (z)
6772 {
6773 machine_mode cm = m;
6774 if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6775 && GET_MODE (c1) != VOIDmode)
6776 cm = GET_MODE (c1);
6777 temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6778 cond_op0, cond_op1),
6779 pc_rtx, pc_rtx, false, false, false);
6780 temp = simplify_gen_binary (MULT, cm, temp,
6781 simplify_gen_binary (MULT, cm, c1,
6782 const_true_rtx));
6783 temp = subst (temp, pc_rtx, pc_rtx, false, false, false);
6784 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6785
6786 if (extend_op != UNKNOWN)
6787 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6788
6789 return temp;
6790 }
6791 }
6792
6793 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6794 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6795 negation of a single bit, we can convert this operation to a shift. We
6796 can actually do this more generally, but it doesn't seem worth it. */
6797
6798 if (true_code == NE
6799 && is_a <scalar_int_mode> (mode, &int_mode)
6800 && XEXP (cond, 1) == const0_rtx
6801 && false_rtx == const0_rtx
6802 && CONST_INT_P (true_rtx)
6803 && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6804 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6805 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6806 == GET_MODE_PRECISION (int_mode))
6807 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6808 return
6809 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6810 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6811
6812 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6813 non-zero bit in A is C1. */
6814 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6815 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6816 && is_a <scalar_int_mode> (mode, &int_mode)
6817 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6818 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6819 == nonzero_bits (XEXP (cond, 0), inner_mode)
6820 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6821 {
6822 rtx val = XEXP (cond, 0);
6823 if (inner_mode == int_mode)
6824 return val;
6825 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6826 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6827 }
6828
6829 return x;
6830 }
6831
6832 /* Simplify X, a SET expression. Return the new expression. */
6833
6834 static rtx
6835 simplify_set (rtx x)
6836 {
6837 rtx src = SET_SRC (x);
6838 rtx dest = SET_DEST (x);
6839 machine_mode mode
6840 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6841 rtx_insn *other_insn;
6842 rtx *cc_use;
6843 scalar_int_mode int_mode;
6844
6845 /* (set (pc) (return)) gets written as (return). */
6846 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6847 return src;
6848
6849 /* Now that we know for sure which bits of SRC we are using, see if we can
6850 simplify the expression for the object knowing that we only need the
6851 low-order bits. */
6852
6853 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6854 {
6855 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, false);
6856 SUBST (SET_SRC (x), src);
6857 }
6858
6859 /* If the source is a COMPARE, look for the use of the comparison result
6860 and try to simplify it unless we already have used undobuf.other_insn. */
6861 if ((GET_MODE_CLASS (mode) == MODE_CC || GET_CODE (src) == COMPARE)
6862 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6863 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6864 && COMPARISON_P (*cc_use)
6865 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6866 {
6867 enum rtx_code old_code = GET_CODE (*cc_use);
6868 enum rtx_code new_code;
6869 rtx op0, op1, tmp;
6870 bool other_changed = false;
6871 rtx inner_compare = NULL_RTX;
6872 machine_mode compare_mode = GET_MODE (dest);
6873
6874 if (GET_CODE (src) == COMPARE)
6875 {
6876 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6877 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6878 {
6879 inner_compare = op0;
6880 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6881 }
6882 }
6883 else
6884 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6885
6886 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6887 op0, op1);
6888 if (!tmp)
6889 new_code = old_code;
6890 else if (!CONSTANT_P (tmp))
6891 {
6892 new_code = GET_CODE (tmp);
6893 op0 = XEXP (tmp, 0);
6894 op1 = XEXP (tmp, 1);
6895 }
6896 else
6897 {
6898 rtx pat = PATTERN (other_insn);
6899 undobuf.other_insn = other_insn;
6900 SUBST (*cc_use, tmp);
6901
6902 /* Attempt to simplify CC user. */
6903 if (GET_CODE (pat) == SET)
6904 {
6905 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6906 if (new_rtx != NULL_RTX)
6907 SUBST (SET_SRC (pat), new_rtx);
6908 }
6909
6910 /* Convert X into a no-op move. */
6911 SUBST (SET_DEST (x), pc_rtx);
6912 SUBST (SET_SRC (x), pc_rtx);
6913 return x;
6914 }
6915
6916 /* Simplify our comparison, if possible. */
6917 new_code = simplify_comparison (new_code, &op0, &op1);
6918
6919 #ifdef SELECT_CC_MODE
6920 /* If this machine has CC modes other than CCmode, check to see if we
6921 need to use a different CC mode here. */
6922 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6923 compare_mode = GET_MODE (op0);
6924 else if (inner_compare
6925 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6926 && new_code == old_code
6927 && op0 == XEXP (inner_compare, 0)
6928 && op1 == XEXP (inner_compare, 1))
6929 compare_mode = GET_MODE (inner_compare);
6930 else
6931 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6932
6933 /* If the mode changed, we have to change SET_DEST, the mode in the
6934 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6935 a hard register, just build new versions with the proper mode. If it
6936 is a pseudo, we lose unless it is only time we set the pseudo, in
6937 which case we can safely change its mode. */
6938 if (compare_mode != GET_MODE (dest))
6939 {
6940 if (can_change_dest_mode (dest, 0, compare_mode))
6941 {
6942 unsigned int regno = REGNO (dest);
6943 rtx new_dest;
6944
6945 if (regno < FIRST_PSEUDO_REGISTER)
6946 new_dest = gen_rtx_REG (compare_mode, regno);
6947 else
6948 {
6949 subst_mode (regno, compare_mode);
6950 new_dest = regno_reg_rtx[regno];
6951 }
6952
6953 SUBST (SET_DEST (x), new_dest);
6954 SUBST (XEXP (*cc_use, 0), new_dest);
6955 other_changed = true;
6956
6957 dest = new_dest;
6958 }
6959 }
6960 #endif /* SELECT_CC_MODE */
6961
6962 /* If the code changed, we have to build a new comparison in
6963 undobuf.other_insn. */
6964 if (new_code != old_code)
6965 {
6966 bool other_changed_previously = other_changed;
6967 unsigned HOST_WIDE_INT mask;
6968 rtx old_cc_use = *cc_use;
6969
6970 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6971 dest, const0_rtx));
6972 other_changed = true;
6973
6974 /* If the only change we made was to change an EQ into an NE or
6975 vice versa, OP0 has only one bit that might be nonzero, and OP1
6976 is zero, check if changing the user of the condition code will
6977 produce a valid insn. If it won't, we can keep the original code
6978 in that insn by surrounding our operation with an XOR. */
6979
6980 if (((old_code == NE && new_code == EQ)
6981 || (old_code == EQ && new_code == NE))
6982 && ! other_changed_previously && op1 == const0_rtx
6983 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6984 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6985 {
6986 rtx pat = PATTERN (other_insn), note = 0;
6987
6988 if ((recog_for_combine (&pat, other_insn, &note) < 0
6989 && ! check_asm_operands (pat)))
6990 {
6991 *cc_use = old_cc_use;
6992 other_changed = false;
6993
6994 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6995 gen_int_mode (mask,
6996 GET_MODE (op0)));
6997 }
6998 }
6999 }
7000
7001 if (other_changed)
7002 undobuf.other_insn = other_insn;
7003
7004 /* Don't generate a compare of a CC with 0, just use that CC. */
7005 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
7006 {
7007 SUBST (SET_SRC (x), op0);
7008 src = SET_SRC (x);
7009 }
7010 /* Otherwise, if we didn't previously have the same COMPARE we
7011 want, create it from scratch. */
7012 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
7013 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
7014 {
7015 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
7016 src = SET_SRC (x);
7017 }
7018 }
7019 else
7020 {
7021 /* Get SET_SRC in a form where we have placed back any
7022 compound expressions. Then do the checks below. */
7023 src = make_compound_operation (src, SET);
7024 SUBST (SET_SRC (x), src);
7025 }
7026
7027 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
7028 and X being a REG or (subreg (reg)), we may be able to convert this to
7029 (set (subreg:m2 x) (op)).
7030
7031 We can always do this if M1 is narrower than M2 because that means that
7032 we only care about the low bits of the result.
7033
7034 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
7035 perform a narrower operation than requested since the high-order bits will
7036 be undefined. On machine where it is defined, this transformation is safe
7037 as long as M1 and M2 have the same number of words. */
7038
7039 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
7040 && !OBJECT_P (SUBREG_REG (src))
7041 && (known_equal_after_align_up
7042 (GET_MODE_SIZE (GET_MODE (src)),
7043 GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
7044 UNITS_PER_WORD))
7045 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
7046 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
7047 && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
7048 GET_MODE (SUBREG_REG (src)),
7049 GET_MODE (src)))
7050 && (REG_P (dest)
7051 || (GET_CODE (dest) == SUBREG
7052 && REG_P (SUBREG_REG (dest)))))
7053 {
7054 SUBST (SET_DEST (x),
7055 gen_lowpart (GET_MODE (SUBREG_REG (src)),
7056 dest));
7057 SUBST (SET_SRC (x), SUBREG_REG (src));
7058
7059 src = SET_SRC (x), dest = SET_DEST (x);
7060 }
7061
7062 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
7063 would require a paradoxical subreg. Replace the subreg with a
7064 zero_extend to avoid the reload that would otherwise be required.
7065 Don't do this unless we have a scalar integer mode, otherwise the
7066 transformation is incorrect. */
7067
7068 enum rtx_code extend_op;
7069 if (paradoxical_subreg_p (src)
7070 && MEM_P (SUBREG_REG (src))
7071 && SCALAR_INT_MODE_P (GET_MODE (src))
7072 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
7073 {
7074 SUBST (SET_SRC (x),
7075 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
7076
7077 src = SET_SRC (x);
7078 }
7079
7080 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7081 are comparing an item known to be 0 or -1 against 0, use a logical
7082 operation instead. Check for one of the arms being an IOR of the other
7083 arm with some value. We compute three terms to be IOR'ed together. In
7084 practice, at most two will be nonzero. Then we do the IOR's. */
7085
7086 if (GET_CODE (dest) != PC
7087 && GET_CODE (src) == IF_THEN_ELSE
7088 && is_int_mode (GET_MODE (src), &int_mode)
7089 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7090 && XEXP (XEXP (src, 0), 1) == const0_rtx
7091 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7092 && (!HAVE_conditional_move
7093 || ! can_conditionally_move_p (int_mode))
7094 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7095 == GET_MODE_PRECISION (int_mode))
7096 && ! side_effects_p (src))
7097 {
7098 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7099 ? XEXP (src, 1) : XEXP (src, 2));
7100 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7101 ? XEXP (src, 2) : XEXP (src, 1));
7102 rtx term1 = const0_rtx, term2, term3;
7103
7104 if (GET_CODE (true_rtx) == IOR
7105 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7106 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7107 else if (GET_CODE (true_rtx) == IOR
7108 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7109 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7110 else if (GET_CODE (false_rtx) == IOR
7111 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7112 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7113 else if (GET_CODE (false_rtx) == IOR
7114 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7115 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7116
7117 term2 = simplify_gen_binary (AND, int_mode,
7118 XEXP (XEXP (src, 0), 0), true_rtx);
7119 term3 = simplify_gen_binary (AND, int_mode,
7120 simplify_gen_unary (NOT, int_mode,
7121 XEXP (XEXP (src, 0), 0),
7122 int_mode),
7123 false_rtx);
7124
7125 SUBST (SET_SRC (x),
7126 simplify_gen_binary (IOR, int_mode,
7127 simplify_gen_binary (IOR, int_mode,
7128 term1, term2),
7129 term3));
7130
7131 src = SET_SRC (x);
7132 }
7133
7134 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7135 whole thing fail. */
7136 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7137 return src;
7138 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7139 return dest;
7140 else
7141 /* Convert this into a field assignment operation, if possible. */
7142 return make_field_assignment (x);
7143 }
7144
7145 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7146 result. */
7147
7148 static rtx
7149 simplify_logical (rtx x)
7150 {
7151 rtx op0 = XEXP (x, 0);
7152 rtx op1 = XEXP (x, 1);
7153 scalar_int_mode mode;
7154
7155 switch (GET_CODE (x))
7156 {
7157 case AND:
7158 /* We can call simplify_and_const_int only if we don't lose
7159 any (sign) bits when converting INTVAL (op1) to
7160 "unsigned HOST_WIDE_INT". */
7161 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7162 && CONST_INT_P (op1)
7163 && (HWI_COMPUTABLE_MODE_P (mode)
7164 || INTVAL (op1) > 0))
7165 {
7166 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7167 if (GET_CODE (x) != AND)
7168 return x;
7169
7170 op0 = XEXP (x, 0);
7171 op1 = XEXP (x, 1);
7172 }
7173
7174 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7175 apply the distributive law and then the inverse distributive
7176 law to see if things simplify. */
7177 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7178 {
7179 rtx result = distribute_and_simplify_rtx (x, 0);
7180 if (result)
7181 return result;
7182 }
7183 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7184 {
7185 rtx result = distribute_and_simplify_rtx (x, 1);
7186 if (result)
7187 return result;
7188 }
7189 break;
7190
7191 case IOR:
7192 /* If we have (ior (and A B) C), apply the distributive law and then
7193 the inverse distributive law to see if things simplify. */
7194
7195 if (GET_CODE (op0) == AND)
7196 {
7197 rtx result = distribute_and_simplify_rtx (x, 0);
7198 if (result)
7199 return result;
7200 }
7201
7202 if (GET_CODE (op1) == AND)
7203 {
7204 rtx result = distribute_and_simplify_rtx (x, 1);
7205 if (result)
7206 return result;
7207 }
7208 break;
7209
7210 default:
7211 gcc_unreachable ();
7212 }
7213
7214 return x;
7215 }
7216
7217 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7218 operations" because they can be replaced with two more basic operations.
7219 ZERO_EXTEND is also considered "compound" because it can be replaced with
7220 an AND operation, which is simpler, though only one operation.
7221
7222 The function expand_compound_operation is called with an rtx expression
7223 and will convert it to the appropriate shifts and AND operations,
7224 simplifying at each stage.
7225
7226 The function make_compound_operation is called to convert an expression
7227 consisting of shifts and ANDs into the equivalent compound expression.
7228 It is the inverse of this function, loosely speaking. */
7229
7230 static rtx
7231 expand_compound_operation (rtx x)
7232 {
7233 unsigned HOST_WIDE_INT pos = 0, len;
7234 bool unsignedp = false;
7235 unsigned int modewidth;
7236 rtx tem;
7237 scalar_int_mode inner_mode;
7238
7239 switch (GET_CODE (x))
7240 {
7241 case ZERO_EXTEND:
7242 unsignedp = true;
7243 /* FALLTHRU */
7244 case SIGN_EXTEND:
7245 /* We can't necessarily use a const_int for a multiword mode;
7246 it depends on implicitly extending the value.
7247 Since we don't know the right way to extend it,
7248 we can't tell whether the implicit way is right.
7249
7250 Even for a mode that is no wider than a const_int,
7251 we can't win, because we need to sign extend one of its bits through
7252 the rest of it, and we don't know which bit. */
7253 if (CONST_INT_P (XEXP (x, 0)))
7254 return x;
7255
7256 /* Reject modes that aren't scalar integers because turning vector
7257 or complex modes into shifts causes problems. */
7258 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7259 return x;
7260
7261 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7262 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7263 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7264 reloaded. If not for that, MEM's would very rarely be safe.
7265
7266 Reject modes bigger than a word, because we might not be able
7267 to reference a two-register group starting with an arbitrary register
7268 (and currently gen_lowpart might crash for a SUBREG). */
7269
7270 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7271 return x;
7272
7273 len = GET_MODE_PRECISION (inner_mode);
7274 /* If the inner object has VOIDmode (the only way this can happen
7275 is if it is an ASM_OPERANDS), we can't do anything since we don't
7276 know how much masking to do. */
7277 if (len == 0)
7278 return x;
7279
7280 break;
7281
7282 case ZERO_EXTRACT:
7283 unsignedp = true;
7284
7285 /* fall through */
7286
7287 case SIGN_EXTRACT:
7288 /* If the operand is a CLOBBER, just return it. */
7289 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7290 return XEXP (x, 0);
7291
7292 if (!CONST_INT_P (XEXP (x, 1))
7293 || !CONST_INT_P (XEXP (x, 2)))
7294 return x;
7295
7296 /* Reject modes that aren't scalar integers because turning vector
7297 or complex modes into shifts causes problems. */
7298 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7299 return x;
7300
7301 len = INTVAL (XEXP (x, 1));
7302 pos = INTVAL (XEXP (x, 2));
7303
7304 /* This should stay within the object being extracted, fail otherwise. */
7305 if (len + pos > GET_MODE_PRECISION (inner_mode))
7306 return x;
7307
7308 if (BITS_BIG_ENDIAN)
7309 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7310
7311 break;
7312
7313 default:
7314 return x;
7315 }
7316
7317 /* We've rejected non-scalar operations by now. */
7318 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7319
7320 /* Convert sign extension to zero extension, if we know that the high
7321 bit is not set, as this is easier to optimize. It will be converted
7322 back to cheaper alternative in make_extraction. */
7323 if (GET_CODE (x) == SIGN_EXTEND
7324 && HWI_COMPUTABLE_MODE_P (mode)
7325 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7326 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7327 == 0))
7328 {
7329 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7330 rtx temp2 = expand_compound_operation (temp);
7331
7332 /* Make sure this is a profitable operation. */
7333 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7334 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7335 return temp2;
7336 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7337 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7338 return temp;
7339 else
7340 return x;
7341 }
7342
7343 /* We can optimize some special cases of ZERO_EXTEND. */
7344 if (GET_CODE (x) == ZERO_EXTEND)
7345 {
7346 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7347 know that the last value didn't have any inappropriate bits
7348 set. */
7349 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7350 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7351 && HWI_COMPUTABLE_MODE_P (mode)
7352 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7353 & ~GET_MODE_MASK (inner_mode)) == 0)
7354 return XEXP (XEXP (x, 0), 0);
7355
7356 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7357 if (GET_CODE (XEXP (x, 0)) == SUBREG
7358 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7359 && subreg_lowpart_p (XEXP (x, 0))
7360 && HWI_COMPUTABLE_MODE_P (mode)
7361 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7362 & ~GET_MODE_MASK (inner_mode)) == 0)
7363 return SUBREG_REG (XEXP (x, 0));
7364
7365 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7366 is a comparison and STORE_FLAG_VALUE permits. This is like
7367 the first case, but it works even when MODE is larger
7368 than HOST_WIDE_INT. */
7369 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7370 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7371 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7372 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7373 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7374 return XEXP (XEXP (x, 0), 0);
7375
7376 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7377 if (GET_CODE (XEXP (x, 0)) == SUBREG
7378 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7379 && subreg_lowpart_p (XEXP (x, 0))
7380 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7381 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7382 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7383 return SUBREG_REG (XEXP (x, 0));
7384
7385 }
7386
7387 /* If we reach here, we want to return a pair of shifts. The inner
7388 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7389 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7390 logical depending on the value of UNSIGNEDP.
7391
7392 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7393 converted into an AND of a shift.
7394
7395 We must check for the case where the left shift would have a negative
7396 count. This can happen in a case like (x >> 31) & 255 on machines
7397 that can't shift by a constant. On those machines, we would first
7398 combine the shift with the AND to produce a variable-position
7399 extraction. Then the constant of 31 would be substituted in
7400 to produce such a position. */
7401
7402 modewidth = GET_MODE_PRECISION (mode);
7403 if (modewidth >= pos + len)
7404 {
7405 tem = gen_lowpart (mode, XEXP (x, 0));
7406 if (!tem || GET_CODE (tem) == CLOBBER)
7407 return x;
7408 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7409 tem, modewidth - pos - len);
7410 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7411 mode, tem, modewidth - len);
7412 }
7413 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7414 {
7415 tem = simplify_shift_const (NULL_RTX, LSHIFTRT, inner_mode,
7416 XEXP (x, 0), pos);
7417 tem = gen_lowpart (mode, tem);
7418 if (!tem || GET_CODE (tem) == CLOBBER)
7419 return x;
7420 tem = simplify_and_const_int (NULL_RTX, mode, tem,
7421 (HOST_WIDE_INT_1U << len) - 1);
7422 }
7423 else
7424 /* Any other cases we can't handle. */
7425 return x;
7426
7427 /* If we couldn't do this for some reason, return the original
7428 expression. */
7429 if (GET_CODE (tem) == CLOBBER)
7430 return x;
7431
7432 return tem;
7433 }
7434
7435 /* X is a SET which contains an assignment of one object into
7436 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7437 or certain SUBREGS). If possible, convert it into a series of
7438 logical operations.
7439
7440 We half-heartedly support variable positions, but do not at all
7441 support variable lengths. */
7442
7443 static const_rtx
7444 expand_field_assignment (const_rtx x)
7445 {
7446 rtx inner;
7447 rtx pos; /* Always counts from low bit. */
7448 int len, inner_len;
7449 rtx mask, cleared, masked;
7450 scalar_int_mode compute_mode;
7451
7452 /* Loop until we find something we can't simplify. */
7453 while (1)
7454 {
7455 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7456 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7457 {
7458 rtx x0 = XEXP (SET_DEST (x), 0);
7459 if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7460 break;
7461 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7462 pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7463 MAX_MODE_INT);
7464 }
7465 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7466 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7467 {
7468 inner = XEXP (SET_DEST (x), 0);
7469 if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7470 break;
7471
7472 len = INTVAL (XEXP (SET_DEST (x), 1));
7473 pos = XEXP (SET_DEST (x), 2);
7474
7475 /* A constant position should stay within the width of INNER. */
7476 if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7477 break;
7478
7479 if (BITS_BIG_ENDIAN)
7480 {
7481 if (CONST_INT_P (pos))
7482 pos = GEN_INT (inner_len - len - INTVAL (pos));
7483 else if (GET_CODE (pos) == MINUS
7484 && CONST_INT_P (XEXP (pos, 1))
7485 && INTVAL (XEXP (pos, 1)) == inner_len - len)
7486 /* If position is ADJUST - X, new position is X. */
7487 pos = XEXP (pos, 0);
7488 else
7489 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7490 gen_int_mode (inner_len - len,
7491 GET_MODE (pos)),
7492 pos);
7493 }
7494 }
7495
7496 /* If the destination is a subreg that overwrites the whole of the inner
7497 register, we can move the subreg to the source. */
7498 else if (GET_CODE (SET_DEST (x)) == SUBREG
7499 /* We need SUBREGs to compute nonzero_bits properly. */
7500 && nonzero_sign_valid
7501 && !read_modify_subreg_p (SET_DEST (x)))
7502 {
7503 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7504 gen_lowpart
7505 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7506 SET_SRC (x)));
7507 continue;
7508 }
7509 else
7510 break;
7511
7512 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7513 inner = SUBREG_REG (inner);
7514
7515 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7516 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7517 {
7518 /* Don't do anything for vector or complex integral types. */
7519 if (! FLOAT_MODE_P (GET_MODE (inner)))
7520 break;
7521
7522 /* Try to find an integral mode to pun with. */
7523 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7524 .exists (&compute_mode))
7525 break;
7526
7527 inner = gen_lowpart (compute_mode, inner);
7528 }
7529
7530 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7531 if (len >= HOST_BITS_PER_WIDE_INT)
7532 break;
7533
7534 /* Don't try to compute in too wide unsupported modes. */
7535 if (!targetm.scalar_mode_supported_p (compute_mode))
7536 break;
7537
7538 /* gen_lowpart_for_combine returns CLOBBER on failure. */
7539 rtx lowpart = gen_lowpart (compute_mode, SET_SRC (x));
7540 if (GET_CODE (lowpart) == CLOBBER)
7541 break;
7542
7543 /* Now compute the equivalent expression. Make a copy of INNER
7544 for the SET_DEST in case it is a MEM into which we will substitute;
7545 we don't want shared RTL in that case. */
7546 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7547 compute_mode);
7548 cleared = simplify_gen_binary (AND, compute_mode,
7549 simplify_gen_unary (NOT, compute_mode,
7550 simplify_gen_binary (ASHIFT,
7551 compute_mode,
7552 mask, pos),
7553 compute_mode),
7554 inner);
7555 masked = simplify_gen_binary (ASHIFT, compute_mode,
7556 simplify_gen_binary (
7557 AND, compute_mode, lowpart, mask),
7558 pos);
7559
7560 x = gen_rtx_SET (copy_rtx (inner),
7561 simplify_gen_binary (IOR, compute_mode,
7562 cleared, masked));
7563 }
7564
7565 return x;
7566 }
7567
7568 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7569 it is an RTX that represents the (variable) starting position; otherwise,
7570 POS is the (constant) starting bit position. Both are counted from the LSB.
7571
7572 UNSIGNEDP is true for an unsigned reference and zero for a signed one.
7573
7574 IN_DEST is true if this is a reference in the destination of a SET.
7575 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7576 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7577 be used.
7578
7579 IN_COMPARE is true if we are in a COMPARE. This means that a
7580 ZERO_EXTRACT should be built even for bits starting at bit 0.
7581
7582 MODE is the desired mode of the result (if IN_DEST == 0).
7583
7584 The result is an RTX for the extraction or NULL_RTX if the target
7585 can't handle it. */
7586
7587 static rtx
7588 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7589 rtx pos_rtx, unsigned HOST_WIDE_INT len, bool unsignedp,
7590 bool in_dest, bool in_compare)
7591 {
7592 /* This mode describes the size of the storage area
7593 to fetch the overall value from. Within that, we
7594 ignore the POS lowest bits, etc. */
7595 machine_mode is_mode = GET_MODE (inner);
7596 machine_mode inner_mode;
7597 scalar_int_mode wanted_inner_mode;
7598 scalar_int_mode wanted_inner_reg_mode = word_mode;
7599 scalar_int_mode pos_mode = word_mode;
7600 machine_mode extraction_mode = word_mode;
7601 rtx new_rtx = 0;
7602 rtx orig_pos_rtx = pos_rtx;
7603 HOST_WIDE_INT orig_pos;
7604
7605 if (pos_rtx && CONST_INT_P (pos_rtx))
7606 pos = INTVAL (pos_rtx), pos_rtx = 0;
7607
7608 if (GET_CODE (inner) == SUBREG
7609 && subreg_lowpart_p (inner)
7610 && (paradoxical_subreg_p (inner)
7611 /* If trying or potentionally trying to extract
7612 bits outside of is_mode, don't look through
7613 non-paradoxical SUBREGs. See PR82192. */
7614 || (pos_rtx == NULL_RTX
7615 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7616 {
7617 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7618 consider just the QI as the memory to extract from.
7619 The subreg adds or removes high bits; its mode is
7620 irrelevant to the meaning of this extraction,
7621 since POS and LEN count from the lsb. */
7622 if (MEM_P (SUBREG_REG (inner)))
7623 is_mode = GET_MODE (SUBREG_REG (inner));
7624 inner = SUBREG_REG (inner);
7625 }
7626 else if (GET_CODE (inner) == ASHIFT
7627 && CONST_INT_P (XEXP (inner, 1))
7628 && pos_rtx == 0 && pos == 0
7629 && len > UINTVAL (XEXP (inner, 1)))
7630 {
7631 /* We're extracting the least significant bits of an rtx
7632 (ashift X (const_int C)), where LEN > C. Extract the
7633 least significant (LEN - C) bits of X, giving an rtx
7634 whose mode is MODE, then shift it left C times. */
7635 new_rtx = make_extraction (mode, XEXP (inner, 0),
7636 0, 0, len - INTVAL (XEXP (inner, 1)),
7637 unsignedp, in_dest, in_compare);
7638 if (new_rtx != 0)
7639 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7640 }
7641 else if (GET_CODE (inner) == MULT
7642 && CONST_INT_P (XEXP (inner, 1))
7643 && pos_rtx == 0 && pos == 0)
7644 {
7645 /* We're extracting the least significant bits of an rtx
7646 (mult X (const_int 2^C)), where LEN > C. Extract the
7647 least significant (LEN - C) bits of X, giving an rtx
7648 whose mode is MODE, then multiply it by 2^C. */
7649 const HOST_WIDE_INT shift_amt = exact_log2 (INTVAL (XEXP (inner, 1)));
7650 if (len > 1 && IN_RANGE (shift_amt, 1, len - 1))
7651 {
7652 new_rtx = make_extraction (mode, XEXP (inner, 0),
7653 0, 0, len - shift_amt,
7654 unsignedp, in_dest, in_compare);
7655 if (new_rtx)
7656 return gen_rtx_MULT (mode, new_rtx, XEXP (inner, 1));
7657 }
7658 }
7659 else if (GET_CODE (inner) == TRUNCATE
7660 /* If trying or potentionally trying to extract
7661 bits outside of is_mode, don't look through
7662 TRUNCATE. See PR82192. */
7663 && pos_rtx == NULL_RTX
7664 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7665 inner = XEXP (inner, 0);
7666
7667 inner_mode = GET_MODE (inner);
7668
7669 /* See if this can be done without an extraction. We never can if the
7670 width of the field is not the same as that of some integer mode. For
7671 registers, we can only avoid the extraction if the position is at the
7672 low-order bit and this is either not in the destination or we have the
7673 appropriate STRICT_LOW_PART operation available.
7674
7675 For MEM, we can avoid an extract if the field starts on an appropriate
7676 boundary and we can change the mode of the memory reference. */
7677
7678 scalar_int_mode tmode;
7679 if (int_mode_for_size (len, 1).exists (&tmode)
7680 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7681 && !MEM_P (inner)
7682 && (pos == 0 || REG_P (inner))
7683 && (inner_mode == tmode
7684 || !REG_P (inner)
7685 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7686 || reg_truncated_to_mode (tmode, inner))
7687 && (! in_dest
7688 || (REG_P (inner)
7689 && have_insn_for (STRICT_LOW_PART, tmode))))
7690 || (MEM_P (inner) && pos_rtx == 0
7691 && (pos
7692 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7693 : BITS_PER_UNIT)) == 0
7694 /* We can't do this if we are widening INNER_MODE (it
7695 may not be aligned, for one thing). */
7696 && !paradoxical_subreg_p (tmode, inner_mode)
7697 && known_le (pos + len, GET_MODE_PRECISION (is_mode))
7698 && (inner_mode == tmode
7699 || (! mode_dependent_address_p (XEXP (inner, 0),
7700 MEM_ADDR_SPACE (inner))
7701 && ! MEM_VOLATILE_P (inner))))))
7702 {
7703 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7704 field. If the original and current mode are the same, we need not
7705 adjust the offset. Otherwise, we do if bytes big endian.
7706
7707 If INNER is not a MEM, get a piece consisting of just the field
7708 of interest (in this case POS % BITS_PER_WORD must be 0). */
7709
7710 if (MEM_P (inner))
7711 {
7712 poly_int64 offset;
7713
7714 /* POS counts from lsb, but make OFFSET count in memory order. */
7715 if (BYTES_BIG_ENDIAN)
7716 offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7717 - len - pos);
7718 else
7719 offset = pos / BITS_PER_UNIT;
7720
7721 new_rtx = adjust_address_nv (inner, tmode, offset);
7722 }
7723 else if (REG_P (inner))
7724 {
7725 if (tmode != inner_mode)
7726 {
7727 /* We can't call gen_lowpart in a DEST since we
7728 always want a SUBREG (see below) and it would sometimes
7729 return a new hard register. */
7730 if (pos || in_dest)
7731 {
7732 poly_uint64 offset
7733 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7734
7735 /* Avoid creating invalid subregs, for example when
7736 simplifying (x>>32)&255. */
7737 if (!validate_subreg (tmode, inner_mode, inner, offset))
7738 return NULL_RTX;
7739
7740 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7741 }
7742 else
7743 new_rtx = gen_lowpart (tmode, inner);
7744 }
7745 else
7746 new_rtx = inner;
7747 }
7748 else
7749 new_rtx = force_to_mode (inner, tmode,
7750 len >= HOST_BITS_PER_WIDE_INT
7751 ? HOST_WIDE_INT_M1U
7752 : (HOST_WIDE_INT_1U << len) - 1, false);
7753
7754 /* If this extraction is going into the destination of a SET,
7755 make a STRICT_LOW_PART unless we made a MEM. */
7756
7757 if (in_dest)
7758 return (MEM_P (new_rtx) ? new_rtx
7759 : (GET_CODE (new_rtx) != SUBREG
7760 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7761 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7762
7763 if (mode == tmode)
7764 return new_rtx;
7765
7766 if (CONST_SCALAR_INT_P (new_rtx))
7767 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7768 mode, new_rtx, tmode);
7769
7770 /* If we know that no extraneous bits are set, and that the high
7771 bit is not set, convert the extraction to the cheaper of
7772 sign and zero extension, that are equivalent in these cases. */
7773 if (flag_expensive_optimizations
7774 && (HWI_COMPUTABLE_MODE_P (tmode)
7775 && ((nonzero_bits (new_rtx, tmode)
7776 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7777 == 0)))
7778 {
7779 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7780 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7781
7782 /* Prefer ZERO_EXTENSION, since it gives more information to
7783 backends. */
7784 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7785 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7786 return temp;
7787 return temp1;
7788 }
7789
7790 /* Otherwise, sign- or zero-extend unless we already are in the
7791 proper mode. */
7792
7793 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7794 mode, new_rtx));
7795 }
7796
7797 /* Unless this is a COMPARE or we have a funny memory reference,
7798 don't do anything with zero-extending field extracts starting at
7799 the low-order bit since they are simple AND operations. */
7800 if (pos_rtx == 0 && pos == 0 && ! in_dest
7801 && ! in_compare && unsignedp)
7802 return 0;
7803
7804 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7805 if the position is not a constant and the length is not 1. In all
7806 other cases, we would only be going outside our object in cases when
7807 an original shift would have been undefined. */
7808 if (MEM_P (inner)
7809 && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7810 || (pos_rtx != 0 && len != 1)))
7811 return 0;
7812
7813 enum extraction_pattern pattern = (in_dest ? EP_insv
7814 : unsignedp ? EP_extzv : EP_extv);
7815
7816 /* If INNER is not from memory, we want it to have the mode of a register
7817 extraction pattern's structure operand, or word_mode if there is no
7818 such pattern. The same applies to extraction_mode and pos_mode
7819 and their respective operands.
7820
7821 For memory, assume that the desired extraction_mode and pos_mode
7822 are the same as for a register operation, since at present we don't
7823 have named patterns for aligned memory structures. */
7824 class extraction_insn insn;
7825 unsigned int inner_size;
7826 if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7827 && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7828 {
7829 wanted_inner_reg_mode = insn.struct_mode.require ();
7830 pos_mode = insn.pos_mode;
7831 extraction_mode = insn.field_mode;
7832 }
7833
7834 /* Never narrow an object, since that might not be safe. */
7835
7836 if (mode != VOIDmode
7837 && partial_subreg_p (extraction_mode, mode))
7838 extraction_mode = mode;
7839
7840 /* Punt if len is too large for extraction_mode. */
7841 if (maybe_gt (len, GET_MODE_PRECISION (extraction_mode)))
7842 return NULL_RTX;
7843
7844 if (!MEM_P (inner))
7845 wanted_inner_mode = wanted_inner_reg_mode;
7846 else
7847 {
7848 /* Be careful not to go beyond the extracted object and maintain the
7849 natural alignment of the memory. */
7850 wanted_inner_mode = smallest_int_mode_for_size (len).require ();
7851 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7852 > GET_MODE_BITSIZE (wanted_inner_mode))
7853 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7854 }
7855
7856 orig_pos = pos;
7857
7858 if (BITS_BIG_ENDIAN)
7859 {
7860 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7861 BITS_BIG_ENDIAN style. If position is constant, compute new
7862 position. Otherwise, build subtraction.
7863 Note that POS is relative to the mode of the original argument.
7864 If it's a MEM we need to recompute POS relative to that.
7865 However, if we're extracting from (or inserting into) a register,
7866 we want to recompute POS relative to wanted_inner_mode. */
7867 int width;
7868 if (!MEM_P (inner))
7869 width = GET_MODE_BITSIZE (wanted_inner_mode);
7870 else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7871 return NULL_RTX;
7872
7873 if (pos_rtx == 0)
7874 pos = width - len - pos;
7875 else
7876 pos_rtx
7877 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7878 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7879 pos_rtx);
7880 /* POS may be less than 0 now, but we check for that below.
7881 Note that it can only be less than 0 if !MEM_P (inner). */
7882 }
7883
7884 /* If INNER has a wider mode, and this is a constant extraction, try to
7885 make it smaller and adjust the byte to point to the byte containing
7886 the value. */
7887 if (wanted_inner_mode != VOIDmode
7888 && inner_mode != wanted_inner_mode
7889 && ! pos_rtx
7890 && partial_subreg_p (wanted_inner_mode, is_mode)
7891 && MEM_P (inner)
7892 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7893 && ! MEM_VOLATILE_P (inner))
7894 {
7895 poly_int64 offset = 0;
7896
7897 /* The computations below will be correct if the machine is big
7898 endian in both bits and bytes or little endian in bits and bytes.
7899 If it is mixed, we must adjust. */
7900
7901 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7902 adjust OFFSET to compensate. */
7903 if (BYTES_BIG_ENDIAN
7904 && paradoxical_subreg_p (is_mode, inner_mode))
7905 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7906
7907 /* We can now move to the desired byte. */
7908 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7909 * GET_MODE_SIZE (wanted_inner_mode);
7910 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7911
7912 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7913 && is_mode != wanted_inner_mode)
7914 offset = (GET_MODE_SIZE (is_mode)
7915 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7916
7917 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7918 }
7919
7920 /* If INNER is not memory, get it into the proper mode. If we are changing
7921 its mode, POS must be a constant and smaller than the size of the new
7922 mode. */
7923 else if (!MEM_P (inner))
7924 {
7925 /* On the LHS, don't create paradoxical subregs implicitely truncating
7926 the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7927 if (in_dest
7928 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7929 wanted_inner_mode))
7930 return NULL_RTX;
7931
7932 if (GET_MODE (inner) != wanted_inner_mode
7933 && (pos_rtx != 0
7934 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7935 return NULL_RTX;
7936
7937 if (orig_pos < 0)
7938 return NULL_RTX;
7939
7940 inner = force_to_mode (inner, wanted_inner_mode,
7941 pos_rtx
7942 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7943 ? HOST_WIDE_INT_M1U
7944 : (((HOST_WIDE_INT_1U << len) - 1)
7945 << orig_pos), false);
7946 }
7947
7948 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7949 have to zero extend. Otherwise, we can just use a SUBREG.
7950
7951 We dealt with constant rtxes earlier, so pos_rtx cannot
7952 have VOIDmode at this point. */
7953 if (pos_rtx != 0
7954 && (GET_MODE_SIZE (pos_mode)
7955 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7956 {
7957 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7958 GET_MODE (pos_rtx));
7959
7960 /* If we know that no extraneous bits are set, and that the high
7961 bit is not set, convert extraction to cheaper one - either
7962 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7963 cases. */
7964 if (flag_expensive_optimizations
7965 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7966 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7967 & ~(((unsigned HOST_WIDE_INT)
7968 GET_MODE_MASK (GET_MODE (pos_rtx)))
7969 >> 1))
7970 == 0)))
7971 {
7972 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7973 GET_MODE (pos_rtx));
7974
7975 /* Prefer ZERO_EXTENSION, since it gives more information to
7976 backends. */
7977 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7978 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7979 temp = temp1;
7980 }
7981 pos_rtx = temp;
7982 }
7983
7984 /* Make POS_RTX unless we already have it and it is correct. If we don't
7985 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7986 be a CONST_INT. */
7987 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7988 pos_rtx = orig_pos_rtx;
7989
7990 else if (pos_rtx == 0)
7991 pos_rtx = GEN_INT (pos);
7992
7993 /* Make the required operation. See if we can use existing rtx. */
7994 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7995 extraction_mode, inner, GEN_INT (len), pos_rtx);
7996 if (! in_dest)
7997 new_rtx = gen_lowpart (mode, new_rtx);
7998
7999 return new_rtx;
8000 }
8001
8002 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
8003 can be commuted with any other operations in X. Return X without
8004 that shift if so. */
8005
8006 static rtx
8007 extract_left_shift (scalar_int_mode mode, rtx x, int count)
8008 {
8009 enum rtx_code code = GET_CODE (x);
8010 rtx tem;
8011
8012 switch (code)
8013 {
8014 case ASHIFT:
8015 /* This is the shift itself. If it is wide enough, we will return
8016 either the value being shifted if the shift count is equal to
8017 COUNT or a shift for the difference. */
8018 if (CONST_INT_P (XEXP (x, 1))
8019 && INTVAL (XEXP (x, 1)) >= count)
8020 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
8021 INTVAL (XEXP (x, 1)) - count);
8022 break;
8023
8024 case NEG: case NOT:
8025 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8026 return simplify_gen_unary (code, mode, tem, mode);
8027
8028 break;
8029
8030 case PLUS: case IOR: case XOR: case AND:
8031 /* If we can safely shift this constant and we find the inner shift,
8032 make a new operation. */
8033 if (CONST_INT_P (XEXP (x, 1))
8034 && (UINTVAL (XEXP (x, 1))
8035 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
8036 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8037 {
8038 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
8039 return simplify_gen_binary (code, mode, tem,
8040 gen_int_mode (val, mode));
8041 }
8042 break;
8043
8044 default:
8045 break;
8046 }
8047
8048 return 0;
8049 }
8050
8051 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
8052 level of the expression and MODE is its mode. IN_CODE is as for
8053 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
8054 that should be used when recursing on operands of *X_PTR.
8055
8056 There are two possible actions:
8057
8058 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
8059 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
8060
8061 - Return a new rtx, which the caller returns directly. */
8062
8063 static rtx
8064 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
8065 enum rtx_code in_code,
8066 enum rtx_code *next_code_ptr)
8067 {
8068 rtx x = *x_ptr;
8069 enum rtx_code next_code = *next_code_ptr;
8070 enum rtx_code code = GET_CODE (x);
8071 int mode_width = GET_MODE_PRECISION (mode);
8072 rtx rhs, lhs;
8073 rtx new_rtx = 0;
8074 int i;
8075 rtx tem;
8076 scalar_int_mode inner_mode;
8077 bool equality_comparison = false;
8078
8079 if (in_code == EQ)
8080 {
8081 equality_comparison = true;
8082 in_code = COMPARE;
8083 }
8084
8085 /* Process depending on the code of this operation. If NEW is set
8086 nonzero, it will be returned. */
8087
8088 switch (code)
8089 {
8090 case ASHIFT:
8091 /* Convert shifts by constants into multiplications if inside
8092 an address. */
8093 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
8094 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8095 && INTVAL (XEXP (x, 1)) >= 0)
8096 {
8097 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
8098 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
8099
8100 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8101 if (GET_CODE (new_rtx) == NEG)
8102 {
8103 new_rtx = XEXP (new_rtx, 0);
8104 multval = -multval;
8105 }
8106 multval = trunc_int_for_mode (multval, mode);
8107 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8108 }
8109 break;
8110
8111 case PLUS:
8112 lhs = XEXP (x, 0);
8113 rhs = XEXP (x, 1);
8114 lhs = make_compound_operation (lhs, next_code);
8115 rhs = make_compound_operation (rhs, next_code);
8116 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
8117 {
8118 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
8119 XEXP (lhs, 1));
8120 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8121 }
8122 else if (GET_CODE (lhs) == MULT
8123 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
8124 {
8125 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
8126 simplify_gen_unary (NEG, mode,
8127 XEXP (lhs, 1),
8128 mode));
8129 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8130 }
8131 else
8132 {
8133 SUBST (XEXP (x, 0), lhs);
8134 SUBST (XEXP (x, 1), rhs);
8135 }
8136 maybe_swap_commutative_operands (x);
8137 return x;
8138
8139 case MINUS:
8140 lhs = XEXP (x, 0);
8141 rhs = XEXP (x, 1);
8142 lhs = make_compound_operation (lhs, next_code);
8143 rhs = make_compound_operation (rhs, next_code);
8144 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
8145 {
8146 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
8147 XEXP (rhs, 1));
8148 return simplify_gen_binary (PLUS, mode, tem, lhs);
8149 }
8150 else if (GET_CODE (rhs) == MULT
8151 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
8152 {
8153 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
8154 simplify_gen_unary (NEG, mode,
8155 XEXP (rhs, 1),
8156 mode));
8157 return simplify_gen_binary (PLUS, mode, tem, lhs);
8158 }
8159 else
8160 {
8161 SUBST (XEXP (x, 0), lhs);
8162 SUBST (XEXP (x, 1), rhs);
8163 return x;
8164 }
8165
8166 case AND:
8167 /* If the second operand is not a constant, we can't do anything
8168 with it. */
8169 if (!CONST_INT_P (XEXP (x, 1)))
8170 break;
8171
8172 /* If the constant is a power of two minus one and the first operand
8173 is a logical right shift, make an extraction. */
8174 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8175 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8176 {
8177 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8178 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8179 i, true, false, in_code == COMPARE);
8180 }
8181
8182 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8183 else if (GET_CODE (XEXP (x, 0)) == SUBREG
8184 && subreg_lowpart_p (XEXP (x, 0))
8185 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8186 &inner_mode)
8187 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8188 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8189 {
8190 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8191 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8192 new_rtx = make_extraction (inner_mode, new_rtx, 0,
8193 XEXP (inner_x0, 1),
8194 i, true, false, in_code == COMPARE);
8195
8196 /* If we narrowed the mode when dropping the subreg, then we lose. */
8197 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8198 new_rtx = NULL;
8199
8200 /* If that didn't give anything, see if the AND simplifies on
8201 its own. */
8202 if (!new_rtx && i >= 0)
8203 {
8204 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8205 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i,
8206 true, false, in_code == COMPARE);
8207 }
8208 }
8209 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8210 else if ((GET_CODE (XEXP (x, 0)) == XOR
8211 || GET_CODE (XEXP (x, 0)) == IOR)
8212 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8213 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8214 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8215 {
8216 /* Apply the distributive law, and then try to make extractions. */
8217 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8218 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8219 XEXP (x, 1)),
8220 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8221 XEXP (x, 1)));
8222 new_rtx = make_compound_operation (new_rtx, in_code);
8223 }
8224
8225 /* If we are have (and (rotate X C) M) and C is larger than the number
8226 of bits in M, this is an extraction. */
8227
8228 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8229 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8230 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8231 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8232 {
8233 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8234 new_rtx = make_extraction (mode, new_rtx,
8235 (GET_MODE_PRECISION (mode)
8236 - INTVAL (XEXP (XEXP (x, 0), 1))),
8237 NULL_RTX, i, true, false,
8238 in_code == COMPARE);
8239 }
8240
8241 /* On machines without logical shifts, if the operand of the AND is
8242 a logical shift and our mask turns off all the propagated sign
8243 bits, we can replace the logical shift with an arithmetic shift. */
8244 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8245 && !have_insn_for (LSHIFTRT, mode)
8246 && have_insn_for (ASHIFTRT, mode)
8247 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8248 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8249 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8250 && mode_width <= HOST_BITS_PER_WIDE_INT)
8251 {
8252 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8253
8254 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8255 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8256 SUBST (XEXP (x, 0),
8257 gen_rtx_ASHIFTRT (mode,
8258 make_compound_operation (XEXP (XEXP (x,
8259 0),
8260 0),
8261 next_code),
8262 XEXP (XEXP (x, 0), 1)));
8263 }
8264
8265 /* If the constant is one less than a power of two, this might be
8266 representable by an extraction even if no shift is present.
8267 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8268 we are in a COMPARE. */
8269 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8270 new_rtx = make_extraction (mode,
8271 make_compound_operation (XEXP (x, 0),
8272 next_code),
8273 0, NULL_RTX, i,
8274 true, false, in_code == COMPARE);
8275
8276 /* If we are in a comparison and this is an AND with a power of two,
8277 convert this into the appropriate bit extract. */
8278 else if (in_code == COMPARE
8279 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8280 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8281 new_rtx = make_extraction (mode,
8282 make_compound_operation (XEXP (x, 0),
8283 next_code),
8284 i, NULL_RTX, 1, true, false, true);
8285
8286 /* If the one operand is a paradoxical subreg of a register or memory and
8287 the constant (limited to the smaller mode) has only zero bits where
8288 the sub expression has known zero bits, this can be expressed as
8289 a zero_extend. */
8290 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8291 {
8292 rtx sub;
8293
8294 sub = XEXP (XEXP (x, 0), 0);
8295 machine_mode sub_mode = GET_MODE (sub);
8296 int sub_width;
8297 if ((REG_P (sub) || MEM_P (sub))
8298 && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8299 && sub_width < mode_width
8300 && (!WORD_REGISTER_OPERATIONS
8301 || sub_width >= BITS_PER_WORD
8302 /* On WORD_REGISTER_OPERATIONS targets the bits
8303 beyond sub_mode aren't considered undefined,
8304 so optimize only if it is a MEM load when MEM loads
8305 zero extend, because then the upper bits are all zero. */
8306 || (MEM_P (sub)
8307 && load_extend_op (sub_mode) == ZERO_EXTEND)))
8308 {
8309 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8310 unsigned HOST_WIDE_INT mask;
8311
8312 /* Original AND constant with all the known zero bits set. */
8313 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8314 if ((mask & mode_mask) == mode_mask)
8315 {
8316 new_rtx = make_compound_operation (sub, next_code);
8317 new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8318 true, false, in_code == COMPARE);
8319 }
8320 }
8321 }
8322
8323 break;
8324
8325 case LSHIFTRT:
8326 /* If the sign bit is known to be zero, replace this with an
8327 arithmetic shift. */
8328 if (have_insn_for (ASHIFTRT, mode)
8329 && ! have_insn_for (LSHIFTRT, mode)
8330 && mode_width <= HOST_BITS_PER_WIDE_INT
8331 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8332 {
8333 new_rtx = gen_rtx_ASHIFTRT (mode,
8334 make_compound_operation (XEXP (x, 0),
8335 next_code),
8336 XEXP (x, 1));
8337 break;
8338 }
8339
8340 /* fall through */
8341
8342 case ASHIFTRT:
8343 lhs = XEXP (x, 0);
8344 rhs = XEXP (x, 1);
8345
8346 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8347 this is a SIGN_EXTRACT. */
8348 if (CONST_INT_P (rhs)
8349 && GET_CODE (lhs) == ASHIFT
8350 && CONST_INT_P (XEXP (lhs, 1))
8351 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8352 && INTVAL (XEXP (lhs, 1)) >= 0
8353 && INTVAL (rhs) < mode_width)
8354 {
8355 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8356 new_rtx = make_extraction (mode, new_rtx,
8357 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8358 NULL_RTX, mode_width - INTVAL (rhs),
8359 code == LSHIFTRT, false,
8360 in_code == COMPARE);
8361 break;
8362 }
8363
8364 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8365 If so, try to merge the shifts into a SIGN_EXTEND. We could
8366 also do this for some cases of SIGN_EXTRACT, but it doesn't
8367 seem worth the effort; the case checked for occurs on Alpha. */
8368
8369 if (!OBJECT_P (lhs)
8370 && ! (GET_CODE (lhs) == SUBREG
8371 && (OBJECT_P (SUBREG_REG (lhs))))
8372 && CONST_INT_P (rhs)
8373 && INTVAL (rhs) >= 0
8374 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8375 && INTVAL (rhs) < mode_width
8376 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8377 new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8378 next_code),
8379 0, NULL_RTX, mode_width - INTVAL (rhs),
8380 code == LSHIFTRT, false, in_code == COMPARE);
8381
8382 break;
8383
8384 case SUBREG:
8385 /* Call ourselves recursively on the inner expression. If we are
8386 narrowing the object and it has a different RTL code from
8387 what it originally did, do this SUBREG as a force_to_mode. */
8388 {
8389 rtx inner = SUBREG_REG (x), simplified;
8390 enum rtx_code subreg_code = in_code;
8391
8392 /* If the SUBREG is masking of a logical right shift,
8393 make an extraction. */
8394 if (GET_CODE (inner) == LSHIFTRT
8395 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8396 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8397 && CONST_INT_P (XEXP (inner, 1))
8398 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8399 && subreg_lowpart_p (x))
8400 {
8401 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8402 int width = GET_MODE_PRECISION (inner_mode)
8403 - INTVAL (XEXP (inner, 1));
8404 if (width > mode_width)
8405 width = mode_width;
8406 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8407 width, true, false, in_code == COMPARE);
8408 break;
8409 }
8410
8411 /* If in_code is COMPARE, it isn't always safe to pass it through
8412 to the recursive make_compound_operation call. */
8413 if (subreg_code == COMPARE
8414 && (!subreg_lowpart_p (x)
8415 || GET_CODE (inner) == SUBREG
8416 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8417 is (const_int 0), rather than
8418 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8419 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8420 for non-equality comparisons against 0 is not equivalent
8421 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8422 || (GET_CODE (inner) == AND
8423 && CONST_INT_P (XEXP (inner, 1))
8424 && partial_subreg_p (x)
8425 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8426 >= GET_MODE_BITSIZE (mode) - 1)))
8427 subreg_code = SET;
8428
8429 tem = make_compound_operation (inner, subreg_code);
8430
8431 simplified
8432 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8433 if (simplified)
8434 tem = simplified;
8435
8436 if (GET_CODE (tem) != GET_CODE (inner)
8437 && partial_subreg_p (x)
8438 && subreg_lowpart_p (x))
8439 {
8440 rtx newer
8441 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, false);
8442
8443 /* If we have something other than a SUBREG, we might have
8444 done an expansion, so rerun ourselves. */
8445 if (GET_CODE (newer) != SUBREG)
8446 newer = make_compound_operation (newer, in_code);
8447
8448 /* force_to_mode can expand compounds. If it just re-expanded
8449 the compound, use gen_lowpart to convert to the desired
8450 mode. */
8451 if (rtx_equal_p (newer, x)
8452 /* Likewise if it re-expanded the compound only partially.
8453 This happens for SUBREG of ZERO_EXTRACT if they extract
8454 the same number of bits. */
8455 || (GET_CODE (newer) == SUBREG
8456 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8457 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8458 && GET_CODE (inner) == AND
8459 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8460 return gen_lowpart (GET_MODE (x), tem);
8461
8462 return newer;
8463 }
8464
8465 if (simplified)
8466 return tem;
8467 }
8468 break;
8469
8470 default:
8471 break;
8472 }
8473
8474 if (new_rtx)
8475 *x_ptr = gen_lowpart (mode, new_rtx);
8476 *next_code_ptr = next_code;
8477 return NULL_RTX;
8478 }
8479
8480 /* Look at the expression rooted at X. Look for expressions
8481 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8482 Form these expressions.
8483
8484 Return the new rtx, usually just X.
8485
8486 Also, for machines like the VAX that don't have logical shift insns,
8487 try to convert logical to arithmetic shift operations in cases where
8488 they are equivalent. This undoes the canonicalizations to logical
8489 shifts done elsewhere.
8490
8491 We try, as much as possible, to re-use rtl expressions to save memory.
8492
8493 IN_CODE says what kind of expression we are processing. Normally, it is
8494 SET. In a memory address it is MEM. When processing the arguments of
8495 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8496 precisely it is an equality comparison against zero. */
8497
8498 rtx
8499 make_compound_operation (rtx x, enum rtx_code in_code)
8500 {
8501 enum rtx_code code = GET_CODE (x);
8502 const char *fmt;
8503 int i, j;
8504 enum rtx_code next_code;
8505 rtx new_rtx, tem;
8506
8507 /* Select the code to be used in recursive calls. Once we are inside an
8508 address, we stay there. If we have a comparison, set to COMPARE,
8509 but once inside, go back to our default of SET. */
8510
8511 next_code = (code == MEM ? MEM
8512 : ((code == COMPARE || COMPARISON_P (x))
8513 && XEXP (x, 1) == const0_rtx) ? COMPARE
8514 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8515
8516 scalar_int_mode mode;
8517 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8518 {
8519 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8520 &next_code);
8521 if (new_rtx)
8522 return new_rtx;
8523 code = GET_CODE (x);
8524 }
8525
8526 /* Now recursively process each operand of this operation. We need to
8527 handle ZERO_EXTEND specially so that we don't lose track of the
8528 inner mode. */
8529 if (code == ZERO_EXTEND)
8530 {
8531 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8532 tem = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8533 new_rtx, GET_MODE (XEXP (x, 0)));
8534 if (tem)
8535 return tem;
8536 SUBST (XEXP (x, 0), new_rtx);
8537 return x;
8538 }
8539
8540 fmt = GET_RTX_FORMAT (code);
8541 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8542 if (fmt[i] == 'e')
8543 {
8544 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8545 SUBST (XEXP (x, i), new_rtx);
8546 }
8547 else if (fmt[i] == 'E')
8548 for (j = 0; j < XVECLEN (x, i); j++)
8549 {
8550 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8551 SUBST (XVECEXP (x, i, j), new_rtx);
8552 }
8553
8554 maybe_swap_commutative_operands (x);
8555 return x;
8556 }
8557
8558 /* Given M see if it is a value that would select a field of bits
8559 within an item, but not the entire word. Return -1 if not.
8560 Otherwise, return the starting position of the field, where 0 is the
8561 low-order bit.
8562
8563 *PLEN is set to the length of the field. */
8564
8565 static int
8566 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8567 {
8568 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8569 int pos = m ? ctz_hwi (m) : -1;
8570 int len = 0;
8571
8572 if (pos >= 0)
8573 /* Now shift off the low-order zero bits and see if we have a
8574 power of two minus 1. */
8575 len = exact_log2 ((m >> pos) + 1);
8576
8577 if (len <= 0)
8578 pos = -1;
8579
8580 *plen = len;
8581 return pos;
8582 }
8583
8584 /* If X refers to a register that equals REG in value, replace these
8585 references with REG. */
8586 static rtx
8587 canon_reg_for_combine (rtx x, rtx reg)
8588 {
8589 rtx op0, op1, op2;
8590 const char *fmt;
8591 int i;
8592 bool copied;
8593
8594 enum rtx_code code = GET_CODE (x);
8595 switch (GET_RTX_CLASS (code))
8596 {
8597 case RTX_UNARY:
8598 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8599 if (op0 != XEXP (x, 0))
8600 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8601 GET_MODE (reg));
8602 break;
8603
8604 case RTX_BIN_ARITH:
8605 case RTX_COMM_ARITH:
8606 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8607 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8608 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8609 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8610 break;
8611
8612 case RTX_COMPARE:
8613 case RTX_COMM_COMPARE:
8614 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8615 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8616 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8617 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8618 GET_MODE (op0), op0, op1);
8619 break;
8620
8621 case RTX_TERNARY:
8622 case RTX_BITFIELD_OPS:
8623 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8624 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8625 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8626 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8627 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8628 GET_MODE (op0), op0, op1, op2);
8629 /* FALLTHRU */
8630
8631 case RTX_OBJ:
8632 if (REG_P (x))
8633 {
8634 if (rtx_equal_p (get_last_value (reg), x)
8635 || rtx_equal_p (reg, get_last_value (x)))
8636 return reg;
8637 else
8638 break;
8639 }
8640
8641 /* fall through */
8642
8643 default:
8644 fmt = GET_RTX_FORMAT (code);
8645 copied = false;
8646 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8647 if (fmt[i] == 'e')
8648 {
8649 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8650 if (op != XEXP (x, i))
8651 {
8652 if (!copied)
8653 {
8654 copied = true;
8655 x = copy_rtx (x);
8656 }
8657 XEXP (x, i) = op;
8658 }
8659 }
8660 else if (fmt[i] == 'E')
8661 {
8662 int j;
8663 for (j = 0; j < XVECLEN (x, i); j++)
8664 {
8665 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8666 if (op != XVECEXP (x, i, j))
8667 {
8668 if (!copied)
8669 {
8670 copied = true;
8671 x = copy_rtx (x);
8672 }
8673 XVECEXP (x, i, j) = op;
8674 }
8675 }
8676 }
8677
8678 break;
8679 }
8680
8681 return x;
8682 }
8683
8684 /* Return X converted to MODE. If the value is already truncated to
8685 MODE we can just return a subreg even though in the general case we
8686 would need an explicit truncation. */
8687
8688 static rtx
8689 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8690 {
8691 if (!CONST_INT_P (x)
8692 && partial_subreg_p (mode, GET_MODE (x))
8693 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8694 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8695 {
8696 /* Bit-cast X into an integer mode. */
8697 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8698 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8699 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8700 x, GET_MODE (x));
8701 }
8702
8703 return gen_lowpart (mode, x);
8704 }
8705
8706 /* See if X can be simplified knowing that we will only refer to it in
8707 MODE and will only refer to those bits that are nonzero in MASK.
8708 If other bits are being computed or if masking operations are done
8709 that select a superset of the bits in MASK, they can sometimes be
8710 ignored.
8711
8712 Return a possibly simplified expression, but always convert X to
8713 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8714
8715 If JUST_SELECT is true, don't optimize by noticing that bits in MASK
8716 are all off in X. This is used when X will be complemented, by either
8717 NOT, NEG, or XOR. */
8718
8719 static rtx
8720 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8721 bool just_select)
8722 {
8723 enum rtx_code code = GET_CODE (x);
8724 bool next_select = just_select || code == XOR || code == NOT || code == NEG;
8725 machine_mode op_mode;
8726 unsigned HOST_WIDE_INT nonzero;
8727
8728 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8729 code below will do the wrong thing since the mode of such an
8730 expression is VOIDmode.
8731
8732 Also do nothing if X is a CLOBBER; this can happen if X was
8733 the return value from a call to gen_lowpart. */
8734 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8735 return x;
8736
8737 /* We want to perform the operation in its present mode unless we know
8738 that the operation is valid in MODE, in which case we do the operation
8739 in MODE. */
8740 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8741 && have_insn_for (code, mode))
8742 ? mode : GET_MODE (x));
8743
8744 /* It is not valid to do a right-shift in a narrower mode
8745 than the one it came in with. */
8746 if ((code == LSHIFTRT || code == ASHIFTRT)
8747 && partial_subreg_p (mode, GET_MODE (x)))
8748 op_mode = GET_MODE (x);
8749
8750 /* Truncate MASK to fit OP_MODE. */
8751 if (op_mode)
8752 mask &= GET_MODE_MASK (op_mode);
8753
8754 /* Determine what bits of X are guaranteed to be (non)zero. */
8755 nonzero = nonzero_bits (x, mode);
8756
8757 /* If none of the bits in X are needed, return a zero. */
8758 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8759 x = const0_rtx;
8760
8761 /* If X is a CONST_INT, return a new one. Do this here since the
8762 test below will fail. */
8763 if (CONST_INT_P (x))
8764 {
8765 if (SCALAR_INT_MODE_P (mode))
8766 return gen_int_mode (INTVAL (x) & mask, mode);
8767 else
8768 {
8769 x = GEN_INT (INTVAL (x) & mask);
8770 return gen_lowpart_common (mode, x);
8771 }
8772 }
8773
8774 /* If X is narrower than MODE and we want all the bits in X's mode, just
8775 get X in the proper mode. */
8776 if (paradoxical_subreg_p (mode, GET_MODE (x))
8777 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8778 return gen_lowpart (mode, x);
8779
8780 /* We can ignore the effect of a SUBREG if it narrows the mode or
8781 if the constant masks to zero all the bits the mode doesn't have. */
8782 if (GET_CODE (x) == SUBREG
8783 && subreg_lowpart_p (x)
8784 && (partial_subreg_p (x)
8785 || (mask
8786 & GET_MODE_MASK (GET_MODE (x))
8787 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8788 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8789
8790 scalar_int_mode int_mode, xmode;
8791 if (is_a <scalar_int_mode> (mode, &int_mode)
8792 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8793 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8794 integer too. */
8795 return force_int_to_mode (x, int_mode, xmode,
8796 as_a <scalar_int_mode> (op_mode),
8797 mask, just_select);
8798
8799 return gen_lowpart_or_truncate (mode, x);
8800 }
8801
8802 /* Subroutine of force_to_mode that handles cases in which both X and
8803 the result are scalar integers. MODE is the mode of the result,
8804 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8805 is preferred for simplified versions of X. The other arguments
8806 are as for force_to_mode. */
8807
8808 static rtx
8809 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8810 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8811 bool just_select)
8812 {
8813 enum rtx_code code = GET_CODE (x);
8814 bool next_select = just_select || code == XOR || code == NOT || code == NEG;
8815 unsigned HOST_WIDE_INT fuller_mask;
8816 rtx op0, op1, temp;
8817 poly_int64 const_op0;
8818
8819 /* When we have an arithmetic operation, or a shift whose count we
8820 do not know, we need to assume that all bits up to the highest-order
8821 bit in MASK will be needed. This is how we form such a mask. */
8822 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8823 fuller_mask = HOST_WIDE_INT_M1U;
8824 else
8825 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1)) - 1);
8826
8827 switch (code)
8828 {
8829 case CLOBBER:
8830 /* If X is a (clobber (const_int)), return it since we know we are
8831 generating something that won't match. */
8832 return x;
8833
8834 case SIGN_EXTEND:
8835 case ZERO_EXTEND:
8836 case ZERO_EXTRACT:
8837 case SIGN_EXTRACT:
8838 x = expand_compound_operation (x);
8839 if (GET_CODE (x) != code)
8840 return force_to_mode (x, mode, mask, next_select);
8841 break;
8842
8843 case TRUNCATE:
8844 /* Similarly for a truncate. */
8845 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8846
8847 case AND:
8848 /* If this is an AND with a constant, convert it into an AND
8849 whose constant is the AND of that constant with MASK. If it
8850 remains an AND of MASK, delete it since it is redundant. */
8851
8852 if (CONST_INT_P (XEXP (x, 1)))
8853 {
8854 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8855 mask & INTVAL (XEXP (x, 1)));
8856 xmode = op_mode;
8857
8858 /* If X is still an AND, see if it is an AND with a mask that
8859 is just some low-order bits. If so, and it is MASK, we don't
8860 need it. */
8861
8862 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8863 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8864 x = XEXP (x, 0);
8865
8866 /* If it remains an AND, try making another AND with the bits
8867 in the mode mask that aren't in MASK turned on. If the
8868 constant in the AND is wide enough, this might make a
8869 cheaper constant. */
8870
8871 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8872 && GET_MODE_MASK (xmode) != mask
8873 && HWI_COMPUTABLE_MODE_P (xmode))
8874 {
8875 unsigned HOST_WIDE_INT cval
8876 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8877 rtx y;
8878
8879 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8880 gen_int_mode (cval, xmode));
8881 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8882 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8883 x = y;
8884 }
8885
8886 break;
8887 }
8888
8889 goto binop;
8890
8891 case PLUS:
8892 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8893 low-order bits (as in an alignment operation) and FOO is already
8894 aligned to that boundary, mask C1 to that boundary as well.
8895 This may eliminate that PLUS and, later, the AND. */
8896
8897 {
8898 unsigned int width = GET_MODE_PRECISION (mode);
8899 unsigned HOST_WIDE_INT smask = mask;
8900
8901 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8902 number, sign extend it. */
8903
8904 if (width < HOST_BITS_PER_WIDE_INT
8905 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8906 smask |= HOST_WIDE_INT_M1U << width;
8907
8908 if (CONST_INT_P (XEXP (x, 1))
8909 && pow2p_hwi (- smask)
8910 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8911 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8912 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8913 (INTVAL (XEXP (x, 1)) & smask)),
8914 mode, smask, next_select);
8915 }
8916
8917 /* fall through */
8918
8919 case MULT:
8920 /* Substituting into the operands of a widening MULT is not likely to
8921 create RTL matching a machine insn. */
8922 if (code == MULT
8923 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8924 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8925 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8926 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8927 && REG_P (XEXP (XEXP (x, 0), 0))
8928 && REG_P (XEXP (XEXP (x, 1), 0)))
8929 return gen_lowpart_or_truncate (mode, x);
8930
8931 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8932 most significant bit in MASK since carries from those bits will
8933 affect the bits we are interested in. */
8934 mask = fuller_mask;
8935 goto binop;
8936
8937 case MINUS:
8938 /* If X is (minus C Y) where C's least set bit is larger than any bit
8939 in the mask, then we may replace with (neg Y). */
8940 if (poly_int_rtx_p (XEXP (x, 0), &const_op0)
8941 && known_alignment (poly_uint64 (const_op0)) > mask)
8942 {
8943 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8944 return force_to_mode (x, mode, mask, next_select);
8945 }
8946
8947 /* Similarly, if C contains every bit in the fuller_mask, then we may
8948 replace with (not Y). */
8949 if (CONST_INT_P (XEXP (x, 0))
8950 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8951 {
8952 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8953 return force_to_mode (x, mode, mask, next_select);
8954 }
8955
8956 mask = fuller_mask;
8957 goto binop;
8958
8959 case IOR:
8960 case XOR:
8961 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8962 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8963 operation which may be a bitfield extraction. Ensure that the
8964 constant we form is not wider than the mode of X. */
8965
8966 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8967 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8968 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8969 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8970 && CONST_INT_P (XEXP (x, 1))
8971 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8972 + floor_log2 (INTVAL (XEXP (x, 1))))
8973 < GET_MODE_PRECISION (xmode))
8974 && (UINTVAL (XEXP (x, 1))
8975 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8976 {
8977 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8978 << INTVAL (XEXP (XEXP (x, 0), 1)),
8979 xmode);
8980 temp = simplify_gen_binary (GET_CODE (x), xmode,
8981 XEXP (XEXP (x, 0), 0), temp);
8982 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8983 XEXP (XEXP (x, 0), 1));
8984 return force_to_mode (x, mode, mask, next_select);
8985 }
8986
8987 binop:
8988 /* For most binary operations, just propagate into the operation and
8989 change the mode if we have an operation of that mode. */
8990
8991 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8992 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8993
8994 /* If we ended up truncating both operands, truncate the result of the
8995 operation instead. */
8996 if (GET_CODE (op0) == TRUNCATE
8997 && GET_CODE (op1) == TRUNCATE)
8998 {
8999 op0 = XEXP (op0, 0);
9000 op1 = XEXP (op1, 0);
9001 }
9002
9003 op0 = gen_lowpart_or_truncate (op_mode, op0);
9004 op1 = gen_lowpart_or_truncate (op_mode, op1);
9005
9006 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
9007 {
9008 x = simplify_gen_binary (code, op_mode, op0, op1);
9009 xmode = op_mode;
9010 }
9011 break;
9012
9013 case ASHIFT:
9014 /* For left shifts, do the same, but just for the first operand.
9015 However, we cannot do anything with shifts where we cannot
9016 guarantee that the counts are smaller than the size of the mode
9017 because such a count will have a different meaning in a
9018 wider mode. */
9019
9020 if (! (CONST_INT_P (XEXP (x, 1))
9021 && INTVAL (XEXP (x, 1)) >= 0
9022 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
9023 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
9024 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
9025 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
9026 break;
9027
9028 /* If the shift count is a constant and we can do arithmetic in
9029 the mode of the shift, refine which bits we need. Otherwise, use the
9030 conservative form of the mask. */
9031 if (CONST_INT_P (XEXP (x, 1))
9032 && INTVAL (XEXP (x, 1)) >= 0
9033 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
9034 && HWI_COMPUTABLE_MODE_P (op_mode))
9035 mask >>= INTVAL (XEXP (x, 1));
9036 else
9037 mask = fuller_mask;
9038
9039 op0 = gen_lowpart_or_truncate (op_mode,
9040 force_to_mode (XEXP (x, 0), mode,
9041 mask, next_select));
9042
9043 if (op_mode != xmode || op0 != XEXP (x, 0))
9044 {
9045 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
9046 xmode = op_mode;
9047 }
9048 break;
9049
9050 case LSHIFTRT:
9051 /* Here we can only do something if the shift count is a constant,
9052 this shift constant is valid for the host, and we can do arithmetic
9053 in OP_MODE. */
9054
9055 if (CONST_INT_P (XEXP (x, 1))
9056 && INTVAL (XEXP (x, 1)) >= 0
9057 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
9058 && HWI_COMPUTABLE_MODE_P (op_mode))
9059 {
9060 rtx inner = XEXP (x, 0);
9061 unsigned HOST_WIDE_INT inner_mask;
9062
9063 /* Select the mask of the bits we need for the shift operand. */
9064 inner_mask = mask << INTVAL (XEXP (x, 1));
9065
9066 /* We can only change the mode of the shift if we can do arithmetic
9067 in the mode of the shift and INNER_MASK is no wider than the
9068 width of X's mode. */
9069 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
9070 op_mode = xmode;
9071
9072 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
9073
9074 if (xmode != op_mode || inner != XEXP (x, 0))
9075 {
9076 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
9077 xmode = op_mode;
9078 }
9079 }
9080
9081 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
9082 shift and AND produces only copies of the sign bit (C2 is one less
9083 than a power of two), we can do this with just a shift. */
9084
9085 if (GET_CODE (x) == LSHIFTRT
9086 && CONST_INT_P (XEXP (x, 1))
9087 /* The shift puts one of the sign bit copies in the least significant
9088 bit. */
9089 && ((INTVAL (XEXP (x, 1))
9090 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
9091 >= GET_MODE_PRECISION (xmode))
9092 && pow2p_hwi (mask + 1)
9093 /* Number of bits left after the shift must be more than the mask
9094 needs. */
9095 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
9096 <= GET_MODE_PRECISION (xmode))
9097 /* Must be more sign bit copies than the mask needs. */
9098 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
9099 >= exact_log2 (mask + 1)))
9100 {
9101 int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
9102 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
9103 gen_int_shift_amount (xmode, nbits));
9104 }
9105 goto shiftrt;
9106
9107 case ASHIFTRT:
9108 /* If we are just looking for the sign bit, we don't need this shift at
9109 all, even if it has a variable count. */
9110 if (val_signbit_p (xmode, mask))
9111 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9112
9113 /* If this is a shift by a constant, get a mask that contains those bits
9114 that are not copies of the sign bit. We then have two cases: If
9115 MASK only includes those bits, this can be a logical shift, which may
9116 allow simplifications. If MASK is a single-bit field not within
9117 those bits, we are requesting a copy of the sign bit and hence can
9118 shift the sign bit to the appropriate location. */
9119
9120 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9121 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9122 {
9123 unsigned HOST_WIDE_INT nonzero;
9124 int i;
9125
9126 /* If the considered data is wider than HOST_WIDE_INT, we can't
9127 represent a mask for all its bits in a single scalar.
9128 But we only care about the lower bits, so calculate these. */
9129
9130 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9131 {
9132 nonzero = HOST_WIDE_INT_M1U;
9133
9134 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9135 is the number of bits a full-width mask would have set.
9136 We need only shift if these are fewer than nonzero can
9137 hold. If not, we must keep all bits set in nonzero. */
9138
9139 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9140 < HOST_BITS_PER_WIDE_INT)
9141 nonzero >>= INTVAL (XEXP (x, 1))
9142 + HOST_BITS_PER_WIDE_INT
9143 - GET_MODE_PRECISION (xmode);
9144 }
9145 else
9146 {
9147 nonzero = GET_MODE_MASK (xmode);
9148 nonzero >>= INTVAL (XEXP (x, 1));
9149 }
9150
9151 if ((mask & ~nonzero) == 0)
9152 {
9153 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9154 XEXP (x, 0), INTVAL (XEXP (x, 1)));
9155 if (GET_CODE (x) != ASHIFTRT)
9156 return force_to_mode (x, mode, mask, next_select);
9157 }
9158
9159 else if ((i = exact_log2 (mask)) >= 0)
9160 {
9161 x = simplify_shift_const
9162 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9163 GET_MODE_PRECISION (xmode) - 1 - i);
9164
9165 if (GET_CODE (x) != ASHIFTRT)
9166 return force_to_mode (x, mode, mask, next_select);
9167 }
9168 }
9169
9170 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
9171 even if the shift count isn't a constant. */
9172 if (mask == 1)
9173 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9174
9175 shiftrt:
9176
9177 /* If this is a zero- or sign-extension operation that just affects bits
9178 we don't care about, remove it. Be sure the call above returned
9179 something that is still a shift. */
9180
9181 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9182 && CONST_INT_P (XEXP (x, 1))
9183 && INTVAL (XEXP (x, 1)) >= 0
9184 && (INTVAL (XEXP (x, 1))
9185 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9186 && GET_CODE (XEXP (x, 0)) == ASHIFT
9187 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9188 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask, next_select);
9189
9190 break;
9191
9192 case ROTATE:
9193 case ROTATERT:
9194 /* If the shift count is constant and we can do computations
9195 in the mode of X, compute where the bits we care about are.
9196 Otherwise, we can't do anything. Don't change the mode of
9197 the shift or propagate MODE into the shift, though. */
9198 if (CONST_INT_P (XEXP (x, 1))
9199 && INTVAL (XEXP (x, 1)) >= 0)
9200 {
9201 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9202 xmode, gen_int_mode (mask, xmode),
9203 XEXP (x, 1));
9204 if (temp && CONST_INT_P (temp))
9205 x = simplify_gen_binary (code, xmode,
9206 force_to_mode (XEXP (x, 0), xmode,
9207 INTVAL (temp), next_select),
9208 XEXP (x, 1));
9209 }
9210 break;
9211
9212 case NEG:
9213 /* If we just want the low-order bit, the NEG isn't needed since it
9214 won't change the low-order bit. */
9215 if (mask == 1)
9216 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9217
9218 /* We need any bits less significant than the most significant bit in
9219 MASK since carries from those bits will affect the bits we are
9220 interested in. */
9221 mask = fuller_mask;
9222 goto unop;
9223
9224 case NOT:
9225 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9226 same as the XOR case above. Ensure that the constant we form is not
9227 wider than the mode of X. */
9228
9229 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9230 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9231 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9232 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9233 < GET_MODE_PRECISION (xmode))
9234 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9235 {
9236 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9237 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9238 x = simplify_gen_binary (LSHIFTRT, xmode,
9239 temp, XEXP (XEXP (x, 0), 1));
9240
9241 return force_to_mode (x, mode, mask, next_select);
9242 }
9243
9244 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9245 use the full mask inside the NOT. */
9246 mask = fuller_mask;
9247
9248 unop:
9249 op0 = gen_lowpart_or_truncate (op_mode,
9250 force_to_mode (XEXP (x, 0), mode, mask,
9251 next_select));
9252 if (op_mode != xmode || op0 != XEXP (x, 0))
9253 {
9254 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9255 xmode = op_mode;
9256 }
9257 break;
9258
9259 case NE:
9260 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9261 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9262 which is equal to STORE_FLAG_VALUE. */
9263 if ((mask & ~STORE_FLAG_VALUE) == 0
9264 && XEXP (x, 1) == const0_rtx
9265 && GET_MODE (XEXP (x, 0)) == mode
9266 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9267 && (nonzero_bits (XEXP (x, 0), mode)
9268 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9269 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9270
9271 break;
9272
9273 case IF_THEN_ELSE:
9274 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9275 written in a narrower mode. We play it safe and do not do so. */
9276
9277 op0 = gen_lowpart_or_truncate (xmode,
9278 force_to_mode (XEXP (x, 1), mode,
9279 mask, next_select));
9280 op1 = gen_lowpart_or_truncate (xmode,
9281 force_to_mode (XEXP (x, 2), mode,
9282 mask, next_select));
9283 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9284 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9285 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9286 op0, op1);
9287 break;
9288
9289 default:
9290 break;
9291 }
9292
9293 /* Ensure we return a value of the proper mode. */
9294 return gen_lowpart_or_truncate (mode, x);
9295 }
9296
9297 /* Return nonzero if X is an expression that has one of two values depending on
9298 whether some other value is zero or nonzero. In that case, we return the
9299 value that is being tested, *PTRUE is set to the value if the rtx being
9300 returned has a nonzero value, and *PFALSE is set to the other alternative.
9301
9302 If we return zero, we set *PTRUE and *PFALSE to X. */
9303
9304 static rtx
9305 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9306 {
9307 machine_mode mode = GET_MODE (x);
9308 enum rtx_code code = GET_CODE (x);
9309 rtx cond0, cond1, true0, true1, false0, false1;
9310 unsigned HOST_WIDE_INT nz;
9311 scalar_int_mode int_mode;
9312
9313 /* If we are comparing a value against zero, we are done. */
9314 if ((code == NE || code == EQ)
9315 && XEXP (x, 1) == const0_rtx)
9316 {
9317 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9318 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9319 return XEXP (x, 0);
9320 }
9321
9322 /* If this is a unary operation whose operand has one of two values, apply
9323 our opcode to compute those values. */
9324 else if (UNARY_P (x)
9325 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9326 {
9327 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9328 *pfalse = simplify_gen_unary (code, mode, false0,
9329 GET_MODE (XEXP (x, 0)));
9330 return cond0;
9331 }
9332
9333 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9334 make can't possibly match and would suppress other optimizations. */
9335 else if (code == COMPARE)
9336 ;
9337
9338 /* If this is a binary operation, see if either side has only one of two
9339 values. If either one does or if both do and they are conditional on
9340 the same value, compute the new true and false values. */
9341 else if (BINARY_P (x))
9342 {
9343 rtx op0 = XEXP (x, 0);
9344 rtx op1 = XEXP (x, 1);
9345 cond0 = if_then_else_cond (op0, &true0, &false0);
9346 cond1 = if_then_else_cond (op1, &true1, &false1);
9347
9348 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9349 && (REG_P (op0) || REG_P (op1)))
9350 {
9351 /* Try to enable a simplification by undoing work done by
9352 if_then_else_cond if it converted a REG into something more
9353 complex. */
9354 if (REG_P (op0))
9355 {
9356 cond0 = 0;
9357 true0 = false0 = op0;
9358 }
9359 else
9360 {
9361 cond1 = 0;
9362 true1 = false1 = op1;
9363 }
9364 }
9365
9366 if ((cond0 != 0 || cond1 != 0)
9367 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9368 {
9369 /* If if_then_else_cond returned zero, then true/false are the
9370 same rtl. We must copy one of them to prevent invalid rtl
9371 sharing. */
9372 if (cond0 == 0)
9373 true0 = copy_rtx (true0);
9374 else if (cond1 == 0)
9375 true1 = copy_rtx (true1);
9376
9377 if (COMPARISON_P (x))
9378 {
9379 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9380 true0, true1);
9381 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9382 false0, false1);
9383 }
9384 else
9385 {
9386 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9387 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9388 }
9389
9390 return cond0 ? cond0 : cond1;
9391 }
9392
9393 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9394 operands is zero when the other is nonzero, and vice-versa,
9395 and STORE_FLAG_VALUE is 1 or -1. */
9396
9397 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9398 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9399 || code == UMAX)
9400 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9401 {
9402 rtx op0 = XEXP (XEXP (x, 0), 1);
9403 rtx op1 = XEXP (XEXP (x, 1), 1);
9404
9405 cond0 = XEXP (XEXP (x, 0), 0);
9406 cond1 = XEXP (XEXP (x, 1), 0);
9407
9408 if (COMPARISON_P (cond0)
9409 && COMPARISON_P (cond1)
9410 && SCALAR_INT_MODE_P (mode)
9411 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9412 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9413 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9414 || ((swap_condition (GET_CODE (cond0))
9415 == reversed_comparison_code (cond1, NULL))
9416 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9417 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9418 && ! side_effects_p (x))
9419 {
9420 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9421 *pfalse = simplify_gen_binary (MULT, mode,
9422 (code == MINUS
9423 ? simplify_gen_unary (NEG, mode,
9424 op1, mode)
9425 : op1),
9426 const_true_rtx);
9427 return cond0;
9428 }
9429 }
9430
9431 /* Similarly for MULT, AND and UMIN, except that for these the result
9432 is always zero. */
9433 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9434 && (code == MULT || code == AND || code == UMIN)
9435 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9436 {
9437 cond0 = XEXP (XEXP (x, 0), 0);
9438 cond1 = XEXP (XEXP (x, 1), 0);
9439
9440 if (COMPARISON_P (cond0)
9441 && COMPARISON_P (cond1)
9442 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9443 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9444 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9445 || ((swap_condition (GET_CODE (cond0))
9446 == reversed_comparison_code (cond1, NULL))
9447 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9448 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9449 && ! side_effects_p (x))
9450 {
9451 *ptrue = *pfalse = const0_rtx;
9452 return cond0;
9453 }
9454 }
9455 }
9456
9457 else if (code == IF_THEN_ELSE)
9458 {
9459 /* If we have IF_THEN_ELSE already, extract the condition and
9460 canonicalize it if it is NE or EQ. */
9461 cond0 = XEXP (x, 0);
9462 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9463 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9464 return XEXP (cond0, 0);
9465 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9466 {
9467 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9468 return XEXP (cond0, 0);
9469 }
9470 else
9471 return cond0;
9472 }
9473
9474 /* If X is a SUBREG, we can narrow both the true and false values
9475 if the inner expression, if there is a condition. */
9476 else if (code == SUBREG
9477 && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9478 &false0)) != 0)
9479 {
9480 true0 = simplify_gen_subreg (mode, true0,
9481 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9482 false0 = simplify_gen_subreg (mode, false0,
9483 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9484 if (true0 && false0)
9485 {
9486 *ptrue = true0;
9487 *pfalse = false0;
9488 return cond0;
9489 }
9490 }
9491
9492 /* If X is a constant, this isn't special and will cause confusions
9493 if we treat it as such. Likewise if it is equivalent to a constant. */
9494 else if (CONSTANT_P (x)
9495 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9496 ;
9497
9498 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9499 will be least confusing to the rest of the compiler. */
9500 else if (mode == BImode)
9501 {
9502 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9503 return x;
9504 }
9505
9506 /* If X is known to be either 0 or -1, those are the true and
9507 false values when testing X. */
9508 else if (x == constm1_rtx || x == const0_rtx
9509 || (is_a <scalar_int_mode> (mode, &int_mode)
9510 && (num_sign_bit_copies (x, int_mode)
9511 == GET_MODE_PRECISION (int_mode))))
9512 {
9513 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9514 return x;
9515 }
9516
9517 /* Likewise for 0 or a single bit. */
9518 else if (HWI_COMPUTABLE_MODE_P (mode)
9519 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9520 {
9521 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9522 return x;
9523 }
9524
9525 /* Otherwise fail; show no condition with true and false values the same. */
9526 *ptrue = *pfalse = x;
9527 return 0;
9528 }
9529
9530 /* Return the value of expression X given the fact that condition COND
9531 is known to be true when applied to REG as its first operand and VAL
9532 as its second. X is known to not be shared and so can be modified in
9533 place.
9534
9535 We only handle the simplest cases, and specifically those cases that
9536 arise with IF_THEN_ELSE expressions. */
9537
9538 static rtx
9539 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9540 {
9541 enum rtx_code code = GET_CODE (x);
9542 const char *fmt;
9543 int i, j;
9544
9545 if (side_effects_p (x))
9546 return x;
9547
9548 /* If either operand of the condition is a floating point value,
9549 then we have to avoid collapsing an EQ comparison. */
9550 if (cond == EQ
9551 && rtx_equal_p (x, reg)
9552 && ! FLOAT_MODE_P (GET_MODE (x))
9553 && ! FLOAT_MODE_P (GET_MODE (val)))
9554 return val;
9555
9556 if (cond == UNEQ && rtx_equal_p (x, reg))
9557 return val;
9558
9559 /* If X is (abs REG) and we know something about REG's relationship
9560 with zero, we may be able to simplify this. */
9561
9562 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9563 switch (cond)
9564 {
9565 case GE: case GT: case EQ:
9566 return XEXP (x, 0);
9567 case LT: case LE:
9568 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9569 XEXP (x, 0),
9570 GET_MODE (XEXP (x, 0)));
9571 default:
9572 break;
9573 }
9574
9575 /* The only other cases we handle are MIN, MAX, and comparisons if the
9576 operands are the same as REG and VAL. */
9577
9578 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9579 {
9580 if (rtx_equal_p (XEXP (x, 0), val))
9581 {
9582 std::swap (val, reg);
9583 cond = swap_condition (cond);
9584 }
9585
9586 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9587 {
9588 if (COMPARISON_P (x))
9589 {
9590 if (comparison_dominates_p (cond, code))
9591 return VECTOR_MODE_P (GET_MODE (x)) ? x : const_true_rtx;
9592
9593 code = reversed_comparison_code (x, NULL);
9594 if (code != UNKNOWN
9595 && comparison_dominates_p (cond, code))
9596 return CONST0_RTX (GET_MODE (x));
9597 else
9598 return x;
9599 }
9600 else if (code == SMAX || code == SMIN
9601 || code == UMIN || code == UMAX)
9602 {
9603 int unsignedp = (code == UMIN || code == UMAX);
9604
9605 /* Do not reverse the condition when it is NE or EQ.
9606 This is because we cannot conclude anything about
9607 the value of 'SMAX (x, y)' when x is not equal to y,
9608 but we can when x equals y. */
9609 if ((code == SMAX || code == UMAX)
9610 && ! (cond == EQ || cond == NE))
9611 cond = reverse_condition (cond);
9612
9613 switch (cond)
9614 {
9615 case GE: case GT:
9616 return unsignedp ? x : XEXP (x, 1);
9617 case LE: case LT:
9618 return unsignedp ? x : XEXP (x, 0);
9619 case GEU: case GTU:
9620 return unsignedp ? XEXP (x, 1) : x;
9621 case LEU: case LTU:
9622 return unsignedp ? XEXP (x, 0) : x;
9623 default:
9624 break;
9625 }
9626 }
9627 }
9628 }
9629 else if (code == SUBREG)
9630 {
9631 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9632 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9633
9634 if (SUBREG_REG (x) != r)
9635 {
9636 /* We must simplify subreg here, before we lose track of the
9637 original inner_mode. */
9638 new_rtx = simplify_subreg (GET_MODE (x), r,
9639 inner_mode, SUBREG_BYTE (x));
9640 if (new_rtx)
9641 return new_rtx;
9642 else
9643 SUBST (SUBREG_REG (x), r);
9644 }
9645
9646 return x;
9647 }
9648 /* We don't have to handle SIGN_EXTEND here, because even in the
9649 case of replacing something with a modeless CONST_INT, a
9650 CONST_INT is already (supposed to be) a valid sign extension for
9651 its narrower mode, which implies it's already properly
9652 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9653 story is different. */
9654 else if (code == ZERO_EXTEND)
9655 {
9656 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9657 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9658
9659 if (XEXP (x, 0) != r)
9660 {
9661 /* We must simplify the zero_extend here, before we lose
9662 track of the original inner_mode. */
9663 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9664 r, inner_mode);
9665 if (new_rtx)
9666 return new_rtx;
9667 else
9668 SUBST (XEXP (x, 0), r);
9669 }
9670
9671 return x;
9672 }
9673
9674 fmt = GET_RTX_FORMAT (code);
9675 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9676 {
9677 if (fmt[i] == 'e')
9678 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9679 else if (fmt[i] == 'E')
9680 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9681 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9682 cond, reg, val));
9683 }
9684
9685 return x;
9686 }
9687
9688 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9689 assignment as a field assignment. */
9690
9691 static bool
9692 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9693 {
9694 if (widen_x && GET_MODE (x) != GET_MODE (y))
9695 {
9696 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9697 return false;
9698 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9699 return false;
9700 x = adjust_address_nv (x, GET_MODE (y),
9701 byte_lowpart_offset (GET_MODE (y),
9702 GET_MODE (x)));
9703 }
9704
9705 if (x == y || rtx_equal_p (x, y))
9706 return true;
9707
9708 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9709 return false;
9710
9711 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9712 Note that all SUBREGs of MEM are paradoxical; otherwise they
9713 would have been rewritten. */
9714 if (MEM_P (x) && GET_CODE (y) == SUBREG
9715 && MEM_P (SUBREG_REG (y))
9716 && rtx_equal_p (SUBREG_REG (y),
9717 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9718 return true;
9719
9720 if (MEM_P (y) && GET_CODE (x) == SUBREG
9721 && MEM_P (SUBREG_REG (x))
9722 && rtx_equal_p (SUBREG_REG (x),
9723 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9724 return true;
9725
9726 /* We used to see if get_last_value of X and Y were the same but that's
9727 not correct. In one direction, we'll cause the assignment to have
9728 the wrong destination and in the case, we'll import a register into this
9729 insn that might have already have been dead. So fail if none of the
9730 above cases are true. */
9731 return false;
9732 }
9733
9734 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9735 Return that assignment if so.
9736
9737 We only handle the most common cases. */
9738
9739 static rtx
9740 make_field_assignment (rtx x)
9741 {
9742 rtx dest = SET_DEST (x);
9743 rtx src = SET_SRC (x);
9744 rtx assign;
9745 rtx rhs, lhs;
9746 HOST_WIDE_INT c1;
9747 HOST_WIDE_INT pos;
9748 unsigned HOST_WIDE_INT len;
9749 rtx other;
9750
9751 /* All the rules in this function are specific to scalar integers. */
9752 scalar_int_mode mode;
9753 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9754 return x;
9755
9756 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9757 a clear of a one-bit field. We will have changed it to
9758 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9759 for a SUBREG. */
9760
9761 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9762 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9763 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9764 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9765 {
9766 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9767 1, true, true, false);
9768 if (assign != 0)
9769 return gen_rtx_SET (assign, const0_rtx);
9770 return x;
9771 }
9772
9773 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9774 && subreg_lowpart_p (XEXP (src, 0))
9775 && partial_subreg_p (XEXP (src, 0))
9776 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9777 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9778 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9779 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9780 {
9781 assign = make_extraction (VOIDmode, dest, 0,
9782 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9783 1, true, true, false);
9784 if (assign != 0)
9785 return gen_rtx_SET (assign, const0_rtx);
9786 return x;
9787 }
9788
9789 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9790 one-bit field. */
9791 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9792 && XEXP (XEXP (src, 0), 0) == const1_rtx
9793 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9794 {
9795 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9796 1, true, true, false);
9797 if (assign != 0)
9798 return gen_rtx_SET (assign, const1_rtx);
9799 return x;
9800 }
9801
9802 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9803 SRC is an AND with all bits of that field set, then we can discard
9804 the AND. */
9805 if (GET_CODE (dest) == ZERO_EXTRACT
9806 && CONST_INT_P (XEXP (dest, 1))
9807 && GET_CODE (src) == AND
9808 && CONST_INT_P (XEXP (src, 1)))
9809 {
9810 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9811 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9812 unsigned HOST_WIDE_INT ze_mask;
9813
9814 if (width >= HOST_BITS_PER_WIDE_INT)
9815 ze_mask = -1;
9816 else
9817 ze_mask = (HOST_WIDE_INT_1U << width) - 1;
9818
9819 /* Complete overlap. We can remove the source AND. */
9820 if ((and_mask & ze_mask) == ze_mask)
9821 return gen_rtx_SET (dest, XEXP (src, 0));
9822
9823 /* Partial overlap. We can reduce the source AND. */
9824 if ((and_mask & ze_mask) != and_mask)
9825 {
9826 src = gen_rtx_AND (mode, XEXP (src, 0),
9827 gen_int_mode (and_mask & ze_mask, mode));
9828 return gen_rtx_SET (dest, src);
9829 }
9830 }
9831
9832 /* The other case we handle is assignments into a constant-position
9833 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9834 a mask that has all one bits except for a group of zero bits and
9835 OTHER is known to have zeros where C1 has ones, this is such an
9836 assignment. Compute the position and length from C1. Shift OTHER
9837 to the appropriate position, force it to the required mode, and
9838 make the extraction. Check for the AND in both operands. */
9839
9840 /* One or more SUBREGs might obscure the constant-position field
9841 assignment. The first one we are likely to encounter is an outer
9842 narrowing SUBREG, which we can just strip for the purposes of
9843 identifying the constant-field assignment. */
9844 scalar_int_mode src_mode = mode;
9845 if (GET_CODE (src) == SUBREG
9846 && subreg_lowpart_p (src)
9847 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9848 src = SUBREG_REG (src);
9849
9850 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9851 return x;
9852
9853 rhs = expand_compound_operation (XEXP (src, 0));
9854 lhs = expand_compound_operation (XEXP (src, 1));
9855
9856 if (GET_CODE (rhs) == AND
9857 && CONST_INT_P (XEXP (rhs, 1))
9858 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9859 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9860 /* The second SUBREG that might get in the way is a paradoxical
9861 SUBREG around the first operand of the AND. We want to
9862 pretend the operand is as wide as the destination here. We
9863 do this by adjusting the MEM to wider mode for the sole
9864 purpose of the call to rtx_equal_for_field_assignment_p. Also
9865 note this trick only works for MEMs. */
9866 else if (GET_CODE (rhs) == AND
9867 && paradoxical_subreg_p (XEXP (rhs, 0))
9868 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9869 && CONST_INT_P (XEXP (rhs, 1))
9870 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9871 dest, true))
9872 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9873 else if (GET_CODE (lhs) == AND
9874 && CONST_INT_P (XEXP (lhs, 1))
9875 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9876 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9877 /* The second SUBREG that might get in the way is a paradoxical
9878 SUBREG around the first operand of the AND. We want to
9879 pretend the operand is as wide as the destination here. We
9880 do this by adjusting the MEM to wider mode for the sole
9881 purpose of the call to rtx_equal_for_field_assignment_p. Also
9882 note this trick only works for MEMs. */
9883 else if (GET_CODE (lhs) == AND
9884 && paradoxical_subreg_p (XEXP (lhs, 0))
9885 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9886 && CONST_INT_P (XEXP (lhs, 1))
9887 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9888 dest, true))
9889 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9890 else
9891 return x;
9892
9893 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9894 if (pos < 0
9895 || pos + len > GET_MODE_PRECISION (mode)
9896 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9897 || (c1 & nonzero_bits (other, mode)) != 0)
9898 return x;
9899
9900 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len,
9901 true, true, false);
9902 if (assign == 0)
9903 return x;
9904
9905 /* The mode to use for the source is the mode of the assignment, or of
9906 what is inside a possible STRICT_LOW_PART. */
9907 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9908 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9909
9910 /* Shift OTHER right POS places and make it the source, restricting it
9911 to the proper length and mode. */
9912
9913 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9914 src_mode, other, pos),
9915 dest);
9916 src = force_to_mode (src, new_mode,
9917 len >= HOST_BITS_PER_WIDE_INT
9918 ? HOST_WIDE_INT_M1U
9919 : (HOST_WIDE_INT_1U << len) - 1, false);
9920
9921 /* If SRC is masked by an AND that does not make a difference in
9922 the value being stored, strip it. */
9923 if (GET_CODE (assign) == ZERO_EXTRACT
9924 && CONST_INT_P (XEXP (assign, 1))
9925 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9926 && GET_CODE (src) == AND
9927 && CONST_INT_P (XEXP (src, 1))
9928 && UINTVAL (XEXP (src, 1))
9929 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9930 src = XEXP (src, 0);
9931
9932 return gen_rtx_SET (assign, src);
9933 }
9934
9935 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9936 if so. */
9937
9938 static rtx
9939 apply_distributive_law (rtx x)
9940 {
9941 enum rtx_code code = GET_CODE (x);
9942 enum rtx_code inner_code;
9943 rtx lhs, rhs, other;
9944 rtx tem;
9945
9946 /* Distributivity is not true for floating point as it can change the
9947 value. So we don't do it unless -funsafe-math-optimizations. */
9948 if (FLOAT_MODE_P (GET_MODE (x))
9949 && ! flag_unsafe_math_optimizations)
9950 return x;
9951
9952 /* The outer operation can only be one of the following: */
9953 if (code != IOR && code != AND && code != XOR
9954 && code != PLUS && code != MINUS)
9955 return x;
9956
9957 lhs = XEXP (x, 0);
9958 rhs = XEXP (x, 1);
9959
9960 /* If either operand is a primitive we can't do anything, so get out
9961 fast. */
9962 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9963 return x;
9964
9965 lhs = expand_compound_operation (lhs);
9966 rhs = expand_compound_operation (rhs);
9967 inner_code = GET_CODE (lhs);
9968 if (inner_code != GET_CODE (rhs))
9969 return x;
9970
9971 /* See if the inner and outer operations distribute. */
9972 switch (inner_code)
9973 {
9974 case LSHIFTRT:
9975 case ASHIFTRT:
9976 case AND:
9977 case IOR:
9978 /* These all distribute except over PLUS. */
9979 if (code == PLUS || code == MINUS)
9980 return x;
9981 break;
9982
9983 case MULT:
9984 if (code != PLUS && code != MINUS)
9985 return x;
9986 break;
9987
9988 case ASHIFT:
9989 /* This is also a multiply, so it distributes over everything. */
9990 break;
9991
9992 /* This used to handle SUBREG, but this turned out to be counter-
9993 productive, since (subreg (op ...)) usually is not handled by
9994 insn patterns, and this "optimization" therefore transformed
9995 recognizable patterns into unrecognizable ones. Therefore the
9996 SUBREG case was removed from here.
9997
9998 It is possible that distributing SUBREG over arithmetic operations
9999 leads to an intermediate result than can then be optimized further,
10000 e.g. by moving the outer SUBREG to the other side of a SET as done
10001 in simplify_set. This seems to have been the original intent of
10002 handling SUBREGs here.
10003
10004 However, with current GCC this does not appear to actually happen,
10005 at least on major platforms. If some case is found where removing
10006 the SUBREG case here prevents follow-on optimizations, distributing
10007 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
10008
10009 default:
10010 return x;
10011 }
10012
10013 /* Set LHS and RHS to the inner operands (A and B in the example
10014 above) and set OTHER to the common operand (C in the example).
10015 There is only one way to do this unless the inner operation is
10016 commutative. */
10017 if (COMMUTATIVE_ARITH_P (lhs)
10018 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
10019 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
10020 else if (COMMUTATIVE_ARITH_P (lhs)
10021 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
10022 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
10023 else if (COMMUTATIVE_ARITH_P (lhs)
10024 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
10025 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
10026 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
10027 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
10028 else
10029 return x;
10030
10031 /* Form the new inner operation, seeing if it simplifies first. */
10032 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
10033
10034 /* There is one exception to the general way of distributing:
10035 (a | c) ^ (b | c) -> (a ^ b) & ~c */
10036 if (code == XOR && inner_code == IOR)
10037 {
10038 inner_code = AND;
10039 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
10040 }
10041
10042 /* We may be able to continuing distributing the result, so call
10043 ourselves recursively on the inner operation before forming the
10044 outer operation, which we return. */
10045 return simplify_gen_binary (inner_code, GET_MODE (x),
10046 apply_distributive_law (tem), other);
10047 }
10048
10049 /* See if X is of the form (* (+ A B) C), and if so convert to
10050 (+ (* A C) (* B C)) and try to simplify.
10051
10052 Most of the time, this results in no change. However, if some of
10053 the operands are the same or inverses of each other, simplifications
10054 will result.
10055
10056 For example, (and (ior A B) (not B)) can occur as the result of
10057 expanding a bit field assignment. When we apply the distributive
10058 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
10059 which then simplifies to (and (A (not B))).
10060
10061 Note that no checks happen on the validity of applying the inverse
10062 distributive law. This is pointless since we can do it in the
10063 few places where this routine is called.
10064
10065 N is the index of the term that is decomposed (the arithmetic operation,
10066 i.e. (+ A B) in the first example above). !N is the index of the term that
10067 is distributed, i.e. of C in the first example above. */
10068 static rtx
10069 distribute_and_simplify_rtx (rtx x, int n)
10070 {
10071 machine_mode mode;
10072 enum rtx_code outer_code, inner_code;
10073 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
10074
10075 /* Distributivity is not true for floating point as it can change the
10076 value. So we don't do it unless -funsafe-math-optimizations. */
10077 if (FLOAT_MODE_P (GET_MODE (x))
10078 && ! flag_unsafe_math_optimizations)
10079 return NULL_RTX;
10080
10081 decomposed = XEXP (x, n);
10082 if (!ARITHMETIC_P (decomposed))
10083 return NULL_RTX;
10084
10085 mode = GET_MODE (x);
10086 outer_code = GET_CODE (x);
10087 distributed = XEXP (x, !n);
10088
10089 inner_code = GET_CODE (decomposed);
10090 inner_op0 = XEXP (decomposed, 0);
10091 inner_op1 = XEXP (decomposed, 1);
10092
10093 /* Special case (and (xor B C) (not A)), which is equivalent to
10094 (xor (ior A B) (ior A C)) */
10095 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
10096 {
10097 distributed = XEXP (distributed, 0);
10098 outer_code = IOR;
10099 }
10100
10101 if (n == 0)
10102 {
10103 /* Distribute the second term. */
10104 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
10105 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
10106 }
10107 else
10108 {
10109 /* Distribute the first term. */
10110 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
10111 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
10112 }
10113
10114 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
10115 new_op0, new_op1));
10116 if (GET_CODE (tmp) != outer_code
10117 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
10118 < set_src_cost (x, mode, optimize_this_for_speed_p)))
10119 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 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10130 unsigned HOST_WIDE_INT constop)
10131 {
10132 unsigned HOST_WIDE_INT nonzero;
10133 unsigned HOST_WIDE_INT orig_constop;
10134 rtx orig_varop;
10135 int i;
10136
10137 orig_varop = varop;
10138 orig_constop = constop;
10139 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 varop = force_to_mode (varop, mode, constop, false);
10150
10151 /* If VAROP is a CLOBBER, we will fail so return it. */
10152 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 if (CONST_INT_P (varop))
10158 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 scalar_int_mode tmode = mode;
10165 if (WORD_REGISTER_OPERATIONS && GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
10166 tmode = word_mode;
10167 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 constop &= nonzero;
10174
10175 /* If we don't have any bits left, return zero. */
10176 if (constop == 0 && !side_effects_p (varop))
10177 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 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), tmode) == 1
10182 && (i = exact_log2 (constop)) >= 0)
10183 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 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10192 {
10193 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10194 return
10195 gen_lowpart
10196 (mode,
10197 apply_distributive_law
10198 (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 if (GET_CODE (varop) == PLUS
10212 && pow2p_hwi (constop + 1))
10213 {
10214 rtx o0, o1;
10215
10216 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10217 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10218 if (o0 == const0_rtx)
10219 return o1;
10220 if (o1 == const0_rtx)
10221 return o0;
10222 }
10223
10224 /* Make a SUBREG if necessary. If we can't make it, fail. */
10225 varop = gen_lowpart (mode, varop);
10226 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10227 return NULL_RTX;
10228
10229 /* If we are only masking insignificant bits, return VAROP. */
10230 if (constop == nonzero)
10231 return varop;
10232
10233 if (varop == orig_varop && constop == orig_constop)
10234 return NULL_RTX;
10235
10236 /* Otherwise, return an AND. */
10237 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 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10249 unsigned HOST_WIDE_INT constop)
10250 {
10251 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10252 if (tem)
10253 return tem;
10254
10255 if (!x)
10256 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10257 gen_int_mode (constop, mode));
10258 if (GET_MODE (x) != mode)
10259 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 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 rtx tem;
10276 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 rsp = &reg_stat[REGNO (x)];
10284 if (rsp->last_set_value != 0
10285 && (rsp->last_set_mode == mode
10286 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10287 && GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10288 && GET_MODE_CLASS (mode) == MODE_INT))
10289 && ((rsp->last_set_label >= label_tick_ebb_start
10290 && rsp->last_set_label < label_tick)
10291 || (rsp->last_set_label == label_tick
10292 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10293 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10294 && REGNO (x) < reg_n_sets_max
10295 && REG_N_SETS (REGNO (x)) == 1
10296 && !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 *nonzero &= rsp->last_set_nonzero_bits;
10307 return NULL;
10308 }
10309
10310 tem = get_last_value (x);
10311 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 if (nonzero_sign_valid && rsp->nonzero_bits)
10320 {
10321 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10322
10323 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10324 /* We don't know anything about the upper bits. */
10325 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10326
10327 *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 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 rtx tem;
10344 reg_stat_type *rsp;
10345
10346 rsp = &reg_stat[REGNO (x)];
10347 if (rsp->last_set_value != 0
10348 && rsp->last_set_mode == mode
10349 && ((rsp->last_set_label >= label_tick_ebb_start
10350 && rsp->last_set_label < label_tick)
10351 || (rsp->last_set_label == label_tick
10352 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10353 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10354 && REGNO (x) < reg_n_sets_max
10355 && REG_N_SETS (REGNO (x)) == 1
10356 && !REGNO_REG_SET_P
10357 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10358 REGNO (x)))))
10359 {
10360 *result = rsp->last_set_sign_bit_copies;
10361 return NULL;
10362 }
10363
10364 tem = get_last_value (x);
10365 if (tem != 0)
10366 return tem;
10367
10368 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10369 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10370 *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 extended_count (const_rtx x, machine_mode mode, bool unsignedp)
10388 {
10389 if (nonzero_sign_valid == 0)
10390 return 0;
10391
10392 scalar_int_mode int_mode;
10393 return (unsignedp
10394 ? (is_a <scalar_int_mode> (mode, &int_mode)
10395 && HWI_COMPUTABLE_MODE_P (int_mode)
10396 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10397 - floor_log2 (nonzero_bits (x, int_mode)))
10398 : 0)
10399 : 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 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 enum rtx_code op0 = *pop0;
10429 HOST_WIDE_INT const0 = *pconst0;
10430
10431 const0 &= GET_MODE_MASK (mode);
10432 const1 &= GET_MODE_MASK (mode);
10433
10434 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10435 if (op0 == AND)
10436 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 if (op1 == UNKNOWN || op0 == SET)
10442 return true;
10443
10444 else if (op0 == UNKNOWN)
10445 op0 = op1, const0 = const1;
10446
10447 else if (op0 == op1)
10448 {
10449 switch (op0)
10450 {
10451 case AND:
10452 const0 &= const1;
10453 break;
10454 case IOR:
10455 const0 |= const1;
10456 break;
10457 case XOR:
10458 const0 ^= const1;
10459 break;
10460 case PLUS:
10461 const0 += const1;
10462 break;
10463 case NEG:
10464 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 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 else if (const0 != const1)
10478 return false;
10479
10480 else
10481 switch (op0)
10482 {
10483 case IOR:
10484 if (op1 == AND)
10485 /* (a & b) | b == b */
10486 op0 = SET;
10487 else /* op1 == XOR */
10488 /* (a ^ b) | b == a | b */
10489 {;}
10490 break;
10491
10492 case XOR:
10493 if (op1 == AND)
10494 /* (a & b) ^ b == (~a) & b */
10495 op0 = AND, *pcomp_p = true;
10496 else /* op1 == IOR */
10497 /* (a | b) ^ b == a & ~b */
10498 op0 = AND, const0 = ~const0;
10499 break;
10500
10501 case AND:
10502 if (op1 == IOR)
10503 /* (a | b) & b == b */
10504 op0 = SET;
10505 else /* op1 == XOR */
10506 /* (a ^ b) & b) == (~a) & b */
10507 *pcomp_p = true;
10508 break;
10509 default:
10510 break;
10511 }
10512
10513 /* Check for NO-OP cases. */
10514 const0 &= GET_MODE_MASK (mode);
10515 if (const0 == 0
10516 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10517 op0 = UNKNOWN;
10518 else if (const0 == 0 && op0 == AND)
10519 op0 = SET;
10520 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10521 && op0 == AND)
10522 op0 = UNKNOWN;
10523
10524 *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 if (op0 != UNKNOWN && op0 != NEG)
10530 *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 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 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 switch (code)
10551 {
10552 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 if (num_sign_bit_copies (op, mode)
10556 > (unsigned) (GET_MODE_PRECISION (mode)
10557 - GET_MODE_PRECISION (orig_mode)))
10558 return mode;
10559 return orig_mode;
10560
10561 case LSHIFTRT:
10562 /* Similarly here but with zero bits. */
10563 if (HWI_COMPUTABLE_MODE_P (mode)
10564 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10565 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 if (outer_code == AND)
10570 {
10571 int care_bits = low_bitmask_len (orig_mode, outer_const);
10572
10573 if (care_bits >= 0
10574 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10575 return mode;
10576 }
10577 /* fall through */
10578
10579 case ROTATE:
10580 return orig_mode;
10581
10582 case ROTATERT:
10583 gcc_unreachable ();
10584
10585 default:
10586 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 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10600 rtx varop, int orig_count)
10601 {
10602 enum rtx_code orig_code = code;
10603 rtx orig_varop = varop;
10604 int count, log2;
10605 machine_mode mode = result_mode;
10606 machine_mode shift_mode;
10607 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 enum rtx_code outer_op = UNKNOWN;
10610 HOST_WIDE_INT outer_const = 0;
10611 bool complement_p = false;
10612 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 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 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 while (count != 0)
10632 {
10633 /* If we have an operand of (clobber (const_int 0)), fail. */
10634 if (GET_CODE (varop) == CLOBBER)
10635 return NULL_RTX;
10636
10637 /* Convert ROTATERT to ROTATE. */
10638 if (code == ROTATERT)
10639 {
10640 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10641 code = ROTATE;
10642 count = bitsize - count;
10643 }
10644
10645 shift_mode = result_mode;
10646 if (shift_mode != mode)
10647 {
10648 /* We only change the modes of scalar shifts. */
10649 int_mode = as_a <scalar_int_mode> (mode);
10650 int_result_mode = as_a <scalar_int_mode> (result_mode);
10651 shift_mode = try_widen_shift_mode (code, varop, count,
10652 int_result_mode, int_mode,
10653 outer_op, outer_const);
10654 }
10655
10656 scalar_int_mode shift_unit_mode;
10657 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 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10672 {
10673 if (code == ASHIFTRT)
10674 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10675 else if (code == ROTATE || code == ROTATERT)
10676 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 varop = const0_rtx;
10682 count = 0;
10683 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 if (complement_p)
10690 break;
10691
10692 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 if (code == ASHIFTRT
10697 && (num_sign_bit_copies (varop, shift_unit_mode)
10698 == 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 if (code == ASHIFTRT
10710 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10711 >= GET_MODE_PRECISION (shift_unit_mode)))
10712 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 if (code == ASHIFTRT
10719 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10720 && val_signbit_known_clear_p (shift_unit_mode,
10721 nonzero_bits (varop,
10722 shift_unit_mode)))
10723 code = LSHIFTRT;
10724
10725 if (((code == LSHIFTRT
10726 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10727 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10728 || (code == ASHIFT
10729 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10730 && !((nonzero_bits (varop, shift_unit_mode) << count)
10731 & GET_MODE_MASK (shift_unit_mode))))
10732 && !side_effects_p (varop))
10733 varop = const0_rtx;
10734 }
10735
10736 switch (GET_CODE (varop))
10737 {
10738 case SIGN_EXTEND:
10739 case ZERO_EXTEND:
10740 case SIGN_EXTRACT:
10741 case ZERO_EXTRACT:
10742 new_rtx = expand_compound_operation (varop);
10743 if (new_rtx != varop)
10744 {
10745 varop = new_rtx;
10746 continue;
10747 }
10748 break;
10749
10750 case MEM:
10751 /* The following rules apply only to scalars. */
10752 if (shift_mode != shift_unit_mode)
10753 break;
10754 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 if ((code == ASHIFTRT || code == LSHIFTRT)
10760 && ! mode_dependent_address_p (XEXP (varop, 0),
10761 MEM_ADDR_SPACE (varop))
10762 && ! MEM_VOLATILE_P (varop)
10763 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10764 .exists (&tmode)))
10765 {
10766 new_rtx = adjust_address_nv (varop, tmode,
10767 BYTES_BIG_ENDIAN ? 0
10768 : count / BITS_PER_UNIT);
10769
10770 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10771 : ZERO_EXTEND, int_mode, new_rtx);
10772 count = 0;
10773 continue;
10774 }
10775 break;
10776
10777 case SUBREG:
10778 /* The following rules apply only to scalars. */
10779 if (shift_mode != shift_unit_mode)
10780 break;
10781 int_mode = as_a <scalar_int_mode> (mode);
10782 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 if (subreg_lowpart_p (varop)
10788 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10789 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10790 && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10791 == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10792 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10793 {
10794 varop = SUBREG_REG (varop);
10795 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10796 mode = inner_mode;
10797 continue;
10798 }
10799 break;
10800
10801 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 if (CONST_INT_P (XEXP (varop, 1))
10806 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10807 {
10808 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10809 varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10810 XEXP (varop, 0), log2_rtx);
10811 continue;
10812 }
10813 break;
10814
10815 case UDIV:
10816 /* Similar, for when divides are cheaper. */
10817 if (CONST_INT_P (XEXP (varop, 1))
10818 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10819 {
10820 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10821 varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10822 XEXP (varop, 0), log2_rtx);
10823 continue;
10824 }
10825 break;
10826
10827 case ASHIFTRT:
10828 /* If we are extracting just the sign bit of an arithmetic
10829 right shift, that shift is not needed. However, the sign
10830 bit of a wider mode may be different from what would be
10831 interpreted as the sign bit in a narrower mode, so, if
10832 the result is narrower, don't discard the shift. */
10833 if (code == LSHIFTRT
10834 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10835 && (GET_MODE_UNIT_BITSIZE (result_mode)
10836 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10837 {
10838 varop = XEXP (varop, 0);
10839 continue;
10840 }
10841
10842 /* fall through */
10843
10844 case LSHIFTRT:
10845 case ASHIFT:
10846 case ROTATE:
10847 /* The following rules apply only to scalars. */
10848 if (shift_mode != shift_unit_mode)
10849 break;
10850 int_mode = as_a <scalar_int_mode> (mode);
10851 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10852 int_result_mode = as_a <scalar_int_mode> (result_mode);
10853
10854 /* Here we have two nested shifts. The result is usually the
10855 AND of a new shift with a mask. We compute the result below. */
10856 if (CONST_INT_P (XEXP (varop, 1))
10857 && INTVAL (XEXP (varop, 1)) >= 0
10858 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10859 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10860 && HWI_COMPUTABLE_MODE_P (int_mode))
10861 {
10862 enum rtx_code first_code = GET_CODE (varop);
10863 unsigned int first_count = INTVAL (XEXP (varop, 1));
10864 unsigned HOST_WIDE_INT mask;
10865 rtx mask_rtx;
10866
10867 /* We have one common special case. We can't do any merging if
10868 the inner code is an ASHIFTRT of a smaller mode. However, if
10869 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10870 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10871 we can convert it to
10872 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10873 This simplifies certain SIGN_EXTEND operations. */
10874 if (code == ASHIFT && first_code == ASHIFTRT
10875 && count == (GET_MODE_PRECISION (int_result_mode)
10876 - GET_MODE_PRECISION (int_varop_mode)))
10877 {
10878 /* C3 has the low-order C1 bits zero. */
10879
10880 mask = GET_MODE_MASK (int_mode)
10881 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10882
10883 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10884 XEXP (varop, 0), mask);
10885 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10886 int_result_mode, varop, count);
10887 count = first_count;
10888 code = ASHIFTRT;
10889 continue;
10890 }
10891
10892 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10893 than C1 high-order bits equal to the sign bit, we can convert
10894 this to either an ASHIFT or an ASHIFTRT depending on the
10895 two counts.
10896
10897 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10898
10899 if (code == ASHIFTRT && first_code == ASHIFT
10900 && int_varop_mode == shift_unit_mode
10901 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10902 > first_count))
10903 {
10904 varop = XEXP (varop, 0);
10905 count -= first_count;
10906 if (count < 0)
10907 {
10908 count = -count;
10909 code = ASHIFT;
10910 }
10911
10912 continue;
10913 }
10914
10915 /* There are some cases we can't do. If CODE is ASHIFTRT,
10916 we can only do this if FIRST_CODE is also ASHIFTRT.
10917
10918 We can't do the case when CODE is ROTATE and FIRST_CODE is
10919 ASHIFTRT.
10920
10921 If the mode of this shift is not the mode of the outer shift,
10922 we can't do this if either shift is a right shift or ROTATE.
10923
10924 Finally, we can't do any of these if the mode is too wide
10925 unless the codes are the same.
10926
10927 Handle the case where the shift codes are the same
10928 first. */
10929
10930 if (code == first_code)
10931 {
10932 if (int_varop_mode != int_result_mode
10933 && (code == ASHIFTRT || code == LSHIFTRT
10934 || code == ROTATE))
10935 break;
10936
10937 count += first_count;
10938 varop = XEXP (varop, 0);
10939 continue;
10940 }
10941
10942 if (code == ASHIFTRT
10943 || (code == ROTATE && first_code == ASHIFTRT)
10944 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10945 || (int_varop_mode != int_result_mode
10946 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10947 || first_code == ROTATE
10948 || code == ROTATE)))
10949 break;
10950
10951 /* To compute the mask to apply after the shift, shift the
10952 nonzero bits of the inner shift the same way the
10953 outer shift will. */
10954
10955 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10956 int_result_mode);
10957 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10958 mask_rtx
10959 = simplify_const_binary_operation (code, int_result_mode,
10960 mask_rtx, count_rtx);
10961
10962 /* Give up if we can't compute an outer operation to use. */
10963 if (mask_rtx == 0
10964 || !CONST_INT_P (mask_rtx)
10965 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10966 INTVAL (mask_rtx),
10967 int_result_mode, &complement_p))
10968 break;
10969
10970 /* If the shifts are in the same direction, we add the
10971 counts. Otherwise, we subtract them. */
10972 if ((code == ASHIFTRT || code == LSHIFTRT)
10973 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10974 count += first_count;
10975 else
10976 count -= first_count;
10977
10978 /* If COUNT is positive, the new shift is usually CODE,
10979 except for the two exceptions below, in which case it is
10980 FIRST_CODE. If the count is negative, FIRST_CODE should
10981 always be used */
10982 if (count > 0
10983 && ((first_code == ROTATE && code == ASHIFT)
10984 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10985 code = first_code;
10986 else if (count < 0)
10987 code = first_code, count = -count;
10988
10989 varop = XEXP (varop, 0);
10990 continue;
10991 }
10992
10993 /* If we have (A << B << C) for any shift, we can convert this to
10994 (A << C << B). This wins if A is a constant. Only try this if
10995 B is not a constant. */
10996
10997 else if (GET_CODE (varop) == code
10998 && CONST_INT_P (XEXP (varop, 0))
10999 && !CONST_INT_P (XEXP (varop, 1)))
11000 {
11001 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
11002 sure the result will be masked. See PR70222. */
11003 if (code == LSHIFTRT
11004 && int_mode != int_result_mode
11005 && !merge_outer_ops (&outer_op, &outer_const, AND,
11006 GET_MODE_MASK (int_result_mode)
11007 >> orig_count, int_result_mode,
11008 &complement_p))
11009 break;
11010 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
11011 up outer sign extension (often left and right shift) is
11012 hardly more efficient than the original. See PR70429.
11013 Similarly punt for rotates with different modes.
11014 See PR97386. */
11015 if ((code == ASHIFTRT || code == ROTATE)
11016 && int_mode != int_result_mode)
11017 break;
11018
11019 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
11020 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
11021 XEXP (varop, 0),
11022 count_rtx);
11023 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
11024 count = 0;
11025 continue;
11026 }
11027 break;
11028
11029 case NOT:
11030 /* The following rules apply only to scalars. */
11031 if (shift_mode != shift_unit_mode)
11032 break;
11033
11034 /* Make this fit the case below. */
11035 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
11036 continue;
11037
11038 case IOR:
11039 case AND:
11040 case XOR:
11041 /* The following rules apply only to scalars. */
11042 if (shift_mode != shift_unit_mode)
11043 break;
11044 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11045 int_result_mode = as_a <scalar_int_mode> (result_mode);
11046
11047 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
11048 with C the size of VAROP - 1 and the shift is logical if
11049 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11050 we have an (le X 0) operation. If we have an arithmetic shift
11051 and STORE_FLAG_VALUE is 1 or we have a logical shift with
11052 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
11053
11054 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
11055 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
11056 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11057 && (code == LSHIFTRT || code == ASHIFTRT)
11058 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11059 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11060 {
11061 count = 0;
11062 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
11063 const0_rtx);
11064
11065 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11066 varop = gen_rtx_NEG (int_varop_mode, varop);
11067
11068 continue;
11069 }
11070
11071 /* If we have (shift (logical)), move the logical to the outside
11072 to allow it to possibly combine with another logical and the
11073 shift to combine with another shift. This also canonicalizes to
11074 what a ZERO_EXTRACT looks like. Also, some machines have
11075 (and (shift)) insns. */
11076
11077 if (CONST_INT_P (XEXP (varop, 1))
11078 /* We can't do this if we have (ashiftrt (xor)) and the
11079 constant has its sign bit set in shift_unit_mode with
11080 shift_unit_mode wider than result_mode. */
11081 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11082 && int_result_mode != shift_unit_mode
11083 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11084 shift_unit_mode) < 0)
11085 && (new_rtx = simplify_const_binary_operation
11086 (code, int_result_mode,
11087 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11088 gen_int_shift_amount (int_result_mode, count))) != 0
11089 && CONST_INT_P (new_rtx)
11090 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
11091 INTVAL (new_rtx), int_result_mode,
11092 &complement_p))
11093 {
11094 varop = XEXP (varop, 0);
11095 continue;
11096 }
11097
11098 /* If we can't do that, try to simplify the shift in each arm of the
11099 logical expression, make a new logical expression, and apply
11100 the inverse distributive law. This also can't be done for
11101 (ashiftrt (xor)) where we've widened the shift and the constant
11102 changes the sign bit. */
11103 if (CONST_INT_P (XEXP (varop, 1))
11104 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11105 && int_result_mode != shift_unit_mode
11106 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11107 shift_unit_mode) < 0))
11108 {
11109 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11110 XEXP (varop, 0), count);
11111 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11112 XEXP (varop, 1), count);
11113
11114 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
11115 lhs, rhs);
11116 varop = apply_distributive_law (varop);
11117
11118 count = 0;
11119 continue;
11120 }
11121 break;
11122
11123 case EQ:
11124 /* The following rules apply only to scalars. */
11125 if (shift_mode != shift_unit_mode)
11126 break;
11127 int_result_mode = as_a <scalar_int_mode> (result_mode);
11128
11129 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
11130 says that the sign bit can be tested, FOO has mode MODE, C is
11131 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11132 that may be nonzero. */
11133 if (code == LSHIFTRT
11134 && XEXP (varop, 1) == const0_rtx
11135 && GET_MODE (XEXP (varop, 0)) == int_result_mode
11136 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11137 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11138 && STORE_FLAG_VALUE == -1
11139 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11140 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11141 int_result_mode, &complement_p))
11142 {
11143 varop = XEXP (varop, 0);
11144 count = 0;
11145 continue;
11146 }
11147 break;
11148
11149 case NEG:
11150 /* The following rules apply only to scalars. */
11151 if (shift_mode != shift_unit_mode)
11152 break;
11153 int_result_mode = as_a <scalar_int_mode> (result_mode);
11154
11155 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11156 than the number of bits in the mode is equivalent to A. */
11157 if (code == LSHIFTRT
11158 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11159 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11160 {
11161 varop = XEXP (varop, 0);
11162 count = 0;
11163 continue;
11164 }
11165
11166 /* NEG commutes with ASHIFT since it is multiplication. Move the
11167 NEG outside to allow shifts to combine. */
11168 if (code == ASHIFT
11169 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11170 int_result_mode, &complement_p))
11171 {
11172 varop = XEXP (varop, 0);
11173 continue;
11174 }
11175 break;
11176
11177 case PLUS:
11178 /* The following rules apply only to scalars. */
11179 if (shift_mode != shift_unit_mode)
11180 break;
11181 int_result_mode = as_a <scalar_int_mode> (result_mode);
11182
11183 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11184 is one less than the number of bits in the mode is
11185 equivalent to (xor A 1). */
11186 if (code == LSHIFTRT
11187 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11188 && XEXP (varop, 1) == constm1_rtx
11189 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11190 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11191 int_result_mode, &complement_p))
11192 {
11193 count = 0;
11194 varop = XEXP (varop, 0);
11195 continue;
11196 }
11197
11198 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11199 that might be nonzero in BAR are those being shifted out and those
11200 bits are known zero in FOO, we can replace the PLUS with FOO.
11201 Similarly in the other operand order. This code occurs when
11202 we are computing the size of a variable-size array. */
11203
11204 if ((code == ASHIFTRT || code == LSHIFTRT)
11205 && count < HOST_BITS_PER_WIDE_INT
11206 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11207 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11208 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11209 {
11210 varop = XEXP (varop, 0);
11211 continue;
11212 }
11213 else if ((code == ASHIFTRT || code == LSHIFTRT)
11214 && count < HOST_BITS_PER_WIDE_INT
11215 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11216 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11217 >> count) == 0
11218 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11219 & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11220 {
11221 varop = XEXP (varop, 1);
11222 continue;
11223 }
11224
11225 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11226 if (code == ASHIFT
11227 && CONST_INT_P (XEXP (varop, 1))
11228 && (new_rtx = simplify_const_binary_operation
11229 (ASHIFT, int_result_mode,
11230 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11231 gen_int_shift_amount (int_result_mode, count))) != 0
11232 && CONST_INT_P (new_rtx)
11233 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11234 INTVAL (new_rtx), int_result_mode,
11235 &complement_p))
11236 {
11237 varop = XEXP (varop, 0);
11238 continue;
11239 }
11240
11241 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11242 signbit', and attempt to change the PLUS to an XOR and move it to
11243 the outer operation as is done above in the AND/IOR/XOR case
11244 leg for shift(logical). See details in logical handling above
11245 for reasoning in doing so. */
11246 if (code == LSHIFTRT
11247 && CONST_INT_P (XEXP (varop, 1))
11248 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11249 && (new_rtx = simplify_const_binary_operation
11250 (code, int_result_mode,
11251 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11252 gen_int_shift_amount (int_result_mode, count))) != 0
11253 && CONST_INT_P (new_rtx)
11254 && merge_outer_ops (&outer_op, &outer_const, XOR,
11255 INTVAL (new_rtx), int_result_mode,
11256 &complement_p))
11257 {
11258 varop = XEXP (varop, 0);
11259 continue;
11260 }
11261
11262 break;
11263
11264 case MINUS:
11265 /* The following rules apply only to scalars. */
11266 if (shift_mode != shift_unit_mode)
11267 break;
11268 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11269
11270 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11271 with C the size of VAROP - 1 and the shift is logical if
11272 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11273 we have a (gt X 0) operation. If the shift is arithmetic with
11274 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11275 we have a (neg (gt X 0)) operation. */
11276
11277 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11278 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11279 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11280 && (code == LSHIFTRT || code == ASHIFTRT)
11281 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11282 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11283 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11284 {
11285 count = 0;
11286 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11287 const0_rtx);
11288
11289 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11290 varop = gen_rtx_NEG (int_varop_mode, varop);
11291
11292 continue;
11293 }
11294 break;
11295
11296 case TRUNCATE:
11297 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11298 if the truncate does not affect the value. */
11299 if (code == LSHIFTRT
11300 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11301 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11302 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11303 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11304 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11305 {
11306 rtx varop_inner = XEXP (varop, 0);
11307 int new_count = count + INTVAL (XEXP (varop_inner, 1));
11308 rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11309 new_count);
11310 varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11311 XEXP (varop_inner, 0),
11312 new_count_rtx);
11313 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11314 count = 0;
11315 continue;
11316 }
11317 break;
11318
11319 default:
11320 break;
11321 }
11322
11323 break;
11324 }
11325
11326 shift_mode = result_mode;
11327 if (shift_mode != mode)
11328 {
11329 /* We only change the modes of scalar shifts. */
11330 int_mode = as_a <scalar_int_mode> (mode);
11331 int_result_mode = as_a <scalar_int_mode> (result_mode);
11332 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11333 int_mode, outer_op, outer_const);
11334 }
11335
11336 /* We have now finished analyzing the shift. The result should be
11337 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11338 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11339 to the result of the shift. OUTER_CONST is the relevant constant,
11340 but we must turn off all bits turned off in the shift. */
11341
11342 if (outer_op == UNKNOWN
11343 && orig_code == code && orig_count == count
11344 && varop == orig_varop
11345 && shift_mode == GET_MODE (varop))
11346 return NULL_RTX;
11347
11348 /* Make a SUBREG if necessary. If we can't make it, fail. */
11349 varop = gen_lowpart (shift_mode, varop);
11350 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11351 return NULL_RTX;
11352
11353 /* If we have an outer operation and we just made a shift, it is
11354 possible that we could have simplified the shift were it not
11355 for the outer operation. So try to do the simplification
11356 recursively. */
11357
11358 if (outer_op != UNKNOWN)
11359 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11360 else
11361 x = NULL_RTX;
11362
11363 if (x == NULL_RTX)
11364 x = simplify_gen_binary (code, shift_mode, varop,
11365 gen_int_shift_amount (shift_mode, count));
11366
11367 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11368 turn off all the bits that the shift would have turned off. */
11369 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11370 /* We only change the modes of scalar shifts. */
11371 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11372 x, GET_MODE_MASK (result_mode) >> orig_count);
11373
11374 /* Do the remainder of the processing in RESULT_MODE. */
11375 x = gen_lowpart_or_truncate (result_mode, x);
11376
11377 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11378 operation. */
11379 if (complement_p)
11380 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11381
11382 if (outer_op != UNKNOWN)
11383 {
11384 int_result_mode = as_a <scalar_int_mode> (result_mode);
11385
11386 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11387 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11388 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11389
11390 if (outer_op == AND)
11391 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11392 else if (outer_op == SET)
11393 {
11394 /* This means that we have determined that the result is
11395 equivalent to a constant. This should be rare. */
11396 if (!side_effects_p (x))
11397 x = GEN_INT (outer_const);
11398 }
11399 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11400 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11401 else
11402 x = simplify_gen_binary (outer_op, int_result_mode, x,
11403 GEN_INT (outer_const));
11404 }
11405
11406 return x;
11407 }
11408
11409 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11410 The result of the shift is RESULT_MODE. If we cannot simplify it,
11411 return X or, if it is NULL, synthesize the expression with
11412 simplify_gen_binary. Otherwise, return a simplified value.
11413
11414 The shift is normally computed in the widest mode we find in VAROP, as
11415 long as it isn't a different number of words than RESULT_MODE. Exceptions
11416 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11417
11418 static rtx
11419 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11420 rtx varop, int count)
11421 {
11422 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11423 if (tem)
11424 return tem;
11425
11426 if (!x)
11427 x = simplify_gen_binary (code, GET_MODE (varop), varop,
11428 gen_int_shift_amount (GET_MODE (varop), count));
11429 if (GET_MODE (x) != result_mode)
11430 x = gen_lowpart (result_mode, x);
11431 return x;
11432 }
11433
11434
11435 /* A subroutine of recog_for_combine. See there for arguments and
11436 return value. */
11437
11438 static int
11439 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes,
11440 unsigned old_nregs, unsigned new_nregs)
11441 {
11442 rtx pat = *pnewpat;
11443 rtx pat_without_clobbers;
11444 int insn_code_number;
11445 int num_clobbers_to_add = 0;
11446 int i;
11447 rtx notes = NULL_RTX;
11448 rtx old_notes, old_pat;
11449 int old_icode;
11450
11451 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11452 we use to indicate that something didn't match. If we find such a
11453 thing, force rejection. */
11454 if (GET_CODE (pat) == PARALLEL)
11455 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11456 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11457 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11458 return -1;
11459
11460 old_pat = PATTERN (insn);
11461 old_notes = REG_NOTES (insn);
11462 PATTERN (insn) = pat;
11463 REG_NOTES (insn) = NULL_RTX;
11464
11465 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11466 if (dump_file && (dump_flags & TDF_DETAILS))
11467 {
11468 if (insn_code_number < 0)
11469 fputs ("Failed to match this instruction:\n", dump_file);
11470 else
11471 fputs ("Successfully matched this instruction:\n", dump_file);
11472 print_rtl_single (dump_file, pat);
11473 }
11474
11475 /* If it isn't, there is the possibility that we previously had an insn
11476 that clobbered some register as a side effect, but the combined
11477 insn doesn't need to do that. So try once more without the clobbers
11478 unless this represents an ASM insn. */
11479
11480 if (insn_code_number < 0 && ! check_asm_operands (pat)
11481 && GET_CODE (pat) == PARALLEL)
11482 {
11483 int pos;
11484
11485 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11486 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11487 {
11488 if (i != pos)
11489 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11490 pos++;
11491 }
11492
11493 SUBST_INT (XVECLEN (pat, 0), pos);
11494
11495 if (pos == 1)
11496 pat = XVECEXP (pat, 0, 0);
11497
11498 PATTERN (insn) = pat;
11499 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11500 if (dump_file && (dump_flags & TDF_DETAILS))
11501 {
11502 if (insn_code_number < 0)
11503 fputs ("Failed to match this instruction:\n", dump_file);
11504 else
11505 fputs ("Successfully matched this instruction:\n", dump_file);
11506 print_rtl_single (dump_file, pat);
11507 }
11508 }
11509
11510 pat_without_clobbers = pat;
11511
11512 PATTERN (insn) = old_pat;
11513 REG_NOTES (insn) = old_notes;
11514
11515 /* Recognize all noop sets, these will be killed by followup pass. */
11516 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11517 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11518
11519 /* If we had any clobbers to add, make a new pattern than contains
11520 them. Then check to make sure that all of them are dead. */
11521 if (num_clobbers_to_add)
11522 {
11523 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11524 rtvec_alloc (GET_CODE (pat) == PARALLEL
11525 ? (XVECLEN (pat, 0)
11526 + num_clobbers_to_add)
11527 : num_clobbers_to_add + 1));
11528
11529 if (GET_CODE (pat) == PARALLEL)
11530 for (i = 0; i < XVECLEN (pat, 0); i++)
11531 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11532 else
11533 XVECEXP (newpat, 0, 0) = pat;
11534
11535 add_clobbers (newpat, insn_code_number);
11536
11537 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11538 i < XVECLEN (newpat, 0); i++)
11539 {
11540 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11541 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11542 return -1;
11543 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11544 {
11545 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11546 notes = alloc_reg_note (REG_UNUSED,
11547 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11548 }
11549 }
11550 pat = newpat;
11551 }
11552
11553 if (insn_code_number >= 0
11554 && insn_code_number != NOOP_MOVE_INSN_CODE)
11555 {
11556 /* Create the reg dead notes if needed for the regs that were created via split. */
11557 for (; old_nregs < new_nregs; old_nregs++)
11558 notes = alloc_reg_note (REG_DEAD, regno_reg_rtx[old_nregs], notes);
11559 old_pat = PATTERN (insn);
11560 old_notes = REG_NOTES (insn);
11561 old_icode = INSN_CODE (insn);
11562 PATTERN (insn) = pat;
11563 REG_NOTES (insn) = notes;
11564 INSN_CODE (insn) = insn_code_number;
11565
11566 /* Allow targets to reject combined insn. */
11567 if (!targetm.legitimate_combined_insn (insn))
11568 {
11569 if (dump_file && (dump_flags & TDF_DETAILS))
11570 fputs ("Instruction not appropriate for target.",
11571 dump_file);
11572
11573 /* Callers expect recog_for_combine to strip
11574 clobbers from the pattern on failure. */
11575 pat = pat_without_clobbers;
11576 notes = NULL_RTX;
11577
11578 insn_code_number = -1;
11579 }
11580
11581 PATTERN (insn) = old_pat;
11582 REG_NOTES (insn) = old_notes;
11583 INSN_CODE (insn) = old_icode;
11584 }
11585
11586 *pnewpat = pat;
11587 *pnotes = notes;
11588
11589 return insn_code_number;
11590 }
11591
11592 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11593 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11594 Return whether anything was so changed. */
11595
11596 static bool
11597 change_zero_ext (rtx pat)
11598 {
11599 bool changed = false;
11600 rtx *src = &SET_SRC (pat);
11601
11602 subrtx_ptr_iterator::array_type array;
11603 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11604 {
11605 rtx x = **iter;
11606 scalar_int_mode mode, inner_mode;
11607 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11608 continue;
11609 int size;
11610
11611 if (GET_CODE (x) == ZERO_EXTRACT
11612 && CONST_INT_P (XEXP (x, 1))
11613 && CONST_INT_P (XEXP (x, 2))
11614 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11615 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11616 {
11617 size = INTVAL (XEXP (x, 1));
11618
11619 int start = INTVAL (XEXP (x, 2));
11620 if (BITS_BIG_ENDIAN)
11621 start = GET_MODE_PRECISION (inner_mode) - size - start;
11622
11623 if (start != 0)
11624 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11625 gen_int_shift_amount (inner_mode, start));
11626 else
11627 x = XEXP (x, 0);
11628
11629 if (mode != inner_mode)
11630 {
11631 if (REG_P (x) && HARD_REGISTER_P (x)
11632 && !can_change_dest_mode (x, 0, mode))
11633 continue;
11634
11635 x = gen_lowpart_SUBREG (mode, x);
11636 }
11637 }
11638 else if (GET_CODE (x) == ZERO_EXTEND
11639 && GET_CODE (XEXP (x, 0)) == SUBREG
11640 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11641 && !paradoxical_subreg_p (XEXP (x, 0))
11642 && subreg_lowpart_p (XEXP (x, 0)))
11643 {
11644 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11645 size = GET_MODE_PRECISION (inner_mode);
11646 x = SUBREG_REG (XEXP (x, 0));
11647 if (GET_MODE (x) != mode)
11648 {
11649 if (REG_P (x) && HARD_REGISTER_P (x)
11650 && !can_change_dest_mode (x, 0, mode))
11651 continue;
11652
11653 x = gen_lowpart_SUBREG (mode, x);
11654 }
11655 }
11656 else if (GET_CODE (x) == ZERO_EXTEND
11657 && REG_P (XEXP (x, 0))
11658 && HARD_REGISTER_P (XEXP (x, 0))
11659 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11660 {
11661 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11662 size = GET_MODE_PRECISION (inner_mode);
11663 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11664 }
11665 else
11666 continue;
11667
11668 if (!(GET_CODE (x) == LSHIFTRT
11669 && CONST_INT_P (XEXP (x, 1))
11670 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11671 {
11672 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11673 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11674 }
11675
11676 SUBST (**iter, x);
11677 changed = true;
11678 }
11679
11680 if (changed)
11681 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11682 maybe_swap_commutative_operands (**iter);
11683
11684 rtx *dst = &SET_DEST (pat);
11685 scalar_int_mode mode;
11686 if (GET_CODE (*dst) == ZERO_EXTRACT
11687 && REG_P (XEXP (*dst, 0))
11688 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11689 && CONST_INT_P (XEXP (*dst, 1))
11690 && CONST_INT_P (XEXP (*dst, 2)))
11691 {
11692 rtx reg = XEXP (*dst, 0);
11693 int width = INTVAL (XEXP (*dst, 1));
11694 int offset = INTVAL (XEXP (*dst, 2));
11695 int reg_width = GET_MODE_PRECISION (mode);
11696 if (BITS_BIG_ENDIAN)
11697 offset = reg_width - width - offset;
11698
11699 rtx x, y, z, w;
11700 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11701 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11702 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11703 if (offset)
11704 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11705 else
11706 y = SET_SRC (pat);
11707 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11708 w = gen_rtx_IOR (mode, x, z);
11709 SUBST (SET_DEST (pat), reg);
11710 SUBST (SET_SRC (pat), w);
11711
11712 changed = true;
11713 }
11714
11715 return changed;
11716 }
11717
11718 /* Like recog, but we receive the address of a pointer to a new pattern.
11719 We try to match the rtx that the pointer points to.
11720 If that fails, we may try to modify or replace the pattern,
11721 storing the replacement into the same pointer object.
11722
11723 Modifications include deletion or addition of CLOBBERs. If the
11724 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11725 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11726 (and undo if that fails).
11727
11728 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11729 the CLOBBERs are placed.
11730 If OLD_NREGS != NEW_NREGS, then PNOTES also includes REG_DEAD notes added.
11731
11732 The value is the final insn code from the pattern ultimately matched,
11733 or -1. */
11734
11735 static int
11736 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes,
11737 unsigned int old_nregs, unsigned int new_nregs)
11738 {
11739 rtx pat = *pnewpat;
11740 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes,
11741 old_nregs, new_nregs);
11742 if (insn_code_number >= 0 || check_asm_operands (pat))
11743 return insn_code_number;
11744
11745 void *marker = get_undo_marker ();
11746 bool changed = false;
11747
11748 if (GET_CODE (pat) == SET)
11749 {
11750 /* For an unrecognized single set of a constant, try placing it in
11751 the constant pool, if this function already uses one. */
11752 rtx src = SET_SRC (pat);
11753 if (CONSTANT_P (src)
11754 && !CONST_INT_P (src)
11755 && crtl->uses_const_pool
11756 && SET_DEST (pat) != pc_rtx)
11757 {
11758 machine_mode mode = GET_MODE (src);
11759 if (mode == VOIDmode)
11760 mode = GET_MODE (SET_DEST (pat));
11761 src = force_const_mem (mode, src);
11762 if (src)
11763 {
11764 SUBST (SET_SRC (pat), src);
11765 changed = true;
11766 }
11767 }
11768 else
11769 changed = change_zero_ext (pat);
11770 }
11771 else if (GET_CODE (pat) == PARALLEL)
11772 {
11773 int i;
11774 for (i = 0; i < XVECLEN (pat, 0); i++)
11775 {
11776 rtx set = XVECEXP (pat, 0, i);
11777 if (GET_CODE (set) == SET)
11778 changed |= change_zero_ext (set);
11779 }
11780 }
11781
11782 if (changed)
11783 {
11784 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes,
11785 old_nregs, new_nregs);
11786
11787 if (insn_code_number < 0)
11788 undo_to_marker (marker);
11789 }
11790
11791 return insn_code_number;
11792 }
11793
11794 /* Like gen_lowpart_general but for use by combine. In combine it
11795 is not possible to create any new pseudoregs. However, it is
11796 safe to create invalid memory addresses, because combine will
11797 try to recognize them and all they will do is make the combine
11798 attempt fail.
11799
11800 If for some reason this cannot do its job, an rtx
11801 (clobber (const_int 0)) is returned.
11802 An insn containing that will not be recognized. */
11803
11804 static rtx
11805 gen_lowpart_for_combine (machine_mode omode, rtx x)
11806 {
11807 machine_mode imode = GET_MODE (x);
11808 rtx result;
11809
11810 if (omode == imode)
11811 return x;
11812
11813 /* We can only support MODE being wider than a word if X is a
11814 constant integer or has a mode the same size. */
11815 if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11816 && ! (CONST_SCALAR_INT_P (x)
11817 || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11818 goto fail;
11819
11820 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11821 won't know what to do. So we will strip off the SUBREG here and
11822 process normally. */
11823 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11824 {
11825 x = SUBREG_REG (x);
11826
11827 /* For use in case we fall down into the address adjustments
11828 further below, we need to adjust the known mode and size of
11829 x; imode and isize, since we just adjusted x. */
11830 imode = GET_MODE (x);
11831
11832 if (imode == omode)
11833 return x;
11834 }
11835
11836 result = gen_lowpart_common (omode, x);
11837
11838 if (result)
11839 return result;
11840
11841 if (MEM_P (x))
11842 {
11843 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11844 address. */
11845 if (MEM_VOLATILE_P (x)
11846 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11847 goto fail;
11848
11849 /* If we want to refer to something bigger than the original memref,
11850 generate a paradoxical subreg instead. That will force a reload
11851 of the original memref X. */
11852 if (paradoxical_subreg_p (omode, imode))
11853 return gen_rtx_SUBREG (omode, x, 0);
11854
11855 poly_int64 offset = byte_lowpart_offset (omode, imode);
11856 return adjust_address_nv (x, omode, offset);
11857 }
11858
11859 /* If X is a comparison operator, rewrite it in a new mode. This
11860 probably won't match, but may allow further simplifications. */
11861 else if (COMPARISON_P (x)
11862 && SCALAR_INT_MODE_P (imode)
11863 && SCALAR_INT_MODE_P (omode))
11864 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11865
11866 /* If we couldn't simplify X any other way, just enclose it in a
11867 SUBREG. Normally, this SUBREG won't match, but some patterns may
11868 include an explicit SUBREG or we may simplify it further in combine. */
11869 else
11870 {
11871 rtx res;
11872
11873 if (imode == VOIDmode)
11874 {
11875 imode = int_mode_for_mode (omode).require ();
11876 x = gen_lowpart_common (imode, x);
11877 if (x == NULL)
11878 goto fail;
11879 }
11880 res = lowpart_subreg (omode, x, imode);
11881 if (res)
11882 return res;
11883 }
11884
11885 fail:
11886 return gen_rtx_CLOBBER (omode, const0_rtx);
11887 }
11888
11889 /* Try to simplify a comparison between OP0 and a constant OP1,
11890 where CODE is the comparison code that will be tested, into a
11891 (CODE OP0 const0_rtx) form.
11892
11893 The result is a possibly different comparison code to use.
11894 *POP0 and *POP1 may be updated. */
11895
11896 static enum rtx_code
11897 simplify_compare_const (enum rtx_code code, machine_mode mode,
11898 rtx *pop0, rtx *pop1)
11899 {
11900 scalar_int_mode int_mode;
11901 rtx op0 = *pop0;
11902 HOST_WIDE_INT const_op = INTVAL (*pop1);
11903
11904 /* Get the constant we are comparing against and turn off all bits
11905 not on in our mode. */
11906 if (mode != VOIDmode)
11907 const_op = trunc_int_for_mode (const_op, mode);
11908
11909 /* If we are comparing against a constant power of two and the value
11910 being compared can only have that single bit nonzero (e.g., it was
11911 `and'ed with that bit), we can replace this with a comparison
11912 with zero. */
11913 if (const_op
11914 && (code == EQ || code == NE || code == GEU || code == LTU
11915 /* This optimization is incorrect for signed >= INT_MIN or
11916 < INT_MIN, those are always true or always false. */
11917 || ((code == GE || code == LT) && const_op > 0))
11918 && is_a <scalar_int_mode> (mode, &int_mode)
11919 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11920 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11921 && (nonzero_bits (op0, int_mode)
11922 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11923 {
11924 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11925 const_op = 0;
11926 }
11927
11928 /* Similarly, if we are comparing a value known to be either -1 or
11929 0 with -1, change it to the opposite comparison against zero. */
11930 if (const_op == -1
11931 && (code == EQ || code == NE || code == GT || code == LE
11932 || code == GEU || code == LTU)
11933 && is_a <scalar_int_mode> (mode, &int_mode)
11934 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11935 {
11936 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11937 const_op = 0;
11938 }
11939
11940 /* Do some canonicalizations based on the comparison code. We prefer
11941 comparisons against zero and then prefer equality comparisons.
11942 If we can reduce the size of a constant, we will do that too. */
11943 switch (code)
11944 {
11945 case LT:
11946 /* < C is equivalent to <= (C - 1) */
11947 if (const_op > 0)
11948 {
11949 const_op -= 1;
11950 code = LE;
11951 /* ... fall through to LE case below. */
11952 gcc_fallthrough ();
11953 }
11954 else
11955 break;
11956
11957 case LE:
11958 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11959 if (const_op < 0)
11960 {
11961 const_op += 1;
11962 code = LT;
11963 }
11964
11965 /* If we are doing a <= 0 comparison on a value known to have
11966 a zero sign bit, we can replace this with == 0. */
11967 else if (const_op == 0
11968 && is_a <scalar_int_mode> (mode, &int_mode)
11969 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11970 && (nonzero_bits (op0, int_mode)
11971 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11972 == 0)
11973 code = EQ;
11974 break;
11975
11976 case GE:
11977 /* >= C is equivalent to > (C - 1). */
11978 if (const_op > 0)
11979 {
11980 const_op -= 1;
11981 code = GT;
11982 /* ... fall through to GT below. */
11983 gcc_fallthrough ();
11984 }
11985 else
11986 break;
11987
11988 case GT:
11989 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11990 if (const_op < 0)
11991 {
11992 const_op += 1;
11993 code = GE;
11994 }
11995
11996 /* If we are doing a > 0 comparison on a value known to have
11997 a zero sign bit, we can replace this with != 0. */
11998 else if (const_op == 0
11999 && is_a <scalar_int_mode> (mode, &int_mode)
12000 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12001 && (nonzero_bits (op0, int_mode)
12002 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12003 == 0)
12004 code = NE;
12005 break;
12006
12007 case LTU:
12008 /* < C is equivalent to <= (C - 1). */
12009 if (const_op > 0)
12010 {
12011 const_op -= 1;
12012 code = LEU;
12013 /* ... fall through ... */
12014 gcc_fallthrough ();
12015 }
12016 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
12017 else if (is_a <scalar_int_mode> (mode, &int_mode)
12018 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12019 && (((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode))
12020 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12021 {
12022 const_op = 0;
12023 code = GE;
12024 break;
12025 }
12026 else
12027 break;
12028
12029 case LEU:
12030 /* unsigned <= 0 is equivalent to == 0 */
12031 if (const_op == 0)
12032 code = EQ;
12033 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
12034 else if (is_a <scalar_int_mode> (mode, &int_mode)
12035 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12036 && ((unsigned HOST_WIDE_INT) const_op
12037 == ((HOST_WIDE_INT_1U
12038 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
12039 {
12040 const_op = 0;
12041 code = GE;
12042 }
12043 break;
12044
12045 case GEU:
12046 /* >= C is equivalent to > (C - 1). */
12047 if (const_op > 1)
12048 {
12049 const_op -= 1;
12050 code = GTU;
12051 /* ... fall through ... */
12052 gcc_fallthrough ();
12053 }
12054
12055 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
12056 else if (is_a <scalar_int_mode> (mode, &int_mode)
12057 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12058 && (((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode))
12059 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
12060 {
12061 const_op = 0;
12062 code = LT;
12063 break;
12064 }
12065 else
12066 break;
12067
12068 case GTU:
12069 /* unsigned > 0 is equivalent to != 0 */
12070 if (const_op == 0)
12071 code = NE;
12072 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
12073 else if (is_a <scalar_int_mode> (mode, &int_mode)
12074 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12075 && ((unsigned HOST_WIDE_INT) const_op
12076 == (HOST_WIDE_INT_1U
12077 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
12078 {
12079 const_op = 0;
12080 code = LT;
12081 }
12082 break;
12083
12084 default:
12085 break;
12086 }
12087
12088 /* Narrow non-symmetric comparison of memory and constant as e.g.
12089 x0...x7 <= 0x3fffffffffffffff into x0 <= 0x3f where x0 is the most
12090 significant byte. Likewise, transform x0...x7 >= 0x4000000000000000 into
12091 x0 >= 0x40. */
12092 if ((code == LEU || code == LTU || code == GEU || code == GTU)
12093 && is_a <scalar_int_mode> (GET_MODE (op0), &int_mode)
12094 && HWI_COMPUTABLE_MODE_P (int_mode)
12095 && MEM_P (op0)
12096 && !MEM_VOLATILE_P (op0)
12097 /* The optimization makes only sense for constants which are big enough
12098 so that we have a chance to chop off something at all. */
12099 && ((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode)) > 0xff
12100 /* Ensure that we do not overflow during normalization. */
12101 && (code != GTU
12102 || ((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode))
12103 < HOST_WIDE_INT_M1U)
12104 && trunc_int_for_mode (const_op, int_mode) == const_op)
12105 {
12106 unsigned HOST_WIDE_INT n
12107 = (unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode);
12108 enum rtx_code adjusted_code;
12109
12110 /* Normalize code to either LEU or GEU. */
12111 if (code == LTU)
12112 {
12113 --n;
12114 adjusted_code = LEU;
12115 }
12116 else if (code == GTU)
12117 {
12118 ++n;
12119 adjusted_code = GEU;
12120 }
12121 else
12122 adjusted_code = code;
12123
12124 scalar_int_mode narrow_mode_iter;
12125 FOR_EACH_MODE_UNTIL (narrow_mode_iter, int_mode)
12126 {
12127 unsigned nbits = GET_MODE_PRECISION (int_mode)
12128 - GET_MODE_PRECISION (narrow_mode_iter);
12129 unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << nbits) - 1;
12130 unsigned HOST_WIDE_INT lower_bits = n & mask;
12131 if ((adjusted_code == LEU && lower_bits == mask)
12132 || (adjusted_code == GEU && lower_bits == 0))
12133 {
12134 n >>= nbits;
12135 break;
12136 }
12137 }
12138
12139 if (narrow_mode_iter < int_mode)
12140 {
12141 if (dump_file && (dump_flags & TDF_DETAILS))
12142 {
12143 fprintf (
12144 dump_file, "narrow comparison from mode %s to %s: (MEM %s "
12145 HOST_WIDE_INT_PRINT_HEX ") to (MEM %s "
12146 HOST_WIDE_INT_PRINT_HEX ").\n", GET_MODE_NAME (int_mode),
12147 GET_MODE_NAME (narrow_mode_iter), GET_RTX_NAME (code),
12148 (unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode),
12149 GET_RTX_NAME (adjusted_code), n);
12150 }
12151 poly_int64 offset = (BYTES_BIG_ENDIAN
12152 ? 0
12153 : (GET_MODE_SIZE (int_mode)
12154 - GET_MODE_SIZE (narrow_mode_iter)));
12155 *pop0 = adjust_address_nv (op0, narrow_mode_iter, offset);
12156 *pop1 = gen_int_mode (n, narrow_mode_iter);
12157 return adjusted_code;
12158 }
12159 }
12160
12161 *pop1 = GEN_INT (const_op);
12162 return code;
12163 }
12164
12165 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
12166 comparison code that will be tested.
12167
12168 The result is a possibly different comparison code to use. *POP0 and
12169 *POP1 may be updated.
12170
12171 It is possible that we might detect that a comparison is either always
12172 true or always false. However, we do not perform general constant
12173 folding in combine, so this knowledge isn't useful. Such tautologies
12174 should have been detected earlier. Hence we ignore all such cases. */
12175
12176 static enum rtx_code
12177 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
12178 {
12179 rtx op0 = *pop0;
12180 rtx op1 = *pop1;
12181 rtx tem, tem1;
12182 int i;
12183 scalar_int_mode mode, inner_mode, tmode;
12184 opt_scalar_int_mode tmode_iter;
12185
12186 /* Try a few ways of applying the same transformation to both operands. */
12187 while (1)
12188 {
12189 /* The test below this one won't handle SIGN_EXTENDs on these machines,
12190 so check specially. */
12191 if (!WORD_REGISTER_OPERATIONS
12192 && code != GTU && code != GEU && code != LTU && code != LEU
12193 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
12194 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12195 && GET_CODE (XEXP (op1, 0)) == ASHIFT
12196 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
12197 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
12198 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
12199 && (is_a <scalar_int_mode>
12200 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
12201 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
12202 && CONST_INT_P (XEXP (op0, 1))
12203 && XEXP (op0, 1) == XEXP (op1, 1)
12204 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12205 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
12206 && (INTVAL (XEXP (op0, 1))
12207 == (GET_MODE_PRECISION (mode)
12208 - GET_MODE_PRECISION (inner_mode))))
12209 {
12210 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
12211 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
12212 }
12213
12214 /* If both operands are the same constant shift, see if we can ignore the
12215 shift. We can if the shift is a rotate or if the bits shifted out of
12216 this shift are known to be zero for both inputs and if the type of
12217 comparison is compatible with the shift. */
12218 if (GET_CODE (op0) == GET_CODE (op1)
12219 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
12220 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
12221 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
12222 && (code != GT && code != LT && code != GE && code != LE))
12223 || (GET_CODE (op0) == ASHIFTRT
12224 && (code != GTU && code != LTU
12225 && code != GEU && code != LEU)))
12226 && CONST_INT_P (XEXP (op0, 1))
12227 && INTVAL (XEXP (op0, 1)) >= 0
12228 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12229 && XEXP (op0, 1) == XEXP (op1, 1))
12230 {
12231 machine_mode mode = GET_MODE (op0);
12232 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12233 int shift_count = INTVAL (XEXP (op0, 1));
12234
12235 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
12236 mask &= (mask >> shift_count) << shift_count;
12237 else if (GET_CODE (op0) == ASHIFT)
12238 mask = (mask & (mask << shift_count)) >> shift_count;
12239
12240 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12241 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12242 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12243 else
12244 break;
12245 }
12246
12247 /* If both operands are AND's of a paradoxical SUBREG by constant, the
12248 SUBREGs are of the same mode, and, in both cases, the AND would
12249 be redundant if the comparison was done in the narrower mode,
12250 do the comparison in the narrower mode (e.g., we are AND'ing with 1
12251 and the operand's possibly nonzero bits are 0xffffff01; in that case
12252 if we only care about QImode, we don't need the AND). This case
12253 occurs if the output mode of an scc insn is not SImode and
12254 STORE_FLAG_VALUE == 1 (e.g., the 386).
12255
12256 Similarly, check for a case where the AND's are ZERO_EXTEND
12257 operations from some narrower mode even though a SUBREG is not
12258 present. */
12259
12260 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12261 && CONST_INT_P (XEXP (op0, 1))
12262 && CONST_INT_P (XEXP (op1, 1)))
12263 {
12264 rtx inner_op0 = XEXP (op0, 0);
12265 rtx inner_op1 = XEXP (op1, 0);
12266 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12267 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12268 bool changed = false;
12269
12270 if (paradoxical_subreg_p (inner_op0)
12271 && GET_CODE (inner_op1) == SUBREG
12272 && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12273 && (GET_MODE (SUBREG_REG (inner_op0))
12274 == GET_MODE (SUBREG_REG (inner_op1)))
12275 && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12276 GET_MODE (SUBREG_REG (inner_op0)))) == 0
12277 && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12278 GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12279 {
12280 op0 = SUBREG_REG (inner_op0);
12281 op1 = SUBREG_REG (inner_op1);
12282
12283 /* The resulting comparison is always unsigned since we masked
12284 off the original sign bit. */
12285 code = unsigned_condition (code);
12286
12287 changed = true;
12288 }
12289
12290 else if (c0 == c1)
12291 FOR_EACH_MODE_UNTIL (tmode,
12292 as_a <scalar_int_mode> (GET_MODE (op0)))
12293 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12294 {
12295 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12296 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12297 code = unsigned_condition (code);
12298 changed = true;
12299 break;
12300 }
12301
12302 if (! changed)
12303 break;
12304 }
12305
12306 /* If both operands are NOT, we can strip off the outer operation
12307 and adjust the comparison code for swapped operands; similarly for
12308 NEG, except that this must be an equality comparison. */
12309 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12310 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12311 && (code == EQ || code == NE)))
12312 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12313
12314 else
12315 break;
12316 }
12317
12318 /* If the first operand is a constant, swap the operands and adjust the
12319 comparison code appropriately, but don't do this if the second operand
12320 is already a constant integer. */
12321 if (swap_commutative_operands_p (op0, op1))
12322 {
12323 std::swap (op0, op1);
12324 code = swap_condition (code);
12325 }
12326
12327 /* We now enter a loop during which we will try to simplify the comparison.
12328 For the most part, we only are concerned with comparisons with zero,
12329 but some things may really be comparisons with zero but not start
12330 out looking that way. */
12331
12332 while (CONST_INT_P (op1))
12333 {
12334 machine_mode raw_mode = GET_MODE (op0);
12335 scalar_int_mode int_mode;
12336 int equality_comparison_p;
12337 int sign_bit_comparison_p;
12338 int unsigned_comparison_p;
12339 HOST_WIDE_INT const_op;
12340
12341 /* We only want to handle integral modes. This catches VOIDmode,
12342 CCmode, and the floating-point modes. An exception is that we
12343 can handle VOIDmode if OP0 is a COMPARE or a comparison
12344 operation. */
12345
12346 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12347 && ! (raw_mode == VOIDmode
12348 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12349 break;
12350
12351 /* Try to simplify the compare to constant, possibly changing the
12352 comparison op, and/or changing op1 to zero. */
12353 code = simplify_compare_const (code, raw_mode, &op0, &op1);
12354 const_op = INTVAL (op1);
12355
12356 /* Compute some predicates to simplify code below. */
12357
12358 equality_comparison_p = (code == EQ || code == NE);
12359 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12360 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12361 || code == GEU);
12362
12363 /* If this is a sign bit comparison and we can do arithmetic in
12364 MODE, say that we will only be needing the sign bit of OP0. */
12365 if (sign_bit_comparison_p
12366 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12367 && HWI_COMPUTABLE_MODE_P (int_mode))
12368 op0 = force_to_mode (op0, int_mode,
12369 HOST_WIDE_INT_1U
12370 << (GET_MODE_PRECISION (int_mode) - 1), false);
12371
12372 if (COMPARISON_P (op0))
12373 {
12374 /* We can't do anything if OP0 is a condition code value, rather
12375 than an actual data value. */
12376 if (const_op != 0
12377 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12378 break;
12379
12380 /* Get the two operands being compared. */
12381 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12382 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12383 else
12384 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12385
12386 /* Check for the cases where we simply want the result of the
12387 earlier test or the opposite of that result. */
12388 if (code == NE || code == EQ
12389 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12390 && (code == LT || code == GE)))
12391 {
12392 enum rtx_code new_code;
12393 if (code == LT || code == NE)
12394 new_code = GET_CODE (op0);
12395 else
12396 new_code = reversed_comparison_code (op0, NULL);
12397
12398 if (new_code != UNKNOWN)
12399 {
12400 code = new_code;
12401 op0 = tem;
12402 op1 = tem1;
12403 continue;
12404 }
12405 }
12406 break;
12407 }
12408
12409 if (raw_mode == VOIDmode)
12410 break;
12411 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12412
12413 /* Now try cases based on the opcode of OP0. If none of the cases
12414 does a "continue", we exit this loop immediately after the
12415 switch. */
12416
12417 unsigned int mode_width = GET_MODE_PRECISION (mode);
12418 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12419 switch (GET_CODE (op0))
12420 {
12421 case ZERO_EXTRACT:
12422 /* If we are extracting a single bit from a variable position in
12423 a constant that has only a single bit set and are comparing it
12424 with zero, we can convert this into an equality comparison
12425 between the position and the location of the single bit. */
12426 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12427 have already reduced the shift count modulo the word size. */
12428 if (!SHIFT_COUNT_TRUNCATED
12429 && CONST_INT_P (XEXP (op0, 0))
12430 && XEXP (op0, 1) == const1_rtx
12431 && equality_comparison_p && const_op == 0
12432 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12433 {
12434 if (BITS_BIG_ENDIAN)
12435 i = BITS_PER_WORD - 1 - i;
12436
12437 op0 = XEXP (op0, 2);
12438 op1 = GEN_INT (i);
12439 const_op = i;
12440
12441 /* Result is nonzero iff shift count is equal to I. */
12442 code = reverse_condition (code);
12443 continue;
12444 }
12445
12446 /* fall through */
12447
12448 case SIGN_EXTRACT:
12449 tem = expand_compound_operation (op0);
12450 if (tem != op0)
12451 {
12452 op0 = tem;
12453 continue;
12454 }
12455 break;
12456
12457 case NOT:
12458 /* If testing for equality, we can take the NOT of the constant. */
12459 if (equality_comparison_p
12460 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12461 {
12462 op0 = XEXP (op0, 0);
12463 op1 = tem;
12464 continue;
12465 }
12466
12467 /* If just looking at the sign bit, reverse the sense of the
12468 comparison. */
12469 if (sign_bit_comparison_p)
12470 {
12471 op0 = XEXP (op0, 0);
12472 code = (code == GE ? LT : GE);
12473 continue;
12474 }
12475 break;
12476
12477 case NEG:
12478 /* If testing for equality, we can take the NEG of the constant. */
12479 if (equality_comparison_p
12480 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12481 {
12482 op0 = XEXP (op0, 0);
12483 op1 = tem;
12484 continue;
12485 }
12486
12487 /* The remaining cases only apply to comparisons with zero. */
12488 if (const_op != 0)
12489 break;
12490
12491 /* When X is ABS or is known positive,
12492 (neg X) is < 0 if and only if X != 0. */
12493
12494 if (sign_bit_comparison_p
12495 && (GET_CODE (XEXP (op0, 0)) == ABS
12496 || (mode_width <= HOST_BITS_PER_WIDE_INT
12497 && (nonzero_bits (XEXP (op0, 0), mode)
12498 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12499 == 0)))
12500 {
12501 op0 = XEXP (op0, 0);
12502 code = (code == LT ? NE : EQ);
12503 continue;
12504 }
12505
12506 /* If we have NEG of something whose two high-order bits are the
12507 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12508 if (num_sign_bit_copies (op0, mode) >= 2)
12509 {
12510 op0 = XEXP (op0, 0);
12511 code = swap_condition (code);
12512 continue;
12513 }
12514 break;
12515
12516 case ROTATE:
12517 /* If we are testing equality and our count is a constant, we
12518 can perform the inverse operation on our RHS. */
12519 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12520 && (tem = simplify_binary_operation (ROTATERT, mode,
12521 op1, XEXP (op0, 1))) != 0)
12522 {
12523 op0 = XEXP (op0, 0);
12524 op1 = tem;
12525 continue;
12526 }
12527
12528 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12529 a particular bit. Convert it to an AND of a constant of that
12530 bit. This will be converted into a ZERO_EXTRACT. */
12531 if (const_op == 0 && sign_bit_comparison_p
12532 && CONST_INT_P (XEXP (op0, 1))
12533 && mode_width <= HOST_BITS_PER_WIDE_INT
12534 && UINTVAL (XEXP (op0, 1)) < mode_width)
12535 {
12536 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12537 (HOST_WIDE_INT_1U
12538 << (mode_width - 1
12539 - INTVAL (XEXP (op0, 1)))));
12540 code = (code == LT ? NE : EQ);
12541 continue;
12542 }
12543
12544 /* Fall through. */
12545
12546 case ABS:
12547 /* ABS is ignorable inside an equality comparison with zero. */
12548 if (const_op == 0 && equality_comparison_p)
12549 {
12550 op0 = XEXP (op0, 0);
12551 continue;
12552 }
12553 break;
12554
12555 case SIGN_EXTEND:
12556 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12557 (compare FOO CONST) if CONST fits in FOO's mode and we
12558 are either testing inequality or have an unsigned
12559 comparison with ZERO_EXTEND or a signed comparison with
12560 SIGN_EXTEND. But don't do it if we don't have a compare
12561 insn of the given mode, since we'd have to revert it
12562 later on, and then we wouldn't know whether to sign- or
12563 zero-extend. */
12564 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12565 && ! unsigned_comparison_p
12566 && HWI_COMPUTABLE_MODE_P (mode)
12567 && trunc_int_for_mode (const_op, mode) == const_op
12568 && have_insn_for (COMPARE, mode))
12569 {
12570 op0 = XEXP (op0, 0);
12571 continue;
12572 }
12573 break;
12574
12575 case SUBREG:
12576 /* Check for the case where we are comparing A - C1 with C2, that is
12577
12578 (subreg:MODE (plus (A) (-C1))) op (C2)
12579
12580 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12581 comparison in the wider mode. One of the following two conditions
12582 must be true in order for this to be valid:
12583
12584 1. The mode extension results in the same bit pattern being added
12585 on both sides and the comparison is equality or unsigned. As
12586 C2 has been truncated to fit in MODE, the pattern can only be
12587 all 0s or all 1s.
12588
12589 2. The mode extension results in the sign bit being copied on
12590 each side.
12591
12592 The difficulty here is that we have predicates for A but not for
12593 (A - C1) so we need to check that C1 is within proper bounds so
12594 as to perturbate A as little as possible. */
12595
12596 if (mode_width <= HOST_BITS_PER_WIDE_INT
12597 && subreg_lowpart_p (op0)
12598 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12599 &inner_mode)
12600 && GET_MODE_PRECISION (inner_mode) > mode_width
12601 && GET_CODE (SUBREG_REG (op0)) == PLUS
12602 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12603 {
12604 rtx a = XEXP (SUBREG_REG (op0), 0);
12605 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12606
12607 if ((c1 > 0
12608 && (unsigned HOST_WIDE_INT) c1
12609 < HOST_WIDE_INT_1U << (mode_width - 1)
12610 && (equality_comparison_p || unsigned_comparison_p)
12611 /* (A - C1) zero-extends if it is positive and sign-extends
12612 if it is negative, C2 both zero- and sign-extends. */
12613 && (((nonzero_bits (a, inner_mode)
12614 & ~GET_MODE_MASK (mode)) == 0
12615 && const_op >= 0)
12616 /* (A - C1) sign-extends if it is positive and 1-extends
12617 if it is negative, C2 both sign- and 1-extends. */
12618 || (num_sign_bit_copies (a, inner_mode)
12619 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12620 - mode_width)
12621 && const_op < 0)))
12622 || ((unsigned HOST_WIDE_INT) c1
12623 < HOST_WIDE_INT_1U << (mode_width - 2)
12624 /* (A - C1) always sign-extends, like C2. */
12625 && num_sign_bit_copies (a, inner_mode)
12626 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12627 - (mode_width - 1))))
12628 {
12629 op0 = SUBREG_REG (op0);
12630 continue;
12631 }
12632 }
12633
12634 /* If the inner mode is narrower and we are extracting the low part,
12635 we can treat the SUBREG as if it were a ZERO_EXTEND ... */
12636 if (paradoxical_subreg_p (op0))
12637 {
12638 if (WORD_REGISTER_OPERATIONS
12639 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12640 &inner_mode)
12641 && GET_MODE_PRECISION (inner_mode) < BITS_PER_WORD
12642 /* On WORD_REGISTER_OPERATIONS targets the bits
12643 beyond sub_mode aren't considered undefined,
12644 so optimize only if it is a MEM load when MEM loads
12645 zero extend, because then the upper bits are all zero. */
12646 && !(MEM_P (SUBREG_REG (op0))
12647 && load_extend_op (inner_mode) == ZERO_EXTEND))
12648 break;
12649 /* FALLTHROUGH to case ZERO_EXTEND */
12650 }
12651 else if (subreg_lowpart_p (op0)
12652 && GET_MODE_CLASS (mode) == MODE_INT
12653 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12654 && (code == NE || code == EQ)
12655 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12656 && !paradoxical_subreg_p (op0)
12657 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12658 & ~GET_MODE_MASK (mode)) == 0)
12659 {
12660 /* Remove outer subregs that don't do anything. */
12661 tem = gen_lowpart (inner_mode, op1);
12662
12663 if ((nonzero_bits (tem, inner_mode)
12664 & ~GET_MODE_MASK (mode)) == 0)
12665 {
12666 op0 = SUBREG_REG (op0);
12667 op1 = tem;
12668 continue;
12669 }
12670 break;
12671 }
12672 else
12673 break;
12674
12675 /* FALLTHROUGH */
12676
12677 case ZERO_EXTEND:
12678 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12679 && (unsigned_comparison_p || equality_comparison_p)
12680 && HWI_COMPUTABLE_MODE_P (mode)
12681 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12682 && const_op >= 0
12683 && have_insn_for (COMPARE, mode))
12684 {
12685 op0 = XEXP (op0, 0);
12686 continue;
12687 }
12688 break;
12689
12690 case PLUS:
12691 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12692 this for equality comparisons due to pathological cases involving
12693 overflows. */
12694 if (equality_comparison_p
12695 && (tem = simplify_binary_operation (MINUS, mode,
12696 op1, XEXP (op0, 1))) != 0)
12697 {
12698 op0 = XEXP (op0, 0);
12699 op1 = tem;
12700 continue;
12701 }
12702
12703 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12704 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12705 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12706 {
12707 op0 = XEXP (XEXP (op0, 0), 0);
12708 code = (code == LT ? EQ : NE);
12709 continue;
12710 }
12711 break;
12712
12713 case MINUS:
12714 /* We used to optimize signed comparisons against zero, but that
12715 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12716 arrive here as equality comparisons, or (GEU, LTU) are
12717 optimized away. No need to special-case them. */
12718
12719 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12720 (eq B (minus A C)), whichever simplifies. We can only do
12721 this for equality comparisons due to pathological cases involving
12722 overflows. */
12723 if (equality_comparison_p
12724 && (tem = simplify_binary_operation (PLUS, mode,
12725 XEXP (op0, 1), op1)) != 0)
12726 {
12727 op0 = XEXP (op0, 0);
12728 op1 = tem;
12729 continue;
12730 }
12731
12732 if (equality_comparison_p
12733 && (tem = simplify_binary_operation (MINUS, mode,
12734 XEXP (op0, 0), op1)) != 0)
12735 {
12736 op0 = XEXP (op0, 1);
12737 op1 = tem;
12738 continue;
12739 }
12740
12741 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12742 of bits in X minus 1, is one iff X > 0. */
12743 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12744 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12745 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12746 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12747 {
12748 op0 = XEXP (op0, 1);
12749 code = (code == GE ? LE : GT);
12750 continue;
12751 }
12752 break;
12753
12754 case XOR:
12755 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12756 if C is zero or B is a constant. */
12757 if (equality_comparison_p
12758 && (tem = simplify_binary_operation (XOR, mode,
12759 XEXP (op0, 1), op1)) != 0)
12760 {
12761 op0 = XEXP (op0, 0);
12762 op1 = tem;
12763 continue;
12764 }
12765 break;
12766
12767
12768 case IOR:
12769 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12770 iff X <= 0. */
12771 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12772 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12773 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12774 {
12775 op0 = XEXP (op0, 1);
12776 code = (code == GE ? GT : LE);
12777 continue;
12778 }
12779 break;
12780
12781 case AND:
12782 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12783 will be converted to a ZERO_EXTRACT later. */
12784 if (const_op == 0 && equality_comparison_p
12785 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12786 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12787 {
12788 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12789 XEXP (XEXP (op0, 0), 1));
12790 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12791 continue;
12792 }
12793
12794 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12795 zero and X is a comparison and C1 and C2 describe only bits set
12796 in STORE_FLAG_VALUE, we can compare with X. */
12797 if (const_op == 0 && equality_comparison_p
12798 && mode_width <= HOST_BITS_PER_WIDE_INT
12799 && CONST_INT_P (XEXP (op0, 1))
12800 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12801 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12802 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12803 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12804 {
12805 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12806 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12807 if ((~STORE_FLAG_VALUE & mask) == 0
12808 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12809 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12810 && COMPARISON_P (tem))))
12811 {
12812 op0 = XEXP (XEXP (op0, 0), 0);
12813 continue;
12814 }
12815 }
12816
12817 /* If we are doing an equality comparison of an AND of a bit equal
12818 to the sign bit, replace this with a LT or GE comparison of
12819 the underlying value. */
12820 if (equality_comparison_p
12821 && const_op == 0
12822 && CONST_INT_P (XEXP (op0, 1))
12823 && mode_width <= HOST_BITS_PER_WIDE_INT
12824 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12825 == HOST_WIDE_INT_1U << (mode_width - 1)))
12826 {
12827 op0 = XEXP (op0, 0);
12828 code = (code == EQ ? GE : LT);
12829 continue;
12830 }
12831
12832 /* If this AND operation is really a ZERO_EXTEND from a narrower
12833 mode, the constant fits within that mode, and this is either an
12834 equality or unsigned comparison, try to do this comparison in
12835 the narrower mode.
12836
12837 Note that in:
12838
12839 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12840 -> (ne:DI (reg:SI 4) (const_int 0))
12841
12842 unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12843 known to hold a value of the required mode the
12844 transformation is invalid. */
12845 if ((equality_comparison_p || unsigned_comparison_p)
12846 && CONST_INT_P (XEXP (op0, 1))
12847 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12848 & GET_MODE_MASK (mode))
12849 + 1)) >= 0
12850 && const_op >> i == 0
12851 && int_mode_for_size (i, 1).exists (&tmode))
12852 {
12853 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12854 continue;
12855 }
12856
12857 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12858 fits in both M1 and M2 and the SUBREG is either paradoxical
12859 or represents the low part, permute the SUBREG and the AND
12860 and try again. */
12861 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12862 && CONST_INT_P (XEXP (op0, 1)))
12863 {
12864 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12865 /* Require an integral mode, to avoid creating something like
12866 (AND:SF ...). */
12867 if ((is_a <scalar_int_mode>
12868 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12869 /* It is unsafe to commute the AND into the SUBREG if the
12870 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12871 not defined. As originally written the upper bits
12872 have a defined value due to the AND operation.
12873 However, if we commute the AND inside the SUBREG then
12874 they no longer have defined values and the meaning of
12875 the code has been changed.
12876 Also C1 should not change value in the smaller mode,
12877 see PR67028 (a positive C1 can become negative in the
12878 smaller mode, so that the AND does no longer mask the
12879 upper bits). */
12880 && ((WORD_REGISTER_OPERATIONS
12881 && mode_width > GET_MODE_PRECISION (tmode)
12882 && mode_width <= BITS_PER_WORD
12883 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12884 || (mode_width <= GET_MODE_PRECISION (tmode)
12885 && subreg_lowpart_p (XEXP (op0, 0))))
12886 && mode_width <= HOST_BITS_PER_WIDE_INT
12887 && HWI_COMPUTABLE_MODE_P (tmode)
12888 && (c1 & ~mask) == 0
12889 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12890 && c1 != mask
12891 && c1 != GET_MODE_MASK (tmode))
12892 {
12893 op0 = simplify_gen_binary (AND, tmode,
12894 SUBREG_REG (XEXP (op0, 0)),
12895 gen_int_mode (c1, tmode));
12896 op0 = gen_lowpart (mode, op0);
12897 continue;
12898 }
12899 }
12900
12901 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12902 if (const_op == 0 && equality_comparison_p
12903 && XEXP (op0, 1) == const1_rtx
12904 && GET_CODE (XEXP (op0, 0)) == NOT)
12905 {
12906 op0 = simplify_and_const_int (NULL_RTX, mode,
12907 XEXP (XEXP (op0, 0), 0), 1);
12908 code = (code == NE ? EQ : NE);
12909 continue;
12910 }
12911
12912 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12913 (eq (and (lshiftrt X) 1) 0).
12914 Also handle the case where (not X) is expressed using xor. */
12915 if (const_op == 0 && equality_comparison_p
12916 && XEXP (op0, 1) == const1_rtx
12917 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12918 {
12919 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12920 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12921
12922 if (GET_CODE (shift_op) == NOT
12923 || (GET_CODE (shift_op) == XOR
12924 && CONST_INT_P (XEXP (shift_op, 1))
12925 && CONST_INT_P (shift_count)
12926 && HWI_COMPUTABLE_MODE_P (mode)
12927 && (UINTVAL (XEXP (shift_op, 1))
12928 == HOST_WIDE_INT_1U
12929 << INTVAL (shift_count))))
12930 {
12931 op0
12932 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12933 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12934 code = (code == NE ? EQ : NE);
12935 continue;
12936 }
12937 }
12938 break;
12939
12940 case ASHIFT:
12941 /* If we have (compare (ashift FOO N) (const_int C)) and
12942 the high order N bits of FOO (N+1 if an inequality comparison)
12943 are known to be zero, we can do this by comparing FOO with C
12944 shifted right N bits so long as the low-order N bits of C are
12945 zero. */
12946 if (CONST_INT_P (XEXP (op0, 1))
12947 && INTVAL (XEXP (op0, 1)) >= 0
12948 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12949 < HOST_BITS_PER_WIDE_INT)
12950 && (((unsigned HOST_WIDE_INT) const_op
12951 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12952 - 1)) == 0)
12953 && mode_width <= HOST_BITS_PER_WIDE_INT
12954 && (nonzero_bits (XEXP (op0, 0), mode)
12955 & ~(mask >> (INTVAL (XEXP (op0, 1))
12956 + ! equality_comparison_p))) == 0)
12957 {
12958 /* We must perform a logical shift, not an arithmetic one,
12959 as we want the top N bits of C to be zero. */
12960 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12961
12962 temp >>= INTVAL (XEXP (op0, 1));
12963 op1 = gen_int_mode (temp, mode);
12964 op0 = XEXP (op0, 0);
12965 continue;
12966 }
12967
12968 /* If we are doing a sign bit comparison, it means we are testing
12969 a particular bit. Convert it to the appropriate AND. */
12970 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12971 && mode_width <= HOST_BITS_PER_WIDE_INT)
12972 {
12973 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12974 (HOST_WIDE_INT_1U
12975 << (mode_width - 1
12976 - INTVAL (XEXP (op0, 1)))));
12977 code = (code == LT ? NE : EQ);
12978 continue;
12979 }
12980
12981 /* If this an equality comparison with zero and we are shifting
12982 the low bit to the sign bit, we can convert this to an AND of the
12983 low-order bit. */
12984 if (const_op == 0 && equality_comparison_p
12985 && CONST_INT_P (XEXP (op0, 1))
12986 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12987 {
12988 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12989 continue;
12990 }
12991 break;
12992
12993 case ASHIFTRT:
12994 /* If this is an equality comparison with zero, we can do this
12995 as a logical shift, which might be much simpler. */
12996 if (equality_comparison_p && const_op == 0
12997 && CONST_INT_P (XEXP (op0, 1)))
12998 {
12999 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
13000 XEXP (op0, 0),
13001 INTVAL (XEXP (op0, 1)));
13002 continue;
13003 }
13004
13005 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
13006 do the comparison in a narrower mode. */
13007 if (! unsigned_comparison_p
13008 && CONST_INT_P (XEXP (op0, 1))
13009 && GET_CODE (XEXP (op0, 0)) == ASHIFT
13010 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
13011 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
13012 .exists (&tmode))
13013 && (((unsigned HOST_WIDE_INT) const_op
13014 + (GET_MODE_MASK (tmode) >> 1) + 1)
13015 <= GET_MODE_MASK (tmode)))
13016 {
13017 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
13018 continue;
13019 }
13020
13021 /* Likewise if OP0 is a PLUS of a sign extension with a
13022 constant, which is usually represented with the PLUS
13023 between the shifts. */
13024 if (! unsigned_comparison_p
13025 && CONST_INT_P (XEXP (op0, 1))
13026 && GET_CODE (XEXP (op0, 0)) == PLUS
13027 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
13028 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
13029 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
13030 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
13031 .exists (&tmode))
13032 && (((unsigned HOST_WIDE_INT) const_op
13033 + (GET_MODE_MASK (tmode) >> 1) + 1)
13034 <= GET_MODE_MASK (tmode)))
13035 {
13036 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
13037 rtx add_const = XEXP (XEXP (op0, 0), 1);
13038 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
13039 add_const, XEXP (op0, 1));
13040
13041 op0 = simplify_gen_binary (PLUS, tmode,
13042 gen_lowpart (tmode, inner),
13043 new_const);
13044 continue;
13045 }
13046
13047 /* FALLTHROUGH */
13048 case LSHIFTRT:
13049 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
13050 the low order N bits of FOO are known to be zero, we can do this
13051 by comparing FOO with C shifted left N bits so long as no
13052 overflow occurs. Even if the low order N bits of FOO aren't known
13053 to be zero, if the comparison is >= or < we can use the same
13054 optimization and for > or <= by setting all the low
13055 order N bits in the comparison constant. */
13056 if (CONST_INT_P (XEXP (op0, 1))
13057 && INTVAL (XEXP (op0, 1)) > 0
13058 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
13059 && mode_width <= HOST_BITS_PER_WIDE_INT
13060 && (((unsigned HOST_WIDE_INT) const_op
13061 + (GET_CODE (op0) != LSHIFTRT
13062 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
13063 + 1)
13064 : 0))
13065 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
13066 {
13067 unsigned HOST_WIDE_INT low_bits
13068 = (nonzero_bits (XEXP (op0, 0), mode)
13069 & ((HOST_WIDE_INT_1U
13070 << INTVAL (XEXP (op0, 1))) - 1));
13071 if (low_bits == 0 || !equality_comparison_p)
13072 {
13073 /* If the shift was logical, then we must make the condition
13074 unsigned. */
13075 if (GET_CODE (op0) == LSHIFTRT)
13076 code = unsigned_condition (code);
13077
13078 const_op = (unsigned HOST_WIDE_INT) const_op
13079 << INTVAL (XEXP (op0, 1));
13080 if (low_bits != 0
13081 && (code == GT || code == GTU
13082 || code == LE || code == LEU))
13083 const_op
13084 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
13085 op1 = GEN_INT (const_op);
13086 op0 = XEXP (op0, 0);
13087 continue;
13088 }
13089 }
13090
13091 /* If we are using this shift to extract just the sign bit, we
13092 can replace this with an LT or GE comparison. */
13093 if (const_op == 0
13094 && (equality_comparison_p || sign_bit_comparison_p)
13095 && CONST_INT_P (XEXP (op0, 1))
13096 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
13097 {
13098 op0 = XEXP (op0, 0);
13099 code = (code == NE || code == GT ? LT : GE);
13100 continue;
13101 }
13102 break;
13103
13104 default:
13105 break;
13106 }
13107
13108 break;
13109 }
13110
13111 /* Now make any compound operations involved in this comparison. Then,
13112 check for an outmost SUBREG on OP0 that is not doing anything or is
13113 paradoxical. The latter transformation must only be performed when
13114 it is known that the "extra" bits will be the same in op0 and op1 or
13115 that they don't matter. There are three cases to consider:
13116
13117 1. SUBREG_REG (op0) is a register. In this case the bits are don't
13118 care bits and we can assume they have any convenient value. So
13119 making the transformation is safe.
13120
13121 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
13122 In this case the upper bits of op0 are undefined. We should not make
13123 the simplification in that case as we do not know the contents of
13124 those bits.
13125
13126 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
13127 In that case we know those bits are zeros or ones. We must also be
13128 sure that they are the same as the upper bits of op1.
13129
13130 We can never remove a SUBREG for a non-equality comparison because
13131 the sign bit is in a different place in the underlying object. */
13132
13133 rtx_code op0_mco_code = SET;
13134 if (op1 == const0_rtx)
13135 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
13136
13137 op0 = make_compound_operation (op0, op0_mco_code);
13138 op1 = make_compound_operation (op1, SET);
13139
13140 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
13141 && is_int_mode (GET_MODE (op0), &mode)
13142 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
13143 && (code == NE || code == EQ))
13144 {
13145 if (paradoxical_subreg_p (op0))
13146 {
13147 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
13148 implemented. */
13149 if (REG_P (SUBREG_REG (op0)))
13150 {
13151 op0 = SUBREG_REG (op0);
13152 op1 = gen_lowpart (inner_mode, op1);
13153 }
13154 }
13155 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
13156 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
13157 & ~GET_MODE_MASK (mode)) == 0)
13158 {
13159 tem = gen_lowpart (inner_mode, op1);
13160
13161 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
13162 op0 = SUBREG_REG (op0), op1 = tem;
13163 }
13164 }
13165
13166 /* We now do the opposite procedure: Some machines don't have compare
13167 insns in all modes. If OP0's mode is an integer mode smaller than a
13168 word and we can't do a compare in that mode, see if there is a larger
13169 mode for which we can do the compare. There are a number of cases in
13170 which we can use the wider mode. */
13171
13172 if (is_int_mode (GET_MODE (op0), &mode)
13173 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
13174 && ! have_insn_for (COMPARE, mode))
13175 FOR_EACH_WIDER_MODE (tmode_iter, mode)
13176 {
13177 tmode = tmode_iter.require ();
13178 if (!HWI_COMPUTABLE_MODE_P (tmode))
13179 break;
13180 if (have_insn_for (COMPARE, tmode))
13181 {
13182 int zero_extended;
13183
13184 /* If this is a test for negative, we can make an explicit
13185 test of the sign bit. Test this first so we can use
13186 a paradoxical subreg to extend OP0. */
13187
13188 if (op1 == const0_rtx && (code == LT || code == GE)
13189 && HWI_COMPUTABLE_MODE_P (mode))
13190 {
13191 unsigned HOST_WIDE_INT sign
13192 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
13193 op0 = simplify_gen_binary (AND, tmode,
13194 gen_lowpart (tmode, op0),
13195 gen_int_mode (sign, tmode));
13196 code = (code == LT) ? NE : EQ;
13197 break;
13198 }
13199
13200 /* If the only nonzero bits in OP0 and OP1 are those in the
13201 narrower mode and this is an equality or unsigned comparison,
13202 we can use the wider mode. Similarly for sign-extended
13203 values, in which case it is true for all comparisons. */
13204 zero_extended = ((code == EQ || code == NE
13205 || code == GEU || code == GTU
13206 || code == LEU || code == LTU)
13207 && (nonzero_bits (op0, tmode)
13208 & ~GET_MODE_MASK (mode)) == 0
13209 && ((CONST_INT_P (op1)
13210 || (nonzero_bits (op1, tmode)
13211 & ~GET_MODE_MASK (mode)) == 0)));
13212
13213 if (zero_extended
13214 || ((num_sign_bit_copies (op0, tmode)
13215 > (unsigned int) (GET_MODE_PRECISION (tmode)
13216 - GET_MODE_PRECISION (mode)))
13217 && (num_sign_bit_copies (op1, tmode)
13218 > (unsigned int) (GET_MODE_PRECISION (tmode)
13219 - GET_MODE_PRECISION (mode)))))
13220 {
13221 /* If OP0 is an AND and we don't have an AND in MODE either,
13222 make a new AND in the proper mode. */
13223 if (GET_CODE (op0) == AND
13224 && !have_insn_for (AND, mode))
13225 op0 = simplify_gen_binary (AND, tmode,
13226 gen_lowpart (tmode,
13227 XEXP (op0, 0)),
13228 gen_lowpart (tmode,
13229 XEXP (op0, 1)));
13230 else
13231 {
13232 if (zero_extended)
13233 {
13234 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
13235 op0, mode);
13236 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
13237 op1, mode);
13238 }
13239 else
13240 {
13241 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
13242 op0, mode);
13243 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
13244 op1, mode);
13245 }
13246 break;
13247 }
13248 }
13249 }
13250 }
13251
13252 /* We may have changed the comparison operands. Re-canonicalize. */
13253 if (swap_commutative_operands_p (op0, op1))
13254 {
13255 std::swap (op0, op1);
13256 code = swap_condition (code);
13257 }
13258
13259 /* If this machine only supports a subset of valid comparisons, see if we
13260 can convert an unsupported one into a supported one. */
13261 target_canonicalize_comparison (&code, &op0, &op1, 0);
13262
13263 *pop0 = op0;
13264 *pop1 = op1;
13265
13266 return code;
13267 }
13268
13269 /* Utility function for record_value_for_reg. Count number of
13270 rtxs in X. */
13271 static int
13272 count_rtxs (rtx x)
13273 {
13274 enum rtx_code code = GET_CODE (x);
13275 const char *fmt;
13276 int i, j, ret = 1;
13277
13278 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13279 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13280 {
13281 rtx x0 = XEXP (x, 0);
13282 rtx x1 = XEXP (x, 1);
13283
13284 if (x0 == x1)
13285 return 1 + 2 * count_rtxs (x0);
13286
13287 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13288 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13289 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13290 return 2 + 2 * count_rtxs (x0)
13291 + count_rtxs (x == XEXP (x1, 0)
13292 ? XEXP (x1, 1) : XEXP (x1, 0));
13293
13294 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13295 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13296 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13297 return 2 + 2 * count_rtxs (x1)
13298 + count_rtxs (x == XEXP (x0, 0)
13299 ? XEXP (x0, 1) : XEXP (x0, 0));
13300 }
13301
13302 fmt = GET_RTX_FORMAT (code);
13303 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13304 if (fmt[i] == 'e')
13305 ret += count_rtxs (XEXP (x, i));
13306 else if (fmt[i] == 'E')
13307 for (j = 0; j < XVECLEN (x, i); j++)
13308 ret += count_rtxs (XVECEXP (x, i, j));
13309
13310 return ret;
13311 }
13312
13313 /* Utility function for following routine. Called when X is part of a value
13314 being stored into last_set_value. Sets last_set_table_tick
13315 for each register mentioned. Similar to mention_regs in cse.cc */
13316
13317 static void
13318 update_table_tick (rtx x)
13319 {
13320 enum rtx_code code = GET_CODE (x);
13321 const char *fmt = GET_RTX_FORMAT (code);
13322 int i, j;
13323
13324 if (code == REG)
13325 {
13326 unsigned int regno = REGNO (x);
13327 unsigned int endregno = END_REGNO (x);
13328 unsigned int r;
13329
13330 for (r = regno; r < endregno; r++)
13331 {
13332 reg_stat_type *rsp = &reg_stat[r];
13333 rsp->last_set_table_tick = label_tick;
13334 }
13335
13336 return;
13337 }
13338
13339 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13340 if (fmt[i] == 'e')
13341 {
13342 /* Check for identical subexpressions. If x contains
13343 identical subexpression we only have to traverse one of
13344 them. */
13345 if (i == 0 && ARITHMETIC_P (x))
13346 {
13347 /* Note that at this point x1 has already been
13348 processed. */
13349 rtx x0 = XEXP (x, 0);
13350 rtx x1 = XEXP (x, 1);
13351
13352 /* If x0 and x1 are identical then there is no need to
13353 process x0. */
13354 if (x0 == x1)
13355 break;
13356
13357 /* If x0 is identical to a subexpression of x1 then while
13358 processing x1, x0 has already been processed. Thus we
13359 are done with x. */
13360 if (ARITHMETIC_P (x1)
13361 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13362 break;
13363
13364 /* If x1 is identical to a subexpression of x0 then we
13365 still have to process the rest of x0. */
13366 if (ARITHMETIC_P (x0)
13367 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13368 {
13369 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13370 break;
13371 }
13372 }
13373
13374 update_table_tick (XEXP (x, i));
13375 }
13376 else if (fmt[i] == 'E')
13377 for (j = 0; j < XVECLEN (x, i); j++)
13378 update_table_tick (XVECEXP (x, i, j));
13379 }
13380
13381 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13382 are saying that the register is clobbered and we no longer know its
13383 value. If INSN is zero, don't update reg_stat[].last_set; this is
13384 only permitted with VALUE also zero and is used to invalidate the
13385 register. */
13386
13387 static void
13388 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13389 {
13390 unsigned int regno = REGNO (reg);
13391 unsigned int endregno = END_REGNO (reg);
13392 unsigned int i;
13393 reg_stat_type *rsp;
13394
13395 /* If VALUE contains REG and we have a previous value for REG, substitute
13396 the previous value. */
13397 if (value && insn && reg_overlap_mentioned_p (reg, value))
13398 {
13399 rtx tem;
13400
13401 /* Set things up so get_last_value is allowed to see anything set up to
13402 our insn. */
13403 subst_low_luid = DF_INSN_LUID (insn);
13404 tem = get_last_value (reg);
13405
13406 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13407 it isn't going to be useful and will take a lot of time to process,
13408 so just use the CLOBBER. */
13409
13410 if (tem)
13411 {
13412 if (ARITHMETIC_P (tem)
13413 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13414 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13415 tem = XEXP (tem, 0);
13416 else if (count_occurrences (value, reg, 1) >= 2)
13417 {
13418 /* If there are two or more occurrences of REG in VALUE,
13419 prevent the value from growing too much. */
13420 if (count_rtxs (tem) > param_max_last_value_rtl)
13421 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13422 }
13423
13424 value = replace_rtx (copy_rtx (value), reg, tem);
13425 }
13426 }
13427
13428 /* For each register modified, show we don't know its value, that
13429 we don't know about its bitwise content, that its value has been
13430 updated, and that we don't know the location of the death of the
13431 register. */
13432 for (i = regno; i < endregno; i++)
13433 {
13434 rsp = &reg_stat[i];
13435
13436 if (insn)
13437 rsp->last_set = insn;
13438
13439 rsp->last_set_value = 0;
13440 rsp->last_set_mode = VOIDmode;
13441 rsp->last_set_nonzero_bits = 0;
13442 rsp->last_set_sign_bit_copies = 0;
13443 rsp->last_death = 0;
13444 rsp->truncated_to_mode = VOIDmode;
13445 }
13446
13447 /* Mark registers that are being referenced in this value. */
13448 if (value)
13449 update_table_tick (value);
13450
13451 /* Now update the status of each register being set.
13452 If someone is using this register in this block, set this register
13453 to invalid since we will get confused between the two lives in this
13454 basic block. This makes using this register always invalid. In cse, we
13455 scan the table to invalidate all entries using this register, but this
13456 is too much work for us. */
13457
13458 for (i = regno; i < endregno; i++)
13459 {
13460 rsp = &reg_stat[i];
13461 rsp->last_set_label = label_tick;
13462 if (!insn
13463 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13464 rsp->last_set_invalid = true;
13465 else
13466 rsp->last_set_invalid = false;
13467 }
13468
13469 /* The value being assigned might refer to X (like in "x++;"). In that
13470 case, we must replace it with (clobber (const_int 0)) to prevent
13471 infinite loops. */
13472 rsp = &reg_stat[regno];
13473 if (value && !get_last_value_validate (&value, insn, label_tick, false))
13474 {
13475 value = copy_rtx (value);
13476 if (!get_last_value_validate (&value, insn, label_tick, true))
13477 value = 0;
13478 }
13479
13480 /* For the main register being modified, update the value, the mode, the
13481 nonzero bits, and the number of sign bit copies. */
13482
13483 rsp->last_set_value = value;
13484
13485 if (value)
13486 {
13487 machine_mode mode = GET_MODE (reg);
13488 subst_low_luid = DF_INSN_LUID (insn);
13489 rsp->last_set_mode = mode;
13490 if (GET_MODE_CLASS (mode) == MODE_INT
13491 && HWI_COMPUTABLE_MODE_P (mode))
13492 mode = nonzero_bits_mode;
13493 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13494 rsp->last_set_sign_bit_copies
13495 = num_sign_bit_copies (value, GET_MODE (reg));
13496 }
13497 }
13498
13499 /* Called via note_stores from record_dead_and_set_regs to handle one
13500 SET or CLOBBER in an insn. DATA is the instruction in which the
13501 set is occurring. */
13502
13503 static void
13504 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13505 {
13506 rtx_insn *record_dead_insn = (rtx_insn *) data;
13507
13508 if (GET_CODE (dest) == SUBREG)
13509 dest = SUBREG_REG (dest);
13510
13511 if (!record_dead_insn)
13512 {
13513 if (REG_P (dest))
13514 record_value_for_reg (dest, NULL, NULL_RTX);
13515 return;
13516 }
13517
13518 if (REG_P (dest))
13519 {
13520 /* If we are setting the whole register, we know its value. */
13521 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13522 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13523 /* We can handle a SUBREG if it's the low part, but we must be
13524 careful with paradoxical SUBREGs on RISC architectures because
13525 we cannot strip e.g. an extension around a load and record the
13526 naked load since the RTL middle-end considers that the upper bits
13527 are defined according to LOAD_EXTEND_OP. */
13528 else if (GET_CODE (setter) == SET
13529 && GET_CODE (SET_DEST (setter)) == SUBREG
13530 && SUBREG_REG (SET_DEST (setter)) == dest
13531 && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13532 BITS_PER_WORD)
13533 && subreg_lowpart_p (SET_DEST (setter)))
13534 {
13535 if (WORD_REGISTER_OPERATIONS
13536 && word_register_operation_p (SET_SRC (setter))
13537 && paradoxical_subreg_p (SET_DEST (setter)))
13538 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13539 else if (!partial_subreg_p (SET_DEST (setter)))
13540 record_value_for_reg (dest, record_dead_insn,
13541 gen_lowpart (GET_MODE (dest),
13542 SET_SRC (setter)));
13543 else
13544 {
13545 record_value_for_reg (dest, record_dead_insn,
13546 gen_lowpart (GET_MODE (dest),
13547 SET_SRC (setter)));
13548
13549 unsigned HOST_WIDE_INT mask;
13550 reg_stat_type *rsp = &reg_stat[REGNO (dest)];
13551 mask = GET_MODE_MASK (GET_MODE (SET_DEST (setter)));
13552 rsp->last_set_nonzero_bits |= ~mask;
13553 rsp->last_set_sign_bit_copies = 1;
13554 }
13555 }
13556 /* Otherwise show that we don't know the value. */
13557 else
13558 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13559 }
13560 else if (MEM_P (dest)
13561 /* Ignore pushes, they clobber nothing. */
13562 && ! push_operand (dest, GET_MODE (dest)))
13563 mem_last_set = DF_INSN_LUID (record_dead_insn);
13564 }
13565
13566 /* Update the records of when each REG was most recently set or killed
13567 for the things done by INSN. This is the last thing done in processing
13568 INSN in the combiner loop.
13569
13570 We update reg_stat[], in particular fields last_set, last_set_value,
13571 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13572 last_death, and also the similar information mem_last_set (which insn
13573 most recently modified memory) and last_call_luid (which insn was the
13574 most recent subroutine call). */
13575
13576 static void
13577 record_dead_and_set_regs (rtx_insn *insn)
13578 {
13579 rtx link;
13580 unsigned int i;
13581
13582 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13583 {
13584 if (REG_NOTE_KIND (link) == REG_DEAD
13585 && REG_P (XEXP (link, 0)))
13586 {
13587 unsigned int regno = REGNO (XEXP (link, 0));
13588 unsigned int endregno = END_REGNO (XEXP (link, 0));
13589
13590 for (i = regno; i < endregno; i++)
13591 {
13592 reg_stat_type *rsp;
13593
13594 rsp = &reg_stat[i];
13595 rsp->last_death = insn;
13596 }
13597 }
13598 else if (REG_NOTE_KIND (link) == REG_INC)
13599 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13600 }
13601
13602 if (CALL_P (insn))
13603 {
13604 HARD_REG_SET callee_clobbers
13605 = insn_callee_abi (insn).full_and_partial_reg_clobbers ();
13606 hard_reg_set_iterator hrsi;
13607 EXECUTE_IF_SET_IN_HARD_REG_SET (callee_clobbers, 0, i, hrsi)
13608 {
13609 reg_stat_type *rsp;
13610
13611 /* ??? We could try to preserve some information from the last
13612 set of register I if the call doesn't actually clobber
13613 (reg:last_set_mode I), which might be true for ABIs with
13614 partial clobbers. However, it would be difficult to
13615 update last_set_nonzero_bits and last_sign_bit_copies
13616 to account for the part of I that actually was clobbered.
13617 It wouldn't help much anyway, since we rarely see this
13618 situation before RA. */
13619 rsp = &reg_stat[i];
13620 rsp->last_set_invalid = true;
13621 rsp->last_set = insn;
13622 rsp->last_set_value = 0;
13623 rsp->last_set_mode = VOIDmode;
13624 rsp->last_set_nonzero_bits = 0;
13625 rsp->last_set_sign_bit_copies = 0;
13626 rsp->last_death = 0;
13627 rsp->truncated_to_mode = VOIDmode;
13628 }
13629
13630 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13631
13632 /* We can't combine into a call pattern. Remember, though, that
13633 the return value register is set at this LUID. We could
13634 still replace a register with the return value from the
13635 wrong subroutine call! */
13636 note_stores (insn, record_dead_and_set_regs_1, NULL_RTX);
13637 }
13638 else
13639 note_stores (insn, record_dead_and_set_regs_1, insn);
13640 }
13641
13642 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13643 register present in the SUBREG, so for each such SUBREG go back and
13644 adjust nonzero and sign bit information of the registers that are
13645 known to have some zero/sign bits set.
13646
13647 This is needed because when combine blows the SUBREGs away, the
13648 information on zero/sign bits is lost and further combines can be
13649 missed because of that. */
13650
13651 static void
13652 record_promoted_value (rtx_insn *insn, rtx subreg)
13653 {
13654 struct insn_link *links;
13655 rtx set;
13656 unsigned int regno = REGNO (SUBREG_REG (subreg));
13657 machine_mode mode = GET_MODE (subreg);
13658
13659 if (!HWI_COMPUTABLE_MODE_P (mode))
13660 return;
13661
13662 for (links = LOG_LINKS (insn); links;)
13663 {
13664 reg_stat_type *rsp;
13665
13666 insn = links->insn;
13667 set = single_set (insn);
13668
13669 if (! set || !REG_P (SET_DEST (set))
13670 || REGNO (SET_DEST (set)) != regno
13671 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13672 {
13673 links = links->next;
13674 continue;
13675 }
13676
13677 rsp = &reg_stat[regno];
13678 if (rsp->last_set == insn)
13679 {
13680 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13681 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13682 }
13683
13684 if (REG_P (SET_SRC (set)))
13685 {
13686 regno = REGNO (SET_SRC (set));
13687 links = LOG_LINKS (insn);
13688 }
13689 else
13690 break;
13691 }
13692 }
13693
13694 /* Check if X, a register, is known to contain a value already
13695 truncated to MODE. In this case we can use a subreg to refer to
13696 the truncated value even though in the generic case we would need
13697 an explicit truncation. */
13698
13699 static bool
13700 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13701 {
13702 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13703 machine_mode truncated = rsp->truncated_to_mode;
13704
13705 if (truncated == 0
13706 || rsp->truncation_label < label_tick_ebb_start)
13707 return false;
13708 if (!partial_subreg_p (mode, truncated))
13709 return true;
13710 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13711 return true;
13712 return false;
13713 }
13714
13715 /* If X is a hard reg or a subreg record the mode that the register is
13716 accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13717 able to turn a truncate into a subreg using this information. Return true
13718 if traversing X is complete. */
13719
13720 static bool
13721 record_truncated_value (rtx x)
13722 {
13723 machine_mode truncated_mode;
13724 reg_stat_type *rsp;
13725
13726 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13727 {
13728 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13729 truncated_mode = GET_MODE (x);
13730
13731 if (!partial_subreg_p (truncated_mode, original_mode))
13732 return true;
13733
13734 truncated_mode = GET_MODE (x);
13735 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13736 return true;
13737
13738 x = SUBREG_REG (x);
13739 }
13740 /* ??? For hard-regs we now record everything. We might be able to
13741 optimize this using last_set_mode. */
13742 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13743 truncated_mode = GET_MODE (x);
13744 else
13745 return false;
13746
13747 rsp = &reg_stat[REGNO (x)];
13748 if (rsp->truncated_to_mode == 0
13749 || rsp->truncation_label < label_tick_ebb_start
13750 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13751 {
13752 rsp->truncated_to_mode = truncated_mode;
13753 rsp->truncation_label = label_tick;
13754 }
13755
13756 return true;
13757 }
13758
13759 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13760 the modes they are used in. This can help truning TRUNCATEs into
13761 SUBREGs. */
13762
13763 static void
13764 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13765 {
13766 subrtx_var_iterator::array_type array;
13767 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13768 if (record_truncated_value (*iter))
13769 iter.skip_subrtxes ();
13770 }
13771
13772 /* Scan X for promoted SUBREGs. For each one found,
13773 note what it implies to the registers used in it. */
13774
13775 static void
13776 check_promoted_subreg (rtx_insn *insn, rtx x)
13777 {
13778 if (GET_CODE (x) == SUBREG
13779 && SUBREG_PROMOTED_VAR_P (x)
13780 && REG_P (SUBREG_REG (x)))
13781 record_promoted_value (insn, x);
13782 else
13783 {
13784 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13785 int i, j;
13786
13787 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13788 switch (format[i])
13789 {
13790 case 'e':
13791 check_promoted_subreg (insn, XEXP (x, i));
13792 break;
13793 case 'V':
13794 case 'E':
13795 if (XVEC (x, i) != 0)
13796 for (j = 0; j < XVECLEN (x, i); j++)
13797 check_promoted_subreg (insn, XVECEXP (x, i, j));
13798 break;
13799 }
13800 }
13801 }
13802
13803 /* Verify that all the registers and memory references mentioned in *LOC are
13804 still valid. *LOC was part of a value set in INSN when label_tick was
13805 equal to TICK. Return false if some are not. If REPLACE is true, replace
13806 the invalid references with (clobber (const_int 0)) and return true. This
13807 replacement is useful because we often can get useful information about
13808 the form of a value (e.g., if it was produced by a shift that always
13809 produces -1 or 0) even though we don't know exactly what registers it
13810 was produced from. */
13811
13812 static bool
13813 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, bool replace)
13814 {
13815 rtx x = *loc;
13816 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13817 int len = GET_RTX_LENGTH (GET_CODE (x));
13818 int i, j;
13819
13820 if (REG_P (x))
13821 {
13822 unsigned int regno = REGNO (x);
13823 unsigned int endregno = END_REGNO (x);
13824 unsigned int j;
13825
13826 for (j = regno; j < endregno; j++)
13827 {
13828 reg_stat_type *rsp = &reg_stat[j];
13829 if (rsp->last_set_invalid
13830 /* If this is a pseudo-register that was only set once and not
13831 live at the beginning of the function, it is always valid. */
13832 || (! (regno >= FIRST_PSEUDO_REGISTER
13833 && regno < reg_n_sets_max
13834 && REG_N_SETS (regno) == 1
13835 && (!REGNO_REG_SET_P
13836 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13837 regno)))
13838 && rsp->last_set_label > tick))
13839 {
13840 if (replace)
13841 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13842 return replace;
13843 }
13844 }
13845
13846 return true;
13847 }
13848 /* If this is a memory reference, make sure that there were no stores after
13849 it that might have clobbered the value. We don't have alias info, so we
13850 assume any store invalidates it. Moreover, we only have local UIDs, so
13851 we also assume that there were stores in the intervening basic blocks. */
13852 else if (MEM_P (x) && !MEM_READONLY_P (x)
13853 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13854 {
13855 if (replace)
13856 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13857 return replace;
13858 }
13859
13860 for (i = 0; i < len; i++)
13861 {
13862 if (fmt[i] == 'e')
13863 {
13864 /* Check for identical subexpressions. If x contains
13865 identical subexpression we only have to traverse one of
13866 them. */
13867 if (i == 1 && ARITHMETIC_P (x))
13868 {
13869 /* Note that at this point x0 has already been checked
13870 and found valid. */
13871 rtx x0 = XEXP (x, 0);
13872 rtx x1 = XEXP (x, 1);
13873
13874 /* If x0 and x1 are identical then x is also valid. */
13875 if (x0 == x1)
13876 return true;
13877
13878 /* If x1 is identical to a subexpression of x0 then
13879 while checking x0, x1 has already been checked. Thus
13880 it is valid and so as x. */
13881 if (ARITHMETIC_P (x0)
13882 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13883 return true;
13884
13885 /* If x0 is identical to a subexpression of x1 then x is
13886 valid iff the rest of x1 is valid. */
13887 if (ARITHMETIC_P (x1)
13888 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13889 return
13890 get_last_value_validate (&XEXP (x1,
13891 x0 == XEXP (x1, 0) ? 1 : 0),
13892 insn, tick, replace);
13893 }
13894
13895 if (!get_last_value_validate (&XEXP (x, i), insn, tick, replace))
13896 return false;
13897 }
13898 else if (fmt[i] == 'E')
13899 for (j = 0; j < XVECLEN (x, i); j++)
13900 if (!get_last_value_validate (&XVECEXP (x, i, j),
13901 insn, tick, replace))
13902 return false;
13903 }
13904
13905 /* If we haven't found a reason for it to be invalid, it is valid. */
13906 return true;
13907 }
13908
13909 /* Get the last value assigned to X, if known. Some registers
13910 in the value may be replaced with (clobber (const_int 0)) if their value
13911 is known longer known reliably. */
13912
13913 static rtx
13914 get_last_value (const_rtx x)
13915 {
13916 unsigned int regno;
13917 rtx value;
13918 reg_stat_type *rsp;
13919
13920 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13921 then convert it to the desired mode. If this is a paradoxical SUBREG,
13922 we cannot predict what values the "extra" bits might have. */
13923 if (GET_CODE (x) == SUBREG
13924 && subreg_lowpart_p (x)
13925 && !paradoxical_subreg_p (x)
13926 && (value = get_last_value (SUBREG_REG (x))) != 0)
13927 return gen_lowpart (GET_MODE (x), value);
13928
13929 if (!REG_P (x))
13930 return 0;
13931
13932 regno = REGNO (x);
13933 rsp = &reg_stat[regno];
13934 value = rsp->last_set_value;
13935
13936 /* If we don't have a value, or if it isn't for this basic block and
13937 it's either a hard register, set more than once, or it's a live
13938 at the beginning of the function, return 0.
13939
13940 Because if it's not live at the beginning of the function then the reg
13941 is always set before being used (is never used without being set).
13942 And, if it's set only once, and it's always set before use, then all
13943 uses must have the same last value, even if it's not from this basic
13944 block. */
13945
13946 if (value == 0
13947 || (rsp->last_set_label < label_tick_ebb_start
13948 && (regno < FIRST_PSEUDO_REGISTER
13949 || regno >= reg_n_sets_max
13950 || REG_N_SETS (regno) != 1
13951 || REGNO_REG_SET_P
13952 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13953 return 0;
13954
13955 /* If the value was set in a later insn than the ones we are processing,
13956 we can't use it even if the register was only set once. */
13957 if (rsp->last_set_label == label_tick
13958 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13959 return 0;
13960
13961 /* If fewer bits were set than what we are asked for now, we cannot use
13962 the value. */
13963 if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13964 GET_MODE_PRECISION (GET_MODE (x))))
13965 return 0;
13966
13967 /* If the value has all its registers valid, return it. */
13968 if (get_last_value_validate (&value, rsp->last_set,
13969 rsp->last_set_label, false))
13970 return value;
13971
13972 /* Otherwise, make a copy and replace any invalid register with
13973 (clobber (const_int 0)). If that fails for some reason, return 0. */
13974
13975 value = copy_rtx (value);
13976 if (get_last_value_validate (&value, rsp->last_set,
13977 rsp->last_set_label, true))
13978 return value;
13979
13980 return 0;
13981 }
13982
13983 /* Define three variables used for communication between the following
13984 routines. */
13985
13986 static unsigned int reg_dead_regno, reg_dead_endregno;
13987 static int reg_dead_flag;
13988 rtx reg_dead_reg;
13989
13990 /* Function called via note_stores from reg_dead_at_p.
13991
13992 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13993 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13994
13995 static void
13996 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13997 {
13998 unsigned int regno, endregno;
13999
14000 if (!REG_P (dest))
14001 return;
14002
14003 regno = REGNO (dest);
14004 endregno = END_REGNO (dest);
14005 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
14006 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
14007 }
14008
14009 /* Return true if REG is known to be dead at INSN.
14010
14011 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
14012 referencing REG, it is dead. If we hit a SET referencing REG, it is
14013 live. Otherwise, see if it is live or dead at the start of the basic
14014 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
14015 must be assumed to be always live. */
14016
14017 static bool
14018 reg_dead_at_p (rtx reg, rtx_insn *insn)
14019 {
14020 basic_block block;
14021 unsigned int i;
14022
14023 /* Set variables for reg_dead_at_p_1. */
14024 reg_dead_regno = REGNO (reg);
14025 reg_dead_endregno = END_REGNO (reg);
14026 reg_dead_reg = reg;
14027
14028 reg_dead_flag = 0;
14029
14030 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
14031 we allow the machine description to decide whether use-and-clobber
14032 patterns are OK. */
14033 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
14034 {
14035 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
14036 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
14037 return false;
14038 }
14039
14040 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
14041 beginning of basic block. */
14042 block = BLOCK_FOR_INSN (insn);
14043 for (;;)
14044 {
14045 if (INSN_P (insn))
14046 {
14047 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
14048 return true;
14049
14050 note_stores (insn, reg_dead_at_p_1, NULL);
14051 if (reg_dead_flag)
14052 return reg_dead_flag == 1 ? 1 : 0;
14053
14054 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
14055 return true;
14056 }
14057
14058 if (insn == BB_HEAD (block))
14059 break;
14060
14061 insn = PREV_INSN (insn);
14062 }
14063
14064 /* Look at live-in sets for the basic block that we were in. */
14065 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
14066 if (REGNO_REG_SET_P (df_get_live_in (block), i))
14067 return false;
14068
14069 return true;
14070 }
14071
14072 /* Note hard registers in X that are used. */
14073
14074 static void
14075 mark_used_regs_combine (rtx x)
14076 {
14077 RTX_CODE code = GET_CODE (x);
14078 unsigned int regno;
14079 int i;
14080
14081 switch (code)
14082 {
14083 case LABEL_REF:
14084 case SYMBOL_REF:
14085 case CONST:
14086 CASE_CONST_ANY:
14087 case PC:
14088 case ADDR_VEC:
14089 case ADDR_DIFF_VEC:
14090 case ASM_INPUT:
14091 return;
14092
14093 case CLOBBER:
14094 /* If we are clobbering a MEM, mark any hard registers inside the
14095 address as used. */
14096 if (MEM_P (XEXP (x, 0)))
14097 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
14098 return;
14099
14100 case REG:
14101 regno = REGNO (x);
14102 /* A hard reg in a wide mode may really be multiple registers.
14103 If so, mark all of them just like the first. */
14104 if (regno < FIRST_PSEUDO_REGISTER)
14105 {
14106 /* None of this applies to the stack, frame or arg pointers. */
14107 if (regno == STACK_POINTER_REGNUM
14108 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
14109 && regno == HARD_FRAME_POINTER_REGNUM)
14110 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
14111 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
14112 || regno == FRAME_POINTER_REGNUM)
14113 return;
14114
14115 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
14116 }
14117 return;
14118
14119 case SET:
14120 {
14121 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
14122 the address. */
14123 rtx testreg = SET_DEST (x);
14124
14125 while (GET_CODE (testreg) == SUBREG
14126 || GET_CODE (testreg) == ZERO_EXTRACT
14127 || GET_CODE (testreg) == STRICT_LOW_PART)
14128 testreg = XEXP (testreg, 0);
14129
14130 if (MEM_P (testreg))
14131 mark_used_regs_combine (XEXP (testreg, 0));
14132
14133 mark_used_regs_combine (SET_SRC (x));
14134 }
14135 return;
14136
14137 default:
14138 break;
14139 }
14140
14141 /* Recursively scan the operands of this expression. */
14142
14143 {
14144 const char *fmt = GET_RTX_FORMAT (code);
14145
14146 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
14147 {
14148 if (fmt[i] == 'e')
14149 mark_used_regs_combine (XEXP (x, i));
14150 else if (fmt[i] == 'E')
14151 {
14152 int j;
14153
14154 for (j = 0; j < XVECLEN (x, i); j++)
14155 mark_used_regs_combine (XVECEXP (x, i, j));
14156 }
14157 }
14158 }
14159 }
14160
14161 /* Remove register number REGNO from the dead registers list of INSN.
14162
14163 Return the note used to record the death, if there was one. */
14164
14165 rtx
14166 remove_death (unsigned int regno, rtx_insn *insn)
14167 {
14168 rtx note = find_regno_note (insn, REG_DEAD, regno);
14169
14170 if (note)
14171 remove_note (insn, note);
14172
14173 return note;
14174 }
14175
14176 /* For each register (hardware or pseudo) used within expression X, if its
14177 death is in an instruction with luid between FROM_LUID (inclusive) and
14178 TO_INSN (exclusive), put a REG_DEAD note for that register in the
14179 list headed by PNOTES.
14180
14181 That said, don't move registers killed by maybe_kill_insn.
14182
14183 This is done when X is being merged by combination into TO_INSN. These
14184 notes will then be distributed as needed. */
14185
14186 static void
14187 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
14188 rtx *pnotes)
14189 {
14190 const char *fmt;
14191 int len, i;
14192 enum rtx_code code = GET_CODE (x);
14193
14194 if (code == REG)
14195 {
14196 unsigned int regno = REGNO (x);
14197 rtx_insn *where_dead = reg_stat[regno].last_death;
14198
14199 /* If we do not know where the register died, it may still die between
14200 FROM_LUID and TO_INSN. If so, find it. This is PR83304. */
14201 if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
14202 {
14203 rtx_insn *insn = prev_real_nondebug_insn (to_insn);
14204 while (insn
14205 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
14206 && DF_INSN_LUID (insn) >= from_luid)
14207 {
14208 if (dead_or_set_regno_p (insn, regno))
14209 {
14210 if (find_regno_note (insn, REG_DEAD, regno))
14211 where_dead = insn;
14212 break;
14213 }
14214
14215 insn = prev_real_nondebug_insn (insn);
14216 }
14217 }
14218
14219 /* Don't move the register if it gets killed in between from and to. */
14220 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
14221 && ! reg_referenced_p (x, maybe_kill_insn))
14222 return;
14223
14224 if (where_dead
14225 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
14226 && DF_INSN_LUID (where_dead) >= from_luid
14227 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
14228 {
14229 rtx note = remove_death (regno, where_dead);
14230
14231 /* It is possible for the call above to return 0. This can occur
14232 when last_death points to I2 or I1 that we combined with.
14233 In that case make a new note.
14234
14235 We must also check for the case where X is a hard register
14236 and NOTE is a death note for a range of hard registers
14237 including X. In that case, we must put REG_DEAD notes for
14238 the remaining registers in place of NOTE. */
14239
14240 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
14241 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
14242 {
14243 unsigned int deadregno = REGNO (XEXP (note, 0));
14244 unsigned int deadend = END_REGNO (XEXP (note, 0));
14245 unsigned int ourend = END_REGNO (x);
14246 unsigned int i;
14247
14248 for (i = deadregno; i < deadend; i++)
14249 if (i < regno || i >= ourend)
14250 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
14251 }
14252
14253 /* If we didn't find any note, or if we found a REG_DEAD note that
14254 covers only part of the given reg, and we have a multi-reg hard
14255 register, then to be safe we must check for REG_DEAD notes
14256 for each register other than the first. They could have
14257 their own REG_DEAD notes lying around. */
14258 else if ((note == 0
14259 || (note != 0
14260 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
14261 GET_MODE (x))))
14262 && regno < FIRST_PSEUDO_REGISTER
14263 && REG_NREGS (x) > 1)
14264 {
14265 unsigned int ourend = END_REGNO (x);
14266 unsigned int i, offset;
14267 rtx oldnotes = 0;
14268
14269 if (note)
14270 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
14271 else
14272 offset = 1;
14273
14274 for (i = regno + offset; i < ourend; i++)
14275 move_deaths (regno_reg_rtx[i],
14276 maybe_kill_insn, from_luid, to_insn, &oldnotes);
14277 }
14278
14279 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14280 {
14281 XEXP (note, 1) = *pnotes;
14282 *pnotes = note;
14283 }
14284 else
14285 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14286 }
14287
14288 return;
14289 }
14290
14291 else if (GET_CODE (x) == SET)
14292 {
14293 rtx dest = SET_DEST (x);
14294
14295 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14296
14297 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14298 that accesses one word of a multi-word item, some
14299 piece of everything register in the expression is used by
14300 this insn, so remove any old death. */
14301 /* ??? So why do we test for equality of the sizes? */
14302
14303 if (GET_CODE (dest) == ZERO_EXTRACT
14304 || GET_CODE (dest) == STRICT_LOW_PART
14305 || (GET_CODE (dest) == SUBREG
14306 && !read_modify_subreg_p (dest)))
14307 {
14308 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14309 return;
14310 }
14311
14312 /* If this is some other SUBREG, we know it replaces the entire
14313 value, so use that as the destination. */
14314 if (GET_CODE (dest) == SUBREG)
14315 dest = SUBREG_REG (dest);
14316
14317 /* If this is a MEM, adjust deaths of anything used in the address.
14318 For a REG (the only other possibility), the entire value is
14319 being replaced so the old value is not used in this insn. */
14320
14321 if (MEM_P (dest))
14322 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14323 to_insn, pnotes);
14324 return;
14325 }
14326
14327 else if (GET_CODE (x) == CLOBBER)
14328 return;
14329
14330 len = GET_RTX_LENGTH (code);
14331 fmt = GET_RTX_FORMAT (code);
14332
14333 for (i = 0; i < len; i++)
14334 {
14335 if (fmt[i] == 'E')
14336 {
14337 int j;
14338 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14339 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14340 to_insn, pnotes);
14341 }
14342 else if (fmt[i] == 'e')
14343 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14344 }
14345 }
14346
14347 /* Return true if X is the target of a bit-field assignment in BODY, the
14348 pattern of an insn. X must be a REG. */
14349
14350 static bool
14351 reg_bitfield_target_p (rtx x, rtx body)
14352 {
14353 int i;
14354
14355 if (GET_CODE (body) == SET)
14356 {
14357 rtx dest = SET_DEST (body);
14358 rtx target;
14359 unsigned int regno, tregno, endregno, endtregno;
14360
14361 if (GET_CODE (dest) == ZERO_EXTRACT)
14362 target = XEXP (dest, 0);
14363 else if (GET_CODE (dest) == STRICT_LOW_PART)
14364 target = SUBREG_REG (XEXP (dest, 0));
14365 else
14366 return false;
14367
14368 if (GET_CODE (target) == SUBREG)
14369 target = SUBREG_REG (target);
14370
14371 if (!REG_P (target))
14372 return false;
14373
14374 tregno = REGNO (target), regno = REGNO (x);
14375 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14376 return target == x;
14377
14378 endtregno = end_hard_regno (GET_MODE (target), tregno);
14379 endregno = end_hard_regno (GET_MODE (x), regno);
14380
14381 return endregno > tregno && regno < endtregno;
14382 }
14383
14384 else if (GET_CODE (body) == PARALLEL)
14385 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14386 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14387 return true;
14388
14389 return false;
14390 }
14391
14392 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14393 as appropriate. I3 and I2 are the insns resulting from the combination
14394 insns including FROM (I2 may be zero).
14395
14396 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14397 not need REG_DEAD notes because they are being substituted for. This
14398 saves searching in the most common cases.
14399
14400 Each note in the list is either ignored or placed on some insns, depending
14401 on the type of note. */
14402
14403 static void
14404 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14405 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14406 {
14407 rtx note, next_note;
14408 rtx tem_note;
14409 rtx_insn *tem_insn;
14410
14411 for (note = notes; note; note = next_note)
14412 {
14413 rtx_insn *place = 0, *place2 = 0;
14414
14415 next_note = XEXP (note, 1);
14416 switch (REG_NOTE_KIND (note))
14417 {
14418 case REG_BR_PROB:
14419 case REG_BR_PRED:
14420 /* Doesn't matter much where we put this, as long as it's somewhere.
14421 It is preferable to keep these notes on branches, which is most
14422 likely to be i3. */
14423 place = i3;
14424 break;
14425
14426 case REG_NON_LOCAL_GOTO:
14427 if (JUMP_P (i3))
14428 place = i3;
14429 else
14430 {
14431 gcc_assert (i2 && JUMP_P (i2));
14432 place = i2;
14433 }
14434 break;
14435
14436 case REG_EH_REGION:
14437 {
14438 /* The landing pad handling needs to be kept in sync with the
14439 prerequisite checking in try_combine. */
14440 int lp_nr = INTVAL (XEXP (note, 0));
14441 /* A REG_EH_REGION note transfering control can only ever come
14442 from i3. */
14443 if (lp_nr > 0)
14444 gcc_assert (from_insn == i3);
14445 /* We are making sure there is a single effective REG_EH_REGION
14446 note and it's valid to put it on i3. */
14447 if (!insn_could_throw_p (from_insn)
14448 && !(lp_nr == INT_MIN && can_nonlocal_goto (from_insn)))
14449 /* Throw away stray notes on insns that can never throw or
14450 make a nonlocal goto. */
14451 ;
14452 else
14453 {
14454 if (CALL_P (i3))
14455 place = i3;
14456 else
14457 {
14458 gcc_assert (cfun->can_throw_non_call_exceptions);
14459 /* If i3 can still trap preserve the note, otherwise we've
14460 combined things such that we can now prove that the
14461 instructions can't trap. Drop the note in this case. */
14462 if (may_trap_p (i3))
14463 place = i3;
14464 }
14465 }
14466 break;
14467 }
14468
14469 case REG_ARGS_SIZE:
14470 /* ??? How to distribute between i3-i1. Assume i3 contains the
14471 entire adjustment. Assert i3 contains at least some adjust. */
14472 if (!noop_move_p (i3))
14473 {
14474 poly_int64 old_size, args_size = get_args_size (note);
14475 /* fixup_args_size_notes looks at REG_NORETURN note,
14476 so ensure the note is placed there first. */
14477 if (CALL_P (i3))
14478 {
14479 rtx *np;
14480 for (np = &next_note; *np; np = &XEXP (*np, 1))
14481 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14482 {
14483 rtx n = *np;
14484 *np = XEXP (n, 1);
14485 XEXP (n, 1) = REG_NOTES (i3);
14486 REG_NOTES (i3) = n;
14487 break;
14488 }
14489 }
14490 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14491 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14492 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14493 gcc_assert (maybe_ne (old_size, args_size)
14494 || (CALL_P (i3)
14495 && !ACCUMULATE_OUTGOING_ARGS
14496 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14497 }
14498 break;
14499
14500 case REG_NORETURN:
14501 case REG_SETJMP:
14502 case REG_TM:
14503 case REG_CALL_DECL:
14504 case REG_UNTYPED_CALL:
14505 case REG_CALL_NOCF_CHECK:
14506 /* These notes must remain with the call. It should not be
14507 possible for both I2 and I3 to be a call. */
14508 if (CALL_P (i3))
14509 place = i3;
14510 else
14511 {
14512 gcc_assert (i2 && CALL_P (i2));
14513 place = i2;
14514 }
14515 break;
14516
14517 case REG_UNUSED:
14518 /* Any clobbers for i3 may still exist, and so we must process
14519 REG_UNUSED notes from that insn.
14520
14521 Any clobbers from i2 or i1 can only exist if they were added by
14522 recog_for_combine. In that case, recog_for_combine created the
14523 necessary REG_UNUSED notes. Trying to keep any original
14524 REG_UNUSED notes from these insns can cause incorrect output
14525 if it is for the same register as the original i3 dest.
14526 In that case, we will notice that the register is set in i3,
14527 and then add a REG_UNUSED note for the destination of i3, which
14528 is wrong. However, it is possible to have REG_UNUSED notes from
14529 i2 or i1 for register which were both used and clobbered, so
14530 we keep notes from i2 or i1 if they will turn into REG_DEAD
14531 notes. */
14532
14533 /* If this register is set or clobbered between FROM_INSN and I3,
14534 we should not create a note for it. */
14535 if (reg_set_between_p (XEXP (note, 0), from_insn, i3))
14536 break;
14537
14538 /* If this register is set or clobbered in I3, put the note there
14539 unless there is one already. */
14540 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14541 {
14542 if (from_insn != i3)
14543 break;
14544
14545 if (! (REG_P (XEXP (note, 0))
14546 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14547 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14548 place = i3;
14549 }
14550 /* Otherwise, if this register is used by I3, then this register
14551 now dies here, so we must put a REG_DEAD note here unless there
14552 is one already. */
14553 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14554 {
14555 if (! (REG_P (XEXP (note, 0))
14556 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
14557 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14558 {
14559 PUT_REG_NOTE_KIND (note, REG_DEAD);
14560 place = i3;
14561 }
14562 }
14563
14564 /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14565 but we can't tell which at this point. We must reset any
14566 expectations we had about the value that was previously
14567 stored in the reg. ??? Ideally, we'd adjust REG_N_SETS
14568 and, if appropriate, restore its previous value, but we
14569 don't have enough information for that at this point. */
14570 else
14571 {
14572 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14573
14574 /* Otherwise, if this register is now referenced in i2
14575 then the register used to be modified in one of the
14576 original insns. If it was i3 (say, in an unused
14577 parallel), it's now completely gone, so the note can
14578 be discarded. But if it was modified in i2, i1 or i0
14579 and we still reference it in i2, then we're
14580 referencing the previous value, and since the
14581 register was modified and REG_UNUSED, we know that
14582 the previous value is now dead. So, if we only
14583 reference the register in i2, we change the note to
14584 REG_DEAD, to reflect the previous value. However, if
14585 we're also setting or clobbering the register as
14586 scratch, we know (because the register was not
14587 referenced in i3) that it's unused, just as it was
14588 unused before, and we place the note in i2. */
14589 if (from_insn != i3 && i2 && INSN_P (i2)
14590 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14591 {
14592 if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14593 PUT_REG_NOTE_KIND (note, REG_DEAD);
14594 if (! (REG_P (XEXP (note, 0))
14595 ? find_regno_note (i2, REG_NOTE_KIND (note),
14596 REGNO (XEXP (note, 0)))
14597 : find_reg_note (i2, REG_NOTE_KIND (note),
14598 XEXP (note, 0))))
14599 place = i2;
14600 }
14601 }
14602
14603 break;
14604
14605 case REG_EQUAL:
14606 case REG_EQUIV:
14607 case REG_NOALIAS:
14608 /* These notes say something about results of an insn. We can
14609 only support them if they used to be on I3 in which case they
14610 remain on I3. Otherwise they are ignored.
14611
14612 If the note refers to an expression that is not a constant, we
14613 must also ignore the note since we cannot tell whether the
14614 equivalence is still true. It might be possible to do
14615 slightly better than this (we only have a problem if I2DEST
14616 or I1DEST is present in the expression), but it doesn't
14617 seem worth the trouble. */
14618
14619 if (from_insn == i3
14620 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14621 place = i3;
14622 break;
14623
14624 case REG_INC:
14625 /* These notes say something about how a register is used. They must
14626 be present on any use of the register in I2 or I3. */
14627 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14628 place = i3;
14629
14630 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14631 {
14632 if (place)
14633 place2 = i2;
14634 else
14635 place = i2;
14636 }
14637 break;
14638
14639 case REG_LABEL_TARGET:
14640 case REG_LABEL_OPERAND:
14641 /* This can show up in several ways -- either directly in the
14642 pattern, or hidden off in the constant pool with (or without?)
14643 a REG_EQUAL note. */
14644 /* ??? Ignore the without-reg_equal-note problem for now. */
14645 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14646 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14647 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14648 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14649 place = i3;
14650
14651 if (i2
14652 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14653 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14654 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14655 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14656 {
14657 if (place)
14658 place2 = i2;
14659 else
14660 place = i2;
14661 }
14662
14663 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14664 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14665 there. */
14666 if (place && JUMP_P (place)
14667 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14668 && (JUMP_LABEL (place) == NULL
14669 || JUMP_LABEL (place) == XEXP (note, 0)))
14670 {
14671 rtx label = JUMP_LABEL (place);
14672
14673 if (!label)
14674 JUMP_LABEL (place) = XEXP (note, 0);
14675 else if (LABEL_P (label))
14676 LABEL_NUSES (label)--;
14677 }
14678
14679 if (place2 && JUMP_P (place2)
14680 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14681 && (JUMP_LABEL (place2) == NULL
14682 || JUMP_LABEL (place2) == XEXP (note, 0)))
14683 {
14684 rtx label = JUMP_LABEL (place2);
14685
14686 if (!label)
14687 JUMP_LABEL (place2) = XEXP (note, 0);
14688 else if (LABEL_P (label))
14689 LABEL_NUSES (label)--;
14690 place2 = 0;
14691 }
14692 break;
14693
14694 case REG_NONNEG:
14695 /* This note says something about the value of a register prior
14696 to the execution of an insn. It is too much trouble to see
14697 if the note is still correct in all situations. It is better
14698 to simply delete it. */
14699 break;
14700
14701 case REG_DEAD:
14702 /* If we replaced the right hand side of FROM_INSN with a
14703 REG_EQUAL note, the original use of the dying register
14704 will not have been combined into I3 and I2. In such cases,
14705 FROM_INSN is guaranteed to be the first of the combined
14706 instructions, so we simply need to search back before
14707 FROM_INSN for the previous use or set of this register,
14708 then alter the notes there appropriately.
14709
14710 If the register is used as an input in I3, it dies there.
14711 Similarly for I2, if it is nonzero and adjacent to I3.
14712
14713 If the register is not used as an input in either I3 or I2
14714 and it is not one of the registers we were supposed to eliminate,
14715 there are two possibilities. We might have a non-adjacent I2
14716 or we might have somehow eliminated an additional register
14717 from a computation. For example, we might have had A & B where
14718 we discover that B will always be zero. In this case we will
14719 eliminate the reference to A.
14720
14721 In both cases, we must search to see if we can find a previous
14722 use of A and put the death note there. */
14723
14724 if (from_insn
14725 && from_insn == i2mod
14726 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14727 tem_insn = from_insn;
14728 else
14729 {
14730 if (from_insn
14731 && CALL_P (from_insn)
14732 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14733 place = from_insn;
14734 else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14735 {
14736 /* If the new I2 sets the same register that is marked
14737 dead in the note, we do not in general know where to
14738 put the note. One important case we _can_ handle is
14739 when the note comes from I3. */
14740 if (from_insn == i3)
14741 place = i3;
14742 else
14743 break;
14744 }
14745 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14746 place = i3;
14747 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14748 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14749 place = i2;
14750 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14751 && !(i2mod
14752 && reg_overlap_mentioned_p (XEXP (note, 0),
14753 i2mod_old_rhs)))
14754 || rtx_equal_p (XEXP (note, 0), elim_i1)
14755 || rtx_equal_p (XEXP (note, 0), elim_i0))
14756 break;
14757 tem_insn = i3;
14758 }
14759
14760 if (place == 0)
14761 {
14762 basic_block bb = this_basic_block;
14763
14764 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14765 {
14766 if (!NONDEBUG_INSN_P (tem_insn))
14767 {
14768 if (tem_insn == BB_HEAD (bb))
14769 break;
14770 continue;
14771 }
14772
14773 /* If the register is being set at TEM_INSN, see if that is all
14774 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14775 into a REG_UNUSED note instead. Don't delete sets to
14776 global register vars. */
14777 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14778 || !global_regs[REGNO (XEXP (note, 0))])
14779 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14780 {
14781 rtx set = single_set (tem_insn);
14782 rtx inner_dest = 0;
14783
14784 if (set != 0)
14785 for (inner_dest = SET_DEST (set);
14786 (GET_CODE (inner_dest) == STRICT_LOW_PART
14787 || GET_CODE (inner_dest) == SUBREG
14788 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14789 inner_dest = XEXP (inner_dest, 0))
14790 ;
14791
14792 /* Verify that it was the set, and not a clobber that
14793 modified the register.
14794
14795 If we cannot delete the setter due to side
14796 effects, mark the user with an UNUSED note instead
14797 of deleting it. */
14798
14799 if (set != 0 && ! side_effects_p (SET_SRC (set))
14800 && rtx_equal_p (XEXP (note, 0), inner_dest))
14801 {
14802 /* Move the notes and links of TEM_INSN elsewhere.
14803 This might delete other dead insns recursively.
14804 First set the pattern to something that won't use
14805 any register. */
14806 rtx old_notes = REG_NOTES (tem_insn);
14807
14808 PATTERN (tem_insn) = pc_rtx;
14809 REG_NOTES (tem_insn) = NULL;
14810
14811 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14812 NULL_RTX, NULL_RTX, NULL_RTX);
14813 distribute_links (LOG_LINKS (tem_insn));
14814
14815 unsigned int regno = REGNO (XEXP (note, 0));
14816 reg_stat_type *rsp = &reg_stat[regno];
14817 if (rsp->last_set == tem_insn)
14818 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14819
14820 SET_INSN_DELETED (tem_insn);
14821 if (tem_insn == i2)
14822 i2 = NULL;
14823 }
14824 else
14825 {
14826 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14827
14828 /* If there isn't already a REG_UNUSED note, put one
14829 here. Do not place a REG_DEAD note, even if
14830 the register is also used here; that would not
14831 match the algorithm used in lifetime analysis
14832 and can cause the consistency check in the
14833 scheduler to fail. */
14834 if (! find_regno_note (tem_insn, REG_UNUSED,
14835 REGNO (XEXP (note, 0))))
14836 place = tem_insn;
14837 break;
14838 }
14839 }
14840 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14841 || (CALL_P (tem_insn)
14842 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14843 {
14844 place = tem_insn;
14845
14846 /* If we are doing a 3->2 combination, and we have a
14847 register which formerly died in i3 and was not used
14848 by i2, which now no longer dies in i3 and is used in
14849 i2 but does not die in i2, and place is between i2
14850 and i3, then we may need to move a link from place to
14851 i2. */
14852 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14853 && from_insn
14854 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14855 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14856 {
14857 struct insn_link *links = LOG_LINKS (place);
14858 LOG_LINKS (place) = NULL;
14859 distribute_links (links);
14860 }
14861 break;
14862 }
14863
14864 if (tem_insn == BB_HEAD (bb))
14865 break;
14866 }
14867
14868 }
14869
14870 /* If the register is set or already dead at PLACE, we needn't do
14871 anything with this note if it is still a REG_DEAD note.
14872 We check here if it is set at all, not if is it totally replaced,
14873 which is what `dead_or_set_p' checks, so also check for it being
14874 set partially. */
14875
14876 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14877 {
14878 unsigned int regno = REGNO (XEXP (note, 0));
14879 reg_stat_type *rsp = &reg_stat[regno];
14880
14881 if (dead_or_set_p (place, XEXP (note, 0))
14882 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14883 {
14884 /* Unless the register previously died in PLACE, clear
14885 last_death. [I no longer understand why this is
14886 being done.] */
14887 if (rsp->last_death != place)
14888 rsp->last_death = 0;
14889 place = 0;
14890 }
14891 else
14892 rsp->last_death = place;
14893
14894 /* If this is a death note for a hard reg that is occupying
14895 multiple registers, ensure that we are still using all
14896 parts of the object. If we find a piece of the object
14897 that is unused, we must arrange for an appropriate REG_DEAD
14898 note to be added for it. However, we can't just emit a USE
14899 and tag the note to it, since the register might actually
14900 be dead; so we recourse, and the recursive call then finds
14901 the previous insn that used this register. */
14902
14903 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14904 {
14905 unsigned int endregno = END_REGNO (XEXP (note, 0));
14906 bool all_used = true;
14907 unsigned int i;
14908
14909 for (i = regno; i < endregno; i++)
14910 if ((! refers_to_regno_p (i, PATTERN (place))
14911 && ! find_regno_fusage (place, USE, i))
14912 || dead_or_set_regno_p (place, i))
14913 {
14914 all_used = false;
14915 break;
14916 }
14917
14918 if (! all_used)
14919 {
14920 /* Put only REG_DEAD notes for pieces that are
14921 not already dead or set. */
14922
14923 for (i = regno; i < endregno;
14924 i += hard_regno_nregs (i, reg_raw_mode[i]))
14925 {
14926 rtx piece = regno_reg_rtx[i];
14927 basic_block bb = this_basic_block;
14928
14929 if (! dead_or_set_p (place, piece)
14930 && ! reg_bitfield_target_p (piece,
14931 PATTERN (place)))
14932 {
14933 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14934 NULL_RTX);
14935
14936 distribute_notes (new_note, place, place,
14937 NULL, NULL_RTX, NULL_RTX,
14938 NULL_RTX);
14939 }
14940 else if (! refers_to_regno_p (i, PATTERN (place))
14941 && ! find_regno_fusage (place, USE, i))
14942 for (tem_insn = PREV_INSN (place); ;
14943 tem_insn = PREV_INSN (tem_insn))
14944 {
14945 if (!NONDEBUG_INSN_P (tem_insn))
14946 {
14947 if (tem_insn == BB_HEAD (bb))
14948 break;
14949 continue;
14950 }
14951 if (dead_or_set_p (tem_insn, piece)
14952 || reg_bitfield_target_p (piece,
14953 PATTERN (tem_insn)))
14954 {
14955 add_reg_note (tem_insn, REG_UNUSED, piece);
14956 break;
14957 }
14958 }
14959 }
14960
14961 place = 0;
14962 }
14963 }
14964 }
14965 break;
14966
14967 default:
14968 /* Any other notes should not be present at this point in the
14969 compilation. */
14970 gcc_unreachable ();
14971 }
14972
14973 if (place)
14974 {
14975 XEXP (note, 1) = REG_NOTES (place);
14976 REG_NOTES (place) = note;
14977
14978 /* Set added_notes_insn to the earliest insn we added a note to. */
14979 if (added_notes_insn == 0
14980 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14981 added_notes_insn = place;
14982 }
14983
14984 if (place2)
14985 {
14986 add_shallow_copy_of_reg_note (place2, note);
14987
14988 /* Set added_notes_insn to the earliest insn we added a note to. */
14989 if (added_notes_insn == 0
14990 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14991 added_notes_insn = place2;
14992 }
14993 }
14994 }
14995
14996 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14997 I3, I2, and I1 to new locations. This is also called to add a link
14998 pointing at I3 when I3's destination is changed.
14999
15000 If START is nonnull and an insn, we know that the next location for each
15001 link is no earlier than START. LIMIT is the maximum number of nondebug
15002 instructions that can be scanned when looking for the next use of a
15003 definition. */
15004
15005 static void
15006 distribute_links (struct insn_link *links, rtx_insn *start, int limit)
15007 {
15008 struct insn_link *link, *next_link;
15009
15010 for (link = links; link; link = next_link)
15011 {
15012 rtx_insn *place = 0;
15013 rtx_insn *insn;
15014 rtx set, reg;
15015
15016 next_link = link->next;
15017
15018 /* If the insn that this link points to is a NOTE, ignore it. */
15019 if (NOTE_P (link->insn))
15020 continue;
15021
15022 set = 0;
15023 rtx pat = PATTERN (link->insn);
15024 if (GET_CODE (pat) == SET)
15025 set = pat;
15026 else if (GET_CODE (pat) == PARALLEL)
15027 {
15028 int i;
15029 for (i = 0; i < XVECLEN (pat, 0); i++)
15030 {
15031 set = XVECEXP (pat, 0, i);
15032 if (GET_CODE (set) != SET)
15033 continue;
15034
15035 reg = SET_DEST (set);
15036 while (GET_CODE (reg) == ZERO_EXTRACT
15037 || GET_CODE (reg) == STRICT_LOW_PART
15038 || GET_CODE (reg) == SUBREG)
15039 reg = XEXP (reg, 0);
15040
15041 if (!REG_P (reg))
15042 continue;
15043
15044 if (REGNO (reg) == link->regno)
15045 break;
15046 }
15047 if (i == XVECLEN (pat, 0))
15048 continue;
15049 }
15050 else
15051 continue;
15052
15053 reg = SET_DEST (set);
15054
15055 while (GET_CODE (reg) == ZERO_EXTRACT
15056 || GET_CODE (reg) == STRICT_LOW_PART
15057 || GET_CODE (reg) == SUBREG)
15058 reg = XEXP (reg, 0);
15059
15060 if (reg == pc_rtx)
15061 continue;
15062
15063 /* A LOG_LINK is defined as being placed on the first insn that uses
15064 a register and points to the insn that sets the register. Start
15065 searching at the next insn after the target of the link and stop
15066 when we reach a set of the register or the end of the basic block.
15067
15068 Note that this correctly handles the link that used to point from
15069 I3 to I2. Also note that not much searching is typically done here
15070 since most links don't point very far away. */
15071
15072 int count = 0;
15073 insn = start;
15074 if (!insn || NOTE_P (insn))
15075 insn = NEXT_INSN (link->insn);
15076 else
15077 count = link->insn_count;
15078 for (;
15079 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
15080 || BB_HEAD (this_basic_block->next_bb) != insn));
15081 insn = NEXT_INSN (insn))
15082 if (DEBUG_INSN_P (insn))
15083 continue;
15084 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
15085 {
15086 if (reg_referenced_p (reg, PATTERN (insn)))
15087 place = insn;
15088 break;
15089 }
15090 else if (CALL_P (insn)
15091 && find_reg_fusage (insn, USE, reg))
15092 {
15093 place = insn;
15094 break;
15095 }
15096 else if (INSN_P (insn) && reg_set_p (reg, insn))
15097 break;
15098 else if (count >= limit)
15099 break;
15100 else
15101 count += 1;
15102 link->insn_count = count;
15103
15104 /* If we found a place to put the link, place it there unless there
15105 is already a link to the same insn as LINK at that point. */
15106
15107 if (place)
15108 {
15109 struct insn_link *link2;
15110
15111 FOR_EACH_LOG_LINK (link2, place)
15112 if (link2->insn == link->insn && link2->regno == link->regno)
15113 break;
15114
15115 if (link2 == NULL)
15116 {
15117 link->next = LOG_LINKS (place);
15118 LOG_LINKS (place) = link;
15119
15120 /* Set added_links_insn to the earliest insn we added a
15121 link to. */
15122 if (added_links_insn == 0
15123 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
15124 added_links_insn = place;
15125 }
15126 }
15127 }
15128 }
15129
15130 /* Check for any register or memory mentioned in EQUIV that is not
15131 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
15132 of EXPR where some registers may have been replaced by constants. */
15133
15134 static bool
15135 unmentioned_reg_p (rtx equiv, rtx expr)
15136 {
15137 subrtx_iterator::array_type array;
15138 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
15139 {
15140 const_rtx x = *iter;
15141 if ((REG_P (x) || MEM_P (x))
15142 && !reg_mentioned_p (x, expr))
15143 return true;
15144 }
15145 return false;
15146 }
15147
15148 /* Make pseudo-to-pseudo copies after every hard-reg-to-pseudo-copy, because
15149 the reg-to-reg copy can usefully combine with later instructions, but we
15150 do not want to combine the hard reg into later instructions, for that
15151 restricts register allocation. */
15152 static void
15153 make_more_copies (void)
15154 {
15155 basic_block bb;
15156
15157 FOR_EACH_BB_FN (bb, cfun)
15158 {
15159 rtx_insn *insn;
15160
15161 FOR_BB_INSNS (bb, insn)
15162 {
15163 if (!NONDEBUG_INSN_P (insn))
15164 continue;
15165
15166 rtx set = single_set (insn);
15167 if (!set)
15168 continue;
15169
15170 rtx dest = SET_DEST (set);
15171 if (!(REG_P (dest) && !HARD_REGISTER_P (dest)))
15172 continue;
15173
15174 rtx src = SET_SRC (set);
15175 if (!(REG_P (src) && HARD_REGISTER_P (src)))
15176 continue;
15177 if (TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src)))
15178 continue;
15179
15180 rtx new_reg = gen_reg_rtx (GET_MODE (dest));
15181
15182 /* The "original" pseudo copies have important attributes
15183 attached, like pointerness. We want that for these copies
15184 too, for use by insn recognition and later passes. */
15185 set_reg_attrs_from_value (new_reg, dest);
15186
15187 rtx_insn *new_insn = gen_move_insn (new_reg, src);
15188 SET_SRC (set) = new_reg;
15189 emit_insn_before (new_insn, insn);
15190 df_insn_rescan (insn);
15191 }
15192 }
15193 }
15194
15195 /* Try combining insns through substitution. */
15196 static void
15197 rest_of_handle_combine (void)
15198 {
15199 make_more_copies ();
15200
15201 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
15202 df_note_add_problem ();
15203 df_analyze ();
15204
15205 regstat_init_n_sets_and_refs ();
15206 reg_n_sets_max = max_reg_num ();
15207
15208 bool rebuild_jump_labels_after_combine
15209 = combine_instructions (get_insns (), max_reg_num ());
15210
15211 /* Combining insns may have turned an indirect jump into a
15212 direct jump. Rebuild the JUMP_LABEL fields of jumping
15213 instructions. */
15214 if (rebuild_jump_labels_after_combine)
15215 {
15216 if (dom_info_available_p (CDI_DOMINATORS))
15217 free_dominance_info (CDI_DOMINATORS);
15218 timevar_push (TV_JUMP);
15219 rebuild_jump_labels (get_insns ());
15220 cleanup_cfg (0);
15221 timevar_pop (TV_JUMP);
15222 }
15223
15224 regstat_free_n_sets_and_refs ();
15225 }
15226
15227 namespace {
15228
15229 const pass_data pass_data_combine =
15230 {
15231 RTL_PASS, /* type */
15232 "combine", /* name */
15233 OPTGROUP_NONE, /* optinfo_flags */
15234 TV_COMBINE, /* tv_id */
15235 PROP_cfglayout, /* properties_required */
15236 0, /* properties_provided */
15237 0, /* properties_destroyed */
15238 0, /* todo_flags_start */
15239 TODO_df_finish, /* todo_flags_finish */
15240 };
15241
15242 class pass_combine : public rtl_opt_pass
15243 {
15244 public:
15245 pass_combine (gcc::context *ctxt)
15246 : rtl_opt_pass (pass_data_combine, ctxt)
15247 {}
15248
15249 /* opt_pass methods: */
15250 bool gate (function *) final override { return (optimize > 0); }
15251 unsigned int execute (function *) final override
15252 {
15253 rest_of_handle_combine ();
15254 return 0;
15255 }
15256
15257 }; // class pass_combine
15258
15259 } // anon namespace
15260
15261 rtl_opt_pass *
15262 make_pass_combine (gcc::context *ctxt)
15263 {
15264 return new pass_combine (ctxt);
15265 }