]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/combine.c
15e6833c003f365d5c093a13d69d3f8bdd2899ee
[thirdparty/gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2014 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 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 use_crosses_set_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 "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "stor-layout.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "basic-block.h"
90 #include "insn-config.h"
91 #include "hashtab.h"
92 #include "hash-set.h"
93 #include "vec.h"
94 #include "machmode.h"
95 #include "input.h"
96 #include "function.h"
97 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
98 #include "expr.h"
99 #include "insn-attr.h"
100 #include "recog.h"
101 #include "diagnostic-core.h"
102 #include "target.h"
103 #include "optabs.h"
104 #include "insn-codes.h"
105 #include "rtlhooks-def.h"
106 #include "params.h"
107 #include "tree-pass.h"
108 #include "df.h"
109 #include "valtrack.h"
110 #include "cgraph.h"
111 #include "obstack.h"
112 #include "statistics.h"
113 #include "params.h"
114 #include "rtl-iter.h"
115
116 /* Number of attempts to combine instructions in this function. */
117
118 static int combine_attempts;
119
120 /* Number of attempts that got as far as substitution in this function. */
121
122 static int combine_merges;
123
124 /* Number of instructions combined with added SETs in this function. */
125
126 static int combine_extras;
127
128 /* Number of instructions combined in this function. */
129
130 static int combine_successes;
131
132 /* Totals over entire compilation. */
133
134 static int total_attempts, total_merges, total_extras, total_successes;
135
136 /* combine_instructions may try to replace the right hand side of the
137 second instruction with the value of an associated REG_EQUAL note
138 before throwing it at try_combine. That is problematic when there
139 is a REG_DEAD note for a register used in the old right hand side
140 and can cause distribute_notes to do wrong things. This is the
141 second instruction if it has been so modified, null otherwise. */
142
143 static rtx_insn *i2mod;
144
145 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
146
147 static rtx i2mod_old_rhs;
148
149 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
150
151 static rtx i2mod_new_rhs;
152 \f
153 typedef struct reg_stat_struct {
154 /* Record last point of death of (hard or pseudo) register n. */
155 rtx_insn *last_death;
156
157 /* Record last point of modification of (hard or pseudo) register n. */
158 rtx_insn *last_set;
159
160 /* The next group of fields allows the recording of the last value assigned
161 to (hard or pseudo) register n. We use this information to see if an
162 operation being processed is redundant given a prior operation performed
163 on the register. For example, an `and' with a constant is redundant if
164 all the zero bits are already known to be turned off.
165
166 We use an approach similar to that used by cse, but change it in the
167 following ways:
168
169 (1) We do not want to reinitialize at each label.
170 (2) It is useful, but not critical, to know the actual value assigned
171 to a register. Often just its form is helpful.
172
173 Therefore, we maintain the following fields:
174
175 last_set_value the last value assigned
176 last_set_label records the value of label_tick when the
177 register was assigned
178 last_set_table_tick records the value of label_tick when a
179 value using the register is assigned
180 last_set_invalid set to nonzero when it is not valid
181 to use the value of this register in some
182 register's value
183
184 To understand the usage of these tables, it is important to understand
185 the distinction between the value in last_set_value being valid and
186 the register being validly contained in some other expression in the
187 table.
188
189 (The next two parameters are out of date).
190
191 reg_stat[i].last_set_value is valid if it is nonzero, and either
192 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
193
194 Register I may validly appear in any expression returned for the value
195 of another register if reg_n_sets[i] is 1. It may also appear in the
196 value for register J if reg_stat[j].last_set_invalid is zero, or
197 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
198
199 If an expression is found in the table containing a register which may
200 not validly appear in an expression, the register is replaced by
201 something that won't match, (clobber (const_int 0)). */
202
203 /* Record last value assigned to (hard or pseudo) register n. */
204
205 rtx last_set_value;
206
207 /* Record the value of label_tick when an expression involving register n
208 is placed in last_set_value. */
209
210 int last_set_table_tick;
211
212 /* Record the value of label_tick when the value for register n is placed in
213 last_set_value. */
214
215 int last_set_label;
216
217 /* These fields are maintained in parallel with last_set_value and are
218 used to store the mode in which the register was last set, the bits
219 that were known to be zero when it was last set, and the number of
220 sign bits copies it was known to have when it was last set. */
221
222 unsigned HOST_WIDE_INT last_set_nonzero_bits;
223 char last_set_sign_bit_copies;
224 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
225
226 /* Set nonzero if references to register n in expressions should not be
227 used. last_set_invalid is set nonzero when this register is being
228 assigned to and last_set_table_tick == label_tick. */
229
230 char last_set_invalid;
231
232 /* Some registers that are set more than once and used in more than one
233 basic block are nevertheless always set in similar ways. For example,
234 a QImode register may be loaded from memory in two places on a machine
235 where byte loads zero extend.
236
237 We record in the following fields if a register has some leading bits
238 that are always equal to the sign bit, and what we know about the
239 nonzero bits of a register, specifically which bits are known to be
240 zero.
241
242 If an entry is zero, it means that we don't know anything special. */
243
244 unsigned char sign_bit_copies;
245
246 unsigned HOST_WIDE_INT nonzero_bits;
247
248 /* Record the value of the label_tick when the last truncation
249 happened. The field truncated_to_mode is only valid if
250 truncation_label == label_tick. */
251
252 int truncation_label;
253
254 /* Record the last truncation seen for this register. If truncation
255 is not a nop to this mode we might be able to save an explicit
256 truncation if we know that value already contains a truncated
257 value. */
258
259 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
260 } reg_stat_type;
261
262
263 static vec<reg_stat_type> reg_stat;
264
265 /* Record the luid of the last insn that invalidated memory
266 (anything that writes memory, and subroutine calls, but not pushes). */
267
268 static int mem_last_set;
269
270 /* Record the luid of the last CALL_INSN
271 so we can tell whether a potential combination crosses any calls. */
272
273 static int last_call_luid;
274
275 /* When `subst' is called, this is the insn that is being modified
276 (by combining in a previous insn). The PATTERN of this insn
277 is still the old pattern partially modified and it should not be
278 looked at, but this may be used to examine the successors of the insn
279 to judge whether a simplification is valid. */
280
281 static rtx_insn *subst_insn;
282
283 /* This is the lowest LUID that `subst' is currently dealing with.
284 get_last_value will not return a value if the register was set at or
285 after this LUID. If not for this mechanism, we could get confused if
286 I2 or I1 in try_combine were an insn that used the old value of a register
287 to obtain a new value. In that case, we might erroneously get the
288 new value of the register when we wanted the old one. */
289
290 static int subst_low_luid;
291
292 /* This contains any hard registers that are used in newpat; reg_dead_at_p
293 must consider all these registers to be always live. */
294
295 static HARD_REG_SET newpat_used_regs;
296
297 /* This is an insn to which a LOG_LINKS entry has been added. If this
298 insn is the earlier than I2 or I3, combine should rescan starting at
299 that location. */
300
301 static rtx_insn *added_links_insn;
302
303 /* Basic block in which we are performing combines. */
304 static basic_block this_basic_block;
305 static bool optimize_this_for_speed_p;
306
307 \f
308 /* Length of the currently allocated uid_insn_cost array. */
309
310 static int max_uid_known;
311
312 /* The following array records the insn_rtx_cost for every insn
313 in the instruction stream. */
314
315 static int *uid_insn_cost;
316
317 /* The following array records the LOG_LINKS for every insn in the
318 instruction stream as struct insn_link pointers. */
319
320 struct insn_link {
321 rtx_insn *insn;
322 struct insn_link *next;
323 };
324
325 static struct insn_link **uid_log_links;
326
327 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
328 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
329
330 #define FOR_EACH_LOG_LINK(L, INSN) \
331 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
332
333 /* Links for LOG_LINKS are allocated from this obstack. */
334
335 static struct obstack insn_link_obstack;
336
337 /* Allocate a link. */
338
339 static inline struct insn_link *
340 alloc_insn_link (rtx_insn *insn, struct insn_link *next)
341 {
342 struct insn_link *l
343 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
344 sizeof (struct insn_link));
345 l->insn = insn;
346 l->next = next;
347 return l;
348 }
349
350 /* Incremented for each basic block. */
351
352 static int label_tick;
353
354 /* Reset to label_tick for each extended basic block in scanning order. */
355
356 static int label_tick_ebb_start;
357
358 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
359 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
360
361 static enum machine_mode nonzero_bits_mode;
362
363 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
364 be safely used. It is zero while computing them and after combine has
365 completed. This former test prevents propagating values based on
366 previously set values, which can be incorrect if a variable is modified
367 in a loop. */
368
369 static int nonzero_sign_valid;
370
371 \f
372 /* Record one modification to rtl structure
373 to be undone by storing old_contents into *where. */
374
375 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
376
377 struct undo
378 {
379 struct undo *next;
380 enum undo_kind kind;
381 union { rtx r; int i; enum machine_mode m; struct insn_link *l; } old_contents;
382 union { rtx *r; int *i; struct insn_link **l; } where;
383 };
384
385 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
386 num_undo says how many are currently recorded.
387
388 other_insn is nonzero if we have modified some other insn in the process
389 of working on subst_insn. It must be verified too. */
390
391 struct undobuf
392 {
393 struct undo *undos;
394 struct undo *frees;
395 rtx_insn *other_insn;
396 };
397
398 static struct undobuf undobuf;
399
400 /* Number of times the pseudo being substituted for
401 was found and replaced. */
402
403 static int n_occurrences;
404
405 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
406 enum machine_mode,
407 unsigned HOST_WIDE_INT,
408 unsigned HOST_WIDE_INT *);
409 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
410 enum machine_mode,
411 unsigned int, unsigned int *);
412 static void do_SUBST (rtx *, rtx);
413 static void do_SUBST_INT (int *, int);
414 static void init_reg_last (void);
415 static void setup_incoming_promotions (rtx_insn *);
416 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
417 static int cant_combine_insn_p (rtx_insn *);
418 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
419 rtx_insn *, rtx_insn *, rtx *, rtx *);
420 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
421 static int contains_muldiv (rtx);
422 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
423 int *, rtx_insn *);
424 static void undo_all (void);
425 static void undo_commit (void);
426 static rtx *find_split_point (rtx *, rtx_insn *, bool);
427 static rtx subst (rtx, rtx, rtx, int, int, int);
428 static rtx combine_simplify_rtx (rtx, enum machine_mode, int, int);
429 static rtx simplify_if_then_else (rtx);
430 static rtx simplify_set (rtx);
431 static rtx simplify_logical (rtx);
432 static rtx expand_compound_operation (rtx);
433 static const_rtx expand_field_assignment (const_rtx);
434 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
435 rtx, unsigned HOST_WIDE_INT, int, int, int);
436 static rtx extract_left_shift (rtx, int);
437 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
438 unsigned HOST_WIDE_INT *);
439 static rtx canon_reg_for_combine (rtx, rtx);
440 static rtx force_to_mode (rtx, enum machine_mode,
441 unsigned HOST_WIDE_INT, int);
442 static rtx if_then_else_cond (rtx, rtx *, rtx *);
443 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
444 static int rtx_equal_for_field_assignment_p (rtx, rtx);
445 static rtx make_field_assignment (rtx);
446 static rtx apply_distributive_law (rtx);
447 static rtx distribute_and_simplify_rtx (rtx, int);
448 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
449 unsigned HOST_WIDE_INT);
450 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
451 unsigned HOST_WIDE_INT);
452 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
453 HOST_WIDE_INT, enum machine_mode, int *);
454 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
455 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
456 int);
457 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
458 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
459 static enum rtx_code simplify_compare_const (enum rtx_code, enum machine_mode,
460 rtx, rtx *);
461 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
462 static void update_table_tick (rtx);
463 static void record_value_for_reg (rtx, rtx_insn *, rtx);
464 static void check_promoted_subreg (rtx_insn *, rtx);
465 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
466 static void record_dead_and_set_regs (rtx_insn *);
467 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
468 static rtx get_last_value (const_rtx);
469 static int use_crosses_set_p (const_rtx, int);
470 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
471 static int reg_dead_at_p (rtx, rtx_insn *);
472 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
473 static int reg_bitfield_target_p (rtx, rtx);
474 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
475 static void distribute_links (struct insn_link *);
476 static void mark_used_regs_combine (rtx);
477 static void record_promoted_value (rtx_insn *, rtx);
478 static bool unmentioned_reg_p (rtx, rtx);
479 static void record_truncated_values (rtx *, void *);
480 static bool reg_truncated_to_mode (enum machine_mode, const_rtx);
481 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
482 \f
483
484 /* It is not safe to use ordinary gen_lowpart in combine.
485 See comments in gen_lowpart_for_combine. */
486 #undef RTL_HOOKS_GEN_LOWPART
487 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
488
489 /* Our implementation of gen_lowpart never emits a new pseudo. */
490 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
491 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
492
493 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
494 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
495
496 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
497 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
498
499 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
500 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
501
502 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
503
504 \f
505 /* Convenience wrapper for the canonicalize_comparison target hook.
506 Target hooks cannot use enum rtx_code. */
507 static inline void
508 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
509 bool op0_preserve_value)
510 {
511 int code_int = (int)*code;
512 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
513 *code = (enum rtx_code)code_int;
514 }
515
516 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
517 PATTERN can not be split. Otherwise, it returns an insn sequence.
518 This is a wrapper around split_insns which ensures that the
519 reg_stat vector is made larger if the splitter creates a new
520 register. */
521
522 static rtx_insn *
523 combine_split_insns (rtx pattern, rtx insn)
524 {
525 rtx_insn *ret;
526 unsigned int nregs;
527
528 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
529 nregs = max_reg_num ();
530 if (nregs > reg_stat.length ())
531 reg_stat.safe_grow_cleared (nregs);
532 return ret;
533 }
534
535 /* This is used by find_single_use to locate an rtx in LOC that
536 contains exactly one use of DEST, which is typically either a REG
537 or CC0. It returns a pointer to the innermost rtx expression
538 containing DEST. Appearances of DEST that are being used to
539 totally replace it are not counted. */
540
541 static rtx *
542 find_single_use_1 (rtx dest, rtx *loc)
543 {
544 rtx x = *loc;
545 enum rtx_code code = GET_CODE (x);
546 rtx *result = NULL;
547 rtx *this_result;
548 int i;
549 const char *fmt;
550
551 switch (code)
552 {
553 case CONST:
554 case LABEL_REF:
555 case SYMBOL_REF:
556 CASE_CONST_ANY:
557 case CLOBBER:
558 return 0;
559
560 case SET:
561 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
562 of a REG that occupies all of the REG, the insn uses DEST if
563 it is mentioned in the destination or the source. Otherwise, we
564 need just check the source. */
565 if (GET_CODE (SET_DEST (x)) != CC0
566 && GET_CODE (SET_DEST (x)) != PC
567 && !REG_P (SET_DEST (x))
568 && ! (GET_CODE (SET_DEST (x)) == SUBREG
569 && REG_P (SUBREG_REG (SET_DEST (x)))
570 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
571 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
572 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
573 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
574 break;
575
576 return find_single_use_1 (dest, &SET_SRC (x));
577
578 case MEM:
579 case SUBREG:
580 return find_single_use_1 (dest, &XEXP (x, 0));
581
582 default:
583 break;
584 }
585
586 /* If it wasn't one of the common cases above, check each expression and
587 vector of this code. Look for a unique usage of DEST. */
588
589 fmt = GET_RTX_FORMAT (code);
590 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
591 {
592 if (fmt[i] == 'e')
593 {
594 if (dest == XEXP (x, i)
595 || (REG_P (dest) && REG_P (XEXP (x, i))
596 && REGNO (dest) == REGNO (XEXP (x, i))))
597 this_result = loc;
598 else
599 this_result = find_single_use_1 (dest, &XEXP (x, i));
600
601 if (result == NULL)
602 result = this_result;
603 else if (this_result)
604 /* Duplicate usage. */
605 return NULL;
606 }
607 else if (fmt[i] == 'E')
608 {
609 int j;
610
611 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
612 {
613 if (XVECEXP (x, i, j) == dest
614 || (REG_P (dest)
615 && REG_P (XVECEXP (x, i, j))
616 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
617 this_result = loc;
618 else
619 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
620
621 if (result == NULL)
622 result = this_result;
623 else if (this_result)
624 return NULL;
625 }
626 }
627 }
628
629 return result;
630 }
631
632
633 /* See if DEST, produced in INSN, is used only a single time in the
634 sequel. If so, return a pointer to the innermost rtx expression in which
635 it is used.
636
637 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
638
639 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
640 care about REG_DEAD notes or LOG_LINKS.
641
642 Otherwise, we find the single use by finding an insn that has a
643 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
644 only referenced once in that insn, we know that it must be the first
645 and last insn referencing DEST. */
646
647 static rtx *
648 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
649 {
650 basic_block bb;
651 rtx_insn *next;
652 rtx *result;
653 struct insn_link *link;
654
655 #ifdef HAVE_cc0
656 if (dest == cc0_rtx)
657 {
658 next = NEXT_INSN (insn);
659 if (next == 0
660 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
661 return 0;
662
663 result = find_single_use_1 (dest, &PATTERN (next));
664 if (result && ploc)
665 *ploc = next;
666 return result;
667 }
668 #endif
669
670 if (!REG_P (dest))
671 return 0;
672
673 bb = BLOCK_FOR_INSN (insn);
674 for (next = NEXT_INSN (insn);
675 next && BLOCK_FOR_INSN (next) == bb;
676 next = NEXT_INSN (next))
677 if (INSN_P (next) && dead_or_set_p (next, dest))
678 {
679 FOR_EACH_LOG_LINK (link, next)
680 if (link->insn == insn)
681 break;
682
683 if (link)
684 {
685 result = find_single_use_1 (dest, &PATTERN (next));
686 if (ploc)
687 *ploc = next;
688 return result;
689 }
690 }
691
692 return 0;
693 }
694 \f
695 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
696 insn. The substitution can be undone by undo_all. If INTO is already
697 set to NEWVAL, do not record this change. Because computing NEWVAL might
698 also call SUBST, we have to compute it before we put anything into
699 the undo table. */
700
701 static void
702 do_SUBST (rtx *into, rtx newval)
703 {
704 struct undo *buf;
705 rtx oldval = *into;
706
707 if (oldval == newval)
708 return;
709
710 /* We'd like to catch as many invalid transformations here as
711 possible. Unfortunately, there are way too many mode changes
712 that are perfectly valid, so we'd waste too much effort for
713 little gain doing the checks here. Focus on catching invalid
714 transformations involving integer constants. */
715 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
716 && CONST_INT_P (newval))
717 {
718 /* Sanity check that we're replacing oldval with a CONST_INT
719 that is a valid sign-extension for the original mode. */
720 gcc_assert (INTVAL (newval)
721 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
722
723 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
724 CONST_INT is not valid, because after the replacement, the
725 original mode would be gone. Unfortunately, we can't tell
726 when do_SUBST is called to replace the operand thereof, so we
727 perform this test on oldval instead, checking whether an
728 invalid replacement took place before we got here. */
729 gcc_assert (!(GET_CODE (oldval) == SUBREG
730 && CONST_INT_P (SUBREG_REG (oldval))));
731 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
732 && CONST_INT_P (XEXP (oldval, 0))));
733 }
734
735 if (undobuf.frees)
736 buf = undobuf.frees, undobuf.frees = buf->next;
737 else
738 buf = XNEW (struct undo);
739
740 buf->kind = UNDO_RTX;
741 buf->where.r = into;
742 buf->old_contents.r = oldval;
743 *into = newval;
744
745 buf->next = undobuf.undos, undobuf.undos = buf;
746 }
747
748 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
749
750 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
751 for the value of a HOST_WIDE_INT value (including CONST_INT) is
752 not safe. */
753
754 static void
755 do_SUBST_INT (int *into, int newval)
756 {
757 struct undo *buf;
758 int oldval = *into;
759
760 if (oldval == newval)
761 return;
762
763 if (undobuf.frees)
764 buf = undobuf.frees, undobuf.frees = buf->next;
765 else
766 buf = XNEW (struct undo);
767
768 buf->kind = UNDO_INT;
769 buf->where.i = into;
770 buf->old_contents.i = oldval;
771 *into = newval;
772
773 buf->next = undobuf.undos, undobuf.undos = buf;
774 }
775
776 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
777
778 /* Similar to SUBST, but just substitute the mode. This is used when
779 changing the mode of a pseudo-register, so that any other
780 references to the entry in the regno_reg_rtx array will change as
781 well. */
782
783 static void
784 do_SUBST_MODE (rtx *into, enum machine_mode newval)
785 {
786 struct undo *buf;
787 enum machine_mode oldval = GET_MODE (*into);
788
789 if (oldval == newval)
790 return;
791
792 if (undobuf.frees)
793 buf = undobuf.frees, undobuf.frees = buf->next;
794 else
795 buf = XNEW (struct undo);
796
797 buf->kind = UNDO_MODE;
798 buf->where.r = into;
799 buf->old_contents.m = oldval;
800 adjust_reg_mode (*into, newval);
801
802 buf->next = undobuf.undos, undobuf.undos = buf;
803 }
804
805 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
806
807 #ifndef HAVE_cc0
808 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
809
810 static void
811 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
812 {
813 struct undo *buf;
814 struct insn_link * oldval = *into;
815
816 if (oldval == newval)
817 return;
818
819 if (undobuf.frees)
820 buf = undobuf.frees, undobuf.frees = buf->next;
821 else
822 buf = XNEW (struct undo);
823
824 buf->kind = UNDO_LINKS;
825 buf->where.l = into;
826 buf->old_contents.l = oldval;
827 *into = newval;
828
829 buf->next = undobuf.undos, undobuf.undos = buf;
830 }
831
832 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
833 #endif
834 \f
835 /* Subroutine of try_combine. Determine whether the replacement patterns
836 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
837 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
838 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
839 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
840 of all the instructions can be estimated and the replacements are more
841 expensive than the original sequence. */
842
843 static bool
844 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
845 rtx newpat, rtx newi2pat, rtx newotherpat)
846 {
847 int i0_cost, i1_cost, i2_cost, i3_cost;
848 int new_i2_cost, new_i3_cost;
849 int old_cost, new_cost;
850
851 /* Lookup the original insn_rtx_costs. */
852 i2_cost = INSN_COST (i2);
853 i3_cost = INSN_COST (i3);
854
855 if (i1)
856 {
857 i1_cost = INSN_COST (i1);
858 if (i0)
859 {
860 i0_cost = INSN_COST (i0);
861 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
862 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
863 }
864 else
865 {
866 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
867 ? i1_cost + i2_cost + i3_cost : 0);
868 i0_cost = 0;
869 }
870 }
871 else
872 {
873 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
874 i1_cost = i0_cost = 0;
875 }
876
877 /* Calculate the replacement insn_rtx_costs. */
878 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
879 if (newi2pat)
880 {
881 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
882 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
883 ? new_i2_cost + new_i3_cost : 0;
884 }
885 else
886 {
887 new_cost = new_i3_cost;
888 new_i2_cost = 0;
889 }
890
891 if (undobuf.other_insn)
892 {
893 int old_other_cost, new_other_cost;
894
895 old_other_cost = INSN_COST (undobuf.other_insn);
896 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
897 if (old_other_cost > 0 && new_other_cost > 0)
898 {
899 old_cost += old_other_cost;
900 new_cost += new_other_cost;
901 }
902 else
903 old_cost = 0;
904 }
905
906 /* Disallow this combination if both new_cost and old_cost are greater than
907 zero, and new_cost is greater than old cost. */
908 if (old_cost > 0 && new_cost > old_cost)
909 {
910 if (dump_file)
911 {
912 if (i0)
913 {
914 fprintf (dump_file,
915 "rejecting combination of insns %d, %d, %d and %d\n",
916 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2),
917 INSN_UID (i3));
918 fprintf (dump_file, "original costs %d + %d + %d + %d = %d\n",
919 i0_cost, i1_cost, i2_cost, i3_cost, old_cost);
920 }
921 else if (i1)
922 {
923 fprintf (dump_file,
924 "rejecting combination of insns %d, %d and %d\n",
925 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
926 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
927 i1_cost, i2_cost, i3_cost, old_cost);
928 }
929 else
930 {
931 fprintf (dump_file,
932 "rejecting combination of insns %d and %d\n",
933 INSN_UID (i2), INSN_UID (i3));
934 fprintf (dump_file, "original costs %d + %d = %d\n",
935 i2_cost, i3_cost, old_cost);
936 }
937
938 if (newi2pat)
939 {
940 fprintf (dump_file, "replacement costs %d + %d = %d\n",
941 new_i2_cost, new_i3_cost, new_cost);
942 }
943 else
944 fprintf (dump_file, "replacement cost %d\n", new_cost);
945 }
946
947 return false;
948 }
949
950 /* Update the uid_insn_cost array with the replacement costs. */
951 INSN_COST (i2) = new_i2_cost;
952 INSN_COST (i3) = new_i3_cost;
953 if (i1)
954 {
955 INSN_COST (i1) = 0;
956 if (i0)
957 INSN_COST (i0) = 0;
958 }
959
960 return true;
961 }
962
963
964 /* Delete any insns that copy a register to itself. */
965
966 static void
967 delete_noop_moves (void)
968 {
969 rtx_insn *insn, *next;
970 basic_block bb;
971
972 FOR_EACH_BB_FN (bb, cfun)
973 {
974 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
975 {
976 next = NEXT_INSN (insn);
977 if (INSN_P (insn) && noop_move_p (insn))
978 {
979 if (dump_file)
980 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
981
982 delete_insn_and_edges (insn);
983 }
984 }
985 }
986 }
987
988 \f
989 /* Fill in log links field for all insns. */
990
991 static void
992 create_log_links (void)
993 {
994 basic_block bb;
995 rtx_insn **next_use;
996 rtx_insn *insn;
997 df_ref def, use;
998
999 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1000
1001 /* Pass through each block from the end, recording the uses of each
1002 register and establishing log links when def is encountered.
1003 Note that we do not clear next_use array in order to save time,
1004 so we have to test whether the use is in the same basic block as def.
1005
1006 There are a few cases below when we do not consider the definition or
1007 usage -- these are taken from original flow.c did. Don't ask me why it is
1008 done this way; I don't know and if it works, I don't want to know. */
1009
1010 FOR_EACH_BB_FN (bb, cfun)
1011 {
1012 FOR_BB_INSNS_REVERSE (bb, insn)
1013 {
1014 if (!NONDEBUG_INSN_P (insn))
1015 continue;
1016
1017 /* Log links are created only once. */
1018 gcc_assert (!LOG_LINKS (insn));
1019
1020 FOR_EACH_INSN_DEF (def, insn)
1021 {
1022 int regno = DF_REF_REGNO (def);
1023 rtx_insn *use_insn;
1024
1025 if (!next_use[regno])
1026 continue;
1027
1028 /* Do not consider if it is pre/post modification in MEM. */
1029 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1030 continue;
1031
1032 /* Do not make the log link for frame pointer. */
1033 if ((regno == FRAME_POINTER_REGNUM
1034 && (! reload_completed || frame_pointer_needed))
1035 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1036 || (regno == HARD_FRAME_POINTER_REGNUM
1037 && (! reload_completed || frame_pointer_needed))
1038 #endif
1039 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1040 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1041 #endif
1042 )
1043 continue;
1044
1045 use_insn = next_use[regno];
1046 if (BLOCK_FOR_INSN (use_insn) == bb)
1047 {
1048 /* flow.c claimed:
1049
1050 We don't build a LOG_LINK for hard registers contained
1051 in ASM_OPERANDs. If these registers get replaced,
1052 we might wind up changing the semantics of the insn,
1053 even if reload can make what appear to be valid
1054 assignments later. */
1055 if (regno >= FIRST_PSEUDO_REGISTER
1056 || asm_noperands (PATTERN (use_insn)) < 0)
1057 {
1058 /* Don't add duplicate links between instructions. */
1059 struct insn_link *links;
1060 FOR_EACH_LOG_LINK (links, use_insn)
1061 if (insn == links->insn)
1062 break;
1063
1064 if (!links)
1065 LOG_LINKS (use_insn)
1066 = alloc_insn_link (insn, LOG_LINKS (use_insn));
1067 }
1068 }
1069 next_use[regno] = NULL;
1070 }
1071
1072 FOR_EACH_INSN_USE (use, insn)
1073 {
1074 int regno = DF_REF_REGNO (use);
1075
1076 /* Do not consider the usage of the stack pointer
1077 by function call. */
1078 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1079 continue;
1080
1081 next_use[regno] = insn;
1082 }
1083 }
1084 }
1085
1086 free (next_use);
1087 }
1088
1089 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1090 true if we found a LOG_LINK that proves that A feeds B. This only works
1091 if there are no instructions between A and B which could have a link
1092 depending on A, since in that case we would not record a link for B.
1093 We also check the implicit dependency created by a cc0 setter/user
1094 pair. */
1095
1096 static bool
1097 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1098 {
1099 struct insn_link *links;
1100 FOR_EACH_LOG_LINK (links, b)
1101 if (links->insn == a)
1102 return true;
1103 #ifdef HAVE_cc0
1104 if (sets_cc0_p (a))
1105 return true;
1106 #endif
1107 return false;
1108 }
1109 \f
1110 /* Main entry point for combiner. F is the first insn of the function.
1111 NREGS is the first unused pseudo-reg number.
1112
1113 Return nonzero if the combiner has turned an indirect jump
1114 instruction into a direct jump. */
1115 static int
1116 combine_instructions (rtx_insn *f, unsigned int nregs)
1117 {
1118 rtx_insn *insn, *next;
1119 #ifdef HAVE_cc0
1120 rtx_insn *prev;
1121 #endif
1122 struct insn_link *links, *nextlinks;
1123 rtx_insn *first;
1124 basic_block last_bb;
1125
1126 int new_direct_jump_p = 0;
1127
1128 for (first = f; first && !INSN_P (first); )
1129 first = NEXT_INSN (first);
1130 if (!first)
1131 return 0;
1132
1133 combine_attempts = 0;
1134 combine_merges = 0;
1135 combine_extras = 0;
1136 combine_successes = 0;
1137
1138 rtl_hooks = combine_rtl_hooks;
1139
1140 reg_stat.safe_grow_cleared (nregs);
1141
1142 init_recog_no_volatile ();
1143
1144 /* Allocate array for insn info. */
1145 max_uid_known = get_max_uid ();
1146 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1147 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1148 gcc_obstack_init (&insn_link_obstack);
1149
1150 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1151
1152 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1153 problems when, for example, we have j <<= 1 in a loop. */
1154
1155 nonzero_sign_valid = 0;
1156 label_tick = label_tick_ebb_start = 1;
1157
1158 /* Scan all SETs and see if we can deduce anything about what
1159 bits are known to be zero for some registers and how many copies
1160 of the sign bit are known to exist for those registers.
1161
1162 Also set any known values so that we can use it while searching
1163 for what bits are known to be set. */
1164
1165 setup_incoming_promotions (first);
1166 /* Allow the entry block and the first block to fall into the same EBB.
1167 Conceptually the incoming promotions are assigned to the entry block. */
1168 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1169
1170 create_log_links ();
1171 FOR_EACH_BB_FN (this_basic_block, cfun)
1172 {
1173 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1174 last_call_luid = 0;
1175 mem_last_set = -1;
1176
1177 label_tick++;
1178 if (!single_pred_p (this_basic_block)
1179 || single_pred (this_basic_block) != last_bb)
1180 label_tick_ebb_start = label_tick;
1181 last_bb = this_basic_block;
1182
1183 FOR_BB_INSNS (this_basic_block, insn)
1184 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1185 {
1186 #ifdef AUTO_INC_DEC
1187 rtx links;
1188 #endif
1189
1190 subst_low_luid = DF_INSN_LUID (insn);
1191 subst_insn = insn;
1192
1193 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1194 insn);
1195 record_dead_and_set_regs (insn);
1196
1197 #ifdef AUTO_INC_DEC
1198 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1199 if (REG_NOTE_KIND (links) == REG_INC)
1200 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1201 insn);
1202 #endif
1203
1204 /* Record the current insn_rtx_cost of this instruction. */
1205 if (NONJUMP_INSN_P (insn))
1206 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1207 optimize_this_for_speed_p);
1208 if (dump_file)
1209 fprintf (dump_file, "insn_cost %d: %d\n",
1210 INSN_UID (insn), INSN_COST (insn));
1211 }
1212 }
1213
1214 nonzero_sign_valid = 1;
1215
1216 /* Now scan all the insns in forward order. */
1217 label_tick = label_tick_ebb_start = 1;
1218 init_reg_last ();
1219 setup_incoming_promotions (first);
1220 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1221 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1222
1223 FOR_EACH_BB_FN (this_basic_block, cfun)
1224 {
1225 rtx_insn *last_combined_insn = NULL;
1226 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1227 last_call_luid = 0;
1228 mem_last_set = -1;
1229
1230 label_tick++;
1231 if (!single_pred_p (this_basic_block)
1232 || single_pred (this_basic_block) != last_bb)
1233 label_tick_ebb_start = label_tick;
1234 last_bb = this_basic_block;
1235
1236 rtl_profile_for_bb (this_basic_block);
1237 for (insn = BB_HEAD (this_basic_block);
1238 insn != NEXT_INSN (BB_END (this_basic_block));
1239 insn = next ? next : NEXT_INSN (insn))
1240 {
1241 next = 0;
1242 if (!NONDEBUG_INSN_P (insn))
1243 continue;
1244
1245 while (last_combined_insn
1246 && last_combined_insn->deleted ())
1247 last_combined_insn = PREV_INSN (last_combined_insn);
1248 if (last_combined_insn == NULL_RTX
1249 || BARRIER_P (last_combined_insn)
1250 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1251 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1252 last_combined_insn = insn;
1253
1254 /* See if we know about function return values before this
1255 insn based upon SUBREG flags. */
1256 check_promoted_subreg (insn, PATTERN (insn));
1257
1258 /* See if we can find hardregs and subreg of pseudos in
1259 narrower modes. This could help turning TRUNCATEs
1260 into SUBREGs. */
1261 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1262
1263 /* Try this insn with each insn it links back to. */
1264
1265 FOR_EACH_LOG_LINK (links, insn)
1266 if ((next = try_combine (insn, links->insn, NULL,
1267 NULL, &new_direct_jump_p,
1268 last_combined_insn)) != 0)
1269 {
1270 statistics_counter_event (cfun, "two-insn combine", 1);
1271 goto retry;
1272 }
1273
1274 /* Try each sequence of three linked insns ending with this one. */
1275
1276 if (max_combine >= 3)
1277 FOR_EACH_LOG_LINK (links, insn)
1278 {
1279 rtx_insn *link = links->insn;
1280
1281 /* If the linked insn has been replaced by a note, then there
1282 is no point in pursuing this chain any further. */
1283 if (NOTE_P (link))
1284 continue;
1285
1286 FOR_EACH_LOG_LINK (nextlinks, link)
1287 if ((next = try_combine (insn, link, nextlinks->insn,
1288 NULL, &new_direct_jump_p,
1289 last_combined_insn)) != 0)
1290 {
1291 statistics_counter_event (cfun, "three-insn combine", 1);
1292 goto retry;
1293 }
1294 }
1295
1296 #ifdef HAVE_cc0
1297 /* Try to combine a jump insn that uses CC0
1298 with a preceding insn that sets CC0, and maybe with its
1299 logical predecessor as well.
1300 This is how we make decrement-and-branch insns.
1301 We need this special code because data flow connections
1302 via CC0 do not get entered in LOG_LINKS. */
1303
1304 if (JUMP_P (insn)
1305 && (prev = prev_nonnote_insn (insn)) != 0
1306 && NONJUMP_INSN_P (prev)
1307 && sets_cc0_p (PATTERN (prev)))
1308 {
1309 if ((next = try_combine (insn, prev, NULL, NULL,
1310 &new_direct_jump_p,
1311 last_combined_insn)) != 0)
1312 goto retry;
1313
1314 FOR_EACH_LOG_LINK (nextlinks, prev)
1315 if ((next = try_combine (insn, prev, nextlinks->insn,
1316 NULL, &new_direct_jump_p,
1317 last_combined_insn)) != 0)
1318 goto retry;
1319 }
1320
1321 /* Do the same for an insn that explicitly references CC0. */
1322 if (NONJUMP_INSN_P (insn)
1323 && (prev = prev_nonnote_insn (insn)) != 0
1324 && NONJUMP_INSN_P (prev)
1325 && sets_cc0_p (PATTERN (prev))
1326 && GET_CODE (PATTERN (insn)) == SET
1327 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1328 {
1329 if ((next = try_combine (insn, prev, NULL, NULL,
1330 &new_direct_jump_p,
1331 last_combined_insn)) != 0)
1332 goto retry;
1333
1334 FOR_EACH_LOG_LINK (nextlinks, prev)
1335 if ((next = try_combine (insn, prev, nextlinks->insn,
1336 NULL, &new_direct_jump_p,
1337 last_combined_insn)) != 0)
1338 goto retry;
1339 }
1340
1341 /* Finally, see if any of the insns that this insn links to
1342 explicitly references CC0. If so, try this insn, that insn,
1343 and its predecessor if it sets CC0. */
1344 FOR_EACH_LOG_LINK (links, insn)
1345 if (NONJUMP_INSN_P (links->insn)
1346 && GET_CODE (PATTERN (links->insn)) == SET
1347 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1348 && (prev = prev_nonnote_insn (links->insn)) != 0
1349 && NONJUMP_INSN_P (prev)
1350 && sets_cc0_p (PATTERN (prev))
1351 && (next = try_combine (insn, links->insn,
1352 prev, NULL, &new_direct_jump_p,
1353 last_combined_insn)) != 0)
1354 goto retry;
1355 #endif
1356
1357 /* Try combining an insn with two different insns whose results it
1358 uses. */
1359 if (max_combine >= 3)
1360 FOR_EACH_LOG_LINK (links, insn)
1361 for (nextlinks = links->next; nextlinks;
1362 nextlinks = nextlinks->next)
1363 if ((next = try_combine (insn, links->insn,
1364 nextlinks->insn, NULL,
1365 &new_direct_jump_p,
1366 last_combined_insn)) != 0)
1367
1368 {
1369 statistics_counter_event (cfun, "three-insn combine", 1);
1370 goto retry;
1371 }
1372
1373 /* Try four-instruction combinations. */
1374 if (max_combine >= 4)
1375 FOR_EACH_LOG_LINK (links, insn)
1376 {
1377 struct insn_link *next1;
1378 rtx_insn *link = links->insn;
1379
1380 /* If the linked insn has been replaced by a note, then there
1381 is no point in pursuing this chain any further. */
1382 if (NOTE_P (link))
1383 continue;
1384
1385 FOR_EACH_LOG_LINK (next1, link)
1386 {
1387 rtx_insn *link1 = next1->insn;
1388 if (NOTE_P (link1))
1389 continue;
1390 /* I0 -> I1 -> I2 -> I3. */
1391 FOR_EACH_LOG_LINK (nextlinks, link1)
1392 if ((next = try_combine (insn, link, link1,
1393 nextlinks->insn,
1394 &new_direct_jump_p,
1395 last_combined_insn)) != 0)
1396 {
1397 statistics_counter_event (cfun, "four-insn combine", 1);
1398 goto retry;
1399 }
1400 /* I0, I1 -> I2, I2 -> I3. */
1401 for (nextlinks = next1->next; nextlinks;
1402 nextlinks = nextlinks->next)
1403 if ((next = try_combine (insn, link, link1,
1404 nextlinks->insn,
1405 &new_direct_jump_p,
1406 last_combined_insn)) != 0)
1407 {
1408 statistics_counter_event (cfun, "four-insn combine", 1);
1409 goto retry;
1410 }
1411 }
1412
1413 for (next1 = links->next; next1; next1 = next1->next)
1414 {
1415 rtx_insn *link1 = next1->insn;
1416 if (NOTE_P (link1))
1417 continue;
1418 /* I0 -> I2; I1, I2 -> I3. */
1419 FOR_EACH_LOG_LINK (nextlinks, link)
1420 if ((next = try_combine (insn, link, link1,
1421 nextlinks->insn,
1422 &new_direct_jump_p,
1423 last_combined_insn)) != 0)
1424 {
1425 statistics_counter_event (cfun, "four-insn combine", 1);
1426 goto retry;
1427 }
1428 /* I0 -> I1; I1, I2 -> I3. */
1429 FOR_EACH_LOG_LINK (nextlinks, link1)
1430 if ((next = try_combine (insn, link, link1,
1431 nextlinks->insn,
1432 &new_direct_jump_p,
1433 last_combined_insn)) != 0)
1434 {
1435 statistics_counter_event (cfun, "four-insn combine", 1);
1436 goto retry;
1437 }
1438 }
1439 }
1440
1441 /* Try this insn with each REG_EQUAL note it links back to. */
1442 FOR_EACH_LOG_LINK (links, insn)
1443 {
1444 rtx set, note;
1445 rtx_insn *temp = links->insn;
1446 if ((set = single_set (temp)) != 0
1447 && (note = find_reg_equal_equiv_note (temp)) != 0
1448 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1449 /* Avoid using a register that may already been marked
1450 dead by an earlier instruction. */
1451 && ! unmentioned_reg_p (note, SET_SRC (set))
1452 && (GET_MODE (note) == VOIDmode
1453 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1454 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1455 {
1456 /* Temporarily replace the set's source with the
1457 contents of the REG_EQUAL note. The insn will
1458 be deleted or recognized by try_combine. */
1459 rtx orig = SET_SRC (set);
1460 SET_SRC (set) = note;
1461 i2mod = temp;
1462 i2mod_old_rhs = copy_rtx (orig);
1463 i2mod_new_rhs = copy_rtx (note);
1464 next = try_combine (insn, i2mod, NULL, NULL,
1465 &new_direct_jump_p,
1466 last_combined_insn);
1467 i2mod = NULL;
1468 if (next)
1469 {
1470 statistics_counter_event (cfun, "insn-with-note combine", 1);
1471 goto retry;
1472 }
1473 SET_SRC (set) = orig;
1474 }
1475 }
1476
1477 if (!NOTE_P (insn))
1478 record_dead_and_set_regs (insn);
1479
1480 retry:
1481 ;
1482 }
1483 }
1484
1485 default_rtl_profile ();
1486 clear_bb_flags ();
1487 new_direct_jump_p |= purge_all_dead_edges ();
1488 delete_noop_moves ();
1489
1490 /* Clean up. */
1491 obstack_free (&insn_link_obstack, NULL);
1492 free (uid_log_links);
1493 free (uid_insn_cost);
1494 reg_stat.release ();
1495
1496 {
1497 struct undo *undo, *next;
1498 for (undo = undobuf.frees; undo; undo = next)
1499 {
1500 next = undo->next;
1501 free (undo);
1502 }
1503 undobuf.frees = 0;
1504 }
1505
1506 total_attempts += combine_attempts;
1507 total_merges += combine_merges;
1508 total_extras += combine_extras;
1509 total_successes += combine_successes;
1510
1511 nonzero_sign_valid = 0;
1512 rtl_hooks = general_rtl_hooks;
1513
1514 /* Make recognizer allow volatile MEMs again. */
1515 init_recog ();
1516
1517 return new_direct_jump_p;
1518 }
1519
1520 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1521
1522 static void
1523 init_reg_last (void)
1524 {
1525 unsigned int i;
1526 reg_stat_type *p;
1527
1528 FOR_EACH_VEC_ELT (reg_stat, i, p)
1529 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1530 }
1531 \f
1532 /* Set up any promoted values for incoming argument registers. */
1533
1534 static void
1535 setup_incoming_promotions (rtx_insn *first)
1536 {
1537 tree arg;
1538 bool strictly_local = false;
1539
1540 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1541 arg = DECL_CHAIN (arg))
1542 {
1543 rtx x, reg = DECL_INCOMING_RTL (arg);
1544 int uns1, uns3;
1545 enum machine_mode mode1, mode2, mode3, mode4;
1546
1547 /* Only continue if the incoming argument is in a register. */
1548 if (!REG_P (reg))
1549 continue;
1550
1551 /* Determine, if possible, whether all call sites of the current
1552 function lie within the current compilation unit. (This does
1553 take into account the exporting of a function via taking its
1554 address, and so forth.) */
1555 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1556
1557 /* The mode and signedness of the argument before any promotions happen
1558 (equal to the mode of the pseudo holding it at that stage). */
1559 mode1 = TYPE_MODE (TREE_TYPE (arg));
1560 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1561
1562 /* The mode and signedness of the argument after any source language and
1563 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1564 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1565 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1566
1567 /* The mode and signedness of the argument as it is actually passed,
1568 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1569 mode3 = promote_function_mode (DECL_ARG_TYPE (arg), mode2, &uns3,
1570 TREE_TYPE (cfun->decl), 0);
1571
1572 /* The mode of the register in which the argument is being passed. */
1573 mode4 = GET_MODE (reg);
1574
1575 /* Eliminate sign extensions in the callee when:
1576 (a) A mode promotion has occurred; */
1577 if (mode1 == mode3)
1578 continue;
1579 /* (b) The mode of the register is the same as the mode of
1580 the argument as it is passed; */
1581 if (mode3 != mode4)
1582 continue;
1583 /* (c) There's no language level extension; */
1584 if (mode1 == mode2)
1585 ;
1586 /* (c.1) All callers are from the current compilation unit. If that's
1587 the case we don't have to rely on an ABI, we only have to know
1588 what we're generating right now, and we know that we will do the
1589 mode1 to mode2 promotion with the given sign. */
1590 else if (!strictly_local)
1591 continue;
1592 /* (c.2) The combination of the two promotions is useful. This is
1593 true when the signs match, or if the first promotion is unsigned.
1594 In the later case, (sign_extend (zero_extend x)) is the same as
1595 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1596 else if (uns1)
1597 uns3 = true;
1598 else if (uns3)
1599 continue;
1600
1601 /* Record that the value was promoted from mode1 to mode3,
1602 so that any sign extension at the head of the current
1603 function may be eliminated. */
1604 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1605 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1606 record_value_for_reg (reg, first, x);
1607 }
1608 }
1609
1610 /* Called via note_stores. If X is a pseudo that is narrower than
1611 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1612
1613 If we are setting only a portion of X and we can't figure out what
1614 portion, assume all bits will be used since we don't know what will
1615 be happening.
1616
1617 Similarly, set how many bits of X are known to be copies of the sign bit
1618 at all locations in the function. This is the smallest number implied
1619 by any set of X. */
1620
1621 static void
1622 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1623 {
1624 rtx_insn *insn = (rtx_insn *) data;
1625 unsigned int num;
1626
1627 if (REG_P (x)
1628 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1629 /* If this register is undefined at the start of the file, we can't
1630 say what its contents were. */
1631 && ! REGNO_REG_SET_P
1632 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1633 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1634 {
1635 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1636
1637 if (set == 0 || GET_CODE (set) == CLOBBER)
1638 {
1639 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1640 rsp->sign_bit_copies = 1;
1641 return;
1642 }
1643
1644 /* If this register is being initialized using itself, and the
1645 register is uninitialized in this basic block, and there are
1646 no LOG_LINKS which set the register, then part of the
1647 register is uninitialized. In that case we can't assume
1648 anything about the number of nonzero bits.
1649
1650 ??? We could do better if we checked this in
1651 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1652 could avoid making assumptions about the insn which initially
1653 sets the register, while still using the information in other
1654 insns. We would have to be careful to check every insn
1655 involved in the combination. */
1656
1657 if (insn
1658 && reg_referenced_p (x, PATTERN (insn))
1659 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1660 REGNO (x)))
1661 {
1662 struct insn_link *link;
1663
1664 FOR_EACH_LOG_LINK (link, insn)
1665 if (dead_or_set_p (link->insn, x))
1666 break;
1667 if (!link)
1668 {
1669 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1670 rsp->sign_bit_copies = 1;
1671 return;
1672 }
1673 }
1674
1675 /* If this is a complex assignment, see if we can convert it into a
1676 simple assignment. */
1677 set = expand_field_assignment (set);
1678
1679 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1680 set what we know about X. */
1681
1682 if (SET_DEST (set) == x
1683 || (paradoxical_subreg_p (SET_DEST (set))
1684 && SUBREG_REG (SET_DEST (set)) == x))
1685 {
1686 rtx src = SET_SRC (set);
1687
1688 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1689 /* If X is narrower than a word and SRC is a non-negative
1690 constant that would appear negative in the mode of X,
1691 sign-extend it for use in reg_stat[].nonzero_bits because some
1692 machines (maybe most) will actually do the sign-extension
1693 and this is the conservative approach.
1694
1695 ??? For 2.5, try to tighten up the MD files in this regard
1696 instead of this kludge. */
1697
1698 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1699 && CONST_INT_P (src)
1700 && INTVAL (src) > 0
1701 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1702 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1703 #endif
1704
1705 /* Don't call nonzero_bits if it cannot change anything. */
1706 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1707 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1708 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1709 if (rsp->sign_bit_copies == 0
1710 || rsp->sign_bit_copies > num)
1711 rsp->sign_bit_copies = num;
1712 }
1713 else
1714 {
1715 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1716 rsp->sign_bit_copies = 1;
1717 }
1718 }
1719 }
1720 \f
1721 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1722 optionally insns that were previously combined into I3 or that will be
1723 combined into the merger of INSN and I3. The order is PRED, PRED2,
1724 INSN, SUCC, SUCC2, I3.
1725
1726 Return 0 if the combination is not allowed for any reason.
1727
1728 If the combination is allowed, *PDEST will be set to the single
1729 destination of INSN and *PSRC to the single source, and this function
1730 will return 1. */
1731
1732 static int
1733 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1734 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1735 rtx *pdest, rtx *psrc)
1736 {
1737 int i;
1738 const_rtx set = 0;
1739 rtx src, dest;
1740 rtx_insn *p;
1741 #ifdef AUTO_INC_DEC
1742 rtx link;
1743 #endif
1744 bool all_adjacent = true;
1745 int (*is_volatile_p) (const_rtx);
1746
1747 if (succ)
1748 {
1749 if (succ2)
1750 {
1751 if (next_active_insn (succ2) != i3)
1752 all_adjacent = false;
1753 if (next_active_insn (succ) != succ2)
1754 all_adjacent = false;
1755 }
1756 else if (next_active_insn (succ) != i3)
1757 all_adjacent = false;
1758 if (next_active_insn (insn) != succ)
1759 all_adjacent = false;
1760 }
1761 else if (next_active_insn (insn) != i3)
1762 all_adjacent = false;
1763
1764 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1765 or a PARALLEL consisting of such a SET and CLOBBERs.
1766
1767 If INSN has CLOBBER parallel parts, ignore them for our processing.
1768 By definition, these happen during the execution of the insn. When it
1769 is merged with another insn, all bets are off. If they are, in fact,
1770 needed and aren't also supplied in I3, they may be added by
1771 recog_for_combine. Otherwise, it won't match.
1772
1773 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1774 note.
1775
1776 Get the source and destination of INSN. If more than one, can't
1777 combine. */
1778
1779 if (GET_CODE (PATTERN (insn)) == SET)
1780 set = PATTERN (insn);
1781 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1782 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1783 {
1784 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1785 {
1786 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1787
1788 switch (GET_CODE (elt))
1789 {
1790 /* This is important to combine floating point insns
1791 for the SH4 port. */
1792 case USE:
1793 /* Combining an isolated USE doesn't make sense.
1794 We depend here on combinable_i3pat to reject them. */
1795 /* The code below this loop only verifies that the inputs of
1796 the SET in INSN do not change. We call reg_set_between_p
1797 to verify that the REG in the USE does not change between
1798 I3 and INSN.
1799 If the USE in INSN was for a pseudo register, the matching
1800 insn pattern will likely match any register; combining this
1801 with any other USE would only be safe if we knew that the
1802 used registers have identical values, or if there was
1803 something to tell them apart, e.g. different modes. For
1804 now, we forgo such complicated tests and simply disallow
1805 combining of USES of pseudo registers with any other USE. */
1806 if (REG_P (XEXP (elt, 0))
1807 && GET_CODE (PATTERN (i3)) == PARALLEL)
1808 {
1809 rtx i3pat = PATTERN (i3);
1810 int i = XVECLEN (i3pat, 0) - 1;
1811 unsigned int regno = REGNO (XEXP (elt, 0));
1812
1813 do
1814 {
1815 rtx i3elt = XVECEXP (i3pat, 0, i);
1816
1817 if (GET_CODE (i3elt) == USE
1818 && REG_P (XEXP (i3elt, 0))
1819 && (REGNO (XEXP (i3elt, 0)) == regno
1820 ? reg_set_between_p (XEXP (elt, 0),
1821 PREV_INSN (insn), i3)
1822 : regno >= FIRST_PSEUDO_REGISTER))
1823 return 0;
1824 }
1825 while (--i >= 0);
1826 }
1827 break;
1828
1829 /* We can ignore CLOBBERs. */
1830 case CLOBBER:
1831 break;
1832
1833 case SET:
1834 /* Ignore SETs whose result isn't used but not those that
1835 have side-effects. */
1836 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1837 && insn_nothrow_p (insn)
1838 && !side_effects_p (elt))
1839 break;
1840
1841 /* If we have already found a SET, this is a second one and
1842 so we cannot combine with this insn. */
1843 if (set)
1844 return 0;
1845
1846 set = elt;
1847 break;
1848
1849 default:
1850 /* Anything else means we can't combine. */
1851 return 0;
1852 }
1853 }
1854
1855 if (set == 0
1856 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1857 so don't do anything with it. */
1858 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1859 return 0;
1860 }
1861 else
1862 return 0;
1863
1864 if (set == 0)
1865 return 0;
1866
1867 /* The simplification in expand_field_assignment may call back to
1868 get_last_value, so set safe guard here. */
1869 subst_low_luid = DF_INSN_LUID (insn);
1870
1871 set = expand_field_assignment (set);
1872 src = SET_SRC (set), dest = SET_DEST (set);
1873
1874 /* Don't eliminate a store in the stack pointer. */
1875 if (dest == stack_pointer_rtx
1876 /* Don't combine with an insn that sets a register to itself if it has
1877 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1878 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1879 /* Can't merge an ASM_OPERANDS. */
1880 || GET_CODE (src) == ASM_OPERANDS
1881 /* Can't merge a function call. */
1882 || GET_CODE (src) == CALL
1883 /* Don't eliminate a function call argument. */
1884 || (CALL_P (i3)
1885 && (find_reg_fusage (i3, USE, dest)
1886 || (REG_P (dest)
1887 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1888 && global_regs[REGNO (dest)])))
1889 /* Don't substitute into an incremented register. */
1890 || FIND_REG_INC_NOTE (i3, dest)
1891 || (succ && FIND_REG_INC_NOTE (succ, dest))
1892 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1893 /* Don't substitute into a non-local goto, this confuses CFG. */
1894 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1895 /* Make sure that DEST is not used after SUCC but before I3. */
1896 || (!all_adjacent
1897 && ((succ2
1898 && (reg_used_between_p (dest, succ2, i3)
1899 || reg_used_between_p (dest, succ, succ2)))
1900 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1901 /* Make sure that the value that is to be substituted for the register
1902 does not use any registers whose values alter in between. However,
1903 If the insns are adjacent, a use can't cross a set even though we
1904 think it might (this can happen for a sequence of insns each setting
1905 the same destination; last_set of that register might point to
1906 a NOTE). If INSN has a REG_EQUIV note, the register is always
1907 equivalent to the memory so the substitution is valid even if there
1908 are intervening stores. Also, don't move a volatile asm or
1909 UNSPEC_VOLATILE across any other insns. */
1910 || (! all_adjacent
1911 && (((!MEM_P (src)
1912 || ! find_reg_note (insn, REG_EQUIV, src))
1913 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1914 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1915 || GET_CODE (src) == UNSPEC_VOLATILE))
1916 /* Don't combine across a CALL_INSN, because that would possibly
1917 change whether the life span of some REGs crosses calls or not,
1918 and it is a pain to update that information.
1919 Exception: if source is a constant, moving it later can't hurt.
1920 Accept that as a special case. */
1921 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1922 return 0;
1923
1924 /* DEST must either be a REG or CC0. */
1925 if (REG_P (dest))
1926 {
1927 /* If register alignment is being enforced for multi-word items in all
1928 cases except for parameters, it is possible to have a register copy
1929 insn referencing a hard register that is not allowed to contain the
1930 mode being copied and which would not be valid as an operand of most
1931 insns. Eliminate this problem by not combining with such an insn.
1932
1933 Also, on some machines we don't want to extend the life of a hard
1934 register. */
1935
1936 if (REG_P (src)
1937 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1938 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1939 /* Don't extend the life of a hard register unless it is
1940 user variable (if we have few registers) or it can't
1941 fit into the desired register (meaning something special
1942 is going on).
1943 Also avoid substituting a return register into I3, because
1944 reload can't handle a conflict with constraints of other
1945 inputs. */
1946 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1947 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1948 return 0;
1949 }
1950 else if (GET_CODE (dest) != CC0)
1951 return 0;
1952
1953
1954 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1955 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1956 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1957 {
1958 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1959
1960 /* If the clobber represents an earlyclobber operand, we must not
1961 substitute an expression containing the clobbered register.
1962 As we do not analyze the constraint strings here, we have to
1963 make the conservative assumption. However, if the register is
1964 a fixed hard reg, the clobber cannot represent any operand;
1965 we leave it up to the machine description to either accept or
1966 reject use-and-clobber patterns. */
1967 if (!REG_P (reg)
1968 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1969 || !fixed_regs[REGNO (reg)])
1970 if (reg_overlap_mentioned_p (reg, src))
1971 return 0;
1972 }
1973
1974 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1975 or not), reject, unless nothing volatile comes between it and I3 */
1976
1977 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1978 {
1979 /* Make sure neither succ nor succ2 contains a volatile reference. */
1980 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
1981 return 0;
1982 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1983 return 0;
1984 /* We'll check insns between INSN and I3 below. */
1985 }
1986
1987 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1988 to be an explicit register variable, and was chosen for a reason. */
1989
1990 if (GET_CODE (src) == ASM_OPERANDS
1991 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1992 return 0;
1993
1994 /* If INSN contains volatile references (specifically volatile MEMs),
1995 we cannot combine across any other volatile references.
1996 Even if INSN doesn't contain volatile references, any intervening
1997 volatile insn might affect machine state. */
1998
1999 is_volatile_p = volatile_refs_p (PATTERN (insn))
2000 ? volatile_refs_p
2001 : volatile_insn_p;
2002
2003 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2004 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2005 return 0;
2006
2007 /* If INSN contains an autoincrement or autodecrement, make sure that
2008 register is not used between there and I3, and not already used in
2009 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2010 Also insist that I3 not be a jump; if it were one
2011 and the incremented register were spilled, we would lose. */
2012
2013 #ifdef AUTO_INC_DEC
2014 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2015 if (REG_NOTE_KIND (link) == REG_INC
2016 && (JUMP_P (i3)
2017 || reg_used_between_p (XEXP (link, 0), insn, i3)
2018 || (pred != NULL_RTX
2019 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2020 || (pred2 != NULL_RTX
2021 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2022 || (succ != NULL_RTX
2023 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2024 || (succ2 != NULL_RTX
2025 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2026 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2027 return 0;
2028 #endif
2029
2030 #ifdef HAVE_cc0
2031 /* Don't combine an insn that follows a CC0-setting insn.
2032 An insn that uses CC0 must not be separated from the one that sets it.
2033 We do, however, allow I2 to follow a CC0-setting insn if that insn
2034 is passed as I1; in that case it will be deleted also.
2035 We also allow combining in this case if all the insns are adjacent
2036 because that would leave the two CC0 insns adjacent as well.
2037 It would be more logical to test whether CC0 occurs inside I1 or I2,
2038 but that would be much slower, and this ought to be equivalent. */
2039
2040 p = prev_nonnote_insn (insn);
2041 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2042 && ! all_adjacent)
2043 return 0;
2044 #endif
2045
2046 /* If we get here, we have passed all the tests and the combination is
2047 to be allowed. */
2048
2049 *pdest = dest;
2050 *psrc = src;
2051
2052 return 1;
2053 }
2054 \f
2055 /* LOC is the location within I3 that contains its pattern or the component
2056 of a PARALLEL of the pattern. We validate that it is valid for combining.
2057
2058 One problem is if I3 modifies its output, as opposed to replacing it
2059 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2060 doing so would produce an insn that is not equivalent to the original insns.
2061
2062 Consider:
2063
2064 (set (reg:DI 101) (reg:DI 100))
2065 (set (subreg:SI (reg:DI 101) 0) <foo>)
2066
2067 This is NOT equivalent to:
2068
2069 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2070 (set (reg:DI 101) (reg:DI 100))])
2071
2072 Not only does this modify 100 (in which case it might still be valid
2073 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2074
2075 We can also run into a problem if I2 sets a register that I1
2076 uses and I1 gets directly substituted into I3 (not via I2). In that
2077 case, we would be getting the wrong value of I2DEST into I3, so we
2078 must reject the combination. This case occurs when I2 and I1 both
2079 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2080 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2081 of a SET must prevent combination from occurring. The same situation
2082 can occur for I0, in which case I0_NOT_IN_SRC is set.
2083
2084 Before doing the above check, we first try to expand a field assignment
2085 into a set of logical operations.
2086
2087 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2088 we place a register that is both set and used within I3. If more than one
2089 such register is detected, we fail.
2090
2091 Return 1 if the combination is valid, zero otherwise. */
2092
2093 static int
2094 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2095 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2096 {
2097 rtx x = *loc;
2098
2099 if (GET_CODE (x) == SET)
2100 {
2101 rtx set = x ;
2102 rtx dest = SET_DEST (set);
2103 rtx src = SET_SRC (set);
2104 rtx inner_dest = dest;
2105 rtx subdest;
2106
2107 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2108 || GET_CODE (inner_dest) == SUBREG
2109 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2110 inner_dest = XEXP (inner_dest, 0);
2111
2112 /* Check for the case where I3 modifies its output, as discussed
2113 above. We don't want to prevent pseudos from being combined
2114 into the address of a MEM, so only prevent the combination if
2115 i1 or i2 set the same MEM. */
2116 if ((inner_dest != dest &&
2117 (!MEM_P (inner_dest)
2118 || rtx_equal_p (i2dest, inner_dest)
2119 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2120 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2121 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2122 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2123 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2124
2125 /* This is the same test done in can_combine_p except we can't test
2126 all_adjacent; we don't have to, since this instruction will stay
2127 in place, thus we are not considering increasing the lifetime of
2128 INNER_DEST.
2129
2130 Also, if this insn sets a function argument, combining it with
2131 something that might need a spill could clobber a previous
2132 function argument; the all_adjacent test in can_combine_p also
2133 checks this; here, we do a more specific test for this case. */
2134
2135 || (REG_P (inner_dest)
2136 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2137 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2138 GET_MODE (inner_dest))))
2139 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2140 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2141 return 0;
2142
2143 /* If DEST is used in I3, it is being killed in this insn, so
2144 record that for later. We have to consider paradoxical
2145 subregs here, since they kill the whole register, but we
2146 ignore partial subregs, STRICT_LOW_PART, etc.
2147 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2148 STACK_POINTER_REGNUM, since these are always considered to be
2149 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2150 subdest = dest;
2151 if (GET_CODE (subdest) == SUBREG
2152 && (GET_MODE_SIZE (GET_MODE (subdest))
2153 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2154 subdest = SUBREG_REG (subdest);
2155 if (pi3dest_killed
2156 && REG_P (subdest)
2157 && reg_referenced_p (subdest, PATTERN (i3))
2158 && REGNO (subdest) != FRAME_POINTER_REGNUM
2159 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2160 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2161 #endif
2162 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2163 && (REGNO (subdest) != ARG_POINTER_REGNUM
2164 || ! fixed_regs [REGNO (subdest)])
2165 #endif
2166 && REGNO (subdest) != STACK_POINTER_REGNUM)
2167 {
2168 if (*pi3dest_killed)
2169 return 0;
2170
2171 *pi3dest_killed = subdest;
2172 }
2173 }
2174
2175 else if (GET_CODE (x) == PARALLEL)
2176 {
2177 int i;
2178
2179 for (i = 0; i < XVECLEN (x, 0); i++)
2180 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2181 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2182 return 0;
2183 }
2184
2185 return 1;
2186 }
2187 \f
2188 /* Return 1 if X is an arithmetic expression that contains a multiplication
2189 and division. We don't count multiplications by powers of two here. */
2190
2191 static int
2192 contains_muldiv (rtx x)
2193 {
2194 switch (GET_CODE (x))
2195 {
2196 case MOD: case DIV: case UMOD: case UDIV:
2197 return 1;
2198
2199 case MULT:
2200 return ! (CONST_INT_P (XEXP (x, 1))
2201 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2202 default:
2203 if (BINARY_P (x))
2204 return contains_muldiv (XEXP (x, 0))
2205 || contains_muldiv (XEXP (x, 1));
2206
2207 if (UNARY_P (x))
2208 return contains_muldiv (XEXP (x, 0));
2209
2210 return 0;
2211 }
2212 }
2213 \f
2214 /* Determine whether INSN can be used in a combination. Return nonzero if
2215 not. This is used in try_combine to detect early some cases where we
2216 can't perform combinations. */
2217
2218 static int
2219 cant_combine_insn_p (rtx_insn *insn)
2220 {
2221 rtx set;
2222 rtx src, dest;
2223
2224 /* If this isn't really an insn, we can't do anything.
2225 This can occur when flow deletes an insn that it has merged into an
2226 auto-increment address. */
2227 if (! INSN_P (insn))
2228 return 1;
2229
2230 /* Never combine loads and stores involving hard regs that are likely
2231 to be spilled. The register allocator can usually handle such
2232 reg-reg moves by tying. If we allow the combiner to make
2233 substitutions of likely-spilled regs, reload might die.
2234 As an exception, we allow combinations involving fixed regs; these are
2235 not available to the register allocator so there's no risk involved. */
2236
2237 set = single_set (insn);
2238 if (! set)
2239 return 0;
2240 src = SET_SRC (set);
2241 dest = SET_DEST (set);
2242 if (GET_CODE (src) == SUBREG)
2243 src = SUBREG_REG (src);
2244 if (GET_CODE (dest) == SUBREG)
2245 dest = SUBREG_REG (dest);
2246 if (REG_P (src) && REG_P (dest)
2247 && ((HARD_REGISTER_P (src)
2248 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2249 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2250 || (HARD_REGISTER_P (dest)
2251 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2252 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2253 return 1;
2254
2255 return 0;
2256 }
2257
2258 struct likely_spilled_retval_info
2259 {
2260 unsigned regno, nregs;
2261 unsigned mask;
2262 };
2263
2264 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2265 hard registers that are known to be written to / clobbered in full. */
2266 static void
2267 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2268 {
2269 struct likely_spilled_retval_info *const info =
2270 (struct likely_spilled_retval_info *) data;
2271 unsigned regno, nregs;
2272 unsigned new_mask;
2273
2274 if (!REG_P (XEXP (set, 0)))
2275 return;
2276 regno = REGNO (x);
2277 if (regno >= info->regno + info->nregs)
2278 return;
2279 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2280 if (regno + nregs <= info->regno)
2281 return;
2282 new_mask = (2U << (nregs - 1)) - 1;
2283 if (regno < info->regno)
2284 new_mask >>= info->regno - regno;
2285 else
2286 new_mask <<= regno - info->regno;
2287 info->mask &= ~new_mask;
2288 }
2289
2290 /* Return nonzero iff part of the return value is live during INSN, and
2291 it is likely spilled. This can happen when more than one insn is needed
2292 to copy the return value, e.g. when we consider to combine into the
2293 second copy insn for a complex value. */
2294
2295 static int
2296 likely_spilled_retval_p (rtx_insn *insn)
2297 {
2298 rtx_insn *use = BB_END (this_basic_block);
2299 rtx reg;
2300 rtx_insn *p;
2301 unsigned regno, nregs;
2302 /* We assume here that no machine mode needs more than
2303 32 hard registers when the value overlaps with a register
2304 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2305 unsigned mask;
2306 struct likely_spilled_retval_info info;
2307
2308 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2309 return 0;
2310 reg = XEXP (PATTERN (use), 0);
2311 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2312 return 0;
2313 regno = REGNO (reg);
2314 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2315 if (nregs == 1)
2316 return 0;
2317 mask = (2U << (nregs - 1)) - 1;
2318
2319 /* Disregard parts of the return value that are set later. */
2320 info.regno = regno;
2321 info.nregs = nregs;
2322 info.mask = mask;
2323 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2324 if (INSN_P (p))
2325 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2326 mask = info.mask;
2327
2328 /* Check if any of the (probably) live return value registers is
2329 likely spilled. */
2330 nregs --;
2331 do
2332 {
2333 if ((mask & 1 << nregs)
2334 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2335 return 1;
2336 } while (nregs--);
2337 return 0;
2338 }
2339
2340 /* Adjust INSN after we made a change to its destination.
2341
2342 Changing the destination can invalidate notes that say something about
2343 the results of the insn and a LOG_LINK pointing to the insn. */
2344
2345 static void
2346 adjust_for_new_dest (rtx_insn *insn)
2347 {
2348 /* For notes, be conservative and simply remove them. */
2349 remove_reg_equal_equiv_notes (insn);
2350
2351 /* The new insn will have a destination that was previously the destination
2352 of an insn just above it. Call distribute_links to make a LOG_LINK from
2353 the next use of that destination. */
2354 distribute_links (alloc_insn_link (insn, NULL));
2355
2356 df_insn_rescan (insn);
2357 }
2358
2359 /* Return TRUE if combine can reuse reg X in mode MODE.
2360 ADDED_SETS is nonzero if the original set is still required. */
2361 static bool
2362 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2363 {
2364 unsigned int regno;
2365
2366 if (!REG_P (x))
2367 return false;
2368
2369 regno = REGNO (x);
2370 /* Allow hard registers if the new mode is legal, and occupies no more
2371 registers than the old mode. */
2372 if (regno < FIRST_PSEUDO_REGISTER)
2373 return (HARD_REGNO_MODE_OK (regno, mode)
2374 && (hard_regno_nregs[regno][GET_MODE (x)]
2375 >= hard_regno_nregs[regno][mode]));
2376
2377 /* Or a pseudo that is only used once. */
2378 return (REG_N_SETS (regno) == 1 && !added_sets
2379 && !REG_USERVAR_P (x));
2380 }
2381
2382
2383 /* Check whether X, the destination of a set, refers to part of
2384 the register specified by REG. */
2385
2386 static bool
2387 reg_subword_p (rtx x, rtx reg)
2388 {
2389 /* Check that reg is an integer mode register. */
2390 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2391 return false;
2392
2393 if (GET_CODE (x) == STRICT_LOW_PART
2394 || GET_CODE (x) == ZERO_EXTRACT)
2395 x = XEXP (x, 0);
2396
2397 return GET_CODE (x) == SUBREG
2398 && SUBREG_REG (x) == reg
2399 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2400 }
2401
2402 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2403 Note that the INSN should be deleted *after* removing dead edges, so
2404 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2405 but not for a (set (pc) (label_ref FOO)). */
2406
2407 static void
2408 update_cfg_for_uncondjump (rtx_insn *insn)
2409 {
2410 basic_block bb = BLOCK_FOR_INSN (insn);
2411 gcc_assert (BB_END (bb) == insn);
2412
2413 purge_dead_edges (bb);
2414
2415 delete_insn (insn);
2416 if (EDGE_COUNT (bb->succs) == 1)
2417 {
2418 rtx_insn *insn;
2419
2420 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2421
2422 /* Remove barriers from the footer if there are any. */
2423 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2424 if (BARRIER_P (insn))
2425 {
2426 if (PREV_INSN (insn))
2427 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2428 else
2429 BB_FOOTER (bb) = NEXT_INSN (insn);
2430 if (NEXT_INSN (insn))
2431 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2432 }
2433 else if (LABEL_P (insn))
2434 break;
2435 }
2436 }
2437
2438 /* Try to combine the insns I0, I1 and I2 into I3.
2439 Here I0, I1 and I2 appear earlier than I3.
2440 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2441 I3.
2442
2443 If we are combining more than two insns and the resulting insn is not
2444 recognized, try splitting it into two insns. If that happens, I2 and I3
2445 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2446 Otherwise, I0, I1 and I2 are pseudo-deleted.
2447
2448 Return 0 if the combination does not work. Then nothing is changed.
2449 If we did the combination, return the insn at which combine should
2450 resume scanning.
2451
2452 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2453 new direct jump instruction.
2454
2455 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2456 been I3 passed to an earlier try_combine within the same basic
2457 block. */
2458
2459 static rtx_insn *
2460 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2461 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2462 {
2463 /* New patterns for I3 and I2, respectively. */
2464 rtx newpat, newi2pat = 0;
2465 rtvec newpat_vec_with_clobbers = 0;
2466 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2467 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2468 dead. */
2469 int added_sets_0, added_sets_1, added_sets_2;
2470 /* Total number of SETs to put into I3. */
2471 int total_sets;
2472 /* Nonzero if I2's or I1's body now appears in I3. */
2473 int i2_is_used = 0, i1_is_used = 0;
2474 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2475 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2476 /* Contains I3 if the destination of I3 is used in its source, which means
2477 that the old life of I3 is being killed. If that usage is placed into
2478 I2 and not in I3, a REG_DEAD note must be made. */
2479 rtx i3dest_killed = 0;
2480 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2481 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2482 /* Copy of SET_SRC of I1 and I0, if needed. */
2483 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2484 /* Set if I2DEST was reused as a scratch register. */
2485 bool i2scratch = false;
2486 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2487 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2488 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2489 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2490 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2491 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2492 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2493 /* Notes that must be added to REG_NOTES in I3 and I2. */
2494 rtx new_i3_notes, new_i2_notes;
2495 /* Notes that we substituted I3 into I2 instead of the normal case. */
2496 int i3_subst_into_i2 = 0;
2497 /* Notes that I1, I2 or I3 is a MULT operation. */
2498 int have_mult = 0;
2499 int swap_i2i3 = 0;
2500 int changed_i3_dest = 0;
2501
2502 int maxreg;
2503 rtx_insn *temp_insn;
2504 rtx temp_expr;
2505 struct insn_link *link;
2506 rtx other_pat = 0;
2507 rtx new_other_notes;
2508 int i;
2509
2510 /* Only try four-insn combinations when there's high likelihood of
2511 success. Look for simple insns, such as loads of constants or
2512 binary operations involving a constant. */
2513 if (i0)
2514 {
2515 int i;
2516 int ngood = 0;
2517 int nshift = 0;
2518
2519 if (!flag_expensive_optimizations)
2520 return 0;
2521
2522 for (i = 0; i < 4; i++)
2523 {
2524 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2525 rtx set = single_set (insn);
2526 rtx src;
2527 if (!set)
2528 continue;
2529 src = SET_SRC (set);
2530 if (CONSTANT_P (src))
2531 {
2532 ngood += 2;
2533 break;
2534 }
2535 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2536 ngood++;
2537 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2538 || GET_CODE (src) == LSHIFTRT)
2539 nshift++;
2540 }
2541 if (ngood < 2 && nshift < 2)
2542 return 0;
2543 }
2544
2545 /* Exit early if one of the insns involved can't be used for
2546 combinations. */
2547 if (cant_combine_insn_p (i3)
2548 || cant_combine_insn_p (i2)
2549 || (i1 && cant_combine_insn_p (i1))
2550 || (i0 && cant_combine_insn_p (i0))
2551 || likely_spilled_retval_p (i3))
2552 return 0;
2553
2554 combine_attempts++;
2555 undobuf.other_insn = 0;
2556
2557 /* Reset the hard register usage information. */
2558 CLEAR_HARD_REG_SET (newpat_used_regs);
2559
2560 if (dump_file && (dump_flags & TDF_DETAILS))
2561 {
2562 if (i0)
2563 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2564 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2565 else if (i1)
2566 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2567 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2568 else
2569 fprintf (dump_file, "\nTrying %d -> %d:\n",
2570 INSN_UID (i2), INSN_UID (i3));
2571 }
2572
2573 /* If multiple insns feed into one of I2 or I3, they can be in any
2574 order. To simplify the code below, reorder them in sequence. */
2575 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2576 temp_insn = i2, i2 = i0, i0 = temp_insn;
2577 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2578 temp_insn = i1, i1 = i0, i0 = temp_insn;
2579 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2580 temp_insn = i1, i1 = i2, i2 = temp_insn;
2581
2582 added_links_insn = 0;
2583
2584 /* First check for one important special case that the code below will
2585 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2586 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2587 we may be able to replace that destination with the destination of I3.
2588 This occurs in the common code where we compute both a quotient and
2589 remainder into a structure, in which case we want to do the computation
2590 directly into the structure to avoid register-register copies.
2591
2592 Note that this case handles both multiple sets in I2 and also cases
2593 where I2 has a number of CLOBBERs inside the PARALLEL.
2594
2595 We make very conservative checks below and only try to handle the
2596 most common cases of this. For example, we only handle the case
2597 where I2 and I3 are adjacent to avoid making difficult register
2598 usage tests. */
2599
2600 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2601 && REG_P (SET_SRC (PATTERN (i3)))
2602 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2603 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2604 && GET_CODE (PATTERN (i2)) == PARALLEL
2605 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2606 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2607 below would need to check what is inside (and reg_overlap_mentioned_p
2608 doesn't support those codes anyway). Don't allow those destinations;
2609 the resulting insn isn't likely to be recognized anyway. */
2610 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2611 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2612 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2613 SET_DEST (PATTERN (i3)))
2614 && next_active_insn (i2) == i3)
2615 {
2616 rtx p2 = PATTERN (i2);
2617
2618 /* Make sure that the destination of I3,
2619 which we are going to substitute into one output of I2,
2620 is not used within another output of I2. We must avoid making this:
2621 (parallel [(set (mem (reg 69)) ...)
2622 (set (reg 69) ...)])
2623 which is not well-defined as to order of actions.
2624 (Besides, reload can't handle output reloads for this.)
2625
2626 The problem can also happen if the dest of I3 is a memory ref,
2627 if another dest in I2 is an indirect memory ref. */
2628 for (i = 0; i < XVECLEN (p2, 0); i++)
2629 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2630 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2631 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2632 SET_DEST (XVECEXP (p2, 0, i))))
2633 break;
2634
2635 if (i == XVECLEN (p2, 0))
2636 for (i = 0; i < XVECLEN (p2, 0); i++)
2637 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2638 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2639 {
2640 combine_merges++;
2641
2642 subst_insn = i3;
2643 subst_low_luid = DF_INSN_LUID (i2);
2644
2645 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2646 i2src = SET_SRC (XVECEXP (p2, 0, i));
2647 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2648 i2dest_killed = dead_or_set_p (i2, i2dest);
2649
2650 /* Replace the dest in I2 with our dest and make the resulting
2651 insn the new pattern for I3. Then skip to where we validate
2652 the pattern. Everything was set up above. */
2653 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2654 newpat = p2;
2655 i3_subst_into_i2 = 1;
2656 goto validate_replacement;
2657 }
2658 }
2659
2660 /* If I2 is setting a pseudo to a constant and I3 is setting some
2661 sub-part of it to another constant, merge them by making a new
2662 constant. */
2663 if (i1 == 0
2664 && (temp_expr = single_set (i2)) != 0
2665 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2666 && GET_CODE (PATTERN (i3)) == SET
2667 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2668 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2669 {
2670 rtx dest = SET_DEST (PATTERN (i3));
2671 int offset = -1;
2672 int width = 0;
2673
2674 if (GET_CODE (dest) == ZERO_EXTRACT)
2675 {
2676 if (CONST_INT_P (XEXP (dest, 1))
2677 && CONST_INT_P (XEXP (dest, 2)))
2678 {
2679 width = INTVAL (XEXP (dest, 1));
2680 offset = INTVAL (XEXP (dest, 2));
2681 dest = XEXP (dest, 0);
2682 if (BITS_BIG_ENDIAN)
2683 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2684 }
2685 }
2686 else
2687 {
2688 if (GET_CODE (dest) == STRICT_LOW_PART)
2689 dest = XEXP (dest, 0);
2690 width = GET_MODE_PRECISION (GET_MODE (dest));
2691 offset = 0;
2692 }
2693
2694 if (offset >= 0)
2695 {
2696 /* If this is the low part, we're done. */
2697 if (subreg_lowpart_p (dest))
2698 ;
2699 /* Handle the case where inner is twice the size of outer. */
2700 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2701 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2702 offset += GET_MODE_PRECISION (GET_MODE (dest));
2703 /* Otherwise give up for now. */
2704 else
2705 offset = -1;
2706 }
2707
2708 if (offset >= 0)
2709 {
2710 rtx inner = SET_SRC (PATTERN (i3));
2711 rtx outer = SET_SRC (temp_expr);
2712
2713 wide_int o
2714 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2715 std::make_pair (inner, GET_MODE (dest)),
2716 offset, width);
2717
2718 combine_merges++;
2719 subst_insn = i3;
2720 subst_low_luid = DF_INSN_LUID (i2);
2721 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2722 i2dest = SET_DEST (temp_expr);
2723 i2dest_killed = dead_or_set_p (i2, i2dest);
2724
2725 /* Replace the source in I2 with the new constant and make the
2726 resulting insn the new pattern for I3. Then skip to where we
2727 validate the pattern. Everything was set up above. */
2728 SUBST (SET_SRC (temp_expr),
2729 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2730
2731 newpat = PATTERN (i2);
2732
2733 /* The dest of I3 has been replaced with the dest of I2. */
2734 changed_i3_dest = 1;
2735 goto validate_replacement;
2736 }
2737 }
2738
2739 #ifndef HAVE_cc0
2740 /* If we have no I1 and I2 looks like:
2741 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2742 (set Y OP)])
2743 make up a dummy I1 that is
2744 (set Y OP)
2745 and change I2 to be
2746 (set (reg:CC X) (compare:CC Y (const_int 0)))
2747
2748 (We can ignore any trailing CLOBBERs.)
2749
2750 This undoes a previous combination and allows us to match a branch-and-
2751 decrement insn. */
2752
2753 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2754 && XVECLEN (PATTERN (i2), 0) >= 2
2755 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2756 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2757 == MODE_CC)
2758 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2759 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2760 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2761 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2762 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2763 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2764 {
2765 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2766 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2767 break;
2768
2769 if (i == 1)
2770 {
2771 /* We make I1 with the same INSN_UID as I2. This gives it
2772 the same DF_INSN_LUID for value tracking. Our fake I1 will
2773 never appear in the insn stream so giving it the same INSN_UID
2774 as I2 will not cause a problem. */
2775
2776 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2777 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2778 -1, NULL_RTX);
2779 INSN_UID (i1) = INSN_UID (i2);
2780
2781 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2782 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2783 SET_DEST (PATTERN (i1)));
2784 SUBST_LINK (LOG_LINKS (i2), alloc_insn_link (i1, LOG_LINKS (i2)));
2785 }
2786 }
2787 #endif
2788
2789 /* Verify that I2 and I1 are valid for combining. */
2790 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2791 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2792 &i1dest, &i1src))
2793 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2794 &i0dest, &i0src)))
2795 {
2796 undo_all ();
2797 return 0;
2798 }
2799
2800 /* Record whether I2DEST is used in I2SRC and similarly for the other
2801 cases. Knowing this will help in register status updating below. */
2802 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2803 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2804 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2805 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2806 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2807 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2808 i2dest_killed = dead_or_set_p (i2, i2dest);
2809 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2810 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2811
2812 /* For the earlier insns, determine which of the subsequent ones they
2813 feed. */
2814 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2815 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2816 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2817 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2818 && reg_overlap_mentioned_p (i0dest, i2src))));
2819
2820 /* Ensure that I3's pattern can be the destination of combines. */
2821 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2822 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2823 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2824 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2825 &i3dest_killed))
2826 {
2827 undo_all ();
2828 return 0;
2829 }
2830
2831 /* See if any of the insns is a MULT operation. Unless one is, we will
2832 reject a combination that is, since it must be slower. Be conservative
2833 here. */
2834 if (GET_CODE (i2src) == MULT
2835 || (i1 != 0 && GET_CODE (i1src) == MULT)
2836 || (i0 != 0 && GET_CODE (i0src) == MULT)
2837 || (GET_CODE (PATTERN (i3)) == SET
2838 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2839 have_mult = 1;
2840
2841 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2842 We used to do this EXCEPT in one case: I3 has a post-inc in an
2843 output operand. However, that exception can give rise to insns like
2844 mov r3,(r3)+
2845 which is a famous insn on the PDP-11 where the value of r3 used as the
2846 source was model-dependent. Avoid this sort of thing. */
2847
2848 #if 0
2849 if (!(GET_CODE (PATTERN (i3)) == SET
2850 && REG_P (SET_SRC (PATTERN (i3)))
2851 && MEM_P (SET_DEST (PATTERN (i3)))
2852 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2853 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2854 /* It's not the exception. */
2855 #endif
2856 #ifdef AUTO_INC_DEC
2857 {
2858 rtx link;
2859 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2860 if (REG_NOTE_KIND (link) == REG_INC
2861 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2862 || (i1 != 0
2863 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2864 {
2865 undo_all ();
2866 return 0;
2867 }
2868 }
2869 #endif
2870
2871 /* See if the SETs in I1 or I2 need to be kept around in the merged
2872 instruction: whenever the value set there is still needed past I3.
2873 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
2874
2875 For the SET in I1, we have two cases: if I1 and I2 independently feed
2876 into I3, the set in I1 needs to be kept around unless I1DEST dies
2877 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2878 in I1 needs to be kept around unless I1DEST dies or is set in either
2879 I2 or I3. The same considerations apply to I0. */
2880
2881 added_sets_2 = !dead_or_set_p (i3, i2dest);
2882
2883 if (i1)
2884 added_sets_1 = !(dead_or_set_p (i3, i1dest)
2885 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
2886 else
2887 added_sets_1 = 0;
2888
2889 if (i0)
2890 added_sets_0 = !(dead_or_set_p (i3, i0dest)
2891 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
2892 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
2893 && dead_or_set_p (i2, i0dest)));
2894 else
2895 added_sets_0 = 0;
2896
2897 /* We are about to copy insns for the case where they need to be kept
2898 around. Check that they can be copied in the merged instruction. */
2899
2900 if (targetm.cannot_copy_insn_p
2901 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
2902 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
2903 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
2904 {
2905 undo_all ();
2906 return 0;
2907 }
2908
2909 /* If the set in I2 needs to be kept around, we must make a copy of
2910 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2911 PATTERN (I2), we are only substituting for the original I1DEST, not into
2912 an already-substituted copy. This also prevents making self-referential
2913 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2914 I2DEST. */
2915
2916 if (added_sets_2)
2917 {
2918 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2919 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2920 else
2921 i2pat = copy_rtx (PATTERN (i2));
2922 }
2923
2924 if (added_sets_1)
2925 {
2926 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2927 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2928 else
2929 i1pat = copy_rtx (PATTERN (i1));
2930 }
2931
2932 if (added_sets_0)
2933 {
2934 if (GET_CODE (PATTERN (i0)) == PARALLEL)
2935 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
2936 else
2937 i0pat = copy_rtx (PATTERN (i0));
2938 }
2939
2940 combine_merges++;
2941
2942 /* Substitute in the latest insn for the regs set by the earlier ones. */
2943
2944 maxreg = max_reg_num ();
2945
2946 subst_insn = i3;
2947
2948 #ifndef HAVE_cc0
2949 /* Many machines that don't use CC0 have insns that can both perform an
2950 arithmetic operation and set the condition code. These operations will
2951 be represented as a PARALLEL with the first element of the vector
2952 being a COMPARE of an arithmetic operation with the constant zero.
2953 The second element of the vector will set some pseudo to the result
2954 of the same arithmetic operation. If we simplify the COMPARE, we won't
2955 match such a pattern and so will generate an extra insn. Here we test
2956 for this case, where both the comparison and the operation result are
2957 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2958 I2SRC. Later we will make the PARALLEL that contains I2. */
2959
2960 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2961 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2962 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
2963 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2964 {
2965 rtx newpat_dest;
2966 rtx *cc_use_loc = NULL;
2967 rtx_insn *cc_use_insn = NULL;
2968 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
2969 enum machine_mode compare_mode, orig_compare_mode;
2970 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
2971
2972 newpat = PATTERN (i3);
2973 newpat_dest = SET_DEST (newpat);
2974 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
2975
2976 if (undobuf.other_insn == 0
2977 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
2978 &cc_use_insn)))
2979 {
2980 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
2981 compare_code = simplify_compare_const (compare_code,
2982 GET_MODE (i2dest), op0, &op1);
2983 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
2984 }
2985
2986 /* Do the rest only if op1 is const0_rtx, which may be the
2987 result of simplification. */
2988 if (op1 == const0_rtx)
2989 {
2990 /* If a single use of the CC is found, prepare to modify it
2991 when SELECT_CC_MODE returns a new CC-class mode, or when
2992 the above simplify_compare_const() returned a new comparison
2993 operator. undobuf.other_insn is assigned the CC use insn
2994 when modifying it. */
2995 if (cc_use_loc)
2996 {
2997 #ifdef SELECT_CC_MODE
2998 enum machine_mode new_mode
2999 = SELECT_CC_MODE (compare_code, op0, op1);
3000 if (new_mode != orig_compare_mode
3001 && can_change_dest_mode (SET_DEST (newpat),
3002 added_sets_2, new_mode))
3003 {
3004 unsigned int regno = REGNO (newpat_dest);
3005 compare_mode = new_mode;
3006 if (regno < FIRST_PSEUDO_REGISTER)
3007 newpat_dest = gen_rtx_REG (compare_mode, regno);
3008 else
3009 {
3010 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3011 newpat_dest = regno_reg_rtx[regno];
3012 }
3013 }
3014 #endif
3015 /* Cases for modifying the CC-using comparison. */
3016 if (compare_code != orig_compare_code
3017 /* ??? Do we need to verify the zero rtx? */
3018 && XEXP (*cc_use_loc, 1) == const0_rtx)
3019 {
3020 /* Replace cc_use_loc with entire new RTX. */
3021 SUBST (*cc_use_loc,
3022 gen_rtx_fmt_ee (compare_code, compare_mode,
3023 newpat_dest, const0_rtx));
3024 undobuf.other_insn = cc_use_insn;
3025 }
3026 else if (compare_mode != orig_compare_mode)
3027 {
3028 /* Just replace the CC reg with a new mode. */
3029 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3030 undobuf.other_insn = cc_use_insn;
3031 }
3032 }
3033
3034 /* Now we modify the current newpat:
3035 First, SET_DEST(newpat) is updated if the CC mode has been
3036 altered. For targets without SELECT_CC_MODE, this should be
3037 optimized away. */
3038 if (compare_mode != orig_compare_mode)
3039 SUBST (SET_DEST (newpat), newpat_dest);
3040 /* This is always done to propagate i2src into newpat. */
3041 SUBST (SET_SRC (newpat),
3042 gen_rtx_COMPARE (compare_mode, op0, op1));
3043 /* Create new version of i2pat if needed; the below PARALLEL
3044 creation needs this to work correctly. */
3045 if (! rtx_equal_p (i2src, op0))
3046 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3047 i2_is_used = 1;
3048 }
3049 }
3050 #endif
3051
3052 if (i2_is_used == 0)
3053 {
3054 /* It is possible that the source of I2 or I1 may be performing
3055 an unneeded operation, such as a ZERO_EXTEND of something
3056 that is known to have the high part zero. Handle that case
3057 by letting subst look at the inner insns.
3058
3059 Another way to do this would be to have a function that tries
3060 to simplify a single insn instead of merging two or more
3061 insns. We don't do this because of the potential of infinite
3062 loops and because of the potential extra memory required.
3063 However, doing it the way we are is a bit of a kludge and
3064 doesn't catch all cases.
3065
3066 But only do this if -fexpensive-optimizations since it slows
3067 things down and doesn't usually win.
3068
3069 This is not done in the COMPARE case above because the
3070 unmodified I2PAT is used in the PARALLEL and so a pattern
3071 with a modified I2SRC would not match. */
3072
3073 if (flag_expensive_optimizations)
3074 {
3075 /* Pass pc_rtx so no substitutions are done, just
3076 simplifications. */
3077 if (i1)
3078 {
3079 subst_low_luid = DF_INSN_LUID (i1);
3080 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3081 }
3082
3083 subst_low_luid = DF_INSN_LUID (i2);
3084 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3085 }
3086
3087 n_occurrences = 0; /* `subst' counts here */
3088 subst_low_luid = DF_INSN_LUID (i2);
3089
3090 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3091 copy of I2SRC each time we substitute it, in order to avoid creating
3092 self-referential RTL when we will be substituting I1SRC for I1DEST
3093 later. Likewise if I0 feeds into I2, either directly or indirectly
3094 through I1, and I0DEST is in I0SRC. */
3095 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3096 (i1_feeds_i2_n && i1dest_in_i1src)
3097 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3098 && i0dest_in_i0src));
3099 substed_i2 = 1;
3100
3101 /* Record whether I2's body now appears within I3's body. */
3102 i2_is_used = n_occurrences;
3103 }
3104
3105 /* If we already got a failure, don't try to do more. Otherwise, try to
3106 substitute I1 if we have it. */
3107
3108 if (i1 && GET_CODE (newpat) != CLOBBER)
3109 {
3110 /* Check that an autoincrement side-effect on I1 has not been lost.
3111 This happens if I1DEST is mentioned in I2 and dies there, and
3112 has disappeared from the new pattern. */
3113 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3114 && i1_feeds_i2_n
3115 && dead_or_set_p (i2, i1dest)
3116 && !reg_overlap_mentioned_p (i1dest, newpat))
3117 /* Before we can do this substitution, we must redo the test done
3118 above (see detailed comments there) that ensures I1DEST isn't
3119 mentioned in any SETs in NEWPAT that are field assignments. */
3120 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3121 0, 0, 0))
3122 {
3123 undo_all ();
3124 return 0;
3125 }
3126
3127 n_occurrences = 0;
3128 subst_low_luid = DF_INSN_LUID (i1);
3129
3130 /* If the following substitution will modify I1SRC, make a copy of it
3131 for the case where it is substituted for I1DEST in I2PAT later. */
3132 if (added_sets_2 && i1_feeds_i2_n)
3133 i1src_copy = copy_rtx (i1src);
3134
3135 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3136 copy of I1SRC each time we substitute it, in order to avoid creating
3137 self-referential RTL when we will be substituting I0SRC for I0DEST
3138 later. */
3139 newpat = subst (newpat, i1dest, i1src, 0, 0,
3140 i0_feeds_i1_n && i0dest_in_i0src);
3141 substed_i1 = 1;
3142
3143 /* Record whether I1's body now appears within I3's body. */
3144 i1_is_used = n_occurrences;
3145 }
3146
3147 /* Likewise for I0 if we have it. */
3148
3149 if (i0 && GET_CODE (newpat) != CLOBBER)
3150 {
3151 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3152 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3153 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3154 && !reg_overlap_mentioned_p (i0dest, newpat))
3155 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3156 0, 0, 0))
3157 {
3158 undo_all ();
3159 return 0;
3160 }
3161
3162 /* If the following substitution will modify I0SRC, make a copy of it
3163 for the case where it is substituted for I0DEST in I1PAT later. */
3164 if (added_sets_1 && i0_feeds_i1_n)
3165 i0src_copy = copy_rtx (i0src);
3166 /* And a copy for I0DEST in I2PAT substitution. */
3167 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3168 || (i0_feeds_i2_n)))
3169 i0src_copy2 = copy_rtx (i0src);
3170
3171 n_occurrences = 0;
3172 subst_low_luid = DF_INSN_LUID (i0);
3173 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3174 substed_i0 = 1;
3175 }
3176
3177 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3178 to count all the ways that I2SRC and I1SRC can be used. */
3179 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3180 && i2_is_used + added_sets_2 > 1)
3181 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3182 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3183 > 1))
3184 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3185 && (n_occurrences + added_sets_0
3186 + (added_sets_1 && i0_feeds_i1_n)
3187 + (added_sets_2 && i0_feeds_i2_n)
3188 > 1))
3189 /* Fail if we tried to make a new register. */
3190 || max_reg_num () != maxreg
3191 /* Fail if we couldn't do something and have a CLOBBER. */
3192 || GET_CODE (newpat) == CLOBBER
3193 /* Fail if this new pattern is a MULT and we didn't have one before
3194 at the outer level. */
3195 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3196 && ! have_mult))
3197 {
3198 undo_all ();
3199 return 0;
3200 }
3201
3202 /* If the actions of the earlier insns must be kept
3203 in addition to substituting them into the latest one,
3204 we must make a new PARALLEL for the latest insn
3205 to hold additional the SETs. */
3206
3207 if (added_sets_0 || added_sets_1 || added_sets_2)
3208 {
3209 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3210 combine_extras++;
3211
3212 if (GET_CODE (newpat) == PARALLEL)
3213 {
3214 rtvec old = XVEC (newpat, 0);
3215 total_sets = XVECLEN (newpat, 0) + extra_sets;
3216 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3217 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3218 sizeof (old->elem[0]) * old->num_elem);
3219 }
3220 else
3221 {
3222 rtx old = newpat;
3223 total_sets = 1 + extra_sets;
3224 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3225 XVECEXP (newpat, 0, 0) = old;
3226 }
3227
3228 if (added_sets_0)
3229 XVECEXP (newpat, 0, --total_sets) = i0pat;
3230
3231 if (added_sets_1)
3232 {
3233 rtx t = i1pat;
3234 if (i0_feeds_i1_n)
3235 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3236
3237 XVECEXP (newpat, 0, --total_sets) = t;
3238 }
3239 if (added_sets_2)
3240 {
3241 rtx t = i2pat;
3242 if (i1_feeds_i2_n)
3243 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3244 i0_feeds_i1_n && i0dest_in_i0src);
3245 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3246 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3247
3248 XVECEXP (newpat, 0, --total_sets) = t;
3249 }
3250 }
3251
3252 validate_replacement:
3253
3254 /* Note which hard regs this insn has as inputs. */
3255 mark_used_regs_combine (newpat);
3256
3257 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3258 consider splitting this pattern, we might need these clobbers. */
3259 if (i1 && GET_CODE (newpat) == PARALLEL
3260 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3261 {
3262 int len = XVECLEN (newpat, 0);
3263
3264 newpat_vec_with_clobbers = rtvec_alloc (len);
3265 for (i = 0; i < len; i++)
3266 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3267 }
3268
3269 /* Is the result of combination a valid instruction? */
3270 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3271
3272 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
3273 the second SET's destination is a register that is unused and isn't
3274 marked as an instruction that might trap in an EH region. In that case,
3275 we just need the first SET. This can occur when simplifying a divmod
3276 insn. We *must* test for this case here because the code below that
3277 splits two independent SETs doesn't handle this case correctly when it
3278 updates the register status.
3279
3280 It's pointless doing this if we originally had two sets, one from
3281 i3, and one from i2. Combining then splitting the parallel results
3282 in the original i2 again plus an invalid insn (which we delete).
3283 The net effect is only to move instructions around, which makes
3284 debug info less accurate.
3285
3286 Also check the case where the first SET's destination is unused.
3287 That would not cause incorrect code, but does cause an unneeded
3288 insn to remain. */
3289
3290 if (insn_code_number < 0
3291 && !(added_sets_2 && i1 == 0)
3292 && GET_CODE (newpat) == PARALLEL
3293 && XVECLEN (newpat, 0) == 2
3294 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3295 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3296 && asm_noperands (newpat) < 0)
3297 {
3298 rtx set0 = XVECEXP (newpat, 0, 0);
3299 rtx set1 = XVECEXP (newpat, 0, 1);
3300
3301 if (((REG_P (SET_DEST (set1))
3302 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3303 || (GET_CODE (SET_DEST (set1)) == SUBREG
3304 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3305 && insn_nothrow_p (i3)
3306 && !side_effects_p (SET_SRC (set1)))
3307 {
3308 newpat = set0;
3309 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3310 }
3311
3312 else if (((REG_P (SET_DEST (set0))
3313 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3314 || (GET_CODE (SET_DEST (set0)) == SUBREG
3315 && find_reg_note (i3, REG_UNUSED,
3316 SUBREG_REG (SET_DEST (set0)))))
3317 && insn_nothrow_p (i3)
3318 && !side_effects_p (SET_SRC (set0)))
3319 {
3320 newpat = set1;
3321 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3322
3323 if (insn_code_number >= 0)
3324 changed_i3_dest = 1;
3325 }
3326 }
3327
3328 /* If we were combining three insns and the result is a simple SET
3329 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3330 insns. There are two ways to do this. It can be split using a
3331 machine-specific method (like when you have an addition of a large
3332 constant) or by combine in the function find_split_point. */
3333
3334 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3335 && asm_noperands (newpat) < 0)
3336 {
3337 rtx parallel, *split;
3338 rtx_insn *m_split_insn;
3339
3340 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3341 use I2DEST as a scratch register will help. In the latter case,
3342 convert I2DEST to the mode of the source of NEWPAT if we can. */
3343
3344 m_split_insn = combine_split_insns (newpat, i3);
3345
3346 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3347 inputs of NEWPAT. */
3348
3349 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3350 possible to try that as a scratch reg. This would require adding
3351 more code to make it work though. */
3352
3353 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3354 {
3355 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3356
3357 /* First try to split using the original register as a
3358 scratch register. */
3359 parallel = gen_rtx_PARALLEL (VOIDmode,
3360 gen_rtvec (2, newpat,
3361 gen_rtx_CLOBBER (VOIDmode,
3362 i2dest)));
3363 m_split_insn = combine_split_insns (parallel, i3);
3364
3365 /* If that didn't work, try changing the mode of I2DEST if
3366 we can. */
3367 if (m_split_insn == 0
3368 && new_mode != GET_MODE (i2dest)
3369 && new_mode != VOIDmode
3370 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3371 {
3372 enum machine_mode old_mode = GET_MODE (i2dest);
3373 rtx ni2dest;
3374
3375 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3376 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3377 else
3378 {
3379 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3380 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3381 }
3382
3383 parallel = (gen_rtx_PARALLEL
3384 (VOIDmode,
3385 gen_rtvec (2, newpat,
3386 gen_rtx_CLOBBER (VOIDmode,
3387 ni2dest))));
3388 m_split_insn = combine_split_insns (parallel, i3);
3389
3390 if (m_split_insn == 0
3391 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3392 {
3393 struct undo *buf;
3394
3395 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3396 buf = undobuf.undos;
3397 undobuf.undos = buf->next;
3398 buf->next = undobuf.frees;
3399 undobuf.frees = buf;
3400 }
3401 }
3402
3403 i2scratch = m_split_insn != 0;
3404 }
3405
3406 /* If recog_for_combine has discarded clobbers, try to use them
3407 again for the split. */
3408 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3409 {
3410 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3411 m_split_insn = combine_split_insns (parallel, i3);
3412 }
3413
3414 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3415 {
3416 rtx m_split_pat = PATTERN (m_split_insn);
3417 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3418 if (insn_code_number >= 0)
3419 newpat = m_split_pat;
3420 }
3421 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3422 && (next_nonnote_nondebug_insn (i2) == i3
3423 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3424 {
3425 rtx i2set, i3set;
3426 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3427 newi2pat = PATTERN (m_split_insn);
3428
3429 i3set = single_set (NEXT_INSN (m_split_insn));
3430 i2set = single_set (m_split_insn);
3431
3432 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3433
3434 /* If I2 or I3 has multiple SETs, we won't know how to track
3435 register status, so don't use these insns. If I2's destination
3436 is used between I2 and I3, we also can't use these insns. */
3437
3438 if (i2_code_number >= 0 && i2set && i3set
3439 && (next_nonnote_nondebug_insn (i2) == i3
3440 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3441 insn_code_number = recog_for_combine (&newi3pat, i3,
3442 &new_i3_notes);
3443 if (insn_code_number >= 0)
3444 newpat = newi3pat;
3445
3446 /* It is possible that both insns now set the destination of I3.
3447 If so, we must show an extra use of it. */
3448
3449 if (insn_code_number >= 0)
3450 {
3451 rtx new_i3_dest = SET_DEST (i3set);
3452 rtx new_i2_dest = SET_DEST (i2set);
3453
3454 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3455 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3456 || GET_CODE (new_i3_dest) == SUBREG)
3457 new_i3_dest = XEXP (new_i3_dest, 0);
3458
3459 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3460 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3461 || GET_CODE (new_i2_dest) == SUBREG)
3462 new_i2_dest = XEXP (new_i2_dest, 0);
3463
3464 if (REG_P (new_i3_dest)
3465 && REG_P (new_i2_dest)
3466 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3467 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3468 }
3469 }
3470
3471 /* If we can split it and use I2DEST, go ahead and see if that
3472 helps things be recognized. Verify that none of the registers
3473 are set between I2 and I3. */
3474 if (insn_code_number < 0
3475 && (split = find_split_point (&newpat, i3, false)) != 0
3476 #ifdef HAVE_cc0
3477 && REG_P (i2dest)
3478 #endif
3479 /* We need I2DEST in the proper mode. If it is a hard register
3480 or the only use of a pseudo, we can change its mode.
3481 Make sure we don't change a hard register to have a mode that
3482 isn't valid for it, or change the number of registers. */
3483 && (GET_MODE (*split) == GET_MODE (i2dest)
3484 || GET_MODE (*split) == VOIDmode
3485 || can_change_dest_mode (i2dest, added_sets_2,
3486 GET_MODE (*split)))
3487 && (next_nonnote_nondebug_insn (i2) == i3
3488 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3489 /* We can't overwrite I2DEST if its value is still used by
3490 NEWPAT. */
3491 && ! reg_referenced_p (i2dest, newpat))
3492 {
3493 rtx newdest = i2dest;
3494 enum rtx_code split_code = GET_CODE (*split);
3495 enum machine_mode split_mode = GET_MODE (*split);
3496 bool subst_done = false;
3497 newi2pat = NULL_RTX;
3498
3499 i2scratch = true;
3500
3501 /* *SPLIT may be part of I2SRC, so make sure we have the
3502 original expression around for later debug processing.
3503 We should not need I2SRC any more in other cases. */
3504 if (MAY_HAVE_DEBUG_INSNS)
3505 i2src = copy_rtx (i2src);
3506 else
3507 i2src = NULL;
3508
3509 /* Get NEWDEST as a register in the proper mode. We have already
3510 validated that we can do this. */
3511 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3512 {
3513 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3514 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3515 else
3516 {
3517 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3518 newdest = regno_reg_rtx[REGNO (i2dest)];
3519 }
3520 }
3521
3522 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3523 an ASHIFT. This can occur if it was inside a PLUS and hence
3524 appeared to be a memory address. This is a kludge. */
3525 if (split_code == MULT
3526 && CONST_INT_P (XEXP (*split, 1))
3527 && INTVAL (XEXP (*split, 1)) > 0
3528 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3529 {
3530 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3531 XEXP (*split, 0), GEN_INT (i)));
3532 /* Update split_code because we may not have a multiply
3533 anymore. */
3534 split_code = GET_CODE (*split);
3535 }
3536
3537 #ifdef INSN_SCHEDULING
3538 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3539 be written as a ZERO_EXTEND. */
3540 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3541 {
3542 #ifdef LOAD_EXTEND_OP
3543 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3544 what it really is. */
3545 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3546 == SIGN_EXTEND)
3547 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3548 SUBREG_REG (*split)));
3549 else
3550 #endif
3551 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3552 SUBREG_REG (*split)));
3553 }
3554 #endif
3555
3556 /* Attempt to split binary operators using arithmetic identities. */
3557 if (BINARY_P (SET_SRC (newpat))
3558 && split_mode == GET_MODE (SET_SRC (newpat))
3559 && ! side_effects_p (SET_SRC (newpat)))
3560 {
3561 rtx setsrc = SET_SRC (newpat);
3562 enum machine_mode mode = GET_MODE (setsrc);
3563 enum rtx_code code = GET_CODE (setsrc);
3564 rtx src_op0 = XEXP (setsrc, 0);
3565 rtx src_op1 = XEXP (setsrc, 1);
3566
3567 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3568 if (rtx_equal_p (src_op0, src_op1))
3569 {
3570 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3571 SUBST (XEXP (setsrc, 0), newdest);
3572 SUBST (XEXP (setsrc, 1), newdest);
3573 subst_done = true;
3574 }
3575 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3576 else if ((code == PLUS || code == MULT)
3577 && GET_CODE (src_op0) == code
3578 && GET_CODE (XEXP (src_op0, 0)) == code
3579 && (INTEGRAL_MODE_P (mode)
3580 || (FLOAT_MODE_P (mode)
3581 && flag_unsafe_math_optimizations)))
3582 {
3583 rtx p = XEXP (XEXP (src_op0, 0), 0);
3584 rtx q = XEXP (XEXP (src_op0, 0), 1);
3585 rtx r = XEXP (src_op0, 1);
3586 rtx s = src_op1;
3587
3588 /* Split both "((X op Y) op X) op Y" and
3589 "((X op Y) op Y) op X" as "T op T" where T is
3590 "X op Y". */
3591 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3592 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3593 {
3594 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3595 XEXP (src_op0, 0));
3596 SUBST (XEXP (setsrc, 0), newdest);
3597 SUBST (XEXP (setsrc, 1), newdest);
3598 subst_done = true;
3599 }
3600 /* Split "((X op X) op Y) op Y)" as "T op T" where
3601 T is "X op Y". */
3602 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3603 {
3604 rtx tmp = simplify_gen_binary (code, mode, p, r);
3605 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3606 SUBST (XEXP (setsrc, 0), newdest);
3607 SUBST (XEXP (setsrc, 1), newdest);
3608 subst_done = true;
3609 }
3610 }
3611 }
3612
3613 if (!subst_done)
3614 {
3615 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3616 SUBST (*split, newdest);
3617 }
3618
3619 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3620
3621 /* recog_for_combine might have added CLOBBERs to newi2pat.
3622 Make sure NEWPAT does not depend on the clobbered regs. */
3623 if (GET_CODE (newi2pat) == PARALLEL)
3624 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3625 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3626 {
3627 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3628 if (reg_overlap_mentioned_p (reg, newpat))
3629 {
3630 undo_all ();
3631 return 0;
3632 }
3633 }
3634
3635 /* If the split point was a MULT and we didn't have one before,
3636 don't use one now. */
3637 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3638 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3639 }
3640 }
3641
3642 /* Check for a case where we loaded from memory in a narrow mode and
3643 then sign extended it, but we need both registers. In that case,
3644 we have a PARALLEL with both loads from the same memory location.
3645 We can split this into a load from memory followed by a register-register
3646 copy. This saves at least one insn, more if register allocation can
3647 eliminate the copy.
3648
3649 We cannot do this if the destination of the first assignment is a
3650 condition code register or cc0. We eliminate this case by making sure
3651 the SET_DEST and SET_SRC have the same mode.
3652
3653 We cannot do this if the destination of the second assignment is
3654 a register that we have already assumed is zero-extended. Similarly
3655 for a SUBREG of such a register. */
3656
3657 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3658 && GET_CODE (newpat) == PARALLEL
3659 && XVECLEN (newpat, 0) == 2
3660 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3661 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3662 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3663 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3664 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3665 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3666 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3667 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3668 DF_INSN_LUID (i2))
3669 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3670 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3671 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3672 (REG_P (temp_expr)
3673 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3674 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3675 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3676 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3677 != GET_MODE_MASK (word_mode))))
3678 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3679 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3680 (REG_P (temp_expr)
3681 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3682 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3683 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3684 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3685 != GET_MODE_MASK (word_mode)))))
3686 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3687 SET_SRC (XVECEXP (newpat, 0, 1)))
3688 && ! find_reg_note (i3, REG_UNUSED,
3689 SET_DEST (XVECEXP (newpat, 0, 0))))
3690 {
3691 rtx ni2dest;
3692
3693 newi2pat = XVECEXP (newpat, 0, 0);
3694 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3695 newpat = XVECEXP (newpat, 0, 1);
3696 SUBST (SET_SRC (newpat),
3697 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3698 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3699
3700 if (i2_code_number >= 0)
3701 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3702
3703 if (insn_code_number >= 0)
3704 swap_i2i3 = 1;
3705 }
3706
3707 /* Similarly, check for a case where we have a PARALLEL of two independent
3708 SETs but we started with three insns. In this case, we can do the sets
3709 as two separate insns. This case occurs when some SET allows two
3710 other insns to combine, but the destination of that SET is still live. */
3711
3712 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3713 && GET_CODE (newpat) == PARALLEL
3714 && XVECLEN (newpat, 0) == 2
3715 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3716 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3717 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3718 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3719 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3720 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3721 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3722 XVECEXP (newpat, 0, 0))
3723 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3724 XVECEXP (newpat, 0, 1))
3725 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3726 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3727 {
3728 rtx set0 = XVECEXP (newpat, 0, 0);
3729 rtx set1 = XVECEXP (newpat, 0, 1);
3730
3731 /* Normally, it doesn't matter which of the two is done first,
3732 but the one that references cc0 can't be the second, and
3733 one which uses any regs/memory set in between i2 and i3 can't
3734 be first. The PARALLEL might also have been pre-existing in i3,
3735 so we need to make sure that we won't wrongly hoist a SET to i2
3736 that would conflict with a death note present in there. */
3737 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3738 && !(REG_P (SET_DEST (set1))
3739 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3740 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3741 && find_reg_note (i2, REG_DEAD,
3742 SUBREG_REG (SET_DEST (set1))))
3743 #ifdef HAVE_cc0
3744 && !reg_referenced_p (cc0_rtx, set0)
3745 #endif
3746 /* If I3 is a jump, ensure that set0 is a jump so that
3747 we do not create invalid RTL. */
3748 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3749 )
3750 {
3751 newi2pat = set1;
3752 newpat = set0;
3753 }
3754 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3755 && !(REG_P (SET_DEST (set0))
3756 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3757 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3758 && find_reg_note (i2, REG_DEAD,
3759 SUBREG_REG (SET_DEST (set0))))
3760 #ifdef HAVE_cc0
3761 && !reg_referenced_p (cc0_rtx, set1)
3762 #endif
3763 /* If I3 is a jump, ensure that set1 is a jump so that
3764 we do not create invalid RTL. */
3765 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3766 )
3767 {
3768 newi2pat = set0;
3769 newpat = set1;
3770 }
3771 else
3772 {
3773 undo_all ();
3774 return 0;
3775 }
3776
3777 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3778
3779 if (i2_code_number >= 0)
3780 {
3781 /* recog_for_combine might have added CLOBBERs to newi2pat.
3782 Make sure NEWPAT does not depend on the clobbered regs. */
3783 if (GET_CODE (newi2pat) == PARALLEL)
3784 {
3785 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3786 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3787 {
3788 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3789 if (reg_overlap_mentioned_p (reg, newpat))
3790 {
3791 undo_all ();
3792 return 0;
3793 }
3794 }
3795 }
3796
3797 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3798 }
3799 }
3800
3801 /* If it still isn't recognized, fail and change things back the way they
3802 were. */
3803 if ((insn_code_number < 0
3804 /* Is the result a reasonable ASM_OPERANDS? */
3805 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3806 {
3807 undo_all ();
3808 return 0;
3809 }
3810
3811 /* If we had to change another insn, make sure it is valid also. */
3812 if (undobuf.other_insn)
3813 {
3814 CLEAR_HARD_REG_SET (newpat_used_regs);
3815
3816 other_pat = PATTERN (undobuf.other_insn);
3817 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3818 &new_other_notes);
3819
3820 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3821 {
3822 undo_all ();
3823 return 0;
3824 }
3825 }
3826
3827 #ifdef HAVE_cc0
3828 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3829 they are adjacent to each other or not. */
3830 {
3831 rtx_insn *p = prev_nonnote_insn (i3);
3832 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3833 && sets_cc0_p (newi2pat))
3834 {
3835 undo_all ();
3836 return 0;
3837 }
3838 }
3839 #endif
3840
3841 /* Only allow this combination if insn_rtx_costs reports that the
3842 replacement instructions are cheaper than the originals. */
3843 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
3844 {
3845 undo_all ();
3846 return 0;
3847 }
3848
3849 if (MAY_HAVE_DEBUG_INSNS)
3850 {
3851 struct undo *undo;
3852
3853 for (undo = undobuf.undos; undo; undo = undo->next)
3854 if (undo->kind == UNDO_MODE)
3855 {
3856 rtx reg = *undo->where.r;
3857 enum machine_mode new_mode = GET_MODE (reg);
3858 enum machine_mode old_mode = undo->old_contents.m;
3859
3860 /* Temporarily revert mode back. */
3861 adjust_reg_mode (reg, old_mode);
3862
3863 if (reg == i2dest && i2scratch)
3864 {
3865 /* If we used i2dest as a scratch register with a
3866 different mode, substitute it for the original
3867 i2src while its original mode is temporarily
3868 restored, and then clear i2scratch so that we don't
3869 do it again later. */
3870 propagate_for_debug (i2, last_combined_insn, reg, i2src,
3871 this_basic_block);
3872 i2scratch = false;
3873 /* Put back the new mode. */
3874 adjust_reg_mode (reg, new_mode);
3875 }
3876 else
3877 {
3878 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
3879 rtx_insn *first, *last;
3880
3881 if (reg == i2dest)
3882 {
3883 first = i2;
3884 last = last_combined_insn;
3885 }
3886 else
3887 {
3888 first = i3;
3889 last = undobuf.other_insn;
3890 gcc_assert (last);
3891 if (DF_INSN_LUID (last)
3892 < DF_INSN_LUID (last_combined_insn))
3893 last = last_combined_insn;
3894 }
3895
3896 /* We're dealing with a reg that changed mode but not
3897 meaning, so we want to turn it into a subreg for
3898 the new mode. However, because of REG sharing and
3899 because its mode had already changed, we have to do
3900 it in two steps. First, replace any debug uses of
3901 reg, with its original mode temporarily restored,
3902 with this copy we have created; then, replace the
3903 copy with the SUBREG of the original shared reg,
3904 once again changed to the new mode. */
3905 propagate_for_debug (first, last, reg, tempreg,
3906 this_basic_block);
3907 adjust_reg_mode (reg, new_mode);
3908 propagate_for_debug (first, last, tempreg,
3909 lowpart_subreg (old_mode, reg, new_mode),
3910 this_basic_block);
3911 }
3912 }
3913 }
3914
3915 /* If we will be able to accept this, we have made a
3916 change to the destination of I3. This requires us to
3917 do a few adjustments. */
3918
3919 if (changed_i3_dest)
3920 {
3921 PATTERN (i3) = newpat;
3922 adjust_for_new_dest (i3);
3923 }
3924
3925 /* We now know that we can do this combination. Merge the insns and
3926 update the status of registers and LOG_LINKS. */
3927
3928 if (undobuf.other_insn)
3929 {
3930 rtx note, next;
3931
3932 PATTERN (undobuf.other_insn) = other_pat;
3933
3934 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
3935 ensure that they are still valid. Then add any non-duplicate
3936 notes added by recog_for_combine. */
3937 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3938 {
3939 next = XEXP (note, 1);
3940
3941 if ((REG_NOTE_KIND (note) == REG_DEAD
3942 && !reg_referenced_p (XEXP (note, 0),
3943 PATTERN (undobuf.other_insn)))
3944 ||(REG_NOTE_KIND (note) == REG_UNUSED
3945 && !reg_set_p (XEXP (note, 0),
3946 PATTERN (undobuf.other_insn))))
3947 remove_note (undobuf.other_insn, note);
3948 }
3949
3950 distribute_notes (new_other_notes, undobuf.other_insn,
3951 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
3952 NULL_RTX);
3953 }
3954
3955 if (swap_i2i3)
3956 {
3957 rtx_insn *insn;
3958 struct insn_link *link;
3959 rtx ni2dest;
3960
3961 /* I3 now uses what used to be its destination and which is now
3962 I2's destination. This requires us to do a few adjustments. */
3963 PATTERN (i3) = newpat;
3964 adjust_for_new_dest (i3);
3965
3966 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3967 so we still will.
3968
3969 However, some later insn might be using I2's dest and have
3970 a LOG_LINK pointing at I3. We must remove this link.
3971 The simplest way to remove the link is to point it at I1,
3972 which we know will be a NOTE. */
3973
3974 /* newi2pat is usually a SET here; however, recog_for_combine might
3975 have added some clobbers. */
3976 if (GET_CODE (newi2pat) == PARALLEL)
3977 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3978 else
3979 ni2dest = SET_DEST (newi2pat);
3980
3981 for (insn = NEXT_INSN (i3);
3982 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
3983 || insn != BB_HEAD (this_basic_block->next_bb));
3984 insn = NEXT_INSN (insn))
3985 {
3986 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3987 {
3988 FOR_EACH_LOG_LINK (link, insn)
3989 if (link->insn == i3)
3990 link->insn = i1;
3991
3992 break;
3993 }
3994 }
3995 }
3996
3997 {
3998 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
3999 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4000 rtx midnotes = 0;
4001 int from_luid;
4002 /* Compute which registers we expect to eliminate. newi2pat may be setting
4003 either i3dest or i2dest, so we must check it. Also, i1dest may be the
4004 same as i3dest, in which case newi2pat may be setting i1dest. */
4005 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4006 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4007 || !i2dest_killed
4008 ? 0 : i2dest);
4009 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4010 || (newi2pat && reg_set_p (i1dest, newi2pat))
4011 || !i1dest_killed
4012 ? 0 : i1dest);
4013 rtx elim_i0 = (i0 == 0 || i0dest_in_i0src
4014 || (newi2pat && reg_set_p (i0dest, newi2pat))
4015 || !i0dest_killed
4016 ? 0 : i0dest);
4017
4018 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4019 clear them. */
4020 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4021 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4022 if (i1)
4023 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4024 if (i0)
4025 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4026
4027 /* Ensure that we do not have something that should not be shared but
4028 occurs multiple times in the new insns. Check this by first
4029 resetting all the `used' flags and then copying anything is shared. */
4030
4031 reset_used_flags (i3notes);
4032 reset_used_flags (i2notes);
4033 reset_used_flags (i1notes);
4034 reset_used_flags (i0notes);
4035 reset_used_flags (newpat);
4036 reset_used_flags (newi2pat);
4037 if (undobuf.other_insn)
4038 reset_used_flags (PATTERN (undobuf.other_insn));
4039
4040 i3notes = copy_rtx_if_shared (i3notes);
4041 i2notes = copy_rtx_if_shared (i2notes);
4042 i1notes = copy_rtx_if_shared (i1notes);
4043 i0notes = copy_rtx_if_shared (i0notes);
4044 newpat = copy_rtx_if_shared (newpat);
4045 newi2pat = copy_rtx_if_shared (newi2pat);
4046 if (undobuf.other_insn)
4047 reset_used_flags (PATTERN (undobuf.other_insn));
4048
4049 INSN_CODE (i3) = insn_code_number;
4050 PATTERN (i3) = newpat;
4051
4052 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4053 {
4054 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4055
4056 reset_used_flags (call_usage);
4057 call_usage = copy_rtx (call_usage);
4058
4059 if (substed_i2)
4060 {
4061 /* I2SRC must still be meaningful at this point. Some splitting
4062 operations can invalidate I2SRC, but those operations do not
4063 apply to calls. */
4064 gcc_assert (i2src);
4065 replace_rtx (call_usage, i2dest, i2src);
4066 }
4067
4068 if (substed_i1)
4069 replace_rtx (call_usage, i1dest, i1src);
4070 if (substed_i0)
4071 replace_rtx (call_usage, i0dest, i0src);
4072
4073 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4074 }
4075
4076 if (undobuf.other_insn)
4077 INSN_CODE (undobuf.other_insn) = other_code_number;
4078
4079 /* We had one special case above where I2 had more than one set and
4080 we replaced a destination of one of those sets with the destination
4081 of I3. In that case, we have to update LOG_LINKS of insns later
4082 in this basic block. Note that this (expensive) case is rare.
4083
4084 Also, in this case, we must pretend that all REG_NOTEs for I2
4085 actually came from I3, so that REG_UNUSED notes from I2 will be
4086 properly handled. */
4087
4088 if (i3_subst_into_i2)
4089 {
4090 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4091 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4092 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4093 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4094 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4095 && ! find_reg_note (i2, REG_UNUSED,
4096 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4097 for (temp_insn = NEXT_INSN (i2);
4098 temp_insn
4099 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4100 || BB_HEAD (this_basic_block) != temp_insn);
4101 temp_insn = NEXT_INSN (temp_insn))
4102 if (temp_insn != i3 && INSN_P (temp_insn))
4103 FOR_EACH_LOG_LINK (link, temp_insn)
4104 if (link->insn == i2)
4105 link->insn = i3;
4106
4107 if (i3notes)
4108 {
4109 rtx link = i3notes;
4110 while (XEXP (link, 1))
4111 link = XEXP (link, 1);
4112 XEXP (link, 1) = i2notes;
4113 }
4114 else
4115 i3notes = i2notes;
4116 i2notes = 0;
4117 }
4118
4119 LOG_LINKS (i3) = NULL;
4120 REG_NOTES (i3) = 0;
4121 LOG_LINKS (i2) = NULL;
4122 REG_NOTES (i2) = 0;
4123
4124 if (newi2pat)
4125 {
4126 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4127 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4128 this_basic_block);
4129 INSN_CODE (i2) = i2_code_number;
4130 PATTERN (i2) = newi2pat;
4131 }
4132 else
4133 {
4134 if (MAY_HAVE_DEBUG_INSNS && i2src)
4135 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4136 this_basic_block);
4137 SET_INSN_DELETED (i2);
4138 }
4139
4140 if (i1)
4141 {
4142 LOG_LINKS (i1) = NULL;
4143 REG_NOTES (i1) = 0;
4144 if (MAY_HAVE_DEBUG_INSNS)
4145 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4146 this_basic_block);
4147 SET_INSN_DELETED (i1);
4148 }
4149
4150 if (i0)
4151 {
4152 LOG_LINKS (i0) = NULL;
4153 REG_NOTES (i0) = 0;
4154 if (MAY_HAVE_DEBUG_INSNS)
4155 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4156 this_basic_block);
4157 SET_INSN_DELETED (i0);
4158 }
4159
4160 /* Get death notes for everything that is now used in either I3 or
4161 I2 and used to die in a previous insn. If we built two new
4162 patterns, move from I1 to I2 then I2 to I3 so that we get the
4163 proper movement on registers that I2 modifies. */
4164
4165 if (i0)
4166 from_luid = DF_INSN_LUID (i0);
4167 else if (i1)
4168 from_luid = DF_INSN_LUID (i1);
4169 else
4170 from_luid = DF_INSN_LUID (i2);
4171 if (newi2pat)
4172 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4173 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4174
4175 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4176 if (i3notes)
4177 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4178 elim_i2, elim_i1, elim_i0);
4179 if (i2notes)
4180 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4181 elim_i2, elim_i1, elim_i0);
4182 if (i1notes)
4183 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4184 elim_i2, elim_i1, elim_i0);
4185 if (i0notes)
4186 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4187 elim_i2, elim_i1, elim_i0);
4188 if (midnotes)
4189 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4190 elim_i2, elim_i1, elim_i0);
4191
4192 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4193 know these are REG_UNUSED and want them to go to the desired insn,
4194 so we always pass it as i3. */
4195
4196 if (newi2pat && new_i2_notes)
4197 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4198 NULL_RTX);
4199
4200 if (new_i3_notes)
4201 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4202 NULL_RTX);
4203
4204 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4205 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4206 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4207 in that case, it might delete I2. Similarly for I2 and I1.
4208 Show an additional death due to the REG_DEAD note we make here. If
4209 we discard it in distribute_notes, we will decrement it again. */
4210
4211 if (i3dest_killed)
4212 {
4213 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4214 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4215 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4216 elim_i1, elim_i0);
4217 else
4218 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4219 elim_i2, elim_i1, elim_i0);
4220 }
4221
4222 if (i2dest_in_i2src)
4223 {
4224 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4225 if (newi2pat && reg_set_p (i2dest, newi2pat))
4226 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4227 NULL_RTX, NULL_RTX);
4228 else
4229 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4230 NULL_RTX, NULL_RTX, NULL_RTX);
4231 }
4232
4233 if (i1dest_in_i1src)
4234 {
4235 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4236 if (newi2pat && reg_set_p (i1dest, newi2pat))
4237 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4238 NULL_RTX, NULL_RTX);
4239 else
4240 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4241 NULL_RTX, NULL_RTX, NULL_RTX);
4242 }
4243
4244 if (i0dest_in_i0src)
4245 {
4246 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4247 if (newi2pat && reg_set_p (i0dest, newi2pat))
4248 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4249 NULL_RTX, NULL_RTX);
4250 else
4251 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4252 NULL_RTX, NULL_RTX, NULL_RTX);
4253 }
4254
4255 distribute_links (i3links);
4256 distribute_links (i2links);
4257 distribute_links (i1links);
4258 distribute_links (i0links);
4259
4260 if (REG_P (i2dest))
4261 {
4262 struct insn_link *link;
4263 rtx_insn *i2_insn = 0;
4264 rtx i2_val = 0, set;
4265
4266 /* The insn that used to set this register doesn't exist, and
4267 this life of the register may not exist either. See if one of
4268 I3's links points to an insn that sets I2DEST. If it does,
4269 that is now the last known value for I2DEST. If we don't update
4270 this and I2 set the register to a value that depended on its old
4271 contents, we will get confused. If this insn is used, thing
4272 will be set correctly in combine_instructions. */
4273 FOR_EACH_LOG_LINK (link, i3)
4274 if ((set = single_set (link->insn)) != 0
4275 && rtx_equal_p (i2dest, SET_DEST (set)))
4276 i2_insn = link->insn, i2_val = SET_SRC (set);
4277
4278 record_value_for_reg (i2dest, i2_insn, i2_val);
4279
4280 /* If the reg formerly set in I2 died only once and that was in I3,
4281 zero its use count so it won't make `reload' do any work. */
4282 if (! added_sets_2
4283 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4284 && ! i2dest_in_i2src)
4285 INC_REG_N_SETS (REGNO (i2dest), -1);
4286 }
4287
4288 if (i1 && REG_P (i1dest))
4289 {
4290 struct insn_link *link;
4291 rtx_insn *i1_insn = 0;
4292 rtx i1_val = 0, set;
4293
4294 FOR_EACH_LOG_LINK (link, i3)
4295 if ((set = single_set (link->insn)) != 0
4296 && rtx_equal_p (i1dest, SET_DEST (set)))
4297 i1_insn = link->insn, i1_val = SET_SRC (set);
4298
4299 record_value_for_reg (i1dest, i1_insn, i1_val);
4300
4301 if (! added_sets_1 && ! i1dest_in_i1src)
4302 INC_REG_N_SETS (REGNO (i1dest), -1);
4303 }
4304
4305 if (i0 && REG_P (i0dest))
4306 {
4307 struct insn_link *link;
4308 rtx_insn *i0_insn = 0;
4309 rtx i0_val = 0, set;
4310
4311 FOR_EACH_LOG_LINK (link, i3)
4312 if ((set = single_set (link->insn)) != 0
4313 && rtx_equal_p (i0dest, SET_DEST (set)))
4314 i0_insn = link->insn, i0_val = SET_SRC (set);
4315
4316 record_value_for_reg (i0dest, i0_insn, i0_val);
4317
4318 if (! added_sets_0 && ! i0dest_in_i0src)
4319 INC_REG_N_SETS (REGNO (i0dest), -1);
4320 }
4321
4322 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4323 been made to this insn. The order is important, because newi2pat
4324 can affect nonzero_bits of newpat. */
4325 if (newi2pat)
4326 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4327 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4328 }
4329
4330 if (undobuf.other_insn != NULL_RTX)
4331 {
4332 if (dump_file)
4333 {
4334 fprintf (dump_file, "modifying other_insn ");
4335 dump_insn_slim (dump_file, undobuf.other_insn);
4336 }
4337 df_insn_rescan (undobuf.other_insn);
4338 }
4339
4340 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4341 {
4342 if (dump_file)
4343 {
4344 fprintf (dump_file, "modifying insn i0 ");
4345 dump_insn_slim (dump_file, i0);
4346 }
4347 df_insn_rescan (i0);
4348 }
4349
4350 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4351 {
4352 if (dump_file)
4353 {
4354 fprintf (dump_file, "modifying insn i1 ");
4355 dump_insn_slim (dump_file, i1);
4356 }
4357 df_insn_rescan (i1);
4358 }
4359
4360 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4361 {
4362 if (dump_file)
4363 {
4364 fprintf (dump_file, "modifying insn i2 ");
4365 dump_insn_slim (dump_file, i2);
4366 }
4367 df_insn_rescan (i2);
4368 }
4369
4370 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4371 {
4372 if (dump_file)
4373 {
4374 fprintf (dump_file, "modifying insn i3 ");
4375 dump_insn_slim (dump_file, i3);
4376 }
4377 df_insn_rescan (i3);
4378 }
4379
4380 /* Set new_direct_jump_p if a new return or simple jump instruction
4381 has been created. Adjust the CFG accordingly. */
4382 if (returnjump_p (i3) || any_uncondjump_p (i3))
4383 {
4384 *new_direct_jump_p = 1;
4385 mark_jump_label (PATTERN (i3), i3, 0);
4386 update_cfg_for_uncondjump (i3);
4387 }
4388
4389 if (undobuf.other_insn != NULL_RTX
4390 && (returnjump_p (undobuf.other_insn)
4391 || any_uncondjump_p (undobuf.other_insn)))
4392 {
4393 *new_direct_jump_p = 1;
4394 update_cfg_for_uncondjump (undobuf.other_insn);
4395 }
4396
4397 /* A noop might also need cleaning up of CFG, if it comes from the
4398 simplification of a jump. */
4399 if (JUMP_P (i3)
4400 && GET_CODE (newpat) == SET
4401 && SET_SRC (newpat) == pc_rtx
4402 && SET_DEST (newpat) == pc_rtx)
4403 {
4404 *new_direct_jump_p = 1;
4405 update_cfg_for_uncondjump (i3);
4406 }
4407
4408 if (undobuf.other_insn != NULL_RTX
4409 && JUMP_P (undobuf.other_insn)
4410 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4411 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4412 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4413 {
4414 *new_direct_jump_p = 1;
4415 update_cfg_for_uncondjump (undobuf.other_insn);
4416 }
4417
4418 combine_successes++;
4419 undo_commit ();
4420
4421 if (added_links_insn
4422 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4423 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4424 return added_links_insn;
4425 else
4426 return newi2pat ? i2 : i3;
4427 }
4428 \f
4429 /* Undo all the modifications recorded in undobuf. */
4430
4431 static void
4432 undo_all (void)
4433 {
4434 struct undo *undo, *next;
4435
4436 for (undo = undobuf.undos; undo; undo = next)
4437 {
4438 next = undo->next;
4439 switch (undo->kind)
4440 {
4441 case UNDO_RTX:
4442 *undo->where.r = undo->old_contents.r;
4443 break;
4444 case UNDO_INT:
4445 *undo->where.i = undo->old_contents.i;
4446 break;
4447 case UNDO_MODE:
4448 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4449 break;
4450 case UNDO_LINKS:
4451 *undo->where.l = undo->old_contents.l;
4452 break;
4453 default:
4454 gcc_unreachable ();
4455 }
4456
4457 undo->next = undobuf.frees;
4458 undobuf.frees = undo;
4459 }
4460
4461 undobuf.undos = 0;
4462 }
4463
4464 /* We've committed to accepting the changes we made. Move all
4465 of the undos to the free list. */
4466
4467 static void
4468 undo_commit (void)
4469 {
4470 struct undo *undo, *next;
4471
4472 for (undo = undobuf.undos; undo; undo = next)
4473 {
4474 next = undo->next;
4475 undo->next = undobuf.frees;
4476 undobuf.frees = undo;
4477 }
4478 undobuf.undos = 0;
4479 }
4480 \f
4481 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4482 where we have an arithmetic expression and return that point. LOC will
4483 be inside INSN.
4484
4485 try_combine will call this function to see if an insn can be split into
4486 two insns. */
4487
4488 static rtx *
4489 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4490 {
4491 rtx x = *loc;
4492 enum rtx_code code = GET_CODE (x);
4493 rtx *split;
4494 unsigned HOST_WIDE_INT len = 0;
4495 HOST_WIDE_INT pos = 0;
4496 int unsignedp = 0;
4497 rtx inner = NULL_RTX;
4498
4499 /* First special-case some codes. */
4500 switch (code)
4501 {
4502 case SUBREG:
4503 #ifdef INSN_SCHEDULING
4504 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4505 point. */
4506 if (MEM_P (SUBREG_REG (x)))
4507 return loc;
4508 #endif
4509 return find_split_point (&SUBREG_REG (x), insn, false);
4510
4511 case MEM:
4512 #ifdef HAVE_lo_sum
4513 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4514 using LO_SUM and HIGH. */
4515 if (GET_CODE (XEXP (x, 0)) == CONST
4516 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4517 {
4518 enum machine_mode address_mode = get_address_mode (x);
4519
4520 SUBST (XEXP (x, 0),
4521 gen_rtx_LO_SUM (address_mode,
4522 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4523 XEXP (x, 0)));
4524 return &XEXP (XEXP (x, 0), 0);
4525 }
4526 #endif
4527
4528 /* If we have a PLUS whose second operand is a constant and the
4529 address is not valid, perhaps will can split it up using
4530 the machine-specific way to split large constants. We use
4531 the first pseudo-reg (one of the virtual regs) as a placeholder;
4532 it will not remain in the result. */
4533 if (GET_CODE (XEXP (x, 0)) == PLUS
4534 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4535 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4536 MEM_ADDR_SPACE (x)))
4537 {
4538 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4539 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4540 XEXP (x, 0)),
4541 subst_insn);
4542
4543 /* This should have produced two insns, each of which sets our
4544 placeholder. If the source of the second is a valid address,
4545 we can make put both sources together and make a split point
4546 in the middle. */
4547
4548 if (seq
4549 && NEXT_INSN (seq) != NULL_RTX
4550 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4551 && NONJUMP_INSN_P (seq)
4552 && GET_CODE (PATTERN (seq)) == SET
4553 && SET_DEST (PATTERN (seq)) == reg
4554 && ! reg_mentioned_p (reg,
4555 SET_SRC (PATTERN (seq)))
4556 && NONJUMP_INSN_P (NEXT_INSN (seq))
4557 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4558 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4559 && memory_address_addr_space_p
4560 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4561 MEM_ADDR_SPACE (x)))
4562 {
4563 rtx src1 = SET_SRC (PATTERN (seq));
4564 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4565
4566 /* Replace the placeholder in SRC2 with SRC1. If we can
4567 find where in SRC2 it was placed, that can become our
4568 split point and we can replace this address with SRC2.
4569 Just try two obvious places. */
4570
4571 src2 = replace_rtx (src2, reg, src1);
4572 split = 0;
4573 if (XEXP (src2, 0) == src1)
4574 split = &XEXP (src2, 0);
4575 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4576 && XEXP (XEXP (src2, 0), 0) == src1)
4577 split = &XEXP (XEXP (src2, 0), 0);
4578
4579 if (split)
4580 {
4581 SUBST (XEXP (x, 0), src2);
4582 return split;
4583 }
4584 }
4585
4586 /* If that didn't work, perhaps the first operand is complex and
4587 needs to be computed separately, so make a split point there.
4588 This will occur on machines that just support REG + CONST
4589 and have a constant moved through some previous computation. */
4590
4591 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4592 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4593 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4594 return &XEXP (XEXP (x, 0), 0);
4595 }
4596
4597 /* If we have a PLUS whose first operand is complex, try computing it
4598 separately by making a split there. */
4599 if (GET_CODE (XEXP (x, 0)) == PLUS
4600 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4601 MEM_ADDR_SPACE (x))
4602 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4603 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4604 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4605 return &XEXP (XEXP (x, 0), 0);
4606 break;
4607
4608 case SET:
4609 #ifdef HAVE_cc0
4610 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4611 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4612 we need to put the operand into a register. So split at that
4613 point. */
4614
4615 if (SET_DEST (x) == cc0_rtx
4616 && GET_CODE (SET_SRC (x)) != COMPARE
4617 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4618 && !OBJECT_P (SET_SRC (x))
4619 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4620 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4621 return &SET_SRC (x);
4622 #endif
4623
4624 /* See if we can split SET_SRC as it stands. */
4625 split = find_split_point (&SET_SRC (x), insn, true);
4626 if (split && split != &SET_SRC (x))
4627 return split;
4628
4629 /* See if we can split SET_DEST as it stands. */
4630 split = find_split_point (&SET_DEST (x), insn, false);
4631 if (split && split != &SET_DEST (x))
4632 return split;
4633
4634 /* See if this is a bitfield assignment with everything constant. If
4635 so, this is an IOR of an AND, so split it into that. */
4636 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4637 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4638 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4639 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4640 && CONST_INT_P (SET_SRC (x))
4641 && ((INTVAL (XEXP (SET_DEST (x), 1))
4642 + INTVAL (XEXP (SET_DEST (x), 2)))
4643 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4644 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4645 {
4646 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4647 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4648 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4649 rtx dest = XEXP (SET_DEST (x), 0);
4650 enum machine_mode mode = GET_MODE (dest);
4651 unsigned HOST_WIDE_INT mask
4652 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4653 rtx or_mask;
4654
4655 if (BITS_BIG_ENDIAN)
4656 pos = GET_MODE_PRECISION (mode) - len - pos;
4657
4658 or_mask = gen_int_mode (src << pos, mode);
4659 if (src == mask)
4660 SUBST (SET_SRC (x),
4661 simplify_gen_binary (IOR, mode, dest, or_mask));
4662 else
4663 {
4664 rtx negmask = gen_int_mode (~(mask << pos), mode);
4665 SUBST (SET_SRC (x),
4666 simplify_gen_binary (IOR, mode,
4667 simplify_gen_binary (AND, mode,
4668 dest, negmask),
4669 or_mask));
4670 }
4671
4672 SUBST (SET_DEST (x), dest);
4673
4674 split = find_split_point (&SET_SRC (x), insn, true);
4675 if (split && split != &SET_SRC (x))
4676 return split;
4677 }
4678
4679 /* Otherwise, see if this is an operation that we can split into two.
4680 If so, try to split that. */
4681 code = GET_CODE (SET_SRC (x));
4682
4683 switch (code)
4684 {
4685 case AND:
4686 /* If we are AND'ing with a large constant that is only a single
4687 bit and the result is only being used in a context where we
4688 need to know if it is zero or nonzero, replace it with a bit
4689 extraction. This will avoid the large constant, which might
4690 have taken more than one insn to make. If the constant were
4691 not a valid argument to the AND but took only one insn to make,
4692 this is no worse, but if it took more than one insn, it will
4693 be better. */
4694
4695 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4696 && REG_P (XEXP (SET_SRC (x), 0))
4697 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4698 && REG_P (SET_DEST (x))
4699 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4700 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4701 && XEXP (*split, 0) == SET_DEST (x)
4702 && XEXP (*split, 1) == const0_rtx)
4703 {
4704 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4705 XEXP (SET_SRC (x), 0),
4706 pos, NULL_RTX, 1, 1, 0, 0);
4707 if (extraction != 0)
4708 {
4709 SUBST (SET_SRC (x), extraction);
4710 return find_split_point (loc, insn, false);
4711 }
4712 }
4713 break;
4714
4715 case NE:
4716 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4717 is known to be on, this can be converted into a NEG of a shift. */
4718 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4719 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4720 && 1 <= (pos = exact_log2
4721 (nonzero_bits (XEXP (SET_SRC (x), 0),
4722 GET_MODE (XEXP (SET_SRC (x), 0))))))
4723 {
4724 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4725
4726 SUBST (SET_SRC (x),
4727 gen_rtx_NEG (mode,
4728 gen_rtx_LSHIFTRT (mode,
4729 XEXP (SET_SRC (x), 0),
4730 GEN_INT (pos))));
4731
4732 split = find_split_point (&SET_SRC (x), insn, true);
4733 if (split && split != &SET_SRC (x))
4734 return split;
4735 }
4736 break;
4737
4738 case SIGN_EXTEND:
4739 inner = XEXP (SET_SRC (x), 0);
4740
4741 /* We can't optimize if either mode is a partial integer
4742 mode as we don't know how many bits are significant
4743 in those modes. */
4744 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4745 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4746 break;
4747
4748 pos = 0;
4749 len = GET_MODE_PRECISION (GET_MODE (inner));
4750 unsignedp = 0;
4751 break;
4752
4753 case SIGN_EXTRACT:
4754 case ZERO_EXTRACT:
4755 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4756 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4757 {
4758 inner = XEXP (SET_SRC (x), 0);
4759 len = INTVAL (XEXP (SET_SRC (x), 1));
4760 pos = INTVAL (XEXP (SET_SRC (x), 2));
4761
4762 if (BITS_BIG_ENDIAN)
4763 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4764 unsignedp = (code == ZERO_EXTRACT);
4765 }
4766 break;
4767
4768 default:
4769 break;
4770 }
4771
4772 if (len && pos >= 0
4773 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4774 {
4775 enum machine_mode mode = GET_MODE (SET_SRC (x));
4776
4777 /* For unsigned, we have a choice of a shift followed by an
4778 AND or two shifts. Use two shifts for field sizes where the
4779 constant might be too large. We assume here that we can
4780 always at least get 8-bit constants in an AND insn, which is
4781 true for every current RISC. */
4782
4783 if (unsignedp && len <= 8)
4784 {
4785 unsigned HOST_WIDE_INT mask
4786 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4787 SUBST (SET_SRC (x),
4788 gen_rtx_AND (mode,
4789 gen_rtx_LSHIFTRT
4790 (mode, gen_lowpart (mode, inner),
4791 GEN_INT (pos)),
4792 gen_int_mode (mask, mode)));
4793
4794 split = find_split_point (&SET_SRC (x), insn, true);
4795 if (split && split != &SET_SRC (x))
4796 return split;
4797 }
4798 else
4799 {
4800 SUBST (SET_SRC (x),
4801 gen_rtx_fmt_ee
4802 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4803 gen_rtx_ASHIFT (mode,
4804 gen_lowpart (mode, inner),
4805 GEN_INT (GET_MODE_PRECISION (mode)
4806 - len - pos)),
4807 GEN_INT (GET_MODE_PRECISION (mode) - len)));
4808
4809 split = find_split_point (&SET_SRC (x), insn, true);
4810 if (split && split != &SET_SRC (x))
4811 return split;
4812 }
4813 }
4814
4815 /* See if this is a simple operation with a constant as the second
4816 operand. It might be that this constant is out of range and hence
4817 could be used as a split point. */
4818 if (BINARY_P (SET_SRC (x))
4819 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4820 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4821 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4822 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4823 return &XEXP (SET_SRC (x), 1);
4824
4825 /* Finally, see if this is a simple operation with its first operand
4826 not in a register. The operation might require this operand in a
4827 register, so return it as a split point. We can always do this
4828 because if the first operand were another operation, we would have
4829 already found it as a split point. */
4830 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4831 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4832 return &XEXP (SET_SRC (x), 0);
4833
4834 return 0;
4835
4836 case AND:
4837 case IOR:
4838 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4839 it is better to write this as (not (ior A B)) so we can split it.
4840 Similarly for IOR. */
4841 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4842 {
4843 SUBST (*loc,
4844 gen_rtx_NOT (GET_MODE (x),
4845 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4846 GET_MODE (x),
4847 XEXP (XEXP (x, 0), 0),
4848 XEXP (XEXP (x, 1), 0))));
4849 return find_split_point (loc, insn, set_src);
4850 }
4851
4852 /* Many RISC machines have a large set of logical insns. If the
4853 second operand is a NOT, put it first so we will try to split the
4854 other operand first. */
4855 if (GET_CODE (XEXP (x, 1)) == NOT)
4856 {
4857 rtx tem = XEXP (x, 0);
4858 SUBST (XEXP (x, 0), XEXP (x, 1));
4859 SUBST (XEXP (x, 1), tem);
4860 }
4861 break;
4862
4863 case PLUS:
4864 case MINUS:
4865 /* Canonicalization can produce (minus A (mult B C)), where C is a
4866 constant. It may be better to try splitting (plus (mult B -C) A)
4867 instead if this isn't a multiply by a power of two. */
4868 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
4869 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4870 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
4871 {
4872 enum machine_mode mode = GET_MODE (x);
4873 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
4874 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
4875 SUBST (*loc, gen_rtx_PLUS (mode,
4876 gen_rtx_MULT (mode,
4877 XEXP (XEXP (x, 1), 0),
4878 gen_int_mode (other_int,
4879 mode)),
4880 XEXP (x, 0)));
4881 return find_split_point (loc, insn, set_src);
4882 }
4883
4884 /* Split at a multiply-accumulate instruction. However if this is
4885 the SET_SRC, we likely do not have such an instruction and it's
4886 worthless to try this split. */
4887 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
4888 return loc;
4889
4890 default:
4891 break;
4892 }
4893
4894 /* Otherwise, select our actions depending on our rtx class. */
4895 switch (GET_RTX_CLASS (code))
4896 {
4897 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4898 case RTX_TERNARY:
4899 split = find_split_point (&XEXP (x, 2), insn, false);
4900 if (split)
4901 return split;
4902 /* ... fall through ... */
4903 case RTX_BIN_ARITH:
4904 case RTX_COMM_ARITH:
4905 case RTX_COMPARE:
4906 case RTX_COMM_COMPARE:
4907 split = find_split_point (&XEXP (x, 1), insn, false);
4908 if (split)
4909 return split;
4910 /* ... fall through ... */
4911 case RTX_UNARY:
4912 /* Some machines have (and (shift ...) ...) insns. If X is not
4913 an AND, but XEXP (X, 0) is, use it as our split point. */
4914 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4915 return &XEXP (x, 0);
4916
4917 split = find_split_point (&XEXP (x, 0), insn, false);
4918 if (split)
4919 return split;
4920 return loc;
4921
4922 default:
4923 /* Otherwise, we don't have a split point. */
4924 return 0;
4925 }
4926 }
4927 \f
4928 /* Throughout X, replace FROM with TO, and return the result.
4929 The result is TO if X is FROM;
4930 otherwise the result is X, but its contents may have been modified.
4931 If they were modified, a record was made in undobuf so that
4932 undo_all will (among other things) return X to its original state.
4933
4934 If the number of changes necessary is too much to record to undo,
4935 the excess changes are not made, so the result is invalid.
4936 The changes already made can still be undone.
4937 undobuf.num_undo is incremented for such changes, so by testing that
4938 the caller can tell whether the result is valid.
4939
4940 `n_occurrences' is incremented each time FROM is replaced.
4941
4942 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4943
4944 IN_COND is nonzero if we are at the top level of a condition.
4945
4946 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4947 by copying if `n_occurrences' is nonzero. */
4948
4949 static rtx
4950 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
4951 {
4952 enum rtx_code code = GET_CODE (x);
4953 enum machine_mode op0_mode = VOIDmode;
4954 const char *fmt;
4955 int len, i;
4956 rtx new_rtx;
4957
4958 /* Two expressions are equal if they are identical copies of a shared
4959 RTX or if they are both registers with the same register number
4960 and mode. */
4961
4962 #define COMBINE_RTX_EQUAL_P(X,Y) \
4963 ((X) == (Y) \
4964 || (REG_P (X) && REG_P (Y) \
4965 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4966
4967 /* Do not substitute into clobbers of regs -- this will never result in
4968 valid RTL. */
4969 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
4970 return x;
4971
4972 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4973 {
4974 n_occurrences++;
4975 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4976 }
4977
4978 /* If X and FROM are the same register but different modes, they
4979 will not have been seen as equal above. However, the log links code
4980 will make a LOG_LINKS entry for that case. If we do nothing, we
4981 will try to rerecognize our original insn and, when it succeeds,
4982 we will delete the feeding insn, which is incorrect.
4983
4984 So force this insn not to match in this (rare) case. */
4985 if (! in_dest && code == REG && REG_P (from)
4986 && reg_overlap_mentioned_p (x, from))
4987 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4988
4989 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4990 of which may contain things that can be combined. */
4991 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4992 return x;
4993
4994 /* It is possible to have a subexpression appear twice in the insn.
4995 Suppose that FROM is a register that appears within TO.
4996 Then, after that subexpression has been scanned once by `subst',
4997 the second time it is scanned, TO may be found. If we were
4998 to scan TO here, we would find FROM within it and create a
4999 self-referent rtl structure which is completely wrong. */
5000 if (COMBINE_RTX_EQUAL_P (x, to))
5001 return to;
5002
5003 /* Parallel asm_operands need special attention because all of the
5004 inputs are shared across the arms. Furthermore, unsharing the
5005 rtl results in recognition failures. Failure to handle this case
5006 specially can result in circular rtl.
5007
5008 Solve this by doing a normal pass across the first entry of the
5009 parallel, and only processing the SET_DESTs of the subsequent
5010 entries. Ug. */
5011
5012 if (code == PARALLEL
5013 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5014 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5015 {
5016 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5017
5018 /* If this substitution failed, this whole thing fails. */
5019 if (GET_CODE (new_rtx) == CLOBBER
5020 && XEXP (new_rtx, 0) == const0_rtx)
5021 return new_rtx;
5022
5023 SUBST (XVECEXP (x, 0, 0), new_rtx);
5024
5025 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5026 {
5027 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5028
5029 if (!REG_P (dest)
5030 && GET_CODE (dest) != CC0
5031 && GET_CODE (dest) != PC)
5032 {
5033 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5034
5035 /* If this substitution failed, this whole thing fails. */
5036 if (GET_CODE (new_rtx) == CLOBBER
5037 && XEXP (new_rtx, 0) == const0_rtx)
5038 return new_rtx;
5039
5040 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5041 }
5042 }
5043 }
5044 else
5045 {
5046 len = GET_RTX_LENGTH (code);
5047 fmt = GET_RTX_FORMAT (code);
5048
5049 /* We don't need to process a SET_DEST that is a register, CC0,
5050 or PC, so set up to skip this common case. All other cases
5051 where we want to suppress replacing something inside a
5052 SET_SRC are handled via the IN_DEST operand. */
5053 if (code == SET
5054 && (REG_P (SET_DEST (x))
5055 || GET_CODE (SET_DEST (x)) == CC0
5056 || GET_CODE (SET_DEST (x)) == PC))
5057 fmt = "ie";
5058
5059 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5060 constant. */
5061 if (fmt[0] == 'e')
5062 op0_mode = GET_MODE (XEXP (x, 0));
5063
5064 for (i = 0; i < len; i++)
5065 {
5066 if (fmt[i] == 'E')
5067 {
5068 int j;
5069 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5070 {
5071 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5072 {
5073 new_rtx = (unique_copy && n_occurrences
5074 ? copy_rtx (to) : to);
5075 n_occurrences++;
5076 }
5077 else
5078 {
5079 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5080 unique_copy);
5081
5082 /* If this substitution failed, this whole thing
5083 fails. */
5084 if (GET_CODE (new_rtx) == CLOBBER
5085 && XEXP (new_rtx, 0) == const0_rtx)
5086 return new_rtx;
5087 }
5088
5089 SUBST (XVECEXP (x, i, j), new_rtx);
5090 }
5091 }
5092 else if (fmt[i] == 'e')
5093 {
5094 /* If this is a register being set, ignore it. */
5095 new_rtx = XEXP (x, i);
5096 if (in_dest
5097 && i == 0
5098 && (((code == SUBREG || code == ZERO_EXTRACT)
5099 && REG_P (new_rtx))
5100 || code == STRICT_LOW_PART))
5101 ;
5102
5103 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5104 {
5105 /* In general, don't install a subreg involving two
5106 modes not tieable. It can worsen register
5107 allocation, and can even make invalid reload
5108 insns, since the reg inside may need to be copied
5109 from in the outside mode, and that may be invalid
5110 if it is an fp reg copied in integer mode.
5111
5112 We allow two exceptions to this: It is valid if
5113 it is inside another SUBREG and the mode of that
5114 SUBREG and the mode of the inside of TO is
5115 tieable and it is valid if X is a SET that copies
5116 FROM to CC0. */
5117
5118 if (GET_CODE (to) == SUBREG
5119 && ! MODES_TIEABLE_P (GET_MODE (to),
5120 GET_MODE (SUBREG_REG (to)))
5121 && ! (code == SUBREG
5122 && MODES_TIEABLE_P (GET_MODE (x),
5123 GET_MODE (SUBREG_REG (to))))
5124 #ifdef HAVE_cc0
5125 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5126 #endif
5127 )
5128 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5129
5130 if (code == SUBREG
5131 && REG_P (to)
5132 && REGNO (to) < FIRST_PSEUDO_REGISTER
5133 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5134 SUBREG_BYTE (x),
5135 GET_MODE (x)) < 0)
5136 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5137
5138 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5139 n_occurrences++;
5140 }
5141 else
5142 /* If we are in a SET_DEST, suppress most cases unless we
5143 have gone inside a MEM, in which case we want to
5144 simplify the address. We assume here that things that
5145 are actually part of the destination have their inner
5146 parts in the first expression. This is true for SUBREG,
5147 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5148 things aside from REG and MEM that should appear in a
5149 SET_DEST. */
5150 new_rtx = subst (XEXP (x, i), from, to,
5151 (((in_dest
5152 && (code == SUBREG || code == STRICT_LOW_PART
5153 || code == ZERO_EXTRACT))
5154 || code == SET)
5155 && i == 0),
5156 code == IF_THEN_ELSE && i == 0,
5157 unique_copy);
5158
5159 /* If we found that we will have to reject this combination,
5160 indicate that by returning the CLOBBER ourselves, rather than
5161 an expression containing it. This will speed things up as
5162 well as prevent accidents where two CLOBBERs are considered
5163 to be equal, thus producing an incorrect simplification. */
5164
5165 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5166 return new_rtx;
5167
5168 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5169 {
5170 enum machine_mode mode = GET_MODE (x);
5171
5172 x = simplify_subreg (GET_MODE (x), new_rtx,
5173 GET_MODE (SUBREG_REG (x)),
5174 SUBREG_BYTE (x));
5175 if (! x)
5176 x = gen_rtx_CLOBBER (mode, const0_rtx);
5177 }
5178 else if (CONST_SCALAR_INT_P (new_rtx)
5179 && GET_CODE (x) == ZERO_EXTEND)
5180 {
5181 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5182 new_rtx, GET_MODE (XEXP (x, 0)));
5183 gcc_assert (x);
5184 }
5185 else
5186 SUBST (XEXP (x, i), new_rtx);
5187 }
5188 }
5189 }
5190
5191 /* Check if we are loading something from the constant pool via float
5192 extension; in this case we would undo compress_float_constant
5193 optimization and degenerate constant load to an immediate value. */
5194 if (GET_CODE (x) == FLOAT_EXTEND
5195 && MEM_P (XEXP (x, 0))
5196 && MEM_READONLY_P (XEXP (x, 0)))
5197 {
5198 rtx tmp = avoid_constant_pool_reference (x);
5199 if (x != tmp)
5200 return x;
5201 }
5202
5203 /* Try to simplify X. If the simplification changed the code, it is likely
5204 that further simplification will help, so loop, but limit the number
5205 of repetitions that will be performed. */
5206
5207 for (i = 0; i < 4; i++)
5208 {
5209 /* If X is sufficiently simple, don't bother trying to do anything
5210 with it. */
5211 if (code != CONST_INT && code != REG && code != CLOBBER)
5212 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5213
5214 if (GET_CODE (x) == code)
5215 break;
5216
5217 code = GET_CODE (x);
5218
5219 /* We no longer know the original mode of operand 0 since we
5220 have changed the form of X) */
5221 op0_mode = VOIDmode;
5222 }
5223
5224 return x;
5225 }
5226 \f
5227 /* Simplify X, a piece of RTL. We just operate on the expression at the
5228 outer level; call `subst' to simplify recursively. Return the new
5229 expression.
5230
5231 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5232 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5233 of a condition. */
5234
5235 static rtx
5236 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest,
5237 int in_cond)
5238 {
5239 enum rtx_code code = GET_CODE (x);
5240 enum machine_mode mode = GET_MODE (x);
5241 rtx temp;
5242 int i;
5243
5244 /* If this is a commutative operation, put a constant last and a complex
5245 expression first. We don't need to do this for comparisons here. */
5246 if (COMMUTATIVE_ARITH_P (x)
5247 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5248 {
5249 temp = XEXP (x, 0);
5250 SUBST (XEXP (x, 0), XEXP (x, 1));
5251 SUBST (XEXP (x, 1), temp);
5252 }
5253
5254 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5255 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5256 things. Check for cases where both arms are testing the same
5257 condition.
5258
5259 Don't do anything if all operands are very simple. */
5260
5261 if ((BINARY_P (x)
5262 && ((!OBJECT_P (XEXP (x, 0))
5263 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5264 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5265 || (!OBJECT_P (XEXP (x, 1))
5266 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5267 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5268 || (UNARY_P (x)
5269 && (!OBJECT_P (XEXP (x, 0))
5270 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5271 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5272 {
5273 rtx cond, true_rtx, false_rtx;
5274
5275 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5276 if (cond != 0
5277 /* If everything is a comparison, what we have is highly unlikely
5278 to be simpler, so don't use it. */
5279 && ! (COMPARISON_P (x)
5280 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5281 {
5282 rtx cop1 = const0_rtx;
5283 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5284
5285 if (cond_code == NE && COMPARISON_P (cond))
5286 return x;
5287
5288 /* Simplify the alternative arms; this may collapse the true and
5289 false arms to store-flag values. Be careful to use copy_rtx
5290 here since true_rtx or false_rtx might share RTL with x as a
5291 result of the if_then_else_cond call above. */
5292 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5293 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5294
5295 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5296 is unlikely to be simpler. */
5297 if (general_operand (true_rtx, VOIDmode)
5298 && general_operand (false_rtx, VOIDmode))
5299 {
5300 enum rtx_code reversed;
5301
5302 /* Restarting if we generate a store-flag expression will cause
5303 us to loop. Just drop through in this case. */
5304
5305 /* If the result values are STORE_FLAG_VALUE and zero, we can
5306 just make the comparison operation. */
5307 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5308 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5309 cond, cop1);
5310 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5311 && ((reversed = reversed_comparison_code_parts
5312 (cond_code, cond, cop1, NULL))
5313 != UNKNOWN))
5314 x = simplify_gen_relational (reversed, mode, VOIDmode,
5315 cond, cop1);
5316
5317 /* Likewise, we can make the negate of a comparison operation
5318 if the result values are - STORE_FLAG_VALUE and zero. */
5319 else if (CONST_INT_P (true_rtx)
5320 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5321 && false_rtx == const0_rtx)
5322 x = simplify_gen_unary (NEG, mode,
5323 simplify_gen_relational (cond_code,
5324 mode, VOIDmode,
5325 cond, cop1),
5326 mode);
5327 else if (CONST_INT_P (false_rtx)
5328 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5329 && true_rtx == const0_rtx
5330 && ((reversed = reversed_comparison_code_parts
5331 (cond_code, cond, cop1, NULL))
5332 != UNKNOWN))
5333 x = simplify_gen_unary (NEG, mode,
5334 simplify_gen_relational (reversed,
5335 mode, VOIDmode,
5336 cond, cop1),
5337 mode);
5338 else
5339 return gen_rtx_IF_THEN_ELSE (mode,
5340 simplify_gen_relational (cond_code,
5341 mode,
5342 VOIDmode,
5343 cond,
5344 cop1),
5345 true_rtx, false_rtx);
5346
5347 code = GET_CODE (x);
5348 op0_mode = VOIDmode;
5349 }
5350 }
5351 }
5352
5353 /* Try to fold this expression in case we have constants that weren't
5354 present before. */
5355 temp = 0;
5356 switch (GET_RTX_CLASS (code))
5357 {
5358 case RTX_UNARY:
5359 if (op0_mode == VOIDmode)
5360 op0_mode = GET_MODE (XEXP (x, 0));
5361 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5362 break;
5363 case RTX_COMPARE:
5364 case RTX_COMM_COMPARE:
5365 {
5366 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5367 if (cmp_mode == VOIDmode)
5368 {
5369 cmp_mode = GET_MODE (XEXP (x, 1));
5370 if (cmp_mode == VOIDmode)
5371 cmp_mode = op0_mode;
5372 }
5373 temp = simplify_relational_operation (code, mode, cmp_mode,
5374 XEXP (x, 0), XEXP (x, 1));
5375 }
5376 break;
5377 case RTX_COMM_ARITH:
5378 case RTX_BIN_ARITH:
5379 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5380 break;
5381 case RTX_BITFIELD_OPS:
5382 case RTX_TERNARY:
5383 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5384 XEXP (x, 1), XEXP (x, 2));
5385 break;
5386 default:
5387 break;
5388 }
5389
5390 if (temp)
5391 {
5392 x = temp;
5393 code = GET_CODE (temp);
5394 op0_mode = VOIDmode;
5395 mode = GET_MODE (temp);
5396 }
5397
5398 /* First see if we can apply the inverse distributive law. */
5399 if (code == PLUS || code == MINUS
5400 || code == AND || code == IOR || code == XOR)
5401 {
5402 x = apply_distributive_law (x);
5403 code = GET_CODE (x);
5404 op0_mode = VOIDmode;
5405 }
5406
5407 /* If CODE is an associative operation not otherwise handled, see if we
5408 can associate some operands. This can win if they are constants or
5409 if they are logically related (i.e. (a & b) & a). */
5410 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5411 || code == AND || code == IOR || code == XOR
5412 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5413 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5414 || (flag_associative_math && FLOAT_MODE_P (mode))))
5415 {
5416 if (GET_CODE (XEXP (x, 0)) == code)
5417 {
5418 rtx other = XEXP (XEXP (x, 0), 0);
5419 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5420 rtx inner_op1 = XEXP (x, 1);
5421 rtx inner;
5422
5423 /* Make sure we pass the constant operand if any as the second
5424 one if this is a commutative operation. */
5425 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5426 {
5427 rtx tem = inner_op0;
5428 inner_op0 = inner_op1;
5429 inner_op1 = tem;
5430 }
5431 inner = simplify_binary_operation (code == MINUS ? PLUS
5432 : code == DIV ? MULT
5433 : code,
5434 mode, inner_op0, inner_op1);
5435
5436 /* For commutative operations, try the other pair if that one
5437 didn't simplify. */
5438 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5439 {
5440 other = XEXP (XEXP (x, 0), 1);
5441 inner = simplify_binary_operation (code, mode,
5442 XEXP (XEXP (x, 0), 0),
5443 XEXP (x, 1));
5444 }
5445
5446 if (inner)
5447 return simplify_gen_binary (code, mode, other, inner);
5448 }
5449 }
5450
5451 /* A little bit of algebraic simplification here. */
5452 switch (code)
5453 {
5454 case MEM:
5455 /* Ensure that our address has any ASHIFTs converted to MULT in case
5456 address-recognizing predicates are called later. */
5457 temp = make_compound_operation (XEXP (x, 0), MEM);
5458 SUBST (XEXP (x, 0), temp);
5459 break;
5460
5461 case SUBREG:
5462 if (op0_mode == VOIDmode)
5463 op0_mode = GET_MODE (SUBREG_REG (x));
5464
5465 /* See if this can be moved to simplify_subreg. */
5466 if (CONSTANT_P (SUBREG_REG (x))
5467 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5468 /* Don't call gen_lowpart if the inner mode
5469 is VOIDmode and we cannot simplify it, as SUBREG without
5470 inner mode is invalid. */
5471 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5472 || gen_lowpart_common (mode, SUBREG_REG (x))))
5473 return gen_lowpart (mode, SUBREG_REG (x));
5474
5475 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5476 break;
5477 {
5478 rtx temp;
5479 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5480 SUBREG_BYTE (x));
5481 if (temp)
5482 return temp;
5483
5484 /* If op is known to have all lower bits zero, the result is zero. */
5485 if (!in_dest
5486 && SCALAR_INT_MODE_P (mode)
5487 && SCALAR_INT_MODE_P (op0_mode)
5488 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5489 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5490 && HWI_COMPUTABLE_MODE_P (op0_mode)
5491 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5492 & GET_MODE_MASK (mode)) == 0)
5493 return CONST0_RTX (mode);
5494 }
5495
5496 /* Don't change the mode of the MEM if that would change the meaning
5497 of the address. */
5498 if (MEM_P (SUBREG_REG (x))
5499 && (MEM_VOLATILE_P (SUBREG_REG (x))
5500 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5501 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5502 return gen_rtx_CLOBBER (mode, const0_rtx);
5503
5504 /* Note that we cannot do any narrowing for non-constants since
5505 we might have been counting on using the fact that some bits were
5506 zero. We now do this in the SET. */
5507
5508 break;
5509
5510 case NEG:
5511 temp = expand_compound_operation (XEXP (x, 0));
5512
5513 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5514 replaced by (lshiftrt X C). This will convert
5515 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5516
5517 if (GET_CODE (temp) == ASHIFTRT
5518 && CONST_INT_P (XEXP (temp, 1))
5519 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5520 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5521 INTVAL (XEXP (temp, 1)));
5522
5523 /* If X has only a single bit that might be nonzero, say, bit I, convert
5524 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5525 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5526 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5527 or a SUBREG of one since we'd be making the expression more
5528 complex if it was just a register. */
5529
5530 if (!REG_P (temp)
5531 && ! (GET_CODE (temp) == SUBREG
5532 && REG_P (SUBREG_REG (temp)))
5533 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5534 {
5535 rtx temp1 = simplify_shift_const
5536 (NULL_RTX, ASHIFTRT, mode,
5537 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5538 GET_MODE_PRECISION (mode) - 1 - i),
5539 GET_MODE_PRECISION (mode) - 1 - i);
5540
5541 /* If all we did was surround TEMP with the two shifts, we
5542 haven't improved anything, so don't use it. Otherwise,
5543 we are better off with TEMP1. */
5544 if (GET_CODE (temp1) != ASHIFTRT
5545 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5546 || XEXP (XEXP (temp1, 0), 0) != temp)
5547 return temp1;
5548 }
5549 break;
5550
5551 case TRUNCATE:
5552 /* We can't handle truncation to a partial integer mode here
5553 because we don't know the real bitsize of the partial
5554 integer mode. */
5555 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5556 break;
5557
5558 if (HWI_COMPUTABLE_MODE_P (mode))
5559 SUBST (XEXP (x, 0),
5560 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5561 GET_MODE_MASK (mode), 0));
5562
5563 /* We can truncate a constant value and return it. */
5564 if (CONST_INT_P (XEXP (x, 0)))
5565 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5566
5567 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5568 whose value is a comparison can be replaced with a subreg if
5569 STORE_FLAG_VALUE permits. */
5570 if (HWI_COMPUTABLE_MODE_P (mode)
5571 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5572 && (temp = get_last_value (XEXP (x, 0)))
5573 && COMPARISON_P (temp))
5574 return gen_lowpart (mode, XEXP (x, 0));
5575 break;
5576
5577 case CONST:
5578 /* (const (const X)) can become (const X). Do it this way rather than
5579 returning the inner CONST since CONST can be shared with a
5580 REG_EQUAL note. */
5581 if (GET_CODE (XEXP (x, 0)) == CONST)
5582 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5583 break;
5584
5585 #ifdef HAVE_lo_sum
5586 case LO_SUM:
5587 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5588 can add in an offset. find_split_point will split this address up
5589 again if it doesn't match. */
5590 if (GET_CODE (XEXP (x, 0)) == HIGH
5591 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5592 return XEXP (x, 1);
5593 break;
5594 #endif
5595
5596 case PLUS:
5597 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5598 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5599 bit-field and can be replaced by either a sign_extend or a
5600 sign_extract. The `and' may be a zero_extend and the two
5601 <c>, -<c> constants may be reversed. */
5602 if (GET_CODE (XEXP (x, 0)) == XOR
5603 && CONST_INT_P (XEXP (x, 1))
5604 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5605 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5606 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5607 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5608 && HWI_COMPUTABLE_MODE_P (mode)
5609 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5610 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5611 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5612 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5613 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5614 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5615 == (unsigned int) i + 1))))
5616 return simplify_shift_const
5617 (NULL_RTX, ASHIFTRT, mode,
5618 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5619 XEXP (XEXP (XEXP (x, 0), 0), 0),
5620 GET_MODE_PRECISION (mode) - (i + 1)),
5621 GET_MODE_PRECISION (mode) - (i + 1));
5622
5623 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5624 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5625 the bitsize of the mode - 1. This allows simplification of
5626 "a = (b & 8) == 0;" */
5627 if (XEXP (x, 1) == constm1_rtx
5628 && !REG_P (XEXP (x, 0))
5629 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5630 && REG_P (SUBREG_REG (XEXP (x, 0))))
5631 && nonzero_bits (XEXP (x, 0), mode) == 1)
5632 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5633 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5634 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5635 GET_MODE_PRECISION (mode) - 1),
5636 GET_MODE_PRECISION (mode) - 1);
5637
5638 /* If we are adding two things that have no bits in common, convert
5639 the addition into an IOR. This will often be further simplified,
5640 for example in cases like ((a & 1) + (a & 2)), which can
5641 become a & 3. */
5642
5643 if (HWI_COMPUTABLE_MODE_P (mode)
5644 && (nonzero_bits (XEXP (x, 0), mode)
5645 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5646 {
5647 /* Try to simplify the expression further. */
5648 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5649 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5650
5651 /* If we could, great. If not, do not go ahead with the IOR
5652 replacement, since PLUS appears in many special purpose
5653 address arithmetic instructions. */
5654 if (GET_CODE (temp) != CLOBBER
5655 && (GET_CODE (temp) != IOR
5656 || ((XEXP (temp, 0) != XEXP (x, 0)
5657 || XEXP (temp, 1) != XEXP (x, 1))
5658 && (XEXP (temp, 0) != XEXP (x, 1)
5659 || XEXP (temp, 1) != XEXP (x, 0)))))
5660 return temp;
5661 }
5662 break;
5663
5664 case MINUS:
5665 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5666 (and <foo> (const_int pow2-1)) */
5667 if (GET_CODE (XEXP (x, 1)) == AND
5668 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5669 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5670 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5671 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5672 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5673 break;
5674
5675 case MULT:
5676 /* If we have (mult (plus A B) C), apply the distributive law and then
5677 the inverse distributive law to see if things simplify. This
5678 occurs mostly in addresses, often when unrolling loops. */
5679
5680 if (GET_CODE (XEXP (x, 0)) == PLUS)
5681 {
5682 rtx result = distribute_and_simplify_rtx (x, 0);
5683 if (result)
5684 return result;
5685 }
5686
5687 /* Try simplify a*(b/c) as (a*b)/c. */
5688 if (FLOAT_MODE_P (mode) && flag_associative_math
5689 && GET_CODE (XEXP (x, 0)) == DIV)
5690 {
5691 rtx tem = simplify_binary_operation (MULT, mode,
5692 XEXP (XEXP (x, 0), 0),
5693 XEXP (x, 1));
5694 if (tem)
5695 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5696 }
5697 break;
5698
5699 case UDIV:
5700 /* If this is a divide by a power of two, treat it as a shift if
5701 its first operand is a shift. */
5702 if (CONST_INT_P (XEXP (x, 1))
5703 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5704 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5705 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5706 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5707 || GET_CODE (XEXP (x, 0)) == ROTATE
5708 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5709 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5710 break;
5711
5712 case EQ: case NE:
5713 case GT: case GTU: case GE: case GEU:
5714 case LT: case LTU: case LE: case LEU:
5715 case UNEQ: case LTGT:
5716 case UNGT: case UNGE:
5717 case UNLT: case UNLE:
5718 case UNORDERED: case ORDERED:
5719 /* If the first operand is a condition code, we can't do anything
5720 with it. */
5721 if (GET_CODE (XEXP (x, 0)) == COMPARE
5722 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5723 && ! CC0_P (XEXP (x, 0))))
5724 {
5725 rtx op0 = XEXP (x, 0);
5726 rtx op1 = XEXP (x, 1);
5727 enum rtx_code new_code;
5728
5729 if (GET_CODE (op0) == COMPARE)
5730 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5731
5732 /* Simplify our comparison, if possible. */
5733 new_code = simplify_comparison (code, &op0, &op1);
5734
5735 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5736 if only the low-order bit is possibly nonzero in X (such as when
5737 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5738 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5739 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5740 (plus X 1).
5741
5742 Remove any ZERO_EXTRACT we made when thinking this was a
5743 comparison. It may now be simpler to use, e.g., an AND. If a
5744 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5745 the call to make_compound_operation in the SET case.
5746
5747 Don't apply these optimizations if the caller would
5748 prefer a comparison rather than a value.
5749 E.g., for the condition in an IF_THEN_ELSE most targets need
5750 an explicit comparison. */
5751
5752 if (in_cond)
5753 ;
5754
5755 else if (STORE_FLAG_VALUE == 1
5756 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5757 && op1 == const0_rtx
5758 && mode == GET_MODE (op0)
5759 && nonzero_bits (op0, mode) == 1)
5760 return gen_lowpart (mode,
5761 expand_compound_operation (op0));
5762
5763 else if (STORE_FLAG_VALUE == 1
5764 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5765 && op1 == const0_rtx
5766 && mode == GET_MODE (op0)
5767 && (num_sign_bit_copies (op0, mode)
5768 == GET_MODE_PRECISION (mode)))
5769 {
5770 op0 = expand_compound_operation (op0);
5771 return simplify_gen_unary (NEG, mode,
5772 gen_lowpart (mode, op0),
5773 mode);
5774 }
5775
5776 else if (STORE_FLAG_VALUE == 1
5777 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5778 && op1 == const0_rtx
5779 && mode == GET_MODE (op0)
5780 && nonzero_bits (op0, mode) == 1)
5781 {
5782 op0 = expand_compound_operation (op0);
5783 return simplify_gen_binary (XOR, mode,
5784 gen_lowpart (mode, op0),
5785 const1_rtx);
5786 }
5787
5788 else if (STORE_FLAG_VALUE == 1
5789 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5790 && op1 == const0_rtx
5791 && mode == GET_MODE (op0)
5792 && (num_sign_bit_copies (op0, mode)
5793 == GET_MODE_PRECISION (mode)))
5794 {
5795 op0 = expand_compound_operation (op0);
5796 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5797 }
5798
5799 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5800 those above. */
5801 if (in_cond)
5802 ;
5803
5804 else if (STORE_FLAG_VALUE == -1
5805 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5806 && op1 == const0_rtx
5807 && (num_sign_bit_copies (op0, mode)
5808 == GET_MODE_PRECISION (mode)))
5809 return gen_lowpart (mode,
5810 expand_compound_operation (op0));
5811
5812 else if (STORE_FLAG_VALUE == -1
5813 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5814 && op1 == const0_rtx
5815 && mode == GET_MODE (op0)
5816 && nonzero_bits (op0, mode) == 1)
5817 {
5818 op0 = expand_compound_operation (op0);
5819 return simplify_gen_unary (NEG, mode,
5820 gen_lowpart (mode, op0),
5821 mode);
5822 }
5823
5824 else if (STORE_FLAG_VALUE == -1
5825 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5826 && op1 == const0_rtx
5827 && mode == GET_MODE (op0)
5828 && (num_sign_bit_copies (op0, mode)
5829 == GET_MODE_PRECISION (mode)))
5830 {
5831 op0 = expand_compound_operation (op0);
5832 return simplify_gen_unary (NOT, mode,
5833 gen_lowpart (mode, op0),
5834 mode);
5835 }
5836
5837 /* If X is 0/1, (eq X 0) is X-1. */
5838 else if (STORE_FLAG_VALUE == -1
5839 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5840 && op1 == const0_rtx
5841 && mode == GET_MODE (op0)
5842 && nonzero_bits (op0, mode) == 1)
5843 {
5844 op0 = expand_compound_operation (op0);
5845 return plus_constant (mode, gen_lowpart (mode, op0), -1);
5846 }
5847
5848 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5849 one bit that might be nonzero, we can convert (ne x 0) to
5850 (ashift x c) where C puts the bit in the sign bit. Remove any
5851 AND with STORE_FLAG_VALUE when we are done, since we are only
5852 going to test the sign bit. */
5853 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5854 && HWI_COMPUTABLE_MODE_P (mode)
5855 && val_signbit_p (mode, STORE_FLAG_VALUE)
5856 && op1 == const0_rtx
5857 && mode == GET_MODE (op0)
5858 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5859 {
5860 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5861 expand_compound_operation (op0),
5862 GET_MODE_PRECISION (mode) - 1 - i);
5863 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5864 return XEXP (x, 0);
5865 else
5866 return x;
5867 }
5868
5869 /* If the code changed, return a whole new comparison.
5870 We also need to avoid using SUBST in cases where
5871 simplify_comparison has widened a comparison with a CONST_INT,
5872 since in that case the wider CONST_INT may fail the sanity
5873 checks in do_SUBST. */
5874 if (new_code != code
5875 || (CONST_INT_P (op1)
5876 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
5877 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
5878 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5879
5880 /* Otherwise, keep this operation, but maybe change its operands.
5881 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5882 SUBST (XEXP (x, 0), op0);
5883 SUBST (XEXP (x, 1), op1);
5884 }
5885 break;
5886
5887 case IF_THEN_ELSE:
5888 return simplify_if_then_else (x);
5889
5890 case ZERO_EXTRACT:
5891 case SIGN_EXTRACT:
5892 case ZERO_EXTEND:
5893 case SIGN_EXTEND:
5894 /* If we are processing SET_DEST, we are done. */
5895 if (in_dest)
5896 return x;
5897
5898 return expand_compound_operation (x);
5899
5900 case SET:
5901 return simplify_set (x);
5902
5903 case AND:
5904 case IOR:
5905 return simplify_logical (x);
5906
5907 case ASHIFT:
5908 case LSHIFTRT:
5909 case ASHIFTRT:
5910 case ROTATE:
5911 case ROTATERT:
5912 /* If this is a shift by a constant amount, simplify it. */
5913 if (CONST_INT_P (XEXP (x, 1)))
5914 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5915 INTVAL (XEXP (x, 1)));
5916
5917 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5918 SUBST (XEXP (x, 1),
5919 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5920 ((unsigned HOST_WIDE_INT) 1
5921 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5922 - 1,
5923 0));
5924 break;
5925
5926 default:
5927 break;
5928 }
5929
5930 return x;
5931 }
5932 \f
5933 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5934
5935 static rtx
5936 simplify_if_then_else (rtx x)
5937 {
5938 enum machine_mode mode = GET_MODE (x);
5939 rtx cond = XEXP (x, 0);
5940 rtx true_rtx = XEXP (x, 1);
5941 rtx false_rtx = XEXP (x, 2);
5942 enum rtx_code true_code = GET_CODE (cond);
5943 int comparison_p = COMPARISON_P (cond);
5944 rtx temp;
5945 int i;
5946 enum rtx_code false_code;
5947 rtx reversed;
5948
5949 /* Simplify storing of the truth value. */
5950 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5951 return simplify_gen_relational (true_code, mode, VOIDmode,
5952 XEXP (cond, 0), XEXP (cond, 1));
5953
5954 /* Also when the truth value has to be reversed. */
5955 if (comparison_p
5956 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5957 && (reversed = reversed_comparison (cond, mode)))
5958 return reversed;
5959
5960 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5961 in it is being compared against certain values. Get the true and false
5962 comparisons and see if that says anything about the value of each arm. */
5963
5964 if (comparison_p
5965 && ((false_code = reversed_comparison_code (cond, NULL))
5966 != UNKNOWN)
5967 && REG_P (XEXP (cond, 0)))
5968 {
5969 HOST_WIDE_INT nzb;
5970 rtx from = XEXP (cond, 0);
5971 rtx true_val = XEXP (cond, 1);
5972 rtx false_val = true_val;
5973 int swapped = 0;
5974
5975 /* If FALSE_CODE is EQ, swap the codes and arms. */
5976
5977 if (false_code == EQ)
5978 {
5979 swapped = 1, true_code = EQ, false_code = NE;
5980 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5981 }
5982
5983 /* If we are comparing against zero and the expression being tested has
5984 only a single bit that might be nonzero, that is its value when it is
5985 not equal to zero. Similarly if it is known to be -1 or 0. */
5986
5987 if (true_code == EQ && true_val == const0_rtx
5988 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5989 {
5990 false_code = EQ;
5991 false_val = gen_int_mode (nzb, GET_MODE (from));
5992 }
5993 else if (true_code == EQ && true_val == const0_rtx
5994 && (num_sign_bit_copies (from, GET_MODE (from))
5995 == GET_MODE_PRECISION (GET_MODE (from))))
5996 {
5997 false_code = EQ;
5998 false_val = constm1_rtx;
5999 }
6000
6001 /* Now simplify an arm if we know the value of the register in the
6002 branch and it is used in the arm. Be careful due to the potential
6003 of locally-shared RTL. */
6004
6005 if (reg_mentioned_p (from, true_rtx))
6006 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6007 from, true_val),
6008 pc_rtx, pc_rtx, 0, 0, 0);
6009 if (reg_mentioned_p (from, false_rtx))
6010 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6011 from, false_val),
6012 pc_rtx, pc_rtx, 0, 0, 0);
6013
6014 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6015 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6016
6017 true_rtx = XEXP (x, 1);
6018 false_rtx = XEXP (x, 2);
6019 true_code = GET_CODE (cond);
6020 }
6021
6022 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6023 reversed, do so to avoid needing two sets of patterns for
6024 subtract-and-branch insns. Similarly if we have a constant in the true
6025 arm, the false arm is the same as the first operand of the comparison, or
6026 the false arm is more complicated than the true arm. */
6027
6028 if (comparison_p
6029 && reversed_comparison_code (cond, NULL) != UNKNOWN
6030 && (true_rtx == pc_rtx
6031 || (CONSTANT_P (true_rtx)
6032 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6033 || true_rtx == const0_rtx
6034 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6035 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6036 && !OBJECT_P (false_rtx))
6037 || reg_mentioned_p (true_rtx, false_rtx)
6038 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6039 {
6040 true_code = reversed_comparison_code (cond, NULL);
6041 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6042 SUBST (XEXP (x, 1), false_rtx);
6043 SUBST (XEXP (x, 2), true_rtx);
6044
6045 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6046 cond = XEXP (x, 0);
6047
6048 /* It is possible that the conditional has been simplified out. */
6049 true_code = GET_CODE (cond);
6050 comparison_p = COMPARISON_P (cond);
6051 }
6052
6053 /* If the two arms are identical, we don't need the comparison. */
6054
6055 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6056 return true_rtx;
6057
6058 /* Convert a == b ? b : a to "a". */
6059 if (true_code == EQ && ! side_effects_p (cond)
6060 && !HONOR_NANS (mode)
6061 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6062 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6063 return false_rtx;
6064 else if (true_code == NE && ! side_effects_p (cond)
6065 && !HONOR_NANS (mode)
6066 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6067 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6068 return true_rtx;
6069
6070 /* Look for cases where we have (abs x) or (neg (abs X)). */
6071
6072 if (GET_MODE_CLASS (mode) == MODE_INT
6073 && comparison_p
6074 && XEXP (cond, 1) == const0_rtx
6075 && GET_CODE (false_rtx) == NEG
6076 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6077 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6078 && ! side_effects_p (true_rtx))
6079 switch (true_code)
6080 {
6081 case GT:
6082 case GE:
6083 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6084 case LT:
6085 case LE:
6086 return
6087 simplify_gen_unary (NEG, mode,
6088 simplify_gen_unary (ABS, mode, true_rtx, mode),
6089 mode);
6090 default:
6091 break;
6092 }
6093
6094 /* Look for MIN or MAX. */
6095
6096 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6097 && comparison_p
6098 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6099 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6100 && ! side_effects_p (cond))
6101 switch (true_code)
6102 {
6103 case GE:
6104 case GT:
6105 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6106 case LE:
6107 case LT:
6108 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6109 case GEU:
6110 case GTU:
6111 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6112 case LEU:
6113 case LTU:
6114 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6115 default:
6116 break;
6117 }
6118
6119 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6120 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6121 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6122 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6123 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6124 neither 1 or -1, but it isn't worth checking for. */
6125
6126 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6127 && comparison_p
6128 && GET_MODE_CLASS (mode) == MODE_INT
6129 && ! side_effects_p (x))
6130 {
6131 rtx t = make_compound_operation (true_rtx, SET);
6132 rtx f = make_compound_operation (false_rtx, SET);
6133 rtx cond_op0 = XEXP (cond, 0);
6134 rtx cond_op1 = XEXP (cond, 1);
6135 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6136 enum machine_mode m = mode;
6137 rtx z = 0, c1 = NULL_RTX;
6138
6139 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6140 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6141 || GET_CODE (t) == ASHIFT
6142 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6143 && rtx_equal_p (XEXP (t, 0), f))
6144 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6145
6146 /* If an identity-zero op is commutative, check whether there
6147 would be a match if we swapped the operands. */
6148 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6149 || GET_CODE (t) == XOR)
6150 && rtx_equal_p (XEXP (t, 1), f))
6151 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6152 else if (GET_CODE (t) == SIGN_EXTEND
6153 && (GET_CODE (XEXP (t, 0)) == PLUS
6154 || GET_CODE (XEXP (t, 0)) == MINUS
6155 || GET_CODE (XEXP (t, 0)) == IOR
6156 || GET_CODE (XEXP (t, 0)) == XOR
6157 || GET_CODE (XEXP (t, 0)) == ASHIFT
6158 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6159 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6160 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6161 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6162 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6163 && (num_sign_bit_copies (f, GET_MODE (f))
6164 > (unsigned int)
6165 (GET_MODE_PRECISION (mode)
6166 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6167 {
6168 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6169 extend_op = SIGN_EXTEND;
6170 m = GET_MODE (XEXP (t, 0));
6171 }
6172 else if (GET_CODE (t) == SIGN_EXTEND
6173 && (GET_CODE (XEXP (t, 0)) == PLUS
6174 || GET_CODE (XEXP (t, 0)) == IOR
6175 || GET_CODE (XEXP (t, 0)) == XOR)
6176 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6177 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6178 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6179 && (num_sign_bit_copies (f, GET_MODE (f))
6180 > (unsigned int)
6181 (GET_MODE_PRECISION (mode)
6182 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6183 {
6184 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6185 extend_op = SIGN_EXTEND;
6186 m = GET_MODE (XEXP (t, 0));
6187 }
6188 else if (GET_CODE (t) == ZERO_EXTEND
6189 && (GET_CODE (XEXP (t, 0)) == PLUS
6190 || GET_CODE (XEXP (t, 0)) == MINUS
6191 || GET_CODE (XEXP (t, 0)) == IOR
6192 || GET_CODE (XEXP (t, 0)) == XOR
6193 || GET_CODE (XEXP (t, 0)) == ASHIFT
6194 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6195 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6196 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6197 && HWI_COMPUTABLE_MODE_P (mode)
6198 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6199 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6200 && ((nonzero_bits (f, GET_MODE (f))
6201 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6202 == 0))
6203 {
6204 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6205 extend_op = ZERO_EXTEND;
6206 m = GET_MODE (XEXP (t, 0));
6207 }
6208 else if (GET_CODE (t) == ZERO_EXTEND
6209 && (GET_CODE (XEXP (t, 0)) == PLUS
6210 || GET_CODE (XEXP (t, 0)) == IOR
6211 || GET_CODE (XEXP (t, 0)) == XOR)
6212 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6213 && HWI_COMPUTABLE_MODE_P (mode)
6214 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6215 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6216 && ((nonzero_bits (f, GET_MODE (f))
6217 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6218 == 0))
6219 {
6220 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6221 extend_op = ZERO_EXTEND;
6222 m = GET_MODE (XEXP (t, 0));
6223 }
6224
6225 if (z)
6226 {
6227 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6228 cond_op0, cond_op1),
6229 pc_rtx, pc_rtx, 0, 0, 0);
6230 temp = simplify_gen_binary (MULT, m, temp,
6231 simplify_gen_binary (MULT, m, c1,
6232 const_true_rtx));
6233 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6234 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6235
6236 if (extend_op != UNKNOWN)
6237 temp = simplify_gen_unary (extend_op, mode, temp, m);
6238
6239 return temp;
6240 }
6241 }
6242
6243 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6244 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6245 negation of a single bit, we can convert this operation to a shift. We
6246 can actually do this more generally, but it doesn't seem worth it. */
6247
6248 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6249 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6250 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6251 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6252 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6253 == GET_MODE_PRECISION (mode))
6254 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6255 return
6256 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6257 gen_lowpart (mode, XEXP (cond, 0)), i);
6258
6259 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6260 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6261 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6262 && GET_MODE (XEXP (cond, 0)) == mode
6263 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6264 == nonzero_bits (XEXP (cond, 0), mode)
6265 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6266 return XEXP (cond, 0);
6267
6268 return x;
6269 }
6270 \f
6271 /* Simplify X, a SET expression. Return the new expression. */
6272
6273 static rtx
6274 simplify_set (rtx x)
6275 {
6276 rtx src = SET_SRC (x);
6277 rtx dest = SET_DEST (x);
6278 enum machine_mode mode
6279 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6280 rtx_insn *other_insn;
6281 rtx *cc_use;
6282
6283 /* (set (pc) (return)) gets written as (return). */
6284 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6285 return src;
6286
6287 /* Now that we know for sure which bits of SRC we are using, see if we can
6288 simplify the expression for the object knowing that we only need the
6289 low-order bits. */
6290
6291 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6292 {
6293 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6294 SUBST (SET_SRC (x), src);
6295 }
6296
6297 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6298 the comparison result and try to simplify it unless we already have used
6299 undobuf.other_insn. */
6300 if ((GET_MODE_CLASS (mode) == MODE_CC
6301 || GET_CODE (src) == COMPARE
6302 || CC0_P (dest))
6303 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6304 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6305 && COMPARISON_P (*cc_use)
6306 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6307 {
6308 enum rtx_code old_code = GET_CODE (*cc_use);
6309 enum rtx_code new_code;
6310 rtx op0, op1, tmp;
6311 int other_changed = 0;
6312 rtx inner_compare = NULL_RTX;
6313 enum machine_mode compare_mode = GET_MODE (dest);
6314
6315 if (GET_CODE (src) == COMPARE)
6316 {
6317 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6318 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6319 {
6320 inner_compare = op0;
6321 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6322 }
6323 }
6324 else
6325 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6326
6327 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6328 op0, op1);
6329 if (!tmp)
6330 new_code = old_code;
6331 else if (!CONSTANT_P (tmp))
6332 {
6333 new_code = GET_CODE (tmp);
6334 op0 = XEXP (tmp, 0);
6335 op1 = XEXP (tmp, 1);
6336 }
6337 else
6338 {
6339 rtx pat = PATTERN (other_insn);
6340 undobuf.other_insn = other_insn;
6341 SUBST (*cc_use, tmp);
6342
6343 /* Attempt to simplify CC user. */
6344 if (GET_CODE (pat) == SET)
6345 {
6346 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6347 if (new_rtx != NULL_RTX)
6348 SUBST (SET_SRC (pat), new_rtx);
6349 }
6350
6351 /* Convert X into a no-op move. */
6352 SUBST (SET_DEST (x), pc_rtx);
6353 SUBST (SET_SRC (x), pc_rtx);
6354 return x;
6355 }
6356
6357 /* Simplify our comparison, if possible. */
6358 new_code = simplify_comparison (new_code, &op0, &op1);
6359
6360 #ifdef SELECT_CC_MODE
6361 /* If this machine has CC modes other than CCmode, check to see if we
6362 need to use a different CC mode here. */
6363 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6364 compare_mode = GET_MODE (op0);
6365 else if (inner_compare
6366 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6367 && new_code == old_code
6368 && op0 == XEXP (inner_compare, 0)
6369 && op1 == XEXP (inner_compare, 1))
6370 compare_mode = GET_MODE (inner_compare);
6371 else
6372 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6373
6374 #ifndef HAVE_cc0
6375 /* If the mode changed, we have to change SET_DEST, the mode in the
6376 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6377 a hard register, just build new versions with the proper mode. If it
6378 is a pseudo, we lose unless it is only time we set the pseudo, in
6379 which case we can safely change its mode. */
6380 if (compare_mode != GET_MODE (dest))
6381 {
6382 if (can_change_dest_mode (dest, 0, compare_mode))
6383 {
6384 unsigned int regno = REGNO (dest);
6385 rtx new_dest;
6386
6387 if (regno < FIRST_PSEUDO_REGISTER)
6388 new_dest = gen_rtx_REG (compare_mode, regno);
6389 else
6390 {
6391 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6392 new_dest = regno_reg_rtx[regno];
6393 }
6394
6395 SUBST (SET_DEST (x), new_dest);
6396 SUBST (XEXP (*cc_use, 0), new_dest);
6397 other_changed = 1;
6398
6399 dest = new_dest;
6400 }
6401 }
6402 #endif /* cc0 */
6403 #endif /* SELECT_CC_MODE */
6404
6405 /* If the code changed, we have to build a new comparison in
6406 undobuf.other_insn. */
6407 if (new_code != old_code)
6408 {
6409 int other_changed_previously = other_changed;
6410 unsigned HOST_WIDE_INT mask;
6411 rtx old_cc_use = *cc_use;
6412
6413 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6414 dest, const0_rtx));
6415 other_changed = 1;
6416
6417 /* If the only change we made was to change an EQ into an NE or
6418 vice versa, OP0 has only one bit that might be nonzero, and OP1
6419 is zero, check if changing the user of the condition code will
6420 produce a valid insn. If it won't, we can keep the original code
6421 in that insn by surrounding our operation with an XOR. */
6422
6423 if (((old_code == NE && new_code == EQ)
6424 || (old_code == EQ && new_code == NE))
6425 && ! other_changed_previously && op1 == const0_rtx
6426 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6427 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6428 {
6429 rtx pat = PATTERN (other_insn), note = 0;
6430
6431 if ((recog_for_combine (&pat, other_insn, &note) < 0
6432 && ! check_asm_operands (pat)))
6433 {
6434 *cc_use = old_cc_use;
6435 other_changed = 0;
6436
6437 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6438 gen_int_mode (mask,
6439 GET_MODE (op0)));
6440 }
6441 }
6442 }
6443
6444 if (other_changed)
6445 undobuf.other_insn = other_insn;
6446
6447 /* Otherwise, if we didn't previously have a COMPARE in the
6448 correct mode, we need one. */
6449 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6450 {
6451 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6452 src = SET_SRC (x);
6453 }
6454 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6455 {
6456 SUBST (SET_SRC (x), op0);
6457 src = SET_SRC (x);
6458 }
6459 /* Otherwise, update the COMPARE if needed. */
6460 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6461 {
6462 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6463 src = SET_SRC (x);
6464 }
6465 }
6466 else
6467 {
6468 /* Get SET_SRC in a form where we have placed back any
6469 compound expressions. Then do the checks below. */
6470 src = make_compound_operation (src, SET);
6471 SUBST (SET_SRC (x), src);
6472 }
6473
6474 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6475 and X being a REG or (subreg (reg)), we may be able to convert this to
6476 (set (subreg:m2 x) (op)).
6477
6478 We can always do this if M1 is narrower than M2 because that means that
6479 we only care about the low bits of the result.
6480
6481 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6482 perform a narrower operation than requested since the high-order bits will
6483 be undefined. On machine where it is defined, this transformation is safe
6484 as long as M1 and M2 have the same number of words. */
6485
6486 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6487 && !OBJECT_P (SUBREG_REG (src))
6488 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6489 / UNITS_PER_WORD)
6490 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6491 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6492 #ifndef WORD_REGISTER_OPERATIONS
6493 && (GET_MODE_SIZE (GET_MODE (src))
6494 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6495 #endif
6496 #ifdef CANNOT_CHANGE_MODE_CLASS
6497 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6498 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6499 GET_MODE (SUBREG_REG (src)),
6500 GET_MODE (src)))
6501 #endif
6502 && (REG_P (dest)
6503 || (GET_CODE (dest) == SUBREG
6504 && REG_P (SUBREG_REG (dest)))))
6505 {
6506 SUBST (SET_DEST (x),
6507 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6508 dest));
6509 SUBST (SET_SRC (x), SUBREG_REG (src));
6510
6511 src = SET_SRC (x), dest = SET_DEST (x);
6512 }
6513
6514 #ifdef HAVE_cc0
6515 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6516 in SRC. */
6517 if (dest == cc0_rtx
6518 && GET_CODE (src) == SUBREG
6519 && subreg_lowpart_p (src)
6520 && (GET_MODE_PRECISION (GET_MODE (src))
6521 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6522 {
6523 rtx inner = SUBREG_REG (src);
6524 enum machine_mode inner_mode = GET_MODE (inner);
6525
6526 /* Here we make sure that we don't have a sign bit on. */
6527 if (val_signbit_known_clear_p (GET_MODE (src),
6528 nonzero_bits (inner, inner_mode)))
6529 {
6530 SUBST (SET_SRC (x), inner);
6531 src = SET_SRC (x);
6532 }
6533 }
6534 #endif
6535
6536 #ifdef LOAD_EXTEND_OP
6537 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6538 would require a paradoxical subreg. Replace the subreg with a
6539 zero_extend to avoid the reload that would otherwise be required. */
6540
6541 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6542 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6543 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6544 && SUBREG_BYTE (src) == 0
6545 && paradoxical_subreg_p (src)
6546 && MEM_P (SUBREG_REG (src)))
6547 {
6548 SUBST (SET_SRC (x),
6549 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6550 GET_MODE (src), SUBREG_REG (src)));
6551
6552 src = SET_SRC (x);
6553 }
6554 #endif
6555
6556 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6557 are comparing an item known to be 0 or -1 against 0, use a logical
6558 operation instead. Check for one of the arms being an IOR of the other
6559 arm with some value. We compute three terms to be IOR'ed together. In
6560 practice, at most two will be nonzero. Then we do the IOR's. */
6561
6562 if (GET_CODE (dest) != PC
6563 && GET_CODE (src) == IF_THEN_ELSE
6564 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6565 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6566 && XEXP (XEXP (src, 0), 1) == const0_rtx
6567 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6568 #ifdef HAVE_conditional_move
6569 && ! can_conditionally_move_p (GET_MODE (src))
6570 #endif
6571 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6572 GET_MODE (XEXP (XEXP (src, 0), 0)))
6573 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6574 && ! side_effects_p (src))
6575 {
6576 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6577 ? XEXP (src, 1) : XEXP (src, 2));
6578 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6579 ? XEXP (src, 2) : XEXP (src, 1));
6580 rtx term1 = const0_rtx, term2, term3;
6581
6582 if (GET_CODE (true_rtx) == IOR
6583 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6584 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6585 else if (GET_CODE (true_rtx) == IOR
6586 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6587 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6588 else if (GET_CODE (false_rtx) == IOR
6589 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6590 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6591 else if (GET_CODE (false_rtx) == IOR
6592 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6593 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6594
6595 term2 = simplify_gen_binary (AND, GET_MODE (src),
6596 XEXP (XEXP (src, 0), 0), true_rtx);
6597 term3 = simplify_gen_binary (AND, GET_MODE (src),
6598 simplify_gen_unary (NOT, GET_MODE (src),
6599 XEXP (XEXP (src, 0), 0),
6600 GET_MODE (src)),
6601 false_rtx);
6602
6603 SUBST (SET_SRC (x),
6604 simplify_gen_binary (IOR, GET_MODE (src),
6605 simplify_gen_binary (IOR, GET_MODE (src),
6606 term1, term2),
6607 term3));
6608
6609 src = SET_SRC (x);
6610 }
6611
6612 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6613 whole thing fail. */
6614 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6615 return src;
6616 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6617 return dest;
6618 else
6619 /* Convert this into a field assignment operation, if possible. */
6620 return make_field_assignment (x);
6621 }
6622 \f
6623 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6624 result. */
6625
6626 static rtx
6627 simplify_logical (rtx x)
6628 {
6629 enum machine_mode mode = GET_MODE (x);
6630 rtx op0 = XEXP (x, 0);
6631 rtx op1 = XEXP (x, 1);
6632
6633 switch (GET_CODE (x))
6634 {
6635 case AND:
6636 /* We can call simplify_and_const_int only if we don't lose
6637 any (sign) bits when converting INTVAL (op1) to
6638 "unsigned HOST_WIDE_INT". */
6639 if (CONST_INT_P (op1)
6640 && (HWI_COMPUTABLE_MODE_P (mode)
6641 || INTVAL (op1) > 0))
6642 {
6643 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6644 if (GET_CODE (x) != AND)
6645 return x;
6646
6647 op0 = XEXP (x, 0);
6648 op1 = XEXP (x, 1);
6649 }
6650
6651 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6652 apply the distributive law and then the inverse distributive
6653 law to see if things simplify. */
6654 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6655 {
6656 rtx result = distribute_and_simplify_rtx (x, 0);
6657 if (result)
6658 return result;
6659 }
6660 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6661 {
6662 rtx result = distribute_and_simplify_rtx (x, 1);
6663 if (result)
6664 return result;
6665 }
6666 break;
6667
6668 case IOR:
6669 /* If we have (ior (and A B) C), apply the distributive law and then
6670 the inverse distributive law to see if things simplify. */
6671
6672 if (GET_CODE (op0) == AND)
6673 {
6674 rtx result = distribute_and_simplify_rtx (x, 0);
6675 if (result)
6676 return result;
6677 }
6678
6679 if (GET_CODE (op1) == AND)
6680 {
6681 rtx result = distribute_and_simplify_rtx (x, 1);
6682 if (result)
6683 return result;
6684 }
6685 break;
6686
6687 default:
6688 gcc_unreachable ();
6689 }
6690
6691 return x;
6692 }
6693 \f
6694 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6695 operations" because they can be replaced with two more basic operations.
6696 ZERO_EXTEND is also considered "compound" because it can be replaced with
6697 an AND operation, which is simpler, though only one operation.
6698
6699 The function expand_compound_operation is called with an rtx expression
6700 and will convert it to the appropriate shifts and AND operations,
6701 simplifying at each stage.
6702
6703 The function make_compound_operation is called to convert an expression
6704 consisting of shifts and ANDs into the equivalent compound expression.
6705 It is the inverse of this function, loosely speaking. */
6706
6707 static rtx
6708 expand_compound_operation (rtx x)
6709 {
6710 unsigned HOST_WIDE_INT pos = 0, len;
6711 int unsignedp = 0;
6712 unsigned int modewidth;
6713 rtx tem;
6714
6715 switch (GET_CODE (x))
6716 {
6717 case ZERO_EXTEND:
6718 unsignedp = 1;
6719 case SIGN_EXTEND:
6720 /* We can't necessarily use a const_int for a multiword mode;
6721 it depends on implicitly extending the value.
6722 Since we don't know the right way to extend it,
6723 we can't tell whether the implicit way is right.
6724
6725 Even for a mode that is no wider than a const_int,
6726 we can't win, because we need to sign extend one of its bits through
6727 the rest of it, and we don't know which bit. */
6728 if (CONST_INT_P (XEXP (x, 0)))
6729 return x;
6730
6731 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6732 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6733 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6734 reloaded. If not for that, MEM's would very rarely be safe.
6735
6736 Reject MODEs bigger than a word, because we might not be able
6737 to reference a two-register group starting with an arbitrary register
6738 (and currently gen_lowpart might crash for a SUBREG). */
6739
6740 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6741 return x;
6742
6743 /* Reject MODEs that aren't scalar integers because turning vector
6744 or complex modes into shifts causes problems. */
6745
6746 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6747 return x;
6748
6749 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6750 /* If the inner object has VOIDmode (the only way this can happen
6751 is if it is an ASM_OPERANDS), we can't do anything since we don't
6752 know how much masking to do. */
6753 if (len == 0)
6754 return x;
6755
6756 break;
6757
6758 case ZERO_EXTRACT:
6759 unsignedp = 1;
6760
6761 /* ... fall through ... */
6762
6763 case SIGN_EXTRACT:
6764 /* If the operand is a CLOBBER, just return it. */
6765 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6766 return XEXP (x, 0);
6767
6768 if (!CONST_INT_P (XEXP (x, 1))
6769 || !CONST_INT_P (XEXP (x, 2))
6770 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6771 return x;
6772
6773 /* Reject MODEs that aren't scalar integers because turning vector
6774 or complex modes into shifts causes problems. */
6775
6776 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6777 return x;
6778
6779 len = INTVAL (XEXP (x, 1));
6780 pos = INTVAL (XEXP (x, 2));
6781
6782 /* This should stay within the object being extracted, fail otherwise. */
6783 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6784 return x;
6785
6786 if (BITS_BIG_ENDIAN)
6787 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6788
6789 break;
6790
6791 default:
6792 return x;
6793 }
6794 /* Convert sign extension to zero extension, if we know that the high
6795 bit is not set, as this is easier to optimize. It will be converted
6796 back to cheaper alternative in make_extraction. */
6797 if (GET_CODE (x) == SIGN_EXTEND
6798 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6799 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6800 & ~(((unsigned HOST_WIDE_INT)
6801 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6802 >> 1))
6803 == 0)))
6804 {
6805 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6806 rtx temp2 = expand_compound_operation (temp);
6807
6808 /* Make sure this is a profitable operation. */
6809 if (set_src_cost (x, optimize_this_for_speed_p)
6810 > set_src_cost (temp2, optimize_this_for_speed_p))
6811 return temp2;
6812 else if (set_src_cost (x, optimize_this_for_speed_p)
6813 > set_src_cost (temp, optimize_this_for_speed_p))
6814 return temp;
6815 else
6816 return x;
6817 }
6818
6819 /* We can optimize some special cases of ZERO_EXTEND. */
6820 if (GET_CODE (x) == ZERO_EXTEND)
6821 {
6822 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6823 know that the last value didn't have any inappropriate bits
6824 set. */
6825 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6826 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6827 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6828 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6829 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6830 return XEXP (XEXP (x, 0), 0);
6831
6832 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6833 if (GET_CODE (XEXP (x, 0)) == SUBREG
6834 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6835 && subreg_lowpart_p (XEXP (x, 0))
6836 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6837 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6838 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6839 return SUBREG_REG (XEXP (x, 0));
6840
6841 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6842 is a comparison and STORE_FLAG_VALUE permits. This is like
6843 the first case, but it works even when GET_MODE (x) is larger
6844 than HOST_WIDE_INT. */
6845 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6846 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6847 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6848 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6849 <= HOST_BITS_PER_WIDE_INT)
6850 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6851 return XEXP (XEXP (x, 0), 0);
6852
6853 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6854 if (GET_CODE (XEXP (x, 0)) == SUBREG
6855 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6856 && subreg_lowpart_p (XEXP (x, 0))
6857 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6858 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
6859 <= HOST_BITS_PER_WIDE_INT)
6860 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6861 return SUBREG_REG (XEXP (x, 0));
6862
6863 }
6864
6865 /* If we reach here, we want to return a pair of shifts. The inner
6866 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6867 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6868 logical depending on the value of UNSIGNEDP.
6869
6870 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6871 converted into an AND of a shift.
6872
6873 We must check for the case where the left shift would have a negative
6874 count. This can happen in a case like (x >> 31) & 255 on machines
6875 that can't shift by a constant. On those machines, we would first
6876 combine the shift with the AND to produce a variable-position
6877 extraction. Then the constant of 31 would be substituted in
6878 to produce such a position. */
6879
6880 modewidth = GET_MODE_PRECISION (GET_MODE (x));
6881 if (modewidth >= pos + len)
6882 {
6883 enum machine_mode mode = GET_MODE (x);
6884 tem = gen_lowpart (mode, XEXP (x, 0));
6885 if (!tem || GET_CODE (tem) == CLOBBER)
6886 return x;
6887 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6888 tem, modewidth - pos - len);
6889 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6890 mode, tem, modewidth - len);
6891 }
6892 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6893 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6894 simplify_shift_const (NULL_RTX, LSHIFTRT,
6895 GET_MODE (x),
6896 XEXP (x, 0), pos),
6897 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
6898 else
6899 /* Any other cases we can't handle. */
6900 return x;
6901
6902 /* If we couldn't do this for some reason, return the original
6903 expression. */
6904 if (GET_CODE (tem) == CLOBBER)
6905 return x;
6906
6907 return tem;
6908 }
6909 \f
6910 /* X is a SET which contains an assignment of one object into
6911 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6912 or certain SUBREGS). If possible, convert it into a series of
6913 logical operations.
6914
6915 We half-heartedly support variable positions, but do not at all
6916 support variable lengths. */
6917
6918 static const_rtx
6919 expand_field_assignment (const_rtx x)
6920 {
6921 rtx inner;
6922 rtx pos; /* Always counts from low bit. */
6923 int len;
6924 rtx mask, cleared, masked;
6925 enum machine_mode compute_mode;
6926
6927 /* Loop until we find something we can't simplify. */
6928 while (1)
6929 {
6930 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6931 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6932 {
6933 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6934 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
6935 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6936 }
6937 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6938 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
6939 {
6940 inner = XEXP (SET_DEST (x), 0);
6941 len = INTVAL (XEXP (SET_DEST (x), 1));
6942 pos = XEXP (SET_DEST (x), 2);
6943
6944 /* A constant position should stay within the width of INNER. */
6945 if (CONST_INT_P (pos)
6946 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
6947 break;
6948
6949 if (BITS_BIG_ENDIAN)
6950 {
6951 if (CONST_INT_P (pos))
6952 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
6953 - INTVAL (pos));
6954 else if (GET_CODE (pos) == MINUS
6955 && CONST_INT_P (XEXP (pos, 1))
6956 && (INTVAL (XEXP (pos, 1))
6957 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
6958 /* If position is ADJUST - X, new position is X. */
6959 pos = XEXP (pos, 0);
6960 else
6961 {
6962 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
6963 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6964 gen_int_mode (prec - len,
6965 GET_MODE (pos)),
6966 pos);
6967 }
6968 }
6969 }
6970
6971 /* A SUBREG between two modes that occupy the same numbers of words
6972 can be done by moving the SUBREG to the source. */
6973 else if (GET_CODE (SET_DEST (x)) == SUBREG
6974 /* We need SUBREGs to compute nonzero_bits properly. */
6975 && nonzero_sign_valid
6976 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6977 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6978 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6979 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6980 {
6981 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6982 gen_lowpart
6983 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6984 SET_SRC (x)));
6985 continue;
6986 }
6987 else
6988 break;
6989
6990 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6991 inner = SUBREG_REG (inner);
6992
6993 compute_mode = GET_MODE (inner);
6994
6995 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6996 if (! SCALAR_INT_MODE_P (compute_mode))
6997 {
6998 enum machine_mode imode;
6999
7000 /* Don't do anything for vector or complex integral types. */
7001 if (! FLOAT_MODE_P (compute_mode))
7002 break;
7003
7004 /* Try to find an integral mode to pun with. */
7005 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7006 if (imode == BLKmode)
7007 break;
7008
7009 compute_mode = imode;
7010 inner = gen_lowpart (imode, inner);
7011 }
7012
7013 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7014 if (len >= HOST_BITS_PER_WIDE_INT)
7015 break;
7016
7017 /* Now compute the equivalent expression. Make a copy of INNER
7018 for the SET_DEST in case it is a MEM into which we will substitute;
7019 we don't want shared RTL in that case. */
7020 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7021 compute_mode);
7022 cleared = simplify_gen_binary (AND, compute_mode,
7023 simplify_gen_unary (NOT, compute_mode,
7024 simplify_gen_binary (ASHIFT,
7025 compute_mode,
7026 mask, pos),
7027 compute_mode),
7028 inner);
7029 masked = simplify_gen_binary (ASHIFT, compute_mode,
7030 simplify_gen_binary (
7031 AND, compute_mode,
7032 gen_lowpart (compute_mode, SET_SRC (x)),
7033 mask),
7034 pos);
7035
7036 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7037 simplify_gen_binary (IOR, compute_mode,
7038 cleared, masked));
7039 }
7040
7041 return x;
7042 }
7043 \f
7044 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7045 it is an RTX that represents the (variable) starting position; otherwise,
7046 POS is the (constant) starting bit position. Both are counted from the LSB.
7047
7048 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7049
7050 IN_DEST is nonzero if this is a reference in the destination of a SET.
7051 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7052 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7053 be used.
7054
7055 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7056 ZERO_EXTRACT should be built even for bits starting at bit 0.
7057
7058 MODE is the desired mode of the result (if IN_DEST == 0).
7059
7060 The result is an RTX for the extraction or NULL_RTX if the target
7061 can't handle it. */
7062
7063 static rtx
7064 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7065 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7066 int in_dest, int in_compare)
7067 {
7068 /* This mode describes the size of the storage area
7069 to fetch the overall value from. Within that, we
7070 ignore the POS lowest bits, etc. */
7071 enum machine_mode is_mode = GET_MODE (inner);
7072 enum machine_mode inner_mode;
7073 enum machine_mode wanted_inner_mode;
7074 enum machine_mode wanted_inner_reg_mode = word_mode;
7075 enum machine_mode pos_mode = word_mode;
7076 enum machine_mode extraction_mode = word_mode;
7077 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7078 rtx new_rtx = 0;
7079 rtx orig_pos_rtx = pos_rtx;
7080 HOST_WIDE_INT orig_pos;
7081
7082 if (pos_rtx && CONST_INT_P (pos_rtx))
7083 pos = INTVAL (pos_rtx), pos_rtx = 0;
7084
7085 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7086 {
7087 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7088 consider just the QI as the memory to extract from.
7089 The subreg adds or removes high bits; its mode is
7090 irrelevant to the meaning of this extraction,
7091 since POS and LEN count from the lsb. */
7092 if (MEM_P (SUBREG_REG (inner)))
7093 is_mode = GET_MODE (SUBREG_REG (inner));
7094 inner = SUBREG_REG (inner);
7095 }
7096 else if (GET_CODE (inner) == ASHIFT
7097 && CONST_INT_P (XEXP (inner, 1))
7098 && pos_rtx == 0 && pos == 0
7099 && len > UINTVAL (XEXP (inner, 1)))
7100 {
7101 /* We're extracting the least significant bits of an rtx
7102 (ashift X (const_int C)), where LEN > C. Extract the
7103 least significant (LEN - C) bits of X, giving an rtx
7104 whose mode is MODE, then shift it left C times. */
7105 new_rtx = make_extraction (mode, XEXP (inner, 0),
7106 0, 0, len - INTVAL (XEXP (inner, 1)),
7107 unsignedp, in_dest, in_compare);
7108 if (new_rtx != 0)
7109 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7110 }
7111 else if (GET_CODE (inner) == TRUNCATE)
7112 inner = XEXP (inner, 0);
7113
7114 inner_mode = GET_MODE (inner);
7115
7116 /* See if this can be done without an extraction. We never can if the
7117 width of the field is not the same as that of some integer mode. For
7118 registers, we can only avoid the extraction if the position is at the
7119 low-order bit and this is either not in the destination or we have the
7120 appropriate STRICT_LOW_PART operation available.
7121
7122 For MEM, we can avoid an extract if the field starts on an appropriate
7123 boundary and we can change the mode of the memory reference. */
7124
7125 if (tmode != BLKmode
7126 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7127 && !MEM_P (inner)
7128 && (inner_mode == tmode
7129 || !REG_P (inner)
7130 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7131 || reg_truncated_to_mode (tmode, inner))
7132 && (! in_dest
7133 || (REG_P (inner)
7134 && have_insn_for (STRICT_LOW_PART, tmode))))
7135 || (MEM_P (inner) && pos_rtx == 0
7136 && (pos
7137 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7138 : BITS_PER_UNIT)) == 0
7139 /* We can't do this if we are widening INNER_MODE (it
7140 may not be aligned, for one thing). */
7141 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7142 && (inner_mode == tmode
7143 || (! mode_dependent_address_p (XEXP (inner, 0),
7144 MEM_ADDR_SPACE (inner))
7145 && ! MEM_VOLATILE_P (inner))))))
7146 {
7147 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7148 field. If the original and current mode are the same, we need not
7149 adjust the offset. Otherwise, we do if bytes big endian.
7150
7151 If INNER is not a MEM, get a piece consisting of just the field
7152 of interest (in this case POS % BITS_PER_WORD must be 0). */
7153
7154 if (MEM_P (inner))
7155 {
7156 HOST_WIDE_INT offset;
7157
7158 /* POS counts from lsb, but make OFFSET count in memory order. */
7159 if (BYTES_BIG_ENDIAN)
7160 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7161 else
7162 offset = pos / BITS_PER_UNIT;
7163
7164 new_rtx = adjust_address_nv (inner, tmode, offset);
7165 }
7166 else if (REG_P (inner))
7167 {
7168 if (tmode != inner_mode)
7169 {
7170 /* We can't call gen_lowpart in a DEST since we
7171 always want a SUBREG (see below) and it would sometimes
7172 return a new hard register. */
7173 if (pos || in_dest)
7174 {
7175 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7176
7177 if (WORDS_BIG_ENDIAN
7178 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7179 final_word = ((GET_MODE_SIZE (inner_mode)
7180 - GET_MODE_SIZE (tmode))
7181 / UNITS_PER_WORD) - final_word;
7182
7183 final_word *= UNITS_PER_WORD;
7184 if (BYTES_BIG_ENDIAN &&
7185 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7186 final_word += (GET_MODE_SIZE (inner_mode)
7187 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7188
7189 /* Avoid creating invalid subregs, for example when
7190 simplifying (x>>32)&255. */
7191 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7192 return NULL_RTX;
7193
7194 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7195 }
7196 else
7197 new_rtx = gen_lowpart (tmode, inner);
7198 }
7199 else
7200 new_rtx = inner;
7201 }
7202 else
7203 new_rtx = force_to_mode (inner, tmode,
7204 len >= HOST_BITS_PER_WIDE_INT
7205 ? ~(unsigned HOST_WIDE_INT) 0
7206 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7207 0);
7208
7209 /* If this extraction is going into the destination of a SET,
7210 make a STRICT_LOW_PART unless we made a MEM. */
7211
7212 if (in_dest)
7213 return (MEM_P (new_rtx) ? new_rtx
7214 : (GET_CODE (new_rtx) != SUBREG
7215 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7216 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7217
7218 if (mode == tmode)
7219 return new_rtx;
7220
7221 if (CONST_SCALAR_INT_P (new_rtx))
7222 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7223 mode, new_rtx, tmode);
7224
7225 /* If we know that no extraneous bits are set, and that the high
7226 bit is not set, convert the extraction to the cheaper of
7227 sign and zero extension, that are equivalent in these cases. */
7228 if (flag_expensive_optimizations
7229 && (HWI_COMPUTABLE_MODE_P (tmode)
7230 && ((nonzero_bits (new_rtx, tmode)
7231 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7232 == 0)))
7233 {
7234 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7235 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7236
7237 /* Prefer ZERO_EXTENSION, since it gives more information to
7238 backends. */
7239 if (set_src_cost (temp, optimize_this_for_speed_p)
7240 <= set_src_cost (temp1, optimize_this_for_speed_p))
7241 return temp;
7242 return temp1;
7243 }
7244
7245 /* Otherwise, sign- or zero-extend unless we already are in the
7246 proper mode. */
7247
7248 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7249 mode, new_rtx));
7250 }
7251
7252 /* Unless this is a COMPARE or we have a funny memory reference,
7253 don't do anything with zero-extending field extracts starting at
7254 the low-order bit since they are simple AND operations. */
7255 if (pos_rtx == 0 && pos == 0 && ! in_dest
7256 && ! in_compare && unsignedp)
7257 return 0;
7258
7259 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7260 if the position is not a constant and the length is not 1. In all
7261 other cases, we would only be going outside our object in cases when
7262 an original shift would have been undefined. */
7263 if (MEM_P (inner)
7264 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7265 || (pos_rtx != 0 && len != 1)))
7266 return 0;
7267
7268 enum extraction_pattern pattern = (in_dest ? EP_insv
7269 : unsignedp ? EP_extzv : EP_extv);
7270
7271 /* If INNER is not from memory, we want it to have the mode of a register
7272 extraction pattern's structure operand, or word_mode if there is no
7273 such pattern. The same applies to extraction_mode and pos_mode
7274 and their respective operands.
7275
7276 For memory, assume that the desired extraction_mode and pos_mode
7277 are the same as for a register operation, since at present we don't
7278 have named patterns for aligned memory structures. */
7279 struct extraction_insn insn;
7280 if (get_best_reg_extraction_insn (&insn, pattern,
7281 GET_MODE_BITSIZE (inner_mode), mode))
7282 {
7283 wanted_inner_reg_mode = insn.struct_mode;
7284 pos_mode = insn.pos_mode;
7285 extraction_mode = insn.field_mode;
7286 }
7287
7288 /* Never narrow an object, since that might not be safe. */
7289
7290 if (mode != VOIDmode
7291 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7292 extraction_mode = mode;
7293
7294 if (!MEM_P (inner))
7295 wanted_inner_mode = wanted_inner_reg_mode;
7296 else
7297 {
7298 /* Be careful not to go beyond the extracted object and maintain the
7299 natural alignment of the memory. */
7300 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7301 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7302 > GET_MODE_BITSIZE (wanted_inner_mode))
7303 {
7304 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7305 gcc_assert (wanted_inner_mode != VOIDmode);
7306 }
7307 }
7308
7309 orig_pos = pos;
7310
7311 if (BITS_BIG_ENDIAN)
7312 {
7313 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7314 BITS_BIG_ENDIAN style. If position is constant, compute new
7315 position. Otherwise, build subtraction.
7316 Note that POS is relative to the mode of the original argument.
7317 If it's a MEM we need to recompute POS relative to that.
7318 However, if we're extracting from (or inserting into) a register,
7319 we want to recompute POS relative to wanted_inner_mode. */
7320 int width = (MEM_P (inner)
7321 ? GET_MODE_BITSIZE (is_mode)
7322 : GET_MODE_BITSIZE (wanted_inner_mode));
7323
7324 if (pos_rtx == 0)
7325 pos = width - len - pos;
7326 else
7327 pos_rtx
7328 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7329 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7330 pos_rtx);
7331 /* POS may be less than 0 now, but we check for that below.
7332 Note that it can only be less than 0 if !MEM_P (inner). */
7333 }
7334
7335 /* If INNER has a wider mode, and this is a constant extraction, try to
7336 make it smaller and adjust the byte to point to the byte containing
7337 the value. */
7338 if (wanted_inner_mode != VOIDmode
7339 && inner_mode != wanted_inner_mode
7340 && ! pos_rtx
7341 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7342 && MEM_P (inner)
7343 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7344 && ! MEM_VOLATILE_P (inner))
7345 {
7346 int offset = 0;
7347
7348 /* The computations below will be correct if the machine is big
7349 endian in both bits and bytes or little endian in bits and bytes.
7350 If it is mixed, we must adjust. */
7351
7352 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7353 adjust OFFSET to compensate. */
7354 if (BYTES_BIG_ENDIAN
7355 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7356 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7357
7358 /* We can now move to the desired byte. */
7359 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7360 * GET_MODE_SIZE (wanted_inner_mode);
7361 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7362
7363 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7364 && is_mode != wanted_inner_mode)
7365 offset = (GET_MODE_SIZE (is_mode)
7366 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7367
7368 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7369 }
7370
7371 /* If INNER is not memory, get it into the proper mode. If we are changing
7372 its mode, POS must be a constant and smaller than the size of the new
7373 mode. */
7374 else if (!MEM_P (inner))
7375 {
7376 /* On the LHS, don't create paradoxical subregs implicitely truncating
7377 the register unless TRULY_NOOP_TRUNCATION. */
7378 if (in_dest
7379 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7380 wanted_inner_mode))
7381 return NULL_RTX;
7382
7383 if (GET_MODE (inner) != wanted_inner_mode
7384 && (pos_rtx != 0
7385 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7386 return NULL_RTX;
7387
7388 if (orig_pos < 0)
7389 return NULL_RTX;
7390
7391 inner = force_to_mode (inner, wanted_inner_mode,
7392 pos_rtx
7393 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7394 ? ~(unsigned HOST_WIDE_INT) 0
7395 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7396 << orig_pos),
7397 0);
7398 }
7399
7400 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7401 have to zero extend. Otherwise, we can just use a SUBREG. */
7402 if (pos_rtx != 0
7403 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7404 {
7405 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7406 GET_MODE (pos_rtx));
7407
7408 /* If we know that no extraneous bits are set, and that the high
7409 bit is not set, convert extraction to cheaper one - either
7410 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7411 cases. */
7412 if (flag_expensive_optimizations
7413 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7414 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7415 & ~(((unsigned HOST_WIDE_INT)
7416 GET_MODE_MASK (GET_MODE (pos_rtx)))
7417 >> 1))
7418 == 0)))
7419 {
7420 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7421 GET_MODE (pos_rtx));
7422
7423 /* Prefer ZERO_EXTENSION, since it gives more information to
7424 backends. */
7425 if (set_src_cost (temp1, optimize_this_for_speed_p)
7426 < set_src_cost (temp, optimize_this_for_speed_p))
7427 temp = temp1;
7428 }
7429 pos_rtx = temp;
7430 }
7431
7432 /* Make POS_RTX unless we already have it and it is correct. If we don't
7433 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7434 be a CONST_INT. */
7435 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7436 pos_rtx = orig_pos_rtx;
7437
7438 else if (pos_rtx == 0)
7439 pos_rtx = GEN_INT (pos);
7440
7441 /* Make the required operation. See if we can use existing rtx. */
7442 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7443 extraction_mode, inner, GEN_INT (len), pos_rtx);
7444 if (! in_dest)
7445 new_rtx = gen_lowpart (mode, new_rtx);
7446
7447 return new_rtx;
7448 }
7449 \f
7450 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7451 with any other operations in X. Return X without that shift if so. */
7452
7453 static rtx
7454 extract_left_shift (rtx x, int count)
7455 {
7456 enum rtx_code code = GET_CODE (x);
7457 enum machine_mode mode = GET_MODE (x);
7458 rtx tem;
7459
7460 switch (code)
7461 {
7462 case ASHIFT:
7463 /* This is the shift itself. If it is wide enough, we will return
7464 either the value being shifted if the shift count is equal to
7465 COUNT or a shift for the difference. */
7466 if (CONST_INT_P (XEXP (x, 1))
7467 && INTVAL (XEXP (x, 1)) >= count)
7468 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7469 INTVAL (XEXP (x, 1)) - count);
7470 break;
7471
7472 case NEG: case NOT:
7473 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7474 return simplify_gen_unary (code, mode, tem, mode);
7475
7476 break;
7477
7478 case PLUS: case IOR: case XOR: case AND:
7479 /* If we can safely shift this constant and we find the inner shift,
7480 make a new operation. */
7481 if (CONST_INT_P (XEXP (x, 1))
7482 && (UINTVAL (XEXP (x, 1))
7483 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7484 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7485 {
7486 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7487 return simplify_gen_binary (code, mode, tem,
7488 gen_int_mode (val, mode));
7489 }
7490 break;
7491
7492 default:
7493 break;
7494 }
7495
7496 return 0;
7497 }
7498 \f
7499 /* Look at the expression rooted at X. Look for expressions
7500 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7501 Form these expressions.
7502
7503 Return the new rtx, usually just X.
7504
7505 Also, for machines like the VAX that don't have logical shift insns,
7506 try to convert logical to arithmetic shift operations in cases where
7507 they are equivalent. This undoes the canonicalizations to logical
7508 shifts done elsewhere.
7509
7510 We try, as much as possible, to re-use rtl expressions to save memory.
7511
7512 IN_CODE says what kind of expression we are processing. Normally, it is
7513 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7514 being kludges), it is MEM. When processing the arguments of a comparison
7515 or a COMPARE against zero, it is COMPARE. */
7516
7517 rtx
7518 make_compound_operation (rtx x, enum rtx_code in_code)
7519 {
7520 enum rtx_code code = GET_CODE (x);
7521 enum machine_mode mode = GET_MODE (x);
7522 int mode_width = GET_MODE_PRECISION (mode);
7523 rtx rhs, lhs;
7524 enum rtx_code next_code;
7525 int i, j;
7526 rtx new_rtx = 0;
7527 rtx tem;
7528 const char *fmt;
7529
7530 /* Select the code to be used in recursive calls. Once we are inside an
7531 address, we stay there. If we have a comparison, set to COMPARE,
7532 but once inside, go back to our default of SET. */
7533
7534 next_code = (code == MEM ? MEM
7535 : ((code == PLUS || code == MINUS)
7536 && SCALAR_INT_MODE_P (mode)) ? MEM
7537 : ((code == COMPARE || COMPARISON_P (x))
7538 && XEXP (x, 1) == const0_rtx) ? COMPARE
7539 : in_code == COMPARE ? SET : in_code);
7540
7541 /* Process depending on the code of this operation. If NEW is set
7542 nonzero, it will be returned. */
7543
7544 switch (code)
7545 {
7546 case ASHIFT:
7547 /* Convert shifts by constants into multiplications if inside
7548 an address. */
7549 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7550 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7551 && INTVAL (XEXP (x, 1)) >= 0
7552 && SCALAR_INT_MODE_P (mode))
7553 {
7554 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7555 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7556
7557 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7558 if (GET_CODE (new_rtx) == NEG)
7559 {
7560 new_rtx = XEXP (new_rtx, 0);
7561 multval = -multval;
7562 }
7563 multval = trunc_int_for_mode (multval, mode);
7564 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7565 }
7566 break;
7567
7568 case PLUS:
7569 lhs = XEXP (x, 0);
7570 rhs = XEXP (x, 1);
7571 lhs = make_compound_operation (lhs, next_code);
7572 rhs = make_compound_operation (rhs, next_code);
7573 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7574 && SCALAR_INT_MODE_P (mode))
7575 {
7576 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7577 XEXP (lhs, 1));
7578 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7579 }
7580 else if (GET_CODE (lhs) == MULT
7581 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7582 {
7583 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7584 simplify_gen_unary (NEG, mode,
7585 XEXP (lhs, 1),
7586 mode));
7587 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7588 }
7589 else
7590 {
7591 SUBST (XEXP (x, 0), lhs);
7592 SUBST (XEXP (x, 1), rhs);
7593 goto maybe_swap;
7594 }
7595 x = gen_lowpart (mode, new_rtx);
7596 goto maybe_swap;
7597
7598 case MINUS:
7599 lhs = XEXP (x, 0);
7600 rhs = XEXP (x, 1);
7601 lhs = make_compound_operation (lhs, next_code);
7602 rhs = make_compound_operation (rhs, next_code);
7603 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7604 && SCALAR_INT_MODE_P (mode))
7605 {
7606 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7607 XEXP (rhs, 1));
7608 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7609 }
7610 else if (GET_CODE (rhs) == MULT
7611 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7612 {
7613 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7614 simplify_gen_unary (NEG, mode,
7615 XEXP (rhs, 1),
7616 mode));
7617 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7618 }
7619 else
7620 {
7621 SUBST (XEXP (x, 0), lhs);
7622 SUBST (XEXP (x, 1), rhs);
7623 return x;
7624 }
7625 return gen_lowpart (mode, new_rtx);
7626
7627 case AND:
7628 /* If the second operand is not a constant, we can't do anything
7629 with it. */
7630 if (!CONST_INT_P (XEXP (x, 1)))
7631 break;
7632
7633 /* If the constant is a power of two minus one and the first operand
7634 is a logical right shift, make an extraction. */
7635 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7636 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7637 {
7638 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7639 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7640 0, in_code == COMPARE);
7641 }
7642
7643 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7644 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7645 && subreg_lowpart_p (XEXP (x, 0))
7646 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7647 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7648 {
7649 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7650 next_code);
7651 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7652 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7653 0, in_code == COMPARE);
7654 }
7655 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7656 else if ((GET_CODE (XEXP (x, 0)) == XOR
7657 || GET_CODE (XEXP (x, 0)) == IOR)
7658 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7659 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7660 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7661 {
7662 /* Apply the distributive law, and then try to make extractions. */
7663 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7664 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7665 XEXP (x, 1)),
7666 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7667 XEXP (x, 1)));
7668 new_rtx = make_compound_operation (new_rtx, in_code);
7669 }
7670
7671 /* If we are have (and (rotate X C) M) and C is larger than the number
7672 of bits in M, this is an extraction. */
7673
7674 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7675 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7676 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7677 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7678 {
7679 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7680 new_rtx = make_extraction (mode, new_rtx,
7681 (GET_MODE_PRECISION (mode)
7682 - INTVAL (XEXP (XEXP (x, 0), 1))),
7683 NULL_RTX, i, 1, 0, in_code == COMPARE);
7684 }
7685
7686 /* On machines without logical shifts, if the operand of the AND is
7687 a logical shift and our mask turns off all the propagated sign
7688 bits, we can replace the logical shift with an arithmetic shift. */
7689 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7690 && !have_insn_for (LSHIFTRT, mode)
7691 && have_insn_for (ASHIFTRT, mode)
7692 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7693 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7694 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7695 && mode_width <= HOST_BITS_PER_WIDE_INT)
7696 {
7697 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7698
7699 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7700 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7701 SUBST (XEXP (x, 0),
7702 gen_rtx_ASHIFTRT (mode,
7703 make_compound_operation
7704 (XEXP (XEXP (x, 0), 0), next_code),
7705 XEXP (XEXP (x, 0), 1)));
7706 }
7707
7708 /* If the constant is one less than a power of two, this might be
7709 representable by an extraction even if no shift is present.
7710 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7711 we are in a COMPARE. */
7712 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7713 new_rtx = make_extraction (mode,
7714 make_compound_operation (XEXP (x, 0),
7715 next_code),
7716 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7717
7718 /* If we are in a comparison and this is an AND with a power of two,
7719 convert this into the appropriate bit extract. */
7720 else if (in_code == COMPARE
7721 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7722 new_rtx = make_extraction (mode,
7723 make_compound_operation (XEXP (x, 0),
7724 next_code),
7725 i, NULL_RTX, 1, 1, 0, 1);
7726
7727 break;
7728
7729 case LSHIFTRT:
7730 /* If the sign bit is known to be zero, replace this with an
7731 arithmetic shift. */
7732 if (have_insn_for (ASHIFTRT, mode)
7733 && ! have_insn_for (LSHIFTRT, mode)
7734 && mode_width <= HOST_BITS_PER_WIDE_INT
7735 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7736 {
7737 new_rtx = gen_rtx_ASHIFTRT (mode,
7738 make_compound_operation (XEXP (x, 0),
7739 next_code),
7740 XEXP (x, 1));
7741 break;
7742 }
7743
7744 /* ... fall through ... */
7745
7746 case ASHIFTRT:
7747 lhs = XEXP (x, 0);
7748 rhs = XEXP (x, 1);
7749
7750 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7751 this is a SIGN_EXTRACT. */
7752 if (CONST_INT_P (rhs)
7753 && GET_CODE (lhs) == ASHIFT
7754 && CONST_INT_P (XEXP (lhs, 1))
7755 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7756 && INTVAL (XEXP (lhs, 1)) >= 0
7757 && INTVAL (rhs) < mode_width)
7758 {
7759 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7760 new_rtx = make_extraction (mode, new_rtx,
7761 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7762 NULL_RTX, mode_width - INTVAL (rhs),
7763 code == LSHIFTRT, 0, in_code == COMPARE);
7764 break;
7765 }
7766
7767 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7768 If so, try to merge the shifts into a SIGN_EXTEND. We could
7769 also do this for some cases of SIGN_EXTRACT, but it doesn't
7770 seem worth the effort; the case checked for occurs on Alpha. */
7771
7772 if (!OBJECT_P (lhs)
7773 && ! (GET_CODE (lhs) == SUBREG
7774 && (OBJECT_P (SUBREG_REG (lhs))))
7775 && CONST_INT_P (rhs)
7776 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7777 && INTVAL (rhs) < mode_width
7778 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7779 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7780 0, NULL_RTX, mode_width - INTVAL (rhs),
7781 code == LSHIFTRT, 0, in_code == COMPARE);
7782
7783 break;
7784
7785 case SUBREG:
7786 /* Call ourselves recursively on the inner expression. If we are
7787 narrowing the object and it has a different RTL code from
7788 what it originally did, do this SUBREG as a force_to_mode. */
7789 {
7790 rtx inner = SUBREG_REG (x), simplified;
7791 enum rtx_code subreg_code = in_code;
7792
7793 /* If in_code is COMPARE, it isn't always safe to pass it through
7794 to the recursive make_compound_operation call. */
7795 if (subreg_code == COMPARE
7796 && (!subreg_lowpart_p (x)
7797 || GET_CODE (inner) == SUBREG
7798 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7799 is (const_int 0), rather than
7800 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
7801 || (GET_CODE (inner) == AND
7802 && CONST_INT_P (XEXP (inner, 1))
7803 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7804 && exact_log2 (UINTVAL (XEXP (inner, 1)))
7805 >= GET_MODE_BITSIZE (mode))))
7806 subreg_code = SET;
7807
7808 tem = make_compound_operation (inner, subreg_code);
7809
7810 simplified
7811 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
7812 if (simplified)
7813 tem = simplified;
7814
7815 if (GET_CODE (tem) != GET_CODE (inner)
7816 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
7817 && subreg_lowpart_p (x))
7818 {
7819 rtx newer
7820 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
7821
7822 /* If we have something other than a SUBREG, we might have
7823 done an expansion, so rerun ourselves. */
7824 if (GET_CODE (newer) != SUBREG)
7825 newer = make_compound_operation (newer, in_code);
7826
7827 /* force_to_mode can expand compounds. If it just re-expanded the
7828 compound, use gen_lowpart to convert to the desired mode. */
7829 if (rtx_equal_p (newer, x)
7830 /* Likewise if it re-expanded the compound only partially.
7831 This happens for SUBREG of ZERO_EXTRACT if they extract
7832 the same number of bits. */
7833 || (GET_CODE (newer) == SUBREG
7834 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
7835 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
7836 && GET_CODE (inner) == AND
7837 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
7838 return gen_lowpart (GET_MODE (x), tem);
7839
7840 return newer;
7841 }
7842
7843 if (simplified)
7844 return tem;
7845 }
7846 break;
7847
7848 default:
7849 break;
7850 }
7851
7852 if (new_rtx)
7853 {
7854 x = gen_lowpart (mode, new_rtx);
7855 code = GET_CODE (x);
7856 }
7857
7858 /* Now recursively process each operand of this operation. We need to
7859 handle ZERO_EXTEND specially so that we don't lose track of the
7860 inner mode. */
7861 if (GET_CODE (x) == ZERO_EXTEND)
7862 {
7863 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7864 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
7865 new_rtx, GET_MODE (XEXP (x, 0)));
7866 if (tem)
7867 return tem;
7868 SUBST (XEXP (x, 0), new_rtx);
7869 return x;
7870 }
7871
7872 fmt = GET_RTX_FORMAT (code);
7873 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7874 if (fmt[i] == 'e')
7875 {
7876 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7877 SUBST (XEXP (x, i), new_rtx);
7878 }
7879 else if (fmt[i] == 'E')
7880 for (j = 0; j < XVECLEN (x, i); j++)
7881 {
7882 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
7883 SUBST (XVECEXP (x, i, j), new_rtx);
7884 }
7885
7886 maybe_swap:
7887 /* If this is a commutative operation, the changes to the operands
7888 may have made it noncanonical. */
7889 if (COMMUTATIVE_ARITH_P (x)
7890 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7891 {
7892 tem = XEXP (x, 0);
7893 SUBST (XEXP (x, 0), XEXP (x, 1));
7894 SUBST (XEXP (x, 1), tem);
7895 }
7896
7897 return x;
7898 }
7899 \f
7900 /* Given M see if it is a value that would select a field of bits
7901 within an item, but not the entire word. Return -1 if not.
7902 Otherwise, return the starting position of the field, where 0 is the
7903 low-order bit.
7904
7905 *PLEN is set to the length of the field. */
7906
7907 static int
7908 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7909 {
7910 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7911 int pos = m ? ctz_hwi (m) : -1;
7912 int len = 0;
7913
7914 if (pos >= 0)
7915 /* Now shift off the low-order zero bits and see if we have a
7916 power of two minus 1. */
7917 len = exact_log2 ((m >> pos) + 1);
7918
7919 if (len <= 0)
7920 pos = -1;
7921
7922 *plen = len;
7923 return pos;
7924 }
7925 \f
7926 /* If X refers to a register that equals REG in value, replace these
7927 references with REG. */
7928 static rtx
7929 canon_reg_for_combine (rtx x, rtx reg)
7930 {
7931 rtx op0, op1, op2;
7932 const char *fmt;
7933 int i;
7934 bool copied;
7935
7936 enum rtx_code code = GET_CODE (x);
7937 switch (GET_RTX_CLASS (code))
7938 {
7939 case RTX_UNARY:
7940 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7941 if (op0 != XEXP (x, 0))
7942 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7943 GET_MODE (reg));
7944 break;
7945
7946 case RTX_BIN_ARITH:
7947 case RTX_COMM_ARITH:
7948 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7949 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7950 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7951 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7952 break;
7953
7954 case RTX_COMPARE:
7955 case RTX_COMM_COMPARE:
7956 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7957 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7958 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7959 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7960 GET_MODE (op0), op0, op1);
7961 break;
7962
7963 case RTX_TERNARY:
7964 case RTX_BITFIELD_OPS:
7965 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7966 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7967 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7968 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7969 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7970 GET_MODE (op0), op0, op1, op2);
7971
7972 case RTX_OBJ:
7973 if (REG_P (x))
7974 {
7975 if (rtx_equal_p (get_last_value (reg), x)
7976 || rtx_equal_p (reg, get_last_value (x)))
7977 return reg;
7978 else
7979 break;
7980 }
7981
7982 /* fall through */
7983
7984 default:
7985 fmt = GET_RTX_FORMAT (code);
7986 copied = false;
7987 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7988 if (fmt[i] == 'e')
7989 {
7990 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7991 if (op != XEXP (x, i))
7992 {
7993 if (!copied)
7994 {
7995 copied = true;
7996 x = copy_rtx (x);
7997 }
7998 XEXP (x, i) = op;
7999 }
8000 }
8001 else if (fmt[i] == 'E')
8002 {
8003 int j;
8004 for (j = 0; j < XVECLEN (x, i); j++)
8005 {
8006 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8007 if (op != XVECEXP (x, i, j))
8008 {
8009 if (!copied)
8010 {
8011 copied = true;
8012 x = copy_rtx (x);
8013 }
8014 XVECEXP (x, i, j) = op;
8015 }
8016 }
8017 }
8018
8019 break;
8020 }
8021
8022 return x;
8023 }
8024
8025 /* Return X converted to MODE. If the value is already truncated to
8026 MODE we can just return a subreg even though in the general case we
8027 would need an explicit truncation. */
8028
8029 static rtx
8030 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
8031 {
8032 if (!CONST_INT_P (x)
8033 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8034 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8035 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8036 {
8037 /* Bit-cast X into an integer mode. */
8038 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8039 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8040 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8041 x, GET_MODE (x));
8042 }
8043
8044 return gen_lowpart (mode, x);
8045 }
8046
8047 /* See if X can be simplified knowing that we will only refer to it in
8048 MODE and will only refer to those bits that are nonzero in MASK.
8049 If other bits are being computed or if masking operations are done
8050 that select a superset of the bits in MASK, they can sometimes be
8051 ignored.
8052
8053 Return a possibly simplified expression, but always convert X to
8054 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8055
8056 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8057 are all off in X. This is used when X will be complemented, by either
8058 NOT, NEG, or XOR. */
8059
8060 static rtx
8061 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
8062 int just_select)
8063 {
8064 enum rtx_code code = GET_CODE (x);
8065 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8066 enum machine_mode op_mode;
8067 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8068 rtx op0, op1, temp;
8069
8070 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8071 code below will do the wrong thing since the mode of such an
8072 expression is VOIDmode.
8073
8074 Also do nothing if X is a CLOBBER; this can happen if X was
8075 the return value from a call to gen_lowpart. */
8076 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8077 return x;
8078
8079 /* We want to perform the operation in its present mode unless we know
8080 that the operation is valid in MODE, in which case we do the operation
8081 in MODE. */
8082 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8083 && have_insn_for (code, mode))
8084 ? mode : GET_MODE (x));
8085
8086 /* It is not valid to do a right-shift in a narrower mode
8087 than the one it came in with. */
8088 if ((code == LSHIFTRT || code == ASHIFTRT)
8089 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8090 op_mode = GET_MODE (x);
8091
8092 /* Truncate MASK to fit OP_MODE. */
8093 if (op_mode)
8094 mask &= GET_MODE_MASK (op_mode);
8095
8096 /* When we have an arithmetic operation, or a shift whose count we
8097 do not know, we need to assume that all bits up to the highest-order
8098 bit in MASK will be needed. This is how we form such a mask. */
8099 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8100 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8101 else
8102 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8103 - 1);
8104
8105 /* Determine what bits of X are guaranteed to be (non)zero. */
8106 nonzero = nonzero_bits (x, mode);
8107
8108 /* If none of the bits in X are needed, return a zero. */
8109 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8110 x = const0_rtx;
8111
8112 /* If X is a CONST_INT, return a new one. Do this here since the
8113 test below will fail. */
8114 if (CONST_INT_P (x))
8115 {
8116 if (SCALAR_INT_MODE_P (mode))
8117 return gen_int_mode (INTVAL (x) & mask, mode);
8118 else
8119 {
8120 x = GEN_INT (INTVAL (x) & mask);
8121 return gen_lowpart_common (mode, x);
8122 }
8123 }
8124
8125 /* If X is narrower than MODE and we want all the bits in X's mode, just
8126 get X in the proper mode. */
8127 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8128 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8129 return gen_lowpart (mode, x);
8130
8131 /* We can ignore the effect of a SUBREG if it narrows the mode or
8132 if the constant masks to zero all the bits the mode doesn't have. */
8133 if (GET_CODE (x) == SUBREG
8134 && subreg_lowpart_p (x)
8135 && ((GET_MODE_SIZE (GET_MODE (x))
8136 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8137 || (0 == (mask
8138 & GET_MODE_MASK (GET_MODE (x))
8139 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8140 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8141
8142 /* The arithmetic simplifications here only work for scalar integer modes. */
8143 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8144 return gen_lowpart_or_truncate (mode, x);
8145
8146 switch (code)
8147 {
8148 case CLOBBER:
8149 /* If X is a (clobber (const_int)), return it since we know we are
8150 generating something that won't match. */
8151 return x;
8152
8153 case SIGN_EXTEND:
8154 case ZERO_EXTEND:
8155 case ZERO_EXTRACT:
8156 case SIGN_EXTRACT:
8157 x = expand_compound_operation (x);
8158 if (GET_CODE (x) != code)
8159 return force_to_mode (x, mode, mask, next_select);
8160 break;
8161
8162 case TRUNCATE:
8163 /* Similarly for a truncate. */
8164 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8165
8166 case AND:
8167 /* If this is an AND with a constant, convert it into an AND
8168 whose constant is the AND of that constant with MASK. If it
8169 remains an AND of MASK, delete it since it is redundant. */
8170
8171 if (CONST_INT_P (XEXP (x, 1)))
8172 {
8173 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8174 mask & INTVAL (XEXP (x, 1)));
8175
8176 /* If X is still an AND, see if it is an AND with a mask that
8177 is just some low-order bits. If so, and it is MASK, we don't
8178 need it. */
8179
8180 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8181 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8182 == mask))
8183 x = XEXP (x, 0);
8184
8185 /* If it remains an AND, try making another AND with the bits
8186 in the mode mask that aren't in MASK turned on. If the
8187 constant in the AND is wide enough, this might make a
8188 cheaper constant. */
8189
8190 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8191 && GET_MODE_MASK (GET_MODE (x)) != mask
8192 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8193 {
8194 unsigned HOST_WIDE_INT cval
8195 = UINTVAL (XEXP (x, 1))
8196 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8197 rtx y;
8198
8199 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8200 gen_int_mode (cval, GET_MODE (x)));
8201 if (set_src_cost (y, optimize_this_for_speed_p)
8202 < set_src_cost (x, optimize_this_for_speed_p))
8203 x = y;
8204 }
8205
8206 break;
8207 }
8208
8209 goto binop;
8210
8211 case PLUS:
8212 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8213 low-order bits (as in an alignment operation) and FOO is already
8214 aligned to that boundary, mask C1 to that boundary as well.
8215 This may eliminate that PLUS and, later, the AND. */
8216
8217 {
8218 unsigned int width = GET_MODE_PRECISION (mode);
8219 unsigned HOST_WIDE_INT smask = mask;
8220
8221 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8222 number, sign extend it. */
8223
8224 if (width < HOST_BITS_PER_WIDE_INT
8225 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8226 smask |= HOST_WIDE_INT_M1U << width;
8227
8228 if (CONST_INT_P (XEXP (x, 1))
8229 && exact_log2 (- smask) >= 0
8230 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8231 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8232 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8233 (INTVAL (XEXP (x, 1)) & smask)),
8234 mode, smask, next_select);
8235 }
8236
8237 /* ... fall through ... */
8238
8239 case MULT:
8240 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8241 most significant bit in MASK since carries from those bits will
8242 affect the bits we are interested in. */
8243 mask = fuller_mask;
8244 goto binop;
8245
8246 case MINUS:
8247 /* If X is (minus C Y) where C's least set bit is larger than any bit
8248 in the mask, then we may replace with (neg Y). */
8249 if (CONST_INT_P (XEXP (x, 0))
8250 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8251 {
8252 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8253 GET_MODE (x));
8254 return force_to_mode (x, mode, mask, next_select);
8255 }
8256
8257 /* Similarly, if C contains every bit in the fuller_mask, then we may
8258 replace with (not Y). */
8259 if (CONST_INT_P (XEXP (x, 0))
8260 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8261 {
8262 x = simplify_gen_unary (NOT, GET_MODE (x),
8263 XEXP (x, 1), GET_MODE (x));
8264 return force_to_mode (x, mode, mask, next_select);
8265 }
8266
8267 mask = fuller_mask;
8268 goto binop;
8269
8270 case IOR:
8271 case XOR:
8272 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8273 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8274 operation which may be a bitfield extraction. Ensure that the
8275 constant we form is not wider than the mode of X. */
8276
8277 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8278 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8279 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8280 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8281 && CONST_INT_P (XEXP (x, 1))
8282 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8283 + floor_log2 (INTVAL (XEXP (x, 1))))
8284 < GET_MODE_PRECISION (GET_MODE (x)))
8285 && (UINTVAL (XEXP (x, 1))
8286 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8287 {
8288 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8289 << INTVAL (XEXP (XEXP (x, 0), 1)),
8290 GET_MODE (x));
8291 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8292 XEXP (XEXP (x, 0), 0), temp);
8293 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8294 XEXP (XEXP (x, 0), 1));
8295 return force_to_mode (x, mode, mask, next_select);
8296 }
8297
8298 binop:
8299 /* For most binary operations, just propagate into the operation and
8300 change the mode if we have an operation of that mode. */
8301
8302 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8303 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8304
8305 /* If we ended up truncating both operands, truncate the result of the
8306 operation instead. */
8307 if (GET_CODE (op0) == TRUNCATE
8308 && GET_CODE (op1) == TRUNCATE)
8309 {
8310 op0 = XEXP (op0, 0);
8311 op1 = XEXP (op1, 0);
8312 }
8313
8314 op0 = gen_lowpart_or_truncate (op_mode, op0);
8315 op1 = gen_lowpart_or_truncate (op_mode, op1);
8316
8317 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8318 x = simplify_gen_binary (code, op_mode, op0, op1);
8319 break;
8320
8321 case ASHIFT:
8322 /* For left shifts, do the same, but just for the first operand.
8323 However, we cannot do anything with shifts where we cannot
8324 guarantee that the counts are smaller than the size of the mode
8325 because such a count will have a different meaning in a
8326 wider mode. */
8327
8328 if (! (CONST_INT_P (XEXP (x, 1))
8329 && INTVAL (XEXP (x, 1)) >= 0
8330 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8331 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8332 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8333 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8334 break;
8335
8336 /* If the shift count is a constant and we can do arithmetic in
8337 the mode of the shift, refine which bits we need. Otherwise, use the
8338 conservative form of the mask. */
8339 if (CONST_INT_P (XEXP (x, 1))
8340 && INTVAL (XEXP (x, 1)) >= 0
8341 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8342 && HWI_COMPUTABLE_MODE_P (op_mode))
8343 mask >>= INTVAL (XEXP (x, 1));
8344 else
8345 mask = fuller_mask;
8346
8347 op0 = gen_lowpart_or_truncate (op_mode,
8348 force_to_mode (XEXP (x, 0), op_mode,
8349 mask, next_select));
8350
8351 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8352 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8353 break;
8354
8355 case LSHIFTRT:
8356 /* Here we can only do something if the shift count is a constant,
8357 this shift constant is valid for the host, and we can do arithmetic
8358 in OP_MODE. */
8359
8360 if (CONST_INT_P (XEXP (x, 1))
8361 && INTVAL (XEXP (x, 1)) >= 0
8362 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8363 && HWI_COMPUTABLE_MODE_P (op_mode))
8364 {
8365 rtx inner = XEXP (x, 0);
8366 unsigned HOST_WIDE_INT inner_mask;
8367
8368 /* Select the mask of the bits we need for the shift operand. */
8369 inner_mask = mask << INTVAL (XEXP (x, 1));
8370
8371 /* We can only change the mode of the shift if we can do arithmetic
8372 in the mode of the shift and INNER_MASK is no wider than the
8373 width of X's mode. */
8374 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8375 op_mode = GET_MODE (x);
8376
8377 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8378
8379 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8380 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8381 }
8382
8383 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8384 shift and AND produces only copies of the sign bit (C2 is one less
8385 than a power of two), we can do this with just a shift. */
8386
8387 if (GET_CODE (x) == LSHIFTRT
8388 && CONST_INT_P (XEXP (x, 1))
8389 /* The shift puts one of the sign bit copies in the least significant
8390 bit. */
8391 && ((INTVAL (XEXP (x, 1))
8392 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8393 >= GET_MODE_PRECISION (GET_MODE (x)))
8394 && exact_log2 (mask + 1) >= 0
8395 /* Number of bits left after the shift must be more than the mask
8396 needs. */
8397 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8398 <= GET_MODE_PRECISION (GET_MODE (x)))
8399 /* Must be more sign bit copies than the mask needs. */
8400 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8401 >= exact_log2 (mask + 1)))
8402 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8403 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8404 - exact_log2 (mask + 1)));
8405
8406 goto shiftrt;
8407
8408 case ASHIFTRT:
8409 /* If we are just looking for the sign bit, we don't need this shift at
8410 all, even if it has a variable count. */
8411 if (val_signbit_p (GET_MODE (x), mask))
8412 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8413
8414 /* If this is a shift by a constant, get a mask that contains those bits
8415 that are not copies of the sign bit. We then have two cases: If
8416 MASK only includes those bits, this can be a logical shift, which may
8417 allow simplifications. If MASK is a single-bit field not within
8418 those bits, we are requesting a copy of the sign bit and hence can
8419 shift the sign bit to the appropriate location. */
8420
8421 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8422 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8423 {
8424 int i;
8425
8426 /* If the considered data is wider than HOST_WIDE_INT, we can't
8427 represent a mask for all its bits in a single scalar.
8428 But we only care about the lower bits, so calculate these. */
8429
8430 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8431 {
8432 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8433
8434 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8435 is the number of bits a full-width mask would have set.
8436 We need only shift if these are fewer than nonzero can
8437 hold. If not, we must keep all bits set in nonzero. */
8438
8439 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8440 < HOST_BITS_PER_WIDE_INT)
8441 nonzero >>= INTVAL (XEXP (x, 1))
8442 + HOST_BITS_PER_WIDE_INT
8443 - GET_MODE_PRECISION (GET_MODE (x)) ;
8444 }
8445 else
8446 {
8447 nonzero = GET_MODE_MASK (GET_MODE (x));
8448 nonzero >>= INTVAL (XEXP (x, 1));
8449 }
8450
8451 if ((mask & ~nonzero) == 0)
8452 {
8453 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8454 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8455 if (GET_CODE (x) != ASHIFTRT)
8456 return force_to_mode (x, mode, mask, next_select);
8457 }
8458
8459 else if ((i = exact_log2 (mask)) >= 0)
8460 {
8461 x = simplify_shift_const
8462 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8463 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8464
8465 if (GET_CODE (x) != ASHIFTRT)
8466 return force_to_mode (x, mode, mask, next_select);
8467 }
8468 }
8469
8470 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8471 even if the shift count isn't a constant. */
8472 if (mask == 1)
8473 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8474 XEXP (x, 0), XEXP (x, 1));
8475
8476 shiftrt:
8477
8478 /* If this is a zero- or sign-extension operation that just affects bits
8479 we don't care about, remove it. Be sure the call above returned
8480 something that is still a shift. */
8481
8482 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8483 && CONST_INT_P (XEXP (x, 1))
8484 && INTVAL (XEXP (x, 1)) >= 0
8485 && (INTVAL (XEXP (x, 1))
8486 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8487 && GET_CODE (XEXP (x, 0)) == ASHIFT
8488 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8489 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8490 next_select);
8491
8492 break;
8493
8494 case ROTATE:
8495 case ROTATERT:
8496 /* If the shift count is constant and we can do computations
8497 in the mode of X, compute where the bits we care about are.
8498 Otherwise, we can't do anything. Don't change the mode of
8499 the shift or propagate MODE into the shift, though. */
8500 if (CONST_INT_P (XEXP (x, 1))
8501 && INTVAL (XEXP (x, 1)) >= 0)
8502 {
8503 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8504 GET_MODE (x),
8505 gen_int_mode (mask, GET_MODE (x)),
8506 XEXP (x, 1));
8507 if (temp && CONST_INT_P (temp))
8508 x = simplify_gen_binary (code, GET_MODE (x),
8509 force_to_mode (XEXP (x, 0), GET_MODE (x),
8510 INTVAL (temp), next_select),
8511 XEXP (x, 1));
8512 }
8513 break;
8514
8515 case NEG:
8516 /* If we just want the low-order bit, the NEG isn't needed since it
8517 won't change the low-order bit. */
8518 if (mask == 1)
8519 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8520
8521 /* We need any bits less significant than the most significant bit in
8522 MASK since carries from those bits will affect the bits we are
8523 interested in. */
8524 mask = fuller_mask;
8525 goto unop;
8526
8527 case NOT:
8528 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8529 same as the XOR case above. Ensure that the constant we form is not
8530 wider than the mode of X. */
8531
8532 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8533 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8534 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8535 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8536 < GET_MODE_PRECISION (GET_MODE (x)))
8537 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8538 {
8539 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8540 GET_MODE (x));
8541 temp = simplify_gen_binary (XOR, GET_MODE (x),
8542 XEXP (XEXP (x, 0), 0), temp);
8543 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8544 temp, XEXP (XEXP (x, 0), 1));
8545
8546 return force_to_mode (x, mode, mask, next_select);
8547 }
8548
8549 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8550 use the full mask inside the NOT. */
8551 mask = fuller_mask;
8552
8553 unop:
8554 op0 = gen_lowpart_or_truncate (op_mode,
8555 force_to_mode (XEXP (x, 0), mode, mask,
8556 next_select));
8557 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8558 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8559 break;
8560
8561 case NE:
8562 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8563 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8564 which is equal to STORE_FLAG_VALUE. */
8565 if ((mask & ~STORE_FLAG_VALUE) == 0
8566 && XEXP (x, 1) == const0_rtx
8567 && GET_MODE (XEXP (x, 0)) == mode
8568 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8569 && (nonzero_bits (XEXP (x, 0), mode)
8570 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8571 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8572
8573 break;
8574
8575 case IF_THEN_ELSE:
8576 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8577 written in a narrower mode. We play it safe and do not do so. */
8578
8579 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8580 force_to_mode (XEXP (x, 1), mode,
8581 mask, next_select));
8582 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8583 force_to_mode (XEXP (x, 2), mode,
8584 mask, next_select));
8585 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8586 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8587 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8588 op0, op1);
8589 break;
8590
8591 default:
8592 break;
8593 }
8594
8595 /* Ensure we return a value of the proper mode. */
8596 return gen_lowpart_or_truncate (mode, x);
8597 }
8598 \f
8599 /* Return nonzero if X is an expression that has one of two values depending on
8600 whether some other value is zero or nonzero. In that case, we return the
8601 value that is being tested, *PTRUE is set to the value if the rtx being
8602 returned has a nonzero value, and *PFALSE is set to the other alternative.
8603
8604 If we return zero, we set *PTRUE and *PFALSE to X. */
8605
8606 static rtx
8607 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8608 {
8609 enum machine_mode mode = GET_MODE (x);
8610 enum rtx_code code = GET_CODE (x);
8611 rtx cond0, cond1, true0, true1, false0, false1;
8612 unsigned HOST_WIDE_INT nz;
8613
8614 /* If we are comparing a value against zero, we are done. */
8615 if ((code == NE || code == EQ)
8616 && XEXP (x, 1) == const0_rtx)
8617 {
8618 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8619 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8620 return XEXP (x, 0);
8621 }
8622
8623 /* If this is a unary operation whose operand has one of two values, apply
8624 our opcode to compute those values. */
8625 else if (UNARY_P (x)
8626 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8627 {
8628 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8629 *pfalse = simplify_gen_unary (code, mode, false0,
8630 GET_MODE (XEXP (x, 0)));
8631 return cond0;
8632 }
8633
8634 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8635 make can't possibly match and would suppress other optimizations. */
8636 else if (code == COMPARE)
8637 ;
8638
8639 /* If this is a binary operation, see if either side has only one of two
8640 values. If either one does or if both do and they are conditional on
8641 the same value, compute the new true and false values. */
8642 else if (BINARY_P (x))
8643 {
8644 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8645 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8646
8647 if ((cond0 != 0 || cond1 != 0)
8648 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8649 {
8650 /* If if_then_else_cond returned zero, then true/false are the
8651 same rtl. We must copy one of them to prevent invalid rtl
8652 sharing. */
8653 if (cond0 == 0)
8654 true0 = copy_rtx (true0);
8655 else if (cond1 == 0)
8656 true1 = copy_rtx (true1);
8657
8658 if (COMPARISON_P (x))
8659 {
8660 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8661 true0, true1);
8662 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8663 false0, false1);
8664 }
8665 else
8666 {
8667 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8668 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8669 }
8670
8671 return cond0 ? cond0 : cond1;
8672 }
8673
8674 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8675 operands is zero when the other is nonzero, and vice-versa,
8676 and STORE_FLAG_VALUE is 1 or -1. */
8677
8678 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8679 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8680 || code == UMAX)
8681 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8682 {
8683 rtx op0 = XEXP (XEXP (x, 0), 1);
8684 rtx op1 = XEXP (XEXP (x, 1), 1);
8685
8686 cond0 = XEXP (XEXP (x, 0), 0);
8687 cond1 = XEXP (XEXP (x, 1), 0);
8688
8689 if (COMPARISON_P (cond0)
8690 && COMPARISON_P (cond1)
8691 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8692 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8693 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8694 || ((swap_condition (GET_CODE (cond0))
8695 == reversed_comparison_code (cond1, NULL))
8696 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8697 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8698 && ! side_effects_p (x))
8699 {
8700 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8701 *pfalse = simplify_gen_binary (MULT, mode,
8702 (code == MINUS
8703 ? simplify_gen_unary (NEG, mode,
8704 op1, mode)
8705 : op1),
8706 const_true_rtx);
8707 return cond0;
8708 }
8709 }
8710
8711 /* Similarly for MULT, AND and UMIN, except that for these the result
8712 is always zero. */
8713 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8714 && (code == MULT || code == AND || code == UMIN)
8715 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8716 {
8717 cond0 = XEXP (XEXP (x, 0), 0);
8718 cond1 = XEXP (XEXP (x, 1), 0);
8719
8720 if (COMPARISON_P (cond0)
8721 && COMPARISON_P (cond1)
8722 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8723 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8724 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8725 || ((swap_condition (GET_CODE (cond0))
8726 == reversed_comparison_code (cond1, NULL))
8727 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8728 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8729 && ! side_effects_p (x))
8730 {
8731 *ptrue = *pfalse = const0_rtx;
8732 return cond0;
8733 }
8734 }
8735 }
8736
8737 else if (code == IF_THEN_ELSE)
8738 {
8739 /* If we have IF_THEN_ELSE already, extract the condition and
8740 canonicalize it if it is NE or EQ. */
8741 cond0 = XEXP (x, 0);
8742 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8743 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8744 return XEXP (cond0, 0);
8745 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8746 {
8747 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8748 return XEXP (cond0, 0);
8749 }
8750 else
8751 return cond0;
8752 }
8753
8754 /* If X is a SUBREG, we can narrow both the true and false values
8755 if the inner expression, if there is a condition. */
8756 else if (code == SUBREG
8757 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8758 &true0, &false0)))
8759 {
8760 true0 = simplify_gen_subreg (mode, true0,
8761 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8762 false0 = simplify_gen_subreg (mode, false0,
8763 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8764 if (true0 && false0)
8765 {
8766 *ptrue = true0;
8767 *pfalse = false0;
8768 return cond0;
8769 }
8770 }
8771
8772 /* If X is a constant, this isn't special and will cause confusions
8773 if we treat it as such. Likewise if it is equivalent to a constant. */
8774 else if (CONSTANT_P (x)
8775 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8776 ;
8777
8778 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8779 will be least confusing to the rest of the compiler. */
8780 else if (mode == BImode)
8781 {
8782 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8783 return x;
8784 }
8785
8786 /* If X is known to be either 0 or -1, those are the true and
8787 false values when testing X. */
8788 else if (x == constm1_rtx || x == const0_rtx
8789 || (mode != VOIDmode
8790 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8791 {
8792 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8793 return x;
8794 }
8795
8796 /* Likewise for 0 or a single bit. */
8797 else if (HWI_COMPUTABLE_MODE_P (mode)
8798 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
8799 {
8800 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
8801 return x;
8802 }
8803
8804 /* Otherwise fail; show no condition with true and false values the same. */
8805 *ptrue = *pfalse = x;
8806 return 0;
8807 }
8808 \f
8809 /* Return the value of expression X given the fact that condition COND
8810 is known to be true when applied to REG as its first operand and VAL
8811 as its second. X is known to not be shared and so can be modified in
8812 place.
8813
8814 We only handle the simplest cases, and specifically those cases that
8815 arise with IF_THEN_ELSE expressions. */
8816
8817 static rtx
8818 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
8819 {
8820 enum rtx_code code = GET_CODE (x);
8821 rtx temp;
8822 const char *fmt;
8823 int i, j;
8824
8825 if (side_effects_p (x))
8826 return x;
8827
8828 /* If either operand of the condition is a floating point value,
8829 then we have to avoid collapsing an EQ comparison. */
8830 if (cond == EQ
8831 && rtx_equal_p (x, reg)
8832 && ! FLOAT_MODE_P (GET_MODE (x))
8833 && ! FLOAT_MODE_P (GET_MODE (val)))
8834 return val;
8835
8836 if (cond == UNEQ && rtx_equal_p (x, reg))
8837 return val;
8838
8839 /* If X is (abs REG) and we know something about REG's relationship
8840 with zero, we may be able to simplify this. */
8841
8842 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8843 switch (cond)
8844 {
8845 case GE: case GT: case EQ:
8846 return XEXP (x, 0);
8847 case LT: case LE:
8848 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8849 XEXP (x, 0),
8850 GET_MODE (XEXP (x, 0)));
8851 default:
8852 break;
8853 }
8854
8855 /* The only other cases we handle are MIN, MAX, and comparisons if the
8856 operands are the same as REG and VAL. */
8857
8858 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8859 {
8860 if (rtx_equal_p (XEXP (x, 0), val))
8861 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8862
8863 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8864 {
8865 if (COMPARISON_P (x))
8866 {
8867 if (comparison_dominates_p (cond, code))
8868 return const_true_rtx;
8869
8870 code = reversed_comparison_code (x, NULL);
8871 if (code != UNKNOWN
8872 && comparison_dominates_p (cond, code))
8873 return const0_rtx;
8874 else
8875 return x;
8876 }
8877 else if (code == SMAX || code == SMIN
8878 || code == UMIN || code == UMAX)
8879 {
8880 int unsignedp = (code == UMIN || code == UMAX);
8881
8882 /* Do not reverse the condition when it is NE or EQ.
8883 This is because we cannot conclude anything about
8884 the value of 'SMAX (x, y)' when x is not equal to y,
8885 but we can when x equals y. */
8886 if ((code == SMAX || code == UMAX)
8887 && ! (cond == EQ || cond == NE))
8888 cond = reverse_condition (cond);
8889
8890 switch (cond)
8891 {
8892 case GE: case GT:
8893 return unsignedp ? x : XEXP (x, 1);
8894 case LE: case LT:
8895 return unsignedp ? x : XEXP (x, 0);
8896 case GEU: case GTU:
8897 return unsignedp ? XEXP (x, 1) : x;
8898 case LEU: case LTU:
8899 return unsignedp ? XEXP (x, 0) : x;
8900 default:
8901 break;
8902 }
8903 }
8904 }
8905 }
8906 else if (code == SUBREG)
8907 {
8908 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8909 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8910
8911 if (SUBREG_REG (x) != r)
8912 {
8913 /* We must simplify subreg here, before we lose track of the
8914 original inner_mode. */
8915 new_rtx = simplify_subreg (GET_MODE (x), r,
8916 inner_mode, SUBREG_BYTE (x));
8917 if (new_rtx)
8918 return new_rtx;
8919 else
8920 SUBST (SUBREG_REG (x), r);
8921 }
8922
8923 return x;
8924 }
8925 /* We don't have to handle SIGN_EXTEND here, because even in the
8926 case of replacing something with a modeless CONST_INT, a
8927 CONST_INT is already (supposed to be) a valid sign extension for
8928 its narrower mode, which implies it's already properly
8929 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8930 story is different. */
8931 else if (code == ZERO_EXTEND)
8932 {
8933 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8934 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8935
8936 if (XEXP (x, 0) != r)
8937 {
8938 /* We must simplify the zero_extend here, before we lose
8939 track of the original inner_mode. */
8940 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8941 r, inner_mode);
8942 if (new_rtx)
8943 return new_rtx;
8944 else
8945 SUBST (XEXP (x, 0), r);
8946 }
8947
8948 return x;
8949 }
8950
8951 fmt = GET_RTX_FORMAT (code);
8952 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8953 {
8954 if (fmt[i] == 'e')
8955 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8956 else if (fmt[i] == 'E')
8957 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8958 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8959 cond, reg, val));
8960 }
8961
8962 return x;
8963 }
8964 \f
8965 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8966 assignment as a field assignment. */
8967
8968 static int
8969 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8970 {
8971 if (x == y || rtx_equal_p (x, y))
8972 return 1;
8973
8974 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8975 return 0;
8976
8977 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8978 Note that all SUBREGs of MEM are paradoxical; otherwise they
8979 would have been rewritten. */
8980 if (MEM_P (x) && GET_CODE (y) == SUBREG
8981 && MEM_P (SUBREG_REG (y))
8982 && rtx_equal_p (SUBREG_REG (y),
8983 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8984 return 1;
8985
8986 if (MEM_P (y) && GET_CODE (x) == SUBREG
8987 && MEM_P (SUBREG_REG (x))
8988 && rtx_equal_p (SUBREG_REG (x),
8989 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8990 return 1;
8991
8992 /* We used to see if get_last_value of X and Y were the same but that's
8993 not correct. In one direction, we'll cause the assignment to have
8994 the wrong destination and in the case, we'll import a register into this
8995 insn that might have already have been dead. So fail if none of the
8996 above cases are true. */
8997 return 0;
8998 }
8999 \f
9000 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9001 Return that assignment if so.
9002
9003 We only handle the most common cases. */
9004
9005 static rtx
9006 make_field_assignment (rtx x)
9007 {
9008 rtx dest = SET_DEST (x);
9009 rtx src = SET_SRC (x);
9010 rtx assign;
9011 rtx rhs, lhs;
9012 HOST_WIDE_INT c1;
9013 HOST_WIDE_INT pos;
9014 unsigned HOST_WIDE_INT len;
9015 rtx other;
9016 enum machine_mode mode;
9017
9018 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9019 a clear of a one-bit field. We will have changed it to
9020 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9021 for a SUBREG. */
9022
9023 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9024 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9025 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9026 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9027 {
9028 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9029 1, 1, 1, 0);
9030 if (assign != 0)
9031 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9032 return x;
9033 }
9034
9035 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9036 && subreg_lowpart_p (XEXP (src, 0))
9037 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9038 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9039 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9040 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9041 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9042 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9043 {
9044 assign = make_extraction (VOIDmode, dest, 0,
9045 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9046 1, 1, 1, 0);
9047 if (assign != 0)
9048 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9049 return x;
9050 }
9051
9052 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9053 one-bit field. */
9054 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9055 && XEXP (XEXP (src, 0), 0) == const1_rtx
9056 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9057 {
9058 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9059 1, 1, 1, 0);
9060 if (assign != 0)
9061 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9062 return x;
9063 }
9064
9065 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9066 SRC is an AND with all bits of that field set, then we can discard
9067 the AND. */
9068 if (GET_CODE (dest) == ZERO_EXTRACT
9069 && CONST_INT_P (XEXP (dest, 1))
9070 && GET_CODE (src) == AND
9071 && CONST_INT_P (XEXP (src, 1)))
9072 {
9073 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9074 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9075 unsigned HOST_WIDE_INT ze_mask;
9076
9077 if (width >= HOST_BITS_PER_WIDE_INT)
9078 ze_mask = -1;
9079 else
9080 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9081
9082 /* Complete overlap. We can remove the source AND. */
9083 if ((and_mask & ze_mask) == ze_mask)
9084 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9085
9086 /* Partial overlap. We can reduce the source AND. */
9087 if ((and_mask & ze_mask) != and_mask)
9088 {
9089 mode = GET_MODE (src);
9090 src = gen_rtx_AND (mode, XEXP (src, 0),
9091 gen_int_mode (and_mask & ze_mask, mode));
9092 return gen_rtx_SET (VOIDmode, dest, src);
9093 }
9094 }
9095
9096 /* The other case we handle is assignments into a constant-position
9097 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9098 a mask that has all one bits except for a group of zero bits and
9099 OTHER is known to have zeros where C1 has ones, this is such an
9100 assignment. Compute the position and length from C1. Shift OTHER
9101 to the appropriate position, force it to the required mode, and
9102 make the extraction. Check for the AND in both operands. */
9103
9104 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9105 return x;
9106
9107 rhs = expand_compound_operation (XEXP (src, 0));
9108 lhs = expand_compound_operation (XEXP (src, 1));
9109
9110 if (GET_CODE (rhs) == AND
9111 && CONST_INT_P (XEXP (rhs, 1))
9112 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9113 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9114 else if (GET_CODE (lhs) == AND
9115 && CONST_INT_P (XEXP (lhs, 1))
9116 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9117 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9118 else
9119 return x;
9120
9121 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9122 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9123 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9124 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9125 return x;
9126
9127 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9128 if (assign == 0)
9129 return x;
9130
9131 /* The mode to use for the source is the mode of the assignment, or of
9132 what is inside a possible STRICT_LOW_PART. */
9133 mode = (GET_CODE (assign) == STRICT_LOW_PART
9134 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9135
9136 /* Shift OTHER right POS places and make it the source, restricting it
9137 to the proper length and mode. */
9138
9139 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9140 GET_MODE (src),
9141 other, pos),
9142 dest);
9143 src = force_to_mode (src, mode,
9144 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9145 ? ~(unsigned HOST_WIDE_INT) 0
9146 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9147 0);
9148
9149 /* If SRC is masked by an AND that does not make a difference in
9150 the value being stored, strip it. */
9151 if (GET_CODE (assign) == ZERO_EXTRACT
9152 && CONST_INT_P (XEXP (assign, 1))
9153 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9154 && GET_CODE (src) == AND
9155 && CONST_INT_P (XEXP (src, 1))
9156 && UINTVAL (XEXP (src, 1))
9157 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9158 src = XEXP (src, 0);
9159
9160 return gen_rtx_SET (VOIDmode, assign, src);
9161 }
9162 \f
9163 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9164 if so. */
9165
9166 static rtx
9167 apply_distributive_law (rtx x)
9168 {
9169 enum rtx_code code = GET_CODE (x);
9170 enum rtx_code inner_code;
9171 rtx lhs, rhs, other;
9172 rtx tem;
9173
9174 /* Distributivity is not true for floating point as it can change the
9175 value. So we don't do it unless -funsafe-math-optimizations. */
9176 if (FLOAT_MODE_P (GET_MODE (x))
9177 && ! flag_unsafe_math_optimizations)
9178 return x;
9179
9180 /* The outer operation can only be one of the following: */
9181 if (code != IOR && code != AND && code != XOR
9182 && code != PLUS && code != MINUS)
9183 return x;
9184
9185 lhs = XEXP (x, 0);
9186 rhs = XEXP (x, 1);
9187
9188 /* If either operand is a primitive we can't do anything, so get out
9189 fast. */
9190 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9191 return x;
9192
9193 lhs = expand_compound_operation (lhs);
9194 rhs = expand_compound_operation (rhs);
9195 inner_code = GET_CODE (lhs);
9196 if (inner_code != GET_CODE (rhs))
9197 return x;
9198
9199 /* See if the inner and outer operations distribute. */
9200 switch (inner_code)
9201 {
9202 case LSHIFTRT:
9203 case ASHIFTRT:
9204 case AND:
9205 case IOR:
9206 /* These all distribute except over PLUS. */
9207 if (code == PLUS || code == MINUS)
9208 return x;
9209 break;
9210
9211 case MULT:
9212 if (code != PLUS && code != MINUS)
9213 return x;
9214 break;
9215
9216 case ASHIFT:
9217 /* This is also a multiply, so it distributes over everything. */
9218 break;
9219
9220 /* This used to handle SUBREG, but this turned out to be counter-
9221 productive, since (subreg (op ...)) usually is not handled by
9222 insn patterns, and this "optimization" therefore transformed
9223 recognizable patterns into unrecognizable ones. Therefore the
9224 SUBREG case was removed from here.
9225
9226 It is possible that distributing SUBREG over arithmetic operations
9227 leads to an intermediate result than can then be optimized further,
9228 e.g. by moving the outer SUBREG to the other side of a SET as done
9229 in simplify_set. This seems to have been the original intent of
9230 handling SUBREGs here.
9231
9232 However, with current GCC this does not appear to actually happen,
9233 at least on major platforms. If some case is found where removing
9234 the SUBREG case here prevents follow-on optimizations, distributing
9235 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9236
9237 default:
9238 return x;
9239 }
9240
9241 /* Set LHS and RHS to the inner operands (A and B in the example
9242 above) and set OTHER to the common operand (C in the example).
9243 There is only one way to do this unless the inner operation is
9244 commutative. */
9245 if (COMMUTATIVE_ARITH_P (lhs)
9246 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9247 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9248 else if (COMMUTATIVE_ARITH_P (lhs)
9249 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9250 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9251 else if (COMMUTATIVE_ARITH_P (lhs)
9252 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9253 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9254 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9255 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9256 else
9257 return x;
9258
9259 /* Form the new inner operation, seeing if it simplifies first. */
9260 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9261
9262 /* There is one exception to the general way of distributing:
9263 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9264 if (code == XOR && inner_code == IOR)
9265 {
9266 inner_code = AND;
9267 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9268 }
9269
9270 /* We may be able to continuing distributing the result, so call
9271 ourselves recursively on the inner operation before forming the
9272 outer operation, which we return. */
9273 return simplify_gen_binary (inner_code, GET_MODE (x),
9274 apply_distributive_law (tem), other);
9275 }
9276
9277 /* See if X is of the form (* (+ A B) C), and if so convert to
9278 (+ (* A C) (* B C)) and try to simplify.
9279
9280 Most of the time, this results in no change. However, if some of
9281 the operands are the same or inverses of each other, simplifications
9282 will result.
9283
9284 For example, (and (ior A B) (not B)) can occur as the result of
9285 expanding a bit field assignment. When we apply the distributive
9286 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9287 which then simplifies to (and (A (not B))).
9288
9289 Note that no checks happen on the validity of applying the inverse
9290 distributive law. This is pointless since we can do it in the
9291 few places where this routine is called.
9292
9293 N is the index of the term that is decomposed (the arithmetic operation,
9294 i.e. (+ A B) in the first example above). !N is the index of the term that
9295 is distributed, i.e. of C in the first example above. */
9296 static rtx
9297 distribute_and_simplify_rtx (rtx x, int n)
9298 {
9299 enum machine_mode mode;
9300 enum rtx_code outer_code, inner_code;
9301 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9302
9303 /* Distributivity is not true for floating point as it can change the
9304 value. So we don't do it unless -funsafe-math-optimizations. */
9305 if (FLOAT_MODE_P (GET_MODE (x))
9306 && ! flag_unsafe_math_optimizations)
9307 return NULL_RTX;
9308
9309 decomposed = XEXP (x, n);
9310 if (!ARITHMETIC_P (decomposed))
9311 return NULL_RTX;
9312
9313 mode = GET_MODE (x);
9314 outer_code = GET_CODE (x);
9315 distributed = XEXP (x, !n);
9316
9317 inner_code = GET_CODE (decomposed);
9318 inner_op0 = XEXP (decomposed, 0);
9319 inner_op1 = XEXP (decomposed, 1);
9320
9321 /* Special case (and (xor B C) (not A)), which is equivalent to
9322 (xor (ior A B) (ior A C)) */
9323 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9324 {
9325 distributed = XEXP (distributed, 0);
9326 outer_code = IOR;
9327 }
9328
9329 if (n == 0)
9330 {
9331 /* Distribute the second term. */
9332 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9333 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9334 }
9335 else
9336 {
9337 /* Distribute the first term. */
9338 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9339 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9340 }
9341
9342 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9343 new_op0, new_op1));
9344 if (GET_CODE (tmp) != outer_code
9345 && (set_src_cost (tmp, optimize_this_for_speed_p)
9346 < set_src_cost (x, optimize_this_for_speed_p)))
9347 return tmp;
9348
9349 return NULL_RTX;
9350 }
9351 \f
9352 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9353 in MODE. Return an equivalent form, if different from (and VAROP
9354 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9355
9356 static rtx
9357 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
9358 unsigned HOST_WIDE_INT constop)
9359 {
9360 unsigned HOST_WIDE_INT nonzero;
9361 unsigned HOST_WIDE_INT orig_constop;
9362 rtx orig_varop;
9363 int i;
9364
9365 orig_varop = varop;
9366 orig_constop = constop;
9367 if (GET_CODE (varop) == CLOBBER)
9368 return NULL_RTX;
9369
9370 /* Simplify VAROP knowing that we will be only looking at some of the
9371 bits in it.
9372
9373 Note by passing in CONSTOP, we guarantee that the bits not set in
9374 CONSTOP are not significant and will never be examined. We must
9375 ensure that is the case by explicitly masking out those bits
9376 before returning. */
9377 varop = force_to_mode (varop, mode, constop, 0);
9378
9379 /* If VAROP is a CLOBBER, we will fail so return it. */
9380 if (GET_CODE (varop) == CLOBBER)
9381 return varop;
9382
9383 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9384 to VAROP and return the new constant. */
9385 if (CONST_INT_P (varop))
9386 return gen_int_mode (INTVAL (varop) & constop, mode);
9387
9388 /* See what bits may be nonzero in VAROP. Unlike the general case of
9389 a call to nonzero_bits, here we don't care about bits outside
9390 MODE. */
9391
9392 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9393
9394 /* Turn off all bits in the constant that are known to already be zero.
9395 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9396 which is tested below. */
9397
9398 constop &= nonzero;
9399
9400 /* If we don't have any bits left, return zero. */
9401 if (constop == 0)
9402 return const0_rtx;
9403
9404 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9405 a power of two, we can replace this with an ASHIFT. */
9406 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9407 && (i = exact_log2 (constop)) >= 0)
9408 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9409
9410 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9411 or XOR, then try to apply the distributive law. This may eliminate
9412 operations if either branch can be simplified because of the AND.
9413 It may also make some cases more complex, but those cases probably
9414 won't match a pattern either with or without this. */
9415
9416 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9417 return
9418 gen_lowpart
9419 (mode,
9420 apply_distributive_law
9421 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9422 simplify_and_const_int (NULL_RTX,
9423 GET_MODE (varop),
9424 XEXP (varop, 0),
9425 constop),
9426 simplify_and_const_int (NULL_RTX,
9427 GET_MODE (varop),
9428 XEXP (varop, 1),
9429 constop))));
9430
9431 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9432 the AND and see if one of the operands simplifies to zero. If so, we
9433 may eliminate it. */
9434
9435 if (GET_CODE (varop) == PLUS
9436 && exact_log2 (constop + 1) >= 0)
9437 {
9438 rtx o0, o1;
9439
9440 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9441 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9442 if (o0 == const0_rtx)
9443 return o1;
9444 if (o1 == const0_rtx)
9445 return o0;
9446 }
9447
9448 /* Make a SUBREG if necessary. If we can't make it, fail. */
9449 varop = gen_lowpart (mode, varop);
9450 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9451 return NULL_RTX;
9452
9453 /* If we are only masking insignificant bits, return VAROP. */
9454 if (constop == nonzero)
9455 return varop;
9456
9457 if (varop == orig_varop && constop == orig_constop)
9458 return NULL_RTX;
9459
9460 /* Otherwise, return an AND. */
9461 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9462 }
9463
9464
9465 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9466 in MODE.
9467
9468 Return an equivalent form, if different from X. Otherwise, return X. If
9469 X is zero, we are to always construct the equivalent form. */
9470
9471 static rtx
9472 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
9473 unsigned HOST_WIDE_INT constop)
9474 {
9475 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9476 if (tem)
9477 return tem;
9478
9479 if (!x)
9480 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9481 gen_int_mode (constop, mode));
9482 if (GET_MODE (x) != mode)
9483 x = gen_lowpart (mode, x);
9484 return x;
9485 }
9486 \f
9487 /* Given a REG, X, compute which bits in X can be nonzero.
9488 We don't care about bits outside of those defined in MODE.
9489
9490 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9491 a shift, AND, or zero_extract, we can do better. */
9492
9493 static rtx
9494 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
9495 const_rtx known_x ATTRIBUTE_UNUSED,
9496 enum machine_mode known_mode ATTRIBUTE_UNUSED,
9497 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9498 unsigned HOST_WIDE_INT *nonzero)
9499 {
9500 rtx tem;
9501 reg_stat_type *rsp;
9502
9503 /* If X is a register whose nonzero bits value is current, use it.
9504 Otherwise, if X is a register whose value we can find, use that
9505 value. Otherwise, use the previously-computed global nonzero bits
9506 for this register. */
9507
9508 rsp = &reg_stat[REGNO (x)];
9509 if (rsp->last_set_value != 0
9510 && (rsp->last_set_mode == mode
9511 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9512 && GET_MODE_CLASS (mode) == MODE_INT))
9513 && ((rsp->last_set_label >= label_tick_ebb_start
9514 && rsp->last_set_label < label_tick)
9515 || (rsp->last_set_label == label_tick
9516 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9517 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9518 && REG_N_SETS (REGNO (x)) == 1
9519 && !REGNO_REG_SET_P
9520 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9521 REGNO (x)))))
9522 {
9523 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9524
9525 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9526 /* We don't know anything about the upper bits. */
9527 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9528
9529 *nonzero &= mask;
9530 return NULL;
9531 }
9532
9533 tem = get_last_value (x);
9534
9535 if (tem)
9536 {
9537 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9538 /* If X is narrower than MODE and TEM is a non-negative
9539 constant that would appear negative in the mode of X,
9540 sign-extend it for use in reg_nonzero_bits because some
9541 machines (maybe most) will actually do the sign-extension
9542 and this is the conservative approach.
9543
9544 ??? For 2.5, try to tighten up the MD files in this regard
9545 instead of this kludge. */
9546
9547 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9548 && CONST_INT_P (tem)
9549 && INTVAL (tem) > 0
9550 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9551 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9552 #endif
9553 return tem;
9554 }
9555 else if (nonzero_sign_valid && rsp->nonzero_bits)
9556 {
9557 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9558
9559 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9560 /* We don't know anything about the upper bits. */
9561 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9562
9563 *nonzero &= mask;
9564 }
9565
9566 return NULL;
9567 }
9568
9569 /* Return the number of bits at the high-order end of X that are known to
9570 be equal to the sign bit. X will be used in mode MODE; if MODE is
9571 VOIDmode, X will be used in its own mode. The returned value will always
9572 be between 1 and the number of bits in MODE. */
9573
9574 static rtx
9575 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
9576 const_rtx known_x ATTRIBUTE_UNUSED,
9577 enum machine_mode known_mode
9578 ATTRIBUTE_UNUSED,
9579 unsigned int known_ret ATTRIBUTE_UNUSED,
9580 unsigned int *result)
9581 {
9582 rtx tem;
9583 reg_stat_type *rsp;
9584
9585 rsp = &reg_stat[REGNO (x)];
9586 if (rsp->last_set_value != 0
9587 && rsp->last_set_mode == mode
9588 && ((rsp->last_set_label >= label_tick_ebb_start
9589 && rsp->last_set_label < label_tick)
9590 || (rsp->last_set_label == label_tick
9591 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9592 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9593 && REG_N_SETS (REGNO (x)) == 1
9594 && !REGNO_REG_SET_P
9595 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9596 REGNO (x)))))
9597 {
9598 *result = rsp->last_set_sign_bit_copies;
9599 return NULL;
9600 }
9601
9602 tem = get_last_value (x);
9603 if (tem != 0)
9604 return tem;
9605
9606 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9607 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9608 *result = rsp->sign_bit_copies;
9609
9610 return NULL;
9611 }
9612 \f
9613 /* Return the number of "extended" bits there are in X, when interpreted
9614 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9615 unsigned quantities, this is the number of high-order zero bits.
9616 For signed quantities, this is the number of copies of the sign bit
9617 minus 1. In both case, this function returns the number of "spare"
9618 bits. For example, if two quantities for which this function returns
9619 at least 1 are added, the addition is known not to overflow.
9620
9621 This function will always return 0 unless called during combine, which
9622 implies that it must be called from a define_split. */
9623
9624 unsigned int
9625 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
9626 {
9627 if (nonzero_sign_valid == 0)
9628 return 0;
9629
9630 return (unsignedp
9631 ? (HWI_COMPUTABLE_MODE_P (mode)
9632 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9633 - floor_log2 (nonzero_bits (x, mode)))
9634 : 0)
9635 : num_sign_bit_copies (x, mode) - 1);
9636 }
9637
9638 /* This function is called from `simplify_shift_const' to merge two
9639 outer operations. Specifically, we have already found that we need
9640 to perform operation *POP0 with constant *PCONST0 at the outermost
9641 position. We would now like to also perform OP1 with constant CONST1
9642 (with *POP0 being done last).
9643
9644 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9645 the resulting operation. *PCOMP_P is set to 1 if we would need to
9646 complement the innermost operand, otherwise it is unchanged.
9647
9648 MODE is the mode in which the operation will be done. No bits outside
9649 the width of this mode matter. It is assumed that the width of this mode
9650 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9651
9652 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9653 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9654 result is simply *PCONST0.
9655
9656 If the resulting operation cannot be expressed as one operation, we
9657 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9658
9659 static int
9660 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9661 {
9662 enum rtx_code op0 = *pop0;
9663 HOST_WIDE_INT const0 = *pconst0;
9664
9665 const0 &= GET_MODE_MASK (mode);
9666 const1 &= GET_MODE_MASK (mode);
9667
9668 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9669 if (op0 == AND)
9670 const1 &= const0;
9671
9672 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9673 if OP0 is SET. */
9674
9675 if (op1 == UNKNOWN || op0 == SET)
9676 return 1;
9677
9678 else if (op0 == UNKNOWN)
9679 op0 = op1, const0 = const1;
9680
9681 else if (op0 == op1)
9682 {
9683 switch (op0)
9684 {
9685 case AND:
9686 const0 &= const1;
9687 break;
9688 case IOR:
9689 const0 |= const1;
9690 break;
9691 case XOR:
9692 const0 ^= const1;
9693 break;
9694 case PLUS:
9695 const0 += const1;
9696 break;
9697 case NEG:
9698 op0 = UNKNOWN;
9699 break;
9700 default:
9701 break;
9702 }
9703 }
9704
9705 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9706 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9707 return 0;
9708
9709 /* If the two constants aren't the same, we can't do anything. The
9710 remaining six cases can all be done. */
9711 else if (const0 != const1)
9712 return 0;
9713
9714 else
9715 switch (op0)
9716 {
9717 case IOR:
9718 if (op1 == AND)
9719 /* (a & b) | b == b */
9720 op0 = SET;
9721 else /* op1 == XOR */
9722 /* (a ^ b) | b == a | b */
9723 {;}
9724 break;
9725
9726 case XOR:
9727 if (op1 == AND)
9728 /* (a & b) ^ b == (~a) & b */
9729 op0 = AND, *pcomp_p = 1;
9730 else /* op1 == IOR */
9731 /* (a | b) ^ b == a & ~b */
9732 op0 = AND, const0 = ~const0;
9733 break;
9734
9735 case AND:
9736 if (op1 == IOR)
9737 /* (a | b) & b == b */
9738 op0 = SET;
9739 else /* op1 == XOR */
9740 /* (a ^ b) & b) == (~a) & b */
9741 *pcomp_p = 1;
9742 break;
9743 default:
9744 break;
9745 }
9746
9747 /* Check for NO-OP cases. */
9748 const0 &= GET_MODE_MASK (mode);
9749 if (const0 == 0
9750 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9751 op0 = UNKNOWN;
9752 else if (const0 == 0 && op0 == AND)
9753 op0 = SET;
9754 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9755 && op0 == AND)
9756 op0 = UNKNOWN;
9757
9758 *pop0 = op0;
9759
9760 /* ??? Slightly redundant with the above mask, but not entirely.
9761 Moving this above means we'd have to sign-extend the mode mask
9762 for the final test. */
9763 if (op0 != UNKNOWN && op0 != NEG)
9764 *pconst0 = trunc_int_for_mode (const0, mode);
9765
9766 return 1;
9767 }
9768 \f
9769 /* A helper to simplify_shift_const_1 to determine the mode we can perform
9770 the shift in. The original shift operation CODE is performed on OP in
9771 ORIG_MODE. Return the wider mode MODE if we can perform the operation
9772 in that mode. Return ORIG_MODE otherwise. We can also assume that the
9773 result of the shift is subject to operation OUTER_CODE with operand
9774 OUTER_CONST. */
9775
9776 static enum machine_mode
9777 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
9778 enum machine_mode orig_mode, enum machine_mode mode,
9779 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
9780 {
9781 if (orig_mode == mode)
9782 return mode;
9783 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
9784
9785 /* In general we can't perform in wider mode for right shift and rotate. */
9786 switch (code)
9787 {
9788 case ASHIFTRT:
9789 /* We can still widen if the bits brought in from the left are identical
9790 to the sign bit of ORIG_MODE. */
9791 if (num_sign_bit_copies (op, mode)
9792 > (unsigned) (GET_MODE_PRECISION (mode)
9793 - GET_MODE_PRECISION (orig_mode)))
9794 return mode;
9795 return orig_mode;
9796
9797 case LSHIFTRT:
9798 /* Similarly here but with zero bits. */
9799 if (HWI_COMPUTABLE_MODE_P (mode)
9800 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
9801 return mode;
9802
9803 /* We can also widen if the bits brought in will be masked off. This
9804 operation is performed in ORIG_MODE. */
9805 if (outer_code == AND)
9806 {
9807 int care_bits = low_bitmask_len (orig_mode, outer_const);
9808
9809 if (care_bits >= 0
9810 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
9811 return mode;
9812 }
9813 /* fall through */
9814
9815 case ROTATE:
9816 return orig_mode;
9817
9818 case ROTATERT:
9819 gcc_unreachable ();
9820
9821 default:
9822 return mode;
9823 }
9824 }
9825
9826 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
9827 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
9828 if we cannot simplify it. Otherwise, return a simplified value.
9829
9830 The shift is normally computed in the widest mode we find in VAROP, as
9831 long as it isn't a different number of words than RESULT_MODE. Exceptions
9832 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9833
9834 static rtx
9835 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
9836 rtx varop, int orig_count)
9837 {
9838 enum rtx_code orig_code = code;
9839 rtx orig_varop = varop;
9840 int count;
9841 enum machine_mode mode = result_mode;
9842 enum machine_mode shift_mode, tmode;
9843 unsigned int mode_words
9844 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9845 /* We form (outer_op (code varop count) (outer_const)). */
9846 enum rtx_code outer_op = UNKNOWN;
9847 HOST_WIDE_INT outer_const = 0;
9848 int complement_p = 0;
9849 rtx new_rtx, x;
9850
9851 /* Make sure and truncate the "natural" shift on the way in. We don't
9852 want to do this inside the loop as it makes it more difficult to
9853 combine shifts. */
9854 if (SHIFT_COUNT_TRUNCATED)
9855 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9856
9857 /* If we were given an invalid count, don't do anything except exactly
9858 what was requested. */
9859
9860 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
9861 return NULL_RTX;
9862
9863 count = orig_count;
9864
9865 /* Unless one of the branches of the `if' in this loop does a `continue',
9866 we will `break' the loop after the `if'. */
9867
9868 while (count != 0)
9869 {
9870 /* If we have an operand of (clobber (const_int 0)), fail. */
9871 if (GET_CODE (varop) == CLOBBER)
9872 return NULL_RTX;
9873
9874 /* Convert ROTATERT to ROTATE. */
9875 if (code == ROTATERT)
9876 {
9877 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
9878 code = ROTATE;
9879 if (VECTOR_MODE_P (result_mode))
9880 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9881 else
9882 count = bitsize - count;
9883 }
9884
9885 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
9886 mode, outer_op, outer_const);
9887
9888 /* Handle cases where the count is greater than the size of the mode
9889 minus 1. For ASHIFT, use the size minus one as the count (this can
9890 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9891 take the count modulo the size. For other shifts, the result is
9892 zero.
9893
9894 Since these shifts are being produced by the compiler by combining
9895 multiple operations, each of which are defined, we know what the
9896 result is supposed to be. */
9897
9898 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
9899 {
9900 if (code == ASHIFTRT)
9901 count = GET_MODE_PRECISION (shift_mode) - 1;
9902 else if (code == ROTATE || code == ROTATERT)
9903 count %= GET_MODE_PRECISION (shift_mode);
9904 else
9905 {
9906 /* We can't simply return zero because there may be an
9907 outer op. */
9908 varop = const0_rtx;
9909 count = 0;
9910 break;
9911 }
9912 }
9913
9914 /* If we discovered we had to complement VAROP, leave. Making a NOT
9915 here would cause an infinite loop. */
9916 if (complement_p)
9917 break;
9918
9919 /* An arithmetic right shift of a quantity known to be -1 or 0
9920 is a no-op. */
9921 if (code == ASHIFTRT
9922 && (num_sign_bit_copies (varop, shift_mode)
9923 == GET_MODE_PRECISION (shift_mode)))
9924 {
9925 count = 0;
9926 break;
9927 }
9928
9929 /* If we are doing an arithmetic right shift and discarding all but
9930 the sign bit copies, this is equivalent to doing a shift by the
9931 bitsize minus one. Convert it into that shift because it will often
9932 allow other simplifications. */
9933
9934 if (code == ASHIFTRT
9935 && (count + num_sign_bit_copies (varop, shift_mode)
9936 >= GET_MODE_PRECISION (shift_mode)))
9937 count = GET_MODE_PRECISION (shift_mode) - 1;
9938
9939 /* We simplify the tests below and elsewhere by converting
9940 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9941 `make_compound_operation' will convert it to an ASHIFTRT for
9942 those machines (such as VAX) that don't have an LSHIFTRT. */
9943 if (code == ASHIFTRT
9944 && val_signbit_known_clear_p (shift_mode,
9945 nonzero_bits (varop, shift_mode)))
9946 code = LSHIFTRT;
9947
9948 if (((code == LSHIFTRT
9949 && HWI_COMPUTABLE_MODE_P (shift_mode)
9950 && !(nonzero_bits (varop, shift_mode) >> count))
9951 || (code == ASHIFT
9952 && HWI_COMPUTABLE_MODE_P (shift_mode)
9953 && !((nonzero_bits (varop, shift_mode) << count)
9954 & GET_MODE_MASK (shift_mode))))
9955 && !side_effects_p (varop))
9956 varop = const0_rtx;
9957
9958 switch (GET_CODE (varop))
9959 {
9960 case SIGN_EXTEND:
9961 case ZERO_EXTEND:
9962 case SIGN_EXTRACT:
9963 case ZERO_EXTRACT:
9964 new_rtx = expand_compound_operation (varop);
9965 if (new_rtx != varop)
9966 {
9967 varop = new_rtx;
9968 continue;
9969 }
9970 break;
9971
9972 case MEM:
9973 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9974 minus the width of a smaller mode, we can do this with a
9975 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9976 if ((code == ASHIFTRT || code == LSHIFTRT)
9977 && ! mode_dependent_address_p (XEXP (varop, 0),
9978 MEM_ADDR_SPACE (varop))
9979 && ! MEM_VOLATILE_P (varop)
9980 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9981 MODE_INT, 1)) != BLKmode)
9982 {
9983 new_rtx = adjust_address_nv (varop, tmode,
9984 BYTES_BIG_ENDIAN ? 0
9985 : count / BITS_PER_UNIT);
9986
9987 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9988 : ZERO_EXTEND, mode, new_rtx);
9989 count = 0;
9990 continue;
9991 }
9992 break;
9993
9994 case SUBREG:
9995 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9996 the same number of words as what we've seen so far. Then store
9997 the widest mode in MODE. */
9998 if (subreg_lowpart_p (varop)
9999 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10000 > GET_MODE_SIZE (GET_MODE (varop)))
10001 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10002 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10003 == mode_words
10004 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10005 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10006 {
10007 varop = SUBREG_REG (varop);
10008 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10009 mode = GET_MODE (varop);
10010 continue;
10011 }
10012 break;
10013
10014 case MULT:
10015 /* Some machines use MULT instead of ASHIFT because MULT
10016 is cheaper. But it is still better on those machines to
10017 merge two shifts into one. */
10018 if (CONST_INT_P (XEXP (varop, 1))
10019 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10020 {
10021 varop
10022 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10023 XEXP (varop, 0),
10024 GEN_INT (exact_log2 (
10025 UINTVAL (XEXP (varop, 1)))));
10026 continue;
10027 }
10028 break;
10029
10030 case UDIV:
10031 /* Similar, for when divides are cheaper. */
10032 if (CONST_INT_P (XEXP (varop, 1))
10033 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10034 {
10035 varop
10036 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10037 XEXP (varop, 0),
10038 GEN_INT (exact_log2 (
10039 UINTVAL (XEXP (varop, 1)))));
10040 continue;
10041 }
10042 break;
10043
10044 case ASHIFTRT:
10045 /* If we are extracting just the sign bit of an arithmetic
10046 right shift, that shift is not needed. However, the sign
10047 bit of a wider mode may be different from what would be
10048 interpreted as the sign bit in a narrower mode, so, if
10049 the result is narrower, don't discard the shift. */
10050 if (code == LSHIFTRT
10051 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10052 && (GET_MODE_BITSIZE (result_mode)
10053 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10054 {
10055 varop = XEXP (varop, 0);
10056 continue;
10057 }
10058
10059 /* ... fall through ... */
10060
10061 case LSHIFTRT:
10062 case ASHIFT:
10063 case ROTATE:
10064 /* Here we have two nested shifts. The result is usually the
10065 AND of a new shift with a mask. We compute the result below. */
10066 if (CONST_INT_P (XEXP (varop, 1))
10067 && INTVAL (XEXP (varop, 1)) >= 0
10068 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10069 && HWI_COMPUTABLE_MODE_P (result_mode)
10070 && HWI_COMPUTABLE_MODE_P (mode)
10071 && !VECTOR_MODE_P (result_mode))
10072 {
10073 enum rtx_code first_code = GET_CODE (varop);
10074 unsigned int first_count = INTVAL (XEXP (varop, 1));
10075 unsigned HOST_WIDE_INT mask;
10076 rtx mask_rtx;
10077
10078 /* We have one common special case. We can't do any merging if
10079 the inner code is an ASHIFTRT of a smaller mode. However, if
10080 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10081 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10082 we can convert it to
10083 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10084 This simplifies certain SIGN_EXTEND operations. */
10085 if (code == ASHIFT && first_code == ASHIFTRT
10086 && count == (GET_MODE_PRECISION (result_mode)
10087 - GET_MODE_PRECISION (GET_MODE (varop))))
10088 {
10089 /* C3 has the low-order C1 bits zero. */
10090
10091 mask = GET_MODE_MASK (mode)
10092 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10093
10094 varop = simplify_and_const_int (NULL_RTX, result_mode,
10095 XEXP (varop, 0), mask);
10096 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10097 varop, count);
10098 count = first_count;
10099 code = ASHIFTRT;
10100 continue;
10101 }
10102
10103 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10104 than C1 high-order bits equal to the sign bit, we can convert
10105 this to either an ASHIFT or an ASHIFTRT depending on the
10106 two counts.
10107
10108 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10109
10110 if (code == ASHIFTRT && first_code == ASHIFT
10111 && GET_MODE (varop) == shift_mode
10112 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10113 > first_count))
10114 {
10115 varop = XEXP (varop, 0);
10116 count -= first_count;
10117 if (count < 0)
10118 {
10119 count = -count;
10120 code = ASHIFT;
10121 }
10122
10123 continue;
10124 }
10125
10126 /* There are some cases we can't do. If CODE is ASHIFTRT,
10127 we can only do this if FIRST_CODE is also ASHIFTRT.
10128
10129 We can't do the case when CODE is ROTATE and FIRST_CODE is
10130 ASHIFTRT.
10131
10132 If the mode of this shift is not the mode of the outer shift,
10133 we can't do this if either shift is a right shift or ROTATE.
10134
10135 Finally, we can't do any of these if the mode is too wide
10136 unless the codes are the same.
10137
10138 Handle the case where the shift codes are the same
10139 first. */
10140
10141 if (code == first_code)
10142 {
10143 if (GET_MODE (varop) != result_mode
10144 && (code == ASHIFTRT || code == LSHIFTRT
10145 || code == ROTATE))
10146 break;
10147
10148 count += first_count;
10149 varop = XEXP (varop, 0);
10150 continue;
10151 }
10152
10153 if (code == ASHIFTRT
10154 || (code == ROTATE && first_code == ASHIFTRT)
10155 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10156 || (GET_MODE (varop) != result_mode
10157 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10158 || first_code == ROTATE
10159 || code == ROTATE)))
10160 break;
10161
10162 /* To compute the mask to apply after the shift, shift the
10163 nonzero bits of the inner shift the same way the
10164 outer shift will. */
10165
10166 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10167 result_mode);
10168
10169 mask_rtx
10170 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10171 GEN_INT (count));
10172
10173 /* Give up if we can't compute an outer operation to use. */
10174 if (mask_rtx == 0
10175 || !CONST_INT_P (mask_rtx)
10176 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10177 INTVAL (mask_rtx),
10178 result_mode, &complement_p))
10179 break;
10180
10181 /* If the shifts are in the same direction, we add the
10182 counts. Otherwise, we subtract them. */
10183 if ((code == ASHIFTRT || code == LSHIFTRT)
10184 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10185 count += first_count;
10186 else
10187 count -= first_count;
10188
10189 /* If COUNT is positive, the new shift is usually CODE,
10190 except for the two exceptions below, in which case it is
10191 FIRST_CODE. If the count is negative, FIRST_CODE should
10192 always be used */
10193 if (count > 0
10194 && ((first_code == ROTATE && code == ASHIFT)
10195 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10196 code = first_code;
10197 else if (count < 0)
10198 code = first_code, count = -count;
10199
10200 varop = XEXP (varop, 0);
10201 continue;
10202 }
10203
10204 /* If we have (A << B << C) for any shift, we can convert this to
10205 (A << C << B). This wins if A is a constant. Only try this if
10206 B is not a constant. */
10207
10208 else if (GET_CODE (varop) == code
10209 && CONST_INT_P (XEXP (varop, 0))
10210 && !CONST_INT_P (XEXP (varop, 1)))
10211 {
10212 rtx new_rtx = simplify_const_binary_operation (code, mode,
10213 XEXP (varop, 0),
10214 GEN_INT (count));
10215 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10216 count = 0;
10217 continue;
10218 }
10219 break;
10220
10221 case NOT:
10222 if (VECTOR_MODE_P (mode))
10223 break;
10224
10225 /* Make this fit the case below. */
10226 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10227 continue;
10228
10229 case IOR:
10230 case AND:
10231 case XOR:
10232 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10233 with C the size of VAROP - 1 and the shift is logical if
10234 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10235 we have an (le X 0) operation. If we have an arithmetic shift
10236 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10237 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10238
10239 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10240 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10241 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10242 && (code == LSHIFTRT || code == ASHIFTRT)
10243 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10244 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10245 {
10246 count = 0;
10247 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10248 const0_rtx);
10249
10250 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10251 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10252
10253 continue;
10254 }
10255
10256 /* If we have (shift (logical)), move the logical to the outside
10257 to allow it to possibly combine with another logical and the
10258 shift to combine with another shift. This also canonicalizes to
10259 what a ZERO_EXTRACT looks like. Also, some machines have
10260 (and (shift)) insns. */
10261
10262 if (CONST_INT_P (XEXP (varop, 1))
10263 /* We can't do this if we have (ashiftrt (xor)) and the
10264 constant has its sign bit set in shift_mode with shift_mode
10265 wider than result_mode. */
10266 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10267 && result_mode != shift_mode
10268 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10269 shift_mode))
10270 && (new_rtx = simplify_const_binary_operation
10271 (code, result_mode,
10272 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10273 GEN_INT (count))) != 0
10274 && CONST_INT_P (new_rtx)
10275 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10276 INTVAL (new_rtx), result_mode, &complement_p))
10277 {
10278 varop = XEXP (varop, 0);
10279 continue;
10280 }
10281
10282 /* If we can't do that, try to simplify the shift in each arm of the
10283 logical expression, make a new logical expression, and apply
10284 the inverse distributive law. This also can't be done for
10285 (ashiftrt (xor)) where we've widened the shift and the constant
10286 changes the sign bit. */
10287 if (CONST_INT_P (XEXP (varop, 1))
10288 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10289 && result_mode != shift_mode
10290 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10291 shift_mode)))
10292 {
10293 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10294 XEXP (varop, 0), count);
10295 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10296 XEXP (varop, 1), count);
10297
10298 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10299 lhs, rhs);
10300 varop = apply_distributive_law (varop);
10301
10302 count = 0;
10303 continue;
10304 }
10305 break;
10306
10307 case EQ:
10308 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10309 says that the sign bit can be tested, FOO has mode MODE, C is
10310 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10311 that may be nonzero. */
10312 if (code == LSHIFTRT
10313 && XEXP (varop, 1) == const0_rtx
10314 && GET_MODE (XEXP (varop, 0)) == result_mode
10315 && count == (GET_MODE_PRECISION (result_mode) - 1)
10316 && HWI_COMPUTABLE_MODE_P (result_mode)
10317 && STORE_FLAG_VALUE == -1
10318 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10319 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10320 &complement_p))
10321 {
10322 varop = XEXP (varop, 0);
10323 count = 0;
10324 continue;
10325 }
10326 break;
10327
10328 case NEG:
10329 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10330 than the number of bits in the mode is equivalent to A. */
10331 if (code == LSHIFTRT
10332 && count == (GET_MODE_PRECISION (result_mode) - 1)
10333 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10334 {
10335 varop = XEXP (varop, 0);
10336 count = 0;
10337 continue;
10338 }
10339
10340 /* NEG commutes with ASHIFT since it is multiplication. Move the
10341 NEG outside to allow shifts to combine. */
10342 if (code == ASHIFT
10343 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10344 &complement_p))
10345 {
10346 varop = XEXP (varop, 0);
10347 continue;
10348 }
10349 break;
10350
10351 case PLUS:
10352 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10353 is one less than the number of bits in the mode is
10354 equivalent to (xor A 1). */
10355 if (code == LSHIFTRT
10356 && count == (GET_MODE_PRECISION (result_mode) - 1)
10357 && XEXP (varop, 1) == constm1_rtx
10358 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10359 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10360 &complement_p))
10361 {
10362 count = 0;
10363 varop = XEXP (varop, 0);
10364 continue;
10365 }
10366
10367 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10368 that might be nonzero in BAR are those being shifted out and those
10369 bits are known zero in FOO, we can replace the PLUS with FOO.
10370 Similarly in the other operand order. This code occurs when
10371 we are computing the size of a variable-size array. */
10372
10373 if ((code == ASHIFTRT || code == LSHIFTRT)
10374 && count < HOST_BITS_PER_WIDE_INT
10375 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10376 && (nonzero_bits (XEXP (varop, 1), result_mode)
10377 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10378 {
10379 varop = XEXP (varop, 0);
10380 continue;
10381 }
10382 else if ((code == ASHIFTRT || code == LSHIFTRT)
10383 && count < HOST_BITS_PER_WIDE_INT
10384 && HWI_COMPUTABLE_MODE_P (result_mode)
10385 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10386 >> count)
10387 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10388 & nonzero_bits (XEXP (varop, 1),
10389 result_mode)))
10390 {
10391 varop = XEXP (varop, 1);
10392 continue;
10393 }
10394
10395 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10396 if (code == ASHIFT
10397 && CONST_INT_P (XEXP (varop, 1))
10398 && (new_rtx = simplify_const_binary_operation
10399 (ASHIFT, result_mode,
10400 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10401 GEN_INT (count))) != 0
10402 && CONST_INT_P (new_rtx)
10403 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10404 INTVAL (new_rtx), result_mode, &complement_p))
10405 {
10406 varop = XEXP (varop, 0);
10407 continue;
10408 }
10409
10410 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10411 signbit', and attempt to change the PLUS to an XOR and move it to
10412 the outer operation as is done above in the AND/IOR/XOR case
10413 leg for shift(logical). See details in logical handling above
10414 for reasoning in doing so. */
10415 if (code == LSHIFTRT
10416 && CONST_INT_P (XEXP (varop, 1))
10417 && mode_signbit_p (result_mode, XEXP (varop, 1))
10418 && (new_rtx = simplify_const_binary_operation
10419 (code, result_mode,
10420 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10421 GEN_INT (count))) != 0
10422 && CONST_INT_P (new_rtx)
10423 && merge_outer_ops (&outer_op, &outer_const, XOR,
10424 INTVAL (new_rtx), result_mode, &complement_p))
10425 {
10426 varop = XEXP (varop, 0);
10427 continue;
10428 }
10429
10430 break;
10431
10432 case MINUS:
10433 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10434 with C the size of VAROP - 1 and the shift is logical if
10435 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10436 we have a (gt X 0) operation. If the shift is arithmetic with
10437 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10438 we have a (neg (gt X 0)) operation. */
10439
10440 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10441 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10442 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10443 && (code == LSHIFTRT || code == ASHIFTRT)
10444 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10445 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10446 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10447 {
10448 count = 0;
10449 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10450 const0_rtx);
10451
10452 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10453 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10454
10455 continue;
10456 }
10457 break;
10458
10459 case TRUNCATE:
10460 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10461 if the truncate does not affect the value. */
10462 if (code == LSHIFTRT
10463 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10464 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10465 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10466 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10467 - GET_MODE_PRECISION (GET_MODE (varop)))))
10468 {
10469 rtx varop_inner = XEXP (varop, 0);
10470
10471 varop_inner
10472 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10473 XEXP (varop_inner, 0),
10474 GEN_INT
10475 (count + INTVAL (XEXP (varop_inner, 1))));
10476 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10477 count = 0;
10478 continue;
10479 }
10480 break;
10481
10482 default:
10483 break;
10484 }
10485
10486 break;
10487 }
10488
10489 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10490 outer_op, outer_const);
10491
10492 /* We have now finished analyzing the shift. The result should be
10493 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10494 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10495 to the result of the shift. OUTER_CONST is the relevant constant,
10496 but we must turn off all bits turned off in the shift. */
10497
10498 if (outer_op == UNKNOWN
10499 && orig_code == code && orig_count == count
10500 && varop == orig_varop
10501 && shift_mode == GET_MODE (varop))
10502 return NULL_RTX;
10503
10504 /* Make a SUBREG if necessary. If we can't make it, fail. */
10505 varop = gen_lowpart (shift_mode, varop);
10506 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10507 return NULL_RTX;
10508
10509 /* If we have an outer operation and we just made a shift, it is
10510 possible that we could have simplified the shift were it not
10511 for the outer operation. So try to do the simplification
10512 recursively. */
10513
10514 if (outer_op != UNKNOWN)
10515 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10516 else
10517 x = NULL_RTX;
10518
10519 if (x == NULL_RTX)
10520 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10521
10522 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10523 turn off all the bits that the shift would have turned off. */
10524 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10525 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10526 GET_MODE_MASK (result_mode) >> orig_count);
10527
10528 /* Do the remainder of the processing in RESULT_MODE. */
10529 x = gen_lowpart_or_truncate (result_mode, x);
10530
10531 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10532 operation. */
10533 if (complement_p)
10534 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10535
10536 if (outer_op != UNKNOWN)
10537 {
10538 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10539 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10540 outer_const = trunc_int_for_mode (outer_const, result_mode);
10541
10542 if (outer_op == AND)
10543 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10544 else if (outer_op == SET)
10545 {
10546 /* This means that we have determined that the result is
10547 equivalent to a constant. This should be rare. */
10548 if (!side_effects_p (x))
10549 x = GEN_INT (outer_const);
10550 }
10551 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10552 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10553 else
10554 x = simplify_gen_binary (outer_op, result_mode, x,
10555 GEN_INT (outer_const));
10556 }
10557
10558 return x;
10559 }
10560
10561 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10562 The result of the shift is RESULT_MODE. If we cannot simplify it,
10563 return X or, if it is NULL, synthesize the expression with
10564 simplify_gen_binary. Otherwise, return a simplified value.
10565
10566 The shift is normally computed in the widest mode we find in VAROP, as
10567 long as it isn't a different number of words than RESULT_MODE. Exceptions
10568 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10569
10570 static rtx
10571 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
10572 rtx varop, int count)
10573 {
10574 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10575 if (tem)
10576 return tem;
10577
10578 if (!x)
10579 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10580 if (GET_MODE (x) != result_mode)
10581 x = gen_lowpart (result_mode, x);
10582 return x;
10583 }
10584
10585 \f
10586 /* Like recog, but we receive the address of a pointer to a new pattern.
10587 We try to match the rtx that the pointer points to.
10588 If that fails, we may try to modify or replace the pattern,
10589 storing the replacement into the same pointer object.
10590
10591 Modifications include deletion or addition of CLOBBERs.
10592
10593 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10594 the CLOBBERs are placed.
10595
10596 The value is the final insn code from the pattern ultimately matched,
10597 or -1. */
10598
10599 static int
10600 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10601 {
10602 rtx pat = *pnewpat;
10603 rtx pat_without_clobbers;
10604 int insn_code_number;
10605 int num_clobbers_to_add = 0;
10606 int i;
10607 rtx notes = NULL_RTX;
10608 rtx old_notes, old_pat;
10609 int old_icode;
10610
10611 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10612 we use to indicate that something didn't match. If we find such a
10613 thing, force rejection. */
10614 if (GET_CODE (pat) == PARALLEL)
10615 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10616 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10617 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10618 return -1;
10619
10620 old_pat = PATTERN (insn);
10621 old_notes = REG_NOTES (insn);
10622 PATTERN (insn) = pat;
10623 REG_NOTES (insn) = NULL_RTX;
10624
10625 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10626 if (dump_file && (dump_flags & TDF_DETAILS))
10627 {
10628 if (insn_code_number < 0)
10629 fputs ("Failed to match this instruction:\n", dump_file);
10630 else
10631 fputs ("Successfully matched this instruction:\n", dump_file);
10632 print_rtl_single (dump_file, pat);
10633 }
10634
10635 /* If it isn't, there is the possibility that we previously had an insn
10636 that clobbered some register as a side effect, but the combined
10637 insn doesn't need to do that. So try once more without the clobbers
10638 unless this represents an ASM insn. */
10639
10640 if (insn_code_number < 0 && ! check_asm_operands (pat)
10641 && GET_CODE (pat) == PARALLEL)
10642 {
10643 int pos;
10644
10645 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10646 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10647 {
10648 if (i != pos)
10649 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10650 pos++;
10651 }
10652
10653 SUBST_INT (XVECLEN (pat, 0), pos);
10654
10655 if (pos == 1)
10656 pat = XVECEXP (pat, 0, 0);
10657
10658 PATTERN (insn) = pat;
10659 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10660 if (dump_file && (dump_flags & TDF_DETAILS))
10661 {
10662 if (insn_code_number < 0)
10663 fputs ("Failed to match this instruction:\n", dump_file);
10664 else
10665 fputs ("Successfully matched this instruction:\n", dump_file);
10666 print_rtl_single (dump_file, pat);
10667 }
10668 }
10669
10670 pat_without_clobbers = pat;
10671
10672 PATTERN (insn) = old_pat;
10673 REG_NOTES (insn) = old_notes;
10674
10675 /* Recognize all noop sets, these will be killed by followup pass. */
10676 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10677 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10678
10679 /* If we had any clobbers to add, make a new pattern than contains
10680 them. Then check to make sure that all of them are dead. */
10681 if (num_clobbers_to_add)
10682 {
10683 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10684 rtvec_alloc (GET_CODE (pat) == PARALLEL
10685 ? (XVECLEN (pat, 0)
10686 + num_clobbers_to_add)
10687 : num_clobbers_to_add + 1));
10688
10689 if (GET_CODE (pat) == PARALLEL)
10690 for (i = 0; i < XVECLEN (pat, 0); i++)
10691 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10692 else
10693 XVECEXP (newpat, 0, 0) = pat;
10694
10695 add_clobbers (newpat, insn_code_number);
10696
10697 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10698 i < XVECLEN (newpat, 0); i++)
10699 {
10700 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10701 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10702 return -1;
10703 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10704 {
10705 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10706 notes = alloc_reg_note (REG_UNUSED,
10707 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10708 }
10709 }
10710 pat = newpat;
10711 }
10712
10713 if (insn_code_number >= 0
10714 && insn_code_number != NOOP_MOVE_INSN_CODE)
10715 {
10716 old_pat = PATTERN (insn);
10717 old_notes = REG_NOTES (insn);
10718 old_icode = INSN_CODE (insn);
10719 PATTERN (insn) = pat;
10720 REG_NOTES (insn) = notes;
10721
10722 /* Allow targets to reject combined insn. */
10723 if (!targetm.legitimate_combined_insn (insn))
10724 {
10725 if (dump_file && (dump_flags & TDF_DETAILS))
10726 fputs ("Instruction not appropriate for target.",
10727 dump_file);
10728
10729 /* Callers expect recog_for_combine to strip
10730 clobbers from the pattern on failure. */
10731 pat = pat_without_clobbers;
10732 notes = NULL_RTX;
10733
10734 insn_code_number = -1;
10735 }
10736
10737 PATTERN (insn) = old_pat;
10738 REG_NOTES (insn) = old_notes;
10739 INSN_CODE (insn) = old_icode;
10740 }
10741
10742 *pnewpat = pat;
10743 *pnotes = notes;
10744
10745 return insn_code_number;
10746 }
10747 \f
10748 /* Like gen_lowpart_general but for use by combine. In combine it
10749 is not possible to create any new pseudoregs. However, it is
10750 safe to create invalid memory addresses, because combine will
10751 try to recognize them and all they will do is make the combine
10752 attempt fail.
10753
10754 If for some reason this cannot do its job, an rtx
10755 (clobber (const_int 0)) is returned.
10756 An insn containing that will not be recognized. */
10757
10758 static rtx
10759 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
10760 {
10761 enum machine_mode imode = GET_MODE (x);
10762 unsigned int osize = GET_MODE_SIZE (omode);
10763 unsigned int isize = GET_MODE_SIZE (imode);
10764 rtx result;
10765
10766 if (omode == imode)
10767 return x;
10768
10769 /* We can only support MODE being wider than a word if X is a
10770 constant integer or has a mode the same size. */
10771 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
10772 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
10773 goto fail;
10774
10775 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10776 won't know what to do. So we will strip off the SUBREG here and
10777 process normally. */
10778 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
10779 {
10780 x = SUBREG_REG (x);
10781
10782 /* For use in case we fall down into the address adjustments
10783 further below, we need to adjust the known mode and size of
10784 x; imode and isize, since we just adjusted x. */
10785 imode = GET_MODE (x);
10786
10787 if (imode == omode)
10788 return x;
10789
10790 isize = GET_MODE_SIZE (imode);
10791 }
10792
10793 result = gen_lowpart_common (omode, x);
10794
10795 if (result)
10796 return result;
10797
10798 if (MEM_P (x))
10799 {
10800 int offset = 0;
10801
10802 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10803 address. */
10804 if (MEM_VOLATILE_P (x)
10805 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
10806 goto fail;
10807
10808 /* If we want to refer to something bigger than the original memref,
10809 generate a paradoxical subreg instead. That will force a reload
10810 of the original memref X. */
10811 if (isize < osize)
10812 return gen_rtx_SUBREG (omode, x, 0);
10813
10814 if (WORDS_BIG_ENDIAN)
10815 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
10816
10817 /* Adjust the address so that the address-after-the-data is
10818 unchanged. */
10819 if (BYTES_BIG_ENDIAN)
10820 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
10821
10822 return adjust_address_nv (x, omode, offset);
10823 }
10824
10825 /* If X is a comparison operator, rewrite it in a new mode. This
10826 probably won't match, but may allow further simplifications. */
10827 else if (COMPARISON_P (x))
10828 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
10829
10830 /* If we couldn't simplify X any other way, just enclose it in a
10831 SUBREG. Normally, this SUBREG won't match, but some patterns may
10832 include an explicit SUBREG or we may simplify it further in combine. */
10833 else
10834 {
10835 int offset = 0;
10836 rtx res;
10837
10838 offset = subreg_lowpart_offset (omode, imode);
10839 if (imode == VOIDmode)
10840 {
10841 imode = int_mode_for_mode (omode);
10842 x = gen_lowpart_common (imode, x);
10843 if (x == NULL)
10844 goto fail;
10845 }
10846 res = simplify_gen_subreg (omode, x, imode, offset);
10847 if (res)
10848 return res;
10849 }
10850
10851 fail:
10852 return gen_rtx_CLOBBER (omode, const0_rtx);
10853 }
10854 \f
10855 /* Try to simplify a comparison between OP0 and a constant OP1,
10856 where CODE is the comparison code that will be tested, into a
10857 (CODE OP0 const0_rtx) form.
10858
10859 The result is a possibly different comparison code to use.
10860 *POP1 may be updated. */
10861
10862 static enum rtx_code
10863 simplify_compare_const (enum rtx_code code, enum machine_mode mode,
10864 rtx op0, rtx *pop1)
10865 {
10866 unsigned int mode_width = GET_MODE_PRECISION (mode);
10867 HOST_WIDE_INT const_op = INTVAL (*pop1);
10868
10869 /* Get the constant we are comparing against and turn off all bits
10870 not on in our mode. */
10871 if (mode != VOIDmode)
10872 const_op = trunc_int_for_mode (const_op, mode);
10873
10874 /* If we are comparing against a constant power of two and the value
10875 being compared can only have that single bit nonzero (e.g., it was
10876 `and'ed with that bit), we can replace this with a comparison
10877 with zero. */
10878 if (const_op
10879 && (code == EQ || code == NE || code == GE || code == GEU
10880 || code == LT || code == LTU)
10881 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10882 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
10883 && (nonzero_bits (op0, mode)
10884 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
10885 {
10886 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10887 const_op = 0;
10888 }
10889
10890 /* Similarly, if we are comparing a value known to be either -1 or
10891 0 with -1, change it to the opposite comparison against zero. */
10892 if (const_op == -1
10893 && (code == EQ || code == NE || code == GT || code == LE
10894 || code == GEU || code == LTU)
10895 && num_sign_bit_copies (op0, mode) == mode_width)
10896 {
10897 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10898 const_op = 0;
10899 }
10900
10901 /* Do some canonicalizations based on the comparison code. We prefer
10902 comparisons against zero and then prefer equality comparisons.
10903 If we can reduce the size of a constant, we will do that too. */
10904 switch (code)
10905 {
10906 case LT:
10907 /* < C is equivalent to <= (C - 1) */
10908 if (const_op > 0)
10909 {
10910 const_op -= 1;
10911 code = LE;
10912 /* ... fall through to LE case below. */
10913 }
10914 else
10915 break;
10916
10917 case LE:
10918 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10919 if (const_op < 0)
10920 {
10921 const_op += 1;
10922 code = LT;
10923 }
10924
10925 /* If we are doing a <= 0 comparison on a value known to have
10926 a zero sign bit, we can replace this with == 0. */
10927 else if (const_op == 0
10928 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10929 && (nonzero_bits (op0, mode)
10930 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10931 == 0)
10932 code = EQ;
10933 break;
10934
10935 case GE:
10936 /* >= C is equivalent to > (C - 1). */
10937 if (const_op > 0)
10938 {
10939 const_op -= 1;
10940 code = GT;
10941 /* ... fall through to GT below. */
10942 }
10943 else
10944 break;
10945
10946 case GT:
10947 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10948 if (const_op < 0)
10949 {
10950 const_op += 1;
10951 code = GE;
10952 }
10953
10954 /* If we are doing a > 0 comparison on a value known to have
10955 a zero sign bit, we can replace this with != 0. */
10956 else if (const_op == 0
10957 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
10958 && (nonzero_bits (op0, mode)
10959 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10960 == 0)
10961 code = NE;
10962 break;
10963
10964 case LTU:
10965 /* < C is equivalent to <= (C - 1). */
10966 if (const_op > 0)
10967 {
10968 const_op -= 1;
10969 code = LEU;
10970 /* ... fall through ... */
10971 }
10972 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10973 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10974 && (unsigned HOST_WIDE_INT) const_op
10975 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
10976 {
10977 const_op = 0;
10978 code = GE;
10979 break;
10980 }
10981 else
10982 break;
10983
10984 case LEU:
10985 /* unsigned <= 0 is equivalent to == 0 */
10986 if (const_op == 0)
10987 code = EQ;
10988 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10989 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
10990 && (unsigned HOST_WIDE_INT) const_op
10991 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
10992 {
10993 const_op = 0;
10994 code = GE;
10995 }
10996 break;
10997
10998 case GEU:
10999 /* >= C is equivalent to > (C - 1). */
11000 if (const_op > 1)
11001 {
11002 const_op -= 1;
11003 code = GTU;
11004 /* ... fall through ... */
11005 }
11006
11007 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11008 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11009 && (unsigned HOST_WIDE_INT) const_op
11010 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11011 {
11012 const_op = 0;
11013 code = LT;
11014 break;
11015 }
11016 else
11017 break;
11018
11019 case GTU:
11020 /* unsigned > 0 is equivalent to != 0 */
11021 if (const_op == 0)
11022 code = NE;
11023 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11024 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11025 && (unsigned HOST_WIDE_INT) const_op
11026 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11027 {
11028 const_op = 0;
11029 code = LT;
11030 }
11031 break;
11032
11033 default:
11034 break;
11035 }
11036
11037 *pop1 = GEN_INT (const_op);
11038 return code;
11039 }
11040 \f
11041 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11042 comparison code that will be tested.
11043
11044 The result is a possibly different comparison code to use. *POP0 and
11045 *POP1 may be updated.
11046
11047 It is possible that we might detect that a comparison is either always
11048 true or always false. However, we do not perform general constant
11049 folding in combine, so this knowledge isn't useful. Such tautologies
11050 should have been detected earlier. Hence we ignore all such cases. */
11051
11052 static enum rtx_code
11053 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11054 {
11055 rtx op0 = *pop0;
11056 rtx op1 = *pop1;
11057 rtx tem, tem1;
11058 int i;
11059 enum machine_mode mode, tmode;
11060
11061 /* Try a few ways of applying the same transformation to both operands. */
11062 while (1)
11063 {
11064 #ifndef WORD_REGISTER_OPERATIONS
11065 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11066 so check specially. */
11067 if (code != GTU && code != GEU && code != LTU && code != LEU
11068 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11069 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11070 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11071 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11072 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11073 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11074 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11075 && CONST_INT_P (XEXP (op0, 1))
11076 && XEXP (op0, 1) == XEXP (op1, 1)
11077 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11078 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11079 && (INTVAL (XEXP (op0, 1))
11080 == (GET_MODE_PRECISION (GET_MODE (op0))
11081 - (GET_MODE_PRECISION
11082 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11083 {
11084 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11085 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11086 }
11087 #endif
11088
11089 /* If both operands are the same constant shift, see if we can ignore the
11090 shift. We can if the shift is a rotate or if the bits shifted out of
11091 this shift are known to be zero for both inputs and if the type of
11092 comparison is compatible with the shift. */
11093 if (GET_CODE (op0) == GET_CODE (op1)
11094 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11095 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11096 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11097 && (code != GT && code != LT && code != GE && code != LE))
11098 || (GET_CODE (op0) == ASHIFTRT
11099 && (code != GTU && code != LTU
11100 && code != GEU && code != LEU)))
11101 && CONST_INT_P (XEXP (op0, 1))
11102 && INTVAL (XEXP (op0, 1)) >= 0
11103 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11104 && XEXP (op0, 1) == XEXP (op1, 1))
11105 {
11106 enum machine_mode mode = GET_MODE (op0);
11107 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11108 int shift_count = INTVAL (XEXP (op0, 1));
11109
11110 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11111 mask &= (mask >> shift_count) << shift_count;
11112 else if (GET_CODE (op0) == ASHIFT)
11113 mask = (mask & (mask << shift_count)) >> shift_count;
11114
11115 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11116 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11117 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11118 else
11119 break;
11120 }
11121
11122 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11123 SUBREGs are of the same mode, and, in both cases, the AND would
11124 be redundant if the comparison was done in the narrower mode,
11125 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11126 and the operand's possibly nonzero bits are 0xffffff01; in that case
11127 if we only care about QImode, we don't need the AND). This case
11128 occurs if the output mode of an scc insn is not SImode and
11129 STORE_FLAG_VALUE == 1 (e.g., the 386).
11130
11131 Similarly, check for a case where the AND's are ZERO_EXTEND
11132 operations from some narrower mode even though a SUBREG is not
11133 present. */
11134
11135 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11136 && CONST_INT_P (XEXP (op0, 1))
11137 && CONST_INT_P (XEXP (op1, 1)))
11138 {
11139 rtx inner_op0 = XEXP (op0, 0);
11140 rtx inner_op1 = XEXP (op1, 0);
11141 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11142 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11143 int changed = 0;
11144
11145 if (paradoxical_subreg_p (inner_op0)
11146 && GET_CODE (inner_op1) == SUBREG
11147 && (GET_MODE (SUBREG_REG (inner_op0))
11148 == GET_MODE (SUBREG_REG (inner_op1)))
11149 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11150 <= HOST_BITS_PER_WIDE_INT)
11151 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11152 GET_MODE (SUBREG_REG (inner_op0)))))
11153 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11154 GET_MODE (SUBREG_REG (inner_op1))))))
11155 {
11156 op0 = SUBREG_REG (inner_op0);
11157 op1 = SUBREG_REG (inner_op1);
11158
11159 /* The resulting comparison is always unsigned since we masked
11160 off the original sign bit. */
11161 code = unsigned_condition (code);
11162
11163 changed = 1;
11164 }
11165
11166 else if (c0 == c1)
11167 for (tmode = GET_CLASS_NARROWEST_MODE
11168 (GET_MODE_CLASS (GET_MODE (op0)));
11169 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11170 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11171 {
11172 op0 = gen_lowpart (tmode, inner_op0);
11173 op1 = gen_lowpart (tmode, inner_op1);
11174 code = unsigned_condition (code);
11175 changed = 1;
11176 break;
11177 }
11178
11179 if (! changed)
11180 break;
11181 }
11182
11183 /* If both operands are NOT, we can strip off the outer operation
11184 and adjust the comparison code for swapped operands; similarly for
11185 NEG, except that this must be an equality comparison. */
11186 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11187 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11188 && (code == EQ || code == NE)))
11189 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11190
11191 else
11192 break;
11193 }
11194
11195 /* If the first operand is a constant, swap the operands and adjust the
11196 comparison code appropriately, but don't do this if the second operand
11197 is already a constant integer. */
11198 if (swap_commutative_operands_p (op0, op1))
11199 {
11200 tem = op0, op0 = op1, op1 = tem;
11201 code = swap_condition (code);
11202 }
11203
11204 /* We now enter a loop during which we will try to simplify the comparison.
11205 For the most part, we only are concerned with comparisons with zero,
11206 but some things may really be comparisons with zero but not start
11207 out looking that way. */
11208
11209 while (CONST_INT_P (op1))
11210 {
11211 enum machine_mode mode = GET_MODE (op0);
11212 unsigned int mode_width = GET_MODE_PRECISION (mode);
11213 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11214 int equality_comparison_p;
11215 int sign_bit_comparison_p;
11216 int unsigned_comparison_p;
11217 HOST_WIDE_INT const_op;
11218
11219 /* We only want to handle integral modes. This catches VOIDmode,
11220 CCmode, and the floating-point modes. An exception is that we
11221 can handle VOIDmode if OP0 is a COMPARE or a comparison
11222 operation. */
11223
11224 if (GET_MODE_CLASS (mode) != MODE_INT
11225 && ! (mode == VOIDmode
11226 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11227 break;
11228
11229 /* Try to simplify the compare to constant, possibly changing the
11230 comparison op, and/or changing op1 to zero. */
11231 code = simplify_compare_const (code, mode, op0, &op1);
11232 const_op = INTVAL (op1);
11233
11234 /* Compute some predicates to simplify code below. */
11235
11236 equality_comparison_p = (code == EQ || code == NE);
11237 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11238 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11239 || code == GEU);
11240
11241 /* If this is a sign bit comparison and we can do arithmetic in
11242 MODE, say that we will only be needing the sign bit of OP0. */
11243 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11244 op0 = force_to_mode (op0, mode,
11245 (unsigned HOST_WIDE_INT) 1
11246 << (GET_MODE_PRECISION (mode) - 1),
11247 0);
11248
11249 /* Now try cases based on the opcode of OP0. If none of the cases
11250 does a "continue", we exit this loop immediately after the
11251 switch. */
11252
11253 switch (GET_CODE (op0))
11254 {
11255 case ZERO_EXTRACT:
11256 /* If we are extracting a single bit from a variable position in
11257 a constant that has only a single bit set and are comparing it
11258 with zero, we can convert this into an equality comparison
11259 between the position and the location of the single bit. */
11260 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11261 have already reduced the shift count modulo the word size. */
11262 if (!SHIFT_COUNT_TRUNCATED
11263 && CONST_INT_P (XEXP (op0, 0))
11264 && XEXP (op0, 1) == const1_rtx
11265 && equality_comparison_p && const_op == 0
11266 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11267 {
11268 if (BITS_BIG_ENDIAN)
11269 i = BITS_PER_WORD - 1 - i;
11270
11271 op0 = XEXP (op0, 2);
11272 op1 = GEN_INT (i);
11273 const_op = i;
11274
11275 /* Result is nonzero iff shift count is equal to I. */
11276 code = reverse_condition (code);
11277 continue;
11278 }
11279
11280 /* ... fall through ... */
11281
11282 case SIGN_EXTRACT:
11283 tem = expand_compound_operation (op0);
11284 if (tem != op0)
11285 {
11286 op0 = tem;
11287 continue;
11288 }
11289 break;
11290
11291 case NOT:
11292 /* If testing for equality, we can take the NOT of the constant. */
11293 if (equality_comparison_p
11294 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11295 {
11296 op0 = XEXP (op0, 0);
11297 op1 = tem;
11298 continue;
11299 }
11300
11301 /* If just looking at the sign bit, reverse the sense of the
11302 comparison. */
11303 if (sign_bit_comparison_p)
11304 {
11305 op0 = XEXP (op0, 0);
11306 code = (code == GE ? LT : GE);
11307 continue;
11308 }
11309 break;
11310
11311 case NEG:
11312 /* If testing for equality, we can take the NEG of the constant. */
11313 if (equality_comparison_p
11314 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11315 {
11316 op0 = XEXP (op0, 0);
11317 op1 = tem;
11318 continue;
11319 }
11320
11321 /* The remaining cases only apply to comparisons with zero. */
11322 if (const_op != 0)
11323 break;
11324
11325 /* When X is ABS or is known positive,
11326 (neg X) is < 0 if and only if X != 0. */
11327
11328 if (sign_bit_comparison_p
11329 && (GET_CODE (XEXP (op0, 0)) == ABS
11330 || (mode_width <= HOST_BITS_PER_WIDE_INT
11331 && (nonzero_bits (XEXP (op0, 0), mode)
11332 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11333 == 0)))
11334 {
11335 op0 = XEXP (op0, 0);
11336 code = (code == LT ? NE : EQ);
11337 continue;
11338 }
11339
11340 /* If we have NEG of something whose two high-order bits are the
11341 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11342 if (num_sign_bit_copies (op0, mode) >= 2)
11343 {
11344 op0 = XEXP (op0, 0);
11345 code = swap_condition (code);
11346 continue;
11347 }
11348 break;
11349
11350 case ROTATE:
11351 /* If we are testing equality and our count is a constant, we
11352 can perform the inverse operation on our RHS. */
11353 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11354 && (tem = simplify_binary_operation (ROTATERT, mode,
11355 op1, XEXP (op0, 1))) != 0)
11356 {
11357 op0 = XEXP (op0, 0);
11358 op1 = tem;
11359 continue;
11360 }
11361
11362 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11363 a particular bit. Convert it to an AND of a constant of that
11364 bit. This will be converted into a ZERO_EXTRACT. */
11365 if (const_op == 0 && sign_bit_comparison_p
11366 && CONST_INT_P (XEXP (op0, 1))
11367 && mode_width <= HOST_BITS_PER_WIDE_INT)
11368 {
11369 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11370 ((unsigned HOST_WIDE_INT) 1
11371 << (mode_width - 1
11372 - INTVAL (XEXP (op0, 1)))));
11373 code = (code == LT ? NE : EQ);
11374 continue;
11375 }
11376
11377 /* Fall through. */
11378
11379 case ABS:
11380 /* ABS is ignorable inside an equality comparison with zero. */
11381 if (const_op == 0 && equality_comparison_p)
11382 {
11383 op0 = XEXP (op0, 0);
11384 continue;
11385 }
11386 break;
11387
11388 case SIGN_EXTEND:
11389 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11390 (compare FOO CONST) if CONST fits in FOO's mode and we
11391 are either testing inequality or have an unsigned
11392 comparison with ZERO_EXTEND or a signed comparison with
11393 SIGN_EXTEND. But don't do it if we don't have a compare
11394 insn of the given mode, since we'd have to revert it
11395 later on, and then we wouldn't know whether to sign- or
11396 zero-extend. */
11397 mode = GET_MODE (XEXP (op0, 0));
11398 if (GET_MODE_CLASS (mode) == MODE_INT
11399 && ! unsigned_comparison_p
11400 && HWI_COMPUTABLE_MODE_P (mode)
11401 && trunc_int_for_mode (const_op, mode) == const_op
11402 && have_insn_for (COMPARE, mode))
11403 {
11404 op0 = XEXP (op0, 0);
11405 continue;
11406 }
11407 break;
11408
11409 case SUBREG:
11410 /* Check for the case where we are comparing A - C1 with C2, that is
11411
11412 (subreg:MODE (plus (A) (-C1))) op (C2)
11413
11414 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11415 comparison in the wider mode. One of the following two conditions
11416 must be true in order for this to be valid:
11417
11418 1. The mode extension results in the same bit pattern being added
11419 on both sides and the comparison is equality or unsigned. As
11420 C2 has been truncated to fit in MODE, the pattern can only be
11421 all 0s or all 1s.
11422
11423 2. The mode extension results in the sign bit being copied on
11424 each side.
11425
11426 The difficulty here is that we have predicates for A but not for
11427 (A - C1) so we need to check that C1 is within proper bounds so
11428 as to perturbate A as little as possible. */
11429
11430 if (mode_width <= HOST_BITS_PER_WIDE_INT
11431 && subreg_lowpart_p (op0)
11432 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11433 && GET_CODE (SUBREG_REG (op0)) == PLUS
11434 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11435 {
11436 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11437 rtx a = XEXP (SUBREG_REG (op0), 0);
11438 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11439
11440 if ((c1 > 0
11441 && (unsigned HOST_WIDE_INT) c1
11442 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11443 && (equality_comparison_p || unsigned_comparison_p)
11444 /* (A - C1) zero-extends if it is positive and sign-extends
11445 if it is negative, C2 both zero- and sign-extends. */
11446 && ((0 == (nonzero_bits (a, inner_mode)
11447 & ~GET_MODE_MASK (mode))
11448 && const_op >= 0)
11449 /* (A - C1) sign-extends if it is positive and 1-extends
11450 if it is negative, C2 both sign- and 1-extends. */
11451 || (num_sign_bit_copies (a, inner_mode)
11452 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11453 - mode_width)
11454 && const_op < 0)))
11455 || ((unsigned HOST_WIDE_INT) c1
11456 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11457 /* (A - C1) always sign-extends, like C2. */
11458 && num_sign_bit_copies (a, inner_mode)
11459 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11460 - (mode_width - 1))))
11461 {
11462 op0 = SUBREG_REG (op0);
11463 continue;
11464 }
11465 }
11466
11467 /* If the inner mode is narrower and we are extracting the low part,
11468 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11469 if (subreg_lowpart_p (op0)
11470 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11471 /* Fall through */ ;
11472 else
11473 break;
11474
11475 /* ... fall through ... */
11476
11477 case ZERO_EXTEND:
11478 mode = GET_MODE (XEXP (op0, 0));
11479 if (GET_MODE_CLASS (mode) == MODE_INT
11480 && (unsigned_comparison_p || equality_comparison_p)
11481 && HWI_COMPUTABLE_MODE_P (mode)
11482 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11483 && const_op >= 0
11484 && have_insn_for (COMPARE, mode))
11485 {
11486 op0 = XEXP (op0, 0);
11487 continue;
11488 }
11489 break;
11490
11491 case PLUS:
11492 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11493 this for equality comparisons due to pathological cases involving
11494 overflows. */
11495 if (equality_comparison_p
11496 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11497 op1, XEXP (op0, 1))))
11498 {
11499 op0 = XEXP (op0, 0);
11500 op1 = tem;
11501 continue;
11502 }
11503
11504 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11505 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11506 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11507 {
11508 op0 = XEXP (XEXP (op0, 0), 0);
11509 code = (code == LT ? EQ : NE);
11510 continue;
11511 }
11512 break;
11513
11514 case MINUS:
11515 /* We used to optimize signed comparisons against zero, but that
11516 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11517 arrive here as equality comparisons, or (GEU, LTU) are
11518 optimized away. No need to special-case them. */
11519
11520 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11521 (eq B (minus A C)), whichever simplifies. We can only do
11522 this for equality comparisons due to pathological cases involving
11523 overflows. */
11524 if (equality_comparison_p
11525 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11526 XEXP (op0, 1), op1)))
11527 {
11528 op0 = XEXP (op0, 0);
11529 op1 = tem;
11530 continue;
11531 }
11532
11533 if (equality_comparison_p
11534 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11535 XEXP (op0, 0), op1)))
11536 {
11537 op0 = XEXP (op0, 1);
11538 op1 = tem;
11539 continue;
11540 }
11541
11542 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11543 of bits in X minus 1, is one iff X > 0. */
11544 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11545 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11546 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11547 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11548 {
11549 op0 = XEXP (op0, 1);
11550 code = (code == GE ? LE : GT);
11551 continue;
11552 }
11553 break;
11554
11555 case XOR:
11556 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11557 if C is zero or B is a constant. */
11558 if (equality_comparison_p
11559 && 0 != (tem = simplify_binary_operation (XOR, mode,
11560 XEXP (op0, 1), op1)))
11561 {
11562 op0 = XEXP (op0, 0);
11563 op1 = tem;
11564 continue;
11565 }
11566 break;
11567
11568 case EQ: case NE:
11569 case UNEQ: case LTGT:
11570 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11571 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11572 case UNORDERED: case ORDERED:
11573 /* We can't do anything if OP0 is a condition code value, rather
11574 than an actual data value. */
11575 if (const_op != 0
11576 || CC0_P (XEXP (op0, 0))
11577 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11578 break;
11579
11580 /* Get the two operands being compared. */
11581 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11582 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11583 else
11584 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11585
11586 /* Check for the cases where we simply want the result of the
11587 earlier test or the opposite of that result. */
11588 if (code == NE || code == EQ
11589 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11590 && (code == LT || code == GE)))
11591 {
11592 enum rtx_code new_code;
11593 if (code == LT || code == NE)
11594 new_code = GET_CODE (op0);
11595 else
11596 new_code = reversed_comparison_code (op0, NULL);
11597
11598 if (new_code != UNKNOWN)
11599 {
11600 code = new_code;
11601 op0 = tem;
11602 op1 = tem1;
11603 continue;
11604 }
11605 }
11606 break;
11607
11608 case IOR:
11609 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11610 iff X <= 0. */
11611 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11612 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11613 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11614 {
11615 op0 = XEXP (op0, 1);
11616 code = (code == GE ? GT : LE);
11617 continue;
11618 }
11619 break;
11620
11621 case AND:
11622 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11623 will be converted to a ZERO_EXTRACT later. */
11624 if (const_op == 0 && equality_comparison_p
11625 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11626 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11627 {
11628 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11629 XEXP (XEXP (op0, 0), 1));
11630 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11631 continue;
11632 }
11633
11634 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11635 zero and X is a comparison and C1 and C2 describe only bits set
11636 in STORE_FLAG_VALUE, we can compare with X. */
11637 if (const_op == 0 && equality_comparison_p
11638 && mode_width <= HOST_BITS_PER_WIDE_INT
11639 && CONST_INT_P (XEXP (op0, 1))
11640 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11641 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11642 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11643 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11644 {
11645 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11646 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11647 if ((~STORE_FLAG_VALUE & mask) == 0
11648 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11649 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11650 && COMPARISON_P (tem))))
11651 {
11652 op0 = XEXP (XEXP (op0, 0), 0);
11653 continue;
11654 }
11655 }
11656
11657 /* If we are doing an equality comparison of an AND of a bit equal
11658 to the sign bit, replace this with a LT or GE comparison of
11659 the underlying value. */
11660 if (equality_comparison_p
11661 && const_op == 0
11662 && CONST_INT_P (XEXP (op0, 1))
11663 && mode_width <= HOST_BITS_PER_WIDE_INT
11664 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11665 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11666 {
11667 op0 = XEXP (op0, 0);
11668 code = (code == EQ ? GE : LT);
11669 continue;
11670 }
11671
11672 /* If this AND operation is really a ZERO_EXTEND from a narrower
11673 mode, the constant fits within that mode, and this is either an
11674 equality or unsigned comparison, try to do this comparison in
11675 the narrower mode.
11676
11677 Note that in:
11678
11679 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11680 -> (ne:DI (reg:SI 4) (const_int 0))
11681
11682 unless TRULY_NOOP_TRUNCATION allows it or the register is
11683 known to hold a value of the required mode the
11684 transformation is invalid. */
11685 if ((equality_comparison_p || unsigned_comparison_p)
11686 && CONST_INT_P (XEXP (op0, 1))
11687 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11688 & GET_MODE_MASK (mode))
11689 + 1)) >= 0
11690 && const_op >> i == 0
11691 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11692 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11693 || (REG_P (XEXP (op0, 0))
11694 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11695 {
11696 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11697 continue;
11698 }
11699
11700 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11701 fits in both M1 and M2 and the SUBREG is either paradoxical
11702 or represents the low part, permute the SUBREG and the AND
11703 and try again. */
11704 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11705 {
11706 unsigned HOST_WIDE_INT c1;
11707 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11708 /* Require an integral mode, to avoid creating something like
11709 (AND:SF ...). */
11710 if (SCALAR_INT_MODE_P (tmode)
11711 /* It is unsafe to commute the AND into the SUBREG if the
11712 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11713 not defined. As originally written the upper bits
11714 have a defined value due to the AND operation.
11715 However, if we commute the AND inside the SUBREG then
11716 they no longer have defined values and the meaning of
11717 the code has been changed. */
11718 && (0
11719 #ifdef WORD_REGISTER_OPERATIONS
11720 || (mode_width > GET_MODE_PRECISION (tmode)
11721 && mode_width <= BITS_PER_WORD)
11722 #endif
11723 || (mode_width <= GET_MODE_PRECISION (tmode)
11724 && subreg_lowpart_p (XEXP (op0, 0))))
11725 && CONST_INT_P (XEXP (op0, 1))
11726 && mode_width <= HOST_BITS_PER_WIDE_INT
11727 && HWI_COMPUTABLE_MODE_P (tmode)
11728 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11729 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11730 && c1 != mask
11731 && c1 != GET_MODE_MASK (tmode))
11732 {
11733 op0 = simplify_gen_binary (AND, tmode,
11734 SUBREG_REG (XEXP (op0, 0)),
11735 gen_int_mode (c1, tmode));
11736 op0 = gen_lowpart (mode, op0);
11737 continue;
11738 }
11739 }
11740
11741 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11742 if (const_op == 0 && equality_comparison_p
11743 && XEXP (op0, 1) == const1_rtx
11744 && GET_CODE (XEXP (op0, 0)) == NOT)
11745 {
11746 op0 = simplify_and_const_int (NULL_RTX, mode,
11747 XEXP (XEXP (op0, 0), 0), 1);
11748 code = (code == NE ? EQ : NE);
11749 continue;
11750 }
11751
11752 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11753 (eq (and (lshiftrt X) 1) 0).
11754 Also handle the case where (not X) is expressed using xor. */
11755 if (const_op == 0 && equality_comparison_p
11756 && XEXP (op0, 1) == const1_rtx
11757 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11758 {
11759 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11760 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11761
11762 if (GET_CODE (shift_op) == NOT
11763 || (GET_CODE (shift_op) == XOR
11764 && CONST_INT_P (XEXP (shift_op, 1))
11765 && CONST_INT_P (shift_count)
11766 && HWI_COMPUTABLE_MODE_P (mode)
11767 && (UINTVAL (XEXP (shift_op, 1))
11768 == (unsigned HOST_WIDE_INT) 1
11769 << INTVAL (shift_count))))
11770 {
11771 op0
11772 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
11773 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11774 code = (code == NE ? EQ : NE);
11775 continue;
11776 }
11777 }
11778 break;
11779
11780 case ASHIFT:
11781 /* If we have (compare (ashift FOO N) (const_int C)) and
11782 the high order N bits of FOO (N+1 if an inequality comparison)
11783 are known to be zero, we can do this by comparing FOO with C
11784 shifted right N bits so long as the low-order N bits of C are
11785 zero. */
11786 if (CONST_INT_P (XEXP (op0, 1))
11787 && INTVAL (XEXP (op0, 1)) >= 0
11788 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11789 < HOST_BITS_PER_WIDE_INT)
11790 && (((unsigned HOST_WIDE_INT) const_op
11791 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
11792 - 1)) == 0)
11793 && mode_width <= HOST_BITS_PER_WIDE_INT
11794 && (nonzero_bits (XEXP (op0, 0), mode)
11795 & ~(mask >> (INTVAL (XEXP (op0, 1))
11796 + ! equality_comparison_p))) == 0)
11797 {
11798 /* We must perform a logical shift, not an arithmetic one,
11799 as we want the top N bits of C to be zero. */
11800 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11801
11802 temp >>= INTVAL (XEXP (op0, 1));
11803 op1 = gen_int_mode (temp, mode);
11804 op0 = XEXP (op0, 0);
11805 continue;
11806 }
11807
11808 /* If we are doing a sign bit comparison, it means we are testing
11809 a particular bit. Convert it to the appropriate AND. */
11810 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
11811 && mode_width <= HOST_BITS_PER_WIDE_INT)
11812 {
11813 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11814 ((unsigned HOST_WIDE_INT) 1
11815 << (mode_width - 1
11816 - INTVAL (XEXP (op0, 1)))));
11817 code = (code == LT ? NE : EQ);
11818 continue;
11819 }
11820
11821 /* If this an equality comparison with zero and we are shifting
11822 the low bit to the sign bit, we can convert this to an AND of the
11823 low-order bit. */
11824 if (const_op == 0 && equality_comparison_p
11825 && CONST_INT_P (XEXP (op0, 1))
11826 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11827 {
11828 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
11829 continue;
11830 }
11831 break;
11832
11833 case ASHIFTRT:
11834 /* If this is an equality comparison with zero, we can do this
11835 as a logical shift, which might be much simpler. */
11836 if (equality_comparison_p && const_op == 0
11837 && CONST_INT_P (XEXP (op0, 1)))
11838 {
11839 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11840 XEXP (op0, 0),
11841 INTVAL (XEXP (op0, 1)));
11842 continue;
11843 }
11844
11845 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11846 do the comparison in a narrower mode. */
11847 if (! unsigned_comparison_p
11848 && CONST_INT_P (XEXP (op0, 1))
11849 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11850 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11851 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11852 MODE_INT, 1)) != BLKmode
11853 && (((unsigned HOST_WIDE_INT) const_op
11854 + (GET_MODE_MASK (tmode) >> 1) + 1)
11855 <= GET_MODE_MASK (tmode)))
11856 {
11857 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11858 continue;
11859 }
11860
11861 /* Likewise if OP0 is a PLUS of a sign extension with a
11862 constant, which is usually represented with the PLUS
11863 between the shifts. */
11864 if (! unsigned_comparison_p
11865 && CONST_INT_P (XEXP (op0, 1))
11866 && GET_CODE (XEXP (op0, 0)) == PLUS
11867 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11868 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11869 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11870 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11871 MODE_INT, 1)) != BLKmode
11872 && (((unsigned HOST_WIDE_INT) const_op
11873 + (GET_MODE_MASK (tmode) >> 1) + 1)
11874 <= GET_MODE_MASK (tmode)))
11875 {
11876 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11877 rtx add_const = XEXP (XEXP (op0, 0), 1);
11878 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
11879 add_const, XEXP (op0, 1));
11880
11881 op0 = simplify_gen_binary (PLUS, tmode,
11882 gen_lowpart (tmode, inner),
11883 new_const);
11884 continue;
11885 }
11886
11887 /* ... fall through ... */
11888 case LSHIFTRT:
11889 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11890 the low order N bits of FOO are known to be zero, we can do this
11891 by comparing FOO with C shifted left N bits so long as no
11892 overflow occurs. Even if the low order N bits of FOO aren't known
11893 to be zero, if the comparison is >= or < we can use the same
11894 optimization and for > or <= by setting all the low
11895 order N bits in the comparison constant. */
11896 if (CONST_INT_P (XEXP (op0, 1))
11897 && INTVAL (XEXP (op0, 1)) > 0
11898 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11899 && mode_width <= HOST_BITS_PER_WIDE_INT
11900 && (((unsigned HOST_WIDE_INT) const_op
11901 + (GET_CODE (op0) != LSHIFTRT
11902 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11903 + 1)
11904 : 0))
11905 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11906 {
11907 unsigned HOST_WIDE_INT low_bits
11908 = (nonzero_bits (XEXP (op0, 0), mode)
11909 & (((unsigned HOST_WIDE_INT) 1
11910 << INTVAL (XEXP (op0, 1))) - 1));
11911 if (low_bits == 0 || !equality_comparison_p)
11912 {
11913 /* If the shift was logical, then we must make the condition
11914 unsigned. */
11915 if (GET_CODE (op0) == LSHIFTRT)
11916 code = unsigned_condition (code);
11917
11918 const_op <<= INTVAL (XEXP (op0, 1));
11919 if (low_bits != 0
11920 && (code == GT || code == GTU
11921 || code == LE || code == LEU))
11922 const_op
11923 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
11924 op1 = GEN_INT (const_op);
11925 op0 = XEXP (op0, 0);
11926 continue;
11927 }
11928 }
11929
11930 /* If we are using this shift to extract just the sign bit, we
11931 can replace this with an LT or GE comparison. */
11932 if (const_op == 0
11933 && (equality_comparison_p || sign_bit_comparison_p)
11934 && CONST_INT_P (XEXP (op0, 1))
11935 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
11936 {
11937 op0 = XEXP (op0, 0);
11938 code = (code == NE || code == GT ? LT : GE);
11939 continue;
11940 }
11941 break;
11942
11943 default:
11944 break;
11945 }
11946
11947 break;
11948 }
11949
11950 /* Now make any compound operations involved in this comparison. Then,
11951 check for an outmost SUBREG on OP0 that is not doing anything or is
11952 paradoxical. The latter transformation must only be performed when
11953 it is known that the "extra" bits will be the same in op0 and op1 or
11954 that they don't matter. There are three cases to consider:
11955
11956 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11957 care bits and we can assume they have any convenient value. So
11958 making the transformation is safe.
11959
11960 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11961 In this case the upper bits of op0 are undefined. We should not make
11962 the simplification in that case as we do not know the contents of
11963 those bits.
11964
11965 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11966 UNKNOWN. In that case we know those bits are zeros or ones. We must
11967 also be sure that they are the same as the upper bits of op1.
11968
11969 We can never remove a SUBREG for a non-equality comparison because
11970 the sign bit is in a different place in the underlying object. */
11971
11972 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11973 op1 = make_compound_operation (op1, SET);
11974
11975 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11976 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11977 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11978 && (code == NE || code == EQ))
11979 {
11980 if (paradoxical_subreg_p (op0))
11981 {
11982 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11983 implemented. */
11984 if (REG_P (SUBREG_REG (op0)))
11985 {
11986 op0 = SUBREG_REG (op0);
11987 op1 = gen_lowpart (GET_MODE (op0), op1);
11988 }
11989 }
11990 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
11991 <= HOST_BITS_PER_WIDE_INT)
11992 && (nonzero_bits (SUBREG_REG (op0),
11993 GET_MODE (SUBREG_REG (op0)))
11994 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11995 {
11996 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11997
11998 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11999 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12000 op0 = SUBREG_REG (op0), op1 = tem;
12001 }
12002 }
12003
12004 /* We now do the opposite procedure: Some machines don't have compare
12005 insns in all modes. If OP0's mode is an integer mode smaller than a
12006 word and we can't do a compare in that mode, see if there is a larger
12007 mode for which we can do the compare. There are a number of cases in
12008 which we can use the wider mode. */
12009
12010 mode = GET_MODE (op0);
12011 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12012 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12013 && ! have_insn_for (COMPARE, mode))
12014 for (tmode = GET_MODE_WIDER_MODE (mode);
12015 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12016 tmode = GET_MODE_WIDER_MODE (tmode))
12017 if (have_insn_for (COMPARE, tmode))
12018 {
12019 int zero_extended;
12020
12021 /* If this is a test for negative, we can make an explicit
12022 test of the sign bit. Test this first so we can use
12023 a paradoxical subreg to extend OP0. */
12024
12025 if (op1 == const0_rtx && (code == LT || code == GE)
12026 && HWI_COMPUTABLE_MODE_P (mode))
12027 {
12028 unsigned HOST_WIDE_INT sign
12029 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12030 op0 = simplify_gen_binary (AND, tmode,
12031 gen_lowpart (tmode, op0),
12032 gen_int_mode (sign, tmode));
12033 code = (code == LT) ? NE : EQ;
12034 break;
12035 }
12036
12037 /* If the only nonzero bits in OP0 and OP1 are those in the
12038 narrower mode and this is an equality or unsigned comparison,
12039 we can use the wider mode. Similarly for sign-extended
12040 values, in which case it is true for all comparisons. */
12041 zero_extended = ((code == EQ || code == NE
12042 || code == GEU || code == GTU
12043 || code == LEU || code == LTU)
12044 && (nonzero_bits (op0, tmode)
12045 & ~GET_MODE_MASK (mode)) == 0
12046 && ((CONST_INT_P (op1)
12047 || (nonzero_bits (op1, tmode)
12048 & ~GET_MODE_MASK (mode)) == 0)));
12049
12050 if (zero_extended
12051 || ((num_sign_bit_copies (op0, tmode)
12052 > (unsigned int) (GET_MODE_PRECISION (tmode)
12053 - GET_MODE_PRECISION (mode)))
12054 && (num_sign_bit_copies (op1, tmode)
12055 > (unsigned int) (GET_MODE_PRECISION (tmode)
12056 - GET_MODE_PRECISION (mode)))))
12057 {
12058 /* If OP0 is an AND and we don't have an AND in MODE either,
12059 make a new AND in the proper mode. */
12060 if (GET_CODE (op0) == AND
12061 && !have_insn_for (AND, mode))
12062 op0 = simplify_gen_binary (AND, tmode,
12063 gen_lowpart (tmode,
12064 XEXP (op0, 0)),
12065 gen_lowpart (tmode,
12066 XEXP (op0, 1)));
12067 else
12068 {
12069 if (zero_extended)
12070 {
12071 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12072 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12073 }
12074 else
12075 {
12076 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12077 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12078 }
12079 break;
12080 }
12081 }
12082 }
12083
12084 /* We may have changed the comparison operands. Re-canonicalize. */
12085 if (swap_commutative_operands_p (op0, op1))
12086 {
12087 tem = op0, op0 = op1, op1 = tem;
12088 code = swap_condition (code);
12089 }
12090
12091 /* If this machine only supports a subset of valid comparisons, see if we
12092 can convert an unsupported one into a supported one. */
12093 target_canonicalize_comparison (&code, &op0, &op1, 0);
12094
12095 *pop0 = op0;
12096 *pop1 = op1;
12097
12098 return code;
12099 }
12100 \f
12101 /* Utility function for record_value_for_reg. Count number of
12102 rtxs in X. */
12103 static int
12104 count_rtxs (rtx x)
12105 {
12106 enum rtx_code code = GET_CODE (x);
12107 const char *fmt;
12108 int i, j, ret = 1;
12109
12110 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12111 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12112 {
12113 rtx x0 = XEXP (x, 0);
12114 rtx x1 = XEXP (x, 1);
12115
12116 if (x0 == x1)
12117 return 1 + 2 * count_rtxs (x0);
12118
12119 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12120 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12121 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12122 return 2 + 2 * count_rtxs (x0)
12123 + count_rtxs (x == XEXP (x1, 0)
12124 ? XEXP (x1, 1) : XEXP (x1, 0));
12125
12126 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12127 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12128 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12129 return 2 + 2 * count_rtxs (x1)
12130 + count_rtxs (x == XEXP (x0, 0)
12131 ? XEXP (x0, 1) : XEXP (x0, 0));
12132 }
12133
12134 fmt = GET_RTX_FORMAT (code);
12135 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12136 if (fmt[i] == 'e')
12137 ret += count_rtxs (XEXP (x, i));
12138 else if (fmt[i] == 'E')
12139 for (j = 0; j < XVECLEN (x, i); j++)
12140 ret += count_rtxs (XVECEXP (x, i, j));
12141
12142 return ret;
12143 }
12144 \f
12145 /* Utility function for following routine. Called when X is part of a value
12146 being stored into last_set_value. Sets last_set_table_tick
12147 for each register mentioned. Similar to mention_regs in cse.c */
12148
12149 static void
12150 update_table_tick (rtx x)
12151 {
12152 enum rtx_code code = GET_CODE (x);
12153 const char *fmt = GET_RTX_FORMAT (code);
12154 int i, j;
12155
12156 if (code == REG)
12157 {
12158 unsigned int regno = REGNO (x);
12159 unsigned int endregno = END_REGNO (x);
12160 unsigned int r;
12161
12162 for (r = regno; r < endregno; r++)
12163 {
12164 reg_stat_type *rsp = &reg_stat[r];
12165 rsp->last_set_table_tick = label_tick;
12166 }
12167
12168 return;
12169 }
12170
12171 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12172 if (fmt[i] == 'e')
12173 {
12174 /* Check for identical subexpressions. If x contains
12175 identical subexpression we only have to traverse one of
12176 them. */
12177 if (i == 0 && ARITHMETIC_P (x))
12178 {
12179 /* Note that at this point x1 has already been
12180 processed. */
12181 rtx x0 = XEXP (x, 0);
12182 rtx x1 = XEXP (x, 1);
12183
12184 /* If x0 and x1 are identical then there is no need to
12185 process x0. */
12186 if (x0 == x1)
12187 break;
12188
12189 /* If x0 is identical to a subexpression of x1 then while
12190 processing x1, x0 has already been processed. Thus we
12191 are done with x. */
12192 if (ARITHMETIC_P (x1)
12193 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12194 break;
12195
12196 /* If x1 is identical to a subexpression of x0 then we
12197 still have to process the rest of x0. */
12198 if (ARITHMETIC_P (x0)
12199 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12200 {
12201 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12202 break;
12203 }
12204 }
12205
12206 update_table_tick (XEXP (x, i));
12207 }
12208 else if (fmt[i] == 'E')
12209 for (j = 0; j < XVECLEN (x, i); j++)
12210 update_table_tick (XVECEXP (x, i, j));
12211 }
12212
12213 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12214 are saying that the register is clobbered and we no longer know its
12215 value. If INSN is zero, don't update reg_stat[].last_set; this is
12216 only permitted with VALUE also zero and is used to invalidate the
12217 register. */
12218
12219 static void
12220 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12221 {
12222 unsigned int regno = REGNO (reg);
12223 unsigned int endregno = END_REGNO (reg);
12224 unsigned int i;
12225 reg_stat_type *rsp;
12226
12227 /* If VALUE contains REG and we have a previous value for REG, substitute
12228 the previous value. */
12229 if (value && insn && reg_overlap_mentioned_p (reg, value))
12230 {
12231 rtx tem;
12232
12233 /* Set things up so get_last_value is allowed to see anything set up to
12234 our insn. */
12235 subst_low_luid = DF_INSN_LUID (insn);
12236 tem = get_last_value (reg);
12237
12238 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12239 it isn't going to be useful and will take a lot of time to process,
12240 so just use the CLOBBER. */
12241
12242 if (tem)
12243 {
12244 if (ARITHMETIC_P (tem)
12245 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12246 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12247 tem = XEXP (tem, 0);
12248 else if (count_occurrences (value, reg, 1) >= 2)
12249 {
12250 /* If there are two or more occurrences of REG in VALUE,
12251 prevent the value from growing too much. */
12252 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12253 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12254 }
12255
12256 value = replace_rtx (copy_rtx (value), reg, tem);
12257 }
12258 }
12259
12260 /* For each register modified, show we don't know its value, that
12261 we don't know about its bitwise content, that its value has been
12262 updated, and that we don't know the location of the death of the
12263 register. */
12264 for (i = regno; i < endregno; i++)
12265 {
12266 rsp = &reg_stat[i];
12267
12268 if (insn)
12269 rsp->last_set = insn;
12270
12271 rsp->last_set_value = 0;
12272 rsp->last_set_mode = VOIDmode;
12273 rsp->last_set_nonzero_bits = 0;
12274 rsp->last_set_sign_bit_copies = 0;
12275 rsp->last_death = 0;
12276 rsp->truncated_to_mode = VOIDmode;
12277 }
12278
12279 /* Mark registers that are being referenced in this value. */
12280 if (value)
12281 update_table_tick (value);
12282
12283 /* Now update the status of each register being set.
12284 If someone is using this register in this block, set this register
12285 to invalid since we will get confused between the two lives in this
12286 basic block. This makes using this register always invalid. In cse, we
12287 scan the table to invalidate all entries using this register, but this
12288 is too much work for us. */
12289
12290 for (i = regno; i < endregno; i++)
12291 {
12292 rsp = &reg_stat[i];
12293 rsp->last_set_label = label_tick;
12294 if (!insn
12295 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12296 rsp->last_set_invalid = 1;
12297 else
12298 rsp->last_set_invalid = 0;
12299 }
12300
12301 /* The value being assigned might refer to X (like in "x++;"). In that
12302 case, we must replace it with (clobber (const_int 0)) to prevent
12303 infinite loops. */
12304 rsp = &reg_stat[regno];
12305 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12306 {
12307 value = copy_rtx (value);
12308 if (!get_last_value_validate (&value, insn, label_tick, 1))
12309 value = 0;
12310 }
12311
12312 /* For the main register being modified, update the value, the mode, the
12313 nonzero bits, and the number of sign bit copies. */
12314
12315 rsp->last_set_value = value;
12316
12317 if (value)
12318 {
12319 enum machine_mode mode = GET_MODE (reg);
12320 subst_low_luid = DF_INSN_LUID (insn);
12321 rsp->last_set_mode = mode;
12322 if (GET_MODE_CLASS (mode) == MODE_INT
12323 && HWI_COMPUTABLE_MODE_P (mode))
12324 mode = nonzero_bits_mode;
12325 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12326 rsp->last_set_sign_bit_copies
12327 = num_sign_bit_copies (value, GET_MODE (reg));
12328 }
12329 }
12330
12331 /* Called via note_stores from record_dead_and_set_regs to handle one
12332 SET or CLOBBER in an insn. DATA is the instruction in which the
12333 set is occurring. */
12334
12335 static void
12336 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12337 {
12338 rtx_insn *record_dead_insn = (rtx_insn *) data;
12339
12340 if (GET_CODE (dest) == SUBREG)
12341 dest = SUBREG_REG (dest);
12342
12343 if (!record_dead_insn)
12344 {
12345 if (REG_P (dest))
12346 record_value_for_reg (dest, NULL, NULL_RTX);
12347 return;
12348 }
12349
12350 if (REG_P (dest))
12351 {
12352 /* If we are setting the whole register, we know its value. Otherwise
12353 show that we don't know the value. We can handle SUBREG in
12354 some cases. */
12355 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12356 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12357 else if (GET_CODE (setter) == SET
12358 && GET_CODE (SET_DEST (setter)) == SUBREG
12359 && SUBREG_REG (SET_DEST (setter)) == dest
12360 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12361 && subreg_lowpart_p (SET_DEST (setter)))
12362 record_value_for_reg (dest, record_dead_insn,
12363 gen_lowpart (GET_MODE (dest),
12364 SET_SRC (setter)));
12365 else
12366 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12367 }
12368 else if (MEM_P (dest)
12369 /* Ignore pushes, they clobber nothing. */
12370 && ! push_operand (dest, GET_MODE (dest)))
12371 mem_last_set = DF_INSN_LUID (record_dead_insn);
12372 }
12373
12374 /* Update the records of when each REG was most recently set or killed
12375 for the things done by INSN. This is the last thing done in processing
12376 INSN in the combiner loop.
12377
12378 We update reg_stat[], in particular fields last_set, last_set_value,
12379 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12380 last_death, and also the similar information mem_last_set (which insn
12381 most recently modified memory) and last_call_luid (which insn was the
12382 most recent subroutine call). */
12383
12384 static void
12385 record_dead_and_set_regs (rtx_insn *insn)
12386 {
12387 rtx link;
12388 unsigned int i;
12389
12390 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12391 {
12392 if (REG_NOTE_KIND (link) == REG_DEAD
12393 && REG_P (XEXP (link, 0)))
12394 {
12395 unsigned int regno = REGNO (XEXP (link, 0));
12396 unsigned int endregno = END_REGNO (XEXP (link, 0));
12397
12398 for (i = regno; i < endregno; i++)
12399 {
12400 reg_stat_type *rsp;
12401
12402 rsp = &reg_stat[i];
12403 rsp->last_death = insn;
12404 }
12405 }
12406 else if (REG_NOTE_KIND (link) == REG_INC)
12407 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12408 }
12409
12410 if (CALL_P (insn))
12411 {
12412 hard_reg_set_iterator hrsi;
12413 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12414 {
12415 reg_stat_type *rsp;
12416
12417 rsp = &reg_stat[i];
12418 rsp->last_set_invalid = 1;
12419 rsp->last_set = insn;
12420 rsp->last_set_value = 0;
12421 rsp->last_set_mode = VOIDmode;
12422 rsp->last_set_nonzero_bits = 0;
12423 rsp->last_set_sign_bit_copies = 0;
12424 rsp->last_death = 0;
12425 rsp->truncated_to_mode = VOIDmode;
12426 }
12427
12428 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12429
12430 /* We can't combine into a call pattern. Remember, though, that
12431 the return value register is set at this LUID. We could
12432 still replace a register with the return value from the
12433 wrong subroutine call! */
12434 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12435 }
12436 else
12437 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12438 }
12439
12440 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12441 register present in the SUBREG, so for each such SUBREG go back and
12442 adjust nonzero and sign bit information of the registers that are
12443 known to have some zero/sign bits set.
12444
12445 This is needed because when combine blows the SUBREGs away, the
12446 information on zero/sign bits is lost and further combines can be
12447 missed because of that. */
12448
12449 static void
12450 record_promoted_value (rtx_insn *insn, rtx subreg)
12451 {
12452 struct insn_link *links;
12453 rtx set;
12454 unsigned int regno = REGNO (SUBREG_REG (subreg));
12455 enum machine_mode mode = GET_MODE (subreg);
12456
12457 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12458 return;
12459
12460 for (links = LOG_LINKS (insn); links;)
12461 {
12462 reg_stat_type *rsp;
12463
12464 insn = links->insn;
12465 set = single_set (insn);
12466
12467 if (! set || !REG_P (SET_DEST (set))
12468 || REGNO (SET_DEST (set)) != regno
12469 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12470 {
12471 links = links->next;
12472 continue;
12473 }
12474
12475 rsp = &reg_stat[regno];
12476 if (rsp->last_set == insn)
12477 {
12478 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12479 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12480 }
12481
12482 if (REG_P (SET_SRC (set)))
12483 {
12484 regno = REGNO (SET_SRC (set));
12485 links = LOG_LINKS (insn);
12486 }
12487 else
12488 break;
12489 }
12490 }
12491
12492 /* Check if X, a register, is known to contain a value already
12493 truncated to MODE. In this case we can use a subreg to refer to
12494 the truncated value even though in the generic case we would need
12495 an explicit truncation. */
12496
12497 static bool
12498 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
12499 {
12500 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12501 enum machine_mode truncated = rsp->truncated_to_mode;
12502
12503 if (truncated == 0
12504 || rsp->truncation_label < label_tick_ebb_start)
12505 return false;
12506 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12507 return true;
12508 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12509 return true;
12510 return false;
12511 }
12512
12513 /* If X is a hard reg or a subreg record the mode that the register is
12514 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12515 to turn a truncate into a subreg using this information. Return true
12516 if traversing X is complete. */
12517
12518 static bool
12519 record_truncated_value (rtx x)
12520 {
12521 enum machine_mode truncated_mode;
12522 reg_stat_type *rsp;
12523
12524 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12525 {
12526 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12527 truncated_mode = GET_MODE (x);
12528
12529 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12530 return true;
12531
12532 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12533 return true;
12534
12535 x = SUBREG_REG (x);
12536 }
12537 /* ??? For hard-regs we now record everything. We might be able to
12538 optimize this using last_set_mode. */
12539 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12540 truncated_mode = GET_MODE (x);
12541 else
12542 return false;
12543
12544 rsp = &reg_stat[REGNO (x)];
12545 if (rsp->truncated_to_mode == 0
12546 || rsp->truncation_label < label_tick_ebb_start
12547 || (GET_MODE_SIZE (truncated_mode)
12548 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12549 {
12550 rsp->truncated_to_mode = truncated_mode;
12551 rsp->truncation_label = label_tick;
12552 }
12553
12554 return true;
12555 }
12556
12557 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12558 the modes they are used in. This can help truning TRUNCATEs into
12559 SUBREGs. */
12560
12561 static void
12562 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12563 {
12564 subrtx_var_iterator::array_type array;
12565 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12566 if (record_truncated_value (*iter))
12567 iter.skip_subrtxes ();
12568 }
12569
12570 /* Scan X for promoted SUBREGs. For each one found,
12571 note what it implies to the registers used in it. */
12572
12573 static void
12574 check_promoted_subreg (rtx_insn *insn, rtx x)
12575 {
12576 if (GET_CODE (x) == SUBREG
12577 && SUBREG_PROMOTED_VAR_P (x)
12578 && REG_P (SUBREG_REG (x)))
12579 record_promoted_value (insn, x);
12580 else
12581 {
12582 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12583 int i, j;
12584
12585 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12586 switch (format[i])
12587 {
12588 case 'e':
12589 check_promoted_subreg (insn, XEXP (x, i));
12590 break;
12591 case 'V':
12592 case 'E':
12593 if (XVEC (x, i) != 0)
12594 for (j = 0; j < XVECLEN (x, i); j++)
12595 check_promoted_subreg (insn, XVECEXP (x, i, j));
12596 break;
12597 }
12598 }
12599 }
12600 \f
12601 /* Verify that all the registers and memory references mentioned in *LOC are
12602 still valid. *LOC was part of a value set in INSN when label_tick was
12603 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12604 the invalid references with (clobber (const_int 0)) and return 1. This
12605 replacement is useful because we often can get useful information about
12606 the form of a value (e.g., if it was produced by a shift that always
12607 produces -1 or 0) even though we don't know exactly what registers it
12608 was produced from. */
12609
12610 static int
12611 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12612 {
12613 rtx x = *loc;
12614 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12615 int len = GET_RTX_LENGTH (GET_CODE (x));
12616 int i, j;
12617
12618 if (REG_P (x))
12619 {
12620 unsigned int regno = REGNO (x);
12621 unsigned int endregno = END_REGNO (x);
12622 unsigned int j;
12623
12624 for (j = regno; j < endregno; j++)
12625 {
12626 reg_stat_type *rsp = &reg_stat[j];
12627 if (rsp->last_set_invalid
12628 /* If this is a pseudo-register that was only set once and not
12629 live at the beginning of the function, it is always valid. */
12630 || (! (regno >= FIRST_PSEUDO_REGISTER
12631 && REG_N_SETS (regno) == 1
12632 && (!REGNO_REG_SET_P
12633 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12634 regno)))
12635 && rsp->last_set_label > tick))
12636 {
12637 if (replace)
12638 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12639 return replace;
12640 }
12641 }
12642
12643 return 1;
12644 }
12645 /* If this is a memory reference, make sure that there were no stores after
12646 it that might have clobbered the value. We don't have alias info, so we
12647 assume any store invalidates it. Moreover, we only have local UIDs, so
12648 we also assume that there were stores in the intervening basic blocks. */
12649 else if (MEM_P (x) && !MEM_READONLY_P (x)
12650 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12651 {
12652 if (replace)
12653 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12654 return replace;
12655 }
12656
12657 for (i = 0; i < len; i++)
12658 {
12659 if (fmt[i] == 'e')
12660 {
12661 /* Check for identical subexpressions. If x contains
12662 identical subexpression we only have to traverse one of
12663 them. */
12664 if (i == 1 && ARITHMETIC_P (x))
12665 {
12666 /* Note that at this point x0 has already been checked
12667 and found valid. */
12668 rtx x0 = XEXP (x, 0);
12669 rtx x1 = XEXP (x, 1);
12670
12671 /* If x0 and x1 are identical then x is also valid. */
12672 if (x0 == x1)
12673 return 1;
12674
12675 /* If x1 is identical to a subexpression of x0 then
12676 while checking x0, x1 has already been checked. Thus
12677 it is valid and so as x. */
12678 if (ARITHMETIC_P (x0)
12679 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12680 return 1;
12681
12682 /* If x0 is identical to a subexpression of x1 then x is
12683 valid iff the rest of x1 is valid. */
12684 if (ARITHMETIC_P (x1)
12685 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12686 return
12687 get_last_value_validate (&XEXP (x1,
12688 x0 == XEXP (x1, 0) ? 1 : 0),
12689 insn, tick, replace);
12690 }
12691
12692 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12693 replace) == 0)
12694 return 0;
12695 }
12696 else if (fmt[i] == 'E')
12697 for (j = 0; j < XVECLEN (x, i); j++)
12698 if (get_last_value_validate (&XVECEXP (x, i, j),
12699 insn, tick, replace) == 0)
12700 return 0;
12701 }
12702
12703 /* If we haven't found a reason for it to be invalid, it is valid. */
12704 return 1;
12705 }
12706
12707 /* Get the last value assigned to X, if known. Some registers
12708 in the value may be replaced with (clobber (const_int 0)) if their value
12709 is known longer known reliably. */
12710
12711 static rtx
12712 get_last_value (const_rtx x)
12713 {
12714 unsigned int regno;
12715 rtx value;
12716 reg_stat_type *rsp;
12717
12718 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12719 then convert it to the desired mode. If this is a paradoxical SUBREG,
12720 we cannot predict what values the "extra" bits might have. */
12721 if (GET_CODE (x) == SUBREG
12722 && subreg_lowpart_p (x)
12723 && !paradoxical_subreg_p (x)
12724 && (value = get_last_value (SUBREG_REG (x))) != 0)
12725 return gen_lowpart (GET_MODE (x), value);
12726
12727 if (!REG_P (x))
12728 return 0;
12729
12730 regno = REGNO (x);
12731 rsp = &reg_stat[regno];
12732 value = rsp->last_set_value;
12733
12734 /* If we don't have a value, or if it isn't for this basic block and
12735 it's either a hard register, set more than once, or it's a live
12736 at the beginning of the function, return 0.
12737
12738 Because if it's not live at the beginning of the function then the reg
12739 is always set before being used (is never used without being set).
12740 And, if it's set only once, and it's always set before use, then all
12741 uses must have the same last value, even if it's not from this basic
12742 block. */
12743
12744 if (value == 0
12745 || (rsp->last_set_label < label_tick_ebb_start
12746 && (regno < FIRST_PSEUDO_REGISTER
12747 || REG_N_SETS (regno) != 1
12748 || REGNO_REG_SET_P
12749 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12750 return 0;
12751
12752 /* If the value was set in a later insn than the ones we are processing,
12753 we can't use it even if the register was only set once. */
12754 if (rsp->last_set_label == label_tick
12755 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12756 return 0;
12757
12758 /* If the value has all its registers valid, return it. */
12759 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12760 return value;
12761
12762 /* Otherwise, make a copy and replace any invalid register with
12763 (clobber (const_int 0)). If that fails for some reason, return 0. */
12764
12765 value = copy_rtx (value);
12766 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
12767 return value;
12768
12769 return 0;
12770 }
12771 \f
12772 /* Return nonzero if expression X refers to a REG or to memory
12773 that is set in an instruction more recent than FROM_LUID. */
12774
12775 static int
12776 use_crosses_set_p (const_rtx x, int from_luid)
12777 {
12778 const char *fmt;
12779 int i;
12780 enum rtx_code code = GET_CODE (x);
12781
12782 if (code == REG)
12783 {
12784 unsigned int regno = REGNO (x);
12785 unsigned endreg = END_REGNO (x);
12786
12787 #ifdef PUSH_ROUNDING
12788 /* Don't allow uses of the stack pointer to be moved,
12789 because we don't know whether the move crosses a push insn. */
12790 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12791 return 1;
12792 #endif
12793 for (; regno < endreg; regno++)
12794 {
12795 reg_stat_type *rsp = &reg_stat[regno];
12796 if (rsp->last_set
12797 && rsp->last_set_label == label_tick
12798 && DF_INSN_LUID (rsp->last_set) > from_luid)
12799 return 1;
12800 }
12801 return 0;
12802 }
12803
12804 if (code == MEM && mem_last_set > from_luid)
12805 return 1;
12806
12807 fmt = GET_RTX_FORMAT (code);
12808
12809 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12810 {
12811 if (fmt[i] == 'E')
12812 {
12813 int j;
12814 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12815 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
12816 return 1;
12817 }
12818 else if (fmt[i] == 'e'
12819 && use_crosses_set_p (XEXP (x, i), from_luid))
12820 return 1;
12821 }
12822 return 0;
12823 }
12824 \f
12825 /* Define three variables used for communication between the following
12826 routines. */
12827
12828 static unsigned int reg_dead_regno, reg_dead_endregno;
12829 static int reg_dead_flag;
12830
12831 /* Function called via note_stores from reg_dead_at_p.
12832
12833 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12834 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12835
12836 static void
12837 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
12838 {
12839 unsigned int regno, endregno;
12840
12841 if (!REG_P (dest))
12842 return;
12843
12844 regno = REGNO (dest);
12845 endregno = END_REGNO (dest);
12846 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12847 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12848 }
12849
12850 /* Return nonzero if REG is known to be dead at INSN.
12851
12852 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12853 referencing REG, it is dead. If we hit a SET referencing REG, it is
12854 live. Otherwise, see if it is live or dead at the start of the basic
12855 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12856 must be assumed to be always live. */
12857
12858 static int
12859 reg_dead_at_p (rtx reg, rtx_insn *insn)
12860 {
12861 basic_block block;
12862 unsigned int i;
12863
12864 /* Set variables for reg_dead_at_p_1. */
12865 reg_dead_regno = REGNO (reg);
12866 reg_dead_endregno = END_REGNO (reg);
12867
12868 reg_dead_flag = 0;
12869
12870 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
12871 we allow the machine description to decide whether use-and-clobber
12872 patterns are OK. */
12873 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12874 {
12875 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12876 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
12877 return 0;
12878 }
12879
12880 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
12881 beginning of basic block. */
12882 block = BLOCK_FOR_INSN (insn);
12883 for (;;)
12884 {
12885 if (INSN_P (insn))
12886 {
12887 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12888 if (reg_dead_flag)
12889 return reg_dead_flag == 1 ? 1 : 0;
12890
12891 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12892 return 1;
12893 }
12894
12895 if (insn == BB_HEAD (block))
12896 break;
12897
12898 insn = PREV_INSN (insn);
12899 }
12900
12901 /* Look at live-in sets for the basic block that we were in. */
12902 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12903 if (REGNO_REG_SET_P (df_get_live_in (block), i))
12904 return 0;
12905
12906 return 1;
12907 }
12908 \f
12909 /* Note hard registers in X that are used. */
12910
12911 static void
12912 mark_used_regs_combine (rtx x)
12913 {
12914 RTX_CODE code = GET_CODE (x);
12915 unsigned int regno;
12916 int i;
12917
12918 switch (code)
12919 {
12920 case LABEL_REF:
12921 case SYMBOL_REF:
12922 case CONST:
12923 CASE_CONST_ANY:
12924 case PC:
12925 case ADDR_VEC:
12926 case ADDR_DIFF_VEC:
12927 case ASM_INPUT:
12928 #ifdef HAVE_cc0
12929 /* CC0 must die in the insn after it is set, so we don't need to take
12930 special note of it here. */
12931 case CC0:
12932 #endif
12933 return;
12934
12935 case CLOBBER:
12936 /* If we are clobbering a MEM, mark any hard registers inside the
12937 address as used. */
12938 if (MEM_P (XEXP (x, 0)))
12939 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12940 return;
12941
12942 case REG:
12943 regno = REGNO (x);
12944 /* A hard reg in a wide mode may really be multiple registers.
12945 If so, mark all of them just like the first. */
12946 if (regno < FIRST_PSEUDO_REGISTER)
12947 {
12948 /* None of this applies to the stack, frame or arg pointers. */
12949 if (regno == STACK_POINTER_REGNUM
12950 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
12951 || regno == HARD_FRAME_POINTER_REGNUM
12952 #endif
12953 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12954 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12955 #endif
12956 || regno == FRAME_POINTER_REGNUM)
12957 return;
12958
12959 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12960 }
12961 return;
12962
12963 case SET:
12964 {
12965 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12966 the address. */
12967 rtx testreg = SET_DEST (x);
12968
12969 while (GET_CODE (testreg) == SUBREG
12970 || GET_CODE (testreg) == ZERO_EXTRACT
12971 || GET_CODE (testreg) == STRICT_LOW_PART)
12972 testreg = XEXP (testreg, 0);
12973
12974 if (MEM_P (testreg))
12975 mark_used_regs_combine (XEXP (testreg, 0));
12976
12977 mark_used_regs_combine (SET_SRC (x));
12978 }
12979 return;
12980
12981 default:
12982 break;
12983 }
12984
12985 /* Recursively scan the operands of this expression. */
12986
12987 {
12988 const char *fmt = GET_RTX_FORMAT (code);
12989
12990 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12991 {
12992 if (fmt[i] == 'e')
12993 mark_used_regs_combine (XEXP (x, i));
12994 else if (fmt[i] == 'E')
12995 {
12996 int j;
12997
12998 for (j = 0; j < XVECLEN (x, i); j++)
12999 mark_used_regs_combine (XVECEXP (x, i, j));
13000 }
13001 }
13002 }
13003 }
13004 \f
13005 /* Remove register number REGNO from the dead registers list of INSN.
13006
13007 Return the note used to record the death, if there was one. */
13008
13009 rtx
13010 remove_death (unsigned int regno, rtx_insn *insn)
13011 {
13012 rtx note = find_regno_note (insn, REG_DEAD, regno);
13013
13014 if (note)
13015 remove_note (insn, note);
13016
13017 return note;
13018 }
13019
13020 /* For each register (hardware or pseudo) used within expression X, if its
13021 death is in an instruction with luid between FROM_LUID (inclusive) and
13022 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13023 list headed by PNOTES.
13024
13025 That said, don't move registers killed by maybe_kill_insn.
13026
13027 This is done when X is being merged by combination into TO_INSN. These
13028 notes will then be distributed as needed. */
13029
13030 static void
13031 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13032 rtx *pnotes)
13033 {
13034 const char *fmt;
13035 int len, i;
13036 enum rtx_code code = GET_CODE (x);
13037
13038 if (code == REG)
13039 {
13040 unsigned int regno = REGNO (x);
13041 rtx_insn *where_dead = reg_stat[regno].last_death;
13042
13043 /* Don't move the register if it gets killed in between from and to. */
13044 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13045 && ! reg_referenced_p (x, maybe_kill_insn))
13046 return;
13047
13048 if (where_dead
13049 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13050 && DF_INSN_LUID (where_dead) >= from_luid
13051 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13052 {
13053 rtx note = remove_death (regno, where_dead);
13054
13055 /* It is possible for the call above to return 0. This can occur
13056 when last_death points to I2 or I1 that we combined with.
13057 In that case make a new note.
13058
13059 We must also check for the case where X is a hard register
13060 and NOTE is a death note for a range of hard registers
13061 including X. In that case, we must put REG_DEAD notes for
13062 the remaining registers in place of NOTE. */
13063
13064 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13065 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13066 > GET_MODE_SIZE (GET_MODE (x))))
13067 {
13068 unsigned int deadregno = REGNO (XEXP (note, 0));
13069 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13070 unsigned int ourend = END_HARD_REGNO (x);
13071 unsigned int i;
13072
13073 for (i = deadregno; i < deadend; i++)
13074 if (i < regno || i >= ourend)
13075 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13076 }
13077
13078 /* If we didn't find any note, or if we found a REG_DEAD note that
13079 covers only part of the given reg, and we have a multi-reg hard
13080 register, then to be safe we must check for REG_DEAD notes
13081 for each register other than the first. They could have
13082 their own REG_DEAD notes lying around. */
13083 else if ((note == 0
13084 || (note != 0
13085 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13086 < GET_MODE_SIZE (GET_MODE (x)))))
13087 && regno < FIRST_PSEUDO_REGISTER
13088 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13089 {
13090 unsigned int ourend = END_HARD_REGNO (x);
13091 unsigned int i, offset;
13092 rtx oldnotes = 0;
13093
13094 if (note)
13095 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13096 else
13097 offset = 1;
13098
13099 for (i = regno + offset; i < ourend; i++)
13100 move_deaths (regno_reg_rtx[i],
13101 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13102 }
13103
13104 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13105 {
13106 XEXP (note, 1) = *pnotes;
13107 *pnotes = note;
13108 }
13109 else
13110 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13111 }
13112
13113 return;
13114 }
13115
13116 else if (GET_CODE (x) == SET)
13117 {
13118 rtx dest = SET_DEST (x);
13119
13120 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13121
13122 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13123 that accesses one word of a multi-word item, some
13124 piece of everything register in the expression is used by
13125 this insn, so remove any old death. */
13126 /* ??? So why do we test for equality of the sizes? */
13127
13128 if (GET_CODE (dest) == ZERO_EXTRACT
13129 || GET_CODE (dest) == STRICT_LOW_PART
13130 || (GET_CODE (dest) == SUBREG
13131 && (((GET_MODE_SIZE (GET_MODE (dest))
13132 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13133 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13134 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13135 {
13136 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13137 return;
13138 }
13139
13140 /* If this is some other SUBREG, we know it replaces the entire
13141 value, so use that as the destination. */
13142 if (GET_CODE (dest) == SUBREG)
13143 dest = SUBREG_REG (dest);
13144
13145 /* If this is a MEM, adjust deaths of anything used in the address.
13146 For a REG (the only other possibility), the entire value is
13147 being replaced so the old value is not used in this insn. */
13148
13149 if (MEM_P (dest))
13150 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13151 to_insn, pnotes);
13152 return;
13153 }
13154
13155 else if (GET_CODE (x) == CLOBBER)
13156 return;
13157
13158 len = GET_RTX_LENGTH (code);
13159 fmt = GET_RTX_FORMAT (code);
13160
13161 for (i = 0; i < len; i++)
13162 {
13163 if (fmt[i] == 'E')
13164 {
13165 int j;
13166 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13167 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13168 to_insn, pnotes);
13169 }
13170 else if (fmt[i] == 'e')
13171 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13172 }
13173 }
13174 \f
13175 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13176 pattern of an insn. X must be a REG. */
13177
13178 static int
13179 reg_bitfield_target_p (rtx x, rtx body)
13180 {
13181 int i;
13182
13183 if (GET_CODE (body) == SET)
13184 {
13185 rtx dest = SET_DEST (body);
13186 rtx target;
13187 unsigned int regno, tregno, endregno, endtregno;
13188
13189 if (GET_CODE (dest) == ZERO_EXTRACT)
13190 target = XEXP (dest, 0);
13191 else if (GET_CODE (dest) == STRICT_LOW_PART)
13192 target = SUBREG_REG (XEXP (dest, 0));
13193 else
13194 return 0;
13195
13196 if (GET_CODE (target) == SUBREG)
13197 target = SUBREG_REG (target);
13198
13199 if (!REG_P (target))
13200 return 0;
13201
13202 tregno = REGNO (target), regno = REGNO (x);
13203 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13204 return target == x;
13205
13206 endtregno = end_hard_regno (GET_MODE (target), tregno);
13207 endregno = end_hard_regno (GET_MODE (x), regno);
13208
13209 return endregno > tregno && regno < endtregno;
13210 }
13211
13212 else if (GET_CODE (body) == PARALLEL)
13213 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13214 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13215 return 1;
13216
13217 return 0;
13218 }
13219 \f
13220 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13221 as appropriate. I3 and I2 are the insns resulting from the combination
13222 insns including FROM (I2 may be zero).
13223
13224 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13225 not need REG_DEAD notes because they are being substituted for. This
13226 saves searching in the most common cases.
13227
13228 Each note in the list is either ignored or placed on some insns, depending
13229 on the type of note. */
13230
13231 static void
13232 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13233 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13234 {
13235 rtx note, next_note;
13236 rtx tem_note;
13237 rtx_insn *tem_insn;
13238
13239 for (note = notes; note; note = next_note)
13240 {
13241 rtx_insn *place = 0, *place2 = 0;
13242
13243 next_note = XEXP (note, 1);
13244 switch (REG_NOTE_KIND (note))
13245 {
13246 case REG_BR_PROB:
13247 case REG_BR_PRED:
13248 /* Doesn't matter much where we put this, as long as it's somewhere.
13249 It is preferable to keep these notes on branches, which is most
13250 likely to be i3. */
13251 place = i3;
13252 break;
13253
13254 case REG_NON_LOCAL_GOTO:
13255 if (JUMP_P (i3))
13256 place = i3;
13257 else
13258 {
13259 gcc_assert (i2 && JUMP_P (i2));
13260 place = i2;
13261 }
13262 break;
13263
13264 case REG_EH_REGION:
13265 /* These notes must remain with the call or trapping instruction. */
13266 if (CALL_P (i3))
13267 place = i3;
13268 else if (i2 && CALL_P (i2))
13269 place = i2;
13270 else
13271 {
13272 gcc_assert (cfun->can_throw_non_call_exceptions);
13273 if (may_trap_p (i3))
13274 place = i3;
13275 else if (i2 && may_trap_p (i2))
13276 place = i2;
13277 /* ??? Otherwise assume we've combined things such that we
13278 can now prove that the instructions can't trap. Drop the
13279 note in this case. */
13280 }
13281 break;
13282
13283 case REG_ARGS_SIZE:
13284 /* ??? How to distribute between i3-i1. Assume i3 contains the
13285 entire adjustment. Assert i3 contains at least some adjust. */
13286 if (!noop_move_p (i3))
13287 {
13288 int old_size, args_size = INTVAL (XEXP (note, 0));
13289 /* fixup_args_size_notes looks at REG_NORETURN note,
13290 so ensure the note is placed there first. */
13291 if (CALL_P (i3))
13292 {
13293 rtx *np;
13294 for (np = &next_note; *np; np = &XEXP (*np, 1))
13295 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13296 {
13297 rtx n = *np;
13298 *np = XEXP (n, 1);
13299 XEXP (n, 1) = REG_NOTES (i3);
13300 REG_NOTES (i3) = n;
13301 break;
13302 }
13303 }
13304 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13305 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13306 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13307 gcc_assert (old_size != args_size
13308 || (CALL_P (i3)
13309 && !ACCUMULATE_OUTGOING_ARGS
13310 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13311 }
13312 break;
13313
13314 case REG_NORETURN:
13315 case REG_SETJMP:
13316 case REG_TM:
13317 case REG_CALL_DECL:
13318 /* These notes must remain with the call. It should not be
13319 possible for both I2 and I3 to be a call. */
13320 if (CALL_P (i3))
13321 place = i3;
13322 else
13323 {
13324 gcc_assert (i2 && CALL_P (i2));
13325 place = i2;
13326 }
13327 break;
13328
13329 case REG_UNUSED:
13330 /* Any clobbers for i3 may still exist, and so we must process
13331 REG_UNUSED notes from that insn.
13332
13333 Any clobbers from i2 or i1 can only exist if they were added by
13334 recog_for_combine. In that case, recog_for_combine created the
13335 necessary REG_UNUSED notes. Trying to keep any original
13336 REG_UNUSED notes from these insns can cause incorrect output
13337 if it is for the same register as the original i3 dest.
13338 In that case, we will notice that the register is set in i3,
13339 and then add a REG_UNUSED note for the destination of i3, which
13340 is wrong. However, it is possible to have REG_UNUSED notes from
13341 i2 or i1 for register which were both used and clobbered, so
13342 we keep notes from i2 or i1 if they will turn into REG_DEAD
13343 notes. */
13344
13345 /* If this register is set or clobbered in I3, put the note there
13346 unless there is one already. */
13347 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13348 {
13349 if (from_insn != i3)
13350 break;
13351
13352 if (! (REG_P (XEXP (note, 0))
13353 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13354 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13355 place = i3;
13356 }
13357 /* Otherwise, if this register is used by I3, then this register
13358 now dies here, so we must put a REG_DEAD note here unless there
13359 is one already. */
13360 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13361 && ! (REG_P (XEXP (note, 0))
13362 ? find_regno_note (i3, REG_DEAD,
13363 REGNO (XEXP (note, 0)))
13364 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13365 {
13366 PUT_REG_NOTE_KIND (note, REG_DEAD);
13367 place = i3;
13368 }
13369 break;
13370
13371 case REG_EQUAL:
13372 case REG_EQUIV:
13373 case REG_NOALIAS:
13374 /* These notes say something about results of an insn. We can
13375 only support them if they used to be on I3 in which case they
13376 remain on I3. Otherwise they are ignored.
13377
13378 If the note refers to an expression that is not a constant, we
13379 must also ignore the note since we cannot tell whether the
13380 equivalence is still true. It might be possible to do
13381 slightly better than this (we only have a problem if I2DEST
13382 or I1DEST is present in the expression), but it doesn't
13383 seem worth the trouble. */
13384
13385 if (from_insn == i3
13386 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13387 place = i3;
13388 break;
13389
13390 case REG_INC:
13391 /* These notes say something about how a register is used. They must
13392 be present on any use of the register in I2 or I3. */
13393 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13394 place = i3;
13395
13396 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13397 {
13398 if (place)
13399 place2 = i2;
13400 else
13401 place = i2;
13402 }
13403 break;
13404
13405 case REG_LABEL_TARGET:
13406 case REG_LABEL_OPERAND:
13407 /* This can show up in several ways -- either directly in the
13408 pattern, or hidden off in the constant pool with (or without?)
13409 a REG_EQUAL note. */
13410 /* ??? Ignore the without-reg_equal-note problem for now. */
13411 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13412 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13413 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13414 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13415 place = i3;
13416
13417 if (i2
13418 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13419 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13420 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13421 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13422 {
13423 if (place)
13424 place2 = i2;
13425 else
13426 place = i2;
13427 }
13428
13429 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13430 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13431 there. */
13432 if (place && JUMP_P (place)
13433 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13434 && (JUMP_LABEL (place) == NULL
13435 || JUMP_LABEL (place) == XEXP (note, 0)))
13436 {
13437 rtx label = JUMP_LABEL (place);
13438
13439 if (!label)
13440 JUMP_LABEL (place) = XEXP (note, 0);
13441 else if (LABEL_P (label))
13442 LABEL_NUSES (label)--;
13443 }
13444
13445 if (place2 && JUMP_P (place2)
13446 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13447 && (JUMP_LABEL (place2) == NULL
13448 || JUMP_LABEL (place2) == XEXP (note, 0)))
13449 {
13450 rtx label = JUMP_LABEL (place2);
13451
13452 if (!label)
13453 JUMP_LABEL (place2) = XEXP (note, 0);
13454 else if (LABEL_P (label))
13455 LABEL_NUSES (label)--;
13456 place2 = 0;
13457 }
13458 break;
13459
13460 case REG_NONNEG:
13461 /* This note says something about the value of a register prior
13462 to the execution of an insn. It is too much trouble to see
13463 if the note is still correct in all situations. It is better
13464 to simply delete it. */
13465 break;
13466
13467 case REG_DEAD:
13468 /* If we replaced the right hand side of FROM_INSN with a
13469 REG_EQUAL note, the original use of the dying register
13470 will not have been combined into I3 and I2. In such cases,
13471 FROM_INSN is guaranteed to be the first of the combined
13472 instructions, so we simply need to search back before
13473 FROM_INSN for the previous use or set of this register,
13474 then alter the notes there appropriately.
13475
13476 If the register is used as an input in I3, it dies there.
13477 Similarly for I2, if it is nonzero and adjacent to I3.
13478
13479 If the register is not used as an input in either I3 or I2
13480 and it is not one of the registers we were supposed to eliminate,
13481 there are two possibilities. We might have a non-adjacent I2
13482 or we might have somehow eliminated an additional register
13483 from a computation. For example, we might have had A & B where
13484 we discover that B will always be zero. In this case we will
13485 eliminate the reference to A.
13486
13487 In both cases, we must search to see if we can find a previous
13488 use of A and put the death note there. */
13489
13490 if (from_insn
13491 && from_insn == i2mod
13492 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13493 tem_insn = from_insn;
13494 else
13495 {
13496 if (from_insn
13497 && CALL_P (from_insn)
13498 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13499 place = from_insn;
13500 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13501 place = i3;
13502 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13503 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13504 place = i2;
13505 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13506 && !(i2mod
13507 && reg_overlap_mentioned_p (XEXP (note, 0),
13508 i2mod_old_rhs)))
13509 || rtx_equal_p (XEXP (note, 0), elim_i1)
13510 || rtx_equal_p (XEXP (note, 0), elim_i0))
13511 break;
13512 tem_insn = i3;
13513 }
13514
13515 if (place == 0)
13516 {
13517 basic_block bb = this_basic_block;
13518
13519 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13520 {
13521 if (!NONDEBUG_INSN_P (tem_insn))
13522 {
13523 if (tem_insn == BB_HEAD (bb))
13524 break;
13525 continue;
13526 }
13527
13528 /* If the register is being set at TEM_INSN, see if that is all
13529 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13530 into a REG_UNUSED note instead. Don't delete sets to
13531 global register vars. */
13532 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13533 || !global_regs[REGNO (XEXP (note, 0))])
13534 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13535 {
13536 rtx set = single_set (tem_insn);
13537 rtx inner_dest = 0;
13538 #ifdef HAVE_cc0
13539 rtx_insn *cc0_setter = NULL;
13540 #endif
13541
13542 if (set != 0)
13543 for (inner_dest = SET_DEST (set);
13544 (GET_CODE (inner_dest) == STRICT_LOW_PART
13545 || GET_CODE (inner_dest) == SUBREG
13546 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13547 inner_dest = XEXP (inner_dest, 0))
13548 ;
13549
13550 /* Verify that it was the set, and not a clobber that
13551 modified the register.
13552
13553 CC0 targets must be careful to maintain setter/user
13554 pairs. If we cannot delete the setter due to side
13555 effects, mark the user with an UNUSED note instead
13556 of deleting it. */
13557
13558 if (set != 0 && ! side_effects_p (SET_SRC (set))
13559 && rtx_equal_p (XEXP (note, 0), inner_dest)
13560 #ifdef HAVE_cc0
13561 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13562 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13563 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13564 #endif
13565 )
13566 {
13567 /* Move the notes and links of TEM_INSN elsewhere.
13568 This might delete other dead insns recursively.
13569 First set the pattern to something that won't use
13570 any register. */
13571 rtx old_notes = REG_NOTES (tem_insn);
13572
13573 PATTERN (tem_insn) = pc_rtx;
13574 REG_NOTES (tem_insn) = NULL;
13575
13576 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13577 NULL_RTX, NULL_RTX, NULL_RTX);
13578 distribute_links (LOG_LINKS (tem_insn));
13579
13580 SET_INSN_DELETED (tem_insn);
13581 if (tem_insn == i2)
13582 i2 = NULL;
13583
13584 #ifdef HAVE_cc0
13585 /* Delete the setter too. */
13586 if (cc0_setter)
13587 {
13588 PATTERN (cc0_setter) = pc_rtx;
13589 old_notes = REG_NOTES (cc0_setter);
13590 REG_NOTES (cc0_setter) = NULL;
13591
13592 distribute_notes (old_notes, cc0_setter,
13593 cc0_setter, NULL,
13594 NULL_RTX, NULL_RTX, NULL_RTX);
13595 distribute_links (LOG_LINKS (cc0_setter));
13596
13597 SET_INSN_DELETED (cc0_setter);
13598 if (cc0_setter == i2)
13599 i2 = NULL;
13600 }
13601 #endif
13602 }
13603 else
13604 {
13605 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13606
13607 /* If there isn't already a REG_UNUSED note, put one
13608 here. Do not place a REG_DEAD note, even if
13609 the register is also used here; that would not
13610 match the algorithm used in lifetime analysis
13611 and can cause the consistency check in the
13612 scheduler to fail. */
13613 if (! find_regno_note (tem_insn, REG_UNUSED,
13614 REGNO (XEXP (note, 0))))
13615 place = tem_insn;
13616 break;
13617 }
13618 }
13619 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13620 || (CALL_P (tem_insn)
13621 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13622 {
13623 place = tem_insn;
13624
13625 /* If we are doing a 3->2 combination, and we have a
13626 register which formerly died in i3 and was not used
13627 by i2, which now no longer dies in i3 and is used in
13628 i2 but does not die in i2, and place is between i2
13629 and i3, then we may need to move a link from place to
13630 i2. */
13631 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13632 && from_insn
13633 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13634 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13635 {
13636 struct insn_link *links = LOG_LINKS (place);
13637 LOG_LINKS (place) = NULL;
13638 distribute_links (links);
13639 }
13640 break;
13641 }
13642
13643 if (tem_insn == BB_HEAD (bb))
13644 break;
13645 }
13646
13647 }
13648
13649 /* If the register is set or already dead at PLACE, we needn't do
13650 anything with this note if it is still a REG_DEAD note.
13651 We check here if it is set at all, not if is it totally replaced,
13652 which is what `dead_or_set_p' checks, so also check for it being
13653 set partially. */
13654
13655 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13656 {
13657 unsigned int regno = REGNO (XEXP (note, 0));
13658 reg_stat_type *rsp = &reg_stat[regno];
13659
13660 if (dead_or_set_p (place, XEXP (note, 0))
13661 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13662 {
13663 /* Unless the register previously died in PLACE, clear
13664 last_death. [I no longer understand why this is
13665 being done.] */
13666 if (rsp->last_death != place)
13667 rsp->last_death = 0;
13668 place = 0;
13669 }
13670 else
13671 rsp->last_death = place;
13672
13673 /* If this is a death note for a hard reg that is occupying
13674 multiple registers, ensure that we are still using all
13675 parts of the object. If we find a piece of the object
13676 that is unused, we must arrange for an appropriate REG_DEAD
13677 note to be added for it. However, we can't just emit a USE
13678 and tag the note to it, since the register might actually
13679 be dead; so we recourse, and the recursive call then finds
13680 the previous insn that used this register. */
13681
13682 if (place && regno < FIRST_PSEUDO_REGISTER
13683 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13684 {
13685 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13686 bool all_used = true;
13687 unsigned int i;
13688
13689 for (i = regno; i < endregno; i++)
13690 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13691 && ! find_regno_fusage (place, USE, i))
13692 || dead_or_set_regno_p (place, i))
13693 {
13694 all_used = false;
13695 break;
13696 }
13697
13698 if (! all_used)
13699 {
13700 /* Put only REG_DEAD notes for pieces that are
13701 not already dead or set. */
13702
13703 for (i = regno; i < endregno;
13704 i += hard_regno_nregs[i][reg_raw_mode[i]])
13705 {
13706 rtx piece = regno_reg_rtx[i];
13707 basic_block bb = this_basic_block;
13708
13709 if (! dead_or_set_p (place, piece)
13710 && ! reg_bitfield_target_p (piece,
13711 PATTERN (place)))
13712 {
13713 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13714 NULL_RTX);
13715
13716 distribute_notes (new_note, place, place,
13717 NULL, NULL_RTX, NULL_RTX,
13718 NULL_RTX);
13719 }
13720 else if (! refers_to_regno_p (i, i + 1,
13721 PATTERN (place), 0)
13722 && ! find_regno_fusage (place, USE, i))
13723 for (tem_insn = PREV_INSN (place); ;
13724 tem_insn = PREV_INSN (tem_insn))
13725 {
13726 if (!NONDEBUG_INSN_P (tem_insn))
13727 {
13728 if (tem_insn == BB_HEAD (bb))
13729 break;
13730 continue;
13731 }
13732 if (dead_or_set_p (tem_insn, piece)
13733 || reg_bitfield_target_p (piece,
13734 PATTERN (tem_insn)))
13735 {
13736 add_reg_note (tem_insn, REG_UNUSED, piece);
13737 break;
13738 }
13739 }
13740 }
13741
13742 place = 0;
13743 }
13744 }
13745 }
13746 break;
13747
13748 default:
13749 /* Any other notes should not be present at this point in the
13750 compilation. */
13751 gcc_unreachable ();
13752 }
13753
13754 if (place)
13755 {
13756 XEXP (note, 1) = REG_NOTES (place);
13757 REG_NOTES (place) = note;
13758 }
13759
13760 if (place2)
13761 add_shallow_copy_of_reg_note (place2, note);
13762 }
13763 }
13764 \f
13765 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13766 I3, I2, and I1 to new locations. This is also called to add a link
13767 pointing at I3 when I3's destination is changed. */
13768
13769 static void
13770 distribute_links (struct insn_link *links)
13771 {
13772 struct insn_link *link, *next_link;
13773
13774 for (link = links; link; link = next_link)
13775 {
13776 rtx_insn *place = 0;
13777 rtx_insn *insn;
13778 rtx set, reg;
13779
13780 next_link = link->next;
13781
13782 /* If the insn that this link points to is a NOTE or isn't a single
13783 set, ignore it. In the latter case, it isn't clear what we
13784 can do other than ignore the link, since we can't tell which
13785 register it was for. Such links wouldn't be used by combine
13786 anyway.
13787
13788 It is not possible for the destination of the target of the link to
13789 have been changed by combine. The only potential of this is if we
13790 replace I3, I2, and I1 by I3 and I2. But in that case the
13791 destination of I2 also remains unchanged. */
13792
13793 if (NOTE_P (link->insn)
13794 || (set = single_set (link->insn)) == 0)
13795 continue;
13796
13797 reg = SET_DEST (set);
13798 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13799 || GET_CODE (reg) == STRICT_LOW_PART)
13800 reg = XEXP (reg, 0);
13801
13802 /* A LOG_LINK is defined as being placed on the first insn that uses
13803 a register and points to the insn that sets the register. Start
13804 searching at the next insn after the target of the link and stop
13805 when we reach a set of the register or the end of the basic block.
13806
13807 Note that this correctly handles the link that used to point from
13808 I3 to I2. Also note that not much searching is typically done here
13809 since most links don't point very far away. */
13810
13811 for (insn = NEXT_INSN (link->insn);
13812 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
13813 || BB_HEAD (this_basic_block->next_bb) != insn));
13814 insn = NEXT_INSN (insn))
13815 if (DEBUG_INSN_P (insn))
13816 continue;
13817 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13818 {
13819 if (reg_referenced_p (reg, PATTERN (insn)))
13820 place = insn;
13821 break;
13822 }
13823 else if (CALL_P (insn)
13824 && find_reg_fusage (insn, USE, reg))
13825 {
13826 place = insn;
13827 break;
13828 }
13829 else if (INSN_P (insn) && reg_set_p (reg, insn))
13830 break;
13831
13832 /* If we found a place to put the link, place it there unless there
13833 is already a link to the same insn as LINK at that point. */
13834
13835 if (place)
13836 {
13837 struct insn_link *link2;
13838
13839 FOR_EACH_LOG_LINK (link2, place)
13840 if (link2->insn == link->insn)
13841 break;
13842
13843 if (link2 == NULL)
13844 {
13845 link->next = LOG_LINKS (place);
13846 LOG_LINKS (place) = link;
13847
13848 /* Set added_links_insn to the earliest insn we added a
13849 link to. */
13850 if (added_links_insn == 0
13851 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
13852 added_links_insn = place;
13853 }
13854 }
13855 }
13856 }
13857 \f
13858 /* Check for any register or memory mentioned in EQUIV that is not
13859 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13860 of EXPR where some registers may have been replaced by constants. */
13861
13862 static bool
13863 unmentioned_reg_p (rtx equiv, rtx expr)
13864 {
13865 subrtx_iterator::array_type array;
13866 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
13867 {
13868 const_rtx x = *iter;
13869 if ((REG_P (x) || MEM_P (x))
13870 && !reg_mentioned_p (x, expr))
13871 return true;
13872 }
13873 return false;
13874 }
13875 \f
13876 DEBUG_FUNCTION void
13877 dump_combine_stats (FILE *file)
13878 {
13879 fprintf
13880 (file,
13881 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13882 combine_attempts, combine_merges, combine_extras, combine_successes);
13883 }
13884
13885 void
13886 dump_combine_total_stats (FILE *file)
13887 {
13888 fprintf
13889 (file,
13890 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13891 total_attempts, total_merges, total_extras, total_successes);
13892 }
13893 \f
13894 /* Try combining insns through substitution. */
13895 static unsigned int
13896 rest_of_handle_combine (void)
13897 {
13898 int rebuild_jump_labels_after_combine;
13899
13900 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
13901 df_note_add_problem ();
13902 df_analyze ();
13903
13904 regstat_init_n_sets_and_refs ();
13905
13906 rebuild_jump_labels_after_combine
13907 = combine_instructions (get_insns (), max_reg_num ());
13908
13909 /* Combining insns may have turned an indirect jump into a
13910 direct jump. Rebuild the JUMP_LABEL fields of jumping
13911 instructions. */
13912 if (rebuild_jump_labels_after_combine)
13913 {
13914 timevar_push (TV_JUMP);
13915 rebuild_jump_labels (get_insns ());
13916 cleanup_cfg (0);
13917 timevar_pop (TV_JUMP);
13918 }
13919
13920 regstat_free_n_sets_and_refs ();
13921 return 0;
13922 }
13923
13924 namespace {
13925
13926 const pass_data pass_data_combine =
13927 {
13928 RTL_PASS, /* type */
13929 "combine", /* name */
13930 OPTGROUP_NONE, /* optinfo_flags */
13931 TV_COMBINE, /* tv_id */
13932 PROP_cfglayout, /* properties_required */
13933 0, /* properties_provided */
13934 0, /* properties_destroyed */
13935 0, /* todo_flags_start */
13936 TODO_df_finish, /* todo_flags_finish */
13937 };
13938
13939 class pass_combine : public rtl_opt_pass
13940 {
13941 public:
13942 pass_combine (gcc::context *ctxt)
13943 : rtl_opt_pass (pass_data_combine, ctxt)
13944 {}
13945
13946 /* opt_pass methods: */
13947 virtual bool gate (function *) { return (optimize > 0); }
13948 virtual unsigned int execute (function *)
13949 {
13950 return rest_of_handle_combine ();
13951 }
13952
13953 }; // class pass_combine
13954
13955 } // anon namespace
13956
13957 rtl_opt_pass *
13958 make_pass_combine (gcc::context *ctxt)
13959 {
13960 return new pass_combine (ctxt);
13961 }