PR other/16615 [1/5]
[platform/upstream/gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987-2019 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    LOG_LINKS does not have links for use of the CC0.  They don't
37    need to, because the insn that sets the CC0 is always immediately
38    before the insn that tests it.  So we always regard a branch
39    insn as having a logical link to the preceding insn.  The same is true
40    for an insn explicitly using CC0.
41
42    We check (with modified_between_p) to avoid combining in such a way
43    as to move a computation to a place where its value would be different.
44
45    Combination is done by mathematically substituting the previous
46    insn(s) values for the regs they set into the expressions in
47    the later insns that refer to these regs.  If the result is a valid insn
48    for our target machine, according to the machine description,
49    we install it, delete the earlier insns, and update the data flow
50    information (LOG_LINKS and REG_NOTES) for what we did.
51
52    There are a few exceptions where the dataflow information isn't
53    completely updated (however this is only a local issue since it is
54    regenerated before the next pass that uses it):
55
56    - reg_live_length is not updated
57    - reg_n_refs is not adjusted in the rare case when a register is
58      no longer required in a computation
59    - there are extremely rare cases (see distribute_notes) when a
60      REG_DEAD note is lost
61    - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62      removed because there is no way to know which register it was
63      linking
64
65    To simplify substitution, we combine only when the earlier insn(s)
66    consist of only a single assignment.  To simplify updating afterward,
67    we never combine when a subroutine call appears in the middle.
68
69    Since we do not represent assignments to CC0 explicitly except when that
70    is all an insn does, there is no LOG_LINKS entry in an insn that uses
71    the condition code for the insn that set the condition code.
72    Fortunately, these two insns must be consecutive.
73    Therefore, every JUMP_INSN is taken to have an implicit logical link
74    to the preceding insn.  This is not quite right, since non-jumps can
75    also use the condition code; but in practice such insns would not
76    combine anyway.  */
77
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "backend.h"
82 #include "target.h"
83 #include "rtl.h"
84 #include "tree.h"
85 #include "cfghooks.h"
86 #include "predict.h"
87 #include "df.h"
88 #include "memmodel.h"
89 #include "tm_p.h"
90 #include "optabs.h"
91 #include "regs.h"
92 #include "emit-rtl.h"
93 #include "recog.h"
94 #include "cgraph.h"
95 #include "stor-layout.h"
96 #include "cfgrtl.h"
97 #include "cfgcleanup.h"
98 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
99 #include "explow.h"
100 #include "insn-attr.h"
101 #include "rtlhooks-def.h"
102 #include "expr.h"
103 #include "params.h"
104 #include "tree-pass.h"
105 #include "valtrack.h"
106 #include "rtl-iter.h"
107 #include "print-rtl.h"
108
109 /* Number of attempts to combine instructions in this function.  */
110
111 static int combine_attempts;
112
113 /* Number of attempts that got as far as substitution in this function.  */
114
115 static int combine_merges;
116
117 /* Number of instructions combined with added SETs in this function.  */
118
119 static int combine_extras;
120
121 /* Number of instructions combined in this function.  */
122
123 static int combine_successes;
124
125 /* Totals over entire compilation.  */
126
127 static int total_attempts, total_merges, total_extras, total_successes;
128
129 /* combine_instructions may try to replace the right hand side of the
130    second instruction with the value of an associated REG_EQUAL note
131    before throwing it at try_combine.  That is problematic when there
132    is a REG_DEAD note for a register used in the old right hand side
133    and can cause distribute_notes to do wrong things.  This is the
134    second instruction if it has been so modified, null otherwise.  */
135
136 static rtx_insn *i2mod;
137
138 /* When I2MOD is nonnull, this is a copy of the old right hand side.  */
139
140 static rtx i2mod_old_rhs;
141
142 /* When I2MOD is nonnull, this is a copy of the new right hand side.  */
143
144 static rtx i2mod_new_rhs;
145 \f
146 struct reg_stat_type {
147   /* Record last point of death of (hard or pseudo) register n.  */
148   rtx_insn                      *last_death;
149
150   /* Record last point of modification of (hard or pseudo) register n.  */
151   rtx_insn                      *last_set;
152
153   /* The next group of fields allows the recording of the last value assigned
154      to (hard or pseudo) register n.  We use this information to see if an
155      operation being processed is redundant given a prior operation performed
156      on the register.  For example, an `and' with a constant is redundant if
157      all the zero bits are already known to be turned off.
158
159      We use an approach similar to that used by cse, but change it in the
160      following ways:
161
162      (1) We do not want to reinitialize at each label.
163      (2) It is useful, but not critical, to know the actual value assigned
164          to a register.  Often just its form is helpful.
165
166      Therefore, we maintain the following fields:
167
168      last_set_value             the last value assigned
169      last_set_label             records the value of label_tick when the
170                                 register was assigned
171      last_set_table_tick        records the value of label_tick when a
172                                 value using the register is assigned
173      last_set_invalid           set to nonzero when it is not valid
174                                 to use the value of this register in some
175                                 register's value
176
177      To understand the usage of these tables, it is important to understand
178      the distinction between the value in last_set_value being valid and
179      the register being validly contained in some other expression in the
180      table.
181
182      (The next two parameters are out of date).
183
184      reg_stat[i].last_set_value is valid if it is nonzero, and either
185      reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186
187      Register I may validly appear in any expression returned for the value
188      of another register if reg_n_sets[i] is 1.  It may also appear in the
189      value for register J if reg_stat[j].last_set_invalid is zero, or
190      reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191
192      If an expression is found in the table containing a register which may
193      not validly appear in an expression, the register is replaced by
194      something that won't match, (clobber (const_int 0)).  */
195
196   /* Record last value assigned to (hard or pseudo) register n.  */
197
198   rtx                           last_set_value;
199
200   /* Record the value of label_tick when an expression involving register n
201      is placed in last_set_value.  */
202
203   int                           last_set_table_tick;
204
205   /* Record the value of label_tick when the value for register n is placed in
206      last_set_value.  */
207
208   int                           last_set_label;
209
210   /* These fields are maintained in parallel with last_set_value and are
211      used to store the mode in which the register was last set, the bits
212      that were known to be zero when it was last set, and the number of
213      sign bits copies it was known to have when it was last set.  */
214
215   unsigned HOST_WIDE_INT        last_set_nonzero_bits;
216   char                          last_set_sign_bit_copies;
217   ENUM_BITFIELD(machine_mode)   last_set_mode : 8;
218
219   /* Set nonzero if references to register n in expressions should not be
220      used.  last_set_invalid is set nonzero when this register is being
221      assigned to and last_set_table_tick == label_tick.  */
222
223   char                          last_set_invalid;
224
225   /* Some registers that are set more than once and used in more than one
226      basic block are nevertheless always set in similar ways.  For example,
227      a QImode register may be loaded from memory in two places on a machine
228      where byte loads zero extend.
229
230      We record in the following fields if a register has some leading bits
231      that are always equal to the sign bit, and what we know about the
232      nonzero bits of a register, specifically which bits are known to be
233      zero.
234
235      If an entry is zero, it means that we don't know anything special.  */
236
237   unsigned char                 sign_bit_copies;
238
239   unsigned HOST_WIDE_INT        nonzero_bits;
240
241   /* Record the value of the label_tick when the last truncation
242      happened.  The field truncated_to_mode is only valid if
243      truncation_label == label_tick.  */
244
245   int                           truncation_label;
246
247   /* Record the last truncation seen for this register.  If truncation
248      is not a nop to this mode we might be able to save an explicit
249      truncation if we know that value already contains a truncated
250      value.  */
251
252   ENUM_BITFIELD(machine_mode)   truncated_to_mode : 8;
253 };
254
255
256 static vec<reg_stat_type> reg_stat;
257
258 /* One plus the highest pseudo for which we track REG_N_SETS.
259    regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
260    but during combine_split_insns new pseudos can be created.  As we don't have
261    updated DF information in that case, it is hard to initialize the array
262    after growing.  The combiner only cares about REG_N_SETS (regno) == 1,
263    so instead of growing the arrays, just assume all newly created pseudos
264    during combine might be set multiple times.  */
265
266 static unsigned int reg_n_sets_max;
267
268 /* Record the luid of the last insn that invalidated memory
269    (anything that writes memory, and subroutine calls, but not pushes).  */
270
271 static int mem_last_set;
272
273 /* Record the luid of the last CALL_INSN
274    so we can tell whether a potential combination crosses any calls.  */
275
276 static int last_call_luid;
277
278 /* When `subst' is called, this is the insn that is being modified
279    (by combining in a previous insn).  The PATTERN of this insn
280    is still the old pattern partially modified and it should not be
281    looked at, but this may be used to examine the successors of the insn
282    to judge whether a simplification is valid.  */
283
284 static rtx_insn *subst_insn;
285
286 /* This is the lowest LUID that `subst' is currently dealing with.
287    get_last_value will not return a value if the register was set at or
288    after this LUID.  If not for this mechanism, we could get confused if
289    I2 or I1 in try_combine were an insn that used the old value of a register
290    to obtain a new value.  In that case, we might erroneously get the
291    new value of the register when we wanted the old one.  */
292
293 static int subst_low_luid;
294
295 /* This contains any hard registers that are used in newpat; reg_dead_at_p
296    must consider all these registers to be always live.  */
297
298 static HARD_REG_SET newpat_used_regs;
299
300 /* This is an insn to which a LOG_LINKS entry has been added.  If this
301    insn is the earlier than I2 or I3, combine should rescan starting at
302    that location.  */
303
304 static rtx_insn *added_links_insn;
305
306 /* And similarly, for notes.  */
307
308 static rtx_insn *added_notes_insn;
309
310 /* Basic block in which we are performing combines.  */
311 static basic_block this_basic_block;
312 static bool optimize_this_for_speed_p;
313
314 \f
315 /* Length of the currently allocated uid_insn_cost array.  */
316
317 static int max_uid_known;
318
319 /* The following array records the insn_cost for every insn
320    in the instruction stream.  */
321
322 static int *uid_insn_cost;
323
324 /* The following array records the LOG_LINKS for every insn in the
325    instruction stream as struct insn_link pointers.  */
326
327 struct insn_link {
328   rtx_insn *insn;
329   unsigned int regno;
330   struct insn_link *next;
331 };
332
333 static struct insn_link **uid_log_links;
334
335 static inline int
336 insn_uid_check (const_rtx insn)
337 {
338   int uid = INSN_UID (insn);
339   gcc_checking_assert (uid <= max_uid_known);
340   return uid;
341 }
342
343 #define INSN_COST(INSN)         (uid_insn_cost[insn_uid_check (INSN)])
344 #define LOG_LINKS(INSN)         (uid_log_links[insn_uid_check (INSN)])
345
346 #define FOR_EACH_LOG_LINK(L, INSN)                              \
347   for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
348
349 /* Links for LOG_LINKS are allocated from this obstack.  */
350
351 static struct obstack insn_link_obstack;
352
353 /* Allocate a link.  */
354
355 static inline struct insn_link *
356 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
357 {
358   struct insn_link *l
359     = (struct insn_link *) obstack_alloc (&insn_link_obstack,
360                                           sizeof (struct insn_link));
361   l->insn = insn;
362   l->regno = regno;
363   l->next = next;
364   return l;
365 }
366
367 /* Incremented for each basic block.  */
368
369 static int label_tick;
370
371 /* Reset to label_tick for each extended basic block in scanning order.  */
372
373 static int label_tick_ebb_start;
374
375 /* Mode used to compute significance in reg_stat[].nonzero_bits.  It is the
376    largest integer mode that can fit in HOST_BITS_PER_WIDE_INT.  */
377
378 static scalar_int_mode nonzero_bits_mode;
379
380 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
381    be safely used.  It is zero while computing them and after combine has
382    completed.  This former test prevents propagating values based on
383    previously set values, which can be incorrect if a variable is modified
384    in a loop.  */
385
386 static int nonzero_sign_valid;
387
388 \f
389 /* Record one modification to rtl structure
390    to be undone by storing old_contents into *where.  */
391
392 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
393
394 struct undo
395 {
396   struct undo *next;
397   enum undo_kind kind;
398   union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
399   union { rtx *r; int *i; struct insn_link **l; } where;
400 };
401
402 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
403    num_undo says how many are currently recorded.
404
405    other_insn is nonzero if we have modified some other insn in the process
406    of working on subst_insn.  It must be verified too.  */
407
408 struct undobuf
409 {
410   struct undo *undos;
411   struct undo *frees;
412   rtx_insn *other_insn;
413 };
414
415 static struct undobuf undobuf;
416
417 /* Number of times the pseudo being substituted for
418    was found and replaced.  */
419
420 static int n_occurrences;
421
422 static rtx reg_nonzero_bits_for_combine (const_rtx, scalar_int_mode,
423                                          scalar_int_mode,
424                                          unsigned HOST_WIDE_INT *);
425 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, scalar_int_mode,
426                                                 scalar_int_mode,
427                                                 unsigned int *);
428 static void do_SUBST (rtx *, rtx);
429 static void do_SUBST_INT (int *, int);
430 static void init_reg_last (void);
431 static void setup_incoming_promotions (rtx_insn *);
432 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
433 static int cant_combine_insn_p (rtx_insn *);
434 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
435                           rtx_insn *, rtx_insn *, rtx *, rtx *);
436 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
437 static int contains_muldiv (rtx);
438 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
439                               int *, rtx_insn *);
440 static void undo_all (void);
441 static void undo_commit (void);
442 static rtx *find_split_point (rtx *, rtx_insn *, bool);
443 static rtx subst (rtx, rtx, rtx, int, int, int);
444 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
445 static rtx simplify_if_then_else (rtx);
446 static rtx simplify_set (rtx);
447 static rtx simplify_logical (rtx);
448 static rtx expand_compound_operation (rtx);
449 static const_rtx expand_field_assignment (const_rtx);
450 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
451                             rtx, unsigned HOST_WIDE_INT, int, int, int);
452 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
453                               unsigned HOST_WIDE_INT *);
454 static rtx canon_reg_for_combine (rtx, rtx);
455 static rtx force_int_to_mode (rtx, scalar_int_mode, scalar_int_mode,
456                               scalar_int_mode, unsigned HOST_WIDE_INT, int);
457 static rtx force_to_mode (rtx, machine_mode,
458                           unsigned HOST_WIDE_INT, int);
459 static rtx if_then_else_cond (rtx, rtx *, rtx *);
460 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
461 static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
462 static rtx make_field_assignment (rtx);
463 static rtx apply_distributive_law (rtx);
464 static rtx distribute_and_simplify_rtx (rtx, int);
465 static rtx simplify_and_const_int_1 (scalar_int_mode, rtx,
466                                      unsigned HOST_WIDE_INT);
467 static rtx simplify_and_const_int (rtx, scalar_int_mode, rtx,
468                                    unsigned HOST_WIDE_INT);
469 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
470                             HOST_WIDE_INT, machine_mode, int *);
471 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
472 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
473                                  int);
474 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
475 static rtx gen_lowpart_for_combine (machine_mode, rtx);
476 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
477                                              rtx, rtx *);
478 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
479 static void update_table_tick (rtx);
480 static void record_value_for_reg (rtx, rtx_insn *, rtx);
481 static void check_promoted_subreg (rtx_insn *, rtx);
482 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
483 static void record_dead_and_set_regs (rtx_insn *);
484 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
485 static rtx get_last_value (const_rtx);
486 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
487 static int reg_dead_at_p (rtx, rtx_insn *);
488 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
489 static int reg_bitfield_target_p (rtx, rtx);
490 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
491 static void distribute_links (struct insn_link *);
492 static void mark_used_regs_combine (rtx);
493 static void record_promoted_value (rtx_insn *, rtx);
494 static bool unmentioned_reg_p (rtx, rtx);
495 static void record_truncated_values (rtx *, void *);
496 static bool reg_truncated_to_mode (machine_mode, const_rtx);
497 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
498 \f
499
500 /* It is not safe to use ordinary gen_lowpart in combine.
501    See comments in gen_lowpart_for_combine.  */
502 #undef RTL_HOOKS_GEN_LOWPART
503 #define RTL_HOOKS_GEN_LOWPART              gen_lowpart_for_combine
504
505 /* Our implementation of gen_lowpart never emits a new pseudo.  */
506 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
507 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT      gen_lowpart_for_combine
508
509 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
510 #define RTL_HOOKS_REG_NONZERO_REG_BITS     reg_nonzero_bits_for_combine
511
512 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
513 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES  reg_num_sign_bit_copies_for_combine
514
515 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
516 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE    reg_truncated_to_mode
517
518 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
519
520 \f
521 /* Convenience wrapper for the canonicalize_comparison target hook.
522    Target hooks cannot use enum rtx_code.  */
523 static inline void
524 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
525                                 bool op0_preserve_value)
526 {
527   int code_int = (int)*code;
528   targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
529   *code = (enum rtx_code)code_int;
530 }
531
532 /* Try to split PATTERN found in INSN.  This returns NULL_RTX if
533    PATTERN cannot be split.  Otherwise, it returns an insn sequence.
534    This is a wrapper around split_insns which ensures that the
535    reg_stat vector is made larger if the splitter creates a new
536    register.  */
537
538 static rtx_insn *
539 combine_split_insns (rtx pattern, rtx_insn *insn)
540 {
541   rtx_insn *ret;
542   unsigned int nregs;
543
544   ret = split_insns (pattern, insn);
545   nregs = max_reg_num ();
546   if (nregs > reg_stat.length ())
547     reg_stat.safe_grow_cleared (nregs);
548   return ret;
549 }
550
551 /* This is used by find_single_use to locate an rtx in LOC that
552    contains exactly one use of DEST, which is typically either a REG
553    or CC0.  It returns a pointer to the innermost rtx expression
554    containing DEST.  Appearances of DEST that are being used to
555    totally replace it are not counted.  */
556
557 static rtx *
558 find_single_use_1 (rtx dest, rtx *loc)
559 {
560   rtx x = *loc;
561   enum rtx_code code = GET_CODE (x);
562   rtx *result = NULL;
563   rtx *this_result;
564   int i;
565   const char *fmt;
566
567   switch (code)
568     {
569     case CONST:
570     case LABEL_REF:
571     case SYMBOL_REF:
572     CASE_CONST_ANY:
573     case CLOBBER:
574     case CLOBBER_HIGH:
575       return 0;
576
577     case SET:
578       /* If the destination is anything other than CC0, PC, a REG or a SUBREG
579          of a REG that occupies all of the REG, the insn uses DEST if
580          it is mentioned in the destination or the source.  Otherwise, we
581          need just check the source.  */
582       if (GET_CODE (SET_DEST (x)) != CC0
583           && GET_CODE (SET_DEST (x)) != PC
584           && !REG_P (SET_DEST (x))
585           && ! (GET_CODE (SET_DEST (x)) == SUBREG
586                 && REG_P (SUBREG_REG (SET_DEST (x)))
587                 && !read_modify_subreg_p (SET_DEST (x))))
588         break;
589
590       return find_single_use_1 (dest, &SET_SRC (x));
591
592     case MEM:
593     case SUBREG:
594       return find_single_use_1 (dest, &XEXP (x, 0));
595
596     default:
597       break;
598     }
599
600   /* If it wasn't one of the common cases above, check each expression and
601      vector of this code.  Look for a unique usage of DEST.  */
602
603   fmt = GET_RTX_FORMAT (code);
604   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
605     {
606       if (fmt[i] == 'e')
607         {
608           if (dest == XEXP (x, i)
609               || (REG_P (dest) && REG_P (XEXP (x, i))
610                   && REGNO (dest) == REGNO (XEXP (x, i))))
611             this_result = loc;
612           else
613             this_result = find_single_use_1 (dest, &XEXP (x, i));
614
615           if (result == NULL)
616             result = this_result;
617           else if (this_result)
618             /* Duplicate usage.  */
619             return NULL;
620         }
621       else if (fmt[i] == 'E')
622         {
623           int j;
624
625           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
626             {
627               if (XVECEXP (x, i, j) == dest
628                   || (REG_P (dest)
629                       && REG_P (XVECEXP (x, i, j))
630                       && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
631                 this_result = loc;
632               else
633                 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
634
635               if (result == NULL)
636                 result = this_result;
637               else if (this_result)
638                 return NULL;
639             }
640         }
641     }
642
643   return result;
644 }
645
646
647 /* See if DEST, produced in INSN, is used only a single time in the
648    sequel.  If so, return a pointer to the innermost rtx expression in which
649    it is used.
650
651    If PLOC is nonzero, *PLOC is set to the insn containing the single use.
652
653    If DEST is cc0_rtx, we look only at the next insn.  In that case, we don't
654    care about REG_DEAD notes or LOG_LINKS.
655
656    Otherwise, we find the single use by finding an insn that has a
657    LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST.  If DEST is
658    only referenced once in that insn, we know that it must be the first
659    and last insn referencing DEST.  */
660
661 static rtx *
662 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
663 {
664   basic_block bb;
665   rtx_insn *next;
666   rtx *result;
667   struct insn_link *link;
668
669   if (dest == cc0_rtx)
670     {
671       next = NEXT_INSN (insn);
672       if (next == 0
673           || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
674         return 0;
675
676       result = find_single_use_1 (dest, &PATTERN (next));
677       if (result && ploc)
678         *ploc = next;
679       return result;
680     }
681
682   if (!REG_P (dest))
683     return 0;
684
685   bb = BLOCK_FOR_INSN (insn);
686   for (next = NEXT_INSN (insn);
687        next && BLOCK_FOR_INSN (next) == bb;
688        next = NEXT_INSN (next))
689     if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
690       {
691         FOR_EACH_LOG_LINK (link, next)
692           if (link->insn == insn && link->regno == REGNO (dest))
693             break;
694
695         if (link)
696           {
697             result = find_single_use_1 (dest, &PATTERN (next));
698             if (ploc)
699               *ploc = next;
700             return result;
701           }
702       }
703
704   return 0;
705 }
706 \f
707 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
708    insn.  The substitution can be undone by undo_all.  If INTO is already
709    set to NEWVAL, do not record this change.  Because computing NEWVAL might
710    also call SUBST, we have to compute it before we put anything into
711    the undo table.  */
712
713 static void
714 do_SUBST (rtx *into, rtx newval)
715 {
716   struct undo *buf;
717   rtx oldval = *into;
718
719   if (oldval == newval)
720     return;
721
722   /* We'd like to catch as many invalid transformations here as
723      possible.  Unfortunately, there are way too many mode changes
724      that are perfectly valid, so we'd waste too much effort for
725      little gain doing the checks here.  Focus on catching invalid
726      transformations involving integer constants.  */
727   if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
728       && CONST_INT_P (newval))
729     {
730       /* Sanity check that we're replacing oldval with a CONST_INT
731          that is a valid sign-extension for the original mode.  */
732       gcc_assert (INTVAL (newval)
733                   == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
734
735       /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
736          CONST_INT is not valid, because after the replacement, the
737          original mode would be gone.  Unfortunately, we can't tell
738          when do_SUBST is called to replace the operand thereof, so we
739          perform this test on oldval instead, checking whether an
740          invalid replacement took place before we got here.  */
741       gcc_assert (!(GET_CODE (oldval) == SUBREG
742                     && CONST_INT_P (SUBREG_REG (oldval))));
743       gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
744                     && CONST_INT_P (XEXP (oldval, 0))));
745     }
746
747   if (undobuf.frees)
748     buf = undobuf.frees, undobuf.frees = buf->next;
749   else
750     buf = XNEW (struct undo);
751
752   buf->kind = UNDO_RTX;
753   buf->where.r = into;
754   buf->old_contents.r = oldval;
755   *into = newval;
756
757   buf->next = undobuf.undos, undobuf.undos = buf;
758 }
759
760 #define SUBST(INTO, NEWVAL)     do_SUBST (&(INTO), (NEWVAL))
761
762 /* Similar to SUBST, but NEWVAL is an int expression.  Note that substitution
763    for the value of a HOST_WIDE_INT value (including CONST_INT) is
764    not safe.  */
765
766 static void
767 do_SUBST_INT (int *into, int newval)
768 {
769   struct undo *buf;
770   int oldval = *into;
771
772   if (oldval == newval)
773     return;
774
775   if (undobuf.frees)
776     buf = undobuf.frees, undobuf.frees = buf->next;
777   else
778     buf = XNEW (struct undo);
779
780   buf->kind = UNDO_INT;
781   buf->where.i = into;
782   buf->old_contents.i = oldval;
783   *into = newval;
784
785   buf->next = undobuf.undos, undobuf.undos = buf;
786 }
787
788 #define SUBST_INT(INTO, NEWVAL)  do_SUBST_INT (&(INTO), (NEWVAL))
789
790 /* Similar to SUBST, but just substitute the mode.  This is used when
791    changing the mode of a pseudo-register, so that any other
792    references to the entry in the regno_reg_rtx array will change as
793    well.  */
794
795 static void
796 do_SUBST_MODE (rtx *into, machine_mode newval)
797 {
798   struct undo *buf;
799   machine_mode oldval = GET_MODE (*into);
800
801   if (oldval == newval)
802     return;
803
804   if (undobuf.frees)
805     buf = undobuf.frees, undobuf.frees = buf->next;
806   else
807     buf = XNEW (struct undo);
808
809   buf->kind = UNDO_MODE;
810   buf->where.r = into;
811   buf->old_contents.m = oldval;
812   adjust_reg_mode (*into, newval);
813
814   buf->next = undobuf.undos, undobuf.undos = buf;
815 }
816
817 #define SUBST_MODE(INTO, NEWVAL)  do_SUBST_MODE (&(INTO), (NEWVAL))
818
819 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression.  */
820
821 static void
822 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
823 {
824   struct undo *buf;
825   struct insn_link * oldval = *into;
826
827   if (oldval == newval)
828     return;
829
830   if (undobuf.frees)
831     buf = undobuf.frees, undobuf.frees = buf->next;
832   else
833     buf = XNEW (struct undo);
834
835   buf->kind = UNDO_LINKS;
836   buf->where.l = into;
837   buf->old_contents.l = oldval;
838   *into = newval;
839
840   buf->next = undobuf.undos, undobuf.undos = buf;
841 }
842
843 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
844 \f
845 /* Subroutine of try_combine.  Determine whether the replacement patterns
846    NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_cost
847    than the original sequence I0, I1, I2, I3 and undobuf.other_insn.  Note
848    that I0, I1 and/or NEWI2PAT may be NULL_RTX.  Similarly, NEWOTHERPAT and
849    undobuf.other_insn may also both be NULL_RTX.  Return false if the cost
850    of all the instructions can be estimated and the replacements are more
851    expensive than the original sequence.  */
852
853 static bool
854 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
855                        rtx newpat, rtx newi2pat, rtx newotherpat)
856 {
857   int i0_cost, i1_cost, i2_cost, i3_cost;
858   int new_i2_cost, new_i3_cost;
859   int old_cost, new_cost;
860
861   /* Lookup the original insn_costs.  */
862   i2_cost = INSN_COST (i2);
863   i3_cost = INSN_COST (i3);
864
865   if (i1)
866     {
867       i1_cost = INSN_COST (i1);
868       if (i0)
869         {
870           i0_cost = INSN_COST (i0);
871           old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
872                       ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
873         }
874       else
875         {
876           old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
877                       ? i1_cost + i2_cost + i3_cost : 0);
878           i0_cost = 0;
879         }
880     }
881   else
882     {
883       old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
884       i1_cost = i0_cost = 0;
885     }
886
887   /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
888      correct that.  */
889   if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
890     old_cost -= i1_cost;
891
892
893   /* Calculate the replacement insn_costs.  */
894   rtx tmp = PATTERN (i3);
895   PATTERN (i3) = newpat;
896   int tmpi = INSN_CODE (i3);
897   INSN_CODE (i3) = -1;
898   new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
899   PATTERN (i3) = tmp;
900   INSN_CODE (i3) = tmpi;
901   if (newi2pat)
902     {
903       tmp = PATTERN (i2);
904       PATTERN (i2) = newi2pat;
905       tmpi = INSN_CODE (i2);
906       INSN_CODE (i2) = -1;
907       new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
908       PATTERN (i2) = tmp;
909       INSN_CODE (i2) = tmpi;
910       new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
911                  ? new_i2_cost + new_i3_cost : 0;
912     }
913   else
914     {
915       new_cost = new_i3_cost;
916       new_i2_cost = 0;
917     }
918
919   if (undobuf.other_insn)
920     {
921       int old_other_cost, new_other_cost;
922
923       old_other_cost = INSN_COST (undobuf.other_insn);
924       tmp = PATTERN (undobuf.other_insn);
925       PATTERN (undobuf.other_insn) = newotherpat;
926       tmpi = INSN_CODE (undobuf.other_insn);
927       INSN_CODE (undobuf.other_insn) = -1;
928       new_other_cost = insn_cost (undobuf.other_insn,
929                                   optimize_this_for_speed_p);
930       PATTERN (undobuf.other_insn) = tmp;
931       INSN_CODE (undobuf.other_insn) = tmpi;
932       if (old_other_cost > 0 && new_other_cost > 0)
933         {
934           old_cost += old_other_cost;
935           new_cost += new_other_cost;
936         }
937       else
938         old_cost = 0;
939     }
940
941   /* Disallow this combination if both new_cost and old_cost are greater than
942      zero, and new_cost is greater than old cost.  */
943   int reject = old_cost > 0 && new_cost > old_cost;
944
945   if (dump_file)
946     {
947       fprintf (dump_file, "%s combination of insns ",
948                reject ? "rejecting" : "allowing");
949       if (i0)
950         fprintf (dump_file, "%d, ", INSN_UID (i0));
951       if (i1 && INSN_UID (i1) != INSN_UID (i2))
952         fprintf (dump_file, "%d, ", INSN_UID (i1));
953       fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
954
955       fprintf (dump_file, "original costs ");
956       if (i0)
957         fprintf (dump_file, "%d + ", i0_cost);
958       if (i1 && INSN_UID (i1) != INSN_UID (i2))
959         fprintf (dump_file, "%d + ", i1_cost);
960       fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
961
962       if (newi2pat)
963         fprintf (dump_file, "replacement costs %d + %d = %d\n",
964                  new_i2_cost, new_i3_cost, new_cost);
965       else
966         fprintf (dump_file, "replacement cost %d\n", new_cost);
967     }
968
969   if (reject)
970     return false;
971
972   /* Update the uid_insn_cost array with the replacement costs.  */
973   INSN_COST (i2) = new_i2_cost;
974   INSN_COST (i3) = new_i3_cost;
975   if (i1)
976     {
977       INSN_COST (i1) = 0;
978       if (i0)
979         INSN_COST (i0) = 0;
980     }
981
982   return true;
983 }
984
985
986 /* Delete any insns that copy a register to itself.  */
987
988 static void
989 delete_noop_moves (void)
990 {
991   rtx_insn *insn, *next;
992   basic_block bb;
993
994   FOR_EACH_BB_FN (bb, cfun)
995     {
996       for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
997         {
998           next = NEXT_INSN (insn);
999           if (INSN_P (insn) && noop_move_p (insn))
1000             {
1001               if (dump_file)
1002                 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1003
1004               delete_insn_and_edges (insn);
1005             }
1006         }
1007     }
1008 }
1009
1010 \f
1011 /* Return false if we do not want to (or cannot) combine DEF.  */
1012 static bool
1013 can_combine_def_p (df_ref def)
1014 {
1015   /* Do not consider if it is pre/post modification in MEM.  */
1016   if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1017     return false;
1018
1019   unsigned int regno = DF_REF_REGNO (def);
1020
1021   /* Do not combine frame pointer adjustments.  */
1022   if ((regno == FRAME_POINTER_REGNUM
1023        && (!reload_completed || frame_pointer_needed))
1024       || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1025           && regno == HARD_FRAME_POINTER_REGNUM
1026           && (!reload_completed || frame_pointer_needed))
1027       || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1028           && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1029     return false;
1030
1031   return true;
1032 }
1033
1034 /* Return false if we do not want to (or cannot) combine USE.  */
1035 static bool
1036 can_combine_use_p (df_ref use)
1037 {
1038   /* Do not consider the usage of the stack pointer by function call.  */
1039   if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1040     return false;
1041
1042   return true;
1043 }
1044
1045 /* Fill in log links field for all insns.  */
1046
1047 static void
1048 create_log_links (void)
1049 {
1050   basic_block bb;
1051   rtx_insn **next_use;
1052   rtx_insn *insn;
1053   df_ref def, use;
1054
1055   next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1056
1057   /* Pass through each block from the end, recording the uses of each
1058      register and establishing log links when def is encountered.
1059      Note that we do not clear next_use array in order to save time,
1060      so we have to test whether the use is in the same basic block as def.
1061
1062      There are a few cases below when we do not consider the definition or
1063      usage -- these are taken from original flow.c did. Don't ask me why it is
1064      done this way; I don't know and if it works, I don't want to know.  */
1065
1066   FOR_EACH_BB_FN (bb, cfun)
1067     {
1068       FOR_BB_INSNS_REVERSE (bb, insn)
1069         {
1070           if (!NONDEBUG_INSN_P (insn))
1071             continue;
1072
1073           /* Log links are created only once.  */
1074           gcc_assert (!LOG_LINKS (insn));
1075
1076           FOR_EACH_INSN_DEF (def, insn)
1077             {
1078               unsigned int regno = DF_REF_REGNO (def);
1079               rtx_insn *use_insn;
1080
1081               if (!next_use[regno])
1082                 continue;
1083
1084               if (!can_combine_def_p (def))
1085                 continue;
1086
1087               use_insn = next_use[regno];
1088               next_use[regno] = NULL;
1089
1090               if (BLOCK_FOR_INSN (use_insn) != bb)
1091                 continue;
1092
1093               /* flow.c claimed:
1094
1095                  We don't build a LOG_LINK for hard registers contained
1096                  in ASM_OPERANDs.  If these registers get replaced,
1097                  we might wind up changing the semantics of the insn,
1098                  even if reload can make what appear to be valid
1099                  assignments later.  */
1100               if (regno < FIRST_PSEUDO_REGISTER
1101                   && asm_noperands (PATTERN (use_insn)) >= 0)
1102                 continue;
1103
1104               /* Don't add duplicate links between instructions.  */
1105               struct insn_link *links;
1106               FOR_EACH_LOG_LINK (links, use_insn)
1107                 if (insn == links->insn && regno == links->regno)
1108                   break;
1109
1110               if (!links)
1111                 LOG_LINKS (use_insn)
1112                   = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1113             }
1114
1115           FOR_EACH_INSN_USE (use, insn)
1116             if (can_combine_use_p (use))
1117               next_use[DF_REF_REGNO (use)] = insn;
1118         }
1119     }
1120
1121   free (next_use);
1122 }
1123
1124 /* Walk the LOG_LINKS of insn B to see if we find a reference to A.  Return
1125    true if we found a LOG_LINK that proves that A feeds B.  This only works
1126    if there are no instructions between A and B which could have a link
1127    depending on A, since in that case we would not record a link for B.
1128    We also check the implicit dependency created by a cc0 setter/user
1129    pair.  */
1130
1131 static bool
1132 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1133 {
1134   struct insn_link *links;
1135   FOR_EACH_LOG_LINK (links, b)
1136     if (links->insn == a)
1137       return true;
1138   if (HAVE_cc0 && sets_cc0_p (a))
1139     return true;
1140   return false;
1141 }
1142 \f
1143 /* Main entry point for combiner.  F is the first insn of the function.
1144    NREGS is the first unused pseudo-reg number.
1145
1146    Return nonzero if the combiner has turned an indirect jump
1147    instruction into a direct jump.  */
1148 static int
1149 combine_instructions (rtx_insn *f, unsigned int nregs)
1150 {
1151   rtx_insn *insn, *next;
1152   rtx_insn *prev;
1153   struct insn_link *links, *nextlinks;
1154   rtx_insn *first;
1155   basic_block last_bb;
1156
1157   int new_direct_jump_p = 0;
1158
1159   for (first = f; first && !NONDEBUG_INSN_P (first); )
1160     first = NEXT_INSN (first);
1161   if (!first)
1162     return 0;
1163
1164   combine_attempts = 0;
1165   combine_merges = 0;
1166   combine_extras = 0;
1167   combine_successes = 0;
1168
1169   rtl_hooks = combine_rtl_hooks;
1170
1171   reg_stat.safe_grow_cleared (nregs);
1172
1173   init_recog_no_volatile ();
1174
1175   /* Allocate array for insn info.  */
1176   max_uid_known = get_max_uid ();
1177   uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1178   uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1179   gcc_obstack_init (&insn_link_obstack);
1180
1181   nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1182
1183   /* Don't use reg_stat[].nonzero_bits when computing it.  This can cause
1184      problems when, for example, we have j <<= 1 in a loop.  */
1185
1186   nonzero_sign_valid = 0;
1187   label_tick = label_tick_ebb_start = 1;
1188
1189   /* Scan all SETs and see if we can deduce anything about what
1190      bits are known to be zero for some registers and how many copies
1191      of the sign bit are known to exist for those registers.
1192
1193      Also set any known values so that we can use it while searching
1194      for what bits are known to be set.  */
1195
1196   setup_incoming_promotions (first);
1197   /* Allow the entry block and the first block to fall into the same EBB.
1198      Conceptually the incoming promotions are assigned to the entry block.  */
1199   last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1200
1201   create_log_links ();
1202   FOR_EACH_BB_FN (this_basic_block, cfun)
1203     {
1204       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1205       last_call_luid = 0;
1206       mem_last_set = -1;
1207
1208       label_tick++;
1209       if (!single_pred_p (this_basic_block)
1210           || single_pred (this_basic_block) != last_bb)
1211         label_tick_ebb_start = label_tick;
1212       last_bb = this_basic_block;
1213
1214       FOR_BB_INSNS (this_basic_block, insn)
1215         if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1216           {
1217             rtx links;
1218
1219             subst_low_luid = DF_INSN_LUID (insn);
1220             subst_insn = insn;
1221
1222             note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1223                          insn);
1224             record_dead_and_set_regs (insn);
1225
1226             if (AUTO_INC_DEC)
1227               for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1228                 if (REG_NOTE_KIND (links) == REG_INC)
1229                   set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1230                                                     insn);
1231
1232             /* Record the current insn_cost of this instruction.  */
1233             if (NONJUMP_INSN_P (insn))
1234               INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1235             if (dump_file)
1236               {
1237                 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1238                 dump_insn_slim (dump_file, insn);
1239               }
1240           }
1241     }
1242
1243   nonzero_sign_valid = 1;
1244
1245   /* Now scan all the insns in forward order.  */
1246   label_tick = label_tick_ebb_start = 1;
1247   init_reg_last ();
1248   setup_incoming_promotions (first);
1249   last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1250   int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1251
1252   FOR_EACH_BB_FN (this_basic_block, cfun)
1253     {
1254       rtx_insn *last_combined_insn = NULL;
1255
1256       /* Ignore instruction combination in basic blocks that are going to
1257          be removed as unreachable anyway.  See PR82386.  */
1258       if (EDGE_COUNT (this_basic_block->preds) == 0)
1259         continue;
1260
1261       optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1262       last_call_luid = 0;
1263       mem_last_set = -1;
1264
1265       label_tick++;
1266       if (!single_pred_p (this_basic_block)
1267           || single_pred (this_basic_block) != last_bb)
1268         label_tick_ebb_start = label_tick;
1269       last_bb = this_basic_block;
1270
1271       rtl_profile_for_bb (this_basic_block);
1272       for (insn = BB_HEAD (this_basic_block);
1273            insn != NEXT_INSN (BB_END (this_basic_block));
1274            insn = next ? next : NEXT_INSN (insn))
1275         {
1276           next = 0;
1277           if (!NONDEBUG_INSN_P (insn))
1278             continue;
1279
1280           while (last_combined_insn
1281                  && (!NONDEBUG_INSN_P (last_combined_insn)
1282                      || last_combined_insn->deleted ()))
1283             last_combined_insn = PREV_INSN (last_combined_insn);
1284           if (last_combined_insn == NULL_RTX
1285               || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1286               || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1287             last_combined_insn = insn;
1288
1289           /* See if we know about function return values before this
1290              insn based upon SUBREG flags.  */
1291           check_promoted_subreg (insn, PATTERN (insn));
1292
1293           /* See if we can find hardregs and subreg of pseudos in
1294              narrower modes.  This could help turning TRUNCATEs
1295              into SUBREGs.  */
1296           note_uses (&PATTERN (insn), record_truncated_values, NULL);
1297
1298           /* Try this insn with each insn it links back to.  */
1299
1300           FOR_EACH_LOG_LINK (links, insn)
1301             if ((next = try_combine (insn, links->insn, NULL,
1302                                      NULL, &new_direct_jump_p,
1303                                      last_combined_insn)) != 0)
1304               {
1305                 statistics_counter_event (cfun, "two-insn combine", 1);
1306                 goto retry;
1307               }
1308
1309           /* Try each sequence of three linked insns ending with this one.  */
1310
1311           if (max_combine >= 3)
1312             FOR_EACH_LOG_LINK (links, insn)
1313               {
1314                 rtx_insn *link = links->insn;
1315
1316                 /* If the linked insn has been replaced by a note, then there
1317                    is no point in pursuing this chain any further.  */
1318                 if (NOTE_P (link))
1319                   continue;
1320
1321                 FOR_EACH_LOG_LINK (nextlinks, link)
1322                   if ((next = try_combine (insn, link, nextlinks->insn,
1323                                            NULL, &new_direct_jump_p,
1324                                            last_combined_insn)) != 0)
1325                     {
1326                       statistics_counter_event (cfun, "three-insn combine", 1);
1327                       goto retry;
1328                     }
1329               }
1330
1331           /* Try to combine a jump insn that uses CC0
1332              with a preceding insn that sets CC0, and maybe with its
1333              logical predecessor as well.
1334              This is how we make decrement-and-branch insns.
1335              We need this special code because data flow connections
1336              via CC0 do not get entered in LOG_LINKS.  */
1337
1338           if (HAVE_cc0
1339               && JUMP_P (insn)
1340               && (prev = prev_nonnote_insn (insn)) != 0
1341               && NONJUMP_INSN_P (prev)
1342               && sets_cc0_p (PATTERN (prev)))
1343             {
1344               if ((next = try_combine (insn, prev, NULL, NULL,
1345                                        &new_direct_jump_p,
1346                                        last_combined_insn)) != 0)
1347                 goto retry;
1348
1349               FOR_EACH_LOG_LINK (nextlinks, prev)
1350                   if ((next = try_combine (insn, prev, nextlinks->insn,
1351                                            NULL, &new_direct_jump_p,
1352                                            last_combined_insn)) != 0)
1353                     goto retry;
1354             }
1355
1356           /* Do the same for an insn that explicitly references CC0.  */
1357           if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1358               && (prev = prev_nonnote_insn (insn)) != 0
1359               && NONJUMP_INSN_P (prev)
1360               && sets_cc0_p (PATTERN (prev))
1361               && GET_CODE (PATTERN (insn)) == SET
1362               && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1363             {
1364               if ((next = try_combine (insn, prev, NULL, NULL,
1365                                        &new_direct_jump_p,
1366                                        last_combined_insn)) != 0)
1367                 goto retry;
1368
1369               FOR_EACH_LOG_LINK (nextlinks, prev)
1370                   if ((next = try_combine (insn, prev, nextlinks->insn,
1371                                            NULL, &new_direct_jump_p,
1372                                            last_combined_insn)) != 0)
1373                     goto retry;
1374             }
1375
1376           /* Finally, see if any of the insns that this insn links to
1377              explicitly references CC0.  If so, try this insn, that insn,
1378              and its predecessor if it sets CC0.  */
1379           if (HAVE_cc0)
1380             {
1381               FOR_EACH_LOG_LINK (links, insn)
1382                 if (NONJUMP_INSN_P (links->insn)
1383                     && GET_CODE (PATTERN (links->insn)) == SET
1384                     && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1385                     && (prev = prev_nonnote_insn (links->insn)) != 0
1386                     && NONJUMP_INSN_P (prev)
1387                     && sets_cc0_p (PATTERN (prev))
1388                     && (next = try_combine (insn, links->insn,
1389                                             prev, NULL, &new_direct_jump_p,
1390                                             last_combined_insn)) != 0)
1391                   goto retry;
1392             }
1393
1394           /* Try combining an insn with two different insns whose results it
1395              uses.  */
1396           if (max_combine >= 3)
1397             FOR_EACH_LOG_LINK (links, insn)
1398               for (nextlinks = links->next; nextlinks;
1399                    nextlinks = nextlinks->next)
1400                 if ((next = try_combine (insn, links->insn,
1401                                          nextlinks->insn, NULL,
1402                                          &new_direct_jump_p,
1403                                          last_combined_insn)) != 0)
1404
1405                   {
1406                     statistics_counter_event (cfun, "three-insn combine", 1);
1407                     goto retry;
1408                   }
1409
1410           /* Try four-instruction combinations.  */
1411           if (max_combine >= 4)
1412             FOR_EACH_LOG_LINK (links, insn)
1413               {
1414                 struct insn_link *next1;
1415                 rtx_insn *link = links->insn;
1416
1417                 /* If the linked insn has been replaced by a note, then there
1418                    is no point in pursuing this chain any further.  */
1419                 if (NOTE_P (link))
1420                   continue;
1421
1422                 FOR_EACH_LOG_LINK (next1, link)
1423                   {
1424                     rtx_insn *link1 = next1->insn;
1425                     if (NOTE_P (link1))
1426                       continue;
1427                     /* I0 -> I1 -> I2 -> I3.  */
1428                     FOR_EACH_LOG_LINK (nextlinks, link1)
1429                       if ((next = try_combine (insn, link, link1,
1430                                                nextlinks->insn,
1431                                                &new_direct_jump_p,
1432                                                last_combined_insn)) != 0)
1433                         {
1434                           statistics_counter_event (cfun, "four-insn combine", 1);
1435                           goto retry;
1436                         }
1437                     /* I0, I1 -> I2, I2 -> I3.  */
1438                     for (nextlinks = next1->next; nextlinks;
1439                          nextlinks = nextlinks->next)
1440                       if ((next = try_combine (insn, link, link1,
1441                                                nextlinks->insn,
1442                                                &new_direct_jump_p,
1443                                                last_combined_insn)) != 0)
1444                         {
1445                           statistics_counter_event (cfun, "four-insn combine", 1);
1446                           goto retry;
1447                         }
1448                   }
1449
1450                 for (next1 = links->next; next1; next1 = next1->next)
1451                   {
1452                     rtx_insn *link1 = next1->insn;
1453                     if (NOTE_P (link1))
1454                       continue;
1455                     /* I0 -> I2; I1, I2 -> I3.  */
1456                     FOR_EACH_LOG_LINK (nextlinks, link)
1457                       if ((next = try_combine (insn, link, link1,
1458                                                nextlinks->insn,
1459                                                &new_direct_jump_p,
1460                                                last_combined_insn)) != 0)
1461                         {
1462                           statistics_counter_event (cfun, "four-insn combine", 1);
1463                           goto retry;
1464                         }
1465                     /* I0 -> I1; I1, I2 -> I3.  */
1466                     FOR_EACH_LOG_LINK (nextlinks, link1)
1467                       if ((next = try_combine (insn, link, link1,
1468                                                nextlinks->insn,
1469                                                &new_direct_jump_p,
1470                                                last_combined_insn)) != 0)
1471                         {
1472                           statistics_counter_event (cfun, "four-insn combine", 1);
1473                           goto retry;
1474                         }
1475                   }
1476               }
1477
1478           /* Try this insn with each REG_EQUAL note it links back to.  */
1479           FOR_EACH_LOG_LINK (links, insn)
1480             {
1481               rtx set, note;
1482               rtx_insn *temp = links->insn;
1483               if ((set = single_set (temp)) != 0
1484                   && (note = find_reg_equal_equiv_note (temp)) != 0
1485                   && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1486                   /* Avoid using a register that may already been marked
1487                      dead by an earlier instruction.  */
1488                   && ! unmentioned_reg_p (note, SET_SRC (set))
1489                   && (GET_MODE (note) == VOIDmode
1490                       ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1491                       : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1492                          && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1493                              || (GET_MODE (XEXP (SET_DEST (set), 0))
1494                                  == GET_MODE (note))))))
1495                 {
1496                   /* Temporarily replace the set's source with the
1497                      contents of the REG_EQUAL note.  The insn will
1498                      be deleted or recognized by try_combine.  */
1499                   rtx orig_src = SET_SRC (set);
1500                   rtx orig_dest = SET_DEST (set);
1501                   if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1502                     SET_DEST (set) = XEXP (SET_DEST (set), 0);
1503                   SET_SRC (set) = note;
1504                   i2mod = temp;
1505                   i2mod_old_rhs = copy_rtx (orig_src);
1506                   i2mod_new_rhs = copy_rtx (note);
1507                   next = try_combine (insn, i2mod, NULL, NULL,
1508                                       &new_direct_jump_p,
1509                                       last_combined_insn);
1510                   i2mod = NULL;
1511                   if (next)
1512                     {
1513                       statistics_counter_event (cfun, "insn-with-note combine", 1);
1514                       goto retry;
1515                     }
1516                   SET_SRC (set) = orig_src;
1517                   SET_DEST (set) = orig_dest;
1518                 }
1519             }
1520
1521           if (!NOTE_P (insn))
1522             record_dead_and_set_regs (insn);
1523
1524 retry:
1525           ;
1526         }
1527     }
1528
1529   default_rtl_profile ();
1530   clear_bb_flags ();
1531   new_direct_jump_p |= purge_all_dead_edges ();
1532   delete_noop_moves ();
1533
1534   /* Clean up.  */
1535   obstack_free (&insn_link_obstack, NULL);
1536   free (uid_log_links);
1537   free (uid_insn_cost);
1538   reg_stat.release ();
1539
1540   {
1541     struct undo *undo, *next;
1542     for (undo = undobuf.frees; undo; undo = next)
1543       {
1544         next = undo->next;
1545         free (undo);
1546       }
1547     undobuf.frees = 0;
1548   }
1549
1550   total_attempts += combine_attempts;
1551   total_merges += combine_merges;
1552   total_extras += combine_extras;
1553   total_successes += combine_successes;
1554
1555   nonzero_sign_valid = 0;
1556   rtl_hooks = general_rtl_hooks;
1557
1558   /* Make recognizer allow volatile MEMs again.  */
1559   init_recog ();
1560
1561   return new_direct_jump_p;
1562 }
1563
1564 /* Wipe the last_xxx fields of reg_stat in preparation for another pass.  */
1565
1566 static void
1567 init_reg_last (void)
1568 {
1569   unsigned int i;
1570   reg_stat_type *p;
1571
1572   FOR_EACH_VEC_ELT (reg_stat, i, p)
1573     memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1574 }
1575 \f
1576 /* Set up any promoted values for incoming argument registers.  */
1577
1578 static void
1579 setup_incoming_promotions (rtx_insn *first)
1580 {
1581   tree arg;
1582   bool strictly_local = false;
1583
1584   for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1585        arg = DECL_CHAIN (arg))
1586     {
1587       rtx x, reg = DECL_INCOMING_RTL (arg);
1588       int uns1, uns3;
1589       machine_mode mode1, mode2, mode3, mode4;
1590
1591       /* Only continue if the incoming argument is in a register.  */
1592       if (!REG_P (reg))
1593         continue;
1594
1595       /* Determine, if possible, whether all call sites of the current
1596          function lie within the current compilation unit.  (This does
1597          take into account the exporting of a function via taking its
1598          address, and so forth.)  */
1599       strictly_local = cgraph_node::local_info (current_function_decl)->local;
1600
1601       /* The mode and signedness of the argument before any promotions happen
1602          (equal to the mode of the pseudo holding it at that stage).  */
1603       mode1 = TYPE_MODE (TREE_TYPE (arg));
1604       uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1605
1606       /* The mode and signedness of the argument after any source language and
1607          TARGET_PROMOTE_PROTOTYPES-driven promotions.  */
1608       mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1609       uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1610
1611       /* The mode and signedness of the argument as it is actually passed,
1612          see assign_parm_setup_reg in function.c.  */
1613       mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1614                                      TREE_TYPE (cfun->decl), 0);
1615
1616       /* The mode of the register in which the argument is being passed.  */
1617       mode4 = GET_MODE (reg);
1618
1619       /* Eliminate sign extensions in the callee when:
1620          (a) A mode promotion has occurred;  */
1621       if (mode1 == mode3)
1622         continue;
1623       /* (b) The mode of the register is the same as the mode of
1624              the argument as it is passed; */
1625       if (mode3 != mode4)
1626         continue;
1627       /* (c) There's no language level extension;  */
1628       if (mode1 == mode2)
1629         ;
1630       /* (c.1) All callers are from the current compilation unit.  If that's
1631          the case we don't have to rely on an ABI, we only have to know
1632          what we're generating right now, and we know that we will do the
1633          mode1 to mode2 promotion with the given sign.  */
1634       else if (!strictly_local)
1635         continue;
1636       /* (c.2) The combination of the two promotions is useful.  This is
1637          true when the signs match, or if the first promotion is unsigned.
1638          In the later case, (sign_extend (zero_extend x)) is the same as
1639          (zero_extend (zero_extend x)), so make sure to force UNS3 true.  */
1640       else if (uns1)
1641         uns3 = true;
1642       else if (uns3)
1643         continue;
1644
1645       /* Record that the value was promoted from mode1 to mode3,
1646          so that any sign extension at the head of the current
1647          function may be eliminated.  */
1648       x = gen_rtx_CLOBBER (mode1, const0_rtx);
1649       x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1650       record_value_for_reg (reg, first, x);
1651     }
1652 }
1653
1654 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1655    that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1656    because some machines (maybe most) will actually do the sign-extension and
1657    this is the conservative approach.
1658
1659    ??? For 2.5, try to tighten up the MD files in this regard instead of this
1660    kludge.  */
1661
1662 static rtx
1663 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1664 {
1665   scalar_int_mode int_mode;
1666   if (CONST_INT_P (src)
1667       && is_a <scalar_int_mode> (mode, &int_mode)
1668       && GET_MODE_PRECISION (int_mode) < prec
1669       && INTVAL (src) > 0
1670       && val_signbit_known_set_p (int_mode, INTVAL (src)))
1671     src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1672
1673   return src;
1674 }
1675
1676 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1677    and SET.  */
1678
1679 static void
1680 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1681                            rtx x)
1682 {
1683   rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1684   unsigned HOST_WIDE_INT bits = 0;
1685   rtx reg_equal = NULL, src = SET_SRC (set);
1686   unsigned int num = 0;
1687
1688   if (reg_equal_note)
1689     reg_equal = XEXP (reg_equal_note, 0);
1690
1691   if (SHORT_IMMEDIATES_SIGN_EXTEND)
1692     {
1693       src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1694       if (reg_equal)
1695         reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1696     }
1697
1698   /* Don't call nonzero_bits if it cannot change anything.  */
1699   if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1700     {
1701       machine_mode mode = GET_MODE (x);
1702       if (GET_MODE_CLASS (mode) == MODE_INT
1703           && HWI_COMPUTABLE_MODE_P (mode))
1704         mode = nonzero_bits_mode;
1705       bits = nonzero_bits (src, mode);
1706       if (reg_equal && bits)
1707         bits &= nonzero_bits (reg_equal, mode);
1708       rsp->nonzero_bits |= bits;
1709     }
1710
1711   /* Don't call num_sign_bit_copies if it cannot change anything.  */
1712   if (rsp->sign_bit_copies != 1)
1713     {
1714       num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1715       if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1716         {
1717           unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1718           if (num == 0 || numeq > num)
1719             num = numeq;
1720         }
1721       if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1722         rsp->sign_bit_copies = num;
1723     }
1724 }
1725
1726 /* Called via note_stores.  If X is a pseudo that is narrower than
1727    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1728
1729    If we are setting only a portion of X and we can't figure out what
1730    portion, assume all bits will be used since we don't know what will
1731    be happening.
1732
1733    Similarly, set how many bits of X are known to be copies of the sign bit
1734    at all locations in the function.  This is the smallest number implied
1735    by any set of X.  */
1736
1737 static void
1738 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1739 {
1740   rtx_insn *insn = (rtx_insn *) data;
1741   scalar_int_mode mode;
1742
1743   if (REG_P (x)
1744       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1745       /* If this register is undefined at the start of the file, we can't
1746          say what its contents were.  */
1747       && ! REGNO_REG_SET_P
1748            (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1749       && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1750       && HWI_COMPUTABLE_MODE_P (mode))
1751     {
1752       reg_stat_type *rsp = &reg_stat[REGNO (x)];
1753
1754       if (set == 0 || GET_CODE (set) == CLOBBER)
1755         {
1756           rsp->nonzero_bits = GET_MODE_MASK (mode);
1757           rsp->sign_bit_copies = 1;
1758           return;
1759         }
1760
1761       /* Should not happen as we only using pseduo registers.  */
1762       gcc_assert (GET_CODE (set) != CLOBBER_HIGH);
1763
1764       /* If this register is being initialized using itself, and the
1765          register is uninitialized in this basic block, and there are
1766          no LOG_LINKS which set the register, then part of the
1767          register is uninitialized.  In that case we can't assume
1768          anything about the number of nonzero bits.
1769
1770          ??? We could do better if we checked this in
1771          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1772          could avoid making assumptions about the insn which initially
1773          sets the register, while still using the information in other
1774          insns.  We would have to be careful to check every insn
1775          involved in the combination.  */
1776
1777       if (insn
1778           && reg_referenced_p (x, PATTERN (insn))
1779           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1780                                REGNO (x)))
1781         {
1782           struct insn_link *link;
1783
1784           FOR_EACH_LOG_LINK (link, insn)
1785             if (dead_or_set_p (link->insn, x))
1786               break;
1787           if (!link)
1788             {
1789               rsp->nonzero_bits = GET_MODE_MASK (mode);
1790               rsp->sign_bit_copies = 1;
1791               return;
1792             }
1793         }
1794
1795       /* If this is a complex assignment, see if we can convert it into a
1796          simple assignment.  */
1797       set = expand_field_assignment (set);
1798
1799       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1800          set what we know about X.  */
1801
1802       if (SET_DEST (set) == x
1803           || (paradoxical_subreg_p (SET_DEST (set))
1804               && SUBREG_REG (SET_DEST (set)) == x))
1805         update_rsp_from_reg_equal (rsp, insn, set, x);
1806       else
1807         {
1808           rsp->nonzero_bits = GET_MODE_MASK (mode);
1809           rsp->sign_bit_copies = 1;
1810         }
1811     }
1812 }
1813 \f
1814 /* See if INSN can be combined into I3.  PRED, PRED2, SUCC and SUCC2 are
1815    optionally insns that were previously combined into I3 or that will be
1816    combined into the merger of INSN and I3.  The order is PRED, PRED2,
1817    INSN, SUCC, SUCC2, I3.
1818
1819    Return 0 if the combination is not allowed for any reason.
1820
1821    If the combination is allowed, *PDEST will be set to the single
1822    destination of INSN and *PSRC to the single source, and this function
1823    will return 1.  */
1824
1825 static int
1826 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1827                rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1828                rtx *pdest, rtx *psrc)
1829 {
1830   int i;
1831   const_rtx set = 0;
1832   rtx src, dest;
1833   rtx_insn *p;
1834   rtx link;
1835   bool all_adjacent = true;
1836   int (*is_volatile_p) (const_rtx);
1837
1838   if (succ)
1839     {
1840       if (succ2)
1841         {
1842           if (next_active_insn (succ2) != i3)
1843             all_adjacent = false;
1844           if (next_active_insn (succ) != succ2)
1845             all_adjacent = false;
1846         }
1847       else if (next_active_insn (succ) != i3)
1848         all_adjacent = false;
1849       if (next_active_insn (insn) != succ)
1850         all_adjacent = false;
1851     }
1852   else if (next_active_insn (insn) != i3)
1853     all_adjacent = false;
1854     
1855   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1856      or a PARALLEL consisting of such a SET and CLOBBERs.
1857
1858      If INSN has CLOBBER parallel parts, ignore them for our processing.
1859      By definition, these happen during the execution of the insn.  When it
1860      is merged with another insn, all bets are off.  If they are, in fact,
1861      needed and aren't also supplied in I3, they may be added by
1862      recog_for_combine.  Otherwise, it won't match.
1863
1864      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1865      note.
1866
1867      Get the source and destination of INSN.  If more than one, can't
1868      combine.  */
1869
1870   if (GET_CODE (PATTERN (insn)) == SET)
1871     set = PATTERN (insn);
1872   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1873            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1874     {
1875       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1876         {
1877           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1878
1879           switch (GET_CODE (elt))
1880             {
1881             /* This is important to combine floating point insns
1882                for the SH4 port.  */
1883             case USE:
1884               /* Combining an isolated USE doesn't make sense.
1885                  We depend here on combinable_i3pat to reject them.  */
1886               /* The code below this loop only verifies that the inputs of
1887                  the SET in INSN do not change.  We call reg_set_between_p
1888                  to verify that the REG in the USE does not change between
1889                  I3 and INSN.
1890                  If the USE in INSN was for a pseudo register, the matching
1891                  insn pattern will likely match any register; combining this
1892                  with any other USE would only be safe if we knew that the
1893                  used registers have identical values, or if there was
1894                  something to tell them apart, e.g. different modes.  For
1895                  now, we forgo such complicated tests and simply disallow
1896                  combining of USES of pseudo registers with any other USE.  */
1897               if (REG_P (XEXP (elt, 0))
1898                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1899                 {
1900                   rtx i3pat = PATTERN (i3);
1901                   int i = XVECLEN (i3pat, 0) - 1;
1902                   unsigned int regno = REGNO (XEXP (elt, 0));
1903
1904                   do
1905                     {
1906                       rtx i3elt = XVECEXP (i3pat, 0, i);
1907
1908                       if (GET_CODE (i3elt) == USE
1909                           && REG_P (XEXP (i3elt, 0))
1910                           && (REGNO (XEXP (i3elt, 0)) == regno
1911                               ? reg_set_between_p (XEXP (elt, 0),
1912                                                    PREV_INSN (insn), i3)
1913                               : regno >= FIRST_PSEUDO_REGISTER))
1914                         return 0;
1915                     }
1916                   while (--i >= 0);
1917                 }
1918               break;
1919
1920               /* We can ignore CLOBBERs.  */
1921             case CLOBBER:
1922             case CLOBBER_HIGH:
1923               break;
1924
1925             case SET:
1926               /* Ignore SETs whose result isn't used but not those that
1927                  have side-effects.  */
1928               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1929                   && insn_nothrow_p (insn)
1930                   && !side_effects_p (elt))
1931                 break;
1932
1933               /* If we have already found a SET, this is a second one and
1934                  so we cannot combine with this insn.  */
1935               if (set)
1936                 return 0;
1937
1938               set = elt;
1939               break;
1940
1941             default:
1942               /* Anything else means we can't combine.  */
1943               return 0;
1944             }
1945         }
1946
1947       if (set == 0
1948           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1949              so don't do anything with it.  */
1950           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1951         return 0;
1952     }
1953   else
1954     return 0;
1955
1956   if (set == 0)
1957     return 0;
1958
1959   /* The simplification in expand_field_assignment may call back to
1960      get_last_value, so set safe guard here.  */
1961   subst_low_luid = DF_INSN_LUID (insn);
1962
1963   set = expand_field_assignment (set);
1964   src = SET_SRC (set), dest = SET_DEST (set);
1965
1966   /* Do not eliminate user-specified register if it is in an
1967      asm input because we may break the register asm usage defined
1968      in GCC manual if allow to do so.
1969      Be aware that this may cover more cases than we expect but this
1970      should be harmless.  */
1971   if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1972       && extract_asm_operands (PATTERN (i3)))
1973     return 0;
1974
1975   /* Don't eliminate a store in the stack pointer.  */
1976   if (dest == stack_pointer_rtx
1977       /* Don't combine with an insn that sets a register to itself if it has
1978          a REG_EQUAL note.  This may be part of a LIBCALL sequence.  */
1979       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1980       /* Can't merge an ASM_OPERANDS.  */
1981       || GET_CODE (src) == ASM_OPERANDS
1982       /* Can't merge a function call.  */
1983       || GET_CODE (src) == CALL
1984       /* Don't eliminate a function call argument.  */
1985       || (CALL_P (i3)
1986           && (find_reg_fusage (i3, USE, dest)
1987               || (REG_P (dest)
1988                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1989                   && global_regs[REGNO (dest)])))
1990       /* Don't substitute into an incremented register.  */
1991       || FIND_REG_INC_NOTE (i3, dest)
1992       || (succ && FIND_REG_INC_NOTE (succ, dest))
1993       || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1994       /* Don't substitute into a non-local goto, this confuses CFG.  */
1995       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1996       /* Make sure that DEST is not used after INSN but before SUCC, or
1997          after SUCC and before SUCC2, or after SUCC2 but before I3.  */
1998       || (!all_adjacent
1999           && ((succ2
2000                && (reg_used_between_p (dest, succ2, i3)
2001                    || reg_used_between_p (dest, succ, succ2)))
2002               || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
2003               || (!succ2 && !succ && reg_used_between_p (dest, insn, i3))
2004               || (succ
2005                   /* SUCC and SUCC2 can be split halves from a PARALLEL; in
2006                      that case SUCC is not in the insn stream, so use SUCC2
2007                      instead for this test.  */
2008                   && reg_used_between_p (dest, insn,
2009                                          succ2
2010                                          && INSN_UID (succ) == INSN_UID (succ2)
2011                                          ? succ2 : succ))))
2012       /* Make sure that the value that is to be substituted for the register
2013          does not use any registers whose values alter in between.  However,
2014          If the insns are adjacent, a use can't cross a set even though we
2015          think it might (this can happen for a sequence of insns each setting
2016          the same destination; last_set of that register might point to
2017          a NOTE).  If INSN has a REG_EQUIV note, the register is always
2018          equivalent to the memory so the substitution is valid even if there
2019          are intervening stores.  Also, don't move a volatile asm or
2020          UNSPEC_VOLATILE across any other insns.  */
2021       || (! all_adjacent
2022           && (((!MEM_P (src)
2023                 || ! find_reg_note (insn, REG_EQUIV, src))
2024                && modified_between_p (src, insn, i3))
2025               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
2026               || GET_CODE (src) == UNSPEC_VOLATILE))
2027       /* Don't combine across a CALL_INSN, because that would possibly
2028          change whether the life span of some REGs crosses calls or not,
2029          and it is a pain to update that information.
2030          Exception: if source is a constant, moving it later can't hurt.
2031          Accept that as a special case.  */
2032       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
2033     return 0;
2034
2035   /* DEST must either be a REG or CC0.  */
2036   if (REG_P (dest))
2037     {
2038       /* If register alignment is being enforced for multi-word items in all
2039          cases except for parameters, it is possible to have a register copy
2040          insn referencing a hard register that is not allowed to contain the
2041          mode being copied and which would not be valid as an operand of most
2042          insns.  Eliminate this problem by not combining with such an insn.
2043
2044          Also, on some machines we don't want to extend the life of a hard
2045          register.  */
2046
2047       if (REG_P (src)
2048           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2049                && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
2050               /* Don't extend the life of a hard register unless it is
2051                  user variable (if we have few registers) or it can't
2052                  fit into the desired register (meaning something special
2053                  is going on).
2054                  Also avoid substituting a return register into I3, because
2055                  reload can't handle a conflict with constraints of other
2056                  inputs.  */
2057               || (REGNO (src) < FIRST_PSEUDO_REGISTER
2058                   && !targetm.hard_regno_mode_ok (REGNO (src),
2059                                                   GET_MODE (src)))))
2060         return 0;
2061     }
2062   else if (GET_CODE (dest) != CC0)
2063     return 0;
2064
2065
2066   if (GET_CODE (PATTERN (i3)) == PARALLEL)
2067     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2068       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2069         {
2070           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2071
2072           /* If the clobber represents an earlyclobber operand, we must not
2073              substitute an expression containing the clobbered register.
2074              As we do not analyze the constraint strings here, we have to
2075              make the conservative assumption.  However, if the register is
2076              a fixed hard reg, the clobber cannot represent any operand;
2077              we leave it up to the machine description to either accept or
2078              reject use-and-clobber patterns.  */
2079           if (!REG_P (reg)
2080               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2081               || !fixed_regs[REGNO (reg)])
2082             if (reg_overlap_mentioned_p (reg, src))
2083               return 0;
2084         }
2085
2086   /* If INSN contains anything volatile, or is an `asm' (whether volatile
2087      or not), reject, unless nothing volatile comes between it and I3 */
2088
2089   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2090     {
2091       /* Make sure neither succ nor succ2 contains a volatile reference.  */
2092       if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2093         return 0;
2094       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2095         return 0;
2096       /* We'll check insns between INSN and I3 below.  */
2097     }
2098
2099   /* If INSN is an asm, and DEST is a hard register, reject, since it has
2100      to be an explicit register variable, and was chosen for a reason.  */
2101
2102   if (GET_CODE (src) == ASM_OPERANDS
2103       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2104     return 0;
2105
2106   /* If INSN contains volatile references (specifically volatile MEMs),
2107      we cannot combine across any other volatile references.
2108      Even if INSN doesn't contain volatile references, any intervening
2109      volatile insn might affect machine state.  */
2110
2111   is_volatile_p = volatile_refs_p (PATTERN (insn))
2112     ? volatile_refs_p
2113     : volatile_insn_p;
2114     
2115   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2116     if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2117       return 0;
2118
2119   /* If INSN contains an autoincrement or autodecrement, make sure that
2120      register is not used between there and I3, and not already used in
2121      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
2122      Also insist that I3 not be a jump; if it were one
2123      and the incremented register were spilled, we would lose.  */
2124
2125   if (AUTO_INC_DEC)
2126     for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2127       if (REG_NOTE_KIND (link) == REG_INC
2128           && (JUMP_P (i3)
2129               || reg_used_between_p (XEXP (link, 0), insn, i3)
2130               || (pred != NULL_RTX
2131                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2132               || (pred2 != NULL_RTX
2133                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2134               || (succ != NULL_RTX
2135                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2136               || (succ2 != NULL_RTX
2137                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2138               || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2139         return 0;
2140
2141   /* Don't combine an insn that follows a CC0-setting insn.
2142      An insn that uses CC0 must not be separated from the one that sets it.
2143      We do, however, allow I2 to follow a CC0-setting insn if that insn
2144      is passed as I1; in that case it will be deleted also.
2145      We also allow combining in this case if all the insns are adjacent
2146      because that would leave the two CC0 insns adjacent as well.
2147      It would be more logical to test whether CC0 occurs inside I1 or I2,
2148      but that would be much slower, and this ought to be equivalent.  */
2149
2150   if (HAVE_cc0)
2151     {
2152       p = prev_nonnote_insn (insn);
2153       if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2154           && ! all_adjacent)
2155         return 0;
2156     }
2157
2158   /* If we get here, we have passed all the tests and the combination is
2159      to be allowed.  */
2160
2161   *pdest = dest;
2162   *psrc = src;
2163
2164   return 1;
2165 }
2166 \f
2167 /* LOC is the location within I3 that contains its pattern or the component
2168    of a PARALLEL of the pattern.  We validate that it is valid for combining.
2169
2170    One problem is if I3 modifies its output, as opposed to replacing it
2171    entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2172    doing so would produce an insn that is not equivalent to the original insns.
2173
2174    Consider:
2175
2176          (set (reg:DI 101) (reg:DI 100))
2177          (set (subreg:SI (reg:DI 101) 0) <foo>)
2178
2179    This is NOT equivalent to:
2180
2181          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2182                     (set (reg:DI 101) (reg:DI 100))])
2183
2184    Not only does this modify 100 (in which case it might still be valid
2185    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2186
2187    We can also run into a problem if I2 sets a register that I1
2188    uses and I1 gets directly substituted into I3 (not via I2).  In that
2189    case, we would be getting the wrong value of I2DEST into I3, so we
2190    must reject the combination.  This case occurs when I2 and I1 both
2191    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2192    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2193    of a SET must prevent combination from occurring.  The same situation
2194    can occur for I0, in which case I0_NOT_IN_SRC is set.
2195
2196    Before doing the above check, we first try to expand a field assignment
2197    into a set of logical operations.
2198
2199    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2200    we place a register that is both set and used within I3.  If more than one
2201    such register is detected, we fail.
2202
2203    Return 1 if the combination is valid, zero otherwise.  */
2204
2205 static int
2206 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2207                   int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2208 {
2209   rtx x = *loc;
2210
2211   if (GET_CODE (x) == SET)
2212     {
2213       rtx set = x ;
2214       rtx dest = SET_DEST (set);
2215       rtx src = SET_SRC (set);
2216       rtx inner_dest = dest;
2217       rtx subdest;
2218
2219       while (GET_CODE (inner_dest) == STRICT_LOW_PART
2220              || GET_CODE (inner_dest) == SUBREG
2221              || GET_CODE (inner_dest) == ZERO_EXTRACT)
2222         inner_dest = XEXP (inner_dest, 0);
2223
2224       /* Check for the case where I3 modifies its output, as discussed
2225          above.  We don't want to prevent pseudos from being combined
2226          into the address of a MEM, so only prevent the combination if
2227          i1 or i2 set the same MEM.  */
2228       if ((inner_dest != dest &&
2229            (!MEM_P (inner_dest)
2230             || rtx_equal_p (i2dest, inner_dest)
2231             || (i1dest && rtx_equal_p (i1dest, inner_dest))
2232             || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2233            && (reg_overlap_mentioned_p (i2dest, inner_dest)
2234                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2235                || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2236
2237           /* This is the same test done in can_combine_p except we can't test
2238              all_adjacent; we don't have to, since this instruction will stay
2239              in place, thus we are not considering increasing the lifetime of
2240              INNER_DEST.
2241
2242              Also, if this insn sets a function argument, combining it with
2243              something that might need a spill could clobber a previous
2244              function argument; the all_adjacent test in can_combine_p also
2245              checks this; here, we do a more specific test for this case.  */
2246
2247           || (REG_P (inner_dest)
2248               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2249               && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2250                                               GET_MODE (inner_dest)))
2251           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2252           || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2253         return 0;
2254
2255       /* If DEST is used in I3, it is being killed in this insn, so
2256          record that for later.  We have to consider paradoxical
2257          subregs here, since they kill the whole register, but we
2258          ignore partial subregs, STRICT_LOW_PART, etc.
2259          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2260          STACK_POINTER_REGNUM, since these are always considered to be
2261          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
2262       subdest = dest;
2263       if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2264         subdest = SUBREG_REG (subdest);
2265       if (pi3dest_killed
2266           && REG_P (subdest)
2267           && reg_referenced_p (subdest, PATTERN (i3))
2268           && REGNO (subdest) != FRAME_POINTER_REGNUM
2269           && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2270               || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2271           && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2272               || (REGNO (subdest) != ARG_POINTER_REGNUM
2273                   || ! fixed_regs [REGNO (subdest)]))
2274           && REGNO (subdest) != STACK_POINTER_REGNUM)
2275         {
2276           if (*pi3dest_killed)
2277             return 0;
2278
2279           *pi3dest_killed = subdest;
2280         }
2281     }
2282
2283   else if (GET_CODE (x) == PARALLEL)
2284     {
2285       int i;
2286
2287       for (i = 0; i < XVECLEN (x, 0); i++)
2288         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2289                                 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2290           return 0;
2291     }
2292
2293   return 1;
2294 }
2295 \f
2296 /* Return 1 if X is an arithmetic expression that contains a multiplication
2297    and division.  We don't count multiplications by powers of two here.  */
2298
2299 static int
2300 contains_muldiv (rtx x)
2301 {
2302   switch (GET_CODE (x))
2303     {
2304     case MOD:  case DIV:  case UMOD:  case UDIV:
2305       return 1;
2306
2307     case MULT:
2308       return ! (CONST_INT_P (XEXP (x, 1))
2309                 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2310     default:
2311       if (BINARY_P (x))
2312         return contains_muldiv (XEXP (x, 0))
2313             || contains_muldiv (XEXP (x, 1));
2314
2315       if (UNARY_P (x))
2316         return contains_muldiv (XEXP (x, 0));
2317
2318       return 0;
2319     }
2320 }
2321 \f
2322 /* Determine whether INSN can be used in a combination.  Return nonzero if
2323    not.  This is used in try_combine to detect early some cases where we
2324    can't perform combinations.  */
2325
2326 static int
2327 cant_combine_insn_p (rtx_insn *insn)
2328 {
2329   rtx set;
2330   rtx src, dest;
2331
2332   /* If this isn't really an insn, we can't do anything.
2333      This can occur when flow deletes an insn that it has merged into an
2334      auto-increment address.  */
2335   if (!NONDEBUG_INSN_P (insn))
2336     return 1;
2337
2338   /* Never combine loads and stores involving hard regs that are likely
2339      to be spilled.  The register allocator can usually handle such
2340      reg-reg moves by tying.  If we allow the combiner to make
2341      substitutions of likely-spilled regs, reload might die.
2342      As an exception, we allow combinations involving fixed regs; these are
2343      not available to the register allocator so there's no risk involved.  */
2344
2345   set = single_set (insn);
2346   if (! set)
2347     return 0;
2348   src = SET_SRC (set);
2349   dest = SET_DEST (set);
2350   if (GET_CODE (src) == SUBREG)
2351     src = SUBREG_REG (src);
2352   if (GET_CODE (dest) == SUBREG)
2353     dest = SUBREG_REG (dest);
2354   if (REG_P (src) && REG_P (dest)
2355       && ((HARD_REGISTER_P (src)
2356            && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2357 #ifdef LEAF_REGISTERS
2358            && ! LEAF_REGISTERS [REGNO (src)])
2359 #else
2360            )
2361 #endif
2362           || (HARD_REGISTER_P (dest)
2363               && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2364               && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2365     return 1;
2366
2367   return 0;
2368 }
2369
2370 struct likely_spilled_retval_info
2371 {
2372   unsigned regno, nregs;
2373   unsigned mask;
2374 };
2375
2376 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2377    hard registers that are known to be written to / clobbered in full.  */
2378 static void
2379 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2380 {
2381   struct likely_spilled_retval_info *const info =
2382     (struct likely_spilled_retval_info *) data;
2383   unsigned regno, nregs;
2384   unsigned new_mask;
2385
2386   if (!REG_P (XEXP (set, 0)))
2387     return;
2388   regno = REGNO (x);
2389   if (regno >= info->regno + info->nregs)
2390     return;
2391   nregs = REG_NREGS (x);
2392   if (regno + nregs <= info->regno)
2393     return;
2394   new_mask = (2U << (nregs - 1)) - 1;
2395   if (regno < info->regno)
2396     new_mask >>= info->regno - regno;
2397   else
2398     new_mask <<= regno - info->regno;
2399   info->mask &= ~new_mask;
2400 }
2401
2402 /* Return nonzero iff part of the return value is live during INSN, and
2403    it is likely spilled.  This can happen when more than one insn is needed
2404    to copy the return value, e.g. when we consider to combine into the
2405    second copy insn for a complex value.  */
2406
2407 static int
2408 likely_spilled_retval_p (rtx_insn *insn)
2409 {
2410   rtx_insn *use = BB_END (this_basic_block);
2411   rtx reg;
2412   rtx_insn *p;
2413   unsigned regno, nregs;
2414   /* We assume here that no machine mode needs more than
2415      32 hard registers when the value overlaps with a register
2416      for which TARGET_FUNCTION_VALUE_REGNO_P is true.  */
2417   unsigned mask;
2418   struct likely_spilled_retval_info info;
2419
2420   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2421     return 0;
2422   reg = XEXP (PATTERN (use), 0);
2423   if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2424     return 0;
2425   regno = REGNO (reg);
2426   nregs = REG_NREGS (reg);
2427   if (nregs == 1)
2428     return 0;
2429   mask = (2U << (nregs - 1)) - 1;
2430
2431   /* Disregard parts of the return value that are set later.  */
2432   info.regno = regno;
2433   info.nregs = nregs;
2434   info.mask = mask;
2435   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2436     if (INSN_P (p))
2437       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2438   mask = info.mask;
2439
2440   /* Check if any of the (probably) live return value registers is
2441      likely spilled.  */
2442   nregs --;
2443   do
2444     {
2445       if ((mask & 1 << nregs)
2446           && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2447         return 1;
2448     } while (nregs--);
2449   return 0;
2450 }
2451
2452 /* Adjust INSN after we made a change to its destination.
2453
2454    Changing the destination can invalidate notes that say something about
2455    the results of the insn and a LOG_LINK pointing to the insn.  */
2456
2457 static void
2458 adjust_for_new_dest (rtx_insn *insn)
2459 {
2460   /* For notes, be conservative and simply remove them.  */
2461   remove_reg_equal_equiv_notes (insn);
2462
2463   /* The new insn will have a destination that was previously the destination
2464      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2465      the next use of that destination.  */
2466
2467   rtx set = single_set (insn);
2468   gcc_assert (set);
2469
2470   rtx reg = SET_DEST (set);
2471
2472   while (GET_CODE (reg) == ZERO_EXTRACT
2473          || GET_CODE (reg) == STRICT_LOW_PART
2474          || GET_CODE (reg) == SUBREG)
2475     reg = XEXP (reg, 0);
2476   gcc_assert (REG_P (reg));
2477
2478   distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2479
2480   df_insn_rescan (insn);
2481 }
2482
2483 /* Return TRUE if combine can reuse reg X in mode MODE.
2484    ADDED_SETS is nonzero if the original set is still required.  */
2485 static bool
2486 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2487 {
2488   unsigned int regno;
2489
2490   if (!REG_P (x))
2491     return false;
2492
2493   /* Don't change between modes with different underlying register sizes,
2494      since this could lead to invalid subregs.  */
2495   if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2496                 REGMODE_NATURAL_SIZE (GET_MODE (x))))
2497     return false;
2498
2499   regno = REGNO (x);
2500   /* Allow hard registers if the new mode is legal, and occupies no more
2501      registers than the old mode.  */
2502   if (regno < FIRST_PSEUDO_REGISTER)
2503     return (targetm.hard_regno_mode_ok (regno, mode)
2504             && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2505
2506   /* Or a pseudo that is only used once.  */
2507   return (regno < reg_n_sets_max
2508           && REG_N_SETS (regno) == 1
2509           && !added_sets
2510           && !REG_USERVAR_P (x));
2511 }
2512
2513
2514 /* Check whether X, the destination of a set, refers to part of
2515    the register specified by REG.  */
2516
2517 static bool
2518 reg_subword_p (rtx x, rtx reg)
2519 {
2520   /* Check that reg is an integer mode register.  */
2521   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2522     return false;
2523
2524   if (GET_CODE (x) == STRICT_LOW_PART
2525       || GET_CODE (x) == ZERO_EXTRACT)
2526     x = XEXP (x, 0);
2527
2528   return GET_CODE (x) == SUBREG
2529          && SUBREG_REG (x) == reg
2530          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2531 }
2532
2533 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2534    Note that the INSN should be deleted *after* removing dead edges, so
2535    that the kept edge is the fallthrough edge for a (set (pc) (pc))
2536    but not for a (set (pc) (label_ref FOO)).  */
2537
2538 static void
2539 update_cfg_for_uncondjump (rtx_insn *insn)
2540 {
2541   basic_block bb = BLOCK_FOR_INSN (insn);
2542   gcc_assert (BB_END (bb) == insn);
2543
2544   purge_dead_edges (bb);
2545
2546   delete_insn (insn);
2547   if (EDGE_COUNT (bb->succs) == 1)
2548     {
2549       rtx_insn *insn;
2550
2551       single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2552
2553       /* Remove barriers from the footer if there are any.  */
2554       for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2555         if (BARRIER_P (insn))
2556           {
2557             if (PREV_INSN (insn))
2558               SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2559             else
2560               BB_FOOTER (bb) = NEXT_INSN (insn);
2561             if (NEXT_INSN (insn))
2562               SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2563           }
2564         else if (LABEL_P (insn))
2565           break;
2566     }
2567 }
2568
2569 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2570    by an arbitrary number of CLOBBERs.  */
2571 static bool
2572 is_parallel_of_n_reg_sets (rtx pat, int n)
2573 {
2574   if (GET_CODE (pat) != PARALLEL)
2575     return false;
2576
2577   int len = XVECLEN (pat, 0);
2578   if (len < n)
2579     return false;
2580
2581   int i;
2582   for (i = 0; i < n; i++)
2583     if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2584         || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2585       return false;
2586   for ( ; i < len; i++)
2587     switch (GET_CODE (XVECEXP (pat, 0, i)))
2588       {
2589       case CLOBBER:
2590         if (XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2591           return false;
2592         break;
2593       case CLOBBER_HIGH:
2594         break;
2595       default:
2596         return false;
2597       }
2598   return true;
2599 }
2600
2601 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2602    CLOBBERs), can be split into individual SETs in that order, without
2603    changing semantics.  */
2604 static bool
2605 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2606 {
2607   if (!insn_nothrow_p (insn))
2608     return false;
2609
2610   rtx pat = PATTERN (insn);
2611
2612   int i, j;
2613   for (i = 0; i < n; i++)
2614     {
2615       if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2616         return false;
2617
2618       rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2619
2620       for (j = i + 1; j < n; j++)
2621         if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2622           return false;
2623     }
2624
2625   return true;
2626 }
2627
2628 /* Return whether X is just a single set, with the source
2629    a general_operand.  */
2630 static bool
2631 is_just_move (rtx x)
2632 {
2633   if (INSN_P (x))
2634     x = PATTERN (x);
2635
2636   return (GET_CODE (x) == SET && general_operand (SET_SRC (x), VOIDmode));
2637 }
2638
2639 /* Try to combine the insns I0, I1 and I2 into I3.
2640    Here I0, I1 and I2 appear earlier than I3.
2641    I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2642    I3.
2643
2644    If we are combining more than two insns and the resulting insn is not
2645    recognized, try splitting it into two insns.  If that happens, I2 and I3
2646    are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2647    Otherwise, I0, I1 and I2 are pseudo-deleted.
2648
2649    Return 0 if the combination does not work.  Then nothing is changed.
2650    If we did the combination, return the insn at which combine should
2651    resume scanning.
2652
2653    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2654    new direct jump instruction.
2655
2656    LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2657    been I3 passed to an earlier try_combine within the same basic
2658    block.  */
2659
2660 static rtx_insn *
2661 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2662              int *new_direct_jump_p, rtx_insn *last_combined_insn)
2663 {
2664   /* New patterns for I3 and I2, respectively.  */
2665   rtx newpat, newi2pat = 0;
2666   rtvec newpat_vec_with_clobbers = 0;
2667   int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2668   /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2669      dead.  */
2670   int added_sets_0, added_sets_1, added_sets_2;
2671   /* Total number of SETs to put into I3.  */
2672   int total_sets;
2673   /* Nonzero if I2's or I1's body now appears in I3.  */
2674   int i2_is_used = 0, i1_is_used = 0;
2675   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2676   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2677   /* Contains I3 if the destination of I3 is used in its source, which means
2678      that the old life of I3 is being killed.  If that usage is placed into
2679      I2 and not in I3, a REG_DEAD note must be made.  */
2680   rtx i3dest_killed = 0;
2681   /* SET_DEST and SET_SRC of I2, I1 and I0.  */
2682   rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2683   /* Copy of SET_SRC of I1 and I0, if needed.  */
2684   rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2685   /* Set if I2DEST was reused as a scratch register.  */
2686   bool i2scratch = false;
2687   /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases.  */
2688   rtx i0pat = 0, i1pat = 0, i2pat = 0;
2689   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2690   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2691   int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2692   int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2693   int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2694   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2695   rtx new_i3_notes, new_i2_notes;
2696   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2697   int i3_subst_into_i2 = 0;
2698   /* Notes that I1, I2 or I3 is a MULT operation.  */
2699   int have_mult = 0;
2700   int swap_i2i3 = 0;
2701   int split_i2i3 = 0;
2702   int changed_i3_dest = 0;
2703   bool i2_was_move = false, i3_was_move = false;
2704
2705   int maxreg;
2706   rtx_insn *temp_insn;
2707   rtx temp_expr;
2708   struct insn_link *link;
2709   rtx other_pat = 0;
2710   rtx new_other_notes;
2711   int i;
2712   scalar_int_mode dest_mode, temp_mode;
2713
2714   /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2715      never be).  */
2716   if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2717     return 0;
2718
2719   /* Only try four-insn combinations when there's high likelihood of
2720      success.  Look for simple insns, such as loads of constants or
2721      binary operations involving a constant.  */
2722   if (i0)
2723     {
2724       int i;
2725       int ngood = 0;
2726       int nshift = 0;
2727       rtx set0, set3;
2728
2729       if (!flag_expensive_optimizations)
2730         return 0;
2731
2732       for (i = 0; i < 4; i++)
2733         {
2734           rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2735           rtx set = single_set (insn);
2736           rtx src;
2737           if (!set)
2738             continue;
2739           src = SET_SRC (set);
2740           if (CONSTANT_P (src))
2741             {
2742               ngood += 2;
2743               break;
2744             }
2745           else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2746             ngood++;
2747           else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2748                    || GET_CODE (src) == LSHIFTRT)
2749             nshift++;
2750         }
2751
2752       /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2753          are likely manipulating its value.  Ideally we'll be able to combine
2754          all four insns into a bitfield insertion of some kind. 
2755
2756          Note the source in I0 might be inside a sign/zero extension and the
2757          memory modes in I0 and I3 might be different.  So extract the address
2758          from the destination of I3 and search for it in the source of I0.
2759
2760          In the event that there's a match but the source/dest do not actually
2761          refer to the same memory, the worst that happens is we try some
2762          combinations that we wouldn't have otherwise.  */
2763       if ((set0 = single_set (i0))
2764           /* Ensure the source of SET0 is a MEM, possibly buried inside
2765              an extension.  */
2766           && (GET_CODE (SET_SRC (set0)) == MEM
2767               || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2768                    || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2769                   && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2770           && (set3 = single_set (i3))
2771           /* Ensure the destination of SET3 is a MEM.  */
2772           && GET_CODE (SET_DEST (set3)) == MEM
2773           /* Would it be better to extract the base address for the MEM
2774              in SET3 and look for that?  I don't have cases where it matters
2775              but I could envision such cases.  */
2776           && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2777         ngood += 2;
2778
2779       if (ngood < 2 && nshift < 2)
2780         return 0;
2781     }
2782
2783   /* Exit early if one of the insns involved can't be used for
2784      combinations.  */
2785   if (CALL_P (i2)
2786       || (i1 && CALL_P (i1))
2787       || (i0 && CALL_P (i0))
2788       || cant_combine_insn_p (i3)
2789       || cant_combine_insn_p (i2)
2790       || (i1 && cant_combine_insn_p (i1))
2791       || (i0 && cant_combine_insn_p (i0))
2792       || likely_spilled_retval_p (i3))
2793     return 0;
2794
2795   combine_attempts++;
2796   undobuf.other_insn = 0;
2797
2798   /* Reset the hard register usage information.  */
2799   CLEAR_HARD_REG_SET (newpat_used_regs);
2800
2801   if (dump_file && (dump_flags & TDF_DETAILS))
2802     {
2803       if (i0)
2804         fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2805                  INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2806       else if (i1)
2807         fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2808                  INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2809       else
2810         fprintf (dump_file, "\nTrying %d -> %d:\n",
2811                  INSN_UID (i2), INSN_UID (i3));
2812
2813       if (i0)
2814         dump_insn_slim (dump_file, i0);
2815       if (i1)
2816         dump_insn_slim (dump_file, i1);
2817       dump_insn_slim (dump_file, i2);
2818       dump_insn_slim (dump_file, i3);
2819     }
2820
2821   /* If multiple insns feed into one of I2 or I3, they can be in any
2822      order.  To simplify the code below, reorder them in sequence.  */
2823   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2824     std::swap (i0, i2);
2825   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2826     std::swap (i0, i1);
2827   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2828     std::swap (i1, i2);
2829
2830   added_links_insn = 0;
2831   added_notes_insn = 0;
2832
2833   /* First check for one important special case that the code below will
2834      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2835      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2836      we may be able to replace that destination with the destination of I3.
2837      This occurs in the common code where we compute both a quotient and
2838      remainder into a structure, in which case we want to do the computation
2839      directly into the structure to avoid register-register copies.
2840
2841      Note that this case handles both multiple sets in I2 and also cases
2842      where I2 has a number of CLOBBERs inside the PARALLEL.
2843
2844      We make very conservative checks below and only try to handle the
2845      most common cases of this.  For example, we only handle the case
2846      where I2 and I3 are adjacent to avoid making difficult register
2847      usage tests.  */
2848
2849   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2850       && REG_P (SET_SRC (PATTERN (i3)))
2851       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2852       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2853       && GET_CODE (PATTERN (i2)) == PARALLEL
2854       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2855       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2856          below would need to check what is inside (and reg_overlap_mentioned_p
2857          doesn't support those codes anyway).  Don't allow those destinations;
2858          the resulting insn isn't likely to be recognized anyway.  */
2859       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2860       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2861       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2862                                     SET_DEST (PATTERN (i3)))
2863       && next_active_insn (i2) == i3)
2864     {
2865       rtx p2 = PATTERN (i2);
2866
2867       /* Make sure that the destination of I3,
2868          which we are going to substitute into one output of I2,
2869          is not used within another output of I2.  We must avoid making this:
2870          (parallel [(set (mem (reg 69)) ...)
2871                     (set (reg 69) ...)])
2872          which is not well-defined as to order of actions.
2873          (Besides, reload can't handle output reloads for this.)
2874
2875          The problem can also happen if the dest of I3 is a memory ref,
2876          if another dest in I2 is an indirect memory ref.
2877
2878          Neither can this PARALLEL be an asm.  We do not allow combining
2879          that usually (see can_combine_p), so do not here either.  */
2880       bool ok = true;
2881       for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2882         {
2883           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2884                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER
2885                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER_HIGH)
2886               && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2887                                           SET_DEST (XVECEXP (p2, 0, i))))
2888             ok = false;
2889           else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2890                    && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2891             ok = false;
2892         }
2893
2894       if (ok)
2895         for (i = 0; i < XVECLEN (p2, 0); i++)
2896           if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2897               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2898             {
2899               combine_merges++;
2900
2901               subst_insn = i3;
2902               subst_low_luid = DF_INSN_LUID (i2);
2903
2904               added_sets_2 = added_sets_1 = added_sets_0 = 0;
2905               i2src = SET_SRC (XVECEXP (p2, 0, i));
2906               i2dest = SET_DEST (XVECEXP (p2, 0, i));
2907               i2dest_killed = dead_or_set_p (i2, i2dest);
2908
2909               /* Replace the dest in I2 with our dest and make the resulting
2910                  insn the new pattern for I3.  Then skip to where we validate
2911                  the pattern.  Everything was set up above.  */
2912               SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2913               newpat = p2;
2914               i3_subst_into_i2 = 1;
2915               goto validate_replacement;
2916             }
2917     }
2918
2919   /* If I2 is setting a pseudo to a constant and I3 is setting some
2920      sub-part of it to another constant, merge them by making a new
2921      constant.  */
2922   if (i1 == 0
2923       && (temp_expr = single_set (i2)) != 0
2924       && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2925       && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2926       && GET_CODE (PATTERN (i3)) == SET
2927       && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2928       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2929     {
2930       rtx dest = SET_DEST (PATTERN (i3));
2931       rtx temp_dest = SET_DEST (temp_expr);
2932       int offset = -1;
2933       int width = 0;
2934
2935       if (GET_CODE (dest) == ZERO_EXTRACT)
2936         {
2937           if (CONST_INT_P (XEXP (dest, 1))
2938               && CONST_INT_P (XEXP (dest, 2))
2939               && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2940                                          &dest_mode))
2941             {
2942               width = INTVAL (XEXP (dest, 1));
2943               offset = INTVAL (XEXP (dest, 2));
2944               dest = XEXP (dest, 0);
2945               if (BITS_BIG_ENDIAN)
2946                 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2947             }
2948         }
2949       else
2950         {
2951           if (GET_CODE (dest) == STRICT_LOW_PART)
2952             dest = XEXP (dest, 0);
2953           if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2954             {
2955               width = GET_MODE_PRECISION (dest_mode);
2956               offset = 0;
2957             }
2958         }
2959
2960       if (offset >= 0)
2961         {
2962           /* If this is the low part, we're done.  */
2963           if (subreg_lowpart_p (dest))
2964             ;
2965           /* Handle the case where inner is twice the size of outer.  */
2966           else if (GET_MODE_PRECISION (temp_mode)
2967                    == 2 * GET_MODE_PRECISION (dest_mode))
2968             offset += GET_MODE_PRECISION (dest_mode);
2969           /* Otherwise give up for now.  */
2970           else
2971             offset = -1;
2972         }
2973
2974       if (offset >= 0)
2975         {
2976           rtx inner = SET_SRC (PATTERN (i3));
2977           rtx outer = SET_SRC (temp_expr);
2978
2979           wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2980                                    rtx_mode_t (inner, dest_mode),
2981                                    offset, width);
2982
2983           combine_merges++;
2984           subst_insn = i3;
2985           subst_low_luid = DF_INSN_LUID (i2);
2986           added_sets_2 = added_sets_1 = added_sets_0 = 0;
2987           i2dest = temp_dest;
2988           i2dest_killed = dead_or_set_p (i2, i2dest);
2989
2990           /* Replace the source in I2 with the new constant and make the
2991              resulting insn the new pattern for I3.  Then skip to where we
2992              validate the pattern.  Everything was set up above.  */
2993           SUBST (SET_SRC (temp_expr),
2994                  immed_wide_int_const (o, temp_mode));
2995
2996           newpat = PATTERN (i2);
2997
2998           /* The dest of I3 has been replaced with the dest of I2.  */
2999           changed_i3_dest = 1;
3000           goto validate_replacement;
3001         }
3002     }
3003
3004   /* If we have no I1 and I2 looks like:
3005         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
3006                    (set Y OP)])
3007      make up a dummy I1 that is
3008         (set Y OP)
3009      and change I2 to be
3010         (set (reg:CC X) (compare:CC Y (const_int 0)))
3011
3012      (We can ignore any trailing CLOBBERs.)
3013
3014      This undoes a previous combination and allows us to match a branch-and-
3015      decrement insn.  */
3016
3017   if (!HAVE_cc0 && i1 == 0
3018       && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3019       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
3020           == MODE_CC)
3021       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
3022       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
3023       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
3024                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
3025       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3026       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3027     {
3028       /* We make I1 with the same INSN_UID as I2.  This gives it
3029          the same DF_INSN_LUID for value tracking.  Our fake I1 will
3030          never appear in the insn stream so giving it the same INSN_UID
3031          as I2 will not cause a problem.  */
3032
3033       i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3034                          XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
3035                          -1, NULL_RTX);
3036       INSN_UID (i1) = INSN_UID (i2);
3037
3038       SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
3039       SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
3040              SET_DEST (PATTERN (i1)));
3041       unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
3042       SUBST_LINK (LOG_LINKS (i2),
3043                   alloc_insn_link (i1, regno, LOG_LINKS (i2)));
3044     }
3045
3046   /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
3047      make those two SETs separate I1 and I2 insns, and make an I0 that is
3048      the original I1.  */
3049   if (!HAVE_cc0 && i0 == 0
3050       && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3051       && can_split_parallel_of_n_reg_sets (i2, 2)
3052       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3053       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
3054       && !reg_set_between_p  (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3055       && !reg_set_between_p  (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3056     {
3057       /* If there is no I1, there is no I0 either.  */
3058       i0 = i1;
3059
3060       /* We make I1 with the same INSN_UID as I2.  This gives it
3061          the same DF_INSN_LUID for value tracking.  Our fake I1 will
3062          never appear in the insn stream so giving it the same INSN_UID
3063          as I2 will not cause a problem.  */
3064
3065       i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3066                          XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
3067                          -1, NULL_RTX);
3068       INSN_UID (i1) = INSN_UID (i2);
3069
3070       SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
3071     }
3072
3073   /* Verify that I2 and maybe I1 and I0 can be combined into I3.  */
3074   if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
3075     {
3076       if (dump_file)
3077         fprintf (dump_file, "Can't combine i2 into i3\n");
3078       undo_all ();
3079       return 0;
3080     }
3081   if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
3082     {
3083       if (dump_file)
3084         fprintf (dump_file, "Can't combine i1 into i3\n");
3085       undo_all ();
3086       return 0;
3087     }
3088   if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
3089     {
3090       if (dump_file)
3091         fprintf (dump_file, "Can't combine i0 into i3\n");
3092       undo_all ();
3093       return 0;
3094     }
3095
3096   /* Record whether i2 and i3 are trivial moves.  */
3097   i2_was_move = is_just_move (i2);
3098   i3_was_move = is_just_move (i3);
3099
3100   /* Record whether I2DEST is used in I2SRC and similarly for the other
3101      cases.  Knowing this will help in register status updating below.  */
3102   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3103   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3104   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3105   i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3106   i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3107   i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3108   i2dest_killed = dead_or_set_p (i2, i2dest);
3109   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3110   i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3111
3112   /* For the earlier insns, determine which of the subsequent ones they
3113      feed.  */
3114   i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3115   i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3116   i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3117                           : (!reg_overlap_mentioned_p (i1dest, i0dest)
3118                              && reg_overlap_mentioned_p (i0dest, i2src))));
3119
3120   /* Ensure that I3's pattern can be the destination of combines.  */
3121   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3122                           i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3123                           i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3124                                  || (i1dest_in_i0src && !i0_feeds_i1_n)),
3125                           &i3dest_killed))
3126     {
3127       undo_all ();
3128       return 0;
3129     }
3130
3131   /* See if any of the insns is a MULT operation.  Unless one is, we will
3132      reject a combination that is, since it must be slower.  Be conservative
3133      here.  */
3134   if (GET_CODE (i2src) == MULT
3135       || (i1 != 0 && GET_CODE (i1src) == MULT)
3136       || (i0 != 0 && GET_CODE (i0src) == MULT)
3137       || (GET_CODE (PATTERN (i3)) == SET
3138           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3139     have_mult = 1;
3140
3141   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3142      We used to do this EXCEPT in one case: I3 has a post-inc in an
3143      output operand.  However, that exception can give rise to insns like
3144         mov r3,(r3)+
3145      which is a famous insn on the PDP-11 where the value of r3 used as the
3146      source was model-dependent.  Avoid this sort of thing.  */
3147
3148 #if 0
3149   if (!(GET_CODE (PATTERN (i3)) == SET
3150         && REG_P (SET_SRC (PATTERN (i3)))
3151         && MEM_P (SET_DEST (PATTERN (i3)))
3152         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3153             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3154     /* It's not the exception.  */
3155 #endif
3156     if (AUTO_INC_DEC)
3157       {
3158         rtx link;
3159         for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3160           if (REG_NOTE_KIND (link) == REG_INC
3161               && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3162                   || (i1 != 0
3163                       && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3164             {
3165               undo_all ();
3166               return 0;
3167             }
3168       }
3169
3170   /* See if the SETs in I1 or I2 need to be kept around in the merged
3171      instruction: whenever the value set there is still needed past I3.
3172      For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3173
3174      For the SET in I1, we have two cases: if I1 and I2 independently feed
3175      into I3, the set in I1 needs to be kept around unless I1DEST dies
3176      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
3177      in I1 needs to be kept around unless I1DEST dies or is set in either
3178      I2 or I3.  The same considerations apply to I0.  */
3179
3180   added_sets_2 = !dead_or_set_p (i3, i2dest);
3181
3182   if (i1)
3183     added_sets_1 = !(dead_or_set_p (i3, i1dest)
3184                      || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3185   else
3186     added_sets_1 = 0;
3187
3188   if (i0)
3189     added_sets_0 =  !(dead_or_set_p (i3, i0dest)
3190                       || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3191                       || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3192                           && dead_or_set_p (i2, i0dest)));
3193   else
3194     added_sets_0 = 0;
3195
3196   /* We are about to copy insns for the case where they need to be kept
3197      around.  Check that they can be copied in the merged instruction.  */
3198
3199   if (targetm.cannot_copy_insn_p
3200       && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3201           || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3202           || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3203     {
3204       undo_all ();
3205       return 0;
3206     }
3207
3208   /* If the set in I2 needs to be kept around, we must make a copy of
3209      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3210      PATTERN (I2), we are only substituting for the original I1DEST, not into
3211      an already-substituted copy.  This also prevents making self-referential
3212      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3213      I2DEST.  */
3214
3215   if (added_sets_2)
3216     {
3217       if (GET_CODE (PATTERN (i2)) == PARALLEL)
3218         i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3219       else
3220         i2pat = copy_rtx (PATTERN (i2));
3221     }
3222
3223   if (added_sets_1)
3224     {
3225       if (GET_CODE (PATTERN (i1)) == PARALLEL)
3226         i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3227       else
3228         i1pat = copy_rtx (PATTERN (i1));
3229     }
3230
3231   if (added_sets_0)
3232     {
3233       if (GET_CODE (PATTERN (i0)) == PARALLEL)
3234         i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3235       else
3236         i0pat = copy_rtx (PATTERN (i0));
3237     }
3238
3239   combine_merges++;
3240
3241   /* Substitute in the latest insn for the regs set by the earlier ones.  */
3242
3243   maxreg = max_reg_num ();
3244
3245   subst_insn = i3;
3246
3247   /* Many machines that don't use CC0 have insns that can both perform an
3248      arithmetic operation and set the condition code.  These operations will
3249      be represented as a PARALLEL with the first element of the vector
3250      being a COMPARE of an arithmetic operation with the constant zero.
3251      The second element of the vector will set some pseudo to the result
3252      of the same arithmetic operation.  If we simplify the COMPARE, we won't
3253      match such a pattern and so will generate an extra insn.   Here we test
3254      for this case, where both the comparison and the operation result are
3255      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3256      I2SRC.  Later we will make the PARALLEL that contains I2.  */
3257
3258   if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3259       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3260       && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3261       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3262     {
3263       rtx newpat_dest;
3264       rtx *cc_use_loc = NULL;
3265       rtx_insn *cc_use_insn = NULL;
3266       rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3267       machine_mode compare_mode, orig_compare_mode;
3268       enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3269       scalar_int_mode mode;
3270
3271       newpat = PATTERN (i3);
3272       newpat_dest = SET_DEST (newpat);
3273       compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3274
3275       if (undobuf.other_insn == 0
3276           && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3277                                             &cc_use_insn)))
3278         {
3279           compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3280           if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3281             compare_code = simplify_compare_const (compare_code, mode,
3282                                                    op0, &op1);
3283           target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3284         }
3285
3286       /* Do the rest only if op1 is const0_rtx, which may be the
3287          result of simplification.  */
3288       if (op1 == const0_rtx)
3289         {
3290           /* If a single use of the CC is found, prepare to modify it
3291              when SELECT_CC_MODE returns a new CC-class mode, or when
3292              the above simplify_compare_const() returned a new comparison
3293              operator.  undobuf.other_insn is assigned the CC use insn
3294              when modifying it.  */
3295           if (cc_use_loc)
3296             {
3297 #ifdef SELECT_CC_MODE
3298               machine_mode new_mode
3299                 = SELECT_CC_MODE (compare_code, op0, op1);
3300               if (new_mode != orig_compare_mode
3301                   && can_change_dest_mode (SET_DEST (newpat),
3302                                            added_sets_2, new_mode))
3303                 {
3304                   unsigned int regno = REGNO (newpat_dest);
3305                   compare_mode = new_mode;
3306                   if (regno < FIRST_PSEUDO_REGISTER)
3307                     newpat_dest = gen_rtx_REG (compare_mode, regno);
3308                   else
3309                     {
3310                       SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3311                       newpat_dest = regno_reg_rtx[regno];
3312                     }
3313                 }
3314 #endif
3315               /* Cases for modifying the CC-using comparison.  */
3316               if (compare_code != orig_compare_code
3317                   /* ??? Do we need to verify the zero rtx?  */
3318                   && XEXP (*cc_use_loc, 1) == const0_rtx)
3319                 {
3320                   /* Replace cc_use_loc with entire new RTX.  */
3321                   SUBST (*cc_use_loc,
3322                          gen_rtx_fmt_ee (compare_code, GET_MODE (*cc_use_loc),
3323                                          newpat_dest, const0_rtx));
3324                   undobuf.other_insn = cc_use_insn;
3325                 }
3326               else if (compare_mode != orig_compare_mode)
3327                 {
3328                   /* Just replace the CC reg with a new mode.  */
3329                   SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3330                   undobuf.other_insn = cc_use_insn;
3331                 }
3332             }
3333
3334           /* Now we modify the current newpat:
3335              First, SET_DEST(newpat) is updated if the CC mode has been
3336              altered. For targets without SELECT_CC_MODE, this should be
3337              optimized away.  */
3338           if (compare_mode != orig_compare_mode)
3339             SUBST (SET_DEST (newpat), newpat_dest);
3340           /* This is always done to propagate i2src into newpat.  */
3341           SUBST (SET_SRC (newpat),
3342                  gen_rtx_COMPARE (compare_mode, op0, op1));
3343           /* Create new version of i2pat if needed; the below PARALLEL
3344              creation needs this to work correctly.  */
3345           if (! rtx_equal_p (i2src, op0))
3346             i2pat = gen_rtx_SET (i2dest, op0);
3347           i2_is_used = 1;
3348         }
3349     }
3350
3351   if (i2_is_used == 0)
3352     {
3353       /* It is possible that the source of I2 or I1 may be performing
3354          an unneeded operation, such as a ZERO_EXTEND of something
3355          that is known to have the high part zero.  Handle that case
3356          by letting subst look at the inner insns.
3357
3358          Another way to do this would be to have a function that tries
3359          to simplify a single insn instead of merging two or more
3360          insns.  We don't do this because of the potential of infinite
3361          loops and because of the potential extra memory required.
3362          However, doing it the way we are is a bit of a kludge and
3363          doesn't catch all cases.
3364
3365          But only do this if -fexpensive-optimizations since it slows
3366          things down and doesn't usually win.
3367
3368          This is not done in the COMPARE case above because the
3369          unmodified I2PAT is used in the PARALLEL and so a pattern
3370          with a modified I2SRC would not match.  */
3371
3372       if (flag_expensive_optimizations)
3373         {
3374           /* Pass pc_rtx so no substitutions are done, just
3375              simplifications.  */
3376           if (i1)
3377             {
3378               subst_low_luid = DF_INSN_LUID (i1);
3379               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3380             }
3381
3382           subst_low_luid = DF_INSN_LUID (i2);
3383           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3384         }
3385
3386       n_occurrences = 0;                /* `subst' counts here */
3387       subst_low_luid = DF_INSN_LUID (i2);
3388
3389       /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3390          copy of I2SRC each time we substitute it, in order to avoid creating
3391          self-referential RTL when we will be substituting I1SRC for I1DEST
3392          later.  Likewise if I0 feeds into I2, either directly or indirectly
3393          through I1, and I0DEST is in I0SRC.  */
3394       newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3395                       (i1_feeds_i2_n && i1dest_in_i1src)
3396                       || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3397                           && i0dest_in_i0src));
3398       substed_i2 = 1;
3399
3400       /* Record whether I2's body now appears within I3's body.  */
3401       i2_is_used = n_occurrences;
3402     }
3403
3404   /* If we already got a failure, don't try to do more.  Otherwise, try to
3405      substitute I1 if we have it.  */
3406
3407   if (i1 && GET_CODE (newpat) != CLOBBER)
3408     {
3409       /* Check that an autoincrement side-effect on I1 has not been lost.
3410          This happens if I1DEST is mentioned in I2 and dies there, and
3411          has disappeared from the new pattern.  */
3412       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3413            && i1_feeds_i2_n
3414            && dead_or_set_p (i2, i1dest)
3415            && !reg_overlap_mentioned_p (i1dest, newpat))
3416            /* Before we can do this substitution, we must redo the test done
3417               above (see detailed comments there) that ensures I1DEST isn't
3418               mentioned in any SETs in NEWPAT that are field assignments.  */
3419           || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3420                                 0, 0, 0))
3421         {
3422           undo_all ();
3423           return 0;
3424         }
3425
3426       n_occurrences = 0;
3427       subst_low_luid = DF_INSN_LUID (i1);
3428
3429       /* If the following substitution will modify I1SRC, make a copy of it
3430          for the case where it is substituted for I1DEST in I2PAT later.  */
3431       if (added_sets_2 && i1_feeds_i2_n)
3432         i1src_copy = copy_rtx (i1src);
3433
3434       /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3435          copy of I1SRC each time we substitute it, in order to avoid creating
3436          self-referential RTL when we will be substituting I0SRC for I0DEST
3437          later.  */
3438       newpat = subst (newpat, i1dest, i1src, 0, 0,
3439                       i0_feeds_i1_n && i0dest_in_i0src);
3440       substed_i1 = 1;
3441
3442       /* Record whether I1's body now appears within I3's body.  */
3443       i1_is_used = n_occurrences;
3444     }
3445
3446   /* Likewise for I0 if we have it.  */
3447
3448   if (i0 && GET_CODE (newpat) != CLOBBER)
3449     {
3450       if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3451            && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3452                || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3453            && !reg_overlap_mentioned_p (i0dest, newpat))
3454           || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3455                                 0, 0, 0))
3456         {
3457           undo_all ();
3458           return 0;
3459         }
3460
3461       /* If the following substitution will modify I0SRC, make a copy of it
3462          for the case where it is substituted for I0DEST in I1PAT later.  */
3463       if (added_sets_1 && i0_feeds_i1_n)
3464         i0src_copy = copy_rtx (i0src);
3465       /* And a copy for I0DEST in I2PAT substitution.  */
3466       if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3467                            || (i0_feeds_i2_n)))
3468         i0src_copy2 = copy_rtx (i0src);
3469
3470       n_occurrences = 0;
3471       subst_low_luid = DF_INSN_LUID (i0);
3472       newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3473       substed_i0 = 1;
3474     }
3475
3476   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
3477      to count all the ways that I2SRC and I1SRC can be used.  */
3478   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3479        && i2_is_used + added_sets_2 > 1)
3480       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3481           && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3482               > 1))
3483       || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3484           && (n_occurrences + added_sets_0
3485               + (added_sets_1 && i0_feeds_i1_n)
3486               + (added_sets_2 && i0_feeds_i2_n)
3487               > 1))
3488       /* Fail if we tried to make a new register.  */
3489       || max_reg_num () != maxreg
3490       /* Fail if we couldn't do something and have a CLOBBER.  */
3491       || GET_CODE (newpat) == CLOBBER
3492       /* Fail if this new pattern is a MULT and we didn't have one before
3493          at the outer level.  */
3494       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3495           && ! have_mult))
3496     {
3497       undo_all ();
3498       return 0;
3499     }
3500
3501   /* If the actions of the earlier insns must be kept
3502      in addition to substituting them into the latest one,
3503      we must make a new PARALLEL for the latest insn
3504      to hold additional the SETs.  */
3505
3506   if (added_sets_0 || added_sets_1 || added_sets_2)
3507     {
3508       int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3509       combine_extras++;
3510
3511       if (GET_CODE (newpat) == PARALLEL)
3512         {
3513           rtvec old = XVEC (newpat, 0);
3514           total_sets = XVECLEN (newpat, 0) + extra_sets;
3515           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3516           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3517                   sizeof (old->elem[0]) * old->num_elem);
3518         }
3519       else
3520         {
3521           rtx old = newpat;
3522           total_sets = 1 + extra_sets;
3523           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3524           XVECEXP (newpat, 0, 0) = old;
3525         }
3526
3527       if (added_sets_0)
3528         XVECEXP (newpat, 0, --total_sets) = i0pat;
3529
3530       if (added_sets_1)
3531         {
3532           rtx t = i1pat;
3533           if (i0_feeds_i1_n)
3534             t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3535
3536           XVECEXP (newpat, 0, --total_sets) = t;
3537         }
3538       if (added_sets_2)
3539         {
3540           rtx t = i2pat;
3541           if (i1_feeds_i2_n)
3542             t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3543                        i0_feeds_i1_n && i0dest_in_i0src);
3544           if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3545             t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3546
3547           XVECEXP (newpat, 0, --total_sets) = t;
3548         }
3549     }
3550
3551  validate_replacement:
3552
3553   /* Note which hard regs this insn has as inputs.  */
3554   mark_used_regs_combine (newpat);
3555
3556   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
3557      consider splitting this pattern, we might need these clobbers.  */
3558   if (i1 && GET_CODE (newpat) == PARALLEL
3559       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3560     {
3561       int len = XVECLEN (newpat, 0);
3562
3563       newpat_vec_with_clobbers = rtvec_alloc (len);
3564       for (i = 0; i < len; i++)
3565         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3566     }
3567
3568   /* We have recognized nothing yet.  */
3569   insn_code_number = -1;
3570
3571   /* See if this is a PARALLEL of two SETs where one SET's destination is
3572      a register that is unused and this isn't marked as an instruction that
3573      might trap in an EH region.  In that case, we just need the other SET.
3574      We prefer this over the PARALLEL.
3575
3576      This can occur when simplifying a divmod insn.  We *must* test for this
3577      case here because the code below that splits two independent SETs doesn't
3578      handle this case correctly when it updates the register status.
3579
3580      It's pointless doing this if we originally had two sets, one from
3581      i3, and one from i2.  Combining then splitting the parallel results
3582      in the original i2 again plus an invalid insn (which we delete).
3583      The net effect is only to move instructions around, which makes
3584      debug info less accurate.
3585
3586      If the remaining SET came from I2 its destination should not be used
3587      between I2 and I3.  See PR82024.  */
3588
3589   if (!(added_sets_2 && i1 == 0)
3590       && is_parallel_of_n_reg_sets (newpat, 2)
3591       && asm_noperands (newpat) < 0)
3592     {
3593       rtx set0 = XVECEXP (newpat, 0, 0);
3594       rtx set1 = XVECEXP (newpat, 0, 1);
3595       rtx oldpat = newpat;
3596
3597       if (((REG_P (SET_DEST (set1))
3598             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3599            || (GET_CODE (SET_DEST (set1)) == SUBREG
3600                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3601           && insn_nothrow_p (i3)
3602           && !side_effects_p (SET_SRC (set1)))
3603         {
3604           newpat = set0;
3605           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3606         }
3607
3608       else if (((REG_P (SET_DEST (set0))
3609                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3610                 || (GET_CODE (SET_DEST (set0)) == SUBREG
3611                     && find_reg_note (i3, REG_UNUSED,
3612                                       SUBREG_REG (SET_DEST (set0)))))
3613                && insn_nothrow_p (i3)
3614                && !side_effects_p (SET_SRC (set0)))
3615         {
3616           rtx dest = SET_DEST (set1);
3617           if (GET_CODE (dest) == SUBREG)
3618             dest = SUBREG_REG (dest);
3619           if (!reg_used_between_p (dest, i2, i3))
3620             {
3621               newpat = set1;
3622               insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3623
3624               if (insn_code_number >= 0)
3625                 changed_i3_dest = 1;
3626             }
3627         }
3628
3629       if (insn_code_number < 0)
3630         newpat = oldpat;
3631     }
3632
3633   /* Is the result of combination a valid instruction?  */
3634   if (insn_code_number < 0)
3635     insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3636
3637   /* If we were combining three insns and the result is a simple SET
3638      with no ASM_OPERANDS that wasn't recognized, try to split it into two
3639      insns.  There are two ways to do this.  It can be split using a
3640      machine-specific method (like when you have an addition of a large
3641      constant) or by combine in the function find_split_point.  */
3642
3643   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3644       && asm_noperands (newpat) < 0)
3645     {
3646       rtx parallel, *split;
3647       rtx_insn *m_split_insn;
3648
3649       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
3650          use I2DEST as a scratch register will help.  In the latter case,
3651          convert I2DEST to the mode of the source of NEWPAT if we can.  */
3652
3653       m_split_insn = combine_split_insns (newpat, i3);
3654
3655       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3656          inputs of NEWPAT.  */
3657
3658       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3659          possible to try that as a scratch reg.  This would require adding
3660          more code to make it work though.  */
3661
3662       if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3663         {
3664           machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3665
3666           /* ??? Reusing i2dest without resetting the reg_stat entry for it
3667              (temporarily, until we are committed to this instruction
3668              combination) does not work: for example, any call to nonzero_bits
3669              on the register (from a splitter in the MD file, for example)
3670              will get the old information, which is invalid.
3671
3672              Since nowadays we can create registers during combine just fine,
3673              we should just create a new one here, not reuse i2dest.  */
3674
3675           /* First try to split using the original register as a
3676              scratch register.  */
3677           parallel = gen_rtx_PARALLEL (VOIDmode,
3678                                        gen_rtvec (2, newpat,
3679                                                   gen_rtx_CLOBBER (VOIDmode,
3680                                                                    i2dest)));
3681           m_split_insn = combine_split_insns (parallel, i3);
3682
3683           /* If that didn't work, try changing the mode of I2DEST if
3684              we can.  */
3685           if (m_split_insn == 0
3686               && new_mode != GET_MODE (i2dest)
3687               && new_mode != VOIDmode
3688               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3689             {
3690               machine_mode old_mode = GET_MODE (i2dest);
3691               rtx ni2dest;
3692
3693               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3694                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3695               else
3696                 {
3697                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3698                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
3699                 }
3700
3701               parallel = (gen_rtx_PARALLEL
3702                           (VOIDmode,
3703                            gen_rtvec (2, newpat,
3704                                       gen_rtx_CLOBBER (VOIDmode,
3705                                                        ni2dest))));
3706               m_split_insn = combine_split_insns (parallel, i3);
3707
3708               if (m_split_insn == 0
3709                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3710                 {
3711                   struct undo *buf;
3712
3713                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3714                   buf = undobuf.undos;
3715                   undobuf.undos = buf->next;
3716                   buf->next = undobuf.frees;
3717                   undobuf.frees = buf;
3718                 }
3719             }
3720
3721           i2scratch = m_split_insn != 0;
3722         }
3723
3724       /* If recog_for_combine has discarded clobbers, try to use them
3725          again for the split.  */
3726       if (m_split_insn == 0 && newpat_vec_with_clobbers)
3727         {
3728           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3729           m_split_insn = combine_split_insns (parallel, i3);
3730         }
3731
3732       if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3733         {
3734           rtx m_split_pat = PATTERN (m_split_insn);
3735           insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3736           if (insn_code_number >= 0)
3737             newpat = m_split_pat;
3738         }
3739       else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3740                && (next_nonnote_nondebug_insn (i2) == i3
3741                    || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3742         {
3743           rtx i2set, i3set;
3744           rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3745           newi2pat = PATTERN (m_split_insn);
3746
3747           i3set = single_set (NEXT_INSN (m_split_insn));
3748           i2set = single_set (m_split_insn);
3749
3750           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3751
3752           /* If I2 or I3 has multiple SETs, we won't know how to track
3753              register status, so don't use these insns.  If I2's destination
3754              is used between I2 and I3, we also can't use these insns.  */
3755
3756           if (i2_code_number >= 0 && i2set && i3set
3757               && (next_nonnote_nondebug_insn (i2) == i3
3758                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3759             insn_code_number = recog_for_combine (&newi3pat, i3,
3760                                                   &new_i3_notes);
3761           if (insn_code_number >= 0)
3762             newpat = newi3pat;
3763
3764           /* It is possible that both insns now set the destination of I3.
3765              If so, we must show an extra use of it.  */
3766
3767           if (insn_code_number >= 0)
3768             {
3769               rtx new_i3_dest = SET_DEST (i3set);
3770               rtx new_i2_dest = SET_DEST (i2set);
3771
3772               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3773                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3774                      || GET_CODE (new_i3_dest) == SUBREG)
3775                 new_i3_dest = XEXP (new_i3_dest, 0);
3776
3777               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3778                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3779                      || GET_CODE (new_i2_dest) == SUBREG)
3780                 new_i2_dest = XEXP (new_i2_dest, 0);
3781
3782               if (REG_P (new_i3_dest)
3783                   && REG_P (new_i2_dest)
3784                   && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3785                   && REGNO (new_i2_dest) < reg_n_sets_max)
3786                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3787             }
3788         }
3789
3790       /* If we can split it and use I2DEST, go ahead and see if that
3791          helps things be recognized.  Verify that none of the registers
3792          are set between I2 and I3.  */
3793       if (insn_code_number < 0
3794           && (split = find_split_point (&newpat, i3, false)) != 0
3795           && (!HAVE_cc0 || REG_P (i2dest))
3796           /* We need I2DEST in the proper mode.  If it is a hard register
3797              or the only use of a pseudo, we can change its mode.
3798              Make sure we don't change a hard register to have a mode that
3799              isn't valid for it, or change the number of registers.  */
3800           && (GET_MODE (*split) == GET_MODE (i2dest)
3801               || GET_MODE (*split) == VOIDmode
3802               || can_change_dest_mode (i2dest, added_sets_2,
3803                                        GET_MODE (*split)))
3804           && (next_nonnote_nondebug_insn (i2) == i3
3805               || !modified_between_p (*split, i2, i3))
3806           /* We can't overwrite I2DEST if its value is still used by
3807              NEWPAT.  */
3808           && ! reg_referenced_p (i2dest, newpat))
3809         {
3810           rtx newdest = i2dest;
3811           enum rtx_code split_code = GET_CODE (*split);
3812           machine_mode split_mode = GET_MODE (*split);
3813           bool subst_done = false;
3814           newi2pat = NULL_RTX;
3815
3816           i2scratch = true;
3817
3818           /* *SPLIT may be part of I2SRC, so make sure we have the
3819              original expression around for later debug processing.
3820              We should not need I2SRC any more in other cases.  */
3821           if (MAY_HAVE_DEBUG_BIND_INSNS)
3822             i2src = copy_rtx (i2src);
3823           else
3824             i2src = NULL;
3825
3826           /* Get NEWDEST as a register in the proper mode.  We have already
3827              validated that we can do this.  */
3828           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3829             {
3830               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3831                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3832               else
3833                 {
3834                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3835                   newdest = regno_reg_rtx[REGNO (i2dest)];
3836                 }
3837             }
3838
3839           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3840              an ASHIFT.  This can occur if it was inside a PLUS and hence
3841              appeared to be a memory address.  This is a kludge.  */
3842           if (split_code == MULT
3843               && CONST_INT_P (XEXP (*split, 1))
3844               && INTVAL (XEXP (*split, 1)) > 0
3845               && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3846             {
3847               rtx i_rtx = gen_int_shift_amount (split_mode, i);
3848               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3849                                              XEXP (*split, 0), i_rtx));
3850               /* Update split_code because we may not have a multiply
3851                  anymore.  */
3852               split_code = GET_CODE (*split);
3853             }
3854
3855           /* Similarly for (plus (mult FOO (const_int pow2))).  */
3856           if (split_code == PLUS
3857               && GET_CODE (XEXP (*split, 0)) == MULT
3858               && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3859               && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3860               && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3861             {
3862               rtx nsplit = XEXP (*split, 0);
3863               rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3864               SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3865                                                        XEXP (nsplit, 0),
3866                                                        i_rtx));
3867               /* Update split_code because we may not have a multiply
3868                  anymore.  */
3869               split_code = GET_CODE (*split);
3870             }
3871
3872 #ifdef INSN_SCHEDULING
3873           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3874              be written as a ZERO_EXTEND.  */
3875           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3876             {
3877               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3878                  what it really is.  */
3879               if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3880                   == SIGN_EXTEND)
3881                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3882                                                     SUBREG_REG (*split)));
3883               else
3884                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3885                                                     SUBREG_REG (*split)));
3886             }
3887 #endif
3888
3889           /* Attempt to split binary operators using arithmetic identities.  */
3890           if (BINARY_P (SET_SRC (newpat))
3891               && split_mode == GET_MODE (SET_SRC (newpat))
3892               && ! side_effects_p (SET_SRC (newpat)))
3893             {
3894               rtx setsrc = SET_SRC (newpat);
3895               machine_mode mode = GET_MODE (setsrc);
3896               enum rtx_code code = GET_CODE (setsrc);
3897               rtx src_op0 = XEXP (setsrc, 0);
3898               rtx src_op1 = XEXP (setsrc, 1);
3899
3900               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3901               if (rtx_equal_p (src_op0, src_op1))
3902                 {
3903                   newi2pat = gen_rtx_SET (newdest, src_op0);
3904                   SUBST (XEXP (setsrc, 0), newdest);
3905                   SUBST (XEXP (setsrc, 1), newdest);
3906                   subst_done = true;
3907                 }
3908               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3909               else if ((code == PLUS || code == MULT)
3910                        && GET_CODE (src_op0) == code
3911                        && GET_CODE (XEXP (src_op0, 0)) == code
3912                        && (INTEGRAL_MODE_P (mode)
3913                            || (FLOAT_MODE_P (mode)
3914                                && flag_unsafe_math_optimizations)))
3915                 {
3916                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3917                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3918                   rtx r = XEXP (src_op0, 1);
3919                   rtx s = src_op1;
3920
3921                   /* Split both "((X op Y) op X) op Y" and
3922                      "((X op Y) op Y) op X" as "T op T" where T is
3923                      "X op Y".  */
3924                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3925                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3926                     {
3927                       newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3928                       SUBST (XEXP (setsrc, 0), newdest);
3929                       SUBST (XEXP (setsrc, 1), newdest);
3930                       subst_done = true;
3931                     }
3932                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3933                      T is "X op Y".  */
3934                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3935                     {
3936                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3937                       newi2pat = gen_rtx_SET (newdest, tmp);
3938                       SUBST (XEXP (setsrc, 0), newdest);
3939                       SUBST (XEXP (setsrc, 1), newdest);
3940                       subst_done = true;
3941                     }
3942                 }
3943             }
3944
3945           if (!subst_done)
3946             {
3947               newi2pat = gen_rtx_SET (newdest, *split);
3948               SUBST (*split, newdest);
3949             }
3950
3951           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3952
3953           /* recog_for_combine might have added CLOBBERs to newi2pat.
3954              Make sure NEWPAT does not depend on the clobbered regs.  */
3955           if (GET_CODE (newi2pat) == PARALLEL)
3956             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3957               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3958                 {
3959                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3960                   if (reg_overlap_mentioned_p (reg, newpat))
3961                     {
3962                       undo_all ();
3963                       return 0;
3964                     }
3965                 }
3966
3967           /* If the split point was a MULT and we didn't have one before,
3968              don't use one now.  */
3969           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3970             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3971         }
3972     }
3973
3974   /* Check for a case where we loaded from memory in a narrow mode and
3975      then sign extended it, but we need both registers.  In that case,
3976      we have a PARALLEL with both loads from the same memory location.
3977      We can split this into a load from memory followed by a register-register
3978      copy.  This saves at least one insn, more if register allocation can
3979      eliminate the copy.
3980
3981      We cannot do this if the destination of the first assignment is a
3982      condition code register or cc0.  We eliminate this case by making sure
3983      the SET_DEST and SET_SRC have the same mode.
3984
3985      We cannot do this if the destination of the second assignment is
3986      a register that we have already assumed is zero-extended.  Similarly
3987      for a SUBREG of such a register.  */
3988
3989   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3990            && GET_CODE (newpat) == PARALLEL
3991            && XVECLEN (newpat, 0) == 2
3992            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3993            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3994            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3995                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3996            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3997            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3998                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3999            && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
4000            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4001            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4002            && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
4003                  (REG_P (temp_expr)
4004                   && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
4005                   && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4006                                BITS_PER_WORD)
4007                   && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4008                                HOST_BITS_PER_INT)
4009                   && (reg_stat[REGNO (temp_expr)].nonzero_bits
4010                       != GET_MODE_MASK (word_mode))))
4011            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
4012                  && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
4013                      (REG_P (temp_expr)
4014                       && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
4015                       && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4016                                    BITS_PER_WORD)
4017                       && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4018                                    HOST_BITS_PER_INT)
4019                       && (reg_stat[REGNO (temp_expr)].nonzero_bits
4020                           != GET_MODE_MASK (word_mode)))))
4021            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4022                                          SET_SRC (XVECEXP (newpat, 0, 1)))
4023            && ! find_reg_note (i3, REG_UNUSED,
4024                                SET_DEST (XVECEXP (newpat, 0, 0))))
4025     {
4026       rtx ni2dest;
4027
4028       newi2pat = XVECEXP (newpat, 0, 0);
4029       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
4030       newpat = XVECEXP (newpat, 0, 1);
4031       SUBST (SET_SRC (newpat),
4032              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
4033       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4034
4035       if (i2_code_number >= 0)
4036         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4037
4038       if (insn_code_number >= 0)
4039         swap_i2i3 = 1;
4040     }
4041
4042   /* Similarly, check for a case where we have a PARALLEL of two independent
4043      SETs but we started with three insns.  In this case, we can do the sets
4044      as two separate insns.  This case occurs when some SET allows two
4045      other insns to combine, but the destination of that SET is still live.
4046
4047      Also do this if we started with two insns and (at least) one of the
4048      resulting sets is a noop; this noop will be deleted later.
4049
4050      Also do this if we started with two insns neither of which was a simple
4051      move.  */
4052
4053   else if (insn_code_number < 0 && asm_noperands (newpat) < 0
4054            && GET_CODE (newpat) == PARALLEL
4055            && XVECLEN (newpat, 0) == 2
4056            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
4057            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
4058            && (i1
4059                || set_noop_p (XVECEXP (newpat, 0, 0))
4060                || set_noop_p (XVECEXP (newpat, 0, 1))
4061                || (!i2_was_move && !i3_was_move))
4062            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
4063            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
4064            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4065            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4066            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4067                                   XVECEXP (newpat, 0, 0))
4068            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
4069                                   XVECEXP (newpat, 0, 1))
4070            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
4071                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
4072     {
4073       rtx set0 = XVECEXP (newpat, 0, 0);
4074       rtx set1 = XVECEXP (newpat, 0, 1);
4075
4076       /* Normally, it doesn't matter which of the two is done first,
4077          but the one that references cc0 can't be the second, and
4078          one which uses any regs/memory set in between i2 and i3 can't
4079          be first.  The PARALLEL might also have been pre-existing in i3,
4080          so we need to make sure that we won't wrongly hoist a SET to i2
4081          that would conflict with a death note present in there, or would
4082          have its dest modified between i2 and i3.  */
4083       if (!modified_between_p (SET_SRC (set1), i2, i3)
4084           && !(REG_P (SET_DEST (set1))
4085                && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4086           && !(GET_CODE (SET_DEST (set1)) == SUBREG
4087                && find_reg_note (i2, REG_DEAD,
4088                                  SUBREG_REG (SET_DEST (set1))))
4089           && !modified_between_p (SET_DEST (set1), i2, i3)
4090           && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
4091           /* If I3 is a jump, ensure that set0 is a jump so that
4092              we do not create invalid RTL.  */
4093           && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4094          )
4095         {
4096           newi2pat = set1;
4097           newpat = set0;
4098         }
4099       else if (!modified_between_p (SET_SRC (set0), i2, i3)
4100                && !(REG_P (SET_DEST (set0))
4101                     && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4102                && !(GET_CODE (SET_DEST (set0)) == SUBREG
4103                     && find_reg_note (i2, REG_DEAD,
4104                                       SUBREG_REG (SET_DEST (set0))))
4105                && !modified_between_p (SET_DEST (set0), i2, i3)
4106                && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4107                /* If I3 is a jump, ensure that set1 is a jump so that
4108                   we do not create invalid RTL.  */
4109                && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4110               )
4111         {
4112           newi2pat = set0;
4113           newpat = set1;
4114         }
4115       else
4116         {
4117           undo_all ();
4118           return 0;
4119         }
4120
4121       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4122
4123       if (i2_code_number >= 0)
4124         {
4125           /* recog_for_combine might have added CLOBBERs to newi2pat.
4126              Make sure NEWPAT does not depend on the clobbered regs.  */
4127           if (GET_CODE (newi2pat) == PARALLEL)
4128             {
4129               for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4130                 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4131                   {
4132                     rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4133                     if (reg_overlap_mentioned_p (reg, newpat))
4134                       {
4135                         undo_all ();
4136                         return 0;
4137                       }
4138                   }
4139             }
4140
4141           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4142
4143           if (insn_code_number >= 0)
4144             split_i2i3 = 1;
4145         }
4146     }
4147
4148   /* If it still isn't recognized, fail and change things back the way they
4149      were.  */
4150   if ((insn_code_number < 0
4151        /* Is the result a reasonable ASM_OPERANDS?  */
4152        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4153     {
4154       undo_all ();
4155       return 0;
4156     }
4157
4158   /* If we had to change another insn, make sure it is valid also.  */
4159   if (undobuf.other_insn)
4160     {
4161       CLEAR_HARD_REG_SET (newpat_used_regs);
4162
4163       other_pat = PATTERN (undobuf.other_insn);
4164       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4165                                              &new_other_notes);
4166
4167       if (other_code_number < 0 && ! check_asm_operands (other_pat))
4168         {
4169           undo_all ();
4170           return 0;
4171         }
4172     }
4173
4174   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4175      they are adjacent to each other or not.  */
4176   if (HAVE_cc0)
4177     {
4178       rtx_insn *p = prev_nonnote_insn (i3);
4179       if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4180           && sets_cc0_p (newi2pat))
4181         {
4182           undo_all ();
4183           return 0;
4184         }
4185     }
4186
4187   /* Only allow this combination if insn_cost reports that the
4188      replacement instructions are cheaper than the originals.  */
4189   if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4190     {
4191       undo_all ();
4192       return 0;
4193     }
4194
4195   if (MAY_HAVE_DEBUG_BIND_INSNS)
4196     {
4197       struct undo *undo;
4198
4199       for (undo = undobuf.undos; undo; undo = undo->next)
4200         if (undo->kind == UNDO_MODE)
4201           {
4202             rtx reg = *undo->where.r;
4203             machine_mode new_mode = GET_MODE (reg);
4204             machine_mode old_mode = undo->old_contents.m;
4205
4206             /* Temporarily revert mode back.  */
4207             adjust_reg_mode (reg, old_mode);
4208
4209             if (reg == i2dest && i2scratch)
4210               {
4211                 /* If we used i2dest as a scratch register with a
4212                    different mode, substitute it for the original
4213                    i2src while its original mode is temporarily
4214                    restored, and then clear i2scratch so that we don't
4215                    do it again later.  */
4216                 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4217                                      this_basic_block);
4218                 i2scratch = false;
4219                 /* Put back the new mode.  */
4220                 adjust_reg_mode (reg, new_mode);
4221               }
4222             else
4223               {
4224                 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4225                 rtx_insn *first, *last;
4226
4227                 if (reg == i2dest)
4228                   {
4229                     first = i2;
4230                     last = last_combined_insn;
4231                   }
4232                 else
4233                   {
4234                     first = i3;
4235                     last = undobuf.other_insn;
4236                     gcc_assert (last);
4237                     if (DF_INSN_LUID (last)
4238                         < DF_INSN_LUID (last_combined_insn))
4239                       last = last_combined_insn;
4240                   }
4241
4242                 /* We're dealing with a reg that changed mode but not
4243                    meaning, so we want to turn it into a subreg for
4244                    the new mode.  However, because of REG sharing and
4245                    because its mode had already changed, we have to do
4246                    it in two steps.  First, replace any debug uses of
4247                    reg, with its original mode temporarily restored,
4248                    with this copy we have created; then, replace the
4249                    copy with the SUBREG of the original shared reg,
4250                    once again changed to the new mode.  */
4251                 propagate_for_debug (first, last, reg, tempreg,
4252                                      this_basic_block);
4253                 adjust_reg_mode (reg, new_mode);
4254                 propagate_for_debug (first, last, tempreg,
4255                                      lowpart_subreg (old_mode, reg, new_mode),
4256                                      this_basic_block);
4257               }
4258           }
4259     }
4260
4261   /* If we will be able to accept this, we have made a
4262      change to the destination of I3.  This requires us to
4263      do a few adjustments.  */
4264
4265   if (changed_i3_dest)
4266     {
4267       PATTERN (i3) = newpat;
4268       adjust_for_new_dest (i3);
4269     }
4270
4271   /* We now know that we can do this combination.  Merge the insns and
4272      update the status of registers and LOG_LINKS.  */
4273
4274   if (undobuf.other_insn)
4275     {
4276       rtx note, next;
4277
4278       PATTERN (undobuf.other_insn) = other_pat;
4279
4280       /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4281          ensure that they are still valid.  Then add any non-duplicate
4282          notes added by recog_for_combine.  */
4283       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4284         {
4285           next = XEXP (note, 1);
4286
4287           if ((REG_NOTE_KIND (note) == REG_DEAD
4288                && !reg_referenced_p (XEXP (note, 0),
4289                                      PATTERN (undobuf.other_insn)))
4290               ||(REG_NOTE_KIND (note) == REG_UNUSED
4291                  && !reg_set_p (XEXP (note, 0),
4292                                 PATTERN (undobuf.other_insn)))
4293               /* Simply drop equal note since it may be no longer valid
4294                  for other_insn.  It may be possible to record that CC
4295                  register is changed and only discard those notes, but
4296                  in practice it's unnecessary complication and doesn't
4297                  give any meaningful improvement.
4298
4299                  See PR78559.  */
4300               || REG_NOTE_KIND (note) == REG_EQUAL
4301               || REG_NOTE_KIND (note) == REG_EQUIV)
4302             remove_note (undobuf.other_insn, note);
4303         }
4304
4305       distribute_notes  (new_other_notes, undobuf.other_insn,
4306                         undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4307                         NULL_RTX);
4308     }
4309
4310   if (swap_i2i3)
4311     {
4312       /* I3 now uses what used to be its destination and which is now
4313          I2's destination.  This requires us to do a few adjustments.  */
4314       PATTERN (i3) = newpat;
4315       adjust_for_new_dest (i3);
4316     }
4317
4318   if (swap_i2i3 || split_i2i3)
4319     {
4320       /* We might need a LOG_LINK from I3 to I2.  But then we used to
4321          have one, so we still will.
4322
4323          However, some later insn might be using I2's dest and have
4324          a LOG_LINK pointing at I3.  We should change it to point at
4325          I2 instead.  */
4326
4327       /* newi2pat is usually a SET here; however, recog_for_combine might
4328          have added some clobbers.  */
4329       rtx x = newi2pat;
4330       if (GET_CODE (x) == PARALLEL)
4331         x = XVECEXP (newi2pat, 0, 0);
4332
4333       /* It can only be a SET of a REG or of a SUBREG of a REG.  */
4334       unsigned int regno = reg_or_subregno (SET_DEST (x));
4335
4336       bool done = false;
4337       for (rtx_insn *insn = NEXT_INSN (i3);
4338            !done
4339            && insn
4340            && NONDEBUG_INSN_P (insn)
4341            && BLOCK_FOR_INSN (insn) == this_basic_block;
4342            insn = NEXT_INSN (insn))
4343         {
4344           struct insn_link *link;
4345           FOR_EACH_LOG_LINK (link, insn)
4346             if (link->insn == i3 && link->regno == regno)
4347               {
4348                 link->insn = i2;
4349                 done = true;
4350                 break;
4351               }
4352         }
4353     }
4354
4355   {
4356     rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4357     struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4358     rtx midnotes = 0;
4359     int from_luid;
4360     /* Compute which registers we expect to eliminate.  newi2pat may be setting
4361        either i3dest or i2dest, so we must check it.  */
4362     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4363                    || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4364                    || !i2dest_killed
4365                    ? 0 : i2dest);
4366     /* For i1, we need to compute both local elimination and global
4367        elimination information with respect to newi2pat because i1dest
4368        may be the same as i3dest, in which case newi2pat may be setting
4369        i1dest.  Global information is used when distributing REG_DEAD
4370        note for i2 and i3, in which case it does matter if newi2pat sets
4371        i1dest or not.
4372
4373        Local information is used when distributing REG_DEAD note for i1,
4374        in which case it doesn't matter if newi2pat sets i1dest or not.
4375        See PR62151, if we have four insns combination:
4376            i0: r0 <- i0src
4377            i1: r1 <- i1src (using r0)
4378                      REG_DEAD (r0)
4379            i2: r0 <- i2src (using r1)
4380            i3: r3 <- i3src (using r0)
4381            ix: using r0
4382        From i1's point of view, r0 is eliminated, no matter if it is set
4383        by newi2pat or not.  In other words, REG_DEAD info for r0 in i1
4384        should be discarded.
4385
4386        Note local information only affects cases in forms like "I1->I2->I3",
4387        "I0->I1->I2->I3" or "I0&I1->I2, I2->I3".  For other cases like
4388        "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4389        i0dest anyway.  */
4390     rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4391                          || !i1dest_killed
4392                          ? 0 : i1dest);
4393     rtx elim_i1 = (local_elim_i1 == 0
4394                    || (newi2pat && reg_set_p (i1dest, newi2pat))
4395                    ? 0 : i1dest);
4396     /* Same case as i1.  */
4397     rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4398                          ? 0 : i0dest);
4399     rtx elim_i0 = (local_elim_i0 == 0
4400                    || (newi2pat && reg_set_p (i0dest, newi2pat))
4401                    ? 0 : i0dest);
4402
4403     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4404        clear them.  */
4405     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4406     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4407     if (i1)
4408       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4409     if (i0)
4410       i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4411
4412     /* Ensure that we do not have something that should not be shared but
4413        occurs multiple times in the new insns.  Check this by first
4414        resetting all the `used' flags and then copying anything is shared.  */
4415
4416     reset_used_flags (i3notes);
4417     reset_used_flags (i2notes);
4418     reset_used_flags (i1notes);
4419     reset_used_flags (i0notes);
4420     reset_used_flags (newpat);
4421     reset_used_flags (newi2pat);
4422     if (undobuf.other_insn)
4423       reset_used_flags (PATTERN (undobuf.other_insn));
4424
4425     i3notes = copy_rtx_if_shared (i3notes);
4426     i2notes = copy_rtx_if_shared (i2notes);
4427     i1notes = copy_rtx_if_shared (i1notes);
4428     i0notes = copy_rtx_if_shared (i0notes);
4429     newpat = copy_rtx_if_shared (newpat);
4430     newi2pat = copy_rtx_if_shared (newi2pat);
4431     if (undobuf.other_insn)
4432       reset_used_flags (PATTERN (undobuf.other_insn));
4433
4434     INSN_CODE (i3) = insn_code_number;
4435     PATTERN (i3) = newpat;
4436
4437     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4438       {
4439         for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4440              link = XEXP (link, 1))
4441           {
4442             if (substed_i2)
4443               {
4444                 /* I2SRC must still be meaningful at this point.  Some
4445                    splitting operations can invalidate I2SRC, but those
4446                    operations do not apply to calls.  */
4447                 gcc_assert (i2src);
4448                 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4449                                                        i2dest, i2src);
4450               }
4451             if (substed_i1)
4452               XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4453                                                      i1dest, i1src);
4454             if (substed_i0)
4455               XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4456                                                      i0dest, i0src);
4457           }
4458       }
4459
4460     if (undobuf.other_insn)
4461       INSN_CODE (undobuf.other_insn) = other_code_number;
4462
4463     /* We had one special case above where I2 had more than one set and
4464        we replaced a destination of one of those sets with the destination
4465        of I3.  In that case, we have to update LOG_LINKS of insns later
4466        in this basic block.  Note that this (expensive) case is rare.
4467
4468        Also, in this case, we must pretend that all REG_NOTEs for I2
4469        actually came from I3, so that REG_UNUSED notes from I2 will be
4470        properly handled.  */
4471
4472     if (i3_subst_into_i2)
4473       {
4474         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4475           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4476                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4477               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4478               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4479               && ! find_reg_note (i2, REG_UNUSED,
4480                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4481             for (temp_insn = NEXT_INSN (i2);
4482                  temp_insn
4483                  && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4484                      || BB_HEAD (this_basic_block) != temp_insn);
4485                  temp_insn = NEXT_INSN (temp_insn))
4486               if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4487                 FOR_EACH_LOG_LINK (link, temp_insn)
4488                   if (link->insn == i2)
4489                     link->insn = i3;
4490
4491         if (i3notes)
4492           {
4493             rtx link = i3notes;
4494             while (XEXP (link, 1))
4495               link = XEXP (link, 1);
4496             XEXP (link, 1) = i2notes;
4497           }
4498         else
4499           i3notes = i2notes;
4500         i2notes = 0;
4501       }
4502
4503     LOG_LINKS (i3) = NULL;
4504     REG_NOTES (i3) = 0;
4505     LOG_LINKS (i2) = NULL;
4506     REG_NOTES (i2) = 0;
4507
4508     if (newi2pat)
4509       {
4510         if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4511           propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4512                                this_basic_block);
4513         INSN_CODE (i2) = i2_code_number;
4514         PATTERN (i2) = newi2pat;
4515       }
4516     else
4517       {
4518         if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4519           propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4520                                this_basic_block);
4521         SET_INSN_DELETED (i2);
4522       }
4523
4524     if (i1)
4525       {
4526         LOG_LINKS (i1) = NULL;
4527         REG_NOTES (i1) = 0;
4528         if (MAY_HAVE_DEBUG_BIND_INSNS)
4529           propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4530                                this_basic_block);
4531         SET_INSN_DELETED (i1);
4532       }
4533
4534     if (i0)
4535       {
4536         LOG_LINKS (i0) = NULL;
4537         REG_NOTES (i0) = 0;
4538         if (MAY_HAVE_DEBUG_BIND_INSNS)
4539           propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4540                                this_basic_block);
4541         SET_INSN_DELETED (i0);
4542       }
4543
4544     /* Get death notes for everything that is now used in either I3 or
4545        I2 and used to die in a previous insn.  If we built two new
4546        patterns, move from I1 to I2 then I2 to I3 so that we get the
4547        proper movement on registers that I2 modifies.  */
4548
4549     if (i0)
4550       from_luid = DF_INSN_LUID (i0);
4551     else if (i1)
4552       from_luid = DF_INSN_LUID (i1);
4553     else
4554       from_luid = DF_INSN_LUID (i2);
4555     if (newi2pat)
4556       move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4557     move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4558
4559     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
4560     if (i3notes)
4561       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4562                         elim_i2, elim_i1, elim_i0);
4563     if (i2notes)
4564       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4565                         elim_i2, elim_i1, elim_i0);
4566     if (i1notes)
4567       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4568                         elim_i2, local_elim_i1, local_elim_i0);
4569     if (i0notes)
4570       distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4571                         elim_i2, elim_i1, local_elim_i0);
4572     if (midnotes)
4573       distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4574                         elim_i2, elim_i1, elim_i0);
4575
4576     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
4577        know these are REG_UNUSED and want them to go to the desired insn,
4578        so we always pass it as i3.  */
4579
4580     if (newi2pat && new_i2_notes)
4581       distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4582                         NULL_RTX);
4583
4584     if (new_i3_notes)
4585       distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4586                         NULL_RTX);
4587
4588     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
4589        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
4590        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
4591        in that case, it might delete I2.  Similarly for I2 and I1.
4592        Show an additional death due to the REG_DEAD note we make here.  If
4593        we discard it in distribute_notes, we will decrement it again.  */
4594
4595     if (i3dest_killed)
4596       {
4597         rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4598         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4599           distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4600                             elim_i1, elim_i0);
4601         else
4602           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4603                             elim_i2, elim_i1, elim_i0);
4604       }
4605
4606     if (i2dest_in_i2src)
4607       {
4608         rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4609         if (newi2pat && reg_set_p (i2dest, newi2pat))
4610           distribute_notes (new_note,  NULL, i2, NULL, NULL_RTX,
4611                             NULL_RTX, NULL_RTX);
4612         else
4613           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4614                             NULL_RTX, NULL_RTX, NULL_RTX);
4615       }
4616
4617     if (i1dest_in_i1src)
4618       {
4619         rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4620         if (newi2pat && reg_set_p (i1dest, newi2pat))
4621           distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4622                             NULL_RTX, NULL_RTX);
4623         else
4624           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4625                             NULL_RTX, NULL_RTX, NULL_RTX);
4626       }
4627
4628     if (i0dest_in_i0src)
4629       {
4630         rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4631         if (newi2pat && reg_set_p (i0dest, newi2pat))
4632           distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4633                             NULL_RTX, NULL_RTX);
4634         else
4635           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4636                             NULL_RTX, NULL_RTX, NULL_RTX);
4637       }
4638
4639     distribute_links (i3links);
4640     distribute_links (i2links);
4641     distribute_links (i1links);
4642     distribute_links (i0links);
4643
4644     if (REG_P (i2dest))
4645       {
4646         struct insn_link *link;
4647         rtx_insn *i2_insn = 0;
4648         rtx i2_val = 0, set;
4649
4650         /* The insn that used to set this register doesn't exist, and
4651            this life of the register may not exist either.  See if one of
4652            I3's links points to an insn that sets I2DEST.  If it does,
4653            that is now the last known value for I2DEST. If we don't update
4654            this and I2 set the register to a value that depended on its old
4655            contents, we will get confused.  If this insn is used, thing
4656            will be set correctly in combine_instructions.  */
4657         FOR_EACH_LOG_LINK (link, i3)
4658           if ((set = single_set (link->insn)) != 0
4659               && rtx_equal_p (i2dest, SET_DEST (set)))
4660             i2_insn = link->insn, i2_val = SET_SRC (set);
4661
4662         record_value_for_reg (i2dest, i2_insn, i2_val);
4663
4664         /* If the reg formerly set in I2 died only once and that was in I3,
4665            zero its use count so it won't make `reload' do any work.  */
4666         if (! added_sets_2
4667             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4668             && ! i2dest_in_i2src
4669             && REGNO (i2dest) < reg_n_sets_max)
4670           INC_REG_N_SETS (REGNO (i2dest), -1);
4671       }
4672
4673     if (i1 && REG_P (i1dest))
4674       {
4675         struct insn_link *link;
4676         rtx_insn *i1_insn = 0;
4677         rtx i1_val = 0, set;
4678
4679         FOR_EACH_LOG_LINK (link, i3)
4680           if ((set = single_set (link->insn)) != 0
4681               && rtx_equal_p (i1dest, SET_DEST (set)))
4682             i1_insn = link->insn, i1_val = SET_SRC (set);
4683
4684         record_value_for_reg (i1dest, i1_insn, i1_val);
4685
4686         if (! added_sets_1
4687             && ! i1dest_in_i1src
4688             && REGNO (i1dest) < reg_n_sets_max)
4689           INC_REG_N_SETS (REGNO (i1dest), -1);
4690       }
4691
4692     if (i0 && REG_P (i0dest))
4693       {
4694         struct insn_link *link;
4695         rtx_insn *i0_insn = 0;
4696         rtx i0_val = 0, set;
4697
4698         FOR_EACH_LOG_LINK (link, i3)
4699           if ((set = single_set (link->insn)) != 0
4700               && rtx_equal_p (i0dest, SET_DEST (set)))
4701             i0_insn = link->insn, i0_val = SET_SRC (set);
4702
4703         record_value_for_reg (i0dest, i0_insn, i0_val);
4704
4705         if (! added_sets_0
4706             && ! i0dest_in_i0src
4707             && REGNO (i0dest) < reg_n_sets_max)
4708           INC_REG_N_SETS (REGNO (i0dest), -1);
4709       }
4710
4711     /* Update reg_stat[].nonzero_bits et al for any changes that may have
4712        been made to this insn.  The order is important, because newi2pat
4713        can affect nonzero_bits of newpat.  */
4714     if (newi2pat)
4715       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4716     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4717   }
4718
4719   if (undobuf.other_insn != NULL_RTX)
4720     {
4721       if (dump_file)
4722         {
4723           fprintf (dump_file, "modifying other_insn ");
4724           dump_insn_slim (dump_file, undobuf.other_insn);
4725         }
4726       df_insn_rescan (undobuf.other_insn);
4727     }
4728
4729   if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4730     {
4731       if (dump_file)
4732         {
4733           fprintf (dump_file, "modifying insn i0 ");
4734           dump_insn_slim (dump_file, i0);
4735         }
4736       df_insn_rescan (i0);
4737     }
4738
4739   if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4740     {
4741       if (dump_file)
4742         {
4743           fprintf (dump_file, "modifying insn i1 ");
4744           dump_insn_slim (dump_file, i1);
4745         }
4746       df_insn_rescan (i1);
4747     }
4748
4749   if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4750     {
4751       if (dump_file)
4752         {
4753           fprintf (dump_file, "modifying insn i2 ");
4754           dump_insn_slim (dump_file, i2);
4755         }
4756       df_insn_rescan (i2);
4757     }
4758
4759   if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4760     {
4761       if (dump_file)
4762         {
4763           fprintf (dump_file, "modifying insn i3 ");
4764           dump_insn_slim (dump_file, i3);
4765         }
4766       df_insn_rescan (i3);
4767     }
4768
4769   /* Set new_direct_jump_p if a new return or simple jump instruction
4770      has been created.  Adjust the CFG accordingly.  */
4771   if (returnjump_p (i3) || any_uncondjump_p (i3))
4772     {
4773       *new_direct_jump_p = 1;
4774       mark_jump_label (PATTERN (i3), i3, 0);
4775       update_cfg_for_uncondjump (i3);
4776     }
4777
4778   if (undobuf.other_insn != NULL_RTX
4779       && (returnjump_p (undobuf.other_insn)
4780           || any_uncondjump_p (undobuf.other_insn)))
4781     {
4782       *new_direct_jump_p = 1;
4783       update_cfg_for_uncondjump (undobuf.other_insn);
4784     }
4785
4786   if (GET_CODE (PATTERN (i3)) == TRAP_IF
4787       && XEXP (PATTERN (i3), 0) == const1_rtx)
4788     {
4789       basic_block bb = BLOCK_FOR_INSN (i3);
4790       gcc_assert (bb);
4791       remove_edge (split_block (bb, i3));
4792       emit_barrier_after_bb (bb);
4793       *new_direct_jump_p = 1;
4794     }
4795
4796   if (undobuf.other_insn
4797       && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4798       && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4799     {
4800       basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4801       gcc_assert (bb);
4802       remove_edge (split_block (bb, undobuf.other_insn));
4803       emit_barrier_after_bb (bb);
4804       *new_direct_jump_p = 1;
4805     }
4806
4807   /* A noop might also need cleaning up of CFG, if it comes from the
4808      simplification of a jump.  */
4809   if (JUMP_P (i3)
4810       && GET_CODE (newpat) == SET
4811       && SET_SRC (newpat) == pc_rtx
4812       && SET_DEST (newpat) == pc_rtx)
4813     {
4814       *new_direct_jump_p = 1;
4815       update_cfg_for_uncondjump (i3);
4816     }
4817
4818   if (undobuf.other_insn != NULL_RTX
4819       && JUMP_P (undobuf.other_insn)
4820       && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4821       && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4822       && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4823     {
4824       *new_direct_jump_p = 1;
4825       update_cfg_for_uncondjump (undobuf.other_insn);
4826     }
4827
4828   combine_successes++;
4829   undo_commit ();
4830
4831   rtx_insn *ret = newi2pat ? i2 : i3;
4832   if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4833     ret = added_links_insn;
4834   if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4835     ret = added_notes_insn;
4836
4837   return ret;
4838 }
4839 \f
4840 /* Get a marker for undoing to the current state.  */
4841
4842 static void *
4843 get_undo_marker (void)
4844 {
4845   return undobuf.undos;
4846 }
4847
4848 /* Undo the modifications up to the marker.  */
4849
4850 static void
4851 undo_to_marker (void *marker)
4852 {
4853   struct undo *undo, *next;
4854
4855   for (undo = undobuf.undos; undo != marker; undo = next)
4856     {
4857       gcc_assert (undo);
4858
4859       next = undo->next;
4860       switch (undo->kind)
4861         {
4862         case UNDO_RTX:
4863           *undo->where.r = undo->old_contents.r;
4864           break;
4865         case UNDO_INT:
4866           *undo->where.i = undo->old_contents.i;
4867           break;
4868         case UNDO_MODE:
4869           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4870           break;
4871         case UNDO_LINKS:
4872           *undo->where.l = undo->old_contents.l;
4873           break;
4874         default:
4875           gcc_unreachable ();
4876         }
4877
4878       undo->next = undobuf.frees;
4879       undobuf.frees = undo;
4880     }
4881
4882   undobuf.undos = (struct undo *) marker;
4883 }
4884
4885 /* Undo all the modifications recorded in undobuf.  */
4886
4887 static void
4888 undo_all (void)
4889 {
4890   undo_to_marker (0);
4891 }
4892
4893 /* We've committed to accepting the changes we made.  Move all
4894    of the undos to the free list.  */
4895
4896 static void
4897 undo_commit (void)
4898 {
4899   struct undo *undo, *next;
4900
4901   for (undo = undobuf.undos; undo; undo = next)
4902     {
4903       next = undo->next;
4904       undo->next = undobuf.frees;
4905       undobuf.frees = undo;
4906     }
4907   undobuf.undos = 0;
4908 }
4909 \f
4910 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4911    where we have an arithmetic expression and return that point.  LOC will
4912    be inside INSN.
4913
4914    try_combine will call this function to see if an insn can be split into
4915    two insns.  */
4916
4917 static rtx *
4918 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4919 {
4920   rtx x = *loc;
4921   enum rtx_code code = GET_CODE (x);
4922   rtx *split;
4923   unsigned HOST_WIDE_INT len = 0;
4924   HOST_WIDE_INT pos = 0;
4925   int unsignedp = 0;
4926   rtx inner = NULL_RTX;
4927   scalar_int_mode mode, inner_mode;
4928
4929   /* First special-case some codes.  */
4930   switch (code)
4931     {
4932     case SUBREG:
4933 #ifdef INSN_SCHEDULING
4934       /* If we are making a paradoxical SUBREG invalid, it becomes a split
4935          point.  */
4936       if (MEM_P (SUBREG_REG (x)))
4937         return loc;
4938 #endif
4939       return find_split_point (&SUBREG_REG (x), insn, false);
4940
4941     case MEM:
4942       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4943          using LO_SUM and HIGH.  */
4944       if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4945                           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4946         {
4947           machine_mode address_mode = get_address_mode (x);
4948
4949           SUBST (XEXP (x, 0),
4950                  gen_rtx_LO_SUM (address_mode,
4951                                  gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4952                                  XEXP (x, 0)));
4953           return &XEXP (XEXP (x, 0), 0);
4954         }
4955
4956       /* If we have a PLUS whose second operand is a constant and the
4957          address is not valid, perhaps we can split it up using
4958          the machine-specific way to split large constants.  We use
4959          the first pseudo-reg (one of the virtual regs) as a placeholder;
4960          it will not remain in the result.  */
4961       if (GET_CODE (XEXP (x, 0)) == PLUS
4962           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4963           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4964                                             MEM_ADDR_SPACE (x)))
4965         {
4966           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4967           rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4968                                                subst_insn);
4969
4970           /* This should have produced two insns, each of which sets our
4971              placeholder.  If the source of the second is a valid address,
4972              we can put both sources together and make a split point
4973              in the middle.  */
4974
4975           if (seq
4976               && NEXT_INSN (seq) != NULL_RTX
4977               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4978               && NONJUMP_INSN_P (seq)
4979               && GET_CODE (PATTERN (seq)) == SET
4980               && SET_DEST (PATTERN (seq)) == reg
4981               && ! reg_mentioned_p (reg,
4982                                     SET_SRC (PATTERN (seq)))
4983               && NONJUMP_INSN_P (NEXT_INSN (seq))
4984               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4985               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4986               && memory_address_addr_space_p
4987                    (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4988                     MEM_ADDR_SPACE (x)))
4989             {
4990               rtx src1 = SET_SRC (PATTERN (seq));
4991               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4992
4993               /* Replace the placeholder in SRC2 with SRC1.  If we can
4994                  find where in SRC2 it was placed, that can become our
4995                  split point and we can replace this address with SRC2.
4996                  Just try two obvious places.  */
4997
4998               src2 = replace_rtx (src2, reg, src1);
4999               split = 0;
5000               if (XEXP (src2, 0) == src1)
5001                 split = &XEXP (src2, 0);
5002               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
5003                        && XEXP (XEXP (src2, 0), 0) == src1)
5004                 split = &XEXP (XEXP (src2, 0), 0);
5005
5006               if (split)
5007                 {
5008                   SUBST (XEXP (x, 0), src2);
5009                   return split;
5010                 }
5011             }
5012
5013           /* If that didn't work and we have a nested plus, like:
5014              ((REG1 * CONST1) + REG2) + CONST2 and (REG1 + REG2) + CONST2
5015              is valid address, try to split (REG1 * CONST1).  */
5016           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5017               && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5018               && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5019               && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SUBREG
5020                     && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5021                                                          0), 0)))))
5022             {
5023               rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 0);
5024               XEXP (XEXP (XEXP (x, 0), 0), 0) = reg;
5025               if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5026                                                MEM_ADDR_SPACE (x)))
5027                 {
5028                   XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5029                   return &XEXP (XEXP (XEXP (x, 0), 0), 0);
5030                 }
5031               XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5032             }
5033           else if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5034                    && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5035                    && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5036                    && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SUBREG
5037                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5038                                                               0), 1)))))
5039             {
5040               rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 1);
5041               XEXP (XEXP (XEXP (x, 0), 0), 1) = reg;
5042               if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5043                                                MEM_ADDR_SPACE (x)))
5044                 {
5045                   XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5046                   return &XEXP (XEXP (XEXP (x, 0), 0), 1);
5047                 }
5048               XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5049             }
5050
5051           /* If that didn't work, perhaps the first operand is complex and
5052              needs to be computed separately, so make a split point there.
5053              This will occur on machines that just support REG + CONST
5054              and have a constant moved through some previous computation.  */
5055           if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
5056               && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5057                     && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5058             return &XEXP (XEXP (x, 0), 0);
5059         }
5060
5061       /* If we have a PLUS whose first operand is complex, try computing it
5062          separately by making a split there.  */
5063       if (GET_CODE (XEXP (x, 0)) == PLUS
5064           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5065                                             MEM_ADDR_SPACE (x))
5066           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
5067           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5068                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5069         return &XEXP (XEXP (x, 0), 0);
5070       break;
5071
5072     case SET:
5073       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
5074          ZERO_EXTRACT, the most likely reason why this doesn't match is that
5075          we need to put the operand into a register.  So split at that
5076          point.  */
5077
5078       if (SET_DEST (x) == cc0_rtx
5079           && GET_CODE (SET_SRC (x)) != COMPARE
5080           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
5081           && !OBJECT_P (SET_SRC (x))
5082           && ! (GET_CODE (SET_SRC (x)) == SUBREG
5083                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
5084         return &SET_SRC (x);
5085
5086       /* See if we can split SET_SRC as it stands.  */
5087       split = find_split_point (&SET_SRC (x), insn, true);
5088       if (split && split != &SET_SRC (x))
5089         return split;
5090
5091       /* See if we can split SET_DEST as it stands.  */
5092       split = find_split_point (&SET_DEST (x), insn, false);
5093       if (split && split != &SET_DEST (x))
5094         return split;
5095
5096       /* See if this is a bitfield assignment with everything constant.  If
5097          so, this is an IOR of an AND, so split it into that.  */
5098       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5099           && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5100                                      &inner_mode)
5101           && HWI_COMPUTABLE_MODE_P (inner_mode)
5102           && CONST_INT_P (XEXP (SET_DEST (x), 1))
5103           && CONST_INT_P (XEXP (SET_DEST (x), 2))
5104           && CONST_INT_P (SET_SRC (x))
5105           && ((INTVAL (XEXP (SET_DEST (x), 1))
5106                + INTVAL (XEXP (SET_DEST (x), 2)))
5107               <= GET_MODE_PRECISION (inner_mode))
5108           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5109         {
5110           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5111           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5112           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
5113           rtx dest = XEXP (SET_DEST (x), 0);
5114           unsigned HOST_WIDE_INT mask
5115             = (HOST_WIDE_INT_1U << len) - 1;
5116           rtx or_mask;
5117
5118           if (BITS_BIG_ENDIAN)
5119             pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5120
5121           or_mask = gen_int_mode (src << pos, inner_mode);
5122           if (src == mask)
5123             SUBST (SET_SRC (x),
5124                    simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5125           else
5126             {
5127               rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5128               SUBST (SET_SRC (x),
5129                      simplify_gen_binary (IOR, inner_mode,
5130                                           simplify_gen_binary (AND, inner_mode,
5131                                                                dest, negmask),
5132                                           or_mask));
5133             }
5134
5135           SUBST (SET_DEST (x), dest);
5136
5137           split = find_split_point (&SET_SRC (x), insn, true);
5138           if (split && split != &SET_SRC (x))
5139             return split;
5140         }
5141
5142       /* Otherwise, see if this is an operation that we can split into two.
5143          If so, try to split that.  */
5144       code = GET_CODE (SET_SRC (x));
5145
5146       switch (code)
5147         {
5148         case AND:
5149           /* If we are AND'ing with a large constant that is only a single
5150              bit and the result is only being used in a context where we
5151              need to know if it is zero or nonzero, replace it with a bit
5152              extraction.  This will avoid the large constant, which might
5153              have taken more than one insn to make.  If the constant were
5154              not a valid argument to the AND but took only one insn to make,
5155              this is no worse, but if it took more than one insn, it will
5156              be better.  */
5157
5158           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5159               && REG_P (XEXP (SET_SRC (x), 0))
5160               && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5161               && REG_P (SET_DEST (x))
5162               && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5163               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5164               && XEXP (*split, 0) == SET_DEST (x)
5165               && XEXP (*split, 1) == const0_rtx)
5166             {
5167               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5168                                                 XEXP (SET_SRC (x), 0),
5169                                                 pos, NULL_RTX, 1, 1, 0, 0);
5170               if (extraction != 0)
5171                 {
5172                   SUBST (SET_SRC (x), extraction);
5173                   return find_split_point (loc, insn, false);
5174                 }
5175             }
5176           break;
5177
5178         case NE:
5179           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5180              is known to be on, this can be converted into a NEG of a shift.  */
5181           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5182               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5183               && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5184                                                    GET_MODE (XEXP (SET_SRC (x),
5185                                                              0))))) >= 1))
5186             {
5187               machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5188               rtx pos_rtx = gen_int_shift_amount (mode, pos);
5189               SUBST (SET_SRC (x),
5190                      gen_rtx_NEG (mode,
5191                                   gen_rtx_LSHIFTRT (mode,
5192                                                     XEXP (SET_SRC (x), 0),
5193                                                     pos_rtx)));
5194
5195               split = find_split_point (&SET_SRC (x), insn, true);
5196               if (split && split != &SET_SRC (x))
5197                 return split;
5198             }
5199           break;
5200
5201         case SIGN_EXTEND:
5202           inner = XEXP (SET_SRC (x), 0);
5203
5204           /* We can't optimize if either mode is a partial integer
5205              mode as we don't know how many bits are significant
5206              in those modes.  */
5207           if (!is_int_mode (GET_MODE (inner), &inner_mode)
5208               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5209             break;
5210
5211           pos = 0;
5212           len = GET_MODE_PRECISION (inner_mode);
5213           unsignedp = 0;
5214           break;
5215
5216         case SIGN_EXTRACT:
5217         case ZERO_EXTRACT:
5218           if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5219                                       &inner_mode)
5220               && CONST_INT_P (XEXP (SET_SRC (x), 1))
5221               && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5222             {
5223               inner = XEXP (SET_SRC (x), 0);
5224               len = INTVAL (XEXP (SET_SRC (x), 1));
5225               pos = INTVAL (XEXP (SET_SRC (x), 2));
5226
5227               if (BITS_BIG_ENDIAN)
5228                 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5229               unsignedp = (code == ZERO_EXTRACT);
5230             }
5231           break;
5232
5233         default:
5234           break;
5235         }
5236
5237       if (len
5238           && known_subrange_p (pos, len,
5239                                0, GET_MODE_PRECISION (GET_MODE (inner)))
5240           && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5241         {
5242           /* For unsigned, we have a choice of a shift followed by an
5243              AND or two shifts.  Use two shifts for field sizes where the
5244              constant might be too large.  We assume here that we can
5245              always at least get 8-bit constants in an AND insn, which is
5246              true for every current RISC.  */
5247
5248           if (unsignedp && len <= 8)
5249             {
5250               unsigned HOST_WIDE_INT mask
5251                 = (HOST_WIDE_INT_1U << len) - 1;
5252               rtx pos_rtx = gen_int_shift_amount (mode, pos);
5253               SUBST (SET_SRC (x),
5254                      gen_rtx_AND (mode,
5255                                   gen_rtx_LSHIFTRT
5256                                   (mode, gen_lowpart (mode, inner), pos_rtx),
5257                                   gen_int_mode (mask, mode)));
5258
5259               split = find_split_point (&SET_SRC (x), insn, true);
5260               if (split && split != &SET_SRC (x))
5261                 return split;
5262             }
5263           else
5264             {
5265               int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5266               int right_bits = GET_MODE_PRECISION (mode) - len;
5267               SUBST (SET_SRC (x),
5268                      gen_rtx_fmt_ee
5269                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5270                       gen_rtx_ASHIFT (mode,
5271                                       gen_lowpart (mode, inner),
5272                                       gen_int_shift_amount (mode, left_bits)),
5273                       gen_int_shift_amount (mode, right_bits)));
5274
5275               split = find_split_point (&SET_SRC (x), insn, true);
5276               if (split && split != &SET_SRC (x))
5277                 return split;
5278             }
5279         }
5280
5281       /* See if this is a simple operation with a constant as the second
5282          operand.  It might be that this constant is out of range and hence
5283          could be used as a split point.  */
5284       if (BINARY_P (SET_SRC (x))
5285           && CONSTANT_P (XEXP (SET_SRC (x), 1))
5286           && (OBJECT_P (XEXP (SET_SRC (x), 0))
5287               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5288                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5289         return &XEXP (SET_SRC (x), 1);
5290
5291       /* Finally, see if this is a simple operation with its first operand
5292          not in a register.  The operation might require this operand in a
5293          register, so return it as a split point.  We can always do this
5294          because if the first operand were another operation, we would have
5295          already found it as a split point.  */
5296       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5297           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5298         return &XEXP (SET_SRC (x), 0);
5299
5300       return 0;
5301
5302     case AND:
5303     case IOR:
5304       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5305          it is better to write this as (not (ior A B)) so we can split it.
5306          Similarly for IOR.  */
5307       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5308         {
5309           SUBST (*loc,
5310                  gen_rtx_NOT (GET_MODE (x),
5311                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5312                                               GET_MODE (x),
5313                                               XEXP (XEXP (x, 0), 0),
5314                                               XEXP (XEXP (x, 1), 0))));
5315           return find_split_point (loc, insn, set_src);
5316         }
5317
5318       /* Many RISC machines have a large set of logical insns.  If the
5319          second operand is a NOT, put it first so we will try to split the
5320          other operand first.  */
5321       if (GET_CODE (XEXP (x, 1)) == NOT)
5322         {
5323           rtx tem = XEXP (x, 0);
5324           SUBST (XEXP (x, 0), XEXP (x, 1));
5325           SUBST (XEXP (x, 1), tem);
5326         }
5327       break;
5328
5329     case PLUS:
5330     case MINUS:
5331       /* Canonicalization can produce (minus A (mult B C)), where C is a
5332          constant.  It may be better to try splitting (plus (mult B -C) A)
5333          instead if this isn't a multiply by a power of two.  */
5334       if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5335           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5336           && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5337         {
5338           machine_mode mode = GET_MODE (x);
5339           unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5340           HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5341           SUBST (*loc, gen_rtx_PLUS (mode,
5342                                      gen_rtx_MULT (mode,
5343                                                    XEXP (XEXP (x, 1), 0),
5344                                                    gen_int_mode (other_int,
5345                                                                  mode)),
5346                                      XEXP (x, 0)));
5347           return find_split_point (loc, insn, set_src);
5348         }
5349
5350       /* Split at a multiply-accumulate instruction.  However if this is
5351          the SET_SRC, we likely do not have such an instruction and it's
5352          worthless to try this split.  */
5353       if (!set_src
5354           && (GET_CODE (XEXP (x, 0)) == MULT
5355               || (GET_CODE (XEXP (x, 0)) == ASHIFT
5356                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5357         return loc;
5358
5359     default:
5360       break;
5361     }
5362
5363   /* Otherwise, select our actions depending on our rtx class.  */
5364   switch (GET_RTX_CLASS (code))
5365     {
5366     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
5367     case RTX_TERNARY:
5368       split = find_split_point (&XEXP (x, 2), insn, false);
5369       if (split)
5370         return split;
5371       /* fall through */
5372     case RTX_BIN_ARITH:
5373     case RTX_COMM_ARITH:
5374     case RTX_COMPARE:
5375     case RTX_COMM_COMPARE:
5376       split = find_split_point (&XEXP (x, 1), insn, false);
5377       if (split)
5378         return split;
5379       /* fall through */
5380     case RTX_UNARY:
5381       /* Some machines have (and (shift ...) ...) insns.  If X is not
5382          an AND, but XEXP (X, 0) is, use it as our split point.  */
5383       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5384         return &XEXP (x, 0);
5385
5386       split = find_split_point (&XEXP (x, 0), insn, false);
5387       if (split)
5388         return split;
5389       return loc;
5390
5391     default:
5392       /* Otherwise, we don't have a split point.  */
5393       return 0;
5394     }
5395 }
5396 \f
5397 /* Throughout X, replace FROM with TO, and return the result.
5398    The result is TO if X is FROM;
5399    otherwise the result is X, but its contents may have been modified.
5400    If they were modified, a record was made in undobuf so that
5401    undo_all will (among other things) return X to its original state.
5402
5403    If the number of changes necessary is too much to record to undo,
5404    the excess changes are not made, so the result is invalid.
5405    The changes already made can still be undone.
5406    undobuf.num_undo is incremented for such changes, so by testing that
5407    the caller can tell whether the result is valid.
5408
5409    `n_occurrences' is incremented each time FROM is replaced.
5410
5411    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5412
5413    IN_COND is nonzero if we are at the top level of a condition.
5414
5415    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
5416    by copying if `n_occurrences' is nonzero.  */
5417
5418 static rtx
5419 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5420 {
5421   enum rtx_code code = GET_CODE (x);
5422   machine_mode op0_mode = VOIDmode;
5423   const char *fmt;
5424   int len, i;
5425   rtx new_rtx;
5426
5427 /* Two expressions are equal if they are identical copies of a shared
5428    RTX or if they are both registers with the same register number
5429    and mode.  */
5430
5431 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
5432   ((X) == (Y)                                           \
5433    || (REG_P (X) && REG_P (Y)   \
5434        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5435
5436   /* Do not substitute into clobbers of regs -- this will never result in
5437      valid RTL.  */
5438   if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5439     return x;
5440
5441   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5442     {
5443       n_occurrences++;
5444       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5445     }
5446
5447   /* If X and FROM are the same register but different modes, they
5448      will not have been seen as equal above.  However, the log links code
5449      will make a LOG_LINKS entry for that case.  If we do nothing, we
5450      will try to rerecognize our original insn and, when it succeeds,
5451      we will delete the feeding insn, which is incorrect.
5452
5453      So force this insn not to match in this (rare) case.  */
5454   if (! in_dest && code == REG && REG_P (from)
5455       && reg_overlap_mentioned_p (x, from))
5456     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5457
5458   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5459      of which may contain things that can be combined.  */
5460   if (code != MEM && code != LO_SUM && OBJECT_P (x))
5461     return x;
5462
5463   /* It is possible to have a subexpression appear twice in the insn.
5464      Suppose that FROM is a register that appears within TO.
5465      Then, after that subexpression has been scanned once by `subst',
5466      the second time it is scanned, TO may be found.  If we were
5467      to scan TO here, we would find FROM within it and create a
5468      self-referent rtl structure which is completely wrong.  */
5469   if (COMBINE_RTX_EQUAL_P (x, to))
5470     return to;
5471
5472   /* Parallel asm_operands need special attention because all of the
5473      inputs are shared across the arms.  Furthermore, unsharing the
5474      rtl results in recognition failures.  Failure to handle this case
5475      specially can result in circular rtl.
5476
5477      Solve this by doing a normal pass across the first entry of the
5478      parallel, and only processing the SET_DESTs of the subsequent
5479      entries.  Ug.  */
5480
5481   if (code == PARALLEL
5482       && GET_CODE (XVECEXP (x, 0, 0)) == SET
5483       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5484     {
5485       new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5486
5487       /* If this substitution failed, this whole thing fails.  */
5488       if (GET_CODE (new_rtx) == CLOBBER
5489           && XEXP (new_rtx, 0) == const0_rtx)
5490         return new_rtx;
5491
5492       SUBST (XVECEXP (x, 0, 0), new_rtx);
5493
5494       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5495         {
5496           rtx dest = SET_DEST (XVECEXP (x, 0, i));
5497
5498           if (!REG_P (dest)
5499               && GET_CODE (dest) != CC0
5500               && GET_CODE (dest) != PC)
5501             {
5502               new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5503
5504               /* If this substitution failed, this whole thing fails.  */
5505               if (GET_CODE (new_rtx) == CLOBBER
5506                   && XEXP (new_rtx, 0) == const0_rtx)
5507                 return new_rtx;
5508
5509               SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5510             }
5511         }
5512     }
5513   else
5514     {
5515       len = GET_RTX_LENGTH (code);
5516       fmt = GET_RTX_FORMAT (code);
5517
5518       /* We don't need to process a SET_DEST that is a register, CC0,
5519          or PC, so set up to skip this common case.  All other cases
5520          where we want to suppress replacing something inside a
5521          SET_SRC are handled via the IN_DEST operand.  */
5522       if (code == SET
5523           && (REG_P (SET_DEST (x))
5524               || GET_CODE (SET_DEST (x)) == CC0
5525               || GET_CODE (SET_DEST (x)) == PC))
5526         fmt = "ie";
5527
5528       /* Trying to simplify the operands of a widening MULT is not likely
5529          to create RTL matching a machine insn.  */
5530       if (code == MULT
5531           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5532               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5533           && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5534               || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5535           && REG_P (XEXP (XEXP (x, 0), 0))
5536           && REG_P (XEXP (XEXP (x, 1), 0))
5537           && from == to)
5538         return x;
5539
5540
5541       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5542          constant.  */
5543       if (fmt[0] == 'e')
5544         op0_mode = GET_MODE (XEXP (x, 0));
5545
5546       for (i = 0; i < len; i++)
5547         {
5548           if (fmt[i] == 'E')
5549             {
5550               int j;
5551               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5552                 {
5553                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5554                     {
5555                       new_rtx = (unique_copy && n_occurrences
5556                              ? copy_rtx (to) : to);
5557                       n_occurrences++;
5558                     }
5559                   else
5560                     {
5561                       new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5562                                        unique_copy);
5563
5564                       /* If this substitution failed, this whole thing
5565                          fails.  */
5566                       if (GET_CODE (new_rtx) == CLOBBER
5567                           && XEXP (new_rtx, 0) == const0_rtx)
5568                         return new_rtx;
5569                     }
5570
5571                   SUBST (XVECEXP (x, i, j), new_rtx);
5572                 }
5573             }
5574           else if (fmt[i] == 'e')
5575             {
5576               /* If this is a register being set, ignore it.  */
5577               new_rtx = XEXP (x, i);
5578               if (in_dest
5579                   && i == 0
5580                   && (((code == SUBREG || code == ZERO_EXTRACT)
5581                        && REG_P (new_rtx))
5582                       || code == STRICT_LOW_PART))
5583                 ;
5584
5585               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5586                 {
5587                   /* In general, don't install a subreg involving two
5588                      modes not tieable.  It can worsen register
5589                      allocation, and can even make invalid reload
5590                      insns, since the reg inside may need to be copied
5591                      from in the outside mode, and that may be invalid
5592                      if it is an fp reg copied in integer mode.
5593
5594                      We allow two exceptions to this: It is valid if
5595                      it is inside another SUBREG and the mode of that
5596                      SUBREG and the mode of the inside of TO is
5597                      tieable and it is valid if X is a SET that copies
5598                      FROM to CC0.  */
5599
5600                   if (GET_CODE (to) == SUBREG
5601                       && !targetm.modes_tieable_p (GET_MODE (to),
5602                                                    GET_MODE (SUBREG_REG (to)))
5603                       && ! (code == SUBREG
5604                             && (targetm.modes_tieable_p
5605                                 (GET_MODE (x), GET_MODE (SUBREG_REG (to)))))
5606                       && (!HAVE_cc0
5607                           || (! (code == SET
5608                                  && i == 1
5609                                  && XEXP (x, 0) == cc0_rtx))))
5610                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5611
5612                   if (code == SUBREG
5613                       && REG_P (to)
5614                       && REGNO (to) < FIRST_PSEUDO_REGISTER
5615                       && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5616                                                 SUBREG_BYTE (x),
5617                                                 GET_MODE (x)) < 0)
5618                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5619
5620                   new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5621                   n_occurrences++;
5622                 }
5623               else
5624                 /* If we are in a SET_DEST, suppress most cases unless we
5625                    have gone inside a MEM, in which case we want to
5626                    simplify the address.  We assume here that things that
5627                    are actually part of the destination have their inner
5628                    parts in the first expression.  This is true for SUBREG,
5629                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5630                    things aside from REG and MEM that should appear in a
5631                    SET_DEST.  */
5632                 new_rtx = subst (XEXP (x, i), from, to,
5633                              (((in_dest
5634                                 && (code == SUBREG || code == STRICT_LOW_PART
5635                                     || code == ZERO_EXTRACT))
5636                                || code == SET)
5637                               && i == 0),
5638                                  code == IF_THEN_ELSE && i == 0,
5639                                  unique_copy);
5640
5641               /* If we found that we will have to reject this combination,
5642                  indicate that by returning the CLOBBER ourselves, rather than
5643                  an expression containing it.  This will speed things up as
5644                  well as prevent accidents where two CLOBBERs are considered
5645                  to be equal, thus producing an incorrect simplification.  */
5646
5647               if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5648                 return new_rtx;
5649
5650               if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5651                 {
5652                   machine_mode mode = GET_MODE (x);
5653
5654                   x = simplify_subreg (GET_MODE (x), new_rtx,
5655                                        GET_MODE (SUBREG_REG (x)),
5656                                        SUBREG_BYTE (x));
5657                   if (! x)
5658                     x = gen_rtx_CLOBBER (mode, const0_rtx);
5659                 }
5660               else if (CONST_SCALAR_INT_P (new_rtx)
5661                        && (GET_CODE (x) == ZERO_EXTEND
5662                            || GET_CODE (x) == FLOAT
5663                            || GET_CODE (x) == UNSIGNED_FLOAT))
5664                 {
5665                   x = simplify_unary_operation (GET_CODE (x), GET_MODE (x),
5666                                                 new_rtx,
5667                                                 GET_MODE (XEXP (x, 0)));
5668                   if (!x)
5669                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5670                 }
5671               else
5672                 SUBST (XEXP (x, i), new_rtx);
5673             }
5674         }
5675     }
5676
5677   /* Check if we are loading something from the constant pool via float
5678      extension; in this case we would undo compress_float_constant
5679      optimization and degenerate constant load to an immediate value.  */
5680   if (GET_CODE (x) == FLOAT_EXTEND
5681       && MEM_P (XEXP (x, 0))
5682       && MEM_READONLY_P (XEXP (x, 0)))
5683     {
5684       rtx tmp = avoid_constant_pool_reference (x);
5685       if (x != tmp)
5686         return x;
5687     }
5688
5689   /* Try to simplify X.  If the simplification changed the code, it is likely
5690      that further simplification will help, so loop, but limit the number
5691      of repetitions that will be performed.  */
5692
5693   for (i = 0; i < 4; i++)
5694     {
5695       /* If X is sufficiently simple, don't bother trying to do anything
5696          with it.  */
5697       if (code != CONST_INT && code != REG && code != CLOBBER)
5698         x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5699
5700       if (GET_CODE (x) == code)
5701         break;
5702
5703       code = GET_CODE (x);
5704
5705       /* We no longer know the original mode of operand 0 since we
5706          have changed the form of X)  */
5707       op0_mode = VOIDmode;
5708     }
5709
5710   return x;
5711 }
5712 \f
5713 /* If X is a commutative operation whose operands are not in the canonical
5714    order, use substitutions to swap them.  */
5715
5716 static void
5717 maybe_swap_commutative_operands (rtx x)
5718 {
5719   if (COMMUTATIVE_ARITH_P (x)
5720       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5721     {
5722       rtx temp = XEXP (x, 0);
5723       SUBST (XEXP (x, 0), XEXP (x, 1));
5724       SUBST (XEXP (x, 1), temp);
5725     }
5726 }
5727
5728 /* Simplify X, a piece of RTL.  We just operate on the expression at the
5729    outer level; call `subst' to simplify recursively.  Return the new
5730    expression.
5731
5732    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
5733    if we are inside a SET_DEST.  IN_COND is nonzero if we are at the top level
5734    of a condition.  */
5735
5736 static rtx
5737 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5738                       int in_cond)
5739 {
5740   enum rtx_code code = GET_CODE (x);
5741   machine_mode mode = GET_MODE (x);
5742   scalar_int_mode int_mode;
5743   rtx temp;
5744   int i;
5745
5746   /* If this is a commutative operation, put a constant last and a complex
5747      expression first.  We don't need to do this for comparisons here.  */
5748   maybe_swap_commutative_operands (x);
5749
5750   /* Try to fold this expression in case we have constants that weren't
5751      present before.  */
5752   temp = 0;
5753   switch (GET_RTX_CLASS (code))
5754     {
5755     case RTX_UNARY:
5756       if (op0_mode == VOIDmode)
5757         op0_mode = GET_MODE (XEXP (x, 0));
5758       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5759       break;
5760     case RTX_COMPARE:
5761     case RTX_COMM_COMPARE:
5762       {
5763         machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5764         if (cmp_mode == VOIDmode)
5765           {
5766             cmp_mode = GET_MODE (XEXP (x, 1));
5767             if (cmp_mode == VOIDmode)
5768               cmp_mode = op0_mode;
5769           }
5770         temp = simplify_relational_operation (code, mode, cmp_mode,
5771                                               XEXP (x, 0), XEXP (x, 1));
5772       }
5773       break;
5774     case RTX_COMM_ARITH:
5775     case RTX_BIN_ARITH:
5776       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5777       break;
5778     case RTX_BITFIELD_OPS:
5779     case RTX_TERNARY:
5780       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5781                                          XEXP (x, 1), XEXP (x, 2));
5782       break;
5783     default:
5784       break;
5785     }
5786
5787   if (temp)
5788     {
5789       x = temp;
5790       code = GET_CODE (temp);
5791       op0_mode = VOIDmode;
5792       mode = GET_MODE (temp);
5793     }
5794
5795   /* If this is a simple operation applied to an IF_THEN_ELSE, try
5796      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
5797      things.  Check for cases where both arms are testing the same
5798      condition.
5799
5800      Don't do anything if all operands are very simple.  */
5801
5802   if ((BINARY_P (x)
5803        && ((!OBJECT_P (XEXP (x, 0))
5804             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5805                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5806            || (!OBJECT_P (XEXP (x, 1))
5807                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5808                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5809       || (UNARY_P (x)
5810           && (!OBJECT_P (XEXP (x, 0))
5811                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5812                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5813     {
5814       rtx cond, true_rtx, false_rtx;
5815
5816       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5817       if (cond != 0
5818           /* If everything is a comparison, what we have is highly unlikely
5819              to be simpler, so don't use it.  */
5820           && ! (COMPARISON_P (x)
5821                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5822           /* Similarly, if we end up with one of the expressions the same
5823              as the original, it is certainly not simpler.  */
5824           && ! rtx_equal_p (x, true_rtx)
5825           && ! rtx_equal_p (x, false_rtx))
5826         {
5827           rtx cop1 = const0_rtx;
5828           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5829
5830           if (cond_code == NE && COMPARISON_P (cond))
5831             return x;
5832
5833           /* Simplify the alternative arms; this may collapse the true and
5834              false arms to store-flag values.  Be careful to use copy_rtx
5835              here since true_rtx or false_rtx might share RTL with x as a
5836              result of the if_then_else_cond call above.  */
5837           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5838           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5839
5840           /* If true_rtx and false_rtx are not general_operands, an if_then_else
5841              is unlikely to be simpler.  */
5842           if (general_operand (true_rtx, VOIDmode)
5843               && general_operand (false_rtx, VOIDmode))
5844             {
5845               enum rtx_code reversed;
5846
5847               /* Restarting if we generate a store-flag expression will cause
5848                  us to loop.  Just drop through in this case.  */
5849
5850               /* If the result values are STORE_FLAG_VALUE and zero, we can
5851                  just make the comparison operation.  */
5852               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5853                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5854                                              cond, cop1);
5855               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5856                        && ((reversed = reversed_comparison_code_parts
5857                                         (cond_code, cond, cop1, NULL))
5858                            != UNKNOWN))
5859                 x = simplify_gen_relational (reversed, mode, VOIDmode,
5860                                              cond, cop1);
5861
5862               /* Likewise, we can make the negate of a comparison operation
5863                  if the result values are - STORE_FLAG_VALUE and zero.  */
5864               else if (CONST_INT_P (true_rtx)
5865                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5866                        && false_rtx == const0_rtx)
5867                 x = simplify_gen_unary (NEG, mode,
5868                                         simplify_gen_relational (cond_code,
5869                                                                  mode, VOIDmode,
5870                                                                  cond, cop1),
5871                                         mode);
5872               else if (CONST_INT_P (false_rtx)
5873                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5874                        && true_rtx == const0_rtx
5875                        && ((reversed = reversed_comparison_code_parts
5876                                         (cond_code, cond, cop1, NULL))
5877                            != UNKNOWN))
5878                 x = simplify_gen_unary (NEG, mode,
5879                                         simplify_gen_relational (reversed,
5880                                                                  mode, VOIDmode,
5881                                                                  cond, cop1),
5882                                         mode);
5883               else
5884                 return gen_rtx_IF_THEN_ELSE (mode,
5885                                              simplify_gen_relational (cond_code,
5886                                                                       mode,
5887                                                                       VOIDmode,
5888                                                                       cond,
5889                                                                       cop1),
5890                                              true_rtx, false_rtx);
5891
5892               code = GET_CODE (x);
5893               op0_mode = VOIDmode;
5894             }
5895         }
5896     }
5897
5898   /* First see if we can apply the inverse distributive law.  */
5899   if (code == PLUS || code == MINUS
5900       || code == AND || code == IOR || code == XOR)
5901     {
5902       x = apply_distributive_law (x);
5903       code = GET_CODE (x);
5904       op0_mode = VOIDmode;
5905     }
5906
5907   /* If CODE is an associative operation not otherwise handled, see if we
5908      can associate some operands.  This can win if they are constants or
5909      if they are logically related (i.e. (a & b) & a).  */
5910   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5911        || code == AND || code == IOR || code == XOR
5912        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5913       && ((INTEGRAL_MODE_P (mode) && code != DIV)
5914           || (flag_associative_math && FLOAT_MODE_P (mode))))
5915     {
5916       if (GET_CODE (XEXP (x, 0)) == code)
5917         {
5918           rtx other = XEXP (XEXP (x, 0), 0);
5919           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5920           rtx inner_op1 = XEXP (x, 1);
5921           rtx inner;
5922
5923           /* Make sure we pass the constant operand if any as the second
5924              one if this is a commutative operation.  */
5925           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5926             std::swap (inner_op0, inner_op1);
5927           inner = simplify_binary_operation (code == MINUS ? PLUS
5928                                              : code == DIV ? MULT
5929                                              : code,
5930                                              mode, inner_op0, inner_op1);
5931
5932           /* For commutative operations, try the other pair if that one
5933              didn't simplify.  */
5934           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5935             {
5936               other = XEXP (XEXP (x, 0), 1);
5937               inner = simplify_binary_operation (code, mode,
5938                                                  XEXP (XEXP (x, 0), 0),
5939                                                  XEXP (x, 1));
5940             }
5941
5942           if (inner)
5943             return simplify_gen_binary (code, mode, other, inner);
5944         }
5945     }
5946
5947   /* A little bit of algebraic simplification here.  */
5948   switch (code)
5949     {
5950     case MEM:
5951       /* Ensure that our address has any ASHIFTs converted to MULT in case
5952          address-recognizing predicates are called later.  */
5953       temp = make_compound_operation (XEXP (x, 0), MEM);
5954       SUBST (XEXP (x, 0), temp);
5955       break;
5956
5957     case SUBREG:
5958       if (op0_mode == VOIDmode)
5959         op0_mode = GET_MODE (SUBREG_REG (x));
5960
5961       /* See if this can be moved to simplify_subreg.  */
5962       if (CONSTANT_P (SUBREG_REG (x))
5963           && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5964              /* Don't call gen_lowpart if the inner mode
5965                 is VOIDmode and we cannot simplify it, as SUBREG without
5966                 inner mode is invalid.  */
5967           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5968               || gen_lowpart_common (mode, SUBREG_REG (x))))
5969         return gen_lowpart (mode, SUBREG_REG (x));
5970
5971       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5972         break;
5973       {
5974         rtx temp;
5975         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5976                                 SUBREG_BYTE (x));
5977         if (temp)
5978           return temp;
5979
5980         /* If op is known to have all lower bits zero, the result is zero.  */
5981         scalar_int_mode int_mode, int_op0_mode;
5982         if (!in_dest
5983             && is_a <scalar_int_mode> (mode, &int_mode)
5984             && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5985             && (GET_MODE_PRECISION (int_mode)
5986                 < GET_MODE_PRECISION (int_op0_mode))
5987             && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
5988                          SUBREG_BYTE (x))
5989             && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5990             && ((nonzero_bits (SUBREG_REG (x), int_op0_mode)
5991                  & GET_MODE_MASK (int_mode)) == 0)
5992             && !side_effects_p (SUBREG_REG (x)))
5993           return CONST0_RTX (int_mode);
5994       }
5995
5996       /* Don't change the mode of the MEM if that would change the meaning
5997          of the address.  */
5998       if (MEM_P (SUBREG_REG (x))
5999           && (MEM_VOLATILE_P (SUBREG_REG (x))
6000               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
6001                                            MEM_ADDR_SPACE (SUBREG_REG (x)))))
6002         return gen_rtx_CLOBBER (mode, const0_rtx);
6003
6004       /* Note that we cannot do any narrowing for non-constants since
6005          we might have been counting on using the fact that some bits were
6006          zero.  We now do this in the SET.  */
6007
6008       break;
6009
6010     case NEG:
6011       temp = expand_compound_operation (XEXP (x, 0));
6012
6013       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
6014          replaced by (lshiftrt X C).  This will convert
6015          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
6016
6017       if (GET_CODE (temp) == ASHIFTRT
6018           && CONST_INT_P (XEXP (temp, 1))
6019           && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
6020         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
6021                                      INTVAL (XEXP (temp, 1)));
6022
6023       /* If X has only a single bit that might be nonzero, say, bit I, convert
6024          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
6025          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
6026          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
6027          or a SUBREG of one since we'd be making the expression more
6028          complex if it was just a register.  */
6029
6030       if (!REG_P (temp)
6031           && ! (GET_CODE (temp) == SUBREG
6032                 && REG_P (SUBREG_REG (temp)))
6033           && is_a <scalar_int_mode> (mode, &int_mode)
6034           && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
6035         {
6036           rtx temp1 = simplify_shift_const
6037             (NULL_RTX, ASHIFTRT, int_mode,
6038              simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
6039                                    GET_MODE_PRECISION (int_mode) - 1 - i),
6040              GET_MODE_PRECISION (int_mode) - 1 - i);
6041
6042           /* If all we did was surround TEMP with the two shifts, we
6043              haven't improved anything, so don't use it.  Otherwise,
6044              we are better off with TEMP1.  */
6045           if (GET_CODE (temp1) != ASHIFTRT
6046               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
6047               || XEXP (XEXP (temp1, 0), 0) != temp)
6048             return temp1;
6049         }
6050       break;
6051
6052     case TRUNCATE:
6053       /* We can't handle truncation to a partial integer mode here
6054          because we don't know the real bitsize of the partial
6055          integer mode.  */
6056       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6057         break;
6058
6059       if (HWI_COMPUTABLE_MODE_P (mode))
6060         SUBST (XEXP (x, 0),
6061                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6062                               GET_MODE_MASK (mode), 0));
6063
6064       /* We can truncate a constant value and return it.  */
6065       {
6066         poly_int64 c;
6067         if (poly_int_rtx_p (XEXP (x, 0), &c))
6068           return gen_int_mode (c, mode);
6069       }
6070
6071       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
6072          whose value is a comparison can be replaced with a subreg if
6073          STORE_FLAG_VALUE permits.  */
6074       if (HWI_COMPUTABLE_MODE_P (mode)
6075           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
6076           && (temp = get_last_value (XEXP (x, 0)))
6077           && COMPARISON_P (temp))
6078         return gen_lowpart (mode, XEXP (x, 0));
6079       break;
6080
6081     case CONST:
6082       /* (const (const X)) can become (const X).  Do it this way rather than
6083          returning the inner CONST since CONST can be shared with a
6084          REG_EQUAL note.  */
6085       if (GET_CODE (XEXP (x, 0)) == CONST)
6086         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
6087       break;
6088
6089     case LO_SUM:
6090       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
6091          can add in an offset.  find_split_point will split this address up
6092          again if it doesn't match.  */
6093       if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6094           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6095         return XEXP (x, 1);
6096       break;
6097
6098     case PLUS:
6099       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6100          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6101          bit-field and can be replaced by either a sign_extend or a
6102          sign_extract.  The `and' may be a zero_extend and the two
6103          <c>, -<c> constants may be reversed.  */
6104       if (GET_CODE (XEXP (x, 0)) == XOR
6105           && is_a <scalar_int_mode> (mode, &int_mode)
6106           && CONST_INT_P (XEXP (x, 1))
6107           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6108           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
6109           && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
6110               || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
6111           && HWI_COMPUTABLE_MODE_P (int_mode)
6112           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6113                && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6114                && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6115                    == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6116               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6117                   && known_eq ((GET_MODE_PRECISION
6118                                 (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6119                                (unsigned int) i + 1))))
6120         return simplify_shift_const
6121           (NULL_RTX, ASHIFTRT, int_mode,
6122            simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6123                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
6124                                  GET_MODE_PRECISION (int_mode) - (i + 1)),
6125            GET_MODE_PRECISION (int_mode) - (i + 1));
6126
6127       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6128          can become (ashiftrt (ashift (xor x 1) C) C) where C is
6129          the bitsize of the mode - 1.  This allows simplification of
6130          "a = (b & 8) == 0;"  */
6131       if (XEXP (x, 1) == constm1_rtx
6132           && !REG_P (XEXP (x, 0))
6133           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6134                 && REG_P (SUBREG_REG (XEXP (x, 0))))
6135           && is_a <scalar_int_mode> (mode, &int_mode)
6136           && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6137         return simplify_shift_const
6138           (NULL_RTX, ASHIFTRT, int_mode,
6139            simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6140                                  gen_rtx_XOR (int_mode, XEXP (x, 0),
6141                                               const1_rtx),
6142                                  GET_MODE_PRECISION (int_mode) - 1),
6143            GET_MODE_PRECISION (int_mode) - 1);
6144
6145       /* If we are adding two things that have no bits in common, convert
6146          the addition into an IOR.  This will often be further simplified,
6147          for example in cases like ((a & 1) + (a & 2)), which can
6148          become a & 3.  */
6149
6150       if (HWI_COMPUTABLE_MODE_P (mode)
6151           && (nonzero_bits (XEXP (x, 0), mode)
6152               & nonzero_bits (XEXP (x, 1), mode)) == 0)
6153         {
6154           /* Try to simplify the expression further.  */
6155           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6156           temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
6157
6158           /* If we could, great.  If not, do not go ahead with the IOR
6159              replacement, since PLUS appears in many special purpose
6160              address arithmetic instructions.  */
6161           if (GET_CODE (temp) != CLOBBER
6162               && (GET_CODE (temp) != IOR
6163                   || ((XEXP (temp, 0) != XEXP (x, 0)
6164                        || XEXP (temp, 1) != XEXP (x, 1))
6165                       && (XEXP (temp, 0) != XEXP (x, 1)
6166                           || XEXP (temp, 1) != XEXP (x, 0)))))
6167             return temp;
6168         }
6169
6170       /* Canonicalize x + x into x << 1.  */
6171       if (GET_MODE_CLASS (mode) == MODE_INT
6172           && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6173           && !side_effects_p (XEXP (x, 0)))
6174         return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6175
6176       break;
6177
6178     case MINUS:
6179       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6180          (and <foo> (const_int pow2-1))  */
6181       if (is_a <scalar_int_mode> (mode, &int_mode)
6182           && GET_CODE (XEXP (x, 1)) == AND
6183           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6184           && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6185           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6186         return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6187                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6188       break;
6189
6190     case MULT:
6191       /* If we have (mult (plus A B) C), apply the distributive law and then
6192          the inverse distributive law to see if things simplify.  This
6193          occurs mostly in addresses, often when unrolling loops.  */
6194
6195       if (GET_CODE (XEXP (x, 0)) == PLUS)
6196         {
6197           rtx result = distribute_and_simplify_rtx (x, 0);
6198           if (result)
6199             return result;
6200         }
6201
6202       /* Try simplify a*(b/c) as (a*b)/c.  */
6203       if (FLOAT_MODE_P (mode) && flag_associative_math
6204           && GET_CODE (XEXP (x, 0)) == DIV)
6205         {
6206           rtx tem = simplify_binary_operation (MULT, mode,
6207                                                XEXP (XEXP (x, 0), 0),
6208                                                XEXP (x, 1));
6209           if (tem)
6210             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6211         }
6212       break;
6213
6214     case UDIV:
6215       /* If this is a divide by a power of two, treat it as a shift if
6216          its first operand is a shift.  */
6217       if (is_a <scalar_int_mode> (mode, &int_mode)
6218           && CONST_INT_P (XEXP (x, 1))
6219           && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6220           && (GET_CODE (XEXP (x, 0)) == ASHIFT
6221               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6222               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6223               || GET_CODE (XEXP (x, 0)) == ROTATE
6224               || GET_CODE (XEXP (x, 0)) == ROTATERT))
6225         return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6226                                      XEXP (x, 0), i);
6227       break;
6228
6229     case EQ:  case NE:
6230     case GT:  case GTU:  case GE:  case GEU:
6231     case LT:  case LTU:  case LE:  case LEU:
6232     case UNEQ:  case LTGT:
6233     case UNGT:  case UNGE:
6234     case UNLT:  case UNLE:
6235     case UNORDERED: case ORDERED:
6236       /* If the first operand is a condition code, we can't do anything
6237          with it.  */
6238       if (GET_CODE (XEXP (x, 0)) == COMPARE
6239           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6240               && ! CC0_P (XEXP (x, 0))))
6241         {
6242           rtx op0 = XEXP (x, 0);
6243           rtx op1 = XEXP (x, 1);
6244           enum rtx_code new_code;
6245
6246           if (GET_CODE (op0) == COMPARE)
6247             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6248
6249           /* Simplify our comparison, if possible.  */
6250           new_code = simplify_comparison (code, &op0, &op1);
6251
6252           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6253              if only the low-order bit is possibly nonzero in X (such as when
6254              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
6255              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
6256              known to be either 0 or -1, NE becomes a NEG and EQ becomes
6257              (plus X 1).
6258
6259              Remove any ZERO_EXTRACT we made when thinking this was a
6260              comparison.  It may now be simpler to use, e.g., an AND.  If a
6261              ZERO_EXTRACT is indeed appropriate, it will be placed back by
6262              the call to make_compound_operation in the SET case.
6263
6264              Don't apply these optimizations if the caller would
6265              prefer a comparison rather than a value.
6266              E.g., for the condition in an IF_THEN_ELSE most targets need
6267              an explicit comparison.  */
6268
6269           if (in_cond)
6270             ;
6271
6272           else if (STORE_FLAG_VALUE == 1
6273                    && new_code == NE
6274                    && is_int_mode (mode, &int_mode)
6275                    && op1 == const0_rtx
6276                    && int_mode == GET_MODE (op0)
6277                    && nonzero_bits (op0, int_mode) == 1)
6278             return gen_lowpart (int_mode,
6279                                 expand_compound_operation (op0));
6280
6281           else if (STORE_FLAG_VALUE == 1
6282                    && new_code == NE
6283                    && is_int_mode (mode, &int_mode)
6284                    && op1 == const0_rtx
6285                    && int_mode == GET_MODE (op0)
6286                    && (num_sign_bit_copies (op0, int_mode)
6287                        == GET_MODE_PRECISION (int_mode)))
6288             {
6289               op0 = expand_compound_operation (op0);
6290               return simplify_gen_unary (NEG, int_mode,
6291                                          gen_lowpart (int_mode, op0),
6292                                          int_mode);
6293             }
6294
6295           else if (STORE_FLAG_VALUE == 1
6296                    && new_code == EQ
6297                    && is_int_mode (mode, &int_mode)
6298                    && op1 == const0_rtx
6299                    && int_mode == GET_MODE (op0)
6300                    && nonzero_bits (op0, int_mode) == 1)
6301             {
6302               op0 = expand_compound_operation (op0);
6303               return simplify_gen_binary (XOR, int_mode,
6304                                           gen_lowpart (int_mode, op0),
6305                                           const1_rtx);
6306             }
6307
6308           else if (STORE_FLAG_VALUE == 1
6309                    && new_code == EQ
6310                    && is_int_mode (mode, &int_mode)
6311                    && op1 == const0_rtx
6312                    && int_mode == GET_MODE (op0)
6313                    && (num_sign_bit_copies (op0, int_mode)
6314                        == GET_MODE_PRECISION (int_mode)))
6315             {
6316               op0 = expand_compound_operation (op0);
6317               return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6318             }
6319
6320           /* If STORE_FLAG_VALUE is -1, we have cases similar to
6321              those above.  */
6322           if (in_cond)
6323             ;
6324
6325           else if (STORE_FLAG_VALUE == -1
6326                    && new_code == NE
6327                    && is_int_mode (mode, &int_mode)
6328                    && op1 == const0_rtx
6329                    && int_mode == GET_MODE (op0)
6330                    && (num_sign_bit_copies (op0, int_mode)
6331                        == GET_MODE_PRECISION (int_mode)))
6332             return gen_lowpart (int_mode, expand_compound_operation (op0));
6333
6334           else if (STORE_FLAG_VALUE == -1
6335                    && new_code == NE
6336                    && is_int_mode (mode, &int_mode)
6337                    && op1 == const0_rtx
6338                    && int_mode == GET_MODE (op0)
6339                    && nonzero_bits (op0, int_mode) == 1)
6340             {
6341               op0 = expand_compound_operation (op0);
6342               return simplify_gen_unary (NEG, int_mode,
6343                                          gen_lowpart (int_mode, op0),
6344                                          int_mode);
6345             }
6346
6347           else if (STORE_FLAG_VALUE == -1
6348                    && new_code == EQ
6349                    && is_int_mode (mode, &int_mode)
6350                    && op1 == const0_rtx
6351                    && int_mode == GET_MODE (op0)
6352                    && (num_sign_bit_copies (op0, int_mode)
6353                        == GET_MODE_PRECISION (int_mode)))
6354             {
6355               op0 = expand_compound_operation (op0);
6356               return simplify_gen_unary (NOT, int_mode,
6357                                          gen_lowpart (int_mode, op0),
6358                                          int_mode);
6359             }
6360
6361           /* If X is 0/1, (eq X 0) is X-1.  */
6362           else if (STORE_FLAG_VALUE == -1
6363                    && new_code == EQ
6364                    && is_int_mode (mode, &int_mode)
6365                    && op1 == const0_rtx
6366                    && int_mode == GET_MODE (op0)
6367                    && nonzero_bits (op0, int_mode) == 1)
6368             {
6369               op0 = expand_compound_operation (op0);
6370               return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6371             }
6372
6373           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6374              one bit that might be nonzero, we can convert (ne x 0) to
6375              (ashift x c) where C puts the bit in the sign bit.  Remove any
6376              AND with STORE_FLAG_VALUE when we are done, since we are only
6377              going to test the sign bit.  */
6378           if (new_code == NE
6379               && is_int_mode (mode, &int_mode)
6380               && HWI_COMPUTABLE_MODE_P (int_mode)
6381               && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6382               && op1 == const0_rtx
6383               && int_mode == GET_MODE (op0)
6384               && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6385             {
6386               x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6387                                         expand_compound_operation (op0),
6388                                         GET_MODE_PRECISION (int_mode) - 1 - i);
6389               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6390                 return XEXP (x, 0);
6391               else
6392                 return x;
6393             }
6394
6395           /* If the code changed, return a whole new comparison.
6396              We also need to avoid using SUBST in cases where
6397              simplify_comparison has widened a comparison with a CONST_INT,
6398              since in that case the wider CONST_INT may fail the sanity
6399              checks in do_SUBST.  */
6400           if (new_code != code
6401               || (CONST_INT_P (op1)
6402                   && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6403                   && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6404             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6405
6406           /* Otherwise, keep this operation, but maybe change its operands.
6407              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
6408           SUBST (XEXP (x, 0), op0);
6409           SUBST (XEXP (x, 1), op1);
6410         }
6411       break;
6412
6413     case IF_THEN_ELSE:
6414       return simplify_if_then_else (x);
6415
6416     case ZERO_EXTRACT:
6417     case SIGN_EXTRACT:
6418     case ZERO_EXTEND:
6419     case SIGN_EXTEND:
6420       /* If we are processing SET_DEST, we are done.  */
6421       if (in_dest)
6422         return x;
6423
6424       return expand_compound_operation (x);
6425
6426     case SET:
6427       return simplify_set (x);
6428
6429     case AND:
6430     case IOR:
6431       return simplify_logical (x);
6432
6433     case ASHIFT:
6434     case LSHIFTRT:
6435     case ASHIFTRT:
6436     case ROTATE:
6437     case ROTATERT:
6438       /* If this is a shift by a constant amount, simplify it.  */
6439       if (CONST_INT_P (XEXP (x, 1)))
6440         return simplify_shift_const (x, code, mode, XEXP (x, 0),
6441                                      INTVAL (XEXP (x, 1)));
6442
6443       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6444         SUBST (XEXP (x, 1),
6445                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6446                               (HOST_WIDE_INT_1U
6447                                << exact_log2 (GET_MODE_UNIT_BITSIZE
6448                                               (GET_MODE (x))))
6449                               - 1,
6450                               0));
6451       break;
6452
6453     default:
6454       break;
6455     }
6456
6457   return x;
6458 }
6459 \f
6460 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
6461
6462 static rtx
6463 simplify_if_then_else (rtx x)
6464 {
6465   machine_mode mode = GET_MODE (x);
6466   rtx cond = XEXP (x, 0);
6467   rtx true_rtx = XEXP (x, 1);
6468   rtx false_rtx = XEXP (x, 2);
6469   enum rtx_code true_code = GET_CODE (cond);
6470   int comparison_p = COMPARISON_P (cond);
6471   rtx temp;
6472   int i;
6473   enum rtx_code false_code;
6474   rtx reversed;
6475   scalar_int_mode int_mode, inner_mode;
6476
6477   /* Simplify storing of the truth value.  */
6478   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6479     return simplify_gen_relational (true_code, mode, VOIDmode,
6480                                     XEXP (cond, 0), XEXP (cond, 1));
6481
6482   /* Also when the truth value has to be reversed.  */
6483   if (comparison_p
6484       && true_rtx == const0_rtx && false_rtx == const_true_rtx
6485       && (reversed = reversed_comparison (cond, mode)))
6486     return reversed;
6487
6488   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6489      in it is being compared against certain values.  Get the true and false
6490      comparisons and see if that says anything about the value of each arm.  */
6491
6492   if (comparison_p
6493       && ((false_code = reversed_comparison_code (cond, NULL))
6494           != UNKNOWN)
6495       && REG_P (XEXP (cond, 0)))
6496     {
6497       HOST_WIDE_INT nzb;
6498       rtx from = XEXP (cond, 0);
6499       rtx true_val = XEXP (cond, 1);
6500       rtx false_val = true_val;
6501       int swapped = 0;
6502
6503       /* If FALSE_CODE is EQ, swap the codes and arms.  */
6504
6505       if (false_code == EQ)
6506         {
6507           swapped = 1, true_code = EQ, false_code = NE;
6508           std::swap (true_rtx, false_rtx);
6509         }
6510
6511       scalar_int_mode from_mode;
6512       if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6513         {
6514           /* If we are comparing against zero and the expression being
6515              tested has only a single bit that might be nonzero, that is
6516              its value when it is not equal to zero.  Similarly if it is
6517              known to be -1 or 0.  */
6518           if (true_code == EQ
6519               && true_val == const0_rtx
6520               && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6521             {
6522               false_code = EQ;
6523               false_val = gen_int_mode (nzb, from_mode);
6524             }
6525           else if (true_code == EQ
6526                    && true_val == const0_rtx
6527                    && (num_sign_bit_copies (from, from_mode)
6528                        == GET_MODE_PRECISION (from_mode)))
6529             {
6530               false_code = EQ;
6531               false_val = constm1_rtx;
6532             }
6533         }
6534
6535       /* Now simplify an arm if we know the value of the register in the
6536          branch and it is used in the arm.  Be careful due to the potential
6537          of locally-shared RTL.  */
6538
6539       if (reg_mentioned_p (from, true_rtx))
6540         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6541                                       from, true_val),
6542                           pc_rtx, pc_rtx, 0, 0, 0);
6543       if (reg_mentioned_p (from, false_rtx))
6544         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6545                                        from, false_val),
6546                            pc_rtx, pc_rtx, 0, 0, 0);
6547
6548       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6549       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6550
6551       true_rtx = XEXP (x, 1);
6552       false_rtx = XEXP (x, 2);
6553       true_code = GET_CODE (cond);
6554     }
6555
6556   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6557      reversed, do so to avoid needing two sets of patterns for
6558      subtract-and-branch insns.  Similarly if we have a constant in the true
6559      arm, the false arm is the same as the first operand of the comparison, or
6560      the false arm is more complicated than the true arm.  */
6561
6562   if (comparison_p
6563       && reversed_comparison_code (cond, NULL) != UNKNOWN
6564       && (true_rtx == pc_rtx
6565           || (CONSTANT_P (true_rtx)
6566               && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6567           || true_rtx == const0_rtx
6568           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6569           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6570               && !OBJECT_P (false_rtx))
6571           || reg_mentioned_p (true_rtx, false_rtx)
6572           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6573     {
6574       true_code = reversed_comparison_code (cond, NULL);
6575       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6576       SUBST (XEXP (x, 1), false_rtx);
6577       SUBST (XEXP (x, 2), true_rtx);
6578
6579       std::swap (true_rtx, false_rtx);
6580       cond = XEXP (x, 0);
6581
6582       /* It is possible that the conditional has been simplified out.  */
6583       true_code = GET_CODE (cond);
6584       comparison_p = COMPARISON_P (cond);
6585     }
6586
6587   /* If the two arms are identical, we don't need the comparison.  */
6588
6589   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6590     return true_rtx;
6591
6592   /* Convert a == b ? b : a to "a".  */
6593   if (true_code == EQ && ! side_effects_p (cond)
6594       && !HONOR_NANS (mode)
6595       && rtx_equal_p (XEXP (cond, 0), false_rtx)
6596       && rtx_equal_p (XEXP (cond, 1), true_rtx))
6597     return false_rtx;
6598   else if (true_code == NE && ! side_effects_p (cond)
6599            && !HONOR_NANS (mode)
6600            && rtx_equal_p (XEXP (cond, 0), true_rtx)
6601            && rtx_equal_p (XEXP (cond, 1), false_rtx))
6602     return true_rtx;
6603
6604   /* Look for cases where we have (abs x) or (neg (abs X)).  */
6605
6606   if (GET_MODE_CLASS (mode) == MODE_INT
6607       && comparison_p
6608       && XEXP (cond, 1) == const0_rtx
6609       && GET_CODE (false_rtx) == NEG
6610       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6611       && rtx_equal_p (true_rtx, XEXP (cond, 0))
6612       && ! side_effects_p (true_rtx))
6613     switch (true_code)
6614       {
6615       case GT:
6616       case GE:
6617         return simplify_gen_unary (ABS, mode, true_rtx, mode);
6618       case LT:
6619       case LE:
6620         return
6621           simplify_gen_unary (NEG, mode,
6622                               simplify_gen_unary (ABS, mode, true_rtx, mode),
6623                               mode);
6624       default:
6625         break;
6626       }
6627
6628   /* Look for MIN or MAX.  */
6629
6630   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6631       && comparison_p
6632       && rtx_equal_p (XEXP (cond, 0), true_rtx)
6633       && rtx_equal_p (XEXP (cond, 1), false_rtx)
6634       && ! side_effects_p (cond))
6635     switch (true_code)
6636       {
6637       case GE:
6638       case GT:
6639         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6640       case LE:
6641       case LT:
6642         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6643       case GEU:
6644       case GTU:
6645         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6646       case LEU:
6647       case LTU:
6648         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6649       default:
6650         break;
6651       }
6652
6653   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6654      second operand is zero, this can be done as (OP Z (mult COND C2)) where
6655      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6656      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6657      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6658      neither 1 or -1, but it isn't worth checking for.  */
6659
6660   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6661       && comparison_p
6662       && is_int_mode (mode, &int_mode)
6663       && ! side_effects_p (x))
6664     {
6665       rtx t = make_compound_operation (true_rtx, SET);
6666       rtx f = make_compound_operation (false_rtx, SET);
6667       rtx cond_op0 = XEXP (cond, 0);
6668       rtx cond_op1 = XEXP (cond, 1);
6669       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6670       scalar_int_mode m = int_mode;
6671       rtx z = 0, c1 = NULL_RTX;
6672
6673       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6674            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6675            || GET_CODE (t) == ASHIFT
6676            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6677           && rtx_equal_p (XEXP (t, 0), f))
6678         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6679
6680       /* If an identity-zero op is commutative, check whether there
6681          would be a match if we swapped the operands.  */
6682       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6683                 || GET_CODE (t) == XOR)
6684                && rtx_equal_p (XEXP (t, 1), f))
6685         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6686       else if (GET_CODE (t) == SIGN_EXTEND
6687                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6688                && (GET_CODE (XEXP (t, 0)) == PLUS
6689                    || GET_CODE (XEXP (t, 0)) == MINUS
6690                    || GET_CODE (XEXP (t, 0)) == IOR
6691                    || GET_CODE (XEXP (t, 0)) == XOR
6692                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6693                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6694                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6695                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6696                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6697                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6698                && (num_sign_bit_copies (f, GET_MODE (f))
6699                    > (unsigned int)
6700                      (GET_MODE_PRECISION (int_mode)
6701                       - GET_MODE_PRECISION (inner_mode))))
6702         {
6703           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6704           extend_op = SIGN_EXTEND;
6705           m = inner_mode;
6706         }
6707       else if (GET_CODE (t) == SIGN_EXTEND
6708                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6709                && (GET_CODE (XEXP (t, 0)) == PLUS
6710                    || GET_CODE (XEXP (t, 0)) == IOR
6711                    || GET_CODE (XEXP (t, 0)) == XOR)
6712                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6713                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6714                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6715                && (num_sign_bit_copies (f, GET_MODE (f))
6716                    > (unsigned int)
6717                      (GET_MODE_PRECISION (int_mode)
6718                       - GET_MODE_PRECISION (inner_mode))))
6719         {
6720           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6721           extend_op = SIGN_EXTEND;
6722           m = inner_mode;
6723         }
6724       else if (GET_CODE (t) == ZERO_EXTEND
6725                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6726                && (GET_CODE (XEXP (t, 0)) == PLUS
6727                    || GET_CODE (XEXP (t, 0)) == MINUS
6728                    || GET_CODE (XEXP (t, 0)) == IOR
6729                    || GET_CODE (XEXP (t, 0)) == XOR
6730                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6731                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6732                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6733                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6734                && HWI_COMPUTABLE_MODE_P (int_mode)
6735                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6736                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6737                && ((nonzero_bits (f, GET_MODE (f))
6738                     & ~GET_MODE_MASK (inner_mode))
6739                    == 0))
6740         {
6741           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6742           extend_op = ZERO_EXTEND;
6743           m = inner_mode;
6744         }
6745       else if (GET_CODE (t) == ZERO_EXTEND
6746                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6747                && (GET_CODE (XEXP (t, 0)) == PLUS
6748                    || GET_CODE (XEXP (t, 0)) == IOR
6749                    || GET_CODE (XEXP (t, 0)) == XOR)
6750                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6751                && HWI_COMPUTABLE_MODE_P (int_mode)
6752                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6753                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6754                && ((nonzero_bits (f, GET_MODE (f))
6755                     & ~GET_MODE_MASK (inner_mode))
6756                    == 0))
6757         {
6758           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6759           extend_op = ZERO_EXTEND;
6760           m = inner_mode;
6761         }
6762
6763       if (z)
6764         {
6765           machine_mode cm = m;
6766           if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6767               && GET_MODE (c1) != VOIDmode)
6768             cm = GET_MODE (c1);
6769           temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6770                                                  cond_op0, cond_op1),
6771                         pc_rtx, pc_rtx, 0, 0, 0);
6772           temp = simplify_gen_binary (MULT, cm, temp,
6773                                       simplify_gen_binary (MULT, cm, c1,
6774                                                            const_true_rtx));
6775           temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6776           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6777
6778           if (extend_op != UNKNOWN)
6779             temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6780
6781           return temp;
6782         }
6783     }
6784
6785   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6786      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6787      negation of a single bit, we can convert this operation to a shift.  We
6788      can actually do this more generally, but it doesn't seem worth it.  */
6789
6790   if (true_code == NE
6791       && is_a <scalar_int_mode> (mode, &int_mode)
6792       && XEXP (cond, 1) == const0_rtx
6793       && false_rtx == const0_rtx
6794       && CONST_INT_P (true_rtx)
6795       && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6796            && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6797           || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6798                == GET_MODE_PRECISION (int_mode))
6799               && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6800     return
6801       simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6802                             gen_lowpart (int_mode, XEXP (cond, 0)), i);
6803
6804   /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6805      non-zero bit in A is C1.  */
6806   if (true_code == NE && XEXP (cond, 1) == const0_rtx
6807       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6808       && is_a <scalar_int_mode> (mode, &int_mode)
6809       && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6810       && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6811           == nonzero_bits (XEXP (cond, 0), inner_mode)
6812       && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6813     {
6814       rtx val = XEXP (cond, 0);
6815       if (inner_mode == int_mode)
6816         return val;
6817       else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6818         return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6819     }
6820
6821   return x;
6822 }
6823 \f
6824 /* Simplify X, a SET expression.  Return the new expression.  */
6825
6826 static rtx
6827 simplify_set (rtx x)
6828 {
6829   rtx src = SET_SRC (x);
6830   rtx dest = SET_DEST (x);
6831   machine_mode mode
6832     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6833   rtx_insn *other_insn;
6834   rtx *cc_use;
6835   scalar_int_mode int_mode;
6836
6837   /* (set (pc) (return)) gets written as (return).  */
6838   if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6839     return src;
6840
6841   /* Now that we know for sure which bits of SRC we are using, see if we can
6842      simplify the expression for the object knowing that we only need the
6843      low-order bits.  */
6844
6845   if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6846     {
6847       src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6848       SUBST (SET_SRC (x), src);
6849     }
6850
6851   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6852      the comparison result and try to simplify it unless we already have used
6853      undobuf.other_insn.  */
6854   if ((GET_MODE_CLASS (mode) == MODE_CC
6855        || GET_CODE (src) == COMPARE
6856        || CC0_P (dest))
6857       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6858       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6859       && COMPARISON_P (*cc_use)
6860       && rtx_equal_p (XEXP (*cc_use, 0), dest))
6861     {
6862       enum rtx_code old_code = GET_CODE (*cc_use);
6863       enum rtx_code new_code;
6864       rtx op0, op1, tmp;
6865       int other_changed = 0;
6866       rtx inner_compare = NULL_RTX;
6867       machine_mode compare_mode = GET_MODE (dest);
6868
6869       if (GET_CODE (src) == COMPARE)
6870         {
6871           op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6872           if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6873             {
6874               inner_compare = op0;
6875               op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6876             }
6877         }
6878       else
6879         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6880
6881       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6882                                            op0, op1);
6883       if (!tmp)
6884         new_code = old_code;
6885       else if (!CONSTANT_P (tmp))
6886         {
6887           new_code = GET_CODE (tmp);
6888           op0 = XEXP (tmp, 0);
6889           op1 = XEXP (tmp, 1);
6890         }
6891       else
6892         {
6893           rtx pat = PATTERN (other_insn);
6894           undobuf.other_insn = other_insn;
6895           SUBST (*cc_use, tmp);
6896
6897           /* Attempt to simplify CC user.  */
6898           if (GET_CODE (pat) == SET)
6899             {
6900               rtx new_rtx = simplify_rtx (SET_SRC (pat));
6901               if (new_rtx != NULL_RTX)
6902                 SUBST (SET_SRC (pat), new_rtx);
6903             }
6904
6905           /* Convert X into a no-op move.  */
6906           SUBST (SET_DEST (x), pc_rtx);
6907           SUBST (SET_SRC (x), pc_rtx);
6908           return x;
6909         }
6910
6911       /* Simplify our comparison, if possible.  */
6912       new_code = simplify_comparison (new_code, &op0, &op1);
6913
6914 #ifdef SELECT_CC_MODE
6915       /* If this machine has CC modes other than CCmode, check to see if we
6916          need to use a different CC mode here.  */
6917       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6918         compare_mode = GET_MODE (op0);
6919       else if (inner_compare
6920                && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6921                && new_code == old_code
6922                && op0 == XEXP (inner_compare, 0)
6923                && op1 == XEXP (inner_compare, 1))
6924         compare_mode = GET_MODE (inner_compare);
6925       else
6926         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6927
6928       /* If the mode changed, we have to change SET_DEST, the mode in the
6929          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
6930          a hard register, just build new versions with the proper mode.  If it
6931          is a pseudo, we lose unless it is only time we set the pseudo, in
6932          which case we can safely change its mode.  */
6933       if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6934         {
6935           if (can_change_dest_mode (dest, 0, compare_mode))
6936             {
6937               unsigned int regno = REGNO (dest);
6938               rtx new_dest;
6939
6940               if (regno < FIRST_PSEUDO_REGISTER)
6941                 new_dest = gen_rtx_REG (compare_mode, regno);
6942               else
6943                 {
6944                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6945                   new_dest = regno_reg_rtx[regno];
6946                 }
6947
6948               SUBST (SET_DEST (x), new_dest);
6949               SUBST (XEXP (*cc_use, 0), new_dest);
6950               other_changed = 1;
6951
6952               dest = new_dest;
6953             }
6954         }
6955 #endif  /* SELECT_CC_MODE */
6956
6957       /* If the code changed, we have to build a new comparison in
6958          undobuf.other_insn.  */
6959       if (new_code != old_code)
6960         {
6961           int other_changed_previously = other_changed;
6962           unsigned HOST_WIDE_INT mask;
6963           rtx old_cc_use = *cc_use;
6964
6965           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6966                                           dest, const0_rtx));
6967           other_changed = 1;
6968
6969           /* If the only change we made was to change an EQ into an NE or
6970              vice versa, OP0 has only one bit that might be nonzero, and OP1
6971              is zero, check if changing the user of the condition code will
6972              produce a valid insn.  If it won't, we can keep the original code
6973              in that insn by surrounding our operation with an XOR.  */
6974
6975           if (((old_code == NE && new_code == EQ)
6976                || (old_code == EQ && new_code == NE))
6977               && ! other_changed_previously && op1 == const0_rtx
6978               && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6979               && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6980             {
6981               rtx pat = PATTERN (other_insn), note = 0;
6982
6983               if ((recog_for_combine (&pat, other_insn, &note) < 0
6984                    && ! check_asm_operands (pat)))
6985                 {
6986                   *cc_use = old_cc_use;
6987                   other_changed = 0;
6988
6989                   op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6990                                              gen_int_mode (mask,
6991                                                            GET_MODE (op0)));
6992                 }
6993             }
6994         }
6995
6996       if (other_changed)
6997         undobuf.other_insn = other_insn;
6998
6999       /* Don't generate a compare of a CC with 0, just use that CC.  */
7000       if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
7001         {
7002           SUBST (SET_SRC (x), op0);
7003           src = SET_SRC (x);
7004         }
7005       /* Otherwise, if we didn't previously have the same COMPARE we
7006          want, create it from scratch.  */
7007       else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
7008                || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
7009         {
7010           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
7011           src = SET_SRC (x);
7012         }
7013     }
7014   else
7015     {
7016       /* Get SET_SRC in a form where we have placed back any
7017          compound expressions.  Then do the checks below.  */
7018       src = make_compound_operation (src, SET);
7019       SUBST (SET_SRC (x), src);
7020     }
7021
7022   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
7023      and X being a REG or (subreg (reg)), we may be able to convert this to
7024      (set (subreg:m2 x) (op)).
7025
7026      We can always do this if M1 is narrower than M2 because that means that
7027      we only care about the low bits of the result.
7028
7029      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
7030      perform a narrower operation than requested since the high-order bits will
7031      be undefined.  On machine where it is defined, this transformation is safe
7032      as long as M1 and M2 have the same number of words.  */
7033
7034   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
7035       && !OBJECT_P (SUBREG_REG (src))
7036       && (known_equal_after_align_up
7037           (GET_MODE_SIZE (GET_MODE (src)),
7038            GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
7039            UNITS_PER_WORD))
7040       && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
7041       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
7042             && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
7043                                        GET_MODE (SUBREG_REG (src)),
7044                                        GET_MODE (src)))
7045       && (REG_P (dest)
7046           || (GET_CODE (dest) == SUBREG
7047               && REG_P (SUBREG_REG (dest)))))
7048     {
7049       SUBST (SET_DEST (x),
7050              gen_lowpart (GET_MODE (SUBREG_REG (src)),
7051                                       dest));
7052       SUBST (SET_SRC (x), SUBREG_REG (src));
7053
7054       src = SET_SRC (x), dest = SET_DEST (x);
7055     }
7056
7057   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
7058      in SRC.  */
7059   if (dest == cc0_rtx
7060       && partial_subreg_p (src)
7061       && subreg_lowpart_p (src))
7062     {
7063       rtx inner = SUBREG_REG (src);
7064       machine_mode inner_mode = GET_MODE (inner);
7065
7066       /* Here we make sure that we don't have a sign bit on.  */
7067       if (val_signbit_known_clear_p (GET_MODE (src),
7068                                      nonzero_bits (inner, inner_mode)))
7069         {
7070           SUBST (SET_SRC (x), inner);
7071           src = SET_SRC (x);
7072         }
7073     }
7074
7075   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
7076      would require a paradoxical subreg.  Replace the subreg with a
7077      zero_extend to avoid the reload that would otherwise be required.
7078      Don't do this unless we have a scalar integer mode, otherwise the
7079      transformation is incorrect.  */
7080
7081   enum rtx_code extend_op;
7082   if (paradoxical_subreg_p (src)
7083       && MEM_P (SUBREG_REG (src))
7084       && SCALAR_INT_MODE_P (GET_MODE (src))
7085       && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
7086     {
7087       SUBST (SET_SRC (x),
7088              gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
7089
7090       src = SET_SRC (x);
7091     }
7092
7093   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7094      are comparing an item known to be 0 or -1 against 0, use a logical
7095      operation instead. Check for one of the arms being an IOR of the other
7096      arm with some value.  We compute three terms to be IOR'ed together.  In
7097      practice, at most two will be nonzero.  Then we do the IOR's.  */
7098
7099   if (GET_CODE (dest) != PC
7100       && GET_CODE (src) == IF_THEN_ELSE
7101       && is_int_mode (GET_MODE (src), &int_mode)
7102       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7103       && XEXP (XEXP (src, 0), 1) == const0_rtx
7104       && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7105       && (!HAVE_conditional_move
7106           || ! can_conditionally_move_p (int_mode))
7107       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7108           == GET_MODE_PRECISION (int_mode))
7109       && ! side_effects_p (src))
7110     {
7111       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7112                       ? XEXP (src, 1) : XEXP (src, 2));
7113       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7114                    ? XEXP (src, 2) : XEXP (src, 1));
7115       rtx term1 = const0_rtx, term2, term3;
7116
7117       if (GET_CODE (true_rtx) == IOR
7118           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7119         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7120       else if (GET_CODE (true_rtx) == IOR
7121                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7122         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7123       else if (GET_CODE (false_rtx) == IOR
7124                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7125         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7126       else if (GET_CODE (false_rtx) == IOR
7127                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7128         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7129
7130       term2 = simplify_gen_binary (AND, int_mode,
7131                                    XEXP (XEXP (src, 0), 0), true_rtx);
7132       term3 = simplify_gen_binary (AND, int_mode,
7133                                    simplify_gen_unary (NOT, int_mode,
7134                                                        XEXP (XEXP (src, 0), 0),
7135                                                        int_mode),
7136                                    false_rtx);
7137
7138       SUBST (SET_SRC (x),
7139              simplify_gen_binary (IOR, int_mode,
7140                                   simplify_gen_binary (IOR, int_mode,
7141                                                        term1, term2),
7142                                   term3));
7143
7144       src = SET_SRC (x);
7145     }
7146
7147   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7148      whole thing fail.  */
7149   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7150     return src;
7151   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7152     return dest;
7153   else
7154     /* Convert this into a field assignment operation, if possible.  */
7155     return make_field_assignment (x);
7156 }
7157 \f
7158 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7159    result.  */
7160
7161 static rtx
7162 simplify_logical (rtx x)
7163 {
7164   rtx op0 = XEXP (x, 0);
7165   rtx op1 = XEXP (x, 1);
7166   scalar_int_mode mode;
7167
7168   switch (GET_CODE (x))
7169     {
7170     case AND:
7171       /* We can call simplify_and_const_int only if we don't lose
7172          any (sign) bits when converting INTVAL (op1) to
7173          "unsigned HOST_WIDE_INT".  */
7174       if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7175           && CONST_INT_P (op1)
7176           && (HWI_COMPUTABLE_MODE_P (mode)
7177               || INTVAL (op1) > 0))
7178         {
7179           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7180           if (GET_CODE (x) != AND)
7181             return x;
7182
7183           op0 = XEXP (x, 0);
7184           op1 = XEXP (x, 1);
7185         }
7186
7187       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7188          apply the distributive law and then the inverse distributive
7189          law to see if things simplify.  */
7190       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7191         {
7192           rtx result = distribute_and_simplify_rtx (x, 0);
7193           if (result)
7194             return result;
7195         }
7196       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7197         {
7198           rtx result = distribute_and_simplify_rtx (x, 1);
7199           if (result)
7200             return result;
7201         }
7202       break;
7203
7204     case IOR:
7205       /* If we have (ior (and A B) C), apply the distributive law and then
7206          the inverse distributive law to see if things simplify.  */
7207
7208       if (GET_CODE (op0) == AND)
7209         {
7210           rtx result = distribute_and_simplify_rtx (x, 0);
7211           if (result)
7212             return result;
7213         }
7214
7215       if (GET_CODE (op1) == AND)
7216         {
7217           rtx result = distribute_and_simplify_rtx (x, 1);
7218           if (result)
7219             return result;
7220         }
7221       break;
7222
7223     default:
7224       gcc_unreachable ();
7225     }
7226
7227   return x;
7228 }
7229 \f
7230 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7231    operations" because they can be replaced with two more basic operations.
7232    ZERO_EXTEND is also considered "compound" because it can be replaced with
7233    an AND operation, which is simpler, though only one operation.
7234
7235    The function expand_compound_operation is called with an rtx expression
7236    and will convert it to the appropriate shifts and AND operations,
7237    simplifying at each stage.
7238
7239    The function make_compound_operation is called to convert an expression
7240    consisting of shifts and ANDs into the equivalent compound expression.
7241    It is the inverse of this function, loosely speaking.  */
7242
7243 static rtx
7244 expand_compound_operation (rtx x)
7245 {
7246   unsigned HOST_WIDE_INT pos = 0, len;
7247   int unsignedp = 0;
7248   unsigned int modewidth;
7249   rtx tem;
7250   scalar_int_mode inner_mode;
7251
7252   switch (GET_CODE (x))
7253     {
7254     case ZERO_EXTEND:
7255       unsignedp = 1;
7256       /* FALLTHRU */
7257     case SIGN_EXTEND:
7258       /* We can't necessarily use a const_int for a multiword mode;
7259          it depends on implicitly extending the value.
7260          Since we don't know the right way to extend it,
7261          we can't tell whether the implicit way is right.
7262
7263          Even for a mode that is no wider than a const_int,
7264          we can't win, because we need to sign extend one of its bits through
7265          the rest of it, and we don't know which bit.  */
7266       if (CONST_INT_P (XEXP (x, 0)))
7267         return x;
7268
7269       /* Reject modes that aren't scalar integers because turning vector
7270          or complex modes into shifts causes problems.  */
7271       if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7272         return x;
7273
7274       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7275          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
7276          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7277          reloaded. If not for that, MEM's would very rarely be safe.
7278
7279          Reject modes bigger than a word, because we might not be able
7280          to reference a two-register group starting with an arbitrary register
7281          (and currently gen_lowpart might crash for a SUBREG).  */
7282
7283       if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7284         return x;
7285
7286       len = GET_MODE_PRECISION (inner_mode);
7287       /* If the inner object has VOIDmode (the only way this can happen
7288          is if it is an ASM_OPERANDS), we can't do anything since we don't
7289          know how much masking to do.  */
7290       if (len == 0)
7291         return x;
7292
7293       break;
7294
7295     case ZERO_EXTRACT:
7296       unsignedp = 1;
7297
7298       /* fall through */
7299
7300     case SIGN_EXTRACT:
7301       /* If the operand is a CLOBBER, just return it.  */
7302       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7303         return XEXP (x, 0);
7304
7305       if (!CONST_INT_P (XEXP (x, 1))
7306           || !CONST_INT_P (XEXP (x, 2)))
7307         return x;
7308
7309       /* Reject modes that aren't scalar integers because turning vector
7310          or complex modes into shifts causes problems.  */
7311       if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7312         return x;
7313
7314       len = INTVAL (XEXP (x, 1));
7315       pos = INTVAL (XEXP (x, 2));
7316
7317       /* This should stay within the object being extracted, fail otherwise.  */
7318       if (len + pos > GET_MODE_PRECISION (inner_mode))
7319         return x;
7320
7321       if (BITS_BIG_ENDIAN)
7322         pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7323
7324       break;
7325
7326     default:
7327       return x;
7328     }
7329
7330   /* We've rejected non-scalar operations by now.  */
7331   scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7332
7333   /* Convert sign extension to zero extension, if we know that the high
7334      bit is not set, as this is easier to optimize.  It will be converted
7335      back to cheaper alternative in make_extraction.  */
7336   if (GET_CODE (x) == SIGN_EXTEND
7337       && HWI_COMPUTABLE_MODE_P (mode)
7338       && ((nonzero_bits (XEXP (x, 0), inner_mode)
7339            & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7340           == 0))
7341     {
7342       rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7343       rtx temp2 = expand_compound_operation (temp);
7344
7345       /* Make sure this is a profitable operation.  */
7346       if (set_src_cost (x, mode, optimize_this_for_speed_p)
7347           > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7348        return temp2;
7349       else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7350                > set_src_cost (temp, mode, optimize_this_for_speed_p))
7351        return temp;
7352       else
7353        return x;
7354     }
7355
7356   /* We can optimize some special cases of ZERO_EXTEND.  */
7357   if (GET_CODE (x) == ZERO_EXTEND)
7358     {
7359       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7360          know that the last value didn't have any inappropriate bits
7361          set.  */
7362       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7363           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7364           && HWI_COMPUTABLE_MODE_P (mode)
7365           && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7366               & ~GET_MODE_MASK (inner_mode)) == 0)
7367         return XEXP (XEXP (x, 0), 0);
7368
7369       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
7370       if (GET_CODE (XEXP (x, 0)) == SUBREG
7371           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7372           && subreg_lowpart_p (XEXP (x, 0))
7373           && HWI_COMPUTABLE_MODE_P (mode)
7374           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7375               & ~GET_MODE_MASK (inner_mode)) == 0)
7376         return SUBREG_REG (XEXP (x, 0));
7377
7378       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7379          is a comparison and STORE_FLAG_VALUE permits.  This is like
7380          the first case, but it works even when MODE is larger
7381          than HOST_WIDE_INT.  */
7382       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7383           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7384           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7385           && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7386           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7387         return XEXP (XEXP (x, 0), 0);
7388
7389       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
7390       if (GET_CODE (XEXP (x, 0)) == SUBREG
7391           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7392           && subreg_lowpart_p (XEXP (x, 0))
7393           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7394           && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7395           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7396         return SUBREG_REG (XEXP (x, 0));
7397
7398     }
7399
7400   /* If we reach here, we want to return a pair of shifts.  The inner
7401      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
7402      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
7403      logical depending on the value of UNSIGNEDP.
7404
7405      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7406      converted into an AND of a shift.
7407
7408      We must check for the case where the left shift would have a negative
7409      count.  This can happen in a case like (x >> 31) & 255 on machines
7410      that can't shift by a constant.  On those machines, we would first
7411      combine the shift with the AND to produce a variable-position
7412      extraction.  Then the constant of 31 would be substituted in
7413      to produce such a position.  */
7414
7415   modewidth = GET_MODE_PRECISION (mode);
7416   if (modewidth >= pos + len)
7417     {
7418       tem = gen_lowpart (mode, XEXP (x, 0));
7419       if (!tem || GET_CODE (tem) == CLOBBER)
7420         return x;
7421       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7422                                   tem, modewidth - pos - len);
7423       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7424                                   mode, tem, modewidth - len);
7425     }
7426   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7427     tem = simplify_and_const_int (NULL_RTX, mode,
7428                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
7429                                                         mode, XEXP (x, 0),
7430                                                         pos),
7431                                   (HOST_WIDE_INT_1U << len) - 1);
7432   else
7433     /* Any other cases we can't handle.  */
7434     return x;
7435
7436   /* If we couldn't do this for some reason, return the original
7437      expression.  */
7438   if (GET_CODE (tem) == CLOBBER)
7439     return x;
7440
7441   return tem;
7442 }
7443 \f
7444 /* X is a SET which contains an assignment of one object into
7445    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7446    or certain SUBREGS). If possible, convert it into a series of
7447    logical operations.
7448
7449    We half-heartedly support variable positions, but do not at all
7450    support variable lengths.  */
7451
7452 static const_rtx
7453 expand_field_assignment (const_rtx x)
7454 {
7455   rtx inner;
7456   rtx pos;                      /* Always counts from low bit.  */
7457   int len, inner_len;
7458   rtx mask, cleared, masked;
7459   scalar_int_mode compute_mode;
7460
7461   /* Loop until we find something we can't simplify.  */
7462   while (1)
7463     {
7464       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7465           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7466         {
7467           rtx x0 = XEXP (SET_DEST (x), 0);
7468           if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7469             break;
7470           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7471           pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7472                               MAX_MODE_INT);
7473         }
7474       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7475                && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7476         {
7477           inner = XEXP (SET_DEST (x), 0);
7478           if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7479             break;
7480
7481           len = INTVAL (XEXP (SET_DEST (x), 1));
7482           pos = XEXP (SET_DEST (x), 2);
7483
7484           /* A constant position should stay within the width of INNER.  */
7485           if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7486             break;
7487
7488           if (BITS_BIG_ENDIAN)
7489             {
7490               if (CONST_INT_P (pos))
7491                 pos = GEN_INT (inner_len - len - INTVAL (pos));
7492               else if (GET_CODE (pos) == MINUS
7493                        && CONST_INT_P (XEXP (pos, 1))
7494                        && INTVAL (XEXP (pos, 1)) == inner_len - len)
7495                 /* If position is ADJUST - X, new position is X.  */
7496                 pos = XEXP (pos, 0);
7497               else
7498                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7499                                            gen_int_mode (inner_len - len,
7500                                                          GET_MODE (pos)),
7501                                            pos);
7502             }
7503         }
7504
7505       /* If the destination is a subreg that overwrites the whole of the inner
7506          register, we can move the subreg to the source.  */
7507       else if (GET_CODE (SET_DEST (x)) == SUBREG
7508                /* We need SUBREGs to compute nonzero_bits properly.  */
7509                && nonzero_sign_valid
7510                && !read_modify_subreg_p (SET_DEST (x)))
7511         {
7512           x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7513                            gen_lowpart
7514                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
7515                             SET_SRC (x)));
7516           continue;
7517         }
7518       else
7519         break;
7520
7521       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7522         inner = SUBREG_REG (inner);
7523
7524       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
7525       if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7526         {
7527           /* Don't do anything for vector or complex integral types.  */
7528           if (! FLOAT_MODE_P (GET_MODE (inner)))
7529             break;
7530
7531           /* Try to find an integral mode to pun with.  */
7532           if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7533               .exists (&compute_mode))
7534             break;
7535
7536           inner = gen_lowpart (compute_mode, inner);
7537         }
7538
7539       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
7540       if (len >= HOST_BITS_PER_WIDE_INT)
7541         break;
7542
7543       /* Don't try to compute in too wide unsupported modes.  */
7544       if (!targetm.scalar_mode_supported_p (compute_mode))
7545         break;
7546
7547       /* Now compute the equivalent expression.  Make a copy of INNER
7548          for the SET_DEST in case it is a MEM into which we will substitute;
7549          we don't want shared RTL in that case.  */
7550       mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7551                            compute_mode);
7552       cleared = simplify_gen_binary (AND, compute_mode,
7553                                      simplify_gen_unary (NOT, compute_mode,
7554                                        simplify_gen_binary (ASHIFT,
7555                                                             compute_mode,
7556                                                             mask, pos),
7557                                        compute_mode),
7558                                      inner);
7559       masked = simplify_gen_binary (ASHIFT, compute_mode,
7560                                     simplify_gen_binary (
7561                                       AND, compute_mode,
7562                                       gen_lowpart (compute_mode, SET_SRC (x)),
7563                                       mask),
7564                                     pos);
7565
7566       x = gen_rtx_SET (copy_rtx (inner),
7567                        simplify_gen_binary (IOR, compute_mode,
7568                                             cleared, masked));
7569     }
7570
7571   return x;
7572 }
7573 \f
7574 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
7575    it is an RTX that represents the (variable) starting position; otherwise,
7576    POS is the (constant) starting bit position.  Both are counted from the LSB.
7577
7578    UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7579
7580    IN_DEST is nonzero if this is a reference in the destination of a SET.
7581    This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
7582    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7583    be used.
7584
7585    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
7586    ZERO_EXTRACT should be built even for bits starting at bit 0.
7587
7588    MODE is the desired mode of the result (if IN_DEST == 0).
7589
7590    The result is an RTX for the extraction or NULL_RTX if the target
7591    can't handle it.  */
7592
7593 static rtx
7594 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7595                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7596                  int in_dest, int in_compare)
7597 {
7598   /* This mode describes the size of the storage area
7599      to fetch the overall value from.  Within that, we
7600      ignore the POS lowest bits, etc.  */
7601   machine_mode is_mode = GET_MODE (inner);
7602   machine_mode inner_mode;
7603   scalar_int_mode wanted_inner_mode;
7604   scalar_int_mode wanted_inner_reg_mode = word_mode;
7605   scalar_int_mode pos_mode = word_mode;
7606   machine_mode extraction_mode = word_mode;
7607   rtx new_rtx = 0;
7608   rtx orig_pos_rtx = pos_rtx;
7609   HOST_WIDE_INT orig_pos;
7610
7611   if (pos_rtx && CONST_INT_P (pos_rtx))
7612     pos = INTVAL (pos_rtx), pos_rtx = 0;
7613
7614   if (GET_CODE (inner) == SUBREG
7615       && subreg_lowpart_p (inner)
7616       && (paradoxical_subreg_p (inner)
7617           /* If trying or potentionally trying to extract
7618              bits outside of is_mode, don't look through
7619              non-paradoxical SUBREGs.  See PR82192.  */
7620           || (pos_rtx == NULL_RTX
7621               && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7622     {
7623       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7624          consider just the QI as the memory to extract from.
7625          The subreg adds or removes high bits; its mode is
7626          irrelevant to the meaning of this extraction,
7627          since POS and LEN count from the lsb.  */
7628       if (MEM_P (SUBREG_REG (inner)))
7629         is_mode = GET_MODE (SUBREG_REG (inner));
7630       inner = SUBREG_REG (inner);
7631     }
7632   else if (GET_CODE (inner) == ASHIFT
7633            && CONST_INT_P (XEXP (inner, 1))
7634            && pos_rtx == 0 && pos == 0
7635            && len > UINTVAL (XEXP (inner, 1)))
7636     {
7637       /* We're extracting the least significant bits of an rtx
7638          (ashift X (const_int C)), where LEN > C.  Extract the
7639          least significant (LEN - C) bits of X, giving an rtx
7640          whose mode is MODE, then shift it left C times.  */
7641       new_rtx = make_extraction (mode, XEXP (inner, 0),
7642                              0, 0, len - INTVAL (XEXP (inner, 1)),
7643                              unsignedp, in_dest, in_compare);
7644       if (new_rtx != 0)
7645         return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7646     }
7647   else if (GET_CODE (inner) == TRUNCATE
7648            /* If trying or potentionally trying to extract
7649               bits outside of is_mode, don't look through
7650               TRUNCATE.  See PR82192.  */
7651            && pos_rtx == NULL_RTX
7652            && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7653     inner = XEXP (inner, 0);
7654
7655   inner_mode = GET_MODE (inner);
7656
7657   /* See if this can be done without an extraction.  We never can if the
7658      width of the field is not the same as that of some integer mode. For
7659      registers, we can only avoid the extraction if the position is at the
7660      low-order bit and this is either not in the destination or we have the
7661      appropriate STRICT_LOW_PART operation available.
7662
7663      For MEM, we can avoid an extract if the field starts on an appropriate
7664      boundary and we can change the mode of the memory reference.  */
7665
7666   scalar_int_mode tmode;
7667   if (int_mode_for_size (len, 1).exists (&tmode)
7668       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7669            && !MEM_P (inner)
7670            && (pos == 0 || REG_P (inner))
7671            && (inner_mode == tmode
7672                || !REG_P (inner)
7673                || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7674                || reg_truncated_to_mode (tmode, inner))
7675            && (! in_dest
7676                || (REG_P (inner)
7677                    && have_insn_for (STRICT_LOW_PART, tmode))))
7678           || (MEM_P (inner) && pos_rtx == 0
7679               && (pos
7680                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7681                      : BITS_PER_UNIT)) == 0
7682               /* We can't do this if we are widening INNER_MODE (it
7683                  may not be aligned, for one thing).  */
7684               && !paradoxical_subreg_p (tmode, inner_mode)
7685               && (inner_mode == tmode
7686                   || (! mode_dependent_address_p (XEXP (inner, 0),
7687                                                   MEM_ADDR_SPACE (inner))
7688                       && ! MEM_VOLATILE_P (inner))))))
7689     {
7690       /* If INNER is a MEM, make a new MEM that encompasses just the desired
7691          field.  If the original and current mode are the same, we need not
7692          adjust the offset.  Otherwise, we do if bytes big endian.
7693
7694          If INNER is not a MEM, get a piece consisting of just the field
7695          of interest (in this case POS % BITS_PER_WORD must be 0).  */
7696
7697       if (MEM_P (inner))
7698         {
7699           poly_int64 offset;
7700
7701           /* POS counts from lsb, but make OFFSET count in memory order.  */
7702           if (BYTES_BIG_ENDIAN)
7703             offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7704                                                - len - pos);
7705           else
7706             offset = pos / BITS_PER_UNIT;
7707
7708           new_rtx = adjust_address_nv (inner, tmode, offset);
7709         }
7710       else if (REG_P (inner))
7711         {
7712           if (tmode != inner_mode)
7713             {
7714               /* We can't call gen_lowpart in a DEST since we
7715                  always want a SUBREG (see below) and it would sometimes
7716                  return a new hard register.  */
7717               if (pos || in_dest)
7718                 {
7719                   poly_uint64 offset
7720                     = subreg_offset_from_lsb (tmode, inner_mode, pos);
7721
7722                   /* Avoid creating invalid subregs, for example when
7723                      simplifying (x>>32)&255.  */
7724                   if (!validate_subreg (tmode, inner_mode, inner, offset))
7725                     return NULL_RTX;
7726
7727                   new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7728                 }
7729               else
7730                 new_rtx = gen_lowpart (tmode, inner);
7731             }
7732           else
7733             new_rtx = inner;
7734         }
7735       else
7736         new_rtx = force_to_mode (inner, tmode,
7737                                  len >= HOST_BITS_PER_WIDE_INT
7738                                  ? HOST_WIDE_INT_M1U
7739                                  : (HOST_WIDE_INT_1U << len) - 1, 0);
7740
7741       /* If this extraction is going into the destination of a SET,
7742          make a STRICT_LOW_PART unless we made a MEM.  */
7743
7744       if (in_dest)
7745         return (MEM_P (new_rtx) ? new_rtx
7746                 : (GET_CODE (new_rtx) != SUBREG
7747                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
7748                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7749
7750       if (mode == tmode)
7751         return new_rtx;
7752
7753       if (CONST_SCALAR_INT_P (new_rtx))
7754         return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7755                                          mode, new_rtx, tmode);
7756
7757       /* If we know that no extraneous bits are set, and that the high
7758          bit is not set, convert the extraction to the cheaper of
7759          sign and zero extension, that are equivalent in these cases.  */
7760       if (flag_expensive_optimizations
7761           && (HWI_COMPUTABLE_MODE_P (tmode)
7762               && ((nonzero_bits (new_rtx, tmode)
7763                    & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7764                   == 0)))
7765         {
7766           rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7767           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7768
7769           /* Prefer ZERO_EXTENSION, since it gives more information to
7770              backends.  */
7771           if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7772               <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7773             return temp;
7774           return temp1;
7775         }
7776
7777       /* Otherwise, sign- or zero-extend unless we already are in the
7778          proper mode.  */
7779
7780       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7781                              mode, new_rtx));
7782     }
7783
7784   /* Unless this is a COMPARE or we have a funny memory reference,
7785      don't do anything with zero-extending field extracts starting at
7786      the low-order bit since they are simple AND operations.  */
7787   if (pos_rtx == 0 && pos == 0 && ! in_dest
7788       && ! in_compare && unsignedp)
7789     return 0;
7790
7791   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7792      if the position is not a constant and the length is not 1.  In all
7793      other cases, we would only be going outside our object in cases when
7794      an original shift would have been undefined.  */
7795   if (MEM_P (inner)
7796       && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7797           || (pos_rtx != 0 && len != 1)))
7798     return 0;
7799
7800   enum extraction_pattern pattern = (in_dest ? EP_insv
7801                                      : unsignedp ? EP_extzv : EP_extv);
7802
7803   /* If INNER is not from memory, we want it to have the mode of a register
7804      extraction pattern's structure operand, or word_mode if there is no
7805      such pattern.  The same applies to extraction_mode and pos_mode
7806      and their respective operands.
7807
7808      For memory, assume that the desired extraction_mode and pos_mode
7809      are the same as for a register operation, since at present we don't
7810      have named patterns for aligned memory structures.  */
7811   struct extraction_insn insn;
7812   unsigned int inner_size;
7813   if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7814       && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7815     {
7816       wanted_inner_reg_mode = insn.struct_mode.require ();
7817       pos_mode = insn.pos_mode;
7818       extraction_mode = insn.field_mode;
7819     }
7820
7821   /* Never narrow an object, since that might not be safe.  */
7822
7823   if (mode != VOIDmode
7824       && partial_subreg_p (extraction_mode, mode))
7825     extraction_mode = mode;
7826
7827   if (!MEM_P (inner))
7828     wanted_inner_mode = wanted_inner_reg_mode;
7829   else
7830     {
7831       /* Be careful not to go beyond the extracted object and maintain the
7832          natural alignment of the memory.  */
7833       wanted_inner_mode = smallest_int_mode_for_size (len);
7834       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7835              > GET_MODE_BITSIZE (wanted_inner_mode))
7836         wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7837     }
7838
7839   orig_pos = pos;
7840
7841   if (BITS_BIG_ENDIAN)
7842     {
7843       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7844          BITS_BIG_ENDIAN style.  If position is constant, compute new
7845          position.  Otherwise, build subtraction.
7846          Note that POS is relative to the mode of the original argument.
7847          If it's a MEM we need to recompute POS relative to that.
7848          However, if we're extracting from (or inserting into) a register,
7849          we want to recompute POS relative to wanted_inner_mode.  */
7850       int width;
7851       if (!MEM_P (inner))
7852         width = GET_MODE_BITSIZE (wanted_inner_mode);
7853       else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7854         return NULL_RTX;
7855
7856       if (pos_rtx == 0)
7857         pos = width - len - pos;
7858       else
7859         pos_rtx
7860           = gen_rtx_MINUS (GET_MODE (pos_rtx),
7861                            gen_int_mode (width - len, GET_MODE (pos_rtx)),
7862                            pos_rtx);
7863       /* POS may be less than 0 now, but we check for that below.
7864          Note that it can only be less than 0 if !MEM_P (inner).  */
7865     }
7866
7867   /* If INNER has a wider mode, and this is a constant extraction, try to
7868      make it smaller and adjust the byte to point to the byte containing
7869      the value.  */
7870   if (wanted_inner_mode != VOIDmode
7871       && inner_mode != wanted_inner_mode
7872       && ! pos_rtx
7873       && partial_subreg_p (wanted_inner_mode, is_mode)
7874       && MEM_P (inner)
7875       && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7876       && ! MEM_VOLATILE_P (inner))
7877     {
7878       poly_int64 offset = 0;
7879
7880       /* The computations below will be correct if the machine is big
7881          endian in both bits and bytes or little endian in bits and bytes.
7882          If it is mixed, we must adjust.  */
7883
7884       /* If bytes are big endian and we had a paradoxical SUBREG, we must
7885          adjust OFFSET to compensate.  */
7886       if (BYTES_BIG_ENDIAN
7887           && paradoxical_subreg_p (is_mode, inner_mode))
7888         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7889
7890       /* We can now move to the desired byte.  */
7891       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7892                 * GET_MODE_SIZE (wanted_inner_mode);
7893       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7894
7895       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7896           && is_mode != wanted_inner_mode)
7897         offset = (GET_MODE_SIZE (is_mode)
7898                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
7899
7900       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7901     }
7902
7903   /* If INNER is not memory, get it into the proper mode.  If we are changing
7904      its mode, POS must be a constant and smaller than the size of the new
7905      mode.  */
7906   else if (!MEM_P (inner))
7907     {
7908       /* On the LHS, don't create paradoxical subregs implicitely truncating
7909          the register unless TARGET_TRULY_NOOP_TRUNCATION.  */
7910       if (in_dest
7911           && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7912                                              wanted_inner_mode))
7913         return NULL_RTX;
7914
7915       if (GET_MODE (inner) != wanted_inner_mode
7916           && (pos_rtx != 0
7917               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7918         return NULL_RTX;
7919
7920       if (orig_pos < 0)
7921         return NULL_RTX;
7922
7923       inner = force_to_mode (inner, wanted_inner_mode,
7924                              pos_rtx
7925                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7926                              ? HOST_WIDE_INT_M1U
7927                              : (((HOST_WIDE_INT_1U << len) - 1)
7928                                 << orig_pos),
7929                              0);
7930     }
7931
7932   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
7933      have to zero extend.  Otherwise, we can just use a SUBREG.
7934
7935      We dealt with constant rtxes earlier, so pos_rtx cannot
7936      have VOIDmode at this point.  */
7937   if (pos_rtx != 0
7938       && (GET_MODE_SIZE (pos_mode)
7939           > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7940     {
7941       rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7942                                      GET_MODE (pos_rtx));
7943
7944       /* If we know that no extraneous bits are set, and that the high
7945          bit is not set, convert extraction to cheaper one - either
7946          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7947          cases.  */
7948       if (flag_expensive_optimizations
7949           && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7950               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7951                    & ~(((unsigned HOST_WIDE_INT)
7952                         GET_MODE_MASK (GET_MODE (pos_rtx)))
7953                        >> 1))
7954                   == 0)))
7955         {
7956           rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7957                                           GET_MODE (pos_rtx));
7958
7959           /* Prefer ZERO_EXTENSION, since it gives more information to
7960              backends.  */
7961           if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7962               < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7963             temp = temp1;
7964         }
7965       pos_rtx = temp;
7966     }
7967
7968   /* Make POS_RTX unless we already have it and it is correct.  If we don't
7969      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7970      be a CONST_INT.  */
7971   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7972     pos_rtx = orig_pos_rtx;
7973
7974   else if (pos_rtx == 0)
7975     pos_rtx = GEN_INT (pos);
7976
7977   /* Make the required operation.  See if we can use existing rtx.  */
7978   new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7979                          extraction_mode, inner, GEN_INT (len), pos_rtx);
7980   if (! in_dest)
7981     new_rtx = gen_lowpart (mode, new_rtx);
7982
7983   return new_rtx;
7984 }
7985 \f
7986 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7987    can be commuted with any other operations in X.  Return X without
7988    that shift if so.  */
7989
7990 static rtx
7991 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7992 {
7993   enum rtx_code code = GET_CODE (x);
7994   rtx tem;
7995
7996   switch (code)
7997     {
7998     case ASHIFT:
7999       /* This is the shift itself.  If it is wide enough, we will return
8000          either the value being shifted if the shift count is equal to
8001          COUNT or a shift for the difference.  */
8002       if (CONST_INT_P (XEXP (x, 1))
8003           && INTVAL (XEXP (x, 1)) >= count)
8004         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
8005                                      INTVAL (XEXP (x, 1)) - count);
8006       break;
8007
8008     case NEG:  case NOT:
8009       if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8010         return simplify_gen_unary (code, mode, tem, mode);
8011
8012       break;
8013
8014     case PLUS:  case IOR:  case XOR:  case AND:
8015       /* If we can safely shift this constant and we find the inner shift,
8016          make a new operation.  */
8017       if (CONST_INT_P (XEXP (x, 1))
8018           && (UINTVAL (XEXP (x, 1))
8019               & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
8020           && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8021         {
8022           HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
8023           return simplify_gen_binary (code, mode, tem,
8024                                       gen_int_mode (val, mode));
8025         }
8026       break;
8027
8028     default:
8029       break;
8030     }
8031
8032   return 0;
8033 }
8034 \f
8035 /* Subroutine of make_compound_operation.  *X_PTR is the rtx at the current
8036    level of the expression and MODE is its mode.  IN_CODE is as for
8037    make_compound_operation.  *NEXT_CODE_PTR is the value of IN_CODE
8038    that should be used when recursing on operands of *X_PTR.
8039
8040    There are two possible actions:
8041
8042    - Return null.  This tells the caller to recurse on *X_PTR with IN_CODE
8043      equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
8044
8045    - Return a new rtx, which the caller returns directly.  */
8046
8047 static rtx
8048 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
8049                              enum rtx_code in_code,
8050                              enum rtx_code *next_code_ptr)
8051 {
8052   rtx x = *x_ptr;
8053   enum rtx_code next_code = *next_code_ptr;
8054   enum rtx_code code = GET_CODE (x);
8055   int mode_width = GET_MODE_PRECISION (mode);
8056   rtx rhs, lhs;
8057   rtx new_rtx = 0;
8058   int i;
8059   rtx tem;
8060   scalar_int_mode inner_mode;
8061   bool equality_comparison = false;
8062
8063   if (in_code == EQ)
8064     {
8065       equality_comparison = true;
8066       in_code = COMPARE;
8067     }
8068
8069   /* Process depending on the code of this operation.  If NEW is set
8070      nonzero, it will be returned.  */
8071
8072   switch (code)
8073     {
8074     case ASHIFT:
8075       /* Convert shifts by constants into multiplications if inside
8076          an address.  */
8077       if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
8078           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8079           && INTVAL (XEXP (x, 1)) >= 0)
8080         {
8081           HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
8082           HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
8083
8084           new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8085           if (GET_CODE (new_rtx) == NEG)
8086             {
8087               new_rtx = XEXP (new_rtx, 0);
8088               multval = -multval;
8089             }
8090           multval = trunc_int_for_mode (multval, mode);
8091           new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8092         }
8093       break;
8094
8095     case PLUS:
8096       lhs = XEXP (x, 0);
8097       rhs = XEXP (x, 1);
8098       lhs = make_compound_operation (lhs, next_code);
8099       rhs = make_compound_operation (rhs, next_code);
8100       if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
8101         {
8102           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
8103                                      XEXP (lhs, 1));
8104           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8105         }
8106       else if (GET_CODE (lhs) == MULT
8107                && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
8108         {
8109           tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
8110                                      simplify_gen_unary (NEG, mode,
8111                                                          XEXP (lhs, 1),
8112                                                          mode));
8113           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8114         }
8115       else
8116         {
8117           SUBST (XEXP (x, 0), lhs);
8118           SUBST (XEXP (x, 1), rhs);
8119         }
8120       maybe_swap_commutative_operands (x);
8121       return x;
8122
8123     case MINUS:
8124       lhs = XEXP (x, 0);
8125       rhs = XEXP (x, 1);
8126       lhs = make_compound_operation (lhs, next_code);
8127       rhs = make_compound_operation (rhs, next_code);
8128       if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
8129         {
8130           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
8131                                      XEXP (rhs, 1));
8132           return simplify_gen_binary (PLUS, mode, tem, lhs);
8133         }
8134       else if (GET_CODE (rhs) == MULT
8135                && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
8136         {
8137           tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
8138                                      simplify_gen_unary (NEG, mode,
8139                                                          XEXP (rhs, 1),
8140                                                          mode));
8141           return simplify_gen_binary (PLUS, mode, tem, lhs);
8142         }
8143       else
8144         {
8145           SUBST (XEXP (x, 0), lhs);
8146           SUBST (XEXP (x, 1), rhs);
8147           return x;
8148         }
8149
8150     case AND:
8151       /* If the second operand is not a constant, we can't do anything
8152          with it.  */
8153       if (!CONST_INT_P (XEXP (x, 1)))
8154         break;
8155
8156       /* If the constant is a power of two minus one and the first operand
8157          is a logical right shift, make an extraction.  */
8158       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8159           && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8160         {
8161           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8162           new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8163                                      i, 1, 0, in_code == COMPARE);
8164         }
8165
8166       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
8167       else if (GET_CODE (XEXP (x, 0)) == SUBREG
8168                && subreg_lowpart_p (XEXP (x, 0))
8169                && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8170                                           &inner_mode)
8171                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8172                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8173         {
8174           rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8175           new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8176           new_rtx = make_extraction (inner_mode, new_rtx, 0,
8177                                      XEXP (inner_x0, 1),
8178                                      i, 1, 0, in_code == COMPARE);
8179
8180           /* If we narrowed the mode when dropping the subreg, then we lose.  */
8181           if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8182             new_rtx = NULL;
8183
8184           /* If that didn't give anything, see if the AND simplifies on
8185              its own.  */
8186           if (!new_rtx && i >= 0)
8187             {
8188               new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8189               new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8190                                          0, in_code == COMPARE);
8191             }
8192         }
8193       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
8194       else if ((GET_CODE (XEXP (x, 0)) == XOR
8195                 || GET_CODE (XEXP (x, 0)) == IOR)
8196                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8197                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8198                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8199         {
8200           /* Apply the distributive law, and then try to make extractions.  */
8201           new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8202                                     gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8203                                                  XEXP (x, 1)),
8204                                     gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8205                                                  XEXP (x, 1)));
8206           new_rtx = make_compound_operation (new_rtx, in_code);
8207         }
8208
8209       /* If we are have (and (rotate X C) M) and C is larger than the number
8210          of bits in M, this is an extraction.  */
8211
8212       else if (GET_CODE (XEXP (x, 0)) == ROTATE
8213                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8214                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8215                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8216         {
8217           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8218           new_rtx = make_extraction (mode, new_rtx,
8219                                      (GET_MODE_PRECISION (mode)
8220                                       - INTVAL (XEXP (XEXP (x, 0), 1))),
8221                                      NULL_RTX, i, 1, 0, in_code == COMPARE);
8222         }
8223
8224       /* On machines without logical shifts, if the operand of the AND is
8225          a logical shift and our mask turns off all the propagated sign
8226          bits, we can replace the logical shift with an arithmetic shift.  */
8227       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8228                && !have_insn_for (LSHIFTRT, mode)
8229                && have_insn_for (ASHIFTRT, mode)
8230                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8231                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8232                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8233                && mode_width <= HOST_BITS_PER_WIDE_INT)
8234         {
8235           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8236
8237           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8238           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8239             SUBST (XEXP (x, 0),
8240                    gen_rtx_ASHIFTRT (mode,
8241                                      make_compound_operation (XEXP (XEXP (x,
8242                                                                           0),
8243                                                                     0),
8244                                                               next_code),
8245                                      XEXP (XEXP (x, 0), 1)));
8246         }
8247
8248       /* If the constant is one less than a power of two, this might be
8249          representable by an extraction even if no shift is present.
8250          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8251          we are in a COMPARE.  */
8252       else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8253         new_rtx = make_extraction (mode,
8254                                    make_compound_operation (XEXP (x, 0),
8255                                                             next_code),
8256                                    0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8257
8258       /* If we are in a comparison and this is an AND with a power of two,
8259          convert this into the appropriate bit extract.  */
8260       else if (in_code == COMPARE
8261                && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8262                && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8263         new_rtx = make_extraction (mode,
8264                                    make_compound_operation (XEXP (x, 0),
8265                                                             next_code),
8266                                    i, NULL_RTX, 1, 1, 0, 1);
8267
8268       /* If the one operand is a paradoxical subreg of a register or memory and
8269          the constant (limited to the smaller mode) has only zero bits where
8270          the sub expression has known zero bits, this can be expressed as
8271          a zero_extend.  */
8272       else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8273         {
8274           rtx sub;
8275
8276           sub = XEXP (XEXP (x, 0), 0);
8277           machine_mode sub_mode = GET_MODE (sub);
8278           int sub_width;
8279           if ((REG_P (sub) || MEM_P (sub))
8280               && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8281               && sub_width < mode_width)
8282             {
8283               unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8284               unsigned HOST_WIDE_INT mask;
8285
8286               /* original AND constant with all the known zero bits set */
8287               mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8288               if ((mask & mode_mask) == mode_mask)
8289                 {
8290                   new_rtx = make_compound_operation (sub, next_code);
8291                   new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8292                                              1, 0, in_code == COMPARE);
8293                 }
8294             }
8295         }
8296
8297       break;
8298
8299     case LSHIFTRT:
8300       /* If the sign bit is known to be zero, replace this with an
8301          arithmetic shift.  */
8302       if (have_insn_for (ASHIFTRT, mode)
8303           && ! have_insn_for (LSHIFTRT, mode)
8304           && mode_width <= HOST_BITS_PER_WIDE_INT
8305           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8306         {
8307           new_rtx = gen_rtx_ASHIFTRT (mode,
8308                                       make_compound_operation (XEXP (x, 0),
8309                                                                next_code),
8310                                       XEXP (x, 1));
8311           break;
8312         }
8313
8314       /* fall through */
8315
8316     case ASHIFTRT:
8317       lhs = XEXP (x, 0);
8318       rhs = XEXP (x, 1);
8319
8320       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8321          this is a SIGN_EXTRACT.  */
8322       if (CONST_INT_P (rhs)
8323           && GET_CODE (lhs) == ASHIFT
8324           && CONST_INT_P (XEXP (lhs, 1))
8325           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8326           && INTVAL (XEXP (lhs, 1)) >= 0
8327           && INTVAL (rhs) < mode_width)
8328         {
8329           new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8330           new_rtx = make_extraction (mode, new_rtx,
8331                                      INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8332                                      NULL_RTX, mode_width - INTVAL (rhs),
8333                                      code == LSHIFTRT, 0, in_code == COMPARE);
8334           break;
8335         }
8336
8337       /* See if we have operations between an ASHIFTRT and an ASHIFT.
8338          If so, try to merge the shifts into a SIGN_EXTEND.  We could
8339          also do this for some cases of SIGN_EXTRACT, but it doesn't
8340          seem worth the effort; the case checked for occurs on Alpha.  */
8341
8342       if (!OBJECT_P (lhs)
8343           && ! (GET_CODE (lhs) == SUBREG
8344                 && (OBJECT_P (SUBREG_REG (lhs))))
8345           && CONST_INT_P (rhs)
8346           && INTVAL (rhs) >= 0
8347           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8348           && INTVAL (rhs) < mode_width
8349           && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8350         new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8351                                                                   next_code),
8352                                    0, NULL_RTX, mode_width - INTVAL (rhs),
8353                                    code == LSHIFTRT, 0, in_code == COMPARE);
8354
8355       break;
8356
8357     case SUBREG:
8358       /* Call ourselves recursively on the inner expression.  If we are
8359          narrowing the object and it has a different RTL code from
8360          what it originally did, do this SUBREG as a force_to_mode.  */
8361       {
8362         rtx inner = SUBREG_REG (x), simplified;
8363         enum rtx_code subreg_code = in_code;
8364
8365         /* If the SUBREG is masking of a logical right shift,
8366            make an extraction.  */
8367         if (GET_CODE (inner) == LSHIFTRT
8368             && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8369             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8370             && CONST_INT_P (XEXP (inner, 1))
8371             && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8372             && subreg_lowpart_p (x))
8373           {
8374             new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8375             int width = GET_MODE_PRECISION (inner_mode)
8376                         - INTVAL (XEXP (inner, 1));
8377             if (width > mode_width)
8378               width = mode_width;
8379             new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8380                                        width, 1, 0, in_code == COMPARE);
8381             break;
8382           }
8383
8384         /* If in_code is COMPARE, it isn't always safe to pass it through
8385            to the recursive make_compound_operation call.  */
8386         if (subreg_code == COMPARE
8387             && (!subreg_lowpart_p (x)
8388                 || GET_CODE (inner) == SUBREG
8389                 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8390                    is (const_int 0), rather than
8391                    (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8392                    Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8393                    for non-equality comparisons against 0 is not equivalent
8394                    to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0).  */
8395                 || (GET_CODE (inner) == AND
8396                     && CONST_INT_P (XEXP (inner, 1))
8397                     && partial_subreg_p (x)
8398                     && exact_log2 (UINTVAL (XEXP (inner, 1)))
8399                        >= GET_MODE_BITSIZE (mode) - 1)))
8400           subreg_code = SET;
8401
8402         tem = make_compound_operation (inner, subreg_code);
8403
8404         simplified
8405           = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8406         if (simplified)
8407           tem = simplified;
8408
8409         if (GET_CODE (tem) != GET_CODE (inner)
8410             && partial_subreg_p (x)
8411             && subreg_lowpart_p (x))
8412           {
8413             rtx newer
8414               = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8415
8416             /* If we have something other than a SUBREG, we might have
8417                done an expansion, so rerun ourselves.  */
8418             if (GET_CODE (newer) != SUBREG)
8419               newer = make_compound_operation (newer, in_code);
8420
8421             /* force_to_mode can expand compounds.  If it just re-expanded
8422                the compound, use gen_lowpart to convert to the desired
8423                mode.  */
8424             if (rtx_equal_p (newer, x)
8425                 /* Likewise if it re-expanded the compound only partially.
8426                    This happens for SUBREG of ZERO_EXTRACT if they extract
8427                    the same number of bits.  */
8428                 || (GET_CODE (newer) == SUBREG
8429                     && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8430                         || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8431                     && GET_CODE (inner) == AND
8432                     && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8433               return gen_lowpart (GET_MODE (x), tem);
8434
8435             return newer;
8436           }
8437
8438         if (simplified)
8439           return tem;
8440       }
8441       break;
8442
8443     default:
8444       break;
8445     }
8446
8447   if (new_rtx)
8448     *x_ptr = gen_lowpart (mode, new_rtx);
8449   *next_code_ptr = next_code;
8450   return NULL_RTX;
8451 }
8452
8453 /* Look at the expression rooted at X.  Look for expressions
8454    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8455    Form these expressions.
8456
8457    Return the new rtx, usually just X.
8458
8459    Also, for machines like the VAX that don't have logical shift insns,
8460    try to convert logical to arithmetic shift operations in cases where
8461    they are equivalent.  This undoes the canonicalizations to logical
8462    shifts done elsewhere.
8463
8464    We try, as much as possible, to re-use rtl expressions to save memory.
8465
8466    IN_CODE says what kind of expression we are processing.  Normally, it is
8467    SET.  In a memory address it is MEM.  When processing the arguments of
8468    a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8469    precisely it is an equality comparison against zero.  */
8470
8471 rtx
8472 make_compound_operation (rtx x, enum rtx_code in_code)
8473 {
8474   enum rtx_code code = GET_CODE (x);
8475   const char *fmt;
8476   int i, j;
8477   enum rtx_code next_code;
8478   rtx new_rtx, tem;
8479
8480   /* Select the code to be used in recursive calls.  Once we are inside an
8481      address, we stay there.  If we have a comparison, set to COMPARE,
8482      but once inside, go back to our default of SET.  */
8483
8484   next_code = (code == MEM ? MEM
8485                : ((code == COMPARE || COMPARISON_P (x))
8486                   && XEXP (x, 1) == const0_rtx) ? COMPARE
8487                : in_code == COMPARE || in_code == EQ ? SET : in_code);
8488
8489   scalar_int_mode mode;
8490   if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8491     {
8492       rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8493                                                  &next_code);
8494       if (new_rtx)
8495         return new_rtx;
8496       code = GET_CODE (x);
8497     }
8498
8499   /* Now recursively process each operand of this operation.  We need to
8500      handle ZERO_EXTEND specially so that we don't lose track of the
8501      inner mode.  */
8502   if (code == ZERO_EXTEND)
8503     {
8504       new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8505       tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8506                                             new_rtx, GET_MODE (XEXP (x, 0)));
8507       if (tem)
8508         return tem;
8509       SUBST (XEXP (x, 0), new_rtx);
8510       return x;
8511     }
8512
8513   fmt = GET_RTX_FORMAT (code);
8514   for (i = 0; i < GET_RTX_LENGTH (code); i++)
8515     if (fmt[i] == 'e')
8516       {
8517         new_rtx = make_compound_operation (XEXP (x, i), next_code);
8518         SUBST (XEXP (x, i), new_rtx);
8519       }
8520     else if (fmt[i] == 'E')
8521       for (j = 0; j < XVECLEN (x, i); j++)
8522         {
8523           new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8524           SUBST (XVECEXP (x, i, j), new_rtx);
8525         }
8526
8527   maybe_swap_commutative_operands (x);
8528   return x;
8529 }
8530 \f
8531 /* Given M see if it is a value that would select a field of bits
8532    within an item, but not the entire word.  Return -1 if not.
8533    Otherwise, return the starting position of the field, where 0 is the
8534    low-order bit.
8535
8536    *PLEN is set to the length of the field.  */
8537
8538 static int
8539 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8540 {
8541   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
8542   int pos = m ? ctz_hwi (m) : -1;
8543   int len = 0;
8544
8545   if (pos >= 0)
8546     /* Now shift off the low-order zero bits and see if we have a
8547        power of two minus 1.  */
8548     len = exact_log2 ((m >> pos) + 1);
8549
8550   if (len <= 0)
8551     pos = -1;
8552
8553   *plen = len;
8554   return pos;
8555 }
8556 \f
8557 /* If X refers to a register that equals REG in value, replace these
8558    references with REG.  */
8559 static rtx
8560 canon_reg_for_combine (rtx x, rtx reg)
8561 {
8562   rtx op0, op1, op2;
8563   const char *fmt;
8564   int i;
8565   bool copied;
8566
8567   enum rtx_code code = GET_CODE (x);
8568   switch (GET_RTX_CLASS (code))
8569     {
8570     case RTX_UNARY:
8571       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8572       if (op0 != XEXP (x, 0))
8573         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8574                                    GET_MODE (reg));
8575       break;
8576
8577     case RTX_BIN_ARITH:
8578     case RTX_COMM_ARITH:
8579       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8580       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8581       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8582         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8583       break;
8584
8585     case RTX_COMPARE:
8586     case RTX_COMM_COMPARE:
8587       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8588       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8589       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8590         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8591                                         GET_MODE (op0), op0, op1);
8592       break;
8593
8594     case RTX_TERNARY:
8595     case RTX_BITFIELD_OPS:
8596       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8597       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8598       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8599       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8600         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8601                                      GET_MODE (op0), op0, op1, op2);
8602       /* FALLTHRU */
8603
8604     case RTX_OBJ:
8605       if (REG_P (x))
8606         {
8607           if (rtx_equal_p (get_last_value (reg), x)
8608               || rtx_equal_p (reg, get_last_value (x)))
8609             return reg;
8610           else
8611             break;
8612         }
8613
8614       /* fall through */
8615
8616     default:
8617       fmt = GET_RTX_FORMAT (code);
8618       copied = false;
8619       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8620         if (fmt[i] == 'e')
8621           {
8622             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8623             if (op != XEXP (x, i))
8624               {
8625                 if (!copied)
8626                   {
8627                     copied = true;
8628                     x = copy_rtx (x);
8629                   }
8630                 XEXP (x, i) = op;
8631               }
8632           }
8633         else if (fmt[i] == 'E')
8634           {
8635             int j;
8636             for (j = 0; j < XVECLEN (x, i); j++)
8637               {
8638                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8639                 if (op != XVECEXP (x, i, j))
8640                   {
8641                     if (!copied)
8642                       {
8643                         copied = true;
8644                         x = copy_rtx (x);
8645                       }
8646                     XVECEXP (x, i, j) = op;
8647                   }
8648               }
8649           }
8650
8651       break;
8652     }
8653
8654   return x;
8655 }
8656
8657 /* Return X converted to MODE.  If the value is already truncated to
8658    MODE we can just return a subreg even though in the general case we
8659    would need an explicit truncation.  */
8660
8661 static rtx
8662 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8663 {
8664   if (!CONST_INT_P (x)
8665       && partial_subreg_p (mode, GET_MODE (x))
8666       && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8667       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8668     {
8669       /* Bit-cast X into an integer mode.  */
8670       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8671         x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8672       x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8673                               x, GET_MODE (x));
8674     }
8675
8676   return gen_lowpart (mode, x);
8677 }
8678
8679 /* See if X can be simplified knowing that we will only refer to it in
8680    MODE and will only refer to those bits that are nonzero in MASK.
8681    If other bits are being computed or if masking operations are done
8682    that select a superset of the bits in MASK, they can sometimes be
8683    ignored.
8684
8685    Return a possibly simplified expression, but always convert X to
8686    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
8687
8688    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8689    are all off in X.  This is used when X will be complemented, by either
8690    NOT, NEG, or XOR.  */
8691
8692 static rtx
8693 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8694                int just_select)
8695 {
8696   enum rtx_code code = GET_CODE (x);
8697   int next_select = just_select || code == XOR || code == NOT || code == NEG;
8698   machine_mode op_mode;
8699   unsigned HOST_WIDE_INT nonzero;
8700
8701   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
8702      code below will do the wrong thing since the mode of such an
8703      expression is VOIDmode.
8704
8705      Also do nothing if X is a CLOBBER; this can happen if X was
8706      the return value from a call to gen_lowpart.  */
8707   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8708     return x;
8709
8710   /* We want to perform the operation in its present mode unless we know
8711      that the operation is valid in MODE, in which case we do the operation
8712      in MODE.  */
8713   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8714               && have_insn_for (code, mode))
8715              ? mode : GET_MODE (x));
8716
8717   /* It is not valid to do a right-shift in a narrower mode
8718      than the one it came in with.  */
8719   if ((code == LSHIFTRT || code == ASHIFTRT)
8720       && partial_subreg_p (mode, GET_MODE (x)))
8721     op_mode = GET_MODE (x);
8722
8723   /* Truncate MASK to fit OP_MODE.  */
8724   if (op_mode)
8725     mask &= GET_MODE_MASK (op_mode);
8726
8727   /* Determine what bits of X are guaranteed to be (non)zero.  */
8728   nonzero = nonzero_bits (x, mode);
8729
8730   /* If none of the bits in X are needed, return a zero.  */
8731   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8732     x = const0_rtx;
8733
8734   /* If X is a CONST_INT, return a new one.  Do this here since the
8735      test below will fail.  */
8736   if (CONST_INT_P (x))
8737     {
8738       if (SCALAR_INT_MODE_P (mode))
8739         return gen_int_mode (INTVAL (x) & mask, mode);
8740       else
8741         {
8742           x = GEN_INT (INTVAL (x) & mask);
8743           return gen_lowpart_common (mode, x);
8744         }
8745     }
8746
8747   /* If X is narrower than MODE and we want all the bits in X's mode, just
8748      get X in the proper mode.  */
8749   if (paradoxical_subreg_p (mode, GET_MODE (x))
8750       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8751     return gen_lowpart (mode, x);
8752
8753   /* We can ignore the effect of a SUBREG if it narrows the mode or
8754      if the constant masks to zero all the bits the mode doesn't have.  */
8755   if (GET_CODE (x) == SUBREG
8756       && subreg_lowpart_p (x)
8757       && (partial_subreg_p (x)
8758           || (mask
8759               & GET_MODE_MASK (GET_MODE (x))
8760               & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8761     return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8762
8763   scalar_int_mode int_mode, xmode;
8764   if (is_a <scalar_int_mode> (mode, &int_mode)
8765       && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8766     /* OP_MODE is either MODE or XMODE, so it must be a scalar
8767        integer too.  */
8768     return force_int_to_mode (x, int_mode, xmode,
8769                               as_a <scalar_int_mode> (op_mode),
8770                               mask, just_select);
8771
8772   return gen_lowpart_or_truncate (mode, x);
8773 }
8774
8775 /* Subroutine of force_to_mode that handles cases in which both X and
8776    the result are scalar integers.  MODE is the mode of the result,
8777    XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8778    is preferred for simplified versions of X.  The other arguments
8779    are as for force_to_mode.  */
8780
8781 static rtx
8782 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8783                    scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8784                    int just_select)
8785 {
8786   enum rtx_code code = GET_CODE (x);
8787   int next_select = just_select || code == XOR || code == NOT || code == NEG;
8788   unsigned HOST_WIDE_INT fuller_mask;
8789   rtx op0, op1, temp;
8790   poly_int64 const_op0;
8791
8792   /* When we have an arithmetic operation, or a shift whose count we
8793      do not know, we need to assume that all bits up to the highest-order
8794      bit in MASK will be needed.  This is how we form such a mask.  */
8795   if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8796     fuller_mask = HOST_WIDE_INT_M1U;
8797   else
8798     fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8799                    - 1);
8800
8801   switch (code)
8802     {
8803     case CLOBBER:
8804       /* If X is a (clobber (const_int)), return it since we know we are
8805          generating something that won't match.  */
8806       return x;
8807
8808     case SIGN_EXTEND:
8809     case ZERO_EXTEND:
8810     case ZERO_EXTRACT:
8811     case SIGN_EXTRACT:
8812       x = expand_compound_operation (x);
8813       if (GET_CODE (x) != code)
8814         return force_to_mode (x, mode, mask, next_select);
8815       break;
8816
8817     case TRUNCATE:
8818       /* Similarly for a truncate.  */
8819       return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8820
8821     case AND:
8822       /* If this is an AND with a constant, convert it into an AND
8823          whose constant is the AND of that constant with MASK.  If it
8824          remains an AND of MASK, delete it since it is redundant.  */
8825
8826       if (CONST_INT_P (XEXP (x, 1)))
8827         {
8828           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8829                                       mask & INTVAL (XEXP (x, 1)));
8830           xmode = op_mode;
8831
8832           /* If X is still an AND, see if it is an AND with a mask that
8833              is just some low-order bits.  If so, and it is MASK, we don't
8834              need it.  */
8835
8836           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8837               && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8838             x = XEXP (x, 0);
8839
8840           /* If it remains an AND, try making another AND with the bits
8841              in the mode mask that aren't in MASK turned on.  If the
8842              constant in the AND is wide enough, this might make a
8843              cheaper constant.  */
8844
8845           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8846               && GET_MODE_MASK (xmode) != mask
8847               && HWI_COMPUTABLE_MODE_P (xmode))
8848             {
8849               unsigned HOST_WIDE_INT cval
8850                 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8851               rtx y;
8852
8853               y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8854                                        gen_int_mode (cval, xmode));
8855               if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8856                   < set_src_cost (x, xmode, optimize_this_for_speed_p))
8857                 x = y;
8858             }
8859
8860           break;
8861         }
8862
8863       goto binop;
8864
8865     case PLUS:
8866       /* In (and (plus FOO C1) M), if M is a mask that just turns off
8867          low-order bits (as in an alignment operation) and FOO is already
8868          aligned to that boundary, mask C1 to that boundary as well.
8869          This may eliminate that PLUS and, later, the AND.  */
8870
8871       {
8872         unsigned int width = GET_MODE_PRECISION (mode);
8873         unsigned HOST_WIDE_INT smask = mask;
8874
8875         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8876            number, sign extend it.  */
8877
8878         if (width < HOST_BITS_PER_WIDE_INT
8879             && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8880           smask |= HOST_WIDE_INT_M1U << width;
8881
8882         if (CONST_INT_P (XEXP (x, 1))
8883             && pow2p_hwi (- smask)
8884             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8885             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8886           return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8887                                                (INTVAL (XEXP (x, 1)) & smask)),
8888                                 mode, smask, next_select);
8889       }
8890
8891       /* fall through */
8892
8893     case MULT:
8894       /* Substituting into the operands of a widening MULT is not likely to
8895          create RTL matching a machine insn.  */
8896       if (code == MULT
8897           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8898               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8899           && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8900               || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8901           && REG_P (XEXP (XEXP (x, 0), 0))
8902           && REG_P (XEXP (XEXP (x, 1), 0)))
8903         return gen_lowpart_or_truncate (mode, x);
8904
8905       /* For PLUS, MINUS and MULT, we need any bits less significant than the
8906          most significant bit in MASK since carries from those bits will
8907          affect the bits we are interested in.  */
8908       mask = fuller_mask;
8909       goto binop;
8910
8911     case MINUS:
8912       /* If X is (minus C Y) where C's least set bit is larger than any bit
8913          in the mask, then we may replace with (neg Y).  */
8914       if (poly_int_rtx_p (XEXP (x, 0), &const_op0)
8915           && (unsigned HOST_WIDE_INT) known_alignment (const_op0) > mask)
8916         {
8917           x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8918           return force_to_mode (x, mode, mask, next_select);
8919         }
8920
8921       /* Similarly, if C contains every bit in the fuller_mask, then we may
8922          replace with (not Y).  */
8923       if (CONST_INT_P (XEXP (x, 0))
8924           && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8925         {
8926           x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8927           return force_to_mode (x, mode, mask, next_select);
8928         }
8929
8930       mask = fuller_mask;
8931       goto binop;
8932
8933     case IOR:
8934     case XOR:
8935       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8936          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8937          operation which may be a bitfield extraction.  Ensure that the
8938          constant we form is not wider than the mode of X.  */
8939
8940       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8941           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8942           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8943           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8944           && CONST_INT_P (XEXP (x, 1))
8945           && ((INTVAL (XEXP (XEXP (x, 0), 1))
8946                + floor_log2 (INTVAL (XEXP (x, 1))))
8947               < GET_MODE_PRECISION (xmode))
8948           && (UINTVAL (XEXP (x, 1))
8949               & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8950         {
8951           temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8952                                << INTVAL (XEXP (XEXP (x, 0), 1)),
8953                                xmode);
8954           temp = simplify_gen_binary (GET_CODE (x), xmode,
8955                                       XEXP (XEXP (x, 0), 0), temp);
8956           x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8957                                    XEXP (XEXP (x, 0), 1));
8958           return force_to_mode (x, mode, mask, next_select);
8959         }
8960
8961     binop:
8962       /* For most binary operations, just propagate into the operation and
8963          change the mode if we have an operation of that mode.  */
8964
8965       op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8966       op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8967
8968       /* If we ended up truncating both operands, truncate the result of the
8969          operation instead.  */
8970       if (GET_CODE (op0) == TRUNCATE
8971           && GET_CODE (op1) == TRUNCATE)
8972         {
8973           op0 = XEXP (op0, 0);
8974           op1 = XEXP (op1, 0);
8975         }
8976
8977       op0 = gen_lowpart_or_truncate (op_mode, op0);
8978       op1 = gen_lowpart_or_truncate (op_mode, op1);
8979
8980       if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8981         {
8982           x = simplify_gen_binary (code, op_mode, op0, op1);
8983           xmode = op_mode;
8984         }
8985       break;
8986
8987     case ASHIFT:
8988       /* For left shifts, do the same, but just for the first operand.
8989          However, we cannot do anything with shifts where we cannot
8990          guarantee that the counts are smaller than the size of the mode
8991          because such a count will have a different meaning in a
8992          wider mode.  */
8993
8994       if (! (CONST_INT_P (XEXP (x, 1))
8995              && INTVAL (XEXP (x, 1)) >= 0
8996              && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8997           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8998                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8999                     < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
9000         break;
9001
9002       /* If the shift count is a constant and we can do arithmetic in
9003          the mode of the shift, refine which bits we need.  Otherwise, use the
9004          conservative form of the mask.  */
9005       if (CONST_INT_P (XEXP (x, 1))
9006           && INTVAL (XEXP (x, 1)) >= 0
9007           && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
9008           && HWI_COMPUTABLE_MODE_P (op_mode))
9009         mask >>= INTVAL (XEXP (x, 1));
9010       else
9011         mask = fuller_mask;
9012
9013       op0 = gen_lowpart_or_truncate (op_mode,
9014                                      force_to_mode (XEXP (x, 0), mode,
9015                                                     mask, next_select));
9016
9017       if (op_mode != xmode || op0 != XEXP (x, 0))
9018         {
9019           x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
9020           xmode = op_mode;
9021         }
9022       break;
9023
9024     case LSHIFTRT:
9025       /* Here we can only do something if the shift count is a constant,
9026          this shift constant is valid for the host, and we can do arithmetic
9027          in OP_MODE.  */
9028
9029       if (CONST_INT_P (XEXP (x, 1))
9030           && INTVAL (XEXP (x, 1)) >= 0
9031           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
9032           && HWI_COMPUTABLE_MODE_P (op_mode))
9033         {
9034           rtx inner = XEXP (x, 0);
9035           unsigned HOST_WIDE_INT inner_mask;
9036
9037           /* Select the mask of the bits we need for the shift operand.  */
9038           inner_mask = mask << INTVAL (XEXP (x, 1));
9039
9040           /* We can only change the mode of the shift if we can do arithmetic
9041              in the mode of the shift and INNER_MASK is no wider than the
9042              width of X's mode.  */
9043           if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
9044             op_mode = xmode;
9045
9046           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
9047
9048           if (xmode != op_mode || inner != XEXP (x, 0))
9049             {
9050               x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
9051               xmode = op_mode;
9052             }
9053         }
9054
9055       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
9056          shift and AND produces only copies of the sign bit (C2 is one less
9057          than a power of two), we can do this with just a shift.  */
9058
9059       if (GET_CODE (x) == LSHIFTRT
9060           && CONST_INT_P (XEXP (x, 1))
9061           /* The shift puts one of the sign bit copies in the least significant
9062              bit.  */
9063           && ((INTVAL (XEXP (x, 1))
9064                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
9065               >= GET_MODE_PRECISION (xmode))
9066           && pow2p_hwi (mask + 1)
9067           /* Number of bits left after the shift must be more than the mask
9068              needs.  */
9069           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
9070               <= GET_MODE_PRECISION (xmode))
9071           /* Must be more sign bit copies than the mask needs.  */
9072           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
9073               >= exact_log2 (mask + 1)))
9074         {
9075           int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
9076           x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
9077                                    gen_int_shift_amount (xmode, nbits));
9078         }
9079       goto shiftrt;
9080
9081     case ASHIFTRT:
9082       /* If we are just looking for the sign bit, we don't need this shift at
9083          all, even if it has a variable count.  */
9084       if (val_signbit_p (xmode, mask))
9085         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9086
9087       /* If this is a shift by a constant, get a mask that contains those bits
9088          that are not copies of the sign bit.  We then have two cases:  If
9089          MASK only includes those bits, this can be a logical shift, which may
9090          allow simplifications.  If MASK is a single-bit field not within
9091          those bits, we are requesting a copy of the sign bit and hence can
9092          shift the sign bit to the appropriate location.  */
9093
9094       if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9095           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9096         {
9097           unsigned HOST_WIDE_INT nonzero;
9098           int i;
9099
9100           /* If the considered data is wider than HOST_WIDE_INT, we can't
9101              represent a mask for all its bits in a single scalar.
9102              But we only care about the lower bits, so calculate these.  */
9103
9104           if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9105             {
9106               nonzero = HOST_WIDE_INT_M1U;
9107
9108               /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9109                  is the number of bits a full-width mask would have set.
9110                  We need only shift if these are fewer than nonzero can
9111                  hold.  If not, we must keep all bits set in nonzero.  */
9112
9113               if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9114                   < HOST_BITS_PER_WIDE_INT)
9115                 nonzero >>= INTVAL (XEXP (x, 1))
9116                             + HOST_BITS_PER_WIDE_INT
9117                             - GET_MODE_PRECISION (xmode);
9118             }
9119           else
9120             {
9121               nonzero = GET_MODE_MASK (xmode);
9122               nonzero >>= INTVAL (XEXP (x, 1));
9123             }
9124
9125           if ((mask & ~nonzero) == 0)
9126             {
9127               x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9128                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
9129               if (GET_CODE (x) != ASHIFTRT)
9130                 return force_to_mode (x, mode, mask, next_select);
9131             }
9132
9133           else if ((i = exact_log2 (mask)) >= 0)
9134             {
9135               x = simplify_shift_const
9136                   (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9137                    GET_MODE_PRECISION (xmode) - 1 - i);
9138
9139               if (GET_CODE (x) != ASHIFTRT)
9140                 return force_to_mode (x, mode, mask, next_select);
9141             }
9142         }
9143
9144       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
9145          even if the shift count isn't a constant.  */
9146       if (mask == 1)
9147         x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9148
9149     shiftrt:
9150
9151       /* If this is a zero- or sign-extension operation that just affects bits
9152          we don't care about, remove it.  Be sure the call above returned
9153          something that is still a shift.  */
9154
9155       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9156           && CONST_INT_P (XEXP (x, 1))
9157           && INTVAL (XEXP (x, 1)) >= 0
9158           && (INTVAL (XEXP (x, 1))
9159               <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9160           && GET_CODE (XEXP (x, 0)) == ASHIFT
9161           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9162         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
9163                               next_select);
9164
9165       break;
9166
9167     case ROTATE:
9168     case ROTATERT:
9169       /* If the shift count is constant and we can do computations
9170          in the mode of X, compute where the bits we care about are.
9171          Otherwise, we can't do anything.  Don't change the mode of
9172          the shift or propagate MODE into the shift, though.  */
9173       if (CONST_INT_P (XEXP (x, 1))
9174           && INTVAL (XEXP (x, 1)) >= 0)
9175         {
9176           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9177                                             xmode, gen_int_mode (mask, xmode),
9178                                             XEXP (x, 1));
9179           if (temp && CONST_INT_P (temp))
9180             x = simplify_gen_binary (code, xmode,
9181                                      force_to_mode (XEXP (x, 0), xmode,
9182                                                     INTVAL (temp), next_select),
9183                                      XEXP (x, 1));
9184         }
9185       break;
9186
9187     case NEG:
9188       /* If we just want the low-order bit, the NEG isn't needed since it
9189          won't change the low-order bit.  */
9190       if (mask == 1)
9191         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9192
9193       /* We need any bits less significant than the most significant bit in
9194          MASK since carries from those bits will affect the bits we are
9195          interested in.  */
9196       mask = fuller_mask;
9197       goto unop;
9198
9199     case NOT:
9200       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9201          same as the XOR case above.  Ensure that the constant we form is not
9202          wider than the mode of X.  */
9203
9204       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9205           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9206           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9207           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9208               < GET_MODE_PRECISION (xmode))
9209           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9210         {
9211           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9212           temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9213           x = simplify_gen_binary (LSHIFTRT, xmode,
9214                                    temp, XEXP (XEXP (x, 0), 1));
9215
9216           return force_to_mode (x, mode, mask, next_select);
9217         }
9218
9219       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9220          use the full mask inside the NOT.  */
9221       mask = fuller_mask;
9222
9223     unop:
9224       op0 = gen_lowpart_or_truncate (op_mode,
9225                                      force_to_mode (XEXP (x, 0), mode, mask,
9226                                                     next_select));
9227       if (op_mode != xmode || op0 != XEXP (x, 0))
9228         {
9229           x = simplify_gen_unary (code, op_mode, op0, op_mode);
9230           xmode = op_mode;
9231         }
9232       break;
9233
9234     case NE:
9235       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9236          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9237          which is equal to STORE_FLAG_VALUE.  */
9238       if ((mask & ~STORE_FLAG_VALUE) == 0
9239           && XEXP (x, 1) == const0_rtx
9240           && GET_MODE (XEXP (x, 0)) == mode
9241           && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9242           && (nonzero_bits (XEXP (x, 0), mode)
9243               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9244         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9245
9246       break;
9247
9248     case IF_THEN_ELSE:
9249       /* We have no way of knowing if the IF_THEN_ELSE can itself be
9250          written in a narrower mode.  We play it safe and do not do so.  */
9251
9252       op0 = gen_lowpart_or_truncate (xmode,
9253                                      force_to_mode (XEXP (x, 1), mode,
9254                                                     mask, next_select));
9255       op1 = gen_lowpart_or_truncate (xmode,
9256                                      force_to_mode (XEXP (x, 2), mode,
9257                                                     mask, next_select));
9258       if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9259         x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9260                                   GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9261                                   op0, op1);
9262       break;
9263
9264     default:
9265       break;
9266     }
9267
9268   /* Ensure we return a value of the proper mode.  */
9269   return gen_lowpart_or_truncate (mode, x);
9270 }
9271 \f
9272 /* Return nonzero if X is an expression that has one of two values depending on
9273    whether some other value is zero or nonzero.  In that case, we return the
9274    value that is being tested, *PTRUE is set to the value if the rtx being
9275    returned has a nonzero value, and *PFALSE is set to the other alternative.
9276
9277    If we return zero, we set *PTRUE and *PFALSE to X.  */
9278
9279 static rtx
9280 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9281 {
9282   machine_mode mode = GET_MODE (x);
9283   enum rtx_code code = GET_CODE (x);
9284   rtx cond0, cond1, true0, true1, false0, false1;
9285   unsigned HOST_WIDE_INT nz;
9286   scalar_int_mode int_mode;
9287
9288   /* If we are comparing a value against zero, we are done.  */
9289   if ((code == NE || code == EQ)
9290       && XEXP (x, 1) == const0_rtx)
9291     {
9292       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9293       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9294       return XEXP (x, 0);
9295     }
9296
9297   /* If this is a unary operation whose operand has one of two values, apply
9298      our opcode to compute those values.  */
9299   else if (UNARY_P (x)
9300            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9301     {
9302       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9303       *pfalse = simplify_gen_unary (code, mode, false0,
9304                                     GET_MODE (XEXP (x, 0)));
9305       return cond0;
9306     }
9307
9308   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9309      make can't possibly match and would suppress other optimizations.  */
9310   else if (code == COMPARE)
9311     ;
9312
9313   /* If this is a binary operation, see if either side has only one of two
9314      values.  If either one does or if both do and they are conditional on
9315      the same value, compute the new true and false values.  */
9316   else if (BINARY_P (x))
9317     {
9318       rtx op0 = XEXP (x, 0);
9319       rtx op1 = XEXP (x, 1);
9320       cond0 = if_then_else_cond (op0, &true0, &false0);
9321       cond1 = if_then_else_cond (op1, &true1, &false1);
9322
9323       if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9324           && (REG_P (op0) || REG_P (op1)))
9325         {
9326           /* Try to enable a simplification by undoing work done by
9327              if_then_else_cond if it converted a REG into something more
9328              complex.  */
9329           if (REG_P (op0))
9330             {
9331               cond0 = 0;
9332               true0 = false0 = op0;
9333             }
9334           else
9335             {
9336               cond1 = 0;
9337               true1 = false1 = op1;
9338             }
9339         }
9340
9341       if ((cond0 != 0 || cond1 != 0)
9342           && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9343         {
9344           /* If if_then_else_cond returned zero, then true/false are the
9345              same rtl.  We must copy one of them to prevent invalid rtl
9346              sharing.  */
9347           if (cond0 == 0)
9348             true0 = copy_rtx (true0);
9349           else if (cond1 == 0)
9350             true1 = copy_rtx (true1);
9351
9352           if (COMPARISON_P (x))
9353             {
9354               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9355                                                 true0, true1);
9356               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9357                                                  false0, false1);
9358              }
9359           else
9360             {
9361               *ptrue = simplify_gen_binary (code, mode, true0, true1);
9362               *pfalse = simplify_gen_binary (code, mode, false0, false1);
9363             }
9364
9365           return cond0 ? cond0 : cond1;
9366         }
9367
9368       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9369          operands is zero when the other is nonzero, and vice-versa,
9370          and STORE_FLAG_VALUE is 1 or -1.  */
9371
9372       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9373           && (code == PLUS || code == IOR || code == XOR || code == MINUS
9374               || code == UMAX)
9375           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9376         {
9377           rtx op0 = XEXP (XEXP (x, 0), 1);
9378           rtx op1 = XEXP (XEXP (x, 1), 1);
9379
9380           cond0 = XEXP (XEXP (x, 0), 0);
9381           cond1 = XEXP (XEXP (x, 1), 0);
9382
9383           if (COMPARISON_P (cond0)
9384               && COMPARISON_P (cond1)
9385               && SCALAR_INT_MODE_P (mode)
9386               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9387                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9388                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9389                   || ((swap_condition (GET_CODE (cond0))
9390                        == reversed_comparison_code (cond1, NULL))
9391                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9392                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9393               && ! side_effects_p (x))
9394             {
9395               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9396               *pfalse = simplify_gen_binary (MULT, mode,
9397                                              (code == MINUS
9398                                               ? simplify_gen_unary (NEG, mode,
9399                                                                     op1, mode)
9400                                               : op1),
9401                                               const_true_rtx);
9402               return cond0;
9403             }
9404         }
9405
9406       /* Similarly for MULT, AND and UMIN, except that for these the result
9407          is always zero.  */
9408       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9409           && (code == MULT || code == AND || code == UMIN)
9410           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9411         {
9412           cond0 = XEXP (XEXP (x, 0), 0);
9413           cond1 = XEXP (XEXP (x, 1), 0);
9414
9415           if (COMPARISON_P (cond0)
9416               && COMPARISON_P (cond1)
9417               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9418                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9419                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9420                   || ((swap_condition (GET_CODE (cond0))
9421                        == reversed_comparison_code (cond1, NULL))
9422                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9423                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9424               && ! side_effects_p (x))
9425             {
9426               *ptrue = *pfalse = const0_rtx;
9427               return cond0;
9428             }
9429         }
9430     }
9431
9432   else if (code == IF_THEN_ELSE)
9433     {
9434       /* If we have IF_THEN_ELSE already, extract the condition and
9435          canonicalize it if it is NE or EQ.  */
9436       cond0 = XEXP (x, 0);
9437       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9438       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9439         return XEXP (cond0, 0);
9440       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9441         {
9442           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9443           return XEXP (cond0, 0);
9444         }
9445       else
9446         return cond0;
9447     }
9448
9449   /* If X is a SUBREG, we can narrow both the true and false values
9450      if the inner expression, if there is a condition.  */
9451   else if (code == SUBREG
9452            && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9453                                           &false0)) != 0)
9454     {
9455       true0 = simplify_gen_subreg (mode, true0,
9456                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9457       false0 = simplify_gen_subreg (mode, false0,
9458                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9459       if (true0 && false0)
9460         {
9461           *ptrue = true0;
9462           *pfalse = false0;
9463           return cond0;
9464         }
9465     }
9466
9467   /* If X is a constant, this isn't special and will cause confusions
9468      if we treat it as such.  Likewise if it is equivalent to a constant.  */
9469   else if (CONSTANT_P (x)
9470            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9471     ;
9472
9473   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9474      will be least confusing to the rest of the compiler.  */
9475   else if (mode == BImode)
9476     {
9477       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9478       return x;
9479     }
9480
9481   /* If X is known to be either 0 or -1, those are the true and
9482      false values when testing X.  */
9483   else if (x == constm1_rtx || x == const0_rtx
9484            || (is_a <scalar_int_mode> (mode, &int_mode)
9485                && (num_sign_bit_copies (x, int_mode)
9486                    == GET_MODE_PRECISION (int_mode))))
9487     {
9488       *ptrue = constm1_rtx, *pfalse = const0_rtx;
9489       return x;
9490     }
9491
9492   /* Likewise for 0 or a single bit.  */
9493   else if (HWI_COMPUTABLE_MODE_P (mode)
9494            && pow2p_hwi (nz = nonzero_bits (x, mode)))
9495     {
9496       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9497       return x;
9498     }
9499
9500   /* Otherwise fail; show no condition with true and false values the same.  */
9501   *ptrue = *pfalse = x;
9502   return 0;
9503 }
9504 \f
9505 /* Return the value of expression X given the fact that condition COND
9506    is known to be true when applied to REG as its first operand and VAL
9507    as its second.  X is known to not be shared and so can be modified in
9508    place.
9509
9510    We only handle the simplest cases, and specifically those cases that
9511    arise with IF_THEN_ELSE expressions.  */
9512
9513 static rtx
9514 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9515 {
9516   enum rtx_code code = GET_CODE (x);
9517   const char *fmt;
9518   int i, j;
9519
9520   if (side_effects_p (x))
9521     return x;
9522
9523   /* If either operand of the condition is a floating point value,
9524      then we have to avoid collapsing an EQ comparison.  */
9525   if (cond == EQ
9526       && rtx_equal_p (x, reg)
9527       && ! FLOAT_MODE_P (GET_MODE (x))
9528       && ! FLOAT_MODE_P (GET_MODE (val)))
9529     return val;
9530
9531   if (cond == UNEQ && rtx_equal_p (x, reg))
9532     return val;
9533
9534   /* If X is (abs REG) and we know something about REG's relationship
9535      with zero, we may be able to simplify this.  */
9536
9537   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9538     switch (cond)
9539       {
9540       case GE:  case GT:  case EQ:
9541         return XEXP (x, 0);
9542       case LT:  case LE:
9543         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9544                                    XEXP (x, 0),
9545                                    GET_MODE (XEXP (x, 0)));
9546       default:
9547         break;
9548       }
9549
9550   /* The only other cases we handle are MIN, MAX, and comparisons if the
9551      operands are the same as REG and VAL.  */
9552
9553   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9554     {
9555       if (rtx_equal_p (XEXP (x, 0), val))
9556         {
9557           std::swap (val, reg);
9558           cond = swap_condition (cond);
9559         }
9560
9561       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9562         {
9563           if (COMPARISON_P (x))
9564             {
9565               if (comparison_dominates_p (cond, code))
9566                 return VECTOR_MODE_P (GET_MODE (x)) ? x : const_true_rtx;
9567
9568               code = reversed_comparison_code (x, NULL);
9569               if (code != UNKNOWN
9570                   && comparison_dominates_p (cond, code))
9571                 return CONST0_RTX (GET_MODE (x));
9572               else
9573                 return x;
9574             }
9575           else if (code == SMAX || code == SMIN
9576                    || code == UMIN || code == UMAX)
9577             {
9578               int unsignedp = (code == UMIN || code == UMAX);
9579
9580               /* Do not reverse the condition when it is NE or EQ.
9581                  This is because we cannot conclude anything about
9582                  the value of 'SMAX (x, y)' when x is not equal to y,
9583                  but we can when x equals y.  */
9584               if ((code == SMAX || code == UMAX)
9585                   && ! (cond == EQ || cond == NE))
9586                 cond = reverse_condition (cond);
9587
9588               switch (cond)
9589                 {
9590                 case GE:   case GT:
9591                   return unsignedp ? x : XEXP (x, 1);
9592                 case LE:   case LT:
9593                   return unsignedp ? x : XEXP (x, 0);
9594                 case GEU:  case GTU:
9595                   return unsignedp ? XEXP (x, 1) : x;
9596                 case LEU:  case LTU:
9597                   return unsignedp ? XEXP (x, 0) : x;
9598                 default:
9599                   break;
9600                 }
9601             }
9602         }
9603     }
9604   else if (code == SUBREG)
9605     {
9606       machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9607       rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9608
9609       if (SUBREG_REG (x) != r)
9610         {
9611           /* We must simplify subreg here, before we lose track of the
9612              original inner_mode.  */
9613           new_rtx = simplify_subreg (GET_MODE (x), r,
9614                                      inner_mode, SUBREG_BYTE (x));
9615           if (new_rtx)
9616             return new_rtx;
9617           else
9618             SUBST (SUBREG_REG (x), r);
9619         }
9620
9621       return x;
9622     }
9623   /* We don't have to handle SIGN_EXTEND here, because even in the
9624      case of replacing something with a modeless CONST_INT, a
9625      CONST_INT is already (supposed to be) a valid sign extension for
9626      its narrower mode, which implies it's already properly
9627      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
9628      story is different.  */
9629   else if (code == ZERO_EXTEND)
9630     {
9631       machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9632       rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9633
9634       if (XEXP (x, 0) != r)
9635         {
9636           /* We must simplify the zero_extend here, before we lose
9637              track of the original inner_mode.  */
9638           new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9639                                               r, inner_mode);
9640           if (new_rtx)
9641             return new_rtx;
9642           else
9643             SUBST (XEXP (x, 0), r);
9644         }
9645
9646       return x;
9647     }
9648
9649   fmt = GET_RTX_FORMAT (code);
9650   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9651     {
9652       if (fmt[i] == 'e')
9653         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9654       else if (fmt[i] == 'E')
9655         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9656           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9657                                                 cond, reg, val));
9658     }
9659
9660   return x;
9661 }
9662 \f
9663 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9664    assignment as a field assignment.  */
9665
9666 static int
9667 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9668 {
9669   if (widen_x && GET_MODE (x) != GET_MODE (y))
9670     {
9671       if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9672         return 0;
9673       if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9674         return 0;
9675       x = adjust_address_nv (x, GET_MODE (y),
9676                              byte_lowpart_offset (GET_MODE (y),
9677                                                   GET_MODE (x)));
9678     }
9679
9680   if (x == y || rtx_equal_p (x, y))
9681     return 1;
9682
9683   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9684     return 0;
9685
9686   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9687      Note that all SUBREGs of MEM are paradoxical; otherwise they
9688      would have been rewritten.  */
9689   if (MEM_P (x) && GET_CODE (y) == SUBREG
9690       && MEM_P (SUBREG_REG (y))
9691       && rtx_equal_p (SUBREG_REG (y),
9692                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9693     return 1;
9694
9695   if (MEM_P (y) && GET_CODE (x) == SUBREG
9696       && MEM_P (SUBREG_REG (x))
9697       && rtx_equal_p (SUBREG_REG (x),
9698                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9699     return 1;
9700
9701   /* We used to see if get_last_value of X and Y were the same but that's
9702      not correct.  In one direction, we'll cause the assignment to have
9703      the wrong destination and in the case, we'll import a register into this
9704      insn that might have already have been dead.   So fail if none of the
9705      above cases are true.  */
9706   return 0;
9707 }
9708 \f
9709 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9710    Return that assignment if so.
9711
9712    We only handle the most common cases.  */
9713
9714 static rtx
9715 make_field_assignment (rtx x)
9716 {
9717   rtx dest = SET_DEST (x);
9718   rtx src = SET_SRC (x);
9719   rtx assign;
9720   rtx rhs, lhs;
9721   HOST_WIDE_INT c1;
9722   HOST_WIDE_INT pos;
9723   unsigned HOST_WIDE_INT len;
9724   rtx other;
9725
9726   /* All the rules in this function are specific to scalar integers.  */
9727   scalar_int_mode mode;
9728   if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9729     return x;
9730
9731   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9732      a clear of a one-bit field.  We will have changed it to
9733      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
9734      for a SUBREG.  */
9735
9736   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9737       && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9738       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9739       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9740     {
9741       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9742                                 1, 1, 1, 0);
9743       if (assign != 0)
9744         return gen_rtx_SET (assign, const0_rtx);
9745       return x;
9746     }
9747
9748   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9749       && subreg_lowpart_p (XEXP (src, 0))
9750       && partial_subreg_p (XEXP (src, 0))
9751       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9752       && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9753       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9754       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9755     {
9756       assign = make_extraction (VOIDmode, dest, 0,
9757                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9758                                 1, 1, 1, 0);
9759       if (assign != 0)
9760         return gen_rtx_SET (assign, const0_rtx);
9761       return x;
9762     }
9763
9764   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9765      one-bit field.  */
9766   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9767       && XEXP (XEXP (src, 0), 0) == const1_rtx
9768       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9769     {
9770       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9771                                 1, 1, 1, 0);
9772       if (assign != 0)
9773         return gen_rtx_SET (assign, const1_rtx);
9774       return x;
9775     }
9776
9777   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9778      SRC is an AND with all bits of that field set, then we can discard
9779      the AND.  */
9780   if (GET_CODE (dest) == ZERO_EXTRACT
9781       && CONST_INT_P (XEXP (dest, 1))
9782       && GET_CODE (src) == AND
9783       && CONST_INT_P (XEXP (src, 1)))
9784     {
9785       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9786       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9787       unsigned HOST_WIDE_INT ze_mask;
9788
9789       if (width >= HOST_BITS_PER_WIDE_INT)
9790         ze_mask = -1;
9791       else
9792         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9793
9794       /* Complete overlap.  We can remove the source AND.  */
9795       if ((and_mask & ze_mask) == ze_mask)
9796         return gen_rtx_SET (dest, XEXP (src, 0));
9797
9798       /* Partial overlap.  We can reduce the source AND.  */
9799       if ((and_mask & ze_mask) != and_mask)
9800         {
9801           src = gen_rtx_AND (mode, XEXP (src, 0),
9802                              gen_int_mode (and_mask & ze_mask, mode));
9803           return gen_rtx_SET (dest, src);
9804         }
9805     }
9806
9807   /* The other case we handle is assignments into a constant-position
9808      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
9809      a mask that has all one bits except for a group of zero bits and
9810      OTHER is known to have zeros where C1 has ones, this is such an
9811      assignment.  Compute the position and length from C1.  Shift OTHER
9812      to the appropriate position, force it to the required mode, and
9813      make the extraction.  Check for the AND in both operands.  */
9814
9815   /* One or more SUBREGs might obscure the constant-position field
9816      assignment.  The first one we are likely to encounter is an outer
9817      narrowing SUBREG, which we can just strip for the purposes of
9818      identifying the constant-field assignment.  */
9819   scalar_int_mode src_mode = mode;
9820   if (GET_CODE (src) == SUBREG
9821       && subreg_lowpart_p (src)
9822       && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9823     src = SUBREG_REG (src);
9824
9825   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9826     return x;
9827
9828   rhs = expand_compound_operation (XEXP (src, 0));
9829   lhs = expand_compound_operation (XEXP (src, 1));
9830
9831   if (GET_CODE (rhs) == AND
9832       && CONST_INT_P (XEXP (rhs, 1))
9833       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9834     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9835   /* The second SUBREG that might get in the way is a paradoxical
9836      SUBREG around the first operand of the AND.  We want to 
9837      pretend the operand is as wide as the destination here.   We
9838      do this by adjusting the MEM to wider mode for the sole
9839      purpose of the call to rtx_equal_for_field_assignment_p.   Also
9840      note this trick only works for MEMs.  */
9841   else if (GET_CODE (rhs) == AND
9842            && paradoxical_subreg_p (XEXP (rhs, 0))
9843            && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9844            && CONST_INT_P (XEXP (rhs, 1))
9845            && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9846                                                 dest, true))
9847     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9848   else if (GET_CODE (lhs) == AND
9849            && CONST_INT_P (XEXP (lhs, 1))
9850            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9851     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9852   /* The second SUBREG that might get in the way is a paradoxical
9853      SUBREG around the first operand of the AND.  We want to 
9854      pretend the operand is as wide as the destination here.   We
9855      do this by adjusting the MEM to wider mode for the sole
9856      purpose of the call to rtx_equal_for_field_assignment_p.   Also
9857      note this trick only works for MEMs.  */
9858   else if (GET_CODE (lhs) == AND
9859            && paradoxical_subreg_p (XEXP (lhs, 0))
9860            && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9861            && CONST_INT_P (XEXP (lhs, 1))
9862            && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9863                                                 dest, true))
9864     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9865   else
9866     return x;
9867
9868   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9869   if (pos < 0
9870       || pos + len > GET_MODE_PRECISION (mode)
9871       || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9872       || (c1 & nonzero_bits (other, mode)) != 0)
9873     return x;
9874
9875   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9876   if (assign == 0)
9877     return x;
9878
9879   /* The mode to use for the source is the mode of the assignment, or of
9880      what is inside a possible STRICT_LOW_PART.  */
9881   machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9882                            ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9883
9884   /* Shift OTHER right POS places and make it the source, restricting it
9885      to the proper length and mode.  */
9886
9887   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9888                                                      src_mode, other, pos),
9889                                dest);
9890   src = force_to_mode (src, new_mode,
9891                        len >= HOST_BITS_PER_WIDE_INT
9892                        ? HOST_WIDE_INT_M1U
9893                        : (HOST_WIDE_INT_1U << len) - 1,
9894                        0);
9895
9896   /* If SRC is masked by an AND that does not make a difference in
9897      the value being stored, strip it.  */
9898   if (GET_CODE (assign) == ZERO_EXTRACT
9899       && CONST_INT_P (XEXP (assign, 1))
9900       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9901       && GET_CODE (src) == AND
9902       && CONST_INT_P (XEXP (src, 1))
9903       && UINTVAL (XEXP (src, 1))
9904          == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9905     src = XEXP (src, 0);
9906
9907   return gen_rtx_SET (assign, src);
9908 }
9909 \f
9910 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9911    if so.  */
9912
9913 static rtx
9914 apply_distributive_law (rtx x)
9915 {
9916   enum rtx_code code = GET_CODE (x);
9917   enum rtx_code inner_code;
9918   rtx lhs, rhs, other;
9919   rtx tem;
9920
9921   /* Distributivity is not true for floating point as it can change the
9922      value.  So we don't do it unless -funsafe-math-optimizations.  */
9923   if (FLOAT_MODE_P (GET_MODE (x))
9924       && ! flag_unsafe_math_optimizations)
9925     return x;
9926
9927   /* The outer operation can only be one of the following:  */
9928   if (code != IOR && code != AND && code != XOR
9929       && code != PLUS && code != MINUS)
9930     return x;
9931
9932   lhs = XEXP (x, 0);
9933   rhs = XEXP (x, 1);
9934
9935   /* If either operand is a primitive we can't do anything, so get out
9936      fast.  */
9937   if (OBJECT_P (lhs) || OBJECT_P (rhs))
9938     return x;
9939
9940   lhs = expand_compound_operation (lhs);
9941   rhs = expand_compound_operation (rhs);
9942   inner_code = GET_CODE (lhs);
9943   if (inner_code != GET_CODE (rhs))
9944     return x;
9945
9946   /* See if the inner and outer operations distribute.  */
9947   switch (inner_code)
9948     {
9949     case LSHIFTRT:
9950     case ASHIFTRT:
9951     case AND:
9952     case IOR:
9953       /* These all distribute except over PLUS.  */
9954       if (code == PLUS || code == MINUS)
9955         return x;
9956       break;
9957
9958     case MULT:
9959       if (code != PLUS && code != MINUS)
9960         return x;
9961       break;
9962
9963     case ASHIFT:
9964       /* This is also a multiply, so it distributes over everything.  */
9965       break;
9966
9967     /* This used to handle SUBREG, but this turned out to be counter-
9968        productive, since (subreg (op ...)) usually is not handled by
9969        insn patterns, and this "optimization" therefore transformed
9970        recognizable patterns into unrecognizable ones.  Therefore the
9971        SUBREG case was removed from here.
9972
9973        It is possible that distributing SUBREG over arithmetic operations
9974        leads to an intermediate result than can then be optimized further,
9975        e.g. by moving the outer SUBREG to the other side of a SET as done
9976        in simplify_set.  This seems to have been the original intent of
9977        handling SUBREGs here.
9978
9979        However, with current GCC this does not appear to actually happen,
9980        at least on major platforms.  If some case is found where removing
9981        the SUBREG case here prevents follow-on optimizations, distributing
9982        SUBREGs ought to be re-added at that place, e.g. in simplify_set.  */
9983
9984     default:
9985       return x;
9986     }
9987
9988   /* Set LHS and RHS to the inner operands (A and B in the example
9989      above) and set OTHER to the common operand (C in the example).
9990      There is only one way to do this unless the inner operation is
9991      commutative.  */
9992   if (COMMUTATIVE_ARITH_P (lhs)
9993       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9994     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9995   else if (COMMUTATIVE_ARITH_P (lhs)
9996            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9997     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9998   else if (COMMUTATIVE_ARITH_P (lhs)
9999            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
10000     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
10001   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
10002     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
10003   else
10004     return x;
10005
10006   /* Form the new inner operation, seeing if it simplifies first.  */
10007   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
10008
10009   /* There is one exception to the general way of distributing:
10010      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
10011   if (code == XOR && inner_code == IOR)
10012     {
10013       inner_code = AND;
10014       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
10015     }
10016
10017   /* We may be able to continuing distributing the result, so call
10018      ourselves recursively on the inner operation before forming the
10019      outer operation, which we return.  */
10020   return simplify_gen_binary (inner_code, GET_MODE (x),
10021                               apply_distributive_law (tem), other);
10022 }
10023
10024 /* See if X is of the form (* (+ A B) C), and if so convert to
10025    (+ (* A C) (* B C)) and try to simplify.
10026
10027    Most of the time, this results in no change.  However, if some of
10028    the operands are the same or inverses of each other, simplifications
10029    will result.
10030
10031    For example, (and (ior A B) (not B)) can occur as the result of
10032    expanding a bit field assignment.  When we apply the distributive
10033    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
10034    which then simplifies to (and (A (not B))).
10035
10036    Note that no checks happen on the validity of applying the inverse
10037    distributive law.  This is pointless since we can do it in the
10038    few places where this routine is called.
10039
10040    N is the index of the term that is decomposed (the arithmetic operation,
10041    i.e. (+ A B) in the first example above).  !N is the index of the term that
10042    is distributed, i.e. of C in the first example above.  */
10043 static rtx
10044 distribute_and_simplify_rtx (rtx x, int n)
10045 {
10046   machine_mode mode;
10047   enum rtx_code outer_code, inner_code;
10048   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
10049
10050   /* Distributivity is not true for floating point as it can change the
10051      value.  So we don't do it unless -funsafe-math-optimizations.  */
10052   if (FLOAT_MODE_P (GET_MODE (x))
10053       && ! flag_unsafe_math_optimizations)
10054     return NULL_RTX;
10055
10056   decomposed = XEXP (x, n);
10057   if (!ARITHMETIC_P (decomposed))
10058     return NULL_RTX;
10059
10060   mode = GET_MODE (x);
10061   outer_code = GET_CODE (x);
10062   distributed = XEXP (x, !n);
10063
10064   inner_code = GET_CODE (decomposed);
10065   inner_op0 = XEXP (decomposed, 0);
10066   inner_op1 = XEXP (decomposed, 1);
10067
10068   /* Special case (and (xor B C) (not A)), which is equivalent to
10069      (xor (ior A B) (ior A C))  */
10070   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
10071     {
10072       distributed = XEXP (distributed, 0);
10073       outer_code = IOR;
10074     }
10075
10076   if (n == 0)
10077     {
10078       /* Distribute the second term.  */
10079       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
10080       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
10081     }
10082   else
10083     {
10084       /* Distribute the first term.  */
10085       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
10086       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
10087     }
10088
10089   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
10090                                                      new_op0, new_op1));
10091   if (GET_CODE (tmp) != outer_code
10092       && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
10093           < set_src_cost (x, mode, optimize_this_for_speed_p)))
10094     return tmp;
10095
10096   return NULL_RTX;
10097 }
10098 \f
10099 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
10100    in MODE.  Return an equivalent form, if different from (and VAROP
10101    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
10102
10103 static rtx
10104 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10105                           unsigned HOST_WIDE_INT constop)
10106 {
10107   unsigned HOST_WIDE_INT nonzero;
10108   unsigned HOST_WIDE_INT orig_constop;
10109   rtx orig_varop;
10110   int i;
10111
10112   orig_varop = varop;
10113   orig_constop = constop;
10114   if (GET_CODE (varop) == CLOBBER)
10115     return NULL_RTX;
10116
10117   /* Simplify VAROP knowing that we will be only looking at some of the
10118      bits in it.
10119
10120      Note by passing in CONSTOP, we guarantee that the bits not set in
10121      CONSTOP are not significant and will never be examined.  We must
10122      ensure that is the case by explicitly masking out those bits
10123      before returning.  */
10124   varop = force_to_mode (varop, mode, constop, 0);
10125
10126   /* If VAROP is a CLOBBER, we will fail so return it.  */
10127   if (GET_CODE (varop) == CLOBBER)
10128     return varop;
10129
10130   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
10131      to VAROP and return the new constant.  */
10132   if (CONST_INT_P (varop))
10133     return gen_int_mode (INTVAL (varop) & constop, mode);
10134
10135   /* See what bits may be nonzero in VAROP.  Unlike the general case of
10136      a call to nonzero_bits, here we don't care about bits outside
10137      MODE.  */
10138
10139   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
10140
10141   /* Turn off all bits in the constant that are known to already be zero.
10142      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
10143      which is tested below.  */
10144
10145   constop &= nonzero;
10146
10147   /* If we don't have any bits left, return zero.  */
10148   if (constop == 0)
10149     return const0_rtx;
10150
10151   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10152      a power of two, we can replace this with an ASHIFT.  */
10153   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
10154       && (i = exact_log2 (constop)) >= 0)
10155     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10156
10157   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10158      or XOR, then try to apply the distributive law.  This may eliminate
10159      operations if either branch can be simplified because of the AND.
10160      It may also make some cases more complex, but those cases probably
10161      won't match a pattern either with or without this.  */
10162
10163   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10164     {
10165       scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10166       return
10167         gen_lowpart
10168           (mode,
10169            apply_distributive_law
10170            (simplify_gen_binary (GET_CODE (varop), varop_mode,
10171                                  simplify_and_const_int (NULL_RTX, varop_mode,
10172                                                          XEXP (varop, 0),
10173                                                          constop),
10174                                  simplify_and_const_int (NULL_RTX, varop_mode,
10175                                                          XEXP (varop, 1),
10176                                                          constop))));
10177     }
10178
10179   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10180      the AND and see if one of the operands simplifies to zero.  If so, we
10181      may eliminate it.  */
10182
10183   if (GET_CODE (varop) == PLUS
10184       && pow2p_hwi (constop + 1))
10185     {
10186       rtx o0, o1;
10187
10188       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10189       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10190       if (o0 == const0_rtx)
10191         return o1;
10192       if (o1 == const0_rtx)
10193         return o0;
10194     }
10195
10196   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
10197   varop = gen_lowpart (mode, varop);
10198   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10199     return NULL_RTX;
10200
10201   /* If we are only masking insignificant bits, return VAROP.  */
10202   if (constop == nonzero)
10203     return varop;
10204
10205   if (varop == orig_varop && constop == orig_constop)
10206     return NULL_RTX;
10207
10208   /* Otherwise, return an AND.  */
10209   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10210 }
10211
10212
10213 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10214    in MODE.
10215
10216    Return an equivalent form, if different from X.  Otherwise, return X.  If
10217    X is zero, we are to always construct the equivalent form.  */
10218
10219 static rtx
10220 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10221                         unsigned HOST_WIDE_INT constop)
10222 {
10223   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10224   if (tem)
10225     return tem;
10226
10227   if (!x)
10228     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10229                              gen_int_mode (constop, mode));
10230   if (GET_MODE (x) != mode)
10231     x = gen_lowpart (mode, x);
10232   return x;
10233 }
10234 \f
10235 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10236    We don't care about bits outside of those defined in MODE.
10237    We DO care about all the bits in MODE, even if XMODE is smaller than MODE.
10238
10239    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10240    a shift, AND, or zero_extract, we can do better.  */
10241
10242 static rtx
10243 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10244                               scalar_int_mode mode,
10245                               unsigned HOST_WIDE_INT *nonzero)
10246 {
10247   rtx tem;
10248   reg_stat_type *rsp;
10249
10250   /* If X is a register whose nonzero bits value is current, use it.
10251      Otherwise, if X is a register whose value we can find, use that
10252      value.  Otherwise, use the previously-computed global nonzero bits
10253      for this register.  */
10254
10255   rsp = &reg_stat[REGNO (x)];
10256   if (rsp->last_set_value != 0
10257       && (rsp->last_set_mode == mode
10258           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10259               && GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10260               && GET_MODE_CLASS (mode) == MODE_INT))
10261       && ((rsp->last_set_label >= label_tick_ebb_start
10262            && rsp->last_set_label < label_tick)
10263           || (rsp->last_set_label == label_tick
10264               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10265           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10266               && REGNO (x) < reg_n_sets_max
10267               && REG_N_SETS (REGNO (x)) == 1
10268               && !REGNO_REG_SET_P
10269                   (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10270                    REGNO (x)))))
10271     {
10272       /* Note that, even if the precision of last_set_mode is lower than that
10273          of mode, record_value_for_reg invoked nonzero_bits on the register
10274          with nonzero_bits_mode (because last_set_mode is necessarily integral
10275          and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10276          are all valid, hence in mode too since nonzero_bits_mode is defined
10277          to the largest HWI_COMPUTABLE_MODE_P mode.  */
10278       *nonzero &= rsp->last_set_nonzero_bits;
10279       return NULL;
10280     }
10281
10282   tem = get_last_value (x);
10283   if (tem)
10284     {
10285       if (SHORT_IMMEDIATES_SIGN_EXTEND)
10286         tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10287
10288       return tem;
10289     }
10290
10291   if (nonzero_sign_valid && rsp->nonzero_bits)
10292     {
10293       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10294
10295       if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10296         /* We don't know anything about the upper bits.  */
10297         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10298
10299       *nonzero &= mask;
10300     }
10301
10302   return NULL;
10303 }
10304
10305 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10306    end of X that are known to be equal to the sign bit.  X will be used
10307    in mode MODE; the returned value will always be between 1 and the
10308    number of bits in MODE.  */
10309
10310 static rtx
10311 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10312                                      scalar_int_mode mode,
10313                                      unsigned int *result)
10314 {
10315   rtx tem;
10316   reg_stat_type *rsp;
10317
10318   rsp = &reg_stat[REGNO (x)];
10319   if (rsp->last_set_value != 0
10320       && rsp->last_set_mode == mode
10321       && ((rsp->last_set_label >= label_tick_ebb_start
10322            && rsp->last_set_label < label_tick)
10323           || (rsp->last_set_label == label_tick
10324               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10325           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10326               && REGNO (x) < reg_n_sets_max
10327               && REG_N_SETS (REGNO (x)) == 1
10328               && !REGNO_REG_SET_P
10329                   (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10330                    REGNO (x)))))
10331     {
10332       *result = rsp->last_set_sign_bit_copies;
10333       return NULL;
10334     }
10335
10336   tem = get_last_value (x);
10337   if (tem != 0)
10338     return tem;
10339
10340   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10341       && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10342     *result = rsp->sign_bit_copies;
10343
10344   return NULL;
10345 }
10346 \f
10347 /* Return the number of "extended" bits there are in X, when interpreted
10348    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
10349    unsigned quantities, this is the number of high-order zero bits.
10350    For signed quantities, this is the number of copies of the sign bit
10351    minus 1.  In both case, this function returns the number of "spare"
10352    bits.  For example, if two quantities for which this function returns
10353    at least 1 are added, the addition is known not to overflow.
10354
10355    This function will always return 0 unless called during combine, which
10356    implies that it must be called from a define_split.  */
10357
10358 unsigned int
10359 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10360 {
10361   if (nonzero_sign_valid == 0)
10362     return 0;
10363
10364   scalar_int_mode int_mode;
10365   return (unsignedp
10366           ? (is_a <scalar_int_mode> (mode, &int_mode)
10367              && HWI_COMPUTABLE_MODE_P (int_mode)
10368              ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10369                                - floor_log2 (nonzero_bits (x, int_mode)))
10370              : 0)
10371           : num_sign_bit_copies (x, mode) - 1);
10372 }
10373
10374 /* This function is called from `simplify_shift_const' to merge two
10375    outer operations.  Specifically, we have already found that we need
10376    to perform operation *POP0 with constant *PCONST0 at the outermost
10377    position.  We would now like to also perform OP1 with constant CONST1
10378    (with *POP0 being done last).
10379
10380    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10381    the resulting operation.  *PCOMP_P is set to 1 if we would need to
10382    complement the innermost operand, otherwise it is unchanged.
10383
10384    MODE is the mode in which the operation will be done.  No bits outside
10385    the width of this mode matter.  It is assumed that the width of this mode
10386    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10387
10388    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
10389    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
10390    result is simply *PCONST0.
10391
10392    If the resulting operation cannot be expressed as one operation, we
10393    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
10394
10395 static int
10396 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, machine_mode mode, int *pcomp_p)
10397 {
10398   enum rtx_code op0 = *pop0;
10399   HOST_WIDE_INT const0 = *pconst0;
10400
10401   const0 &= GET_MODE_MASK (mode);
10402   const1 &= GET_MODE_MASK (mode);
10403
10404   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
10405   if (op0 == AND)
10406     const1 &= const0;
10407
10408   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
10409      if OP0 is SET.  */
10410
10411   if (op1 == UNKNOWN || op0 == SET)
10412     return 1;
10413
10414   else if (op0 == UNKNOWN)
10415     op0 = op1, const0 = const1;
10416
10417   else if (op0 == op1)
10418     {
10419       switch (op0)
10420         {
10421         case AND:
10422           const0 &= const1;
10423           break;
10424         case IOR:
10425           const0 |= const1;
10426           break;
10427         case XOR:
10428           const0 ^= const1;
10429           break;
10430         case PLUS:
10431           const0 += const1;
10432           break;
10433         case NEG:
10434           op0 = UNKNOWN;
10435           break;
10436         default:
10437           break;
10438         }
10439     }
10440
10441   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
10442   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10443     return 0;
10444
10445   /* If the two constants aren't the same, we can't do anything.  The
10446      remaining six cases can all be done.  */
10447   else if (const0 != const1)
10448     return 0;
10449
10450   else
10451     switch (op0)
10452       {
10453       case IOR:
10454         if (op1 == AND)
10455           /* (a & b) | b == b */
10456           op0 = SET;
10457         else /* op1 == XOR */
10458           /* (a ^ b) | b == a | b */
10459           {;}
10460         break;
10461
10462       case XOR:
10463         if (op1 == AND)
10464           /* (a & b) ^ b == (~a) & b */
10465           op0 = AND, *pcomp_p = 1;
10466         else /* op1 == IOR */
10467           /* (a | b) ^ b == a & ~b */
10468           op0 = AND, const0 = ~const0;
10469         break;
10470
10471       case AND:
10472         if (op1 == IOR)
10473           /* (a | b) & b == b */
10474         op0 = SET;
10475         else /* op1 == XOR */
10476           /* (a ^ b) & b) == (~a) & b */
10477           *pcomp_p = 1;
10478         break;
10479       default:
10480         break;
10481       }
10482
10483   /* Check for NO-OP cases.  */
10484   const0 &= GET_MODE_MASK (mode);
10485   if (const0 == 0
10486       && (op0 == IOR || op0 == XOR || op0 == PLUS))
10487     op0 = UNKNOWN;
10488   else if (const0 == 0 && op0 == AND)
10489     op0 = SET;
10490   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10491            && op0 == AND)
10492     op0 = UNKNOWN;
10493
10494   *pop0 = op0;
10495
10496   /* ??? Slightly redundant with the above mask, but not entirely.
10497      Moving this above means we'd have to sign-extend the mode mask
10498      for the final test.  */
10499   if (op0 != UNKNOWN && op0 != NEG)
10500     *pconst0 = trunc_int_for_mode (const0, mode);
10501
10502   return 1;
10503 }
10504 \f
10505 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10506    the shift in.  The original shift operation CODE is performed on OP in
10507    ORIG_MODE.  Return the wider mode MODE if we can perform the operation
10508    in that mode.  Return ORIG_MODE otherwise.  We can also assume that the
10509    result of the shift is subject to operation OUTER_CODE with operand
10510    OUTER_CONST.  */
10511
10512 static scalar_int_mode
10513 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10514                       scalar_int_mode orig_mode, scalar_int_mode mode,
10515                       enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10516 {
10517   gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10518
10519   /* In general we can't perform in wider mode for right shift and rotate.  */
10520   switch (code)
10521     {
10522     case ASHIFTRT:
10523       /* We can still widen if the bits brought in from the left are identical
10524          to the sign bit of ORIG_MODE.  */
10525       if (num_sign_bit_copies (op, mode)
10526           > (unsigned) (GET_MODE_PRECISION (mode)
10527                         - GET_MODE_PRECISION (orig_mode)))
10528         return mode;
10529       return orig_mode;
10530
10531     case LSHIFTRT:
10532       /* Similarly here but with zero bits.  */
10533       if (HWI_COMPUTABLE_MODE_P (mode)
10534           && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10535         return mode;
10536
10537       /* We can also widen if the bits brought in will be masked off.  This
10538          operation is performed in ORIG_MODE.  */
10539       if (outer_code == AND)
10540         {
10541           int care_bits = low_bitmask_len (orig_mode, outer_const);
10542
10543           if (care_bits >= 0
10544               && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10545             return mode;
10546         }
10547       /* fall through */
10548
10549     case ROTATE:
10550       return orig_mode;
10551
10552     case ROTATERT:
10553       gcc_unreachable ();
10554
10555     default:
10556       return mode;
10557     }
10558 }
10559
10560 /* Simplify a shift of VAROP by ORIG_COUNT bits.  CODE says what kind
10561    of shift.  The result of the shift is RESULT_MODE.  Return NULL_RTX
10562    if we cannot simplify it.  Otherwise, return a simplified value.
10563
10564    The shift is normally computed in the widest mode we find in VAROP, as
10565    long as it isn't a different number of words than RESULT_MODE.  Exceptions
10566    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
10567
10568 static rtx
10569 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10570                         rtx varop, int orig_count)
10571 {
10572   enum rtx_code orig_code = code;
10573   rtx orig_varop = varop;
10574   int count, log2;
10575   machine_mode mode = result_mode;
10576   machine_mode shift_mode;
10577   scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10578   /* We form (outer_op (code varop count) (outer_const)).  */
10579   enum rtx_code outer_op = UNKNOWN;
10580   HOST_WIDE_INT outer_const = 0;
10581   int complement_p = 0;
10582   rtx new_rtx, x;
10583
10584   /* Make sure and truncate the "natural" shift on the way in.  We don't
10585      want to do this inside the loop as it makes it more difficult to
10586      combine shifts.  */
10587   if (SHIFT_COUNT_TRUNCATED)
10588     orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10589
10590   /* If we were given an invalid count, don't do anything except exactly
10591      what was requested.  */
10592
10593   if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10594     return NULL_RTX;
10595
10596   count = orig_count;
10597
10598   /* Unless one of the branches of the `if' in this loop does a `continue',
10599      we will `break' the loop after the `if'.  */
10600
10601   while (count != 0)
10602     {
10603       /* If we have an operand of (clobber (const_int 0)), fail.  */
10604       if (GET_CODE (varop) == CLOBBER)
10605         return NULL_RTX;
10606
10607       /* Convert ROTATERT to ROTATE.  */
10608       if (code == ROTATERT)
10609         {
10610           unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10611           code = ROTATE;
10612           count = bitsize - count;
10613         }
10614
10615       shift_mode = result_mode;
10616       if (shift_mode != mode)
10617         {
10618           /* We only change the modes of scalar shifts.  */
10619           int_mode = as_a <scalar_int_mode> (mode);
10620           int_result_mode = as_a <scalar_int_mode> (result_mode);
10621           shift_mode = try_widen_shift_mode (code, varop, count,
10622                                              int_result_mode, int_mode,
10623                                              outer_op, outer_const);
10624         }
10625
10626       scalar_int_mode shift_unit_mode
10627         = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10628
10629       /* Handle cases where the count is greater than the size of the mode
10630          minus 1.  For ASHIFT, use the size minus one as the count (this can
10631          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
10632          take the count modulo the size.  For other shifts, the result is
10633          zero.
10634
10635          Since these shifts are being produced by the compiler by combining
10636          multiple operations, each of which are defined, we know what the
10637          result is supposed to be.  */
10638
10639       if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10640         {
10641           if (code == ASHIFTRT)
10642             count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10643           else if (code == ROTATE || code == ROTATERT)
10644             count %= GET_MODE_PRECISION (shift_unit_mode);
10645           else
10646             {
10647               /* We can't simply return zero because there may be an
10648                  outer op.  */
10649               varop = const0_rtx;
10650               count = 0;
10651               break;
10652             }
10653         }
10654
10655       /* If we discovered we had to complement VAROP, leave.  Making a NOT
10656          here would cause an infinite loop.  */
10657       if (complement_p)
10658         break;
10659
10660       if (shift_mode == shift_unit_mode)
10661         {
10662           /* An arithmetic right shift of a quantity known to be -1 or 0
10663              is a no-op.  */
10664           if (code == ASHIFTRT
10665               && (num_sign_bit_copies (varop, shift_unit_mode)
10666                   == GET_MODE_PRECISION (shift_unit_mode)))
10667             {
10668               count = 0;
10669               break;
10670             }
10671
10672           /* If we are doing an arithmetic right shift and discarding all but
10673              the sign bit copies, this is equivalent to doing a shift by the
10674              bitsize minus one.  Convert it into that shift because it will
10675              often allow other simplifications.  */
10676
10677           if (code == ASHIFTRT
10678               && (count + num_sign_bit_copies (varop, shift_unit_mode)
10679                   >= GET_MODE_PRECISION (shift_unit_mode)))
10680             count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10681
10682           /* We simplify the tests below and elsewhere by converting
10683              ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10684              `make_compound_operation' will convert it to an ASHIFTRT for
10685              those machines (such as VAX) that don't have an LSHIFTRT.  */
10686           if (code == ASHIFTRT
10687               && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10688               && val_signbit_known_clear_p (shift_unit_mode,
10689                                             nonzero_bits (varop,
10690                                                           shift_unit_mode)))
10691             code = LSHIFTRT;
10692
10693           if (((code == LSHIFTRT
10694                 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10695                 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10696                || (code == ASHIFT
10697                    && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10698                    && !((nonzero_bits (varop, shift_unit_mode) << count)
10699                         & GET_MODE_MASK (shift_unit_mode))))
10700               && !side_effects_p (varop))
10701             varop = const0_rtx;
10702         }
10703
10704       switch (GET_CODE (varop))
10705         {
10706         case SIGN_EXTEND:
10707         case ZERO_EXTEND:
10708         case SIGN_EXTRACT:
10709         case ZERO_EXTRACT:
10710           new_rtx = expand_compound_operation (varop);
10711           if (new_rtx != varop)
10712             {
10713               varop = new_rtx;
10714               continue;
10715             }
10716           break;
10717
10718         case MEM:
10719           /* The following rules apply only to scalars.  */
10720           if (shift_mode != shift_unit_mode)
10721             break;
10722           int_mode = as_a <scalar_int_mode> (mode);
10723
10724           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10725              minus the width of a smaller mode, we can do this with a
10726              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
10727           if ((code == ASHIFTRT || code == LSHIFTRT)
10728               && ! mode_dependent_address_p (XEXP (varop, 0),
10729                                              MEM_ADDR_SPACE (varop))
10730               && ! MEM_VOLATILE_P (varop)
10731               && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10732                   .exists (&tmode)))
10733             {
10734               new_rtx = adjust_address_nv (varop, tmode,
10735                                            BYTES_BIG_ENDIAN ? 0
10736                                            : count / BITS_PER_UNIT);
10737
10738               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10739                                      : ZERO_EXTEND, int_mode, new_rtx);
10740               count = 0;
10741               continue;
10742             }
10743           break;
10744
10745         case SUBREG:
10746           /* The following rules apply only to scalars.  */
10747           if (shift_mode != shift_unit_mode)
10748             break;
10749           int_mode = as_a <scalar_int_mode> (mode);
10750           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10751
10752           /* If VAROP is a SUBREG, strip it as long as the inner operand has
10753              the same number of words as what we've seen so far.  Then store
10754              the widest mode in MODE.  */
10755           if (subreg_lowpart_p (varop)
10756               && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10757               && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10758               && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10759                   == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10760               && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10761             {
10762               varop = SUBREG_REG (varop);
10763               if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10764                 mode = inner_mode;
10765               continue;
10766             }
10767           break;
10768
10769         case MULT:
10770           /* Some machines use MULT instead of ASHIFT because MULT
10771              is cheaper.  But it is still better on those machines to
10772              merge two shifts into one.  */
10773           if (CONST_INT_P (XEXP (varop, 1))
10774               && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10775             {
10776               rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10777               varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10778                                            XEXP (varop, 0), log2_rtx);
10779               continue;
10780             }
10781           break;
10782
10783         case UDIV:
10784           /* Similar, for when divides are cheaper.  */
10785           if (CONST_INT_P (XEXP (varop, 1))
10786               && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10787             {
10788               rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10789               varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10790                                            XEXP (varop, 0), log2_rtx);
10791               continue;
10792             }
10793           break;
10794
10795         case ASHIFTRT:
10796           /* If we are extracting just the sign bit of an arithmetic
10797              right shift, that shift is not needed.  However, the sign
10798              bit of a wider mode may be different from what would be
10799              interpreted as the sign bit in a narrower mode, so, if
10800              the result is narrower, don't discard the shift.  */
10801           if (code == LSHIFTRT
10802               && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10803               && (GET_MODE_UNIT_BITSIZE (result_mode)
10804                   >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10805             {
10806               varop = XEXP (varop, 0);
10807               continue;
10808             }
10809
10810           /* fall through */
10811
10812         case LSHIFTRT:
10813         case ASHIFT:
10814         case ROTATE:
10815           /* The following rules apply only to scalars.  */
10816           if (shift_mode != shift_unit_mode)
10817             break;
10818           int_mode = as_a <scalar_int_mode> (mode);
10819           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10820           int_result_mode = as_a <scalar_int_mode> (result_mode);
10821
10822           /* Here we have two nested shifts.  The result is usually the
10823              AND of a new shift with a mask.  We compute the result below.  */
10824           if (CONST_INT_P (XEXP (varop, 1))
10825               && INTVAL (XEXP (varop, 1)) >= 0
10826               && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10827               && HWI_COMPUTABLE_MODE_P (int_result_mode)
10828               && HWI_COMPUTABLE_MODE_P (int_mode))
10829             {
10830               enum rtx_code first_code = GET_CODE (varop);
10831               unsigned int first_count = INTVAL (XEXP (varop, 1));
10832               unsigned HOST_WIDE_INT mask;
10833               rtx mask_rtx;
10834
10835               /* We have one common special case.  We can't do any merging if
10836                  the inner code is an ASHIFTRT of a smaller mode.  However, if
10837                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10838                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10839                  we can convert it to
10840                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10841                  This simplifies certain SIGN_EXTEND operations.  */
10842               if (code == ASHIFT && first_code == ASHIFTRT
10843                   && count == (GET_MODE_PRECISION (int_result_mode)
10844                                - GET_MODE_PRECISION (int_varop_mode)))
10845                 {
10846                   /* C3 has the low-order C1 bits zero.  */
10847
10848                   mask = GET_MODE_MASK (int_mode)
10849                          & ~((HOST_WIDE_INT_1U << first_count) - 1);
10850
10851                   varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10852                                                   XEXP (varop, 0), mask);
10853                   varop = simplify_shift_const (NULL_RTX, ASHIFT,
10854                                                 int_result_mode, varop, count);
10855                   count = first_count;
10856                   code = ASHIFTRT;
10857                   continue;
10858                 }
10859
10860               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10861                  than C1 high-order bits equal to the sign bit, we can convert
10862                  this to either an ASHIFT or an ASHIFTRT depending on the
10863                  two counts.
10864
10865                  We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE.  */
10866
10867               if (code == ASHIFTRT && first_code == ASHIFT
10868                   && int_varop_mode == shift_unit_mode
10869                   && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10870                       > first_count))
10871                 {
10872                   varop = XEXP (varop, 0);
10873                   count -= first_count;
10874                   if (count < 0)
10875                     {
10876                       count = -count;
10877                       code = ASHIFT;
10878                     }
10879
10880                   continue;
10881                 }
10882
10883               /* There are some cases we can't do.  If CODE is ASHIFTRT,
10884                  we can only do this if FIRST_CODE is also ASHIFTRT.
10885
10886                  We can't do the case when CODE is ROTATE and FIRST_CODE is
10887                  ASHIFTRT.
10888
10889                  If the mode of this shift is not the mode of the outer shift,
10890                  we can't do this if either shift is a right shift or ROTATE.
10891
10892                  Finally, we can't do any of these if the mode is too wide
10893                  unless the codes are the same.
10894
10895                  Handle the case where the shift codes are the same
10896                  first.  */
10897
10898               if (code == first_code)
10899                 {
10900                   if (int_varop_mode != int_result_mode
10901                       && (code == ASHIFTRT || code == LSHIFTRT
10902                           || code == ROTATE))
10903                     break;
10904
10905                   count += first_count;
10906                   varop = XEXP (varop, 0);
10907                   continue;
10908                 }
10909
10910               if (code == ASHIFTRT
10911                   || (code == ROTATE && first_code == ASHIFTRT)
10912                   || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10913                   || (int_varop_mode != int_result_mode
10914                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
10915                           || first_code == ROTATE
10916                           || code == ROTATE)))
10917                 break;
10918
10919               /* To compute the mask to apply after the shift, shift the
10920                  nonzero bits of the inner shift the same way the
10921                  outer shift will.  */
10922
10923               mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10924                                        int_result_mode);
10925               rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10926               mask_rtx
10927                 = simplify_const_binary_operation (code, int_result_mode,
10928                                                    mask_rtx, count_rtx);
10929
10930               /* Give up if we can't compute an outer operation to use.  */
10931               if (mask_rtx == 0
10932                   || !CONST_INT_P (mask_rtx)
10933                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
10934                                         INTVAL (mask_rtx),
10935                                         int_result_mode, &complement_p))
10936                 break;
10937
10938               /* If the shifts are in the same direction, we add the
10939                  counts.  Otherwise, we subtract them.  */
10940               if ((code == ASHIFTRT || code == LSHIFTRT)
10941                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10942                 count += first_count;
10943               else
10944                 count -= first_count;
10945
10946               /* If COUNT is positive, the new shift is usually CODE,
10947                  except for the two exceptions below, in which case it is
10948                  FIRST_CODE.  If the count is negative, FIRST_CODE should
10949                  always be used  */
10950               if (count > 0
10951                   && ((first_code == ROTATE && code == ASHIFT)
10952                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
10953                 code = first_code;
10954               else if (count < 0)
10955                 code = first_code, count = -count;
10956
10957               varop = XEXP (varop, 0);
10958               continue;
10959             }
10960
10961           /* If we have (A << B << C) for any shift, we can convert this to
10962              (A << C << B).  This wins if A is a constant.  Only try this if
10963              B is not a constant.  */
10964
10965           else if (GET_CODE (varop) == code
10966                    && CONST_INT_P (XEXP (varop, 0))
10967                    && !CONST_INT_P (XEXP (varop, 1)))
10968             {
10969               /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10970                  sure the result will be masked.  See PR70222.  */
10971               if (code == LSHIFTRT
10972                   && int_mode != int_result_mode
10973                   && !merge_outer_ops (&outer_op, &outer_const, AND,
10974                                        GET_MODE_MASK (int_result_mode)
10975                                        >> orig_count, int_result_mode,
10976                                        &complement_p))
10977                 break;
10978               /* For ((int) (cstLL >> count)) >> cst2 just give up.  Queuing
10979                  up outer sign extension (often left and right shift) is
10980                  hardly more efficient than the original.  See PR70429.  */
10981               if (code == ASHIFTRT && int_mode != int_result_mode)
10982                 break;
10983
10984               rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10985               rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10986                                                              XEXP (varop, 0),
10987                                                              count_rtx);
10988               varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10989               count = 0;
10990               continue;
10991             }
10992           break;
10993
10994         case NOT:
10995           /* The following rules apply only to scalars.  */
10996           if (shift_mode != shift_unit_mode)
10997             break;
10998
10999           /* Make this fit the case below.  */
11000           varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
11001           continue;
11002
11003         case IOR:
11004         case AND:
11005         case XOR:
11006           /* The following rules apply only to scalars.  */
11007           if (shift_mode != shift_unit_mode)
11008             break;
11009           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11010           int_result_mode = as_a <scalar_int_mode> (result_mode);
11011
11012           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
11013              with C the size of VAROP - 1 and the shift is logical if
11014              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11015              we have an (le X 0) operation.   If we have an arithmetic shift
11016              and STORE_FLAG_VALUE is 1 or we have a logical shift with
11017              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
11018
11019           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
11020               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
11021               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11022               && (code == LSHIFTRT || code == ASHIFTRT)
11023               && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11024               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11025             {
11026               count = 0;
11027               varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
11028                                   const0_rtx);
11029
11030               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11031                 varop = gen_rtx_NEG (int_varop_mode, varop);
11032
11033               continue;
11034             }
11035
11036           /* If we have (shift (logical)), move the logical to the outside
11037              to allow it to possibly combine with another logical and the
11038              shift to combine with another shift.  This also canonicalizes to
11039              what a ZERO_EXTRACT looks like.  Also, some machines have
11040              (and (shift)) insns.  */
11041
11042           if (CONST_INT_P (XEXP (varop, 1))
11043               /* We can't do this if we have (ashiftrt (xor))  and the
11044                  constant has its sign bit set in shift_unit_mode with
11045                  shift_unit_mode wider than result_mode.  */
11046               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11047                    && int_result_mode != shift_unit_mode
11048                    && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11049                                           shift_unit_mode) < 0)
11050               && (new_rtx = simplify_const_binary_operation
11051                   (code, int_result_mode,
11052                    gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11053                    gen_int_shift_amount (int_result_mode, count))) != 0
11054               && CONST_INT_P (new_rtx)
11055               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
11056                                   INTVAL (new_rtx), int_result_mode,
11057                                   &complement_p))
11058             {
11059               varop = XEXP (varop, 0);
11060               continue;
11061             }
11062
11063           /* If we can't do that, try to simplify the shift in each arm of the
11064              logical expression, make a new logical expression, and apply
11065              the inverse distributive law.  This also can't be done for
11066              (ashiftrt (xor)) where we've widened the shift and the constant
11067              changes the sign bit.  */
11068           if (CONST_INT_P (XEXP (varop, 1))
11069               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11070                    && int_result_mode != shift_unit_mode
11071                    && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11072                                           shift_unit_mode) < 0))
11073             {
11074               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11075                                               XEXP (varop, 0), count);
11076               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11077                                               XEXP (varop, 1), count);
11078
11079               varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
11080                                            lhs, rhs);
11081               varop = apply_distributive_law (varop);
11082
11083               count = 0;
11084               continue;
11085             }
11086           break;
11087
11088         case EQ:
11089           /* The following rules apply only to scalars.  */
11090           if (shift_mode != shift_unit_mode)
11091             break;
11092           int_result_mode = as_a <scalar_int_mode> (result_mode);
11093
11094           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
11095              says that the sign bit can be tested, FOO has mode MODE, C is
11096              GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11097              that may be nonzero.  */
11098           if (code == LSHIFTRT
11099               && XEXP (varop, 1) == const0_rtx
11100               && GET_MODE (XEXP (varop, 0)) == int_result_mode
11101               && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11102               && HWI_COMPUTABLE_MODE_P (int_result_mode)
11103               && STORE_FLAG_VALUE == -1
11104               && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11105               && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11106                                   int_result_mode, &complement_p))
11107             {
11108               varop = XEXP (varop, 0);
11109               count = 0;
11110               continue;
11111             }
11112           break;
11113
11114         case NEG:
11115           /* The following rules apply only to scalars.  */
11116           if (shift_mode != shift_unit_mode)
11117             break;
11118           int_result_mode = as_a <scalar_int_mode> (result_mode);
11119
11120           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11121              than the number of bits in the mode is equivalent to A.  */
11122           if (code == LSHIFTRT
11123               && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11124               && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11125             {
11126               varop = XEXP (varop, 0);
11127               count = 0;
11128               continue;
11129             }
11130
11131           /* NEG commutes with ASHIFT since it is multiplication.  Move the
11132              NEG outside to allow shifts to combine.  */
11133           if (code == ASHIFT
11134               && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11135                                   int_result_mode, &complement_p))
11136             {
11137               varop = XEXP (varop, 0);
11138               continue;
11139             }
11140           break;
11141
11142         case PLUS:
11143           /* The following rules apply only to scalars.  */
11144           if (shift_mode != shift_unit_mode)
11145             break;
11146           int_result_mode = as_a <scalar_int_mode> (result_mode);
11147
11148           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11149              is one less than the number of bits in the mode is
11150              equivalent to (xor A 1).  */
11151           if (code == LSHIFTRT
11152               && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11153               && XEXP (varop, 1) == constm1_rtx
11154               && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11155               && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11156                                   int_result_mode, &complement_p))
11157             {
11158               count = 0;
11159               varop = XEXP (varop, 0);
11160               continue;
11161             }
11162
11163           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11164              that might be nonzero in BAR are those being shifted out and those
11165              bits are known zero in FOO, we can replace the PLUS with FOO.
11166              Similarly in the other operand order.  This code occurs when
11167              we are computing the size of a variable-size array.  */
11168
11169           if ((code == ASHIFTRT || code == LSHIFTRT)
11170               && count < HOST_BITS_PER_WIDE_INT
11171               && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11172               && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11173                   & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11174             {
11175               varop = XEXP (varop, 0);
11176               continue;
11177             }
11178           else if ((code == ASHIFTRT || code == LSHIFTRT)
11179                    && count < HOST_BITS_PER_WIDE_INT
11180                    && HWI_COMPUTABLE_MODE_P (int_result_mode)
11181                    && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11182                        >> count) == 0
11183                    && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11184                        & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11185             {
11186               varop = XEXP (varop, 1);
11187               continue;
11188             }
11189
11190           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
11191           if (code == ASHIFT
11192               && CONST_INT_P (XEXP (varop, 1))
11193               && (new_rtx = simplify_const_binary_operation
11194                   (ASHIFT, int_result_mode,
11195                    gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11196                    gen_int_shift_amount (int_result_mode, count))) != 0
11197               && CONST_INT_P (new_rtx)
11198               && merge_outer_ops (&outer_op, &outer_const, PLUS,
11199                                   INTVAL (new_rtx), int_result_mode,
11200                                   &complement_p))
11201             {
11202               varop = XEXP (varop, 0);
11203               continue;
11204             }
11205
11206           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11207              signbit', and attempt to change the PLUS to an XOR and move it to
11208              the outer operation as is done above in the AND/IOR/XOR case
11209              leg for shift(logical). See details in logical handling above
11210              for reasoning in doing so.  */
11211           if (code == LSHIFTRT
11212               && CONST_INT_P (XEXP (varop, 1))
11213               && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11214               && (new_rtx = simplify_const_binary_operation
11215                   (code, int_result_mode,
11216                    gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11217                    gen_int_shift_amount (int_result_mode, count))) != 0
11218               && CONST_INT_P (new_rtx)
11219               && merge_outer_ops (&outer_op, &outer_const, XOR,
11220                                   INTVAL (new_rtx), int_result_mode,
11221                                   &complement_p))
11222             {
11223               varop = XEXP (varop, 0);
11224               continue;
11225             }
11226
11227           break;
11228
11229         case MINUS:
11230           /* The following rules apply only to scalars.  */
11231           if (shift_mode != shift_unit_mode)
11232             break;
11233           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11234
11235           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11236              with C the size of VAROP - 1 and the shift is logical if
11237              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11238              we have a (gt X 0) operation.  If the shift is arithmetic with
11239              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11240              we have a (neg (gt X 0)) operation.  */
11241
11242           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11243               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11244               && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11245               && (code == LSHIFTRT || code == ASHIFTRT)
11246               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11247               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11248               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11249             {
11250               count = 0;
11251               varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11252                                   const0_rtx);
11253
11254               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11255                 varop = gen_rtx_NEG (int_varop_mode, varop);
11256
11257               continue;
11258             }
11259           break;
11260
11261         case TRUNCATE:
11262           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11263              if the truncate does not affect the value.  */
11264           if (code == LSHIFTRT
11265               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11266               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11267               && (INTVAL (XEXP (XEXP (varop, 0), 1))
11268                   >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11269                       - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11270             {
11271               rtx varop_inner = XEXP (varop, 0);
11272               int new_count = count + INTVAL (XEXP (varop_inner, 1));
11273               rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11274                                                         new_count);
11275               varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11276                                               XEXP (varop_inner, 0),
11277                                               new_count_rtx);
11278               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11279               count = 0;
11280               continue;
11281             }
11282           break;
11283
11284         default:
11285           break;
11286         }
11287
11288       break;
11289     }
11290
11291   shift_mode = result_mode;
11292   if (shift_mode != mode)
11293     {
11294       /* We only change the modes of scalar shifts.  */
11295       int_mode = as_a <scalar_int_mode> (mode);
11296       int_result_mode = as_a <scalar_int_mode> (result_mode);
11297       shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11298                                          int_mode, outer_op, outer_const);
11299     }
11300
11301   /* We have now finished analyzing the shift.  The result should be
11302      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
11303      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11304      to the result of the shift.  OUTER_CONST is the relevant constant,
11305      but we must turn off all bits turned off in the shift.  */
11306
11307   if (outer_op == UNKNOWN
11308       && orig_code == code && orig_count == count
11309       && varop == orig_varop
11310       && shift_mode == GET_MODE (varop))
11311     return NULL_RTX;
11312
11313   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
11314   varop = gen_lowpart (shift_mode, varop);
11315   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11316     return NULL_RTX;
11317
11318   /* If we have an outer operation and we just made a shift, it is
11319      possible that we could have simplified the shift were it not
11320      for the outer operation.  So try to do the simplification
11321      recursively.  */
11322
11323   if (outer_op != UNKNOWN)
11324     x = simplify_shift_const_1 (code, shift_mode, varop, count);
11325   else
11326     x = NULL_RTX;
11327
11328   if (x == NULL_RTX)
11329     x = simplify_gen_binary (code, shift_mode, varop,
11330                              gen_int_shift_amount (shift_mode, count));
11331
11332   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11333      turn off all the bits that the shift would have turned off.  */
11334   if (orig_code == LSHIFTRT && result_mode != shift_mode)
11335     /* We only change the modes of scalar shifts.  */
11336     x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11337                                 x, GET_MODE_MASK (result_mode) >> orig_count);
11338
11339   /* Do the remainder of the processing in RESULT_MODE.  */
11340   x = gen_lowpart_or_truncate (result_mode, x);
11341
11342   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11343      operation.  */
11344   if (complement_p)
11345     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11346
11347   if (outer_op != UNKNOWN)
11348     {
11349       int_result_mode = as_a <scalar_int_mode> (result_mode);
11350
11351       if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11352           && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11353         outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11354
11355       if (outer_op == AND)
11356         x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11357       else if (outer_op == SET)
11358         {
11359           /* This means that we have determined that the result is
11360              equivalent to a constant.  This should be rare.  */
11361           if (!side_effects_p (x))
11362             x = GEN_INT (outer_const);
11363         }
11364       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11365         x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11366       else
11367         x = simplify_gen_binary (outer_op, int_result_mode, x,
11368                                  GEN_INT (outer_const));
11369     }
11370
11371   return x;
11372 }
11373
11374 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
11375    The result of the shift is RESULT_MODE.  If we cannot simplify it,
11376    return X or, if it is NULL, synthesize the expression with
11377    simplify_gen_binary.  Otherwise, return a simplified value.
11378
11379    The shift is normally computed in the widest mode we find in VAROP, as
11380    long as it isn't a different number of words than RESULT_MODE.  Exceptions
11381    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
11382
11383 static rtx
11384 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11385                       rtx varop, int count)
11386 {
11387   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11388   if (tem)
11389     return tem;
11390
11391   if (!x)
11392     x = simplify_gen_binary (code, GET_MODE (varop), varop,
11393                              gen_int_shift_amount (GET_MODE (varop), count));
11394   if (GET_MODE (x) != result_mode)
11395     x = gen_lowpart (result_mode, x);
11396   return x;
11397 }
11398
11399 \f
11400 /* A subroutine of recog_for_combine.  See there for arguments and
11401    return value.  */
11402
11403 static int
11404 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11405 {
11406   rtx pat = *pnewpat;
11407   rtx pat_without_clobbers;
11408   int insn_code_number;
11409   int num_clobbers_to_add = 0;
11410   int i;
11411   rtx notes = NULL_RTX;
11412   rtx old_notes, old_pat;
11413   int old_icode;
11414
11415   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11416      we use to indicate that something didn't match.  If we find such a
11417      thing, force rejection.  */
11418   if (GET_CODE (pat) == PARALLEL)
11419     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11420       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11421           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11422         return -1;
11423
11424   old_pat = PATTERN (insn);
11425   old_notes = REG_NOTES (insn);
11426   PATTERN (insn) = pat;
11427   REG_NOTES (insn) = NULL_RTX;
11428
11429   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11430   if (dump_file && (dump_flags & TDF_DETAILS))
11431     {
11432       if (insn_code_number < 0)
11433         fputs ("Failed to match this instruction:\n", dump_file);
11434       else
11435         fputs ("Successfully matched this instruction:\n", dump_file);
11436       print_rtl_single (dump_file, pat);
11437     }
11438
11439   /* If it isn't, there is the possibility that we previously had an insn
11440      that clobbered some register as a side effect, but the combined
11441      insn doesn't need to do that.  So try once more without the clobbers
11442      unless this represents an ASM insn.  */
11443
11444   if (insn_code_number < 0 && ! check_asm_operands (pat)
11445       && GET_CODE (pat) == PARALLEL)
11446     {
11447       int pos;
11448
11449       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11450         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11451           {
11452             if (i != pos)
11453               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11454             pos++;
11455           }
11456
11457       SUBST_INT (XVECLEN (pat, 0), pos);
11458
11459       if (pos == 1)
11460         pat = XVECEXP (pat, 0, 0);
11461
11462       PATTERN (insn) = pat;
11463       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11464       if (dump_file && (dump_flags & TDF_DETAILS))
11465         {
11466           if (insn_code_number < 0)
11467             fputs ("Failed to match this instruction:\n", dump_file);
11468           else
11469             fputs ("Successfully matched this instruction:\n", dump_file);
11470           print_rtl_single (dump_file, pat);
11471         }
11472     }
11473
11474   pat_without_clobbers = pat;
11475
11476   PATTERN (insn) = old_pat;
11477   REG_NOTES (insn) = old_notes;
11478
11479   /* Recognize all noop sets, these will be killed by followup pass.  */
11480   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11481     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11482
11483   /* If we had any clobbers to add, make a new pattern than contains
11484      them.  Then check to make sure that all of them are dead.  */
11485   if (num_clobbers_to_add)
11486     {
11487       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11488                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
11489                                                   ? (XVECLEN (pat, 0)
11490                                                      + num_clobbers_to_add)
11491                                                   : num_clobbers_to_add + 1));
11492
11493       if (GET_CODE (pat) == PARALLEL)
11494         for (i = 0; i < XVECLEN (pat, 0); i++)
11495           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11496       else
11497         XVECEXP (newpat, 0, 0) = pat;
11498
11499       add_clobbers (newpat, insn_code_number);
11500
11501       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11502            i < XVECLEN (newpat, 0); i++)
11503         {
11504           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11505               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11506             return -1;
11507           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11508             {
11509               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11510               notes = alloc_reg_note (REG_UNUSED,
11511                                       XEXP (XVECEXP (newpat, 0, i), 0), notes);
11512             }
11513         }
11514       pat = newpat;
11515     }
11516
11517   if (insn_code_number >= 0
11518       && insn_code_number != NOOP_MOVE_INSN_CODE)
11519     {
11520       old_pat = PATTERN (insn);
11521       old_notes = REG_NOTES (insn);
11522       old_icode = INSN_CODE (insn);
11523       PATTERN (insn) = pat;
11524       REG_NOTES (insn) = notes;
11525       INSN_CODE (insn) = insn_code_number;
11526
11527       /* Allow targets to reject combined insn.  */
11528       if (!targetm.legitimate_combined_insn (insn))
11529         {
11530           if (dump_file && (dump_flags & TDF_DETAILS))
11531             fputs ("Instruction not appropriate for target.",
11532                    dump_file);
11533
11534           /* Callers expect recog_for_combine to strip
11535              clobbers from the pattern on failure.  */
11536           pat = pat_without_clobbers;
11537           notes = NULL_RTX;
11538
11539           insn_code_number = -1;
11540         }
11541
11542       PATTERN (insn) = old_pat;
11543       REG_NOTES (insn) = old_notes;
11544       INSN_CODE (insn) = old_icode;
11545     }
11546
11547   *pnewpat = pat;
11548   *pnotes = notes;
11549
11550   return insn_code_number;
11551 }
11552
11553 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11554    expressed as an AND and maybe an LSHIFTRT, to that formulation.
11555    Return whether anything was so changed.  */
11556
11557 static bool
11558 change_zero_ext (rtx pat)
11559 {
11560   bool changed = false;
11561   rtx *src = &SET_SRC (pat);
11562
11563   subrtx_ptr_iterator::array_type array;
11564   FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11565     {
11566       rtx x = **iter;
11567       scalar_int_mode mode, inner_mode;
11568       if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11569         continue;
11570       int size;
11571
11572       if (GET_CODE (x) == ZERO_EXTRACT
11573           && CONST_INT_P (XEXP (x, 1))
11574           && CONST_INT_P (XEXP (x, 2))
11575           && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11576           && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11577         {
11578           size = INTVAL (XEXP (x, 1));
11579
11580           int start = INTVAL (XEXP (x, 2));
11581           if (BITS_BIG_ENDIAN)
11582             start = GET_MODE_PRECISION (inner_mode) - size - start;
11583
11584           if (start != 0)
11585             x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11586                                   gen_int_shift_amount (inner_mode, start));
11587           else
11588             x = XEXP (x, 0);
11589
11590           if (mode != inner_mode)
11591             {
11592               if (REG_P (x) && HARD_REGISTER_P (x)
11593                   && !can_change_dest_mode (x, 0, mode))
11594                 continue;
11595
11596               x = gen_lowpart_SUBREG (mode, x);
11597             }
11598         }
11599       else if (GET_CODE (x) == ZERO_EXTEND
11600                && GET_CODE (XEXP (x, 0)) == SUBREG
11601                && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11602                && !paradoxical_subreg_p (XEXP (x, 0))
11603                && subreg_lowpart_p (XEXP (x, 0)))
11604         {
11605           inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11606           size = GET_MODE_PRECISION (inner_mode);
11607           x = SUBREG_REG (XEXP (x, 0));
11608           if (GET_MODE (x) != mode)
11609             {
11610               if (REG_P (x) && HARD_REGISTER_P (x)
11611                   && !can_change_dest_mode (x, 0, mode))
11612                 continue;
11613
11614               x = gen_lowpart_SUBREG (mode, x);
11615             }
11616         }
11617       else if (GET_CODE (x) == ZERO_EXTEND
11618                && REG_P (XEXP (x, 0))
11619                && HARD_REGISTER_P (XEXP (x, 0))
11620                && can_change_dest_mode (XEXP (x, 0), 0, mode))
11621         {
11622           inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11623           size = GET_MODE_PRECISION (inner_mode);
11624           x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11625         }
11626       else
11627         continue;
11628
11629       if (!(GET_CODE (x) == LSHIFTRT
11630             && CONST_INT_P (XEXP (x, 1))
11631             && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11632         {
11633           wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11634           x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11635         }
11636
11637       SUBST (**iter, x);
11638       changed = true;
11639     }
11640
11641   if (changed)
11642     FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11643       maybe_swap_commutative_operands (**iter);
11644
11645   rtx *dst = &SET_DEST (pat);
11646   scalar_int_mode mode;
11647   if (GET_CODE (*dst) == ZERO_EXTRACT
11648       && REG_P (XEXP (*dst, 0))
11649       && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11650       && CONST_INT_P (XEXP (*dst, 1))
11651       && CONST_INT_P (XEXP (*dst, 2)))
11652     {
11653       rtx reg = XEXP (*dst, 0);
11654       int width = INTVAL (XEXP (*dst, 1));
11655       int offset = INTVAL (XEXP (*dst, 2));
11656       int reg_width = GET_MODE_PRECISION (mode);
11657       if (BITS_BIG_ENDIAN)
11658         offset = reg_width - width - offset;
11659
11660       rtx x, y, z, w;
11661       wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11662       wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11663       x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11664       if (offset)
11665         y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11666       else
11667         y = SET_SRC (pat);
11668       z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11669       w = gen_rtx_IOR (mode, x, z);
11670       SUBST (SET_DEST (pat), reg);
11671       SUBST (SET_SRC (pat), w);
11672
11673       changed = true;
11674     }
11675
11676   return changed;
11677 }
11678
11679 /* Like recog, but we receive the address of a pointer to a new pattern.
11680    We try to match the rtx that the pointer points to.
11681    If that fails, we may try to modify or replace the pattern,
11682    storing the replacement into the same pointer object.
11683
11684    Modifications include deletion or addition of CLOBBERs.  If the
11685    instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11686    to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11687    (and undo if that fails).
11688
11689    PNOTES is a pointer to a location where any REG_UNUSED notes added for
11690    the CLOBBERs are placed.
11691
11692    The value is the final insn code from the pattern ultimately matched,
11693    or -1.  */
11694
11695 static int
11696 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11697 {
11698   rtx pat = *pnewpat;
11699   int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11700   if (insn_code_number >= 0 || check_asm_operands (pat))
11701     return insn_code_number;
11702
11703   void *marker = get_undo_marker ();
11704   bool changed = false;
11705
11706   if (GET_CODE (pat) == SET)
11707     changed = change_zero_ext (pat);
11708   else if (GET_CODE (pat) == PARALLEL)
11709     {
11710       int i;
11711       for (i = 0; i < XVECLEN (pat, 0); i++)
11712         {
11713           rtx set = XVECEXP (pat, 0, i);
11714           if (GET_CODE (set) == SET)
11715             changed |= change_zero_ext (set);
11716         }
11717     }
11718
11719   if (changed)
11720     {
11721       insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11722
11723       if (insn_code_number < 0)
11724         undo_to_marker (marker);
11725     }
11726
11727   return insn_code_number;
11728 }
11729 \f
11730 /* Like gen_lowpart_general but for use by combine.  In combine it
11731    is not possible to create any new pseudoregs.  However, it is
11732    safe to create invalid memory addresses, because combine will
11733    try to recognize them and all they will do is make the combine
11734    attempt fail.
11735
11736    If for some reason this cannot do its job, an rtx
11737    (clobber (const_int 0)) is returned.
11738    An insn containing that will not be recognized.  */
11739
11740 static rtx
11741 gen_lowpart_for_combine (machine_mode omode, rtx x)
11742 {
11743   machine_mode imode = GET_MODE (x);
11744   rtx result;
11745
11746   if (omode == imode)
11747     return x;
11748
11749   /* We can only support MODE being wider than a word if X is a
11750      constant integer or has a mode the same size.  */
11751   if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11752       && ! (CONST_SCALAR_INT_P (x)
11753             || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11754     goto fail;
11755
11756   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
11757      won't know what to do.  So we will strip off the SUBREG here and
11758      process normally.  */
11759   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11760     {
11761       x = SUBREG_REG (x);
11762
11763       /* For use in case we fall down into the address adjustments
11764          further below, we need to adjust the known mode and size of
11765          x; imode and isize, since we just adjusted x.  */
11766       imode = GET_MODE (x);
11767
11768       if (imode == omode)
11769         return x;
11770     }
11771
11772   result = gen_lowpart_common (omode, x);
11773
11774   if (result)
11775     return result;
11776
11777   if (MEM_P (x))
11778     {
11779       /* Refuse to work on a volatile memory ref or one with a mode-dependent
11780          address.  */
11781       if (MEM_VOLATILE_P (x)
11782           || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11783         goto fail;
11784
11785       /* If we want to refer to something bigger than the original memref,
11786          generate a paradoxical subreg instead.  That will force a reload
11787          of the original memref X.  */
11788       if (paradoxical_subreg_p (omode, imode))
11789         return gen_rtx_SUBREG (omode, x, 0);
11790
11791       poly_int64 offset = byte_lowpart_offset (omode, imode);
11792       return adjust_address_nv (x, omode, offset);
11793     }
11794
11795   /* If X is a comparison operator, rewrite it in a new mode.  This
11796      probably won't match, but may allow further simplifications.  */
11797   else if (COMPARISON_P (x))
11798     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11799
11800   /* If we couldn't simplify X any other way, just enclose it in a
11801      SUBREG.  Normally, this SUBREG won't match, but some patterns may
11802      include an explicit SUBREG or we may simplify it further in combine.  */
11803   else
11804     {
11805       rtx res;
11806
11807       if (imode == VOIDmode)
11808         {
11809           imode = int_mode_for_mode (omode).require ();
11810           x = gen_lowpart_common (imode, x);
11811           if (x == NULL)
11812             goto fail;
11813         }
11814       res = lowpart_subreg (omode, x, imode);
11815       if (res)
11816         return res;
11817     }
11818
11819  fail:
11820   return gen_rtx_CLOBBER (omode, const0_rtx);
11821 }
11822 \f
11823 /* Try to simplify a comparison between OP0 and a constant OP1,
11824    where CODE is the comparison code that will be tested, into a
11825    (CODE OP0 const0_rtx) form.
11826
11827    The result is a possibly different comparison code to use.
11828    *POP1 may be updated.  */
11829
11830 static enum rtx_code
11831 simplify_compare_const (enum rtx_code code, machine_mode mode,
11832                         rtx op0, rtx *pop1)
11833 {
11834   scalar_int_mode int_mode;
11835   HOST_WIDE_INT const_op = INTVAL (*pop1);
11836
11837   /* Get the constant we are comparing against and turn off all bits
11838      not on in our mode.  */
11839   if (mode != VOIDmode)
11840     const_op = trunc_int_for_mode (const_op, mode);
11841
11842   /* If we are comparing against a constant power of two and the value
11843      being compared can only have that single bit nonzero (e.g., it was
11844      `and'ed with that bit), we can replace this with a comparison
11845      with zero.  */
11846   if (const_op
11847       && (code == EQ || code == NE || code == GE || code == GEU
11848           || code == LT || code == LTU)
11849       && is_a <scalar_int_mode> (mode, &int_mode)
11850       && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11851       && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11852       && (nonzero_bits (op0, int_mode)
11853           == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11854     {
11855       code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11856       const_op = 0;
11857     }
11858
11859   /* Similarly, if we are comparing a value known to be either -1 or
11860      0 with -1, change it to the opposite comparison against zero.  */
11861   if (const_op == -1
11862       && (code == EQ || code == NE || code == GT || code == LE
11863           || code == GEU || code == LTU)
11864       && is_a <scalar_int_mode> (mode, &int_mode)
11865       && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11866     {
11867       code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11868       const_op = 0;
11869     }
11870
11871   /* Do some canonicalizations based on the comparison code.  We prefer
11872      comparisons against zero and then prefer equality comparisons.
11873      If we can reduce the size of a constant, we will do that too.  */
11874   switch (code)
11875     {
11876     case LT:
11877       /* < C is equivalent to <= (C - 1) */
11878       if (const_op > 0)
11879         {
11880           const_op -= 1;
11881           code = LE;
11882           /* ... fall through to LE case below.  */
11883           gcc_fallthrough ();
11884         }
11885       else
11886         break;
11887
11888     case LE:
11889       /* <= C is equivalent to < (C + 1); we do this for C < 0  */
11890       if (const_op < 0)
11891         {
11892           const_op += 1;
11893           code = LT;
11894         }
11895
11896       /* If we are doing a <= 0 comparison on a value known to have
11897          a zero sign bit, we can replace this with == 0.  */
11898       else if (const_op == 0
11899                && is_a <scalar_int_mode> (mode, &int_mode)
11900                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11901                && (nonzero_bits (op0, int_mode)
11902                    & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11903                == 0)
11904         code = EQ;
11905       break;
11906
11907     case GE:
11908       /* >= C is equivalent to > (C - 1).  */
11909       if (const_op > 0)
11910         {
11911           const_op -= 1;
11912           code = GT;
11913           /* ... fall through to GT below.  */
11914           gcc_fallthrough ();
11915         }
11916       else
11917         break;
11918
11919     case GT:
11920       /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
11921       if (const_op < 0)
11922         {
11923           const_op += 1;
11924           code = GE;
11925         }
11926
11927       /* If we are doing a > 0 comparison on a value known to have
11928          a zero sign bit, we can replace this with != 0.  */
11929       else if (const_op == 0
11930                && is_a <scalar_int_mode> (mode, &int_mode)
11931                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11932                && (nonzero_bits (op0, int_mode)
11933                    & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11934                == 0)
11935         code = NE;
11936       break;
11937
11938     case LTU:
11939       /* < C is equivalent to <= (C - 1).  */
11940       if (const_op > 0)
11941         {
11942           const_op -= 1;
11943           code = LEU;
11944           /* ... fall through ...  */
11945           gcc_fallthrough ();
11946         }
11947       /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
11948       else if (is_a <scalar_int_mode> (mode, &int_mode)
11949                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11950                && ((unsigned HOST_WIDE_INT) const_op
11951                    == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11952         {
11953           const_op = 0;
11954           code = GE;
11955           break;
11956         }
11957       else
11958         break;
11959
11960     case LEU:
11961       /* unsigned <= 0 is equivalent to == 0 */
11962       if (const_op == 0)
11963         code = EQ;
11964       /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
11965       else if (is_a <scalar_int_mode> (mode, &int_mode)
11966                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11967                && ((unsigned HOST_WIDE_INT) const_op
11968                    == ((HOST_WIDE_INT_1U
11969                         << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11970         {
11971           const_op = 0;
11972           code = GE;
11973         }
11974       break;
11975
11976     case GEU:
11977       /* >= C is equivalent to > (C - 1).  */
11978       if (const_op > 1)
11979         {
11980           const_op -= 1;
11981           code = GTU;
11982           /* ... fall through ...  */
11983           gcc_fallthrough ();
11984         }
11985
11986       /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
11987       else if (is_a <scalar_int_mode> (mode, &int_mode)
11988                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11989                && ((unsigned HOST_WIDE_INT) const_op
11990                    == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11991         {
11992           const_op = 0;
11993           code = LT;
11994           break;
11995         }
11996       else
11997         break;
11998
11999     case GTU:
12000       /* unsigned > 0 is equivalent to != 0 */
12001       if (const_op == 0)
12002         code = NE;
12003       /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
12004       else if (is_a <scalar_int_mode> (mode, &int_mode)
12005                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
12006                && ((unsigned HOST_WIDE_INT) const_op
12007                    == (HOST_WIDE_INT_1U
12008                        << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
12009         {
12010           const_op = 0;
12011           code = LT;
12012         }
12013       break;
12014
12015     default:
12016       break;
12017     }
12018
12019   *pop1 = GEN_INT (const_op);
12020   return code;
12021 }
12022 \f
12023 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
12024    comparison code that will be tested.
12025
12026    The result is a possibly different comparison code to use.  *POP0 and
12027    *POP1 may be updated.
12028
12029    It is possible that we might detect that a comparison is either always
12030    true or always false.  However, we do not perform general constant
12031    folding in combine, so this knowledge isn't useful.  Such tautologies
12032    should have been detected earlier.  Hence we ignore all such cases.  */
12033
12034 static enum rtx_code
12035 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
12036 {
12037   rtx op0 = *pop0;
12038   rtx op1 = *pop1;
12039   rtx tem, tem1;
12040   int i;
12041   scalar_int_mode mode, inner_mode, tmode;
12042   opt_scalar_int_mode tmode_iter;
12043
12044   /* Try a few ways of applying the same transformation to both operands.  */
12045   while (1)
12046     {
12047       /* The test below this one won't handle SIGN_EXTENDs on these machines,
12048          so check specially.  */
12049       if (!WORD_REGISTER_OPERATIONS
12050           && code != GTU && code != GEU && code != LTU && code != LEU
12051           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
12052           && GET_CODE (XEXP (op0, 0)) == ASHIFT
12053           && GET_CODE (XEXP (op1, 0)) == ASHIFT
12054           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
12055           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
12056           && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
12057           && (is_a <scalar_int_mode>
12058               (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
12059           && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
12060           && CONST_INT_P (XEXP (op0, 1))
12061           && XEXP (op0, 1) == XEXP (op1, 1)
12062           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12063           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
12064           && (INTVAL (XEXP (op0, 1))
12065               == (GET_MODE_PRECISION (mode)
12066                   - GET_MODE_PRECISION (inner_mode))))
12067         {
12068           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
12069           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
12070         }
12071
12072       /* If both operands are the same constant shift, see if we can ignore the
12073          shift.  We can if the shift is a rotate or if the bits shifted out of
12074          this shift are known to be zero for both inputs and if the type of
12075          comparison is compatible with the shift.  */
12076       if (GET_CODE (op0) == GET_CODE (op1)
12077           && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
12078           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
12079               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
12080                   && (code != GT && code != LT && code != GE && code != LE))
12081               || (GET_CODE (op0) == ASHIFTRT
12082                   && (code != GTU && code != LTU
12083                       && code != GEU && code != LEU)))
12084           && CONST_INT_P (XEXP (op0, 1))
12085           && INTVAL (XEXP (op0, 1)) >= 0
12086           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12087           && XEXP (op0, 1) == XEXP (op1, 1))
12088         {
12089           machine_mode mode = GET_MODE (op0);
12090           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12091           int shift_count = INTVAL (XEXP (op0, 1));
12092
12093           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
12094             mask &= (mask >> shift_count) << shift_count;
12095           else if (GET_CODE (op0) == ASHIFT)
12096             mask = (mask & (mask << shift_count)) >> shift_count;
12097
12098           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12099               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12100             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12101           else
12102             break;
12103         }
12104
12105       /* If both operands are AND's of a paradoxical SUBREG by constant, the
12106          SUBREGs are of the same mode, and, in both cases, the AND would
12107          be redundant if the comparison was done in the narrower mode,
12108          do the comparison in the narrower mode (e.g., we are AND'ing with 1
12109          and the operand's possibly nonzero bits are 0xffffff01; in that case
12110          if we only care about QImode, we don't need the AND).  This case
12111          occurs if the output mode of an scc insn is not SImode and
12112          STORE_FLAG_VALUE == 1 (e.g., the 386).
12113
12114          Similarly, check for a case where the AND's are ZERO_EXTEND
12115          operations from some narrower mode even though a SUBREG is not
12116          present.  */
12117
12118       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12119                && CONST_INT_P (XEXP (op0, 1))
12120                && CONST_INT_P (XEXP (op1, 1)))
12121         {
12122           rtx inner_op0 = XEXP (op0, 0);
12123           rtx inner_op1 = XEXP (op1, 0);
12124           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12125           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12126           int changed = 0;
12127
12128           if (paradoxical_subreg_p (inner_op0)
12129               && GET_CODE (inner_op1) == SUBREG
12130               && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12131               && (GET_MODE (SUBREG_REG (inner_op0))
12132                   == GET_MODE (SUBREG_REG (inner_op1)))
12133               && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12134                                         GET_MODE (SUBREG_REG (inner_op0)))) == 0
12135               && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12136                                         GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12137             {
12138               op0 = SUBREG_REG (inner_op0);
12139               op1 = SUBREG_REG (inner_op1);
12140
12141               /* The resulting comparison is always unsigned since we masked
12142                  off the original sign bit.  */
12143               code = unsigned_condition (code);
12144
12145               changed = 1;
12146             }
12147
12148           else if (c0 == c1)
12149             FOR_EACH_MODE_UNTIL (tmode,
12150                                  as_a <scalar_int_mode> (GET_MODE (op0)))
12151               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12152                 {
12153                   op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12154                   op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12155                   code = unsigned_condition (code);
12156                   changed = 1;
12157                   break;
12158                 }
12159
12160           if (! changed)
12161             break;
12162         }
12163
12164       /* If both operands are NOT, we can strip off the outer operation
12165          and adjust the comparison code for swapped operands; similarly for
12166          NEG, except that this must be an equality comparison.  */
12167       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12168                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12169                    && (code == EQ || code == NE)))
12170         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12171
12172       else
12173         break;
12174     }
12175
12176   /* If the first operand is a constant, swap the operands and adjust the
12177      comparison code appropriately, but don't do this if the second operand
12178      is already a constant integer.  */
12179   if (swap_commutative_operands_p (op0, op1))
12180     {
12181       std::swap (op0, op1);
12182       code = swap_condition (code);
12183     }
12184
12185   /* We now enter a loop during which we will try to simplify the comparison.
12186      For the most part, we only are concerned with comparisons with zero,
12187      but some things may really be comparisons with zero but not start
12188      out looking that way.  */
12189
12190   while (CONST_INT_P (op1))
12191     {
12192       machine_mode raw_mode = GET_MODE (op0);
12193       scalar_int_mode int_mode;
12194       int equality_comparison_p;
12195       int sign_bit_comparison_p;
12196       int unsigned_comparison_p;
12197       HOST_WIDE_INT const_op;
12198
12199       /* We only want to handle integral modes.  This catches VOIDmode,
12200          CCmode, and the floating-point modes.  An exception is that we
12201          can handle VOIDmode if OP0 is a COMPARE or a comparison
12202          operation.  */
12203
12204       if (GET_MODE_CLASS (raw_mode) != MODE_INT
12205           && ! (raw_mode == VOIDmode
12206                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12207         break;
12208
12209       /* Try to simplify the compare to constant, possibly changing the
12210          comparison op, and/or changing op1 to zero.  */
12211       code = simplify_compare_const (code, raw_mode, op0, &op1);
12212       const_op = INTVAL (op1);
12213
12214       /* Compute some predicates to simplify code below.  */
12215
12216       equality_comparison_p = (code == EQ || code == NE);
12217       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12218       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12219                                || code == GEU);
12220
12221       /* If this is a sign bit comparison and we can do arithmetic in
12222          MODE, say that we will only be needing the sign bit of OP0.  */
12223       if (sign_bit_comparison_p
12224           && is_a <scalar_int_mode> (raw_mode, &int_mode)
12225           && HWI_COMPUTABLE_MODE_P (int_mode))
12226         op0 = force_to_mode (op0, int_mode,
12227                              HOST_WIDE_INT_1U
12228                              << (GET_MODE_PRECISION (int_mode) - 1),
12229                              0);
12230
12231       if (COMPARISON_P (op0))
12232         {
12233           /* We can't do anything if OP0 is a condition code value, rather
12234              than an actual data value.  */
12235           if (const_op != 0
12236               || CC0_P (XEXP (op0, 0))
12237               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12238             break;
12239
12240           /* Get the two operands being compared.  */
12241           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12242             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12243           else
12244             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12245
12246           /* Check for the cases where we simply want the result of the
12247              earlier test or the opposite of that result.  */
12248           if (code == NE || code == EQ
12249               || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12250                   && (code == LT || code == GE)))
12251             {
12252               enum rtx_code new_code;
12253               if (code == LT || code == NE)
12254                 new_code = GET_CODE (op0);
12255               else
12256                 new_code = reversed_comparison_code (op0, NULL);
12257
12258               if (new_code != UNKNOWN)
12259                 {
12260                   code = new_code;
12261                   op0 = tem;
12262                   op1 = tem1;
12263                   continue;
12264                 }
12265             }
12266           break;
12267         }
12268
12269       if (raw_mode == VOIDmode)
12270         break;
12271       scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12272
12273       /* Now try cases based on the opcode of OP0.  If none of the cases
12274          does a "continue", we exit this loop immediately after the
12275          switch.  */
12276
12277       unsigned int mode_width = GET_MODE_PRECISION (mode);
12278       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12279       switch (GET_CODE (op0))
12280         {
12281         case ZERO_EXTRACT:
12282           /* If we are extracting a single bit from a variable position in
12283              a constant that has only a single bit set and are comparing it
12284              with zero, we can convert this into an equality comparison
12285              between the position and the location of the single bit.  */
12286           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12287              have already reduced the shift count modulo the word size.  */
12288           if (!SHIFT_COUNT_TRUNCATED
12289               && CONST_INT_P (XEXP (op0, 0))
12290               && XEXP (op0, 1) == const1_rtx
12291               && equality_comparison_p && const_op == 0
12292               && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12293             {
12294               if (BITS_BIG_ENDIAN)
12295                 i = BITS_PER_WORD - 1 - i;
12296
12297               op0 = XEXP (op0, 2);
12298               op1 = GEN_INT (i);
12299               const_op = i;
12300
12301               /* Result is nonzero iff shift count is equal to I.  */
12302               code = reverse_condition (code);
12303               continue;
12304             }
12305
12306           /* fall through */
12307
12308         case SIGN_EXTRACT:
12309           tem = expand_compound_operation (op0);
12310           if (tem != op0)
12311             {
12312               op0 = tem;
12313               continue;
12314             }
12315           break;
12316
12317         case NOT:
12318           /* If testing for equality, we can take the NOT of the constant.  */
12319           if (equality_comparison_p
12320               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12321             {
12322               op0 = XEXP (op0, 0);
12323               op1 = tem;
12324               continue;
12325             }
12326
12327           /* If just looking at the sign bit, reverse the sense of the
12328              comparison.  */
12329           if (sign_bit_comparison_p)
12330             {
12331               op0 = XEXP (op0, 0);
12332               code = (code == GE ? LT : GE);
12333               continue;
12334             }
12335           break;
12336
12337         case NEG:
12338           /* If testing for equality, we can take the NEG of the constant.  */
12339           if (equality_comparison_p
12340               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12341             {
12342               op0 = XEXP (op0, 0);
12343               op1 = tem;
12344               continue;
12345             }
12346
12347           /* The remaining cases only apply to comparisons with zero.  */
12348           if (const_op != 0)
12349             break;
12350
12351           /* When X is ABS or is known positive,
12352              (neg X) is < 0 if and only if X != 0.  */
12353
12354           if (sign_bit_comparison_p
12355               && (GET_CODE (XEXP (op0, 0)) == ABS
12356                   || (mode_width <= HOST_BITS_PER_WIDE_INT
12357                       && (nonzero_bits (XEXP (op0, 0), mode)
12358                           & (HOST_WIDE_INT_1U << (mode_width - 1)))
12359                          == 0)))
12360             {
12361               op0 = XEXP (op0, 0);
12362               code = (code == LT ? NE : EQ);
12363               continue;
12364             }
12365
12366           /* If we have NEG of something whose two high-order bits are the
12367              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
12368           if (num_sign_bit_copies (op0, mode) >= 2)
12369             {
12370               op0 = XEXP (op0, 0);
12371               code = swap_condition (code);
12372               continue;
12373             }
12374           break;
12375
12376         case ROTATE:
12377           /* If we are testing equality and our count is a constant, we
12378              can perform the inverse operation on our RHS.  */
12379           if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12380               && (tem = simplify_binary_operation (ROTATERT, mode,
12381                                                    op1, XEXP (op0, 1))) != 0)
12382             {
12383               op0 = XEXP (op0, 0);
12384               op1 = tem;
12385               continue;
12386             }
12387
12388           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12389              a particular bit.  Convert it to an AND of a constant of that
12390              bit.  This will be converted into a ZERO_EXTRACT.  */
12391           if (const_op == 0 && sign_bit_comparison_p
12392               && CONST_INT_P (XEXP (op0, 1))
12393               && mode_width <= HOST_BITS_PER_WIDE_INT)
12394             {
12395               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12396                                             (HOST_WIDE_INT_1U
12397                                              << (mode_width - 1
12398                                                  - INTVAL (XEXP (op0, 1)))));
12399               code = (code == LT ? NE : EQ);
12400               continue;
12401             }
12402
12403           /* Fall through.  */
12404
12405         case ABS:
12406           /* ABS is ignorable inside an equality comparison with zero.  */
12407           if (const_op == 0 && equality_comparison_p)
12408             {
12409               op0 = XEXP (op0, 0);
12410               continue;
12411             }
12412           break;
12413
12414         case SIGN_EXTEND:
12415           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12416              (compare FOO CONST) if CONST fits in FOO's mode and we
12417              are either testing inequality or have an unsigned
12418              comparison with ZERO_EXTEND or a signed comparison with
12419              SIGN_EXTEND.  But don't do it if we don't have a compare
12420              insn of the given mode, since we'd have to revert it
12421              later on, and then we wouldn't know whether to sign- or
12422              zero-extend.  */
12423           if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12424               && ! unsigned_comparison_p
12425               && HWI_COMPUTABLE_MODE_P (mode)
12426               && trunc_int_for_mode (const_op, mode) == const_op
12427               && have_insn_for (COMPARE, mode))
12428             {
12429               op0 = XEXP (op0, 0);
12430               continue;
12431             }
12432           break;
12433
12434         case SUBREG:
12435           /* Check for the case where we are comparing A - C1 with C2, that is
12436
12437                (subreg:MODE (plus (A) (-C1))) op (C2)
12438
12439              with C1 a constant, and try to lift the SUBREG, i.e. to do the
12440              comparison in the wider mode.  One of the following two conditions
12441              must be true in order for this to be valid:
12442
12443                1. The mode extension results in the same bit pattern being added
12444                   on both sides and the comparison is equality or unsigned.  As
12445                   C2 has been truncated to fit in MODE, the pattern can only be
12446                   all 0s or all 1s.
12447
12448                2. The mode extension results in the sign bit being copied on
12449                   each side.
12450
12451              The difficulty here is that we have predicates for A but not for
12452              (A - C1) so we need to check that C1 is within proper bounds so
12453              as to perturbate A as little as possible.  */
12454
12455           if (mode_width <= HOST_BITS_PER_WIDE_INT
12456               && subreg_lowpart_p (op0)
12457               && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12458                                          &inner_mode)
12459               && GET_MODE_PRECISION (inner_mode) > mode_width
12460               && GET_CODE (SUBREG_REG (op0)) == PLUS
12461               && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12462             {
12463               rtx a = XEXP (SUBREG_REG (op0), 0);
12464               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12465
12466               if ((c1 > 0
12467                    && (unsigned HOST_WIDE_INT) c1
12468                        < HOST_WIDE_INT_1U << (mode_width - 1)
12469                    && (equality_comparison_p || unsigned_comparison_p)
12470                    /* (A - C1) zero-extends if it is positive and sign-extends
12471                       if it is negative, C2 both zero- and sign-extends.  */
12472                    && (((nonzero_bits (a, inner_mode)
12473                          & ~GET_MODE_MASK (mode)) == 0
12474                         && const_op >= 0)
12475                        /* (A - C1) sign-extends if it is positive and 1-extends
12476                           if it is negative, C2 both sign- and 1-extends.  */
12477                        || (num_sign_bit_copies (a, inner_mode)
12478                            > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12479                                              - mode_width)
12480                            && const_op < 0)))
12481                   || ((unsigned HOST_WIDE_INT) c1
12482                        < HOST_WIDE_INT_1U << (mode_width - 2)
12483                       /* (A - C1) always sign-extends, like C2.  */
12484                       && num_sign_bit_copies (a, inner_mode)
12485                          > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12486                                            - (mode_width - 1))))
12487                 {
12488                   op0 = SUBREG_REG (op0);
12489                   continue;
12490                 }
12491             }
12492
12493           /* If the inner mode is narrower and we are extracting the low part,
12494              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
12495           if (paradoxical_subreg_p (op0))
12496             ;
12497           else if (subreg_lowpart_p (op0)
12498                    && GET_MODE_CLASS (mode) == MODE_INT
12499                    && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12500                    && (code == NE || code == EQ)
12501                    && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12502                    && !paradoxical_subreg_p (op0)
12503                    && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12504                        & ~GET_MODE_MASK (mode)) == 0)
12505             {
12506               /* Remove outer subregs that don't do anything.  */
12507               tem = gen_lowpart (inner_mode, op1);
12508
12509               if ((nonzero_bits (tem, inner_mode)
12510                    & ~GET_MODE_MASK (mode)) == 0)
12511                 {
12512                   op0 = SUBREG_REG (op0);
12513                   op1 = tem;
12514                   continue;
12515                 }
12516               break;
12517             }
12518           else
12519             break;
12520
12521           /* FALLTHROUGH */
12522
12523         case ZERO_EXTEND:
12524           if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12525               && (unsigned_comparison_p || equality_comparison_p)
12526               && HWI_COMPUTABLE_MODE_P (mode)
12527               && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12528               && const_op >= 0
12529               && have_insn_for (COMPARE, mode))
12530             {
12531               op0 = XEXP (op0, 0);
12532               continue;
12533             }
12534           break;
12535
12536         case PLUS:
12537           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
12538              this for equality comparisons due to pathological cases involving
12539              overflows.  */
12540           if (equality_comparison_p
12541               && (tem = simplify_binary_operation (MINUS, mode,
12542                                                    op1, XEXP (op0, 1))) != 0)
12543             {
12544               op0 = XEXP (op0, 0);
12545               op1 = tem;
12546               continue;
12547             }
12548
12549           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
12550           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12551               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12552             {
12553               op0 = XEXP (XEXP (op0, 0), 0);
12554               code = (code == LT ? EQ : NE);
12555               continue;
12556             }
12557           break;
12558
12559         case MINUS:
12560           /* We used to optimize signed comparisons against zero, but that
12561              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
12562              arrive here as equality comparisons, or (GEU, LTU) are
12563              optimized away.  No need to special-case them.  */
12564
12565           /* (eq (minus A B) C) -> (eq A (plus B C)) or
12566              (eq B (minus A C)), whichever simplifies.  We can only do
12567              this for equality comparisons due to pathological cases involving
12568              overflows.  */
12569           if (equality_comparison_p
12570               && (tem = simplify_binary_operation (PLUS, mode,
12571                                                    XEXP (op0, 1), op1)) != 0)
12572             {
12573               op0 = XEXP (op0, 0);
12574               op1 = tem;
12575               continue;
12576             }
12577
12578           if (equality_comparison_p
12579               && (tem = simplify_binary_operation (MINUS, mode,
12580                                                    XEXP (op0, 0), op1)) != 0)
12581             {
12582               op0 = XEXP (op0, 1);
12583               op1 = tem;
12584               continue;
12585             }
12586
12587           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12588              of bits in X minus 1, is one iff X > 0.  */
12589           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12590               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12591               && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12592               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12593             {
12594               op0 = XEXP (op0, 1);
12595               code = (code == GE ? LE : GT);
12596               continue;
12597             }
12598           break;
12599
12600         case XOR:
12601           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
12602              if C is zero or B is a constant.  */
12603           if (equality_comparison_p
12604               && (tem = simplify_binary_operation (XOR, mode,
12605                                                    XEXP (op0, 1), op1)) != 0)
12606             {
12607               op0 = XEXP (op0, 0);
12608               op1 = tem;
12609               continue;
12610             }
12611           break;
12612
12613
12614         case IOR:
12615           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12616              iff X <= 0.  */
12617           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12618               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12619               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12620             {
12621               op0 = XEXP (op0, 1);
12622               code = (code == GE ? GT : LE);
12623               continue;
12624             }
12625           break;
12626
12627         case AND:
12628           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
12629              will be converted to a ZERO_EXTRACT later.  */
12630           if (const_op == 0 && equality_comparison_p
12631               && GET_CODE (XEXP (op0, 0)) == ASHIFT
12632               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12633             {
12634               op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12635                                       XEXP (XEXP (op0, 0), 1));
12636               op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12637               continue;
12638             }
12639
12640           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12641              zero and X is a comparison and C1 and C2 describe only bits set
12642              in STORE_FLAG_VALUE, we can compare with X.  */
12643           if (const_op == 0 && equality_comparison_p
12644               && mode_width <= HOST_BITS_PER_WIDE_INT
12645               && CONST_INT_P (XEXP (op0, 1))
12646               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12647               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12648               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12649               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12650             {
12651               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12652                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
12653               if ((~STORE_FLAG_VALUE & mask) == 0
12654                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12655                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12656                           && COMPARISON_P (tem))))
12657                 {
12658                   op0 = XEXP (XEXP (op0, 0), 0);
12659                   continue;
12660                 }
12661             }
12662
12663           /* If we are doing an equality comparison of an AND of a bit equal
12664              to the sign bit, replace this with a LT or GE comparison of
12665              the underlying value.  */
12666           if (equality_comparison_p
12667               && const_op == 0
12668               && CONST_INT_P (XEXP (op0, 1))
12669               && mode_width <= HOST_BITS_PER_WIDE_INT
12670               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12671                   == HOST_WIDE_INT_1U << (mode_width - 1)))
12672             {
12673               op0 = XEXP (op0, 0);
12674               code = (code == EQ ? GE : LT);
12675               continue;
12676             }
12677
12678           /* If this AND operation is really a ZERO_EXTEND from a narrower
12679              mode, the constant fits within that mode, and this is either an
12680              equality or unsigned comparison, try to do this comparison in
12681              the narrower mode.
12682
12683              Note that in:
12684
12685              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12686              -> (ne:DI (reg:SI 4) (const_int 0))
12687
12688              unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12689              known to hold a value of the required mode the
12690              transformation is invalid.  */
12691           if ((equality_comparison_p || unsigned_comparison_p)
12692               && CONST_INT_P (XEXP (op0, 1))
12693               && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12694                                    & GET_MODE_MASK (mode))
12695                                   + 1)) >= 0
12696               && const_op >> i == 0
12697               && int_mode_for_size (i, 1).exists (&tmode))
12698             {
12699               op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12700               continue;
12701             }
12702
12703           /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12704              fits in both M1 and M2 and the SUBREG is either paradoxical
12705              or represents the low part, permute the SUBREG and the AND
12706              and try again.  */
12707           if (GET_CODE (XEXP (op0, 0)) == SUBREG
12708               && CONST_INT_P (XEXP (op0, 1)))
12709             {
12710               unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12711               /* Require an integral mode, to avoid creating something like
12712                  (AND:SF ...).  */
12713               if ((is_a <scalar_int_mode>
12714                    (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12715                   /* It is unsafe to commute the AND into the SUBREG if the
12716                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12717                      not defined.  As originally written the upper bits
12718                      have a defined value due to the AND operation.
12719                      However, if we commute the AND inside the SUBREG then
12720                      they no longer have defined values and the meaning of
12721                      the code has been changed.
12722                      Also C1 should not change value in the smaller mode,
12723                      see PR67028 (a positive C1 can become negative in the
12724                      smaller mode, so that the AND does no longer mask the
12725                      upper bits).  */
12726                   && ((WORD_REGISTER_OPERATIONS
12727                        && mode_width > GET_MODE_PRECISION (tmode)
12728                        && mode_width <= BITS_PER_WORD
12729                        && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12730                       || (mode_width <= GET_MODE_PRECISION (tmode)
12731                           && subreg_lowpart_p (XEXP (op0, 0))))
12732                   && mode_width <= HOST_BITS_PER_WIDE_INT
12733                   && HWI_COMPUTABLE_MODE_P (tmode)
12734                   && (c1 & ~mask) == 0
12735                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
12736                   && c1 != mask
12737                   && c1 != GET_MODE_MASK (tmode))
12738                 {
12739                   op0 = simplify_gen_binary (AND, tmode,
12740                                              SUBREG_REG (XEXP (op0, 0)),
12741                                              gen_int_mode (c1, tmode));
12742                   op0 = gen_lowpart (mode, op0);
12743                   continue;
12744                 }
12745             }
12746
12747           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
12748           if (const_op == 0 && equality_comparison_p
12749               && XEXP (op0, 1) == const1_rtx
12750               && GET_CODE (XEXP (op0, 0)) == NOT)
12751             {
12752               op0 = simplify_and_const_int (NULL_RTX, mode,
12753                                             XEXP (XEXP (op0, 0), 0), 1);
12754               code = (code == NE ? EQ : NE);
12755               continue;
12756             }
12757
12758           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12759              (eq (and (lshiftrt X) 1) 0).
12760              Also handle the case where (not X) is expressed using xor.  */
12761           if (const_op == 0 && equality_comparison_p
12762               && XEXP (op0, 1) == const1_rtx
12763               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12764             {
12765               rtx shift_op = XEXP (XEXP (op0, 0), 0);
12766               rtx shift_count = XEXP (XEXP (op0, 0), 1);
12767
12768               if (GET_CODE (shift_op) == NOT
12769                   || (GET_CODE (shift_op) == XOR
12770                       && CONST_INT_P (XEXP (shift_op, 1))
12771                       && CONST_INT_P (shift_count)
12772                       && HWI_COMPUTABLE_MODE_P (mode)
12773                       && (UINTVAL (XEXP (shift_op, 1))
12774                           == HOST_WIDE_INT_1U
12775                                << INTVAL (shift_count))))
12776                 {
12777                   op0
12778                     = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12779                   op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12780                   code = (code == NE ? EQ : NE);
12781                   continue;
12782                 }
12783             }
12784           break;
12785
12786         case ASHIFT:
12787           /* If we have (compare (ashift FOO N) (const_int C)) and
12788              the high order N bits of FOO (N+1 if an inequality comparison)
12789              are known to be zero, we can do this by comparing FOO with C
12790              shifted right N bits so long as the low-order N bits of C are
12791              zero.  */
12792           if (CONST_INT_P (XEXP (op0, 1))
12793               && INTVAL (XEXP (op0, 1)) >= 0
12794               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12795                   < HOST_BITS_PER_WIDE_INT)
12796               && (((unsigned HOST_WIDE_INT) const_op
12797                    & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12798                       - 1)) == 0)
12799               && mode_width <= HOST_BITS_PER_WIDE_INT
12800               && (nonzero_bits (XEXP (op0, 0), mode)
12801                   & ~(mask >> (INTVAL (XEXP (op0, 1))
12802                                + ! equality_comparison_p))) == 0)
12803             {
12804               /* We must perform a logical shift, not an arithmetic one,
12805                  as we want the top N bits of C to be zero.  */
12806               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12807
12808               temp >>= INTVAL (XEXP (op0, 1));
12809               op1 = gen_int_mode (temp, mode);
12810               op0 = XEXP (op0, 0);
12811               continue;
12812             }
12813
12814           /* If we are doing a sign bit comparison, it means we are testing
12815              a particular bit.  Convert it to the appropriate AND.  */
12816           if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12817               && mode_width <= HOST_BITS_PER_WIDE_INT)
12818             {
12819               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12820                                             (HOST_WIDE_INT_1U
12821                                              << (mode_width - 1
12822                                                  - INTVAL (XEXP (op0, 1)))));
12823               code = (code == LT ? NE : EQ);
12824               continue;
12825             }
12826
12827           /* If this an equality comparison with zero and we are shifting
12828              the low bit to the sign bit, we can convert this to an AND of the
12829              low-order bit.  */
12830           if (const_op == 0 && equality_comparison_p
12831               && CONST_INT_P (XEXP (op0, 1))
12832               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12833             {
12834               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12835               continue;
12836             }
12837           break;
12838
12839         case ASHIFTRT:
12840           /* If this is an equality comparison with zero, we can do this
12841              as a logical shift, which might be much simpler.  */
12842           if (equality_comparison_p && const_op == 0
12843               && CONST_INT_P (XEXP (op0, 1)))
12844             {
12845               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12846                                           XEXP (op0, 0),
12847                                           INTVAL (XEXP (op0, 1)));
12848               continue;
12849             }
12850
12851           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12852              do the comparison in a narrower mode.  */
12853           if (! unsigned_comparison_p
12854               && CONST_INT_P (XEXP (op0, 1))
12855               && GET_CODE (XEXP (op0, 0)) == ASHIFT
12856               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12857               && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12858                   .exists (&tmode))
12859               && (((unsigned HOST_WIDE_INT) const_op
12860                    + (GET_MODE_MASK (tmode) >> 1) + 1)
12861                   <= GET_MODE_MASK (tmode)))
12862             {
12863               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12864               continue;
12865             }
12866
12867           /* Likewise if OP0 is a PLUS of a sign extension with a
12868              constant, which is usually represented with the PLUS
12869              between the shifts.  */
12870           if (! unsigned_comparison_p
12871               && CONST_INT_P (XEXP (op0, 1))
12872               && GET_CODE (XEXP (op0, 0)) == PLUS
12873               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12874               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12875               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12876               && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12877                   .exists (&tmode))
12878               && (((unsigned HOST_WIDE_INT) const_op
12879                    + (GET_MODE_MASK (tmode) >> 1) + 1)
12880                   <= GET_MODE_MASK (tmode)))
12881             {
12882               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12883               rtx add_const = XEXP (XEXP (op0, 0), 1);
12884               rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12885                                                    add_const, XEXP (op0, 1));
12886
12887               op0 = simplify_gen_binary (PLUS, tmode,
12888                                          gen_lowpart (tmode, inner),
12889                                          new_const);
12890               continue;
12891             }
12892
12893           /* FALLTHROUGH */
12894         case LSHIFTRT:
12895           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12896              the low order N bits of FOO are known to be zero, we can do this
12897              by comparing FOO with C shifted left N bits so long as no
12898              overflow occurs.  Even if the low order N bits of FOO aren't known
12899              to be zero, if the comparison is >= or < we can use the same
12900              optimization and for > or <= by setting all the low
12901              order N bits in the comparison constant.  */
12902           if (CONST_INT_P (XEXP (op0, 1))
12903               && INTVAL (XEXP (op0, 1)) > 0
12904               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12905               && mode_width <= HOST_BITS_PER_WIDE_INT
12906               && (((unsigned HOST_WIDE_INT) const_op
12907                    + (GET_CODE (op0) != LSHIFTRT
12908                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12909                          + 1)
12910                       : 0))
12911                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12912             {
12913               unsigned HOST_WIDE_INT low_bits
12914                 = (nonzero_bits (XEXP (op0, 0), mode)
12915                    & ((HOST_WIDE_INT_1U
12916                        << INTVAL (XEXP (op0, 1))) - 1));
12917               if (low_bits == 0 || !equality_comparison_p)
12918                 {
12919                   /* If the shift was logical, then we must make the condition
12920                      unsigned.  */
12921                   if (GET_CODE (op0) == LSHIFTRT)
12922                     code = unsigned_condition (code);
12923
12924                   const_op = (unsigned HOST_WIDE_INT) const_op
12925                               << INTVAL (XEXP (op0, 1));
12926                   if (low_bits != 0
12927                       && (code == GT || code == GTU
12928                           || code == LE || code == LEU))
12929                     const_op
12930                       |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12931                   op1 = GEN_INT (const_op);
12932                   op0 = XEXP (op0, 0);
12933                   continue;
12934                 }
12935             }
12936
12937           /* If we are using this shift to extract just the sign bit, we
12938              can replace this with an LT or GE comparison.  */
12939           if (const_op == 0
12940               && (equality_comparison_p || sign_bit_comparison_p)
12941               && CONST_INT_P (XEXP (op0, 1))
12942               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12943             {
12944               op0 = XEXP (op0, 0);
12945               code = (code == NE || code == GT ? LT : GE);
12946               continue;
12947             }
12948           break;
12949
12950         default:
12951           break;
12952         }
12953
12954       break;
12955     }
12956
12957   /* Now make any compound operations involved in this comparison.  Then,
12958      check for an outmost SUBREG on OP0 that is not doing anything or is
12959      paradoxical.  The latter transformation must only be performed when
12960      it is known that the "extra" bits will be the same in op0 and op1 or
12961      that they don't matter.  There are three cases to consider:
12962
12963      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
12964      care bits and we can assume they have any convenient value.  So
12965      making the transformation is safe.
12966
12967      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12968      In this case the upper bits of op0 are undefined.  We should not make
12969      the simplification in that case as we do not know the contents of
12970      those bits.
12971
12972      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12973      In that case we know those bits are zeros or ones.  We must also be
12974      sure that they are the same as the upper bits of op1.
12975
12976      We can never remove a SUBREG for a non-equality comparison because
12977      the sign bit is in a different place in the underlying object.  */
12978
12979   rtx_code op0_mco_code = SET;
12980   if (op1 == const0_rtx)
12981     op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12982
12983   op0 = make_compound_operation (op0, op0_mco_code);
12984   op1 = make_compound_operation (op1, SET);
12985
12986   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12987       && is_int_mode (GET_MODE (op0), &mode)
12988       && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12989       && (code == NE || code == EQ))
12990     {
12991       if (paradoxical_subreg_p (op0))
12992         {
12993           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
12994              implemented.  */
12995           if (REG_P (SUBREG_REG (op0)))
12996             {
12997               op0 = SUBREG_REG (op0);
12998               op1 = gen_lowpart (inner_mode, op1);
12999             }
13000         }
13001       else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
13002                && (nonzero_bits (SUBREG_REG (op0), inner_mode)
13003                    & ~GET_MODE_MASK (mode)) == 0)
13004         {
13005           tem = gen_lowpart (inner_mode, op1);
13006
13007           if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
13008             op0 = SUBREG_REG (op0), op1 = tem;
13009         }
13010     }
13011
13012   /* We now do the opposite procedure: Some machines don't have compare
13013      insns in all modes.  If OP0's mode is an integer mode smaller than a
13014      word and we can't do a compare in that mode, see if there is a larger
13015      mode for which we can do the compare.  There are a number of cases in
13016      which we can use the wider mode.  */
13017
13018   if (is_int_mode (GET_MODE (op0), &mode)
13019       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
13020       && ! have_insn_for (COMPARE, mode))
13021     FOR_EACH_WIDER_MODE (tmode_iter, mode)
13022       {
13023         tmode = tmode_iter.require ();
13024         if (!HWI_COMPUTABLE_MODE_P (tmode))
13025           break;
13026         if (have_insn_for (COMPARE, tmode))
13027           {
13028             int zero_extended;
13029
13030             /* If this is a test for negative, we can make an explicit
13031                test of the sign bit.  Test this first so we can use
13032                a paradoxical subreg to extend OP0.  */
13033
13034             if (op1 == const0_rtx && (code == LT || code == GE)
13035                 && HWI_COMPUTABLE_MODE_P (mode))
13036               {
13037                 unsigned HOST_WIDE_INT sign
13038                   = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
13039                 op0 = simplify_gen_binary (AND, tmode,
13040                                            gen_lowpart (tmode, op0),
13041                                            gen_int_mode (sign, tmode));
13042                 code = (code == LT) ? NE : EQ;
13043                 break;
13044               }
13045
13046             /* If the only nonzero bits in OP0 and OP1 are those in the
13047                narrower mode and this is an equality or unsigned comparison,
13048                we can use the wider mode.  Similarly for sign-extended
13049                values, in which case it is true for all comparisons.  */
13050             zero_extended = ((code == EQ || code == NE
13051                               || code == GEU || code == GTU
13052                               || code == LEU || code == LTU)
13053                              && (nonzero_bits (op0, tmode)
13054                                  & ~GET_MODE_MASK (mode)) == 0
13055                              && ((CONST_INT_P (op1)
13056                                   || (nonzero_bits (op1, tmode)
13057                                       & ~GET_MODE_MASK (mode)) == 0)));
13058
13059             if (zero_extended
13060                 || ((num_sign_bit_copies (op0, tmode)
13061                      > (unsigned int) (GET_MODE_PRECISION (tmode)
13062                                        - GET_MODE_PRECISION (mode)))
13063                     && (num_sign_bit_copies (op1, tmode)
13064                         > (unsigned int) (GET_MODE_PRECISION (tmode)
13065                                           - GET_MODE_PRECISION (mode)))))
13066               {
13067                 /* If OP0 is an AND and we don't have an AND in MODE either,
13068                    make a new AND in the proper mode.  */
13069                 if (GET_CODE (op0) == AND
13070                     && !have_insn_for (AND, mode))
13071                   op0 = simplify_gen_binary (AND, tmode,
13072                                              gen_lowpart (tmode,
13073                                                           XEXP (op0, 0)),
13074                                              gen_lowpart (tmode,
13075                                                           XEXP (op0, 1)));
13076                 else
13077                   {
13078                     if (zero_extended)
13079                       {
13080                         op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
13081                                                   op0, mode);
13082                         op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
13083                                                   op1, mode);
13084                       }
13085                     else
13086                       {
13087                         op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
13088                                                   op0, mode);
13089                         op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
13090                                                   op1, mode);
13091                       }
13092                     break;
13093                   }
13094               }
13095           }
13096       }
13097
13098   /* We may have changed the comparison operands.  Re-canonicalize.  */
13099   if (swap_commutative_operands_p (op0, op1))
13100     {
13101       std::swap (op0, op1);
13102       code = swap_condition (code);
13103     }
13104
13105   /* If this machine only supports a subset of valid comparisons, see if we
13106      can convert an unsupported one into a supported one.  */
13107   target_canonicalize_comparison (&code, &op0, &op1, 0);
13108
13109   *pop0 = op0;
13110   *pop1 = op1;
13111
13112   return code;
13113 }
13114 \f
13115 /* Utility function for record_value_for_reg.  Count number of
13116    rtxs in X.  */
13117 static int
13118 count_rtxs (rtx x)
13119 {
13120   enum rtx_code code = GET_CODE (x);
13121   const char *fmt;
13122   int i, j, ret = 1;
13123
13124   if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13125       || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13126     {
13127       rtx x0 = XEXP (x, 0);
13128       rtx x1 = XEXP (x, 1);
13129
13130       if (x0 == x1)
13131         return 1 + 2 * count_rtxs (x0);
13132
13133       if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13134            || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13135           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13136         return 2 + 2 * count_rtxs (x0)
13137                + count_rtxs (x == XEXP (x1, 0)
13138                              ? XEXP (x1, 1) : XEXP (x1, 0));
13139
13140       if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13141            || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13142           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13143         return 2 + 2 * count_rtxs (x1)
13144                + count_rtxs (x == XEXP (x0, 0)
13145                              ? XEXP (x0, 1) : XEXP (x0, 0));
13146     }
13147
13148   fmt = GET_RTX_FORMAT (code);
13149   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13150     if (fmt[i] == 'e')
13151       ret += count_rtxs (XEXP (x, i));
13152     else if (fmt[i] == 'E')
13153       for (j = 0; j < XVECLEN (x, i); j++)
13154         ret += count_rtxs (XVECEXP (x, i, j));
13155
13156   return ret;
13157 }
13158 \f
13159 /* Utility function for following routine.  Called when X is part of a value
13160    being stored into last_set_value.  Sets last_set_table_tick
13161    for each register mentioned.  Similar to mention_regs in cse.c  */
13162
13163 static void
13164 update_table_tick (rtx x)
13165 {
13166   enum rtx_code code = GET_CODE (x);
13167   const char *fmt = GET_RTX_FORMAT (code);
13168   int i, j;
13169
13170   if (code == REG)
13171     {
13172       unsigned int regno = REGNO (x);
13173       unsigned int endregno = END_REGNO (x);
13174       unsigned int r;
13175
13176       for (r = regno; r < endregno; r++)
13177         {
13178           reg_stat_type *rsp = &reg_stat[r];
13179           rsp->last_set_table_tick = label_tick;
13180         }
13181
13182       return;
13183     }
13184
13185   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13186     if (fmt[i] == 'e')
13187       {
13188         /* Check for identical subexpressions.  If x contains
13189            identical subexpression we only have to traverse one of
13190            them.  */
13191         if (i == 0 && ARITHMETIC_P (x))
13192           {
13193             /* Note that at this point x1 has already been
13194                processed.  */
13195             rtx x0 = XEXP (x, 0);
13196             rtx x1 = XEXP (x, 1);
13197
13198             /* If x0 and x1 are identical then there is no need to
13199                process x0.  */
13200             if (x0 == x1)
13201               break;
13202
13203             /* If x0 is identical to a subexpression of x1 then while
13204                processing x1, x0 has already been processed.  Thus we
13205                are done with x.  */
13206             if (ARITHMETIC_P (x1)
13207                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13208               break;
13209
13210             /* If x1 is identical to a subexpression of x0 then we
13211                still have to process the rest of x0.  */
13212             if (ARITHMETIC_P (x0)
13213                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13214               {
13215                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13216                 break;
13217               }
13218           }
13219
13220         update_table_tick (XEXP (x, i));
13221       }
13222     else if (fmt[i] == 'E')
13223       for (j = 0; j < XVECLEN (x, i); j++)
13224         update_table_tick (XVECEXP (x, i, j));
13225 }
13226
13227 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
13228    are saying that the register is clobbered and we no longer know its
13229    value.  If INSN is zero, don't update reg_stat[].last_set; this is
13230    only permitted with VALUE also zero and is used to invalidate the
13231    register.  */
13232
13233 static void
13234 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13235 {
13236   unsigned int regno = REGNO (reg);
13237   unsigned int endregno = END_REGNO (reg);
13238   unsigned int i;
13239   reg_stat_type *rsp;
13240
13241   /* If VALUE contains REG and we have a previous value for REG, substitute
13242      the previous value.  */
13243   if (value && insn && reg_overlap_mentioned_p (reg, value))
13244     {
13245       rtx tem;
13246
13247       /* Set things up so get_last_value is allowed to see anything set up to
13248          our insn.  */
13249       subst_low_luid = DF_INSN_LUID (insn);
13250       tem = get_last_value (reg);
13251
13252       /* If TEM is simply a binary operation with two CLOBBERs as operands,
13253          it isn't going to be useful and will take a lot of time to process,
13254          so just use the CLOBBER.  */
13255
13256       if (tem)
13257         {
13258           if (ARITHMETIC_P (tem)
13259               && GET_CODE (XEXP (tem, 0)) == CLOBBER
13260               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13261             tem = XEXP (tem, 0);
13262           else if (count_occurrences (value, reg, 1) >= 2)
13263             {
13264               /* If there are two or more occurrences of REG in VALUE,
13265                  prevent the value from growing too much.  */
13266               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13267                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13268             }
13269
13270           value = replace_rtx (copy_rtx (value), reg, tem);
13271         }
13272     }
13273
13274   /* For each register modified, show we don't know its value, that
13275      we don't know about its bitwise content, that its value has been
13276      updated, and that we don't know the location of the death of the
13277      register.  */
13278   for (i = regno; i < endregno; i++)
13279     {
13280       rsp = &reg_stat[i];
13281
13282       if (insn)
13283         rsp->last_set = insn;
13284
13285       rsp->last_set_value = 0;
13286       rsp->last_set_mode = VOIDmode;
13287       rsp->last_set_nonzero_bits = 0;
13288       rsp->last_set_sign_bit_copies = 0;
13289       rsp->last_death = 0;
13290       rsp->truncated_to_mode = VOIDmode;
13291     }
13292
13293   /* Mark registers that are being referenced in this value.  */
13294   if (value)
13295     update_table_tick (value);
13296
13297   /* Now update the status of each register being set.
13298      If someone is using this register in this block, set this register
13299      to invalid since we will get confused between the two lives in this
13300      basic block.  This makes using this register always invalid.  In cse, we
13301      scan the table to invalidate all entries using this register, but this
13302      is too much work for us.  */
13303
13304   for (i = regno; i < endregno; i++)
13305     {
13306       rsp = &reg_stat[i];
13307       rsp->last_set_label = label_tick;
13308       if (!insn
13309           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13310         rsp->last_set_invalid = 1;
13311       else
13312         rsp->last_set_invalid = 0;
13313     }
13314
13315   /* The value being assigned might refer to X (like in "x++;").  In that
13316      case, we must replace it with (clobber (const_int 0)) to prevent
13317      infinite loops.  */
13318   rsp = &reg_stat[regno];
13319   if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13320     {
13321       value = copy_rtx (value);
13322       if (!get_last_value_validate (&value, insn, label_tick, 1))
13323         value = 0;
13324     }
13325
13326   /* For the main register being modified, update the value, the mode, the
13327      nonzero bits, and the number of sign bit copies.  */
13328
13329   rsp->last_set_value = value;
13330
13331   if (value)
13332     {
13333       machine_mode mode = GET_MODE (reg);
13334       subst_low_luid = DF_INSN_LUID (insn);
13335       rsp->last_set_mode = mode;
13336       if (GET_MODE_CLASS (mode) == MODE_INT
13337           && HWI_COMPUTABLE_MODE_P (mode))
13338         mode = nonzero_bits_mode;
13339       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13340       rsp->last_set_sign_bit_copies
13341         = num_sign_bit_copies (value, GET_MODE (reg));
13342     }
13343 }
13344
13345 /* Called via note_stores from record_dead_and_set_regs to handle one
13346    SET or CLOBBER in an insn.  DATA is the instruction in which the
13347    set is occurring.  */
13348
13349 static void
13350 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13351 {
13352   rtx_insn *record_dead_insn = (rtx_insn *) data;
13353
13354   if (GET_CODE (dest) == SUBREG)
13355     dest = SUBREG_REG (dest);
13356
13357   if (!record_dead_insn)
13358     {
13359       if (REG_P (dest))
13360         record_value_for_reg (dest, NULL, NULL_RTX);
13361       return;
13362     }
13363
13364   if (REG_P (dest))
13365     {
13366       /* If we are setting the whole register, we know its value.  Otherwise
13367          show that we don't know the value.  We can handle a SUBREG if it's
13368          the low part, but we must be careful with paradoxical SUBREGs on
13369          RISC architectures because we cannot strip e.g. an extension around
13370          a load and record the naked load since the RTL middle-end considers
13371          that the upper bits are defined according to LOAD_EXTEND_OP.  */
13372       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13373         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13374       else if (GET_CODE (setter) == SET
13375                && GET_CODE (SET_DEST (setter)) == SUBREG
13376                && SUBREG_REG (SET_DEST (setter)) == dest
13377                && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13378                             BITS_PER_WORD)
13379                && subreg_lowpart_p (SET_DEST (setter)))
13380         record_value_for_reg (dest, record_dead_insn,
13381                               WORD_REGISTER_OPERATIONS
13382                               && word_register_operation_p (SET_SRC (setter))
13383                               && paradoxical_subreg_p (SET_DEST (setter))
13384                               ? SET_SRC (setter)
13385                               : gen_lowpart (GET_MODE (dest),
13386                                              SET_SRC (setter)));
13387       else if (GET_CODE (setter) == CLOBBER_HIGH)
13388         {
13389           reg_stat_type *rsp = &reg_stat[REGNO (dest)];
13390           if (rsp->last_set_value
13391               && reg_is_clobbered_by_clobber_high
13392                    (REGNO (dest), GET_MODE (rsp->last_set_value),
13393                     XEXP (setter, 0)))
13394             record_value_for_reg (dest, NULL, NULL_RTX);
13395         }
13396       else
13397         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13398     }
13399   else if (MEM_P (dest)
13400            /* Ignore pushes, they clobber nothing.  */
13401            && ! push_operand (dest, GET_MODE (dest)))
13402     mem_last_set = DF_INSN_LUID (record_dead_insn);
13403 }
13404
13405 /* Update the records of when each REG was most recently set or killed
13406    for the things done by INSN.  This is the last thing done in processing
13407    INSN in the combiner loop.
13408
13409    We update reg_stat[], in particular fields last_set, last_set_value,
13410    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13411    last_death, and also the similar information mem_last_set (which insn
13412    most recently modified memory) and last_call_luid (which insn was the
13413    most recent subroutine call).  */
13414
13415 static void
13416 record_dead_and_set_regs (rtx_insn *insn)
13417 {
13418   rtx link;
13419   unsigned int i;
13420
13421   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13422     {
13423       if (REG_NOTE_KIND (link) == REG_DEAD
13424           && REG_P (XEXP (link, 0)))
13425         {
13426           unsigned int regno = REGNO (XEXP (link, 0));
13427           unsigned int endregno = END_REGNO (XEXP (link, 0));
13428
13429           for (i = regno; i < endregno; i++)
13430             {
13431               reg_stat_type *rsp;
13432
13433               rsp = &reg_stat[i];
13434               rsp->last_death = insn;
13435             }
13436         }
13437       else if (REG_NOTE_KIND (link) == REG_INC)
13438         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13439     }
13440
13441   if (CALL_P (insn))
13442     {
13443       hard_reg_set_iterator hrsi;
13444       EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13445         {
13446           reg_stat_type *rsp;
13447
13448           rsp = &reg_stat[i];
13449           rsp->last_set_invalid = 1;
13450           rsp->last_set = insn;
13451           rsp->last_set_value = 0;
13452           rsp->last_set_mode = VOIDmode;
13453           rsp->last_set_nonzero_bits = 0;
13454           rsp->last_set_sign_bit_copies = 0;
13455           rsp->last_death = 0;
13456           rsp->truncated_to_mode = VOIDmode;
13457         }
13458
13459       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13460
13461       /* We can't combine into a call pattern.  Remember, though, that
13462          the return value register is set at this LUID.  We could
13463          still replace a register with the return value from the
13464          wrong subroutine call!  */
13465       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13466     }
13467   else
13468     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13469 }
13470
13471 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13472    register present in the SUBREG, so for each such SUBREG go back and
13473    adjust nonzero and sign bit information of the registers that are
13474    known to have some zero/sign bits set.
13475
13476    This is needed because when combine blows the SUBREGs away, the
13477    information on zero/sign bits is lost and further combines can be
13478    missed because of that.  */
13479
13480 static void
13481 record_promoted_value (rtx_insn *insn, rtx subreg)
13482 {
13483   struct insn_link *links;
13484   rtx set;
13485   unsigned int regno = REGNO (SUBREG_REG (subreg));
13486   machine_mode mode = GET_MODE (subreg);
13487
13488   if (!HWI_COMPUTABLE_MODE_P (mode))
13489     return;
13490
13491   for (links = LOG_LINKS (insn); links;)
13492     {
13493       reg_stat_type *rsp;
13494
13495       insn = links->insn;
13496       set = single_set (insn);
13497
13498       if (! set || !REG_P (SET_DEST (set))
13499           || REGNO (SET_DEST (set)) != regno
13500           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13501         {
13502           links = links->next;
13503           continue;
13504         }
13505
13506       rsp = &reg_stat[regno];
13507       if (rsp->last_set == insn)
13508         {
13509           if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13510             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13511         }
13512
13513       if (REG_P (SET_SRC (set)))
13514         {
13515           regno = REGNO (SET_SRC (set));
13516           links = LOG_LINKS (insn);
13517         }
13518       else
13519         break;
13520     }
13521 }
13522
13523 /* Check if X, a register, is known to contain a value already
13524    truncated to MODE.  In this case we can use a subreg to refer to
13525    the truncated value even though in the generic case we would need
13526    an explicit truncation.  */
13527
13528 static bool
13529 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13530 {
13531   reg_stat_type *rsp = &reg_stat[REGNO (x)];
13532   machine_mode truncated = rsp->truncated_to_mode;
13533
13534   if (truncated == 0
13535       || rsp->truncation_label < label_tick_ebb_start)
13536     return false;
13537   if (!partial_subreg_p (mode, truncated))
13538     return true;
13539   if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13540     return true;
13541   return false;
13542 }
13543
13544 /* If X is a hard reg or a subreg record the mode that the register is
13545    accessed in.  For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13546    able to turn a truncate into a subreg using this information.  Return true
13547    if traversing X is complete.  */
13548
13549 static bool
13550 record_truncated_value (rtx x)
13551 {
13552   machine_mode truncated_mode;
13553   reg_stat_type *rsp;
13554
13555   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13556     {
13557       machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13558       truncated_mode = GET_MODE (x);
13559
13560       if (!partial_subreg_p (truncated_mode, original_mode))
13561         return true;
13562
13563       truncated_mode = GET_MODE (x);
13564       if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13565         return true;
13566
13567       x = SUBREG_REG (x);
13568     }
13569   /* ??? For hard-regs we now record everything.  We might be able to
13570      optimize this using last_set_mode.  */
13571   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13572     truncated_mode = GET_MODE (x);
13573   else
13574     return false;
13575
13576   rsp = &reg_stat[REGNO (x)];
13577   if (rsp->truncated_to_mode == 0
13578       || rsp->truncation_label < label_tick_ebb_start
13579       || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13580     {
13581       rsp->truncated_to_mode = truncated_mode;
13582       rsp->truncation_label = label_tick;
13583     }
13584
13585   return true;
13586 }
13587
13588 /* Callback for note_uses.  Find hardregs and subregs of pseudos and
13589    the modes they are used in.  This can help truning TRUNCATEs into
13590    SUBREGs.  */
13591
13592 static void
13593 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13594 {
13595   subrtx_var_iterator::array_type array;
13596   FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13597     if (record_truncated_value (*iter))
13598       iter.skip_subrtxes ();
13599 }
13600
13601 /* Scan X for promoted SUBREGs.  For each one found,
13602    note what it implies to the registers used in it.  */
13603
13604 static void
13605 check_promoted_subreg (rtx_insn *insn, rtx x)
13606 {
13607   if (GET_CODE (x) == SUBREG
13608       && SUBREG_PROMOTED_VAR_P (x)
13609       && REG_P (SUBREG_REG (x)))
13610     record_promoted_value (insn, x);
13611   else
13612     {
13613       const char *format = GET_RTX_FORMAT (GET_CODE (x));
13614       int i, j;
13615
13616       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13617         switch (format[i])
13618           {
13619           case 'e':
13620             check_promoted_subreg (insn, XEXP (x, i));
13621             break;
13622           case 'V':
13623           case 'E':
13624             if (XVEC (x, i) != 0)
13625               for (j = 0; j < XVECLEN (x, i); j++)
13626                 check_promoted_subreg (insn, XVECEXP (x, i, j));
13627             break;
13628           }
13629     }
13630 }
13631 \f
13632 /* Verify that all the registers and memory references mentioned in *LOC are
13633    still valid.  *LOC was part of a value set in INSN when label_tick was
13634    equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
13635    the invalid references with (clobber (const_int 0)) and return 1.  This
13636    replacement is useful because we often can get useful information about
13637    the form of a value (e.g., if it was produced by a shift that always
13638    produces -1 or 0) even though we don't know exactly what registers it
13639    was produced from.  */
13640
13641 static int
13642 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13643 {
13644   rtx x = *loc;
13645   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13646   int len = GET_RTX_LENGTH (GET_CODE (x));
13647   int i, j;
13648
13649   if (REG_P (x))
13650     {
13651       unsigned int regno = REGNO (x);
13652       unsigned int endregno = END_REGNO (x);
13653       unsigned int j;
13654
13655       for (j = regno; j < endregno; j++)
13656         {
13657           reg_stat_type *rsp = &reg_stat[j];
13658           if (rsp->last_set_invalid
13659               /* If this is a pseudo-register that was only set once and not
13660                  live at the beginning of the function, it is always valid.  */
13661               || (! (regno >= FIRST_PSEUDO_REGISTER
13662                      && regno < reg_n_sets_max
13663                      && REG_N_SETS (regno) == 1
13664                      && (!REGNO_REG_SET_P
13665                          (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13666                           regno)))
13667                   && rsp->last_set_label > tick))
13668           {
13669             if (replace)
13670               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13671             return replace;
13672           }
13673         }
13674
13675       return 1;
13676     }
13677   /* If this is a memory reference, make sure that there were no stores after
13678      it that might have clobbered the value.  We don't have alias info, so we
13679      assume any store invalidates it.  Moreover, we only have local UIDs, so
13680      we also assume that there were stores in the intervening basic blocks.  */
13681   else if (MEM_P (x) && !MEM_READONLY_P (x)
13682            && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13683     {
13684       if (replace)
13685         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13686       return replace;
13687     }
13688
13689   for (i = 0; i < len; i++)
13690     {
13691       if (fmt[i] == 'e')
13692         {
13693           /* Check for identical subexpressions.  If x contains
13694              identical subexpression we only have to traverse one of
13695              them.  */
13696           if (i == 1 && ARITHMETIC_P (x))
13697             {
13698               /* Note that at this point x0 has already been checked
13699                  and found valid.  */
13700               rtx x0 = XEXP (x, 0);
13701               rtx x1 = XEXP (x, 1);
13702
13703               /* If x0 and x1 are identical then x is also valid.  */
13704               if (x0 == x1)
13705                 return 1;
13706
13707               /* If x1 is identical to a subexpression of x0 then
13708                  while checking x0, x1 has already been checked.  Thus
13709                  it is valid and so as x.  */
13710               if (ARITHMETIC_P (x0)
13711                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13712                 return 1;
13713
13714               /* If x0 is identical to a subexpression of x1 then x is
13715                  valid iff the rest of x1 is valid.  */
13716               if (ARITHMETIC_P (x1)
13717                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13718                 return
13719                   get_last_value_validate (&XEXP (x1,
13720                                                   x0 == XEXP (x1, 0) ? 1 : 0),
13721                                            insn, tick, replace);
13722             }
13723
13724           if (get_last_value_validate (&XEXP (x, i), insn, tick,
13725                                        replace) == 0)
13726             return 0;
13727         }
13728       else if (fmt[i] == 'E')
13729         for (j = 0; j < XVECLEN (x, i); j++)
13730           if (get_last_value_validate (&XVECEXP (x, i, j),
13731                                        insn, tick, replace) == 0)
13732             return 0;
13733     }
13734
13735   /* If we haven't found a reason for it to be invalid, it is valid.  */
13736   return 1;
13737 }
13738
13739 /* Get the last value assigned to X, if known.  Some registers
13740    in the value may be replaced with (clobber (const_int 0)) if their value
13741    is known longer known reliably.  */
13742
13743 static rtx
13744 get_last_value (const_rtx x)
13745 {
13746   unsigned int regno;
13747   rtx value;
13748   reg_stat_type *rsp;
13749
13750   /* If this is a non-paradoxical SUBREG, get the value of its operand and
13751      then convert it to the desired mode.  If this is a paradoxical SUBREG,
13752      we cannot predict what values the "extra" bits might have.  */
13753   if (GET_CODE (x) == SUBREG
13754       && subreg_lowpart_p (x)
13755       && !paradoxical_subreg_p (x)
13756       && (value = get_last_value (SUBREG_REG (x))) != 0)
13757     return gen_lowpart (GET_MODE (x), value);
13758
13759   if (!REG_P (x))
13760     return 0;
13761
13762   regno = REGNO (x);
13763   rsp = &reg_stat[regno];
13764   value = rsp->last_set_value;
13765
13766   /* If we don't have a value, or if it isn't for this basic block and
13767      it's either a hard register, set more than once, or it's a live
13768      at the beginning of the function, return 0.
13769
13770      Because if it's not live at the beginning of the function then the reg
13771      is always set before being used (is never used without being set).
13772      And, if it's set only once, and it's always set before use, then all
13773      uses must have the same last value, even if it's not from this basic
13774      block.  */
13775
13776   if (value == 0
13777       || (rsp->last_set_label < label_tick_ebb_start
13778           && (regno < FIRST_PSEUDO_REGISTER
13779               || regno >= reg_n_sets_max
13780               || REG_N_SETS (regno) != 1
13781               || REGNO_REG_SET_P
13782                  (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13783     return 0;
13784
13785   /* If the value was set in a later insn than the ones we are processing,
13786      we can't use it even if the register was only set once.  */
13787   if (rsp->last_set_label == label_tick
13788       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13789     return 0;
13790
13791   /* If fewer bits were set than what we are asked for now, we cannot use
13792      the value.  */
13793   if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13794                 GET_MODE_PRECISION (GET_MODE (x))))
13795     return 0;
13796
13797   /* If the value has all its registers valid, return it.  */
13798   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13799     return value;
13800
13801   /* Otherwise, make a copy and replace any invalid register with
13802      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
13803
13804   value = copy_rtx (value);
13805   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13806     return value;
13807
13808   return 0;
13809 }
13810 \f
13811 /* Define three variables used for communication between the following
13812    routines.  */
13813
13814 static unsigned int reg_dead_regno, reg_dead_endregno;
13815 static int reg_dead_flag;
13816 rtx reg_dead_reg;
13817
13818 /* Function called via note_stores from reg_dead_at_p.
13819
13820    If DEST is within [reg_dead_regno, reg_dead_endregno), set
13821    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
13822
13823 static void
13824 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13825 {
13826   unsigned int regno, endregno;
13827
13828   if (!REG_P (dest))
13829     return;
13830
13831   if (GET_CODE (x) == CLOBBER_HIGH
13832       && !reg_is_clobbered_by_clobber_high (reg_dead_reg, XEXP (x, 0)))
13833     return;
13834
13835   regno = REGNO (dest);
13836   endregno = END_REGNO (dest);
13837   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13838     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13839 }
13840
13841 /* Return nonzero if REG is known to be dead at INSN.
13842
13843    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
13844    referencing REG, it is dead.  If we hit a SET referencing REG, it is
13845    live.  Otherwise, see if it is live or dead at the start of the basic
13846    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
13847    must be assumed to be always live.  */
13848
13849 static int
13850 reg_dead_at_p (rtx reg, rtx_insn *insn)
13851 {
13852   basic_block block;
13853   unsigned int i;
13854
13855   /* Set variables for reg_dead_at_p_1.  */
13856   reg_dead_regno = REGNO (reg);
13857   reg_dead_endregno = END_REGNO (reg);
13858   reg_dead_reg = reg;
13859
13860   reg_dead_flag = 0;
13861
13862   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
13863      we allow the machine description to decide whether use-and-clobber
13864      patterns are OK.  */
13865   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13866     {
13867       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13868         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13869           return 0;
13870     }
13871
13872   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13873      beginning of basic block.  */
13874   block = BLOCK_FOR_INSN (insn);
13875   for (;;)
13876     {
13877       if (INSN_P (insn))
13878         {
13879           if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13880             return 1;
13881
13882           note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13883           if (reg_dead_flag)
13884             return reg_dead_flag == 1 ? 1 : 0;
13885
13886           if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13887             return 1;
13888         }
13889
13890       if (insn == BB_HEAD (block))
13891         break;
13892
13893       insn = PREV_INSN (insn);
13894     }
13895
13896   /* Look at live-in sets for the basic block that we were in.  */
13897   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13898     if (REGNO_REG_SET_P (df_get_live_in (block), i))
13899       return 0;
13900
13901   return 1;
13902 }
13903 \f
13904 /* Note hard registers in X that are used.  */
13905
13906 static void
13907 mark_used_regs_combine (rtx x)
13908 {
13909   RTX_CODE code = GET_CODE (x);
13910   unsigned int regno;
13911   int i;
13912
13913   switch (code)
13914     {
13915     case LABEL_REF:
13916     case SYMBOL_REF:
13917     case CONST:
13918     CASE_CONST_ANY:
13919     case PC:
13920     case ADDR_VEC:
13921     case ADDR_DIFF_VEC:
13922     case ASM_INPUT:
13923     /* CC0 must die in the insn after it is set, so we don't need to take
13924        special note of it here.  */
13925     case CC0:
13926       return;
13927
13928     case CLOBBER:
13929       /* If we are clobbering a MEM, mark any hard registers inside the
13930          address as used.  */
13931       if (MEM_P (XEXP (x, 0)))
13932         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13933       return;
13934
13935     case REG:
13936       regno = REGNO (x);
13937       /* A hard reg in a wide mode may really be multiple registers.
13938          If so, mark all of them just like the first.  */
13939       if (regno < FIRST_PSEUDO_REGISTER)
13940         {
13941           /* None of this applies to the stack, frame or arg pointers.  */
13942           if (regno == STACK_POINTER_REGNUM
13943               || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13944                   && regno == HARD_FRAME_POINTER_REGNUM)
13945               || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13946                   && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13947               || regno == FRAME_POINTER_REGNUM)
13948             return;
13949
13950           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13951         }
13952       return;
13953
13954     case SET:
13955       {
13956         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13957            the address.  */
13958         rtx testreg = SET_DEST (x);
13959
13960         while (GET_CODE (testreg) == SUBREG
13961                || GET_CODE (testreg) == ZERO_EXTRACT
13962                || GET_CODE (testreg) == STRICT_LOW_PART)
13963           testreg = XEXP (testreg, 0);
13964
13965         if (MEM_P (testreg))
13966           mark_used_regs_combine (XEXP (testreg, 0));
13967
13968         mark_used_regs_combine (SET_SRC (x));
13969       }
13970       return;
13971
13972     default:
13973       break;
13974     }
13975
13976   /* Recursively scan the operands of this expression.  */
13977
13978   {
13979     const char *fmt = GET_RTX_FORMAT (code);
13980
13981     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13982       {
13983         if (fmt[i] == 'e')
13984           mark_used_regs_combine (XEXP (x, i));
13985         else if (fmt[i] == 'E')
13986           {
13987             int j;
13988
13989             for (j = 0; j < XVECLEN (x, i); j++)
13990               mark_used_regs_combine (XVECEXP (x, i, j));
13991           }
13992       }
13993   }
13994 }
13995 \f
13996 /* Remove register number REGNO from the dead registers list of INSN.
13997
13998    Return the note used to record the death, if there was one.  */
13999
14000 rtx
14001 remove_death (unsigned int regno, rtx_insn *insn)
14002 {
14003   rtx note = find_regno_note (insn, REG_DEAD, regno);
14004
14005   if (note)
14006     remove_note (insn, note);
14007
14008   return note;
14009 }
14010
14011 /* For each register (hardware or pseudo) used within expression X, if its
14012    death is in an instruction with luid between FROM_LUID (inclusive) and
14013    TO_INSN (exclusive), put a REG_DEAD note for that register in the
14014    list headed by PNOTES.
14015
14016    That said, don't move registers killed by maybe_kill_insn.
14017
14018    This is done when X is being merged by combination into TO_INSN.  These
14019    notes will then be distributed as needed.  */
14020
14021 static void
14022 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
14023              rtx *pnotes)
14024 {
14025   const char *fmt;
14026   int len, i;
14027   enum rtx_code code = GET_CODE (x);
14028
14029   if (code == REG)
14030     {
14031       unsigned int regno = REGNO (x);
14032       rtx_insn *where_dead = reg_stat[regno].last_death;
14033
14034       /* If we do not know where the register died, it may still die between
14035          FROM_LUID and TO_INSN.  If so, find it.  This is PR83304.  */
14036       if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
14037         {
14038           rtx_insn *insn = prev_real_nondebug_insn (to_insn);
14039           while (insn
14040                  && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
14041                  && DF_INSN_LUID (insn) >= from_luid)
14042             {
14043               if (dead_or_set_regno_p (insn, regno))
14044                 {
14045                   if (find_regno_note (insn, REG_DEAD, regno))
14046                     where_dead = insn;
14047                   break;
14048                 }
14049
14050               insn = prev_real_nondebug_insn (insn);
14051             }
14052         }
14053
14054       /* Don't move the register if it gets killed in between from and to.  */
14055       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
14056           && ! reg_referenced_p (x, maybe_kill_insn))
14057         return;
14058
14059       if (where_dead
14060           && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
14061           && DF_INSN_LUID (where_dead) >= from_luid
14062           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
14063         {
14064           rtx note = remove_death (regno, where_dead);
14065
14066           /* It is possible for the call above to return 0.  This can occur
14067              when last_death points to I2 or I1 that we combined with.
14068              In that case make a new note.
14069
14070              We must also check for the case where X is a hard register
14071              and NOTE is a death note for a range of hard registers
14072              including X.  In that case, we must put REG_DEAD notes for
14073              the remaining registers in place of NOTE.  */
14074
14075           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
14076               && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
14077             {
14078               unsigned int deadregno = REGNO (XEXP (note, 0));
14079               unsigned int deadend = END_REGNO (XEXP (note, 0));
14080               unsigned int ourend = END_REGNO (x);
14081               unsigned int i;
14082
14083               for (i = deadregno; i < deadend; i++)
14084                 if (i < regno || i >= ourend)
14085                   add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
14086             }
14087
14088           /* If we didn't find any note, or if we found a REG_DEAD note that
14089              covers only part of the given reg, and we have a multi-reg hard
14090              register, then to be safe we must check for REG_DEAD notes
14091              for each register other than the first.  They could have
14092              their own REG_DEAD notes lying around.  */
14093           else if ((note == 0
14094                     || (note != 0
14095                         && partial_subreg_p (GET_MODE (XEXP (note, 0)),
14096                                              GET_MODE (x))))
14097                    && regno < FIRST_PSEUDO_REGISTER
14098                    && REG_NREGS (x) > 1)
14099             {
14100               unsigned int ourend = END_REGNO (x);
14101               unsigned int i, offset;
14102               rtx oldnotes = 0;
14103
14104               if (note)
14105                 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
14106               else
14107                 offset = 1;
14108
14109               for (i = regno + offset; i < ourend; i++)
14110                 move_deaths (regno_reg_rtx[i],
14111                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
14112             }
14113
14114           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14115             {
14116               XEXP (note, 1) = *pnotes;
14117               *pnotes = note;
14118             }
14119           else
14120             *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14121         }
14122
14123       return;
14124     }
14125
14126   else if (GET_CODE (x) == SET)
14127     {
14128       rtx dest = SET_DEST (x);
14129
14130       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14131
14132       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14133          that accesses one word of a multi-word item, some
14134          piece of everything register in the expression is used by
14135          this insn, so remove any old death.  */
14136       /* ??? So why do we test for equality of the sizes?  */
14137
14138       if (GET_CODE (dest) == ZERO_EXTRACT
14139           || GET_CODE (dest) == STRICT_LOW_PART
14140           || (GET_CODE (dest) == SUBREG
14141               && !read_modify_subreg_p (dest)))
14142         {
14143           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14144           return;
14145         }
14146
14147       /* If this is some other SUBREG, we know it replaces the entire
14148          value, so use that as the destination.  */
14149       if (GET_CODE (dest) == SUBREG)
14150         dest = SUBREG_REG (dest);
14151
14152       /* If this is a MEM, adjust deaths of anything used in the address.
14153          For a REG (the only other possibility), the entire value is
14154          being replaced so the old value is not used in this insn.  */
14155
14156       if (MEM_P (dest))
14157         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14158                      to_insn, pnotes);
14159       return;
14160     }
14161
14162   else if (GET_CODE (x) == CLOBBER)
14163     return;
14164
14165   len = GET_RTX_LENGTH (code);
14166   fmt = GET_RTX_FORMAT (code);
14167
14168   for (i = 0; i < len; i++)
14169     {
14170       if (fmt[i] == 'E')
14171         {
14172           int j;
14173           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14174             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14175                          to_insn, pnotes);
14176         }
14177       else if (fmt[i] == 'e')
14178         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14179     }
14180 }
14181 \f
14182 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14183    pattern of an insn.  X must be a REG.  */
14184
14185 static int
14186 reg_bitfield_target_p (rtx x, rtx body)
14187 {
14188   int i;
14189
14190   if (GET_CODE (body) == SET)
14191     {
14192       rtx dest = SET_DEST (body);
14193       rtx target;
14194       unsigned int regno, tregno, endregno, endtregno;
14195
14196       if (GET_CODE (dest) == ZERO_EXTRACT)
14197         target = XEXP (dest, 0);
14198       else if (GET_CODE (dest) == STRICT_LOW_PART)
14199         target = SUBREG_REG (XEXP (dest, 0));
14200       else
14201         return 0;
14202
14203       if (GET_CODE (target) == SUBREG)
14204         target = SUBREG_REG (target);
14205
14206       if (!REG_P (target))
14207         return 0;
14208
14209       tregno = REGNO (target), regno = REGNO (x);
14210       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14211         return target == x;
14212
14213       endtregno = end_hard_regno (GET_MODE (target), tregno);
14214       endregno = end_hard_regno (GET_MODE (x), regno);
14215
14216       return endregno > tregno && regno < endtregno;
14217     }
14218
14219   else if (GET_CODE (body) == PARALLEL)
14220     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14221       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14222         return 1;
14223
14224   return 0;
14225 }
14226 \f
14227 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14228    as appropriate.  I3 and I2 are the insns resulting from the combination
14229    insns including FROM (I2 may be zero).
14230
14231    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14232    not need REG_DEAD notes because they are being substituted for.  This
14233    saves searching in the most common cases.
14234
14235    Each note in the list is either ignored or placed on some insns, depending
14236    on the type of note.  */
14237
14238 static void
14239 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14240                   rtx elim_i2, rtx elim_i1, rtx elim_i0)
14241 {
14242   rtx note, next_note;
14243   rtx tem_note;
14244   rtx_insn *tem_insn;
14245
14246   for (note = notes; note; note = next_note)
14247     {
14248       rtx_insn *place = 0, *place2 = 0;
14249
14250       next_note = XEXP (note, 1);
14251       switch (REG_NOTE_KIND (note))
14252         {
14253         case REG_BR_PROB:
14254         case REG_BR_PRED:
14255           /* Doesn't matter much where we put this, as long as it's somewhere.
14256              It is preferable to keep these notes on branches, which is most
14257              likely to be i3.  */
14258           place = i3;
14259           break;
14260
14261         case REG_NON_LOCAL_GOTO:
14262           if (JUMP_P (i3))
14263             place = i3;
14264           else
14265             {
14266               gcc_assert (i2 && JUMP_P (i2));
14267               place = i2;
14268             }
14269           break;
14270
14271         case REG_EH_REGION:
14272           /* These notes must remain with the call or trapping instruction.  */
14273           if (CALL_P (i3))
14274             place = i3;
14275           else if (i2 && CALL_P (i2))
14276             place = i2;
14277           else
14278             {
14279               gcc_assert (cfun->can_throw_non_call_exceptions);
14280               if (may_trap_p (i3))
14281                 place = i3;
14282               else if (i2 && may_trap_p (i2))
14283                 place = i2;
14284               /* ??? Otherwise assume we've combined things such that we
14285                  can now prove that the instructions can't trap.  Drop the
14286                  note in this case.  */
14287             }
14288           break;
14289
14290         case REG_ARGS_SIZE:
14291           /* ??? How to distribute between i3-i1.  Assume i3 contains the
14292              entire adjustment.  Assert i3 contains at least some adjust.  */
14293           if (!noop_move_p (i3))
14294             {
14295               poly_int64 old_size, args_size = get_args_size (note);
14296               /* fixup_args_size_notes looks at REG_NORETURN note,
14297                  so ensure the note is placed there first.  */
14298               if (CALL_P (i3))
14299                 {
14300                   rtx *np;
14301                   for (np = &next_note; *np; np = &XEXP (*np, 1))
14302                     if (REG_NOTE_KIND (*np) == REG_NORETURN)
14303                       {
14304                         rtx n = *np;
14305                         *np = XEXP (n, 1);
14306                         XEXP (n, 1) = REG_NOTES (i3);
14307                         REG_NOTES (i3) = n;
14308                         break;
14309                       }
14310                 }
14311               old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14312               /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14313                  REG_ARGS_SIZE note to all noreturn calls, allow that here.  */
14314               gcc_assert (maybe_ne (old_size, args_size)
14315                           || (CALL_P (i3)
14316                               && !ACCUMULATE_OUTGOING_ARGS
14317                               && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14318             }
14319           break;
14320
14321         case REG_NORETURN:
14322         case REG_SETJMP:
14323         case REG_TM:
14324         case REG_CALL_DECL:
14325         case REG_CALL_NOCF_CHECK:
14326           /* These notes must remain with the call.  It should not be
14327              possible for both I2 and I3 to be a call.  */
14328           if (CALL_P (i3))
14329             place = i3;
14330           else
14331             {
14332               gcc_assert (i2 && CALL_P (i2));
14333               place = i2;
14334             }
14335           break;
14336
14337         case REG_UNUSED:
14338           /* Any clobbers for i3 may still exist, and so we must process
14339              REG_UNUSED notes from that insn.
14340
14341              Any clobbers from i2 or i1 can only exist if they were added by
14342              recog_for_combine.  In that case, recog_for_combine created the
14343              necessary REG_UNUSED notes.  Trying to keep any original
14344              REG_UNUSED notes from these insns can cause incorrect output
14345              if it is for the same register as the original i3 dest.
14346              In that case, we will notice that the register is set in i3,
14347              and then add a REG_UNUSED note for the destination of i3, which
14348              is wrong.  However, it is possible to have REG_UNUSED notes from
14349              i2 or i1 for register which were both used and clobbered, so
14350              we keep notes from i2 or i1 if they will turn into REG_DEAD
14351              notes.  */
14352
14353           /* If this register is set or clobbered in I3, put the note there
14354              unless there is one already.  */
14355           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14356             {
14357               if (from_insn != i3)
14358                 break;
14359
14360               if (! (REG_P (XEXP (note, 0))
14361                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14362                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14363                 place = i3;
14364             }
14365           /* Otherwise, if this register is used by I3, then this register
14366              now dies here, so we must put a REG_DEAD note here unless there
14367              is one already.  */
14368           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14369                    && ! (REG_P (XEXP (note, 0))
14370                          ? find_regno_note (i3, REG_DEAD,
14371                                             REGNO (XEXP (note, 0)))
14372                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14373             {
14374               PUT_REG_NOTE_KIND (note, REG_DEAD);
14375               place = i3;
14376             }
14377
14378           /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14379              but we can't tell which at this point.  We must reset any
14380              expectations we had about the value that was previously
14381              stored in the reg.  ??? Ideally, we'd adjust REG_N_SETS
14382              and, if appropriate, restore its previous value, but we
14383              don't have enough information for that at this point.  */
14384           else
14385             {
14386               record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14387
14388               /* Otherwise, if this register is now referenced in i2
14389                  then the register used to be modified in one of the
14390                  original insns.  If it was i3 (say, in an unused
14391                  parallel), it's now completely gone, so the note can
14392                  be discarded.  But if it was modified in i2, i1 or i0
14393                  and we still reference it in i2, then we're
14394                  referencing the previous value, and since the
14395                  register was modified and REG_UNUSED, we know that
14396                  the previous value is now dead.  So, if we only
14397                  reference the register in i2, we change the note to
14398                  REG_DEAD, to reflect the previous value.  However, if
14399                  we're also setting or clobbering the register as
14400                  scratch, we know (because the register was not
14401                  referenced in i3) that it's unused, just as it was
14402                  unused before, and we place the note in i2.  */
14403               if (from_insn != i3 && i2 && INSN_P (i2)
14404                   && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14405                 {
14406                   if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14407                     PUT_REG_NOTE_KIND (note, REG_DEAD);
14408                   if (! (REG_P (XEXP (note, 0))
14409                          ? find_regno_note (i2, REG_NOTE_KIND (note),
14410                                             REGNO (XEXP (note, 0)))
14411                          : find_reg_note (i2, REG_NOTE_KIND (note),
14412                                           XEXP (note, 0))))
14413                     place = i2;
14414                 }
14415             }
14416
14417           break;
14418
14419         case REG_EQUAL:
14420         case REG_EQUIV:
14421         case REG_NOALIAS:
14422           /* These notes say something about results of an insn.  We can
14423              only support them if they used to be on I3 in which case they
14424              remain on I3.  Otherwise they are ignored.
14425
14426              If the note refers to an expression that is not a constant, we
14427              must also ignore the note since we cannot tell whether the
14428              equivalence is still true.  It might be possible to do
14429              slightly better than this (we only have a problem if I2DEST
14430              or I1DEST is present in the expression), but it doesn't
14431              seem worth the trouble.  */
14432
14433           if (from_insn == i3
14434               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14435             place = i3;
14436           break;
14437
14438         case REG_INC:
14439           /* These notes say something about how a register is used.  They must
14440              be present on any use of the register in I2 or I3.  */
14441           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14442             place = i3;
14443
14444           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14445             {
14446               if (place)
14447                 place2 = i2;
14448               else
14449                 place = i2;
14450             }
14451           break;
14452
14453         case REG_LABEL_TARGET:
14454         case REG_LABEL_OPERAND:
14455           /* This can show up in several ways -- either directly in the
14456              pattern, or hidden off in the constant pool with (or without?)
14457              a REG_EQUAL note.  */
14458           /* ??? Ignore the without-reg_equal-note problem for now.  */
14459           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14460               || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14461                   && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14462                   && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14463             place = i3;
14464
14465           if (i2
14466               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14467                   || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14468                       && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14469                       && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14470             {
14471               if (place)
14472                 place2 = i2;
14473               else
14474                 place = i2;
14475             }
14476
14477           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14478              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14479              there.  */
14480           if (place && JUMP_P (place)
14481               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14482               && (JUMP_LABEL (place) == NULL
14483                   || JUMP_LABEL (place) == XEXP (note, 0)))
14484             {
14485               rtx label = JUMP_LABEL (place);
14486
14487               if (!label)
14488                 JUMP_LABEL (place) = XEXP (note, 0);
14489               else if (LABEL_P (label))
14490                 LABEL_NUSES (label)--;
14491             }
14492
14493           if (place2 && JUMP_P (place2)
14494               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14495               && (JUMP_LABEL (place2) == NULL
14496                   || JUMP_LABEL (place2) == XEXP (note, 0)))
14497             {
14498               rtx label = JUMP_LABEL (place2);
14499
14500               if (!label)
14501                 JUMP_LABEL (place2) = XEXP (note, 0);
14502               else if (LABEL_P (label))
14503                 LABEL_NUSES (label)--;
14504               place2 = 0;
14505             }
14506           break;
14507
14508         case REG_NONNEG:
14509           /* This note says something about the value of a register prior
14510              to the execution of an insn.  It is too much trouble to see
14511              if the note is still correct in all situations.  It is better
14512              to simply delete it.  */
14513           break;
14514
14515         case REG_DEAD:
14516           /* If we replaced the right hand side of FROM_INSN with a
14517              REG_EQUAL note, the original use of the dying register
14518              will not have been combined into I3 and I2.  In such cases,
14519              FROM_INSN is guaranteed to be the first of the combined
14520              instructions, so we simply need to search back before
14521              FROM_INSN for the previous use or set of this register,
14522              then alter the notes there appropriately.
14523
14524              If the register is used as an input in I3, it dies there.
14525              Similarly for I2, if it is nonzero and adjacent to I3.
14526
14527              If the register is not used as an input in either I3 or I2
14528              and it is not one of the registers we were supposed to eliminate,
14529              there are two possibilities.  We might have a non-adjacent I2
14530              or we might have somehow eliminated an additional register
14531              from a computation.  For example, we might have had A & B where
14532              we discover that B will always be zero.  In this case we will
14533              eliminate the reference to A.
14534
14535              In both cases, we must search to see if we can find a previous
14536              use of A and put the death note there.  */
14537
14538           if (from_insn
14539               && from_insn == i2mod
14540               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14541             tem_insn = from_insn;
14542           else
14543             {
14544               if (from_insn
14545                   && CALL_P (from_insn)
14546                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14547                 place = from_insn;
14548               else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14549                 {
14550                   /* If the new I2 sets the same register that is marked
14551                      dead in the note, we do not in general know where to
14552                      put the note.  One important case we _can_ handle is
14553                      when the note comes from I3.  */
14554                   if (from_insn == i3)
14555                     place = i3;
14556                   else
14557                     break;
14558                 }
14559               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14560                 place = i3;
14561               else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14562                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14563                 place = i2;
14564               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14565                         && !(i2mod
14566                              && reg_overlap_mentioned_p (XEXP (note, 0),
14567                                                          i2mod_old_rhs)))
14568                        || rtx_equal_p (XEXP (note, 0), elim_i1)
14569                        || rtx_equal_p (XEXP (note, 0), elim_i0))
14570                 break;
14571               tem_insn = i3;
14572             }
14573
14574           if (place == 0)
14575             {
14576               basic_block bb = this_basic_block;
14577
14578               for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14579                 {
14580                   if (!NONDEBUG_INSN_P (tem_insn))
14581                     {
14582                       if (tem_insn == BB_HEAD (bb))
14583                         break;
14584                       continue;
14585                     }
14586
14587                   /* If the register is being set at TEM_INSN, see if that is all
14588                      TEM_INSN is doing.  If so, delete TEM_INSN.  Otherwise, make this
14589                      into a REG_UNUSED note instead. Don't delete sets to
14590                      global register vars.  */
14591                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14592                        || !global_regs[REGNO (XEXP (note, 0))])
14593                       && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14594                     {
14595                       rtx set = single_set (tem_insn);
14596                       rtx inner_dest = 0;
14597                       rtx_insn *cc0_setter = NULL;
14598
14599                       if (set != 0)
14600                         for (inner_dest = SET_DEST (set);
14601                              (GET_CODE (inner_dest) == STRICT_LOW_PART
14602                               || GET_CODE (inner_dest) == SUBREG
14603                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
14604                              inner_dest = XEXP (inner_dest, 0))
14605                           ;
14606
14607                       /* Verify that it was the set, and not a clobber that
14608                          modified the register.
14609
14610                          CC0 targets must be careful to maintain setter/user
14611                          pairs.  If we cannot delete the setter due to side
14612                          effects, mark the user with an UNUSED note instead
14613                          of deleting it.  */
14614
14615                       if (set != 0 && ! side_effects_p (SET_SRC (set))
14616                           && rtx_equal_p (XEXP (note, 0), inner_dest)
14617                           && (!HAVE_cc0
14618                               || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14619                                   || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14620                                       && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14621                         {
14622                           /* Move the notes and links of TEM_INSN elsewhere.
14623                              This might delete other dead insns recursively.
14624                              First set the pattern to something that won't use
14625                              any register.  */
14626                           rtx old_notes = REG_NOTES (tem_insn);
14627
14628                           PATTERN (tem_insn) = pc_rtx;
14629                           REG_NOTES (tem_insn) = NULL;
14630
14631                           distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14632                                             NULL_RTX, NULL_RTX, NULL_RTX);
14633                           distribute_links (LOG_LINKS (tem_insn));
14634
14635                           unsigned int regno = REGNO (XEXP (note, 0));
14636                           reg_stat_type *rsp = &reg_stat[regno];
14637                           if (rsp->last_set == tem_insn)
14638                             record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14639
14640                           SET_INSN_DELETED (tem_insn);
14641                           if (tem_insn == i2)
14642                             i2 = NULL;
14643
14644                           /* Delete the setter too.  */
14645                           if (cc0_setter)
14646                             {
14647                               PATTERN (cc0_setter) = pc_rtx;
14648                               old_notes = REG_NOTES (cc0_setter);
14649                               REG_NOTES (cc0_setter) = NULL;
14650
14651                               distribute_notes (old_notes, cc0_setter,
14652                                                 cc0_setter, NULL,
14653                                                 NULL_RTX, NULL_RTX, NULL_RTX);
14654                               distribute_links (LOG_LINKS (cc0_setter));
14655
14656                               SET_INSN_DELETED (cc0_setter);
14657                               if (cc0_setter == i2)
14658                                 i2 = NULL;
14659                             }
14660                         }
14661                       else
14662                         {
14663                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
14664
14665                           /*  If there isn't already a REG_UNUSED note, put one
14666                               here.  Do not place a REG_DEAD note, even if
14667                               the register is also used here; that would not
14668                               match the algorithm used in lifetime analysis
14669                               and can cause the consistency check in the
14670                               scheduler to fail.  */
14671                           if (! find_regno_note (tem_insn, REG_UNUSED,
14672                                                  REGNO (XEXP (note, 0))))
14673                             place = tem_insn;
14674                           break;
14675                         }
14676                     }
14677                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14678                            || (CALL_P (tem_insn)
14679                                && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14680                     {
14681                       place = tem_insn;
14682
14683                       /* If we are doing a 3->2 combination, and we have a
14684                          register which formerly died in i3 and was not used
14685                          by i2, which now no longer dies in i3 and is used in
14686                          i2 but does not die in i2, and place is between i2
14687                          and i3, then we may need to move a link from place to
14688                          i2.  */
14689                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14690                           && from_insn
14691                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14692                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14693                         {
14694                           struct insn_link *links = LOG_LINKS (place);
14695                           LOG_LINKS (place) = NULL;
14696                           distribute_links (links);
14697                         }
14698                       break;
14699                     }
14700
14701                   if (tem_insn == BB_HEAD (bb))
14702                     break;
14703                 }
14704
14705             }
14706
14707           /* If the register is set or already dead at PLACE, we needn't do
14708              anything with this note if it is still a REG_DEAD note.
14709              We check here if it is set at all, not if is it totally replaced,
14710              which is what `dead_or_set_p' checks, so also check for it being
14711              set partially.  */
14712
14713           if (place && REG_NOTE_KIND (note) == REG_DEAD)
14714             {
14715               unsigned int regno = REGNO (XEXP (note, 0));
14716               reg_stat_type *rsp = &reg_stat[regno];
14717
14718               if (dead_or_set_p (place, XEXP (note, 0))
14719                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14720                 {
14721                   /* Unless the register previously died in PLACE, clear
14722                      last_death.  [I no longer understand why this is
14723                      being done.] */
14724                   if (rsp->last_death != place)
14725                     rsp->last_death = 0;
14726                   place = 0;
14727                 }
14728               else
14729                 rsp->last_death = place;
14730
14731               /* If this is a death note for a hard reg that is occupying
14732                  multiple registers, ensure that we are still using all
14733                  parts of the object.  If we find a piece of the object
14734                  that is unused, we must arrange for an appropriate REG_DEAD
14735                  note to be added for it.  However, we can't just emit a USE
14736                  and tag the note to it, since the register might actually
14737                  be dead; so we recourse, and the recursive call then finds
14738                  the previous insn that used this register.  */
14739
14740               if (place && REG_NREGS (XEXP (note, 0)) > 1)
14741                 {
14742                   unsigned int endregno = END_REGNO (XEXP (note, 0));
14743                   bool all_used = true;
14744                   unsigned int i;
14745
14746                   for (i = regno; i < endregno; i++)
14747                     if ((! refers_to_regno_p (i, PATTERN (place))
14748                          && ! find_regno_fusage (place, USE, i))
14749                         || dead_or_set_regno_p (place, i))
14750                       {
14751                         all_used = false;
14752                         break;
14753                       }
14754
14755                   if (! all_used)
14756                     {
14757                       /* Put only REG_DEAD notes for pieces that are
14758                          not already dead or set.  */
14759
14760                       for (i = regno; i < endregno;
14761                            i += hard_regno_nregs (i, reg_raw_mode[i]))
14762                         {
14763                           rtx piece = regno_reg_rtx[i];
14764                           basic_block bb = this_basic_block;
14765
14766                           if (! dead_or_set_p (place, piece)
14767                               && ! reg_bitfield_target_p (piece,
14768                                                           PATTERN (place)))
14769                             {
14770                               rtx new_note = alloc_reg_note (REG_DEAD, piece,
14771                                                              NULL_RTX);
14772
14773                               distribute_notes (new_note, place, place,
14774                                                 NULL, NULL_RTX, NULL_RTX,
14775                                                 NULL_RTX);
14776                             }
14777                           else if (! refers_to_regno_p (i, PATTERN (place))
14778                                    && ! find_regno_fusage (place, USE, i))
14779                             for (tem_insn = PREV_INSN (place); ;
14780                                  tem_insn = PREV_INSN (tem_insn))
14781                               {
14782                                 if (!NONDEBUG_INSN_P (tem_insn))
14783                                   {
14784                                     if (tem_insn == BB_HEAD (bb))
14785                                       break;
14786                                     continue;
14787                                   }
14788                                 if (dead_or_set_p (tem_insn, piece)
14789                                     || reg_bitfield_target_p (piece,
14790                                                               PATTERN (tem_insn)))
14791                                   {
14792                                     add_reg_note (tem_insn, REG_UNUSED, piece);
14793                                     break;
14794                                   }
14795                               }
14796                         }
14797
14798                       place = 0;
14799                     }
14800                 }
14801             }
14802           break;
14803
14804         default:
14805           /* Any other notes should not be present at this point in the
14806              compilation.  */
14807           gcc_unreachable ();
14808         }
14809
14810       if (place)
14811         {
14812           XEXP (note, 1) = REG_NOTES (place);
14813           REG_NOTES (place) = note;
14814
14815           /* Set added_notes_insn to the earliest insn we added a note to.  */
14816           if (added_notes_insn == 0
14817               || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14818             added_notes_insn = place;
14819         }
14820
14821       if (place2)
14822         {
14823           add_shallow_copy_of_reg_note (place2, note);
14824
14825           /* Set added_notes_insn to the earliest insn we added a note to.  */
14826           if (added_notes_insn == 0
14827               || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14828             added_notes_insn = place2;
14829         }
14830     }
14831 }
14832 \f
14833 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14834    I3, I2, and I1 to new locations.  This is also called to add a link
14835    pointing at I3 when I3's destination is changed.  */
14836
14837 static void
14838 distribute_links (struct insn_link *links)
14839 {
14840   struct insn_link *link, *next_link;
14841
14842   for (link = links; link; link = next_link)
14843     {
14844       rtx_insn *place = 0;
14845       rtx_insn *insn;
14846       rtx set, reg;
14847
14848       next_link = link->next;
14849
14850       /* If the insn that this link points to is a NOTE, ignore it.  */
14851       if (NOTE_P (link->insn))
14852         continue;
14853
14854       set = 0;
14855       rtx pat = PATTERN (link->insn);
14856       if (GET_CODE (pat) == SET)
14857         set = pat;
14858       else if (GET_CODE (pat) == PARALLEL)
14859         {
14860           int i;
14861           for (i = 0; i < XVECLEN (pat, 0); i++)
14862             {
14863               set = XVECEXP (pat, 0, i);
14864               if (GET_CODE (set) != SET)
14865                 continue;
14866
14867               reg = SET_DEST (set);
14868               while (GET_CODE (reg) == ZERO_EXTRACT
14869                      || GET_CODE (reg) == STRICT_LOW_PART
14870                      || GET_CODE (reg) == SUBREG)
14871                 reg = XEXP (reg, 0);
14872
14873               if (!REG_P (reg))
14874                 continue;
14875
14876               if (REGNO (reg) == link->regno)
14877                 break;
14878             }
14879           if (i == XVECLEN (pat, 0))
14880             continue;
14881         }
14882       else
14883         continue;
14884
14885       reg = SET_DEST (set);
14886
14887       while (GET_CODE (reg) == ZERO_EXTRACT
14888              || GET_CODE (reg) == STRICT_LOW_PART
14889              || GET_CODE (reg) == SUBREG)
14890         reg = XEXP (reg, 0);
14891
14892       if (reg == pc_rtx)
14893         continue;
14894
14895       /* A LOG_LINK is defined as being placed on the first insn that uses
14896          a register and points to the insn that sets the register.  Start
14897          searching at the next insn after the target of the link and stop
14898          when we reach a set of the register or the end of the basic block.
14899
14900          Note that this correctly handles the link that used to point from
14901          I3 to I2.  Also note that not much searching is typically done here
14902          since most links don't point very far away.  */
14903
14904       for (insn = NEXT_INSN (link->insn);
14905            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14906                      || BB_HEAD (this_basic_block->next_bb) != insn));
14907            insn = NEXT_INSN (insn))
14908         if (DEBUG_INSN_P (insn))
14909           continue;
14910         else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14911           {
14912             if (reg_referenced_p (reg, PATTERN (insn)))
14913               place = insn;
14914             break;
14915           }
14916         else if (CALL_P (insn)
14917                  && find_reg_fusage (insn, USE, reg))
14918           {
14919             place = insn;
14920             break;
14921           }
14922         else if (INSN_P (insn) && reg_set_p (reg, insn))
14923           break;
14924
14925       /* If we found a place to put the link, place it there unless there
14926          is already a link to the same insn as LINK at that point.  */
14927
14928       if (place)
14929         {
14930           struct insn_link *link2;
14931
14932           FOR_EACH_LOG_LINK (link2, place)
14933             if (link2->insn == link->insn && link2->regno == link->regno)
14934               break;
14935
14936           if (link2 == NULL)
14937             {
14938               link->next = LOG_LINKS (place);
14939               LOG_LINKS (place) = link;
14940
14941               /* Set added_links_insn to the earliest insn we added a
14942                  link to.  */
14943               if (added_links_insn == 0
14944                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14945                 added_links_insn = place;
14946             }
14947         }
14948     }
14949 }
14950 \f
14951 /* Check for any register or memory mentioned in EQUIV that is not
14952    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
14953    of EXPR where some registers may have been replaced by constants.  */
14954
14955 static bool
14956 unmentioned_reg_p (rtx equiv, rtx expr)
14957 {
14958   subrtx_iterator::array_type array;
14959   FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14960     {
14961       const_rtx x = *iter;
14962       if ((REG_P (x) || MEM_P (x))
14963           && !reg_mentioned_p (x, expr))
14964         return true;
14965     }
14966   return false;
14967 }
14968 \f
14969 DEBUG_FUNCTION void
14970 dump_combine_stats (FILE *file)
14971 {
14972   fprintf
14973     (file,
14974      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14975      combine_attempts, combine_merges, combine_extras, combine_successes);
14976 }
14977
14978 void
14979 dump_combine_total_stats (FILE *file)
14980 {
14981   fprintf
14982     (file,
14983      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14984      total_attempts, total_merges, total_extras, total_successes);
14985 }
14986 \f
14987 /* Make pseudo-to-pseudo copies after every hard-reg-to-pseudo-copy, because
14988    the reg-to-reg copy can usefully combine with later instructions, but we
14989    do not want to combine the hard reg into later instructions, for that
14990    restricts register allocation.  */
14991 static void
14992 make_more_copies (void)
14993 {
14994   basic_block bb;
14995
14996   FOR_EACH_BB_FN (bb, cfun)
14997     {
14998       rtx_insn *insn;
14999
15000       FOR_BB_INSNS (bb, insn)
15001         {
15002           if (!NONDEBUG_INSN_P (insn))
15003             continue;
15004
15005           rtx set = single_set (insn);
15006           if (!set)
15007             continue;
15008
15009           rtx dest = SET_DEST (set);
15010           if (!(REG_P (dest) && !HARD_REGISTER_P (dest)))
15011               continue;
15012
15013           rtx src = SET_SRC (set);
15014           if (!(REG_P (src) && HARD_REGISTER_P (src)))
15015             continue;
15016           if (TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src)))
15017             continue;
15018
15019           rtx new_reg = gen_reg_rtx (GET_MODE (dest));
15020           rtx_insn *new_insn = gen_move_insn (new_reg, src);
15021           SET_SRC (set) = new_reg;
15022           emit_insn_before (new_insn, insn);
15023           df_insn_rescan (insn);
15024         }
15025     }
15026 }
15027
15028 /* Try combining insns through substitution.  */
15029 static unsigned int
15030 rest_of_handle_combine (void)
15031 {
15032   make_more_copies ();
15033
15034   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
15035   df_note_add_problem ();
15036   df_analyze ();
15037
15038   regstat_init_n_sets_and_refs ();
15039   reg_n_sets_max = max_reg_num ();
15040
15041   int rebuild_jump_labels_after_combine
15042     = combine_instructions (get_insns (), max_reg_num ());
15043
15044   /* Combining insns may have turned an indirect jump into a
15045      direct jump.  Rebuild the JUMP_LABEL fields of jumping
15046      instructions.  */
15047   if (rebuild_jump_labels_after_combine)
15048     {
15049       if (dom_info_available_p (CDI_DOMINATORS))
15050         free_dominance_info (CDI_DOMINATORS);
15051       timevar_push (TV_JUMP);
15052       rebuild_jump_labels (get_insns ());
15053       cleanup_cfg (0);
15054       timevar_pop (TV_JUMP);
15055     }
15056
15057   regstat_free_n_sets_and_refs ();
15058   return 0;
15059 }
15060
15061 namespace {
15062
15063 const pass_data pass_data_combine =
15064 {
15065   RTL_PASS, /* type */
15066   "combine", /* name */
15067   OPTGROUP_NONE, /* optinfo_flags */
15068   TV_COMBINE, /* tv_id */
15069   PROP_cfglayout, /* properties_required */
15070   0, /* properties_provided */
15071   0, /* properties_destroyed */
15072   0, /* todo_flags_start */
15073   TODO_df_finish, /* todo_flags_finish */
15074 };
15075
15076 class pass_combine : public rtl_opt_pass
15077 {
15078 public:
15079   pass_combine (gcc::context *ctxt)
15080     : rtl_opt_pass (pass_data_combine, ctxt)
15081   {}
15082
15083   /* opt_pass methods: */
15084   virtual bool gate (function *) { return (optimize > 0); }
15085   virtual unsigned int execute (function *)
15086     {
15087       return rest_of_handle_combine ();
15088     }
15089
15090 }; // class pass_combine
15091
15092 } // anon namespace
15093
15094 rtl_opt_pass *
15095 make_pass_combine (gcc::context *ctxt)
15096 {
15097   return new pass_combine (ctxt);
15098 }