re PR target/54589 (struct offset add should be folded into address calculation)
[platform/upstream/gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2    Copyright (C) 1987-2018 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 can not 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       bits = nonzero_bits (src, nonzero_bits_mode);
1702       if (reg_equal && bits)
1703         bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1704       rsp->nonzero_bits |= bits;
1705     }
1706
1707   /* Don't call num_sign_bit_copies if it cannot change anything.  */
1708   if (rsp->sign_bit_copies != 1)
1709     {
1710       num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1711       if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1712         {
1713           unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1714           if (num == 0 || numeq > num)
1715             num = numeq;
1716         }
1717       if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1718         rsp->sign_bit_copies = num;
1719     }
1720 }
1721
1722 /* Called via note_stores.  If X is a pseudo that is narrower than
1723    HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1724
1725    If we are setting only a portion of X and we can't figure out what
1726    portion, assume all bits will be used since we don't know what will
1727    be happening.
1728
1729    Similarly, set how many bits of X are known to be copies of the sign bit
1730    at all locations in the function.  This is the smallest number implied
1731    by any set of X.  */
1732
1733 static void
1734 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1735 {
1736   rtx_insn *insn = (rtx_insn *) data;
1737   scalar_int_mode mode;
1738
1739   if (REG_P (x)
1740       && REGNO (x) >= FIRST_PSEUDO_REGISTER
1741       /* If this register is undefined at the start of the file, we can't
1742          say what its contents were.  */
1743       && ! REGNO_REG_SET_P
1744            (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1745       && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1746       && HWI_COMPUTABLE_MODE_P (mode))
1747     {
1748       reg_stat_type *rsp = &reg_stat[REGNO (x)];
1749
1750       if (set == 0 || GET_CODE (set) == CLOBBER)
1751         {
1752           rsp->nonzero_bits = GET_MODE_MASK (mode);
1753           rsp->sign_bit_copies = 1;
1754           return;
1755         }
1756
1757       /* Should not happen as we only using pseduo registers.  */
1758       gcc_assert (GET_CODE (set) != CLOBBER_HIGH);
1759
1760       /* If this register is being initialized using itself, and the
1761          register is uninitialized in this basic block, and there are
1762          no LOG_LINKS which set the register, then part of the
1763          register is uninitialized.  In that case we can't assume
1764          anything about the number of nonzero bits.
1765
1766          ??? We could do better if we checked this in
1767          reg_{nonzero_bits,num_sign_bit_copies}_for_combine.  Then we
1768          could avoid making assumptions about the insn which initially
1769          sets the register, while still using the information in other
1770          insns.  We would have to be careful to check every insn
1771          involved in the combination.  */
1772
1773       if (insn
1774           && reg_referenced_p (x, PATTERN (insn))
1775           && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1776                                REGNO (x)))
1777         {
1778           struct insn_link *link;
1779
1780           FOR_EACH_LOG_LINK (link, insn)
1781             if (dead_or_set_p (link->insn, x))
1782               break;
1783           if (!link)
1784             {
1785               rsp->nonzero_bits = GET_MODE_MASK (mode);
1786               rsp->sign_bit_copies = 1;
1787               return;
1788             }
1789         }
1790
1791       /* If this is a complex assignment, see if we can convert it into a
1792          simple assignment.  */
1793       set = expand_field_assignment (set);
1794
1795       /* If this is a simple assignment, or we have a paradoxical SUBREG,
1796          set what we know about X.  */
1797
1798       if (SET_DEST (set) == x
1799           || (paradoxical_subreg_p (SET_DEST (set))
1800               && SUBREG_REG (SET_DEST (set)) == x))
1801         update_rsp_from_reg_equal (rsp, insn, set, x);
1802       else
1803         {
1804           rsp->nonzero_bits = GET_MODE_MASK (mode);
1805           rsp->sign_bit_copies = 1;
1806         }
1807     }
1808 }
1809 \f
1810 /* See if INSN can be combined into I3.  PRED, PRED2, SUCC and SUCC2 are
1811    optionally insns that were previously combined into I3 or that will be
1812    combined into the merger of INSN and I3.  The order is PRED, PRED2,
1813    INSN, SUCC, SUCC2, I3.
1814
1815    Return 0 if the combination is not allowed for any reason.
1816
1817    If the combination is allowed, *PDEST will be set to the single
1818    destination of INSN and *PSRC to the single source, and this function
1819    will return 1.  */
1820
1821 static int
1822 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1823                rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1824                rtx *pdest, rtx *psrc)
1825 {
1826   int i;
1827   const_rtx set = 0;
1828   rtx src, dest;
1829   rtx_insn *p;
1830   rtx link;
1831   bool all_adjacent = true;
1832   int (*is_volatile_p) (const_rtx);
1833
1834   if (succ)
1835     {
1836       if (succ2)
1837         {
1838           if (next_active_insn (succ2) != i3)
1839             all_adjacent = false;
1840           if (next_active_insn (succ) != succ2)
1841             all_adjacent = false;
1842         }
1843       else if (next_active_insn (succ) != i3)
1844         all_adjacent = false;
1845       if (next_active_insn (insn) != succ)
1846         all_adjacent = false;
1847     }
1848   else if (next_active_insn (insn) != i3)
1849     all_adjacent = false;
1850     
1851   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1852      or a PARALLEL consisting of such a SET and CLOBBERs.
1853
1854      If INSN has CLOBBER parallel parts, ignore them for our processing.
1855      By definition, these happen during the execution of the insn.  When it
1856      is merged with another insn, all bets are off.  If they are, in fact,
1857      needed and aren't also supplied in I3, they may be added by
1858      recog_for_combine.  Otherwise, it won't match.
1859
1860      We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1861      note.
1862
1863      Get the source and destination of INSN.  If more than one, can't
1864      combine.  */
1865
1866   if (GET_CODE (PATTERN (insn)) == SET)
1867     set = PATTERN (insn);
1868   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1869            && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1870     {
1871       for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1872         {
1873           rtx elt = XVECEXP (PATTERN (insn), 0, i);
1874
1875           switch (GET_CODE (elt))
1876             {
1877             /* This is important to combine floating point insns
1878                for the SH4 port.  */
1879             case USE:
1880               /* Combining an isolated USE doesn't make sense.
1881                  We depend here on combinable_i3pat to reject them.  */
1882               /* The code below this loop only verifies that the inputs of
1883                  the SET in INSN do not change.  We call reg_set_between_p
1884                  to verify that the REG in the USE does not change between
1885                  I3 and INSN.
1886                  If the USE in INSN was for a pseudo register, the matching
1887                  insn pattern will likely match any register; combining this
1888                  with any other USE would only be safe if we knew that the
1889                  used registers have identical values, or if there was
1890                  something to tell them apart, e.g. different modes.  For
1891                  now, we forgo such complicated tests and simply disallow
1892                  combining of USES of pseudo registers with any other USE.  */
1893               if (REG_P (XEXP (elt, 0))
1894                   && GET_CODE (PATTERN (i3)) == PARALLEL)
1895                 {
1896                   rtx i3pat = PATTERN (i3);
1897                   int i = XVECLEN (i3pat, 0) - 1;
1898                   unsigned int regno = REGNO (XEXP (elt, 0));
1899
1900                   do
1901                     {
1902                       rtx i3elt = XVECEXP (i3pat, 0, i);
1903
1904                       if (GET_CODE (i3elt) == USE
1905                           && REG_P (XEXP (i3elt, 0))
1906                           && (REGNO (XEXP (i3elt, 0)) == regno
1907                               ? reg_set_between_p (XEXP (elt, 0),
1908                                                    PREV_INSN (insn), i3)
1909                               : regno >= FIRST_PSEUDO_REGISTER))
1910                         return 0;
1911                     }
1912                   while (--i >= 0);
1913                 }
1914               break;
1915
1916               /* We can ignore CLOBBERs.  */
1917             case CLOBBER:
1918             case CLOBBER_HIGH:
1919               break;
1920
1921             case SET:
1922               /* Ignore SETs whose result isn't used but not those that
1923                  have side-effects.  */
1924               if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1925                   && insn_nothrow_p (insn)
1926                   && !side_effects_p (elt))
1927                 break;
1928
1929               /* If we have already found a SET, this is a second one and
1930                  so we cannot combine with this insn.  */
1931               if (set)
1932                 return 0;
1933
1934               set = elt;
1935               break;
1936
1937             default:
1938               /* Anything else means we can't combine.  */
1939               return 0;
1940             }
1941         }
1942
1943       if (set == 0
1944           /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1945              so don't do anything with it.  */
1946           || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1947         return 0;
1948     }
1949   else
1950     return 0;
1951
1952   if (set == 0)
1953     return 0;
1954
1955   /* The simplification in expand_field_assignment may call back to
1956      get_last_value, so set safe guard here.  */
1957   subst_low_luid = DF_INSN_LUID (insn);
1958
1959   set = expand_field_assignment (set);
1960   src = SET_SRC (set), dest = SET_DEST (set);
1961
1962   /* Do not eliminate user-specified register if it is in an
1963      asm input because we may break the register asm usage defined
1964      in GCC manual if allow to do so.
1965      Be aware that this may cover more cases than we expect but this
1966      should be harmless.  */
1967   if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1968       && extract_asm_operands (PATTERN (i3)))
1969     return 0;
1970
1971   /* Don't eliminate a store in the stack pointer.  */
1972   if (dest == stack_pointer_rtx
1973       /* Don't combine with an insn that sets a register to itself if it has
1974          a REG_EQUAL note.  This may be part of a LIBCALL sequence.  */
1975       || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1976       /* Can't merge an ASM_OPERANDS.  */
1977       || GET_CODE (src) == ASM_OPERANDS
1978       /* Can't merge a function call.  */
1979       || GET_CODE (src) == CALL
1980       /* Don't eliminate a function call argument.  */
1981       || (CALL_P (i3)
1982           && (find_reg_fusage (i3, USE, dest)
1983               || (REG_P (dest)
1984                   && REGNO (dest) < FIRST_PSEUDO_REGISTER
1985                   && global_regs[REGNO (dest)])))
1986       /* Don't substitute into an incremented register.  */
1987       || FIND_REG_INC_NOTE (i3, dest)
1988       || (succ && FIND_REG_INC_NOTE (succ, dest))
1989       || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1990       /* Don't substitute into a non-local goto, this confuses CFG.  */
1991       || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1992       /* Make sure that DEST is not used after INSN but before SUCC, or
1993          after SUCC and before SUCC2, or after SUCC2 but before I3.  */
1994       || (!all_adjacent
1995           && ((succ2
1996                && (reg_used_between_p (dest, succ2, i3)
1997                    || reg_used_between_p (dest, succ, succ2)))
1998               || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1999               || (!succ2 && !succ && reg_used_between_p (dest, insn, i3))
2000               || (succ
2001                   /* SUCC and SUCC2 can be split halves from a PARALLEL; in
2002                      that case SUCC is not in the insn stream, so use SUCC2
2003                      instead for this test.  */
2004                   && reg_used_between_p (dest, insn,
2005                                          succ2
2006                                          && INSN_UID (succ) == INSN_UID (succ2)
2007                                          ? succ2 : succ))))
2008       /* Make sure that the value that is to be substituted for the register
2009          does not use any registers whose values alter in between.  However,
2010          If the insns are adjacent, a use can't cross a set even though we
2011          think it might (this can happen for a sequence of insns each setting
2012          the same destination; last_set of that register might point to
2013          a NOTE).  If INSN has a REG_EQUIV note, the register is always
2014          equivalent to the memory so the substitution is valid even if there
2015          are intervening stores.  Also, don't move a volatile asm or
2016          UNSPEC_VOLATILE across any other insns.  */
2017       || (! all_adjacent
2018           && (((!MEM_P (src)
2019                 || ! find_reg_note (insn, REG_EQUIV, src))
2020                && modified_between_p (src, insn, i3))
2021               || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
2022               || GET_CODE (src) == UNSPEC_VOLATILE))
2023       /* Don't combine across a CALL_INSN, because that would possibly
2024          change whether the life span of some REGs crosses calls or not,
2025          and it is a pain to update that information.
2026          Exception: if source is a constant, moving it later can't hurt.
2027          Accept that as a special case.  */
2028       || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
2029     return 0;
2030
2031   /* DEST must either be a REG or CC0.  */
2032   if (REG_P (dest))
2033     {
2034       /* If register alignment is being enforced for multi-word items in all
2035          cases except for parameters, it is possible to have a register copy
2036          insn referencing a hard register that is not allowed to contain the
2037          mode being copied and which would not be valid as an operand of most
2038          insns.  Eliminate this problem by not combining with such an insn.
2039
2040          Also, on some machines we don't want to extend the life of a hard
2041          register.  */
2042
2043       if (REG_P (src)
2044           && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2045                && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
2046               /* Don't extend the life of a hard register unless it is
2047                  user variable (if we have few registers) or it can't
2048                  fit into the desired register (meaning something special
2049                  is going on).
2050                  Also avoid substituting a return register into I3, because
2051                  reload can't handle a conflict with constraints of other
2052                  inputs.  */
2053               || (REGNO (src) < FIRST_PSEUDO_REGISTER
2054                   && !targetm.hard_regno_mode_ok (REGNO (src),
2055                                                   GET_MODE (src)))))
2056         return 0;
2057     }
2058   else if (GET_CODE (dest) != CC0)
2059     return 0;
2060
2061
2062   if (GET_CODE (PATTERN (i3)) == PARALLEL)
2063     for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2064       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2065         {
2066           rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2067
2068           /* If the clobber represents an earlyclobber operand, we must not
2069              substitute an expression containing the clobbered register.
2070              As we do not analyze the constraint strings here, we have to
2071              make the conservative assumption.  However, if the register is
2072              a fixed hard reg, the clobber cannot represent any operand;
2073              we leave it up to the machine description to either accept or
2074              reject use-and-clobber patterns.  */
2075           if (!REG_P (reg)
2076               || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2077               || !fixed_regs[REGNO (reg)])
2078             if (reg_overlap_mentioned_p (reg, src))
2079               return 0;
2080         }
2081
2082   /* If INSN contains anything volatile, or is an `asm' (whether volatile
2083      or not), reject, unless nothing volatile comes between it and I3 */
2084
2085   if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2086     {
2087       /* Make sure neither succ nor succ2 contains a volatile reference.  */
2088       if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2089         return 0;
2090       if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2091         return 0;
2092       /* We'll check insns between INSN and I3 below.  */
2093     }
2094
2095   /* If INSN is an asm, and DEST is a hard register, reject, since it has
2096      to be an explicit register variable, and was chosen for a reason.  */
2097
2098   if (GET_CODE (src) == ASM_OPERANDS
2099       && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2100     return 0;
2101
2102   /* If INSN contains volatile references (specifically volatile MEMs),
2103      we cannot combine across any other volatile references.
2104      Even if INSN doesn't contain volatile references, any intervening
2105      volatile insn might affect machine state.  */
2106
2107   is_volatile_p = volatile_refs_p (PATTERN (insn))
2108     ? volatile_refs_p
2109     : volatile_insn_p;
2110     
2111   for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2112     if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2113       return 0;
2114
2115   /* If INSN contains an autoincrement or autodecrement, make sure that
2116      register is not used between there and I3, and not already used in
2117      I3 either.  Neither must it be used in PRED or SUCC, if they exist.
2118      Also insist that I3 not be a jump; if it were one
2119      and the incremented register were spilled, we would lose.  */
2120
2121   if (AUTO_INC_DEC)
2122     for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2123       if (REG_NOTE_KIND (link) == REG_INC
2124           && (JUMP_P (i3)
2125               || reg_used_between_p (XEXP (link, 0), insn, i3)
2126               || (pred != NULL_RTX
2127                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2128               || (pred2 != NULL_RTX
2129                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2130               || (succ != NULL_RTX
2131                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2132               || (succ2 != NULL_RTX
2133                   && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2134               || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2135         return 0;
2136
2137   /* Don't combine an insn that follows a CC0-setting insn.
2138      An insn that uses CC0 must not be separated from the one that sets it.
2139      We do, however, allow I2 to follow a CC0-setting insn if that insn
2140      is passed as I1; in that case it will be deleted also.
2141      We also allow combining in this case if all the insns are adjacent
2142      because that would leave the two CC0 insns adjacent as well.
2143      It would be more logical to test whether CC0 occurs inside I1 or I2,
2144      but that would be much slower, and this ought to be equivalent.  */
2145
2146   if (HAVE_cc0)
2147     {
2148       p = prev_nonnote_insn (insn);
2149       if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2150           && ! all_adjacent)
2151         return 0;
2152     }
2153
2154   /* If we get here, we have passed all the tests and the combination is
2155      to be allowed.  */
2156
2157   *pdest = dest;
2158   *psrc = src;
2159
2160   return 1;
2161 }
2162 \f
2163 /* LOC is the location within I3 that contains its pattern or the component
2164    of a PARALLEL of the pattern.  We validate that it is valid for combining.
2165
2166    One problem is if I3 modifies its output, as opposed to replacing it
2167    entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2168    doing so would produce an insn that is not equivalent to the original insns.
2169
2170    Consider:
2171
2172          (set (reg:DI 101) (reg:DI 100))
2173          (set (subreg:SI (reg:DI 101) 0) <foo>)
2174
2175    This is NOT equivalent to:
2176
2177          (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2178                     (set (reg:DI 101) (reg:DI 100))])
2179
2180    Not only does this modify 100 (in which case it might still be valid
2181    if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2182
2183    We can also run into a problem if I2 sets a register that I1
2184    uses and I1 gets directly substituted into I3 (not via I2).  In that
2185    case, we would be getting the wrong value of I2DEST into I3, so we
2186    must reject the combination.  This case occurs when I2 and I1 both
2187    feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2188    If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2189    of a SET must prevent combination from occurring.  The same situation
2190    can occur for I0, in which case I0_NOT_IN_SRC is set.
2191
2192    Before doing the above check, we first try to expand a field assignment
2193    into a set of logical operations.
2194
2195    If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2196    we place a register that is both set and used within I3.  If more than one
2197    such register is detected, we fail.
2198
2199    Return 1 if the combination is valid, zero otherwise.  */
2200
2201 static int
2202 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2203                   int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2204 {
2205   rtx x = *loc;
2206
2207   if (GET_CODE (x) == SET)
2208     {
2209       rtx set = x ;
2210       rtx dest = SET_DEST (set);
2211       rtx src = SET_SRC (set);
2212       rtx inner_dest = dest;
2213       rtx subdest;
2214
2215       while (GET_CODE (inner_dest) == STRICT_LOW_PART
2216              || GET_CODE (inner_dest) == SUBREG
2217              || GET_CODE (inner_dest) == ZERO_EXTRACT)
2218         inner_dest = XEXP (inner_dest, 0);
2219
2220       /* Check for the case where I3 modifies its output, as discussed
2221          above.  We don't want to prevent pseudos from being combined
2222          into the address of a MEM, so only prevent the combination if
2223          i1 or i2 set the same MEM.  */
2224       if ((inner_dest != dest &&
2225            (!MEM_P (inner_dest)
2226             || rtx_equal_p (i2dest, inner_dest)
2227             || (i1dest && rtx_equal_p (i1dest, inner_dest))
2228             || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2229            && (reg_overlap_mentioned_p (i2dest, inner_dest)
2230                || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2231                || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2232
2233           /* This is the same test done in can_combine_p except we can't test
2234              all_adjacent; we don't have to, since this instruction will stay
2235              in place, thus we are not considering increasing the lifetime of
2236              INNER_DEST.
2237
2238              Also, if this insn sets a function argument, combining it with
2239              something that might need a spill could clobber a previous
2240              function argument; the all_adjacent test in can_combine_p also
2241              checks this; here, we do a more specific test for this case.  */
2242
2243           || (REG_P (inner_dest)
2244               && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2245               && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2246                                               GET_MODE (inner_dest)))
2247           || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2248           || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2249         return 0;
2250
2251       /* If DEST is used in I3, it is being killed in this insn, so
2252          record that for later.  We have to consider paradoxical
2253          subregs here, since they kill the whole register, but we
2254          ignore partial subregs, STRICT_LOW_PART, etc.
2255          Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2256          STACK_POINTER_REGNUM, since these are always considered to be
2257          live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
2258       subdest = dest;
2259       if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2260         subdest = SUBREG_REG (subdest);
2261       if (pi3dest_killed
2262           && REG_P (subdest)
2263           && reg_referenced_p (subdest, PATTERN (i3))
2264           && REGNO (subdest) != FRAME_POINTER_REGNUM
2265           && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2266               || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2267           && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2268               || (REGNO (subdest) != ARG_POINTER_REGNUM
2269                   || ! fixed_regs [REGNO (subdest)]))
2270           && REGNO (subdest) != STACK_POINTER_REGNUM)
2271         {
2272           if (*pi3dest_killed)
2273             return 0;
2274
2275           *pi3dest_killed = subdest;
2276         }
2277     }
2278
2279   else if (GET_CODE (x) == PARALLEL)
2280     {
2281       int i;
2282
2283       for (i = 0; i < XVECLEN (x, 0); i++)
2284         if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2285                                 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2286           return 0;
2287     }
2288
2289   return 1;
2290 }
2291 \f
2292 /* Return 1 if X is an arithmetic expression that contains a multiplication
2293    and division.  We don't count multiplications by powers of two here.  */
2294
2295 static int
2296 contains_muldiv (rtx x)
2297 {
2298   switch (GET_CODE (x))
2299     {
2300     case MOD:  case DIV:  case UMOD:  case UDIV:
2301       return 1;
2302
2303     case MULT:
2304       return ! (CONST_INT_P (XEXP (x, 1))
2305                 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2306     default:
2307       if (BINARY_P (x))
2308         return contains_muldiv (XEXP (x, 0))
2309             || contains_muldiv (XEXP (x, 1));
2310
2311       if (UNARY_P (x))
2312         return contains_muldiv (XEXP (x, 0));
2313
2314       return 0;
2315     }
2316 }
2317 \f
2318 /* Determine whether INSN can be used in a combination.  Return nonzero if
2319    not.  This is used in try_combine to detect early some cases where we
2320    can't perform combinations.  */
2321
2322 static int
2323 cant_combine_insn_p (rtx_insn *insn)
2324 {
2325   rtx set;
2326   rtx src, dest;
2327
2328   /* If this isn't really an insn, we can't do anything.
2329      This can occur when flow deletes an insn that it has merged into an
2330      auto-increment address.  */
2331   if (!NONDEBUG_INSN_P (insn))
2332     return 1;
2333
2334   /* Never combine loads and stores involving hard regs that are likely
2335      to be spilled.  The register allocator can usually handle such
2336      reg-reg moves by tying.  If we allow the combiner to make
2337      substitutions of likely-spilled regs, reload might die.
2338      As an exception, we allow combinations involving fixed regs; these are
2339      not available to the register allocator so there's no risk involved.  */
2340
2341   set = single_set (insn);
2342   if (! set)
2343     return 0;
2344   src = SET_SRC (set);
2345   dest = SET_DEST (set);
2346   if (GET_CODE (src) == SUBREG)
2347     src = SUBREG_REG (src);
2348   if (GET_CODE (dest) == SUBREG)
2349     dest = SUBREG_REG (dest);
2350   if (REG_P (src) && REG_P (dest)
2351       && ((HARD_REGISTER_P (src)
2352            && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src)))
2353           || (HARD_REGISTER_P (dest)
2354               && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2355               && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2356     return 1;
2357
2358   return 0;
2359 }
2360
2361 struct likely_spilled_retval_info
2362 {
2363   unsigned regno, nregs;
2364   unsigned mask;
2365 };
2366
2367 /* Called via note_stores by likely_spilled_retval_p.  Remove from info->mask
2368    hard registers that are known to be written to / clobbered in full.  */
2369 static void
2370 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2371 {
2372   struct likely_spilled_retval_info *const info =
2373     (struct likely_spilled_retval_info *) data;
2374   unsigned regno, nregs;
2375   unsigned new_mask;
2376
2377   if (!REG_P (XEXP (set, 0)))
2378     return;
2379   regno = REGNO (x);
2380   if (regno >= info->regno + info->nregs)
2381     return;
2382   nregs = REG_NREGS (x);
2383   if (regno + nregs <= info->regno)
2384     return;
2385   new_mask = (2U << (nregs - 1)) - 1;
2386   if (regno < info->regno)
2387     new_mask >>= info->regno - regno;
2388   else
2389     new_mask <<= regno - info->regno;
2390   info->mask &= ~new_mask;
2391 }
2392
2393 /* Return nonzero iff part of the return value is live during INSN, and
2394    it is likely spilled.  This can happen when more than one insn is needed
2395    to copy the return value, e.g. when we consider to combine into the
2396    second copy insn for a complex value.  */
2397
2398 static int
2399 likely_spilled_retval_p (rtx_insn *insn)
2400 {
2401   rtx_insn *use = BB_END (this_basic_block);
2402   rtx reg;
2403   rtx_insn *p;
2404   unsigned regno, nregs;
2405   /* We assume here that no machine mode needs more than
2406      32 hard registers when the value overlaps with a register
2407      for which TARGET_FUNCTION_VALUE_REGNO_P is true.  */
2408   unsigned mask;
2409   struct likely_spilled_retval_info info;
2410
2411   if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2412     return 0;
2413   reg = XEXP (PATTERN (use), 0);
2414   if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2415     return 0;
2416   regno = REGNO (reg);
2417   nregs = REG_NREGS (reg);
2418   if (nregs == 1)
2419     return 0;
2420   mask = (2U << (nregs - 1)) - 1;
2421
2422   /* Disregard parts of the return value that are set later.  */
2423   info.regno = regno;
2424   info.nregs = nregs;
2425   info.mask = mask;
2426   for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2427     if (INSN_P (p))
2428       note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2429   mask = info.mask;
2430
2431   /* Check if any of the (probably) live return value registers is
2432      likely spilled.  */
2433   nregs --;
2434   do
2435     {
2436       if ((mask & 1 << nregs)
2437           && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2438         return 1;
2439     } while (nregs--);
2440   return 0;
2441 }
2442
2443 /* Adjust INSN after we made a change to its destination.
2444
2445    Changing the destination can invalidate notes that say something about
2446    the results of the insn and a LOG_LINK pointing to the insn.  */
2447
2448 static void
2449 adjust_for_new_dest (rtx_insn *insn)
2450 {
2451   /* For notes, be conservative and simply remove them.  */
2452   remove_reg_equal_equiv_notes (insn);
2453
2454   /* The new insn will have a destination that was previously the destination
2455      of an insn just above it.  Call distribute_links to make a LOG_LINK from
2456      the next use of that destination.  */
2457
2458   rtx set = single_set (insn);
2459   gcc_assert (set);
2460
2461   rtx reg = SET_DEST (set);
2462
2463   while (GET_CODE (reg) == ZERO_EXTRACT
2464          || GET_CODE (reg) == STRICT_LOW_PART
2465          || GET_CODE (reg) == SUBREG)
2466     reg = XEXP (reg, 0);
2467   gcc_assert (REG_P (reg));
2468
2469   distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2470
2471   df_insn_rescan (insn);
2472 }
2473
2474 /* Return TRUE if combine can reuse reg X in mode MODE.
2475    ADDED_SETS is nonzero if the original set is still required.  */
2476 static bool
2477 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2478 {
2479   unsigned int regno;
2480
2481   if (!REG_P (x))
2482     return false;
2483
2484   /* Don't change between modes with different underlying register sizes,
2485      since this could lead to invalid subregs.  */
2486   if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2487                 REGMODE_NATURAL_SIZE (GET_MODE (x))))
2488     return false;
2489
2490   regno = REGNO (x);
2491   /* Allow hard registers if the new mode is legal, and occupies no more
2492      registers than the old mode.  */
2493   if (regno < FIRST_PSEUDO_REGISTER)
2494     return (targetm.hard_regno_mode_ok (regno, mode)
2495             && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2496
2497   /* Or a pseudo that is only used once.  */
2498   return (regno < reg_n_sets_max
2499           && REG_N_SETS (regno) == 1
2500           && !added_sets
2501           && !REG_USERVAR_P (x));
2502 }
2503
2504
2505 /* Check whether X, the destination of a set, refers to part of
2506    the register specified by REG.  */
2507
2508 static bool
2509 reg_subword_p (rtx x, rtx reg)
2510 {
2511   /* Check that reg is an integer mode register.  */
2512   if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2513     return false;
2514
2515   if (GET_CODE (x) == STRICT_LOW_PART
2516       || GET_CODE (x) == ZERO_EXTRACT)
2517     x = XEXP (x, 0);
2518
2519   return GET_CODE (x) == SUBREG
2520          && SUBREG_REG (x) == reg
2521          && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2522 }
2523
2524 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2525    Note that the INSN should be deleted *after* removing dead edges, so
2526    that the kept edge is the fallthrough edge for a (set (pc) (pc))
2527    but not for a (set (pc) (label_ref FOO)).  */
2528
2529 static void
2530 update_cfg_for_uncondjump (rtx_insn *insn)
2531 {
2532   basic_block bb = BLOCK_FOR_INSN (insn);
2533   gcc_assert (BB_END (bb) == insn);
2534
2535   purge_dead_edges (bb);
2536
2537   delete_insn (insn);
2538   if (EDGE_COUNT (bb->succs) == 1)
2539     {
2540       rtx_insn *insn;
2541
2542       single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2543
2544       /* Remove barriers from the footer if there are any.  */
2545       for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2546         if (BARRIER_P (insn))
2547           {
2548             if (PREV_INSN (insn))
2549               SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2550             else
2551               BB_FOOTER (bb) = NEXT_INSN (insn);
2552             if (NEXT_INSN (insn))
2553               SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2554           }
2555         else if (LABEL_P (insn))
2556           break;
2557     }
2558 }
2559
2560 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2561    by an arbitrary number of CLOBBERs.  */
2562 static bool
2563 is_parallel_of_n_reg_sets (rtx pat, int n)
2564 {
2565   if (GET_CODE (pat) != PARALLEL)
2566     return false;
2567
2568   int len = XVECLEN (pat, 0);
2569   if (len < n)
2570     return false;
2571
2572   int i;
2573   for (i = 0; i < n; i++)
2574     if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2575         || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2576       return false;
2577   for ( ; i < len; i++)
2578     switch (GET_CODE (XVECEXP (pat, 0, i)))
2579       {
2580       case CLOBBER:
2581         if (XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2582           return false;
2583         break;
2584       case CLOBBER_HIGH:
2585         break;
2586       default:
2587         return false;
2588       }
2589   return true;
2590 }
2591
2592 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2593    CLOBBERs), can be split into individual SETs in that order, without
2594    changing semantics.  */
2595 static bool
2596 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2597 {
2598   if (!insn_nothrow_p (insn))
2599     return false;
2600
2601   rtx pat = PATTERN (insn);
2602
2603   int i, j;
2604   for (i = 0; i < n; i++)
2605     {
2606       if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2607         return false;
2608
2609       rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2610
2611       for (j = i + 1; j < n; j++)
2612         if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2613           return false;
2614     }
2615
2616   return true;
2617 }
2618
2619 /* Return whether X is just a single set, with the source
2620    a general_operand.  */
2621 static bool
2622 is_just_move (rtx x)
2623 {
2624   if (INSN_P (x))
2625     x = PATTERN (x);
2626
2627   return (GET_CODE (x) == SET && general_operand (SET_SRC (x), VOIDmode));
2628 }
2629
2630 /* Try to combine the insns I0, I1 and I2 into I3.
2631    Here I0, I1 and I2 appear earlier than I3.
2632    I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2633    I3.
2634
2635    If we are combining more than two insns and the resulting insn is not
2636    recognized, try splitting it into two insns.  If that happens, I2 and I3
2637    are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2638    Otherwise, I0, I1 and I2 are pseudo-deleted.
2639
2640    Return 0 if the combination does not work.  Then nothing is changed.
2641    If we did the combination, return the insn at which combine should
2642    resume scanning.
2643
2644    Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2645    new direct jump instruction.
2646
2647    LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2648    been I3 passed to an earlier try_combine within the same basic
2649    block.  */
2650
2651 static rtx_insn *
2652 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2653              int *new_direct_jump_p, rtx_insn *last_combined_insn)
2654 {
2655   /* New patterns for I3 and I2, respectively.  */
2656   rtx newpat, newi2pat = 0;
2657   rtvec newpat_vec_with_clobbers = 0;
2658   int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2659   /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2660      dead.  */
2661   int added_sets_0, added_sets_1, added_sets_2;
2662   /* Total number of SETs to put into I3.  */
2663   int total_sets;
2664   /* Nonzero if I2's or I1's body now appears in I3.  */
2665   int i2_is_used = 0, i1_is_used = 0;
2666   /* INSN_CODEs for new I3, new I2, and user of condition code.  */
2667   int insn_code_number, i2_code_number = 0, other_code_number = 0;
2668   /* Contains I3 if the destination of I3 is used in its source, which means
2669      that the old life of I3 is being killed.  If that usage is placed into
2670      I2 and not in I3, a REG_DEAD note must be made.  */
2671   rtx i3dest_killed = 0;
2672   /* SET_DEST and SET_SRC of I2, I1 and I0.  */
2673   rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2674   /* Copy of SET_SRC of I1 and I0, if needed.  */
2675   rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2676   /* Set if I2DEST was reused as a scratch register.  */
2677   bool i2scratch = false;
2678   /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases.  */
2679   rtx i0pat = 0, i1pat = 0, i2pat = 0;
2680   /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC.  */
2681   int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2682   int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2683   int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2684   int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2685   /* Notes that must be added to REG_NOTES in I3 and I2.  */
2686   rtx new_i3_notes, new_i2_notes;
2687   /* Notes that we substituted I3 into I2 instead of the normal case.  */
2688   int i3_subst_into_i2 = 0;
2689   /* Notes that I1, I2 or I3 is a MULT operation.  */
2690   int have_mult = 0;
2691   int swap_i2i3 = 0;
2692   int split_i2i3 = 0;
2693   int changed_i3_dest = 0;
2694   bool i2_was_move = false, i3_was_move = false;
2695
2696   int maxreg;
2697   rtx_insn *temp_insn;
2698   rtx temp_expr;
2699   struct insn_link *link;
2700   rtx other_pat = 0;
2701   rtx new_other_notes;
2702   int i;
2703   scalar_int_mode dest_mode, temp_mode;
2704
2705   /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2706      never be).  */
2707   if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2708     return 0;
2709
2710   /* Only try four-insn combinations when there's high likelihood of
2711      success.  Look for simple insns, such as loads of constants or
2712      binary operations involving a constant.  */
2713   if (i0)
2714     {
2715       int i;
2716       int ngood = 0;
2717       int nshift = 0;
2718       rtx set0, set3;
2719
2720       if (!flag_expensive_optimizations)
2721         return 0;
2722
2723       for (i = 0; i < 4; i++)
2724         {
2725           rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2726           rtx set = single_set (insn);
2727           rtx src;
2728           if (!set)
2729             continue;
2730           src = SET_SRC (set);
2731           if (CONSTANT_P (src))
2732             {
2733               ngood += 2;
2734               break;
2735             }
2736           else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2737             ngood++;
2738           else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2739                    || GET_CODE (src) == LSHIFTRT)
2740             nshift++;
2741         }
2742
2743       /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2744          are likely manipulating its value.  Ideally we'll be able to combine
2745          all four insns into a bitfield insertion of some kind. 
2746
2747          Note the source in I0 might be inside a sign/zero extension and the
2748          memory modes in I0 and I3 might be different.  So extract the address
2749          from the destination of I3 and search for it in the source of I0.
2750
2751          In the event that there's a match but the source/dest do not actually
2752          refer to the same memory, the worst that happens is we try some
2753          combinations that we wouldn't have otherwise.  */
2754       if ((set0 = single_set (i0))
2755           /* Ensure the source of SET0 is a MEM, possibly buried inside
2756              an extension.  */
2757           && (GET_CODE (SET_SRC (set0)) == MEM
2758               || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2759                    || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2760                   && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2761           && (set3 = single_set (i3))
2762           /* Ensure the destination of SET3 is a MEM.  */
2763           && GET_CODE (SET_DEST (set3)) == MEM
2764           /* Would it be better to extract the base address for the MEM
2765              in SET3 and look for that?  I don't have cases where it matters
2766              but I could envision such cases.  */
2767           && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2768         ngood += 2;
2769
2770       if (ngood < 2 && nshift < 2)
2771         return 0;
2772     }
2773
2774   /* Exit early if one of the insns involved can't be used for
2775      combinations.  */
2776   if (CALL_P (i2)
2777       || (i1 && CALL_P (i1))
2778       || (i0 && CALL_P (i0))
2779       || cant_combine_insn_p (i3)
2780       || cant_combine_insn_p (i2)
2781       || (i1 && cant_combine_insn_p (i1))
2782       || (i0 && cant_combine_insn_p (i0))
2783       || likely_spilled_retval_p (i3))
2784     return 0;
2785
2786   combine_attempts++;
2787   undobuf.other_insn = 0;
2788
2789   /* Reset the hard register usage information.  */
2790   CLEAR_HARD_REG_SET (newpat_used_regs);
2791
2792   if (dump_file && (dump_flags & TDF_DETAILS))
2793     {
2794       if (i0)
2795         fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2796                  INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2797       else if (i1)
2798         fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2799                  INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2800       else
2801         fprintf (dump_file, "\nTrying %d -> %d:\n",
2802                  INSN_UID (i2), INSN_UID (i3));
2803
2804       if (i0)
2805         dump_insn_slim (dump_file, i0);
2806       if (i1)
2807         dump_insn_slim (dump_file, i1);
2808       dump_insn_slim (dump_file, i2);
2809       dump_insn_slim (dump_file, i3);
2810     }
2811
2812   /* If multiple insns feed into one of I2 or I3, they can be in any
2813      order.  To simplify the code below, reorder them in sequence.  */
2814   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2815     std::swap (i0, i2);
2816   if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2817     std::swap (i0, i1);
2818   if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2819     std::swap (i1, i2);
2820
2821   added_links_insn = 0;
2822   added_notes_insn = 0;
2823
2824   /* First check for one important special case that the code below will
2825      not handle.  Namely, the case where I1 is zero, I2 is a PARALLEL
2826      and I3 is a SET whose SET_SRC is a SET_DEST in I2.  In that case,
2827      we may be able to replace that destination with the destination of I3.
2828      This occurs in the common code where we compute both a quotient and
2829      remainder into a structure, in which case we want to do the computation
2830      directly into the structure to avoid register-register copies.
2831
2832      Note that this case handles both multiple sets in I2 and also cases
2833      where I2 has a number of CLOBBERs inside the PARALLEL.
2834
2835      We make very conservative checks below and only try to handle the
2836      most common cases of this.  For example, we only handle the case
2837      where I2 and I3 are adjacent to avoid making difficult register
2838      usage tests.  */
2839
2840   if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2841       && REG_P (SET_SRC (PATTERN (i3)))
2842       && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2843       && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2844       && GET_CODE (PATTERN (i2)) == PARALLEL
2845       && ! side_effects_p (SET_DEST (PATTERN (i3)))
2846       /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2847          below would need to check what is inside (and reg_overlap_mentioned_p
2848          doesn't support those codes anyway).  Don't allow those destinations;
2849          the resulting insn isn't likely to be recognized anyway.  */
2850       && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2851       && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2852       && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2853                                     SET_DEST (PATTERN (i3)))
2854       && next_active_insn (i2) == i3)
2855     {
2856       rtx p2 = PATTERN (i2);
2857
2858       /* Make sure that the destination of I3,
2859          which we are going to substitute into one output of I2,
2860          is not used within another output of I2.  We must avoid making this:
2861          (parallel [(set (mem (reg 69)) ...)
2862                     (set (reg 69) ...)])
2863          which is not well-defined as to order of actions.
2864          (Besides, reload can't handle output reloads for this.)
2865
2866          The problem can also happen if the dest of I3 is a memory ref,
2867          if another dest in I2 is an indirect memory ref.
2868
2869          Neither can this PARALLEL be an asm.  We do not allow combining
2870          that usually (see can_combine_p), so do not here either.  */
2871       bool ok = true;
2872       for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2873         {
2874           if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2875                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER
2876                || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER_HIGH)
2877               && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2878                                           SET_DEST (XVECEXP (p2, 0, i))))
2879             ok = false;
2880           else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2881                    && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2882             ok = false;
2883         }
2884
2885       if (ok)
2886         for (i = 0; i < XVECLEN (p2, 0); i++)
2887           if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2888               && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2889             {
2890               combine_merges++;
2891
2892               subst_insn = i3;
2893               subst_low_luid = DF_INSN_LUID (i2);
2894
2895               added_sets_2 = added_sets_1 = added_sets_0 = 0;
2896               i2src = SET_SRC (XVECEXP (p2, 0, i));
2897               i2dest = SET_DEST (XVECEXP (p2, 0, i));
2898               i2dest_killed = dead_or_set_p (i2, i2dest);
2899
2900               /* Replace the dest in I2 with our dest and make the resulting
2901                  insn the new pattern for I3.  Then skip to where we validate
2902                  the pattern.  Everything was set up above.  */
2903               SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2904               newpat = p2;
2905               i3_subst_into_i2 = 1;
2906               goto validate_replacement;
2907             }
2908     }
2909
2910   /* If I2 is setting a pseudo to a constant and I3 is setting some
2911      sub-part of it to another constant, merge them by making a new
2912      constant.  */
2913   if (i1 == 0
2914       && (temp_expr = single_set (i2)) != 0
2915       && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2916       && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2917       && GET_CODE (PATTERN (i3)) == SET
2918       && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2919       && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2920     {
2921       rtx dest = SET_DEST (PATTERN (i3));
2922       rtx temp_dest = SET_DEST (temp_expr);
2923       int offset = -1;
2924       int width = 0;
2925
2926       if (GET_CODE (dest) == ZERO_EXTRACT)
2927         {
2928           if (CONST_INT_P (XEXP (dest, 1))
2929               && CONST_INT_P (XEXP (dest, 2))
2930               && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2931                                          &dest_mode))
2932             {
2933               width = INTVAL (XEXP (dest, 1));
2934               offset = INTVAL (XEXP (dest, 2));
2935               dest = XEXP (dest, 0);
2936               if (BITS_BIG_ENDIAN)
2937                 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2938             }
2939         }
2940       else
2941         {
2942           if (GET_CODE (dest) == STRICT_LOW_PART)
2943             dest = XEXP (dest, 0);
2944           if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2945             {
2946               width = GET_MODE_PRECISION (dest_mode);
2947               offset = 0;
2948             }
2949         }
2950
2951       if (offset >= 0)
2952         {
2953           /* If this is the low part, we're done.  */
2954           if (subreg_lowpart_p (dest))
2955             ;
2956           /* Handle the case where inner is twice the size of outer.  */
2957           else if (GET_MODE_PRECISION (temp_mode)
2958                    == 2 * GET_MODE_PRECISION (dest_mode))
2959             offset += GET_MODE_PRECISION (dest_mode);
2960           /* Otherwise give up for now.  */
2961           else
2962             offset = -1;
2963         }
2964
2965       if (offset >= 0)
2966         {
2967           rtx inner = SET_SRC (PATTERN (i3));
2968           rtx outer = SET_SRC (temp_expr);
2969
2970           wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2971                                    rtx_mode_t (inner, dest_mode),
2972                                    offset, width);
2973
2974           combine_merges++;
2975           subst_insn = i3;
2976           subst_low_luid = DF_INSN_LUID (i2);
2977           added_sets_2 = added_sets_1 = added_sets_0 = 0;
2978           i2dest = temp_dest;
2979           i2dest_killed = dead_or_set_p (i2, i2dest);
2980
2981           /* Replace the source in I2 with the new constant and make the
2982              resulting insn the new pattern for I3.  Then skip to where we
2983              validate the pattern.  Everything was set up above.  */
2984           SUBST (SET_SRC (temp_expr),
2985                  immed_wide_int_const (o, temp_mode));
2986
2987           newpat = PATTERN (i2);
2988
2989           /* The dest of I3 has been replaced with the dest of I2.  */
2990           changed_i3_dest = 1;
2991           goto validate_replacement;
2992         }
2993     }
2994
2995   /* If we have no I1 and I2 looks like:
2996         (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2997                    (set Y OP)])
2998      make up a dummy I1 that is
2999         (set Y OP)
3000      and change I2 to be
3001         (set (reg:CC X) (compare:CC Y (const_int 0)))
3002
3003      (We can ignore any trailing CLOBBERs.)
3004
3005      This undoes a previous combination and allows us to match a branch-and-
3006      decrement insn.  */
3007
3008   if (!HAVE_cc0 && i1 == 0
3009       && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3010       && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
3011           == MODE_CC)
3012       && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
3013       && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
3014       && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
3015                       SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
3016       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3017       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3018     {
3019       /* We make I1 with the same INSN_UID as I2.  This gives it
3020          the same DF_INSN_LUID for value tracking.  Our fake I1 will
3021          never appear in the insn stream so giving it the same INSN_UID
3022          as I2 will not cause a problem.  */
3023
3024       i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3025                          XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
3026                          -1, NULL_RTX);
3027       INSN_UID (i1) = INSN_UID (i2);
3028
3029       SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
3030       SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
3031              SET_DEST (PATTERN (i1)));
3032       unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
3033       SUBST_LINK (LOG_LINKS (i2),
3034                   alloc_insn_link (i1, regno, LOG_LINKS (i2)));
3035     }
3036
3037   /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
3038      make those two SETs separate I1 and I2 insns, and make an I0 that is
3039      the original I1.  */
3040   if (!HAVE_cc0 && i0 == 0
3041       && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3042       && can_split_parallel_of_n_reg_sets (i2, 2)
3043       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3044       && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
3045       && !reg_set_between_p  (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3046       && !reg_set_between_p  (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3047     {
3048       /* If there is no I1, there is no I0 either.  */
3049       i0 = i1;
3050
3051       /* We make I1 with the same INSN_UID as I2.  This gives it
3052          the same DF_INSN_LUID for value tracking.  Our fake I1 will
3053          never appear in the insn stream so giving it the same INSN_UID
3054          as I2 will not cause a problem.  */
3055
3056       i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3057                          XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
3058                          -1, NULL_RTX);
3059       INSN_UID (i1) = INSN_UID (i2);
3060
3061       SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
3062     }
3063
3064   /* Verify that I2 and maybe I1 and I0 can be combined into I3.  */
3065   if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
3066     {
3067       if (dump_file)
3068         fprintf (dump_file, "Can't combine i2 into i3\n");
3069       undo_all ();
3070       return 0;
3071     }
3072   if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
3073     {
3074       if (dump_file)
3075         fprintf (dump_file, "Can't combine i1 into i3\n");
3076       undo_all ();
3077       return 0;
3078     }
3079   if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
3080     {
3081       if (dump_file)
3082         fprintf (dump_file, "Can't combine i0 into i3\n");
3083       undo_all ();
3084       return 0;
3085     }
3086
3087   /* Record whether i2 and i3 are trivial moves.  */
3088   i2_was_move = is_just_move (i2);
3089   i3_was_move = is_just_move (i3);
3090
3091   /* Record whether I2DEST is used in I2SRC and similarly for the other
3092      cases.  Knowing this will help in register status updating below.  */
3093   i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3094   i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3095   i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3096   i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3097   i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3098   i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3099   i2dest_killed = dead_or_set_p (i2, i2dest);
3100   i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3101   i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3102
3103   /* For the earlier insns, determine which of the subsequent ones they
3104      feed.  */
3105   i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3106   i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3107   i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3108                           : (!reg_overlap_mentioned_p (i1dest, i0dest)
3109                              && reg_overlap_mentioned_p (i0dest, i2src))));
3110
3111   /* Ensure that I3's pattern can be the destination of combines.  */
3112   if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3113                           i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3114                           i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3115                                  || (i1dest_in_i0src && !i0_feeds_i1_n)),
3116                           &i3dest_killed))
3117     {
3118       undo_all ();
3119       return 0;
3120     }
3121
3122   /* See if any of the insns is a MULT operation.  Unless one is, we will
3123      reject a combination that is, since it must be slower.  Be conservative
3124      here.  */
3125   if (GET_CODE (i2src) == MULT
3126       || (i1 != 0 && GET_CODE (i1src) == MULT)
3127       || (i0 != 0 && GET_CODE (i0src) == MULT)
3128       || (GET_CODE (PATTERN (i3)) == SET
3129           && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3130     have_mult = 1;
3131
3132   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3133      We used to do this EXCEPT in one case: I3 has a post-inc in an
3134      output operand.  However, that exception can give rise to insns like
3135         mov r3,(r3)+
3136      which is a famous insn on the PDP-11 where the value of r3 used as the
3137      source was model-dependent.  Avoid this sort of thing.  */
3138
3139 #if 0
3140   if (!(GET_CODE (PATTERN (i3)) == SET
3141         && REG_P (SET_SRC (PATTERN (i3)))
3142         && MEM_P (SET_DEST (PATTERN (i3)))
3143         && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3144             || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3145     /* It's not the exception.  */
3146 #endif
3147     if (AUTO_INC_DEC)
3148       {
3149         rtx link;
3150         for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3151           if (REG_NOTE_KIND (link) == REG_INC
3152               && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3153                   || (i1 != 0
3154                       && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3155             {
3156               undo_all ();
3157               return 0;
3158             }
3159       }
3160
3161   /* See if the SETs in I1 or I2 need to be kept around in the merged
3162      instruction: whenever the value set there is still needed past I3.
3163      For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3164
3165      For the SET in I1, we have two cases: if I1 and I2 independently feed
3166      into I3, the set in I1 needs to be kept around unless I1DEST dies
3167      or is set in I3.  Otherwise (if I1 feeds I2 which feeds I3), the set
3168      in I1 needs to be kept around unless I1DEST dies or is set in either
3169      I2 or I3.  The same considerations apply to I0.  */
3170
3171   added_sets_2 = !dead_or_set_p (i3, i2dest);
3172
3173   if (i1)
3174     added_sets_1 = !(dead_or_set_p (i3, i1dest)
3175                      || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3176   else
3177     added_sets_1 = 0;
3178
3179   if (i0)
3180     added_sets_0 =  !(dead_or_set_p (i3, i0dest)
3181                       || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3182                       || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3183                           && dead_or_set_p (i2, i0dest)));
3184   else
3185     added_sets_0 = 0;
3186
3187   /* We are about to copy insns for the case where they need to be kept
3188      around.  Check that they can be copied in the merged instruction.  */
3189
3190   if (targetm.cannot_copy_insn_p
3191       && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3192           || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3193           || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3194     {
3195       undo_all ();
3196       return 0;
3197     }
3198
3199   /* If the set in I2 needs to be kept around, we must make a copy of
3200      PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3201      PATTERN (I2), we are only substituting for the original I1DEST, not into
3202      an already-substituted copy.  This also prevents making self-referential
3203      rtx.  If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3204      I2DEST.  */
3205
3206   if (added_sets_2)
3207     {
3208       if (GET_CODE (PATTERN (i2)) == PARALLEL)
3209         i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3210       else
3211         i2pat = copy_rtx (PATTERN (i2));
3212     }
3213
3214   if (added_sets_1)
3215     {
3216       if (GET_CODE (PATTERN (i1)) == PARALLEL)
3217         i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3218       else
3219         i1pat = copy_rtx (PATTERN (i1));
3220     }
3221
3222   if (added_sets_0)
3223     {
3224       if (GET_CODE (PATTERN (i0)) == PARALLEL)
3225         i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3226       else
3227         i0pat = copy_rtx (PATTERN (i0));
3228     }
3229
3230   combine_merges++;
3231
3232   /* Substitute in the latest insn for the regs set by the earlier ones.  */
3233
3234   maxreg = max_reg_num ();
3235
3236   subst_insn = i3;
3237
3238   /* Many machines that don't use CC0 have insns that can both perform an
3239      arithmetic operation and set the condition code.  These operations will
3240      be represented as a PARALLEL with the first element of the vector
3241      being a COMPARE of an arithmetic operation with the constant zero.
3242      The second element of the vector will set some pseudo to the result
3243      of the same arithmetic operation.  If we simplify the COMPARE, we won't
3244      match such a pattern and so will generate an extra insn.   Here we test
3245      for this case, where both the comparison and the operation result are
3246      needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3247      I2SRC.  Later we will make the PARALLEL that contains I2.  */
3248
3249   if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3250       && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3251       && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3252       && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3253     {
3254       rtx newpat_dest;
3255       rtx *cc_use_loc = NULL;
3256       rtx_insn *cc_use_insn = NULL;
3257       rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3258       machine_mode compare_mode, orig_compare_mode;
3259       enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3260       scalar_int_mode mode;
3261
3262       newpat = PATTERN (i3);
3263       newpat_dest = SET_DEST (newpat);
3264       compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3265
3266       if (undobuf.other_insn == 0
3267           && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3268                                             &cc_use_insn)))
3269         {
3270           compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3271           if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3272             compare_code = simplify_compare_const (compare_code, mode,
3273                                                    op0, &op1);
3274           target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3275         }
3276
3277       /* Do the rest only if op1 is const0_rtx, which may be the
3278          result of simplification.  */
3279       if (op1 == const0_rtx)
3280         {
3281           /* If a single use of the CC is found, prepare to modify it
3282              when SELECT_CC_MODE returns a new CC-class mode, or when
3283              the above simplify_compare_const() returned a new comparison
3284              operator.  undobuf.other_insn is assigned the CC use insn
3285              when modifying it.  */
3286           if (cc_use_loc)
3287             {
3288 #ifdef SELECT_CC_MODE
3289               machine_mode new_mode
3290                 = SELECT_CC_MODE (compare_code, op0, op1);
3291               if (new_mode != orig_compare_mode
3292                   && can_change_dest_mode (SET_DEST (newpat),
3293                                            added_sets_2, new_mode))
3294                 {
3295                   unsigned int regno = REGNO (newpat_dest);
3296                   compare_mode = new_mode;
3297                   if (regno < FIRST_PSEUDO_REGISTER)
3298                     newpat_dest = gen_rtx_REG (compare_mode, regno);
3299                   else
3300                     {
3301                       SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3302                       newpat_dest = regno_reg_rtx[regno];
3303                     }
3304                 }
3305 #endif
3306               /* Cases for modifying the CC-using comparison.  */
3307               if (compare_code != orig_compare_code
3308                   /* ??? Do we need to verify the zero rtx?  */
3309                   && XEXP (*cc_use_loc, 1) == const0_rtx)
3310                 {
3311                   /* Replace cc_use_loc with entire new RTX.  */
3312                   SUBST (*cc_use_loc,
3313                          gen_rtx_fmt_ee (compare_code, GET_MODE (*cc_use_loc),
3314                                          newpat_dest, const0_rtx));
3315                   undobuf.other_insn = cc_use_insn;
3316                 }
3317               else if (compare_mode != orig_compare_mode)
3318                 {
3319                   /* Just replace the CC reg with a new mode.  */
3320                   SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3321                   undobuf.other_insn = cc_use_insn;
3322                 }
3323             }
3324
3325           /* Now we modify the current newpat:
3326              First, SET_DEST(newpat) is updated if the CC mode has been
3327              altered. For targets without SELECT_CC_MODE, this should be
3328              optimized away.  */
3329           if (compare_mode != orig_compare_mode)
3330             SUBST (SET_DEST (newpat), newpat_dest);
3331           /* This is always done to propagate i2src into newpat.  */
3332           SUBST (SET_SRC (newpat),
3333                  gen_rtx_COMPARE (compare_mode, op0, op1));
3334           /* Create new version of i2pat if needed; the below PARALLEL
3335              creation needs this to work correctly.  */
3336           if (! rtx_equal_p (i2src, op0))
3337             i2pat = gen_rtx_SET (i2dest, op0);
3338           i2_is_used = 1;
3339         }
3340     }
3341
3342   if (i2_is_used == 0)
3343     {
3344       /* It is possible that the source of I2 or I1 may be performing
3345          an unneeded operation, such as a ZERO_EXTEND of something
3346          that is known to have the high part zero.  Handle that case
3347          by letting subst look at the inner insns.
3348
3349          Another way to do this would be to have a function that tries
3350          to simplify a single insn instead of merging two or more
3351          insns.  We don't do this because of the potential of infinite
3352          loops and because of the potential extra memory required.
3353          However, doing it the way we are is a bit of a kludge and
3354          doesn't catch all cases.
3355
3356          But only do this if -fexpensive-optimizations since it slows
3357          things down and doesn't usually win.
3358
3359          This is not done in the COMPARE case above because the
3360          unmodified I2PAT is used in the PARALLEL and so a pattern
3361          with a modified I2SRC would not match.  */
3362
3363       if (flag_expensive_optimizations)
3364         {
3365           /* Pass pc_rtx so no substitutions are done, just
3366              simplifications.  */
3367           if (i1)
3368             {
3369               subst_low_luid = DF_INSN_LUID (i1);
3370               i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3371             }
3372
3373           subst_low_luid = DF_INSN_LUID (i2);
3374           i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3375         }
3376
3377       n_occurrences = 0;                /* `subst' counts here */
3378       subst_low_luid = DF_INSN_LUID (i2);
3379
3380       /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3381          copy of I2SRC each time we substitute it, in order to avoid creating
3382          self-referential RTL when we will be substituting I1SRC for I1DEST
3383          later.  Likewise if I0 feeds into I2, either directly or indirectly
3384          through I1, and I0DEST is in I0SRC.  */
3385       newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3386                       (i1_feeds_i2_n && i1dest_in_i1src)
3387                       || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3388                           && i0dest_in_i0src));
3389       substed_i2 = 1;
3390
3391       /* Record whether I2's body now appears within I3's body.  */
3392       i2_is_used = n_occurrences;
3393     }
3394
3395   /* If we already got a failure, don't try to do more.  Otherwise, try to
3396      substitute I1 if we have it.  */
3397
3398   if (i1 && GET_CODE (newpat) != CLOBBER)
3399     {
3400       /* Check that an autoincrement side-effect on I1 has not been lost.
3401          This happens if I1DEST is mentioned in I2 and dies there, and
3402          has disappeared from the new pattern.  */
3403       if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3404            && i1_feeds_i2_n
3405            && dead_or_set_p (i2, i1dest)
3406            && !reg_overlap_mentioned_p (i1dest, newpat))
3407            /* Before we can do this substitution, we must redo the test done
3408               above (see detailed comments there) that ensures I1DEST isn't
3409               mentioned in any SETs in NEWPAT that are field assignments.  */
3410           || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3411                                 0, 0, 0))
3412         {
3413           undo_all ();
3414           return 0;
3415         }
3416
3417       n_occurrences = 0;
3418       subst_low_luid = DF_INSN_LUID (i1);
3419
3420       /* If the following substitution will modify I1SRC, make a copy of it
3421          for the case where it is substituted for I1DEST in I2PAT later.  */
3422       if (added_sets_2 && i1_feeds_i2_n)
3423         i1src_copy = copy_rtx (i1src);
3424
3425       /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3426          copy of I1SRC each time we substitute it, in order to avoid creating
3427          self-referential RTL when we will be substituting I0SRC for I0DEST
3428          later.  */
3429       newpat = subst (newpat, i1dest, i1src, 0, 0,
3430                       i0_feeds_i1_n && i0dest_in_i0src);
3431       substed_i1 = 1;
3432
3433       /* Record whether I1's body now appears within I3's body.  */
3434       i1_is_used = n_occurrences;
3435     }
3436
3437   /* Likewise for I0 if we have it.  */
3438
3439   if (i0 && GET_CODE (newpat) != CLOBBER)
3440     {
3441       if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3442            && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3443                || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3444            && !reg_overlap_mentioned_p (i0dest, newpat))
3445           || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3446                                 0, 0, 0))
3447         {
3448           undo_all ();
3449           return 0;
3450         }
3451
3452       /* If the following substitution will modify I0SRC, make a copy of it
3453          for the case where it is substituted for I0DEST in I1PAT later.  */
3454       if (added_sets_1 && i0_feeds_i1_n)
3455         i0src_copy = copy_rtx (i0src);
3456       /* And a copy for I0DEST in I2PAT substitution.  */
3457       if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3458                            || (i0_feeds_i2_n)))
3459         i0src_copy2 = copy_rtx (i0src);
3460
3461       n_occurrences = 0;
3462       subst_low_luid = DF_INSN_LUID (i0);
3463       newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3464       substed_i0 = 1;
3465     }
3466
3467   /* Fail if an autoincrement side-effect has been duplicated.  Be careful
3468      to count all the ways that I2SRC and I1SRC can be used.  */
3469   if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3470        && i2_is_used + added_sets_2 > 1)
3471       || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3472           && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3473               > 1))
3474       || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3475           && (n_occurrences + added_sets_0
3476               + (added_sets_1 && i0_feeds_i1_n)
3477               + (added_sets_2 && i0_feeds_i2_n)
3478               > 1))
3479       /* Fail if we tried to make a new register.  */
3480       || max_reg_num () != maxreg
3481       /* Fail if we couldn't do something and have a CLOBBER.  */
3482       || GET_CODE (newpat) == CLOBBER
3483       /* Fail if this new pattern is a MULT and we didn't have one before
3484          at the outer level.  */
3485       || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3486           && ! have_mult))
3487     {
3488       undo_all ();
3489       return 0;
3490     }
3491
3492   /* If the actions of the earlier insns must be kept
3493      in addition to substituting them into the latest one,
3494      we must make a new PARALLEL for the latest insn
3495      to hold additional the SETs.  */
3496
3497   if (added_sets_0 || added_sets_1 || added_sets_2)
3498     {
3499       int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3500       combine_extras++;
3501
3502       if (GET_CODE (newpat) == PARALLEL)
3503         {
3504           rtvec old = XVEC (newpat, 0);
3505           total_sets = XVECLEN (newpat, 0) + extra_sets;
3506           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3507           memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3508                   sizeof (old->elem[0]) * old->num_elem);
3509         }
3510       else
3511         {
3512           rtx old = newpat;
3513           total_sets = 1 + extra_sets;
3514           newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3515           XVECEXP (newpat, 0, 0) = old;
3516         }
3517
3518       if (added_sets_0)
3519         XVECEXP (newpat, 0, --total_sets) = i0pat;
3520
3521       if (added_sets_1)
3522         {
3523           rtx t = i1pat;
3524           if (i0_feeds_i1_n)
3525             t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3526
3527           XVECEXP (newpat, 0, --total_sets) = t;
3528         }
3529       if (added_sets_2)
3530         {
3531           rtx t = i2pat;
3532           if (i1_feeds_i2_n)
3533             t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3534                        i0_feeds_i1_n && i0dest_in_i0src);
3535           if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3536             t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3537
3538           XVECEXP (newpat, 0, --total_sets) = t;
3539         }
3540     }
3541
3542  validate_replacement:
3543
3544   /* Note which hard regs this insn has as inputs.  */
3545   mark_used_regs_combine (newpat);
3546
3547   /* If recog_for_combine fails, it strips existing clobbers.  If we'll
3548      consider splitting this pattern, we might need these clobbers.  */
3549   if (i1 && GET_CODE (newpat) == PARALLEL
3550       && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3551     {
3552       int len = XVECLEN (newpat, 0);
3553
3554       newpat_vec_with_clobbers = rtvec_alloc (len);
3555       for (i = 0; i < len; i++)
3556         RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3557     }
3558
3559   /* We have recognized nothing yet.  */
3560   insn_code_number = -1;
3561
3562   /* See if this is a PARALLEL of two SETs where one SET's destination is
3563      a register that is unused and this isn't marked as an instruction that
3564      might trap in an EH region.  In that case, we just need the other SET.
3565      We prefer this over the PARALLEL.
3566
3567      This can occur when simplifying a divmod insn.  We *must* test for this
3568      case here because the code below that splits two independent SETs doesn't
3569      handle this case correctly when it updates the register status.
3570
3571      It's pointless doing this if we originally had two sets, one from
3572      i3, and one from i2.  Combining then splitting the parallel results
3573      in the original i2 again plus an invalid insn (which we delete).
3574      The net effect is only to move instructions around, which makes
3575      debug info less accurate.
3576
3577      If the remaining SET came from I2 its destination should not be used
3578      between I2 and I3.  See PR82024.  */
3579
3580   if (!(added_sets_2 && i1 == 0)
3581       && is_parallel_of_n_reg_sets (newpat, 2)
3582       && asm_noperands (newpat) < 0)
3583     {
3584       rtx set0 = XVECEXP (newpat, 0, 0);
3585       rtx set1 = XVECEXP (newpat, 0, 1);
3586       rtx oldpat = newpat;
3587
3588       if (((REG_P (SET_DEST (set1))
3589             && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3590            || (GET_CODE (SET_DEST (set1)) == SUBREG
3591                && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3592           && insn_nothrow_p (i3)
3593           && !side_effects_p (SET_SRC (set1)))
3594         {
3595           newpat = set0;
3596           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3597         }
3598
3599       else if (((REG_P (SET_DEST (set0))
3600                  && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3601                 || (GET_CODE (SET_DEST (set0)) == SUBREG
3602                     && find_reg_note (i3, REG_UNUSED,
3603                                       SUBREG_REG (SET_DEST (set0)))))
3604                && insn_nothrow_p (i3)
3605                && !side_effects_p (SET_SRC (set0)))
3606         {
3607           rtx dest = SET_DEST (set1);
3608           if (GET_CODE (dest) == SUBREG)
3609             dest = SUBREG_REG (dest);
3610           if (!reg_used_between_p (dest, i2, i3))
3611             {
3612               newpat = set1;
3613               insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3614
3615               if (insn_code_number >= 0)
3616                 changed_i3_dest = 1;
3617             }
3618         }
3619
3620       if (insn_code_number < 0)
3621         newpat = oldpat;
3622     }
3623
3624   /* Is the result of combination a valid instruction?  */
3625   if (insn_code_number < 0)
3626     insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3627
3628   /* If we were combining three insns and the result is a simple SET
3629      with no ASM_OPERANDS that wasn't recognized, try to split it into two
3630      insns.  There are two ways to do this.  It can be split using a
3631      machine-specific method (like when you have an addition of a large
3632      constant) or by combine in the function find_split_point.  */
3633
3634   if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3635       && asm_noperands (newpat) < 0)
3636     {
3637       rtx parallel, *split;
3638       rtx_insn *m_split_insn;
3639
3640       /* See if the MD file can split NEWPAT.  If it can't, see if letting it
3641          use I2DEST as a scratch register will help.  In the latter case,
3642          convert I2DEST to the mode of the source of NEWPAT if we can.  */
3643
3644       m_split_insn = combine_split_insns (newpat, i3);
3645
3646       /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3647          inputs of NEWPAT.  */
3648
3649       /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3650          possible to try that as a scratch reg.  This would require adding
3651          more code to make it work though.  */
3652
3653       if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3654         {
3655           machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3656
3657           /* ??? Reusing i2dest without resetting the reg_stat entry for it
3658              (temporarily, until we are committed to this instruction
3659              combination) does not work: for example, any call to nonzero_bits
3660              on the register (from a splitter in the MD file, for example)
3661              will get the old information, which is invalid.
3662
3663              Since nowadays we can create registers during combine just fine,
3664              we should just create a new one here, not reuse i2dest.  */
3665
3666           /* First try to split using the original register as a
3667              scratch register.  */
3668           parallel = gen_rtx_PARALLEL (VOIDmode,
3669                                        gen_rtvec (2, newpat,
3670                                                   gen_rtx_CLOBBER (VOIDmode,
3671                                                                    i2dest)));
3672           m_split_insn = combine_split_insns (parallel, i3);
3673
3674           /* If that didn't work, try changing the mode of I2DEST if
3675              we can.  */
3676           if (m_split_insn == 0
3677               && new_mode != GET_MODE (i2dest)
3678               && new_mode != VOIDmode
3679               && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3680             {
3681               machine_mode old_mode = GET_MODE (i2dest);
3682               rtx ni2dest;
3683
3684               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3685                 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3686               else
3687                 {
3688                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3689                   ni2dest = regno_reg_rtx[REGNO (i2dest)];
3690                 }
3691
3692               parallel = (gen_rtx_PARALLEL
3693                           (VOIDmode,
3694                            gen_rtvec (2, newpat,
3695                                       gen_rtx_CLOBBER (VOIDmode,
3696                                                        ni2dest))));
3697               m_split_insn = combine_split_insns (parallel, i3);
3698
3699               if (m_split_insn == 0
3700                   && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3701                 {
3702                   struct undo *buf;
3703
3704                   adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3705                   buf = undobuf.undos;
3706                   undobuf.undos = buf->next;
3707                   buf->next = undobuf.frees;
3708                   undobuf.frees = buf;
3709                 }
3710             }
3711
3712           i2scratch = m_split_insn != 0;
3713         }
3714
3715       /* If recog_for_combine has discarded clobbers, try to use them
3716          again for the split.  */
3717       if (m_split_insn == 0 && newpat_vec_with_clobbers)
3718         {
3719           parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3720           m_split_insn = combine_split_insns (parallel, i3);
3721         }
3722
3723       if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3724         {
3725           rtx m_split_pat = PATTERN (m_split_insn);
3726           insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3727           if (insn_code_number >= 0)
3728             newpat = m_split_pat;
3729         }
3730       else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3731                && (next_nonnote_nondebug_insn (i2) == i3
3732                    || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3733         {
3734           rtx i2set, i3set;
3735           rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3736           newi2pat = PATTERN (m_split_insn);
3737
3738           i3set = single_set (NEXT_INSN (m_split_insn));
3739           i2set = single_set (m_split_insn);
3740
3741           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3742
3743           /* If I2 or I3 has multiple SETs, we won't know how to track
3744              register status, so don't use these insns.  If I2's destination
3745              is used between I2 and I3, we also can't use these insns.  */
3746
3747           if (i2_code_number >= 0 && i2set && i3set
3748               && (next_nonnote_nondebug_insn (i2) == i3
3749                   || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3750             insn_code_number = recog_for_combine (&newi3pat, i3,
3751                                                   &new_i3_notes);
3752           if (insn_code_number >= 0)
3753             newpat = newi3pat;
3754
3755           /* It is possible that both insns now set the destination of I3.
3756              If so, we must show an extra use of it.  */
3757
3758           if (insn_code_number >= 0)
3759             {
3760               rtx new_i3_dest = SET_DEST (i3set);
3761               rtx new_i2_dest = SET_DEST (i2set);
3762
3763               while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3764                      || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3765                      || GET_CODE (new_i3_dest) == SUBREG)
3766                 new_i3_dest = XEXP (new_i3_dest, 0);
3767
3768               while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3769                      || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3770                      || GET_CODE (new_i2_dest) == SUBREG)
3771                 new_i2_dest = XEXP (new_i2_dest, 0);
3772
3773               if (REG_P (new_i3_dest)
3774                   && REG_P (new_i2_dest)
3775                   && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3776                   && REGNO (new_i2_dest) < reg_n_sets_max)
3777                 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3778             }
3779         }
3780
3781       /* If we can split it and use I2DEST, go ahead and see if that
3782          helps things be recognized.  Verify that none of the registers
3783          are set between I2 and I3.  */
3784       if (insn_code_number < 0
3785           && (split = find_split_point (&newpat, i3, false)) != 0
3786           && (!HAVE_cc0 || REG_P (i2dest))
3787           /* We need I2DEST in the proper mode.  If it is a hard register
3788              or the only use of a pseudo, we can change its mode.
3789              Make sure we don't change a hard register to have a mode that
3790              isn't valid for it, or change the number of registers.  */
3791           && (GET_MODE (*split) == GET_MODE (i2dest)
3792               || GET_MODE (*split) == VOIDmode
3793               || can_change_dest_mode (i2dest, added_sets_2,
3794                                        GET_MODE (*split)))
3795           && (next_nonnote_nondebug_insn (i2) == i3
3796               || !modified_between_p (*split, i2, i3))
3797           /* We can't overwrite I2DEST if its value is still used by
3798              NEWPAT.  */
3799           && ! reg_referenced_p (i2dest, newpat))
3800         {
3801           rtx newdest = i2dest;
3802           enum rtx_code split_code = GET_CODE (*split);
3803           machine_mode split_mode = GET_MODE (*split);
3804           bool subst_done = false;
3805           newi2pat = NULL_RTX;
3806
3807           i2scratch = true;
3808
3809           /* *SPLIT may be part of I2SRC, so make sure we have the
3810              original expression around for later debug processing.
3811              We should not need I2SRC any more in other cases.  */
3812           if (MAY_HAVE_DEBUG_BIND_INSNS)
3813             i2src = copy_rtx (i2src);
3814           else
3815             i2src = NULL;
3816
3817           /* Get NEWDEST as a register in the proper mode.  We have already
3818              validated that we can do this.  */
3819           if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3820             {
3821               if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3822                 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3823               else
3824                 {
3825                   SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3826                   newdest = regno_reg_rtx[REGNO (i2dest)];
3827                 }
3828             }
3829
3830           /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3831              an ASHIFT.  This can occur if it was inside a PLUS and hence
3832              appeared to be a memory address.  This is a kludge.  */
3833           if (split_code == MULT
3834               && CONST_INT_P (XEXP (*split, 1))
3835               && INTVAL (XEXP (*split, 1)) > 0
3836               && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3837             {
3838               rtx i_rtx = gen_int_shift_amount (split_mode, i);
3839               SUBST (*split, gen_rtx_ASHIFT (split_mode,
3840                                              XEXP (*split, 0), i_rtx));
3841               /* Update split_code because we may not have a multiply
3842                  anymore.  */
3843               split_code = GET_CODE (*split);
3844             }
3845
3846           /* Similarly for (plus (mult FOO (const_int pow2))).  */
3847           if (split_code == PLUS
3848               && GET_CODE (XEXP (*split, 0)) == MULT
3849               && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3850               && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3851               && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3852             {
3853               rtx nsplit = XEXP (*split, 0);
3854               rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3855               SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3856                                                        XEXP (nsplit, 0),
3857                                                        i_rtx));
3858               /* Update split_code because we may not have a multiply
3859                  anymore.  */
3860               split_code = GET_CODE (*split);
3861             }
3862
3863 #ifdef INSN_SCHEDULING
3864           /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3865              be written as a ZERO_EXTEND.  */
3866           if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3867             {
3868               /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3869                  what it really is.  */
3870               if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3871                   == SIGN_EXTEND)
3872                 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3873                                                     SUBREG_REG (*split)));
3874               else
3875                 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3876                                                     SUBREG_REG (*split)));
3877             }
3878 #endif
3879
3880           /* Attempt to split binary operators using arithmetic identities.  */
3881           if (BINARY_P (SET_SRC (newpat))
3882               && split_mode == GET_MODE (SET_SRC (newpat))
3883               && ! side_effects_p (SET_SRC (newpat)))
3884             {
3885               rtx setsrc = SET_SRC (newpat);
3886               machine_mode mode = GET_MODE (setsrc);
3887               enum rtx_code code = GET_CODE (setsrc);
3888               rtx src_op0 = XEXP (setsrc, 0);
3889               rtx src_op1 = XEXP (setsrc, 1);
3890
3891               /* Split "X = Y op Y" as "Z = Y; X = Z op Z".  */
3892               if (rtx_equal_p (src_op0, src_op1))
3893                 {
3894                   newi2pat = gen_rtx_SET (newdest, src_op0);
3895                   SUBST (XEXP (setsrc, 0), newdest);
3896                   SUBST (XEXP (setsrc, 1), newdest);
3897                   subst_done = true;
3898                 }
3899               /* Split "((P op Q) op R) op S" where op is PLUS or MULT.  */
3900               else if ((code == PLUS || code == MULT)
3901                        && GET_CODE (src_op0) == code
3902                        && GET_CODE (XEXP (src_op0, 0)) == code
3903                        && (INTEGRAL_MODE_P (mode)
3904                            || (FLOAT_MODE_P (mode)
3905                                && flag_unsafe_math_optimizations)))
3906                 {
3907                   rtx p = XEXP (XEXP (src_op0, 0), 0);
3908                   rtx q = XEXP (XEXP (src_op0, 0), 1);
3909                   rtx r = XEXP (src_op0, 1);
3910                   rtx s = src_op1;
3911
3912                   /* Split both "((X op Y) op X) op Y" and
3913                      "((X op Y) op Y) op X" as "T op T" where T is
3914                      "X op Y".  */
3915                   if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3916                        || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3917                     {
3918                       newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3919                       SUBST (XEXP (setsrc, 0), newdest);
3920                       SUBST (XEXP (setsrc, 1), newdest);
3921                       subst_done = true;
3922                     }
3923                   /* Split "((X op X) op Y) op Y)" as "T op T" where
3924                      T is "X op Y".  */
3925                   else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3926                     {
3927                       rtx tmp = simplify_gen_binary (code, mode, p, r);
3928                       newi2pat = gen_rtx_SET (newdest, tmp);
3929                       SUBST (XEXP (setsrc, 0), newdest);
3930                       SUBST (XEXP (setsrc, 1), newdest);
3931                       subst_done = true;
3932                     }
3933                 }
3934             }
3935
3936           if (!subst_done)
3937             {
3938               newi2pat = gen_rtx_SET (newdest, *split);
3939               SUBST (*split, newdest);
3940             }
3941
3942           i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3943
3944           /* recog_for_combine might have added CLOBBERs to newi2pat.
3945              Make sure NEWPAT does not depend on the clobbered regs.  */
3946           if (GET_CODE (newi2pat) == PARALLEL)
3947             for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3948               if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3949                 {
3950                   rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3951                   if (reg_overlap_mentioned_p (reg, newpat))
3952                     {
3953                       undo_all ();
3954                       return 0;
3955                     }
3956                 }
3957
3958           /* If the split point was a MULT and we didn't have one before,
3959              don't use one now.  */
3960           if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3961             insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3962         }
3963     }
3964
3965   /* Check for a case where we loaded from memory in a narrow mode and
3966      then sign extended it, but we need both registers.  In that case,
3967      we have a PARALLEL with both loads from the same memory location.
3968      We can split this into a load from memory followed by a register-register
3969      copy.  This saves at least one insn, more if register allocation can
3970      eliminate the copy.
3971
3972      We cannot do this if the destination of the first assignment is a
3973      condition code register or cc0.  We eliminate this case by making sure
3974      the SET_DEST and SET_SRC have the same mode.
3975
3976      We cannot do this if the destination of the second assignment is
3977      a register that we have already assumed is zero-extended.  Similarly
3978      for a SUBREG of such a register.  */
3979
3980   else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3981            && GET_CODE (newpat) == PARALLEL
3982            && XVECLEN (newpat, 0) == 2
3983            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3984            && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3985            && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3986                == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3987            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3988            && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3989                            XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3990            && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
3991            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3992            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3993            && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3994                  (REG_P (temp_expr)
3995                   && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3996                   && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3997                                BITS_PER_WORD)
3998                   && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3999                                HOST_BITS_PER_INT)
4000                   && (reg_stat[REGNO (temp_expr)].nonzero_bits
4001                       != GET_MODE_MASK (word_mode))))
4002            && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
4003                  && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
4004                      (REG_P (temp_expr)
4005                       && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
4006                       && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4007                                    BITS_PER_WORD)
4008                       && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4009                                    HOST_BITS_PER_INT)
4010                       && (reg_stat[REGNO (temp_expr)].nonzero_bits
4011                           != GET_MODE_MASK (word_mode)))))
4012            && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4013                                          SET_SRC (XVECEXP (newpat, 0, 1)))
4014            && ! find_reg_note (i3, REG_UNUSED,
4015                                SET_DEST (XVECEXP (newpat, 0, 0))))
4016     {
4017       rtx ni2dest;
4018
4019       newi2pat = XVECEXP (newpat, 0, 0);
4020       ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
4021       newpat = XVECEXP (newpat, 0, 1);
4022       SUBST (SET_SRC (newpat),
4023              gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
4024       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4025
4026       if (i2_code_number >= 0)
4027         insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4028
4029       if (insn_code_number >= 0)
4030         swap_i2i3 = 1;
4031     }
4032
4033   /* Similarly, check for a case where we have a PARALLEL of two independent
4034      SETs but we started with three insns.  In this case, we can do the sets
4035      as two separate insns.  This case occurs when some SET allows two
4036      other insns to combine, but the destination of that SET is still live.
4037
4038      Also do this if we started with two insns and (at least) one of the
4039      resulting sets is a noop; this noop will be deleted later.
4040
4041      Also do this if we started with two insns neither of which was a simple
4042      move.  */
4043
4044   else if (insn_code_number < 0 && asm_noperands (newpat) < 0
4045            && GET_CODE (newpat) == PARALLEL
4046            && XVECLEN (newpat, 0) == 2
4047            && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
4048            && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
4049            && (i1
4050                || set_noop_p (XVECEXP (newpat, 0, 0))
4051                || set_noop_p (XVECEXP (newpat, 0, 1))
4052                || (!i2_was_move && !i3_was_move))
4053            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
4054            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
4055            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4056            && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4057            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4058                                   XVECEXP (newpat, 0, 0))
4059            && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
4060                                   XVECEXP (newpat, 0, 1))
4061            && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
4062                  && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
4063     {
4064       rtx set0 = XVECEXP (newpat, 0, 0);
4065       rtx set1 = XVECEXP (newpat, 0, 1);
4066
4067       /* Normally, it doesn't matter which of the two is done first,
4068          but the one that references cc0 can't be the second, and
4069          one which uses any regs/memory set in between i2 and i3 can't
4070          be first.  The PARALLEL might also have been pre-existing in i3,
4071          so we need to make sure that we won't wrongly hoist a SET to i2
4072          that would conflict with a death note present in there, or would
4073          have its dest modified between i2 and i3.  */
4074       if (!modified_between_p (SET_SRC (set1), i2, i3)
4075           && !(REG_P (SET_DEST (set1))
4076                && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4077           && !(GET_CODE (SET_DEST (set1)) == SUBREG
4078                && find_reg_note (i2, REG_DEAD,
4079                                  SUBREG_REG (SET_DEST (set1))))
4080           && !modified_between_p (SET_DEST (set1), i2, i3)
4081           && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
4082           /* If I3 is a jump, ensure that set0 is a jump so that
4083              we do not create invalid RTL.  */
4084           && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4085          )
4086         {
4087           newi2pat = set1;
4088           newpat = set0;
4089         }
4090       else if (!modified_between_p (SET_SRC (set0), i2, i3)
4091                && !(REG_P (SET_DEST (set0))
4092                     && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4093                && !(GET_CODE (SET_DEST (set0)) == SUBREG
4094                     && find_reg_note (i2, REG_DEAD,
4095                                       SUBREG_REG (SET_DEST (set0))))
4096                && !modified_between_p (SET_DEST (set0), i2, i3)
4097                && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4098                /* If I3 is a jump, ensure that set1 is a jump so that
4099                   we do not create invalid RTL.  */
4100                && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4101               )
4102         {
4103           newi2pat = set0;
4104           newpat = set1;
4105         }
4106       else
4107         {
4108           undo_all ();
4109           return 0;
4110         }
4111
4112       i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4113
4114       if (i2_code_number >= 0)
4115         {
4116           /* recog_for_combine might have added CLOBBERs to newi2pat.
4117              Make sure NEWPAT does not depend on the clobbered regs.  */
4118           if (GET_CODE (newi2pat) == PARALLEL)
4119             {
4120               for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4121                 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4122                   {
4123                     rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4124                     if (reg_overlap_mentioned_p (reg, newpat))
4125                       {
4126                         undo_all ();
4127                         return 0;
4128                       }
4129                   }
4130             }
4131
4132           insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4133
4134           if (insn_code_number >= 0)
4135             split_i2i3 = 1;
4136         }
4137     }
4138
4139   /* If it still isn't recognized, fail and change things back the way they
4140      were.  */
4141   if ((insn_code_number < 0
4142        /* Is the result a reasonable ASM_OPERANDS?  */
4143        && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4144     {
4145       undo_all ();
4146       return 0;
4147     }
4148
4149   /* If we had to change another insn, make sure it is valid also.  */
4150   if (undobuf.other_insn)
4151     {
4152       CLEAR_HARD_REG_SET (newpat_used_regs);
4153
4154       other_pat = PATTERN (undobuf.other_insn);
4155       other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4156                                              &new_other_notes);
4157
4158       if (other_code_number < 0 && ! check_asm_operands (other_pat))
4159         {
4160           undo_all ();
4161           return 0;
4162         }
4163     }
4164
4165   /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4166      they are adjacent to each other or not.  */
4167   if (HAVE_cc0)
4168     {
4169       rtx_insn *p = prev_nonnote_insn (i3);
4170       if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4171           && sets_cc0_p (newi2pat))
4172         {
4173           undo_all ();
4174           return 0;
4175         }
4176     }
4177
4178   /* Only allow this combination if insn_cost reports that the
4179      replacement instructions are cheaper than the originals.  */
4180   if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4181     {
4182       undo_all ();
4183       return 0;
4184     }
4185
4186   if (MAY_HAVE_DEBUG_BIND_INSNS)
4187     {
4188       struct undo *undo;
4189
4190       for (undo = undobuf.undos; undo; undo = undo->next)
4191         if (undo->kind == UNDO_MODE)
4192           {
4193             rtx reg = *undo->where.r;
4194             machine_mode new_mode = GET_MODE (reg);
4195             machine_mode old_mode = undo->old_contents.m;
4196
4197             /* Temporarily revert mode back.  */
4198             adjust_reg_mode (reg, old_mode);
4199
4200             if (reg == i2dest && i2scratch)
4201               {
4202                 /* If we used i2dest as a scratch register with a
4203                    different mode, substitute it for the original
4204                    i2src while its original mode is temporarily
4205                    restored, and then clear i2scratch so that we don't
4206                    do it again later.  */
4207                 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4208                                      this_basic_block);
4209                 i2scratch = false;
4210                 /* Put back the new mode.  */
4211                 adjust_reg_mode (reg, new_mode);
4212               }
4213             else
4214               {
4215                 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4216                 rtx_insn *first, *last;
4217
4218                 if (reg == i2dest)
4219                   {
4220                     first = i2;
4221                     last = last_combined_insn;
4222                   }
4223                 else
4224                   {
4225                     first = i3;
4226                     last = undobuf.other_insn;
4227                     gcc_assert (last);
4228                     if (DF_INSN_LUID (last)
4229                         < DF_INSN_LUID (last_combined_insn))
4230                       last = last_combined_insn;
4231                   }
4232
4233                 /* We're dealing with a reg that changed mode but not
4234                    meaning, so we want to turn it into a subreg for
4235                    the new mode.  However, because of REG sharing and
4236                    because its mode had already changed, we have to do
4237                    it in two steps.  First, replace any debug uses of
4238                    reg, with its original mode temporarily restored,
4239                    with this copy we have created; then, replace the
4240                    copy with the SUBREG of the original shared reg,
4241                    once again changed to the new mode.  */
4242                 propagate_for_debug (first, last, reg, tempreg,
4243                                      this_basic_block);
4244                 adjust_reg_mode (reg, new_mode);
4245                 propagate_for_debug (first, last, tempreg,
4246                                      lowpart_subreg (old_mode, reg, new_mode),
4247                                      this_basic_block);
4248               }
4249           }
4250     }
4251
4252   /* If we will be able to accept this, we have made a
4253      change to the destination of I3.  This requires us to
4254      do a few adjustments.  */
4255
4256   if (changed_i3_dest)
4257     {
4258       PATTERN (i3) = newpat;
4259       adjust_for_new_dest (i3);
4260     }
4261
4262   /* We now know that we can do this combination.  Merge the insns and
4263      update the status of registers and LOG_LINKS.  */
4264
4265   if (undobuf.other_insn)
4266     {
4267       rtx note, next;
4268
4269       PATTERN (undobuf.other_insn) = other_pat;
4270
4271       /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4272          ensure that they are still valid.  Then add any non-duplicate
4273          notes added by recog_for_combine.  */
4274       for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4275         {
4276           next = XEXP (note, 1);
4277
4278           if ((REG_NOTE_KIND (note) == REG_DEAD
4279                && !reg_referenced_p (XEXP (note, 0),
4280                                      PATTERN (undobuf.other_insn)))
4281               ||(REG_NOTE_KIND (note) == REG_UNUSED
4282                  && !reg_set_p (XEXP (note, 0),
4283                                 PATTERN (undobuf.other_insn)))
4284               /* Simply drop equal note since it may be no longer valid
4285                  for other_insn.  It may be possible to record that CC
4286                  register is changed and only discard those notes, but
4287                  in practice it's unnecessary complication and doesn't
4288                  give any meaningful improvement.
4289
4290                  See PR78559.  */
4291               || REG_NOTE_KIND (note) == REG_EQUAL
4292               || REG_NOTE_KIND (note) == REG_EQUIV)
4293             remove_note (undobuf.other_insn, note);
4294         }
4295
4296       distribute_notes  (new_other_notes, undobuf.other_insn,
4297                         undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4298                         NULL_RTX);
4299     }
4300
4301   if (swap_i2i3)
4302     {
4303       /* I3 now uses what used to be its destination and which is now
4304          I2's destination.  This requires us to do a few adjustments.  */
4305       PATTERN (i3) = newpat;
4306       adjust_for_new_dest (i3);
4307     }
4308
4309   if (swap_i2i3 || split_i2i3)
4310     {
4311       /* We might need a LOG_LINK from I3 to I2.  But then we used to
4312          have one, so we still will.
4313
4314          However, some later insn might be using I2's dest and have
4315          a LOG_LINK pointing at I3.  We should change it to point at
4316          I2 instead.  */
4317
4318       /* newi2pat is usually a SET here; however, recog_for_combine might
4319          have added some clobbers.  */
4320       rtx x = newi2pat;
4321       if (GET_CODE (x) == PARALLEL)
4322         x = XVECEXP (newi2pat, 0, 0);
4323
4324       /* It can only be a SET of a REG or of a SUBREG of a REG.  */
4325       unsigned int regno = reg_or_subregno (SET_DEST (x));
4326
4327       bool done = false;
4328       for (rtx_insn *insn = NEXT_INSN (i3);
4329            !done
4330            && insn
4331            && NONDEBUG_INSN_P (insn)
4332            && BLOCK_FOR_INSN (insn) == this_basic_block;
4333            insn = NEXT_INSN (insn))
4334         {
4335           struct insn_link *link;
4336           FOR_EACH_LOG_LINK (link, insn)
4337             if (link->insn == i3 && link->regno == regno)
4338               {
4339                 link->insn = i2;
4340                 done = true;
4341                 break;
4342               }
4343         }
4344     }
4345
4346   {
4347     rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4348     struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4349     rtx midnotes = 0;
4350     int from_luid;
4351     /* Compute which registers we expect to eliminate.  newi2pat may be setting
4352        either i3dest or i2dest, so we must check it.  */
4353     rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4354                    || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4355                    || !i2dest_killed
4356                    ? 0 : i2dest);
4357     /* For i1, we need to compute both local elimination and global
4358        elimination information with respect to newi2pat because i1dest
4359        may be the same as i3dest, in which case newi2pat may be setting
4360        i1dest.  Global information is used when distributing REG_DEAD
4361        note for i2 and i3, in which case it does matter if newi2pat sets
4362        i1dest or not.
4363
4364        Local information is used when distributing REG_DEAD note for i1,
4365        in which case it doesn't matter if newi2pat sets i1dest or not.
4366        See PR62151, if we have four insns combination:
4367            i0: r0 <- i0src
4368            i1: r1 <- i1src (using r0)
4369                      REG_DEAD (r0)
4370            i2: r0 <- i2src (using r1)
4371            i3: r3 <- i3src (using r0)
4372            ix: using r0
4373        From i1's point of view, r0 is eliminated, no matter if it is set
4374        by newi2pat or not.  In other words, REG_DEAD info for r0 in i1
4375        should be discarded.
4376
4377        Note local information only affects cases in forms like "I1->I2->I3",
4378        "I0->I1->I2->I3" or "I0&I1->I2, I2->I3".  For other cases like
4379        "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4380        i0dest anyway.  */
4381     rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4382                          || !i1dest_killed
4383                          ? 0 : i1dest);
4384     rtx elim_i1 = (local_elim_i1 == 0
4385                    || (newi2pat && reg_set_p (i1dest, newi2pat))
4386                    ? 0 : i1dest);
4387     /* Same case as i1.  */
4388     rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4389                          ? 0 : i0dest);
4390     rtx elim_i0 = (local_elim_i0 == 0
4391                    || (newi2pat && reg_set_p (i0dest, newi2pat))
4392                    ? 0 : i0dest);
4393
4394     /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4395        clear them.  */
4396     i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4397     i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4398     if (i1)
4399       i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4400     if (i0)
4401       i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4402
4403     /* Ensure that we do not have something that should not be shared but
4404        occurs multiple times in the new insns.  Check this by first
4405        resetting all the `used' flags and then copying anything is shared.  */
4406
4407     reset_used_flags (i3notes);
4408     reset_used_flags (i2notes);
4409     reset_used_flags (i1notes);
4410     reset_used_flags (i0notes);
4411     reset_used_flags (newpat);
4412     reset_used_flags (newi2pat);
4413     if (undobuf.other_insn)
4414       reset_used_flags (PATTERN (undobuf.other_insn));
4415
4416     i3notes = copy_rtx_if_shared (i3notes);
4417     i2notes = copy_rtx_if_shared (i2notes);
4418     i1notes = copy_rtx_if_shared (i1notes);
4419     i0notes = copy_rtx_if_shared (i0notes);
4420     newpat = copy_rtx_if_shared (newpat);
4421     newi2pat = copy_rtx_if_shared (newi2pat);
4422     if (undobuf.other_insn)
4423       reset_used_flags (PATTERN (undobuf.other_insn));
4424
4425     INSN_CODE (i3) = insn_code_number;
4426     PATTERN (i3) = newpat;
4427
4428     if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4429       {
4430         for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4431              link = XEXP (link, 1))
4432           {
4433             if (substed_i2)
4434               {
4435                 /* I2SRC must still be meaningful at this point.  Some
4436                    splitting operations can invalidate I2SRC, but those
4437                    operations do not apply to calls.  */
4438                 gcc_assert (i2src);
4439                 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4440                                                        i2dest, i2src);
4441               }
4442             if (substed_i1)
4443               XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4444                                                      i1dest, i1src);
4445             if (substed_i0)
4446               XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4447                                                      i0dest, i0src);
4448           }
4449       }
4450
4451     if (undobuf.other_insn)
4452       INSN_CODE (undobuf.other_insn) = other_code_number;
4453
4454     /* We had one special case above where I2 had more than one set and
4455        we replaced a destination of one of those sets with the destination
4456        of I3.  In that case, we have to update LOG_LINKS of insns later
4457        in this basic block.  Note that this (expensive) case is rare.
4458
4459        Also, in this case, we must pretend that all REG_NOTEs for I2
4460        actually came from I3, so that REG_UNUSED notes from I2 will be
4461        properly handled.  */
4462
4463     if (i3_subst_into_i2)
4464       {
4465         for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4466           if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4467                || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4468               && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4469               && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4470               && ! find_reg_note (i2, REG_UNUSED,
4471                                   SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4472             for (temp_insn = NEXT_INSN (i2);
4473                  temp_insn
4474                  && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4475                      || BB_HEAD (this_basic_block) != temp_insn);
4476                  temp_insn = NEXT_INSN (temp_insn))
4477               if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4478                 FOR_EACH_LOG_LINK (link, temp_insn)
4479                   if (link->insn == i2)
4480                     link->insn = i3;
4481
4482         if (i3notes)
4483           {
4484             rtx link = i3notes;
4485             while (XEXP (link, 1))
4486               link = XEXP (link, 1);
4487             XEXP (link, 1) = i2notes;
4488           }
4489         else
4490           i3notes = i2notes;
4491         i2notes = 0;
4492       }
4493
4494     LOG_LINKS (i3) = NULL;
4495     REG_NOTES (i3) = 0;
4496     LOG_LINKS (i2) = NULL;
4497     REG_NOTES (i2) = 0;
4498
4499     if (newi2pat)
4500       {
4501         if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4502           propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4503                                this_basic_block);
4504         INSN_CODE (i2) = i2_code_number;
4505         PATTERN (i2) = newi2pat;
4506       }
4507     else
4508       {
4509         if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4510           propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4511                                this_basic_block);
4512         SET_INSN_DELETED (i2);
4513       }
4514
4515     if (i1)
4516       {
4517         LOG_LINKS (i1) = NULL;
4518         REG_NOTES (i1) = 0;
4519         if (MAY_HAVE_DEBUG_BIND_INSNS)
4520           propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4521                                this_basic_block);
4522         SET_INSN_DELETED (i1);
4523       }
4524
4525     if (i0)
4526       {
4527         LOG_LINKS (i0) = NULL;
4528         REG_NOTES (i0) = 0;
4529         if (MAY_HAVE_DEBUG_BIND_INSNS)
4530           propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4531                                this_basic_block);
4532         SET_INSN_DELETED (i0);
4533       }
4534
4535     /* Get death notes for everything that is now used in either I3 or
4536        I2 and used to die in a previous insn.  If we built two new
4537        patterns, move from I1 to I2 then I2 to I3 so that we get the
4538        proper movement on registers that I2 modifies.  */
4539
4540     if (i0)
4541       from_luid = DF_INSN_LUID (i0);
4542     else if (i1)
4543       from_luid = DF_INSN_LUID (i1);
4544     else
4545       from_luid = DF_INSN_LUID (i2);
4546     if (newi2pat)
4547       move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4548     move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4549
4550     /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3.  */
4551     if (i3notes)
4552       distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4553                         elim_i2, elim_i1, elim_i0);
4554     if (i2notes)
4555       distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4556                         elim_i2, elim_i1, elim_i0);
4557     if (i1notes)
4558       distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4559                         elim_i2, local_elim_i1, local_elim_i0);
4560     if (i0notes)
4561       distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4562                         elim_i2, elim_i1, local_elim_i0);
4563     if (midnotes)
4564       distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4565                         elim_i2, elim_i1, elim_i0);
4566
4567     /* Distribute any notes added to I2 or I3 by recog_for_combine.  We
4568        know these are REG_UNUSED and want them to go to the desired insn,
4569        so we always pass it as i3.  */
4570
4571     if (newi2pat && new_i2_notes)
4572       distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4573                         NULL_RTX);
4574
4575     if (new_i3_notes)
4576       distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4577                         NULL_RTX);
4578
4579     /* If I3DEST was used in I3SRC, it really died in I3.  We may need to
4580        put a REG_DEAD note for it somewhere.  If NEWI2PAT exists and sets
4581        I3DEST, the death must be somewhere before I2, not I3.  If we passed I3
4582        in that case, it might delete I2.  Similarly for I2 and I1.
4583        Show an additional death due to the REG_DEAD note we make here.  If
4584        we discard it in distribute_notes, we will decrement it again.  */
4585
4586     if (i3dest_killed)
4587       {
4588         rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4589         if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4590           distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4591                             elim_i1, elim_i0);
4592         else
4593           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4594                             elim_i2, elim_i1, elim_i0);
4595       }
4596
4597     if (i2dest_in_i2src)
4598       {
4599         rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4600         if (newi2pat && reg_set_p (i2dest, newi2pat))
4601           distribute_notes (new_note,  NULL, i2, NULL, NULL_RTX,
4602                             NULL_RTX, NULL_RTX);
4603         else
4604           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4605                             NULL_RTX, NULL_RTX, NULL_RTX);
4606       }
4607
4608     if (i1dest_in_i1src)
4609       {
4610         rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4611         if (newi2pat && reg_set_p (i1dest, newi2pat))
4612           distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4613                             NULL_RTX, NULL_RTX);
4614         else
4615           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4616                             NULL_RTX, NULL_RTX, NULL_RTX);
4617       }
4618
4619     if (i0dest_in_i0src)
4620       {
4621         rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4622         if (newi2pat && reg_set_p (i0dest, newi2pat))
4623           distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4624                             NULL_RTX, NULL_RTX);
4625         else
4626           distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4627                             NULL_RTX, NULL_RTX, NULL_RTX);
4628       }
4629
4630     distribute_links (i3links);
4631     distribute_links (i2links);
4632     distribute_links (i1links);
4633     distribute_links (i0links);
4634
4635     if (REG_P (i2dest))
4636       {
4637         struct insn_link *link;
4638         rtx_insn *i2_insn = 0;
4639         rtx i2_val = 0, set;
4640
4641         /* The insn that used to set this register doesn't exist, and
4642            this life of the register may not exist either.  See if one of
4643            I3's links points to an insn that sets I2DEST.  If it does,
4644            that is now the last known value for I2DEST. If we don't update
4645            this and I2 set the register to a value that depended on its old
4646            contents, we will get confused.  If this insn is used, thing
4647            will be set correctly in combine_instructions.  */
4648         FOR_EACH_LOG_LINK (link, i3)
4649           if ((set = single_set (link->insn)) != 0
4650               && rtx_equal_p (i2dest, SET_DEST (set)))
4651             i2_insn = link->insn, i2_val = SET_SRC (set);
4652
4653         record_value_for_reg (i2dest, i2_insn, i2_val);
4654
4655         /* If the reg formerly set in I2 died only once and that was in I3,
4656            zero its use count so it won't make `reload' do any work.  */
4657         if (! added_sets_2
4658             && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4659             && ! i2dest_in_i2src
4660             && REGNO (i2dest) < reg_n_sets_max)
4661           INC_REG_N_SETS (REGNO (i2dest), -1);
4662       }
4663
4664     if (i1 && REG_P (i1dest))
4665       {
4666         struct insn_link *link;
4667         rtx_insn *i1_insn = 0;
4668         rtx i1_val = 0, set;
4669
4670         FOR_EACH_LOG_LINK (link, i3)
4671           if ((set = single_set (link->insn)) != 0
4672               && rtx_equal_p (i1dest, SET_DEST (set)))
4673             i1_insn = link->insn, i1_val = SET_SRC (set);
4674
4675         record_value_for_reg (i1dest, i1_insn, i1_val);
4676
4677         if (! added_sets_1
4678             && ! i1dest_in_i1src
4679             && REGNO (i1dest) < reg_n_sets_max)
4680           INC_REG_N_SETS (REGNO (i1dest), -1);
4681       }
4682
4683     if (i0 && REG_P (i0dest))
4684       {
4685         struct insn_link *link;
4686         rtx_insn *i0_insn = 0;
4687         rtx i0_val = 0, set;
4688
4689         FOR_EACH_LOG_LINK (link, i3)
4690           if ((set = single_set (link->insn)) != 0
4691               && rtx_equal_p (i0dest, SET_DEST (set)))
4692             i0_insn = link->insn, i0_val = SET_SRC (set);
4693
4694         record_value_for_reg (i0dest, i0_insn, i0_val);
4695
4696         if (! added_sets_0
4697             && ! i0dest_in_i0src
4698             && REGNO (i0dest) < reg_n_sets_max)
4699           INC_REG_N_SETS (REGNO (i0dest), -1);
4700       }
4701
4702     /* Update reg_stat[].nonzero_bits et al for any changes that may have
4703        been made to this insn.  The order is important, because newi2pat
4704        can affect nonzero_bits of newpat.  */
4705     if (newi2pat)
4706       note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4707     note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4708   }
4709
4710   if (undobuf.other_insn != NULL_RTX)
4711     {
4712       if (dump_file)
4713         {
4714           fprintf (dump_file, "modifying other_insn ");
4715           dump_insn_slim (dump_file, undobuf.other_insn);
4716         }
4717       df_insn_rescan (undobuf.other_insn);
4718     }
4719
4720   if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4721     {
4722       if (dump_file)
4723         {
4724           fprintf (dump_file, "modifying insn i0 ");
4725           dump_insn_slim (dump_file, i0);
4726         }
4727       df_insn_rescan (i0);
4728     }
4729
4730   if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4731     {
4732       if (dump_file)
4733         {
4734           fprintf (dump_file, "modifying insn i1 ");
4735           dump_insn_slim (dump_file, i1);
4736         }
4737       df_insn_rescan (i1);
4738     }
4739
4740   if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4741     {
4742       if (dump_file)
4743         {
4744           fprintf (dump_file, "modifying insn i2 ");
4745           dump_insn_slim (dump_file, i2);
4746         }
4747       df_insn_rescan (i2);
4748     }
4749
4750   if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4751     {
4752       if (dump_file)
4753         {
4754           fprintf (dump_file, "modifying insn i3 ");
4755           dump_insn_slim (dump_file, i3);
4756         }
4757       df_insn_rescan (i3);
4758     }
4759
4760   /* Set new_direct_jump_p if a new return or simple jump instruction
4761      has been created.  Adjust the CFG accordingly.  */
4762   if (returnjump_p (i3) || any_uncondjump_p (i3))
4763     {
4764       *new_direct_jump_p = 1;
4765       mark_jump_label (PATTERN (i3), i3, 0);
4766       update_cfg_for_uncondjump (i3);
4767     }
4768
4769   if (undobuf.other_insn != NULL_RTX
4770       && (returnjump_p (undobuf.other_insn)
4771           || any_uncondjump_p (undobuf.other_insn)))
4772     {
4773       *new_direct_jump_p = 1;
4774       update_cfg_for_uncondjump (undobuf.other_insn);
4775     }
4776
4777   if (GET_CODE (PATTERN (i3)) == TRAP_IF
4778       && XEXP (PATTERN (i3), 0) == const1_rtx)
4779     {
4780       basic_block bb = BLOCK_FOR_INSN (i3);
4781       gcc_assert (bb);
4782       remove_edge (split_block (bb, i3));
4783       emit_barrier_after_bb (bb);
4784       *new_direct_jump_p = 1;
4785     }
4786
4787   if (undobuf.other_insn
4788       && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4789       && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4790     {
4791       basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4792       gcc_assert (bb);
4793       remove_edge (split_block (bb, undobuf.other_insn));
4794       emit_barrier_after_bb (bb);
4795       *new_direct_jump_p = 1;
4796     }
4797
4798   /* A noop might also need cleaning up of CFG, if it comes from the
4799      simplification of a jump.  */
4800   if (JUMP_P (i3)
4801       && GET_CODE (newpat) == SET
4802       && SET_SRC (newpat) == pc_rtx
4803       && SET_DEST (newpat) == pc_rtx)
4804     {
4805       *new_direct_jump_p = 1;
4806       update_cfg_for_uncondjump (i3);
4807     }
4808
4809   if (undobuf.other_insn != NULL_RTX
4810       && JUMP_P (undobuf.other_insn)
4811       && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4812       && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4813       && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4814     {
4815       *new_direct_jump_p = 1;
4816       update_cfg_for_uncondjump (undobuf.other_insn);
4817     }
4818
4819   combine_successes++;
4820   undo_commit ();
4821
4822   rtx_insn *ret = newi2pat ? i2 : i3;
4823   if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4824     ret = added_links_insn;
4825   if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4826     ret = added_notes_insn;
4827
4828   return ret;
4829 }
4830 \f
4831 /* Get a marker for undoing to the current state.  */
4832
4833 static void *
4834 get_undo_marker (void)
4835 {
4836   return undobuf.undos;
4837 }
4838
4839 /* Undo the modifications up to the marker.  */
4840
4841 static void
4842 undo_to_marker (void *marker)
4843 {
4844   struct undo *undo, *next;
4845
4846   for (undo = undobuf.undos; undo != marker; undo = next)
4847     {
4848       gcc_assert (undo);
4849
4850       next = undo->next;
4851       switch (undo->kind)
4852         {
4853         case UNDO_RTX:
4854           *undo->where.r = undo->old_contents.r;
4855           break;
4856         case UNDO_INT:
4857           *undo->where.i = undo->old_contents.i;
4858           break;
4859         case UNDO_MODE:
4860           adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4861           break;
4862         case UNDO_LINKS:
4863           *undo->where.l = undo->old_contents.l;
4864           break;
4865         default:
4866           gcc_unreachable ();
4867         }
4868
4869       undo->next = undobuf.frees;
4870       undobuf.frees = undo;
4871     }
4872
4873   undobuf.undos = (struct undo *) marker;
4874 }
4875
4876 /* Undo all the modifications recorded in undobuf.  */
4877
4878 static void
4879 undo_all (void)
4880 {
4881   undo_to_marker (0);
4882 }
4883
4884 /* We've committed to accepting the changes we made.  Move all
4885    of the undos to the free list.  */
4886
4887 static void
4888 undo_commit (void)
4889 {
4890   struct undo *undo, *next;
4891
4892   for (undo = undobuf.undos; undo; undo = next)
4893     {
4894       next = undo->next;
4895       undo->next = undobuf.frees;
4896       undobuf.frees = undo;
4897     }
4898   undobuf.undos = 0;
4899 }
4900 \f
4901 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4902    where we have an arithmetic expression and return that point.  LOC will
4903    be inside INSN.
4904
4905    try_combine will call this function to see if an insn can be split into
4906    two insns.  */
4907
4908 static rtx *
4909 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4910 {
4911   rtx x = *loc;
4912   enum rtx_code code = GET_CODE (x);
4913   rtx *split;
4914   unsigned HOST_WIDE_INT len = 0;
4915   HOST_WIDE_INT pos = 0;
4916   int unsignedp = 0;
4917   rtx inner = NULL_RTX;
4918   scalar_int_mode mode, inner_mode;
4919
4920   /* First special-case some codes.  */
4921   switch (code)
4922     {
4923     case SUBREG:
4924 #ifdef INSN_SCHEDULING
4925       /* If we are making a paradoxical SUBREG invalid, it becomes a split
4926          point.  */
4927       if (MEM_P (SUBREG_REG (x)))
4928         return loc;
4929 #endif
4930       return find_split_point (&SUBREG_REG (x), insn, false);
4931
4932     case MEM:
4933       /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4934          using LO_SUM and HIGH.  */
4935       if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4936                           || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4937         {
4938           machine_mode address_mode = get_address_mode (x);
4939
4940           SUBST (XEXP (x, 0),
4941                  gen_rtx_LO_SUM (address_mode,
4942                                  gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4943                                  XEXP (x, 0)));
4944           return &XEXP (XEXP (x, 0), 0);
4945         }
4946
4947       /* If we have a PLUS whose second operand is a constant and the
4948          address is not valid, perhaps we can split it up using
4949          the machine-specific way to split large constants.  We use
4950          the first pseudo-reg (one of the virtual regs) as a placeholder;
4951          it will not remain in the result.  */
4952       if (GET_CODE (XEXP (x, 0)) == PLUS
4953           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4954           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4955                                             MEM_ADDR_SPACE (x)))
4956         {
4957           rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4958           rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4959                                                subst_insn);
4960
4961           /* This should have produced two insns, each of which sets our
4962              placeholder.  If the source of the second is a valid address,
4963              we can put both sources together and make a split point
4964              in the middle.  */
4965
4966           if (seq
4967               && NEXT_INSN (seq) != NULL_RTX
4968               && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4969               && NONJUMP_INSN_P (seq)
4970               && GET_CODE (PATTERN (seq)) == SET
4971               && SET_DEST (PATTERN (seq)) == reg
4972               && ! reg_mentioned_p (reg,
4973                                     SET_SRC (PATTERN (seq)))
4974               && NONJUMP_INSN_P (NEXT_INSN (seq))
4975               && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4976               && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4977               && memory_address_addr_space_p
4978                    (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4979                     MEM_ADDR_SPACE (x)))
4980             {
4981               rtx src1 = SET_SRC (PATTERN (seq));
4982               rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4983
4984               /* Replace the placeholder in SRC2 with SRC1.  If we can
4985                  find where in SRC2 it was placed, that can become our
4986                  split point and we can replace this address with SRC2.
4987                  Just try two obvious places.  */
4988
4989               src2 = replace_rtx (src2, reg, src1);
4990               split = 0;
4991               if (XEXP (src2, 0) == src1)
4992                 split = &XEXP (src2, 0);
4993               else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4994                        && XEXP (XEXP (src2, 0), 0) == src1)
4995                 split = &XEXP (XEXP (src2, 0), 0);
4996
4997               if (split)
4998                 {
4999                   SUBST (XEXP (x, 0), src2);
5000                   return split;
5001                 }
5002             }
5003
5004           /* If that didn't work and we have a nested plus, like:
5005              ((REG1 * CONST1) + REG2) + CONST2 and (REG1 + REG2) + CONST2
5006              is valid address, try to split (REG1 * CONST1).  */
5007           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5008               && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5009               && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5010               && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SUBREG
5011                     && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5012                                                          0), 0)))))
5013             {
5014               rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 0);
5015               XEXP (XEXP (XEXP (x, 0), 0), 0) = reg;
5016               if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5017                                                MEM_ADDR_SPACE (x)))
5018                 {
5019                   XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5020                   return &XEXP (XEXP (XEXP (x, 0), 0), 0);
5021                 }
5022               XEXP (XEXP (XEXP (x, 0), 0), 0) = tem;
5023             }
5024           else if (GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
5025                    && OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 0))
5026                    && !OBJECT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5027                    && ! (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SUBREG
5028                          && OBJECT_P (SUBREG_REG (XEXP (XEXP (XEXP (x, 0),
5029                                                               0), 1)))))
5030             {
5031               rtx tem = XEXP (XEXP (XEXP (x, 0), 0), 1);
5032               XEXP (XEXP (XEXP (x, 0), 0), 1) = reg;
5033               if (memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5034                                                MEM_ADDR_SPACE (x)))
5035                 {
5036                   XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5037                   return &XEXP (XEXP (XEXP (x, 0), 0), 1);
5038                 }
5039               XEXP (XEXP (XEXP (x, 0), 0), 1) = tem;
5040             }
5041
5042           /* If that didn't work, perhaps the first operand is complex and
5043              needs to be computed separately, so make a split point there.
5044              This will occur on machines that just support REG + CONST
5045              and have a constant moved through some previous computation.  */
5046           if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
5047               && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5048                     && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5049             return &XEXP (XEXP (x, 0), 0);
5050         }
5051
5052       /* If we have a PLUS whose first operand is complex, try computing it
5053          separately by making a split there.  */
5054       if (GET_CODE (XEXP (x, 0)) == PLUS
5055           && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5056                                             MEM_ADDR_SPACE (x))
5057           && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
5058           && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5059                 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5060         return &XEXP (XEXP (x, 0), 0);
5061       break;
5062
5063     case SET:
5064       /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
5065          ZERO_EXTRACT, the most likely reason why this doesn't match is that
5066          we need to put the operand into a register.  So split at that
5067          point.  */
5068
5069       if (SET_DEST (x) == cc0_rtx
5070           && GET_CODE (SET_SRC (x)) != COMPARE
5071           && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
5072           && !OBJECT_P (SET_SRC (x))
5073           && ! (GET_CODE (SET_SRC (x)) == SUBREG
5074                 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
5075         return &SET_SRC (x);
5076
5077       /* See if we can split SET_SRC as it stands.  */
5078       split = find_split_point (&SET_SRC (x), insn, true);
5079       if (split && split != &SET_SRC (x))
5080         return split;
5081
5082       /* See if we can split SET_DEST as it stands.  */
5083       split = find_split_point (&SET_DEST (x), insn, false);
5084       if (split && split != &SET_DEST (x))
5085         return split;
5086
5087       /* See if this is a bitfield assignment with everything constant.  If
5088          so, this is an IOR of an AND, so split it into that.  */
5089       if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5090           && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5091                                      &inner_mode)
5092           && HWI_COMPUTABLE_MODE_P (inner_mode)
5093           && CONST_INT_P (XEXP (SET_DEST (x), 1))
5094           && CONST_INT_P (XEXP (SET_DEST (x), 2))
5095           && CONST_INT_P (SET_SRC (x))
5096           && ((INTVAL (XEXP (SET_DEST (x), 1))
5097                + INTVAL (XEXP (SET_DEST (x), 2)))
5098               <= GET_MODE_PRECISION (inner_mode))
5099           && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5100         {
5101           HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5102           unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5103           unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
5104           rtx dest = XEXP (SET_DEST (x), 0);
5105           unsigned HOST_WIDE_INT mask
5106             = (HOST_WIDE_INT_1U << len) - 1;
5107           rtx or_mask;
5108
5109           if (BITS_BIG_ENDIAN)
5110             pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5111
5112           or_mask = gen_int_mode (src << pos, inner_mode);
5113           if (src == mask)
5114             SUBST (SET_SRC (x),
5115                    simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5116           else
5117             {
5118               rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5119               SUBST (SET_SRC (x),
5120                      simplify_gen_binary (IOR, inner_mode,
5121                                           simplify_gen_binary (AND, inner_mode,
5122                                                                dest, negmask),
5123                                           or_mask));
5124             }
5125
5126           SUBST (SET_DEST (x), dest);
5127
5128           split = find_split_point (&SET_SRC (x), insn, true);
5129           if (split && split != &SET_SRC (x))
5130             return split;
5131         }
5132
5133       /* Otherwise, see if this is an operation that we can split into two.
5134          If so, try to split that.  */
5135       code = GET_CODE (SET_SRC (x));
5136
5137       switch (code)
5138         {
5139         case AND:
5140           /* If we are AND'ing with a large constant that is only a single
5141              bit and the result is only being used in a context where we
5142              need to know if it is zero or nonzero, replace it with a bit
5143              extraction.  This will avoid the large constant, which might
5144              have taken more than one insn to make.  If the constant were
5145              not a valid argument to the AND but took only one insn to make,
5146              this is no worse, but if it took more than one insn, it will
5147              be better.  */
5148
5149           if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5150               && REG_P (XEXP (SET_SRC (x), 0))
5151               && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5152               && REG_P (SET_DEST (x))
5153               && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5154               && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5155               && XEXP (*split, 0) == SET_DEST (x)
5156               && XEXP (*split, 1) == const0_rtx)
5157             {
5158               rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5159                                                 XEXP (SET_SRC (x), 0),
5160                                                 pos, NULL_RTX, 1, 1, 0, 0);
5161               if (extraction != 0)
5162                 {
5163                   SUBST (SET_SRC (x), extraction);
5164                   return find_split_point (loc, insn, false);
5165                 }
5166             }
5167           break;
5168
5169         case NE:
5170           /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5171              is known to be on, this can be converted into a NEG of a shift.  */
5172           if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5173               && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5174               && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5175                                                    GET_MODE (XEXP (SET_SRC (x),
5176                                                              0))))) >= 1))
5177             {
5178               machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5179               rtx pos_rtx = gen_int_shift_amount (mode, pos);
5180               SUBST (SET_SRC (x),
5181                      gen_rtx_NEG (mode,
5182                                   gen_rtx_LSHIFTRT (mode,
5183                                                     XEXP (SET_SRC (x), 0),
5184                                                     pos_rtx)));
5185
5186               split = find_split_point (&SET_SRC (x), insn, true);
5187               if (split && split != &SET_SRC (x))
5188                 return split;
5189             }
5190           break;
5191
5192         case SIGN_EXTEND:
5193           inner = XEXP (SET_SRC (x), 0);
5194
5195           /* We can't optimize if either mode is a partial integer
5196              mode as we don't know how many bits are significant
5197              in those modes.  */
5198           if (!is_int_mode (GET_MODE (inner), &inner_mode)
5199               || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5200             break;
5201
5202           pos = 0;
5203           len = GET_MODE_PRECISION (inner_mode);
5204           unsignedp = 0;
5205           break;
5206
5207         case SIGN_EXTRACT:
5208         case ZERO_EXTRACT:
5209           if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5210                                       &inner_mode)
5211               && CONST_INT_P (XEXP (SET_SRC (x), 1))
5212               && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5213             {
5214               inner = XEXP (SET_SRC (x), 0);
5215               len = INTVAL (XEXP (SET_SRC (x), 1));
5216               pos = INTVAL (XEXP (SET_SRC (x), 2));
5217
5218               if (BITS_BIG_ENDIAN)
5219                 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5220               unsignedp = (code == ZERO_EXTRACT);
5221             }
5222           break;
5223
5224         default:
5225           break;
5226         }
5227
5228       if (len
5229           && known_subrange_p (pos, len,
5230                                0, GET_MODE_PRECISION (GET_MODE (inner)))
5231           && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5232         {
5233           /* For unsigned, we have a choice of a shift followed by an
5234              AND or two shifts.  Use two shifts for field sizes where the
5235              constant might be too large.  We assume here that we can
5236              always at least get 8-bit constants in an AND insn, which is
5237              true for every current RISC.  */
5238
5239           if (unsignedp && len <= 8)
5240             {
5241               unsigned HOST_WIDE_INT mask
5242                 = (HOST_WIDE_INT_1U << len) - 1;
5243               rtx pos_rtx = gen_int_shift_amount (mode, pos);
5244               SUBST (SET_SRC (x),
5245                      gen_rtx_AND (mode,
5246                                   gen_rtx_LSHIFTRT
5247                                   (mode, gen_lowpart (mode, inner), pos_rtx),
5248                                   gen_int_mode (mask, mode)));
5249
5250               split = find_split_point (&SET_SRC (x), insn, true);
5251               if (split && split != &SET_SRC (x))
5252                 return split;
5253             }
5254           else
5255             {
5256               int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5257               int right_bits = GET_MODE_PRECISION (mode) - len;
5258               SUBST (SET_SRC (x),
5259                      gen_rtx_fmt_ee
5260                      (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5261                       gen_rtx_ASHIFT (mode,
5262                                       gen_lowpart (mode, inner),
5263                                       gen_int_shift_amount (mode, left_bits)),
5264                       gen_int_shift_amount (mode, right_bits)));
5265
5266               split = find_split_point (&SET_SRC (x), insn, true);
5267               if (split && split != &SET_SRC (x))
5268                 return split;
5269             }
5270         }
5271
5272       /* See if this is a simple operation with a constant as the second
5273          operand.  It might be that this constant is out of range and hence
5274          could be used as a split point.  */
5275       if (BINARY_P (SET_SRC (x))
5276           && CONSTANT_P (XEXP (SET_SRC (x), 1))
5277           && (OBJECT_P (XEXP (SET_SRC (x), 0))
5278               || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5279                   && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5280         return &XEXP (SET_SRC (x), 1);
5281
5282       /* Finally, see if this is a simple operation with its first operand
5283          not in a register.  The operation might require this operand in a
5284          register, so return it as a split point.  We can always do this
5285          because if the first operand were another operation, we would have
5286          already found it as a split point.  */
5287       if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5288           && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5289         return &XEXP (SET_SRC (x), 0);
5290
5291       return 0;
5292
5293     case AND:
5294     case IOR:
5295       /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5296          it is better to write this as (not (ior A B)) so we can split it.
5297          Similarly for IOR.  */
5298       if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5299         {
5300           SUBST (*loc,
5301                  gen_rtx_NOT (GET_MODE (x),
5302                               gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5303                                               GET_MODE (x),
5304                                               XEXP (XEXP (x, 0), 0),
5305                                               XEXP (XEXP (x, 1), 0))));
5306           return find_split_point (loc, insn, set_src);
5307         }
5308
5309       /* Many RISC machines have a large set of logical insns.  If the
5310          second operand is a NOT, put it first so we will try to split the
5311          other operand first.  */
5312       if (GET_CODE (XEXP (x, 1)) == NOT)
5313         {
5314           rtx tem = XEXP (x, 0);
5315           SUBST (XEXP (x, 0), XEXP (x, 1));
5316           SUBST (XEXP (x, 1), tem);
5317         }
5318       break;
5319
5320     case PLUS:
5321     case MINUS:
5322       /* Canonicalization can produce (minus A (mult B C)), where C is a
5323          constant.  It may be better to try splitting (plus (mult B -C) A)
5324          instead if this isn't a multiply by a power of two.  */
5325       if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5326           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5327           && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5328         {
5329           machine_mode mode = GET_MODE (x);
5330           unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5331           HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5332           SUBST (*loc, gen_rtx_PLUS (mode,
5333                                      gen_rtx_MULT (mode,
5334                                                    XEXP (XEXP (x, 1), 0),
5335                                                    gen_int_mode (other_int,
5336                                                                  mode)),
5337                                      XEXP (x, 0)));
5338           return find_split_point (loc, insn, set_src);
5339         }
5340
5341       /* Split at a multiply-accumulate instruction.  However if this is
5342          the SET_SRC, we likely do not have such an instruction and it's
5343          worthless to try this split.  */
5344       if (!set_src
5345           && (GET_CODE (XEXP (x, 0)) == MULT
5346               || (GET_CODE (XEXP (x, 0)) == ASHIFT
5347                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5348         return loc;
5349
5350     default:
5351       break;
5352     }
5353
5354   /* Otherwise, select our actions depending on our rtx class.  */
5355   switch (GET_RTX_CLASS (code))
5356     {
5357     case RTX_BITFIELD_OPS:              /* This is ZERO_EXTRACT and SIGN_EXTRACT.  */
5358     case RTX_TERNARY:
5359       split = find_split_point (&XEXP (x, 2), insn, false);
5360       if (split)
5361         return split;
5362       /* fall through */
5363     case RTX_BIN_ARITH:
5364     case RTX_COMM_ARITH:
5365     case RTX_COMPARE:
5366     case RTX_COMM_COMPARE:
5367       split = find_split_point (&XEXP (x, 1), insn, false);
5368       if (split)
5369         return split;
5370       /* fall through */
5371     case RTX_UNARY:
5372       /* Some machines have (and (shift ...) ...) insns.  If X is not
5373          an AND, but XEXP (X, 0) is, use it as our split point.  */
5374       if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5375         return &XEXP (x, 0);
5376
5377       split = find_split_point (&XEXP (x, 0), insn, false);
5378       if (split)
5379         return split;
5380       return loc;
5381
5382     default:
5383       /* Otherwise, we don't have a split point.  */
5384       return 0;
5385     }
5386 }
5387 \f
5388 /* Throughout X, replace FROM with TO, and return the result.
5389    The result is TO if X is FROM;
5390    otherwise the result is X, but its contents may have been modified.
5391    If they were modified, a record was made in undobuf so that
5392    undo_all will (among other things) return X to its original state.
5393
5394    If the number of changes necessary is too much to record to undo,
5395    the excess changes are not made, so the result is invalid.
5396    The changes already made can still be undone.
5397    undobuf.num_undo is incremented for such changes, so by testing that
5398    the caller can tell whether the result is valid.
5399
5400    `n_occurrences' is incremented each time FROM is replaced.
5401
5402    IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5403
5404    IN_COND is nonzero if we are at the top level of a condition.
5405
5406    UNIQUE_COPY is nonzero if each substitution must be unique.  We do this
5407    by copying if `n_occurrences' is nonzero.  */
5408
5409 static rtx
5410 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5411 {
5412   enum rtx_code code = GET_CODE (x);
5413   machine_mode op0_mode = VOIDmode;
5414   const char *fmt;
5415   int len, i;
5416   rtx new_rtx;
5417
5418 /* Two expressions are equal if they are identical copies of a shared
5419    RTX or if they are both registers with the same register number
5420    and mode.  */
5421
5422 #define COMBINE_RTX_EQUAL_P(X,Y)                        \
5423   ((X) == (Y)                                           \
5424    || (REG_P (X) && REG_P (Y)   \
5425        && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5426
5427   /* Do not substitute into clobbers of regs -- this will never result in
5428      valid RTL.  */
5429   if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5430     return x;
5431
5432   if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5433     {
5434       n_occurrences++;
5435       return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5436     }
5437
5438   /* If X and FROM are the same register but different modes, they
5439      will not have been seen as equal above.  However, the log links code
5440      will make a LOG_LINKS entry for that case.  If we do nothing, we
5441      will try to rerecognize our original insn and, when it succeeds,
5442      we will delete the feeding insn, which is incorrect.
5443
5444      So force this insn not to match in this (rare) case.  */
5445   if (! in_dest && code == REG && REG_P (from)
5446       && reg_overlap_mentioned_p (x, from))
5447     return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5448
5449   /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5450      of which may contain things that can be combined.  */
5451   if (code != MEM && code != LO_SUM && OBJECT_P (x))
5452     return x;
5453
5454   /* It is possible to have a subexpression appear twice in the insn.
5455      Suppose that FROM is a register that appears within TO.
5456      Then, after that subexpression has been scanned once by `subst',
5457      the second time it is scanned, TO may be found.  If we were
5458      to scan TO here, we would find FROM within it and create a
5459      self-referent rtl structure which is completely wrong.  */
5460   if (COMBINE_RTX_EQUAL_P (x, to))
5461     return to;
5462
5463   /* Parallel asm_operands need special attention because all of the
5464      inputs are shared across the arms.  Furthermore, unsharing the
5465      rtl results in recognition failures.  Failure to handle this case
5466      specially can result in circular rtl.
5467
5468      Solve this by doing a normal pass across the first entry of the
5469      parallel, and only processing the SET_DESTs of the subsequent
5470      entries.  Ug.  */
5471
5472   if (code == PARALLEL
5473       && GET_CODE (XVECEXP (x, 0, 0)) == SET
5474       && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5475     {
5476       new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5477
5478       /* If this substitution failed, this whole thing fails.  */
5479       if (GET_CODE (new_rtx) == CLOBBER
5480           && XEXP (new_rtx, 0) == const0_rtx)
5481         return new_rtx;
5482
5483       SUBST (XVECEXP (x, 0, 0), new_rtx);
5484
5485       for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5486         {
5487           rtx dest = SET_DEST (XVECEXP (x, 0, i));
5488
5489           if (!REG_P (dest)
5490               && GET_CODE (dest) != CC0
5491               && GET_CODE (dest) != PC)
5492             {
5493               new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5494
5495               /* If this substitution failed, this whole thing fails.  */
5496               if (GET_CODE (new_rtx) == CLOBBER
5497                   && XEXP (new_rtx, 0) == const0_rtx)
5498                 return new_rtx;
5499
5500               SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5501             }
5502         }
5503     }
5504   else
5505     {
5506       len = GET_RTX_LENGTH (code);
5507       fmt = GET_RTX_FORMAT (code);
5508
5509       /* We don't need to process a SET_DEST that is a register, CC0,
5510          or PC, so set up to skip this common case.  All other cases
5511          where we want to suppress replacing something inside a
5512          SET_SRC are handled via the IN_DEST operand.  */
5513       if (code == SET
5514           && (REG_P (SET_DEST (x))
5515               || GET_CODE (SET_DEST (x)) == CC0
5516               || GET_CODE (SET_DEST (x)) == PC))
5517         fmt = "ie";
5518
5519       /* Trying to simplify the operands of a widening MULT is not likely
5520          to create RTL matching a machine insn.  */
5521       if (code == MULT
5522           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5523               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5524           && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5525               || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5526           && REG_P (XEXP (XEXP (x, 0), 0))
5527           && REG_P (XEXP (XEXP (x, 1), 0))
5528           && from == to)
5529         return x;
5530
5531
5532       /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5533          constant.  */
5534       if (fmt[0] == 'e')
5535         op0_mode = GET_MODE (XEXP (x, 0));
5536
5537       for (i = 0; i < len; i++)
5538         {
5539           if (fmt[i] == 'E')
5540             {
5541               int j;
5542               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5543                 {
5544                   if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5545                     {
5546                       new_rtx = (unique_copy && n_occurrences
5547                              ? copy_rtx (to) : to);
5548                       n_occurrences++;
5549                     }
5550                   else
5551                     {
5552                       new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5553                                        unique_copy);
5554
5555                       /* If this substitution failed, this whole thing
5556                          fails.  */
5557                       if (GET_CODE (new_rtx) == CLOBBER
5558                           && XEXP (new_rtx, 0) == const0_rtx)
5559                         return new_rtx;
5560                     }
5561
5562                   SUBST (XVECEXP (x, i, j), new_rtx);
5563                 }
5564             }
5565           else if (fmt[i] == 'e')
5566             {
5567               /* If this is a register being set, ignore it.  */
5568               new_rtx = XEXP (x, i);
5569               if (in_dest
5570                   && i == 0
5571                   && (((code == SUBREG || code == ZERO_EXTRACT)
5572                        && REG_P (new_rtx))
5573                       || code == STRICT_LOW_PART))
5574                 ;
5575
5576               else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5577                 {
5578                   /* In general, don't install a subreg involving two
5579                      modes not tieable.  It can worsen register
5580                      allocation, and can even make invalid reload
5581                      insns, since the reg inside may need to be copied
5582                      from in the outside mode, and that may be invalid
5583                      if it is an fp reg copied in integer mode.
5584
5585                      We allow two exceptions to this: It is valid if
5586                      it is inside another SUBREG and the mode of that
5587                      SUBREG and the mode of the inside of TO is
5588                      tieable and it is valid if X is a SET that copies
5589                      FROM to CC0.  */
5590
5591                   if (GET_CODE (to) == SUBREG
5592                       && !targetm.modes_tieable_p (GET_MODE (to),
5593                                                    GET_MODE (SUBREG_REG (to)))
5594                       && ! (code == SUBREG
5595                             && (targetm.modes_tieable_p
5596                                 (GET_MODE (x), GET_MODE (SUBREG_REG (to)))))
5597                       && (!HAVE_cc0
5598                           || (! (code == SET
5599                                  && i == 1
5600                                  && XEXP (x, 0) == cc0_rtx))))
5601                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5602
5603                   if (code == SUBREG
5604                       && REG_P (to)
5605                       && REGNO (to) < FIRST_PSEUDO_REGISTER
5606                       && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5607                                                 SUBREG_BYTE (x),
5608                                                 GET_MODE (x)) < 0)
5609                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5610
5611                   new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5612                   n_occurrences++;
5613                 }
5614               else
5615                 /* If we are in a SET_DEST, suppress most cases unless we
5616                    have gone inside a MEM, in which case we want to
5617                    simplify the address.  We assume here that things that
5618                    are actually part of the destination have their inner
5619                    parts in the first expression.  This is true for SUBREG,
5620                    STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5621                    things aside from REG and MEM that should appear in a
5622                    SET_DEST.  */
5623                 new_rtx = subst (XEXP (x, i), from, to,
5624                              (((in_dest
5625                                 && (code == SUBREG || code == STRICT_LOW_PART
5626                                     || code == ZERO_EXTRACT))
5627                                || code == SET)
5628                               && i == 0),
5629                                  code == IF_THEN_ELSE && i == 0,
5630                                  unique_copy);
5631
5632               /* If we found that we will have to reject this combination,
5633                  indicate that by returning the CLOBBER ourselves, rather than
5634                  an expression containing it.  This will speed things up as
5635                  well as prevent accidents where two CLOBBERs are considered
5636                  to be equal, thus producing an incorrect simplification.  */
5637
5638               if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5639                 return new_rtx;
5640
5641               if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5642                 {
5643                   machine_mode mode = GET_MODE (x);
5644
5645                   x = simplify_subreg (GET_MODE (x), new_rtx,
5646                                        GET_MODE (SUBREG_REG (x)),
5647                                        SUBREG_BYTE (x));
5648                   if (! x)
5649                     x = gen_rtx_CLOBBER (mode, const0_rtx);
5650                 }
5651               else if (CONST_SCALAR_INT_P (new_rtx)
5652                        && (GET_CODE (x) == ZERO_EXTEND
5653                            || GET_CODE (x) == FLOAT
5654                            || GET_CODE (x) == UNSIGNED_FLOAT))
5655                 {
5656                   x = simplify_unary_operation (GET_CODE (x), GET_MODE (x),
5657                                                 new_rtx,
5658                                                 GET_MODE (XEXP (x, 0)));
5659                   if (!x)
5660                     return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5661                 }
5662               else
5663                 SUBST (XEXP (x, i), new_rtx);
5664             }
5665         }
5666     }
5667
5668   /* Check if we are loading something from the constant pool via float
5669      extension; in this case we would undo compress_float_constant
5670      optimization and degenerate constant load to an immediate value.  */
5671   if (GET_CODE (x) == FLOAT_EXTEND
5672       && MEM_P (XEXP (x, 0))
5673       && MEM_READONLY_P (XEXP (x, 0)))
5674     {
5675       rtx tmp = avoid_constant_pool_reference (x);
5676       if (x != tmp)
5677         return x;
5678     }
5679
5680   /* Try to simplify X.  If the simplification changed the code, it is likely
5681      that further simplification will help, so loop, but limit the number
5682      of repetitions that will be performed.  */
5683
5684   for (i = 0; i < 4; i++)
5685     {
5686       /* If X is sufficiently simple, don't bother trying to do anything
5687          with it.  */
5688       if (code != CONST_INT && code != REG && code != CLOBBER)
5689         x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5690
5691       if (GET_CODE (x) == code)
5692         break;
5693
5694       code = GET_CODE (x);
5695
5696       /* We no longer know the original mode of operand 0 since we
5697          have changed the form of X)  */
5698       op0_mode = VOIDmode;
5699     }
5700
5701   return x;
5702 }
5703 \f
5704 /* If X is a commutative operation whose operands are not in the canonical
5705    order, use substitutions to swap them.  */
5706
5707 static void
5708 maybe_swap_commutative_operands (rtx x)
5709 {
5710   if (COMMUTATIVE_ARITH_P (x)
5711       && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5712     {
5713       rtx temp = XEXP (x, 0);
5714       SUBST (XEXP (x, 0), XEXP (x, 1));
5715       SUBST (XEXP (x, 1), temp);
5716     }
5717 }
5718
5719 /* Simplify X, a piece of RTL.  We just operate on the expression at the
5720    outer level; call `subst' to simplify recursively.  Return the new
5721    expression.
5722
5723    OP0_MODE is the original mode of XEXP (x, 0).  IN_DEST is nonzero
5724    if we are inside a SET_DEST.  IN_COND is nonzero if we are at the top level
5725    of a condition.  */
5726
5727 static rtx
5728 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5729                       int in_cond)
5730 {
5731   enum rtx_code code = GET_CODE (x);
5732   machine_mode mode = GET_MODE (x);
5733   scalar_int_mode int_mode;
5734   rtx temp;
5735   int i;
5736
5737   /* If this is a commutative operation, put a constant last and a complex
5738      expression first.  We don't need to do this for comparisons here.  */
5739   maybe_swap_commutative_operands (x);
5740
5741   /* Try to fold this expression in case we have constants that weren't
5742      present before.  */
5743   temp = 0;
5744   switch (GET_RTX_CLASS (code))
5745     {
5746     case RTX_UNARY:
5747       if (op0_mode == VOIDmode)
5748         op0_mode = GET_MODE (XEXP (x, 0));
5749       temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5750       break;
5751     case RTX_COMPARE:
5752     case RTX_COMM_COMPARE:
5753       {
5754         machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5755         if (cmp_mode == VOIDmode)
5756           {
5757             cmp_mode = GET_MODE (XEXP (x, 1));
5758             if (cmp_mode == VOIDmode)
5759               cmp_mode = op0_mode;
5760           }
5761         temp = simplify_relational_operation (code, mode, cmp_mode,
5762                                               XEXP (x, 0), XEXP (x, 1));
5763       }
5764       break;
5765     case RTX_COMM_ARITH:
5766     case RTX_BIN_ARITH:
5767       temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5768       break;
5769     case RTX_BITFIELD_OPS:
5770     case RTX_TERNARY:
5771       temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5772                                          XEXP (x, 1), XEXP (x, 2));
5773       break;
5774     default:
5775       break;
5776     }
5777
5778   if (temp)
5779     {
5780       x = temp;
5781       code = GET_CODE (temp);
5782       op0_mode = VOIDmode;
5783       mode = GET_MODE (temp);
5784     }
5785
5786   /* If this is a simple operation applied to an IF_THEN_ELSE, try
5787      applying it to the arms of the IF_THEN_ELSE.  This often simplifies
5788      things.  Check for cases where both arms are testing the same
5789      condition.
5790
5791      Don't do anything if all operands are very simple.  */
5792
5793   if ((BINARY_P (x)
5794        && ((!OBJECT_P (XEXP (x, 0))
5795             && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5796                   && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5797            || (!OBJECT_P (XEXP (x, 1))
5798                && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5799                      && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5800       || (UNARY_P (x)
5801           && (!OBJECT_P (XEXP (x, 0))
5802                && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5803                      && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5804     {
5805       rtx cond, true_rtx, false_rtx;
5806
5807       cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5808       if (cond != 0
5809           /* If everything is a comparison, what we have is highly unlikely
5810              to be simpler, so don't use it.  */
5811           && ! (COMPARISON_P (x)
5812                 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5813           /* Similarly, if we end up with one of the expressions the same
5814              as the original, it is certainly not simpler.  */
5815           && ! rtx_equal_p (x, true_rtx)
5816           && ! rtx_equal_p (x, false_rtx))
5817         {
5818           rtx cop1 = const0_rtx;
5819           enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5820
5821           if (cond_code == NE && COMPARISON_P (cond))
5822             return x;
5823
5824           /* Simplify the alternative arms; this may collapse the true and
5825              false arms to store-flag values.  Be careful to use copy_rtx
5826              here since true_rtx or false_rtx might share RTL with x as a
5827              result of the if_then_else_cond call above.  */
5828           true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5829           false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5830
5831           /* If true_rtx and false_rtx are not general_operands, an if_then_else
5832              is unlikely to be simpler.  */
5833           if (general_operand (true_rtx, VOIDmode)
5834               && general_operand (false_rtx, VOIDmode))
5835             {
5836               enum rtx_code reversed;
5837
5838               /* Restarting if we generate a store-flag expression will cause
5839                  us to loop.  Just drop through in this case.  */
5840
5841               /* If the result values are STORE_FLAG_VALUE and zero, we can
5842                  just make the comparison operation.  */
5843               if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5844                 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5845                                              cond, cop1);
5846               else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5847                        && ((reversed = reversed_comparison_code_parts
5848                                         (cond_code, cond, cop1, NULL))
5849                            != UNKNOWN))
5850                 x = simplify_gen_relational (reversed, mode, VOIDmode,
5851                                              cond, cop1);
5852
5853               /* Likewise, we can make the negate of a comparison operation
5854                  if the result values are - STORE_FLAG_VALUE and zero.  */
5855               else if (CONST_INT_P (true_rtx)
5856                        && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5857                        && false_rtx == const0_rtx)
5858                 x = simplify_gen_unary (NEG, mode,
5859                                         simplify_gen_relational (cond_code,
5860                                                                  mode, VOIDmode,
5861                                                                  cond, cop1),
5862                                         mode);
5863               else if (CONST_INT_P (false_rtx)
5864                        && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5865                        && true_rtx == const0_rtx
5866                        && ((reversed = reversed_comparison_code_parts
5867                                         (cond_code, cond, cop1, NULL))
5868                            != UNKNOWN))
5869                 x = simplify_gen_unary (NEG, mode,
5870                                         simplify_gen_relational (reversed,
5871                                                                  mode, VOIDmode,
5872                                                                  cond, cop1),
5873                                         mode);
5874               else
5875                 return gen_rtx_IF_THEN_ELSE (mode,
5876                                              simplify_gen_relational (cond_code,
5877                                                                       mode,
5878                                                                       VOIDmode,
5879                                                                       cond,
5880                                                                       cop1),
5881                                              true_rtx, false_rtx);
5882
5883               code = GET_CODE (x);
5884               op0_mode = VOIDmode;
5885             }
5886         }
5887     }
5888
5889   /* First see if we can apply the inverse distributive law.  */
5890   if (code == PLUS || code == MINUS
5891       || code == AND || code == IOR || code == XOR)
5892     {
5893       x = apply_distributive_law (x);
5894       code = GET_CODE (x);
5895       op0_mode = VOIDmode;
5896     }
5897
5898   /* If CODE is an associative operation not otherwise handled, see if we
5899      can associate some operands.  This can win if they are constants or
5900      if they are logically related (i.e. (a & b) & a).  */
5901   if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5902        || code == AND || code == IOR || code == XOR
5903        || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5904       && ((INTEGRAL_MODE_P (mode) && code != DIV)
5905           || (flag_associative_math && FLOAT_MODE_P (mode))))
5906     {
5907       if (GET_CODE (XEXP (x, 0)) == code)
5908         {
5909           rtx other = XEXP (XEXP (x, 0), 0);
5910           rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5911           rtx inner_op1 = XEXP (x, 1);
5912           rtx inner;
5913
5914           /* Make sure we pass the constant operand if any as the second
5915              one if this is a commutative operation.  */
5916           if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5917             std::swap (inner_op0, inner_op1);
5918           inner = simplify_binary_operation (code == MINUS ? PLUS
5919                                              : code == DIV ? MULT
5920                                              : code,
5921                                              mode, inner_op0, inner_op1);
5922
5923           /* For commutative operations, try the other pair if that one
5924              didn't simplify.  */
5925           if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5926             {
5927               other = XEXP (XEXP (x, 0), 1);
5928               inner = simplify_binary_operation (code, mode,
5929                                                  XEXP (XEXP (x, 0), 0),
5930                                                  XEXP (x, 1));
5931             }
5932
5933           if (inner)
5934             return simplify_gen_binary (code, mode, other, inner);
5935         }
5936     }
5937
5938   /* A little bit of algebraic simplification here.  */
5939   switch (code)
5940     {
5941     case MEM:
5942       /* Ensure that our address has any ASHIFTs converted to MULT in case
5943          address-recognizing predicates are called later.  */
5944       temp = make_compound_operation (XEXP (x, 0), MEM);
5945       SUBST (XEXP (x, 0), temp);
5946       break;
5947
5948     case SUBREG:
5949       if (op0_mode == VOIDmode)
5950         op0_mode = GET_MODE (SUBREG_REG (x));
5951
5952       /* See if this can be moved to simplify_subreg.  */
5953       if (CONSTANT_P (SUBREG_REG (x))
5954           && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5955              /* Don't call gen_lowpart if the inner mode
5956                 is VOIDmode and we cannot simplify it, as SUBREG without
5957                 inner mode is invalid.  */
5958           && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5959               || gen_lowpart_common (mode, SUBREG_REG (x))))
5960         return gen_lowpart (mode, SUBREG_REG (x));
5961
5962       if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5963         break;
5964       {
5965         rtx temp;
5966         temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5967                                 SUBREG_BYTE (x));
5968         if (temp)
5969           return temp;
5970
5971         /* If op is known to have all lower bits zero, the result is zero.  */
5972         scalar_int_mode int_mode, int_op0_mode;
5973         if (!in_dest
5974             && is_a <scalar_int_mode> (mode, &int_mode)
5975             && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5976             && (GET_MODE_PRECISION (int_mode)
5977                 < GET_MODE_PRECISION (int_op0_mode))
5978             && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
5979                          SUBREG_BYTE (x))
5980             && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5981             && (nonzero_bits (SUBREG_REG (x), int_op0_mode)
5982                 & GET_MODE_MASK (int_mode)) == 0)
5983           return CONST0_RTX (int_mode);
5984       }
5985
5986       /* Don't change the mode of the MEM if that would change the meaning
5987          of the address.  */
5988       if (MEM_P (SUBREG_REG (x))
5989           && (MEM_VOLATILE_P (SUBREG_REG (x))
5990               || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5991                                            MEM_ADDR_SPACE (SUBREG_REG (x)))))
5992         return gen_rtx_CLOBBER (mode, const0_rtx);
5993
5994       /* Note that we cannot do any narrowing for non-constants since
5995          we might have been counting on using the fact that some bits were
5996          zero.  We now do this in the SET.  */
5997
5998       break;
5999
6000     case NEG:
6001       temp = expand_compound_operation (XEXP (x, 0));
6002
6003       /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
6004          replaced by (lshiftrt X C).  This will convert
6005          (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y).  */
6006
6007       if (GET_CODE (temp) == ASHIFTRT
6008           && CONST_INT_P (XEXP (temp, 1))
6009           && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
6010         return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
6011                                      INTVAL (XEXP (temp, 1)));
6012
6013       /* If X has only a single bit that might be nonzero, say, bit I, convert
6014          (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
6015          MODE minus 1.  This will convert (neg (zero_extract X 1 Y)) to
6016          (sign_extract X 1 Y).  But only do this if TEMP isn't a register
6017          or a SUBREG of one since we'd be making the expression more
6018          complex if it was just a register.  */
6019
6020       if (!REG_P (temp)
6021           && ! (GET_CODE (temp) == SUBREG
6022                 && REG_P (SUBREG_REG (temp)))
6023           && is_a <scalar_int_mode> (mode, &int_mode)
6024           && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
6025         {
6026           rtx temp1 = simplify_shift_const
6027             (NULL_RTX, ASHIFTRT, int_mode,
6028              simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
6029                                    GET_MODE_PRECISION (int_mode) - 1 - i),
6030              GET_MODE_PRECISION (int_mode) - 1 - i);
6031
6032           /* If all we did was surround TEMP with the two shifts, we
6033              haven't improved anything, so don't use it.  Otherwise,
6034              we are better off with TEMP1.  */
6035           if (GET_CODE (temp1) != ASHIFTRT
6036               || GET_CODE (XEXP (temp1, 0)) != ASHIFT
6037               || XEXP (XEXP (temp1, 0), 0) != temp)
6038             return temp1;
6039         }
6040       break;
6041
6042     case TRUNCATE:
6043       /* We can't handle truncation to a partial integer mode here
6044          because we don't know the real bitsize of the partial
6045          integer mode.  */
6046       if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6047         break;
6048
6049       if (HWI_COMPUTABLE_MODE_P (mode))
6050         SUBST (XEXP (x, 0),
6051                force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6052                               GET_MODE_MASK (mode), 0));
6053
6054       /* We can truncate a constant value and return it.  */
6055       {
6056         poly_int64 c;
6057         if (poly_int_rtx_p (XEXP (x, 0), &c))
6058           return gen_int_mode (c, mode);
6059       }
6060
6061       /* Similarly to what we do in simplify-rtx.c, a truncate of a register
6062          whose value is a comparison can be replaced with a subreg if
6063          STORE_FLAG_VALUE permits.  */
6064       if (HWI_COMPUTABLE_MODE_P (mode)
6065           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
6066           && (temp = get_last_value (XEXP (x, 0)))
6067           && COMPARISON_P (temp))
6068         return gen_lowpart (mode, XEXP (x, 0));
6069       break;
6070
6071     case CONST:
6072       /* (const (const X)) can become (const X).  Do it this way rather than
6073          returning the inner CONST since CONST can be shared with a
6074          REG_EQUAL note.  */
6075       if (GET_CODE (XEXP (x, 0)) == CONST)
6076         SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
6077       break;
6078
6079     case LO_SUM:
6080       /* Convert (lo_sum (high FOO) FOO) to FOO.  This is necessary so we
6081          can add in an offset.  find_split_point will split this address up
6082          again if it doesn't match.  */
6083       if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6084           && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6085         return XEXP (x, 1);
6086       break;
6087
6088     case PLUS:
6089       /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6090          when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6091          bit-field and can be replaced by either a sign_extend or a
6092          sign_extract.  The `and' may be a zero_extend and the two
6093          <c>, -<c> constants may be reversed.  */
6094       if (GET_CODE (XEXP (x, 0)) == XOR
6095           && is_a <scalar_int_mode> (mode, &int_mode)
6096           && CONST_INT_P (XEXP (x, 1))
6097           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6098           && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
6099           && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
6100               || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
6101           && HWI_COMPUTABLE_MODE_P (int_mode)
6102           && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6103                && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6104                && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6105                    == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6106               || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6107                   && known_eq ((GET_MODE_PRECISION
6108                                 (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6109                                (unsigned int) i + 1))))
6110         return simplify_shift_const
6111           (NULL_RTX, ASHIFTRT, int_mode,
6112            simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6113                                  XEXP (XEXP (XEXP (x, 0), 0), 0),
6114                                  GET_MODE_PRECISION (int_mode) - (i + 1)),
6115            GET_MODE_PRECISION (int_mode) - (i + 1));
6116
6117       /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6118          can become (ashiftrt (ashift (xor x 1) C) C) where C is
6119          the bitsize of the mode - 1.  This allows simplification of
6120          "a = (b & 8) == 0;"  */
6121       if (XEXP (x, 1) == constm1_rtx
6122           && !REG_P (XEXP (x, 0))
6123           && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6124                 && REG_P (SUBREG_REG (XEXP (x, 0))))
6125           && is_a <scalar_int_mode> (mode, &int_mode)
6126           && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6127         return simplify_shift_const
6128           (NULL_RTX, ASHIFTRT, int_mode,
6129            simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6130                                  gen_rtx_XOR (int_mode, XEXP (x, 0),
6131                                               const1_rtx),
6132                                  GET_MODE_PRECISION (int_mode) - 1),
6133            GET_MODE_PRECISION (int_mode) - 1);
6134
6135       /* If we are adding two things that have no bits in common, convert
6136          the addition into an IOR.  This will often be further simplified,
6137          for example in cases like ((a & 1) + (a & 2)), which can
6138          become a & 3.  */
6139
6140       if (HWI_COMPUTABLE_MODE_P (mode)
6141           && (nonzero_bits (XEXP (x, 0), mode)
6142               & nonzero_bits (XEXP (x, 1), mode)) == 0)
6143         {
6144           /* Try to simplify the expression further.  */
6145           rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6146           temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
6147
6148           /* If we could, great.  If not, do not go ahead with the IOR
6149              replacement, since PLUS appears in many special purpose
6150              address arithmetic instructions.  */
6151           if (GET_CODE (temp) != CLOBBER
6152               && (GET_CODE (temp) != IOR
6153                   || ((XEXP (temp, 0) != XEXP (x, 0)
6154                        || XEXP (temp, 1) != XEXP (x, 1))
6155                       && (XEXP (temp, 0) != XEXP (x, 1)
6156                           || XEXP (temp, 1) != XEXP (x, 0)))))
6157             return temp;
6158         }
6159
6160       /* Canonicalize x + x into x << 1.  */
6161       if (GET_MODE_CLASS (mode) == MODE_INT
6162           && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6163           && !side_effects_p (XEXP (x, 0)))
6164         return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6165
6166       break;
6167
6168     case MINUS:
6169       /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6170          (and <foo> (const_int pow2-1))  */
6171       if (is_a <scalar_int_mode> (mode, &int_mode)
6172           && GET_CODE (XEXP (x, 1)) == AND
6173           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6174           && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6175           && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6176         return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6177                                        -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6178       break;
6179
6180     case MULT:
6181       /* If we have (mult (plus A B) C), apply the distributive law and then
6182          the inverse distributive law to see if things simplify.  This
6183          occurs mostly in addresses, often when unrolling loops.  */
6184
6185       if (GET_CODE (XEXP (x, 0)) == PLUS)
6186         {
6187           rtx result = distribute_and_simplify_rtx (x, 0);
6188           if (result)
6189             return result;
6190         }
6191
6192       /* Try simplify a*(b/c) as (a*b)/c.  */
6193       if (FLOAT_MODE_P (mode) && flag_associative_math
6194           && GET_CODE (XEXP (x, 0)) == DIV)
6195         {
6196           rtx tem = simplify_binary_operation (MULT, mode,
6197                                                XEXP (XEXP (x, 0), 0),
6198                                                XEXP (x, 1));
6199           if (tem)
6200             return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6201         }
6202       break;
6203
6204     case UDIV:
6205       /* If this is a divide by a power of two, treat it as a shift if
6206          its first operand is a shift.  */
6207       if (is_a <scalar_int_mode> (mode, &int_mode)
6208           && CONST_INT_P (XEXP (x, 1))
6209           && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6210           && (GET_CODE (XEXP (x, 0)) == ASHIFT
6211               || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6212               || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6213               || GET_CODE (XEXP (x, 0)) == ROTATE
6214               || GET_CODE (XEXP (x, 0)) == ROTATERT))
6215         return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6216                                      XEXP (x, 0), i);
6217       break;
6218
6219     case EQ:  case NE:
6220     case GT:  case GTU:  case GE:  case GEU:
6221     case LT:  case LTU:  case LE:  case LEU:
6222     case UNEQ:  case LTGT:
6223     case UNGT:  case UNGE:
6224     case UNLT:  case UNLE:
6225     case UNORDERED: case ORDERED:
6226       /* If the first operand is a condition code, we can't do anything
6227          with it.  */
6228       if (GET_CODE (XEXP (x, 0)) == COMPARE
6229           || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6230               && ! CC0_P (XEXP (x, 0))))
6231         {
6232           rtx op0 = XEXP (x, 0);
6233           rtx op1 = XEXP (x, 1);
6234           enum rtx_code new_code;
6235
6236           if (GET_CODE (op0) == COMPARE)
6237             op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6238
6239           /* Simplify our comparison, if possible.  */
6240           new_code = simplify_comparison (code, &op0, &op1);
6241
6242           /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6243              if only the low-order bit is possibly nonzero in X (such as when
6244              X is a ZERO_EXTRACT of one bit).  Similarly, we can convert EQ to
6245              (xor X 1) or (minus 1 X); we use the former.  Finally, if X is
6246              known to be either 0 or -1, NE becomes a NEG and EQ becomes
6247              (plus X 1).
6248
6249              Remove any ZERO_EXTRACT we made when thinking this was a
6250              comparison.  It may now be simpler to use, e.g., an AND.  If a
6251              ZERO_EXTRACT is indeed appropriate, it will be placed back by
6252              the call to make_compound_operation in the SET case.
6253
6254              Don't apply these optimizations if the caller would
6255              prefer a comparison rather than a value.
6256              E.g., for the condition in an IF_THEN_ELSE most targets need
6257              an explicit comparison.  */
6258
6259           if (in_cond)
6260             ;
6261
6262           else if (STORE_FLAG_VALUE == 1
6263                    && new_code == NE
6264                    && is_int_mode (mode, &int_mode)
6265                    && op1 == const0_rtx
6266                    && int_mode == GET_MODE (op0)
6267                    && nonzero_bits (op0, int_mode) == 1)
6268             return gen_lowpart (int_mode,
6269                                 expand_compound_operation (op0));
6270
6271           else if (STORE_FLAG_VALUE == 1
6272                    && new_code == NE
6273                    && is_int_mode (mode, &int_mode)
6274                    && op1 == const0_rtx
6275                    && int_mode == GET_MODE (op0)
6276                    && (num_sign_bit_copies (op0, int_mode)
6277                        == GET_MODE_PRECISION (int_mode)))
6278             {
6279               op0 = expand_compound_operation (op0);
6280               return simplify_gen_unary (NEG, int_mode,
6281                                          gen_lowpart (int_mode, op0),
6282                                          int_mode);
6283             }
6284
6285           else if (STORE_FLAG_VALUE == 1
6286                    && new_code == EQ
6287                    && is_int_mode (mode, &int_mode)
6288                    && op1 == const0_rtx
6289                    && int_mode == GET_MODE (op0)
6290                    && nonzero_bits (op0, int_mode) == 1)
6291             {
6292               op0 = expand_compound_operation (op0);
6293               return simplify_gen_binary (XOR, int_mode,
6294                                           gen_lowpart (int_mode, op0),
6295                                           const1_rtx);
6296             }
6297
6298           else if (STORE_FLAG_VALUE == 1
6299                    && new_code == EQ
6300                    && is_int_mode (mode, &int_mode)
6301                    && op1 == const0_rtx
6302                    && int_mode == GET_MODE (op0)
6303                    && (num_sign_bit_copies (op0, int_mode)
6304                        == GET_MODE_PRECISION (int_mode)))
6305             {
6306               op0 = expand_compound_operation (op0);
6307               return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6308             }
6309
6310           /* If STORE_FLAG_VALUE is -1, we have cases similar to
6311              those above.  */
6312           if (in_cond)
6313             ;
6314
6315           else if (STORE_FLAG_VALUE == -1
6316                    && new_code == NE
6317                    && is_int_mode (mode, &int_mode)
6318                    && op1 == const0_rtx
6319                    && int_mode == GET_MODE (op0)
6320                    && (num_sign_bit_copies (op0, int_mode)
6321                        == GET_MODE_PRECISION (int_mode)))
6322             return gen_lowpart (int_mode, expand_compound_operation (op0));
6323
6324           else if (STORE_FLAG_VALUE == -1
6325                    && new_code == NE
6326                    && is_int_mode (mode, &int_mode)
6327                    && op1 == const0_rtx
6328                    && int_mode == GET_MODE (op0)
6329                    && nonzero_bits (op0, int_mode) == 1)
6330             {
6331               op0 = expand_compound_operation (op0);
6332               return simplify_gen_unary (NEG, int_mode,
6333                                          gen_lowpart (int_mode, op0),
6334                                          int_mode);
6335             }
6336
6337           else if (STORE_FLAG_VALUE == -1
6338                    && new_code == EQ
6339                    && is_int_mode (mode, &int_mode)
6340                    && op1 == const0_rtx
6341                    && int_mode == GET_MODE (op0)
6342                    && (num_sign_bit_copies (op0, int_mode)
6343                        == GET_MODE_PRECISION (int_mode)))
6344             {
6345               op0 = expand_compound_operation (op0);
6346               return simplify_gen_unary (NOT, int_mode,
6347                                          gen_lowpart (int_mode, op0),
6348                                          int_mode);
6349             }
6350
6351           /* If X is 0/1, (eq X 0) is X-1.  */
6352           else if (STORE_FLAG_VALUE == -1
6353                    && new_code == EQ
6354                    && is_int_mode (mode, &int_mode)
6355                    && op1 == const0_rtx
6356                    && int_mode == GET_MODE (op0)
6357                    && nonzero_bits (op0, int_mode) == 1)
6358             {
6359               op0 = expand_compound_operation (op0);
6360               return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6361             }
6362
6363           /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6364              one bit that might be nonzero, we can convert (ne x 0) to
6365              (ashift x c) where C puts the bit in the sign bit.  Remove any
6366              AND with STORE_FLAG_VALUE when we are done, since we are only
6367              going to test the sign bit.  */
6368           if (new_code == NE
6369               && is_int_mode (mode, &int_mode)
6370               && HWI_COMPUTABLE_MODE_P (int_mode)
6371               && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6372               && op1 == const0_rtx
6373               && int_mode == GET_MODE (op0)
6374               && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6375             {
6376               x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6377                                         expand_compound_operation (op0),
6378                                         GET_MODE_PRECISION (int_mode) - 1 - i);
6379               if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6380                 return XEXP (x, 0);
6381               else
6382                 return x;
6383             }
6384
6385           /* If the code changed, return a whole new comparison.
6386              We also need to avoid using SUBST in cases where
6387              simplify_comparison has widened a comparison with a CONST_INT,
6388              since in that case the wider CONST_INT may fail the sanity
6389              checks in do_SUBST.  */
6390           if (new_code != code
6391               || (CONST_INT_P (op1)
6392                   && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6393                   && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6394             return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6395
6396           /* Otherwise, keep this operation, but maybe change its operands.
6397              This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR).  */
6398           SUBST (XEXP (x, 0), op0);
6399           SUBST (XEXP (x, 1), op1);
6400         }
6401       break;
6402
6403     case IF_THEN_ELSE:
6404       return simplify_if_then_else (x);
6405
6406     case ZERO_EXTRACT:
6407     case SIGN_EXTRACT:
6408     case ZERO_EXTEND:
6409     case SIGN_EXTEND:
6410       /* If we are processing SET_DEST, we are done.  */
6411       if (in_dest)
6412         return x;
6413
6414       return expand_compound_operation (x);
6415
6416     case SET:
6417       return simplify_set (x);
6418
6419     case AND:
6420     case IOR:
6421       return simplify_logical (x);
6422
6423     case ASHIFT:
6424     case LSHIFTRT:
6425     case ASHIFTRT:
6426     case ROTATE:
6427     case ROTATERT:
6428       /* If this is a shift by a constant amount, simplify it.  */
6429       if (CONST_INT_P (XEXP (x, 1)))
6430         return simplify_shift_const (x, code, mode, XEXP (x, 0),
6431                                      INTVAL (XEXP (x, 1)));
6432
6433       else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6434         SUBST (XEXP (x, 1),
6435                force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6436                               (HOST_WIDE_INT_1U
6437                                << exact_log2 (GET_MODE_UNIT_BITSIZE
6438                                               (GET_MODE (x))))
6439                               - 1,
6440                               0));
6441       break;
6442
6443     default:
6444       break;
6445     }
6446
6447   return x;
6448 }
6449 \f
6450 /* Simplify X, an IF_THEN_ELSE expression.  Return the new expression.  */
6451
6452 static rtx
6453 simplify_if_then_else (rtx x)
6454 {
6455   machine_mode mode = GET_MODE (x);
6456   rtx cond = XEXP (x, 0);
6457   rtx true_rtx = XEXP (x, 1);
6458   rtx false_rtx = XEXP (x, 2);
6459   enum rtx_code true_code = GET_CODE (cond);
6460   int comparison_p = COMPARISON_P (cond);
6461   rtx temp;
6462   int i;
6463   enum rtx_code false_code;
6464   rtx reversed;
6465   scalar_int_mode int_mode, inner_mode;
6466
6467   /* Simplify storing of the truth value.  */
6468   if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6469     return simplify_gen_relational (true_code, mode, VOIDmode,
6470                                     XEXP (cond, 0), XEXP (cond, 1));
6471
6472   /* Also when the truth value has to be reversed.  */
6473   if (comparison_p
6474       && true_rtx == const0_rtx && false_rtx == const_true_rtx
6475       && (reversed = reversed_comparison (cond, mode)))
6476     return reversed;
6477
6478   /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6479      in it is being compared against certain values.  Get the true and false
6480      comparisons and see if that says anything about the value of each arm.  */
6481
6482   if (comparison_p
6483       && ((false_code = reversed_comparison_code (cond, NULL))
6484           != UNKNOWN)
6485       && REG_P (XEXP (cond, 0)))
6486     {
6487       HOST_WIDE_INT nzb;
6488       rtx from = XEXP (cond, 0);
6489       rtx true_val = XEXP (cond, 1);
6490       rtx false_val = true_val;
6491       int swapped = 0;
6492
6493       /* If FALSE_CODE is EQ, swap the codes and arms.  */
6494
6495       if (false_code == EQ)
6496         {
6497           swapped = 1, true_code = EQ, false_code = NE;
6498           std::swap (true_rtx, false_rtx);
6499         }
6500
6501       scalar_int_mode from_mode;
6502       if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6503         {
6504           /* If we are comparing against zero and the expression being
6505              tested has only a single bit that might be nonzero, that is
6506              its value when it is not equal to zero.  Similarly if it is
6507              known to be -1 or 0.  */
6508           if (true_code == EQ
6509               && true_val == const0_rtx
6510               && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6511             {
6512               false_code = EQ;
6513               false_val = gen_int_mode (nzb, from_mode);
6514             }
6515           else if (true_code == EQ
6516                    && true_val == const0_rtx
6517                    && (num_sign_bit_copies (from, from_mode)
6518                        == GET_MODE_PRECISION (from_mode)))
6519             {
6520               false_code = EQ;
6521               false_val = constm1_rtx;
6522             }
6523         }
6524
6525       /* Now simplify an arm if we know the value of the register in the
6526          branch and it is used in the arm.  Be careful due to the potential
6527          of locally-shared RTL.  */
6528
6529       if (reg_mentioned_p (from, true_rtx))
6530         true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6531                                       from, true_val),
6532                           pc_rtx, pc_rtx, 0, 0, 0);
6533       if (reg_mentioned_p (from, false_rtx))
6534         false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6535                                        from, false_val),
6536                            pc_rtx, pc_rtx, 0, 0, 0);
6537
6538       SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6539       SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6540
6541       true_rtx = XEXP (x, 1);
6542       false_rtx = XEXP (x, 2);
6543       true_code = GET_CODE (cond);
6544     }
6545
6546   /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6547      reversed, do so to avoid needing two sets of patterns for
6548      subtract-and-branch insns.  Similarly if we have a constant in the true
6549      arm, the false arm is the same as the first operand of the comparison, or
6550      the false arm is more complicated than the true arm.  */
6551
6552   if (comparison_p
6553       && reversed_comparison_code (cond, NULL) != UNKNOWN
6554       && (true_rtx == pc_rtx
6555           || (CONSTANT_P (true_rtx)
6556               && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6557           || true_rtx == const0_rtx
6558           || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6559           || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6560               && !OBJECT_P (false_rtx))
6561           || reg_mentioned_p (true_rtx, false_rtx)
6562           || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6563     {
6564       true_code = reversed_comparison_code (cond, NULL);
6565       SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6566       SUBST (XEXP (x, 1), false_rtx);
6567       SUBST (XEXP (x, 2), true_rtx);
6568
6569       std::swap (true_rtx, false_rtx);
6570       cond = XEXP (x, 0);
6571
6572       /* It is possible that the conditional has been simplified out.  */
6573       true_code = GET_CODE (cond);
6574       comparison_p = COMPARISON_P (cond);
6575     }
6576
6577   /* If the two arms are identical, we don't need the comparison.  */
6578
6579   if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6580     return true_rtx;
6581
6582   /* Convert a == b ? b : a to "a".  */
6583   if (true_code == EQ && ! side_effects_p (cond)
6584       && !HONOR_NANS (mode)
6585       && rtx_equal_p (XEXP (cond, 0), false_rtx)
6586       && rtx_equal_p (XEXP (cond, 1), true_rtx))
6587     return false_rtx;
6588   else if (true_code == NE && ! side_effects_p (cond)
6589            && !HONOR_NANS (mode)
6590            && rtx_equal_p (XEXP (cond, 0), true_rtx)
6591            && rtx_equal_p (XEXP (cond, 1), false_rtx))
6592     return true_rtx;
6593
6594   /* Look for cases where we have (abs x) or (neg (abs X)).  */
6595
6596   if (GET_MODE_CLASS (mode) == MODE_INT
6597       && comparison_p
6598       && XEXP (cond, 1) == const0_rtx
6599       && GET_CODE (false_rtx) == NEG
6600       && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6601       && rtx_equal_p (true_rtx, XEXP (cond, 0))
6602       && ! side_effects_p (true_rtx))
6603     switch (true_code)
6604       {
6605       case GT:
6606       case GE:
6607         return simplify_gen_unary (ABS, mode, true_rtx, mode);
6608       case LT:
6609       case LE:
6610         return
6611           simplify_gen_unary (NEG, mode,
6612                               simplify_gen_unary (ABS, mode, true_rtx, mode),
6613                               mode);
6614       default:
6615         break;
6616       }
6617
6618   /* Look for MIN or MAX.  */
6619
6620   if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6621       && comparison_p
6622       && rtx_equal_p (XEXP (cond, 0), true_rtx)
6623       && rtx_equal_p (XEXP (cond, 1), false_rtx)
6624       && ! side_effects_p (cond))
6625     switch (true_code)
6626       {
6627       case GE:
6628       case GT:
6629         return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6630       case LE:
6631       case LT:
6632         return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6633       case GEU:
6634       case GTU:
6635         return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6636       case LEU:
6637       case LTU:
6638         return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6639       default:
6640         break;
6641       }
6642
6643   /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6644      second operand is zero, this can be done as (OP Z (mult COND C2)) where
6645      C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6646      SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6647      We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6648      neither 1 or -1, but it isn't worth checking for.  */
6649
6650   if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6651       && comparison_p
6652       && is_int_mode (mode, &int_mode)
6653       && ! side_effects_p (x))
6654     {
6655       rtx t = make_compound_operation (true_rtx, SET);
6656       rtx f = make_compound_operation (false_rtx, SET);
6657       rtx cond_op0 = XEXP (cond, 0);
6658       rtx cond_op1 = XEXP (cond, 1);
6659       enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6660       scalar_int_mode m = int_mode;
6661       rtx z = 0, c1 = NULL_RTX;
6662
6663       if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6664            || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6665            || GET_CODE (t) == ASHIFT
6666            || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6667           && rtx_equal_p (XEXP (t, 0), f))
6668         c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6669
6670       /* If an identity-zero op is commutative, check whether there
6671          would be a match if we swapped the operands.  */
6672       else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6673                 || GET_CODE (t) == XOR)
6674                && rtx_equal_p (XEXP (t, 1), f))
6675         c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6676       else if (GET_CODE (t) == SIGN_EXTEND
6677                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6678                && (GET_CODE (XEXP (t, 0)) == PLUS
6679                    || GET_CODE (XEXP (t, 0)) == MINUS
6680                    || GET_CODE (XEXP (t, 0)) == IOR
6681                    || GET_CODE (XEXP (t, 0)) == XOR
6682                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6683                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6684                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6685                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6686                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6687                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6688                && (num_sign_bit_copies (f, GET_MODE (f))
6689                    > (unsigned int)
6690                      (GET_MODE_PRECISION (int_mode)
6691                       - GET_MODE_PRECISION (inner_mode))))
6692         {
6693           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6694           extend_op = SIGN_EXTEND;
6695           m = inner_mode;
6696         }
6697       else if (GET_CODE (t) == SIGN_EXTEND
6698                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6699                && (GET_CODE (XEXP (t, 0)) == PLUS
6700                    || GET_CODE (XEXP (t, 0)) == IOR
6701                    || GET_CODE (XEXP (t, 0)) == XOR)
6702                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6703                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6704                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6705                && (num_sign_bit_copies (f, GET_MODE (f))
6706                    > (unsigned int)
6707                      (GET_MODE_PRECISION (int_mode)
6708                       - GET_MODE_PRECISION (inner_mode))))
6709         {
6710           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6711           extend_op = SIGN_EXTEND;
6712           m = inner_mode;
6713         }
6714       else if (GET_CODE (t) == ZERO_EXTEND
6715                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6716                && (GET_CODE (XEXP (t, 0)) == PLUS
6717                    || GET_CODE (XEXP (t, 0)) == MINUS
6718                    || GET_CODE (XEXP (t, 0)) == IOR
6719                    || GET_CODE (XEXP (t, 0)) == XOR
6720                    || GET_CODE (XEXP (t, 0)) == ASHIFT
6721                    || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6722                    || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6723                && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6724                && HWI_COMPUTABLE_MODE_P (int_mode)
6725                && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6726                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6727                && ((nonzero_bits (f, GET_MODE (f))
6728                     & ~GET_MODE_MASK (inner_mode))
6729                    == 0))
6730         {
6731           c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6732           extend_op = ZERO_EXTEND;
6733           m = inner_mode;
6734         }
6735       else if (GET_CODE (t) == ZERO_EXTEND
6736                && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6737                && (GET_CODE (XEXP (t, 0)) == PLUS
6738                    || GET_CODE (XEXP (t, 0)) == IOR
6739                    || GET_CODE (XEXP (t, 0)) == XOR)
6740                && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6741                && HWI_COMPUTABLE_MODE_P (int_mode)
6742                && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6743                && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6744                && ((nonzero_bits (f, GET_MODE (f))
6745                     & ~GET_MODE_MASK (inner_mode))
6746                    == 0))
6747         {
6748           c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6749           extend_op = ZERO_EXTEND;
6750           m = inner_mode;
6751         }
6752
6753       if (z)
6754         {
6755           machine_mode cm = m;
6756           if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6757               && GET_MODE (c1) != VOIDmode)
6758             cm = GET_MODE (c1);
6759           temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6760                                                  cond_op0, cond_op1),
6761                         pc_rtx, pc_rtx, 0, 0, 0);
6762           temp = simplify_gen_binary (MULT, cm, temp,
6763                                       simplify_gen_binary (MULT, cm, c1,
6764                                                            const_true_rtx));
6765           temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6766           temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6767
6768           if (extend_op != UNKNOWN)
6769             temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6770
6771           return temp;
6772         }
6773     }
6774
6775   /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6776      1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6777      negation of a single bit, we can convert this operation to a shift.  We
6778      can actually do this more generally, but it doesn't seem worth it.  */
6779
6780   if (true_code == NE
6781       && is_a <scalar_int_mode> (mode, &int_mode)
6782       && XEXP (cond, 1) == const0_rtx
6783       && false_rtx == const0_rtx
6784       && CONST_INT_P (true_rtx)
6785       && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6786            && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6787           || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6788                == GET_MODE_PRECISION (int_mode))
6789               && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6790     return
6791       simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6792                             gen_lowpart (int_mode, XEXP (cond, 0)), i);
6793
6794   /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6795      non-zero bit in A is C1.  */
6796   if (true_code == NE && XEXP (cond, 1) == const0_rtx
6797       && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6798       && is_a <scalar_int_mode> (mode, &int_mode)
6799       && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6800       && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6801           == nonzero_bits (XEXP (cond, 0), inner_mode)
6802       && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6803     {
6804       rtx val = XEXP (cond, 0);
6805       if (inner_mode == int_mode)
6806         return val;
6807       else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6808         return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6809     }
6810
6811   return x;
6812 }
6813 \f
6814 /* Simplify X, a SET expression.  Return the new expression.  */
6815
6816 static rtx
6817 simplify_set (rtx x)
6818 {
6819   rtx src = SET_SRC (x);
6820   rtx dest = SET_DEST (x);
6821   machine_mode mode
6822     = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6823   rtx_insn *other_insn;
6824   rtx *cc_use;
6825   scalar_int_mode int_mode;
6826
6827   /* (set (pc) (return)) gets written as (return).  */
6828   if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6829     return src;
6830
6831   /* Now that we know for sure which bits of SRC we are using, see if we can
6832      simplify the expression for the object knowing that we only need the
6833      low-order bits.  */
6834
6835   if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6836     {
6837       src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6838       SUBST (SET_SRC (x), src);
6839     }
6840
6841   /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6842      the comparison result and try to simplify it unless we already have used
6843      undobuf.other_insn.  */
6844   if ((GET_MODE_CLASS (mode) == MODE_CC
6845        || GET_CODE (src) == COMPARE
6846        || CC0_P (dest))
6847       && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6848       && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6849       && COMPARISON_P (*cc_use)
6850       && rtx_equal_p (XEXP (*cc_use, 0), dest))
6851     {
6852       enum rtx_code old_code = GET_CODE (*cc_use);
6853       enum rtx_code new_code;
6854       rtx op0, op1, tmp;
6855       int other_changed = 0;
6856       rtx inner_compare = NULL_RTX;
6857       machine_mode compare_mode = GET_MODE (dest);
6858
6859       if (GET_CODE (src) == COMPARE)
6860         {
6861           op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6862           if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6863             {
6864               inner_compare = op0;
6865               op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6866             }
6867         }
6868       else
6869         op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6870
6871       tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6872                                            op0, op1);
6873       if (!tmp)
6874         new_code = old_code;
6875       else if (!CONSTANT_P (tmp))
6876         {
6877           new_code = GET_CODE (tmp);
6878           op0 = XEXP (tmp, 0);
6879           op1 = XEXP (tmp, 1);
6880         }
6881       else
6882         {
6883           rtx pat = PATTERN (other_insn);
6884           undobuf.other_insn = other_insn;
6885           SUBST (*cc_use, tmp);
6886
6887           /* Attempt to simplify CC user.  */
6888           if (GET_CODE (pat) == SET)
6889             {
6890               rtx new_rtx = simplify_rtx (SET_SRC (pat));
6891               if (new_rtx != NULL_RTX)
6892                 SUBST (SET_SRC (pat), new_rtx);
6893             }
6894
6895           /* Convert X into a no-op move.  */
6896           SUBST (SET_DEST (x), pc_rtx);
6897           SUBST (SET_SRC (x), pc_rtx);
6898           return x;
6899         }
6900
6901       /* Simplify our comparison, if possible.  */
6902       new_code = simplify_comparison (new_code, &op0, &op1);
6903
6904 #ifdef SELECT_CC_MODE
6905       /* If this machine has CC modes other than CCmode, check to see if we
6906          need to use a different CC mode here.  */
6907       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6908         compare_mode = GET_MODE (op0);
6909       else if (inner_compare
6910                && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6911                && new_code == old_code
6912                && op0 == XEXP (inner_compare, 0)
6913                && op1 == XEXP (inner_compare, 1))
6914         compare_mode = GET_MODE (inner_compare);
6915       else
6916         compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6917
6918       /* If the mode changed, we have to change SET_DEST, the mode in the
6919          compare, and the mode in the place SET_DEST is used.  If SET_DEST is
6920          a hard register, just build new versions with the proper mode.  If it
6921          is a pseudo, we lose unless it is only time we set the pseudo, in
6922          which case we can safely change its mode.  */
6923       if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6924         {
6925           if (can_change_dest_mode (dest, 0, compare_mode))
6926             {
6927               unsigned int regno = REGNO (dest);
6928               rtx new_dest;
6929
6930               if (regno < FIRST_PSEUDO_REGISTER)
6931                 new_dest = gen_rtx_REG (compare_mode, regno);
6932               else
6933                 {
6934                   SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6935                   new_dest = regno_reg_rtx[regno];
6936                 }
6937
6938               SUBST (SET_DEST (x), new_dest);
6939               SUBST (XEXP (*cc_use, 0), new_dest);
6940               other_changed = 1;
6941
6942               dest = new_dest;
6943             }
6944         }
6945 #endif  /* SELECT_CC_MODE */
6946
6947       /* If the code changed, we have to build a new comparison in
6948          undobuf.other_insn.  */
6949       if (new_code != old_code)
6950         {
6951           int other_changed_previously = other_changed;
6952           unsigned HOST_WIDE_INT mask;
6953           rtx old_cc_use = *cc_use;
6954
6955           SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6956                                           dest, const0_rtx));
6957           other_changed = 1;
6958
6959           /* If the only change we made was to change an EQ into an NE or
6960              vice versa, OP0 has only one bit that might be nonzero, and OP1
6961              is zero, check if changing the user of the condition code will
6962              produce a valid insn.  If it won't, we can keep the original code
6963              in that insn by surrounding our operation with an XOR.  */
6964
6965           if (((old_code == NE && new_code == EQ)
6966                || (old_code == EQ && new_code == NE))
6967               && ! other_changed_previously && op1 == const0_rtx
6968               && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6969               && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6970             {
6971               rtx pat = PATTERN (other_insn), note = 0;
6972
6973               if ((recog_for_combine (&pat, other_insn, &note) < 0
6974                    && ! check_asm_operands (pat)))
6975                 {
6976                   *cc_use = old_cc_use;
6977                   other_changed = 0;
6978
6979                   op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6980                                              gen_int_mode (mask,
6981                                                            GET_MODE (op0)));
6982                 }
6983             }
6984         }
6985
6986       if (other_changed)
6987         undobuf.other_insn = other_insn;
6988
6989       /* Don't generate a compare of a CC with 0, just use that CC.  */
6990       if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6991         {
6992           SUBST (SET_SRC (x), op0);
6993           src = SET_SRC (x);
6994         }
6995       /* Otherwise, if we didn't previously have the same COMPARE we
6996          want, create it from scratch.  */
6997       else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6998                || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6999         {
7000           SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
7001           src = SET_SRC (x);
7002         }
7003     }
7004   else
7005     {
7006       /* Get SET_SRC in a form where we have placed back any
7007          compound expressions.  Then do the checks below.  */
7008       src = make_compound_operation (src, SET);
7009       SUBST (SET_SRC (x), src);
7010     }
7011
7012   /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
7013      and X being a REG or (subreg (reg)), we may be able to convert this to
7014      (set (subreg:m2 x) (op)).
7015
7016      We can always do this if M1 is narrower than M2 because that means that
7017      we only care about the low bits of the result.
7018
7019      However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
7020      perform a narrower operation than requested since the high-order bits will
7021      be undefined.  On machine where it is defined, this transformation is safe
7022      as long as M1 and M2 have the same number of words.  */
7023
7024   if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
7025       && !OBJECT_P (SUBREG_REG (src))
7026       && (known_equal_after_align_up
7027           (GET_MODE_SIZE (GET_MODE (src)),
7028            GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
7029            UNITS_PER_WORD))
7030       && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
7031       && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
7032             && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
7033                                        GET_MODE (SUBREG_REG (src)),
7034                                        GET_MODE (src)))
7035       && (REG_P (dest)
7036           || (GET_CODE (dest) == SUBREG
7037               && REG_P (SUBREG_REG (dest)))))
7038     {
7039       SUBST (SET_DEST (x),
7040              gen_lowpart (GET_MODE (SUBREG_REG (src)),
7041                                       dest));
7042       SUBST (SET_SRC (x), SUBREG_REG (src));
7043
7044       src = SET_SRC (x), dest = SET_DEST (x);
7045     }
7046
7047   /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
7048      in SRC.  */
7049   if (dest == cc0_rtx
7050       && partial_subreg_p (src)
7051       && subreg_lowpart_p (src))
7052     {
7053       rtx inner = SUBREG_REG (src);
7054       machine_mode inner_mode = GET_MODE (inner);
7055
7056       /* Here we make sure that we don't have a sign bit on.  */
7057       if (val_signbit_known_clear_p (GET_MODE (src),
7058                                      nonzero_bits (inner, inner_mode)))
7059         {
7060           SUBST (SET_SRC (x), inner);
7061           src = SET_SRC (x);
7062         }
7063     }
7064
7065   /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
7066      would require a paradoxical subreg.  Replace the subreg with a
7067      zero_extend to avoid the reload that would otherwise be required.
7068      Don't do this unless we have a scalar integer mode, otherwise the
7069      transformation is incorrect.  */
7070
7071   enum rtx_code extend_op;
7072   if (paradoxical_subreg_p (src)
7073       && MEM_P (SUBREG_REG (src))
7074       && SCALAR_INT_MODE_P (GET_MODE (src))
7075       && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
7076     {
7077       SUBST (SET_SRC (x),
7078              gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
7079
7080       src = SET_SRC (x);
7081     }
7082
7083   /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7084      are comparing an item known to be 0 or -1 against 0, use a logical
7085      operation instead. Check for one of the arms being an IOR of the other
7086      arm with some value.  We compute three terms to be IOR'ed together.  In
7087      practice, at most two will be nonzero.  Then we do the IOR's.  */
7088
7089   if (GET_CODE (dest) != PC
7090       && GET_CODE (src) == IF_THEN_ELSE
7091       && is_int_mode (GET_MODE (src), &int_mode)
7092       && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7093       && XEXP (XEXP (src, 0), 1) == const0_rtx
7094       && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7095       && (!HAVE_conditional_move
7096           || ! can_conditionally_move_p (int_mode))
7097       && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7098           == GET_MODE_PRECISION (int_mode))
7099       && ! side_effects_p (src))
7100     {
7101       rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7102                       ? XEXP (src, 1) : XEXP (src, 2));
7103       rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7104                    ? XEXP (src, 2) : XEXP (src, 1));
7105       rtx term1 = const0_rtx, term2, term3;
7106
7107       if (GET_CODE (true_rtx) == IOR
7108           && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7109         term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7110       else if (GET_CODE (true_rtx) == IOR
7111                && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7112         term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7113       else if (GET_CODE (false_rtx) == IOR
7114                && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7115         term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7116       else if (GET_CODE (false_rtx) == IOR
7117                && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7118         term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7119
7120       term2 = simplify_gen_binary (AND, int_mode,
7121                                    XEXP (XEXP (src, 0), 0), true_rtx);
7122       term3 = simplify_gen_binary (AND, int_mode,
7123                                    simplify_gen_unary (NOT, int_mode,
7124                                                        XEXP (XEXP (src, 0), 0),
7125                                                        int_mode),
7126                                    false_rtx);
7127
7128       SUBST (SET_SRC (x),
7129              simplify_gen_binary (IOR, int_mode,
7130                                   simplify_gen_binary (IOR, int_mode,
7131                                                        term1, term2),
7132                                   term3));
7133
7134       src = SET_SRC (x);
7135     }
7136
7137   /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7138      whole thing fail.  */
7139   if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7140     return src;
7141   else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7142     return dest;
7143   else
7144     /* Convert this into a field assignment operation, if possible.  */
7145     return make_field_assignment (x);
7146 }
7147 \f
7148 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7149    result.  */
7150
7151 static rtx
7152 simplify_logical (rtx x)
7153 {
7154   rtx op0 = XEXP (x, 0);
7155   rtx op1 = XEXP (x, 1);
7156   scalar_int_mode mode;
7157
7158   switch (GET_CODE (x))
7159     {
7160     case AND:
7161       /* We can call simplify_and_const_int only if we don't lose
7162          any (sign) bits when converting INTVAL (op1) to
7163          "unsigned HOST_WIDE_INT".  */
7164       if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7165           && CONST_INT_P (op1)
7166           && (HWI_COMPUTABLE_MODE_P (mode)
7167               || INTVAL (op1) > 0))
7168         {
7169           x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7170           if (GET_CODE (x) != AND)
7171             return x;
7172
7173           op0 = XEXP (x, 0);
7174           op1 = XEXP (x, 1);
7175         }
7176
7177       /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7178          apply the distributive law and then the inverse distributive
7179          law to see if things simplify.  */
7180       if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7181         {
7182           rtx result = distribute_and_simplify_rtx (x, 0);
7183           if (result)
7184             return result;
7185         }
7186       if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7187         {
7188           rtx result = distribute_and_simplify_rtx (x, 1);
7189           if (result)
7190             return result;
7191         }
7192       break;
7193
7194     case IOR:
7195       /* If we have (ior (and A B) C), apply the distributive law and then
7196          the inverse distributive law to see if things simplify.  */
7197
7198       if (GET_CODE (op0) == AND)
7199         {
7200           rtx result = distribute_and_simplify_rtx (x, 0);
7201           if (result)
7202             return result;
7203         }
7204
7205       if (GET_CODE (op1) == AND)
7206         {
7207           rtx result = distribute_and_simplify_rtx (x, 1);
7208           if (result)
7209             return result;
7210         }
7211       break;
7212
7213     default:
7214       gcc_unreachable ();
7215     }
7216
7217   return x;
7218 }
7219 \f
7220 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7221    operations" because they can be replaced with two more basic operations.
7222    ZERO_EXTEND is also considered "compound" because it can be replaced with
7223    an AND operation, which is simpler, though only one operation.
7224
7225    The function expand_compound_operation is called with an rtx expression
7226    and will convert it to the appropriate shifts and AND operations,
7227    simplifying at each stage.
7228
7229    The function make_compound_operation is called to convert an expression
7230    consisting of shifts and ANDs into the equivalent compound expression.
7231    It is the inverse of this function, loosely speaking.  */
7232
7233 static rtx
7234 expand_compound_operation (rtx x)
7235 {
7236   unsigned HOST_WIDE_INT pos = 0, len;
7237   int unsignedp = 0;
7238   unsigned int modewidth;
7239   rtx tem;
7240   scalar_int_mode inner_mode;
7241
7242   switch (GET_CODE (x))
7243     {
7244     case ZERO_EXTEND:
7245       unsignedp = 1;
7246       /* FALLTHRU */
7247     case SIGN_EXTEND:
7248       /* We can't necessarily use a const_int for a multiword mode;
7249          it depends on implicitly extending the value.
7250          Since we don't know the right way to extend it,
7251          we can't tell whether the implicit way is right.
7252
7253          Even for a mode that is no wider than a const_int,
7254          we can't win, because we need to sign extend one of its bits through
7255          the rest of it, and we don't know which bit.  */
7256       if (CONST_INT_P (XEXP (x, 0)))
7257         return x;
7258
7259       /* Reject modes that aren't scalar integers because turning vector
7260          or complex modes into shifts causes problems.  */
7261       if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7262         return x;
7263
7264       /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7265          (zero_extend:MODE FROM) or (sign_extend:MODE FROM).  It is for any MEM
7266          because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7267          reloaded. If not for that, MEM's would very rarely be safe.
7268
7269          Reject modes bigger than a word, because we might not be able
7270          to reference a two-register group starting with an arbitrary register
7271          (and currently gen_lowpart might crash for a SUBREG).  */
7272
7273       if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7274         return x;
7275
7276       len = GET_MODE_PRECISION (inner_mode);
7277       /* If the inner object has VOIDmode (the only way this can happen
7278          is if it is an ASM_OPERANDS), we can't do anything since we don't
7279          know how much masking to do.  */
7280       if (len == 0)
7281         return x;
7282
7283       break;
7284
7285     case ZERO_EXTRACT:
7286       unsignedp = 1;
7287
7288       /* fall through */
7289
7290     case SIGN_EXTRACT:
7291       /* If the operand is a CLOBBER, just return it.  */
7292       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7293         return XEXP (x, 0);
7294
7295       if (!CONST_INT_P (XEXP (x, 1))
7296           || !CONST_INT_P (XEXP (x, 2)))
7297         return x;
7298
7299       /* Reject modes that aren't scalar integers because turning vector
7300          or complex modes into shifts causes problems.  */
7301       if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7302         return x;
7303
7304       len = INTVAL (XEXP (x, 1));
7305       pos = INTVAL (XEXP (x, 2));
7306
7307       /* This should stay within the object being extracted, fail otherwise.  */
7308       if (len + pos > GET_MODE_PRECISION (inner_mode))
7309         return x;
7310
7311       if (BITS_BIG_ENDIAN)
7312         pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7313
7314       break;
7315
7316     default:
7317       return x;
7318     }
7319
7320   /* We've rejected non-scalar operations by now.  */
7321   scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7322
7323   /* Convert sign extension to zero extension, if we know that the high
7324      bit is not set, as this is easier to optimize.  It will be converted
7325      back to cheaper alternative in make_extraction.  */
7326   if (GET_CODE (x) == SIGN_EXTEND
7327       && HWI_COMPUTABLE_MODE_P (mode)
7328       && ((nonzero_bits (XEXP (x, 0), inner_mode)
7329            & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7330           == 0))
7331     {
7332       rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7333       rtx temp2 = expand_compound_operation (temp);
7334
7335       /* Make sure this is a profitable operation.  */
7336       if (set_src_cost (x, mode, optimize_this_for_speed_p)
7337           > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7338        return temp2;
7339       else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7340                > set_src_cost (temp, mode, optimize_this_for_speed_p))
7341        return temp;
7342       else
7343        return x;
7344     }
7345
7346   /* We can optimize some special cases of ZERO_EXTEND.  */
7347   if (GET_CODE (x) == ZERO_EXTEND)
7348     {
7349       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7350          know that the last value didn't have any inappropriate bits
7351          set.  */
7352       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7353           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7354           && HWI_COMPUTABLE_MODE_P (mode)
7355           && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7356               & ~GET_MODE_MASK (inner_mode)) == 0)
7357         return XEXP (XEXP (x, 0), 0);
7358
7359       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
7360       if (GET_CODE (XEXP (x, 0)) == SUBREG
7361           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7362           && subreg_lowpart_p (XEXP (x, 0))
7363           && HWI_COMPUTABLE_MODE_P (mode)
7364           && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7365               & ~GET_MODE_MASK (inner_mode)) == 0)
7366         return SUBREG_REG (XEXP (x, 0));
7367
7368       /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7369          is a comparison and STORE_FLAG_VALUE permits.  This is like
7370          the first case, but it works even when MODE is larger
7371          than HOST_WIDE_INT.  */
7372       if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7373           && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7374           && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7375           && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7376           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7377         return XEXP (XEXP (x, 0), 0);
7378
7379       /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)).  */
7380       if (GET_CODE (XEXP (x, 0)) == SUBREG
7381           && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7382           && subreg_lowpart_p (XEXP (x, 0))
7383           && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7384           && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7385           && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7386         return SUBREG_REG (XEXP (x, 0));
7387
7388     }
7389
7390   /* If we reach here, we want to return a pair of shifts.  The inner
7391      shift is a left shift of BITSIZE - POS - LEN bits.  The outer
7392      shift is a right shift of BITSIZE - LEN bits.  It is arithmetic or
7393      logical depending on the value of UNSIGNEDP.
7394
7395      If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7396      converted into an AND of a shift.
7397
7398      We must check for the case where the left shift would have a negative
7399      count.  This can happen in a case like (x >> 31) & 255 on machines
7400      that can't shift by a constant.  On those machines, we would first
7401      combine the shift with the AND to produce a variable-position
7402      extraction.  Then the constant of 31 would be substituted in
7403      to produce such a position.  */
7404
7405   modewidth = GET_MODE_PRECISION (mode);
7406   if (modewidth >= pos + len)
7407     {
7408       tem = gen_lowpart (mode, XEXP (x, 0));
7409       if (!tem || GET_CODE (tem) == CLOBBER)
7410         return x;
7411       tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7412                                   tem, modewidth - pos - len);
7413       tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7414                                   mode, tem, modewidth - len);
7415     }
7416   else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7417     tem = simplify_and_const_int (NULL_RTX, mode,
7418                                   simplify_shift_const (NULL_RTX, LSHIFTRT,
7419                                                         mode, XEXP (x, 0),
7420                                                         pos),
7421                                   (HOST_WIDE_INT_1U << len) - 1);
7422   else
7423     /* Any other cases we can't handle.  */
7424     return x;
7425
7426   /* If we couldn't do this for some reason, return the original
7427      expression.  */
7428   if (GET_CODE (tem) == CLOBBER)
7429     return x;
7430
7431   return tem;
7432 }
7433 \f
7434 /* X is a SET which contains an assignment of one object into
7435    a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7436    or certain SUBREGS). If possible, convert it into a series of
7437    logical operations.
7438
7439    We half-heartedly support variable positions, but do not at all
7440    support variable lengths.  */
7441
7442 static const_rtx
7443 expand_field_assignment (const_rtx x)
7444 {
7445   rtx inner;
7446   rtx pos;                      /* Always counts from low bit.  */
7447   int len, inner_len;
7448   rtx mask, cleared, masked;
7449   scalar_int_mode compute_mode;
7450
7451   /* Loop until we find something we can't simplify.  */
7452   while (1)
7453     {
7454       if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7455           && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7456         {
7457           rtx x0 = XEXP (SET_DEST (x), 0);
7458           if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7459             break;
7460           inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7461           pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7462                               MAX_MODE_INT);
7463         }
7464       else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7465                && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7466         {
7467           inner = XEXP (SET_DEST (x), 0);
7468           if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7469             break;
7470
7471           len = INTVAL (XEXP (SET_DEST (x), 1));
7472           pos = XEXP (SET_DEST (x), 2);
7473
7474           /* A constant position should stay within the width of INNER.  */
7475           if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7476             break;
7477
7478           if (BITS_BIG_ENDIAN)
7479             {
7480               if (CONST_INT_P (pos))
7481                 pos = GEN_INT (inner_len - len - INTVAL (pos));
7482               else if (GET_CODE (pos) == MINUS
7483                        && CONST_INT_P (XEXP (pos, 1))
7484                        && INTVAL (XEXP (pos, 1)) == inner_len - len)
7485                 /* If position is ADJUST - X, new position is X.  */
7486                 pos = XEXP (pos, 0);
7487               else
7488                 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7489                                            gen_int_mode (inner_len - len,
7490                                                          GET_MODE (pos)),
7491                                            pos);
7492             }
7493         }
7494
7495       /* If the destination is a subreg that overwrites the whole of the inner
7496          register, we can move the subreg to the source.  */
7497       else if (GET_CODE (SET_DEST (x)) == SUBREG
7498                /* We need SUBREGs to compute nonzero_bits properly.  */
7499                && nonzero_sign_valid
7500                && !read_modify_subreg_p (SET_DEST (x)))
7501         {
7502           x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7503                            gen_lowpart
7504                            (GET_MODE (SUBREG_REG (SET_DEST (x))),
7505                             SET_SRC (x)));
7506           continue;
7507         }
7508       else
7509         break;
7510
7511       while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7512         inner = SUBREG_REG (inner);
7513
7514       /* Don't attempt bitwise arithmetic on non scalar integer modes.  */
7515       if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7516         {
7517           /* Don't do anything for vector or complex integral types.  */
7518           if (! FLOAT_MODE_P (GET_MODE (inner)))
7519             break;
7520
7521           /* Try to find an integral mode to pun with.  */
7522           if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7523               .exists (&compute_mode))
7524             break;
7525
7526           inner = gen_lowpart (compute_mode, inner);
7527         }
7528
7529       /* Compute a mask of LEN bits, if we can do this on the host machine.  */
7530       if (len >= HOST_BITS_PER_WIDE_INT)
7531         break;
7532
7533       /* Don't try to compute in too wide unsupported modes.  */
7534       if (!targetm.scalar_mode_supported_p (compute_mode))
7535         break;
7536
7537       /* Now compute the equivalent expression.  Make a copy of INNER
7538          for the SET_DEST in case it is a MEM into which we will substitute;
7539          we don't want shared RTL in that case.  */
7540       mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7541                            compute_mode);
7542       cleared = simplify_gen_binary (AND, compute_mode,
7543                                      simplify_gen_unary (NOT, compute_mode,
7544                                        simplify_gen_binary (ASHIFT,
7545                                                             compute_mode,
7546                                                             mask, pos),
7547                                        compute_mode),
7548                                      inner);
7549       masked = simplify_gen_binary (ASHIFT, compute_mode,
7550                                     simplify_gen_binary (
7551                                       AND, compute_mode,
7552                                       gen_lowpart (compute_mode, SET_SRC (x)),
7553                                       mask),
7554                                     pos);
7555
7556       x = gen_rtx_SET (copy_rtx (inner),
7557                        simplify_gen_binary (IOR, compute_mode,
7558                                             cleared, masked));
7559     }
7560
7561   return x;
7562 }
7563 \f
7564 /* Return an RTX for a reference to LEN bits of INNER.  If POS_RTX is nonzero,
7565    it is an RTX that represents the (variable) starting position; otherwise,
7566    POS is the (constant) starting bit position.  Both are counted from the LSB.
7567
7568    UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7569
7570    IN_DEST is nonzero if this is a reference in the destination of a SET.
7571    This is used when a ZERO_ or SIGN_EXTRACT isn't needed.  If nonzero,
7572    a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7573    be used.
7574
7575    IN_COMPARE is nonzero if we are in a COMPARE.  This means that a
7576    ZERO_EXTRACT should be built even for bits starting at bit 0.
7577
7578    MODE is the desired mode of the result (if IN_DEST == 0).
7579
7580    The result is an RTX for the extraction or NULL_RTX if the target
7581    can't handle it.  */
7582
7583 static rtx
7584 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7585                  rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7586                  int in_dest, int in_compare)
7587 {
7588   /* This mode describes the size of the storage area
7589      to fetch the overall value from.  Within that, we
7590      ignore the POS lowest bits, etc.  */
7591   machine_mode is_mode = GET_MODE (inner);
7592   machine_mode inner_mode;
7593   scalar_int_mode wanted_inner_mode;
7594   scalar_int_mode wanted_inner_reg_mode = word_mode;
7595   scalar_int_mode pos_mode = word_mode;
7596   machine_mode extraction_mode = word_mode;
7597   rtx new_rtx = 0;
7598   rtx orig_pos_rtx = pos_rtx;
7599   HOST_WIDE_INT orig_pos;
7600
7601   if (pos_rtx && CONST_INT_P (pos_rtx))
7602     pos = INTVAL (pos_rtx), pos_rtx = 0;
7603
7604   if (GET_CODE (inner) == SUBREG
7605       && subreg_lowpart_p (inner)
7606       && (paradoxical_subreg_p (inner)
7607           /* If trying or potentionally trying to extract
7608              bits outside of is_mode, don't look through
7609              non-paradoxical SUBREGs.  See PR82192.  */
7610           || (pos_rtx == NULL_RTX
7611               && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7612     {
7613       /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7614          consider just the QI as the memory to extract from.
7615          The subreg adds or removes high bits; its mode is
7616          irrelevant to the meaning of this extraction,
7617          since POS and LEN count from the lsb.  */
7618       if (MEM_P (SUBREG_REG (inner)))
7619         is_mode = GET_MODE (SUBREG_REG (inner));
7620       inner = SUBREG_REG (inner);
7621     }
7622   else if (GET_CODE (inner) == ASHIFT
7623            && CONST_INT_P (XEXP (inner, 1))
7624            && pos_rtx == 0 && pos == 0
7625            && len > UINTVAL (XEXP (inner, 1)))
7626     {
7627       /* We're extracting the least significant bits of an rtx
7628          (ashift X (const_int C)), where LEN > C.  Extract the
7629          least significant (LEN - C) bits of X, giving an rtx
7630          whose mode is MODE, then shift it left C times.  */
7631       new_rtx = make_extraction (mode, XEXP (inner, 0),
7632                              0, 0, len - INTVAL (XEXP (inner, 1)),
7633                              unsignedp, in_dest, in_compare);
7634       if (new_rtx != 0)
7635         return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7636     }
7637   else if (GET_CODE (inner) == TRUNCATE
7638            /* If trying or potentionally trying to extract
7639               bits outside of is_mode, don't look through
7640               TRUNCATE.  See PR82192.  */
7641            && pos_rtx == NULL_RTX
7642            && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7643     inner = XEXP (inner, 0);
7644
7645   inner_mode = GET_MODE (inner);
7646
7647   /* See if this can be done without an extraction.  We never can if the
7648      width of the field is not the same as that of some integer mode. For
7649      registers, we can only avoid the extraction if the position is at the
7650      low-order bit and this is either not in the destination or we have the
7651      appropriate STRICT_LOW_PART operation available.
7652
7653      For MEM, we can avoid an extract if the field starts on an appropriate
7654      boundary and we can change the mode of the memory reference.  */
7655
7656   scalar_int_mode tmode;
7657   if (int_mode_for_size (len, 1).exists (&tmode)
7658       && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7659            && !MEM_P (inner)
7660            && (pos == 0 || REG_P (inner))
7661            && (inner_mode == tmode
7662                || !REG_P (inner)
7663                || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7664                || reg_truncated_to_mode (tmode, inner))
7665            && (! in_dest
7666                || (REG_P (inner)
7667                    && have_insn_for (STRICT_LOW_PART, tmode))))
7668           || (MEM_P (inner) && pos_rtx == 0
7669               && (pos
7670                   % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7671                      : BITS_PER_UNIT)) == 0
7672               /* We can't do this if we are widening INNER_MODE (it
7673                  may not be aligned, for one thing).  */
7674               && !paradoxical_subreg_p (tmode, inner_mode)
7675               && (inner_mode == tmode
7676                   || (! mode_dependent_address_p (XEXP (inner, 0),
7677                                                   MEM_ADDR_SPACE (inner))
7678                       && ! MEM_VOLATILE_P (inner))))))
7679     {
7680       /* If INNER is a MEM, make a new MEM that encompasses just the desired
7681          field.  If the original and current mode are the same, we need not
7682          adjust the offset.  Otherwise, we do if bytes big endian.
7683
7684          If INNER is not a MEM, get a piece consisting of just the field
7685          of interest (in this case POS % BITS_PER_WORD must be 0).  */
7686
7687       if (MEM_P (inner))
7688         {
7689           poly_int64 offset;
7690
7691           /* POS counts from lsb, but make OFFSET count in memory order.  */
7692           if (BYTES_BIG_ENDIAN)
7693             offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7694                                                - len - pos);
7695           else
7696             offset = pos / BITS_PER_UNIT;
7697
7698           new_rtx = adjust_address_nv (inner, tmode, offset);
7699         }
7700       else if (REG_P (inner))
7701         {
7702           if (tmode != inner_mode)
7703             {
7704               /* We can't call gen_lowpart in a DEST since we
7705                  always want a SUBREG (see below) and it would sometimes
7706                  return a new hard register.  */
7707               if (pos || in_dest)
7708                 {
7709                   poly_uint64 offset
7710                     = subreg_offset_from_lsb (tmode, inner_mode, pos);
7711
7712                   /* Avoid creating invalid subregs, for example when
7713                      simplifying (x>>32)&255.  */
7714                   if (!validate_subreg (tmode, inner_mode, inner, offset))
7715                     return NULL_RTX;
7716
7717                   new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7718                 }
7719               else
7720                 new_rtx = gen_lowpart (tmode, inner);
7721             }
7722           else
7723             new_rtx = inner;
7724         }
7725       else
7726         new_rtx = force_to_mode (inner, tmode,
7727                                  len >= HOST_BITS_PER_WIDE_INT
7728                                  ? HOST_WIDE_INT_M1U
7729                                  : (HOST_WIDE_INT_1U << len) - 1, 0);
7730
7731       /* If this extraction is going into the destination of a SET,
7732          make a STRICT_LOW_PART unless we made a MEM.  */
7733
7734       if (in_dest)
7735         return (MEM_P (new_rtx) ? new_rtx
7736                 : (GET_CODE (new_rtx) != SUBREG
7737                    ? gen_rtx_CLOBBER (tmode, const0_rtx)
7738                    : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7739
7740       if (mode == tmode)
7741         return new_rtx;
7742
7743       if (CONST_SCALAR_INT_P (new_rtx))
7744         return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7745                                          mode, new_rtx, tmode);
7746
7747       /* If we know that no extraneous bits are set, and that the high
7748          bit is not set, convert the extraction to the cheaper of
7749          sign and zero extension, that are equivalent in these cases.  */
7750       if (flag_expensive_optimizations
7751           && (HWI_COMPUTABLE_MODE_P (tmode)
7752               && ((nonzero_bits (new_rtx, tmode)
7753                    & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7754                   == 0)))
7755         {
7756           rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7757           rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7758
7759           /* Prefer ZERO_EXTENSION, since it gives more information to
7760              backends.  */
7761           if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7762               <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7763             return temp;
7764           return temp1;
7765         }
7766
7767       /* Otherwise, sign- or zero-extend unless we already are in the
7768          proper mode.  */
7769
7770       return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7771                              mode, new_rtx));
7772     }
7773
7774   /* Unless this is a COMPARE or we have a funny memory reference,
7775      don't do anything with zero-extending field extracts starting at
7776      the low-order bit since they are simple AND operations.  */
7777   if (pos_rtx == 0 && pos == 0 && ! in_dest
7778       && ! in_compare && unsignedp)
7779     return 0;
7780
7781   /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7782      if the position is not a constant and the length is not 1.  In all
7783      other cases, we would only be going outside our object in cases when
7784      an original shift would have been undefined.  */
7785   if (MEM_P (inner)
7786       && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7787           || (pos_rtx != 0 && len != 1)))
7788     return 0;
7789
7790   enum extraction_pattern pattern = (in_dest ? EP_insv
7791                                      : unsignedp ? EP_extzv : EP_extv);
7792
7793   /* If INNER is not from memory, we want it to have the mode of a register
7794      extraction pattern's structure operand, or word_mode if there is no
7795      such pattern.  The same applies to extraction_mode and pos_mode
7796      and their respective operands.
7797
7798      For memory, assume that the desired extraction_mode and pos_mode
7799      are the same as for a register operation, since at present we don't
7800      have named patterns for aligned memory structures.  */
7801   struct extraction_insn insn;
7802   unsigned int inner_size;
7803   if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7804       && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7805     {
7806       wanted_inner_reg_mode = insn.struct_mode.require ();
7807       pos_mode = insn.pos_mode;
7808       extraction_mode = insn.field_mode;
7809     }
7810
7811   /* Never narrow an object, since that might not be safe.  */
7812
7813   if (mode != VOIDmode
7814       && partial_subreg_p (extraction_mode, mode))
7815     extraction_mode = mode;
7816
7817   if (!MEM_P (inner))
7818     wanted_inner_mode = wanted_inner_reg_mode;
7819   else
7820     {
7821       /* Be careful not to go beyond the extracted object and maintain the
7822          natural alignment of the memory.  */
7823       wanted_inner_mode = smallest_int_mode_for_size (len);
7824       while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7825              > GET_MODE_BITSIZE (wanted_inner_mode))
7826         wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7827     }
7828
7829   orig_pos = pos;
7830
7831   if (BITS_BIG_ENDIAN)
7832     {
7833       /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7834          BITS_BIG_ENDIAN style.  If position is constant, compute new
7835          position.  Otherwise, build subtraction.
7836          Note that POS is relative to the mode of the original argument.
7837          If it's a MEM we need to recompute POS relative to that.
7838          However, if we're extracting from (or inserting into) a register,
7839          we want to recompute POS relative to wanted_inner_mode.  */
7840       int width;
7841       if (!MEM_P (inner))
7842         width = GET_MODE_BITSIZE (wanted_inner_mode);
7843       else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7844         return NULL_RTX;
7845
7846       if (pos_rtx == 0)
7847         pos = width - len - pos;
7848       else
7849         pos_rtx
7850           = gen_rtx_MINUS (GET_MODE (pos_rtx),
7851                            gen_int_mode (width - len, GET_MODE (pos_rtx)),
7852                            pos_rtx);
7853       /* POS may be less than 0 now, but we check for that below.
7854          Note that it can only be less than 0 if !MEM_P (inner).  */
7855     }
7856
7857   /* If INNER has a wider mode, and this is a constant extraction, try to
7858      make it smaller and adjust the byte to point to the byte containing
7859      the value.  */
7860   if (wanted_inner_mode != VOIDmode
7861       && inner_mode != wanted_inner_mode
7862       && ! pos_rtx
7863       && partial_subreg_p (wanted_inner_mode, is_mode)
7864       && MEM_P (inner)
7865       && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7866       && ! MEM_VOLATILE_P (inner))
7867     {
7868       poly_int64 offset = 0;
7869
7870       /* The computations below will be correct if the machine is big
7871          endian in both bits and bytes or little endian in bits and bytes.
7872          If it is mixed, we must adjust.  */
7873
7874       /* If bytes are big endian and we had a paradoxical SUBREG, we must
7875          adjust OFFSET to compensate.  */
7876       if (BYTES_BIG_ENDIAN
7877           && paradoxical_subreg_p (is_mode, inner_mode))
7878         offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7879
7880       /* We can now move to the desired byte.  */
7881       offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7882                 * GET_MODE_SIZE (wanted_inner_mode);
7883       pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7884
7885       if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7886           && is_mode != wanted_inner_mode)
7887         offset = (GET_MODE_SIZE (is_mode)
7888                   - GET_MODE_SIZE (wanted_inner_mode) - offset);
7889
7890       inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7891     }
7892
7893   /* If INNER is not memory, get it into the proper mode.  If we are changing
7894      its mode, POS must be a constant and smaller than the size of the new
7895      mode.  */
7896   else if (!MEM_P (inner))
7897     {
7898       /* On the LHS, don't create paradoxical subregs implicitely truncating
7899          the register unless TARGET_TRULY_NOOP_TRUNCATION.  */
7900       if (in_dest
7901           && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7902                                              wanted_inner_mode))
7903         return NULL_RTX;
7904
7905       if (GET_MODE (inner) != wanted_inner_mode
7906           && (pos_rtx != 0
7907               || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7908         return NULL_RTX;
7909
7910       if (orig_pos < 0)
7911         return NULL_RTX;
7912
7913       inner = force_to_mode (inner, wanted_inner_mode,
7914                              pos_rtx
7915                              || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7916                              ? HOST_WIDE_INT_M1U
7917                              : (((HOST_WIDE_INT_1U << len) - 1)
7918                                 << orig_pos),
7919                              0);
7920     }
7921
7922   /* Adjust mode of POS_RTX, if needed.  If we want a wider mode, we
7923      have to zero extend.  Otherwise, we can just use a SUBREG.
7924
7925      We dealt with constant rtxes earlier, so pos_rtx cannot
7926      have VOIDmode at this point.  */
7927   if (pos_rtx != 0
7928       && (GET_MODE_SIZE (pos_mode)
7929           > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7930     {
7931       rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7932                                      GET_MODE (pos_rtx));
7933
7934       /* If we know that no extraneous bits are set, and that the high
7935          bit is not set, convert extraction to cheaper one - either
7936          SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7937          cases.  */
7938       if (flag_expensive_optimizations
7939           && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7940               && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7941                    & ~(((unsigned HOST_WIDE_INT)
7942                         GET_MODE_MASK (GET_MODE (pos_rtx)))
7943                        >> 1))
7944                   == 0)))
7945         {
7946           rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7947                                           GET_MODE (pos_rtx));
7948
7949           /* Prefer ZERO_EXTENSION, since it gives more information to
7950              backends.  */
7951           if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7952               < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7953             temp = temp1;
7954         }
7955       pos_rtx = temp;
7956     }
7957
7958   /* Make POS_RTX unless we already have it and it is correct.  If we don't
7959      have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7960      be a CONST_INT.  */
7961   if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7962     pos_rtx = orig_pos_rtx;
7963
7964   else if (pos_rtx == 0)
7965     pos_rtx = GEN_INT (pos);
7966
7967   /* Make the required operation.  See if we can use existing rtx.  */
7968   new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7969                          extraction_mode, inner, GEN_INT (len), pos_rtx);
7970   if (! in_dest)
7971     new_rtx = gen_lowpart (mode, new_rtx);
7972
7973   return new_rtx;
7974 }
7975 \f
7976 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7977    can be commuted with any other operations in X.  Return X without
7978    that shift if so.  */
7979
7980 static rtx
7981 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7982 {
7983   enum rtx_code code = GET_CODE (x);
7984   rtx tem;
7985
7986   switch (code)
7987     {
7988     case ASHIFT:
7989       /* This is the shift itself.  If it is wide enough, we will return
7990          either the value being shifted if the shift count is equal to
7991          COUNT or a shift for the difference.  */
7992       if (CONST_INT_P (XEXP (x, 1))
7993           && INTVAL (XEXP (x, 1)) >= count)
7994         return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7995                                      INTVAL (XEXP (x, 1)) - count);
7996       break;
7997
7998     case NEG:  case NOT:
7999       if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8000         return simplify_gen_unary (code, mode, tem, mode);
8001
8002       break;
8003
8004     case PLUS:  case IOR:  case XOR:  case AND:
8005       /* If we can safely shift this constant and we find the inner shift,
8006          make a new operation.  */
8007       if (CONST_INT_P (XEXP (x, 1))
8008           && (UINTVAL (XEXP (x, 1))
8009               & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
8010           && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
8011         {
8012           HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
8013           return simplify_gen_binary (code, mode, tem,
8014                                       gen_int_mode (val, mode));
8015         }
8016       break;
8017
8018     default:
8019       break;
8020     }
8021
8022   return 0;
8023 }
8024 \f
8025 /* Subroutine of make_compound_operation.  *X_PTR is the rtx at the current
8026    level of the expression and MODE is its mode.  IN_CODE is as for
8027    make_compound_operation.  *NEXT_CODE_PTR is the value of IN_CODE
8028    that should be used when recursing on operands of *X_PTR.
8029
8030    There are two possible actions:
8031
8032    - Return null.  This tells the caller to recurse on *X_PTR with IN_CODE
8033      equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
8034
8035    - Return a new rtx, which the caller returns directly.  */
8036
8037 static rtx
8038 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
8039                              enum rtx_code in_code,
8040                              enum rtx_code *next_code_ptr)
8041 {
8042   rtx x = *x_ptr;
8043   enum rtx_code next_code = *next_code_ptr;
8044   enum rtx_code code = GET_CODE (x);
8045   int mode_width = GET_MODE_PRECISION (mode);
8046   rtx rhs, lhs;
8047   rtx new_rtx = 0;
8048   int i;
8049   rtx tem;
8050   scalar_int_mode inner_mode;
8051   bool equality_comparison = false;
8052
8053   if (in_code == EQ)
8054     {
8055       equality_comparison = true;
8056       in_code = COMPARE;
8057     }
8058
8059   /* Process depending on the code of this operation.  If NEW is set
8060      nonzero, it will be returned.  */
8061
8062   switch (code)
8063     {
8064     case ASHIFT:
8065       /* Convert shifts by constants into multiplications if inside
8066          an address.  */
8067       if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
8068           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8069           && INTVAL (XEXP (x, 1)) >= 0)
8070         {
8071           HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
8072           HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
8073
8074           new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8075           if (GET_CODE (new_rtx) == NEG)
8076             {
8077               new_rtx = XEXP (new_rtx, 0);
8078               multval = -multval;
8079             }
8080           multval = trunc_int_for_mode (multval, mode);
8081           new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8082         }
8083       break;
8084
8085     case PLUS:
8086       lhs = XEXP (x, 0);
8087       rhs = XEXP (x, 1);
8088       lhs = make_compound_operation (lhs, next_code);
8089       rhs = make_compound_operation (rhs, next_code);
8090       if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
8091         {
8092           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
8093                                      XEXP (lhs, 1));
8094           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8095         }
8096       else if (GET_CODE (lhs) == MULT
8097                && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
8098         {
8099           tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
8100                                      simplify_gen_unary (NEG, mode,
8101                                                          XEXP (lhs, 1),
8102                                                          mode));
8103           new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8104         }
8105       else
8106         {
8107           SUBST (XEXP (x, 0), lhs);
8108           SUBST (XEXP (x, 1), rhs);
8109         }
8110       maybe_swap_commutative_operands (x);
8111       return x;
8112
8113     case MINUS:
8114       lhs = XEXP (x, 0);
8115       rhs = XEXP (x, 1);
8116       lhs = make_compound_operation (lhs, next_code);
8117       rhs = make_compound_operation (rhs, next_code);
8118       if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
8119         {
8120           tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
8121                                      XEXP (rhs, 1));
8122           return simplify_gen_binary (PLUS, mode, tem, lhs);
8123         }
8124       else if (GET_CODE (rhs) == MULT
8125                && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
8126         {
8127           tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
8128                                      simplify_gen_unary (NEG, mode,
8129                                                          XEXP (rhs, 1),
8130                                                          mode));
8131           return simplify_gen_binary (PLUS, mode, tem, lhs);
8132         }
8133       else
8134         {
8135           SUBST (XEXP (x, 0), lhs);
8136           SUBST (XEXP (x, 1), rhs);
8137           return x;
8138         }
8139
8140     case AND:
8141       /* If the second operand is not a constant, we can't do anything
8142          with it.  */
8143       if (!CONST_INT_P (XEXP (x, 1)))
8144         break;
8145
8146       /* If the constant is a power of two minus one and the first operand
8147          is a logical right shift, make an extraction.  */
8148       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8149           && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8150         {
8151           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8152           new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8153                                      i, 1, 0, in_code == COMPARE);
8154         }
8155
8156       /* Same as previous, but for (subreg (lshiftrt ...)) in first op.  */
8157       else if (GET_CODE (XEXP (x, 0)) == SUBREG
8158                && subreg_lowpart_p (XEXP (x, 0))
8159                && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8160                                           &inner_mode)
8161                && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8162                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8163         {
8164           rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8165           new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8166           new_rtx = make_extraction (inner_mode, new_rtx, 0,
8167                                      XEXP (inner_x0, 1),
8168                                      i, 1, 0, in_code == COMPARE);
8169
8170           /* If we narrowed the mode when dropping the subreg, then we lose.  */
8171           if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8172             new_rtx = NULL;
8173
8174           /* If that didn't give anything, see if the AND simplifies on
8175              its own.  */
8176           if (!new_rtx && i >= 0)
8177             {
8178               new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8179               new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8180                                          0, in_code == COMPARE);
8181             }
8182         }
8183       /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)).  */
8184       else if ((GET_CODE (XEXP (x, 0)) == XOR
8185                 || GET_CODE (XEXP (x, 0)) == IOR)
8186                && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8187                && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8188                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8189         {
8190           /* Apply the distributive law, and then try to make extractions.  */
8191           new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8192                                     gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8193                                                  XEXP (x, 1)),
8194                                     gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8195                                                  XEXP (x, 1)));
8196           new_rtx = make_compound_operation (new_rtx, in_code);
8197         }
8198
8199       /* If we are have (and (rotate X C) M) and C is larger than the number
8200          of bits in M, this is an extraction.  */
8201
8202       else if (GET_CODE (XEXP (x, 0)) == ROTATE
8203                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8204                && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8205                && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8206         {
8207           new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8208           new_rtx = make_extraction (mode, new_rtx,
8209                                      (GET_MODE_PRECISION (mode)
8210                                       - INTVAL (XEXP (XEXP (x, 0), 1))),
8211                                      NULL_RTX, i, 1, 0, in_code == COMPARE);
8212         }
8213
8214       /* On machines without logical shifts, if the operand of the AND is
8215          a logical shift and our mask turns off all the propagated sign
8216          bits, we can replace the logical shift with an arithmetic shift.  */
8217       else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8218                && !have_insn_for (LSHIFTRT, mode)
8219                && have_insn_for (ASHIFTRT, mode)
8220                && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8221                && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8222                && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8223                && mode_width <= HOST_BITS_PER_WIDE_INT)
8224         {
8225           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8226
8227           mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8228           if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8229             SUBST (XEXP (x, 0),
8230                    gen_rtx_ASHIFTRT (mode,
8231                                      make_compound_operation (XEXP (XEXP (x,
8232                                                                           0),
8233                                                                     0),
8234                                                               next_code),
8235                                      XEXP (XEXP (x, 0), 1)));
8236         }
8237
8238       /* If the constant is one less than a power of two, this might be
8239          representable by an extraction even if no shift is present.
8240          If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8241          we are in a COMPARE.  */
8242       else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8243         new_rtx = make_extraction (mode,
8244                                    make_compound_operation (XEXP (x, 0),
8245                                                             next_code),
8246                                    0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8247
8248       /* If we are in a comparison and this is an AND with a power of two,
8249          convert this into the appropriate bit extract.  */
8250       else if (in_code == COMPARE
8251                && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8252                && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8253         new_rtx = make_extraction (mode,
8254                                    make_compound_operation (XEXP (x, 0),
8255                                                             next_code),
8256                                    i, NULL_RTX, 1, 1, 0, 1);
8257
8258       /* If the one operand is a paradoxical subreg of a register or memory and
8259          the constant (limited to the smaller mode) has only zero bits where
8260          the sub expression has known zero bits, this can be expressed as
8261          a zero_extend.  */
8262       else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8263         {
8264           rtx sub;
8265
8266           sub = XEXP (XEXP (x, 0), 0);
8267           machine_mode sub_mode = GET_MODE (sub);
8268           int sub_width;
8269           if ((REG_P (sub) || MEM_P (sub))
8270               && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8271               && sub_width < mode_width)
8272             {
8273               unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8274               unsigned HOST_WIDE_INT mask;
8275
8276               /* original AND constant with all the known zero bits set */
8277               mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8278               if ((mask & mode_mask) == mode_mask)
8279                 {
8280                   new_rtx = make_compound_operation (sub, next_code);
8281                   new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8282                                              1, 0, in_code == COMPARE);
8283                 }
8284             }
8285         }
8286
8287       break;
8288
8289     case LSHIFTRT:
8290       /* If the sign bit is known to be zero, replace this with an
8291          arithmetic shift.  */
8292       if (have_insn_for (ASHIFTRT, mode)
8293           && ! have_insn_for (LSHIFTRT, mode)
8294           && mode_width <= HOST_BITS_PER_WIDE_INT
8295           && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8296         {
8297           new_rtx = gen_rtx_ASHIFTRT (mode,
8298                                       make_compound_operation (XEXP (x, 0),
8299                                                                next_code),
8300                                       XEXP (x, 1));
8301           break;
8302         }
8303
8304       /* fall through */
8305
8306     case ASHIFTRT:
8307       lhs = XEXP (x, 0);
8308       rhs = XEXP (x, 1);
8309
8310       /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8311          this is a SIGN_EXTRACT.  */
8312       if (CONST_INT_P (rhs)
8313           && GET_CODE (lhs) == ASHIFT
8314           && CONST_INT_P (XEXP (lhs, 1))
8315           && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8316           && INTVAL (XEXP (lhs, 1)) >= 0
8317           && INTVAL (rhs) < mode_width)
8318         {
8319           new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8320           new_rtx = make_extraction (mode, new_rtx,
8321                                      INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8322                                      NULL_RTX, mode_width - INTVAL (rhs),
8323                                      code == LSHIFTRT, 0, in_code == COMPARE);
8324           break;
8325         }
8326
8327       /* See if we have operations between an ASHIFTRT and an ASHIFT.
8328          If so, try to merge the shifts into a SIGN_EXTEND.  We could
8329          also do this for some cases of SIGN_EXTRACT, but it doesn't
8330          seem worth the effort; the case checked for occurs on Alpha.  */
8331
8332       if (!OBJECT_P (lhs)
8333           && ! (GET_CODE (lhs) == SUBREG
8334                 && (OBJECT_P (SUBREG_REG (lhs))))
8335           && CONST_INT_P (rhs)
8336           && INTVAL (rhs) >= 0
8337           && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8338           && INTVAL (rhs) < mode_width
8339           && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8340         new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8341                                                                   next_code),
8342                                    0, NULL_RTX, mode_width - INTVAL (rhs),
8343                                    code == LSHIFTRT, 0, in_code == COMPARE);
8344
8345       break;
8346
8347     case SUBREG:
8348       /* Call ourselves recursively on the inner expression.  If we are
8349          narrowing the object and it has a different RTL code from
8350          what it originally did, do this SUBREG as a force_to_mode.  */
8351       {
8352         rtx inner = SUBREG_REG (x), simplified;
8353         enum rtx_code subreg_code = in_code;
8354
8355         /* If the SUBREG is masking of a logical right shift,
8356            make an extraction.  */
8357         if (GET_CODE (inner) == LSHIFTRT
8358             && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8359             && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8360             && CONST_INT_P (XEXP (inner, 1))
8361             && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8362             && subreg_lowpart_p (x))
8363           {
8364             new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8365             int width = GET_MODE_PRECISION (inner_mode)
8366                         - INTVAL (XEXP (inner, 1));
8367             if (width > mode_width)
8368               width = mode_width;
8369             new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8370                                        width, 1, 0, in_code == COMPARE);
8371             break;
8372           }
8373
8374         /* If in_code is COMPARE, it isn't always safe to pass it through
8375            to the recursive make_compound_operation call.  */
8376         if (subreg_code == COMPARE
8377             && (!subreg_lowpart_p (x)
8378                 || GET_CODE (inner) == SUBREG
8379                 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8380                    is (const_int 0), rather than
8381                    (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8382                    Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8383                    for non-equality comparisons against 0 is not equivalent
8384                    to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0).  */
8385                 || (GET_CODE (inner) == AND
8386                     && CONST_INT_P (XEXP (inner, 1))
8387                     && partial_subreg_p (x)
8388                     && exact_log2 (UINTVAL (XEXP (inner, 1)))
8389                        >= GET_MODE_BITSIZE (mode) - 1)))
8390           subreg_code = SET;
8391
8392         tem = make_compound_operation (inner, subreg_code);
8393
8394         simplified
8395           = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8396         if (simplified)
8397           tem = simplified;
8398
8399         if (GET_CODE (tem) != GET_CODE (inner)
8400             && partial_subreg_p (x)
8401             && subreg_lowpart_p (x))
8402           {
8403             rtx newer
8404               = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8405
8406             /* If we have something other than a SUBREG, we might have
8407                done an expansion, so rerun ourselves.  */
8408             if (GET_CODE (newer) != SUBREG)
8409               newer = make_compound_operation (newer, in_code);
8410
8411             /* force_to_mode can expand compounds.  If it just re-expanded
8412                the compound, use gen_lowpart to convert to the desired
8413                mode.  */
8414             if (rtx_equal_p (newer, x)
8415                 /* Likewise if it re-expanded the compound only partially.
8416                    This happens for SUBREG of ZERO_EXTRACT if they extract
8417                    the same number of bits.  */
8418                 || (GET_CODE (newer) == SUBREG
8419                     && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8420                         || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8421                     && GET_CODE (inner) == AND
8422                     && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8423               return gen_lowpart (GET_MODE (x), tem);
8424
8425             return newer;
8426           }
8427
8428         if (simplified)
8429           return tem;
8430       }
8431       break;
8432
8433     default:
8434       break;
8435     }
8436
8437   if (new_rtx)
8438     *x_ptr = gen_lowpart (mode, new_rtx);
8439   *next_code_ptr = next_code;
8440   return NULL_RTX;
8441 }
8442
8443 /* Look at the expression rooted at X.  Look for expressions
8444    equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8445    Form these expressions.
8446
8447    Return the new rtx, usually just X.
8448
8449    Also, for machines like the VAX that don't have logical shift insns,
8450    try to convert logical to arithmetic shift operations in cases where
8451    they are equivalent.  This undoes the canonicalizations to logical
8452    shifts done elsewhere.
8453
8454    We try, as much as possible, to re-use rtl expressions to save memory.
8455
8456    IN_CODE says what kind of expression we are processing.  Normally, it is
8457    SET.  In a memory address it is MEM.  When processing the arguments of
8458    a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8459    precisely it is an equality comparison against zero.  */
8460
8461 rtx
8462 make_compound_operation (rtx x, enum rtx_code in_code)
8463 {
8464   enum rtx_code code = GET_CODE (x);
8465   const char *fmt;
8466   int i, j;
8467   enum rtx_code next_code;
8468   rtx new_rtx, tem;
8469
8470   /* Select the code to be used in recursive calls.  Once we are inside an
8471      address, we stay there.  If we have a comparison, set to COMPARE,
8472      but once inside, go back to our default of SET.  */
8473
8474   next_code = (code == MEM ? MEM
8475                : ((code == COMPARE || COMPARISON_P (x))
8476                   && XEXP (x, 1) == const0_rtx) ? COMPARE
8477                : in_code == COMPARE || in_code == EQ ? SET : in_code);
8478
8479   scalar_int_mode mode;
8480   if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8481     {
8482       rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8483                                                  &next_code);
8484       if (new_rtx)
8485         return new_rtx;
8486       code = GET_CODE (x);
8487     }
8488
8489   /* Now recursively process each operand of this operation.  We need to
8490      handle ZERO_EXTEND specially so that we don't lose track of the
8491      inner mode.  */
8492   if (code == ZERO_EXTEND)
8493     {
8494       new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8495       tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8496                                             new_rtx, GET_MODE (XEXP (x, 0)));
8497       if (tem)
8498         return tem;
8499       SUBST (XEXP (x, 0), new_rtx);
8500       return x;
8501     }
8502
8503   fmt = GET_RTX_FORMAT (code);
8504   for (i = 0; i < GET_RTX_LENGTH (code); i++)
8505     if (fmt[i] == 'e')
8506       {
8507         new_rtx = make_compound_operation (XEXP (x, i), next_code);
8508         SUBST (XEXP (x, i), new_rtx);
8509       }
8510     else if (fmt[i] == 'E')
8511       for (j = 0; j < XVECLEN (x, i); j++)
8512         {
8513           new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8514           SUBST (XVECEXP (x, i, j), new_rtx);
8515         }
8516
8517   maybe_swap_commutative_operands (x);
8518   return x;
8519 }
8520 \f
8521 /* Given M see if it is a value that would select a field of bits
8522    within an item, but not the entire word.  Return -1 if not.
8523    Otherwise, return the starting position of the field, where 0 is the
8524    low-order bit.
8525
8526    *PLEN is set to the length of the field.  */
8527
8528 static int
8529 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8530 {
8531   /* Get the bit number of the first 1 bit from the right, -1 if none.  */
8532   int pos = m ? ctz_hwi (m) : -1;
8533   int len = 0;
8534
8535   if (pos >= 0)
8536     /* Now shift off the low-order zero bits and see if we have a
8537        power of two minus 1.  */
8538     len = exact_log2 ((m >> pos) + 1);
8539
8540   if (len <= 0)
8541     pos = -1;
8542
8543   *plen = len;
8544   return pos;
8545 }
8546 \f
8547 /* If X refers to a register that equals REG in value, replace these
8548    references with REG.  */
8549 static rtx
8550 canon_reg_for_combine (rtx x, rtx reg)
8551 {
8552   rtx op0, op1, op2;
8553   const char *fmt;
8554   int i;
8555   bool copied;
8556
8557   enum rtx_code code = GET_CODE (x);
8558   switch (GET_RTX_CLASS (code))
8559     {
8560     case RTX_UNARY:
8561       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8562       if (op0 != XEXP (x, 0))
8563         return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8564                                    GET_MODE (reg));
8565       break;
8566
8567     case RTX_BIN_ARITH:
8568     case RTX_COMM_ARITH:
8569       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8570       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8571       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8572         return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8573       break;
8574
8575     case RTX_COMPARE:
8576     case RTX_COMM_COMPARE:
8577       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8578       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8579       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8580         return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8581                                         GET_MODE (op0), op0, op1);
8582       break;
8583
8584     case RTX_TERNARY:
8585     case RTX_BITFIELD_OPS:
8586       op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8587       op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8588       op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8589       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8590         return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8591                                      GET_MODE (op0), op0, op1, op2);
8592       /* FALLTHRU */
8593
8594     case RTX_OBJ:
8595       if (REG_P (x))
8596         {
8597           if (rtx_equal_p (get_last_value (reg), x)
8598               || rtx_equal_p (reg, get_last_value (x)))
8599             return reg;
8600           else
8601             break;
8602         }
8603
8604       /* fall through */
8605
8606     default:
8607       fmt = GET_RTX_FORMAT (code);
8608       copied = false;
8609       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8610         if (fmt[i] == 'e')
8611           {
8612             rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8613             if (op != XEXP (x, i))
8614               {
8615                 if (!copied)
8616                   {
8617                     copied = true;
8618                     x = copy_rtx (x);
8619                   }
8620                 XEXP (x, i) = op;
8621               }
8622           }
8623         else if (fmt[i] == 'E')
8624           {
8625             int j;
8626             for (j = 0; j < XVECLEN (x, i); j++)
8627               {
8628                 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8629                 if (op != XVECEXP (x, i, j))
8630                   {
8631                     if (!copied)
8632                       {
8633                         copied = true;
8634                         x = copy_rtx (x);
8635                       }
8636                     XVECEXP (x, i, j) = op;
8637                   }
8638               }
8639           }
8640
8641       break;
8642     }
8643
8644   return x;
8645 }
8646
8647 /* Return X converted to MODE.  If the value is already truncated to
8648    MODE we can just return a subreg even though in the general case we
8649    would need an explicit truncation.  */
8650
8651 static rtx
8652 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8653 {
8654   if (!CONST_INT_P (x)
8655       && partial_subreg_p (mode, GET_MODE (x))
8656       && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8657       && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8658     {
8659       /* Bit-cast X into an integer mode.  */
8660       if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8661         x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8662       x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8663                               x, GET_MODE (x));
8664     }
8665
8666   return gen_lowpart (mode, x);
8667 }
8668
8669 /* See if X can be simplified knowing that we will only refer to it in
8670    MODE and will only refer to those bits that are nonzero in MASK.
8671    If other bits are being computed or if masking operations are done
8672    that select a superset of the bits in MASK, they can sometimes be
8673    ignored.
8674
8675    Return a possibly simplified expression, but always convert X to
8676    MODE.  If X is a CONST_INT, AND the CONST_INT with MASK.
8677
8678    If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8679    are all off in X.  This is used when X will be complemented, by either
8680    NOT, NEG, or XOR.  */
8681
8682 static rtx
8683 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8684                int just_select)
8685 {
8686   enum rtx_code code = GET_CODE (x);
8687   int next_select = just_select || code == XOR || code == NOT || code == NEG;
8688   machine_mode op_mode;
8689   unsigned HOST_WIDE_INT nonzero;
8690
8691   /* If this is a CALL or ASM_OPERANDS, don't do anything.  Some of the
8692      code below will do the wrong thing since the mode of such an
8693      expression is VOIDmode.
8694
8695      Also do nothing if X is a CLOBBER; this can happen if X was
8696      the return value from a call to gen_lowpart.  */
8697   if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8698     return x;
8699
8700   /* We want to perform the operation in its present mode unless we know
8701      that the operation is valid in MODE, in which case we do the operation
8702      in MODE.  */
8703   op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8704               && have_insn_for (code, mode))
8705              ? mode : GET_MODE (x));
8706
8707   /* It is not valid to do a right-shift in a narrower mode
8708      than the one it came in with.  */
8709   if ((code == LSHIFTRT || code == ASHIFTRT)
8710       && partial_subreg_p (mode, GET_MODE (x)))
8711     op_mode = GET_MODE (x);
8712
8713   /* Truncate MASK to fit OP_MODE.  */
8714   if (op_mode)
8715     mask &= GET_MODE_MASK (op_mode);
8716
8717   /* Determine what bits of X are guaranteed to be (non)zero.  */
8718   nonzero = nonzero_bits (x, mode);
8719
8720   /* If none of the bits in X are needed, return a zero.  */
8721   if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8722     x = const0_rtx;
8723
8724   /* If X is a CONST_INT, return a new one.  Do this here since the
8725      test below will fail.  */
8726   if (CONST_INT_P (x))
8727     {
8728       if (SCALAR_INT_MODE_P (mode))
8729         return gen_int_mode (INTVAL (x) & mask, mode);
8730       else
8731         {
8732           x = GEN_INT (INTVAL (x) & mask);
8733           return gen_lowpart_common (mode, x);
8734         }
8735     }
8736
8737   /* If X is narrower than MODE and we want all the bits in X's mode, just
8738      get X in the proper mode.  */
8739   if (paradoxical_subreg_p (mode, GET_MODE (x))
8740       && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8741     return gen_lowpart (mode, x);
8742
8743   /* We can ignore the effect of a SUBREG if it narrows the mode or
8744      if the constant masks to zero all the bits the mode doesn't have.  */
8745   if (GET_CODE (x) == SUBREG
8746       && subreg_lowpart_p (x)
8747       && (partial_subreg_p (x)
8748           || (mask
8749               & GET_MODE_MASK (GET_MODE (x))
8750               & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8751     return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8752
8753   scalar_int_mode int_mode, xmode;
8754   if (is_a <scalar_int_mode> (mode, &int_mode)
8755       && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8756     /* OP_MODE is either MODE or XMODE, so it must be a scalar
8757        integer too.  */
8758     return force_int_to_mode (x, int_mode, xmode,
8759                               as_a <scalar_int_mode> (op_mode),
8760                               mask, just_select);
8761
8762   return gen_lowpart_or_truncate (mode, x);
8763 }
8764
8765 /* Subroutine of force_to_mode that handles cases in which both X and
8766    the result are scalar integers.  MODE is the mode of the result,
8767    XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8768    is preferred for simplified versions of X.  The other arguments
8769    are as for force_to_mode.  */
8770
8771 static rtx
8772 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8773                    scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8774                    int just_select)
8775 {
8776   enum rtx_code code = GET_CODE (x);
8777   int next_select = just_select || code == XOR || code == NOT || code == NEG;
8778   unsigned HOST_WIDE_INT fuller_mask;
8779   rtx op0, op1, temp;
8780   poly_int64 const_op0;
8781
8782   /* When we have an arithmetic operation, or a shift whose count we
8783      do not know, we need to assume that all bits up to the highest-order
8784      bit in MASK will be needed.  This is how we form such a mask.  */
8785   if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8786     fuller_mask = HOST_WIDE_INT_M1U;
8787   else
8788     fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8789                    - 1);
8790
8791   switch (code)
8792     {
8793     case CLOBBER:
8794       /* If X is a (clobber (const_int)), return it since we know we are
8795          generating something that won't match.  */
8796       return x;
8797
8798     case SIGN_EXTEND:
8799     case ZERO_EXTEND:
8800     case ZERO_EXTRACT:
8801     case SIGN_EXTRACT:
8802       x = expand_compound_operation (x);
8803       if (GET_CODE (x) != code)
8804         return force_to_mode (x, mode, mask, next_select);
8805       break;
8806
8807     case TRUNCATE:
8808       /* Similarly for a truncate.  */
8809       return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8810
8811     case AND:
8812       /* If this is an AND with a constant, convert it into an AND
8813          whose constant is the AND of that constant with MASK.  If it
8814          remains an AND of MASK, delete it since it is redundant.  */
8815
8816       if (CONST_INT_P (XEXP (x, 1)))
8817         {
8818           x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8819                                       mask & INTVAL (XEXP (x, 1)));
8820           xmode = op_mode;
8821
8822           /* If X is still an AND, see if it is an AND with a mask that
8823              is just some low-order bits.  If so, and it is MASK, we don't
8824              need it.  */
8825
8826           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8827               && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8828             x = XEXP (x, 0);
8829
8830           /* If it remains an AND, try making another AND with the bits
8831              in the mode mask that aren't in MASK turned on.  If the
8832              constant in the AND is wide enough, this might make a
8833              cheaper constant.  */
8834
8835           if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8836               && GET_MODE_MASK (xmode) != mask
8837               && HWI_COMPUTABLE_MODE_P (xmode))
8838             {
8839               unsigned HOST_WIDE_INT cval
8840                 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8841               rtx y;
8842
8843               y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8844                                        gen_int_mode (cval, xmode));
8845               if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8846                   < set_src_cost (x, xmode, optimize_this_for_speed_p))
8847                 x = y;
8848             }
8849
8850           break;
8851         }
8852
8853       goto binop;
8854
8855     case PLUS:
8856       /* In (and (plus FOO C1) M), if M is a mask that just turns off
8857          low-order bits (as in an alignment operation) and FOO is already
8858          aligned to that boundary, mask C1 to that boundary as well.
8859          This may eliminate that PLUS and, later, the AND.  */
8860
8861       {
8862         unsigned int width = GET_MODE_PRECISION (mode);
8863         unsigned HOST_WIDE_INT smask = mask;
8864
8865         /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8866            number, sign extend it.  */
8867
8868         if (width < HOST_BITS_PER_WIDE_INT
8869             && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8870           smask |= HOST_WIDE_INT_M1U << width;
8871
8872         if (CONST_INT_P (XEXP (x, 1))
8873             && pow2p_hwi (- smask)
8874             && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8875             && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8876           return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8877                                                (INTVAL (XEXP (x, 1)) & smask)),
8878                                 mode, smask, next_select);
8879       }
8880
8881       /* fall through */
8882
8883     case MULT:
8884       /* Substituting into the operands of a widening MULT is not likely to
8885          create RTL matching a machine insn.  */
8886       if (code == MULT
8887           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8888               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8889           && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8890               || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8891           && REG_P (XEXP (XEXP (x, 0), 0))
8892           && REG_P (XEXP (XEXP (x, 1), 0)))
8893         return gen_lowpart_or_truncate (mode, x);
8894
8895       /* For PLUS, MINUS and MULT, we need any bits less significant than the
8896          most significant bit in MASK since carries from those bits will
8897          affect the bits we are interested in.  */
8898       mask = fuller_mask;
8899       goto binop;
8900
8901     case MINUS:
8902       /* If X is (minus C Y) where C's least set bit is larger than any bit
8903          in the mask, then we may replace with (neg Y).  */
8904       if (poly_int_rtx_p (XEXP (x, 0), &const_op0)
8905           && (unsigned HOST_WIDE_INT) known_alignment (const_op0) > mask)
8906         {
8907           x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8908           return force_to_mode (x, mode, mask, next_select);
8909         }
8910
8911       /* Similarly, if C contains every bit in the fuller_mask, then we may
8912          replace with (not Y).  */
8913       if (CONST_INT_P (XEXP (x, 0))
8914           && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8915         {
8916           x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8917           return force_to_mode (x, mode, mask, next_select);
8918         }
8919
8920       mask = fuller_mask;
8921       goto binop;
8922
8923     case IOR:
8924     case XOR:
8925       /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8926          LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8927          operation which may be a bitfield extraction.  Ensure that the
8928          constant we form is not wider than the mode of X.  */
8929
8930       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8931           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8932           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8933           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8934           && CONST_INT_P (XEXP (x, 1))
8935           && ((INTVAL (XEXP (XEXP (x, 0), 1))
8936                + floor_log2 (INTVAL (XEXP (x, 1))))
8937               < GET_MODE_PRECISION (xmode))
8938           && (UINTVAL (XEXP (x, 1))
8939               & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8940         {
8941           temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8942                                << INTVAL (XEXP (XEXP (x, 0), 1)),
8943                                xmode);
8944           temp = simplify_gen_binary (GET_CODE (x), xmode,
8945                                       XEXP (XEXP (x, 0), 0), temp);
8946           x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8947                                    XEXP (XEXP (x, 0), 1));
8948           return force_to_mode (x, mode, mask, next_select);
8949         }
8950
8951     binop:
8952       /* For most binary operations, just propagate into the operation and
8953          change the mode if we have an operation of that mode.  */
8954
8955       op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8956       op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8957
8958       /* If we ended up truncating both operands, truncate the result of the
8959          operation instead.  */
8960       if (GET_CODE (op0) == TRUNCATE
8961           && GET_CODE (op1) == TRUNCATE)
8962         {
8963           op0 = XEXP (op0, 0);
8964           op1 = XEXP (op1, 0);
8965         }
8966
8967       op0 = gen_lowpart_or_truncate (op_mode, op0);
8968       op1 = gen_lowpart_or_truncate (op_mode, op1);
8969
8970       if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8971         {
8972           x = simplify_gen_binary (code, op_mode, op0, op1);
8973           xmode = op_mode;
8974         }
8975       break;
8976
8977     case ASHIFT:
8978       /* For left shifts, do the same, but just for the first operand.
8979          However, we cannot do anything with shifts where we cannot
8980          guarantee that the counts are smaller than the size of the mode
8981          because such a count will have a different meaning in a
8982          wider mode.  */
8983
8984       if (! (CONST_INT_P (XEXP (x, 1))
8985              && INTVAL (XEXP (x, 1)) >= 0
8986              && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8987           && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8988                 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8989                     < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8990         break;
8991
8992       /* If the shift count is a constant and we can do arithmetic in
8993          the mode of the shift, refine which bits we need.  Otherwise, use the
8994          conservative form of the mask.  */
8995       if (CONST_INT_P (XEXP (x, 1))
8996           && INTVAL (XEXP (x, 1)) >= 0
8997           && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8998           && HWI_COMPUTABLE_MODE_P (op_mode))
8999         mask >>= INTVAL (XEXP (x, 1));
9000       else
9001         mask = fuller_mask;
9002
9003       op0 = gen_lowpart_or_truncate (op_mode,
9004                                      force_to_mode (XEXP (x, 0), mode,
9005                                                     mask, next_select));
9006
9007       if (op_mode != xmode || op0 != XEXP (x, 0))
9008         {
9009           x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
9010           xmode = op_mode;
9011         }
9012       break;
9013
9014     case LSHIFTRT:
9015       /* Here we can only do something if the shift count is a constant,
9016          this shift constant is valid for the host, and we can do arithmetic
9017          in OP_MODE.  */
9018
9019       if (CONST_INT_P (XEXP (x, 1))
9020           && INTVAL (XEXP (x, 1)) >= 0
9021           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
9022           && HWI_COMPUTABLE_MODE_P (op_mode))
9023         {
9024           rtx inner = XEXP (x, 0);
9025           unsigned HOST_WIDE_INT inner_mask;
9026
9027           /* Select the mask of the bits we need for the shift operand.  */
9028           inner_mask = mask << INTVAL (XEXP (x, 1));
9029
9030           /* We can only change the mode of the shift if we can do arithmetic
9031              in the mode of the shift and INNER_MASK is no wider than the
9032              width of X's mode.  */
9033           if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
9034             op_mode = xmode;
9035
9036           inner = force_to_mode (inner, op_mode, inner_mask, next_select);
9037
9038           if (xmode != op_mode || inner != XEXP (x, 0))
9039             {
9040               x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
9041               xmode = op_mode;
9042             }
9043         }
9044
9045       /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
9046          shift and AND produces only copies of the sign bit (C2 is one less
9047          than a power of two), we can do this with just a shift.  */
9048
9049       if (GET_CODE (x) == LSHIFTRT
9050           && CONST_INT_P (XEXP (x, 1))
9051           /* The shift puts one of the sign bit copies in the least significant
9052              bit.  */
9053           && ((INTVAL (XEXP (x, 1))
9054                + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
9055               >= GET_MODE_PRECISION (xmode))
9056           && pow2p_hwi (mask + 1)
9057           /* Number of bits left after the shift must be more than the mask
9058              needs.  */
9059           && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
9060               <= GET_MODE_PRECISION (xmode))
9061           /* Must be more sign bit copies than the mask needs.  */
9062           && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
9063               >= exact_log2 (mask + 1)))
9064         {
9065           int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
9066           x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
9067                                    gen_int_shift_amount (xmode, nbits));
9068         }
9069       goto shiftrt;
9070
9071     case ASHIFTRT:
9072       /* If we are just looking for the sign bit, we don't need this shift at
9073          all, even if it has a variable count.  */
9074       if (val_signbit_p (xmode, mask))
9075         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9076
9077       /* If this is a shift by a constant, get a mask that contains those bits
9078          that are not copies of the sign bit.  We then have two cases:  If
9079          MASK only includes those bits, this can be a logical shift, which may
9080          allow simplifications.  If MASK is a single-bit field not within
9081          those bits, we are requesting a copy of the sign bit and hence can
9082          shift the sign bit to the appropriate location.  */
9083
9084       if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9085           && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9086         {
9087           unsigned HOST_WIDE_INT nonzero;
9088           int i;
9089
9090           /* If the considered data is wider than HOST_WIDE_INT, we can't
9091              represent a mask for all its bits in a single scalar.
9092              But we only care about the lower bits, so calculate these.  */
9093
9094           if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9095             {
9096               nonzero = HOST_WIDE_INT_M1U;
9097
9098               /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9099                  is the number of bits a full-width mask would have set.
9100                  We need only shift if these are fewer than nonzero can
9101                  hold.  If not, we must keep all bits set in nonzero.  */
9102
9103               if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9104                   < HOST_BITS_PER_WIDE_INT)
9105                 nonzero >>= INTVAL (XEXP (x, 1))
9106                             + HOST_BITS_PER_WIDE_INT
9107                             - GET_MODE_PRECISION (xmode);
9108             }
9109           else
9110             {
9111               nonzero = GET_MODE_MASK (xmode);
9112               nonzero >>= INTVAL (XEXP (x, 1));
9113             }
9114
9115           if ((mask & ~nonzero) == 0)
9116             {
9117               x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9118                                         XEXP (x, 0), INTVAL (XEXP (x, 1)));
9119               if (GET_CODE (x) != ASHIFTRT)
9120                 return force_to_mode (x, mode, mask, next_select);
9121             }
9122
9123           else if ((i = exact_log2 (mask)) >= 0)
9124             {
9125               x = simplify_shift_const
9126                   (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9127                    GET_MODE_PRECISION (xmode) - 1 - i);
9128
9129               if (GET_CODE (x) != ASHIFTRT)
9130                 return force_to_mode (x, mode, mask, next_select);
9131             }
9132         }
9133
9134       /* If MASK is 1, convert this to an LSHIFTRT.  This can be done
9135          even if the shift count isn't a constant.  */
9136       if (mask == 1)
9137         x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9138
9139     shiftrt:
9140
9141       /* If this is a zero- or sign-extension operation that just affects bits
9142          we don't care about, remove it.  Be sure the call above returned
9143          something that is still a shift.  */
9144
9145       if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9146           && CONST_INT_P (XEXP (x, 1))
9147           && INTVAL (XEXP (x, 1)) >= 0
9148           && (INTVAL (XEXP (x, 1))
9149               <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9150           && GET_CODE (XEXP (x, 0)) == ASHIFT
9151           && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9152         return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
9153                               next_select);
9154
9155       break;
9156
9157     case ROTATE:
9158     case ROTATERT:
9159       /* If the shift count is constant and we can do computations
9160          in the mode of X, compute where the bits we care about are.
9161          Otherwise, we can't do anything.  Don't change the mode of
9162          the shift or propagate MODE into the shift, though.  */
9163       if (CONST_INT_P (XEXP (x, 1))
9164           && INTVAL (XEXP (x, 1)) >= 0)
9165         {
9166           temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9167                                             xmode, gen_int_mode (mask, xmode),
9168                                             XEXP (x, 1));
9169           if (temp && CONST_INT_P (temp))
9170             x = simplify_gen_binary (code, xmode,
9171                                      force_to_mode (XEXP (x, 0), xmode,
9172                                                     INTVAL (temp), next_select),
9173                                      XEXP (x, 1));
9174         }
9175       break;
9176
9177     case NEG:
9178       /* If we just want the low-order bit, the NEG isn't needed since it
9179          won't change the low-order bit.  */
9180       if (mask == 1)
9181         return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9182
9183       /* We need any bits less significant than the most significant bit in
9184          MASK since carries from those bits will affect the bits we are
9185          interested in.  */
9186       mask = fuller_mask;
9187       goto unop;
9188
9189     case NOT:
9190       /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9191          same as the XOR case above.  Ensure that the constant we form is not
9192          wider than the mode of X.  */
9193
9194       if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9195           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9196           && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9197           && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9198               < GET_MODE_PRECISION (xmode))
9199           && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9200         {
9201           temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9202           temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9203           x = simplify_gen_binary (LSHIFTRT, xmode,
9204                                    temp, XEXP (XEXP (x, 0), 1));
9205
9206           return force_to_mode (x, mode, mask, next_select);
9207         }
9208
9209       /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9210          use the full mask inside the NOT.  */
9211       mask = fuller_mask;
9212
9213     unop:
9214       op0 = gen_lowpart_or_truncate (op_mode,
9215                                      force_to_mode (XEXP (x, 0), mode, mask,
9216                                                     next_select));
9217       if (op_mode != xmode || op0 != XEXP (x, 0))
9218         {
9219           x = simplify_gen_unary (code, op_mode, op0, op_mode);
9220           xmode = op_mode;
9221         }
9222       break;
9223
9224     case NE:
9225       /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9226          in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9227          which is equal to STORE_FLAG_VALUE.  */
9228       if ((mask & ~STORE_FLAG_VALUE) == 0
9229           && XEXP (x, 1) == const0_rtx
9230           && GET_MODE (XEXP (x, 0)) == mode
9231           && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9232           && (nonzero_bits (XEXP (x, 0), mode)
9233               == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9234         return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9235
9236       break;
9237
9238     case IF_THEN_ELSE:
9239       /* We have no way of knowing if the IF_THEN_ELSE can itself be
9240          written in a narrower mode.  We play it safe and do not do so.  */
9241
9242       op0 = gen_lowpart_or_truncate (xmode,
9243                                      force_to_mode (XEXP (x, 1), mode,
9244                                                     mask, next_select));
9245       op1 = gen_lowpart_or_truncate (xmode,
9246                                      force_to_mode (XEXP (x, 2), mode,
9247                                                     mask, next_select));
9248       if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9249         x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9250                                   GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9251                                   op0, op1);
9252       break;
9253
9254     default:
9255       break;
9256     }
9257
9258   /* Ensure we return a value of the proper mode.  */
9259   return gen_lowpart_or_truncate (mode, x);
9260 }
9261 \f
9262 /* Return nonzero if X is an expression that has one of two values depending on
9263    whether some other value is zero or nonzero.  In that case, we return the
9264    value that is being tested, *PTRUE is set to the value if the rtx being
9265    returned has a nonzero value, and *PFALSE is set to the other alternative.
9266
9267    If we return zero, we set *PTRUE and *PFALSE to X.  */
9268
9269 static rtx
9270 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9271 {
9272   machine_mode mode = GET_MODE (x);
9273   enum rtx_code code = GET_CODE (x);
9274   rtx cond0, cond1, true0, true1, false0, false1;
9275   unsigned HOST_WIDE_INT nz;
9276   scalar_int_mode int_mode;
9277
9278   /* If we are comparing a value against zero, we are done.  */
9279   if ((code == NE || code == EQ)
9280       && XEXP (x, 1) == const0_rtx)
9281     {
9282       *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9283       *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9284       return XEXP (x, 0);
9285     }
9286
9287   /* If this is a unary operation whose operand has one of two values, apply
9288      our opcode to compute those values.  */
9289   else if (UNARY_P (x)
9290            && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9291     {
9292       *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9293       *pfalse = simplify_gen_unary (code, mode, false0,
9294                                     GET_MODE (XEXP (x, 0)));
9295       return cond0;
9296     }
9297
9298   /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9299      make can't possibly match and would suppress other optimizations.  */
9300   else if (code == COMPARE)
9301     ;
9302
9303   /* If this is a binary operation, see if either side has only one of two
9304      values.  If either one does or if both do and they are conditional on
9305      the same value, compute the new true and false values.  */
9306   else if (BINARY_P (x))
9307     {
9308       rtx op0 = XEXP (x, 0);
9309       rtx op1 = XEXP (x, 1);
9310       cond0 = if_then_else_cond (op0, &true0, &false0);
9311       cond1 = if_then_else_cond (op1, &true1, &false1);
9312
9313       if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9314           && (REG_P (op0) || REG_P (op1)))
9315         {
9316           /* Try to enable a simplification by undoing work done by
9317              if_then_else_cond if it converted a REG into something more
9318              complex.  */
9319           if (REG_P (op0))
9320             {
9321               cond0 = 0;
9322               true0 = false0 = op0;
9323             }
9324           else
9325             {
9326               cond1 = 0;
9327               true1 = false1 = op1;
9328             }
9329         }
9330
9331       if ((cond0 != 0 || cond1 != 0)
9332           && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9333         {
9334           /* If if_then_else_cond returned zero, then true/false are the
9335              same rtl.  We must copy one of them to prevent invalid rtl
9336              sharing.  */
9337           if (cond0 == 0)
9338             true0 = copy_rtx (true0);
9339           else if (cond1 == 0)
9340             true1 = copy_rtx (true1);
9341
9342           if (COMPARISON_P (x))
9343             {
9344               *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9345                                                 true0, true1);
9346               *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9347                                                  false0, false1);
9348              }
9349           else
9350             {
9351               *ptrue = simplify_gen_binary (code, mode, true0, true1);
9352               *pfalse = simplify_gen_binary (code, mode, false0, false1);
9353             }
9354
9355           return cond0 ? cond0 : cond1;
9356         }
9357
9358       /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9359          operands is zero when the other is nonzero, and vice-versa,
9360          and STORE_FLAG_VALUE is 1 or -1.  */
9361
9362       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9363           && (code == PLUS || code == IOR || code == XOR || code == MINUS
9364               || code == UMAX)
9365           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9366         {
9367           rtx op0 = XEXP (XEXP (x, 0), 1);
9368           rtx op1 = XEXP (XEXP (x, 1), 1);
9369
9370           cond0 = XEXP (XEXP (x, 0), 0);
9371           cond1 = XEXP (XEXP (x, 1), 0);
9372
9373           if (COMPARISON_P (cond0)
9374               && COMPARISON_P (cond1)
9375               && SCALAR_INT_MODE_P (mode)
9376               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9377                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9378                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9379                   || ((swap_condition (GET_CODE (cond0))
9380                        == reversed_comparison_code (cond1, NULL))
9381                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9382                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9383               && ! side_effects_p (x))
9384             {
9385               *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9386               *pfalse = simplify_gen_binary (MULT, mode,
9387                                              (code == MINUS
9388                                               ? simplify_gen_unary (NEG, mode,
9389                                                                     op1, mode)
9390                                               : op1),
9391                                               const_true_rtx);
9392               return cond0;
9393             }
9394         }
9395
9396       /* Similarly for MULT, AND and UMIN, except that for these the result
9397          is always zero.  */
9398       if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9399           && (code == MULT || code == AND || code == UMIN)
9400           && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9401         {
9402           cond0 = XEXP (XEXP (x, 0), 0);
9403           cond1 = XEXP (XEXP (x, 1), 0);
9404
9405           if (COMPARISON_P (cond0)
9406               && COMPARISON_P (cond1)
9407               && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9408                    && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9409                    && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9410                   || ((swap_condition (GET_CODE (cond0))
9411                        == reversed_comparison_code (cond1, NULL))
9412                       && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9413                       && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9414               && ! side_effects_p (x))
9415             {
9416               *ptrue = *pfalse = const0_rtx;
9417               return cond0;
9418             }
9419         }
9420     }
9421
9422   else if (code == IF_THEN_ELSE)
9423     {
9424       /* If we have IF_THEN_ELSE already, extract the condition and
9425          canonicalize it if it is NE or EQ.  */
9426       cond0 = XEXP (x, 0);
9427       *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9428       if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9429         return XEXP (cond0, 0);
9430       else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9431         {
9432           *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9433           return XEXP (cond0, 0);
9434         }
9435       else
9436         return cond0;
9437     }
9438
9439   /* If X is a SUBREG, we can narrow both the true and false values
9440      if the inner expression, if there is a condition.  */
9441   else if (code == SUBREG
9442            && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9443                                           &false0)) != 0)
9444     {
9445       true0 = simplify_gen_subreg (mode, true0,
9446                                    GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9447       false0 = simplify_gen_subreg (mode, false0,
9448                                     GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9449       if (true0 && false0)
9450         {
9451           *ptrue = true0;
9452           *pfalse = false0;
9453           return cond0;
9454         }
9455     }
9456
9457   /* If X is a constant, this isn't special and will cause confusions
9458      if we treat it as such.  Likewise if it is equivalent to a constant.  */
9459   else if (CONSTANT_P (x)
9460            || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9461     ;
9462
9463   /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9464      will be least confusing to the rest of the compiler.  */
9465   else if (mode == BImode)
9466     {
9467       *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9468       return x;
9469     }
9470
9471   /* If X is known to be either 0 or -1, those are the true and
9472      false values when testing X.  */
9473   else if (x == constm1_rtx || x == const0_rtx
9474            || (is_a <scalar_int_mode> (mode, &int_mode)
9475                && (num_sign_bit_copies (x, int_mode)
9476                    == GET_MODE_PRECISION (int_mode))))
9477     {
9478       *ptrue = constm1_rtx, *pfalse = const0_rtx;
9479       return x;
9480     }
9481
9482   /* Likewise for 0 or a single bit.  */
9483   else if (HWI_COMPUTABLE_MODE_P (mode)
9484            && pow2p_hwi (nz = nonzero_bits (x, mode)))
9485     {
9486       *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9487       return x;
9488     }
9489
9490   /* Otherwise fail; show no condition with true and false values the same.  */
9491   *ptrue = *pfalse = x;
9492   return 0;
9493 }
9494 \f
9495 /* Return the value of expression X given the fact that condition COND
9496    is known to be true when applied to REG as its first operand and VAL
9497    as its second.  X is known to not be shared and so can be modified in
9498    place.
9499
9500    We only handle the simplest cases, and specifically those cases that
9501    arise with IF_THEN_ELSE expressions.  */
9502
9503 static rtx
9504 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9505 {
9506   enum rtx_code code = GET_CODE (x);
9507   const char *fmt;
9508   int i, j;
9509
9510   if (side_effects_p (x))
9511     return x;
9512
9513   /* If either operand of the condition is a floating point value,
9514      then we have to avoid collapsing an EQ comparison.  */
9515   if (cond == EQ
9516       && rtx_equal_p (x, reg)
9517       && ! FLOAT_MODE_P (GET_MODE (x))
9518       && ! FLOAT_MODE_P (GET_MODE (val)))
9519     return val;
9520
9521   if (cond == UNEQ && rtx_equal_p (x, reg))
9522     return val;
9523
9524   /* If X is (abs REG) and we know something about REG's relationship
9525      with zero, we may be able to simplify this.  */
9526
9527   if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9528     switch (cond)
9529       {
9530       case GE:  case GT:  case EQ:
9531         return XEXP (x, 0);
9532       case LT:  case LE:
9533         return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9534                                    XEXP (x, 0),
9535                                    GET_MODE (XEXP (x, 0)));
9536       default:
9537         break;
9538       }
9539
9540   /* The only other cases we handle are MIN, MAX, and comparisons if the
9541      operands are the same as REG and VAL.  */
9542
9543   else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9544     {
9545       if (rtx_equal_p (XEXP (x, 0), val))
9546         {
9547           std::swap (val, reg);
9548           cond = swap_condition (cond);
9549         }
9550
9551       if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9552         {
9553           if (COMPARISON_P (x))
9554             {
9555               if (comparison_dominates_p (cond, code))
9556                 return VECTOR_MODE_P (GET_MODE (x)) ? x : const_true_rtx;
9557
9558               code = reversed_comparison_code (x, NULL);
9559               if (code != UNKNOWN
9560                   && comparison_dominates_p (cond, code))
9561                 return CONST0_RTX (GET_MODE (x));
9562               else
9563                 return x;
9564             }
9565           else if (code == SMAX || code == SMIN
9566                    || code == UMIN || code == UMAX)
9567             {
9568               int unsignedp = (code == UMIN || code == UMAX);
9569
9570               /* Do not reverse the condition when it is NE or EQ.
9571                  This is because we cannot conclude anything about
9572                  the value of 'SMAX (x, y)' when x is not equal to y,
9573                  but we can when x equals y.  */
9574               if ((code == SMAX || code == UMAX)
9575                   && ! (cond == EQ || cond == NE))
9576                 cond = reverse_condition (cond);
9577
9578               switch (cond)
9579                 {
9580                 case GE:   case GT:
9581                   return unsignedp ? x : XEXP (x, 1);
9582                 case LE:   case LT:
9583                   return unsignedp ? x : XEXP (x, 0);
9584                 case GEU:  case GTU:
9585                   return unsignedp ? XEXP (x, 1) : x;
9586                 case LEU:  case LTU:
9587                   return unsignedp ? XEXP (x, 0) : x;
9588                 default:
9589                   break;
9590                 }
9591             }
9592         }
9593     }
9594   else if (code == SUBREG)
9595     {
9596       machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9597       rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9598
9599       if (SUBREG_REG (x) != r)
9600         {
9601           /* We must simplify subreg here, before we lose track of the
9602              original inner_mode.  */
9603           new_rtx = simplify_subreg (GET_MODE (x), r,
9604                                      inner_mode, SUBREG_BYTE (x));
9605           if (new_rtx)
9606             return new_rtx;
9607           else
9608             SUBST (SUBREG_REG (x), r);
9609         }
9610
9611       return x;
9612     }
9613   /* We don't have to handle SIGN_EXTEND here, because even in the
9614      case of replacing something with a modeless CONST_INT, a
9615      CONST_INT is already (supposed to be) a valid sign extension for
9616      its narrower mode, which implies it's already properly
9617      sign-extended for the wider mode.  Now, for ZERO_EXTEND, the
9618      story is different.  */
9619   else if (code == ZERO_EXTEND)
9620     {
9621       machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9622       rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9623
9624       if (XEXP (x, 0) != r)
9625         {
9626           /* We must simplify the zero_extend here, before we lose
9627              track of the original inner_mode.  */
9628           new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9629                                               r, inner_mode);
9630           if (new_rtx)
9631             return new_rtx;
9632           else
9633             SUBST (XEXP (x, 0), r);
9634         }
9635
9636       return x;
9637     }
9638
9639   fmt = GET_RTX_FORMAT (code);
9640   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9641     {
9642       if (fmt[i] == 'e')
9643         SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9644       else if (fmt[i] == 'E')
9645         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9646           SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9647                                                 cond, reg, val));
9648     }
9649
9650   return x;
9651 }
9652 \f
9653 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9654    assignment as a field assignment.  */
9655
9656 static int
9657 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9658 {
9659   if (widen_x && GET_MODE (x) != GET_MODE (y))
9660     {
9661       if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9662         return 0;
9663       if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9664         return 0;
9665       x = adjust_address_nv (x, GET_MODE (y),
9666                              byte_lowpart_offset (GET_MODE (y),
9667                                                   GET_MODE (x)));
9668     }
9669
9670   if (x == y || rtx_equal_p (x, y))
9671     return 1;
9672
9673   if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9674     return 0;
9675
9676   /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9677      Note that all SUBREGs of MEM are paradoxical; otherwise they
9678      would have been rewritten.  */
9679   if (MEM_P (x) && GET_CODE (y) == SUBREG
9680       && MEM_P (SUBREG_REG (y))
9681       && rtx_equal_p (SUBREG_REG (y),
9682                       gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9683     return 1;
9684
9685   if (MEM_P (y) && GET_CODE (x) == SUBREG
9686       && MEM_P (SUBREG_REG (x))
9687       && rtx_equal_p (SUBREG_REG (x),
9688                       gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9689     return 1;
9690
9691   /* We used to see if get_last_value of X and Y were the same but that's
9692      not correct.  In one direction, we'll cause the assignment to have
9693      the wrong destination and in the case, we'll import a register into this
9694      insn that might have already have been dead.   So fail if none of the
9695      above cases are true.  */
9696   return 0;
9697 }
9698 \f
9699 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9700    Return that assignment if so.
9701
9702    We only handle the most common cases.  */
9703
9704 static rtx
9705 make_field_assignment (rtx x)
9706 {
9707   rtx dest = SET_DEST (x);
9708   rtx src = SET_SRC (x);
9709   rtx assign;
9710   rtx rhs, lhs;
9711   HOST_WIDE_INT c1;
9712   HOST_WIDE_INT pos;
9713   unsigned HOST_WIDE_INT len;
9714   rtx other;
9715
9716   /* All the rules in this function are specific to scalar integers.  */
9717   scalar_int_mode mode;
9718   if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9719     return x;
9720
9721   /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9722      a clear of a one-bit field.  We will have changed it to
9723      (and (rotate (const_int -2) POS) DEST), so check for that.  Also check
9724      for a SUBREG.  */
9725
9726   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9727       && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9728       && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9729       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9730     {
9731       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9732                                 1, 1, 1, 0);
9733       if (assign != 0)
9734         return gen_rtx_SET (assign, const0_rtx);
9735       return x;
9736     }
9737
9738   if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9739       && subreg_lowpart_p (XEXP (src, 0))
9740       && partial_subreg_p (XEXP (src, 0))
9741       && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9742       && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9743       && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9744       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9745     {
9746       assign = make_extraction (VOIDmode, dest, 0,
9747                                 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9748                                 1, 1, 1, 0);
9749       if (assign != 0)
9750         return gen_rtx_SET (assign, const0_rtx);
9751       return x;
9752     }
9753
9754   /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9755      one-bit field.  */
9756   if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9757       && XEXP (XEXP (src, 0), 0) == const1_rtx
9758       && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9759     {
9760       assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9761                                 1, 1, 1, 0);
9762       if (assign != 0)
9763         return gen_rtx_SET (assign, const1_rtx);
9764       return x;
9765     }
9766
9767   /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9768      SRC is an AND with all bits of that field set, then we can discard
9769      the AND.  */
9770   if (GET_CODE (dest) == ZERO_EXTRACT
9771       && CONST_INT_P (XEXP (dest, 1))
9772       && GET_CODE (src) == AND
9773       && CONST_INT_P (XEXP (src, 1)))
9774     {
9775       HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9776       unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9777       unsigned HOST_WIDE_INT ze_mask;
9778
9779       if (width >= HOST_BITS_PER_WIDE_INT)
9780         ze_mask = -1;
9781       else
9782         ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9783
9784       /* Complete overlap.  We can remove the source AND.  */
9785       if ((and_mask & ze_mask) == ze_mask)
9786         return gen_rtx_SET (dest, XEXP (src, 0));
9787
9788       /* Partial overlap.  We can reduce the source AND.  */
9789       if ((and_mask & ze_mask) != and_mask)
9790         {
9791           src = gen_rtx_AND (mode, XEXP (src, 0),
9792                              gen_int_mode (and_mask & ze_mask, mode));
9793           return gen_rtx_SET (dest, src);
9794         }
9795     }
9796
9797   /* The other case we handle is assignments into a constant-position
9798      field.  They look like (ior/xor (and DEST C1) OTHER).  If C1 represents
9799      a mask that has all one bits except for a group of zero bits and
9800      OTHER is known to have zeros where C1 has ones, this is such an
9801      assignment.  Compute the position and length from C1.  Shift OTHER
9802      to the appropriate position, force it to the required mode, and
9803      make the extraction.  Check for the AND in both operands.  */
9804
9805   /* One or more SUBREGs might obscure the constant-position field
9806      assignment.  The first one we are likely to encounter is an outer
9807      narrowing SUBREG, which we can just strip for the purposes of
9808      identifying the constant-field assignment.  */
9809   scalar_int_mode src_mode = mode;
9810   if (GET_CODE (src) == SUBREG
9811       && subreg_lowpart_p (src)
9812       && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9813     src = SUBREG_REG (src);
9814
9815   if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9816     return x;
9817
9818   rhs = expand_compound_operation (XEXP (src, 0));
9819   lhs = expand_compound_operation (XEXP (src, 1));
9820
9821   if (GET_CODE (rhs) == AND
9822       && CONST_INT_P (XEXP (rhs, 1))
9823       && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9824     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9825   /* The second SUBREG that might get in the way is a paradoxical
9826      SUBREG around the first operand of the AND.  We want to 
9827      pretend the operand is as wide as the destination here.   We
9828      do this by adjusting the MEM to wider mode for the sole
9829      purpose of the call to rtx_equal_for_field_assignment_p.   Also
9830      note this trick only works for MEMs.  */
9831   else if (GET_CODE (rhs) == AND
9832            && paradoxical_subreg_p (XEXP (rhs, 0))
9833            && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9834            && CONST_INT_P (XEXP (rhs, 1))
9835            && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9836                                                 dest, true))
9837     c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9838   else if (GET_CODE (lhs) == AND
9839            && CONST_INT_P (XEXP (lhs, 1))
9840            && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9841     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9842   /* The second SUBREG that might get in the way is a paradoxical
9843      SUBREG around the first operand of the AND.  We want to 
9844      pretend the operand is as wide as the destination here.   We
9845      do this by adjusting the MEM to wider mode for the sole
9846      purpose of the call to rtx_equal_for_field_assignment_p.   Also
9847      note this trick only works for MEMs.  */
9848   else if (GET_CODE (lhs) == AND
9849            && paradoxical_subreg_p (XEXP (lhs, 0))
9850            && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9851            && CONST_INT_P (XEXP (lhs, 1))
9852            && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9853                                                 dest, true))
9854     c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9855   else
9856     return x;
9857
9858   pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9859   if (pos < 0
9860       || pos + len > GET_MODE_PRECISION (mode)
9861       || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9862       || (c1 & nonzero_bits (other, mode)) != 0)
9863     return x;
9864
9865   assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9866   if (assign == 0)
9867     return x;
9868
9869   /* The mode to use for the source is the mode of the assignment, or of
9870      what is inside a possible STRICT_LOW_PART.  */
9871   machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9872                            ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9873
9874   /* Shift OTHER right POS places and make it the source, restricting it
9875      to the proper length and mode.  */
9876
9877   src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9878                                                      src_mode, other, pos),
9879                                dest);
9880   src = force_to_mode (src, new_mode,
9881                        len >= HOST_BITS_PER_WIDE_INT
9882                        ? HOST_WIDE_INT_M1U
9883                        : (HOST_WIDE_INT_1U << len) - 1,
9884                        0);
9885
9886   /* If SRC is masked by an AND that does not make a difference in
9887      the value being stored, strip it.  */
9888   if (GET_CODE (assign) == ZERO_EXTRACT
9889       && CONST_INT_P (XEXP (assign, 1))
9890       && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9891       && GET_CODE (src) == AND
9892       && CONST_INT_P (XEXP (src, 1))
9893       && UINTVAL (XEXP (src, 1))
9894          == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9895     src = XEXP (src, 0);
9896
9897   return gen_rtx_SET (assign, src);
9898 }
9899 \f
9900 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9901    if so.  */
9902
9903 static rtx
9904 apply_distributive_law (rtx x)
9905 {
9906   enum rtx_code code = GET_CODE (x);
9907   enum rtx_code inner_code;
9908   rtx lhs, rhs, other;
9909   rtx tem;
9910
9911   /* Distributivity is not true for floating point as it can change the
9912      value.  So we don't do it unless -funsafe-math-optimizations.  */
9913   if (FLOAT_MODE_P (GET_MODE (x))
9914       && ! flag_unsafe_math_optimizations)
9915     return x;
9916
9917   /* The outer operation can only be one of the following:  */
9918   if (code != IOR && code != AND && code != XOR
9919       && code != PLUS && code != MINUS)
9920     return x;
9921
9922   lhs = XEXP (x, 0);
9923   rhs = XEXP (x, 1);
9924
9925   /* If either operand is a primitive we can't do anything, so get out
9926      fast.  */
9927   if (OBJECT_P (lhs) || OBJECT_P (rhs))
9928     return x;
9929
9930   lhs = expand_compound_operation (lhs);
9931   rhs = expand_compound_operation (rhs);
9932   inner_code = GET_CODE (lhs);
9933   if (inner_code != GET_CODE (rhs))
9934     return x;
9935
9936   /* See if the inner and outer operations distribute.  */
9937   switch (inner_code)
9938     {
9939     case LSHIFTRT:
9940     case ASHIFTRT:
9941     case AND:
9942     case IOR:
9943       /* These all distribute except over PLUS.  */
9944       if (code == PLUS || code == MINUS)
9945         return x;
9946       break;
9947
9948     case MULT:
9949       if (code != PLUS && code != MINUS)
9950         return x;
9951       break;
9952
9953     case ASHIFT:
9954       /* This is also a multiply, so it distributes over everything.  */
9955       break;
9956
9957     /* This used to handle SUBREG, but this turned out to be counter-
9958        productive, since (subreg (op ...)) usually is not handled by
9959        insn patterns, and this "optimization" therefore transformed
9960        recognizable patterns into unrecognizable ones.  Therefore the
9961        SUBREG case was removed from here.
9962
9963        It is possible that distributing SUBREG over arithmetic operations
9964        leads to an intermediate result than can then be optimized further,
9965        e.g. by moving the outer SUBREG to the other side of a SET as done
9966        in simplify_set.  This seems to have been the original intent of
9967        handling SUBREGs here.
9968
9969        However, with current GCC this does not appear to actually happen,
9970        at least on major platforms.  If some case is found where removing
9971        the SUBREG case here prevents follow-on optimizations, distributing
9972        SUBREGs ought to be re-added at that place, e.g. in simplify_set.  */
9973
9974     default:
9975       return x;
9976     }
9977
9978   /* Set LHS and RHS to the inner operands (A and B in the example
9979      above) and set OTHER to the common operand (C in the example).
9980      There is only one way to do this unless the inner operation is
9981      commutative.  */
9982   if (COMMUTATIVE_ARITH_P (lhs)
9983       && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9984     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9985   else if (COMMUTATIVE_ARITH_P (lhs)
9986            && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9987     other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9988   else if (COMMUTATIVE_ARITH_P (lhs)
9989            && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9990     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9991   else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9992     other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9993   else
9994     return x;
9995
9996   /* Form the new inner operation, seeing if it simplifies first.  */
9997   tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9998
9999   /* There is one exception to the general way of distributing:
10000      (a | c) ^ (b | c) -> (a ^ b) & ~c  */
10001   if (code == XOR && inner_code == IOR)
10002     {
10003       inner_code = AND;
10004       other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
10005     }
10006
10007   /* We may be able to continuing distributing the result, so call
10008      ourselves recursively on the inner operation before forming the
10009      outer operation, which we return.  */
10010   return simplify_gen_binary (inner_code, GET_MODE (x),
10011                               apply_distributive_law (tem), other);
10012 }
10013
10014 /* See if X is of the form (* (+ A B) C), and if so convert to
10015    (+ (* A C) (* B C)) and try to simplify.
10016
10017    Most of the time, this results in no change.  However, if some of
10018    the operands are the same or inverses of each other, simplifications
10019    will result.
10020
10021    For example, (and (ior A B) (not B)) can occur as the result of
10022    expanding a bit field assignment.  When we apply the distributive
10023    law to this, we get (ior (and (A (not B))) (and (B (not B)))),
10024    which then simplifies to (and (A (not B))).
10025
10026    Note that no checks happen on the validity of applying the inverse
10027    distributive law.  This is pointless since we can do it in the
10028    few places where this routine is called.
10029
10030    N is the index of the term that is decomposed (the arithmetic operation,
10031    i.e. (+ A B) in the first example above).  !N is the index of the term that
10032    is distributed, i.e. of C in the first example above.  */
10033 static rtx
10034 distribute_and_simplify_rtx (rtx x, int n)
10035 {
10036   machine_mode mode;
10037   enum rtx_code outer_code, inner_code;
10038   rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
10039
10040   /* Distributivity is not true for floating point as it can change the
10041      value.  So we don't do it unless -funsafe-math-optimizations.  */
10042   if (FLOAT_MODE_P (GET_MODE (x))
10043       && ! flag_unsafe_math_optimizations)
10044     return NULL_RTX;
10045
10046   decomposed = XEXP (x, n);
10047   if (!ARITHMETIC_P (decomposed))
10048     return NULL_RTX;
10049
10050   mode = GET_MODE (x);
10051   outer_code = GET_CODE (x);
10052   distributed = XEXP (x, !n);
10053
10054   inner_code = GET_CODE (decomposed);
10055   inner_op0 = XEXP (decomposed, 0);
10056   inner_op1 = XEXP (decomposed, 1);
10057
10058   /* Special case (and (xor B C) (not A)), which is equivalent to
10059      (xor (ior A B) (ior A C))  */
10060   if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
10061     {
10062       distributed = XEXP (distributed, 0);
10063       outer_code = IOR;
10064     }
10065
10066   if (n == 0)
10067     {
10068       /* Distribute the second term.  */
10069       new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
10070       new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
10071     }
10072   else
10073     {
10074       /* Distribute the first term.  */
10075       new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
10076       new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
10077     }
10078
10079   tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
10080                                                      new_op0, new_op1));
10081   if (GET_CODE (tmp) != outer_code
10082       && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
10083           < set_src_cost (x, mode, optimize_this_for_speed_p)))
10084     return tmp;
10085
10086   return NULL_RTX;
10087 }
10088 \f
10089 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
10090    in MODE.  Return an equivalent form, if different from (and VAROP
10091    (const_int CONSTOP)).  Otherwise, return NULL_RTX.  */
10092
10093 static rtx
10094 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10095                           unsigned HOST_WIDE_INT constop)
10096 {
10097   unsigned HOST_WIDE_INT nonzero;
10098   unsigned HOST_WIDE_INT orig_constop;
10099   rtx orig_varop;
10100   int i;
10101
10102   orig_varop = varop;
10103   orig_constop = constop;
10104   if (GET_CODE (varop) == CLOBBER)
10105     return NULL_RTX;
10106
10107   /* Simplify VAROP knowing that we will be only looking at some of the
10108      bits in it.
10109
10110      Note by passing in CONSTOP, we guarantee that the bits not set in
10111      CONSTOP are not significant and will never be examined.  We must
10112      ensure that is the case by explicitly masking out those bits
10113      before returning.  */
10114   varop = force_to_mode (varop, mode, constop, 0);
10115
10116   /* If VAROP is a CLOBBER, we will fail so return it.  */
10117   if (GET_CODE (varop) == CLOBBER)
10118     return varop;
10119
10120   /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
10121      to VAROP and return the new constant.  */
10122   if (CONST_INT_P (varop))
10123     return gen_int_mode (INTVAL (varop) & constop, mode);
10124
10125   /* See what bits may be nonzero in VAROP.  Unlike the general case of
10126      a call to nonzero_bits, here we don't care about bits outside
10127      MODE.  */
10128
10129   nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
10130
10131   /* Turn off all bits in the constant that are known to already be zero.
10132      Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
10133      which is tested below.  */
10134
10135   constop &= nonzero;
10136
10137   /* If we don't have any bits left, return zero.  */
10138   if (constop == 0)
10139     return const0_rtx;
10140
10141   /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10142      a power of two, we can replace this with an ASHIFT.  */
10143   if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
10144       && (i = exact_log2 (constop)) >= 0)
10145     return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10146
10147   /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10148      or XOR, then try to apply the distributive law.  This may eliminate
10149      operations if either branch can be simplified because of the AND.
10150      It may also make some cases more complex, but those cases probably
10151      won't match a pattern either with or without this.  */
10152
10153   if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10154     {
10155       scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10156       return
10157         gen_lowpart
10158           (mode,
10159            apply_distributive_law
10160            (simplify_gen_binary (GET_CODE (varop), varop_mode,
10161                                  simplify_and_const_int (NULL_RTX, varop_mode,
10162                                                          XEXP (varop, 0),
10163                                                          constop),
10164                                  simplify_and_const_int (NULL_RTX, varop_mode,
10165                                                          XEXP (varop, 1),
10166                                                          constop))));
10167     }
10168
10169   /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10170      the AND and see if one of the operands simplifies to zero.  If so, we
10171      may eliminate it.  */
10172
10173   if (GET_CODE (varop) == PLUS
10174       && pow2p_hwi (constop + 1))
10175     {
10176       rtx o0, o1;
10177
10178       o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10179       o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10180       if (o0 == const0_rtx)
10181         return o1;
10182       if (o1 == const0_rtx)
10183         return o0;
10184     }
10185
10186   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
10187   varop = gen_lowpart (mode, varop);
10188   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10189     return NULL_RTX;
10190
10191   /* If we are only masking insignificant bits, return VAROP.  */
10192   if (constop == nonzero)
10193     return varop;
10194
10195   if (varop == orig_varop && constop == orig_constop)
10196     return NULL_RTX;
10197
10198   /* Otherwise, return an AND.  */
10199   return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10200 }
10201
10202
10203 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10204    in MODE.
10205
10206    Return an equivalent form, if different from X.  Otherwise, return X.  If
10207    X is zero, we are to always construct the equivalent form.  */
10208
10209 static rtx
10210 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10211                         unsigned HOST_WIDE_INT constop)
10212 {
10213   rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10214   if (tem)
10215     return tem;
10216
10217   if (!x)
10218     x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10219                              gen_int_mode (constop, mode));
10220   if (GET_MODE (x) != mode)
10221     x = gen_lowpart (mode, x);
10222   return x;
10223 }
10224 \f
10225 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10226    We don't care about bits outside of those defined in MODE.
10227
10228    For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10229    a shift, AND, or zero_extract, we can do better.  */
10230
10231 static rtx
10232 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10233                               scalar_int_mode mode,
10234                               unsigned HOST_WIDE_INT *nonzero)
10235 {
10236   rtx tem;
10237   reg_stat_type *rsp;
10238
10239   /* If X is a register whose nonzero bits value is current, use it.
10240      Otherwise, if X is a register whose value we can find, use that
10241      value.  Otherwise, use the previously-computed global nonzero bits
10242      for this register.  */
10243
10244   rsp = &reg_stat[REGNO (x)];
10245   if (rsp->last_set_value != 0
10246       && (rsp->last_set_mode == mode
10247           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10248               && GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10249               && GET_MODE_CLASS (mode) == MODE_INT))
10250       && ((rsp->last_set_label >= label_tick_ebb_start
10251            && rsp->last_set_label < label_tick)
10252           || (rsp->last_set_label == label_tick
10253               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10254           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10255               && REGNO (x) < reg_n_sets_max
10256               && REG_N_SETS (REGNO (x)) == 1
10257               && !REGNO_REG_SET_P
10258                   (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10259                    REGNO (x)))))
10260     {
10261       /* Note that, even if the precision of last_set_mode is lower than that
10262          of mode, record_value_for_reg invoked nonzero_bits on the register
10263          with nonzero_bits_mode (because last_set_mode is necessarily integral
10264          and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10265          are all valid, hence in mode too since nonzero_bits_mode is defined
10266          to the largest HWI_COMPUTABLE_MODE_P mode.  */
10267       *nonzero &= rsp->last_set_nonzero_bits;
10268       return NULL;
10269     }
10270
10271   tem = get_last_value (x);
10272   if (tem)
10273     {
10274       if (SHORT_IMMEDIATES_SIGN_EXTEND)
10275         tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10276
10277       return tem;
10278     }
10279
10280   if (nonzero_sign_valid && rsp->nonzero_bits)
10281     {
10282       unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10283
10284       if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10285         /* We don't know anything about the upper bits.  */
10286         mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10287
10288       *nonzero &= mask;
10289     }
10290
10291   return NULL;
10292 }
10293
10294 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10295    end of X that are known to be equal to the sign bit.  X will be used
10296    in mode MODE; the returned value will always be between 1 and the
10297    number of bits in MODE.  */
10298
10299 static rtx
10300 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10301                                      scalar_int_mode mode,
10302                                      unsigned int *result)
10303 {
10304   rtx tem;
10305   reg_stat_type *rsp;
10306
10307   rsp = &reg_stat[REGNO (x)];
10308   if (rsp->last_set_value != 0
10309       && rsp->last_set_mode == mode
10310       && ((rsp->last_set_label >= label_tick_ebb_start
10311            && rsp->last_set_label < label_tick)
10312           || (rsp->last_set_label == label_tick
10313               && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10314           || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10315               && REGNO (x) < reg_n_sets_max
10316               && REG_N_SETS (REGNO (x)) == 1
10317               && !REGNO_REG_SET_P
10318                   (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10319                    REGNO (x)))))
10320     {
10321       *result = rsp->last_set_sign_bit_copies;
10322       return NULL;
10323     }
10324
10325   tem = get_last_value (x);
10326   if (tem != 0)
10327     return tem;
10328
10329   if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10330       && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10331     *result = rsp->sign_bit_copies;
10332
10333   return NULL;
10334 }
10335 \f
10336 /* Return the number of "extended" bits there are in X, when interpreted
10337    as a quantity in MODE whose signedness is indicated by UNSIGNEDP.  For
10338    unsigned quantities, this is the number of high-order zero bits.
10339    For signed quantities, this is the number of copies of the sign bit
10340    minus 1.  In both case, this function returns the number of "spare"
10341    bits.  For example, if two quantities for which this function returns
10342    at least 1 are added, the addition is known not to overflow.
10343
10344    This function will always return 0 unless called during combine, which
10345    implies that it must be called from a define_split.  */
10346
10347 unsigned int
10348 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10349 {
10350   if (nonzero_sign_valid == 0)
10351     return 0;
10352
10353   scalar_int_mode int_mode;
10354   return (unsignedp
10355           ? (is_a <scalar_int_mode> (mode, &int_mode)
10356              && HWI_COMPUTABLE_MODE_P (int_mode)
10357              ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10358                                - floor_log2 (nonzero_bits (x, int_mode)))
10359              : 0)
10360           : num_sign_bit_copies (x, mode) - 1);
10361 }
10362
10363 /* This function is called from `simplify_shift_const' to merge two
10364    outer operations.  Specifically, we have already found that we need
10365    to perform operation *POP0 with constant *PCONST0 at the outermost
10366    position.  We would now like to also perform OP1 with constant CONST1
10367    (with *POP0 being done last).
10368
10369    Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10370    the resulting operation.  *PCOMP_P is set to 1 if we would need to
10371    complement the innermost operand, otherwise it is unchanged.
10372
10373    MODE is the mode in which the operation will be done.  No bits outside
10374    the width of this mode matter.  It is assumed that the width of this mode
10375    is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10376
10377    If *POP0 or OP1 are UNKNOWN, it means no operation is required.  Only NEG, PLUS,
10378    IOR, XOR, and AND are supported.  We may set *POP0 to SET if the proper
10379    result is simply *PCONST0.
10380
10381    If the resulting operation cannot be expressed as one operation, we
10382    return 0 and do not change *POP0, *PCONST0, and *PCOMP_P.  */
10383
10384 static int
10385 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)
10386 {
10387   enum rtx_code op0 = *pop0;
10388   HOST_WIDE_INT const0 = *pconst0;
10389
10390   const0 &= GET_MODE_MASK (mode);
10391   const1 &= GET_MODE_MASK (mode);
10392
10393   /* If OP0 is an AND, clear unimportant bits in CONST1.  */
10394   if (op0 == AND)
10395     const1 &= const0;
10396
10397   /* If OP0 or OP1 is UNKNOWN, this is easy.  Similarly if they are the same or
10398      if OP0 is SET.  */
10399
10400   if (op1 == UNKNOWN || op0 == SET)
10401     return 1;
10402
10403   else if (op0 == UNKNOWN)
10404     op0 = op1, const0 = const1;
10405
10406   else if (op0 == op1)
10407     {
10408       switch (op0)
10409         {
10410         case AND:
10411           const0 &= const1;
10412           break;
10413         case IOR:
10414           const0 |= const1;
10415           break;
10416         case XOR:
10417           const0 ^= const1;
10418           break;
10419         case PLUS:
10420           const0 += const1;
10421           break;
10422         case NEG:
10423           op0 = UNKNOWN;
10424           break;
10425         default:
10426           break;
10427         }
10428     }
10429
10430   /* Otherwise, if either is a PLUS or NEG, we can't do anything.  */
10431   else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10432     return 0;
10433
10434   /* If the two constants aren't the same, we can't do anything.  The
10435      remaining six cases can all be done.  */
10436   else if (const0 != const1)
10437     return 0;
10438
10439   else
10440     switch (op0)
10441       {
10442       case IOR:
10443         if (op1 == AND)
10444           /* (a & b) | b == b */
10445           op0 = SET;
10446         else /* op1 == XOR */
10447           /* (a ^ b) | b == a | b */
10448           {;}
10449         break;
10450
10451       case XOR:
10452         if (op1 == AND)
10453           /* (a & b) ^ b == (~a) & b */
10454           op0 = AND, *pcomp_p = 1;
10455         else /* op1 == IOR */
10456           /* (a | b) ^ b == a & ~b */
10457           op0 = AND, const0 = ~const0;
10458         break;
10459
10460       case AND:
10461         if (op1 == IOR)
10462           /* (a | b) & b == b */
10463         op0 = SET;
10464         else /* op1 == XOR */
10465           /* (a ^ b) & b) == (~a) & b */
10466           *pcomp_p = 1;
10467         break;
10468       default:
10469         break;
10470       }
10471
10472   /* Check for NO-OP cases.  */
10473   const0 &= GET_MODE_MASK (mode);
10474   if (const0 == 0
10475       && (op0 == IOR || op0 == XOR || op0 == PLUS))
10476     op0 = UNKNOWN;
10477   else if (const0 == 0 && op0 == AND)
10478     op0 = SET;
10479   else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10480            && op0 == AND)
10481     op0 = UNKNOWN;
10482
10483   *pop0 = op0;
10484
10485   /* ??? Slightly redundant with the above mask, but not entirely.
10486      Moving this above means we'd have to sign-extend the mode mask
10487      for the final test.  */
10488   if (op0 != UNKNOWN && op0 != NEG)
10489     *pconst0 = trunc_int_for_mode (const0, mode);
10490
10491   return 1;
10492 }
10493 \f
10494 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10495    the shift in.  The original shift operation CODE is performed on OP in
10496    ORIG_MODE.  Return the wider mode MODE if we can perform the operation
10497    in that mode.  Return ORIG_MODE otherwise.  We can also assume that the
10498    result of the shift is subject to operation OUTER_CODE with operand
10499    OUTER_CONST.  */
10500
10501 static scalar_int_mode
10502 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10503                       scalar_int_mode orig_mode, scalar_int_mode mode,
10504                       enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10505 {
10506   gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10507
10508   /* In general we can't perform in wider mode for right shift and rotate.  */
10509   switch (code)
10510     {
10511     case ASHIFTRT:
10512       /* We can still widen if the bits brought in from the left are identical
10513          to the sign bit of ORIG_MODE.  */
10514       if (num_sign_bit_copies (op, mode)
10515           > (unsigned) (GET_MODE_PRECISION (mode)
10516                         - GET_MODE_PRECISION (orig_mode)))
10517         return mode;
10518       return orig_mode;
10519
10520     case LSHIFTRT:
10521       /* Similarly here but with zero bits.  */
10522       if (HWI_COMPUTABLE_MODE_P (mode)
10523           && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10524         return mode;
10525
10526       /* We can also widen if the bits brought in will be masked off.  This
10527          operation is performed in ORIG_MODE.  */
10528       if (outer_code == AND)
10529         {
10530           int care_bits = low_bitmask_len (orig_mode, outer_const);
10531
10532           if (care_bits >= 0
10533               && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10534             return mode;
10535         }
10536       /* fall through */
10537
10538     case ROTATE:
10539       return orig_mode;
10540
10541     case ROTATERT:
10542       gcc_unreachable ();
10543
10544     default:
10545       return mode;
10546     }
10547 }
10548
10549 /* Simplify a shift of VAROP by ORIG_COUNT bits.  CODE says what kind
10550    of shift.  The result of the shift is RESULT_MODE.  Return NULL_RTX
10551    if we cannot simplify it.  Otherwise, return a simplified value.
10552
10553    The shift is normally computed in the widest mode we find in VAROP, as
10554    long as it isn't a different number of words than RESULT_MODE.  Exceptions
10555    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
10556
10557 static rtx
10558 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10559                         rtx varop, int orig_count)
10560 {
10561   enum rtx_code orig_code = code;
10562   rtx orig_varop = varop;
10563   int count, log2;
10564   machine_mode mode = result_mode;
10565   machine_mode shift_mode;
10566   scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10567   /* We form (outer_op (code varop count) (outer_const)).  */
10568   enum rtx_code outer_op = UNKNOWN;
10569   HOST_WIDE_INT outer_const = 0;
10570   int complement_p = 0;
10571   rtx new_rtx, x;
10572
10573   /* Make sure and truncate the "natural" shift on the way in.  We don't
10574      want to do this inside the loop as it makes it more difficult to
10575      combine shifts.  */
10576   if (SHIFT_COUNT_TRUNCATED)
10577     orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10578
10579   /* If we were given an invalid count, don't do anything except exactly
10580      what was requested.  */
10581
10582   if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10583     return NULL_RTX;
10584
10585   count = orig_count;
10586
10587   /* Unless one of the branches of the `if' in this loop does a `continue',
10588      we will `break' the loop after the `if'.  */
10589
10590   while (count != 0)
10591     {
10592       /* If we have an operand of (clobber (const_int 0)), fail.  */
10593       if (GET_CODE (varop) == CLOBBER)
10594         return NULL_RTX;
10595
10596       /* Convert ROTATERT to ROTATE.  */
10597       if (code == ROTATERT)
10598         {
10599           unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10600           code = ROTATE;
10601           count = bitsize - count;
10602         }
10603
10604       shift_mode = result_mode;
10605       if (shift_mode != mode)
10606         {
10607           /* We only change the modes of scalar shifts.  */
10608           int_mode = as_a <scalar_int_mode> (mode);
10609           int_result_mode = as_a <scalar_int_mode> (result_mode);
10610           shift_mode = try_widen_shift_mode (code, varop, count,
10611                                              int_result_mode, int_mode,
10612                                              outer_op, outer_const);
10613         }
10614
10615       scalar_int_mode shift_unit_mode
10616         = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10617
10618       /* Handle cases where the count is greater than the size of the mode
10619          minus 1.  For ASHIFT, use the size minus one as the count (this can
10620          occur when simplifying (lshiftrt (ashiftrt ..))).  For rotates,
10621          take the count modulo the size.  For other shifts, the result is
10622          zero.
10623
10624          Since these shifts are being produced by the compiler by combining
10625          multiple operations, each of which are defined, we know what the
10626          result is supposed to be.  */
10627
10628       if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10629         {
10630           if (code == ASHIFTRT)
10631             count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10632           else if (code == ROTATE || code == ROTATERT)
10633             count %= GET_MODE_PRECISION (shift_unit_mode);
10634           else
10635             {
10636               /* We can't simply return zero because there may be an
10637                  outer op.  */
10638               varop = const0_rtx;
10639               count = 0;
10640               break;
10641             }
10642         }
10643
10644       /* If we discovered we had to complement VAROP, leave.  Making a NOT
10645          here would cause an infinite loop.  */
10646       if (complement_p)
10647         break;
10648
10649       if (shift_mode == shift_unit_mode)
10650         {
10651           /* An arithmetic right shift of a quantity known to be -1 or 0
10652              is a no-op.  */
10653           if (code == ASHIFTRT
10654               && (num_sign_bit_copies (varop, shift_unit_mode)
10655                   == GET_MODE_PRECISION (shift_unit_mode)))
10656             {
10657               count = 0;
10658               break;
10659             }
10660
10661           /* If we are doing an arithmetic right shift and discarding all but
10662              the sign bit copies, this is equivalent to doing a shift by the
10663              bitsize minus one.  Convert it into that shift because it will
10664              often allow other simplifications.  */
10665
10666           if (code == ASHIFTRT
10667               && (count + num_sign_bit_copies (varop, shift_unit_mode)
10668                   >= GET_MODE_PRECISION (shift_unit_mode)))
10669             count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10670
10671           /* We simplify the tests below and elsewhere by converting
10672              ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10673              `make_compound_operation' will convert it to an ASHIFTRT for
10674              those machines (such as VAX) that don't have an LSHIFTRT.  */
10675           if (code == ASHIFTRT
10676               && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10677               && val_signbit_known_clear_p (shift_unit_mode,
10678                                             nonzero_bits (varop,
10679                                                           shift_unit_mode)))
10680             code = LSHIFTRT;
10681
10682           if (((code == LSHIFTRT
10683                 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10684                 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10685                || (code == ASHIFT
10686                    && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10687                    && !((nonzero_bits (varop, shift_unit_mode) << count)
10688                         & GET_MODE_MASK (shift_unit_mode))))
10689               && !side_effects_p (varop))
10690             varop = const0_rtx;
10691         }
10692
10693       switch (GET_CODE (varop))
10694         {
10695         case SIGN_EXTEND:
10696         case ZERO_EXTEND:
10697         case SIGN_EXTRACT:
10698         case ZERO_EXTRACT:
10699           new_rtx = expand_compound_operation (varop);
10700           if (new_rtx != varop)
10701             {
10702               varop = new_rtx;
10703               continue;
10704             }
10705           break;
10706
10707         case MEM:
10708           /* The following rules apply only to scalars.  */
10709           if (shift_mode != shift_unit_mode)
10710             break;
10711           int_mode = as_a <scalar_int_mode> (mode);
10712
10713           /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10714              minus the width of a smaller mode, we can do this with a
10715              SIGN_EXTEND or ZERO_EXTEND from the narrower memory location.  */
10716           if ((code == ASHIFTRT || code == LSHIFTRT)
10717               && ! mode_dependent_address_p (XEXP (varop, 0),
10718                                              MEM_ADDR_SPACE (varop))
10719               && ! MEM_VOLATILE_P (varop)
10720               && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10721                   .exists (&tmode)))
10722             {
10723               new_rtx = adjust_address_nv (varop, tmode,
10724                                            BYTES_BIG_ENDIAN ? 0
10725                                            : count / BITS_PER_UNIT);
10726
10727               varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10728                                      : ZERO_EXTEND, int_mode, new_rtx);
10729               count = 0;
10730               continue;
10731             }
10732           break;
10733
10734         case SUBREG:
10735           /* The following rules apply only to scalars.  */
10736           if (shift_mode != shift_unit_mode)
10737             break;
10738           int_mode = as_a <scalar_int_mode> (mode);
10739           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10740
10741           /* If VAROP is a SUBREG, strip it as long as the inner operand has
10742              the same number of words as what we've seen so far.  Then store
10743              the widest mode in MODE.  */
10744           if (subreg_lowpart_p (varop)
10745               && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10746               && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10747               && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10748                   == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10749               && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10750             {
10751               varop = SUBREG_REG (varop);
10752               if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10753                 mode = inner_mode;
10754               continue;
10755             }
10756           break;
10757
10758         case MULT:
10759           /* Some machines use MULT instead of ASHIFT because MULT
10760              is cheaper.  But it is still better on those machines to
10761              merge two shifts into one.  */
10762           if (CONST_INT_P (XEXP (varop, 1))
10763               && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10764             {
10765               rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10766               varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10767                                            XEXP (varop, 0), log2_rtx);
10768               continue;
10769             }
10770           break;
10771
10772         case UDIV:
10773           /* Similar, for when divides are cheaper.  */
10774           if (CONST_INT_P (XEXP (varop, 1))
10775               && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10776             {
10777               rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10778               varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10779                                            XEXP (varop, 0), log2_rtx);
10780               continue;
10781             }
10782           break;
10783
10784         case ASHIFTRT:
10785           /* If we are extracting just the sign bit of an arithmetic
10786              right shift, that shift is not needed.  However, the sign
10787              bit of a wider mode may be different from what would be
10788              interpreted as the sign bit in a narrower mode, so, if
10789              the result is narrower, don't discard the shift.  */
10790           if (code == LSHIFTRT
10791               && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10792               && (GET_MODE_UNIT_BITSIZE (result_mode)
10793                   >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10794             {
10795               varop = XEXP (varop, 0);
10796               continue;
10797             }
10798
10799           /* fall through */
10800
10801         case LSHIFTRT:
10802         case ASHIFT:
10803         case ROTATE:
10804           /* The following rules apply only to scalars.  */
10805           if (shift_mode != shift_unit_mode)
10806             break;
10807           int_mode = as_a <scalar_int_mode> (mode);
10808           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10809           int_result_mode = as_a <scalar_int_mode> (result_mode);
10810
10811           /* Here we have two nested shifts.  The result is usually the
10812              AND of a new shift with a mask.  We compute the result below.  */
10813           if (CONST_INT_P (XEXP (varop, 1))
10814               && INTVAL (XEXP (varop, 1)) >= 0
10815               && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10816               && HWI_COMPUTABLE_MODE_P (int_result_mode)
10817               && HWI_COMPUTABLE_MODE_P (int_mode))
10818             {
10819               enum rtx_code first_code = GET_CODE (varop);
10820               unsigned int first_count = INTVAL (XEXP (varop, 1));
10821               unsigned HOST_WIDE_INT mask;
10822               rtx mask_rtx;
10823
10824               /* We have one common special case.  We can't do any merging if
10825                  the inner code is an ASHIFTRT of a smaller mode.  However, if
10826                  we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10827                  with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10828                  we can convert it to
10829                  (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10830                  This simplifies certain SIGN_EXTEND operations.  */
10831               if (code == ASHIFT && first_code == ASHIFTRT
10832                   && count == (GET_MODE_PRECISION (int_result_mode)
10833                                - GET_MODE_PRECISION (int_varop_mode)))
10834                 {
10835                   /* C3 has the low-order C1 bits zero.  */
10836
10837                   mask = GET_MODE_MASK (int_mode)
10838                          & ~((HOST_WIDE_INT_1U << first_count) - 1);
10839
10840                   varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10841                                                   XEXP (varop, 0), mask);
10842                   varop = simplify_shift_const (NULL_RTX, ASHIFT,
10843                                                 int_result_mode, varop, count);
10844                   count = first_count;
10845                   code = ASHIFTRT;
10846                   continue;
10847                 }
10848
10849               /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10850                  than C1 high-order bits equal to the sign bit, we can convert
10851                  this to either an ASHIFT or an ASHIFTRT depending on the
10852                  two counts.
10853
10854                  We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE.  */
10855
10856               if (code == ASHIFTRT && first_code == ASHIFT
10857                   && int_varop_mode == shift_unit_mode
10858                   && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10859                       > first_count))
10860                 {
10861                   varop = XEXP (varop, 0);
10862                   count -= first_count;
10863                   if (count < 0)
10864                     {
10865                       count = -count;
10866                       code = ASHIFT;
10867                     }
10868
10869                   continue;
10870                 }
10871
10872               /* There are some cases we can't do.  If CODE is ASHIFTRT,
10873                  we can only do this if FIRST_CODE is also ASHIFTRT.
10874
10875                  We can't do the case when CODE is ROTATE and FIRST_CODE is
10876                  ASHIFTRT.
10877
10878                  If the mode of this shift is not the mode of the outer shift,
10879                  we can't do this if either shift is a right shift or ROTATE.
10880
10881                  Finally, we can't do any of these if the mode is too wide
10882                  unless the codes are the same.
10883
10884                  Handle the case where the shift codes are the same
10885                  first.  */
10886
10887               if (code == first_code)
10888                 {
10889                   if (int_varop_mode != int_result_mode
10890                       && (code == ASHIFTRT || code == LSHIFTRT
10891                           || code == ROTATE))
10892                     break;
10893
10894                   count += first_count;
10895                   varop = XEXP (varop, 0);
10896                   continue;
10897                 }
10898
10899               if (code == ASHIFTRT
10900                   || (code == ROTATE && first_code == ASHIFTRT)
10901                   || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10902                   || (int_varop_mode != int_result_mode
10903                       && (first_code == ASHIFTRT || first_code == LSHIFTRT
10904                           || first_code == ROTATE
10905                           || code == ROTATE)))
10906                 break;
10907
10908               /* To compute the mask to apply after the shift, shift the
10909                  nonzero bits of the inner shift the same way the
10910                  outer shift will.  */
10911
10912               mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10913                                        int_result_mode);
10914               rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10915               mask_rtx
10916                 = simplify_const_binary_operation (code, int_result_mode,
10917                                                    mask_rtx, count_rtx);
10918
10919               /* Give up if we can't compute an outer operation to use.  */
10920               if (mask_rtx == 0
10921                   || !CONST_INT_P (mask_rtx)
10922                   || ! merge_outer_ops (&outer_op, &outer_const, AND,
10923                                         INTVAL (mask_rtx),
10924                                         int_result_mode, &complement_p))
10925                 break;
10926
10927               /* If the shifts are in the same direction, we add the
10928                  counts.  Otherwise, we subtract them.  */
10929               if ((code == ASHIFTRT || code == LSHIFTRT)
10930                   == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10931                 count += first_count;
10932               else
10933                 count -= first_count;
10934
10935               /* If COUNT is positive, the new shift is usually CODE,
10936                  except for the two exceptions below, in which case it is
10937                  FIRST_CODE.  If the count is negative, FIRST_CODE should
10938                  always be used  */
10939               if (count > 0
10940                   && ((first_code == ROTATE && code == ASHIFT)
10941                       || (first_code == ASHIFTRT && code == LSHIFTRT)))
10942                 code = first_code;
10943               else if (count < 0)
10944                 code = first_code, count = -count;
10945
10946               varop = XEXP (varop, 0);
10947               continue;
10948             }
10949
10950           /* If we have (A << B << C) for any shift, we can convert this to
10951              (A << C << B).  This wins if A is a constant.  Only try this if
10952              B is not a constant.  */
10953
10954           else if (GET_CODE (varop) == code
10955                    && CONST_INT_P (XEXP (varop, 0))
10956                    && !CONST_INT_P (XEXP (varop, 1)))
10957             {
10958               /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10959                  sure the result will be masked.  See PR70222.  */
10960               if (code == LSHIFTRT
10961                   && int_mode != int_result_mode
10962                   && !merge_outer_ops (&outer_op, &outer_const, AND,
10963                                        GET_MODE_MASK (int_result_mode)
10964                                        >> orig_count, int_result_mode,
10965                                        &complement_p))
10966                 break;
10967               /* For ((int) (cstLL >> count)) >> cst2 just give up.  Queuing
10968                  up outer sign extension (often left and right shift) is
10969                  hardly more efficient than the original.  See PR70429.  */
10970               if (code == ASHIFTRT && int_mode != int_result_mode)
10971                 break;
10972
10973               rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10974               rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10975                                                              XEXP (varop, 0),
10976                                                              count_rtx);
10977               varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10978               count = 0;
10979               continue;
10980             }
10981           break;
10982
10983         case NOT:
10984           /* The following rules apply only to scalars.  */
10985           if (shift_mode != shift_unit_mode)
10986             break;
10987
10988           /* Make this fit the case below.  */
10989           varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10990           continue;
10991
10992         case IOR:
10993         case AND:
10994         case XOR:
10995           /* The following rules apply only to scalars.  */
10996           if (shift_mode != shift_unit_mode)
10997             break;
10998           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10999           int_result_mode = as_a <scalar_int_mode> (result_mode);
11000
11001           /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
11002              with C the size of VAROP - 1 and the shift is logical if
11003              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11004              we have an (le X 0) operation.   If we have an arithmetic shift
11005              and STORE_FLAG_VALUE is 1 or we have a logical shift with
11006              STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation.  */
11007
11008           if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
11009               && XEXP (XEXP (varop, 0), 1) == constm1_rtx
11010               && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11011               && (code == LSHIFTRT || code == ASHIFTRT)
11012               && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11013               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11014             {
11015               count = 0;
11016               varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
11017                                   const0_rtx);
11018
11019               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11020                 varop = gen_rtx_NEG (int_varop_mode, varop);
11021
11022               continue;
11023             }
11024
11025           /* If we have (shift (logical)), move the logical to the outside
11026              to allow it to possibly combine with another logical and the
11027              shift to combine with another shift.  This also canonicalizes to
11028              what a ZERO_EXTRACT looks like.  Also, some machines have
11029              (and (shift)) insns.  */
11030
11031           if (CONST_INT_P (XEXP (varop, 1))
11032               /* We can't do this if we have (ashiftrt (xor))  and the
11033                  constant has its sign bit set in shift_unit_mode with
11034                  shift_unit_mode wider than result_mode.  */
11035               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11036                    && int_result_mode != shift_unit_mode
11037                    && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11038                                           shift_unit_mode) < 0)
11039               && (new_rtx = simplify_const_binary_operation
11040                   (code, int_result_mode,
11041                    gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11042                    gen_int_shift_amount (int_result_mode, count))) != 0
11043               && CONST_INT_P (new_rtx)
11044               && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
11045                                   INTVAL (new_rtx), int_result_mode,
11046                                   &complement_p))
11047             {
11048               varop = XEXP (varop, 0);
11049               continue;
11050             }
11051
11052           /* If we can't do that, try to simplify the shift in each arm of the
11053              logical expression, make a new logical expression, and apply
11054              the inverse distributive law.  This also can't be done for
11055              (ashiftrt (xor)) where we've widened the shift and the constant
11056              changes the sign bit.  */
11057           if (CONST_INT_P (XEXP (varop, 1))
11058               && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11059                    && int_result_mode != shift_unit_mode
11060                    && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11061                                           shift_unit_mode) < 0))
11062             {
11063               rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11064                                               XEXP (varop, 0), count);
11065               rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11066                                               XEXP (varop, 1), count);
11067
11068               varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
11069                                            lhs, rhs);
11070               varop = apply_distributive_law (varop);
11071
11072               count = 0;
11073               continue;
11074             }
11075           break;
11076
11077         case EQ:
11078           /* The following rules apply only to scalars.  */
11079           if (shift_mode != shift_unit_mode)
11080             break;
11081           int_result_mode = as_a <scalar_int_mode> (result_mode);
11082
11083           /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
11084              says that the sign bit can be tested, FOO has mode MODE, C is
11085              GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11086              that may be nonzero.  */
11087           if (code == LSHIFTRT
11088               && XEXP (varop, 1) == const0_rtx
11089               && GET_MODE (XEXP (varop, 0)) == int_result_mode
11090               && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11091               && HWI_COMPUTABLE_MODE_P (int_result_mode)
11092               && STORE_FLAG_VALUE == -1
11093               && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11094               && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11095                                   int_result_mode, &complement_p))
11096             {
11097               varop = XEXP (varop, 0);
11098               count = 0;
11099               continue;
11100             }
11101           break;
11102
11103         case NEG:
11104           /* The following rules apply only to scalars.  */
11105           if (shift_mode != shift_unit_mode)
11106             break;
11107           int_result_mode = as_a <scalar_int_mode> (result_mode);
11108
11109           /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11110              than the number of bits in the mode is equivalent to A.  */
11111           if (code == LSHIFTRT
11112               && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11113               && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11114             {
11115               varop = XEXP (varop, 0);
11116               count = 0;
11117               continue;
11118             }
11119
11120           /* NEG commutes with ASHIFT since it is multiplication.  Move the
11121              NEG outside to allow shifts to combine.  */
11122           if (code == ASHIFT
11123               && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11124                                   int_result_mode, &complement_p))
11125             {
11126               varop = XEXP (varop, 0);
11127               continue;
11128             }
11129           break;
11130
11131         case PLUS:
11132           /* The following rules apply only to scalars.  */
11133           if (shift_mode != shift_unit_mode)
11134             break;
11135           int_result_mode = as_a <scalar_int_mode> (result_mode);
11136
11137           /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11138              is one less than the number of bits in the mode is
11139              equivalent to (xor A 1).  */
11140           if (code == LSHIFTRT
11141               && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11142               && XEXP (varop, 1) == constm1_rtx
11143               && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11144               && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11145                                   int_result_mode, &complement_p))
11146             {
11147               count = 0;
11148               varop = XEXP (varop, 0);
11149               continue;
11150             }
11151
11152           /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11153              that might be nonzero in BAR are those being shifted out and those
11154              bits are known zero in FOO, we can replace the PLUS with FOO.
11155              Similarly in the other operand order.  This code occurs when
11156              we are computing the size of a variable-size array.  */
11157
11158           if ((code == ASHIFTRT || code == LSHIFTRT)
11159               && count < HOST_BITS_PER_WIDE_INT
11160               && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11161               && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11162                   & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11163             {
11164               varop = XEXP (varop, 0);
11165               continue;
11166             }
11167           else if ((code == ASHIFTRT || code == LSHIFTRT)
11168                    && count < HOST_BITS_PER_WIDE_INT
11169                    && HWI_COMPUTABLE_MODE_P (int_result_mode)
11170                    && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11171                        >> count) == 0
11172                    && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11173                        & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11174             {
11175               varop = XEXP (varop, 1);
11176               continue;
11177             }
11178
11179           /* (ashift (plus foo C) N) is (plus (ashift foo N) C').  */
11180           if (code == ASHIFT
11181               && CONST_INT_P (XEXP (varop, 1))
11182               && (new_rtx = simplify_const_binary_operation
11183                   (ASHIFT, int_result_mode,
11184                    gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11185                    gen_int_shift_amount (int_result_mode, count))) != 0
11186               && CONST_INT_P (new_rtx)
11187               && merge_outer_ops (&outer_op, &outer_const, PLUS,
11188                                   INTVAL (new_rtx), int_result_mode,
11189                                   &complement_p))
11190             {
11191               varop = XEXP (varop, 0);
11192               continue;
11193             }
11194
11195           /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11196              signbit', and attempt to change the PLUS to an XOR and move it to
11197              the outer operation as is done above in the AND/IOR/XOR case
11198              leg for shift(logical). See details in logical handling above
11199              for reasoning in doing so.  */
11200           if (code == LSHIFTRT
11201               && CONST_INT_P (XEXP (varop, 1))
11202               && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11203               && (new_rtx = simplify_const_binary_operation
11204                   (code, int_result_mode,
11205                    gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11206                    gen_int_shift_amount (int_result_mode, count))) != 0
11207               && CONST_INT_P (new_rtx)
11208               && merge_outer_ops (&outer_op, &outer_const, XOR,
11209                                   INTVAL (new_rtx), int_result_mode,
11210                                   &complement_p))
11211             {
11212               varop = XEXP (varop, 0);
11213               continue;
11214             }
11215
11216           break;
11217
11218         case MINUS:
11219           /* The following rules apply only to scalars.  */
11220           if (shift_mode != shift_unit_mode)
11221             break;
11222           int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11223
11224           /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11225              with C the size of VAROP - 1 and the shift is logical if
11226              STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11227              we have a (gt X 0) operation.  If the shift is arithmetic with
11228              STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11229              we have a (neg (gt X 0)) operation.  */
11230
11231           if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11232               && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11233               && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11234               && (code == LSHIFTRT || code == ASHIFTRT)
11235               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11236               && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11237               && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11238             {
11239               count = 0;
11240               varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11241                                   const0_rtx);
11242
11243               if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11244                 varop = gen_rtx_NEG (int_varop_mode, varop);
11245
11246               continue;
11247             }
11248           break;
11249
11250         case TRUNCATE:
11251           /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11252              if the truncate does not affect the value.  */
11253           if (code == LSHIFTRT
11254               && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11255               && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11256               && (INTVAL (XEXP (XEXP (varop, 0), 1))
11257                   >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11258                       - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11259             {
11260               rtx varop_inner = XEXP (varop, 0);
11261               int new_count = count + INTVAL (XEXP (varop_inner, 1));
11262               rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11263                                                         new_count);
11264               varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11265                                               XEXP (varop_inner, 0),
11266                                               new_count_rtx);
11267               varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11268               count = 0;
11269               continue;
11270             }
11271           break;
11272
11273         default:
11274           break;
11275         }
11276
11277       break;
11278     }
11279
11280   shift_mode = result_mode;
11281   if (shift_mode != mode)
11282     {
11283       /* We only change the modes of scalar shifts.  */
11284       int_mode = as_a <scalar_int_mode> (mode);
11285       int_result_mode = as_a <scalar_int_mode> (result_mode);
11286       shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11287                                          int_mode, outer_op, outer_const);
11288     }
11289
11290   /* We have now finished analyzing the shift.  The result should be
11291      a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places.  If
11292      OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11293      to the result of the shift.  OUTER_CONST is the relevant constant,
11294      but we must turn off all bits turned off in the shift.  */
11295
11296   if (outer_op == UNKNOWN
11297       && orig_code == code && orig_count == count
11298       && varop == orig_varop
11299       && shift_mode == GET_MODE (varop))
11300     return NULL_RTX;
11301
11302   /* Make a SUBREG if necessary.  If we can't make it, fail.  */
11303   varop = gen_lowpart (shift_mode, varop);
11304   if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11305     return NULL_RTX;
11306
11307   /* If we have an outer operation and we just made a shift, it is
11308      possible that we could have simplified the shift were it not
11309      for the outer operation.  So try to do the simplification
11310      recursively.  */
11311
11312   if (outer_op != UNKNOWN)
11313     x = simplify_shift_const_1 (code, shift_mode, varop, count);
11314   else
11315     x = NULL_RTX;
11316
11317   if (x == NULL_RTX)
11318     x = simplify_gen_binary (code, shift_mode, varop,
11319                              gen_int_shift_amount (shift_mode, count));
11320
11321   /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11322      turn off all the bits that the shift would have turned off.  */
11323   if (orig_code == LSHIFTRT && result_mode != shift_mode)
11324     /* We only change the modes of scalar shifts.  */
11325     x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11326                                 x, GET_MODE_MASK (result_mode) >> orig_count);
11327
11328   /* Do the remainder of the processing in RESULT_MODE.  */
11329   x = gen_lowpart_or_truncate (result_mode, x);
11330
11331   /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11332      operation.  */
11333   if (complement_p)
11334     x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11335
11336   if (outer_op != UNKNOWN)
11337     {
11338       int_result_mode = as_a <scalar_int_mode> (result_mode);
11339
11340       if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11341           && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11342         outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11343
11344       if (outer_op == AND)
11345         x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11346       else if (outer_op == SET)
11347         {
11348           /* This means that we have determined that the result is
11349              equivalent to a constant.  This should be rare.  */
11350           if (!side_effects_p (x))
11351             x = GEN_INT (outer_const);
11352         }
11353       else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11354         x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11355       else
11356         x = simplify_gen_binary (outer_op, int_result_mode, x,
11357                                  GEN_INT (outer_const));
11358     }
11359
11360   return x;
11361 }
11362
11363 /* Simplify a shift of VAROP by COUNT bits.  CODE says what kind of shift.
11364    The result of the shift is RESULT_MODE.  If we cannot simplify it,
11365    return X or, if it is NULL, synthesize the expression with
11366    simplify_gen_binary.  Otherwise, return a simplified value.
11367
11368    The shift is normally computed in the widest mode we find in VAROP, as
11369    long as it isn't a different number of words than RESULT_MODE.  Exceptions
11370    are ASHIFTRT and ROTATE, which are always done in their original mode.  */
11371
11372 static rtx
11373 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11374                       rtx varop, int count)
11375 {
11376   rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11377   if (tem)
11378     return tem;
11379
11380   if (!x)
11381     x = simplify_gen_binary (code, GET_MODE (varop), varop,
11382                              gen_int_shift_amount (GET_MODE (varop), count));
11383   if (GET_MODE (x) != result_mode)
11384     x = gen_lowpart (result_mode, x);
11385   return x;
11386 }
11387
11388 \f
11389 /* A subroutine of recog_for_combine.  See there for arguments and
11390    return value.  */
11391
11392 static int
11393 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11394 {
11395   rtx pat = *pnewpat;
11396   rtx pat_without_clobbers;
11397   int insn_code_number;
11398   int num_clobbers_to_add = 0;
11399   int i;
11400   rtx notes = NULL_RTX;
11401   rtx old_notes, old_pat;
11402   int old_icode;
11403
11404   /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11405      we use to indicate that something didn't match.  If we find such a
11406      thing, force rejection.  */
11407   if (GET_CODE (pat) == PARALLEL)
11408     for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11409       if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11410           && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11411         return -1;
11412
11413   old_pat = PATTERN (insn);
11414   old_notes = REG_NOTES (insn);
11415   PATTERN (insn) = pat;
11416   REG_NOTES (insn) = NULL_RTX;
11417
11418   insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11419   if (dump_file && (dump_flags & TDF_DETAILS))
11420     {
11421       if (insn_code_number < 0)
11422         fputs ("Failed to match this instruction:\n", dump_file);
11423       else
11424         fputs ("Successfully matched this instruction:\n", dump_file);
11425       print_rtl_single (dump_file, pat);
11426     }
11427
11428   /* If it isn't, there is the possibility that we previously had an insn
11429      that clobbered some register as a side effect, but the combined
11430      insn doesn't need to do that.  So try once more without the clobbers
11431      unless this represents an ASM insn.  */
11432
11433   if (insn_code_number < 0 && ! check_asm_operands (pat)
11434       && GET_CODE (pat) == PARALLEL)
11435     {
11436       int pos;
11437
11438       for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11439         if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11440           {
11441             if (i != pos)
11442               SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11443             pos++;
11444           }
11445
11446       SUBST_INT (XVECLEN (pat, 0), pos);
11447
11448       if (pos == 1)
11449         pat = XVECEXP (pat, 0, 0);
11450
11451       PATTERN (insn) = pat;
11452       insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11453       if (dump_file && (dump_flags & TDF_DETAILS))
11454         {
11455           if (insn_code_number < 0)
11456             fputs ("Failed to match this instruction:\n", dump_file);
11457           else
11458             fputs ("Successfully matched this instruction:\n", dump_file);
11459           print_rtl_single (dump_file, pat);
11460         }
11461     }
11462
11463   pat_without_clobbers = pat;
11464
11465   PATTERN (insn) = old_pat;
11466   REG_NOTES (insn) = old_notes;
11467
11468   /* Recognize all noop sets, these will be killed by followup pass.  */
11469   if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11470     insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11471
11472   /* If we had any clobbers to add, make a new pattern than contains
11473      them.  Then check to make sure that all of them are dead.  */
11474   if (num_clobbers_to_add)
11475     {
11476       rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11477                                      rtvec_alloc (GET_CODE (pat) == PARALLEL
11478                                                   ? (XVECLEN (pat, 0)
11479                                                      + num_clobbers_to_add)
11480                                                   : num_clobbers_to_add + 1));
11481
11482       if (GET_CODE (pat) == PARALLEL)
11483         for (i = 0; i < XVECLEN (pat, 0); i++)
11484           XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11485       else
11486         XVECEXP (newpat, 0, 0) = pat;
11487
11488       add_clobbers (newpat, insn_code_number);
11489
11490       for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11491            i < XVECLEN (newpat, 0); i++)
11492         {
11493           if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11494               && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11495             return -1;
11496           if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11497             {
11498               gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11499               notes = alloc_reg_note (REG_UNUSED,
11500                                       XEXP (XVECEXP (newpat, 0, i), 0), notes);
11501             }
11502         }
11503       pat = newpat;
11504     }
11505
11506   if (insn_code_number >= 0
11507       && insn_code_number != NOOP_MOVE_INSN_CODE)
11508     {
11509       old_pat = PATTERN (insn);
11510       old_notes = REG_NOTES (insn);
11511       old_icode = INSN_CODE (insn);
11512       PATTERN (insn) = pat;
11513       REG_NOTES (insn) = notes;
11514       INSN_CODE (insn) = insn_code_number;
11515
11516       /* Allow targets to reject combined insn.  */
11517       if (!targetm.legitimate_combined_insn (insn))
11518         {
11519           if (dump_file && (dump_flags & TDF_DETAILS))
11520             fputs ("Instruction not appropriate for target.",
11521                    dump_file);
11522
11523           /* Callers expect recog_for_combine to strip
11524              clobbers from the pattern on failure.  */
11525           pat = pat_without_clobbers;
11526           notes = NULL_RTX;
11527
11528           insn_code_number = -1;
11529         }
11530
11531       PATTERN (insn) = old_pat;
11532       REG_NOTES (insn) = old_notes;
11533       INSN_CODE (insn) = old_icode;
11534     }
11535
11536   *pnewpat = pat;
11537   *pnotes = notes;
11538
11539   return insn_code_number;
11540 }
11541
11542 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11543    expressed as an AND and maybe an LSHIFTRT, to that formulation.
11544    Return whether anything was so changed.  */
11545
11546 static bool
11547 change_zero_ext (rtx pat)
11548 {
11549   bool changed = false;
11550   rtx *src = &SET_SRC (pat);
11551
11552   subrtx_ptr_iterator::array_type array;
11553   FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11554     {
11555       rtx x = **iter;
11556       scalar_int_mode mode, inner_mode;
11557       if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11558         continue;
11559       int size;
11560
11561       if (GET_CODE (x) == ZERO_EXTRACT
11562           && CONST_INT_P (XEXP (x, 1))
11563           && CONST_INT_P (XEXP (x, 2))
11564           && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11565           && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11566         {
11567           size = INTVAL (XEXP (x, 1));
11568
11569           int start = INTVAL (XEXP (x, 2));
11570           if (BITS_BIG_ENDIAN)
11571             start = GET_MODE_PRECISION (inner_mode) - size - start;
11572
11573           if (start != 0)
11574             x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11575                                   gen_int_shift_amount (inner_mode, start));
11576           else
11577             x = XEXP (x, 0);
11578
11579           if (mode != inner_mode)
11580             {
11581               if (REG_P (x) && HARD_REGISTER_P (x)
11582                   && !can_change_dest_mode (x, 0, mode))
11583                 continue;
11584
11585               x = gen_lowpart_SUBREG (mode, x);
11586             }
11587         }
11588       else if (GET_CODE (x) == ZERO_EXTEND
11589                && GET_CODE (XEXP (x, 0)) == SUBREG
11590                && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11591                && !paradoxical_subreg_p (XEXP (x, 0))
11592                && subreg_lowpart_p (XEXP (x, 0)))
11593         {
11594           inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11595           size = GET_MODE_PRECISION (inner_mode);
11596           x = SUBREG_REG (XEXP (x, 0));
11597           if (GET_MODE (x) != mode)
11598             {
11599               if (REG_P (x) && HARD_REGISTER_P (x)
11600                   && !can_change_dest_mode (x, 0, mode))
11601                 continue;
11602
11603               x = gen_lowpart_SUBREG (mode, x);
11604             }
11605         }
11606       else if (GET_CODE (x) == ZERO_EXTEND
11607                && REG_P (XEXP (x, 0))
11608                && HARD_REGISTER_P (XEXP (x, 0))
11609                && can_change_dest_mode (XEXP (x, 0), 0, mode))
11610         {
11611           inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11612           size = GET_MODE_PRECISION (inner_mode);
11613           x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11614         }
11615       else
11616         continue;
11617
11618       if (!(GET_CODE (x) == LSHIFTRT
11619             && CONST_INT_P (XEXP (x, 1))
11620             && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11621         {
11622           wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11623           x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11624         }
11625
11626       SUBST (**iter, x);
11627       changed = true;
11628     }
11629
11630   if (changed)
11631     FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11632       maybe_swap_commutative_operands (**iter);
11633
11634   rtx *dst = &SET_DEST (pat);
11635   scalar_int_mode mode;
11636   if (GET_CODE (*dst) == ZERO_EXTRACT
11637       && REG_P (XEXP (*dst, 0))
11638       && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11639       && CONST_INT_P (XEXP (*dst, 1))
11640       && CONST_INT_P (XEXP (*dst, 2)))
11641     {
11642       rtx reg = XEXP (*dst, 0);
11643       int width = INTVAL (XEXP (*dst, 1));
11644       int offset = INTVAL (XEXP (*dst, 2));
11645       int reg_width = GET_MODE_PRECISION (mode);
11646       if (BITS_BIG_ENDIAN)
11647         offset = reg_width - width - offset;
11648
11649       rtx x, y, z, w;
11650       wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11651       wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11652       x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11653       if (offset)
11654         y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11655       else
11656         y = SET_SRC (pat);
11657       z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11658       w = gen_rtx_IOR (mode, x, z);
11659       SUBST (SET_DEST (pat), reg);
11660       SUBST (SET_SRC (pat), w);
11661
11662       changed = true;
11663     }
11664
11665   return changed;
11666 }
11667
11668 /* Like recog, but we receive the address of a pointer to a new pattern.
11669    We try to match the rtx that the pointer points to.
11670    If that fails, we may try to modify or replace the pattern,
11671    storing the replacement into the same pointer object.
11672
11673    Modifications include deletion or addition of CLOBBERs.  If the
11674    instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11675    to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11676    (and undo if that fails).
11677
11678    PNOTES is a pointer to a location where any REG_UNUSED notes added for
11679    the CLOBBERs are placed.
11680
11681    The value is the final insn code from the pattern ultimately matched,
11682    or -1.  */
11683
11684 static int
11685 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11686 {
11687   rtx pat = *pnewpat;
11688   int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11689   if (insn_code_number >= 0 || check_asm_operands (pat))
11690     return insn_code_number;
11691
11692   void *marker = get_undo_marker ();
11693   bool changed = false;
11694
11695   if (GET_CODE (pat) == SET)
11696     changed = change_zero_ext (pat);
11697   else if (GET_CODE (pat) == PARALLEL)
11698     {
11699       int i;
11700       for (i = 0; i < XVECLEN (pat, 0); i++)
11701         {
11702           rtx set = XVECEXP (pat, 0, i);
11703           if (GET_CODE (set) == SET)
11704             changed |= change_zero_ext (set);
11705         }
11706     }
11707
11708   if (changed)
11709     {
11710       insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11711
11712       if (insn_code_number < 0)
11713         undo_to_marker (marker);
11714     }
11715
11716   return insn_code_number;
11717 }
11718 \f
11719 /* Like gen_lowpart_general but for use by combine.  In combine it
11720    is not possible to create any new pseudoregs.  However, it is
11721    safe to create invalid memory addresses, because combine will
11722    try to recognize them and all they will do is make the combine
11723    attempt fail.
11724
11725    If for some reason this cannot do its job, an rtx
11726    (clobber (const_int 0)) is returned.
11727    An insn containing that will not be recognized.  */
11728
11729 static rtx
11730 gen_lowpart_for_combine (machine_mode omode, rtx x)
11731 {
11732   machine_mode imode = GET_MODE (x);
11733   rtx result;
11734
11735   if (omode == imode)
11736     return x;
11737
11738   /* We can only support MODE being wider than a word if X is a
11739      constant integer or has a mode the same size.  */
11740   if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11741       && ! (CONST_SCALAR_INT_P (x)
11742             || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11743     goto fail;
11744
11745   /* X might be a paradoxical (subreg (mem)).  In that case, gen_lowpart
11746      won't know what to do.  So we will strip off the SUBREG here and
11747      process normally.  */
11748   if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11749     {
11750       x = SUBREG_REG (x);
11751
11752       /* For use in case we fall down into the address adjustments
11753          further below, we need to adjust the known mode and size of
11754          x; imode and isize, since we just adjusted x.  */
11755       imode = GET_MODE (x);
11756
11757       if (imode == omode)
11758         return x;
11759     }
11760
11761   result = gen_lowpart_common (omode, x);
11762
11763   if (result)
11764     return result;
11765
11766   if (MEM_P (x))
11767     {
11768       /* Refuse to work on a volatile memory ref or one with a mode-dependent
11769          address.  */
11770       if (MEM_VOLATILE_P (x)
11771           || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11772         goto fail;
11773
11774       /* If we want to refer to something bigger than the original memref,
11775          generate a paradoxical subreg instead.  That will force a reload
11776          of the original memref X.  */
11777       if (paradoxical_subreg_p (omode, imode))
11778         return gen_rtx_SUBREG (omode, x, 0);
11779
11780       poly_int64 offset = byte_lowpart_offset (omode, imode);
11781       return adjust_address_nv (x, omode, offset);
11782     }
11783
11784   /* If X is a comparison operator, rewrite it in a new mode.  This
11785      probably won't match, but may allow further simplifications.  */
11786   else if (COMPARISON_P (x))
11787     return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11788
11789   /* If we couldn't simplify X any other way, just enclose it in a
11790      SUBREG.  Normally, this SUBREG won't match, but some patterns may
11791      include an explicit SUBREG or we may simplify it further in combine.  */
11792   else
11793     {
11794       rtx res;
11795
11796       if (imode == VOIDmode)
11797         {
11798           imode = int_mode_for_mode (omode).require ();
11799           x = gen_lowpart_common (imode, x);
11800           if (x == NULL)
11801             goto fail;
11802         }
11803       res = lowpart_subreg (omode, x, imode);
11804       if (res)
11805         return res;
11806     }
11807
11808  fail:
11809   return gen_rtx_CLOBBER (omode, const0_rtx);
11810 }
11811 \f
11812 /* Try to simplify a comparison between OP0 and a constant OP1,
11813    where CODE is the comparison code that will be tested, into a
11814    (CODE OP0 const0_rtx) form.
11815
11816    The result is a possibly different comparison code to use.
11817    *POP1 may be updated.  */
11818
11819 static enum rtx_code
11820 simplify_compare_const (enum rtx_code code, machine_mode mode,
11821                         rtx op0, rtx *pop1)
11822 {
11823   scalar_int_mode int_mode;
11824   HOST_WIDE_INT const_op = INTVAL (*pop1);
11825
11826   /* Get the constant we are comparing against and turn off all bits
11827      not on in our mode.  */
11828   if (mode != VOIDmode)
11829     const_op = trunc_int_for_mode (const_op, mode);
11830
11831   /* If we are comparing against a constant power of two and the value
11832      being compared can only have that single bit nonzero (e.g., it was
11833      `and'ed with that bit), we can replace this with a comparison
11834      with zero.  */
11835   if (const_op
11836       && (code == EQ || code == NE || code == GE || code == GEU
11837           || code == LT || code == LTU)
11838       && is_a <scalar_int_mode> (mode, &int_mode)
11839       && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11840       && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11841       && (nonzero_bits (op0, int_mode)
11842           == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11843     {
11844       code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11845       const_op = 0;
11846     }
11847
11848   /* Similarly, if we are comparing a value known to be either -1 or
11849      0 with -1, change it to the opposite comparison against zero.  */
11850   if (const_op == -1
11851       && (code == EQ || code == NE || code == GT || code == LE
11852           || code == GEU || code == LTU)
11853       && is_a <scalar_int_mode> (mode, &int_mode)
11854       && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11855     {
11856       code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11857       const_op = 0;
11858     }
11859
11860   /* Do some canonicalizations based on the comparison code.  We prefer
11861      comparisons against zero and then prefer equality comparisons.
11862      If we can reduce the size of a constant, we will do that too.  */
11863   switch (code)
11864     {
11865     case LT:
11866       /* < C is equivalent to <= (C - 1) */
11867       if (const_op > 0)
11868         {
11869           const_op -= 1;
11870           code = LE;
11871           /* ... fall through to LE case below.  */
11872           gcc_fallthrough ();
11873         }
11874       else
11875         break;
11876
11877     case LE:
11878       /* <= C is equivalent to < (C + 1); we do this for C < 0  */
11879       if (const_op < 0)
11880         {
11881           const_op += 1;
11882           code = LT;
11883         }
11884
11885       /* If we are doing a <= 0 comparison on a value known to have
11886          a zero sign bit, we can replace this with == 0.  */
11887       else if (const_op == 0
11888                && is_a <scalar_int_mode> (mode, &int_mode)
11889                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11890                && (nonzero_bits (op0, int_mode)
11891                    & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11892                == 0)
11893         code = EQ;
11894       break;
11895
11896     case GE:
11897       /* >= C is equivalent to > (C - 1).  */
11898       if (const_op > 0)
11899         {
11900           const_op -= 1;
11901           code = GT;
11902           /* ... fall through to GT below.  */
11903           gcc_fallthrough ();
11904         }
11905       else
11906         break;
11907
11908     case GT:
11909       /* > C is equivalent to >= (C + 1); we do this for C < 0.  */
11910       if (const_op < 0)
11911         {
11912           const_op += 1;
11913           code = GE;
11914         }
11915
11916       /* If we are doing a > 0 comparison on a value known to have
11917          a zero sign bit, we can replace this with != 0.  */
11918       else if (const_op == 0
11919                && is_a <scalar_int_mode> (mode, &int_mode)
11920                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11921                && (nonzero_bits (op0, int_mode)
11922                    & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11923                == 0)
11924         code = NE;
11925       break;
11926
11927     case LTU:
11928       /* < C is equivalent to <= (C - 1).  */
11929       if (const_op > 0)
11930         {
11931           const_op -= 1;
11932           code = LEU;
11933           /* ... fall through ...  */
11934           gcc_fallthrough ();
11935         }
11936       /* (unsigned) < 0x80000000 is equivalent to >= 0.  */
11937       else if (is_a <scalar_int_mode> (mode, &int_mode)
11938                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11939                && ((unsigned HOST_WIDE_INT) const_op
11940                    == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11941         {
11942           const_op = 0;
11943           code = GE;
11944           break;
11945         }
11946       else
11947         break;
11948
11949     case LEU:
11950       /* unsigned <= 0 is equivalent to == 0 */
11951       if (const_op == 0)
11952         code = EQ;
11953       /* (unsigned) <= 0x7fffffff is equivalent to >= 0.  */
11954       else if (is_a <scalar_int_mode> (mode, &int_mode)
11955                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11956                && ((unsigned HOST_WIDE_INT) const_op
11957                    == ((HOST_WIDE_INT_1U
11958                         << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11959         {
11960           const_op = 0;
11961           code = GE;
11962         }
11963       break;
11964
11965     case GEU:
11966       /* >= C is equivalent to > (C - 1).  */
11967       if (const_op > 1)
11968         {
11969           const_op -= 1;
11970           code = GTU;
11971           /* ... fall through ...  */
11972           gcc_fallthrough ();
11973         }
11974
11975       /* (unsigned) >= 0x80000000 is equivalent to < 0.  */
11976       else if (is_a <scalar_int_mode> (mode, &int_mode)
11977                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11978                && ((unsigned HOST_WIDE_INT) const_op
11979                    == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11980         {
11981           const_op = 0;
11982           code = LT;
11983           break;
11984         }
11985       else
11986         break;
11987
11988     case GTU:
11989       /* unsigned > 0 is equivalent to != 0 */
11990       if (const_op == 0)
11991         code = NE;
11992       /* (unsigned) > 0x7fffffff is equivalent to < 0.  */
11993       else if (is_a <scalar_int_mode> (mode, &int_mode)
11994                && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11995                && ((unsigned HOST_WIDE_INT) const_op
11996                    == (HOST_WIDE_INT_1U
11997                        << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11998         {
11999           const_op = 0;
12000           code = LT;
12001         }
12002       break;
12003
12004     default:
12005       break;
12006     }
12007
12008   *pop1 = GEN_INT (const_op);
12009   return code;
12010 }
12011 \f
12012 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
12013    comparison code that will be tested.
12014
12015    The result is a possibly different comparison code to use.  *POP0 and
12016    *POP1 may be updated.
12017
12018    It is possible that we might detect that a comparison is either always
12019    true or always false.  However, we do not perform general constant
12020    folding in combine, so this knowledge isn't useful.  Such tautologies
12021    should have been detected earlier.  Hence we ignore all such cases.  */
12022
12023 static enum rtx_code
12024 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
12025 {
12026   rtx op0 = *pop0;
12027   rtx op1 = *pop1;
12028   rtx tem, tem1;
12029   int i;
12030   scalar_int_mode mode, inner_mode, tmode;
12031   opt_scalar_int_mode tmode_iter;
12032
12033   /* Try a few ways of applying the same transformation to both operands.  */
12034   while (1)
12035     {
12036       /* The test below this one won't handle SIGN_EXTENDs on these machines,
12037          so check specially.  */
12038       if (!WORD_REGISTER_OPERATIONS
12039           && code != GTU && code != GEU && code != LTU && code != LEU
12040           && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
12041           && GET_CODE (XEXP (op0, 0)) == ASHIFT
12042           && GET_CODE (XEXP (op1, 0)) == ASHIFT
12043           && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
12044           && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
12045           && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
12046           && (is_a <scalar_int_mode>
12047               (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
12048           && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
12049           && CONST_INT_P (XEXP (op0, 1))
12050           && XEXP (op0, 1) == XEXP (op1, 1)
12051           && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12052           && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
12053           && (INTVAL (XEXP (op0, 1))
12054               == (GET_MODE_PRECISION (mode)
12055                   - GET_MODE_PRECISION (inner_mode))))
12056         {
12057           op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
12058           op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
12059         }
12060
12061       /* If both operands are the same constant shift, see if we can ignore the
12062          shift.  We can if the shift is a rotate or if the bits shifted out of
12063          this shift are known to be zero for both inputs and if the type of
12064          comparison is compatible with the shift.  */
12065       if (GET_CODE (op0) == GET_CODE (op1)
12066           && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
12067           && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
12068               || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
12069                   && (code != GT && code != LT && code != GE && code != LE))
12070               || (GET_CODE (op0) == ASHIFTRT
12071                   && (code != GTU && code != LTU
12072                       && code != GEU && code != LEU)))
12073           && CONST_INT_P (XEXP (op0, 1))
12074           && INTVAL (XEXP (op0, 1)) >= 0
12075           && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12076           && XEXP (op0, 1) == XEXP (op1, 1))
12077         {
12078           machine_mode mode = GET_MODE (op0);
12079           unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12080           int shift_count = INTVAL (XEXP (op0, 1));
12081
12082           if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
12083             mask &= (mask >> shift_count) << shift_count;
12084           else if (GET_CODE (op0) == ASHIFT)
12085             mask = (mask & (mask << shift_count)) >> shift_count;
12086
12087           if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12088               && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12089             op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12090           else
12091             break;
12092         }
12093
12094       /* If both operands are AND's of a paradoxical SUBREG by constant, the
12095          SUBREGs are of the same mode, and, in both cases, the AND would
12096          be redundant if the comparison was done in the narrower mode,
12097          do the comparison in the narrower mode (e.g., we are AND'ing with 1
12098          and the operand's possibly nonzero bits are 0xffffff01; in that case
12099          if we only care about QImode, we don't need the AND).  This case
12100          occurs if the output mode of an scc insn is not SImode and
12101          STORE_FLAG_VALUE == 1 (e.g., the 386).
12102
12103          Similarly, check for a case where the AND's are ZERO_EXTEND
12104          operations from some narrower mode even though a SUBREG is not
12105          present.  */
12106
12107       else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12108                && CONST_INT_P (XEXP (op0, 1))
12109                && CONST_INT_P (XEXP (op1, 1)))
12110         {
12111           rtx inner_op0 = XEXP (op0, 0);
12112           rtx inner_op1 = XEXP (op1, 0);
12113           HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12114           HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12115           int changed = 0;
12116
12117           if (paradoxical_subreg_p (inner_op0)
12118               && GET_CODE (inner_op1) == SUBREG
12119               && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12120               && (GET_MODE (SUBREG_REG (inner_op0))
12121                   == GET_MODE (SUBREG_REG (inner_op1)))
12122               && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12123                                         GET_MODE (SUBREG_REG (inner_op0)))) == 0
12124               && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12125                                         GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12126             {
12127               op0 = SUBREG_REG (inner_op0);
12128               op1 = SUBREG_REG (inner_op1);
12129
12130               /* The resulting comparison is always unsigned since we masked
12131                  off the original sign bit.  */
12132               code = unsigned_condition (code);
12133
12134               changed = 1;
12135             }
12136
12137           else if (c0 == c1)
12138             FOR_EACH_MODE_UNTIL (tmode,
12139                                  as_a <scalar_int_mode> (GET_MODE (op0)))
12140               if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12141                 {
12142                   op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12143                   op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12144                   code = unsigned_condition (code);
12145                   changed = 1;
12146                   break;
12147                 }
12148
12149           if (! changed)
12150             break;
12151         }
12152
12153       /* If both operands are NOT, we can strip off the outer operation
12154          and adjust the comparison code for swapped operands; similarly for
12155          NEG, except that this must be an equality comparison.  */
12156       else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12157                || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12158                    && (code == EQ || code == NE)))
12159         op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12160
12161       else
12162         break;
12163     }
12164
12165   /* If the first operand is a constant, swap the operands and adjust the
12166      comparison code appropriately, but don't do this if the second operand
12167      is already a constant integer.  */
12168   if (swap_commutative_operands_p (op0, op1))
12169     {
12170       std::swap (op0, op1);
12171       code = swap_condition (code);
12172     }
12173
12174   /* We now enter a loop during which we will try to simplify the comparison.
12175      For the most part, we only are concerned with comparisons with zero,
12176      but some things may really be comparisons with zero but not start
12177      out looking that way.  */
12178
12179   while (CONST_INT_P (op1))
12180     {
12181       machine_mode raw_mode = GET_MODE (op0);
12182       scalar_int_mode int_mode;
12183       int equality_comparison_p;
12184       int sign_bit_comparison_p;
12185       int unsigned_comparison_p;
12186       HOST_WIDE_INT const_op;
12187
12188       /* We only want to handle integral modes.  This catches VOIDmode,
12189          CCmode, and the floating-point modes.  An exception is that we
12190          can handle VOIDmode if OP0 is a COMPARE or a comparison
12191          operation.  */
12192
12193       if (GET_MODE_CLASS (raw_mode) != MODE_INT
12194           && ! (raw_mode == VOIDmode
12195                 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12196         break;
12197
12198       /* Try to simplify the compare to constant, possibly changing the
12199          comparison op, and/or changing op1 to zero.  */
12200       code = simplify_compare_const (code, raw_mode, op0, &op1);
12201       const_op = INTVAL (op1);
12202
12203       /* Compute some predicates to simplify code below.  */
12204
12205       equality_comparison_p = (code == EQ || code == NE);
12206       sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12207       unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12208                                || code == GEU);
12209
12210       /* If this is a sign bit comparison and we can do arithmetic in
12211          MODE, say that we will only be needing the sign bit of OP0.  */
12212       if (sign_bit_comparison_p
12213           && is_a <scalar_int_mode> (raw_mode, &int_mode)
12214           && HWI_COMPUTABLE_MODE_P (int_mode))
12215         op0 = force_to_mode (op0, int_mode,
12216                              HOST_WIDE_INT_1U
12217                              << (GET_MODE_PRECISION (int_mode) - 1),
12218                              0);
12219
12220       if (COMPARISON_P (op0))
12221         {
12222           /* We can't do anything if OP0 is a condition code value, rather
12223              than an actual data value.  */
12224           if (const_op != 0
12225               || CC0_P (XEXP (op0, 0))
12226               || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12227             break;
12228
12229           /* Get the two operands being compared.  */
12230           if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12231             tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12232           else
12233             tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12234
12235           /* Check for the cases where we simply want the result of the
12236              earlier test or the opposite of that result.  */
12237           if (code == NE || code == EQ
12238               || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12239                   && (code == LT || code == GE)))
12240             {
12241               enum rtx_code new_code;
12242               if (code == LT || code == NE)
12243                 new_code = GET_CODE (op0);
12244               else
12245                 new_code = reversed_comparison_code (op0, NULL);
12246
12247               if (new_code != UNKNOWN)
12248                 {
12249                   code = new_code;
12250                   op0 = tem;
12251                   op1 = tem1;
12252                   continue;
12253                 }
12254             }
12255           break;
12256         }
12257
12258       if (raw_mode == VOIDmode)
12259         break;
12260       scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12261
12262       /* Now try cases based on the opcode of OP0.  If none of the cases
12263          does a "continue", we exit this loop immediately after the
12264          switch.  */
12265
12266       unsigned int mode_width = GET_MODE_PRECISION (mode);
12267       unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12268       switch (GET_CODE (op0))
12269         {
12270         case ZERO_EXTRACT:
12271           /* If we are extracting a single bit from a variable position in
12272              a constant that has only a single bit set and are comparing it
12273              with zero, we can convert this into an equality comparison
12274              between the position and the location of the single bit.  */
12275           /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12276              have already reduced the shift count modulo the word size.  */
12277           if (!SHIFT_COUNT_TRUNCATED
12278               && CONST_INT_P (XEXP (op0, 0))
12279               && XEXP (op0, 1) == const1_rtx
12280               && equality_comparison_p && const_op == 0
12281               && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12282             {
12283               if (BITS_BIG_ENDIAN)
12284                 i = BITS_PER_WORD - 1 - i;
12285
12286               op0 = XEXP (op0, 2);
12287               op1 = GEN_INT (i);
12288               const_op = i;
12289
12290               /* Result is nonzero iff shift count is equal to I.  */
12291               code = reverse_condition (code);
12292               continue;
12293             }
12294
12295           /* fall through */
12296
12297         case SIGN_EXTRACT:
12298           tem = expand_compound_operation (op0);
12299           if (tem != op0)
12300             {
12301               op0 = tem;
12302               continue;
12303             }
12304           break;
12305
12306         case NOT:
12307           /* If testing for equality, we can take the NOT of the constant.  */
12308           if (equality_comparison_p
12309               && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12310             {
12311               op0 = XEXP (op0, 0);
12312               op1 = tem;
12313               continue;
12314             }
12315
12316           /* If just looking at the sign bit, reverse the sense of the
12317              comparison.  */
12318           if (sign_bit_comparison_p)
12319             {
12320               op0 = XEXP (op0, 0);
12321               code = (code == GE ? LT : GE);
12322               continue;
12323             }
12324           break;
12325
12326         case NEG:
12327           /* If testing for equality, we can take the NEG of the constant.  */
12328           if (equality_comparison_p
12329               && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12330             {
12331               op0 = XEXP (op0, 0);
12332               op1 = tem;
12333               continue;
12334             }
12335
12336           /* The remaining cases only apply to comparisons with zero.  */
12337           if (const_op != 0)
12338             break;
12339
12340           /* When X is ABS or is known positive,
12341              (neg X) is < 0 if and only if X != 0.  */
12342
12343           if (sign_bit_comparison_p
12344               && (GET_CODE (XEXP (op0, 0)) == ABS
12345                   || (mode_width <= HOST_BITS_PER_WIDE_INT
12346                       && (nonzero_bits (XEXP (op0, 0), mode)
12347                           & (HOST_WIDE_INT_1U << (mode_width - 1)))
12348                          == 0)))
12349             {
12350               op0 = XEXP (op0, 0);
12351               code = (code == LT ? NE : EQ);
12352               continue;
12353             }
12354
12355           /* If we have NEG of something whose two high-order bits are the
12356              same, we know that "(-a) < 0" is equivalent to "a > 0".  */
12357           if (num_sign_bit_copies (op0, mode) >= 2)
12358             {
12359               op0 = XEXP (op0, 0);
12360               code = swap_condition (code);
12361               continue;
12362             }
12363           break;
12364
12365         case ROTATE:
12366           /* If we are testing equality and our count is a constant, we
12367              can perform the inverse operation on our RHS.  */
12368           if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12369               && (tem = simplify_binary_operation (ROTATERT, mode,
12370                                                    op1, XEXP (op0, 1))) != 0)
12371             {
12372               op0 = XEXP (op0, 0);
12373               op1 = tem;
12374               continue;
12375             }
12376
12377           /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12378              a particular bit.  Convert it to an AND of a constant of that
12379              bit.  This will be converted into a ZERO_EXTRACT.  */
12380           if (const_op == 0 && sign_bit_comparison_p
12381               && CONST_INT_P (XEXP (op0, 1))
12382               && mode_width <= HOST_BITS_PER_WIDE_INT)
12383             {
12384               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12385                                             (HOST_WIDE_INT_1U
12386                                              << (mode_width - 1
12387                                                  - INTVAL (XEXP (op0, 1)))));
12388               code = (code == LT ? NE : EQ);
12389               continue;
12390             }
12391
12392           /* Fall through.  */
12393
12394         case ABS:
12395           /* ABS is ignorable inside an equality comparison with zero.  */
12396           if (const_op == 0 && equality_comparison_p)
12397             {
12398               op0 = XEXP (op0, 0);
12399               continue;
12400             }
12401           break;
12402
12403         case SIGN_EXTEND:
12404           /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12405              (compare FOO CONST) if CONST fits in FOO's mode and we
12406              are either testing inequality or have an unsigned
12407              comparison with ZERO_EXTEND or a signed comparison with
12408              SIGN_EXTEND.  But don't do it if we don't have a compare
12409              insn of the given mode, since we'd have to revert it
12410              later on, and then we wouldn't know whether to sign- or
12411              zero-extend.  */
12412           if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12413               && ! unsigned_comparison_p
12414               && HWI_COMPUTABLE_MODE_P (mode)
12415               && trunc_int_for_mode (const_op, mode) == const_op
12416               && have_insn_for (COMPARE, mode))
12417             {
12418               op0 = XEXP (op0, 0);
12419               continue;
12420             }
12421           break;
12422
12423         case SUBREG:
12424           /* Check for the case where we are comparing A - C1 with C2, that is
12425
12426                (subreg:MODE (plus (A) (-C1))) op (C2)
12427
12428              with C1 a constant, and try to lift the SUBREG, i.e. to do the
12429              comparison in the wider mode.  One of the following two conditions
12430              must be true in order for this to be valid:
12431
12432                1. The mode extension results in the same bit pattern being added
12433                   on both sides and the comparison is equality or unsigned.  As
12434                   C2 has been truncated to fit in MODE, the pattern can only be
12435                   all 0s or all 1s.
12436
12437                2. The mode extension results in the sign bit being copied on
12438                   each side.
12439
12440              The difficulty here is that we have predicates for A but not for
12441              (A - C1) so we need to check that C1 is within proper bounds so
12442              as to perturbate A as little as possible.  */
12443
12444           if (mode_width <= HOST_BITS_PER_WIDE_INT
12445               && subreg_lowpart_p (op0)
12446               && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12447                                          &inner_mode)
12448               && GET_MODE_PRECISION (inner_mode) > mode_width
12449               && GET_CODE (SUBREG_REG (op0)) == PLUS
12450               && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12451             {
12452               rtx a = XEXP (SUBREG_REG (op0), 0);
12453               HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12454
12455               if ((c1 > 0
12456                    && (unsigned HOST_WIDE_INT) c1
12457                        < HOST_WIDE_INT_1U << (mode_width - 1)
12458                    && (equality_comparison_p || unsigned_comparison_p)
12459                    /* (A - C1) zero-extends if it is positive and sign-extends
12460                       if it is negative, C2 both zero- and sign-extends.  */
12461                    && (((nonzero_bits (a, inner_mode)
12462                          & ~GET_MODE_MASK (mode)) == 0
12463                         && const_op >= 0)
12464                        /* (A - C1) sign-extends if it is positive and 1-extends
12465                           if it is negative, C2 both sign- and 1-extends.  */
12466                        || (num_sign_bit_copies (a, inner_mode)
12467                            > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12468                                              - mode_width)
12469                            && const_op < 0)))
12470                   || ((unsigned HOST_WIDE_INT) c1
12471                        < HOST_WIDE_INT_1U << (mode_width - 2)
12472                       /* (A - C1) always sign-extends, like C2.  */
12473                       && num_sign_bit_copies (a, inner_mode)
12474                          > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12475                                            - (mode_width - 1))))
12476                 {
12477                   op0 = SUBREG_REG (op0);
12478                   continue;
12479                 }
12480             }
12481
12482           /* If the inner mode is narrower and we are extracting the low part,
12483              we can treat the SUBREG as if it were a ZERO_EXTEND.  */
12484           if (paradoxical_subreg_p (op0))
12485             ;
12486           else if (subreg_lowpart_p (op0)
12487                    && GET_MODE_CLASS (mode) == MODE_INT
12488                    && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12489                    && (code == NE || code == EQ)
12490                    && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12491                    && !paradoxical_subreg_p (op0)
12492                    && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12493                        & ~GET_MODE_MASK (mode)) == 0)
12494             {
12495               /* Remove outer subregs that don't do anything.  */
12496               tem = gen_lowpart (inner_mode, op1);
12497
12498               if ((nonzero_bits (tem, inner_mode)
12499                    & ~GET_MODE_MASK (mode)) == 0)
12500                 {
12501                   op0 = SUBREG_REG (op0);
12502                   op1 = tem;
12503                   continue;
12504                 }
12505               break;
12506             }
12507           else
12508             break;
12509
12510           /* FALLTHROUGH */
12511
12512         case ZERO_EXTEND:
12513           if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12514               && (unsigned_comparison_p || equality_comparison_p)
12515               && HWI_COMPUTABLE_MODE_P (mode)
12516               && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12517               && const_op >= 0
12518               && have_insn_for (COMPARE, mode))
12519             {
12520               op0 = XEXP (op0, 0);
12521               continue;
12522             }
12523           break;
12524
12525         case PLUS:
12526           /* (eq (plus X A) B) -> (eq X (minus B A)).  We can only do
12527              this for equality comparisons due to pathological cases involving
12528              overflows.  */
12529           if (equality_comparison_p
12530               && (tem = simplify_binary_operation (MINUS, mode,
12531                                                    op1, XEXP (op0, 1))) != 0)
12532             {
12533               op0 = XEXP (op0, 0);
12534               op1 = tem;
12535               continue;
12536             }
12537
12538           /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0.  */
12539           if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12540               && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12541             {
12542               op0 = XEXP (XEXP (op0, 0), 0);
12543               code = (code == LT ? EQ : NE);
12544               continue;
12545             }
12546           break;
12547
12548         case MINUS:
12549           /* We used to optimize signed comparisons against zero, but that
12550              was incorrect.  Unsigned comparisons against zero (GTU, LEU)
12551              arrive here as equality comparisons, or (GEU, LTU) are
12552              optimized away.  No need to special-case them.  */
12553
12554           /* (eq (minus A B) C) -> (eq A (plus B C)) or
12555              (eq B (minus A C)), whichever simplifies.  We can only do
12556              this for equality comparisons due to pathological cases involving
12557              overflows.  */
12558           if (equality_comparison_p
12559               && (tem = simplify_binary_operation (PLUS, mode,
12560                                                    XEXP (op0, 1), op1)) != 0)
12561             {
12562               op0 = XEXP (op0, 0);
12563               op1 = tem;
12564               continue;
12565             }
12566
12567           if (equality_comparison_p
12568               && (tem = simplify_binary_operation (MINUS, mode,
12569                                                    XEXP (op0, 0), op1)) != 0)
12570             {
12571               op0 = XEXP (op0, 1);
12572               op1 = tem;
12573               continue;
12574             }
12575
12576           /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12577              of bits in X minus 1, is one iff X > 0.  */
12578           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12579               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12580               && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12581               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12582             {
12583               op0 = XEXP (op0, 1);
12584               code = (code == GE ? LE : GT);
12585               continue;
12586             }
12587           break;
12588
12589         case XOR:
12590           /* (eq (xor A B) C) -> (eq A (xor B C)).  This is a simplification
12591              if C is zero or B is a constant.  */
12592           if (equality_comparison_p
12593               && (tem = simplify_binary_operation (XOR, mode,
12594                                                    XEXP (op0, 1), op1)) != 0)
12595             {
12596               op0 = XEXP (op0, 0);
12597               op1 = tem;
12598               continue;
12599             }
12600           break;
12601
12602
12603         case IOR:
12604           /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12605              iff X <= 0.  */
12606           if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12607               && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12608               && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12609             {
12610               op0 = XEXP (op0, 1);
12611               code = (code == GE ? GT : LE);
12612               continue;
12613             }
12614           break;
12615
12616         case AND:
12617           /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1).  This
12618              will be converted to a ZERO_EXTRACT later.  */
12619           if (const_op == 0 && equality_comparison_p
12620               && GET_CODE (XEXP (op0, 0)) == ASHIFT
12621               && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12622             {
12623               op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12624                                       XEXP (XEXP (op0, 0), 1));
12625               op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12626               continue;
12627             }
12628
12629           /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12630              zero and X is a comparison and C1 and C2 describe only bits set
12631              in STORE_FLAG_VALUE, we can compare with X.  */
12632           if (const_op == 0 && equality_comparison_p
12633               && mode_width <= HOST_BITS_PER_WIDE_INT
12634               && CONST_INT_P (XEXP (op0, 1))
12635               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12636               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12637               && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12638               && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12639             {
12640               mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12641                       << INTVAL (XEXP (XEXP (op0, 0), 1)));
12642               if ((~STORE_FLAG_VALUE & mask) == 0
12643                   && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12644                       || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12645                           && COMPARISON_P (tem))))
12646                 {
12647                   op0 = XEXP (XEXP (op0, 0), 0);
12648                   continue;
12649                 }
12650             }
12651
12652           /* If we are doing an equality comparison of an AND of a bit equal
12653              to the sign bit, replace this with a LT or GE comparison of
12654              the underlying value.  */
12655           if (equality_comparison_p
12656               && const_op == 0
12657               && CONST_INT_P (XEXP (op0, 1))
12658               && mode_width <= HOST_BITS_PER_WIDE_INT
12659               && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12660                   == HOST_WIDE_INT_1U << (mode_width - 1)))
12661             {
12662               op0 = XEXP (op0, 0);
12663               code = (code == EQ ? GE : LT);
12664               continue;
12665             }
12666
12667           /* If this AND operation is really a ZERO_EXTEND from a narrower
12668              mode, the constant fits within that mode, and this is either an
12669              equality or unsigned comparison, try to do this comparison in
12670              the narrower mode.
12671
12672              Note that in:
12673
12674              (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12675              -> (ne:DI (reg:SI 4) (const_int 0))
12676
12677              unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12678              known to hold a value of the required mode the
12679              transformation is invalid.  */
12680           if ((equality_comparison_p || unsigned_comparison_p)
12681               && CONST_INT_P (XEXP (op0, 1))
12682               && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12683                                    & GET_MODE_MASK (mode))
12684                                   + 1)) >= 0
12685               && const_op >> i == 0
12686               && int_mode_for_size (i, 1).exists (&tmode))
12687             {
12688               op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12689               continue;
12690             }
12691
12692           /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12693              fits in both M1 and M2 and the SUBREG is either paradoxical
12694              or represents the low part, permute the SUBREG and the AND
12695              and try again.  */
12696           if (GET_CODE (XEXP (op0, 0)) == SUBREG
12697               && CONST_INT_P (XEXP (op0, 1)))
12698             {
12699               unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12700               /* Require an integral mode, to avoid creating something like
12701                  (AND:SF ...).  */
12702               if ((is_a <scalar_int_mode>
12703                    (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12704                   /* It is unsafe to commute the AND into the SUBREG if the
12705                      SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12706                      not defined.  As originally written the upper bits
12707                      have a defined value due to the AND operation.
12708                      However, if we commute the AND inside the SUBREG then
12709                      they no longer have defined values and the meaning of
12710                      the code has been changed.
12711                      Also C1 should not change value in the smaller mode,
12712                      see PR67028 (a positive C1 can become negative in the
12713                      smaller mode, so that the AND does no longer mask the
12714                      upper bits).  */
12715                   && ((WORD_REGISTER_OPERATIONS
12716                        && mode_width > GET_MODE_PRECISION (tmode)
12717                        && mode_width <= BITS_PER_WORD
12718                        && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12719                       || (mode_width <= GET_MODE_PRECISION (tmode)
12720                           && subreg_lowpart_p (XEXP (op0, 0))))
12721                   && mode_width <= HOST_BITS_PER_WIDE_INT
12722                   && HWI_COMPUTABLE_MODE_P (tmode)
12723                   && (c1 & ~mask) == 0
12724                   && (c1 & ~GET_MODE_MASK (tmode)) == 0
12725                   && c1 != mask
12726                   && c1 != GET_MODE_MASK (tmode))
12727                 {
12728                   op0 = simplify_gen_binary (AND, tmode,
12729                                              SUBREG_REG (XEXP (op0, 0)),
12730                                              gen_int_mode (c1, tmode));
12731                   op0 = gen_lowpart (mode, op0);
12732                   continue;
12733                 }
12734             }
12735
12736           /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0).  */
12737           if (const_op == 0 && equality_comparison_p
12738               && XEXP (op0, 1) == const1_rtx
12739               && GET_CODE (XEXP (op0, 0)) == NOT)
12740             {
12741               op0 = simplify_and_const_int (NULL_RTX, mode,
12742                                             XEXP (XEXP (op0, 0), 0), 1);
12743               code = (code == NE ? EQ : NE);
12744               continue;
12745             }
12746
12747           /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12748              (eq (and (lshiftrt X) 1) 0).
12749              Also handle the case where (not X) is expressed using xor.  */
12750           if (const_op == 0 && equality_comparison_p
12751               && XEXP (op0, 1) == const1_rtx
12752               && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12753             {
12754               rtx shift_op = XEXP (XEXP (op0, 0), 0);
12755               rtx shift_count = XEXP (XEXP (op0, 0), 1);
12756
12757               if (GET_CODE (shift_op) == NOT
12758                   || (GET_CODE (shift_op) == XOR
12759                       && CONST_INT_P (XEXP (shift_op, 1))
12760                       && CONST_INT_P (shift_count)
12761                       && HWI_COMPUTABLE_MODE_P (mode)
12762                       && (UINTVAL (XEXP (shift_op, 1))
12763                           == HOST_WIDE_INT_1U
12764                                << INTVAL (shift_count))))
12765                 {
12766                   op0
12767                     = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12768                   op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12769                   code = (code == NE ? EQ : NE);
12770                   continue;
12771                 }
12772             }
12773           break;
12774
12775         case ASHIFT:
12776           /* If we have (compare (ashift FOO N) (const_int C)) and
12777              the high order N bits of FOO (N+1 if an inequality comparison)
12778              are known to be zero, we can do this by comparing FOO with C
12779              shifted right N bits so long as the low-order N bits of C are
12780              zero.  */
12781           if (CONST_INT_P (XEXP (op0, 1))
12782               && INTVAL (XEXP (op0, 1)) >= 0
12783               && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12784                   < HOST_BITS_PER_WIDE_INT)
12785               && (((unsigned HOST_WIDE_INT) const_op
12786                    & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12787                       - 1)) == 0)
12788               && mode_width <= HOST_BITS_PER_WIDE_INT
12789               && (nonzero_bits (XEXP (op0, 0), mode)
12790                   & ~(mask >> (INTVAL (XEXP (op0, 1))
12791                                + ! equality_comparison_p))) == 0)
12792             {
12793               /* We must perform a logical shift, not an arithmetic one,
12794                  as we want the top N bits of C to be zero.  */
12795               unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12796
12797               temp >>= INTVAL (XEXP (op0, 1));
12798               op1 = gen_int_mode (temp, mode);
12799               op0 = XEXP (op0, 0);
12800               continue;
12801             }
12802
12803           /* If we are doing a sign bit comparison, it means we are testing
12804              a particular bit.  Convert it to the appropriate AND.  */
12805           if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12806               && mode_width <= HOST_BITS_PER_WIDE_INT)
12807             {
12808               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12809                                             (HOST_WIDE_INT_1U
12810                                              << (mode_width - 1
12811                                                  - INTVAL (XEXP (op0, 1)))));
12812               code = (code == LT ? NE : EQ);
12813               continue;
12814             }
12815
12816           /* If this an equality comparison with zero and we are shifting
12817              the low bit to the sign bit, we can convert this to an AND of the
12818              low-order bit.  */
12819           if (const_op == 0 && equality_comparison_p
12820               && CONST_INT_P (XEXP (op0, 1))
12821               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12822             {
12823               op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12824               continue;
12825             }
12826           break;
12827
12828         case ASHIFTRT:
12829           /* If this is an equality comparison with zero, we can do this
12830              as a logical shift, which might be much simpler.  */
12831           if (equality_comparison_p && const_op == 0
12832               && CONST_INT_P (XEXP (op0, 1)))
12833             {
12834               op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12835                                           XEXP (op0, 0),
12836                                           INTVAL (XEXP (op0, 1)));
12837               continue;
12838             }
12839
12840           /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12841              do the comparison in a narrower mode.  */
12842           if (! unsigned_comparison_p
12843               && CONST_INT_P (XEXP (op0, 1))
12844               && GET_CODE (XEXP (op0, 0)) == ASHIFT
12845               && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12846               && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12847                   .exists (&tmode))
12848               && (((unsigned HOST_WIDE_INT) const_op
12849                    + (GET_MODE_MASK (tmode) >> 1) + 1)
12850                   <= GET_MODE_MASK (tmode)))
12851             {
12852               op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12853               continue;
12854             }
12855
12856           /* Likewise if OP0 is a PLUS of a sign extension with a
12857              constant, which is usually represented with the PLUS
12858              between the shifts.  */
12859           if (! unsigned_comparison_p
12860               && CONST_INT_P (XEXP (op0, 1))
12861               && GET_CODE (XEXP (op0, 0)) == PLUS
12862               && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12863               && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12864               && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12865               && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12866                   .exists (&tmode))
12867               && (((unsigned HOST_WIDE_INT) const_op
12868                    + (GET_MODE_MASK (tmode) >> 1) + 1)
12869                   <= GET_MODE_MASK (tmode)))
12870             {
12871               rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12872               rtx add_const = XEXP (XEXP (op0, 0), 1);
12873               rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12874                                                    add_const, XEXP (op0, 1));
12875
12876               op0 = simplify_gen_binary (PLUS, tmode,
12877                                          gen_lowpart (tmode, inner),
12878                                          new_const);
12879               continue;
12880             }
12881
12882           /* FALLTHROUGH */
12883         case LSHIFTRT:
12884           /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12885              the low order N bits of FOO are known to be zero, we can do this
12886              by comparing FOO with C shifted left N bits so long as no
12887              overflow occurs.  Even if the low order N bits of FOO aren't known
12888              to be zero, if the comparison is >= or < we can use the same
12889              optimization and for > or <= by setting all the low
12890              order N bits in the comparison constant.  */
12891           if (CONST_INT_P (XEXP (op0, 1))
12892               && INTVAL (XEXP (op0, 1)) > 0
12893               && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12894               && mode_width <= HOST_BITS_PER_WIDE_INT
12895               && (((unsigned HOST_WIDE_INT) const_op
12896                    + (GET_CODE (op0) != LSHIFTRT
12897                       ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12898                          + 1)
12899                       : 0))
12900                   <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12901             {
12902               unsigned HOST_WIDE_INT low_bits
12903                 = (nonzero_bits (XEXP (op0, 0), mode)
12904                    & ((HOST_WIDE_INT_1U
12905                        << INTVAL (XEXP (op0, 1))) - 1));
12906               if (low_bits == 0 || !equality_comparison_p)
12907                 {
12908                   /* If the shift was logical, then we must make the condition
12909                      unsigned.  */
12910                   if (GET_CODE (op0) == LSHIFTRT)
12911                     code = unsigned_condition (code);
12912
12913                   const_op = (unsigned HOST_WIDE_INT) const_op
12914                               << INTVAL (XEXP (op0, 1));
12915                   if (low_bits != 0
12916                       && (code == GT || code == GTU
12917                           || code == LE || code == LEU))
12918                     const_op
12919                       |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12920                   op1 = GEN_INT (const_op);
12921                   op0 = XEXP (op0, 0);
12922                   continue;
12923                 }
12924             }
12925
12926           /* If we are using this shift to extract just the sign bit, we
12927              can replace this with an LT or GE comparison.  */
12928           if (const_op == 0
12929               && (equality_comparison_p || sign_bit_comparison_p)
12930               && CONST_INT_P (XEXP (op0, 1))
12931               && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12932             {
12933               op0 = XEXP (op0, 0);
12934               code = (code == NE || code == GT ? LT : GE);
12935               continue;
12936             }
12937           break;
12938
12939         default:
12940           break;
12941         }
12942
12943       break;
12944     }
12945
12946   /* Now make any compound operations involved in this comparison.  Then,
12947      check for an outmost SUBREG on OP0 that is not doing anything or is
12948      paradoxical.  The latter transformation must only be performed when
12949      it is known that the "extra" bits will be the same in op0 and op1 or
12950      that they don't matter.  There are three cases to consider:
12951
12952      1. SUBREG_REG (op0) is a register.  In this case the bits are don't
12953      care bits and we can assume they have any convenient value.  So
12954      making the transformation is safe.
12955
12956      2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12957      In this case the upper bits of op0 are undefined.  We should not make
12958      the simplification in that case as we do not know the contents of
12959      those bits.
12960
12961      3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12962      In that case we know those bits are zeros or ones.  We must also be
12963      sure that they are the same as the upper bits of op1.
12964
12965      We can never remove a SUBREG for a non-equality comparison because
12966      the sign bit is in a different place in the underlying object.  */
12967
12968   rtx_code op0_mco_code = SET;
12969   if (op1 == const0_rtx)
12970     op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12971
12972   op0 = make_compound_operation (op0, op0_mco_code);
12973   op1 = make_compound_operation (op1, SET);
12974
12975   if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12976       && is_int_mode (GET_MODE (op0), &mode)
12977       && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12978       && (code == NE || code == EQ))
12979     {
12980       if (paradoxical_subreg_p (op0))
12981         {
12982           /* For paradoxical subregs, allow case 1 as above.  Case 3 isn't
12983              implemented.  */
12984           if (REG_P (SUBREG_REG (op0)))
12985             {
12986               op0 = SUBREG_REG (op0);
12987               op1 = gen_lowpart (inner_mode, op1);
12988             }
12989         }
12990       else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12991                && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12992                    & ~GET_MODE_MASK (mode)) == 0)
12993         {
12994           tem = gen_lowpart (inner_mode, op1);
12995
12996           if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12997             op0 = SUBREG_REG (op0), op1 = tem;
12998         }
12999     }
13000
13001   /* We now do the opposite procedure: Some machines don't have compare
13002      insns in all modes.  If OP0's mode is an integer mode smaller than a
13003      word and we can't do a compare in that mode, see if there is a larger
13004      mode for which we can do the compare.  There are a number of cases in
13005      which we can use the wider mode.  */
13006
13007   if (is_int_mode (GET_MODE (op0), &mode)
13008       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
13009       && ! have_insn_for (COMPARE, mode))
13010     FOR_EACH_WIDER_MODE (tmode_iter, mode)
13011       {
13012         tmode = tmode_iter.require ();
13013         if (!HWI_COMPUTABLE_MODE_P (tmode))
13014           break;
13015         if (have_insn_for (COMPARE, tmode))
13016           {
13017             int zero_extended;
13018
13019             /* If this is a test for negative, we can make an explicit
13020                test of the sign bit.  Test this first so we can use
13021                a paradoxical subreg to extend OP0.  */
13022
13023             if (op1 == const0_rtx && (code == LT || code == GE)
13024                 && HWI_COMPUTABLE_MODE_P (mode))
13025               {
13026                 unsigned HOST_WIDE_INT sign
13027                   = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
13028                 op0 = simplify_gen_binary (AND, tmode,
13029                                            gen_lowpart (tmode, op0),
13030                                            gen_int_mode (sign, tmode));
13031                 code = (code == LT) ? NE : EQ;
13032                 break;
13033               }
13034
13035             /* If the only nonzero bits in OP0 and OP1 are those in the
13036                narrower mode and this is an equality or unsigned comparison,
13037                we can use the wider mode.  Similarly for sign-extended
13038                values, in which case it is true for all comparisons.  */
13039             zero_extended = ((code == EQ || code == NE
13040                               || code == GEU || code == GTU
13041                               || code == LEU || code == LTU)
13042                              && (nonzero_bits (op0, tmode)
13043                                  & ~GET_MODE_MASK (mode)) == 0
13044                              && ((CONST_INT_P (op1)
13045                                   || (nonzero_bits (op1, tmode)
13046                                       & ~GET_MODE_MASK (mode)) == 0)));
13047
13048             if (zero_extended
13049                 || ((num_sign_bit_copies (op0, tmode)
13050                      > (unsigned int) (GET_MODE_PRECISION (tmode)
13051                                        - GET_MODE_PRECISION (mode)))
13052                     && (num_sign_bit_copies (op1, tmode)
13053                         > (unsigned int) (GET_MODE_PRECISION (tmode)
13054                                           - GET_MODE_PRECISION (mode)))))
13055               {
13056                 /* If OP0 is an AND and we don't have an AND in MODE either,
13057                    make a new AND in the proper mode.  */
13058                 if (GET_CODE (op0) == AND
13059                     && !have_insn_for (AND, mode))
13060                   op0 = simplify_gen_binary (AND, tmode,
13061                                              gen_lowpart (tmode,
13062                                                           XEXP (op0, 0)),
13063                                              gen_lowpart (tmode,
13064                                                           XEXP (op0, 1)));
13065                 else
13066                   {
13067                     if (zero_extended)
13068                       {
13069                         op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
13070                                                   op0, mode);
13071                         op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
13072                                                   op1, mode);
13073                       }
13074                     else
13075                       {
13076                         op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
13077                                                   op0, mode);
13078                         op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
13079                                                   op1, mode);
13080                       }
13081                     break;
13082                   }
13083               }
13084           }
13085       }
13086
13087   /* We may have changed the comparison operands.  Re-canonicalize.  */
13088   if (swap_commutative_operands_p (op0, op1))
13089     {
13090       std::swap (op0, op1);
13091       code = swap_condition (code);
13092     }
13093
13094   /* If this machine only supports a subset of valid comparisons, see if we
13095      can convert an unsupported one into a supported one.  */
13096   target_canonicalize_comparison (&code, &op0, &op1, 0);
13097
13098   *pop0 = op0;
13099   *pop1 = op1;
13100
13101   return code;
13102 }
13103 \f
13104 /* Utility function for record_value_for_reg.  Count number of
13105    rtxs in X.  */
13106 static int
13107 count_rtxs (rtx x)
13108 {
13109   enum rtx_code code = GET_CODE (x);
13110   const char *fmt;
13111   int i, j, ret = 1;
13112
13113   if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13114       || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13115     {
13116       rtx x0 = XEXP (x, 0);
13117       rtx x1 = XEXP (x, 1);
13118
13119       if (x0 == x1)
13120         return 1 + 2 * count_rtxs (x0);
13121
13122       if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13123            || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13124           && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13125         return 2 + 2 * count_rtxs (x0)
13126                + count_rtxs (x == XEXP (x1, 0)
13127                              ? XEXP (x1, 1) : XEXP (x1, 0));
13128
13129       if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13130            || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13131           && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13132         return 2 + 2 * count_rtxs (x1)
13133                + count_rtxs (x == XEXP (x0, 0)
13134                              ? XEXP (x0, 1) : XEXP (x0, 0));
13135     }
13136
13137   fmt = GET_RTX_FORMAT (code);
13138   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13139     if (fmt[i] == 'e')
13140       ret += count_rtxs (XEXP (x, i));
13141     else if (fmt[i] == 'E')
13142       for (j = 0; j < XVECLEN (x, i); j++)
13143         ret += count_rtxs (XVECEXP (x, i, j));
13144
13145   return ret;
13146 }
13147 \f
13148 /* Utility function for following routine.  Called when X is part of a value
13149    being stored into last_set_value.  Sets last_set_table_tick
13150    for each register mentioned.  Similar to mention_regs in cse.c  */
13151
13152 static void
13153 update_table_tick (rtx x)
13154 {
13155   enum rtx_code code = GET_CODE (x);
13156   const char *fmt = GET_RTX_FORMAT (code);
13157   int i, j;
13158
13159   if (code == REG)
13160     {
13161       unsigned int regno = REGNO (x);
13162       unsigned int endregno = END_REGNO (x);
13163       unsigned int r;
13164
13165       for (r = regno; r < endregno; r++)
13166         {
13167           reg_stat_type *rsp = &reg_stat[r];
13168           rsp->last_set_table_tick = label_tick;
13169         }
13170
13171       return;
13172     }
13173
13174   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13175     if (fmt[i] == 'e')
13176       {
13177         /* Check for identical subexpressions.  If x contains
13178            identical subexpression we only have to traverse one of
13179            them.  */
13180         if (i == 0 && ARITHMETIC_P (x))
13181           {
13182             /* Note that at this point x1 has already been
13183                processed.  */
13184             rtx x0 = XEXP (x, 0);
13185             rtx x1 = XEXP (x, 1);
13186
13187             /* If x0 and x1 are identical then there is no need to
13188                process x0.  */
13189             if (x0 == x1)
13190               break;
13191
13192             /* If x0 is identical to a subexpression of x1 then while
13193                processing x1, x0 has already been processed.  Thus we
13194                are done with x.  */
13195             if (ARITHMETIC_P (x1)
13196                 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13197               break;
13198
13199             /* If x1 is identical to a subexpression of x0 then we
13200                still have to process the rest of x0.  */
13201             if (ARITHMETIC_P (x0)
13202                 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13203               {
13204                 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13205                 break;
13206               }
13207           }
13208
13209         update_table_tick (XEXP (x, i));
13210       }
13211     else if (fmt[i] == 'E')
13212       for (j = 0; j < XVECLEN (x, i); j++)
13213         update_table_tick (XVECEXP (x, i, j));
13214 }
13215
13216 /* Record that REG is set to VALUE in insn INSN.  If VALUE is zero, we
13217    are saying that the register is clobbered and we no longer know its
13218    value.  If INSN is zero, don't update reg_stat[].last_set; this is
13219    only permitted with VALUE also zero and is used to invalidate the
13220    register.  */
13221
13222 static void
13223 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13224 {
13225   unsigned int regno = REGNO (reg);
13226   unsigned int endregno = END_REGNO (reg);
13227   unsigned int i;
13228   reg_stat_type *rsp;
13229
13230   /* If VALUE contains REG and we have a previous value for REG, substitute
13231      the previous value.  */
13232   if (value && insn && reg_overlap_mentioned_p (reg, value))
13233     {
13234       rtx tem;
13235
13236       /* Set things up so get_last_value is allowed to see anything set up to
13237          our insn.  */
13238       subst_low_luid = DF_INSN_LUID (insn);
13239       tem = get_last_value (reg);
13240
13241       /* If TEM is simply a binary operation with two CLOBBERs as operands,
13242          it isn't going to be useful and will take a lot of time to process,
13243          so just use the CLOBBER.  */
13244
13245       if (tem)
13246         {
13247           if (ARITHMETIC_P (tem)
13248               && GET_CODE (XEXP (tem, 0)) == CLOBBER
13249               && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13250             tem = XEXP (tem, 0);
13251           else if (count_occurrences (value, reg, 1) >= 2)
13252             {
13253               /* If there are two or more occurrences of REG in VALUE,
13254                  prevent the value from growing too much.  */
13255               if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13256                 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13257             }
13258
13259           value = replace_rtx (copy_rtx (value), reg, tem);
13260         }
13261     }
13262
13263   /* For each register modified, show we don't know its value, that
13264      we don't know about its bitwise content, that its value has been
13265      updated, and that we don't know the location of the death of the
13266      register.  */
13267   for (i = regno; i < endregno; i++)
13268     {
13269       rsp = &reg_stat[i];
13270
13271       if (insn)
13272         rsp->last_set = insn;
13273
13274       rsp->last_set_value = 0;
13275       rsp->last_set_mode = VOIDmode;
13276       rsp->last_set_nonzero_bits = 0;
13277       rsp->last_set_sign_bit_copies = 0;
13278       rsp->last_death = 0;
13279       rsp->truncated_to_mode = VOIDmode;
13280     }
13281
13282   /* Mark registers that are being referenced in this value.  */
13283   if (value)
13284     update_table_tick (value);
13285
13286   /* Now update the status of each register being set.
13287      If someone is using this register in this block, set this register
13288      to invalid since we will get confused between the two lives in this
13289      basic block.  This makes using this register always invalid.  In cse, we
13290      scan the table to invalidate all entries using this register, but this
13291      is too much work for us.  */
13292
13293   for (i = regno; i < endregno; i++)
13294     {
13295       rsp = &reg_stat[i];
13296       rsp->last_set_label = label_tick;
13297       if (!insn
13298           || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13299         rsp->last_set_invalid = 1;
13300       else
13301         rsp->last_set_invalid = 0;
13302     }
13303
13304   /* The value being assigned might refer to X (like in "x++;").  In that
13305      case, we must replace it with (clobber (const_int 0)) to prevent
13306      infinite loops.  */
13307   rsp = &reg_stat[regno];
13308   if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13309     {
13310       value = copy_rtx (value);
13311       if (!get_last_value_validate (&value, insn, label_tick, 1))
13312         value = 0;
13313     }
13314
13315   /* For the main register being modified, update the value, the mode, the
13316      nonzero bits, and the number of sign bit copies.  */
13317
13318   rsp->last_set_value = value;
13319
13320   if (value)
13321     {
13322       machine_mode mode = GET_MODE (reg);
13323       subst_low_luid = DF_INSN_LUID (insn);
13324       rsp->last_set_mode = mode;
13325       if (GET_MODE_CLASS (mode) == MODE_INT
13326           && HWI_COMPUTABLE_MODE_P (mode))
13327         mode = nonzero_bits_mode;
13328       rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13329       rsp->last_set_sign_bit_copies
13330         = num_sign_bit_copies (value, GET_MODE (reg));
13331     }
13332 }
13333
13334 /* Called via note_stores from record_dead_and_set_regs to handle one
13335    SET or CLOBBER in an insn.  DATA is the instruction in which the
13336    set is occurring.  */
13337
13338 static void
13339 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13340 {
13341   rtx_insn *record_dead_insn = (rtx_insn *) data;
13342
13343   if (GET_CODE (dest) == SUBREG)
13344     dest = SUBREG_REG (dest);
13345
13346   if (!record_dead_insn)
13347     {
13348       if (REG_P (dest))
13349         record_value_for_reg (dest, NULL, NULL_RTX);
13350       return;
13351     }
13352
13353   if (REG_P (dest))
13354     {
13355       /* If we are setting the whole register, we know its value.  Otherwise
13356          show that we don't know the value.  We can handle a SUBREG if it's
13357          the low part, but we must be careful with paradoxical SUBREGs on
13358          RISC architectures because we cannot strip e.g. an extension around
13359          a load and record the naked load since the RTL middle-end considers
13360          that the upper bits are defined according to LOAD_EXTEND_OP.  */
13361       if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13362         record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13363       else if (GET_CODE (setter) == SET
13364                && GET_CODE (SET_DEST (setter)) == SUBREG
13365                && SUBREG_REG (SET_DEST (setter)) == dest
13366                && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13367                             BITS_PER_WORD)
13368                && subreg_lowpart_p (SET_DEST (setter)))
13369         record_value_for_reg (dest, record_dead_insn,
13370                               WORD_REGISTER_OPERATIONS
13371                               && word_register_operation_p (SET_SRC (setter))
13372                               && paradoxical_subreg_p (SET_DEST (setter))
13373                               ? SET_SRC (setter)
13374                               : gen_lowpart (GET_MODE (dest),
13375                                              SET_SRC (setter)));
13376       else if (GET_CODE (setter) == CLOBBER_HIGH)
13377         {
13378           reg_stat_type *rsp = &reg_stat[REGNO (dest)];
13379           if (rsp->last_set_value
13380               && reg_is_clobbered_by_clobber_high
13381                    (REGNO (dest), GET_MODE (rsp->last_set_value),
13382                     XEXP (setter, 0)))
13383             record_value_for_reg (dest, NULL, NULL_RTX);
13384         }
13385       else
13386         record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13387     }
13388   else if (MEM_P (dest)
13389            /* Ignore pushes, they clobber nothing.  */
13390            && ! push_operand (dest, GET_MODE (dest)))
13391     mem_last_set = DF_INSN_LUID (record_dead_insn);
13392 }
13393
13394 /* Update the records of when each REG was most recently set or killed
13395    for the things done by INSN.  This is the last thing done in processing
13396    INSN in the combiner loop.
13397
13398    We update reg_stat[], in particular fields last_set, last_set_value,
13399    last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13400    last_death, and also the similar information mem_last_set (which insn
13401    most recently modified memory) and last_call_luid (which insn was the
13402    most recent subroutine call).  */
13403
13404 static void
13405 record_dead_and_set_regs (rtx_insn *insn)
13406 {
13407   rtx link;
13408   unsigned int i;
13409
13410   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13411     {
13412       if (REG_NOTE_KIND (link) == REG_DEAD
13413           && REG_P (XEXP (link, 0)))
13414         {
13415           unsigned int regno = REGNO (XEXP (link, 0));
13416           unsigned int endregno = END_REGNO (XEXP (link, 0));
13417
13418           for (i = regno; i < endregno; i++)
13419             {
13420               reg_stat_type *rsp;
13421
13422               rsp = &reg_stat[i];
13423               rsp->last_death = insn;
13424             }
13425         }
13426       else if (REG_NOTE_KIND (link) == REG_INC)
13427         record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13428     }
13429
13430   if (CALL_P (insn))
13431     {
13432       hard_reg_set_iterator hrsi;
13433       EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13434         {
13435           reg_stat_type *rsp;
13436
13437           rsp = &reg_stat[i];
13438           rsp->last_set_invalid = 1;
13439           rsp->last_set = insn;
13440           rsp->last_set_value = 0;
13441           rsp->last_set_mode = VOIDmode;
13442           rsp->last_set_nonzero_bits = 0;
13443           rsp->last_set_sign_bit_copies = 0;
13444           rsp->last_death = 0;
13445           rsp->truncated_to_mode = VOIDmode;
13446         }
13447
13448       last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13449
13450       /* We can't combine into a call pattern.  Remember, though, that
13451          the return value register is set at this LUID.  We could
13452          still replace a register with the return value from the
13453          wrong subroutine call!  */
13454       note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13455     }
13456   else
13457     note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13458 }
13459
13460 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13461    register present in the SUBREG, so for each such SUBREG go back and
13462    adjust nonzero and sign bit information of the registers that are
13463    known to have some zero/sign bits set.
13464
13465    This is needed because when combine blows the SUBREGs away, the
13466    information on zero/sign bits is lost and further combines can be
13467    missed because of that.  */
13468
13469 static void
13470 record_promoted_value (rtx_insn *insn, rtx subreg)
13471 {
13472   struct insn_link *links;
13473   rtx set;
13474   unsigned int regno = REGNO (SUBREG_REG (subreg));
13475   machine_mode mode = GET_MODE (subreg);
13476
13477   if (!HWI_COMPUTABLE_MODE_P (mode))
13478     return;
13479
13480   for (links = LOG_LINKS (insn); links;)
13481     {
13482       reg_stat_type *rsp;
13483
13484       insn = links->insn;
13485       set = single_set (insn);
13486
13487       if (! set || !REG_P (SET_DEST (set))
13488           || REGNO (SET_DEST (set)) != regno
13489           || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13490         {
13491           links = links->next;
13492           continue;
13493         }
13494
13495       rsp = &reg_stat[regno];
13496       if (rsp->last_set == insn)
13497         {
13498           if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13499             rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13500         }
13501
13502       if (REG_P (SET_SRC (set)))
13503         {
13504           regno = REGNO (SET_SRC (set));
13505           links = LOG_LINKS (insn);
13506         }
13507       else
13508         break;
13509     }
13510 }
13511
13512 /* Check if X, a register, is known to contain a value already
13513    truncated to MODE.  In this case we can use a subreg to refer to
13514    the truncated value even though in the generic case we would need
13515    an explicit truncation.  */
13516
13517 static bool
13518 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13519 {
13520   reg_stat_type *rsp = &reg_stat[REGNO (x)];
13521   machine_mode truncated = rsp->truncated_to_mode;
13522
13523   if (truncated == 0
13524       || rsp->truncation_label < label_tick_ebb_start)
13525     return false;
13526   if (!partial_subreg_p (mode, truncated))
13527     return true;
13528   if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13529     return true;
13530   return false;
13531 }
13532
13533 /* If X is a hard reg or a subreg record the mode that the register is
13534    accessed in.  For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13535    able to turn a truncate into a subreg using this information.  Return true
13536    if traversing X is complete.  */
13537
13538 static bool
13539 record_truncated_value (rtx x)
13540 {
13541   machine_mode truncated_mode;
13542   reg_stat_type *rsp;
13543
13544   if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13545     {
13546       machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13547       truncated_mode = GET_MODE (x);
13548
13549       if (!partial_subreg_p (truncated_mode, original_mode))
13550         return true;
13551
13552       truncated_mode = GET_MODE (x);
13553       if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13554         return true;
13555
13556       x = SUBREG_REG (x);
13557     }
13558   /* ??? For hard-regs we now record everything.  We might be able to
13559      optimize this using last_set_mode.  */
13560   else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13561     truncated_mode = GET_MODE (x);
13562   else
13563     return false;
13564
13565   rsp = &reg_stat[REGNO (x)];
13566   if (rsp->truncated_to_mode == 0
13567       || rsp->truncation_label < label_tick_ebb_start
13568       || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13569     {
13570       rsp->truncated_to_mode = truncated_mode;
13571       rsp->truncation_label = label_tick;
13572     }
13573
13574   return true;
13575 }
13576
13577 /* Callback for note_uses.  Find hardregs and subregs of pseudos and
13578    the modes they are used in.  This can help truning TRUNCATEs into
13579    SUBREGs.  */
13580
13581 static void
13582 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13583 {
13584   subrtx_var_iterator::array_type array;
13585   FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13586     if (record_truncated_value (*iter))
13587       iter.skip_subrtxes ();
13588 }
13589
13590 /* Scan X for promoted SUBREGs.  For each one found,
13591    note what it implies to the registers used in it.  */
13592
13593 static void
13594 check_promoted_subreg (rtx_insn *insn, rtx x)
13595 {
13596   if (GET_CODE (x) == SUBREG
13597       && SUBREG_PROMOTED_VAR_P (x)
13598       && REG_P (SUBREG_REG (x)))
13599     record_promoted_value (insn, x);
13600   else
13601     {
13602       const char *format = GET_RTX_FORMAT (GET_CODE (x));
13603       int i, j;
13604
13605       for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13606         switch (format[i])
13607           {
13608           case 'e':
13609             check_promoted_subreg (insn, XEXP (x, i));
13610             break;
13611           case 'V':
13612           case 'E':
13613             if (XVEC (x, i) != 0)
13614               for (j = 0; j < XVECLEN (x, i); j++)
13615                 check_promoted_subreg (insn, XVECEXP (x, i, j));
13616             break;
13617           }
13618     }
13619 }
13620 \f
13621 /* Verify that all the registers and memory references mentioned in *LOC are
13622    still valid.  *LOC was part of a value set in INSN when label_tick was
13623    equal to TICK.  Return 0 if some are not.  If REPLACE is nonzero, replace
13624    the invalid references with (clobber (const_int 0)) and return 1.  This
13625    replacement is useful because we often can get useful information about
13626    the form of a value (e.g., if it was produced by a shift that always
13627    produces -1 or 0) even though we don't know exactly what registers it
13628    was produced from.  */
13629
13630 static int
13631 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13632 {
13633   rtx x = *loc;
13634   const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13635   int len = GET_RTX_LENGTH (GET_CODE (x));
13636   int i, j;
13637
13638   if (REG_P (x))
13639     {
13640       unsigned int regno = REGNO (x);
13641       unsigned int endregno = END_REGNO (x);
13642       unsigned int j;
13643
13644       for (j = regno; j < endregno; j++)
13645         {
13646           reg_stat_type *rsp = &reg_stat[j];
13647           if (rsp->last_set_invalid
13648               /* If this is a pseudo-register that was only set once and not
13649                  live at the beginning of the function, it is always valid.  */
13650               || (! (regno >= FIRST_PSEUDO_REGISTER
13651                      && regno < reg_n_sets_max
13652                      && REG_N_SETS (regno) == 1
13653                      && (!REGNO_REG_SET_P
13654                          (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13655                           regno)))
13656                   && rsp->last_set_label > tick))
13657           {
13658             if (replace)
13659               *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13660             return replace;
13661           }
13662         }
13663
13664       return 1;
13665     }
13666   /* If this is a memory reference, make sure that there were no stores after
13667      it that might have clobbered the value.  We don't have alias info, so we
13668      assume any store invalidates it.  Moreover, we only have local UIDs, so
13669      we also assume that there were stores in the intervening basic blocks.  */
13670   else if (MEM_P (x) && !MEM_READONLY_P (x)
13671            && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13672     {
13673       if (replace)
13674         *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13675       return replace;
13676     }
13677
13678   for (i = 0; i < len; i++)
13679     {
13680       if (fmt[i] == 'e')
13681         {
13682           /* Check for identical subexpressions.  If x contains
13683              identical subexpression we only have to traverse one of
13684              them.  */
13685           if (i == 1 && ARITHMETIC_P (x))
13686             {
13687               /* Note that at this point x0 has already been checked
13688                  and found valid.  */
13689               rtx x0 = XEXP (x, 0);
13690               rtx x1 = XEXP (x, 1);
13691
13692               /* If x0 and x1 are identical then x is also valid.  */
13693               if (x0 == x1)
13694                 return 1;
13695
13696               /* If x1 is identical to a subexpression of x0 then
13697                  while checking x0, x1 has already been checked.  Thus
13698                  it is valid and so as x.  */
13699               if (ARITHMETIC_P (x0)
13700                   && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13701                 return 1;
13702
13703               /* If x0 is identical to a subexpression of x1 then x is
13704                  valid iff the rest of x1 is valid.  */
13705               if (ARITHMETIC_P (x1)
13706                   && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13707                 return
13708                   get_last_value_validate (&XEXP (x1,
13709                                                   x0 == XEXP (x1, 0) ? 1 : 0),
13710                                            insn, tick, replace);
13711             }
13712
13713           if (get_last_value_validate (&XEXP (x, i), insn, tick,
13714                                        replace) == 0)
13715             return 0;
13716         }
13717       else if (fmt[i] == 'E')
13718         for (j = 0; j < XVECLEN (x, i); j++)
13719           if (get_last_value_validate (&XVECEXP (x, i, j),
13720                                        insn, tick, replace) == 0)
13721             return 0;
13722     }
13723
13724   /* If we haven't found a reason for it to be invalid, it is valid.  */
13725   return 1;
13726 }
13727
13728 /* Get the last value assigned to X, if known.  Some registers
13729    in the value may be replaced with (clobber (const_int 0)) if their value
13730    is known longer known reliably.  */
13731
13732 static rtx
13733 get_last_value (const_rtx x)
13734 {
13735   unsigned int regno;
13736   rtx value;
13737   reg_stat_type *rsp;
13738
13739   /* If this is a non-paradoxical SUBREG, get the value of its operand and
13740      then convert it to the desired mode.  If this is a paradoxical SUBREG,
13741      we cannot predict what values the "extra" bits might have.  */
13742   if (GET_CODE (x) == SUBREG
13743       && subreg_lowpart_p (x)
13744       && !paradoxical_subreg_p (x)
13745       && (value = get_last_value (SUBREG_REG (x))) != 0)
13746     return gen_lowpart (GET_MODE (x), value);
13747
13748   if (!REG_P (x))
13749     return 0;
13750
13751   regno = REGNO (x);
13752   rsp = &reg_stat[regno];
13753   value = rsp->last_set_value;
13754
13755   /* If we don't have a value, or if it isn't for this basic block and
13756      it's either a hard register, set more than once, or it's a live
13757      at the beginning of the function, return 0.
13758
13759      Because if it's not live at the beginning of the function then the reg
13760      is always set before being used (is never used without being set).
13761      And, if it's set only once, and it's always set before use, then all
13762      uses must have the same last value, even if it's not from this basic
13763      block.  */
13764
13765   if (value == 0
13766       || (rsp->last_set_label < label_tick_ebb_start
13767           && (regno < FIRST_PSEUDO_REGISTER
13768               || regno >= reg_n_sets_max
13769               || REG_N_SETS (regno) != 1
13770               || REGNO_REG_SET_P
13771                  (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13772     return 0;
13773
13774   /* If the value was set in a later insn than the ones we are processing,
13775      we can't use it even if the register was only set once.  */
13776   if (rsp->last_set_label == label_tick
13777       && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13778     return 0;
13779
13780   /* If fewer bits were set than what we are asked for now, we cannot use
13781      the value.  */
13782   if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13783                 GET_MODE_PRECISION (GET_MODE (x))))
13784     return 0;
13785
13786   /* If the value has all its registers valid, return it.  */
13787   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13788     return value;
13789
13790   /* Otherwise, make a copy and replace any invalid register with
13791      (clobber (const_int 0)).  If that fails for some reason, return 0.  */
13792
13793   value = copy_rtx (value);
13794   if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13795     return value;
13796
13797   return 0;
13798 }
13799 \f
13800 /* Define three variables used for communication between the following
13801    routines.  */
13802
13803 static unsigned int reg_dead_regno, reg_dead_endregno;
13804 static int reg_dead_flag;
13805 rtx reg_dead_reg;
13806
13807 /* Function called via note_stores from reg_dead_at_p.
13808
13809    If DEST is within [reg_dead_regno, reg_dead_endregno), set
13810    reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET.  */
13811
13812 static void
13813 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13814 {
13815   unsigned int regno, endregno;
13816
13817   if (!REG_P (dest))
13818     return;
13819
13820   if (GET_CODE (x) == CLOBBER_HIGH
13821       && !reg_is_clobbered_by_clobber_high (reg_dead_reg, XEXP (x, 0)))
13822     return;
13823
13824   regno = REGNO (dest);
13825   endregno = END_REGNO (dest);
13826   if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13827     reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13828 }
13829
13830 /* Return nonzero if REG is known to be dead at INSN.
13831
13832    We scan backwards from INSN.  If we hit a REG_DEAD note or a CLOBBER
13833    referencing REG, it is dead.  If we hit a SET referencing REG, it is
13834    live.  Otherwise, see if it is live or dead at the start of the basic
13835    block we are in.  Hard regs marked as being live in NEWPAT_USED_REGS
13836    must be assumed to be always live.  */
13837
13838 static int
13839 reg_dead_at_p (rtx reg, rtx_insn *insn)
13840 {
13841   basic_block block;
13842   unsigned int i;
13843
13844   /* Set variables for reg_dead_at_p_1.  */
13845   reg_dead_regno = REGNO (reg);
13846   reg_dead_endregno = END_REGNO (reg);
13847   reg_dead_reg = reg;
13848
13849   reg_dead_flag = 0;
13850
13851   /* Check that reg isn't mentioned in NEWPAT_USED_REGS.  For fixed registers
13852      we allow the machine description to decide whether use-and-clobber
13853      patterns are OK.  */
13854   if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13855     {
13856       for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13857         if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13858           return 0;
13859     }
13860
13861   /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13862      beginning of basic block.  */
13863   block = BLOCK_FOR_INSN (insn);
13864   for (;;)
13865     {
13866       if (INSN_P (insn))
13867         {
13868           if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13869             return 1;
13870
13871           note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13872           if (reg_dead_flag)
13873             return reg_dead_flag == 1 ? 1 : 0;
13874
13875           if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13876             return 1;
13877         }
13878
13879       if (insn == BB_HEAD (block))
13880         break;
13881
13882       insn = PREV_INSN (insn);
13883     }
13884
13885   /* Look at live-in sets for the basic block that we were in.  */
13886   for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13887     if (REGNO_REG_SET_P (df_get_live_in (block), i))
13888       return 0;
13889
13890   return 1;
13891 }
13892 \f
13893 /* Note hard registers in X that are used.  */
13894
13895 static void
13896 mark_used_regs_combine (rtx x)
13897 {
13898   RTX_CODE code = GET_CODE (x);
13899   unsigned int regno;
13900   int i;
13901
13902   switch (code)
13903     {
13904     case LABEL_REF:
13905     case SYMBOL_REF:
13906     case CONST:
13907     CASE_CONST_ANY:
13908     case PC:
13909     case ADDR_VEC:
13910     case ADDR_DIFF_VEC:
13911     case ASM_INPUT:
13912     /* CC0 must die in the insn after it is set, so we don't need to take
13913        special note of it here.  */
13914     case CC0:
13915       return;
13916
13917     case CLOBBER:
13918       /* If we are clobbering a MEM, mark any hard registers inside the
13919          address as used.  */
13920       if (MEM_P (XEXP (x, 0)))
13921         mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13922       return;
13923
13924     case REG:
13925       regno = REGNO (x);
13926       /* A hard reg in a wide mode may really be multiple registers.
13927          If so, mark all of them just like the first.  */
13928       if (regno < FIRST_PSEUDO_REGISTER)
13929         {
13930           /* None of this applies to the stack, frame or arg pointers.  */
13931           if (regno == STACK_POINTER_REGNUM
13932               || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13933                   && regno == HARD_FRAME_POINTER_REGNUM)
13934               || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13935                   && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13936               || regno == FRAME_POINTER_REGNUM)
13937             return;
13938
13939           add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13940         }
13941       return;
13942
13943     case SET:
13944       {
13945         /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13946            the address.  */
13947         rtx testreg = SET_DEST (x);
13948
13949         while (GET_CODE (testreg) == SUBREG
13950                || GET_CODE (testreg) == ZERO_EXTRACT
13951                || GET_CODE (testreg) == STRICT_LOW_PART)
13952           testreg = XEXP (testreg, 0);
13953
13954         if (MEM_P (testreg))
13955           mark_used_regs_combine (XEXP (testreg, 0));
13956
13957         mark_used_regs_combine (SET_SRC (x));
13958       }
13959       return;
13960
13961     default:
13962       break;
13963     }
13964
13965   /* Recursively scan the operands of this expression.  */
13966
13967   {
13968     const char *fmt = GET_RTX_FORMAT (code);
13969
13970     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13971       {
13972         if (fmt[i] == 'e')
13973           mark_used_regs_combine (XEXP (x, i));
13974         else if (fmt[i] == 'E')
13975           {
13976             int j;
13977
13978             for (j = 0; j < XVECLEN (x, i); j++)
13979               mark_used_regs_combine (XVECEXP (x, i, j));
13980           }
13981       }
13982   }
13983 }
13984 \f
13985 /* Remove register number REGNO from the dead registers list of INSN.
13986
13987    Return the note used to record the death, if there was one.  */
13988
13989 rtx
13990 remove_death (unsigned int regno, rtx_insn *insn)
13991 {
13992   rtx note = find_regno_note (insn, REG_DEAD, regno);
13993
13994   if (note)
13995     remove_note (insn, note);
13996
13997   return note;
13998 }
13999
14000 /* For each register (hardware or pseudo) used within expression X, if its
14001    death is in an instruction with luid between FROM_LUID (inclusive) and
14002    TO_INSN (exclusive), put a REG_DEAD note for that register in the
14003    list headed by PNOTES.
14004
14005    That said, don't move registers killed by maybe_kill_insn.
14006
14007    This is done when X is being merged by combination into TO_INSN.  These
14008    notes will then be distributed as needed.  */
14009
14010 static void
14011 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
14012              rtx *pnotes)
14013 {
14014   const char *fmt;
14015   int len, i;
14016   enum rtx_code code = GET_CODE (x);
14017
14018   if (code == REG)
14019     {
14020       unsigned int regno = REGNO (x);
14021       rtx_insn *where_dead = reg_stat[regno].last_death;
14022
14023       /* If we do not know where the register died, it may still die between
14024          FROM_LUID and TO_INSN.  If so, find it.  This is PR83304.  */
14025       if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
14026         {
14027           rtx_insn *insn = prev_real_nondebug_insn (to_insn);
14028           while (insn
14029                  && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
14030                  && DF_INSN_LUID (insn) >= from_luid)
14031             {
14032               if (dead_or_set_regno_p (insn, regno))
14033                 {
14034                   if (find_regno_note (insn, REG_DEAD, regno))
14035                     where_dead = insn;
14036                   break;
14037                 }
14038
14039               insn = prev_real_nondebug_insn (insn);
14040             }
14041         }
14042
14043       /* Don't move the register if it gets killed in between from and to.  */
14044       if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
14045           && ! reg_referenced_p (x, maybe_kill_insn))
14046         return;
14047
14048       if (where_dead
14049           && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
14050           && DF_INSN_LUID (where_dead) >= from_luid
14051           && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
14052         {
14053           rtx note = remove_death (regno, where_dead);
14054
14055           /* It is possible for the call above to return 0.  This can occur
14056              when last_death points to I2 or I1 that we combined with.
14057              In that case make a new note.
14058
14059              We must also check for the case where X is a hard register
14060              and NOTE is a death note for a range of hard registers
14061              including X.  In that case, we must put REG_DEAD notes for
14062              the remaining registers in place of NOTE.  */
14063
14064           if (note != 0 && regno < FIRST_PSEUDO_REGISTER
14065               && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
14066             {
14067               unsigned int deadregno = REGNO (XEXP (note, 0));
14068               unsigned int deadend = END_REGNO (XEXP (note, 0));
14069               unsigned int ourend = END_REGNO (x);
14070               unsigned int i;
14071
14072               for (i = deadregno; i < deadend; i++)
14073                 if (i < regno || i >= ourend)
14074                   add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
14075             }
14076
14077           /* If we didn't find any note, or if we found a REG_DEAD note that
14078              covers only part of the given reg, and we have a multi-reg hard
14079              register, then to be safe we must check for REG_DEAD notes
14080              for each register other than the first.  They could have
14081              their own REG_DEAD notes lying around.  */
14082           else if ((note == 0
14083                     || (note != 0
14084                         && partial_subreg_p (GET_MODE (XEXP (note, 0)),
14085                                              GET_MODE (x))))
14086                    && regno < FIRST_PSEUDO_REGISTER
14087                    && REG_NREGS (x) > 1)
14088             {
14089               unsigned int ourend = END_REGNO (x);
14090               unsigned int i, offset;
14091               rtx oldnotes = 0;
14092
14093               if (note)
14094                 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
14095               else
14096                 offset = 1;
14097
14098               for (i = regno + offset; i < ourend; i++)
14099                 move_deaths (regno_reg_rtx[i],
14100                              maybe_kill_insn, from_luid, to_insn, &oldnotes);
14101             }
14102
14103           if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14104             {
14105               XEXP (note, 1) = *pnotes;
14106               *pnotes = note;
14107             }
14108           else
14109             *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14110         }
14111
14112       return;
14113     }
14114
14115   else if (GET_CODE (x) == SET)
14116     {
14117       rtx dest = SET_DEST (x);
14118
14119       move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14120
14121       /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14122          that accesses one word of a multi-word item, some
14123          piece of everything register in the expression is used by
14124          this insn, so remove any old death.  */
14125       /* ??? So why do we test for equality of the sizes?  */
14126
14127       if (GET_CODE (dest) == ZERO_EXTRACT
14128           || GET_CODE (dest) == STRICT_LOW_PART
14129           || (GET_CODE (dest) == SUBREG
14130               && !read_modify_subreg_p (dest)))
14131         {
14132           move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14133           return;
14134         }
14135
14136       /* If this is some other SUBREG, we know it replaces the entire
14137          value, so use that as the destination.  */
14138       if (GET_CODE (dest) == SUBREG)
14139         dest = SUBREG_REG (dest);
14140
14141       /* If this is a MEM, adjust deaths of anything used in the address.
14142          For a REG (the only other possibility), the entire value is
14143          being replaced so the old value is not used in this insn.  */
14144
14145       if (MEM_P (dest))
14146         move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14147                      to_insn, pnotes);
14148       return;
14149     }
14150
14151   else if (GET_CODE (x) == CLOBBER)
14152     return;
14153
14154   len = GET_RTX_LENGTH (code);
14155   fmt = GET_RTX_FORMAT (code);
14156
14157   for (i = 0; i < len; i++)
14158     {
14159       if (fmt[i] == 'E')
14160         {
14161           int j;
14162           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14163             move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14164                          to_insn, pnotes);
14165         }
14166       else if (fmt[i] == 'e')
14167         move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14168     }
14169 }
14170 \f
14171 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14172    pattern of an insn.  X must be a REG.  */
14173
14174 static int
14175 reg_bitfield_target_p (rtx x, rtx body)
14176 {
14177   int i;
14178
14179   if (GET_CODE (body) == SET)
14180     {
14181       rtx dest = SET_DEST (body);
14182       rtx target;
14183       unsigned int regno, tregno, endregno, endtregno;
14184
14185       if (GET_CODE (dest) == ZERO_EXTRACT)
14186         target = XEXP (dest, 0);
14187       else if (GET_CODE (dest) == STRICT_LOW_PART)
14188         target = SUBREG_REG (XEXP (dest, 0));
14189       else
14190         return 0;
14191
14192       if (GET_CODE (target) == SUBREG)
14193         target = SUBREG_REG (target);
14194
14195       if (!REG_P (target))
14196         return 0;
14197
14198       tregno = REGNO (target), regno = REGNO (x);
14199       if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14200         return target == x;
14201
14202       endtregno = end_hard_regno (GET_MODE (target), tregno);
14203       endregno = end_hard_regno (GET_MODE (x), regno);
14204
14205       return endregno > tregno && regno < endtregno;
14206     }
14207
14208   else if (GET_CODE (body) == PARALLEL)
14209     for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14210       if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14211         return 1;
14212
14213   return 0;
14214 }
14215 \f
14216 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14217    as appropriate.  I3 and I2 are the insns resulting from the combination
14218    insns including FROM (I2 may be zero).
14219
14220    ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14221    not need REG_DEAD notes because they are being substituted for.  This
14222    saves searching in the most common cases.
14223
14224    Each note in the list is either ignored or placed on some insns, depending
14225    on the type of note.  */
14226
14227 static void
14228 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14229                   rtx elim_i2, rtx elim_i1, rtx elim_i0)
14230 {
14231   rtx note, next_note;
14232   rtx tem_note;
14233   rtx_insn *tem_insn;
14234
14235   for (note = notes; note; note = next_note)
14236     {
14237       rtx_insn *place = 0, *place2 = 0;
14238
14239       next_note = XEXP (note, 1);
14240       switch (REG_NOTE_KIND (note))
14241         {
14242         case REG_BR_PROB:
14243         case REG_BR_PRED:
14244           /* Doesn't matter much where we put this, as long as it's somewhere.
14245              It is preferable to keep these notes on branches, which is most
14246              likely to be i3.  */
14247           place = i3;
14248           break;
14249
14250         case REG_NON_LOCAL_GOTO:
14251           if (JUMP_P (i3))
14252             place = i3;
14253           else
14254             {
14255               gcc_assert (i2 && JUMP_P (i2));
14256               place = i2;
14257             }
14258           break;
14259
14260         case REG_EH_REGION:
14261           /* These notes must remain with the call or trapping instruction.  */
14262           if (CALL_P (i3))
14263             place = i3;
14264           else if (i2 && CALL_P (i2))
14265             place = i2;
14266           else
14267             {
14268               gcc_assert (cfun->can_throw_non_call_exceptions);
14269               if (may_trap_p (i3))
14270                 place = i3;
14271               else if (i2 && may_trap_p (i2))
14272                 place = i2;
14273               /* ??? Otherwise assume we've combined things such that we
14274                  can now prove that the instructions can't trap.  Drop the
14275                  note in this case.  */
14276             }
14277           break;
14278
14279         case REG_ARGS_SIZE:
14280           /* ??? How to distribute between i3-i1.  Assume i3 contains the
14281              entire adjustment.  Assert i3 contains at least some adjust.  */
14282           if (!noop_move_p (i3))
14283             {
14284               poly_int64 old_size, args_size = get_args_size (note);
14285               /* fixup_args_size_notes looks at REG_NORETURN note,
14286                  so ensure the note is placed there first.  */
14287               if (CALL_P (i3))
14288                 {
14289                   rtx *np;
14290                   for (np = &next_note; *np; np = &XEXP (*np, 1))
14291                     if (REG_NOTE_KIND (*np) == REG_NORETURN)
14292                       {
14293                         rtx n = *np;
14294                         *np = XEXP (n, 1);
14295                         XEXP (n, 1) = REG_NOTES (i3);
14296                         REG_NOTES (i3) = n;
14297                         break;
14298                       }
14299                 }
14300               old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14301               /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14302                  REG_ARGS_SIZE note to all noreturn calls, allow that here.  */
14303               gcc_assert (maybe_ne (old_size, args_size)
14304                           || (CALL_P (i3)
14305                               && !ACCUMULATE_OUTGOING_ARGS
14306                               && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14307             }
14308           break;
14309
14310         case REG_NORETURN:
14311         case REG_SETJMP:
14312         case REG_TM:
14313         case REG_CALL_DECL:
14314         case REG_CALL_NOCF_CHECK:
14315           /* These notes must remain with the call.  It should not be
14316              possible for both I2 and I3 to be a call.  */
14317           if (CALL_P (i3))
14318             place = i3;
14319           else
14320             {
14321               gcc_assert (i2 && CALL_P (i2));
14322               place = i2;
14323             }
14324           break;
14325
14326         case REG_UNUSED:
14327           /* Any clobbers for i3 may still exist, and so we must process
14328              REG_UNUSED notes from that insn.
14329
14330              Any clobbers from i2 or i1 can only exist if they were added by
14331              recog_for_combine.  In that case, recog_for_combine created the
14332              necessary REG_UNUSED notes.  Trying to keep any original
14333              REG_UNUSED notes from these insns can cause incorrect output
14334              if it is for the same register as the original i3 dest.
14335              In that case, we will notice that the register is set in i3,
14336              and then add a REG_UNUSED note for the destination of i3, which
14337              is wrong.  However, it is possible to have REG_UNUSED notes from
14338              i2 or i1 for register which were both used and clobbered, so
14339              we keep notes from i2 or i1 if they will turn into REG_DEAD
14340              notes.  */
14341
14342           /* If this register is set or clobbered in I3, put the note there
14343              unless there is one already.  */
14344           if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14345             {
14346               if (from_insn != i3)
14347                 break;
14348
14349               if (! (REG_P (XEXP (note, 0))
14350                      ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14351                      : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14352                 place = i3;
14353             }
14354           /* Otherwise, if this register is used by I3, then this register
14355              now dies here, so we must put a REG_DEAD note here unless there
14356              is one already.  */
14357           else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14358                    && ! (REG_P (XEXP (note, 0))
14359                          ? find_regno_note (i3, REG_DEAD,
14360                                             REGNO (XEXP (note, 0)))
14361                          : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14362             {
14363               PUT_REG_NOTE_KIND (note, REG_DEAD);
14364               place = i3;
14365             }
14366
14367           /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14368              but we can't tell which at this point.  We must reset any
14369              expectations we had about the value that was previously
14370              stored in the reg.  ??? Ideally, we'd adjust REG_N_SETS
14371              and, if appropriate, restore its previous value, but we
14372              don't have enough information for that at this point.  */
14373           else
14374             {
14375               record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14376
14377               /* Otherwise, if this register is now referenced in i2
14378                  then the register used to be modified in one of the
14379                  original insns.  If it was i3 (say, in an unused
14380                  parallel), it's now completely gone, so the note can
14381                  be discarded.  But if it was modified in i2, i1 or i0
14382                  and we still reference it in i2, then we're
14383                  referencing the previous value, and since the
14384                  register was modified and REG_UNUSED, we know that
14385                  the previous value is now dead.  So, if we only
14386                  reference the register in i2, we change the note to
14387                  REG_DEAD, to reflect the previous value.  However, if
14388                  we're also setting or clobbering the register as
14389                  scratch, we know (because the register was not
14390                  referenced in i3) that it's unused, just as it was
14391                  unused before, and we place the note in i2.  */
14392               if (from_insn != i3 && i2 && INSN_P (i2)
14393                   && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14394                 {
14395                   if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14396                     PUT_REG_NOTE_KIND (note, REG_DEAD);
14397                   if (! (REG_P (XEXP (note, 0))
14398                          ? find_regno_note (i2, REG_NOTE_KIND (note),
14399                                             REGNO (XEXP (note, 0)))
14400                          : find_reg_note (i2, REG_NOTE_KIND (note),
14401                                           XEXP (note, 0))))
14402                     place = i2;
14403                 }
14404             }
14405
14406           break;
14407
14408         case REG_EQUAL:
14409         case REG_EQUIV:
14410         case REG_NOALIAS:
14411           /* These notes say something about results of an insn.  We can
14412              only support them if they used to be on I3 in which case they
14413              remain on I3.  Otherwise they are ignored.
14414
14415              If the note refers to an expression that is not a constant, we
14416              must also ignore the note since we cannot tell whether the
14417              equivalence is still true.  It might be possible to do
14418              slightly better than this (we only have a problem if I2DEST
14419              or I1DEST is present in the expression), but it doesn't
14420              seem worth the trouble.  */
14421
14422           if (from_insn == i3
14423               && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14424             place = i3;
14425           break;
14426
14427         case REG_INC:
14428           /* These notes say something about how a register is used.  They must
14429              be present on any use of the register in I2 or I3.  */
14430           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14431             place = i3;
14432
14433           if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14434             {
14435               if (place)
14436                 place2 = i2;
14437               else
14438                 place = i2;
14439             }
14440           break;
14441
14442         case REG_LABEL_TARGET:
14443         case REG_LABEL_OPERAND:
14444           /* This can show up in several ways -- either directly in the
14445              pattern, or hidden off in the constant pool with (or without?)
14446              a REG_EQUAL note.  */
14447           /* ??? Ignore the without-reg_equal-note problem for now.  */
14448           if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14449               || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14450                   && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14451                   && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14452             place = i3;
14453
14454           if (i2
14455               && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14456                   || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14457                       && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14458                       && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14459             {
14460               if (place)
14461                 place2 = i2;
14462               else
14463                 place = i2;
14464             }
14465
14466           /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14467              as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14468              there.  */
14469           if (place && JUMP_P (place)
14470               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14471               && (JUMP_LABEL (place) == NULL
14472                   || JUMP_LABEL (place) == XEXP (note, 0)))
14473             {
14474               rtx label = JUMP_LABEL (place);
14475
14476               if (!label)
14477                 JUMP_LABEL (place) = XEXP (note, 0);
14478               else if (LABEL_P (label))
14479                 LABEL_NUSES (label)--;
14480             }
14481
14482           if (place2 && JUMP_P (place2)
14483               && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14484               && (JUMP_LABEL (place2) == NULL
14485                   || JUMP_LABEL (place2) == XEXP (note, 0)))
14486             {
14487               rtx label = JUMP_LABEL (place2);
14488
14489               if (!label)
14490                 JUMP_LABEL (place2) = XEXP (note, 0);
14491               else if (LABEL_P (label))
14492                 LABEL_NUSES (label)--;
14493               place2 = 0;
14494             }
14495           break;
14496
14497         case REG_NONNEG:
14498           /* This note says something about the value of a register prior
14499              to the execution of an insn.  It is too much trouble to see
14500              if the note is still correct in all situations.  It is better
14501              to simply delete it.  */
14502           break;
14503
14504         case REG_DEAD:
14505           /* If we replaced the right hand side of FROM_INSN with a
14506              REG_EQUAL note, the original use of the dying register
14507              will not have been combined into I3 and I2.  In such cases,
14508              FROM_INSN is guaranteed to be the first of the combined
14509              instructions, so we simply need to search back before
14510              FROM_INSN for the previous use or set of this register,
14511              then alter the notes there appropriately.
14512
14513              If the register is used as an input in I3, it dies there.
14514              Similarly for I2, if it is nonzero and adjacent to I3.
14515
14516              If the register is not used as an input in either I3 or I2
14517              and it is not one of the registers we were supposed to eliminate,
14518              there are two possibilities.  We might have a non-adjacent I2
14519              or we might have somehow eliminated an additional register
14520              from a computation.  For example, we might have had A & B where
14521              we discover that B will always be zero.  In this case we will
14522              eliminate the reference to A.
14523
14524              In both cases, we must search to see if we can find a previous
14525              use of A and put the death note there.  */
14526
14527           if (from_insn
14528               && from_insn == i2mod
14529               && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14530             tem_insn = from_insn;
14531           else
14532             {
14533               if (from_insn
14534                   && CALL_P (from_insn)
14535                   && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14536                 place = from_insn;
14537               else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14538                 {
14539                   /* If the new I2 sets the same register that is marked
14540                      dead in the note, we do not in general know where to
14541                      put the note.  One important case we _can_ handle is
14542                      when the note comes from I3.  */
14543                   if (from_insn == i3)
14544                     place = i3;
14545                   else
14546                     break;
14547                 }
14548               else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14549                 place = i3;
14550               else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14551                        && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14552                 place = i2;
14553               else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14554                         && !(i2mod
14555                              && reg_overlap_mentioned_p (XEXP (note, 0),
14556                                                          i2mod_old_rhs)))
14557                        || rtx_equal_p (XEXP (note, 0), elim_i1)
14558                        || rtx_equal_p (XEXP (note, 0), elim_i0))
14559                 break;
14560               tem_insn = i3;
14561             }
14562
14563           if (place == 0)
14564             {
14565               basic_block bb = this_basic_block;
14566
14567               for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14568                 {
14569                   if (!NONDEBUG_INSN_P (tem_insn))
14570                     {
14571                       if (tem_insn == BB_HEAD (bb))
14572                         break;
14573                       continue;
14574                     }
14575
14576                   /* If the register is being set at TEM_INSN, see if that is all
14577                      TEM_INSN is doing.  If so, delete TEM_INSN.  Otherwise, make this
14578                      into a REG_UNUSED note instead. Don't delete sets to
14579                      global register vars.  */
14580                   if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14581                        || !global_regs[REGNO (XEXP (note, 0))])
14582                       && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14583                     {
14584                       rtx set = single_set (tem_insn);
14585                       rtx inner_dest = 0;
14586                       rtx_insn *cc0_setter = NULL;
14587
14588                       if (set != 0)
14589                         for (inner_dest = SET_DEST (set);
14590                              (GET_CODE (inner_dest) == STRICT_LOW_PART
14591                               || GET_CODE (inner_dest) == SUBREG
14592                               || GET_CODE (inner_dest) == ZERO_EXTRACT);
14593                              inner_dest = XEXP (inner_dest, 0))
14594                           ;
14595
14596                       /* Verify that it was the set, and not a clobber that
14597                          modified the register.
14598
14599                          CC0 targets must be careful to maintain setter/user
14600                          pairs.  If we cannot delete the setter due to side
14601                          effects, mark the user with an UNUSED note instead
14602                          of deleting it.  */
14603
14604                       if (set != 0 && ! side_effects_p (SET_SRC (set))
14605                           && rtx_equal_p (XEXP (note, 0), inner_dest)
14606                           && (!HAVE_cc0
14607                               || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14608                                   || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14609                                       && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14610                         {
14611                           /* Move the notes and links of TEM_INSN elsewhere.
14612                              This might delete other dead insns recursively.
14613                              First set the pattern to something that won't use
14614                              any register.  */
14615                           rtx old_notes = REG_NOTES (tem_insn);
14616
14617                           PATTERN (tem_insn) = pc_rtx;
14618                           REG_NOTES (tem_insn) = NULL;
14619
14620                           distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14621                                             NULL_RTX, NULL_RTX, NULL_RTX);
14622                           distribute_links (LOG_LINKS (tem_insn));
14623
14624                           unsigned int regno = REGNO (XEXP (note, 0));
14625                           reg_stat_type *rsp = &reg_stat[regno];
14626                           if (rsp->last_set == tem_insn)
14627                             record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14628
14629                           SET_INSN_DELETED (tem_insn);
14630                           if (tem_insn == i2)
14631                             i2 = NULL;
14632
14633                           /* Delete the setter too.  */
14634                           if (cc0_setter)
14635                             {
14636                               PATTERN (cc0_setter) = pc_rtx;
14637                               old_notes = REG_NOTES (cc0_setter);
14638                               REG_NOTES (cc0_setter) = NULL;
14639
14640                               distribute_notes (old_notes, cc0_setter,
14641                                                 cc0_setter, NULL,
14642                                                 NULL_RTX, NULL_RTX, NULL_RTX);
14643                               distribute_links (LOG_LINKS (cc0_setter));
14644
14645                               SET_INSN_DELETED (cc0_setter);
14646                               if (cc0_setter == i2)
14647                                 i2 = NULL;
14648                             }
14649                         }
14650                       else
14651                         {
14652                           PUT_REG_NOTE_KIND (note, REG_UNUSED);
14653
14654                           /*  If there isn't already a REG_UNUSED note, put one
14655                               here.  Do not place a REG_DEAD note, even if
14656                               the register is also used here; that would not
14657                               match the algorithm used in lifetime analysis
14658                               and can cause the consistency check in the
14659                               scheduler to fail.  */
14660                           if (! find_regno_note (tem_insn, REG_UNUSED,
14661                                                  REGNO (XEXP (note, 0))))
14662                             place = tem_insn;
14663                           break;
14664                         }
14665                     }
14666                   else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14667                            || (CALL_P (tem_insn)
14668                                && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14669                     {
14670                       place = tem_insn;
14671
14672                       /* If we are doing a 3->2 combination, and we have a
14673                          register which formerly died in i3 and was not used
14674                          by i2, which now no longer dies in i3 and is used in
14675                          i2 but does not die in i2, and place is between i2
14676                          and i3, then we may need to move a link from place to
14677                          i2.  */
14678                       if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14679                           && from_insn
14680                           && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14681                           && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14682                         {
14683                           struct insn_link *links = LOG_LINKS (place);
14684                           LOG_LINKS (place) = NULL;
14685                           distribute_links (links);
14686                         }
14687                       break;
14688                     }
14689
14690                   if (tem_insn == BB_HEAD (bb))
14691                     break;
14692                 }
14693
14694             }
14695
14696           /* If the register is set or already dead at PLACE, we needn't do
14697              anything with this note if it is still a REG_DEAD note.
14698              We check here if it is set at all, not if is it totally replaced,
14699              which is what `dead_or_set_p' checks, so also check for it being
14700              set partially.  */
14701
14702           if (place && REG_NOTE_KIND (note) == REG_DEAD)
14703             {
14704               unsigned int regno = REGNO (XEXP (note, 0));
14705               reg_stat_type *rsp = &reg_stat[regno];
14706
14707               if (dead_or_set_p (place, XEXP (note, 0))
14708                   || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14709                 {
14710                   /* Unless the register previously died in PLACE, clear
14711                      last_death.  [I no longer understand why this is
14712                      being done.] */
14713                   if (rsp->last_death != place)
14714                     rsp->last_death = 0;
14715                   place = 0;
14716                 }
14717               else
14718                 rsp->last_death = place;
14719
14720               /* If this is a death note for a hard reg that is occupying
14721                  multiple registers, ensure that we are still using all
14722                  parts of the object.  If we find a piece of the object
14723                  that is unused, we must arrange for an appropriate REG_DEAD
14724                  note to be added for it.  However, we can't just emit a USE
14725                  and tag the note to it, since the register might actually
14726                  be dead; so we recourse, and the recursive call then finds
14727                  the previous insn that used this register.  */
14728
14729               if (place && REG_NREGS (XEXP (note, 0)) > 1)
14730                 {
14731                   unsigned int endregno = END_REGNO (XEXP (note, 0));
14732                   bool all_used = true;
14733                   unsigned int i;
14734
14735                   for (i = regno; i < endregno; i++)
14736                     if ((! refers_to_regno_p (i, PATTERN (place))
14737                          && ! find_regno_fusage (place, USE, i))
14738                         || dead_or_set_regno_p (place, i))
14739                       {
14740                         all_used = false;
14741                         break;
14742                       }
14743
14744                   if (! all_used)
14745                     {
14746                       /* Put only REG_DEAD notes for pieces that are
14747                          not already dead or set.  */
14748
14749                       for (i = regno; i < endregno;
14750                            i += hard_regno_nregs (i, reg_raw_mode[i]))
14751                         {
14752                           rtx piece = regno_reg_rtx[i];
14753                           basic_block bb = this_basic_block;
14754
14755                           if (! dead_or_set_p (place, piece)
14756                               && ! reg_bitfield_target_p (piece,
14757                                                           PATTERN (place)))
14758                             {
14759                               rtx new_note = alloc_reg_note (REG_DEAD, piece,
14760                                                              NULL_RTX);
14761
14762                               distribute_notes (new_note, place, place,
14763                                                 NULL, NULL_RTX, NULL_RTX,
14764                                                 NULL_RTX);
14765                             }
14766                           else if (! refers_to_regno_p (i, PATTERN (place))
14767                                    && ! find_regno_fusage (place, USE, i))
14768                             for (tem_insn = PREV_INSN (place); ;
14769                                  tem_insn = PREV_INSN (tem_insn))
14770                               {
14771                                 if (!NONDEBUG_INSN_P (tem_insn))
14772                                   {
14773                                     if (tem_insn == BB_HEAD (bb))
14774                                       break;
14775                                     continue;
14776                                   }
14777                                 if (dead_or_set_p (tem_insn, piece)
14778                                     || reg_bitfield_target_p (piece,
14779                                                               PATTERN (tem_insn)))
14780                                   {
14781                                     add_reg_note (tem_insn, REG_UNUSED, piece);
14782                                     break;
14783                                   }
14784                               }
14785                         }
14786
14787                       place = 0;
14788                     }
14789                 }
14790             }
14791           break;
14792
14793         default:
14794           /* Any other notes should not be present at this point in the
14795              compilation.  */
14796           gcc_unreachable ();
14797         }
14798
14799       if (place)
14800         {
14801           XEXP (note, 1) = REG_NOTES (place);
14802           REG_NOTES (place) = note;
14803
14804           /* Set added_notes_insn to the earliest insn we added a note to.  */
14805           if (added_notes_insn == 0
14806               || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14807             added_notes_insn = place;
14808         }
14809
14810       if (place2)
14811         {
14812           add_shallow_copy_of_reg_note (place2, note);
14813
14814           /* Set added_notes_insn to the earliest insn we added a note to.  */
14815           if (added_notes_insn == 0
14816               || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14817             added_notes_insn = place2;
14818         }
14819     }
14820 }
14821 \f
14822 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14823    I3, I2, and I1 to new locations.  This is also called to add a link
14824    pointing at I3 when I3's destination is changed.  */
14825
14826 static void
14827 distribute_links (struct insn_link *links)
14828 {
14829   struct insn_link *link, *next_link;
14830
14831   for (link = links; link; link = next_link)
14832     {
14833       rtx_insn *place = 0;
14834       rtx_insn *insn;
14835       rtx set, reg;
14836
14837       next_link = link->next;
14838
14839       /* If the insn that this link points to is a NOTE, ignore it.  */
14840       if (NOTE_P (link->insn))
14841         continue;
14842
14843       set = 0;
14844       rtx pat = PATTERN (link->insn);
14845       if (GET_CODE (pat) == SET)
14846         set = pat;
14847       else if (GET_CODE (pat) == PARALLEL)
14848         {
14849           int i;
14850           for (i = 0; i < XVECLEN (pat, 0); i++)
14851             {
14852               set = XVECEXP (pat, 0, i);
14853               if (GET_CODE (set) != SET)
14854                 continue;
14855
14856               reg = SET_DEST (set);
14857               while (GET_CODE (reg) == ZERO_EXTRACT
14858                      || GET_CODE (reg) == STRICT_LOW_PART
14859                      || GET_CODE (reg) == SUBREG)
14860                 reg = XEXP (reg, 0);
14861
14862               if (!REG_P (reg))
14863                 continue;
14864
14865               if (REGNO (reg) == link->regno)
14866                 break;
14867             }
14868           if (i == XVECLEN (pat, 0))
14869             continue;
14870         }
14871       else
14872         continue;
14873
14874       reg = SET_DEST (set);
14875
14876       while (GET_CODE (reg) == ZERO_EXTRACT
14877              || GET_CODE (reg) == STRICT_LOW_PART
14878              || GET_CODE (reg) == SUBREG)
14879         reg = XEXP (reg, 0);
14880
14881       if (reg == pc_rtx)
14882         continue;
14883
14884       /* A LOG_LINK is defined as being placed on the first insn that uses
14885          a register and points to the insn that sets the register.  Start
14886          searching at the next insn after the target of the link and stop
14887          when we reach a set of the register or the end of the basic block.
14888
14889          Note that this correctly handles the link that used to point from
14890          I3 to I2.  Also note that not much searching is typically done here
14891          since most links don't point very far away.  */
14892
14893       for (insn = NEXT_INSN (link->insn);
14894            (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14895                      || BB_HEAD (this_basic_block->next_bb) != insn));
14896            insn = NEXT_INSN (insn))
14897         if (DEBUG_INSN_P (insn))
14898           continue;
14899         else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14900           {
14901             if (reg_referenced_p (reg, PATTERN (insn)))
14902               place = insn;
14903             break;
14904           }
14905         else if (CALL_P (insn)
14906                  && find_reg_fusage (insn, USE, reg))
14907           {
14908             place = insn;
14909             break;
14910           }
14911         else if (INSN_P (insn) && reg_set_p (reg, insn))
14912           break;
14913
14914       /* If we found a place to put the link, place it there unless there
14915          is already a link to the same insn as LINK at that point.  */
14916
14917       if (place)
14918         {
14919           struct insn_link *link2;
14920
14921           FOR_EACH_LOG_LINK (link2, place)
14922             if (link2->insn == link->insn && link2->regno == link->regno)
14923               break;
14924
14925           if (link2 == NULL)
14926             {
14927               link->next = LOG_LINKS (place);
14928               LOG_LINKS (place) = link;
14929
14930               /* Set added_links_insn to the earliest insn we added a
14931                  link to.  */
14932               if (added_links_insn == 0
14933                   || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14934                 added_links_insn = place;
14935             }
14936         }
14937     }
14938 }
14939 \f
14940 /* Check for any register or memory mentioned in EQUIV that is not
14941    mentioned in EXPR.  This is used to restrict EQUIV to "specializations"
14942    of EXPR where some registers may have been replaced by constants.  */
14943
14944 static bool
14945 unmentioned_reg_p (rtx equiv, rtx expr)
14946 {
14947   subrtx_iterator::array_type array;
14948   FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14949     {
14950       const_rtx x = *iter;
14951       if ((REG_P (x) || MEM_P (x))
14952           && !reg_mentioned_p (x, expr))
14953         return true;
14954     }
14955   return false;
14956 }
14957 \f
14958 DEBUG_FUNCTION void
14959 dump_combine_stats (FILE *file)
14960 {
14961   fprintf
14962     (file,
14963      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14964      combine_attempts, combine_merges, combine_extras, combine_successes);
14965 }
14966
14967 void
14968 dump_combine_total_stats (FILE *file)
14969 {
14970   fprintf
14971     (file,
14972      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14973      total_attempts, total_merges, total_extras, total_successes);
14974 }
14975 \f
14976 /* Make pseudo-to-pseudo copies after every hard-reg-to-pseudo-copy, because
14977    the reg-to-reg copy can usefully combine with later instructions, but we
14978    do not want to combine the hard reg into later instructions, for that
14979    restricts register allocation.  */
14980 static void
14981 make_more_copies (void)
14982 {
14983   basic_block bb;
14984
14985   FOR_EACH_BB_FN (bb, cfun)
14986     {
14987       rtx_insn *insn;
14988
14989       FOR_BB_INSNS (bb, insn)
14990         {
14991           if (!NONDEBUG_INSN_P (insn))
14992             continue;
14993
14994           rtx set = single_set (insn);
14995           if (!set)
14996             continue;
14997
14998           rtx dest = SET_DEST (set);
14999           if (!(REG_P (dest) && !HARD_REGISTER_P (dest)))
15000               continue;
15001
15002           rtx src = SET_SRC (set);
15003           if (!(REG_P (src) && HARD_REGISTER_P (src)))
15004             continue;
15005           if (TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src)))
15006             continue;
15007
15008           rtx new_reg = gen_reg_rtx (GET_MODE (dest));
15009           rtx_insn *new_insn = gen_move_insn (new_reg, src);
15010           SET_SRC (set) = new_reg;
15011           emit_insn_before (new_insn, insn);
15012           df_insn_rescan (insn);
15013         }
15014     }
15015 }
15016
15017 /* Try combining insns through substitution.  */
15018 static unsigned int
15019 rest_of_handle_combine (void)
15020 {
15021   make_more_copies ();
15022
15023   df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
15024   df_note_add_problem ();
15025   df_analyze ();
15026
15027   regstat_init_n_sets_and_refs ();
15028   reg_n_sets_max = max_reg_num ();
15029
15030   int rebuild_jump_labels_after_combine
15031     = combine_instructions (get_insns (), max_reg_num ());
15032
15033   /* Combining insns may have turned an indirect jump into a
15034      direct jump.  Rebuild the JUMP_LABEL fields of jumping
15035      instructions.  */
15036   if (rebuild_jump_labels_after_combine)
15037     {
15038       if (dom_info_available_p (CDI_DOMINATORS))
15039         free_dominance_info (CDI_DOMINATORS);
15040       timevar_push (TV_JUMP);
15041       rebuild_jump_labels (get_insns ());
15042       cleanup_cfg (0);
15043       timevar_pop (TV_JUMP);
15044     }
15045
15046   regstat_free_n_sets_and_refs ();
15047   return 0;
15048 }
15049
15050 namespace {
15051
15052 const pass_data pass_data_combine =
15053 {
15054   RTL_PASS, /* type */
15055   "combine", /* name */
15056   OPTGROUP_NONE, /* optinfo_flags */
15057   TV_COMBINE, /* tv_id */
15058   PROP_cfglayout, /* properties_required */
15059   0, /* properties_provided */
15060   0, /* properties_destroyed */
15061   0, /* todo_flags_start */
15062   TODO_df_finish, /* todo_flags_finish */
15063 };
15064
15065 class pass_combine : public rtl_opt_pass
15066 {
15067 public:
15068   pass_combine (gcc::context *ctxt)
15069     : rtl_opt_pass (pass_data_combine, ctxt)
15070   {}
15071
15072   /* opt_pass methods: */
15073   virtual bool gate (function *) { return (optimize > 0); }
15074   virtual unsigned int execute (function *)
15075     {
15076       return rest_of_handle_combine ();
15077     }
15078
15079 }; // class pass_combine
15080
15081 } // anon namespace
15082
15083 rtl_opt_pass *
15084 make_pass_combine (gcc::context *ctxt)
15085 {
15086   return new pass_combine (ctxt);
15087 }