combine.c (gen_binary): Remove.
[gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
25
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
31
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
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 created by
53 flow.c aren't completely updated:
54
55 - reg_live_length is not updated
56 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57 removed because there is no way to know which register it was
58 linking
59
60 To simplify substitution, we combine only when the earlier insn(s)
61 consist of only a single assignment. To simplify updating afterward,
62 we never combine when a subroutine call appears in the middle.
63
64 Since we do not represent assignments to CC0 explicitly except when that
65 is all an insn does, there is no LOG_LINKS entry in an insn that uses
66 the condition code for the insn that set the condition code.
67 Fortunately, these two insns must be consecutive.
68 Therefore, every JUMP_INSN is taken to have an implicit logical link
69 to the preceding insn. This is not quite right, since non-jumps can
70 also use the condition code; but in practice such insns would not
71 combine anyway. */
72
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tree.h"
79 #include "tm_p.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
87 #include "expr.h"
88 #include "insn-attr.h"
89 #include "recog.h"
90 #include "real.h"
91 #include "toplev.h"
92 #include "target.h"
93 #include "rtlhooks-def.h"
94
95 /* Number of attempts to combine instructions in this function. */
96
97 static int combine_attempts;
98
99 /* Number of attempts that got as far as substitution in this function. */
100
101 static int combine_merges;
102
103 /* Number of instructions combined with added SETs in this function. */
104
105 static int combine_extras;
106
107 /* Number of instructions combined in this function. */
108
109 static int combine_successes;
110
111 /* Totals over entire compilation. */
112
113 static int total_attempts, total_merges, total_extras, total_successes;
114
115 \f
116 /* Vector mapping INSN_UIDs to cuids.
117 The cuids are like uids but increase monotonically always.
118 Combine always uses cuids so that it can compare them.
119 But actually renumbering the uids, which we used to do,
120 proves to be a bad idea because it makes it hard to compare
121 the dumps produced by earlier passes with those from later passes. */
122
123 static int *uid_cuid;
124 static int max_uid_cuid;
125
126 /* Get the cuid of an insn. */
127
128 #define INSN_CUID(INSN) \
129 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
130
131 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
132 BITS_PER_WORD would invoke undefined behavior. Work around it. */
133
134 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
135 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
136
137 /* Maximum register number, which is the size of the tables below. */
138
139 static unsigned int combine_max_regno;
140
141 struct reg_stat {
142 /* Record last point of death of (hard or pseudo) register n. */
143 rtx last_death;
144
145 /* Record last point of modification of (hard or pseudo) register n. */
146 rtx last_set;
147
148 /* The next group of fields allows the recording of the last value assigned
149 to (hard or pseudo) register n. We use this information to see if an
150 operation being processed is redundant given a prior operation performed
151 on the register. For example, an `and' with a constant is redundant if
152 all the zero bits are already known to be turned off.
153
154 We use an approach similar to that used by cse, but change it in the
155 following ways:
156
157 (1) We do not want to reinitialize at each label.
158 (2) It is useful, but not critical, to know the actual value assigned
159 to a register. Often just its form is helpful.
160
161 Therefore, we maintain the following fields:
162
163 last_set_value the last value assigned
164 last_set_label records the value of label_tick when the
165 register was assigned
166 last_set_table_tick records the value of label_tick when a
167 value using the register is assigned
168 last_set_invalid set to nonzero when it is not valid
169 to use the value of this register in some
170 register's value
171
172 To understand the usage of these tables, it is important to understand
173 the distinction between the value in last_set_value being valid and
174 the register being validly contained in some other expression in the
175 table.
176
177 (The next two parameters are out of date).
178
179 reg_stat[i].last_set_value is valid if it is nonzero, and either
180 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
181
182 Register I may validly appear in any expression returned for the value
183 of another register if reg_n_sets[i] is 1. It may also appear in the
184 value for register J if reg_stat[j].last_set_invalid is zero, or
185 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
186
187 If an expression is found in the table containing a register which may
188 not validly appear in an expression, the register is replaced by
189 something that won't match, (clobber (const_int 0)). */
190
191 /* Record last value assigned to (hard or pseudo) register n. */
192
193 rtx last_set_value;
194
195 /* Record the value of label_tick when an expression involving register n
196 is placed in last_set_value. */
197
198 int last_set_table_tick;
199
200 /* Record the value of label_tick when the value for register n is placed in
201 last_set_value. */
202
203 int last_set_label;
204
205 /* These fields are maintained in parallel with last_set_value and are
206 used to store the mode in which the register was last set, te bits
207 that were known to be zero when it was last set, and the number of
208 sign bits copies it was known to have when it was last set. */
209
210 unsigned HOST_WIDE_INT last_set_nonzero_bits;
211 char last_set_sign_bit_copies;
212 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
213
214 /* Set nonzero if references to register n in expressions should not be
215 used. last_set_invalid is set nonzero when this register is being
216 assigned to and last_set_table_tick == label_tick. */
217
218 char last_set_invalid;
219
220 /* Some registers that are set more than once and used in more than one
221 basic block are nevertheless always set in similar ways. For example,
222 a QImode register may be loaded from memory in two places on a machine
223 where byte loads zero extend.
224
225 We record in the following fields if a register has some leading bits
226 that are always equal to the sign bit, and what we know about the
227 nonzero bits of a register, specifically which bits are known to be
228 zero.
229
230 If an entry is zero, it means that we don't know anything special. */
231
232 unsigned char sign_bit_copies;
233
234 unsigned HOST_WIDE_INT nonzero_bits;
235 };
236
237 static struct reg_stat *reg_stat;
238
239 /* Record the cuid of the last insn that invalidated memory
240 (anything that writes memory, and subroutine calls, but not pushes). */
241
242 static int mem_last_set;
243
244 /* Record the cuid of the last CALL_INSN
245 so we can tell whether a potential combination crosses any calls. */
246
247 static int last_call_cuid;
248
249 /* When `subst' is called, this is the insn that is being modified
250 (by combining in a previous insn). The PATTERN of this insn
251 is still the old pattern partially modified and it should not be
252 looked at, but this may be used to examine the successors of the insn
253 to judge whether a simplification is valid. */
254
255 static rtx subst_insn;
256
257 /* This is the lowest CUID that `subst' is currently dealing with.
258 get_last_value will not return a value if the register was set at or
259 after this CUID. If not for this mechanism, we could get confused if
260 I2 or I1 in try_combine were an insn that used the old value of a register
261 to obtain a new value. In that case, we might erroneously get the
262 new value of the register when we wanted the old one. */
263
264 static int subst_low_cuid;
265
266 /* This contains any hard registers that are used in newpat; reg_dead_at_p
267 must consider all these registers to be always live. */
268
269 static HARD_REG_SET newpat_used_regs;
270
271 /* This is an insn to which a LOG_LINKS entry has been added. If this
272 insn is the earlier than I2 or I3, combine should rescan starting at
273 that location. */
274
275 static rtx added_links_insn;
276
277 /* Basic block in which we are performing combines. */
278 static basic_block this_basic_block;
279
280 /* A bitmap indicating which blocks had registers go dead at entry.
281 After combine, we'll need to re-do global life analysis with
282 those blocks as starting points. */
283 static sbitmap refresh_blocks;
284 \f
285 /* Incremented for each label. */
286
287 static int label_tick;
288
289 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
290 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
291
292 static enum machine_mode nonzero_bits_mode;
293
294 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
295 be safely used. It is zero while computing them and after combine has
296 completed. This former test prevents propagating values based on
297 previously set values, which can be incorrect if a variable is modified
298 in a loop. */
299
300 static int nonzero_sign_valid;
301
302 \f
303 /* Record one modification to rtl structure
304 to be undone by storing old_contents into *where.
305 is_int is 1 if the contents are an int. */
306
307 struct undo
308 {
309 struct undo *next;
310 int is_int;
311 union {rtx r; int i;} old_contents;
312 union {rtx *r; int *i;} where;
313 };
314
315 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
316 num_undo says how many are currently recorded.
317
318 other_insn is nonzero if we have modified some other insn in the process
319 of working on subst_insn. It must be verified too. */
320
321 struct undobuf
322 {
323 struct undo *undos;
324 struct undo *frees;
325 rtx other_insn;
326 };
327
328 static struct undobuf undobuf;
329
330 /* Number of times the pseudo being substituted for
331 was found and replaced. */
332
333 static int n_occurrences;
334
335 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
336 enum machine_mode,
337 unsigned HOST_WIDE_INT,
338 unsigned HOST_WIDE_INT *);
339 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
340 enum machine_mode,
341 unsigned int, unsigned int *);
342 static void do_SUBST (rtx *, rtx);
343 static void do_SUBST_INT (int *, int);
344 static void init_reg_last (void);
345 static void setup_incoming_promotions (void);
346 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
347 static int cant_combine_insn_p (rtx);
348 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
349 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
350 static int contains_muldiv (rtx);
351 static rtx try_combine (rtx, rtx, rtx, int *);
352 static void undo_all (void);
353 static void undo_commit (void);
354 static rtx *find_split_point (rtx *, rtx);
355 static rtx subst (rtx, rtx, rtx, int, int);
356 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
357 static rtx simplify_if_then_else (rtx);
358 static rtx simplify_set (rtx);
359 static rtx simplify_logical (rtx);
360 static rtx expand_compound_operation (rtx);
361 static rtx expand_field_assignment (rtx);
362 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
363 rtx, unsigned HOST_WIDE_INT, int, int, int);
364 static rtx extract_left_shift (rtx, int);
365 static rtx make_compound_operation (rtx, enum rtx_code);
366 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
367 unsigned HOST_WIDE_INT *);
368 static rtx force_to_mode (rtx, enum machine_mode,
369 unsigned HOST_WIDE_INT, rtx, int);
370 static rtx if_then_else_cond (rtx, rtx *, rtx *);
371 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
372 static int rtx_equal_for_field_assignment_p (rtx, rtx);
373 static rtx make_field_assignment (rtx);
374 static rtx apply_distributive_law (rtx);
375 static rtx distribute_and_simplify_rtx (rtx);
376 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
377 unsigned HOST_WIDE_INT);
378 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
379 HOST_WIDE_INT, enum machine_mode, int *);
380 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
381 int);
382 static int recog_for_combine (rtx *, rtx, rtx *);
383 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
384 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
385 static void update_table_tick (rtx);
386 static void record_value_for_reg (rtx, rtx, rtx);
387 static void check_promoted_subreg (rtx, rtx);
388 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
389 static void record_dead_and_set_regs (rtx);
390 static int get_last_value_validate (rtx *, rtx, int, int);
391 static rtx get_last_value (rtx);
392 static int use_crosses_set_p (rtx, int);
393 static void reg_dead_at_p_1 (rtx, rtx, void *);
394 static int reg_dead_at_p (rtx, rtx);
395 static void move_deaths (rtx, rtx, int, rtx, rtx *);
396 static int reg_bitfield_target_p (rtx, rtx);
397 static void distribute_notes (rtx, rtx, rtx, rtx);
398 static void distribute_links (rtx);
399 static void mark_used_regs_combine (rtx);
400 static int insn_cuid (rtx);
401 static void record_promoted_value (rtx, rtx);
402 static rtx reversed_comparison (rtx, enum machine_mode, rtx, rtx);
403 static enum rtx_code combine_reversed_comparison_code (rtx);
404 static int unmentioned_reg_p_1 (rtx *, void *);
405 static bool unmentioned_reg_p (rtx, rtx);
406 \f
407
408 /* It is not safe to use ordinary gen_lowpart in combine.
409 See comments in gen_lowpart_for_combine. */
410 #undef RTL_HOOKS_GEN_LOWPART
411 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
412
413 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
414 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
415
416 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
417 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
418
419 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
420
421 \f
422 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
423 insn. The substitution can be undone by undo_all. If INTO is already
424 set to NEWVAL, do not record this change. Because computing NEWVAL might
425 also call SUBST, we have to compute it before we put anything into
426 the undo table. */
427
428 static void
429 do_SUBST (rtx *into, rtx newval)
430 {
431 struct undo *buf;
432 rtx oldval = *into;
433
434 if (oldval == newval)
435 return;
436
437 /* We'd like to catch as many invalid transformations here as
438 possible. Unfortunately, there are way too many mode changes
439 that are perfectly valid, so we'd waste too much effort for
440 little gain doing the checks here. Focus on catching invalid
441 transformations involving integer constants. */
442 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
443 && GET_CODE (newval) == CONST_INT)
444 {
445 /* Sanity check that we're replacing oldval with a CONST_INT
446 that is a valid sign-extension for the original mode. */
447 if (INTVAL (newval) != trunc_int_for_mode (INTVAL (newval),
448 GET_MODE (oldval)))
449 abort ();
450
451 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
452 CONST_INT is not valid, because after the replacement, the
453 original mode would be gone. Unfortunately, we can't tell
454 when do_SUBST is called to replace the operand thereof, so we
455 perform this test on oldval instead, checking whether an
456 invalid replacement took place before we got here. */
457 if ((GET_CODE (oldval) == SUBREG
458 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT)
459 || (GET_CODE (oldval) == ZERO_EXTEND
460 && GET_CODE (XEXP (oldval, 0)) == CONST_INT))
461 abort ();
462 }
463
464 if (undobuf.frees)
465 buf = undobuf.frees, undobuf.frees = buf->next;
466 else
467 buf = xmalloc (sizeof (struct undo));
468
469 buf->is_int = 0;
470 buf->where.r = into;
471 buf->old_contents.r = oldval;
472 *into = newval;
473
474 buf->next = undobuf.undos, undobuf.undos = buf;
475 }
476
477 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
478
479 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
480 for the value of a HOST_WIDE_INT value (including CONST_INT) is
481 not safe. */
482
483 static void
484 do_SUBST_INT (int *into, int newval)
485 {
486 struct undo *buf;
487 int oldval = *into;
488
489 if (oldval == newval)
490 return;
491
492 if (undobuf.frees)
493 buf = undobuf.frees, undobuf.frees = buf->next;
494 else
495 buf = xmalloc (sizeof (struct undo));
496
497 buf->is_int = 1;
498 buf->where.i = into;
499 buf->old_contents.i = oldval;
500 *into = newval;
501
502 buf->next = undobuf.undos, undobuf.undos = buf;
503 }
504
505 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
506 \f
507 /* Main entry point for combiner. F is the first insn of the function.
508 NREGS is the first unused pseudo-reg number.
509
510 Return nonzero if the combiner has turned an indirect jump
511 instruction into a direct jump. */
512 int
513 combine_instructions (rtx f, unsigned int nregs)
514 {
515 rtx insn, next;
516 #ifdef HAVE_cc0
517 rtx prev;
518 #endif
519 int i;
520 rtx links, nextlinks;
521
522 int new_direct_jump_p = 0;
523
524 combine_attempts = 0;
525 combine_merges = 0;
526 combine_extras = 0;
527 combine_successes = 0;
528
529 combine_max_regno = nregs;
530
531 rtl_hooks = combine_rtl_hooks;
532
533 reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
534
535 init_recog_no_volatile ();
536
537 /* Compute maximum uid value so uid_cuid can be allocated. */
538
539 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
540 if (INSN_UID (insn) > i)
541 i = INSN_UID (insn);
542
543 uid_cuid = xmalloc ((i + 1) * sizeof (int));
544 max_uid_cuid = i;
545
546 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
547
548 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
549 problems when, for example, we have j <<= 1 in a loop. */
550
551 nonzero_sign_valid = 0;
552
553 /* Compute the mapping from uids to cuids.
554 Cuids are numbers assigned to insns, like uids,
555 except that cuids increase monotonically through the code.
556
557 Scan all SETs and see if we can deduce anything about what
558 bits are known to be zero for some registers and how many copies
559 of the sign bit are known to exist for those registers.
560
561 Also set any known values so that we can use it while searching
562 for what bits are known to be set. */
563
564 label_tick = 1;
565
566 setup_incoming_promotions ();
567
568 refresh_blocks = sbitmap_alloc (last_basic_block);
569 sbitmap_zero (refresh_blocks);
570
571 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
572 {
573 uid_cuid[INSN_UID (insn)] = ++i;
574 subst_low_cuid = i;
575 subst_insn = insn;
576
577 if (INSN_P (insn))
578 {
579 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
580 NULL);
581 record_dead_and_set_regs (insn);
582
583 #ifdef AUTO_INC_DEC
584 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
585 if (REG_NOTE_KIND (links) == REG_INC)
586 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
587 NULL);
588 #endif
589 }
590
591 if (GET_CODE (insn) == CODE_LABEL)
592 label_tick++;
593 }
594
595 nonzero_sign_valid = 1;
596
597 /* Now scan all the insns in forward order. */
598
599 label_tick = 1;
600 last_call_cuid = 0;
601 mem_last_set = 0;
602 init_reg_last ();
603 setup_incoming_promotions ();
604
605 FOR_EACH_BB (this_basic_block)
606 {
607 for (insn = BB_HEAD (this_basic_block);
608 insn != NEXT_INSN (BB_END (this_basic_block));
609 insn = next ? next : NEXT_INSN (insn))
610 {
611 next = 0;
612
613 if (GET_CODE (insn) == CODE_LABEL)
614 label_tick++;
615
616 else if (INSN_P (insn))
617 {
618 /* See if we know about function return values before this
619 insn based upon SUBREG flags. */
620 check_promoted_subreg (insn, PATTERN (insn));
621
622 /* Try this insn with each insn it links back to. */
623
624 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
625 if ((next = try_combine (insn, XEXP (links, 0),
626 NULL_RTX, &new_direct_jump_p)) != 0)
627 goto retry;
628
629 /* Try each sequence of three linked insns ending with this one. */
630
631 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
632 {
633 rtx link = XEXP (links, 0);
634
635 /* If the linked insn has been replaced by a note, then there
636 is no point in pursuing this chain any further. */
637 if (GET_CODE (link) == NOTE)
638 continue;
639
640 for (nextlinks = LOG_LINKS (link);
641 nextlinks;
642 nextlinks = XEXP (nextlinks, 1))
643 if ((next = try_combine (insn, link,
644 XEXP (nextlinks, 0),
645 &new_direct_jump_p)) != 0)
646 goto retry;
647 }
648
649 #ifdef HAVE_cc0
650 /* Try to combine a jump insn that uses CC0
651 with a preceding insn that sets CC0, and maybe with its
652 logical predecessor as well.
653 This is how we make decrement-and-branch insns.
654 We need this special code because data flow connections
655 via CC0 do not get entered in LOG_LINKS. */
656
657 if (GET_CODE (insn) == JUMP_INSN
658 && (prev = prev_nonnote_insn (insn)) != 0
659 && GET_CODE (prev) == INSN
660 && sets_cc0_p (PATTERN (prev)))
661 {
662 if ((next = try_combine (insn, prev,
663 NULL_RTX, &new_direct_jump_p)) != 0)
664 goto retry;
665
666 for (nextlinks = LOG_LINKS (prev); nextlinks;
667 nextlinks = XEXP (nextlinks, 1))
668 if ((next = try_combine (insn, prev,
669 XEXP (nextlinks, 0),
670 &new_direct_jump_p)) != 0)
671 goto retry;
672 }
673
674 /* Do the same for an insn that explicitly references CC0. */
675 if (GET_CODE (insn) == INSN
676 && (prev = prev_nonnote_insn (insn)) != 0
677 && GET_CODE (prev) == INSN
678 && sets_cc0_p (PATTERN (prev))
679 && GET_CODE (PATTERN (insn)) == SET
680 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
681 {
682 if ((next = try_combine (insn, prev,
683 NULL_RTX, &new_direct_jump_p)) != 0)
684 goto retry;
685
686 for (nextlinks = LOG_LINKS (prev); nextlinks;
687 nextlinks = XEXP (nextlinks, 1))
688 if ((next = try_combine (insn, prev,
689 XEXP (nextlinks, 0),
690 &new_direct_jump_p)) != 0)
691 goto retry;
692 }
693
694 /* Finally, see if any of the insns that this insn links to
695 explicitly references CC0. If so, try this insn, that insn,
696 and its predecessor if it sets CC0. */
697 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
698 if (GET_CODE (XEXP (links, 0)) == INSN
699 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
700 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
701 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
702 && GET_CODE (prev) == INSN
703 && sets_cc0_p (PATTERN (prev))
704 && (next = try_combine (insn, XEXP (links, 0),
705 prev, &new_direct_jump_p)) != 0)
706 goto retry;
707 #endif
708
709 /* Try combining an insn with two different insns whose results it
710 uses. */
711 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
712 for (nextlinks = XEXP (links, 1); nextlinks;
713 nextlinks = XEXP (nextlinks, 1))
714 if ((next = try_combine (insn, XEXP (links, 0),
715 XEXP (nextlinks, 0),
716 &new_direct_jump_p)) != 0)
717 goto retry;
718
719 /* Try this insn with each REG_EQUAL note it links back to. */
720 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
721 {
722 rtx set, note;
723 rtx temp = XEXP (links, 0);
724 if ((set = single_set (temp)) != 0
725 && (note = find_reg_equal_equiv_note (temp)) != 0
726 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
727 /* Avoid using a register that may already been marked
728 dead by an earlier instruction. */
729 && ! unmentioned_reg_p (XEXP (note, 0), SET_SRC (set)))
730 {
731 /* Temporarily replace the set's source with the
732 contents of the REG_EQUAL note. The insn will
733 be deleted or recognized by try_combine. */
734 rtx orig = SET_SRC (set);
735 SET_SRC (set) = XEXP (note, 0);
736 next = try_combine (insn, temp, NULL_RTX,
737 &new_direct_jump_p);
738 if (next)
739 goto retry;
740 SET_SRC (set) = orig;
741 }
742 }
743
744 if (GET_CODE (insn) != NOTE)
745 record_dead_and_set_regs (insn);
746
747 retry:
748 ;
749 }
750 }
751 }
752 clear_bb_flags ();
753
754 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
755 BASIC_BLOCK (i)->flags |= BB_DIRTY);
756 new_direct_jump_p |= purge_all_dead_edges (0);
757 delete_noop_moves ();
758
759 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
760 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
761 | PROP_KILL_DEAD_CODE);
762
763 /* Clean up. */
764 sbitmap_free (refresh_blocks);
765 free (reg_stat);
766 free (uid_cuid);
767
768 {
769 struct undo *undo, *next;
770 for (undo = undobuf.frees; undo; undo = next)
771 {
772 next = undo->next;
773 free (undo);
774 }
775 undobuf.frees = 0;
776 }
777
778 total_attempts += combine_attempts;
779 total_merges += combine_merges;
780 total_extras += combine_extras;
781 total_successes += combine_successes;
782
783 nonzero_sign_valid = 0;
784 rtl_hooks = general_rtl_hooks;
785
786 /* Make recognizer allow volatile MEMs again. */
787 init_recog ();
788
789 return new_direct_jump_p;
790 }
791
792 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
793
794 static void
795 init_reg_last (void)
796 {
797 unsigned int i;
798 for (i = 0; i < combine_max_regno; i++)
799 memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
800 }
801 \f
802 /* Set up any promoted values for incoming argument registers. */
803
804 static void
805 setup_incoming_promotions (void)
806 {
807 unsigned int regno;
808 rtx reg;
809 enum machine_mode mode;
810 int unsignedp;
811 rtx first = get_insns ();
812
813 if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
814 {
815 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
816 /* Check whether this register can hold an incoming pointer
817 argument. FUNCTION_ARG_REGNO_P tests outgoing register
818 numbers, so translate if necessary due to register windows. */
819 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
820 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
821 {
822 record_value_for_reg
823 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
824 : SIGN_EXTEND),
825 GET_MODE (reg),
826 gen_rtx_CLOBBER (mode, const0_rtx)));
827 }
828 }
829 }
830 \f
831 /* Called via note_stores. If X is a pseudo that is narrower than
832 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
833
834 If we are setting only a portion of X and we can't figure out what
835 portion, assume all bits will be used since we don't know what will
836 be happening.
837
838 Similarly, set how many bits of X are known to be copies of the sign bit
839 at all locations in the function. This is the smallest number implied
840 by any set of X. */
841
842 static void
843 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
844 void *data ATTRIBUTE_UNUSED)
845 {
846 unsigned int num;
847
848 if (GET_CODE (x) == REG
849 && REGNO (x) >= FIRST_PSEUDO_REGISTER
850 /* If this register is undefined at the start of the file, we can't
851 say what its contents were. */
852 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
853 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
854 {
855 if (set == 0 || GET_CODE (set) == CLOBBER)
856 {
857 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
858 reg_stat[REGNO (x)].sign_bit_copies = 1;
859 return;
860 }
861
862 /* If this is a complex assignment, see if we can convert it into a
863 simple assignment. */
864 set = expand_field_assignment (set);
865
866 /* If this is a simple assignment, or we have a paradoxical SUBREG,
867 set what we know about X. */
868
869 if (SET_DEST (set) == x
870 || (GET_CODE (SET_DEST (set)) == SUBREG
871 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
872 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
873 && SUBREG_REG (SET_DEST (set)) == x))
874 {
875 rtx src = SET_SRC (set);
876
877 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
878 /* If X is narrower than a word and SRC is a non-negative
879 constant that would appear negative in the mode of X,
880 sign-extend it for use in reg_stat[].nonzero_bits because some
881 machines (maybe most) will actually do the sign-extension
882 and this is the conservative approach.
883
884 ??? For 2.5, try to tighten up the MD files in this regard
885 instead of this kludge. */
886
887 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
888 && GET_CODE (src) == CONST_INT
889 && INTVAL (src) > 0
890 && 0 != (INTVAL (src)
891 & ((HOST_WIDE_INT) 1
892 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
893 src = GEN_INT (INTVAL (src)
894 | ((HOST_WIDE_INT) (-1)
895 << GET_MODE_BITSIZE (GET_MODE (x))));
896 #endif
897
898 /* Don't call nonzero_bits if it cannot change anything. */
899 if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
900 reg_stat[REGNO (x)].nonzero_bits
901 |= nonzero_bits (src, nonzero_bits_mode);
902 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
903 if (reg_stat[REGNO (x)].sign_bit_copies == 0
904 || reg_stat[REGNO (x)].sign_bit_copies > num)
905 reg_stat[REGNO (x)].sign_bit_copies = num;
906 }
907 else
908 {
909 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
910 reg_stat[REGNO (x)].sign_bit_copies = 1;
911 }
912 }
913 }
914 \f
915 /* See if INSN can be combined into I3. PRED and SUCC are optionally
916 insns that were previously combined into I3 or that will be combined
917 into the merger of INSN and I3.
918
919 Return 0 if the combination is not allowed for any reason.
920
921 If the combination is allowed, *PDEST will be set to the single
922 destination of INSN and *PSRC to the single source, and this function
923 will return 1. */
924
925 static int
926 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
927 rtx *pdest, rtx *psrc)
928 {
929 int i;
930 rtx set = 0, src, dest;
931 rtx p;
932 #ifdef AUTO_INC_DEC
933 rtx link;
934 #endif
935 int all_adjacent = (succ ? (next_active_insn (insn) == succ
936 && next_active_insn (succ) == i3)
937 : next_active_insn (insn) == i3);
938
939 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
940 or a PARALLEL consisting of such a SET and CLOBBERs.
941
942 If INSN has CLOBBER parallel parts, ignore them for our processing.
943 By definition, these happen during the execution of the insn. When it
944 is merged with another insn, all bets are off. If they are, in fact,
945 needed and aren't also supplied in I3, they may be added by
946 recog_for_combine. Otherwise, it won't match.
947
948 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
949 note.
950
951 Get the source and destination of INSN. If more than one, can't
952 combine. */
953
954 if (GET_CODE (PATTERN (insn)) == SET)
955 set = PATTERN (insn);
956 else if (GET_CODE (PATTERN (insn)) == PARALLEL
957 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
958 {
959 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
960 {
961 rtx elt = XVECEXP (PATTERN (insn), 0, i);
962 rtx note;
963
964 switch (GET_CODE (elt))
965 {
966 /* This is important to combine floating point insns
967 for the SH4 port. */
968 case USE:
969 /* Combining an isolated USE doesn't make sense.
970 We depend here on combinable_i3pat to reject them. */
971 /* The code below this loop only verifies that the inputs of
972 the SET in INSN do not change. We call reg_set_between_p
973 to verify that the REG in the USE does not change between
974 I3 and INSN.
975 If the USE in INSN was for a pseudo register, the matching
976 insn pattern will likely match any register; combining this
977 with any other USE would only be safe if we knew that the
978 used registers have identical values, or if there was
979 something to tell them apart, e.g. different modes. For
980 now, we forgo such complicated tests and simply disallow
981 combining of USES of pseudo registers with any other USE. */
982 if (GET_CODE (XEXP (elt, 0)) == REG
983 && GET_CODE (PATTERN (i3)) == PARALLEL)
984 {
985 rtx i3pat = PATTERN (i3);
986 int i = XVECLEN (i3pat, 0) - 1;
987 unsigned int regno = REGNO (XEXP (elt, 0));
988
989 do
990 {
991 rtx i3elt = XVECEXP (i3pat, 0, i);
992
993 if (GET_CODE (i3elt) == USE
994 && GET_CODE (XEXP (i3elt, 0)) == REG
995 && (REGNO (XEXP (i3elt, 0)) == regno
996 ? reg_set_between_p (XEXP (elt, 0),
997 PREV_INSN (insn), i3)
998 : regno >= FIRST_PSEUDO_REGISTER))
999 return 0;
1000 }
1001 while (--i >= 0);
1002 }
1003 break;
1004
1005 /* We can ignore CLOBBERs. */
1006 case CLOBBER:
1007 break;
1008
1009 case SET:
1010 /* Ignore SETs whose result isn't used but not those that
1011 have side-effects. */
1012 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1013 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1014 || INTVAL (XEXP (note, 0)) <= 0)
1015 && ! side_effects_p (elt))
1016 break;
1017
1018 /* If we have already found a SET, this is a second one and
1019 so we cannot combine with this insn. */
1020 if (set)
1021 return 0;
1022
1023 set = elt;
1024 break;
1025
1026 default:
1027 /* Anything else means we can't combine. */
1028 return 0;
1029 }
1030 }
1031
1032 if (set == 0
1033 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1034 so don't do anything with it. */
1035 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1036 return 0;
1037 }
1038 else
1039 return 0;
1040
1041 if (set == 0)
1042 return 0;
1043
1044 set = expand_field_assignment (set);
1045 src = SET_SRC (set), dest = SET_DEST (set);
1046
1047 /* Don't eliminate a store in the stack pointer. */
1048 if (dest == stack_pointer_rtx
1049 /* Don't combine with an insn that sets a register to itself if it has
1050 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1051 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1052 /* Can't merge an ASM_OPERANDS. */
1053 || GET_CODE (src) == ASM_OPERANDS
1054 /* Can't merge a function call. */
1055 || GET_CODE (src) == CALL
1056 /* Don't eliminate a function call argument. */
1057 || (GET_CODE (i3) == CALL_INSN
1058 && (find_reg_fusage (i3, USE, dest)
1059 || (GET_CODE (dest) == REG
1060 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1061 && global_regs[REGNO (dest)])))
1062 /* Don't substitute into an incremented register. */
1063 || FIND_REG_INC_NOTE (i3, dest)
1064 || (succ && FIND_REG_INC_NOTE (succ, dest))
1065 #if 0
1066 /* Don't combine the end of a libcall into anything. */
1067 /* ??? This gives worse code, and appears to be unnecessary, since no
1068 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1069 use REG_RETVAL notes for noconflict blocks, but other code here
1070 makes sure that those insns don't disappear. */
1071 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1072 #endif
1073 /* Make sure that DEST is not used after SUCC but before I3. */
1074 || (succ && ! all_adjacent
1075 && reg_used_between_p (dest, succ, i3))
1076 /* Make sure that the value that is to be substituted for the register
1077 does not use any registers whose values alter in between. However,
1078 If the insns are adjacent, a use can't cross a set even though we
1079 think it might (this can happen for a sequence of insns each setting
1080 the same destination; last_set of that register might point to
1081 a NOTE). If INSN has a REG_EQUIV note, the register is always
1082 equivalent to the memory so the substitution is valid even if there
1083 are intervening stores. Also, don't move a volatile asm or
1084 UNSPEC_VOLATILE across any other insns. */
1085 || (! all_adjacent
1086 && (((GET_CODE (src) != MEM
1087 || ! find_reg_note (insn, REG_EQUIV, src))
1088 && use_crosses_set_p (src, INSN_CUID (insn)))
1089 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1090 || GET_CODE (src) == UNSPEC_VOLATILE))
1091 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1092 better register allocation by not doing the combine. */
1093 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1094 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1095 /* Don't combine across a CALL_INSN, because that would possibly
1096 change whether the life span of some REGs crosses calls or not,
1097 and it is a pain to update that information.
1098 Exception: if source is a constant, moving it later can't hurt.
1099 Accept that special case, because it helps -fforce-addr a lot. */
1100 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1101 return 0;
1102
1103 /* DEST must either be a REG or CC0. */
1104 if (GET_CODE (dest) == REG)
1105 {
1106 /* If register alignment is being enforced for multi-word items in all
1107 cases except for parameters, it is possible to have a register copy
1108 insn referencing a hard register that is not allowed to contain the
1109 mode being copied and which would not be valid as an operand of most
1110 insns. Eliminate this problem by not combining with such an insn.
1111
1112 Also, on some machines we don't want to extend the life of a hard
1113 register. */
1114
1115 if (GET_CODE (src) == REG
1116 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1117 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1118 /* Don't extend the life of a hard register unless it is
1119 user variable (if we have few registers) or it can't
1120 fit into the desired register (meaning something special
1121 is going on).
1122 Also avoid substituting a return register into I3, because
1123 reload can't handle a conflict with constraints of other
1124 inputs. */
1125 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1126 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1127 return 0;
1128 }
1129 else if (GET_CODE (dest) != CC0)
1130 return 0;
1131
1132 /* Don't substitute for a register intended as a clobberable operand.
1133 Similarly, don't substitute an expression containing a register that
1134 will be clobbered in I3. */
1135 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1136 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1137 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1138 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1139 src)
1140 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1141 return 0;
1142
1143 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1144 or not), reject, unless nothing volatile comes between it and I3 */
1145
1146 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1147 {
1148 /* Make sure succ doesn't contain a volatile reference. */
1149 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1150 return 0;
1151
1152 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1153 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1154 return 0;
1155 }
1156
1157 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1158 to be an explicit register variable, and was chosen for a reason. */
1159
1160 if (GET_CODE (src) == ASM_OPERANDS
1161 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1162 return 0;
1163
1164 /* If there are any volatile insns between INSN and I3, reject, because
1165 they might affect machine state. */
1166
1167 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1168 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1169 return 0;
1170
1171 /* If INSN or I2 contains an autoincrement or autodecrement,
1172 make sure that register is not used between there and I3,
1173 and not already used in I3 either.
1174 Also insist that I3 not be a jump; if it were one
1175 and the incremented register were spilled, we would lose. */
1176
1177 #ifdef AUTO_INC_DEC
1178 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1179 if (REG_NOTE_KIND (link) == REG_INC
1180 && (GET_CODE (i3) == JUMP_INSN
1181 || reg_used_between_p (XEXP (link, 0), insn, i3)
1182 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1183 return 0;
1184 #endif
1185
1186 #ifdef HAVE_cc0
1187 /* Don't combine an insn that follows a CC0-setting insn.
1188 An insn that uses CC0 must not be separated from the one that sets it.
1189 We do, however, allow I2 to follow a CC0-setting insn if that insn
1190 is passed as I1; in that case it will be deleted also.
1191 We also allow combining in this case if all the insns are adjacent
1192 because that would leave the two CC0 insns adjacent as well.
1193 It would be more logical to test whether CC0 occurs inside I1 or I2,
1194 but that would be much slower, and this ought to be equivalent. */
1195
1196 p = prev_nonnote_insn (insn);
1197 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1198 && ! all_adjacent)
1199 return 0;
1200 #endif
1201
1202 /* If we get here, we have passed all the tests and the combination is
1203 to be allowed. */
1204
1205 *pdest = dest;
1206 *psrc = src;
1207
1208 return 1;
1209 }
1210 \f
1211 /* LOC is the location within I3 that contains its pattern or the component
1212 of a PARALLEL of the pattern. We validate that it is valid for combining.
1213
1214 One problem is if I3 modifies its output, as opposed to replacing it
1215 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1216 so would produce an insn that is not equivalent to the original insns.
1217
1218 Consider:
1219
1220 (set (reg:DI 101) (reg:DI 100))
1221 (set (subreg:SI (reg:DI 101) 0) <foo>)
1222
1223 This is NOT equivalent to:
1224
1225 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1226 (set (reg:DI 101) (reg:DI 100))])
1227
1228 Not only does this modify 100 (in which case it might still be valid
1229 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1230
1231 We can also run into a problem if I2 sets a register that I1
1232 uses and I1 gets directly substituted into I3 (not via I2). In that
1233 case, we would be getting the wrong value of I2DEST into I3, so we
1234 must reject the combination. This case occurs when I2 and I1 both
1235 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1236 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1237 of a SET must prevent combination from occurring.
1238
1239 Before doing the above check, we first try to expand a field assignment
1240 into a set of logical operations.
1241
1242 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1243 we place a register that is both set and used within I3. If more than one
1244 such register is detected, we fail.
1245
1246 Return 1 if the combination is valid, zero otherwise. */
1247
1248 static int
1249 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1250 int i1_not_in_src, rtx *pi3dest_killed)
1251 {
1252 rtx x = *loc;
1253
1254 if (GET_CODE (x) == SET)
1255 {
1256 rtx set = x ;
1257 rtx dest = SET_DEST (set);
1258 rtx src = SET_SRC (set);
1259 rtx inner_dest = dest;
1260
1261 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1262 || GET_CODE (inner_dest) == SUBREG
1263 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1264 inner_dest = XEXP (inner_dest, 0);
1265
1266 /* Check for the case where I3 modifies its output, as discussed
1267 above. We don't want to prevent pseudos from being combined
1268 into the address of a MEM, so only prevent the combination if
1269 i1 or i2 set the same MEM. */
1270 if ((inner_dest != dest &&
1271 (GET_CODE (inner_dest) != MEM
1272 || rtx_equal_p (i2dest, inner_dest)
1273 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1274 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1275 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1276
1277 /* This is the same test done in can_combine_p except we can't test
1278 all_adjacent; we don't have to, since this instruction will stay
1279 in place, thus we are not considering increasing the lifetime of
1280 INNER_DEST.
1281
1282 Also, if this insn sets a function argument, combining it with
1283 something that might need a spill could clobber a previous
1284 function argument; the all_adjacent test in can_combine_p also
1285 checks this; here, we do a more specific test for this case. */
1286
1287 || (GET_CODE (inner_dest) == REG
1288 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1289 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1290 GET_MODE (inner_dest))))
1291 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1292 return 0;
1293
1294 /* If DEST is used in I3, it is being killed in this insn,
1295 so record that for later.
1296 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1297 STACK_POINTER_REGNUM, since these are always considered to be
1298 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1299 if (pi3dest_killed && GET_CODE (dest) == REG
1300 && reg_referenced_p (dest, PATTERN (i3))
1301 && REGNO (dest) != FRAME_POINTER_REGNUM
1302 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1303 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1304 #endif
1305 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1306 && (REGNO (dest) != ARG_POINTER_REGNUM
1307 || ! fixed_regs [REGNO (dest)])
1308 #endif
1309 && REGNO (dest) != STACK_POINTER_REGNUM)
1310 {
1311 if (*pi3dest_killed)
1312 return 0;
1313
1314 *pi3dest_killed = dest;
1315 }
1316 }
1317
1318 else if (GET_CODE (x) == PARALLEL)
1319 {
1320 int i;
1321
1322 for (i = 0; i < XVECLEN (x, 0); i++)
1323 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1324 i1_not_in_src, pi3dest_killed))
1325 return 0;
1326 }
1327
1328 return 1;
1329 }
1330 \f
1331 /* Return 1 if X is an arithmetic expression that contains a multiplication
1332 and division. We don't count multiplications by powers of two here. */
1333
1334 static int
1335 contains_muldiv (rtx x)
1336 {
1337 switch (GET_CODE (x))
1338 {
1339 case MOD: case DIV: case UMOD: case UDIV:
1340 return 1;
1341
1342 case MULT:
1343 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1344 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1345 default:
1346 if (BINARY_P (x))
1347 return contains_muldiv (XEXP (x, 0))
1348 || contains_muldiv (XEXP (x, 1));
1349
1350 if (UNARY_P (x))
1351 return contains_muldiv (XEXP (x, 0));
1352
1353 return 0;
1354 }
1355 }
1356 \f
1357 /* Determine whether INSN can be used in a combination. Return nonzero if
1358 not. This is used in try_combine to detect early some cases where we
1359 can't perform combinations. */
1360
1361 static int
1362 cant_combine_insn_p (rtx insn)
1363 {
1364 rtx set;
1365 rtx src, dest;
1366
1367 /* If this isn't really an insn, we can't do anything.
1368 This can occur when flow deletes an insn that it has merged into an
1369 auto-increment address. */
1370 if (! INSN_P (insn))
1371 return 1;
1372
1373 /* Never combine loads and stores involving hard regs that are likely
1374 to be spilled. The register allocator can usually handle such
1375 reg-reg moves by tying. If we allow the combiner to make
1376 substitutions of likely-spilled regs, we may abort in reload.
1377 As an exception, we allow combinations involving fixed regs; these are
1378 not available to the register allocator so there's no risk involved. */
1379
1380 set = single_set (insn);
1381 if (! set)
1382 return 0;
1383 src = SET_SRC (set);
1384 dest = SET_DEST (set);
1385 if (GET_CODE (src) == SUBREG)
1386 src = SUBREG_REG (src);
1387 if (GET_CODE (dest) == SUBREG)
1388 dest = SUBREG_REG (dest);
1389 if (REG_P (src) && REG_P (dest)
1390 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1391 && ! fixed_regs[REGNO (src)]
1392 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1393 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1394 && ! fixed_regs[REGNO (dest)]
1395 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1396 return 1;
1397
1398 return 0;
1399 }
1400
1401 /* Adjust INSN after we made a change to its destination.
1402
1403 Changing the destination can invalidate notes that say something about
1404 the results of the insn and a LOG_LINK pointing to the insn. */
1405
1406 static void
1407 adjust_for_new_dest (rtx insn)
1408 {
1409 rtx *loc;
1410
1411 /* For notes, be conservative and simply remove them. */
1412 loc = &REG_NOTES (insn);
1413 while (*loc)
1414 {
1415 enum reg_note kind = REG_NOTE_KIND (*loc);
1416 if (kind == REG_EQUAL || kind == REG_EQUIV)
1417 *loc = XEXP (*loc, 1);
1418 else
1419 loc = &XEXP (*loc, 1);
1420 }
1421
1422 /* The new insn will have a destination that was previously the destination
1423 of an insn just above it. Call distribute_links to make a LOG_LINK from
1424 the next use of that destination. */
1425 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1426 }
1427
1428 /* Try to combine the insns I1 and I2 into I3.
1429 Here I1 and I2 appear earlier than I3.
1430 I1 can be zero; then we combine just I2 into I3.
1431
1432 If we are combining three insns and the resulting insn is not recognized,
1433 try splitting it into two insns. If that happens, I2 and I3 are retained
1434 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1435 are pseudo-deleted.
1436
1437 Return 0 if the combination does not work. Then nothing is changed.
1438 If we did the combination, return the insn at which combine should
1439 resume scanning.
1440
1441 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1442 new direct jump instruction. */
1443
1444 static rtx
1445 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1446 {
1447 /* New patterns for I3 and I2, respectively. */
1448 rtx newpat, newi2pat = 0;
1449 int substed_i2 = 0, substed_i1 = 0;
1450 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1451 int added_sets_1, added_sets_2;
1452 /* Total number of SETs to put into I3. */
1453 int total_sets;
1454 /* Nonzero if I2's body now appears in I3. */
1455 int i2_is_used;
1456 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1457 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1458 /* Contains I3 if the destination of I3 is used in its source, which means
1459 that the old life of I3 is being killed. If that usage is placed into
1460 I2 and not in I3, a REG_DEAD note must be made. */
1461 rtx i3dest_killed = 0;
1462 /* SET_DEST and SET_SRC of I2 and I1. */
1463 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1464 /* PATTERN (I2), or a copy of it in certain cases. */
1465 rtx i2pat;
1466 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1467 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1468 int i1_feeds_i3 = 0;
1469 /* Notes that must be added to REG_NOTES in I3 and I2. */
1470 rtx new_i3_notes, new_i2_notes;
1471 /* Notes that we substituted I3 into I2 instead of the normal case. */
1472 int i3_subst_into_i2 = 0;
1473 /* Notes that I1, I2 or I3 is a MULT operation. */
1474 int have_mult = 0;
1475
1476 int maxreg;
1477 rtx temp;
1478 rtx link;
1479 int i;
1480
1481 /* Exit early if one of the insns involved can't be used for
1482 combinations. */
1483 if (cant_combine_insn_p (i3)
1484 || cant_combine_insn_p (i2)
1485 || (i1 && cant_combine_insn_p (i1))
1486 /* We also can't do anything if I3 has a
1487 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1488 libcall. */
1489 #if 0
1490 /* ??? This gives worse code, and appears to be unnecessary, since no
1491 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1492 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1493 #endif
1494 )
1495 return 0;
1496
1497 combine_attempts++;
1498 undobuf.other_insn = 0;
1499
1500 /* Reset the hard register usage information. */
1501 CLEAR_HARD_REG_SET (newpat_used_regs);
1502
1503 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1504 code below, set I1 to be the earlier of the two insns. */
1505 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1506 temp = i1, i1 = i2, i2 = temp;
1507
1508 added_links_insn = 0;
1509
1510 /* First check for one important special-case that the code below will
1511 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1512 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1513 we may be able to replace that destination with the destination of I3.
1514 This occurs in the common code where we compute both a quotient and
1515 remainder into a structure, in which case we want to do the computation
1516 directly into the structure to avoid register-register copies.
1517
1518 Note that this case handles both multiple sets in I2 and also
1519 cases where I2 has a number of CLOBBER or PARALLELs.
1520
1521 We make very conservative checks below and only try to handle the
1522 most common cases of this. For example, we only handle the case
1523 where I2 and I3 are adjacent to avoid making difficult register
1524 usage tests. */
1525
1526 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1527 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1528 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1529 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1530 && GET_CODE (PATTERN (i2)) == PARALLEL
1531 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1532 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1533 below would need to check what is inside (and reg_overlap_mentioned_p
1534 doesn't support those codes anyway). Don't allow those destinations;
1535 the resulting insn isn't likely to be recognized anyway. */
1536 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1537 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1538 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1539 SET_DEST (PATTERN (i3)))
1540 && next_real_insn (i2) == i3)
1541 {
1542 rtx p2 = PATTERN (i2);
1543
1544 /* Make sure that the destination of I3,
1545 which we are going to substitute into one output of I2,
1546 is not used within another output of I2. We must avoid making this:
1547 (parallel [(set (mem (reg 69)) ...)
1548 (set (reg 69) ...)])
1549 which is not well-defined as to order of actions.
1550 (Besides, reload can't handle output reloads for this.)
1551
1552 The problem can also happen if the dest of I3 is a memory ref,
1553 if another dest in I2 is an indirect memory ref. */
1554 for (i = 0; i < XVECLEN (p2, 0); i++)
1555 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1556 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1557 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1558 SET_DEST (XVECEXP (p2, 0, i))))
1559 break;
1560
1561 if (i == XVECLEN (p2, 0))
1562 for (i = 0; i < XVECLEN (p2, 0); i++)
1563 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1564 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1565 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1566 {
1567 combine_merges++;
1568
1569 subst_insn = i3;
1570 subst_low_cuid = INSN_CUID (i2);
1571
1572 added_sets_2 = added_sets_1 = 0;
1573 i2dest = SET_SRC (PATTERN (i3));
1574
1575 /* Replace the dest in I2 with our dest and make the resulting
1576 insn the new pattern for I3. Then skip to where we
1577 validate the pattern. Everything was set up above. */
1578 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1579 SET_DEST (PATTERN (i3)));
1580
1581 newpat = p2;
1582 i3_subst_into_i2 = 1;
1583 goto validate_replacement;
1584 }
1585 }
1586
1587 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1588 one of those words to another constant, merge them by making a new
1589 constant. */
1590 if (i1 == 0
1591 && (temp = single_set (i2)) != 0
1592 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1593 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1594 && GET_CODE (SET_DEST (temp)) == REG
1595 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1596 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1597 && GET_CODE (PATTERN (i3)) == SET
1598 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1599 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1600 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1601 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1602 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1603 {
1604 HOST_WIDE_INT lo, hi;
1605
1606 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1607 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1608 else
1609 {
1610 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1611 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1612 }
1613
1614 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1615 {
1616 /* We don't handle the case of the target word being wider
1617 than a host wide int. */
1618 if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1619 abort ();
1620
1621 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1622 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1623 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1624 }
1625 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1626 hi = INTVAL (SET_SRC (PATTERN (i3)));
1627 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1628 {
1629 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1630 >> (HOST_BITS_PER_WIDE_INT - 1));
1631
1632 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1633 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1634 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1635 (INTVAL (SET_SRC (PATTERN (i3)))));
1636 if (hi == sign)
1637 hi = lo < 0 ? -1 : 0;
1638 }
1639 else
1640 /* We don't handle the case of the higher word not fitting
1641 entirely in either hi or lo. */
1642 abort ();
1643
1644 combine_merges++;
1645 subst_insn = i3;
1646 subst_low_cuid = INSN_CUID (i2);
1647 added_sets_2 = added_sets_1 = 0;
1648 i2dest = SET_DEST (temp);
1649
1650 SUBST (SET_SRC (temp),
1651 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1652
1653 newpat = PATTERN (i2);
1654 goto validate_replacement;
1655 }
1656
1657 #ifndef HAVE_cc0
1658 /* If we have no I1 and I2 looks like:
1659 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1660 (set Y OP)])
1661 make up a dummy I1 that is
1662 (set Y OP)
1663 and change I2 to be
1664 (set (reg:CC X) (compare:CC Y (const_int 0)))
1665
1666 (We can ignore any trailing CLOBBERs.)
1667
1668 This undoes a previous combination and allows us to match a branch-and-
1669 decrement insn. */
1670
1671 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1672 && XVECLEN (PATTERN (i2), 0) >= 2
1673 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1674 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1675 == MODE_CC)
1676 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1677 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1678 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1679 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1680 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1681 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1682 {
1683 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1684 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1685 break;
1686
1687 if (i == 1)
1688 {
1689 /* We make I1 with the same INSN_UID as I2. This gives it
1690 the same INSN_CUID for value tracking. Our fake I1 will
1691 never appear in the insn stream so giving it the same INSN_UID
1692 as I2 will not cause a problem. */
1693
1694 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1695 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1696 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1697 NULL_RTX);
1698
1699 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1700 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1701 SET_DEST (PATTERN (i1)));
1702 }
1703 }
1704 #endif
1705
1706 /* Verify that I2 and I1 are valid for combining. */
1707 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1708 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1709 {
1710 undo_all ();
1711 return 0;
1712 }
1713
1714 /* Record whether I2DEST is used in I2SRC and similarly for the other
1715 cases. Knowing this will help in register status updating below. */
1716 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1717 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1718 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1719
1720 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1721 in I2SRC. */
1722 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1723
1724 /* Ensure that I3's pattern can be the destination of combines. */
1725 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1726 i1 && i2dest_in_i1src && i1_feeds_i3,
1727 &i3dest_killed))
1728 {
1729 undo_all ();
1730 return 0;
1731 }
1732
1733 /* See if any of the insns is a MULT operation. Unless one is, we will
1734 reject a combination that is, since it must be slower. Be conservative
1735 here. */
1736 if (GET_CODE (i2src) == MULT
1737 || (i1 != 0 && GET_CODE (i1src) == MULT)
1738 || (GET_CODE (PATTERN (i3)) == SET
1739 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1740 have_mult = 1;
1741
1742 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1743 We used to do this EXCEPT in one case: I3 has a post-inc in an
1744 output operand. However, that exception can give rise to insns like
1745 mov r3,(r3)+
1746 which is a famous insn on the PDP-11 where the value of r3 used as the
1747 source was model-dependent. Avoid this sort of thing. */
1748
1749 #if 0
1750 if (!(GET_CODE (PATTERN (i3)) == SET
1751 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1752 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1753 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1754 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1755 /* It's not the exception. */
1756 #endif
1757 #ifdef AUTO_INC_DEC
1758 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1759 if (REG_NOTE_KIND (link) == REG_INC
1760 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1761 || (i1 != 0
1762 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1763 {
1764 undo_all ();
1765 return 0;
1766 }
1767 #endif
1768
1769 /* See if the SETs in I1 or I2 need to be kept around in the merged
1770 instruction: whenever the value set there is still needed past I3.
1771 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1772
1773 For the SET in I1, we have two cases: If I1 and I2 independently
1774 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1775 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1776 in I1 needs to be kept around unless I1DEST dies or is set in either
1777 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1778 I1DEST. If so, we know I1 feeds into I2. */
1779
1780 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1781
1782 added_sets_1
1783 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1784 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1785
1786 /* If the set in I2 needs to be kept around, we must make a copy of
1787 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1788 PATTERN (I2), we are only substituting for the original I1DEST, not into
1789 an already-substituted copy. This also prevents making self-referential
1790 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1791 I2DEST. */
1792
1793 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1794 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1795 : PATTERN (i2));
1796
1797 if (added_sets_2)
1798 i2pat = copy_rtx (i2pat);
1799
1800 combine_merges++;
1801
1802 /* Substitute in the latest insn for the regs set by the earlier ones. */
1803
1804 maxreg = max_reg_num ();
1805
1806 subst_insn = i3;
1807
1808 /* It is possible that the source of I2 or I1 may be performing an
1809 unneeded operation, such as a ZERO_EXTEND of something that is known
1810 to have the high part zero. Handle that case by letting subst look at
1811 the innermost one of them.
1812
1813 Another way to do this would be to have a function that tries to
1814 simplify a single insn instead of merging two or more insns. We don't
1815 do this because of the potential of infinite loops and because
1816 of the potential extra memory required. However, doing it the way
1817 we are is a bit of a kludge and doesn't catch all cases.
1818
1819 But only do this if -fexpensive-optimizations since it slows things down
1820 and doesn't usually win. */
1821
1822 if (flag_expensive_optimizations)
1823 {
1824 /* Pass pc_rtx so no substitutions are done, just simplifications. */
1825 if (i1)
1826 {
1827 subst_low_cuid = INSN_CUID (i1);
1828 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1829 }
1830 else
1831 {
1832 subst_low_cuid = INSN_CUID (i2);
1833 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1834 }
1835 }
1836
1837 #ifndef HAVE_cc0
1838 /* Many machines that don't use CC0 have insns that can both perform an
1839 arithmetic operation and set the condition code. These operations will
1840 be represented as a PARALLEL with the first element of the vector
1841 being a COMPARE of an arithmetic operation with the constant zero.
1842 The second element of the vector will set some pseudo to the result
1843 of the same arithmetic operation. If we simplify the COMPARE, we won't
1844 match such a pattern and so will generate an extra insn. Here we test
1845 for this case, where both the comparison and the operation result are
1846 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1847 I2SRC. Later we will make the PARALLEL that contains I2. */
1848
1849 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1850 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1851 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1852 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1853 {
1854 #ifdef SELECT_CC_MODE
1855 rtx *cc_use;
1856 enum machine_mode compare_mode;
1857 #endif
1858
1859 newpat = PATTERN (i3);
1860 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1861
1862 i2_is_used = 1;
1863
1864 #ifdef SELECT_CC_MODE
1865 /* See if a COMPARE with the operand we substituted in should be done
1866 with the mode that is currently being used. If not, do the same
1867 processing we do in `subst' for a SET; namely, if the destination
1868 is used only once, try to replace it with a register of the proper
1869 mode and also replace the COMPARE. */
1870 if (undobuf.other_insn == 0
1871 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1872 &undobuf.other_insn))
1873 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1874 i2src, const0_rtx))
1875 != GET_MODE (SET_DEST (newpat))))
1876 {
1877 unsigned int regno = REGNO (SET_DEST (newpat));
1878 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1879
1880 if (regno < FIRST_PSEUDO_REGISTER
1881 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1882 && ! REG_USERVAR_P (SET_DEST (newpat))))
1883 {
1884 if (regno >= FIRST_PSEUDO_REGISTER)
1885 SUBST (regno_reg_rtx[regno], new_dest);
1886
1887 SUBST (SET_DEST (newpat), new_dest);
1888 SUBST (XEXP (*cc_use, 0), new_dest);
1889 SUBST (SET_SRC (newpat),
1890 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1891 }
1892 else
1893 undobuf.other_insn = 0;
1894 }
1895 #endif
1896 }
1897 else
1898 #endif
1899 {
1900 n_occurrences = 0; /* `subst' counts here */
1901
1902 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1903 need to make a unique copy of I2SRC each time we substitute it
1904 to avoid self-referential rtl. */
1905
1906 subst_low_cuid = INSN_CUID (i2);
1907 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1908 ! i1_feeds_i3 && i1dest_in_i1src);
1909 substed_i2 = 1;
1910
1911 /* Record whether i2's body now appears within i3's body. */
1912 i2_is_used = n_occurrences;
1913 }
1914
1915 /* If we already got a failure, don't try to do more. Otherwise,
1916 try to substitute in I1 if we have it. */
1917
1918 if (i1 && GET_CODE (newpat) != CLOBBER)
1919 {
1920 /* Before we can do this substitution, we must redo the test done
1921 above (see detailed comments there) that ensures that I1DEST
1922 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1923
1924 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1925 0, (rtx*) 0))
1926 {
1927 undo_all ();
1928 return 0;
1929 }
1930
1931 n_occurrences = 0;
1932 subst_low_cuid = INSN_CUID (i1);
1933 newpat = subst (newpat, i1dest, i1src, 0, 0);
1934 substed_i1 = 1;
1935 }
1936
1937 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1938 to count all the ways that I2SRC and I1SRC can be used. */
1939 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1940 && i2_is_used + added_sets_2 > 1)
1941 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1942 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1943 > 1))
1944 /* Fail if we tried to make a new register (we used to abort, but there's
1945 really no reason to). */
1946 || max_reg_num () != maxreg
1947 /* Fail if we couldn't do something and have a CLOBBER. */
1948 || GET_CODE (newpat) == CLOBBER
1949 /* Fail if this new pattern is a MULT and we didn't have one before
1950 at the outer level. */
1951 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1952 && ! have_mult))
1953 {
1954 undo_all ();
1955 return 0;
1956 }
1957
1958 /* If the actions of the earlier insns must be kept
1959 in addition to substituting them into the latest one,
1960 we must make a new PARALLEL for the latest insn
1961 to hold additional the SETs. */
1962
1963 if (added_sets_1 || added_sets_2)
1964 {
1965 combine_extras++;
1966
1967 if (GET_CODE (newpat) == PARALLEL)
1968 {
1969 rtvec old = XVEC (newpat, 0);
1970 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1971 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1972 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
1973 sizeof (old->elem[0]) * old->num_elem);
1974 }
1975 else
1976 {
1977 rtx old = newpat;
1978 total_sets = 1 + added_sets_1 + added_sets_2;
1979 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1980 XVECEXP (newpat, 0, 0) = old;
1981 }
1982
1983 if (added_sets_1)
1984 XVECEXP (newpat, 0, --total_sets)
1985 = (GET_CODE (PATTERN (i1)) == PARALLEL
1986 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1987
1988 if (added_sets_2)
1989 {
1990 /* If there is no I1, use I2's body as is. We used to also not do
1991 the subst call below if I2 was substituted into I3,
1992 but that could lose a simplification. */
1993 if (i1 == 0)
1994 XVECEXP (newpat, 0, --total_sets) = i2pat;
1995 else
1996 /* See comment where i2pat is assigned. */
1997 XVECEXP (newpat, 0, --total_sets)
1998 = subst (i2pat, i1dest, i1src, 0, 0);
1999 }
2000 }
2001
2002 /* We come here when we are replacing a destination in I2 with the
2003 destination of I3. */
2004 validate_replacement:
2005
2006 /* Note which hard regs this insn has as inputs. */
2007 mark_used_regs_combine (newpat);
2008
2009 /* Is the result of combination a valid instruction? */
2010 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2011
2012 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2013 the second SET's destination is a register that is unused and isn't
2014 marked as an instruction that might trap in an EH region. In that case,
2015 we just need the first SET. This can occur when simplifying a divmod
2016 insn. We *must* test for this case here because the code below that
2017 splits two independent SETs doesn't handle this case correctly when it
2018 updates the register status.
2019
2020 It's pointless doing this if we originally had two sets, one from
2021 i3, and one from i2. Combining then splitting the parallel results
2022 in the original i2 again plus an invalid insn (which we delete).
2023 The net effect is only to move instructions around, which makes
2024 debug info less accurate.
2025
2026 Also check the case where the first SET's destination is unused.
2027 That would not cause incorrect code, but does cause an unneeded
2028 insn to remain. */
2029
2030 if (insn_code_number < 0
2031 && !(added_sets_2 && i1 == 0)
2032 && GET_CODE (newpat) == PARALLEL
2033 && XVECLEN (newpat, 0) == 2
2034 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2035 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2036 && asm_noperands (newpat) < 0)
2037 {
2038 rtx set0 = XVECEXP (newpat, 0, 0);
2039 rtx set1 = XVECEXP (newpat, 0, 1);
2040 rtx note;
2041
2042 if (((GET_CODE (SET_DEST (set1)) == REG
2043 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2044 || (GET_CODE (SET_DEST (set1)) == SUBREG
2045 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2046 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2047 || INTVAL (XEXP (note, 0)) <= 0)
2048 && ! side_effects_p (SET_SRC (set1)))
2049 {
2050 newpat = set0;
2051 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2052 }
2053
2054 else if (((GET_CODE (SET_DEST (set0)) == REG
2055 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2056 || (GET_CODE (SET_DEST (set0)) == SUBREG
2057 && find_reg_note (i3, REG_UNUSED,
2058 SUBREG_REG (SET_DEST (set0)))))
2059 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2060 || INTVAL (XEXP (note, 0)) <= 0)
2061 && ! side_effects_p (SET_SRC (set0)))
2062 {
2063 newpat = set1;
2064 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2065
2066 if (insn_code_number >= 0)
2067 {
2068 /* If we will be able to accept this, we have made a
2069 change to the destination of I3. This requires us to
2070 do a few adjustments. */
2071
2072 PATTERN (i3) = newpat;
2073 adjust_for_new_dest (i3);
2074 }
2075 }
2076 }
2077
2078 /* If we were combining three insns and the result is a simple SET
2079 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2080 insns. There are two ways to do this. It can be split using a
2081 machine-specific method (like when you have an addition of a large
2082 constant) or by combine in the function find_split_point. */
2083
2084 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2085 && asm_noperands (newpat) < 0)
2086 {
2087 rtx m_split, *split;
2088 rtx ni2dest = i2dest;
2089
2090 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2091 use I2DEST as a scratch register will help. In the latter case,
2092 convert I2DEST to the mode of the source of NEWPAT if we can. */
2093
2094 m_split = split_insns (newpat, i3);
2095
2096 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2097 inputs of NEWPAT. */
2098
2099 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2100 possible to try that as a scratch reg. This would require adding
2101 more code to make it work though. */
2102
2103 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2104 {
2105 /* If I2DEST is a hard register or the only use of a pseudo,
2106 we can change its mode. */
2107 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2108 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2109 && GET_CODE (i2dest) == REG
2110 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2111 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2112 && ! REG_USERVAR_P (i2dest))))
2113 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2114 REGNO (i2dest));
2115
2116 m_split = split_insns (gen_rtx_PARALLEL
2117 (VOIDmode,
2118 gen_rtvec (2, newpat,
2119 gen_rtx_CLOBBER (VOIDmode,
2120 ni2dest))),
2121 i3);
2122 /* If the split with the mode-changed register didn't work, try
2123 the original register. */
2124 if (! m_split && ni2dest != i2dest)
2125 {
2126 ni2dest = i2dest;
2127 m_split = split_insns (gen_rtx_PARALLEL
2128 (VOIDmode,
2129 gen_rtvec (2, newpat,
2130 gen_rtx_CLOBBER (VOIDmode,
2131 i2dest))),
2132 i3);
2133 }
2134 }
2135
2136 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2137 {
2138 m_split = PATTERN (m_split);
2139 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2140 if (insn_code_number >= 0)
2141 newpat = m_split;
2142 }
2143 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2144 && (next_real_insn (i2) == i3
2145 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2146 {
2147 rtx i2set, i3set;
2148 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2149 newi2pat = PATTERN (m_split);
2150
2151 i3set = single_set (NEXT_INSN (m_split));
2152 i2set = single_set (m_split);
2153
2154 /* In case we changed the mode of I2DEST, replace it in the
2155 pseudo-register table here. We can't do it above in case this
2156 code doesn't get executed and we do a split the other way. */
2157
2158 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2159 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2160
2161 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2162
2163 /* If I2 or I3 has multiple SETs, we won't know how to track
2164 register status, so don't use these insns. If I2's destination
2165 is used between I2 and I3, we also can't use these insns. */
2166
2167 if (i2_code_number >= 0 && i2set && i3set
2168 && (next_real_insn (i2) == i3
2169 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2170 insn_code_number = recog_for_combine (&newi3pat, i3,
2171 &new_i3_notes);
2172 if (insn_code_number >= 0)
2173 newpat = newi3pat;
2174
2175 /* It is possible that both insns now set the destination of I3.
2176 If so, we must show an extra use of it. */
2177
2178 if (insn_code_number >= 0)
2179 {
2180 rtx new_i3_dest = SET_DEST (i3set);
2181 rtx new_i2_dest = SET_DEST (i2set);
2182
2183 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2184 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2185 || GET_CODE (new_i3_dest) == SUBREG)
2186 new_i3_dest = XEXP (new_i3_dest, 0);
2187
2188 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2189 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2190 || GET_CODE (new_i2_dest) == SUBREG)
2191 new_i2_dest = XEXP (new_i2_dest, 0);
2192
2193 if (GET_CODE (new_i3_dest) == REG
2194 && GET_CODE (new_i2_dest) == REG
2195 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2196 REG_N_SETS (REGNO (new_i2_dest))++;
2197 }
2198 }
2199
2200 /* If we can split it and use I2DEST, go ahead and see if that
2201 helps things be recognized. Verify that none of the registers
2202 are set between I2 and I3. */
2203 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2204 #ifdef HAVE_cc0
2205 && GET_CODE (i2dest) == REG
2206 #endif
2207 /* We need I2DEST in the proper mode. If it is a hard register
2208 or the only use of a pseudo, we can change its mode. */
2209 && (GET_MODE (*split) == GET_MODE (i2dest)
2210 || GET_MODE (*split) == VOIDmode
2211 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2212 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2213 && ! REG_USERVAR_P (i2dest)))
2214 && (next_real_insn (i2) == i3
2215 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2216 /* We can't overwrite I2DEST if its value is still used by
2217 NEWPAT. */
2218 && ! reg_referenced_p (i2dest, newpat))
2219 {
2220 rtx newdest = i2dest;
2221 enum rtx_code split_code = GET_CODE (*split);
2222 enum machine_mode split_mode = GET_MODE (*split);
2223
2224 /* Get NEWDEST as a register in the proper mode. We have already
2225 validated that we can do this. */
2226 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2227 {
2228 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2229
2230 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2231 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2232 }
2233
2234 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2235 an ASHIFT. This can occur if it was inside a PLUS and hence
2236 appeared to be a memory address. This is a kludge. */
2237 if (split_code == MULT
2238 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2239 && INTVAL (XEXP (*split, 1)) > 0
2240 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2241 {
2242 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2243 XEXP (*split, 0), GEN_INT (i)));
2244 /* Update split_code because we may not have a multiply
2245 anymore. */
2246 split_code = GET_CODE (*split);
2247 }
2248
2249 #ifdef INSN_SCHEDULING
2250 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2251 be written as a ZERO_EXTEND. */
2252 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2253 {
2254 #ifdef LOAD_EXTEND_OP
2255 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2256 what it really is. */
2257 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2258 == SIGN_EXTEND)
2259 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2260 SUBREG_REG (*split)));
2261 else
2262 #endif
2263 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2264 SUBREG_REG (*split)));
2265 }
2266 #endif
2267
2268 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2269 SUBST (*split, newdest);
2270 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2271
2272 /* If the split point was a MULT and we didn't have one before,
2273 don't use one now. */
2274 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2275 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2276 }
2277 }
2278
2279 /* Check for a case where we loaded from memory in a narrow mode and
2280 then sign extended it, but we need both registers. In that case,
2281 we have a PARALLEL with both loads from the same memory location.
2282 We can split this into a load from memory followed by a register-register
2283 copy. This saves at least one insn, more if register allocation can
2284 eliminate the copy.
2285
2286 We cannot do this if the destination of the first assignment is a
2287 condition code register or cc0. We eliminate this case by making sure
2288 the SET_DEST and SET_SRC have the same mode.
2289
2290 We cannot do this if the destination of the second assignment is
2291 a register that we have already assumed is zero-extended. Similarly
2292 for a SUBREG of such a register. */
2293
2294 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2295 && GET_CODE (newpat) == PARALLEL
2296 && XVECLEN (newpat, 0) == 2
2297 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2298 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2299 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2300 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2301 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2302 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2303 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2304 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2305 INSN_CUID (i2))
2306 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2307 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2308 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2309 (GET_CODE (temp) == REG
2310 && reg_stat[REGNO (temp)].nonzero_bits != 0
2311 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2312 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2313 && (reg_stat[REGNO (temp)].nonzero_bits
2314 != GET_MODE_MASK (word_mode))))
2315 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2316 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2317 (GET_CODE (temp) == REG
2318 && reg_stat[REGNO (temp)].nonzero_bits != 0
2319 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2320 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2321 && (reg_stat[REGNO (temp)].nonzero_bits
2322 != GET_MODE_MASK (word_mode)))))
2323 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2324 SET_SRC (XVECEXP (newpat, 0, 1)))
2325 && ! find_reg_note (i3, REG_UNUSED,
2326 SET_DEST (XVECEXP (newpat, 0, 0))))
2327 {
2328 rtx ni2dest;
2329
2330 newi2pat = XVECEXP (newpat, 0, 0);
2331 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2332 newpat = XVECEXP (newpat, 0, 1);
2333 SUBST (SET_SRC (newpat),
2334 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2335 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2336
2337 if (i2_code_number >= 0)
2338 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2339
2340 if (insn_code_number >= 0)
2341 {
2342 rtx insn;
2343 rtx link;
2344
2345 /* If we will be able to accept this, we have made a change to the
2346 destination of I3. This requires us to do a few adjustments. */
2347 PATTERN (i3) = newpat;
2348 adjust_for_new_dest (i3);
2349
2350 /* I3 now uses what used to be its destination and which is
2351 now I2's destination. That means we need a LOG_LINK from
2352 I3 to I2. But we used to have one, so we still will.
2353
2354 However, some later insn might be using I2's dest and have
2355 a LOG_LINK pointing at I3. We must remove this link.
2356 The simplest way to remove the link is to point it at I1,
2357 which we know will be a NOTE. */
2358
2359 for (insn = NEXT_INSN (i3);
2360 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2361 || insn != BB_HEAD (this_basic_block->next_bb));
2362 insn = NEXT_INSN (insn))
2363 {
2364 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2365 {
2366 for (link = LOG_LINKS (insn); link;
2367 link = XEXP (link, 1))
2368 if (XEXP (link, 0) == i3)
2369 XEXP (link, 0) = i1;
2370
2371 break;
2372 }
2373 }
2374 }
2375 }
2376
2377 /* Similarly, check for a case where we have a PARALLEL of two independent
2378 SETs but we started with three insns. In this case, we can do the sets
2379 as two separate insns. This case occurs when some SET allows two
2380 other insns to combine, but the destination of that SET is still live. */
2381
2382 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2383 && GET_CODE (newpat) == PARALLEL
2384 && XVECLEN (newpat, 0) == 2
2385 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2386 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2387 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2388 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2389 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2390 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2391 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2392 INSN_CUID (i2))
2393 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2394 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2395 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2396 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2397 XVECEXP (newpat, 0, 0))
2398 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2399 XVECEXP (newpat, 0, 1))
2400 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2401 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2402 {
2403 /* Normally, it doesn't matter which of the two is done first,
2404 but it does if one references cc0. In that case, it has to
2405 be first. */
2406 #ifdef HAVE_cc0
2407 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2408 {
2409 newi2pat = XVECEXP (newpat, 0, 0);
2410 newpat = XVECEXP (newpat, 0, 1);
2411 }
2412 else
2413 #endif
2414 {
2415 newi2pat = XVECEXP (newpat, 0, 1);
2416 newpat = XVECEXP (newpat, 0, 0);
2417 }
2418
2419 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2420
2421 if (i2_code_number >= 0)
2422 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2423 }
2424
2425 /* If it still isn't recognized, fail and change things back the way they
2426 were. */
2427 if ((insn_code_number < 0
2428 /* Is the result a reasonable ASM_OPERANDS? */
2429 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2430 {
2431 undo_all ();
2432 return 0;
2433 }
2434
2435 /* If we had to change another insn, make sure it is valid also. */
2436 if (undobuf.other_insn)
2437 {
2438 rtx other_pat = PATTERN (undobuf.other_insn);
2439 rtx new_other_notes;
2440 rtx note, next;
2441
2442 CLEAR_HARD_REG_SET (newpat_used_regs);
2443
2444 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2445 &new_other_notes);
2446
2447 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2448 {
2449 undo_all ();
2450 return 0;
2451 }
2452
2453 PATTERN (undobuf.other_insn) = other_pat;
2454
2455 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2456 are still valid. Then add any non-duplicate notes added by
2457 recog_for_combine. */
2458 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2459 {
2460 next = XEXP (note, 1);
2461
2462 if (REG_NOTE_KIND (note) == REG_UNUSED
2463 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2464 {
2465 if (GET_CODE (XEXP (note, 0)) == REG)
2466 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2467
2468 remove_note (undobuf.other_insn, note);
2469 }
2470 }
2471
2472 for (note = new_other_notes; note; note = XEXP (note, 1))
2473 if (GET_CODE (XEXP (note, 0)) == REG)
2474 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2475
2476 distribute_notes (new_other_notes, undobuf.other_insn,
2477 undobuf.other_insn, NULL_RTX);
2478 }
2479 #ifdef HAVE_cc0
2480 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2481 they are adjacent to each other or not. */
2482 {
2483 rtx p = prev_nonnote_insn (i3);
2484 if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2485 && sets_cc0_p (newi2pat))
2486 {
2487 undo_all ();
2488 return 0;
2489 }
2490 }
2491 #endif
2492
2493 /* We now know that we can do this combination. Merge the insns and
2494 update the status of registers and LOG_LINKS. */
2495
2496 {
2497 rtx i3notes, i2notes, i1notes = 0;
2498 rtx i3links, i2links, i1links = 0;
2499 rtx midnotes = 0;
2500 unsigned int regno;
2501
2502 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2503 clear them. */
2504 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2505 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2506 if (i1)
2507 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2508
2509 /* Ensure that we do not have something that should not be shared but
2510 occurs multiple times in the new insns. Check this by first
2511 resetting all the `used' flags and then copying anything is shared. */
2512
2513 reset_used_flags (i3notes);
2514 reset_used_flags (i2notes);
2515 reset_used_flags (i1notes);
2516 reset_used_flags (newpat);
2517 reset_used_flags (newi2pat);
2518 if (undobuf.other_insn)
2519 reset_used_flags (PATTERN (undobuf.other_insn));
2520
2521 i3notes = copy_rtx_if_shared (i3notes);
2522 i2notes = copy_rtx_if_shared (i2notes);
2523 i1notes = copy_rtx_if_shared (i1notes);
2524 newpat = copy_rtx_if_shared (newpat);
2525 newi2pat = copy_rtx_if_shared (newi2pat);
2526 if (undobuf.other_insn)
2527 reset_used_flags (PATTERN (undobuf.other_insn));
2528
2529 INSN_CODE (i3) = insn_code_number;
2530 PATTERN (i3) = newpat;
2531
2532 if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2533 {
2534 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2535
2536 reset_used_flags (call_usage);
2537 call_usage = copy_rtx (call_usage);
2538
2539 if (substed_i2)
2540 replace_rtx (call_usage, i2dest, i2src);
2541
2542 if (substed_i1)
2543 replace_rtx (call_usage, i1dest, i1src);
2544
2545 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2546 }
2547
2548 if (undobuf.other_insn)
2549 INSN_CODE (undobuf.other_insn) = other_code_number;
2550
2551 /* We had one special case above where I2 had more than one set and
2552 we replaced a destination of one of those sets with the destination
2553 of I3. In that case, we have to update LOG_LINKS of insns later
2554 in this basic block. Note that this (expensive) case is rare.
2555
2556 Also, in this case, we must pretend that all REG_NOTEs for I2
2557 actually came from I3, so that REG_UNUSED notes from I2 will be
2558 properly handled. */
2559
2560 if (i3_subst_into_i2)
2561 {
2562 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2563 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2564 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2565 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2566 && ! find_reg_note (i2, REG_UNUSED,
2567 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2568 for (temp = NEXT_INSN (i2);
2569 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2570 || BB_HEAD (this_basic_block) != temp);
2571 temp = NEXT_INSN (temp))
2572 if (temp != i3 && INSN_P (temp))
2573 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2574 if (XEXP (link, 0) == i2)
2575 XEXP (link, 0) = i3;
2576
2577 if (i3notes)
2578 {
2579 rtx link = i3notes;
2580 while (XEXP (link, 1))
2581 link = XEXP (link, 1);
2582 XEXP (link, 1) = i2notes;
2583 }
2584 else
2585 i3notes = i2notes;
2586 i2notes = 0;
2587 }
2588
2589 LOG_LINKS (i3) = 0;
2590 REG_NOTES (i3) = 0;
2591 LOG_LINKS (i2) = 0;
2592 REG_NOTES (i2) = 0;
2593
2594 if (newi2pat)
2595 {
2596 INSN_CODE (i2) = i2_code_number;
2597 PATTERN (i2) = newi2pat;
2598 }
2599 else
2600 {
2601 PUT_CODE (i2, NOTE);
2602 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2603 NOTE_SOURCE_FILE (i2) = 0;
2604 }
2605
2606 if (i1)
2607 {
2608 LOG_LINKS (i1) = 0;
2609 REG_NOTES (i1) = 0;
2610 PUT_CODE (i1, NOTE);
2611 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2612 NOTE_SOURCE_FILE (i1) = 0;
2613 }
2614
2615 /* Get death notes for everything that is now used in either I3 or
2616 I2 and used to die in a previous insn. If we built two new
2617 patterns, move from I1 to I2 then I2 to I3 so that we get the
2618 proper movement on registers that I2 modifies. */
2619
2620 if (newi2pat)
2621 {
2622 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2623 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2624 }
2625 else
2626 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2627 i3, &midnotes);
2628
2629 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2630 if (i3notes)
2631 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2632 if (i2notes)
2633 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2634 if (i1notes)
2635 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2636 if (midnotes)
2637 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2638
2639 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2640 know these are REG_UNUSED and want them to go to the desired insn,
2641 so we always pass it as i3. We have not counted the notes in
2642 reg_n_deaths yet, so we need to do so now. */
2643
2644 if (newi2pat && new_i2_notes)
2645 {
2646 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2647 if (GET_CODE (XEXP (temp, 0)) == REG)
2648 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2649
2650 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2651 }
2652
2653 if (new_i3_notes)
2654 {
2655 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2656 if (GET_CODE (XEXP (temp, 0)) == REG)
2657 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2658
2659 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2660 }
2661
2662 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2663 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2664 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2665 in that case, it might delete I2. Similarly for I2 and I1.
2666 Show an additional death due to the REG_DEAD note we make here. If
2667 we discard it in distribute_notes, we will decrement it again. */
2668
2669 if (i3dest_killed)
2670 {
2671 if (GET_CODE (i3dest_killed) == REG)
2672 REG_N_DEATHS (REGNO (i3dest_killed))++;
2673
2674 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2675 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2676 NULL_RTX),
2677 NULL_RTX, i2, NULL_RTX);
2678 else
2679 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2680 NULL_RTX),
2681 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2682 }
2683
2684 if (i2dest_in_i2src)
2685 {
2686 if (GET_CODE (i2dest) == REG)
2687 REG_N_DEATHS (REGNO (i2dest))++;
2688
2689 if (newi2pat && reg_set_p (i2dest, newi2pat))
2690 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2691 NULL_RTX, i2, NULL_RTX);
2692 else
2693 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2694 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2695 }
2696
2697 if (i1dest_in_i1src)
2698 {
2699 if (GET_CODE (i1dest) == REG)
2700 REG_N_DEATHS (REGNO (i1dest))++;
2701
2702 if (newi2pat && reg_set_p (i1dest, newi2pat))
2703 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2704 NULL_RTX, i2, NULL_RTX);
2705 else
2706 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2707 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2708 }
2709
2710 distribute_links (i3links);
2711 distribute_links (i2links);
2712 distribute_links (i1links);
2713
2714 if (GET_CODE (i2dest) == REG)
2715 {
2716 rtx link;
2717 rtx i2_insn = 0, i2_val = 0, set;
2718
2719 /* The insn that used to set this register doesn't exist, and
2720 this life of the register may not exist either. See if one of
2721 I3's links points to an insn that sets I2DEST. If it does,
2722 that is now the last known value for I2DEST. If we don't update
2723 this and I2 set the register to a value that depended on its old
2724 contents, we will get confused. If this insn is used, thing
2725 will be set correctly in combine_instructions. */
2726
2727 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2728 if ((set = single_set (XEXP (link, 0))) != 0
2729 && rtx_equal_p (i2dest, SET_DEST (set)))
2730 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2731
2732 record_value_for_reg (i2dest, i2_insn, i2_val);
2733
2734 /* If the reg formerly set in I2 died only once and that was in I3,
2735 zero its use count so it won't make `reload' do any work. */
2736 if (! added_sets_2
2737 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2738 && ! i2dest_in_i2src)
2739 {
2740 regno = REGNO (i2dest);
2741 REG_N_SETS (regno)--;
2742 }
2743 }
2744
2745 if (i1 && GET_CODE (i1dest) == REG)
2746 {
2747 rtx link;
2748 rtx i1_insn = 0, i1_val = 0, set;
2749
2750 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2751 if ((set = single_set (XEXP (link, 0))) != 0
2752 && rtx_equal_p (i1dest, SET_DEST (set)))
2753 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2754
2755 record_value_for_reg (i1dest, i1_insn, i1_val);
2756
2757 regno = REGNO (i1dest);
2758 if (! added_sets_1 && ! i1dest_in_i1src)
2759 REG_N_SETS (regno)--;
2760 }
2761
2762 /* Update reg_stat[].nonzero_bits et al for any changes that may have
2763 been made to this insn. The order of
2764 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
2765 can affect nonzero_bits of newpat */
2766 if (newi2pat)
2767 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2768 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2769
2770 /* Set new_direct_jump_p if a new return or simple jump instruction
2771 has been created.
2772
2773 If I3 is now an unconditional jump, ensure that it has a
2774 BARRIER following it since it may have initially been a
2775 conditional jump. It may also be the last nonnote insn. */
2776
2777 if (returnjump_p (i3) || any_uncondjump_p (i3))
2778 {
2779 *new_direct_jump_p = 1;
2780 mark_jump_label (PATTERN (i3), i3, 0);
2781
2782 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2783 || GET_CODE (temp) != BARRIER)
2784 emit_barrier_after (i3);
2785 }
2786
2787 if (undobuf.other_insn != NULL_RTX
2788 && (returnjump_p (undobuf.other_insn)
2789 || any_uncondjump_p (undobuf.other_insn)))
2790 {
2791 *new_direct_jump_p = 1;
2792
2793 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2794 || GET_CODE (temp) != BARRIER)
2795 emit_barrier_after (undobuf.other_insn);
2796 }
2797
2798 /* An NOOP jump does not need barrier, but it does need cleaning up
2799 of CFG. */
2800 if (GET_CODE (newpat) == SET
2801 && SET_SRC (newpat) == pc_rtx
2802 && SET_DEST (newpat) == pc_rtx)
2803 *new_direct_jump_p = 1;
2804 }
2805
2806 combine_successes++;
2807 undo_commit ();
2808
2809 if (added_links_insn
2810 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2811 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2812 return added_links_insn;
2813 else
2814 return newi2pat ? i2 : i3;
2815 }
2816 \f
2817 /* Undo all the modifications recorded in undobuf. */
2818
2819 static void
2820 undo_all (void)
2821 {
2822 struct undo *undo, *next;
2823
2824 for (undo = undobuf.undos; undo; undo = next)
2825 {
2826 next = undo->next;
2827 if (undo->is_int)
2828 *undo->where.i = undo->old_contents.i;
2829 else
2830 *undo->where.r = undo->old_contents.r;
2831
2832 undo->next = undobuf.frees;
2833 undobuf.frees = undo;
2834 }
2835
2836 undobuf.undos = 0;
2837 }
2838
2839 /* We've committed to accepting the changes we made. Move all
2840 of the undos to the free list. */
2841
2842 static void
2843 undo_commit (void)
2844 {
2845 struct undo *undo, *next;
2846
2847 for (undo = undobuf.undos; undo; undo = next)
2848 {
2849 next = undo->next;
2850 undo->next = undobuf.frees;
2851 undobuf.frees = undo;
2852 }
2853 undobuf.undos = 0;
2854 }
2855
2856 \f
2857 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2858 where we have an arithmetic expression and return that point. LOC will
2859 be inside INSN.
2860
2861 try_combine will call this function to see if an insn can be split into
2862 two insns. */
2863
2864 static rtx *
2865 find_split_point (rtx *loc, rtx insn)
2866 {
2867 rtx x = *loc;
2868 enum rtx_code code = GET_CODE (x);
2869 rtx *split;
2870 unsigned HOST_WIDE_INT len = 0;
2871 HOST_WIDE_INT pos = 0;
2872 int unsignedp = 0;
2873 rtx inner = NULL_RTX;
2874
2875 /* First special-case some codes. */
2876 switch (code)
2877 {
2878 case SUBREG:
2879 #ifdef INSN_SCHEDULING
2880 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2881 point. */
2882 if (GET_CODE (SUBREG_REG (x)) == MEM)
2883 return loc;
2884 #endif
2885 return find_split_point (&SUBREG_REG (x), insn);
2886
2887 case MEM:
2888 #ifdef HAVE_lo_sum
2889 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2890 using LO_SUM and HIGH. */
2891 if (GET_CODE (XEXP (x, 0)) == CONST
2892 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2893 {
2894 SUBST (XEXP (x, 0),
2895 gen_rtx_LO_SUM (Pmode,
2896 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2897 XEXP (x, 0)));
2898 return &XEXP (XEXP (x, 0), 0);
2899 }
2900 #endif
2901
2902 /* If we have a PLUS whose second operand is a constant and the
2903 address is not valid, perhaps will can split it up using
2904 the machine-specific way to split large constants. We use
2905 the first pseudo-reg (one of the virtual regs) as a placeholder;
2906 it will not remain in the result. */
2907 if (GET_CODE (XEXP (x, 0)) == PLUS
2908 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2909 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2910 {
2911 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2912 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2913 subst_insn);
2914
2915 /* This should have produced two insns, each of which sets our
2916 placeholder. If the source of the second is a valid address,
2917 we can make put both sources together and make a split point
2918 in the middle. */
2919
2920 if (seq
2921 && NEXT_INSN (seq) != NULL_RTX
2922 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2923 && GET_CODE (seq) == INSN
2924 && GET_CODE (PATTERN (seq)) == SET
2925 && SET_DEST (PATTERN (seq)) == reg
2926 && ! reg_mentioned_p (reg,
2927 SET_SRC (PATTERN (seq)))
2928 && GET_CODE (NEXT_INSN (seq)) == INSN
2929 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2930 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2931 && memory_address_p (GET_MODE (x),
2932 SET_SRC (PATTERN (NEXT_INSN (seq)))))
2933 {
2934 rtx src1 = SET_SRC (PATTERN (seq));
2935 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2936
2937 /* Replace the placeholder in SRC2 with SRC1. If we can
2938 find where in SRC2 it was placed, that can become our
2939 split point and we can replace this address with SRC2.
2940 Just try two obvious places. */
2941
2942 src2 = replace_rtx (src2, reg, src1);
2943 split = 0;
2944 if (XEXP (src2, 0) == src1)
2945 split = &XEXP (src2, 0);
2946 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2947 && XEXP (XEXP (src2, 0), 0) == src1)
2948 split = &XEXP (XEXP (src2, 0), 0);
2949
2950 if (split)
2951 {
2952 SUBST (XEXP (x, 0), src2);
2953 return split;
2954 }
2955 }
2956
2957 /* If that didn't work, perhaps the first operand is complex and
2958 needs to be computed separately, so make a split point there.
2959 This will occur on machines that just support REG + CONST
2960 and have a constant moved through some previous computation. */
2961
2962 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
2963 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2964 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
2965 return &XEXP (XEXP (x, 0), 0);
2966 }
2967 break;
2968
2969 case SET:
2970 #ifdef HAVE_cc0
2971 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2972 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2973 we need to put the operand into a register. So split at that
2974 point. */
2975
2976 if (SET_DEST (x) == cc0_rtx
2977 && GET_CODE (SET_SRC (x)) != COMPARE
2978 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2979 && !OBJECT_P (SET_SRC (x))
2980 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2981 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
2982 return &SET_SRC (x);
2983 #endif
2984
2985 /* See if we can split SET_SRC as it stands. */
2986 split = find_split_point (&SET_SRC (x), insn);
2987 if (split && split != &SET_SRC (x))
2988 return split;
2989
2990 /* See if we can split SET_DEST as it stands. */
2991 split = find_split_point (&SET_DEST (x), insn);
2992 if (split && split != &SET_DEST (x))
2993 return split;
2994
2995 /* See if this is a bitfield assignment with everything constant. If
2996 so, this is an IOR of an AND, so split it into that. */
2997 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2998 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2999 <= HOST_BITS_PER_WIDE_INT)
3000 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3001 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3002 && GET_CODE (SET_SRC (x)) == CONST_INT
3003 && ((INTVAL (XEXP (SET_DEST (x), 1))
3004 + INTVAL (XEXP (SET_DEST (x), 2)))
3005 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3006 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3007 {
3008 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3009 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3010 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3011 rtx dest = XEXP (SET_DEST (x), 0);
3012 enum machine_mode mode = GET_MODE (dest);
3013 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3014
3015 if (BITS_BIG_ENDIAN)
3016 pos = GET_MODE_BITSIZE (mode) - len - pos;
3017
3018 if (src == mask)
3019 SUBST (SET_SRC (x),
3020 simplify_gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3021 else
3022 {
3023 rtx negmask = gen_int_mode (~(mask << pos), mode);
3024 SUBST (SET_SRC (x),
3025 simplify_gen_binary (IOR, mode,
3026 simplify_gen_binary (AND, mode,
3027 dest, negmask),
3028 GEN_INT (src << pos)));
3029 }
3030
3031 SUBST (SET_DEST (x), dest);
3032
3033 split = find_split_point (&SET_SRC (x), insn);
3034 if (split && split != &SET_SRC (x))
3035 return split;
3036 }
3037
3038 /* Otherwise, see if this is an operation that we can split into two.
3039 If so, try to split that. */
3040 code = GET_CODE (SET_SRC (x));
3041
3042 switch (code)
3043 {
3044 case AND:
3045 /* If we are AND'ing with a large constant that is only a single
3046 bit and the result is only being used in a context where we
3047 need to know if it is zero or nonzero, replace it with a bit
3048 extraction. This will avoid the large constant, which might
3049 have taken more than one insn to make. If the constant were
3050 not a valid argument to the AND but took only one insn to make,
3051 this is no worse, but if it took more than one insn, it will
3052 be better. */
3053
3054 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3055 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3056 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3057 && GET_CODE (SET_DEST (x)) == REG
3058 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3059 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3060 && XEXP (*split, 0) == SET_DEST (x)
3061 && XEXP (*split, 1) == const0_rtx)
3062 {
3063 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3064 XEXP (SET_SRC (x), 0),
3065 pos, NULL_RTX, 1, 1, 0, 0);
3066 if (extraction != 0)
3067 {
3068 SUBST (SET_SRC (x), extraction);
3069 return find_split_point (loc, insn);
3070 }
3071 }
3072 break;
3073
3074 case NE:
3075 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3076 is known to be on, this can be converted into a NEG of a shift. */
3077 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3078 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3079 && 1 <= (pos = exact_log2
3080 (nonzero_bits (XEXP (SET_SRC (x), 0),
3081 GET_MODE (XEXP (SET_SRC (x), 0))))))
3082 {
3083 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3084
3085 SUBST (SET_SRC (x),
3086 gen_rtx_NEG (mode,
3087 gen_rtx_LSHIFTRT (mode,
3088 XEXP (SET_SRC (x), 0),
3089 GEN_INT (pos))));
3090
3091 split = find_split_point (&SET_SRC (x), insn);
3092 if (split && split != &SET_SRC (x))
3093 return split;
3094 }
3095 break;
3096
3097 case SIGN_EXTEND:
3098 inner = XEXP (SET_SRC (x), 0);
3099
3100 /* We can't optimize if either mode is a partial integer
3101 mode as we don't know how many bits are significant
3102 in those modes. */
3103 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3104 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3105 break;
3106
3107 pos = 0;
3108 len = GET_MODE_BITSIZE (GET_MODE (inner));
3109 unsignedp = 0;
3110 break;
3111
3112 case SIGN_EXTRACT:
3113 case ZERO_EXTRACT:
3114 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3115 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3116 {
3117 inner = XEXP (SET_SRC (x), 0);
3118 len = INTVAL (XEXP (SET_SRC (x), 1));
3119 pos = INTVAL (XEXP (SET_SRC (x), 2));
3120
3121 if (BITS_BIG_ENDIAN)
3122 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3123 unsignedp = (code == ZERO_EXTRACT);
3124 }
3125 break;
3126
3127 default:
3128 break;
3129 }
3130
3131 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3132 {
3133 enum machine_mode mode = GET_MODE (SET_SRC (x));
3134
3135 /* For unsigned, we have a choice of a shift followed by an
3136 AND or two shifts. Use two shifts for field sizes where the
3137 constant might be too large. We assume here that we can
3138 always at least get 8-bit constants in an AND insn, which is
3139 true for every current RISC. */
3140
3141 if (unsignedp && len <= 8)
3142 {
3143 SUBST (SET_SRC (x),
3144 gen_rtx_AND (mode,
3145 gen_rtx_LSHIFTRT
3146 (mode, gen_lowpart (mode, inner),
3147 GEN_INT (pos)),
3148 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3149
3150 split = find_split_point (&SET_SRC (x), insn);
3151 if (split && split != &SET_SRC (x))
3152 return split;
3153 }
3154 else
3155 {
3156 SUBST (SET_SRC (x),
3157 gen_rtx_fmt_ee
3158 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3159 gen_rtx_ASHIFT (mode,
3160 gen_lowpart (mode, inner),
3161 GEN_INT (GET_MODE_BITSIZE (mode)
3162 - len - pos)),
3163 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3164
3165 split = find_split_point (&SET_SRC (x), insn);
3166 if (split && split != &SET_SRC (x))
3167 return split;
3168 }
3169 }
3170
3171 /* See if this is a simple operation with a constant as the second
3172 operand. It might be that this constant is out of range and hence
3173 could be used as a split point. */
3174 if (BINARY_P (SET_SRC (x))
3175 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3176 && (OBJECT_P (XEXP (SET_SRC (x), 0))
3177 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3178 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3179 return &XEXP (SET_SRC (x), 1);
3180
3181 /* Finally, see if this is a simple operation with its first operand
3182 not in a register. The operation might require this operand in a
3183 register, so return it as a split point. We can always do this
3184 because if the first operand were another operation, we would have
3185 already found it as a split point. */
3186 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3187 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3188 return &XEXP (SET_SRC (x), 0);
3189
3190 return 0;
3191
3192 case AND:
3193 case IOR:
3194 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3195 it is better to write this as (not (ior A B)) so we can split it.
3196 Similarly for IOR. */
3197 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3198 {
3199 SUBST (*loc,
3200 gen_rtx_NOT (GET_MODE (x),
3201 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3202 GET_MODE (x),
3203 XEXP (XEXP (x, 0), 0),
3204 XEXP (XEXP (x, 1), 0))));
3205 return find_split_point (loc, insn);
3206 }
3207
3208 /* Many RISC machines have a large set of logical insns. If the
3209 second operand is a NOT, put it first so we will try to split the
3210 other operand first. */
3211 if (GET_CODE (XEXP (x, 1)) == NOT)
3212 {
3213 rtx tem = XEXP (x, 0);
3214 SUBST (XEXP (x, 0), XEXP (x, 1));
3215 SUBST (XEXP (x, 1), tem);
3216 }
3217 break;
3218
3219 default:
3220 break;
3221 }
3222
3223 /* Otherwise, select our actions depending on our rtx class. */
3224 switch (GET_RTX_CLASS (code))
3225 {
3226 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3227 case RTX_TERNARY:
3228 split = find_split_point (&XEXP (x, 2), insn);
3229 if (split)
3230 return split;
3231 /* ... fall through ... */
3232 case RTX_BIN_ARITH:
3233 case RTX_COMM_ARITH:
3234 case RTX_COMPARE:
3235 case RTX_COMM_COMPARE:
3236 split = find_split_point (&XEXP (x, 1), insn);
3237 if (split)
3238 return split;
3239 /* ... fall through ... */
3240 case RTX_UNARY:
3241 /* Some machines have (and (shift ...) ...) insns. If X is not
3242 an AND, but XEXP (X, 0) is, use it as our split point. */
3243 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3244 return &XEXP (x, 0);
3245
3246 split = find_split_point (&XEXP (x, 0), insn);
3247 if (split)
3248 return split;
3249 return loc;
3250
3251 default:
3252 /* Otherwise, we don't have a split point. */
3253 return 0;
3254 }
3255 }
3256 \f
3257 /* Throughout X, replace FROM with TO, and return the result.
3258 The result is TO if X is FROM;
3259 otherwise the result is X, but its contents may have been modified.
3260 If they were modified, a record was made in undobuf so that
3261 undo_all will (among other things) return X to its original state.
3262
3263 If the number of changes necessary is too much to record to undo,
3264 the excess changes are not made, so the result is invalid.
3265 The changes already made can still be undone.
3266 undobuf.num_undo is incremented for such changes, so by testing that
3267 the caller can tell whether the result is valid.
3268
3269 `n_occurrences' is incremented each time FROM is replaced.
3270
3271 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3272
3273 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3274 by copying if `n_occurrences' is nonzero. */
3275
3276 static rtx
3277 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3278 {
3279 enum rtx_code code = GET_CODE (x);
3280 enum machine_mode op0_mode = VOIDmode;
3281 const char *fmt;
3282 int len, i;
3283 rtx new;
3284
3285 /* Two expressions are equal if they are identical copies of a shared
3286 RTX or if they are both registers with the same register number
3287 and mode. */
3288
3289 #define COMBINE_RTX_EQUAL_P(X,Y) \
3290 ((X) == (Y) \
3291 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3292 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3293
3294 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3295 {
3296 n_occurrences++;
3297 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3298 }
3299
3300 /* If X and FROM are the same register but different modes, they will
3301 not have been seen as equal above. However, flow.c will make a
3302 LOG_LINKS entry for that case. If we do nothing, we will try to
3303 rerecognize our original insn and, when it succeeds, we will
3304 delete the feeding insn, which is incorrect.
3305
3306 So force this insn not to match in this (rare) case. */
3307 if (! in_dest && code == REG && GET_CODE (from) == REG
3308 && REGNO (x) == REGNO (from))
3309 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3310
3311 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3312 of which may contain things that can be combined. */
3313 if (code != MEM && code != LO_SUM && OBJECT_P (x))
3314 return x;
3315
3316 /* It is possible to have a subexpression appear twice in the insn.
3317 Suppose that FROM is a register that appears within TO.
3318 Then, after that subexpression has been scanned once by `subst',
3319 the second time it is scanned, TO may be found. If we were
3320 to scan TO here, we would find FROM within it and create a
3321 self-referent rtl structure which is completely wrong. */
3322 if (COMBINE_RTX_EQUAL_P (x, to))
3323 return to;
3324
3325 /* Parallel asm_operands need special attention because all of the
3326 inputs are shared across the arms. Furthermore, unsharing the
3327 rtl results in recognition failures. Failure to handle this case
3328 specially can result in circular rtl.
3329
3330 Solve this by doing a normal pass across the first entry of the
3331 parallel, and only processing the SET_DESTs of the subsequent
3332 entries. Ug. */
3333
3334 if (code == PARALLEL
3335 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3336 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3337 {
3338 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3339
3340 /* If this substitution failed, this whole thing fails. */
3341 if (GET_CODE (new) == CLOBBER
3342 && XEXP (new, 0) == const0_rtx)
3343 return new;
3344
3345 SUBST (XVECEXP (x, 0, 0), new);
3346
3347 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3348 {
3349 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3350
3351 if (GET_CODE (dest) != REG
3352 && GET_CODE (dest) != CC0
3353 && GET_CODE (dest) != PC)
3354 {
3355 new = subst (dest, from, to, 0, unique_copy);
3356
3357 /* If this substitution failed, this whole thing fails. */
3358 if (GET_CODE (new) == CLOBBER
3359 && XEXP (new, 0) == const0_rtx)
3360 return new;
3361
3362 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3363 }
3364 }
3365 }
3366 else
3367 {
3368 len = GET_RTX_LENGTH (code);
3369 fmt = GET_RTX_FORMAT (code);
3370
3371 /* We don't need to process a SET_DEST that is a register, CC0,
3372 or PC, so set up to skip this common case. All other cases
3373 where we want to suppress replacing something inside a
3374 SET_SRC are handled via the IN_DEST operand. */
3375 if (code == SET
3376 && (GET_CODE (SET_DEST (x)) == REG
3377 || GET_CODE (SET_DEST (x)) == CC0
3378 || GET_CODE (SET_DEST (x)) == PC))
3379 fmt = "ie";
3380
3381 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3382 constant. */
3383 if (fmt[0] == 'e')
3384 op0_mode = GET_MODE (XEXP (x, 0));
3385
3386 for (i = 0; i < len; i++)
3387 {
3388 if (fmt[i] == 'E')
3389 {
3390 int j;
3391 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3392 {
3393 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3394 {
3395 new = (unique_copy && n_occurrences
3396 ? copy_rtx (to) : to);
3397 n_occurrences++;
3398 }
3399 else
3400 {
3401 new = subst (XVECEXP (x, i, j), from, to, 0,
3402 unique_copy);
3403
3404 /* If this substitution failed, this whole thing
3405 fails. */
3406 if (GET_CODE (new) == CLOBBER
3407 && XEXP (new, 0) == const0_rtx)
3408 return new;
3409 }
3410
3411 SUBST (XVECEXP (x, i, j), new);
3412 }
3413 }
3414 else if (fmt[i] == 'e')
3415 {
3416 /* If this is a register being set, ignore it. */
3417 new = XEXP (x, i);
3418 if (in_dest
3419 && (code == SUBREG || code == STRICT_LOW_PART
3420 || code == ZERO_EXTRACT)
3421 && i == 0
3422 && GET_CODE (new) == REG)
3423 ;
3424
3425 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3426 {
3427 /* In general, don't install a subreg involving two
3428 modes not tieable. It can worsen register
3429 allocation, and can even make invalid reload
3430 insns, since the reg inside may need to be copied
3431 from in the outside mode, and that may be invalid
3432 if it is an fp reg copied in integer mode.
3433
3434 We allow two exceptions to this: It is valid if
3435 it is inside another SUBREG and the mode of that
3436 SUBREG and the mode of the inside of TO is
3437 tieable and it is valid if X is a SET that copies
3438 FROM to CC0. */
3439
3440 if (GET_CODE (to) == SUBREG
3441 && ! MODES_TIEABLE_P (GET_MODE (to),
3442 GET_MODE (SUBREG_REG (to)))
3443 && ! (code == SUBREG
3444 && MODES_TIEABLE_P (GET_MODE (x),
3445 GET_MODE (SUBREG_REG (to))))
3446 #ifdef HAVE_cc0
3447 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3448 #endif
3449 )
3450 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3451
3452 #ifdef CANNOT_CHANGE_MODE_CLASS
3453 if (code == SUBREG
3454 && GET_CODE (to) == REG
3455 && REGNO (to) < FIRST_PSEUDO_REGISTER
3456 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3457 GET_MODE (to),
3458 GET_MODE (x)))
3459 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3460 #endif
3461
3462 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3463 n_occurrences++;
3464 }
3465 else
3466 /* If we are in a SET_DEST, suppress most cases unless we
3467 have gone inside a MEM, in which case we want to
3468 simplify the address. We assume here that things that
3469 are actually part of the destination have their inner
3470 parts in the first expression. This is true for SUBREG,
3471 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3472 things aside from REG and MEM that should appear in a
3473 SET_DEST. */
3474 new = subst (XEXP (x, i), from, to,
3475 (((in_dest
3476 && (code == SUBREG || code == STRICT_LOW_PART
3477 || code == ZERO_EXTRACT))
3478 || code == SET)
3479 && i == 0), unique_copy);
3480
3481 /* If we found that we will have to reject this combination,
3482 indicate that by returning the CLOBBER ourselves, rather than
3483 an expression containing it. This will speed things up as
3484 well as prevent accidents where two CLOBBERs are considered
3485 to be equal, thus producing an incorrect simplification. */
3486
3487 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3488 return new;
3489
3490 if (GET_CODE (x) == SUBREG
3491 && (GET_CODE (new) == CONST_INT
3492 || GET_CODE (new) == CONST_DOUBLE))
3493 {
3494 enum machine_mode mode = GET_MODE (x);
3495
3496 x = simplify_subreg (GET_MODE (x), new,
3497 GET_MODE (SUBREG_REG (x)),
3498 SUBREG_BYTE (x));
3499 if (! x)
3500 x = gen_rtx_CLOBBER (mode, const0_rtx);
3501 }
3502 else if (GET_CODE (new) == CONST_INT
3503 && GET_CODE (x) == ZERO_EXTEND)
3504 {
3505 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3506 new, GET_MODE (XEXP (x, 0)));
3507 if (! x)
3508 abort ();
3509 }
3510 else
3511 SUBST (XEXP (x, i), new);
3512 }
3513 }
3514 }
3515
3516 /* Try to simplify X. If the simplification changed the code, it is likely
3517 that further simplification will help, so loop, but limit the number
3518 of repetitions that will be performed. */
3519
3520 for (i = 0; i < 4; i++)
3521 {
3522 /* If X is sufficiently simple, don't bother trying to do anything
3523 with it. */
3524 if (code != CONST_INT && code != REG && code != CLOBBER)
3525 x = combine_simplify_rtx (x, op0_mode, in_dest);
3526
3527 if (GET_CODE (x) == code)
3528 break;
3529
3530 code = GET_CODE (x);
3531
3532 /* We no longer know the original mode of operand 0 since we
3533 have changed the form of X) */
3534 op0_mode = VOIDmode;
3535 }
3536
3537 return x;
3538 }
3539 \f
3540 /* Simplify X, a piece of RTL. We just operate on the expression at the
3541 outer level; call `subst' to simplify recursively. Return the new
3542 expression.
3543
3544 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
3545 if we are inside a SET_DEST. */
3546
3547 static rtx
3548 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3549 {
3550 enum rtx_code code = GET_CODE (x);
3551 enum machine_mode mode = GET_MODE (x);
3552 rtx temp;
3553 rtx reversed;
3554 int i;
3555
3556 /* If this is a commutative operation, put a constant last and a complex
3557 expression first. We don't need to do this for comparisons here. */
3558 if (COMMUTATIVE_ARITH_P (x)
3559 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3560 {
3561 temp = XEXP (x, 0);
3562 SUBST (XEXP (x, 0), XEXP (x, 1));
3563 SUBST (XEXP (x, 1), temp);
3564 }
3565
3566 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3567 sign extension of a PLUS with a constant, reverse the order of the sign
3568 extension and the addition. Note that this not the same as the original
3569 code, but overflow is undefined for signed values. Also note that the
3570 PLUS will have been partially moved "inside" the sign-extension, so that
3571 the first operand of X will really look like:
3572 (ashiftrt (plus (ashift A C4) C5) C4).
3573 We convert this to
3574 (plus (ashiftrt (ashift A C4) C2) C4)
3575 and replace the first operand of X with that expression. Later parts
3576 of this function may simplify the expression further.
3577
3578 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3579 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3580 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3581
3582 We do this to simplify address expressions. */
3583
3584 if ((code == PLUS || code == MINUS || code == MULT)
3585 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3586 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3587 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3588 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3589 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3590 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3591 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3592 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3593 XEXP (XEXP (XEXP (x, 0), 0), 1),
3594 XEXP (XEXP (x, 0), 1))) != 0)
3595 {
3596 rtx new
3597 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3598 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3599 INTVAL (XEXP (XEXP (x, 0), 1)));
3600
3601 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3602 INTVAL (XEXP (XEXP (x, 0), 1)));
3603
3604 SUBST (XEXP (x, 0), simplify_gen_binary (PLUS, mode, new, temp));
3605 }
3606
3607 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3608 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3609 things. Check for cases where both arms are testing the same
3610 condition.
3611
3612 Don't do anything if all operands are very simple. */
3613
3614 if ((BINARY_P (x)
3615 && ((!OBJECT_P (XEXP (x, 0))
3616 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3617 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3618 || (!OBJECT_P (XEXP (x, 1))
3619 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3620 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3621 || (UNARY_P (x)
3622 && (!OBJECT_P (XEXP (x, 0))
3623 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3624 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3625 {
3626 rtx cond, true_rtx, false_rtx;
3627
3628 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3629 if (cond != 0
3630 /* If everything is a comparison, what we have is highly unlikely
3631 to be simpler, so don't use it. */
3632 && ! (COMPARISON_P (x)
3633 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3634 {
3635 rtx cop1 = const0_rtx;
3636 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3637
3638 if (cond_code == NE && COMPARISON_P (cond))
3639 return x;
3640
3641 /* Simplify the alternative arms; this may collapse the true and
3642 false arms to store-flag values. Be careful to use copy_rtx
3643 here since true_rtx or false_rtx might share RTL with x as a
3644 result of the if_then_else_cond call above. */
3645 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3646 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3647
3648 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3649 is unlikely to be simpler. */
3650 if (general_operand (true_rtx, VOIDmode)
3651 && general_operand (false_rtx, VOIDmode))
3652 {
3653 enum rtx_code reversed;
3654
3655 /* Restarting if we generate a store-flag expression will cause
3656 us to loop. Just drop through in this case. */
3657
3658 /* If the result values are STORE_FLAG_VALUE and zero, we can
3659 just make the comparison operation. */
3660 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3661 x = simplify_gen_relational (cond_code, mode, VOIDmode,
3662 cond, cop1);
3663 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3664 && ((reversed = reversed_comparison_code_parts
3665 (cond_code, cond, cop1, NULL))
3666 != UNKNOWN))
3667 x = simplify_gen_relational (reversed, mode, VOIDmode,
3668 cond, cop1);
3669
3670 /* Likewise, we can make the negate of a comparison operation
3671 if the result values are - STORE_FLAG_VALUE and zero. */
3672 else if (GET_CODE (true_rtx) == CONST_INT
3673 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3674 && false_rtx == const0_rtx)
3675 x = simplify_gen_unary (NEG, mode,
3676 simplify_gen_relational (cond_code,
3677 mode, VOIDmode,
3678 cond, cop1),
3679 mode);
3680 else if (GET_CODE (false_rtx) == CONST_INT
3681 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3682 && true_rtx == const0_rtx
3683 && ((reversed = reversed_comparison_code_parts
3684 (cond_code, cond, cop1, NULL))
3685 != UNKNOWN))
3686 x = simplify_gen_unary (NEG, mode,
3687 simplify_gen_relational (reversed,
3688 mode, VOIDmode,
3689 cond, cop1),
3690 mode);
3691 else
3692 return gen_rtx_IF_THEN_ELSE (mode,
3693 simplify_gen_relational (cond_code,
3694 mode,
3695 VOIDmode,
3696 cond,
3697 cop1),
3698 true_rtx, false_rtx);
3699
3700 code = GET_CODE (x);
3701 op0_mode = VOIDmode;
3702 }
3703 }
3704 }
3705
3706 /* Try to fold this expression in case we have constants that weren't
3707 present before. */
3708 temp = 0;
3709 switch (GET_RTX_CLASS (code))
3710 {
3711 case RTX_UNARY:
3712 if (op0_mode == VOIDmode)
3713 op0_mode = GET_MODE (XEXP (x, 0));
3714 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3715 break;
3716 case RTX_COMPARE:
3717 case RTX_COMM_COMPARE:
3718 {
3719 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3720 if (cmp_mode == VOIDmode)
3721 {
3722 cmp_mode = GET_MODE (XEXP (x, 1));
3723 if (cmp_mode == VOIDmode)
3724 cmp_mode = op0_mode;
3725 }
3726 temp = simplify_relational_operation (code, mode, cmp_mode,
3727 XEXP (x, 0), XEXP (x, 1));
3728 }
3729 break;
3730 case RTX_COMM_ARITH:
3731 case RTX_BIN_ARITH:
3732 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3733 break;
3734 case RTX_BITFIELD_OPS:
3735 case RTX_TERNARY:
3736 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3737 XEXP (x, 1), XEXP (x, 2));
3738 break;
3739 default:
3740 break;
3741 }
3742
3743 if (temp)
3744 {
3745 x = temp;
3746 code = GET_CODE (temp);
3747 op0_mode = VOIDmode;
3748 mode = GET_MODE (temp);
3749 }
3750
3751 /* First see if we can apply the inverse distributive law. */
3752 if (code == PLUS || code == MINUS
3753 || code == AND || code == IOR || code == XOR)
3754 {
3755 x = apply_distributive_law (x);
3756 code = GET_CODE (x);
3757 op0_mode = VOIDmode;
3758 }
3759
3760 /* If CODE is an associative operation not otherwise handled, see if we
3761 can associate some operands. This can win if they are constants or
3762 if they are logically related (i.e. (a & b) & a). */
3763 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3764 || code == AND || code == IOR || code == XOR
3765 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3766 && ((INTEGRAL_MODE_P (mode) && code != DIV)
3767 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3768 {
3769 if (GET_CODE (XEXP (x, 0)) == code)
3770 {
3771 rtx other = XEXP (XEXP (x, 0), 0);
3772 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3773 rtx inner_op1 = XEXP (x, 1);
3774 rtx inner;
3775
3776 /* Make sure we pass the constant operand if any as the second
3777 one if this is a commutative operation. */
3778 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
3779 {
3780 rtx tem = inner_op0;
3781 inner_op0 = inner_op1;
3782 inner_op1 = tem;
3783 }
3784 inner = simplify_binary_operation (code == MINUS ? PLUS
3785 : code == DIV ? MULT
3786 : code,
3787 mode, inner_op0, inner_op1);
3788
3789 /* For commutative operations, try the other pair if that one
3790 didn't simplify. */
3791 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
3792 {
3793 other = XEXP (XEXP (x, 0), 1);
3794 inner = simplify_binary_operation (code, mode,
3795 XEXP (XEXP (x, 0), 0),
3796 XEXP (x, 1));
3797 }
3798
3799 if (inner)
3800 return simplify_gen_binary (code, mode, other, inner);
3801 }
3802 }
3803
3804 /* A little bit of algebraic simplification here. */
3805 switch (code)
3806 {
3807 case MEM:
3808 /* Ensure that our address has any ASHIFTs converted to MULT in case
3809 address-recognizing predicates are called later. */
3810 temp = make_compound_operation (XEXP (x, 0), MEM);
3811 SUBST (XEXP (x, 0), temp);
3812 break;
3813
3814 case SUBREG:
3815 if (op0_mode == VOIDmode)
3816 op0_mode = GET_MODE (SUBREG_REG (x));
3817
3818 /* See if this can be moved to simplify_subreg. */
3819 if (CONSTANT_P (SUBREG_REG (x))
3820 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3821 /* Don't call gen_lowpart if the inner mode
3822 is VOIDmode and we cannot simplify it, as SUBREG without
3823 inner mode is invalid. */
3824 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3825 || gen_lowpart_common (mode, SUBREG_REG (x))))
3826 return gen_lowpart (mode, SUBREG_REG (x));
3827
3828 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3829 break;
3830 {
3831 rtx temp;
3832 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3833 SUBREG_BYTE (x));
3834 if (temp)
3835 return temp;
3836 }
3837
3838 /* Don't change the mode of the MEM if that would change the meaning
3839 of the address. */
3840 if (GET_CODE (SUBREG_REG (x)) == MEM
3841 && (MEM_VOLATILE_P (SUBREG_REG (x))
3842 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3843 return gen_rtx_CLOBBER (mode, const0_rtx);
3844
3845 /* Note that we cannot do any narrowing for non-constants since
3846 we might have been counting on using the fact that some bits were
3847 zero. We now do this in the SET. */
3848
3849 break;
3850
3851 case NOT:
3852 if (GET_CODE (XEXP (x, 0)) == SUBREG
3853 && subreg_lowpart_p (XEXP (x, 0))
3854 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3855 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3856 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3857 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3858 {
3859 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3860
3861 x = gen_rtx_ROTATE (inner_mode,
3862 simplify_gen_unary (NOT, inner_mode, const1_rtx,
3863 inner_mode),
3864 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3865 return gen_lowpart (mode, x);
3866 }
3867
3868 /* Apply De Morgan's laws to reduce number of patterns for machines
3869 with negating logical insns (and-not, nand, etc.). If result has
3870 only one NOT, put it first, since that is how the patterns are
3871 coded. */
3872
3873 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3874 {
3875 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3876 enum machine_mode op_mode;
3877
3878 op_mode = GET_MODE (in1);
3879 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3880
3881 op_mode = GET_MODE (in2);
3882 if (op_mode == VOIDmode)
3883 op_mode = mode;
3884 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3885
3886 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3887 {
3888 rtx tem = in2;
3889 in2 = in1; in1 = tem;
3890 }
3891
3892 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3893 mode, in1, in2);
3894 }
3895 break;
3896
3897 case NEG:
3898 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3899 if (GET_CODE (XEXP (x, 0)) == XOR
3900 && XEXP (XEXP (x, 0), 1) == const1_rtx
3901 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3902 return simplify_gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3903 constm1_rtx);
3904
3905 temp = expand_compound_operation (XEXP (x, 0));
3906
3907 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3908 replaced by (lshiftrt X C). This will convert
3909 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3910
3911 if (GET_CODE (temp) == ASHIFTRT
3912 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3913 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3914 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3915 INTVAL (XEXP (temp, 1)));
3916
3917 /* If X has only a single bit that might be nonzero, say, bit I, convert
3918 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3919 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3920 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3921 or a SUBREG of one since we'd be making the expression more
3922 complex if it was just a register. */
3923
3924 if (GET_CODE (temp) != REG
3925 && ! (GET_CODE (temp) == SUBREG
3926 && GET_CODE (SUBREG_REG (temp)) == REG)
3927 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3928 {
3929 rtx temp1 = simplify_shift_const
3930 (NULL_RTX, ASHIFTRT, mode,
3931 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3932 GET_MODE_BITSIZE (mode) - 1 - i),
3933 GET_MODE_BITSIZE (mode) - 1 - i);
3934
3935 /* If all we did was surround TEMP with the two shifts, we
3936 haven't improved anything, so don't use it. Otherwise,
3937 we are better off with TEMP1. */
3938 if (GET_CODE (temp1) != ASHIFTRT
3939 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3940 || XEXP (XEXP (temp1, 0), 0) != temp)
3941 return temp1;
3942 }
3943 break;
3944
3945 case TRUNCATE:
3946 /* We can't handle truncation to a partial integer mode here
3947 because we don't know the real bitsize of the partial
3948 integer mode. */
3949 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3950 break;
3951
3952 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3953 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3954 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3955 SUBST (XEXP (x, 0),
3956 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3957 GET_MODE_MASK (mode), NULL_RTX, 0));
3958
3959 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3960 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3961 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3962 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3963 return XEXP (XEXP (x, 0), 0);
3964
3965 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3966 (OP:SI foo:SI) if OP is NEG or ABS. */
3967 if ((GET_CODE (XEXP (x, 0)) == ABS
3968 || GET_CODE (XEXP (x, 0)) == NEG)
3969 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3970 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3971 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3972 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
3973 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
3974
3975 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3976 (truncate:SI x). */
3977 if (GET_CODE (XEXP (x, 0)) == SUBREG
3978 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3979 && subreg_lowpart_p (XEXP (x, 0)))
3980 return SUBREG_REG (XEXP (x, 0));
3981
3982 /* If we know that the value is already truncated, we can
3983 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
3984 is nonzero for the corresponding modes. But don't do this
3985 for an (LSHIFTRT (MULT ...)) since this will cause problems
3986 with the umulXi3_highpart patterns. */
3987 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3988 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3989 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3990 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
3991 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
3992 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
3993 return gen_lowpart (mode, XEXP (x, 0));
3994
3995 /* A truncate of a comparison can be replaced with a subreg if
3996 STORE_FLAG_VALUE permits. This is like the previous test,
3997 but it works even if the comparison is done in a mode larger
3998 than HOST_BITS_PER_WIDE_INT. */
3999 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4000 && COMPARISON_P (XEXP (x, 0))
4001 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4002 return gen_lowpart (mode, XEXP (x, 0));
4003
4004 /* Similarly, a truncate of a register whose value is a
4005 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4006 permits. */
4007 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4008 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4009 && (temp = get_last_value (XEXP (x, 0)))
4010 && COMPARISON_P (temp))
4011 return gen_lowpart (mode, XEXP (x, 0));
4012
4013 break;
4014
4015 case FLOAT_TRUNCATE:
4016 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4017 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4018 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4019 return XEXP (XEXP (x, 0), 0);
4020
4021 /* (float_truncate:SF (float_truncate:DF foo:XF))
4022 = (float_truncate:SF foo:XF).
4023 This may eliminate double rounding, so it is unsafe.
4024
4025 (float_truncate:SF (float_extend:XF foo:DF))
4026 = (float_truncate:SF foo:DF).
4027
4028 (float_truncate:DF (float_extend:XF foo:SF))
4029 = (float_extend:SF foo:DF). */
4030 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4031 && flag_unsafe_math_optimizations)
4032 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4033 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4034 0)))
4035 > GET_MODE_SIZE (mode)
4036 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4037 mode,
4038 XEXP (XEXP (x, 0), 0), mode);
4039
4040 /* (float_truncate (float x)) is (float x) */
4041 if (GET_CODE (XEXP (x, 0)) == FLOAT
4042 && (flag_unsafe_math_optimizations
4043 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4044 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4045 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4046 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4047 return simplify_gen_unary (FLOAT, mode,
4048 XEXP (XEXP (x, 0), 0),
4049 GET_MODE (XEXP (XEXP (x, 0), 0)));
4050
4051 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4052 (OP:SF foo:SF) if OP is NEG or ABS. */
4053 if ((GET_CODE (XEXP (x, 0)) == ABS
4054 || GET_CODE (XEXP (x, 0)) == NEG)
4055 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4056 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4057 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4058 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4059
4060 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4061 is (float_truncate:SF x). */
4062 if (GET_CODE (XEXP (x, 0)) == SUBREG
4063 && subreg_lowpart_p (XEXP (x, 0))
4064 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4065 return SUBREG_REG (XEXP (x, 0));
4066 break;
4067 case FLOAT_EXTEND:
4068 /* (float_extend (float_extend x)) is (float_extend x)
4069
4070 (float_extend (float x)) is (float x) assuming that double
4071 rounding can't happen.
4072 */
4073 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4074 || (GET_CODE (XEXP (x, 0)) == FLOAT
4075 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4076 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4077 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4078 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4079 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4080 XEXP (XEXP (x, 0), 0),
4081 GET_MODE (XEXP (XEXP (x, 0), 0)));
4082
4083 break;
4084 #ifdef HAVE_cc0
4085 case COMPARE:
4086 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4087 using cc0, in which case we want to leave it as a COMPARE
4088 so we can distinguish it from a register-register-copy. */
4089 if (XEXP (x, 1) == const0_rtx)
4090 return XEXP (x, 0);
4091
4092 /* x - 0 is the same as x unless x's mode has signed zeros and
4093 allows rounding towards -infinity. Under those conditions,
4094 0 - 0 is -0. */
4095 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4096 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4097 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4098 return XEXP (x, 0);
4099 break;
4100 #endif
4101
4102 case CONST:
4103 /* (const (const X)) can become (const X). Do it this way rather than
4104 returning the inner CONST since CONST can be shared with a
4105 REG_EQUAL note. */
4106 if (GET_CODE (XEXP (x, 0)) == CONST)
4107 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4108 break;
4109
4110 #ifdef HAVE_lo_sum
4111 case LO_SUM:
4112 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4113 can add in an offset. find_split_point will split this address up
4114 again if it doesn't match. */
4115 if (GET_CODE (XEXP (x, 0)) == HIGH
4116 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4117 return XEXP (x, 1);
4118 break;
4119 #endif
4120
4121 case PLUS:
4122 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4123 */
4124 if (GET_CODE (XEXP (x, 0)) == MULT
4125 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4126 {
4127 rtx in1, in2;
4128
4129 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4130 in2 = XEXP (XEXP (x, 0), 1);
4131 return simplify_gen_binary (MINUS, mode, XEXP (x, 1),
4132 simplify_gen_binary (MULT, mode,
4133 in1, in2));
4134 }
4135
4136 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4137 outermost. That's because that's the way indexed addresses are
4138 supposed to appear. This code used to check many more cases, but
4139 they are now checked elsewhere. */
4140 if (GET_CODE (XEXP (x, 0)) == PLUS
4141 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4142 return simplify_gen_binary (PLUS, mode,
4143 simplify_gen_binary (PLUS, mode,
4144 XEXP (XEXP (x, 0), 0),
4145 XEXP (x, 1)),
4146 XEXP (XEXP (x, 0), 1));
4147
4148 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4149 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4150 bit-field and can be replaced by either a sign_extend or a
4151 sign_extract. The `and' may be a zero_extend and the two
4152 <c>, -<c> constants may be reversed. */
4153 if (GET_CODE (XEXP (x, 0)) == XOR
4154 && GET_CODE (XEXP (x, 1)) == CONST_INT
4155 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4156 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4157 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4158 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4159 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4160 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4161 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4162 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4163 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4164 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4165 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4166 == (unsigned int) i + 1))))
4167 return simplify_shift_const
4168 (NULL_RTX, ASHIFTRT, mode,
4169 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4170 XEXP (XEXP (XEXP (x, 0), 0), 0),
4171 GET_MODE_BITSIZE (mode) - (i + 1)),
4172 GET_MODE_BITSIZE (mode) - (i + 1));
4173
4174 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4175 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4176 is 1. This produces better code than the alternative immediately
4177 below. */
4178 if (COMPARISON_P (XEXP (x, 0))
4179 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4180 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4181 && (reversed = reversed_comparison (XEXP (x, 0), mode,
4182 XEXP (XEXP (x, 0), 0),
4183 XEXP (XEXP (x, 0), 1))))
4184 return
4185 simplify_gen_unary (NEG, mode, reversed, mode);
4186
4187 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4188 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4189 the bitsize of the mode - 1. This allows simplification of
4190 "a = (b & 8) == 0;" */
4191 if (XEXP (x, 1) == constm1_rtx
4192 && GET_CODE (XEXP (x, 0)) != REG
4193 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4194 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4195 && nonzero_bits (XEXP (x, 0), mode) == 1)
4196 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4197 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4198 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4199 GET_MODE_BITSIZE (mode) - 1),
4200 GET_MODE_BITSIZE (mode) - 1);
4201
4202 /* If we are adding two things that have no bits in common, convert
4203 the addition into an IOR. This will often be further simplified,
4204 for example in cases like ((a & 1) + (a & 2)), which can
4205 become a & 3. */
4206
4207 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4208 && (nonzero_bits (XEXP (x, 0), mode)
4209 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4210 {
4211 /* Try to simplify the expression further. */
4212 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4213 temp = combine_simplify_rtx (tor, mode, in_dest);
4214
4215 /* If we could, great. If not, do not go ahead with the IOR
4216 replacement, since PLUS appears in many special purpose
4217 address arithmetic instructions. */
4218 if (GET_CODE (temp) != CLOBBER && temp != tor)
4219 return temp;
4220 }
4221 break;
4222
4223 case MINUS:
4224 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4225 by reversing the comparison code if valid. */
4226 if (STORE_FLAG_VALUE == 1
4227 && XEXP (x, 0) == const1_rtx
4228 && COMPARISON_P (XEXP (x, 1))
4229 && (reversed = reversed_comparison (XEXP (x, 1), mode,
4230 XEXP (XEXP (x, 1), 0),
4231 XEXP (XEXP (x, 1), 1))))
4232 return reversed;
4233
4234 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4235 (and <foo> (const_int pow2-1)) */
4236 if (GET_CODE (XEXP (x, 1)) == AND
4237 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4238 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4239 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4240 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4241 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4242
4243 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4244 */
4245 if (GET_CODE (XEXP (x, 1)) == MULT
4246 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4247 {
4248 rtx in1, in2;
4249
4250 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4251 in2 = XEXP (XEXP (x, 1), 1);
4252 return simplify_gen_binary (PLUS, mode,
4253 simplify_gen_binary (MULT, mode,
4254 in1, in2),
4255 XEXP (x, 0));
4256 }
4257
4258 /* Canonicalize (minus (neg A) (mult B C)) to
4259 (minus (mult (neg B) C) A). */
4260 if (GET_CODE (XEXP (x, 1)) == MULT
4261 && GET_CODE (XEXP (x, 0)) == NEG)
4262 {
4263 rtx in1, in2;
4264
4265 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4266 in2 = XEXP (XEXP (x, 1), 1);
4267 return simplify_gen_binary (MINUS, mode,
4268 simplify_gen_binary (MULT, mode,
4269 in1, in2),
4270 XEXP (XEXP (x, 0), 0));
4271 }
4272
4273 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4274 integers. */
4275 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4276 return simplify_gen_binary (MINUS, mode,
4277 simplify_gen_binary (MINUS, mode,
4278 XEXP (x, 0),
4279 XEXP (XEXP (x, 1), 0)),
4280 XEXP (XEXP (x, 1), 1));
4281 break;
4282
4283 case MULT:
4284 /* If we have (mult (plus A B) C), apply the distributive law and then
4285 the inverse distributive law to see if things simplify. This
4286 occurs mostly in addresses, often when unrolling loops. */
4287
4288 if (GET_CODE (XEXP (x, 0)) == PLUS)
4289 {
4290 rtx result = distribute_and_simplify_rtx (x);
4291 if (result)
4292 return result;
4293 }
4294
4295 /* Try simplify a*(b/c) as (a*b)/c. */
4296 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4297 && GET_CODE (XEXP (x, 0)) == DIV)
4298 {
4299 rtx tem = simplify_binary_operation (MULT, mode,
4300 XEXP (XEXP (x, 0), 0),
4301 XEXP (x, 1));
4302 if (tem)
4303 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4304 }
4305 break;
4306
4307 case UDIV:
4308 /* If this is a divide by a power of two, treat it as a shift if
4309 its first operand is a shift. */
4310 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4311 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4312 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4313 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4314 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4315 || GET_CODE (XEXP (x, 0)) == ROTATE
4316 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4317 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4318 break;
4319
4320 case EQ: case NE:
4321 case GT: case GTU: case GE: case GEU:
4322 case LT: case LTU: case LE: case LEU:
4323 case UNEQ: case LTGT:
4324 case UNGT: case UNGE:
4325 case UNLT: case UNLE:
4326 case UNORDERED: case ORDERED:
4327 /* If the first operand is a condition code, we can't do anything
4328 with it. */
4329 if (GET_CODE (XEXP (x, 0)) == COMPARE
4330 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4331 && ! CC0_P (XEXP (x, 0))))
4332 {
4333 rtx op0 = XEXP (x, 0);
4334 rtx op1 = XEXP (x, 1);
4335 enum rtx_code new_code;
4336
4337 if (GET_CODE (op0) == COMPARE)
4338 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4339
4340 /* Simplify our comparison, if possible. */
4341 new_code = simplify_comparison (code, &op0, &op1);
4342
4343 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4344 if only the low-order bit is possibly nonzero in X (such as when
4345 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4346 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4347 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4348 (plus X 1).
4349
4350 Remove any ZERO_EXTRACT we made when thinking this was a
4351 comparison. It may now be simpler to use, e.g., an AND. If a
4352 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4353 the call to make_compound_operation in the SET case. */
4354
4355 if (STORE_FLAG_VALUE == 1
4356 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4357 && op1 == const0_rtx
4358 && mode == GET_MODE (op0)
4359 && nonzero_bits (op0, mode) == 1)
4360 return gen_lowpart (mode,
4361 expand_compound_operation (op0));
4362
4363 else if (STORE_FLAG_VALUE == 1
4364 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4365 && op1 == const0_rtx
4366 && mode == GET_MODE (op0)
4367 && (num_sign_bit_copies (op0, mode)
4368 == GET_MODE_BITSIZE (mode)))
4369 {
4370 op0 = expand_compound_operation (op0);
4371 return simplify_gen_unary (NEG, mode,
4372 gen_lowpart (mode, op0),
4373 mode);
4374 }
4375
4376 else if (STORE_FLAG_VALUE == 1
4377 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4378 && op1 == const0_rtx
4379 && mode == GET_MODE (op0)
4380 && nonzero_bits (op0, mode) == 1)
4381 {
4382 op0 = expand_compound_operation (op0);
4383 return simplify_gen_binary (XOR, mode,
4384 gen_lowpart (mode, op0),
4385 const1_rtx);
4386 }
4387
4388 else if (STORE_FLAG_VALUE == 1
4389 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4390 && op1 == const0_rtx
4391 && mode == GET_MODE (op0)
4392 && (num_sign_bit_copies (op0, mode)
4393 == GET_MODE_BITSIZE (mode)))
4394 {
4395 op0 = expand_compound_operation (op0);
4396 return plus_constant (gen_lowpart (mode, op0), 1);
4397 }
4398
4399 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4400 those above. */
4401 if (STORE_FLAG_VALUE == -1
4402 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4403 && op1 == const0_rtx
4404 && (num_sign_bit_copies (op0, mode)
4405 == GET_MODE_BITSIZE (mode)))
4406 return gen_lowpart (mode,
4407 expand_compound_operation (op0));
4408
4409 else if (STORE_FLAG_VALUE == -1
4410 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4411 && op1 == const0_rtx
4412 && mode == GET_MODE (op0)
4413 && nonzero_bits (op0, mode) == 1)
4414 {
4415 op0 = expand_compound_operation (op0);
4416 return simplify_gen_unary (NEG, mode,
4417 gen_lowpart (mode, op0),
4418 mode);
4419 }
4420
4421 else if (STORE_FLAG_VALUE == -1
4422 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4423 && op1 == const0_rtx
4424 && mode == GET_MODE (op0)
4425 && (num_sign_bit_copies (op0, mode)
4426 == GET_MODE_BITSIZE (mode)))
4427 {
4428 op0 = expand_compound_operation (op0);
4429 return simplify_gen_unary (NOT, mode,
4430 gen_lowpart (mode, op0),
4431 mode);
4432 }
4433
4434 /* If X is 0/1, (eq X 0) is X-1. */
4435 else if (STORE_FLAG_VALUE == -1
4436 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4437 && op1 == const0_rtx
4438 && mode == GET_MODE (op0)
4439 && nonzero_bits (op0, mode) == 1)
4440 {
4441 op0 = expand_compound_operation (op0);
4442 return plus_constant (gen_lowpart (mode, op0), -1);
4443 }
4444
4445 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4446 one bit that might be nonzero, we can convert (ne x 0) to
4447 (ashift x c) where C puts the bit in the sign bit. Remove any
4448 AND with STORE_FLAG_VALUE when we are done, since we are only
4449 going to test the sign bit. */
4450 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4451 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4452 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4453 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4454 && op1 == const0_rtx
4455 && mode == GET_MODE (op0)
4456 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4457 {
4458 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4459 expand_compound_operation (op0),
4460 GET_MODE_BITSIZE (mode) - 1 - i);
4461 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4462 return XEXP (x, 0);
4463 else
4464 return x;
4465 }
4466
4467 /* If the code changed, return a whole new comparison. */
4468 if (new_code != code)
4469 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4470
4471 /* Otherwise, keep this operation, but maybe change its operands.
4472 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4473 SUBST (XEXP (x, 0), op0);
4474 SUBST (XEXP (x, 1), op1);
4475 }
4476 break;
4477
4478 case IF_THEN_ELSE:
4479 return simplify_if_then_else (x);
4480
4481 case ZERO_EXTRACT:
4482 case SIGN_EXTRACT:
4483 case ZERO_EXTEND:
4484 case SIGN_EXTEND:
4485 /* If we are processing SET_DEST, we are done. */
4486 if (in_dest)
4487 return x;
4488
4489 return expand_compound_operation (x);
4490
4491 case SET:
4492 return simplify_set (x);
4493
4494 case AND:
4495 case IOR:
4496 case XOR:
4497 return simplify_logical (x);
4498
4499 case ABS:
4500 /* (abs (neg <foo>)) -> (abs <foo>) */
4501 if (GET_CODE (XEXP (x, 0)) == NEG)
4502 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4503
4504 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4505 do nothing. */
4506 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4507 break;
4508
4509 /* If operand is something known to be positive, ignore the ABS. */
4510 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4511 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4512 <= HOST_BITS_PER_WIDE_INT)
4513 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4514 & ((HOST_WIDE_INT) 1
4515 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4516 == 0)))
4517 return XEXP (x, 0);
4518
4519 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4520 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4521 return gen_rtx_NEG (mode, XEXP (x, 0));
4522
4523 break;
4524
4525 case FFS:
4526 /* (ffs (*_extend <X>)) = (ffs <X>) */
4527 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4528 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4529 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4530 break;
4531
4532 case POPCOUNT:
4533 case PARITY:
4534 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4535 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4536 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4537 break;
4538
4539 case FLOAT:
4540 /* (float (sign_extend <X>)) = (float <X>). */
4541 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4542 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4543 break;
4544
4545 case ASHIFT:
4546 case LSHIFTRT:
4547 case ASHIFTRT:
4548 case ROTATE:
4549 case ROTATERT:
4550 /* If this is a shift by a constant amount, simplify it. */
4551 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4552 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4553 INTVAL (XEXP (x, 1)));
4554
4555 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4556 SUBST (XEXP (x, 1),
4557 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4558 ((HOST_WIDE_INT) 1
4559 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4560 - 1,
4561 NULL_RTX, 0));
4562 break;
4563
4564 case VEC_SELECT:
4565 {
4566 rtx op0 = XEXP (x, 0);
4567 rtx op1 = XEXP (x, 1);
4568 int len;
4569
4570 if (GET_CODE (op1) != PARALLEL)
4571 abort ();
4572 len = XVECLEN (op1, 0);
4573 if (len == 1
4574 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4575 && GET_CODE (op0) == VEC_CONCAT)
4576 {
4577 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4578
4579 /* Try to find the element in the VEC_CONCAT. */
4580 for (;;)
4581 {
4582 if (GET_MODE (op0) == GET_MODE (x))
4583 return op0;
4584 if (GET_CODE (op0) == VEC_CONCAT)
4585 {
4586 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4587 if (op0_size < offset)
4588 op0 = XEXP (op0, 0);
4589 else
4590 {
4591 offset -= op0_size;
4592 op0 = XEXP (op0, 1);
4593 }
4594 }
4595 else
4596 break;
4597 }
4598 }
4599 }
4600
4601 break;
4602
4603 default:
4604 break;
4605 }
4606
4607 return x;
4608 }
4609 \f
4610 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4611
4612 static rtx
4613 simplify_if_then_else (rtx x)
4614 {
4615 enum machine_mode mode = GET_MODE (x);
4616 rtx cond = XEXP (x, 0);
4617 rtx true_rtx = XEXP (x, 1);
4618 rtx false_rtx = XEXP (x, 2);
4619 enum rtx_code true_code = GET_CODE (cond);
4620 int comparison_p = COMPARISON_P (cond);
4621 rtx temp;
4622 int i;
4623 enum rtx_code false_code;
4624 rtx reversed;
4625
4626 /* Simplify storing of the truth value. */
4627 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4628 return simplify_gen_relational (true_code, mode, VOIDmode,
4629 XEXP (cond, 0), XEXP (cond, 1));
4630
4631 /* Also when the truth value has to be reversed. */
4632 if (comparison_p
4633 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4634 && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4635 XEXP (cond, 1))))
4636 return reversed;
4637
4638 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4639 in it is being compared against certain values. Get the true and false
4640 comparisons and see if that says anything about the value of each arm. */
4641
4642 if (comparison_p
4643 && ((false_code = combine_reversed_comparison_code (cond))
4644 != UNKNOWN)
4645 && GET_CODE (XEXP (cond, 0)) == REG)
4646 {
4647 HOST_WIDE_INT nzb;
4648 rtx from = XEXP (cond, 0);
4649 rtx true_val = XEXP (cond, 1);
4650 rtx false_val = true_val;
4651 int swapped = 0;
4652
4653 /* If FALSE_CODE is EQ, swap the codes and arms. */
4654
4655 if (false_code == EQ)
4656 {
4657 swapped = 1, true_code = EQ, false_code = NE;
4658 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4659 }
4660
4661 /* If we are comparing against zero and the expression being tested has
4662 only a single bit that might be nonzero, that is its value when it is
4663 not equal to zero. Similarly if it is known to be -1 or 0. */
4664
4665 if (true_code == EQ && true_val == const0_rtx
4666 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4667 false_code = EQ, false_val = GEN_INT (nzb);
4668 else if (true_code == EQ && true_val == const0_rtx
4669 && (num_sign_bit_copies (from, GET_MODE (from))
4670 == GET_MODE_BITSIZE (GET_MODE (from))))
4671 false_code = EQ, false_val = constm1_rtx;
4672
4673 /* Now simplify an arm if we know the value of the register in the
4674 branch and it is used in the arm. Be careful due to the potential
4675 of locally-shared RTL. */
4676
4677 if (reg_mentioned_p (from, true_rtx))
4678 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4679 from, true_val),
4680 pc_rtx, pc_rtx, 0, 0);
4681 if (reg_mentioned_p (from, false_rtx))
4682 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4683 from, false_val),
4684 pc_rtx, pc_rtx, 0, 0);
4685
4686 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4687 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4688
4689 true_rtx = XEXP (x, 1);
4690 false_rtx = XEXP (x, 2);
4691 true_code = GET_CODE (cond);
4692 }
4693
4694 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4695 reversed, do so to avoid needing two sets of patterns for
4696 subtract-and-branch insns. Similarly if we have a constant in the true
4697 arm, the false arm is the same as the first operand of the comparison, or
4698 the false arm is more complicated than the true arm. */
4699
4700 if (comparison_p
4701 && combine_reversed_comparison_code (cond) != UNKNOWN
4702 && (true_rtx == pc_rtx
4703 || (CONSTANT_P (true_rtx)
4704 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4705 || true_rtx == const0_rtx
4706 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4707 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4708 && !OBJECT_P (false_rtx))
4709 || reg_mentioned_p (true_rtx, false_rtx)
4710 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4711 {
4712 true_code = reversed_comparison_code (cond, NULL);
4713 SUBST (XEXP (x, 0),
4714 reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4715 XEXP (cond, 1)));
4716
4717 SUBST (XEXP (x, 1), false_rtx);
4718 SUBST (XEXP (x, 2), true_rtx);
4719
4720 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4721 cond = XEXP (x, 0);
4722
4723 /* It is possible that the conditional has been simplified out. */
4724 true_code = GET_CODE (cond);
4725 comparison_p = COMPARISON_P (cond);
4726 }
4727
4728 /* If the two arms are identical, we don't need the comparison. */
4729
4730 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4731 return true_rtx;
4732
4733 /* Convert a == b ? b : a to "a". */
4734 if (true_code == EQ && ! side_effects_p (cond)
4735 && !HONOR_NANS (mode)
4736 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4737 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4738 return false_rtx;
4739 else if (true_code == NE && ! side_effects_p (cond)
4740 && !HONOR_NANS (mode)
4741 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4742 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4743 return true_rtx;
4744
4745 /* Look for cases where we have (abs x) or (neg (abs X)). */
4746
4747 if (GET_MODE_CLASS (mode) == MODE_INT
4748 && GET_CODE (false_rtx) == NEG
4749 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4750 && comparison_p
4751 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4752 && ! side_effects_p (true_rtx))
4753 switch (true_code)
4754 {
4755 case GT:
4756 case GE:
4757 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4758 case LT:
4759 case LE:
4760 return
4761 simplify_gen_unary (NEG, mode,
4762 simplify_gen_unary (ABS, mode, true_rtx, mode),
4763 mode);
4764 default:
4765 break;
4766 }
4767
4768 /* Look for MIN or MAX. */
4769
4770 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4771 && comparison_p
4772 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4773 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4774 && ! side_effects_p (cond))
4775 switch (true_code)
4776 {
4777 case GE:
4778 case GT:
4779 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
4780 case LE:
4781 case LT:
4782 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
4783 case GEU:
4784 case GTU:
4785 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
4786 case LEU:
4787 case LTU:
4788 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
4789 default:
4790 break;
4791 }
4792
4793 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4794 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4795 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4796 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4797 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4798 neither 1 or -1, but it isn't worth checking for. */
4799
4800 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4801 && comparison_p
4802 && GET_MODE_CLASS (mode) == MODE_INT
4803 && ! side_effects_p (x))
4804 {
4805 rtx t = make_compound_operation (true_rtx, SET);
4806 rtx f = make_compound_operation (false_rtx, SET);
4807 rtx cond_op0 = XEXP (cond, 0);
4808 rtx cond_op1 = XEXP (cond, 1);
4809 enum rtx_code op = NIL, extend_op = NIL;
4810 enum machine_mode m = mode;
4811 rtx z = 0, c1 = NULL_RTX;
4812
4813 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4814 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4815 || GET_CODE (t) == ASHIFT
4816 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4817 && rtx_equal_p (XEXP (t, 0), f))
4818 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4819
4820 /* If an identity-zero op is commutative, check whether there
4821 would be a match if we swapped the operands. */
4822 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4823 || GET_CODE (t) == XOR)
4824 && rtx_equal_p (XEXP (t, 1), f))
4825 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4826 else if (GET_CODE (t) == SIGN_EXTEND
4827 && (GET_CODE (XEXP (t, 0)) == PLUS
4828 || GET_CODE (XEXP (t, 0)) == MINUS
4829 || GET_CODE (XEXP (t, 0)) == IOR
4830 || GET_CODE (XEXP (t, 0)) == XOR
4831 || GET_CODE (XEXP (t, 0)) == ASHIFT
4832 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4833 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4834 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4835 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4836 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4837 && (num_sign_bit_copies (f, GET_MODE (f))
4838 > (unsigned int)
4839 (GET_MODE_BITSIZE (mode)
4840 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4841 {
4842 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4843 extend_op = SIGN_EXTEND;
4844 m = GET_MODE (XEXP (t, 0));
4845 }
4846 else if (GET_CODE (t) == SIGN_EXTEND
4847 && (GET_CODE (XEXP (t, 0)) == PLUS
4848 || GET_CODE (XEXP (t, 0)) == IOR
4849 || GET_CODE (XEXP (t, 0)) == XOR)
4850 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4851 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4852 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4853 && (num_sign_bit_copies (f, GET_MODE (f))
4854 > (unsigned int)
4855 (GET_MODE_BITSIZE (mode)
4856 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4857 {
4858 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4859 extend_op = SIGN_EXTEND;
4860 m = GET_MODE (XEXP (t, 0));
4861 }
4862 else if (GET_CODE (t) == ZERO_EXTEND
4863 && (GET_CODE (XEXP (t, 0)) == PLUS
4864 || GET_CODE (XEXP (t, 0)) == MINUS
4865 || GET_CODE (XEXP (t, 0)) == IOR
4866 || GET_CODE (XEXP (t, 0)) == XOR
4867 || GET_CODE (XEXP (t, 0)) == ASHIFT
4868 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4869 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4870 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4871 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4872 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4873 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4874 && ((nonzero_bits (f, GET_MODE (f))
4875 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4876 == 0))
4877 {
4878 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4879 extend_op = ZERO_EXTEND;
4880 m = GET_MODE (XEXP (t, 0));
4881 }
4882 else if (GET_CODE (t) == ZERO_EXTEND
4883 && (GET_CODE (XEXP (t, 0)) == PLUS
4884 || GET_CODE (XEXP (t, 0)) == IOR
4885 || GET_CODE (XEXP (t, 0)) == XOR)
4886 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4887 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4888 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4889 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4890 && ((nonzero_bits (f, GET_MODE (f))
4891 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4892 == 0))
4893 {
4894 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4895 extend_op = ZERO_EXTEND;
4896 m = GET_MODE (XEXP (t, 0));
4897 }
4898
4899 if (z)
4900 {
4901 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
4902 cond_op0, cond_op1),
4903 pc_rtx, pc_rtx, 0, 0);
4904 temp = simplify_gen_binary (MULT, m, temp,
4905 simplify_gen_binary (MULT, m, c1,
4906 const_true_rtx));
4907 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4908 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
4909
4910 if (extend_op != NIL)
4911 temp = simplify_gen_unary (extend_op, mode, temp, m);
4912
4913 return temp;
4914 }
4915 }
4916
4917 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4918 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4919 negation of a single bit, we can convert this operation to a shift. We
4920 can actually do this more generally, but it doesn't seem worth it. */
4921
4922 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4923 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4924 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4925 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
4926 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4927 == GET_MODE_BITSIZE (mode))
4928 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
4929 return
4930 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4931 gen_lowpart (mode, XEXP (cond, 0)), i);
4932
4933 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
4934 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4935 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4936 && GET_MODE (XEXP (cond, 0)) == mode
4937 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
4938 == nonzero_bits (XEXP (cond, 0), mode)
4939 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
4940 return XEXP (cond, 0);
4941
4942 return x;
4943 }
4944 \f
4945 /* Simplify X, a SET expression. Return the new expression. */
4946
4947 static rtx
4948 simplify_set (rtx x)
4949 {
4950 rtx src = SET_SRC (x);
4951 rtx dest = SET_DEST (x);
4952 enum machine_mode mode
4953 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4954 rtx other_insn;
4955 rtx *cc_use;
4956
4957 /* (set (pc) (return)) gets written as (return). */
4958 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4959 return src;
4960
4961 /* Now that we know for sure which bits of SRC we are using, see if we can
4962 simplify the expression for the object knowing that we only need the
4963 low-order bits. */
4964
4965 if (GET_MODE_CLASS (mode) == MODE_INT
4966 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
4967 {
4968 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
4969 SUBST (SET_SRC (x), src);
4970 }
4971
4972 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4973 the comparison result and try to simplify it unless we already have used
4974 undobuf.other_insn. */
4975 if ((GET_MODE_CLASS (mode) == MODE_CC
4976 || GET_CODE (src) == COMPARE
4977 || CC0_P (dest))
4978 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4979 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4980 && COMPARISON_P (*cc_use)
4981 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4982 {
4983 enum rtx_code old_code = GET_CODE (*cc_use);
4984 enum rtx_code new_code;
4985 rtx op0, op1, tmp;
4986 int other_changed = 0;
4987 enum machine_mode compare_mode = GET_MODE (dest);
4988
4989 if (GET_CODE (src) == COMPARE)
4990 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4991 else
4992 op0 = src, op1 = const0_rtx;
4993
4994 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
4995 op0, op1);
4996 if (!tmp)
4997 new_code = old_code;
4998 else if (!CONSTANT_P (tmp))
4999 {
5000 new_code = GET_CODE (tmp);
5001 op0 = XEXP (tmp, 0);
5002 op1 = XEXP (tmp, 1);
5003 }
5004 else
5005 {
5006 rtx pat = PATTERN (other_insn);
5007 undobuf.other_insn = other_insn;
5008 SUBST (*cc_use, tmp);
5009
5010 /* Attempt to simplify CC user. */
5011 if (GET_CODE (pat) == SET)
5012 {
5013 rtx new = simplify_rtx (SET_SRC (pat));
5014 if (new != NULL_RTX)
5015 SUBST (SET_SRC (pat), new);
5016 }
5017
5018 /* Convert X into a no-op move. */
5019 SUBST (SET_DEST (x), pc_rtx);
5020 SUBST (SET_SRC (x), pc_rtx);
5021 return x;
5022 }
5023
5024 /* Simplify our comparison, if possible. */
5025 new_code = simplify_comparison (new_code, &op0, &op1);
5026
5027 #ifdef SELECT_CC_MODE
5028 /* If this machine has CC modes other than CCmode, check to see if we
5029 need to use a different CC mode here. */
5030 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5031 compare_mode = GET_MODE (op0);
5032 else
5033 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5034
5035 #ifndef HAVE_cc0
5036 /* If the mode changed, we have to change SET_DEST, the mode in the
5037 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5038 a hard register, just build new versions with the proper mode. If it
5039 is a pseudo, we lose unless it is only time we set the pseudo, in
5040 which case we can safely change its mode. */
5041 if (compare_mode != GET_MODE (dest))
5042 {
5043 unsigned int regno = REGNO (dest);
5044 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5045
5046 if (regno < FIRST_PSEUDO_REGISTER
5047 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5048 {
5049 if (regno >= FIRST_PSEUDO_REGISTER)
5050 SUBST (regno_reg_rtx[regno], new_dest);
5051
5052 SUBST (SET_DEST (x), new_dest);
5053 SUBST (XEXP (*cc_use, 0), new_dest);
5054 other_changed = 1;
5055
5056 dest = new_dest;
5057 }
5058 }
5059 #endif /* cc0 */
5060 #endif /* SELECT_CC_MODE */
5061
5062 /* If the code changed, we have to build a new comparison in
5063 undobuf.other_insn. */
5064 if (new_code != old_code)
5065 {
5066 int other_changed_previously = other_changed;
5067 unsigned HOST_WIDE_INT mask;
5068
5069 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5070 dest, const0_rtx));
5071 other_changed = 1;
5072
5073 /* If the only change we made was to change an EQ into an NE or
5074 vice versa, OP0 has only one bit that might be nonzero, and OP1
5075 is zero, check if changing the user of the condition code will
5076 produce a valid insn. If it won't, we can keep the original code
5077 in that insn by surrounding our operation with an XOR. */
5078
5079 if (((old_code == NE && new_code == EQ)
5080 || (old_code == EQ && new_code == NE))
5081 && ! other_changed_previously && op1 == const0_rtx
5082 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5083 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5084 {
5085 rtx pat = PATTERN (other_insn), note = 0;
5086
5087 if ((recog_for_combine (&pat, other_insn, &note) < 0
5088 && ! check_asm_operands (pat)))
5089 {
5090 PUT_CODE (*cc_use, old_code);
5091 other_changed = 0;
5092
5093 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5094 op0, GEN_INT (mask));
5095 }
5096 }
5097 }
5098
5099 if (other_changed)
5100 undobuf.other_insn = other_insn;
5101
5102 #ifdef HAVE_cc0
5103 /* If we are now comparing against zero, change our source if
5104 needed. If we do not use cc0, we always have a COMPARE. */
5105 if (op1 == const0_rtx && dest == cc0_rtx)
5106 {
5107 SUBST (SET_SRC (x), op0);
5108 src = op0;
5109 }
5110 else
5111 #endif
5112
5113 /* Otherwise, if we didn't previously have a COMPARE in the
5114 correct mode, we need one. */
5115 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5116 {
5117 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5118 src = SET_SRC (x);
5119 }
5120 else
5121 {
5122 /* Otherwise, update the COMPARE if needed. */
5123 SUBST (XEXP (src, 0), op0);
5124 SUBST (XEXP (src, 1), op1);
5125 }
5126 }
5127 else
5128 {
5129 /* Get SET_SRC in a form where we have placed back any
5130 compound expressions. Then do the checks below. */
5131 src = make_compound_operation (src, SET);
5132 SUBST (SET_SRC (x), src);
5133 }
5134
5135 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5136 and X being a REG or (subreg (reg)), we may be able to convert this to
5137 (set (subreg:m2 x) (op)).
5138
5139 We can always do this if M1 is narrower than M2 because that means that
5140 we only care about the low bits of the result.
5141
5142 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5143 perform a narrower operation than requested since the high-order bits will
5144 be undefined. On machine where it is defined, this transformation is safe
5145 as long as M1 and M2 have the same number of words. */
5146
5147 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5148 && !OBJECT_P (SUBREG_REG (src))
5149 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5150 / UNITS_PER_WORD)
5151 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5152 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5153 #ifndef WORD_REGISTER_OPERATIONS
5154 && (GET_MODE_SIZE (GET_MODE (src))
5155 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5156 #endif
5157 #ifdef CANNOT_CHANGE_MODE_CLASS
5158 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5159 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5160 GET_MODE (SUBREG_REG (src)),
5161 GET_MODE (src)))
5162 #endif
5163 && (GET_CODE (dest) == REG
5164 || (GET_CODE (dest) == SUBREG
5165 && GET_CODE (SUBREG_REG (dest)) == REG)))
5166 {
5167 SUBST (SET_DEST (x),
5168 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5169 dest));
5170 SUBST (SET_SRC (x), SUBREG_REG (src));
5171
5172 src = SET_SRC (x), dest = SET_DEST (x);
5173 }
5174
5175 #ifdef HAVE_cc0
5176 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5177 in SRC. */
5178 if (dest == cc0_rtx
5179 && GET_CODE (src) == SUBREG
5180 && subreg_lowpart_p (src)
5181 && (GET_MODE_BITSIZE (GET_MODE (src))
5182 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5183 {
5184 rtx inner = SUBREG_REG (src);
5185 enum machine_mode inner_mode = GET_MODE (inner);
5186
5187 /* Here we make sure that we don't have a sign bit on. */
5188 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5189 && (nonzero_bits (inner, inner_mode)
5190 < ((unsigned HOST_WIDE_INT) 1
5191 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5192 {
5193 SUBST (SET_SRC (x), inner);
5194 src = SET_SRC (x);
5195 }
5196 }
5197 #endif
5198
5199 #ifdef LOAD_EXTEND_OP
5200 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5201 would require a paradoxical subreg. Replace the subreg with a
5202 zero_extend to avoid the reload that would otherwise be required. */
5203
5204 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5205 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5206 && SUBREG_BYTE (src) == 0
5207 && (GET_MODE_SIZE (GET_MODE (src))
5208 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5209 && GET_CODE (SUBREG_REG (src)) == MEM)
5210 {
5211 SUBST (SET_SRC (x),
5212 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5213 GET_MODE (src), SUBREG_REG (src)));
5214
5215 src = SET_SRC (x);
5216 }
5217 #endif
5218
5219 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5220 are comparing an item known to be 0 or -1 against 0, use a logical
5221 operation instead. Check for one of the arms being an IOR of the other
5222 arm with some value. We compute three terms to be IOR'ed together. In
5223 practice, at most two will be nonzero. Then we do the IOR's. */
5224
5225 if (GET_CODE (dest) != PC
5226 && GET_CODE (src) == IF_THEN_ELSE
5227 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5228 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5229 && XEXP (XEXP (src, 0), 1) == const0_rtx
5230 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5231 #ifdef HAVE_conditional_move
5232 && ! can_conditionally_move_p (GET_MODE (src))
5233 #endif
5234 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5235 GET_MODE (XEXP (XEXP (src, 0), 0)))
5236 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5237 && ! side_effects_p (src))
5238 {
5239 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5240 ? XEXP (src, 1) : XEXP (src, 2));
5241 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5242 ? XEXP (src, 2) : XEXP (src, 1));
5243 rtx term1 = const0_rtx, term2, term3;
5244
5245 if (GET_CODE (true_rtx) == IOR
5246 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5247 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5248 else if (GET_CODE (true_rtx) == IOR
5249 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5250 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5251 else if (GET_CODE (false_rtx) == IOR
5252 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5253 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5254 else if (GET_CODE (false_rtx) == IOR
5255 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5256 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5257
5258 term2 = simplify_gen_binary (AND, GET_MODE (src),
5259 XEXP (XEXP (src, 0), 0), true_rtx);
5260 term3 = simplify_gen_binary (AND, GET_MODE (src),
5261 simplify_gen_unary (NOT, GET_MODE (src),
5262 XEXP (XEXP (src, 0), 0),
5263 GET_MODE (src)),
5264 false_rtx);
5265
5266 SUBST (SET_SRC (x),
5267 simplify_gen_binary (IOR, GET_MODE (src),
5268 simplify_gen_binary (IOR, GET_MODE (src),
5269 term1, term2),
5270 term3));
5271
5272 src = SET_SRC (x);
5273 }
5274
5275 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5276 whole thing fail. */
5277 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5278 return src;
5279 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5280 return dest;
5281 else
5282 /* Convert this into a field assignment operation, if possible. */
5283 return make_field_assignment (x);
5284 }
5285 \f
5286 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5287 result. */
5288
5289 static rtx
5290 simplify_logical (rtx x)
5291 {
5292 enum machine_mode mode = GET_MODE (x);
5293 rtx op0 = XEXP (x, 0);
5294 rtx op1 = XEXP (x, 1);
5295 rtx reversed;
5296
5297 switch (GET_CODE (x))
5298 {
5299 case AND:
5300 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5301 insn (and may simplify more). */
5302 if (GET_CODE (op0) == XOR
5303 && rtx_equal_p (XEXP (op0, 0), op1)
5304 && ! side_effects_p (op1))
5305 x = simplify_gen_binary (AND, mode,
5306 simplify_gen_unary (NOT, mode,
5307 XEXP (op0, 1), mode),
5308 op1);
5309
5310 if (GET_CODE (op0) == XOR
5311 && rtx_equal_p (XEXP (op0, 1), op1)
5312 && ! side_effects_p (op1))
5313 x = simplify_gen_binary (AND, mode,
5314 simplify_gen_unary (NOT, mode,
5315 XEXP (op0, 0), mode),
5316 op1);
5317
5318 /* Similarly for (~(A ^ B)) & A. */
5319 if (GET_CODE (op0) == NOT
5320 && GET_CODE (XEXP (op0, 0)) == XOR
5321 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5322 && ! side_effects_p (op1))
5323 x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5324
5325 if (GET_CODE (op0) == NOT
5326 && GET_CODE (XEXP (op0, 0)) == XOR
5327 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5328 && ! side_effects_p (op1))
5329 x = simplify_gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5330
5331 /* We can call simplify_and_const_int only if we don't lose
5332 any (sign) bits when converting INTVAL (op1) to
5333 "unsigned HOST_WIDE_INT". */
5334 if (GET_CODE (op1) == CONST_INT
5335 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5336 || INTVAL (op1) > 0))
5337 {
5338 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5339
5340 /* If we have (ior (and (X C1) C2)) and the next restart would be
5341 the last, simplify this by making C1 as small as possible
5342 and then exit. Only do this if C1 actually changes: for now
5343 this only saves memory but, should this transformation be
5344 moved to simplify-rtx.c, we'd risk unbounded recursion there. */
5345 if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5346 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5347 && GET_CODE (op1) == CONST_INT
5348 && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5349 return simplify_gen_binary (IOR, mode,
5350 simplify_gen_binary
5351 (AND, mode, XEXP (op0, 0),
5352 GEN_INT (INTVAL (XEXP (op0, 1))
5353 & ~INTVAL (op1))), op1);
5354
5355 if (GET_CODE (x) != AND)
5356 return x;
5357
5358 op0 = XEXP (x, 0);
5359 op1 = XEXP (x, 1);
5360 }
5361
5362 /* Convert (A | B) & A to A. */
5363 if (GET_CODE (op0) == IOR
5364 && (rtx_equal_p (XEXP (op0, 0), op1)
5365 || rtx_equal_p (XEXP (op0, 1), op1))
5366 && ! side_effects_p (XEXP (op0, 0))
5367 && ! side_effects_p (XEXP (op0, 1)))
5368 return op1;
5369
5370 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5371 apply the distributive law and then the inverse distributive
5372 law to see if things simplify. */
5373 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR
5374 || GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5375 {
5376 rtx result = distribute_and_simplify_rtx (x);
5377 if (result)
5378 return result;
5379 }
5380 break;
5381
5382 case IOR:
5383 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5384 if (GET_CODE (op1) == CONST_INT
5385 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5386 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5387 return op1;
5388
5389 /* Convert (A & B) | A to A. */
5390 if (GET_CODE (op0) == AND
5391 && (rtx_equal_p (XEXP (op0, 0), op1)
5392 || rtx_equal_p (XEXP (op0, 1), op1))
5393 && ! side_effects_p (XEXP (op0, 0))
5394 && ! side_effects_p (XEXP (op0, 1)))
5395 return op1;
5396
5397 /* If we have (ior (and A B) C), apply the distributive law and then
5398 the inverse distributive law to see if things simplify. */
5399
5400 if (GET_CODE (op0) == AND || GET_CODE (op1) == AND)
5401 {
5402 rtx result = distribute_and_simplify_rtx (x);
5403 if (result)
5404 return result;
5405 }
5406
5407 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5408 mode size to (rotate A CX). */
5409
5410 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5411 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5412 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5413 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5414 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5415 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5416 == GET_MODE_BITSIZE (mode)))
5417 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5418 (GET_CODE (op0) == ASHIFT
5419 ? XEXP (op0, 1) : XEXP (op1, 1)));
5420
5421 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5422 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5423 does not affect any of the bits in OP1, it can really be done
5424 as a PLUS and we can associate. We do this by seeing if OP1
5425 can be safely shifted left C bits. */
5426 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5427 && GET_CODE (XEXP (op0, 0)) == PLUS
5428 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5429 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5430 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5431 {
5432 int count = INTVAL (XEXP (op0, 1));
5433 HOST_WIDE_INT mask = INTVAL (op1) << count;
5434
5435 if (mask >> count == INTVAL (op1)
5436 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5437 {
5438 SUBST (XEXP (XEXP (op0, 0), 1),
5439 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5440 return op0;
5441 }
5442 }
5443 break;
5444
5445 case XOR:
5446 /* If we are XORing two things that have no bits in common,
5447 convert them into an IOR. This helps to detect rotation encoded
5448 using those methods and possibly other simplifications. */
5449
5450 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5451 && (nonzero_bits (op0, mode)
5452 & nonzero_bits (op1, mode)) == 0)
5453 return (simplify_gen_binary (IOR, mode, op0, op1));
5454
5455 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5456 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5457 (NOT y). */
5458 {
5459 int num_negated = 0;
5460
5461 if (GET_CODE (op0) == NOT)
5462 num_negated++, op0 = XEXP (op0, 0);
5463 if (GET_CODE (op1) == NOT)
5464 num_negated++, op1 = XEXP (op1, 0);
5465
5466 if (num_negated == 2)
5467 {
5468 SUBST (XEXP (x, 0), op0);
5469 SUBST (XEXP (x, 1), op1);
5470 }
5471 else if (num_negated == 1)
5472 return
5473 simplify_gen_unary (NOT, mode,
5474 simplify_gen_binary (XOR, mode, op0, op1),
5475 mode);
5476 }
5477
5478 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5479 correspond to a machine insn or result in further simplifications
5480 if B is a constant. */
5481
5482 if (GET_CODE (op0) == AND
5483 && rtx_equal_p (XEXP (op0, 1), op1)
5484 && ! side_effects_p (op1))
5485 return simplify_gen_binary (AND, mode,
5486 simplify_gen_unary (NOT, mode,
5487 XEXP (op0, 0), mode),
5488 op1);
5489
5490 else if (GET_CODE (op0) == AND
5491 && rtx_equal_p (XEXP (op0, 0), op1)
5492 && ! side_effects_p (op1))
5493 return simplify_gen_binary (AND, mode,
5494 simplify_gen_unary (NOT, mode,
5495 XEXP (op0, 1), mode),
5496 op1);
5497
5498 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5499 comparison if STORE_FLAG_VALUE is 1. */
5500 if (STORE_FLAG_VALUE == 1
5501 && op1 == const1_rtx
5502 && COMPARISON_P (op0)
5503 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5504 XEXP (op0, 1))))
5505 return reversed;
5506
5507 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5508 is (lt foo (const_int 0)), so we can perform the above
5509 simplification if STORE_FLAG_VALUE is 1. */
5510
5511 if (STORE_FLAG_VALUE == 1
5512 && op1 == const1_rtx
5513 && GET_CODE (op0) == LSHIFTRT
5514 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5515 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5516 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5517
5518 /* (xor (comparison foo bar) (const_int sign-bit))
5519 when STORE_FLAG_VALUE is the sign bit. */
5520 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5521 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5522 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5523 && op1 == const_true_rtx
5524 && COMPARISON_P (op0)
5525 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5526 XEXP (op0, 1))))
5527 return reversed;
5528
5529 break;
5530
5531 default:
5532 abort ();
5533 }
5534
5535 return x;
5536 }
5537 \f
5538 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5539 operations" because they can be replaced with two more basic operations.
5540 ZERO_EXTEND is also considered "compound" because it can be replaced with
5541 an AND operation, which is simpler, though only one operation.
5542
5543 The function expand_compound_operation is called with an rtx expression
5544 and will convert it to the appropriate shifts and AND operations,
5545 simplifying at each stage.
5546
5547 The function make_compound_operation is called to convert an expression
5548 consisting of shifts and ANDs into the equivalent compound expression.
5549 It is the inverse of this function, loosely speaking. */
5550
5551 static rtx
5552 expand_compound_operation (rtx x)
5553 {
5554 unsigned HOST_WIDE_INT pos = 0, len;
5555 int unsignedp = 0;
5556 unsigned int modewidth;
5557 rtx tem;
5558
5559 switch (GET_CODE (x))
5560 {
5561 case ZERO_EXTEND:
5562 unsignedp = 1;
5563 case SIGN_EXTEND:
5564 /* We can't necessarily use a const_int for a multiword mode;
5565 it depends on implicitly extending the value.
5566 Since we don't know the right way to extend it,
5567 we can't tell whether the implicit way is right.
5568
5569 Even for a mode that is no wider than a const_int,
5570 we can't win, because we need to sign extend one of its bits through
5571 the rest of it, and we don't know which bit. */
5572 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5573 return x;
5574
5575 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5576 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5577 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5578 reloaded. If not for that, MEM's would very rarely be safe.
5579
5580 Reject MODEs bigger than a word, because we might not be able
5581 to reference a two-register group starting with an arbitrary register
5582 (and currently gen_lowpart might crash for a SUBREG). */
5583
5584 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5585 return x;
5586
5587 /* Reject MODEs that aren't scalar integers because turning vector
5588 or complex modes into shifts causes problems. */
5589
5590 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5591 return x;
5592
5593 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5594 /* If the inner object has VOIDmode (the only way this can happen
5595 is if it is an ASM_OPERANDS), we can't do anything since we don't
5596 know how much masking to do. */
5597 if (len == 0)
5598 return x;
5599
5600 break;
5601
5602 case ZERO_EXTRACT:
5603 unsignedp = 1;
5604 case SIGN_EXTRACT:
5605 /* If the operand is a CLOBBER, just return it. */
5606 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5607 return XEXP (x, 0);
5608
5609 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5610 || GET_CODE (XEXP (x, 2)) != CONST_INT
5611 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5612 return x;
5613
5614 /* Reject MODEs that aren't scalar integers because turning vector
5615 or complex modes into shifts causes problems. */
5616
5617 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5618 return x;
5619
5620 len = INTVAL (XEXP (x, 1));
5621 pos = INTVAL (XEXP (x, 2));
5622
5623 /* If this goes outside the object being extracted, replace the object
5624 with a (use (mem ...)) construct that only combine understands
5625 and is used only for this purpose. */
5626 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5627 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5628
5629 if (BITS_BIG_ENDIAN)
5630 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5631
5632 break;
5633
5634 default:
5635 return x;
5636 }
5637 /* Convert sign extension to zero extension, if we know that the high
5638 bit is not set, as this is easier to optimize. It will be converted
5639 back to cheaper alternative in make_extraction. */
5640 if (GET_CODE (x) == SIGN_EXTEND
5641 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5642 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5643 & ~(((unsigned HOST_WIDE_INT)
5644 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5645 >> 1))
5646 == 0)))
5647 {
5648 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5649 rtx temp2 = expand_compound_operation (temp);
5650
5651 /* Make sure this is a profitable operation. */
5652 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5653 return temp2;
5654 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5655 return temp;
5656 else
5657 return x;
5658 }
5659
5660 /* We can optimize some special cases of ZERO_EXTEND. */
5661 if (GET_CODE (x) == ZERO_EXTEND)
5662 {
5663 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5664 know that the last value didn't have any inappropriate bits
5665 set. */
5666 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5667 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5668 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5669 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5670 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5671 return XEXP (XEXP (x, 0), 0);
5672
5673 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5674 if (GET_CODE (XEXP (x, 0)) == SUBREG
5675 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5676 && subreg_lowpart_p (XEXP (x, 0))
5677 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5678 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5679 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5680 return SUBREG_REG (XEXP (x, 0));
5681
5682 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5683 is a comparison and STORE_FLAG_VALUE permits. This is like
5684 the first case, but it works even when GET_MODE (x) is larger
5685 than HOST_WIDE_INT. */
5686 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5687 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5688 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5689 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5690 <= HOST_BITS_PER_WIDE_INT)
5691 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5692 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5693 return XEXP (XEXP (x, 0), 0);
5694
5695 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5696 if (GET_CODE (XEXP (x, 0)) == SUBREG
5697 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5698 && subreg_lowpart_p (XEXP (x, 0))
5699 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5700 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5701 <= HOST_BITS_PER_WIDE_INT)
5702 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5703 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5704 return SUBREG_REG (XEXP (x, 0));
5705
5706 }
5707
5708 /* If we reach here, we want to return a pair of shifts. The inner
5709 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5710 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5711 logical depending on the value of UNSIGNEDP.
5712
5713 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5714 converted into an AND of a shift.
5715
5716 We must check for the case where the left shift would have a negative
5717 count. This can happen in a case like (x >> 31) & 255 on machines
5718 that can't shift by a constant. On those machines, we would first
5719 combine the shift with the AND to produce a variable-position
5720 extraction. Then the constant of 31 would be substituted in to produce
5721 a such a position. */
5722
5723 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5724 if (modewidth + len >= pos)
5725 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5726 GET_MODE (x),
5727 simplify_shift_const (NULL_RTX, ASHIFT,
5728 GET_MODE (x),
5729 XEXP (x, 0),
5730 modewidth - pos - len),
5731 modewidth - len);
5732
5733 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5734 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5735 simplify_shift_const (NULL_RTX, LSHIFTRT,
5736 GET_MODE (x),
5737 XEXP (x, 0), pos),
5738 ((HOST_WIDE_INT) 1 << len) - 1);
5739 else
5740 /* Any other cases we can't handle. */
5741 return x;
5742
5743 /* If we couldn't do this for some reason, return the original
5744 expression. */
5745 if (GET_CODE (tem) == CLOBBER)
5746 return x;
5747
5748 return tem;
5749 }
5750 \f
5751 /* X is a SET which contains an assignment of one object into
5752 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5753 or certain SUBREGS). If possible, convert it into a series of
5754 logical operations.
5755
5756 We half-heartedly support variable positions, but do not at all
5757 support variable lengths. */
5758
5759 static rtx
5760 expand_field_assignment (rtx x)
5761 {
5762 rtx inner;
5763 rtx pos; /* Always counts from low bit. */
5764 int len;
5765 rtx mask, cleared, masked;
5766 enum machine_mode compute_mode;
5767
5768 /* Loop until we find something we can't simplify. */
5769 while (1)
5770 {
5771 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5772 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5773 {
5774 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5775 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5776 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5777 }
5778 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5779 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5780 {
5781 inner = XEXP (SET_DEST (x), 0);
5782 len = INTVAL (XEXP (SET_DEST (x), 1));
5783 pos = XEXP (SET_DEST (x), 2);
5784
5785 /* If the position is constant and spans the width of INNER,
5786 surround INNER with a USE to indicate this. */
5787 if (GET_CODE (pos) == CONST_INT
5788 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5789 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5790
5791 if (BITS_BIG_ENDIAN)
5792 {
5793 if (GET_CODE (pos) == CONST_INT)
5794 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5795 - INTVAL (pos));
5796 else if (GET_CODE (pos) == MINUS
5797 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5798 && (INTVAL (XEXP (pos, 1))
5799 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5800 /* If position is ADJUST - X, new position is X. */
5801 pos = XEXP (pos, 0);
5802 else
5803 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
5804 GEN_INT (GET_MODE_BITSIZE (
5805 GET_MODE (inner))
5806 - len),
5807 pos);
5808 }
5809 }
5810
5811 /* A SUBREG between two modes that occupy the same numbers of words
5812 can be done by moving the SUBREG to the source. */
5813 else if (GET_CODE (SET_DEST (x)) == SUBREG
5814 /* We need SUBREGs to compute nonzero_bits properly. */
5815 && nonzero_sign_valid
5816 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5817 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5818 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5819 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5820 {
5821 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5822 gen_lowpart
5823 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5824 SET_SRC (x)));
5825 continue;
5826 }
5827 else
5828 break;
5829
5830 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5831 inner = SUBREG_REG (inner);
5832
5833 compute_mode = GET_MODE (inner);
5834
5835 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
5836 if (! SCALAR_INT_MODE_P (compute_mode))
5837 {
5838 enum machine_mode imode;
5839
5840 /* Don't do anything for vector or complex integral types. */
5841 if (! FLOAT_MODE_P (compute_mode))
5842 break;
5843
5844 /* Try to find an integral mode to pun with. */
5845 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5846 if (imode == BLKmode)
5847 break;
5848
5849 compute_mode = imode;
5850 inner = gen_lowpart (imode, inner);
5851 }
5852
5853 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5854 if (len >= HOST_BITS_PER_WIDE_INT)
5855 break;
5856
5857 /* Now compute the equivalent expression. Make a copy of INNER
5858 for the SET_DEST in case it is a MEM into which we will substitute;
5859 we don't want shared RTL in that case. */
5860 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5861 cleared = simplify_gen_binary (AND, compute_mode,
5862 simplify_gen_unary (NOT, compute_mode,
5863 simplify_gen_binary (ASHIFT,
5864 compute_mode,
5865 mask, pos),
5866 compute_mode),
5867 inner);
5868 masked = simplify_gen_binary (ASHIFT, compute_mode,
5869 simplify_gen_binary (
5870 AND, compute_mode,
5871 gen_lowpart (compute_mode, SET_SRC (x)),
5872 mask),
5873 pos);
5874
5875 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
5876 simplify_gen_binary (IOR, compute_mode,
5877 cleared, masked));
5878 }
5879
5880 return x;
5881 }
5882 \f
5883 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5884 it is an RTX that represents a variable starting position; otherwise,
5885 POS is the (constant) starting bit position (counted from the LSB).
5886
5887 INNER may be a USE. This will occur when we started with a bitfield
5888 that went outside the boundary of the object in memory, which is
5889 allowed on most machines. To isolate this case, we produce a USE
5890 whose mode is wide enough and surround the MEM with it. The only
5891 code that understands the USE is this routine. If it is not removed,
5892 it will cause the resulting insn not to match.
5893
5894 UNSIGNEDP is nonzero for an unsigned reference and zero for a
5895 signed reference.
5896
5897 IN_DEST is nonzero if this is a reference in the destination of a
5898 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
5899 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5900 be used.
5901
5902 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
5903 ZERO_EXTRACT should be built even for bits starting at bit 0.
5904
5905 MODE is the desired mode of the result (if IN_DEST == 0).
5906
5907 The result is an RTX for the extraction or NULL_RTX if the target
5908 can't handle it. */
5909
5910 static rtx
5911 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5912 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5913 int in_dest, int in_compare)
5914 {
5915 /* This mode describes the size of the storage area
5916 to fetch the overall value from. Within that, we
5917 ignore the POS lowest bits, etc. */
5918 enum machine_mode is_mode = GET_MODE (inner);
5919 enum machine_mode inner_mode;
5920 enum machine_mode wanted_inner_mode = byte_mode;
5921 enum machine_mode wanted_inner_reg_mode = word_mode;
5922 enum machine_mode pos_mode = word_mode;
5923 enum machine_mode extraction_mode = word_mode;
5924 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5925 int spans_byte = 0;
5926 rtx new = 0;
5927 rtx orig_pos_rtx = pos_rtx;
5928 HOST_WIDE_INT orig_pos;
5929
5930 /* Get some information about INNER and get the innermost object. */
5931 if (GET_CODE (inner) == USE)
5932 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5933 /* We don't need to adjust the position because we set up the USE
5934 to pretend that it was a full-word object. */
5935 spans_byte = 1, inner = XEXP (inner, 0);
5936 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5937 {
5938 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5939 consider just the QI as the memory to extract from.
5940 The subreg adds or removes high bits; its mode is
5941 irrelevant to the meaning of this extraction,
5942 since POS and LEN count from the lsb. */
5943 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5944 is_mode = GET_MODE (SUBREG_REG (inner));
5945 inner = SUBREG_REG (inner);
5946 }
5947 else if (GET_CODE (inner) == ASHIFT
5948 && GET_CODE (XEXP (inner, 1)) == CONST_INT
5949 && pos_rtx == 0 && pos == 0
5950 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
5951 {
5952 /* We're extracting the least significant bits of an rtx
5953 (ashift X (const_int C)), where LEN > C. Extract the
5954 least significant (LEN - C) bits of X, giving an rtx
5955 whose mode is MODE, then shift it left C times. */
5956 new = make_extraction (mode, XEXP (inner, 0),
5957 0, 0, len - INTVAL (XEXP (inner, 1)),
5958 unsignedp, in_dest, in_compare);
5959 if (new != 0)
5960 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
5961 }
5962
5963 inner_mode = GET_MODE (inner);
5964
5965 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5966 pos = INTVAL (pos_rtx), pos_rtx = 0;
5967
5968 /* See if this can be done without an extraction. We never can if the
5969 width of the field is not the same as that of some integer mode. For
5970 registers, we can only avoid the extraction if the position is at the
5971 low-order bit and this is either not in the destination or we have the
5972 appropriate STRICT_LOW_PART operation available.
5973
5974 For MEM, we can avoid an extract if the field starts on an appropriate
5975 boundary and we can change the mode of the memory reference. However,
5976 we cannot directly access the MEM if we have a USE and the underlying
5977 MEM is not TMODE. This combination means that MEM was being used in a
5978 context where bits outside its mode were being referenced; that is only
5979 valid in bit-field insns. */
5980
5981 if (tmode != BLKmode
5982 && ! (spans_byte && inner_mode != tmode)
5983 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5984 && GET_CODE (inner) != MEM
5985 && (! in_dest
5986 || (GET_CODE (inner) == REG
5987 && have_insn_for (STRICT_LOW_PART, tmode))))
5988 || (GET_CODE (inner) == MEM && pos_rtx == 0
5989 && (pos
5990 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5991 : BITS_PER_UNIT)) == 0
5992 /* We can't do this if we are widening INNER_MODE (it
5993 may not be aligned, for one thing). */
5994 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5995 && (inner_mode == tmode
5996 || (! mode_dependent_address_p (XEXP (inner, 0))
5997 && ! MEM_VOLATILE_P (inner))))))
5998 {
5999 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6000 field. If the original and current mode are the same, we need not
6001 adjust the offset. Otherwise, we do if bytes big endian.
6002
6003 If INNER is not a MEM, get a piece consisting of just the field
6004 of interest (in this case POS % BITS_PER_WORD must be 0). */
6005
6006 if (GET_CODE (inner) == MEM)
6007 {
6008 HOST_WIDE_INT offset;
6009
6010 /* POS counts from lsb, but make OFFSET count in memory order. */
6011 if (BYTES_BIG_ENDIAN)
6012 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6013 else
6014 offset = pos / BITS_PER_UNIT;
6015
6016 new = adjust_address_nv (inner, tmode, offset);
6017 }
6018 else if (GET_CODE (inner) == REG)
6019 {
6020 if (tmode != inner_mode)
6021 {
6022 /* We can't call gen_lowpart in a DEST since we
6023 always want a SUBREG (see below) and it would sometimes
6024 return a new hard register. */
6025 if (pos || in_dest)
6026 {
6027 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6028
6029 if (WORDS_BIG_ENDIAN
6030 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6031 final_word = ((GET_MODE_SIZE (inner_mode)
6032 - GET_MODE_SIZE (tmode))
6033 / UNITS_PER_WORD) - final_word;
6034
6035 final_word *= UNITS_PER_WORD;
6036 if (BYTES_BIG_ENDIAN &&
6037 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6038 final_word += (GET_MODE_SIZE (inner_mode)
6039 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6040
6041 /* Avoid creating invalid subregs, for example when
6042 simplifying (x>>32)&255. */
6043 if (final_word >= GET_MODE_SIZE (inner_mode))
6044 return NULL_RTX;
6045
6046 new = gen_rtx_SUBREG (tmode, inner, final_word);
6047 }
6048 else
6049 new = gen_lowpart (tmode, inner);
6050 }
6051 else
6052 new = inner;
6053 }
6054 else
6055 new = force_to_mode (inner, tmode,
6056 len >= HOST_BITS_PER_WIDE_INT
6057 ? ~(unsigned HOST_WIDE_INT) 0
6058 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6059 NULL_RTX, 0);
6060
6061 /* If this extraction is going into the destination of a SET,
6062 make a STRICT_LOW_PART unless we made a MEM. */
6063
6064 if (in_dest)
6065 return (GET_CODE (new) == MEM ? new
6066 : (GET_CODE (new) != SUBREG
6067 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6068 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6069
6070 if (mode == tmode)
6071 return new;
6072
6073 if (GET_CODE (new) == CONST_INT)
6074 return gen_int_mode (INTVAL (new), mode);
6075
6076 /* If we know that no extraneous bits are set, and that the high
6077 bit is not set, convert the extraction to the cheaper of
6078 sign and zero extension, that are equivalent in these cases. */
6079 if (flag_expensive_optimizations
6080 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6081 && ((nonzero_bits (new, tmode)
6082 & ~(((unsigned HOST_WIDE_INT)
6083 GET_MODE_MASK (tmode))
6084 >> 1))
6085 == 0)))
6086 {
6087 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6088 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6089
6090 /* Prefer ZERO_EXTENSION, since it gives more information to
6091 backends. */
6092 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6093 return temp;
6094 return temp1;
6095 }
6096
6097 /* Otherwise, sign- or zero-extend unless we already are in the
6098 proper mode. */
6099
6100 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6101 mode, new));
6102 }
6103
6104 /* Unless this is a COMPARE or we have a funny memory reference,
6105 don't do anything with zero-extending field extracts starting at
6106 the low-order bit since they are simple AND operations. */
6107 if (pos_rtx == 0 && pos == 0 && ! in_dest
6108 && ! in_compare && ! spans_byte && unsignedp)
6109 return 0;
6110
6111 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6112 we would be spanning bytes or if the position is not a constant and the
6113 length is not 1. In all other cases, we would only be going outside
6114 our object in cases when an original shift would have been
6115 undefined. */
6116 if (! spans_byte && GET_CODE (inner) == MEM
6117 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6118 || (pos_rtx != 0 && len != 1)))
6119 return 0;
6120
6121 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6122 and the mode for the result. */
6123 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6124 {
6125 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6126 pos_mode = mode_for_extraction (EP_insv, 2);
6127 extraction_mode = mode_for_extraction (EP_insv, 3);
6128 }
6129
6130 if (! in_dest && unsignedp
6131 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6132 {
6133 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6134 pos_mode = mode_for_extraction (EP_extzv, 3);
6135 extraction_mode = mode_for_extraction (EP_extzv, 0);
6136 }
6137
6138 if (! in_dest && ! unsignedp
6139 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6140 {
6141 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6142 pos_mode = mode_for_extraction (EP_extv, 3);
6143 extraction_mode = mode_for_extraction (EP_extv, 0);
6144 }
6145
6146 /* Never narrow an object, since that might not be safe. */
6147
6148 if (mode != VOIDmode
6149 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6150 extraction_mode = mode;
6151
6152 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6153 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6154 pos_mode = GET_MODE (pos_rtx);
6155
6156 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6157 if we have to change the mode of memory and cannot, the desired mode is
6158 EXTRACTION_MODE. */
6159 if (GET_CODE (inner) != MEM)
6160 wanted_inner_mode = wanted_inner_reg_mode;
6161 else if (inner_mode != wanted_inner_mode
6162 && (mode_dependent_address_p (XEXP (inner, 0))
6163 || MEM_VOLATILE_P (inner)))
6164 wanted_inner_mode = extraction_mode;
6165
6166 orig_pos = pos;
6167
6168 if (BITS_BIG_ENDIAN)
6169 {
6170 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6171 BITS_BIG_ENDIAN style. If position is constant, compute new
6172 position. Otherwise, build subtraction.
6173 Note that POS is relative to the mode of the original argument.
6174 If it's a MEM we need to recompute POS relative to that.
6175 However, if we're extracting from (or inserting into) a register,
6176 we want to recompute POS relative to wanted_inner_mode. */
6177 int width = (GET_CODE (inner) == MEM
6178 ? GET_MODE_BITSIZE (is_mode)
6179 : GET_MODE_BITSIZE (wanted_inner_mode));
6180
6181 if (pos_rtx == 0)
6182 pos = width - len - pos;
6183 else
6184 pos_rtx
6185 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6186 /* POS may be less than 0 now, but we check for that below.
6187 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
6188 }
6189
6190 /* If INNER has a wider mode, make it smaller. If this is a constant
6191 extract, try to adjust the byte to point to the byte containing
6192 the value. */
6193 if (wanted_inner_mode != VOIDmode
6194 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6195 && ((GET_CODE (inner) == MEM
6196 && (inner_mode == wanted_inner_mode
6197 || (! mode_dependent_address_p (XEXP (inner, 0))
6198 && ! MEM_VOLATILE_P (inner))))))
6199 {
6200 int offset = 0;
6201
6202 /* The computations below will be correct if the machine is big
6203 endian in both bits and bytes or little endian in bits and bytes.
6204 If it is mixed, we must adjust. */
6205
6206 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6207 adjust OFFSET to compensate. */
6208 if (BYTES_BIG_ENDIAN
6209 && ! spans_byte
6210 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6211 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6212
6213 /* If this is a constant position, we can move to the desired byte. */
6214 if (pos_rtx == 0)
6215 {
6216 offset += pos / BITS_PER_UNIT;
6217 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6218 }
6219
6220 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6221 && ! spans_byte
6222 && is_mode != wanted_inner_mode)
6223 offset = (GET_MODE_SIZE (is_mode)
6224 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6225
6226 if (offset != 0 || inner_mode != wanted_inner_mode)
6227 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6228 }
6229
6230 /* If INNER is not memory, we can always get it into the proper mode. If we
6231 are changing its mode, POS must be a constant and smaller than the size
6232 of the new mode. */
6233 else if (GET_CODE (inner) != MEM)
6234 {
6235 if (GET_MODE (inner) != wanted_inner_mode
6236 && (pos_rtx != 0
6237 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6238 return 0;
6239
6240 inner = force_to_mode (inner, wanted_inner_mode,
6241 pos_rtx
6242 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6243 ? ~(unsigned HOST_WIDE_INT) 0
6244 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6245 << orig_pos),
6246 NULL_RTX, 0);
6247 }
6248
6249 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6250 have to zero extend. Otherwise, we can just use a SUBREG. */
6251 if (pos_rtx != 0
6252 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6253 {
6254 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6255
6256 /* If we know that no extraneous bits are set, and that the high
6257 bit is not set, convert extraction to cheaper one - either
6258 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6259 cases. */
6260 if (flag_expensive_optimizations
6261 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6262 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6263 & ~(((unsigned HOST_WIDE_INT)
6264 GET_MODE_MASK (GET_MODE (pos_rtx)))
6265 >> 1))
6266 == 0)))
6267 {
6268 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6269
6270 /* Prefer ZERO_EXTENSION, since it gives more information to
6271 backends. */
6272 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6273 temp = temp1;
6274 }
6275 pos_rtx = temp;
6276 }
6277 else if (pos_rtx != 0
6278 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6279 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6280
6281 /* Make POS_RTX unless we already have it and it is correct. If we don't
6282 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6283 be a CONST_INT. */
6284 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6285 pos_rtx = orig_pos_rtx;
6286
6287 else if (pos_rtx == 0)
6288 pos_rtx = GEN_INT (pos);
6289
6290 /* Make the required operation. See if we can use existing rtx. */
6291 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6292 extraction_mode, inner, GEN_INT (len), pos_rtx);
6293 if (! in_dest)
6294 new = gen_lowpart (mode, new);
6295
6296 return new;
6297 }
6298 \f
6299 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6300 with any other operations in X. Return X without that shift if so. */
6301
6302 static rtx
6303 extract_left_shift (rtx x, int count)
6304 {
6305 enum rtx_code code = GET_CODE (x);
6306 enum machine_mode mode = GET_MODE (x);
6307 rtx tem;
6308
6309 switch (code)
6310 {
6311 case ASHIFT:
6312 /* This is the shift itself. If it is wide enough, we will return
6313 either the value being shifted if the shift count is equal to
6314 COUNT or a shift for the difference. */
6315 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6316 && INTVAL (XEXP (x, 1)) >= count)
6317 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6318 INTVAL (XEXP (x, 1)) - count);
6319 break;
6320
6321 case NEG: case NOT:
6322 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6323 return simplify_gen_unary (code, mode, tem, mode);
6324
6325 break;
6326
6327 case PLUS: case IOR: case XOR: case AND:
6328 /* If we can safely shift this constant and we find the inner shift,
6329 make a new operation. */
6330 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6331 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6332 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6333 return simplify_gen_binary (code, mode, tem,
6334 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6335
6336 break;
6337
6338 default:
6339 break;
6340 }
6341
6342 return 0;
6343 }
6344 \f
6345 /* Look at the expression rooted at X. Look for expressions
6346 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6347 Form these expressions.
6348
6349 Return the new rtx, usually just X.
6350
6351 Also, for machines like the VAX that don't have logical shift insns,
6352 try to convert logical to arithmetic shift operations in cases where
6353 they are equivalent. This undoes the canonicalizations to logical
6354 shifts done elsewhere.
6355
6356 We try, as much as possible, to re-use rtl expressions to save memory.
6357
6358 IN_CODE says what kind of expression we are processing. Normally, it is
6359 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6360 being kludges), it is MEM. When processing the arguments of a comparison
6361 or a COMPARE against zero, it is COMPARE. */
6362
6363 static rtx
6364 make_compound_operation (rtx x, enum rtx_code in_code)
6365 {
6366 enum rtx_code code = GET_CODE (x);
6367 enum machine_mode mode = GET_MODE (x);
6368 int mode_width = GET_MODE_BITSIZE (mode);
6369 rtx rhs, lhs;
6370 enum rtx_code next_code;
6371 int i;
6372 rtx new = 0;
6373 rtx tem;
6374 const char *fmt;
6375
6376 /* Select the code to be used in recursive calls. Once we are inside an
6377 address, we stay there. If we have a comparison, set to COMPARE,
6378 but once inside, go back to our default of SET. */
6379
6380 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6381 : ((code == COMPARE || COMPARISON_P (x))
6382 && XEXP (x, 1) == const0_rtx) ? COMPARE
6383 : in_code == COMPARE ? SET : in_code);
6384
6385 /* Process depending on the code of this operation. If NEW is set
6386 nonzero, it will be returned. */
6387
6388 switch (code)
6389 {
6390 case ASHIFT:
6391 /* Convert shifts by constants into multiplications if inside
6392 an address. */
6393 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6394 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6395 && INTVAL (XEXP (x, 1)) >= 0)
6396 {
6397 new = make_compound_operation (XEXP (x, 0), next_code);
6398 new = gen_rtx_MULT (mode, new,
6399 GEN_INT ((HOST_WIDE_INT) 1
6400 << INTVAL (XEXP (x, 1))));
6401 }
6402 break;
6403
6404 case AND:
6405 /* If the second operand is not a constant, we can't do anything
6406 with it. */
6407 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6408 break;
6409
6410 /* If the constant is a power of two minus one and the first operand
6411 is a logical right shift, make an extraction. */
6412 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6413 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6414 {
6415 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6416 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6417 0, in_code == COMPARE);
6418 }
6419
6420 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6421 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6422 && subreg_lowpart_p (XEXP (x, 0))
6423 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6424 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6425 {
6426 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6427 next_code);
6428 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6429 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6430 0, in_code == COMPARE);
6431 }
6432 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6433 else if ((GET_CODE (XEXP (x, 0)) == XOR
6434 || GET_CODE (XEXP (x, 0)) == IOR)
6435 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6436 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6437 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6438 {
6439 /* Apply the distributive law, and then try to make extractions. */
6440 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6441 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6442 XEXP (x, 1)),
6443 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6444 XEXP (x, 1)));
6445 new = make_compound_operation (new, in_code);
6446 }
6447
6448 /* If we are have (and (rotate X C) M) and C is larger than the number
6449 of bits in M, this is an extraction. */
6450
6451 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6452 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6453 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6454 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6455 {
6456 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6457 new = make_extraction (mode, new,
6458 (GET_MODE_BITSIZE (mode)
6459 - INTVAL (XEXP (XEXP (x, 0), 1))),
6460 NULL_RTX, i, 1, 0, in_code == COMPARE);
6461 }
6462
6463 /* On machines without logical shifts, if the operand of the AND is
6464 a logical shift and our mask turns off all the propagated sign
6465 bits, we can replace the logical shift with an arithmetic shift. */
6466 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6467 && !have_insn_for (LSHIFTRT, mode)
6468 && have_insn_for (ASHIFTRT, mode)
6469 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6470 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6471 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6472 && mode_width <= HOST_BITS_PER_WIDE_INT)
6473 {
6474 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6475
6476 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6477 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6478 SUBST (XEXP (x, 0),
6479 gen_rtx_ASHIFTRT (mode,
6480 make_compound_operation
6481 (XEXP (XEXP (x, 0), 0), next_code),
6482 XEXP (XEXP (x, 0), 1)));
6483 }
6484
6485 /* If the constant is one less than a power of two, this might be
6486 representable by an extraction even if no shift is present.
6487 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6488 we are in a COMPARE. */
6489 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6490 new = make_extraction (mode,
6491 make_compound_operation (XEXP (x, 0),
6492 next_code),
6493 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6494
6495 /* If we are in a comparison and this is an AND with a power of two,
6496 convert this into the appropriate bit extract. */
6497 else if (in_code == COMPARE
6498 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6499 new = make_extraction (mode,
6500 make_compound_operation (XEXP (x, 0),
6501 next_code),
6502 i, NULL_RTX, 1, 1, 0, 1);
6503
6504 break;
6505
6506 case LSHIFTRT:
6507 /* If the sign bit is known to be zero, replace this with an
6508 arithmetic shift. */
6509 if (have_insn_for (ASHIFTRT, mode)
6510 && ! have_insn_for (LSHIFTRT, mode)
6511 && mode_width <= HOST_BITS_PER_WIDE_INT
6512 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6513 {
6514 new = gen_rtx_ASHIFTRT (mode,
6515 make_compound_operation (XEXP (x, 0),
6516 next_code),
6517 XEXP (x, 1));
6518 break;
6519 }
6520
6521 /* ... fall through ... */
6522
6523 case ASHIFTRT:
6524 lhs = XEXP (x, 0);
6525 rhs = XEXP (x, 1);
6526
6527 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6528 this is a SIGN_EXTRACT. */
6529 if (GET_CODE (rhs) == CONST_INT
6530 && GET_CODE (lhs) == ASHIFT
6531 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6532 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6533 {
6534 new = make_compound_operation (XEXP (lhs, 0), next_code);
6535 new = make_extraction (mode, new,
6536 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6537 NULL_RTX, mode_width - INTVAL (rhs),
6538 code == LSHIFTRT, 0, in_code == COMPARE);
6539 break;
6540 }
6541
6542 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6543 If so, try to merge the shifts into a SIGN_EXTEND. We could
6544 also do this for some cases of SIGN_EXTRACT, but it doesn't
6545 seem worth the effort; the case checked for occurs on Alpha. */
6546
6547 if (!OBJECT_P (lhs)
6548 && ! (GET_CODE (lhs) == SUBREG
6549 && (OBJECT_P (SUBREG_REG (lhs))))
6550 && GET_CODE (rhs) == CONST_INT
6551 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6552 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6553 new = make_extraction (mode, make_compound_operation (new, next_code),
6554 0, NULL_RTX, mode_width - INTVAL (rhs),
6555 code == LSHIFTRT, 0, in_code == COMPARE);
6556
6557 break;
6558
6559 case SUBREG:
6560 /* Call ourselves recursively on the inner expression. If we are
6561 narrowing the object and it has a different RTL code from
6562 what it originally did, do this SUBREG as a force_to_mode. */
6563
6564 tem = make_compound_operation (SUBREG_REG (x), in_code);
6565 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6566 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6567 && subreg_lowpart_p (x))
6568 {
6569 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6570 NULL_RTX, 0);
6571
6572 /* If we have something other than a SUBREG, we might have
6573 done an expansion, so rerun ourselves. */
6574 if (GET_CODE (newer) != SUBREG)
6575 newer = make_compound_operation (newer, in_code);
6576
6577 return newer;
6578 }
6579
6580 /* If this is a paradoxical subreg, and the new code is a sign or
6581 zero extension, omit the subreg and widen the extension. If it
6582 is a regular subreg, we can still get rid of the subreg by not
6583 widening so much, or in fact removing the extension entirely. */
6584 if ((GET_CODE (tem) == SIGN_EXTEND
6585 || GET_CODE (tem) == ZERO_EXTEND)
6586 && subreg_lowpart_p (x))
6587 {
6588 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6589 || (GET_MODE_SIZE (mode) >
6590 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6591 {
6592 if (! SCALAR_INT_MODE_P (mode))
6593 break;
6594 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6595 }
6596 else
6597 tem = gen_lowpart (mode, XEXP (tem, 0));
6598 return tem;
6599 }
6600 break;
6601
6602 default:
6603 break;
6604 }
6605
6606 if (new)
6607 {
6608 x = gen_lowpart (mode, new);
6609 code = GET_CODE (x);
6610 }
6611
6612 /* Now recursively process each operand of this operation. */
6613 fmt = GET_RTX_FORMAT (code);
6614 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6615 if (fmt[i] == 'e')
6616 {
6617 new = make_compound_operation (XEXP (x, i), next_code);
6618 SUBST (XEXP (x, i), new);
6619 }
6620
6621 return x;
6622 }
6623 \f
6624 /* Given M see if it is a value that would select a field of bits
6625 within an item, but not the entire word. Return -1 if not.
6626 Otherwise, return the starting position of the field, where 0 is the
6627 low-order bit.
6628
6629 *PLEN is set to the length of the field. */
6630
6631 static int
6632 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6633 {
6634 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6635 int pos = exact_log2 (m & -m);
6636 int len = 0;
6637
6638 if (pos >= 0)
6639 /* Now shift off the low-order zero bits and see if we have a
6640 power of two minus 1. */
6641 len = exact_log2 ((m >> pos) + 1);
6642
6643 if (len <= 0)
6644 pos = -1;
6645
6646 *plen = len;
6647 return pos;
6648 }
6649 \f
6650 /* See if X can be simplified knowing that we will only refer to it in
6651 MODE and will only refer to those bits that are nonzero in MASK.
6652 If other bits are being computed or if masking operations are done
6653 that select a superset of the bits in MASK, they can sometimes be
6654 ignored.
6655
6656 Return a possibly simplified expression, but always convert X to
6657 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6658
6659 Also, if REG is nonzero and X is a register equal in value to REG,
6660 replace X with REG.
6661
6662 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6663 are all off in X. This is used when X will be complemented, by either
6664 NOT, NEG, or XOR. */
6665
6666 static rtx
6667 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6668 rtx reg, int just_select)
6669 {
6670 enum rtx_code code = GET_CODE (x);
6671 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6672 enum machine_mode op_mode;
6673 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6674 rtx op0, op1, temp;
6675
6676 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6677 code below will do the wrong thing since the mode of such an
6678 expression is VOIDmode.
6679
6680 Also do nothing if X is a CLOBBER; this can happen if X was
6681 the return value from a call to gen_lowpart. */
6682 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6683 return x;
6684
6685 /* We want to perform the operation is its present mode unless we know
6686 that the operation is valid in MODE, in which case we do the operation
6687 in MODE. */
6688 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6689 && have_insn_for (code, mode))
6690 ? mode : GET_MODE (x));
6691
6692 /* It is not valid to do a right-shift in a narrower mode
6693 than the one it came in with. */
6694 if ((code == LSHIFTRT || code == ASHIFTRT)
6695 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6696 op_mode = GET_MODE (x);
6697
6698 /* Truncate MASK to fit OP_MODE. */
6699 if (op_mode)
6700 mask &= GET_MODE_MASK (op_mode);
6701
6702 /* When we have an arithmetic operation, or a shift whose count we
6703 do not know, we need to assume that all bits up to the highest-order
6704 bit in MASK will be needed. This is how we form such a mask. */
6705 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6706 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6707 else
6708 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6709 - 1);
6710
6711 /* Determine what bits of X are guaranteed to be (non)zero. */
6712 nonzero = nonzero_bits (x, mode);
6713
6714 /* If none of the bits in X are needed, return a zero. */
6715 if (! just_select && (nonzero & mask) == 0)
6716 x = const0_rtx;
6717
6718 /* If X is a CONST_INT, return a new one. Do this here since the
6719 test below will fail. */
6720 if (GET_CODE (x) == CONST_INT)
6721 {
6722 if (SCALAR_INT_MODE_P (mode))
6723 return gen_int_mode (INTVAL (x) & mask, mode);
6724 else
6725 {
6726 x = GEN_INT (INTVAL (x) & mask);
6727 return gen_lowpart_common (mode, x);
6728 }
6729 }
6730
6731 /* If X is narrower than MODE and we want all the bits in X's mode, just
6732 get X in the proper mode. */
6733 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6734 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6735 return gen_lowpart (mode, x);
6736
6737 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6738 MASK are already known to be zero in X, we need not do anything. */
6739 if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6740 return x;
6741
6742 switch (code)
6743 {
6744 case CLOBBER:
6745 /* If X is a (clobber (const_int)), return it since we know we are
6746 generating something that won't match. */
6747 return x;
6748
6749 case USE:
6750 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6751 spanned the boundary of the MEM. If we are now masking so it is
6752 within that boundary, we don't need the USE any more. */
6753 if (! BITS_BIG_ENDIAN
6754 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6755 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6756 break;
6757
6758 case SIGN_EXTEND:
6759 case ZERO_EXTEND:
6760 case ZERO_EXTRACT:
6761 case SIGN_EXTRACT:
6762 x = expand_compound_operation (x);
6763 if (GET_CODE (x) != code)
6764 return force_to_mode (x, mode, mask, reg, next_select);
6765 break;
6766
6767 case REG:
6768 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6769 || rtx_equal_p (reg, get_last_value (x))))
6770 x = reg;
6771 break;
6772
6773 case SUBREG:
6774 if (subreg_lowpart_p (x)
6775 /* We can ignore the effect of this SUBREG if it narrows the mode or
6776 if the constant masks to zero all the bits the mode doesn't
6777 have. */
6778 && ((GET_MODE_SIZE (GET_MODE (x))
6779 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6780 || (0 == (mask
6781 & GET_MODE_MASK (GET_MODE (x))
6782 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6783 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6784 break;
6785
6786 case AND:
6787 /* If this is an AND with a constant, convert it into an AND
6788 whose constant is the AND of that constant with MASK. If it
6789 remains an AND of MASK, delete it since it is redundant. */
6790
6791 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6792 {
6793 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6794 mask & INTVAL (XEXP (x, 1)));
6795
6796 /* If X is still an AND, see if it is an AND with a mask that
6797 is just some low-order bits. If so, and it is MASK, we don't
6798 need it. */
6799
6800 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6801 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6802 == mask))
6803 x = XEXP (x, 0);
6804
6805 /* If it remains an AND, try making another AND with the bits
6806 in the mode mask that aren't in MASK turned on. If the
6807 constant in the AND is wide enough, this might make a
6808 cheaper constant. */
6809
6810 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6811 && GET_MODE_MASK (GET_MODE (x)) != mask
6812 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6813 {
6814 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6815 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6816 int width = GET_MODE_BITSIZE (GET_MODE (x));
6817 rtx y;
6818
6819 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6820 number, sign extend it. */
6821 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6822 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6823 cval |= (HOST_WIDE_INT) -1 << width;
6824
6825 y = simplify_gen_binary (AND, GET_MODE (x),
6826 XEXP (x, 0), GEN_INT (cval));
6827 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6828 x = y;
6829 }
6830
6831 break;
6832 }
6833
6834 goto binop;
6835
6836 case PLUS:
6837 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6838 low-order bits (as in an alignment operation) and FOO is already
6839 aligned to that boundary, mask C1 to that boundary as well.
6840 This may eliminate that PLUS and, later, the AND. */
6841
6842 {
6843 unsigned int width = GET_MODE_BITSIZE (mode);
6844 unsigned HOST_WIDE_INT smask = mask;
6845
6846 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6847 number, sign extend it. */
6848
6849 if (width < HOST_BITS_PER_WIDE_INT
6850 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6851 smask |= (HOST_WIDE_INT) -1 << width;
6852
6853 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6854 && exact_log2 (- smask) >= 0
6855 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6856 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6857 return force_to_mode (plus_constant (XEXP (x, 0),
6858 (INTVAL (XEXP (x, 1)) & smask)),
6859 mode, smask, reg, next_select);
6860 }
6861
6862 /* ... fall through ... */
6863
6864 case MULT:
6865 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6866 most significant bit in MASK since carries from those bits will
6867 affect the bits we are interested in. */
6868 mask = fuller_mask;
6869 goto binop;
6870
6871 case MINUS:
6872 /* If X is (minus C Y) where C's least set bit is larger than any bit
6873 in the mask, then we may replace with (neg Y). */
6874 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6875 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6876 & -INTVAL (XEXP (x, 0))))
6877 > mask))
6878 {
6879 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
6880 GET_MODE (x));
6881 return force_to_mode (x, mode, mask, reg, next_select);
6882 }
6883
6884 /* Similarly, if C contains every bit in the fuller_mask, then we may
6885 replace with (not Y). */
6886 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6887 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
6888 == INTVAL (XEXP (x, 0))))
6889 {
6890 x = simplify_gen_unary (NOT, GET_MODE (x),
6891 XEXP (x, 1), GET_MODE (x));
6892 return force_to_mode (x, mode, mask, reg, next_select);
6893 }
6894
6895 mask = fuller_mask;
6896 goto binop;
6897
6898 case IOR:
6899 case XOR:
6900 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6901 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6902 operation which may be a bitfield extraction. Ensure that the
6903 constant we form is not wider than the mode of X. */
6904
6905 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6906 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6907 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6908 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6909 && GET_CODE (XEXP (x, 1)) == CONST_INT
6910 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6911 + floor_log2 (INTVAL (XEXP (x, 1))))
6912 < GET_MODE_BITSIZE (GET_MODE (x)))
6913 && (INTVAL (XEXP (x, 1))
6914 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6915 {
6916 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6917 << INTVAL (XEXP (XEXP (x, 0), 1)));
6918 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
6919 XEXP (XEXP (x, 0), 0), temp);
6920 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
6921 XEXP (XEXP (x, 0), 1));
6922 return force_to_mode (x, mode, mask, reg, next_select);
6923 }
6924
6925 binop:
6926 /* For most binary operations, just propagate into the operation and
6927 change the mode if we have an operation of that mode. */
6928
6929 op0 = gen_lowpart (op_mode,
6930 force_to_mode (XEXP (x, 0), mode, mask,
6931 reg, next_select));
6932 op1 = gen_lowpart (op_mode,
6933 force_to_mode (XEXP (x, 1), mode, mask,
6934 reg, next_select));
6935
6936 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6937 x = simplify_gen_binary (code, op_mode, op0, op1);
6938 break;
6939
6940 case ASHIFT:
6941 /* For left shifts, do the same, but just for the first operand.
6942 However, we cannot do anything with shifts where we cannot
6943 guarantee that the counts are smaller than the size of the mode
6944 because such a count will have a different meaning in a
6945 wider mode. */
6946
6947 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6948 && INTVAL (XEXP (x, 1)) >= 0
6949 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6950 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6951 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6952 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6953 break;
6954
6955 /* If the shift count is a constant and we can do arithmetic in
6956 the mode of the shift, refine which bits we need. Otherwise, use the
6957 conservative form of the mask. */
6958 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6959 && INTVAL (XEXP (x, 1)) >= 0
6960 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6961 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6962 mask >>= INTVAL (XEXP (x, 1));
6963 else
6964 mask = fuller_mask;
6965
6966 op0 = gen_lowpart (op_mode,
6967 force_to_mode (XEXP (x, 0), op_mode,
6968 mask, reg, next_select));
6969
6970 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6971 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
6972 break;
6973
6974 case LSHIFTRT:
6975 /* Here we can only do something if the shift count is a constant,
6976 this shift constant is valid for the host, and we can do arithmetic
6977 in OP_MODE. */
6978
6979 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6980 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6981 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6982 {
6983 rtx inner = XEXP (x, 0);
6984 unsigned HOST_WIDE_INT inner_mask;
6985
6986 /* Select the mask of the bits we need for the shift operand. */
6987 inner_mask = mask << INTVAL (XEXP (x, 1));
6988
6989 /* We can only change the mode of the shift if we can do arithmetic
6990 in the mode of the shift and INNER_MASK is no wider than the
6991 width of OP_MODE. */
6992 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6993 || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
6994 op_mode = GET_MODE (x);
6995
6996 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
6997
6998 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6999 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7000 }
7001
7002 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7003 shift and AND produces only copies of the sign bit (C2 is one less
7004 than a power of two), we can do this with just a shift. */
7005
7006 if (GET_CODE (x) == LSHIFTRT
7007 && GET_CODE (XEXP (x, 1)) == CONST_INT
7008 /* The shift puts one of the sign bit copies in the least significant
7009 bit. */
7010 && ((INTVAL (XEXP (x, 1))
7011 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7012 >= GET_MODE_BITSIZE (GET_MODE (x)))
7013 && exact_log2 (mask + 1) >= 0
7014 /* Number of bits left after the shift must be more than the mask
7015 needs. */
7016 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7017 <= GET_MODE_BITSIZE (GET_MODE (x)))
7018 /* Must be more sign bit copies than the mask needs. */
7019 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7020 >= exact_log2 (mask + 1)))
7021 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7022 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7023 - exact_log2 (mask + 1)));
7024
7025 goto shiftrt;
7026
7027 case ASHIFTRT:
7028 /* If we are just looking for the sign bit, we don't need this shift at
7029 all, even if it has a variable count. */
7030 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7031 && (mask == ((unsigned HOST_WIDE_INT) 1
7032 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7033 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7034
7035 /* If this is a shift by a constant, get a mask that contains those bits
7036 that are not copies of the sign bit. We then have two cases: If
7037 MASK only includes those bits, this can be a logical shift, which may
7038 allow simplifications. If MASK is a single-bit field not within
7039 those bits, we are requesting a copy of the sign bit and hence can
7040 shift the sign bit to the appropriate location. */
7041
7042 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7043 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7044 {
7045 int i = -1;
7046
7047 /* If the considered data is wider than HOST_WIDE_INT, we can't
7048 represent a mask for all its bits in a single scalar.
7049 But we only care about the lower bits, so calculate these. */
7050
7051 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7052 {
7053 nonzero = ~(HOST_WIDE_INT) 0;
7054
7055 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7056 is the number of bits a full-width mask would have set.
7057 We need only shift if these are fewer than nonzero can
7058 hold. If not, we must keep all bits set in nonzero. */
7059
7060 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7061 < HOST_BITS_PER_WIDE_INT)
7062 nonzero >>= INTVAL (XEXP (x, 1))
7063 + HOST_BITS_PER_WIDE_INT
7064 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7065 }
7066 else
7067 {
7068 nonzero = GET_MODE_MASK (GET_MODE (x));
7069 nonzero >>= INTVAL (XEXP (x, 1));
7070 }
7071
7072 if ((mask & ~nonzero) == 0
7073 || (i = exact_log2 (mask)) >= 0)
7074 {
7075 x = simplify_shift_const
7076 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7077 i < 0 ? INTVAL (XEXP (x, 1))
7078 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7079
7080 if (GET_CODE (x) != ASHIFTRT)
7081 return force_to_mode (x, mode, mask, reg, next_select);
7082 }
7083 }
7084
7085 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7086 even if the shift count isn't a constant. */
7087 if (mask == 1)
7088 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7089 XEXP (x, 0), XEXP (x, 1));
7090
7091 shiftrt:
7092
7093 /* If this is a zero- or sign-extension operation that just affects bits
7094 we don't care about, remove it. Be sure the call above returned
7095 something that is still a shift. */
7096
7097 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7098 && GET_CODE (XEXP (x, 1)) == CONST_INT
7099 && INTVAL (XEXP (x, 1)) >= 0
7100 && (INTVAL (XEXP (x, 1))
7101 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7102 && GET_CODE (XEXP (x, 0)) == ASHIFT
7103 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7104 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7105 reg, next_select);
7106
7107 break;
7108
7109 case ROTATE:
7110 case ROTATERT:
7111 /* If the shift count is constant and we can do computations
7112 in the mode of X, compute where the bits we care about are.
7113 Otherwise, we can't do anything. Don't change the mode of
7114 the shift or propagate MODE into the shift, though. */
7115 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7116 && INTVAL (XEXP (x, 1)) >= 0)
7117 {
7118 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7119 GET_MODE (x), GEN_INT (mask),
7120 XEXP (x, 1));
7121 if (temp && GET_CODE (temp) == CONST_INT)
7122 SUBST (XEXP (x, 0),
7123 force_to_mode (XEXP (x, 0), GET_MODE (x),
7124 INTVAL (temp), reg, next_select));
7125 }
7126 break;
7127
7128 case NEG:
7129 /* If we just want the low-order bit, the NEG isn't needed since it
7130 won't change the low-order bit. */
7131 if (mask == 1)
7132 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7133
7134 /* We need any bits less significant than the most significant bit in
7135 MASK since carries from those bits will affect the bits we are
7136 interested in. */
7137 mask = fuller_mask;
7138 goto unop;
7139
7140 case NOT:
7141 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7142 same as the XOR case above. Ensure that the constant we form is not
7143 wider than the mode of X. */
7144
7145 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7146 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7147 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7148 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7149 < GET_MODE_BITSIZE (GET_MODE (x)))
7150 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7151 {
7152 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7153 GET_MODE (x));
7154 temp = simplify_gen_binary (XOR, GET_MODE (x),
7155 XEXP (XEXP (x, 0), 0), temp);
7156 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7157 temp, XEXP (XEXP (x, 0), 1));
7158
7159 return force_to_mode (x, mode, mask, reg, next_select);
7160 }
7161
7162 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7163 use the full mask inside the NOT. */
7164 mask = fuller_mask;
7165
7166 unop:
7167 op0 = gen_lowpart (op_mode,
7168 force_to_mode (XEXP (x, 0), mode, mask,
7169 reg, next_select));
7170 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7171 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7172 break;
7173
7174 case NE:
7175 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7176 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7177 which is equal to STORE_FLAG_VALUE. */
7178 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7179 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7180 && (nonzero_bits (XEXP (x, 0), mode)
7181 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7182 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7183
7184 break;
7185
7186 case IF_THEN_ELSE:
7187 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7188 written in a narrower mode. We play it safe and do not do so. */
7189
7190 SUBST (XEXP (x, 1),
7191 gen_lowpart (GET_MODE (x),
7192 force_to_mode (XEXP (x, 1), mode,
7193 mask, reg, next_select)));
7194 SUBST (XEXP (x, 2),
7195 gen_lowpart (GET_MODE (x),
7196 force_to_mode (XEXP (x, 2), mode,
7197 mask, reg, next_select)));
7198 break;
7199
7200 default:
7201 break;
7202 }
7203
7204 /* Ensure we return a value of the proper mode. */
7205 return gen_lowpart (mode, x);
7206 }
7207 \f
7208 /* Return nonzero if X is an expression that has one of two values depending on
7209 whether some other value is zero or nonzero. In that case, we return the
7210 value that is being tested, *PTRUE is set to the value if the rtx being
7211 returned has a nonzero value, and *PFALSE is set to the other alternative.
7212
7213 If we return zero, we set *PTRUE and *PFALSE to X. */
7214
7215 static rtx
7216 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7217 {
7218 enum machine_mode mode = GET_MODE (x);
7219 enum rtx_code code = GET_CODE (x);
7220 rtx cond0, cond1, true0, true1, false0, false1;
7221 unsigned HOST_WIDE_INT nz;
7222
7223 /* If we are comparing a value against zero, we are done. */
7224 if ((code == NE || code == EQ)
7225 && XEXP (x, 1) == const0_rtx)
7226 {
7227 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7228 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7229 return XEXP (x, 0);
7230 }
7231
7232 /* If this is a unary operation whose operand has one of two values, apply
7233 our opcode to compute those values. */
7234 else if (UNARY_P (x)
7235 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7236 {
7237 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7238 *pfalse = simplify_gen_unary (code, mode, false0,
7239 GET_MODE (XEXP (x, 0)));
7240 return cond0;
7241 }
7242
7243 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7244 make can't possibly match and would suppress other optimizations. */
7245 else if (code == COMPARE)
7246 ;
7247
7248 /* If this is a binary operation, see if either side has only one of two
7249 values. If either one does or if both do and they are conditional on
7250 the same value, compute the new true and false values. */
7251 else if (BINARY_P (x))
7252 {
7253 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7254 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7255
7256 if ((cond0 != 0 || cond1 != 0)
7257 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7258 {
7259 /* If if_then_else_cond returned zero, then true/false are the
7260 same rtl. We must copy one of them to prevent invalid rtl
7261 sharing. */
7262 if (cond0 == 0)
7263 true0 = copy_rtx (true0);
7264 else if (cond1 == 0)
7265 true1 = copy_rtx (true1);
7266
7267 if (COMPARISON_P (x))
7268 {
7269 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7270 true0, true1);
7271 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7272 false0, false1);
7273 }
7274 else
7275 {
7276 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7277 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7278 }
7279
7280 return cond0 ? cond0 : cond1;
7281 }
7282
7283 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7284 operands is zero when the other is nonzero, and vice-versa,
7285 and STORE_FLAG_VALUE is 1 or -1. */
7286
7287 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7288 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7289 || code == UMAX)
7290 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7291 {
7292 rtx op0 = XEXP (XEXP (x, 0), 1);
7293 rtx op1 = XEXP (XEXP (x, 1), 1);
7294
7295 cond0 = XEXP (XEXP (x, 0), 0);
7296 cond1 = XEXP (XEXP (x, 1), 0);
7297
7298 if (COMPARISON_P (cond0)
7299 && COMPARISON_P (cond1)
7300 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7301 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7302 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7303 || ((swap_condition (GET_CODE (cond0))
7304 == combine_reversed_comparison_code (cond1))
7305 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7306 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7307 && ! side_effects_p (x))
7308 {
7309 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7310 *pfalse = simplify_gen_binary (MULT, mode,
7311 (code == MINUS
7312 ? simplify_gen_unary (NEG, mode,
7313 op1, mode)
7314 : op1),
7315 const_true_rtx);
7316 return cond0;
7317 }
7318 }
7319
7320 /* Similarly for MULT, AND and UMIN, except that for these the result
7321 is always zero. */
7322 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7323 && (code == MULT || code == AND || code == UMIN)
7324 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7325 {
7326 cond0 = XEXP (XEXP (x, 0), 0);
7327 cond1 = XEXP (XEXP (x, 1), 0);
7328
7329 if (COMPARISON_P (cond0)
7330 && COMPARISON_P (cond1)
7331 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7332 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7333 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7334 || ((swap_condition (GET_CODE (cond0))
7335 == combine_reversed_comparison_code (cond1))
7336 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7337 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7338 && ! side_effects_p (x))
7339 {
7340 *ptrue = *pfalse = const0_rtx;
7341 return cond0;
7342 }
7343 }
7344 }
7345
7346 else if (code == IF_THEN_ELSE)
7347 {
7348 /* If we have IF_THEN_ELSE already, extract the condition and
7349 canonicalize it if it is NE or EQ. */
7350 cond0 = XEXP (x, 0);
7351 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7352 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7353 return XEXP (cond0, 0);
7354 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7355 {
7356 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7357 return XEXP (cond0, 0);
7358 }
7359 else
7360 return cond0;
7361 }
7362
7363 /* If X is a SUBREG, we can narrow both the true and false values
7364 if the inner expression, if there is a condition. */
7365 else if (code == SUBREG
7366 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7367 &true0, &false0)))
7368 {
7369 true0 = simplify_gen_subreg (mode, true0,
7370 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7371 false0 = simplify_gen_subreg (mode, false0,
7372 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7373 if (true0 && false0)
7374 {
7375 *ptrue = true0;
7376 *pfalse = false0;
7377 return cond0;
7378 }
7379 }
7380
7381 /* If X is a constant, this isn't special and will cause confusions
7382 if we treat it as such. Likewise if it is equivalent to a constant. */
7383 else if (CONSTANT_P (x)
7384 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7385 ;
7386
7387 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7388 will be least confusing to the rest of the compiler. */
7389 else if (mode == BImode)
7390 {
7391 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7392 return x;
7393 }
7394
7395 /* If X is known to be either 0 or -1, those are the true and
7396 false values when testing X. */
7397 else if (x == constm1_rtx || x == const0_rtx
7398 || (mode != VOIDmode
7399 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7400 {
7401 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7402 return x;
7403 }
7404
7405 /* Likewise for 0 or a single bit. */
7406 else if (SCALAR_INT_MODE_P (mode)
7407 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7408 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7409 {
7410 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7411 return x;
7412 }
7413
7414 /* Otherwise fail; show no condition with true and false values the same. */
7415 *ptrue = *pfalse = x;
7416 return 0;
7417 }
7418 \f
7419 /* Return the value of expression X given the fact that condition COND
7420 is known to be true when applied to REG as its first operand and VAL
7421 as its second. X is known to not be shared and so can be modified in
7422 place.
7423
7424 We only handle the simplest cases, and specifically those cases that
7425 arise with IF_THEN_ELSE expressions. */
7426
7427 static rtx
7428 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7429 {
7430 enum rtx_code code = GET_CODE (x);
7431 rtx temp;
7432 const char *fmt;
7433 int i, j;
7434
7435 if (side_effects_p (x))
7436 return x;
7437
7438 /* If either operand of the condition is a floating point value,
7439 then we have to avoid collapsing an EQ comparison. */
7440 if (cond == EQ
7441 && rtx_equal_p (x, reg)
7442 && ! FLOAT_MODE_P (GET_MODE (x))
7443 && ! FLOAT_MODE_P (GET_MODE (val)))
7444 return val;
7445
7446 if (cond == UNEQ && rtx_equal_p (x, reg))
7447 return val;
7448
7449 /* If X is (abs REG) and we know something about REG's relationship
7450 with zero, we may be able to simplify this. */
7451
7452 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7453 switch (cond)
7454 {
7455 case GE: case GT: case EQ:
7456 return XEXP (x, 0);
7457 case LT: case LE:
7458 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7459 XEXP (x, 0),
7460 GET_MODE (XEXP (x, 0)));
7461 default:
7462 break;
7463 }
7464
7465 /* The only other cases we handle are MIN, MAX, and comparisons if the
7466 operands are the same as REG and VAL. */
7467
7468 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7469 {
7470 if (rtx_equal_p (XEXP (x, 0), val))
7471 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7472
7473 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7474 {
7475 if (COMPARISON_P (x))
7476 {
7477 if (comparison_dominates_p (cond, code))
7478 return const_true_rtx;
7479
7480 code = combine_reversed_comparison_code (x);
7481 if (code != UNKNOWN
7482 && comparison_dominates_p (cond, code))
7483 return const0_rtx;
7484 else
7485 return x;
7486 }
7487 else if (code == SMAX || code == SMIN
7488 || code == UMIN || code == UMAX)
7489 {
7490 int unsignedp = (code == UMIN || code == UMAX);
7491
7492 /* Do not reverse the condition when it is NE or EQ.
7493 This is because we cannot conclude anything about
7494 the value of 'SMAX (x, y)' when x is not equal to y,
7495 but we can when x equals y. */
7496 if ((code == SMAX || code == UMAX)
7497 && ! (cond == EQ || cond == NE))
7498 cond = reverse_condition (cond);
7499
7500 switch (cond)
7501 {
7502 case GE: case GT:
7503 return unsignedp ? x : XEXP (x, 1);
7504 case LE: case LT:
7505 return unsignedp ? x : XEXP (x, 0);
7506 case GEU: case GTU:
7507 return unsignedp ? XEXP (x, 1) : x;
7508 case LEU: case LTU:
7509 return unsignedp ? XEXP (x, 0) : x;
7510 default:
7511 break;
7512 }
7513 }
7514 }
7515 }
7516 else if (code == SUBREG)
7517 {
7518 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7519 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7520
7521 if (SUBREG_REG (x) != r)
7522 {
7523 /* We must simplify subreg here, before we lose track of the
7524 original inner_mode. */
7525 new = simplify_subreg (GET_MODE (x), r,
7526 inner_mode, SUBREG_BYTE (x));
7527 if (new)
7528 return new;
7529 else
7530 SUBST (SUBREG_REG (x), r);
7531 }
7532
7533 return x;
7534 }
7535 /* We don't have to handle SIGN_EXTEND here, because even in the
7536 case of replacing something with a modeless CONST_INT, a
7537 CONST_INT is already (supposed to be) a valid sign extension for
7538 its narrower mode, which implies it's already properly
7539 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7540 story is different. */
7541 else if (code == ZERO_EXTEND)
7542 {
7543 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7544 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7545
7546 if (XEXP (x, 0) != r)
7547 {
7548 /* We must simplify the zero_extend here, before we lose
7549 track of the original inner_mode. */
7550 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7551 r, inner_mode);
7552 if (new)
7553 return new;
7554 else
7555 SUBST (XEXP (x, 0), r);
7556 }
7557
7558 return x;
7559 }
7560
7561 fmt = GET_RTX_FORMAT (code);
7562 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7563 {
7564 if (fmt[i] == 'e')
7565 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7566 else if (fmt[i] == 'E')
7567 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7568 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7569 cond, reg, val));
7570 }
7571
7572 return x;
7573 }
7574 \f
7575 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7576 assignment as a field assignment. */
7577
7578 static int
7579 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7580 {
7581 if (x == y || rtx_equal_p (x, y))
7582 return 1;
7583
7584 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7585 return 0;
7586
7587 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7588 Note that all SUBREGs of MEM are paradoxical; otherwise they
7589 would have been rewritten. */
7590 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7591 && GET_CODE (SUBREG_REG (y)) == MEM
7592 && rtx_equal_p (SUBREG_REG (y),
7593 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7594 return 1;
7595
7596 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7597 && GET_CODE (SUBREG_REG (x)) == MEM
7598 && rtx_equal_p (SUBREG_REG (x),
7599 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7600 return 1;
7601
7602 /* We used to see if get_last_value of X and Y were the same but that's
7603 not correct. In one direction, we'll cause the assignment to have
7604 the wrong destination and in the case, we'll import a register into this
7605 insn that might have already have been dead. So fail if none of the
7606 above cases are true. */
7607 return 0;
7608 }
7609 \f
7610 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7611 Return that assignment if so.
7612
7613 We only handle the most common cases. */
7614
7615 static rtx
7616 make_field_assignment (rtx x)
7617 {
7618 rtx dest = SET_DEST (x);
7619 rtx src = SET_SRC (x);
7620 rtx assign;
7621 rtx rhs, lhs;
7622 HOST_WIDE_INT c1;
7623 HOST_WIDE_INT pos;
7624 unsigned HOST_WIDE_INT len;
7625 rtx other;
7626 enum machine_mode mode;
7627
7628 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7629 a clear of a one-bit field. We will have changed it to
7630 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7631 for a SUBREG. */
7632
7633 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7634 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7635 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7636 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7637 {
7638 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7639 1, 1, 1, 0);
7640 if (assign != 0)
7641 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7642 return x;
7643 }
7644
7645 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7646 && subreg_lowpart_p (XEXP (src, 0))
7647 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7648 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7649 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7650 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7651 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7652 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7653 {
7654 assign = make_extraction (VOIDmode, dest, 0,
7655 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7656 1, 1, 1, 0);
7657 if (assign != 0)
7658 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7659 return x;
7660 }
7661
7662 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7663 one-bit field. */
7664 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7665 && XEXP (XEXP (src, 0), 0) == const1_rtx
7666 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7667 {
7668 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7669 1, 1, 1, 0);
7670 if (assign != 0)
7671 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7672 return x;
7673 }
7674
7675 /* The other case we handle is assignments into a constant-position
7676 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7677 a mask that has all one bits except for a group of zero bits and
7678 OTHER is known to have zeros where C1 has ones, this is such an
7679 assignment. Compute the position and length from C1. Shift OTHER
7680 to the appropriate position, force it to the required mode, and
7681 make the extraction. Check for the AND in both operands. */
7682
7683 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7684 return x;
7685
7686 rhs = expand_compound_operation (XEXP (src, 0));
7687 lhs = expand_compound_operation (XEXP (src, 1));
7688
7689 if (GET_CODE (rhs) == AND
7690 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7691 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7692 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7693 else if (GET_CODE (lhs) == AND
7694 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7695 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7696 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7697 else
7698 return x;
7699
7700 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7701 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7702 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7703 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7704 return x;
7705
7706 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7707 if (assign == 0)
7708 return x;
7709
7710 /* The mode to use for the source is the mode of the assignment, or of
7711 what is inside a possible STRICT_LOW_PART. */
7712 mode = (GET_CODE (assign) == STRICT_LOW_PART
7713 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7714
7715 /* Shift OTHER right POS places and make it the source, restricting it
7716 to the proper length and mode. */
7717
7718 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7719 GET_MODE (src), other, pos),
7720 mode,
7721 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7722 ? ~(unsigned HOST_WIDE_INT) 0
7723 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7724 dest, 0);
7725
7726 /* If SRC is masked by an AND that does not make a difference in
7727 the value being stored, strip it. */
7728 if (GET_CODE (assign) == ZERO_EXTRACT
7729 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7730 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7731 && GET_CODE (src) == AND
7732 && GET_CODE (XEXP (src, 1)) == CONST_INT
7733 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7734 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7735 src = XEXP (src, 0);
7736
7737 return gen_rtx_SET (VOIDmode, assign, src);
7738 }
7739 \f
7740 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7741 if so. */
7742
7743 static rtx
7744 apply_distributive_law (rtx x)
7745 {
7746 enum rtx_code code = GET_CODE (x);
7747 enum rtx_code inner_code;
7748 rtx lhs, rhs, other;
7749 rtx tem;
7750
7751 /* Distributivity is not true for floating point as it can change the
7752 value. So we don't do it unless -funsafe-math-optimizations. */
7753 if (FLOAT_MODE_P (GET_MODE (x))
7754 && ! flag_unsafe_math_optimizations)
7755 return x;
7756
7757 /* The outer operation can only be one of the following: */
7758 if (code != IOR && code != AND && code != XOR
7759 && code != PLUS && code != MINUS)
7760 return x;
7761
7762 lhs = XEXP (x, 0);
7763 rhs = XEXP (x, 1);
7764
7765 /* If either operand is a primitive we can't do anything, so get out
7766 fast. */
7767 if (OBJECT_P (lhs) || OBJECT_P (rhs))
7768 return x;
7769
7770 lhs = expand_compound_operation (lhs);
7771 rhs = expand_compound_operation (rhs);
7772 inner_code = GET_CODE (lhs);
7773 if (inner_code != GET_CODE (rhs))
7774 return x;
7775
7776 /* See if the inner and outer operations distribute. */
7777 switch (inner_code)
7778 {
7779 case LSHIFTRT:
7780 case ASHIFTRT:
7781 case AND:
7782 case IOR:
7783 /* These all distribute except over PLUS. */
7784 if (code == PLUS || code == MINUS)
7785 return x;
7786 break;
7787
7788 case MULT:
7789 if (code != PLUS && code != MINUS)
7790 return x;
7791 break;
7792
7793 case ASHIFT:
7794 /* This is also a multiply, so it distributes over everything. */
7795 break;
7796
7797 case SUBREG:
7798 /* Non-paradoxical SUBREGs distributes over all operations, provided
7799 the inner modes and byte offsets are the same, this is an extraction
7800 of a low-order part, we don't convert an fp operation to int or
7801 vice versa, and we would not be converting a single-word
7802 operation into a multi-word operation. The latter test is not
7803 required, but it prevents generating unneeded multi-word operations.
7804 Some of the previous tests are redundant given the latter test, but
7805 are retained because they are required for correctness.
7806
7807 We produce the result slightly differently in this case. */
7808
7809 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7810 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7811 || ! subreg_lowpart_p (lhs)
7812 || (GET_MODE_CLASS (GET_MODE (lhs))
7813 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7814 || (GET_MODE_SIZE (GET_MODE (lhs))
7815 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7816 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7817 return x;
7818
7819 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7820 SUBREG_REG (lhs), SUBREG_REG (rhs));
7821 return gen_lowpart (GET_MODE (x), tem);
7822
7823 default:
7824 return x;
7825 }
7826
7827 /* Set LHS and RHS to the inner operands (A and B in the example
7828 above) and set OTHER to the common operand (C in the example).
7829 There is only one way to do this unless the inner operation is
7830 commutative. */
7831 if (COMMUTATIVE_ARITH_P (lhs)
7832 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7833 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7834 else if (COMMUTATIVE_ARITH_P (lhs)
7835 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7836 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7837 else if (COMMUTATIVE_ARITH_P (lhs)
7838 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7839 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7840 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7841 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7842 else
7843 return x;
7844
7845 /* Form the new inner operation, seeing if it simplifies first. */
7846 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
7847
7848 /* There is one exception to the general way of distributing:
7849 (a | c) ^ (b | c) -> (a ^ b) & ~c */
7850 if (code == XOR && inner_code == IOR)
7851 {
7852 inner_code = AND;
7853 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7854 }
7855
7856 /* We may be able to continuing distributing the result, so call
7857 ourselves recursively on the inner operation before forming the
7858 outer operation, which we return. */
7859 return simplify_gen_binary (inner_code, GET_MODE (x),
7860 apply_distributive_law (tem), other);
7861 }
7862
7863 /* See if X is of the form (* (+ a b) c), and if so convert to
7864 (+ (* a c) (* b c)) and try to simplify.
7865
7866 Most of the time, this results in no change. However, if some of
7867 the operands are the same or inverses of each other, simplifications
7868 will result.
7869
7870 For example, (and (ior A B) (not B)) can occur as the result of
7871 expanding a bit field assignment. When we apply the distributive
7872 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
7873 which then simplifies to (and (A (not B))).
7874
7875 Note that no checks happen on the validity of applying the inverse
7876 distributive law. This is pointless since we can do it in the
7877 few places where this routine is called. */
7878 static rtx
7879 distribute_and_simplify_rtx (rtx x)
7880 {
7881 enum machine_mode mode;
7882 enum rtx_code outer, inner;
7883 rtx op0, op1, inner_op0, inner_op1, new_op0, new_op1;
7884
7885 mode = GET_MODE (x);
7886 outer = GET_CODE (x);
7887 op0 = XEXP (x, 0);
7888 op1 = XEXP (x, 1);
7889 if (ARITHMETIC_P (op0))
7890 {
7891 inner = GET_CODE (op0);
7892 inner_op0 = XEXP (op0, 0);
7893 inner_op1 = XEXP (op0, 1);
7894
7895 /* (and (xor B C) (not A)) == (xor (ior A B) (ior A C)) */
7896 if (outer == AND && inner == XOR && GET_CODE (op1) == NOT)
7897 {
7898 new_op0 = simplify_gen_binary (IOR, mode, inner_op0, op1);
7899 new_op1 = simplify_gen_binary (IOR, mode, inner_op1, op1);
7900 x = apply_distributive_law (simplify_gen_binary (XOR, mode,
7901 new_op0, new_op1));
7902
7903 if (GET_CODE (x) != AND)
7904 return x;
7905 }
7906 else
7907 {
7908 new_op0 = simplify_gen_binary (outer, mode, inner_op0, op1);
7909 new_op1 = simplify_gen_binary (outer, mode, inner_op1, op1);
7910 x = apply_distributive_law (simplify_gen_binary (inner, mode,
7911 new_op0, new_op1));
7912
7913 if (GET_CODE (x) != outer)
7914 return x;
7915 }
7916 }
7917
7918 if (ARITHMETIC_P (op1))
7919 {
7920 inner = GET_CODE (op1);
7921 inner_op0 = XEXP (op1, 0);
7922 inner_op1 = XEXP (op1, 1);
7923
7924 /* (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
7925 if (outer == AND && inner == XOR && GET_CODE (op0) == NOT)
7926 {
7927 new_op0 = simplify_gen_binary (IOR, mode, inner_op0, op0);
7928 new_op1 = simplify_gen_binary (IOR, mode, inner_op1, op0);
7929 x = apply_distributive_law (simplify_gen_binary (XOR, mode,
7930 new_op0, new_op1));
7931
7932 if (GET_CODE (x) != AND)
7933 return x;
7934 }
7935 else
7936 {
7937 new_op0 = simplify_gen_binary (outer, mode, op0, inner_op0);
7938 new_op1 = simplify_gen_binary (outer, mode, op0, inner_op1);
7939 x = apply_distributive_law (simplify_gen_binary (inner, mode,
7940 new_op0, new_op1));
7941
7942 if (GET_CODE (x) != outer)
7943 return x;
7944 }
7945 }
7946
7947 return NULL_RTX;
7948 }
7949 \f
7950 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7951 in MODE.
7952
7953 Return an equivalent form, if different from X. Otherwise, return X. If
7954 X is zero, we are to always construct the equivalent form. */
7955
7956 static rtx
7957 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7958 unsigned HOST_WIDE_INT constop)
7959 {
7960 unsigned HOST_WIDE_INT nonzero;
7961 int i;
7962
7963 /* Simplify VAROP knowing that we will be only looking at some of the
7964 bits in it.
7965
7966 Note by passing in CONSTOP, we guarantee that the bits not set in
7967 CONSTOP are not significant and will never be examined. We must
7968 ensure that is the case by explicitly masking out those bits
7969 before returning. */
7970 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7971
7972 /* If VAROP is a CLOBBER, we will fail so return it. */
7973 if (GET_CODE (varop) == CLOBBER)
7974 return varop;
7975
7976 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
7977 to VAROP and return the new constant. */
7978 if (GET_CODE (varop) == CONST_INT)
7979 return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
7980
7981 /* See what bits may be nonzero in VAROP. Unlike the general case of
7982 a call to nonzero_bits, here we don't care about bits outside
7983 MODE. */
7984
7985 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7986
7987 /* Turn off all bits in the constant that are known to already be zero.
7988 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7989 which is tested below. */
7990
7991 constop &= nonzero;
7992
7993 /* If we don't have any bits left, return zero. */
7994 if (constop == 0)
7995 return const0_rtx;
7996
7997 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7998 a power of two, we can replace this with an ASHIFT. */
7999 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8000 && (i = exact_log2 (constop)) >= 0)
8001 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8002
8003 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8004 or XOR, then try to apply the distributive law. This may eliminate
8005 operations if either branch can be simplified because of the AND.
8006 It may also make some cases more complex, but those cases probably
8007 won't match a pattern either with or without this. */
8008
8009 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8010 return
8011 gen_lowpart
8012 (mode,
8013 apply_distributive_law
8014 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8015 simplify_and_const_int (NULL_RTX,
8016 GET_MODE (varop),
8017 XEXP (varop, 0),
8018 constop),
8019 simplify_and_const_int (NULL_RTX,
8020 GET_MODE (varop),
8021 XEXP (varop, 1),
8022 constop))));
8023
8024 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8025 the AND and see if one of the operands simplifies to zero. If so, we
8026 may eliminate it. */
8027
8028 if (GET_CODE (varop) == PLUS
8029 && exact_log2 (constop + 1) >= 0)
8030 {
8031 rtx o0, o1;
8032
8033 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8034 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8035 if (o0 == const0_rtx)
8036 return o1;
8037 if (o1 == const0_rtx)
8038 return o0;
8039 }
8040
8041 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
8042 if we already had one (just check for the simplest cases). */
8043 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8044 && GET_MODE (XEXP (x, 0)) == mode
8045 && SUBREG_REG (XEXP (x, 0)) == varop)
8046 varop = XEXP (x, 0);
8047 else
8048 varop = gen_lowpart (mode, varop);
8049
8050 /* If we can't make the SUBREG, try to return what we were given. */
8051 if (GET_CODE (varop) == CLOBBER)
8052 return x ? x : varop;
8053
8054 /* If we are only masking insignificant bits, return VAROP. */
8055 if (constop == nonzero)
8056 x = varop;
8057 else
8058 {
8059 /* Otherwise, return an AND. */
8060 constop = trunc_int_for_mode (constop, mode);
8061 /* See how much, if any, of X we can use. */
8062 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8063 x = simplify_gen_binary (AND, mode, varop, GEN_INT (constop));
8064
8065 else
8066 {
8067 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8068 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8069 SUBST (XEXP (x, 1), GEN_INT (constop));
8070
8071 SUBST (XEXP (x, 0), varop);
8072 }
8073 }
8074
8075 return x;
8076 }
8077 \f
8078 /* Given a REG, X, compute which bits in X can be nonzero.
8079 We don't care about bits outside of those defined in MODE.
8080
8081 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8082 a shift, AND, or zero_extract, we can do better. */
8083
8084 static rtx
8085 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8086 rtx known_x ATTRIBUTE_UNUSED,
8087 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8088 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8089 unsigned HOST_WIDE_INT *nonzero)
8090 {
8091 rtx tem;
8092
8093 /* If X is a register whose nonzero bits value is current, use it.
8094 Otherwise, if X is a register whose value we can find, use that
8095 value. Otherwise, use the previously-computed global nonzero bits
8096 for this register. */
8097
8098 if (reg_stat[REGNO (x)].last_set_value != 0
8099 && (reg_stat[REGNO (x)].last_set_mode == mode
8100 || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8101 && GET_MODE_CLASS (mode) == MODE_INT))
8102 && (reg_stat[REGNO (x)].last_set_label == label_tick
8103 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8104 && REG_N_SETS (REGNO (x)) == 1
8105 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8106 REGNO (x))))
8107 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8108 {
8109 *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8110 return NULL;
8111 }
8112
8113 tem = get_last_value (x);
8114
8115 if (tem)
8116 {
8117 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8118 /* If X is narrower than MODE and TEM is a non-negative
8119 constant that would appear negative in the mode of X,
8120 sign-extend it for use in reg_nonzero_bits because some
8121 machines (maybe most) will actually do the sign-extension
8122 and this is the conservative approach.
8123
8124 ??? For 2.5, try to tighten up the MD files in this regard
8125 instead of this kludge. */
8126
8127 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8128 && GET_CODE (tem) == CONST_INT
8129 && INTVAL (tem) > 0
8130 && 0 != (INTVAL (tem)
8131 & ((HOST_WIDE_INT) 1
8132 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8133 tem = GEN_INT (INTVAL (tem)
8134 | ((HOST_WIDE_INT) (-1)
8135 << GET_MODE_BITSIZE (GET_MODE (x))));
8136 #endif
8137 return tem;
8138 }
8139 else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8140 {
8141 unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8142
8143 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8144 /* We don't know anything about the upper bits. */
8145 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8146 *nonzero &= mask;
8147 }
8148
8149 return NULL;
8150 }
8151
8152 /* Return the number of bits at the high-order end of X that are known to
8153 be equal to the sign bit. X will be used in mode MODE; if MODE is
8154 VOIDmode, X will be used in its own mode. The returned value will always
8155 be between 1 and the number of bits in MODE. */
8156
8157 static rtx
8158 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8159 rtx known_x ATTRIBUTE_UNUSED,
8160 enum machine_mode known_mode
8161 ATTRIBUTE_UNUSED,
8162 unsigned int known_ret ATTRIBUTE_UNUSED,
8163 unsigned int *result)
8164 {
8165 rtx tem;
8166
8167 if (reg_stat[REGNO (x)].last_set_value != 0
8168 && reg_stat[REGNO (x)].last_set_mode == mode
8169 && (reg_stat[REGNO (x)].last_set_label == label_tick
8170 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8171 && REG_N_SETS (REGNO (x)) == 1
8172 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8173 REGNO (x))))
8174 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8175 {
8176 *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8177 return NULL;
8178 }
8179
8180 tem = get_last_value (x);
8181 if (tem != 0)
8182 return tem;
8183
8184 if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8185 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8186 *result = reg_stat[REGNO (x)].sign_bit_copies;
8187
8188 return NULL;
8189 }
8190 \f
8191 /* Return the number of "extended" bits there are in X, when interpreted
8192 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8193 unsigned quantities, this is the number of high-order zero bits.
8194 For signed quantities, this is the number of copies of the sign bit
8195 minus 1. In both case, this function returns the number of "spare"
8196 bits. For example, if two quantities for which this function returns
8197 at least 1 are added, the addition is known not to overflow.
8198
8199 This function will always return 0 unless called during combine, which
8200 implies that it must be called from a define_split. */
8201
8202 unsigned int
8203 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8204 {
8205 if (nonzero_sign_valid == 0)
8206 return 0;
8207
8208 return (unsignedp
8209 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8210 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8211 - floor_log2 (nonzero_bits (x, mode)))
8212 : 0)
8213 : num_sign_bit_copies (x, mode) - 1);
8214 }
8215 \f
8216 /* This function is called from `simplify_shift_const' to merge two
8217 outer operations. Specifically, we have already found that we need
8218 to perform operation *POP0 with constant *PCONST0 at the outermost
8219 position. We would now like to also perform OP1 with constant CONST1
8220 (with *POP0 being done last).
8221
8222 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8223 the resulting operation. *PCOMP_P is set to 1 if we would need to
8224 complement the innermost operand, otherwise it is unchanged.
8225
8226 MODE is the mode in which the operation will be done. No bits outside
8227 the width of this mode matter. It is assumed that the width of this mode
8228 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8229
8230 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8231 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8232 result is simply *PCONST0.
8233
8234 If the resulting operation cannot be expressed as one operation, we
8235 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8236
8237 static int
8238 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)
8239 {
8240 enum rtx_code op0 = *pop0;
8241 HOST_WIDE_INT const0 = *pconst0;
8242
8243 const0 &= GET_MODE_MASK (mode);
8244 const1 &= GET_MODE_MASK (mode);
8245
8246 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8247 if (op0 == AND)
8248 const1 &= const0;
8249
8250 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8251 if OP0 is SET. */
8252
8253 if (op1 == NIL || op0 == SET)
8254 return 1;
8255
8256 else if (op0 == NIL)
8257 op0 = op1, const0 = const1;
8258
8259 else if (op0 == op1)
8260 {
8261 switch (op0)
8262 {
8263 case AND:
8264 const0 &= const1;
8265 break;
8266 case IOR:
8267 const0 |= const1;
8268 break;
8269 case XOR:
8270 const0 ^= const1;
8271 break;
8272 case PLUS:
8273 const0 += const1;
8274 break;
8275 case NEG:
8276 op0 = NIL;
8277 break;
8278 default:
8279 break;
8280 }
8281 }
8282
8283 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8284 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8285 return 0;
8286
8287 /* If the two constants aren't the same, we can't do anything. The
8288 remaining six cases can all be done. */
8289 else if (const0 != const1)
8290 return 0;
8291
8292 else
8293 switch (op0)
8294 {
8295 case IOR:
8296 if (op1 == AND)
8297 /* (a & b) | b == b */
8298 op0 = SET;
8299 else /* op1 == XOR */
8300 /* (a ^ b) | b == a | b */
8301 {;}
8302 break;
8303
8304 case XOR:
8305 if (op1 == AND)
8306 /* (a & b) ^ b == (~a) & b */
8307 op0 = AND, *pcomp_p = 1;
8308 else /* op1 == IOR */
8309 /* (a | b) ^ b == a & ~b */
8310 op0 = AND, const0 = ~const0;
8311 break;
8312
8313 case AND:
8314 if (op1 == IOR)
8315 /* (a | b) & b == b */
8316 op0 = SET;
8317 else /* op1 == XOR */
8318 /* (a ^ b) & b) == (~a) & b */
8319 *pcomp_p = 1;
8320 break;
8321 default:
8322 break;
8323 }
8324
8325 /* Check for NO-OP cases. */
8326 const0 &= GET_MODE_MASK (mode);
8327 if (const0 == 0
8328 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8329 op0 = NIL;
8330 else if (const0 == 0 && op0 == AND)
8331 op0 = SET;
8332 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8333 && op0 == AND)
8334 op0 = NIL;
8335
8336 /* ??? Slightly redundant with the above mask, but not entirely.
8337 Moving this above means we'd have to sign-extend the mode mask
8338 for the final test. */
8339 const0 = trunc_int_for_mode (const0, mode);
8340
8341 *pop0 = op0;
8342 *pconst0 = const0;
8343
8344 return 1;
8345 }
8346 \f
8347 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8348 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
8349 that we started with.
8350
8351 The shift is normally computed in the widest mode we find in VAROP, as
8352 long as it isn't a different number of words than RESULT_MODE. Exceptions
8353 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8354
8355 static rtx
8356 simplify_shift_const (rtx x, enum rtx_code code,
8357 enum machine_mode result_mode, rtx varop,
8358 int orig_count)
8359 {
8360 enum rtx_code orig_code = code;
8361 unsigned int count;
8362 int signed_count;
8363 enum machine_mode mode = result_mode;
8364 enum machine_mode shift_mode, tmode;
8365 unsigned int mode_words
8366 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8367 /* We form (outer_op (code varop count) (outer_const)). */
8368 enum rtx_code outer_op = NIL;
8369 HOST_WIDE_INT outer_const = 0;
8370 rtx const_rtx;
8371 int complement_p = 0;
8372 rtx new;
8373
8374 /* Make sure and truncate the "natural" shift on the way in. We don't
8375 want to do this inside the loop as it makes it more difficult to
8376 combine shifts. */
8377 if (SHIFT_COUNT_TRUNCATED)
8378 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8379
8380 /* If we were given an invalid count, don't do anything except exactly
8381 what was requested. */
8382
8383 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8384 {
8385 if (x)
8386 return x;
8387
8388 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8389 }
8390
8391 count = orig_count;
8392
8393 /* Unless one of the branches of the `if' in this loop does a `continue',
8394 we will `break' the loop after the `if'. */
8395
8396 while (count != 0)
8397 {
8398 /* If we have an operand of (clobber (const_int 0)), just return that
8399 value. */
8400 if (GET_CODE (varop) == CLOBBER)
8401 return varop;
8402
8403 /* If we discovered we had to complement VAROP, leave. Making a NOT
8404 here would cause an infinite loop. */
8405 if (complement_p)
8406 break;
8407
8408 /* Convert ROTATERT to ROTATE. */
8409 if (code == ROTATERT)
8410 {
8411 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8412 code = ROTATE;
8413 if (VECTOR_MODE_P (result_mode))
8414 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8415 else
8416 count = bitsize - count;
8417 }
8418
8419 /* We need to determine what mode we will do the shift in. If the
8420 shift is a right shift or a ROTATE, we must always do it in the mode
8421 it was originally done in. Otherwise, we can do it in MODE, the
8422 widest mode encountered. */
8423 shift_mode
8424 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8425 ? result_mode : mode);
8426
8427 /* Handle cases where the count is greater than the size of the mode
8428 minus 1. For ASHIFT, use the size minus one as the count (this can
8429 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8430 take the count modulo the size. For other shifts, the result is
8431 zero.
8432
8433 Since these shifts are being produced by the compiler by combining
8434 multiple operations, each of which are defined, we know what the
8435 result is supposed to be. */
8436
8437 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8438 {
8439 if (code == ASHIFTRT)
8440 count = GET_MODE_BITSIZE (shift_mode) - 1;
8441 else if (code == ROTATE || code == ROTATERT)
8442 count %= GET_MODE_BITSIZE (shift_mode);
8443 else
8444 {
8445 /* We can't simply return zero because there may be an
8446 outer op. */
8447 varop = const0_rtx;
8448 count = 0;
8449 break;
8450 }
8451 }
8452
8453 /* An arithmetic right shift of a quantity known to be -1 or 0
8454 is a no-op. */
8455 if (code == ASHIFTRT
8456 && (num_sign_bit_copies (varop, shift_mode)
8457 == GET_MODE_BITSIZE (shift_mode)))
8458 {
8459 count = 0;
8460 break;
8461 }
8462
8463 /* If we are doing an arithmetic right shift and discarding all but
8464 the sign bit copies, this is equivalent to doing a shift by the
8465 bitsize minus one. Convert it into that shift because it will often
8466 allow other simplifications. */
8467
8468 if (code == ASHIFTRT
8469 && (count + num_sign_bit_copies (varop, shift_mode)
8470 >= GET_MODE_BITSIZE (shift_mode)))
8471 count = GET_MODE_BITSIZE (shift_mode) - 1;
8472
8473 /* We simplify the tests below and elsewhere by converting
8474 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8475 `make_compound_operation' will convert it to an ASHIFTRT for
8476 those machines (such as VAX) that don't have an LSHIFTRT. */
8477 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8478 && code == ASHIFTRT
8479 && ((nonzero_bits (varop, shift_mode)
8480 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8481 == 0))
8482 code = LSHIFTRT;
8483
8484 if (code == LSHIFTRT
8485 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8486 && !(nonzero_bits (varop, shift_mode) >> count))
8487 varop = const0_rtx;
8488 if (code == ASHIFT
8489 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8490 && !((nonzero_bits (varop, shift_mode) << count)
8491 & GET_MODE_MASK (shift_mode)))
8492 varop = const0_rtx;
8493
8494 switch (GET_CODE (varop))
8495 {
8496 case SIGN_EXTEND:
8497 case ZERO_EXTEND:
8498 case SIGN_EXTRACT:
8499 case ZERO_EXTRACT:
8500 new = expand_compound_operation (varop);
8501 if (new != varop)
8502 {
8503 varop = new;
8504 continue;
8505 }
8506 break;
8507
8508 case MEM:
8509 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8510 minus the width of a smaller mode, we can do this with a
8511 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8512 if ((code == ASHIFTRT || code == LSHIFTRT)
8513 && ! mode_dependent_address_p (XEXP (varop, 0))
8514 && ! MEM_VOLATILE_P (varop)
8515 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8516 MODE_INT, 1)) != BLKmode)
8517 {
8518 new = adjust_address_nv (varop, tmode,
8519 BYTES_BIG_ENDIAN ? 0
8520 : count / BITS_PER_UNIT);
8521
8522 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8523 : ZERO_EXTEND, mode, new);
8524 count = 0;
8525 continue;
8526 }
8527 break;
8528
8529 case USE:
8530 /* Similar to the case above, except that we can only do this if
8531 the resulting mode is the same as that of the underlying
8532 MEM and adjust the address depending on the *bits* endianness
8533 because of the way that bit-field extract insns are defined. */
8534 if ((code == ASHIFTRT || code == LSHIFTRT)
8535 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8536 MODE_INT, 1)) != BLKmode
8537 && tmode == GET_MODE (XEXP (varop, 0)))
8538 {
8539 if (BITS_BIG_ENDIAN)
8540 new = XEXP (varop, 0);
8541 else
8542 {
8543 new = copy_rtx (XEXP (varop, 0));
8544 SUBST (XEXP (new, 0),
8545 plus_constant (XEXP (new, 0),
8546 count / BITS_PER_UNIT));
8547 }
8548
8549 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8550 : ZERO_EXTEND, mode, new);
8551 count = 0;
8552 continue;
8553 }
8554 break;
8555
8556 case SUBREG:
8557 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8558 the same number of words as what we've seen so far. Then store
8559 the widest mode in MODE. */
8560 if (subreg_lowpart_p (varop)
8561 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8562 > GET_MODE_SIZE (GET_MODE (varop)))
8563 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8564 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8565 == mode_words)
8566 {
8567 varop = SUBREG_REG (varop);
8568 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8569 mode = GET_MODE (varop);
8570 continue;
8571 }
8572 break;
8573
8574 case MULT:
8575 /* Some machines use MULT instead of ASHIFT because MULT
8576 is cheaper. But it is still better on those machines to
8577 merge two shifts into one. */
8578 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8579 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8580 {
8581 varop
8582 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8583 XEXP (varop, 0),
8584 GEN_INT (exact_log2 (
8585 INTVAL (XEXP (varop, 1)))));
8586 continue;
8587 }
8588 break;
8589
8590 case UDIV:
8591 /* Similar, for when divides are cheaper. */
8592 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8593 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8594 {
8595 varop
8596 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8597 XEXP (varop, 0),
8598 GEN_INT (exact_log2 (
8599 INTVAL (XEXP (varop, 1)))));
8600 continue;
8601 }
8602 break;
8603
8604 case ASHIFTRT:
8605 /* If we are extracting just the sign bit of an arithmetic
8606 right shift, that shift is not needed. However, the sign
8607 bit of a wider mode may be different from what would be
8608 interpreted as the sign bit in a narrower mode, so, if
8609 the result is narrower, don't discard the shift. */
8610 if (code == LSHIFTRT
8611 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8612 && (GET_MODE_BITSIZE (result_mode)
8613 >= GET_MODE_BITSIZE (GET_MODE (varop))))
8614 {
8615 varop = XEXP (varop, 0);
8616 continue;
8617 }
8618
8619 /* ... fall through ... */
8620
8621 case LSHIFTRT:
8622 case ASHIFT:
8623 case ROTATE:
8624 /* Here we have two nested shifts. The result is usually the
8625 AND of a new shift with a mask. We compute the result below. */
8626 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8627 && INTVAL (XEXP (varop, 1)) >= 0
8628 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8629 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8630 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8631 {
8632 enum rtx_code first_code = GET_CODE (varop);
8633 unsigned int first_count = INTVAL (XEXP (varop, 1));
8634 unsigned HOST_WIDE_INT mask;
8635 rtx mask_rtx;
8636
8637 /* We have one common special case. We can't do any merging if
8638 the inner code is an ASHIFTRT of a smaller mode. However, if
8639 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8640 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8641 we can convert it to
8642 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8643 This simplifies certain SIGN_EXTEND operations. */
8644 if (code == ASHIFT && first_code == ASHIFTRT
8645 && count == (unsigned int)
8646 (GET_MODE_BITSIZE (result_mode)
8647 - GET_MODE_BITSIZE (GET_MODE (varop))))
8648 {
8649 /* C3 has the low-order C1 bits zero. */
8650
8651 mask = (GET_MODE_MASK (mode)
8652 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8653
8654 varop = simplify_and_const_int (NULL_RTX, result_mode,
8655 XEXP (varop, 0), mask);
8656 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8657 varop, count);
8658 count = first_count;
8659 code = ASHIFTRT;
8660 continue;
8661 }
8662
8663 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8664 than C1 high-order bits equal to the sign bit, we can convert
8665 this to either an ASHIFT or an ASHIFTRT depending on the
8666 two counts.
8667
8668 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8669
8670 if (code == ASHIFTRT && first_code == ASHIFT
8671 && GET_MODE (varop) == shift_mode
8672 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8673 > first_count))
8674 {
8675 varop = XEXP (varop, 0);
8676
8677 signed_count = count - first_count;
8678 if (signed_count < 0)
8679 count = -signed_count, code = ASHIFT;
8680 else
8681 count = signed_count;
8682
8683 continue;
8684 }
8685
8686 /* There are some cases we can't do. If CODE is ASHIFTRT,
8687 we can only do this if FIRST_CODE is also ASHIFTRT.
8688
8689 We can't do the case when CODE is ROTATE and FIRST_CODE is
8690 ASHIFTRT.
8691
8692 If the mode of this shift is not the mode of the outer shift,
8693 we can't do this if either shift is a right shift or ROTATE.
8694
8695 Finally, we can't do any of these if the mode is too wide
8696 unless the codes are the same.
8697
8698 Handle the case where the shift codes are the same
8699 first. */
8700
8701 if (code == first_code)
8702 {
8703 if (GET_MODE (varop) != result_mode
8704 && (code == ASHIFTRT || code == LSHIFTRT
8705 || code == ROTATE))
8706 break;
8707
8708 count += first_count;
8709 varop = XEXP (varop, 0);
8710 continue;
8711 }
8712
8713 if (code == ASHIFTRT
8714 || (code == ROTATE && first_code == ASHIFTRT)
8715 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8716 || (GET_MODE (varop) != result_mode
8717 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8718 || first_code == ROTATE
8719 || code == ROTATE)))
8720 break;
8721
8722 /* To compute the mask to apply after the shift, shift the
8723 nonzero bits of the inner shift the same way the
8724 outer shift will. */
8725
8726 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8727
8728 mask_rtx
8729 = simplify_binary_operation (code, result_mode, mask_rtx,
8730 GEN_INT (count));
8731
8732 /* Give up if we can't compute an outer operation to use. */
8733 if (mask_rtx == 0
8734 || GET_CODE (mask_rtx) != CONST_INT
8735 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8736 INTVAL (mask_rtx),
8737 result_mode, &complement_p))
8738 break;
8739
8740 /* If the shifts are in the same direction, we add the
8741 counts. Otherwise, we subtract them. */
8742 signed_count = count;
8743 if ((code == ASHIFTRT || code == LSHIFTRT)
8744 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8745 signed_count += first_count;
8746 else
8747 signed_count -= first_count;
8748
8749 /* If COUNT is positive, the new shift is usually CODE,
8750 except for the two exceptions below, in which case it is
8751 FIRST_CODE. If the count is negative, FIRST_CODE should
8752 always be used */
8753 if (signed_count > 0
8754 && ((first_code == ROTATE && code == ASHIFT)
8755 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8756 code = first_code, count = signed_count;
8757 else if (signed_count < 0)
8758 code = first_code, count = -signed_count;
8759 else
8760 count = signed_count;
8761
8762 varop = XEXP (varop, 0);
8763 continue;
8764 }
8765
8766 /* If we have (A << B << C) for any shift, we can convert this to
8767 (A << C << B). This wins if A is a constant. Only try this if
8768 B is not a constant. */
8769
8770 else if (GET_CODE (varop) == code
8771 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8772 && 0 != (new
8773 = simplify_binary_operation (code, mode,
8774 XEXP (varop, 0),
8775 GEN_INT (count))))
8776 {
8777 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8778 count = 0;
8779 continue;
8780 }
8781 break;
8782
8783 case NOT:
8784 /* Make this fit the case below. */
8785 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8786 GEN_INT (GET_MODE_MASK (mode)));
8787 continue;
8788
8789 case IOR:
8790 case AND:
8791 case XOR:
8792 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8793 with C the size of VAROP - 1 and the shift is logical if
8794 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8795 we have an (le X 0) operation. If we have an arithmetic shift
8796 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8797 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8798
8799 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8800 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8801 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8802 && (code == LSHIFTRT || code == ASHIFTRT)
8803 && count == (unsigned int)
8804 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8805 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8806 {
8807 count = 0;
8808 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
8809 const0_rtx);
8810
8811 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8812 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8813
8814 continue;
8815 }
8816
8817 /* If we have (shift (logical)), move the logical to the outside
8818 to allow it to possibly combine with another logical and the
8819 shift to combine with another shift. This also canonicalizes to
8820 what a ZERO_EXTRACT looks like. Also, some machines have
8821 (and (shift)) insns. */
8822
8823 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8824 /* We can't do this if we have (ashiftrt (xor)) and the
8825 constant has its sign bit set in shift_mode. */
8826 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8827 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8828 shift_mode))
8829 && (new = simplify_binary_operation (code, result_mode,
8830 XEXP (varop, 1),
8831 GEN_INT (count))) != 0
8832 && GET_CODE (new) == CONST_INT
8833 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8834 INTVAL (new), result_mode, &complement_p))
8835 {
8836 varop = XEXP (varop, 0);
8837 continue;
8838 }
8839
8840 /* If we can't do that, try to simplify the shift in each arm of the
8841 logical expression, make a new logical expression, and apply
8842 the inverse distributive law. This also can't be done
8843 for some (ashiftrt (xor)). */
8844 if (code != ASHIFTRT || GET_CODE (varop) != XOR
8845 || 0 <= trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8846 shift_mode))
8847 {
8848 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8849 XEXP (varop, 0), count);
8850 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8851 XEXP (varop, 1), count);
8852
8853 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
8854 lhs, rhs);
8855 varop = apply_distributive_law (varop);
8856
8857 count = 0;
8858 }
8859 break;
8860
8861 case EQ:
8862 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8863 says that the sign bit can be tested, FOO has mode MODE, C is
8864 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8865 that may be nonzero. */
8866 if (code == LSHIFTRT
8867 && XEXP (varop, 1) == const0_rtx
8868 && GET_MODE (XEXP (varop, 0)) == result_mode
8869 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8870 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8871 && ((STORE_FLAG_VALUE
8872 & ((HOST_WIDE_INT) 1
8873 < (GET_MODE_BITSIZE (result_mode) - 1))))
8874 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8875 && merge_outer_ops (&outer_op, &outer_const, XOR,
8876 (HOST_WIDE_INT) 1, result_mode,
8877 &complement_p))
8878 {
8879 varop = XEXP (varop, 0);
8880 count = 0;
8881 continue;
8882 }
8883 break;
8884
8885 case NEG:
8886 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8887 than the number of bits in the mode is equivalent to A. */
8888 if (code == LSHIFTRT
8889 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8890 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8891 {
8892 varop = XEXP (varop, 0);
8893 count = 0;
8894 continue;
8895 }
8896
8897 /* NEG commutes with ASHIFT since it is multiplication. Move the
8898 NEG outside to allow shifts to combine. */
8899 if (code == ASHIFT
8900 && merge_outer_ops (&outer_op, &outer_const, NEG,
8901 (HOST_WIDE_INT) 0, result_mode,
8902 &complement_p))
8903 {
8904 varop = XEXP (varop, 0);
8905 continue;
8906 }
8907 break;
8908
8909 case PLUS:
8910 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8911 is one less than the number of bits in the mode is
8912 equivalent to (xor A 1). */
8913 if (code == LSHIFTRT
8914 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8915 && XEXP (varop, 1) == constm1_rtx
8916 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8917 && merge_outer_ops (&outer_op, &outer_const, XOR,
8918 (HOST_WIDE_INT) 1, result_mode,
8919 &complement_p))
8920 {
8921 count = 0;
8922 varop = XEXP (varop, 0);
8923 continue;
8924 }
8925
8926 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8927 that might be nonzero in BAR are those being shifted out and those
8928 bits are known zero in FOO, we can replace the PLUS with FOO.
8929 Similarly in the other operand order. This code occurs when
8930 we are computing the size of a variable-size array. */
8931
8932 if ((code == ASHIFTRT || code == LSHIFTRT)
8933 && count < HOST_BITS_PER_WIDE_INT
8934 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8935 && (nonzero_bits (XEXP (varop, 1), result_mode)
8936 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8937 {
8938 varop = XEXP (varop, 0);
8939 continue;
8940 }
8941 else if ((code == ASHIFTRT || code == LSHIFTRT)
8942 && count < HOST_BITS_PER_WIDE_INT
8943 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8944 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8945 >> count)
8946 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8947 & nonzero_bits (XEXP (varop, 1),
8948 result_mode)))
8949 {
8950 varop = XEXP (varop, 1);
8951 continue;
8952 }
8953
8954 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
8955 if (code == ASHIFT
8956 && GET_CODE (XEXP (varop, 1)) == CONST_INT
8957 && (new = simplify_binary_operation (ASHIFT, result_mode,
8958 XEXP (varop, 1),
8959 GEN_INT (count))) != 0
8960 && GET_CODE (new) == CONST_INT
8961 && merge_outer_ops (&outer_op, &outer_const, PLUS,
8962 INTVAL (new), result_mode, &complement_p))
8963 {
8964 varop = XEXP (varop, 0);
8965 continue;
8966 }
8967 break;
8968
8969 case MINUS:
8970 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8971 with C the size of VAROP - 1 and the shift is logical if
8972 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8973 we have a (gt X 0) operation. If the shift is arithmetic with
8974 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8975 we have a (neg (gt X 0)) operation. */
8976
8977 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8978 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8979 && count == (unsigned int)
8980 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8981 && (code == LSHIFTRT || code == ASHIFTRT)
8982 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8983 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
8984 == count
8985 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8986 {
8987 count = 0;
8988 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
8989 const0_rtx);
8990
8991 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8992 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8993
8994 continue;
8995 }
8996 break;
8997
8998 case TRUNCATE:
8999 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9000 if the truncate does not affect the value. */
9001 if (code == LSHIFTRT
9002 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9003 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9004 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9005 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9006 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9007 {
9008 rtx varop_inner = XEXP (varop, 0);
9009
9010 varop_inner
9011 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9012 XEXP (varop_inner, 0),
9013 GEN_INT
9014 (count + INTVAL (XEXP (varop_inner, 1))));
9015 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9016 count = 0;
9017 continue;
9018 }
9019 break;
9020
9021 default:
9022 break;
9023 }
9024
9025 break;
9026 }
9027
9028 /* We need to determine what mode to do the shift in. If the shift is
9029 a right shift or ROTATE, we must always do it in the mode it was
9030 originally done in. Otherwise, we can do it in MODE, the widest mode
9031 encountered. The code we care about is that of the shift that will
9032 actually be done, not the shift that was originally requested. */
9033 shift_mode
9034 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9035 ? result_mode : mode);
9036
9037 /* We have now finished analyzing the shift. The result should be
9038 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9039 OUTER_OP is non-NIL, it is an operation that needs to be applied
9040 to the result of the shift. OUTER_CONST is the relevant constant,
9041 but we must turn off all bits turned off in the shift.
9042
9043 If we were passed a value for X, see if we can use any pieces of
9044 it. If not, make new rtx. */
9045
9046 if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9047 && GET_CODE (XEXP (x, 1)) == CONST_INT
9048 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9049 const_rtx = XEXP (x, 1);
9050 else
9051 const_rtx = GEN_INT (count);
9052
9053 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9054 && GET_MODE (XEXP (x, 0)) == shift_mode
9055 && SUBREG_REG (XEXP (x, 0)) == varop)
9056 varop = XEXP (x, 0);
9057 else if (GET_MODE (varop) != shift_mode)
9058 varop = gen_lowpart (shift_mode, varop);
9059
9060 /* If we can't make the SUBREG, try to return what we were given. */
9061 if (GET_CODE (varop) == CLOBBER)
9062 return x ? x : varop;
9063
9064 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9065 if (new != 0)
9066 x = new;
9067 else
9068 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9069
9070 /* If we have an outer operation and we just made a shift, it is
9071 possible that we could have simplified the shift were it not
9072 for the outer operation. So try to do the simplification
9073 recursively. */
9074
9075 if (outer_op != NIL && GET_CODE (x) == code
9076 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9077 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9078 INTVAL (XEXP (x, 1)));
9079
9080 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9081 turn off all the bits that the shift would have turned off. */
9082 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9083 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9084 GET_MODE_MASK (result_mode) >> orig_count);
9085
9086 /* Do the remainder of the processing in RESULT_MODE. */
9087 x = gen_lowpart (result_mode, x);
9088
9089 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9090 operation. */
9091 if (complement_p)
9092 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9093
9094 if (outer_op != NIL)
9095 {
9096 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9097 outer_const = trunc_int_for_mode (outer_const, result_mode);
9098
9099 if (outer_op == AND)
9100 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9101 else if (outer_op == SET)
9102 /* This means that we have determined that the result is
9103 equivalent to a constant. This should be rare. */
9104 x = GEN_INT (outer_const);
9105 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9106 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9107 else
9108 x = simplify_gen_binary (outer_op, result_mode, x,
9109 GEN_INT (outer_const));
9110 }
9111
9112 return x;
9113 }
9114 \f
9115 /* Like recog, but we receive the address of a pointer to a new pattern.
9116 We try to match the rtx that the pointer points to.
9117 If that fails, we may try to modify or replace the pattern,
9118 storing the replacement into the same pointer object.
9119
9120 Modifications include deletion or addition of CLOBBERs.
9121
9122 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9123 the CLOBBERs are placed.
9124
9125 The value is the final insn code from the pattern ultimately matched,
9126 or -1. */
9127
9128 static int
9129 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9130 {
9131 rtx pat = *pnewpat;
9132 int insn_code_number;
9133 int num_clobbers_to_add = 0;
9134 int i;
9135 rtx notes = 0;
9136 rtx old_notes, old_pat;
9137
9138 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9139 we use to indicate that something didn't match. If we find such a
9140 thing, force rejection. */
9141 if (GET_CODE (pat) == PARALLEL)
9142 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9143 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9144 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9145 return -1;
9146
9147 old_pat = PATTERN (insn);
9148 old_notes = REG_NOTES (insn);
9149 PATTERN (insn) = pat;
9150 REG_NOTES (insn) = 0;
9151
9152 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9153
9154 /* If it isn't, there is the possibility that we previously had an insn
9155 that clobbered some register as a side effect, but the combined
9156 insn doesn't need to do that. So try once more without the clobbers
9157 unless this represents an ASM insn. */
9158
9159 if (insn_code_number < 0 && ! check_asm_operands (pat)
9160 && GET_CODE (pat) == PARALLEL)
9161 {
9162 int pos;
9163
9164 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9165 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9166 {
9167 if (i != pos)
9168 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9169 pos++;
9170 }
9171
9172 SUBST_INT (XVECLEN (pat, 0), pos);
9173
9174 if (pos == 1)
9175 pat = XVECEXP (pat, 0, 0);
9176
9177 PATTERN (insn) = pat;
9178 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9179 }
9180 PATTERN (insn) = old_pat;
9181 REG_NOTES (insn) = old_notes;
9182
9183 /* Recognize all noop sets, these will be killed by followup pass. */
9184 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9185 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9186
9187 /* If we had any clobbers to add, make a new pattern than contains
9188 them. Then check to make sure that all of them are dead. */
9189 if (num_clobbers_to_add)
9190 {
9191 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9192 rtvec_alloc (GET_CODE (pat) == PARALLEL
9193 ? (XVECLEN (pat, 0)
9194 + num_clobbers_to_add)
9195 : num_clobbers_to_add + 1));
9196
9197 if (GET_CODE (pat) == PARALLEL)
9198 for (i = 0; i < XVECLEN (pat, 0); i++)
9199 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9200 else
9201 XVECEXP (newpat, 0, 0) = pat;
9202
9203 add_clobbers (newpat, insn_code_number);
9204
9205 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9206 i < XVECLEN (newpat, 0); i++)
9207 {
9208 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9209 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9210 return -1;
9211 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9212 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9213 }
9214 pat = newpat;
9215 }
9216
9217 *pnewpat = pat;
9218 *pnotes = notes;
9219
9220 return insn_code_number;
9221 }
9222 \f
9223 /* Like gen_lowpart_general but for use by combine. In combine it
9224 is not possible to create any new pseudoregs. However, it is
9225 safe to create invalid memory addresses, because combine will
9226 try to recognize them and all they will do is make the combine
9227 attempt fail.
9228
9229 If for some reason this cannot do its job, an rtx
9230 (clobber (const_int 0)) is returned.
9231 An insn containing that will not be recognized. */
9232
9233 static rtx
9234 gen_lowpart_for_combine (enum machine_mode mode, rtx x)
9235 {
9236 rtx result;
9237
9238 if (GET_MODE (x) == mode)
9239 return x;
9240
9241 /* Return identity if this is a CONST or symbolic
9242 reference. */
9243 if (mode == Pmode
9244 && (GET_CODE (x) == CONST
9245 || GET_CODE (x) == SYMBOL_REF
9246 || GET_CODE (x) == LABEL_REF))
9247 return x;
9248
9249 /* We can only support MODE being wider than a word if X is a
9250 constant integer or has a mode the same size. */
9251
9252 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9253 && ! ((GET_MODE (x) == VOIDmode
9254 && (GET_CODE (x) == CONST_INT
9255 || GET_CODE (x) == CONST_DOUBLE))
9256 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9257 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9258
9259 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9260 won't know what to do. So we will strip off the SUBREG here and
9261 process normally. */
9262 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9263 {
9264 x = SUBREG_REG (x);
9265 if (GET_MODE (x) == mode)
9266 return x;
9267 }
9268
9269 result = gen_lowpart_common (mode, x);
9270 #ifdef CANNOT_CHANGE_MODE_CLASS
9271 if (result != 0
9272 && GET_CODE (result) == SUBREG
9273 && GET_CODE (SUBREG_REG (result)) == REG
9274 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
9275 bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
9276 * MAX_MACHINE_MODE
9277 + GET_MODE (result));
9278 #endif
9279
9280 if (result)
9281 return result;
9282
9283 if (GET_CODE (x) == MEM)
9284 {
9285 int offset = 0;
9286
9287 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9288 address. */
9289 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9290 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9291
9292 /* If we want to refer to something bigger than the original memref,
9293 generate a paradoxical subreg instead. That will force a reload
9294 of the original memref X. */
9295 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9296 return gen_rtx_SUBREG (mode, x, 0);
9297
9298 if (WORDS_BIG_ENDIAN)
9299 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9300 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9301
9302 if (BYTES_BIG_ENDIAN)
9303 {
9304 /* Adjust the address so that the address-after-the-data is
9305 unchanged. */
9306 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9307 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9308 }
9309
9310 return adjust_address_nv (x, mode, offset);
9311 }
9312
9313 /* If X is a comparison operator, rewrite it in a new mode. This
9314 probably won't match, but may allow further simplifications. */
9315 else if (COMPARISON_P (x))
9316 return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9317
9318 /* If we couldn't simplify X any other way, just enclose it in a
9319 SUBREG. Normally, this SUBREG won't match, but some patterns may
9320 include an explicit SUBREG or we may simplify it further in combine. */
9321 else
9322 {
9323 int offset = 0;
9324 rtx res;
9325 enum machine_mode sub_mode = GET_MODE (x);
9326
9327 offset = subreg_lowpart_offset (mode, sub_mode);
9328 if (sub_mode == VOIDmode)
9329 {
9330 sub_mode = int_mode_for_mode (mode);
9331 x = gen_lowpart_common (sub_mode, x);
9332 if (x == 0)
9333 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
9334 }
9335 res = simplify_gen_subreg (mode, x, sub_mode, offset);
9336 if (res)
9337 return res;
9338 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9339 }
9340 }
9341 \f
9342 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9343 comparison code that will be tested.
9344
9345 The result is a possibly different comparison code to use. *POP0 and
9346 *POP1 may be updated.
9347
9348 It is possible that we might detect that a comparison is either always
9349 true or always false. However, we do not perform general constant
9350 folding in combine, so this knowledge isn't useful. Such tautologies
9351 should have been detected earlier. Hence we ignore all such cases. */
9352
9353 static enum rtx_code
9354 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9355 {
9356 rtx op0 = *pop0;
9357 rtx op1 = *pop1;
9358 rtx tem, tem1;
9359 int i;
9360 enum machine_mode mode, tmode;
9361
9362 /* Try a few ways of applying the same transformation to both operands. */
9363 while (1)
9364 {
9365 #ifndef WORD_REGISTER_OPERATIONS
9366 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9367 so check specially. */
9368 if (code != GTU && code != GEU && code != LTU && code != LEU
9369 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9370 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9371 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9372 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9373 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9374 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9375 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9376 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9377 && XEXP (op0, 1) == XEXP (op1, 1)
9378 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9379 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9380 && (INTVAL (XEXP (op0, 1))
9381 == (GET_MODE_BITSIZE (GET_MODE (op0))
9382 - (GET_MODE_BITSIZE
9383 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9384 {
9385 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9386 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9387 }
9388 #endif
9389
9390 /* If both operands are the same constant shift, see if we can ignore the
9391 shift. We can if the shift is a rotate or if the bits shifted out of
9392 this shift are known to be zero for both inputs and if the type of
9393 comparison is compatible with the shift. */
9394 if (GET_CODE (op0) == GET_CODE (op1)
9395 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9396 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9397 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9398 && (code != GT && code != LT && code != GE && code != LE))
9399 || (GET_CODE (op0) == ASHIFTRT
9400 && (code != GTU && code != LTU
9401 && code != GEU && code != LEU)))
9402 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9403 && INTVAL (XEXP (op0, 1)) >= 0
9404 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9405 && XEXP (op0, 1) == XEXP (op1, 1))
9406 {
9407 enum machine_mode mode = GET_MODE (op0);
9408 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9409 int shift_count = INTVAL (XEXP (op0, 1));
9410
9411 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9412 mask &= (mask >> shift_count) << shift_count;
9413 else if (GET_CODE (op0) == ASHIFT)
9414 mask = (mask & (mask << shift_count)) >> shift_count;
9415
9416 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9417 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9418 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9419 else
9420 break;
9421 }
9422
9423 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9424 SUBREGs are of the same mode, and, in both cases, the AND would
9425 be redundant if the comparison was done in the narrower mode,
9426 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9427 and the operand's possibly nonzero bits are 0xffffff01; in that case
9428 if we only care about QImode, we don't need the AND). This case
9429 occurs if the output mode of an scc insn is not SImode and
9430 STORE_FLAG_VALUE == 1 (e.g., the 386).
9431
9432 Similarly, check for a case where the AND's are ZERO_EXTEND
9433 operations from some narrower mode even though a SUBREG is not
9434 present. */
9435
9436 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9437 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9438 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9439 {
9440 rtx inner_op0 = XEXP (op0, 0);
9441 rtx inner_op1 = XEXP (op1, 0);
9442 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9443 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9444 int changed = 0;
9445
9446 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9447 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9448 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9449 && (GET_MODE (SUBREG_REG (inner_op0))
9450 == GET_MODE (SUBREG_REG (inner_op1)))
9451 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9452 <= HOST_BITS_PER_WIDE_INT)
9453 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9454 GET_MODE (SUBREG_REG (inner_op0)))))
9455 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9456 GET_MODE (SUBREG_REG (inner_op1))))))
9457 {
9458 op0 = SUBREG_REG (inner_op0);
9459 op1 = SUBREG_REG (inner_op1);
9460
9461 /* The resulting comparison is always unsigned since we masked
9462 off the original sign bit. */
9463 code = unsigned_condition (code);
9464
9465 changed = 1;
9466 }
9467
9468 else if (c0 == c1)
9469 for (tmode = GET_CLASS_NARROWEST_MODE
9470 (GET_MODE_CLASS (GET_MODE (op0)));
9471 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9472 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9473 {
9474 op0 = gen_lowpart (tmode, inner_op0);
9475 op1 = gen_lowpart (tmode, inner_op1);
9476 code = unsigned_condition (code);
9477 changed = 1;
9478 break;
9479 }
9480
9481 if (! changed)
9482 break;
9483 }
9484
9485 /* If both operands are NOT, we can strip off the outer operation
9486 and adjust the comparison code for swapped operands; similarly for
9487 NEG, except that this must be an equality comparison. */
9488 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9489 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9490 && (code == EQ || code == NE)))
9491 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9492
9493 else
9494 break;
9495 }
9496
9497 /* If the first operand is a constant, swap the operands and adjust the
9498 comparison code appropriately, but don't do this if the second operand
9499 is already a constant integer. */
9500 if (swap_commutative_operands_p (op0, op1))
9501 {
9502 tem = op0, op0 = op1, op1 = tem;
9503 code = swap_condition (code);
9504 }
9505
9506 /* We now enter a loop during which we will try to simplify the comparison.
9507 For the most part, we only are concerned with comparisons with zero,
9508 but some things may really be comparisons with zero but not start
9509 out looking that way. */
9510
9511 while (GET_CODE (op1) == CONST_INT)
9512 {
9513 enum machine_mode mode = GET_MODE (op0);
9514 unsigned int mode_width = GET_MODE_BITSIZE (mode);
9515 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9516 int equality_comparison_p;
9517 int sign_bit_comparison_p;
9518 int unsigned_comparison_p;
9519 HOST_WIDE_INT const_op;
9520
9521 /* We only want to handle integral modes. This catches VOIDmode,
9522 CCmode, and the floating-point modes. An exception is that we
9523 can handle VOIDmode if OP0 is a COMPARE or a comparison
9524 operation. */
9525
9526 if (GET_MODE_CLASS (mode) != MODE_INT
9527 && ! (mode == VOIDmode
9528 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9529 break;
9530
9531 /* Get the constant we are comparing against and turn off all bits
9532 not on in our mode. */
9533 const_op = INTVAL (op1);
9534 if (mode != VOIDmode)
9535 const_op = trunc_int_for_mode (const_op, mode);
9536 op1 = GEN_INT (const_op);
9537
9538 /* If we are comparing against a constant power of two and the value
9539 being compared can only have that single bit nonzero (e.g., it was
9540 `and'ed with that bit), we can replace this with a comparison
9541 with zero. */
9542 if (const_op
9543 && (code == EQ || code == NE || code == GE || code == GEU
9544 || code == LT || code == LTU)
9545 && mode_width <= HOST_BITS_PER_WIDE_INT
9546 && exact_log2 (const_op) >= 0
9547 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9548 {
9549 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9550 op1 = const0_rtx, const_op = 0;
9551 }
9552
9553 /* Similarly, if we are comparing a value known to be either -1 or
9554 0 with -1, change it to the opposite comparison against zero. */
9555
9556 if (const_op == -1
9557 && (code == EQ || code == NE || code == GT || code == LE
9558 || code == GEU || code == LTU)
9559 && num_sign_bit_copies (op0, mode) == mode_width)
9560 {
9561 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9562 op1 = const0_rtx, const_op = 0;
9563 }
9564
9565 /* Do some canonicalizations based on the comparison code. We prefer
9566 comparisons against zero and then prefer equality comparisons.
9567 If we can reduce the size of a constant, we will do that too. */
9568
9569 switch (code)
9570 {
9571 case LT:
9572 /* < C is equivalent to <= (C - 1) */
9573 if (const_op > 0)
9574 {
9575 const_op -= 1;
9576 op1 = GEN_INT (const_op);
9577 code = LE;
9578 /* ... fall through to LE case below. */
9579 }
9580 else
9581 break;
9582
9583 case LE:
9584 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9585 if (const_op < 0)
9586 {
9587 const_op += 1;
9588 op1 = GEN_INT (const_op);
9589 code = LT;
9590 }
9591
9592 /* If we are doing a <= 0 comparison on a value known to have
9593 a zero sign bit, we can replace this with == 0. */
9594 else if (const_op == 0
9595 && mode_width <= HOST_BITS_PER_WIDE_INT
9596 && (nonzero_bits (op0, mode)
9597 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9598 code = EQ;
9599 break;
9600
9601 case GE:
9602 /* >= C is equivalent to > (C - 1). */
9603 if (const_op > 0)
9604 {
9605 const_op -= 1;
9606 op1 = GEN_INT (const_op);
9607 code = GT;
9608 /* ... fall through to GT below. */
9609 }
9610 else
9611 break;
9612
9613 case GT:
9614 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
9615 if (const_op < 0)
9616 {
9617 const_op += 1;
9618 op1 = GEN_INT (const_op);
9619 code = GE;
9620 }
9621
9622 /* If we are doing a > 0 comparison on a value known to have
9623 a zero sign bit, we can replace this with != 0. */
9624 else if (const_op == 0
9625 && mode_width <= HOST_BITS_PER_WIDE_INT
9626 && (nonzero_bits (op0, mode)
9627 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9628 code = NE;
9629 break;
9630
9631 case LTU:
9632 /* < C is equivalent to <= (C - 1). */
9633 if (const_op > 0)
9634 {
9635 const_op -= 1;
9636 op1 = GEN_INT (const_op);
9637 code = LEU;
9638 /* ... fall through ... */
9639 }
9640
9641 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9642 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9643 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9644 {
9645 const_op = 0, op1 = const0_rtx;
9646 code = GE;
9647 break;
9648 }
9649 else
9650 break;
9651
9652 case LEU:
9653 /* unsigned <= 0 is equivalent to == 0 */
9654 if (const_op == 0)
9655 code = EQ;
9656
9657 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9658 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9659 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9660 {
9661 const_op = 0, op1 = const0_rtx;
9662 code = GE;
9663 }
9664 break;
9665
9666 case GEU:
9667 /* >= C is equivalent to < (C - 1). */
9668 if (const_op > 1)
9669 {
9670 const_op -= 1;
9671 op1 = GEN_INT (const_op);
9672 code = GTU;
9673 /* ... fall through ... */
9674 }
9675
9676 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9677 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9678 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9679 {
9680 const_op = 0, op1 = const0_rtx;
9681 code = LT;
9682 break;
9683 }
9684 else
9685 break;
9686
9687 case GTU:
9688 /* unsigned > 0 is equivalent to != 0 */
9689 if (const_op == 0)
9690 code = NE;
9691
9692 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9693 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9694 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9695 {
9696 const_op = 0, op1 = const0_rtx;
9697 code = LT;
9698 }
9699 break;
9700
9701 default:
9702 break;
9703 }
9704
9705 /* Compute some predicates to simplify code below. */
9706
9707 equality_comparison_p = (code == EQ || code == NE);
9708 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9709 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9710 || code == GEU);
9711
9712 /* If this is a sign bit comparison and we can do arithmetic in
9713 MODE, say that we will only be needing the sign bit of OP0. */
9714 if (sign_bit_comparison_p
9715 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9716 op0 = force_to_mode (op0, mode,
9717 ((HOST_WIDE_INT) 1
9718 << (GET_MODE_BITSIZE (mode) - 1)),
9719 NULL_RTX, 0);
9720
9721 /* Now try cases based on the opcode of OP0. If none of the cases
9722 does a "continue", we exit this loop immediately after the
9723 switch. */
9724
9725 switch (GET_CODE (op0))
9726 {
9727 case ZERO_EXTRACT:
9728 /* If we are extracting a single bit from a variable position in
9729 a constant that has only a single bit set and are comparing it
9730 with zero, we can convert this into an equality comparison
9731 between the position and the location of the single bit. */
9732 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9733 have already reduced the shift count modulo the word size. */
9734 if (!SHIFT_COUNT_TRUNCATED
9735 && GET_CODE (XEXP (op0, 0)) == CONST_INT
9736 && XEXP (op0, 1) == const1_rtx
9737 && equality_comparison_p && const_op == 0
9738 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9739 {
9740 if (BITS_BIG_ENDIAN)
9741 {
9742 enum machine_mode new_mode
9743 = mode_for_extraction (EP_extzv, 1);
9744 if (new_mode == MAX_MACHINE_MODE)
9745 i = BITS_PER_WORD - 1 - i;
9746 else
9747 {
9748 mode = new_mode;
9749 i = (GET_MODE_BITSIZE (mode) - 1 - i);
9750 }
9751 }
9752
9753 op0 = XEXP (op0, 2);
9754 op1 = GEN_INT (i);
9755 const_op = i;
9756
9757 /* Result is nonzero iff shift count is equal to I. */
9758 code = reverse_condition (code);
9759 continue;
9760 }
9761
9762 /* ... fall through ... */
9763
9764 case SIGN_EXTRACT:
9765 tem = expand_compound_operation (op0);
9766 if (tem != op0)
9767 {
9768 op0 = tem;
9769 continue;
9770 }
9771 break;
9772
9773 case NOT:
9774 /* If testing for equality, we can take the NOT of the constant. */
9775 if (equality_comparison_p
9776 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9777 {
9778 op0 = XEXP (op0, 0);
9779 op1 = tem;
9780 continue;
9781 }
9782
9783 /* If just looking at the sign bit, reverse the sense of the
9784 comparison. */
9785 if (sign_bit_comparison_p)
9786 {
9787 op0 = XEXP (op0, 0);
9788 code = (code == GE ? LT : GE);
9789 continue;
9790 }
9791 break;
9792
9793 case NEG:
9794 /* If testing for equality, we can take the NEG of the constant. */
9795 if (equality_comparison_p
9796 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9797 {
9798 op0 = XEXP (op0, 0);
9799 op1 = tem;
9800 continue;
9801 }
9802
9803 /* The remaining cases only apply to comparisons with zero. */
9804 if (const_op != 0)
9805 break;
9806
9807 /* When X is ABS or is known positive,
9808 (neg X) is < 0 if and only if X != 0. */
9809
9810 if (sign_bit_comparison_p
9811 && (GET_CODE (XEXP (op0, 0)) == ABS
9812 || (mode_width <= HOST_BITS_PER_WIDE_INT
9813 && (nonzero_bits (XEXP (op0, 0), mode)
9814 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9815 {
9816 op0 = XEXP (op0, 0);
9817 code = (code == LT ? NE : EQ);
9818 continue;
9819 }
9820
9821 /* If we have NEG of something whose two high-order bits are the
9822 same, we know that "(-a) < 0" is equivalent to "a > 0". */
9823 if (num_sign_bit_copies (op0, mode) >= 2)
9824 {
9825 op0 = XEXP (op0, 0);
9826 code = swap_condition (code);
9827 continue;
9828 }
9829 break;
9830
9831 case ROTATE:
9832 /* If we are testing equality and our count is a constant, we
9833 can perform the inverse operation on our RHS. */
9834 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9835 && (tem = simplify_binary_operation (ROTATERT, mode,
9836 op1, XEXP (op0, 1))) != 0)
9837 {
9838 op0 = XEXP (op0, 0);
9839 op1 = tem;
9840 continue;
9841 }
9842
9843 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9844 a particular bit. Convert it to an AND of a constant of that
9845 bit. This will be converted into a ZERO_EXTRACT. */
9846 if (const_op == 0 && sign_bit_comparison_p
9847 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9848 && mode_width <= HOST_BITS_PER_WIDE_INT)
9849 {
9850 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9851 ((HOST_WIDE_INT) 1
9852 << (mode_width - 1
9853 - INTVAL (XEXP (op0, 1)))));
9854 code = (code == LT ? NE : EQ);
9855 continue;
9856 }
9857
9858 /* Fall through. */
9859
9860 case ABS:
9861 /* ABS is ignorable inside an equality comparison with zero. */
9862 if (const_op == 0 && equality_comparison_p)
9863 {
9864 op0 = XEXP (op0, 0);
9865 continue;
9866 }
9867 break;
9868
9869 case SIGN_EXTEND:
9870 /* Can simplify (compare (zero/sign_extend FOO) CONST)
9871 to (compare FOO CONST) if CONST fits in FOO's mode and we
9872 are either testing inequality or have an unsigned comparison
9873 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
9874 if (! unsigned_comparison_p
9875 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9876 <= HOST_BITS_PER_WIDE_INT)
9877 && ((unsigned HOST_WIDE_INT) const_op
9878 < (((unsigned HOST_WIDE_INT) 1
9879 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
9880 {
9881 op0 = XEXP (op0, 0);
9882 continue;
9883 }
9884 break;
9885
9886 case SUBREG:
9887 /* Check for the case where we are comparing A - C1 with C2,
9888 both constants are smaller than 1/2 the maximum positive
9889 value in MODE, and the comparison is equality or unsigned.
9890 In that case, if A is either zero-extended to MODE or has
9891 sufficient sign bits so that the high-order bit in MODE
9892 is a copy of the sign in the inner mode, we can prove that it is
9893 safe to do the operation in the wider mode. This simplifies
9894 many range checks. */
9895
9896 if (mode_width <= HOST_BITS_PER_WIDE_INT
9897 && subreg_lowpart_p (op0)
9898 && GET_CODE (SUBREG_REG (op0)) == PLUS
9899 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9900 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9901 && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
9902 < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
9903 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
9904 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9905 GET_MODE (SUBREG_REG (op0)))
9906 & ~GET_MODE_MASK (mode))
9907 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9908 GET_MODE (SUBREG_REG (op0)))
9909 > (unsigned int)
9910 (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9911 - GET_MODE_BITSIZE (mode)))))
9912 {
9913 op0 = SUBREG_REG (op0);
9914 continue;
9915 }
9916
9917 /* If the inner mode is narrower and we are extracting the low part,
9918 we can treat the SUBREG as if it were a ZERO_EXTEND. */
9919 if (subreg_lowpart_p (op0)
9920 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9921 /* Fall through */ ;
9922 else
9923 break;
9924
9925 /* ... fall through ... */
9926
9927 case ZERO_EXTEND:
9928 if ((unsigned_comparison_p || equality_comparison_p)
9929 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9930 <= HOST_BITS_PER_WIDE_INT)
9931 && ((unsigned HOST_WIDE_INT) const_op
9932 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9933 {
9934 op0 = XEXP (op0, 0);
9935 continue;
9936 }
9937 break;
9938
9939 case PLUS:
9940 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
9941 this for equality comparisons due to pathological cases involving
9942 overflows. */
9943 if (equality_comparison_p
9944 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9945 op1, XEXP (op0, 1))))
9946 {
9947 op0 = XEXP (op0, 0);
9948 op1 = tem;
9949 continue;
9950 }
9951
9952 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
9953 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9954 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9955 {
9956 op0 = XEXP (XEXP (op0, 0), 0);
9957 code = (code == LT ? EQ : NE);
9958 continue;
9959 }
9960 break;
9961
9962 case MINUS:
9963 /* We used to optimize signed comparisons against zero, but that
9964 was incorrect. Unsigned comparisons against zero (GTU, LEU)
9965 arrive here as equality comparisons, or (GEU, LTU) are
9966 optimized away. No need to special-case them. */
9967
9968 /* (eq (minus A B) C) -> (eq A (plus B C)) or
9969 (eq B (minus A C)), whichever simplifies. We can only do
9970 this for equality comparisons due to pathological cases involving
9971 overflows. */
9972 if (equality_comparison_p
9973 && 0 != (tem = simplify_binary_operation (PLUS, mode,
9974 XEXP (op0, 1), op1)))
9975 {
9976 op0 = XEXP (op0, 0);
9977 op1 = tem;
9978 continue;
9979 }
9980
9981 if (equality_comparison_p
9982 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9983 XEXP (op0, 0), op1)))
9984 {
9985 op0 = XEXP (op0, 1);
9986 op1 = tem;
9987 continue;
9988 }
9989
9990 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
9991 of bits in X minus 1, is one iff X > 0. */
9992 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
9993 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9994 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
9995 == mode_width - 1
9996 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9997 {
9998 op0 = XEXP (op0, 1);
9999 code = (code == GE ? LE : GT);
10000 continue;
10001 }
10002 break;
10003
10004 case XOR:
10005 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10006 if C is zero or B is a constant. */
10007 if (equality_comparison_p
10008 && 0 != (tem = simplify_binary_operation (XOR, mode,
10009 XEXP (op0, 1), op1)))
10010 {
10011 op0 = XEXP (op0, 0);
10012 op1 = tem;
10013 continue;
10014 }
10015 break;
10016
10017 case EQ: case NE:
10018 case UNEQ: case LTGT:
10019 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10020 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10021 case UNORDERED: case ORDERED:
10022 /* We can't do anything if OP0 is a condition code value, rather
10023 than an actual data value. */
10024 if (const_op != 0
10025 || CC0_P (XEXP (op0, 0))
10026 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10027 break;
10028
10029 /* Get the two operands being compared. */
10030 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10031 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10032 else
10033 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10034
10035 /* Check for the cases where we simply want the result of the
10036 earlier test or the opposite of that result. */
10037 if (code == NE || code == EQ
10038 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10039 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10040 && (STORE_FLAG_VALUE
10041 & (((HOST_WIDE_INT) 1
10042 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10043 && (code == LT || code == GE)))
10044 {
10045 enum rtx_code new_code;
10046 if (code == LT || code == NE)
10047 new_code = GET_CODE (op0);
10048 else
10049 new_code = combine_reversed_comparison_code (op0);
10050
10051 if (new_code != UNKNOWN)
10052 {
10053 code = new_code;
10054 op0 = tem;
10055 op1 = tem1;
10056 continue;
10057 }
10058 }
10059 break;
10060
10061 case IOR:
10062 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10063 iff X <= 0. */
10064 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10065 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10066 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10067 {
10068 op0 = XEXP (op0, 1);
10069 code = (code == GE ? GT : LE);
10070 continue;
10071 }
10072 break;
10073
10074 case AND:
10075 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10076 will be converted to a ZERO_EXTRACT later. */
10077 if (const_op == 0 && equality_comparison_p
10078 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10079 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10080 {
10081 op0 = simplify_and_const_int
10082 (op0, mode, gen_rtx_LSHIFTRT (mode,
10083 XEXP (op0, 1),
10084 XEXP (XEXP (op0, 0), 1)),
10085 (HOST_WIDE_INT) 1);
10086 continue;
10087 }
10088
10089 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10090 zero and X is a comparison and C1 and C2 describe only bits set
10091 in STORE_FLAG_VALUE, we can compare with X. */
10092 if (const_op == 0 && equality_comparison_p
10093 && mode_width <= HOST_BITS_PER_WIDE_INT
10094 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10095 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10096 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10097 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10098 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10099 {
10100 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10101 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10102 if ((~STORE_FLAG_VALUE & mask) == 0
10103 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10104 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10105 && COMPARISON_P (tem))))
10106 {
10107 op0 = XEXP (XEXP (op0, 0), 0);
10108 continue;
10109 }
10110 }
10111
10112 /* If we are doing an equality comparison of an AND of a bit equal
10113 to the sign bit, replace this with a LT or GE comparison of
10114 the underlying value. */
10115 if (equality_comparison_p
10116 && const_op == 0
10117 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10118 && mode_width <= HOST_BITS_PER_WIDE_INT
10119 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10120 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10121 {
10122 op0 = XEXP (op0, 0);
10123 code = (code == EQ ? GE : LT);
10124 continue;
10125 }
10126
10127 /* If this AND operation is really a ZERO_EXTEND from a narrower
10128 mode, the constant fits within that mode, and this is either an
10129 equality or unsigned comparison, try to do this comparison in
10130 the narrower mode. */
10131 if ((equality_comparison_p || unsigned_comparison_p)
10132 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10133 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10134 & GET_MODE_MASK (mode))
10135 + 1)) >= 0
10136 && const_op >> i == 0
10137 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10138 {
10139 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10140 continue;
10141 }
10142
10143 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10144 fits in both M1 and M2 and the SUBREG is either paradoxical
10145 or represents the low part, permute the SUBREG and the AND
10146 and try again. */
10147 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10148 {
10149 unsigned HOST_WIDE_INT c1;
10150 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10151 /* Require an integral mode, to avoid creating something like
10152 (AND:SF ...). */
10153 if (SCALAR_INT_MODE_P (tmode)
10154 /* It is unsafe to commute the AND into the SUBREG if the
10155 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10156 not defined. As originally written the upper bits
10157 have a defined value due to the AND operation.
10158 However, if we commute the AND inside the SUBREG then
10159 they no longer have defined values and the meaning of
10160 the code has been changed. */
10161 && (0
10162 #ifdef WORD_REGISTER_OPERATIONS
10163 || (mode_width > GET_MODE_BITSIZE (tmode)
10164 && mode_width <= BITS_PER_WORD)
10165 #endif
10166 || (mode_width <= GET_MODE_BITSIZE (tmode)
10167 && subreg_lowpart_p (XEXP (op0, 0))))
10168 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10169 && mode_width <= HOST_BITS_PER_WIDE_INT
10170 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10171 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10172 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10173 && c1 != mask
10174 && c1 != GET_MODE_MASK (tmode))
10175 {
10176 op0 = simplify_gen_binary (AND, tmode,
10177 SUBREG_REG (XEXP (op0, 0)),
10178 gen_int_mode (c1, tmode));
10179 op0 = gen_lowpart (mode, op0);
10180 continue;
10181 }
10182 }
10183
10184 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10185 if (const_op == 0 && equality_comparison_p
10186 && XEXP (op0, 1) == const1_rtx
10187 && GET_CODE (XEXP (op0, 0)) == NOT)
10188 {
10189 op0 = simplify_and_const_int
10190 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10191 code = (code == NE ? EQ : NE);
10192 continue;
10193 }
10194
10195 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10196 (eq (and (lshiftrt X) 1) 0).
10197 Also handle the case where (not X) is expressed using xor. */
10198 if (const_op == 0 && equality_comparison_p
10199 && XEXP (op0, 1) == const1_rtx
10200 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10201 {
10202 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10203 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10204
10205 if (GET_CODE (shift_op) == NOT
10206 || (GET_CODE (shift_op) == XOR
10207 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10208 && GET_CODE (shift_count) == CONST_INT
10209 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10210 && (INTVAL (XEXP (shift_op, 1))
10211 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10212 {
10213 op0 = simplify_and_const_int
10214 (NULL_RTX, mode,
10215 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10216 (HOST_WIDE_INT) 1);
10217 code = (code == NE ? EQ : NE);
10218 continue;
10219 }
10220 }
10221 break;
10222
10223 case ASHIFT:
10224 /* If we have (compare (ashift FOO N) (const_int C)) and
10225 the high order N bits of FOO (N+1 if an inequality comparison)
10226 are known to be zero, we can do this by comparing FOO with C
10227 shifted right N bits so long as the low-order N bits of C are
10228 zero. */
10229 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10230 && INTVAL (XEXP (op0, 1)) >= 0
10231 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10232 < HOST_BITS_PER_WIDE_INT)
10233 && ((const_op
10234 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10235 && mode_width <= HOST_BITS_PER_WIDE_INT
10236 && (nonzero_bits (XEXP (op0, 0), mode)
10237 & ~(mask >> (INTVAL (XEXP (op0, 1))
10238 + ! equality_comparison_p))) == 0)
10239 {
10240 /* We must perform a logical shift, not an arithmetic one,
10241 as we want the top N bits of C to be zero. */
10242 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10243
10244 temp >>= INTVAL (XEXP (op0, 1));
10245 op1 = gen_int_mode (temp, mode);
10246 op0 = XEXP (op0, 0);
10247 continue;
10248 }
10249
10250 /* If we are doing a sign bit comparison, it means we are testing
10251 a particular bit. Convert it to the appropriate AND. */
10252 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10253 && mode_width <= HOST_BITS_PER_WIDE_INT)
10254 {
10255 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10256 ((HOST_WIDE_INT) 1
10257 << (mode_width - 1
10258 - INTVAL (XEXP (op0, 1)))));
10259 code = (code == LT ? NE : EQ);
10260 continue;
10261 }
10262
10263 /* If this an equality comparison with zero and we are shifting
10264 the low bit to the sign bit, we can convert this to an AND of the
10265 low-order bit. */
10266 if (const_op == 0 && equality_comparison_p
10267 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10268 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10269 == mode_width - 1)
10270 {
10271 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10272 (HOST_WIDE_INT) 1);
10273 continue;
10274 }
10275 break;
10276
10277 case ASHIFTRT:
10278 /* If this is an equality comparison with zero, we can do this
10279 as a logical shift, which might be much simpler. */
10280 if (equality_comparison_p && const_op == 0
10281 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10282 {
10283 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10284 XEXP (op0, 0),
10285 INTVAL (XEXP (op0, 1)));
10286 continue;
10287 }
10288
10289 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10290 do the comparison in a narrower mode. */
10291 if (! unsigned_comparison_p
10292 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10293 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10294 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10295 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10296 MODE_INT, 1)) != BLKmode
10297 && (((unsigned HOST_WIDE_INT) const_op
10298 + (GET_MODE_MASK (tmode) >> 1) + 1)
10299 <= GET_MODE_MASK (tmode)))
10300 {
10301 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10302 continue;
10303 }
10304
10305 /* Likewise if OP0 is a PLUS of a sign extension with a
10306 constant, which is usually represented with the PLUS
10307 between the shifts. */
10308 if (! unsigned_comparison_p
10309 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10310 && GET_CODE (XEXP (op0, 0)) == PLUS
10311 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10312 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10313 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10314 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10315 MODE_INT, 1)) != BLKmode
10316 && (((unsigned HOST_WIDE_INT) const_op
10317 + (GET_MODE_MASK (tmode) >> 1) + 1)
10318 <= GET_MODE_MASK (tmode)))
10319 {
10320 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10321 rtx add_const = XEXP (XEXP (op0, 0), 1);
10322 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10323 add_const, XEXP (op0, 1));
10324
10325 op0 = simplify_gen_binary (PLUS, tmode,
10326 gen_lowpart (tmode, inner),
10327 new_const);
10328 continue;
10329 }
10330
10331 /* ... fall through ... */
10332 case LSHIFTRT:
10333 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10334 the low order N bits of FOO are known to be zero, we can do this
10335 by comparing FOO with C shifted left N bits so long as no
10336 overflow occurs. */
10337 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10338 && INTVAL (XEXP (op0, 1)) >= 0
10339 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10340 && mode_width <= HOST_BITS_PER_WIDE_INT
10341 && (nonzero_bits (XEXP (op0, 0), mode)
10342 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10343 && (((unsigned HOST_WIDE_INT) const_op
10344 + (GET_CODE (op0) != LSHIFTRT
10345 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10346 + 1)
10347 : 0))
10348 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10349 {
10350 /* If the shift was logical, then we must make the condition
10351 unsigned. */
10352 if (GET_CODE (op0) == LSHIFTRT)
10353 code = unsigned_condition (code);
10354
10355 const_op <<= INTVAL (XEXP (op0, 1));
10356 op1 = GEN_INT (const_op);
10357 op0 = XEXP (op0, 0);
10358 continue;
10359 }
10360
10361 /* If we are using this shift to extract just the sign bit, we
10362 can replace this with an LT or GE comparison. */
10363 if (const_op == 0
10364 && (equality_comparison_p || sign_bit_comparison_p)
10365 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10366 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10367 == mode_width - 1)
10368 {
10369 op0 = XEXP (op0, 0);
10370 code = (code == NE || code == GT ? LT : GE);
10371 continue;
10372 }
10373 break;
10374
10375 default:
10376 break;
10377 }
10378
10379 break;
10380 }
10381
10382 /* Now make any compound operations involved in this comparison. Then,
10383 check for an outmost SUBREG on OP0 that is not doing anything or is
10384 paradoxical. The latter transformation must only be performed when
10385 it is known that the "extra" bits will be the same in op0 and op1 or
10386 that they don't matter. There are three cases to consider:
10387
10388 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10389 care bits and we can assume they have any convenient value. So
10390 making the transformation is safe.
10391
10392 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10393 In this case the upper bits of op0 are undefined. We should not make
10394 the simplification in that case as we do not know the contents of
10395 those bits.
10396
10397 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10398 NIL. In that case we know those bits are zeros or ones. We must
10399 also be sure that they are the same as the upper bits of op1.
10400
10401 We can never remove a SUBREG for a non-equality comparison because
10402 the sign bit is in a different place in the underlying object. */
10403
10404 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10405 op1 = make_compound_operation (op1, SET);
10406
10407 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10408 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10409 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10410 && (code == NE || code == EQ))
10411 {
10412 if (GET_MODE_SIZE (GET_MODE (op0))
10413 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10414 {
10415 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10416 implemented. */
10417 if (GET_CODE (SUBREG_REG (op0)) == REG)
10418 {
10419 op0 = SUBREG_REG (op0);
10420 op1 = gen_lowpart (GET_MODE (op0), op1);
10421 }
10422 }
10423 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10424 <= HOST_BITS_PER_WIDE_INT)
10425 && (nonzero_bits (SUBREG_REG (op0),
10426 GET_MODE (SUBREG_REG (op0)))
10427 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10428 {
10429 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10430
10431 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10432 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10433 op0 = SUBREG_REG (op0), op1 = tem;
10434 }
10435 }
10436
10437 /* We now do the opposite procedure: Some machines don't have compare
10438 insns in all modes. If OP0's mode is an integer mode smaller than a
10439 word and we can't do a compare in that mode, see if there is a larger
10440 mode for which we can do the compare. There are a number of cases in
10441 which we can use the wider mode. */
10442
10443 mode = GET_MODE (op0);
10444 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10445 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10446 && ! have_insn_for (COMPARE, mode))
10447 for (tmode = GET_MODE_WIDER_MODE (mode);
10448 (tmode != VOIDmode
10449 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10450 tmode = GET_MODE_WIDER_MODE (tmode))
10451 if (have_insn_for (COMPARE, tmode))
10452 {
10453 int zero_extended;
10454
10455 /* If the only nonzero bits in OP0 and OP1 are those in the
10456 narrower mode and this is an equality or unsigned comparison,
10457 we can use the wider mode. Similarly for sign-extended
10458 values, in which case it is true for all comparisons. */
10459 zero_extended = ((code == EQ || code == NE
10460 || code == GEU || code == GTU
10461 || code == LEU || code == LTU)
10462 && (nonzero_bits (op0, tmode)
10463 & ~GET_MODE_MASK (mode)) == 0
10464 && ((GET_CODE (op1) == CONST_INT
10465 || (nonzero_bits (op1, tmode)
10466 & ~GET_MODE_MASK (mode)) == 0)));
10467
10468 if (zero_extended
10469 || ((num_sign_bit_copies (op0, tmode)
10470 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10471 - GET_MODE_BITSIZE (mode)))
10472 && (num_sign_bit_copies (op1, tmode)
10473 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10474 - GET_MODE_BITSIZE (mode)))))
10475 {
10476 /* If OP0 is an AND and we don't have an AND in MODE either,
10477 make a new AND in the proper mode. */
10478 if (GET_CODE (op0) == AND
10479 && !have_insn_for (AND, mode))
10480 op0 = simplify_gen_binary (AND, tmode,
10481 gen_lowpart (tmode,
10482 XEXP (op0, 0)),
10483 gen_lowpart (tmode,
10484 XEXP (op0, 1)));
10485
10486 op0 = gen_lowpart (tmode, op0);
10487 if (zero_extended && GET_CODE (op1) == CONST_INT)
10488 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10489 op1 = gen_lowpart (tmode, op1);
10490 break;
10491 }
10492
10493 /* If this is a test for negative, we can make an explicit
10494 test of the sign bit. */
10495
10496 if (op1 == const0_rtx && (code == LT || code == GE)
10497 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10498 {
10499 op0 = simplify_gen_binary (AND, tmode,
10500 gen_lowpart (tmode, op0),
10501 GEN_INT ((HOST_WIDE_INT) 1
10502 << (GET_MODE_BITSIZE (mode)
10503 - 1)));
10504 code = (code == LT) ? NE : EQ;
10505 break;
10506 }
10507 }
10508
10509 #ifdef CANONICALIZE_COMPARISON
10510 /* If this machine only supports a subset of valid comparisons, see if we
10511 can convert an unsupported one into a supported one. */
10512 CANONICALIZE_COMPARISON (code, op0, op1);
10513 #endif
10514
10515 *pop0 = op0;
10516 *pop1 = op1;
10517
10518 return code;
10519 }
10520 \f
10521 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
10522 searching backward. */
10523 static enum rtx_code
10524 combine_reversed_comparison_code (rtx exp)
10525 {
10526 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
10527 rtx x;
10528
10529 if (code1 != UNKNOWN
10530 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
10531 return code1;
10532 /* Otherwise try and find where the condition codes were last set and
10533 use that. */
10534 x = get_last_value (XEXP (exp, 0));
10535 if (!x || GET_CODE (x) != COMPARE)
10536 return UNKNOWN;
10537 return reversed_comparison_code_parts (GET_CODE (exp),
10538 XEXP (x, 0), XEXP (x, 1), NULL);
10539 }
10540
10541 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
10542 Return NULL_RTX in case we fail to do the reversal. */
10543 static rtx
10544 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
10545 {
10546 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
10547 if (reversed_code == UNKNOWN)
10548 return NULL_RTX;
10549 else
10550 return simplify_gen_relational (reversed_code, mode, VOIDmode, op0, op1);
10551 }
10552 \f
10553 /* Utility function for following routine. Called when X is part of a value
10554 being stored into last_set_value. Sets last_set_table_tick
10555 for each register mentioned. Similar to mention_regs in cse.c */
10556
10557 static void
10558 update_table_tick (rtx x)
10559 {
10560 enum rtx_code code = GET_CODE (x);
10561 const char *fmt = GET_RTX_FORMAT (code);
10562 int i;
10563
10564 if (code == REG)
10565 {
10566 unsigned int regno = REGNO (x);
10567 unsigned int endregno
10568 = regno + (regno < FIRST_PSEUDO_REGISTER
10569 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10570 unsigned int r;
10571
10572 for (r = regno; r < endregno; r++)
10573 reg_stat[r].last_set_table_tick = label_tick;
10574
10575 return;
10576 }
10577
10578 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10579 /* Note that we can't have an "E" in values stored; see
10580 get_last_value_validate. */
10581 if (fmt[i] == 'e')
10582 {
10583 /* Check for identical subexpressions. If x contains
10584 identical subexpression we only have to traverse one of
10585 them. */
10586 if (i == 0 && ARITHMETIC_P (x))
10587 {
10588 /* Note that at this point x1 has already been
10589 processed. */
10590 rtx x0 = XEXP (x, 0);
10591 rtx x1 = XEXP (x, 1);
10592
10593 /* If x0 and x1 are identical then there is no need to
10594 process x0. */
10595 if (x0 == x1)
10596 break;
10597
10598 /* If x0 is identical to a subexpression of x1 then while
10599 processing x1, x0 has already been processed. Thus we
10600 are done with x. */
10601 if (ARITHMETIC_P (x1)
10602 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10603 break;
10604
10605 /* If x1 is identical to a subexpression of x0 then we
10606 still have to process the rest of x0. */
10607 if (ARITHMETIC_P (x0)
10608 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10609 {
10610 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10611 break;
10612 }
10613 }
10614
10615 update_table_tick (XEXP (x, i));
10616 }
10617 }
10618
10619 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10620 are saying that the register is clobbered and we no longer know its
10621 value. If INSN is zero, don't update reg_stat[].last_set; this is
10622 only permitted with VALUE also zero and is used to invalidate the
10623 register. */
10624
10625 static void
10626 record_value_for_reg (rtx reg, rtx insn, rtx value)
10627 {
10628 unsigned int regno = REGNO (reg);
10629 unsigned int endregno
10630 = regno + (regno < FIRST_PSEUDO_REGISTER
10631 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10632 unsigned int i;
10633
10634 /* If VALUE contains REG and we have a previous value for REG, substitute
10635 the previous value. */
10636 if (value && insn && reg_overlap_mentioned_p (reg, value))
10637 {
10638 rtx tem;
10639
10640 /* Set things up so get_last_value is allowed to see anything set up to
10641 our insn. */
10642 subst_low_cuid = INSN_CUID (insn);
10643 tem = get_last_value (reg);
10644
10645 /* If TEM is simply a binary operation with two CLOBBERs as operands,
10646 it isn't going to be useful and will take a lot of time to process,
10647 so just use the CLOBBER. */
10648
10649 if (tem)
10650 {
10651 if (ARITHMETIC_P (tem)
10652 && GET_CODE (XEXP (tem, 0)) == CLOBBER
10653 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10654 tem = XEXP (tem, 0);
10655
10656 value = replace_rtx (copy_rtx (value), reg, tem);
10657 }
10658 }
10659
10660 /* For each register modified, show we don't know its value, that
10661 we don't know about its bitwise content, that its value has been
10662 updated, and that we don't know the location of the death of the
10663 register. */
10664 for (i = regno; i < endregno; i++)
10665 {
10666 if (insn)
10667 reg_stat[i].last_set = insn;
10668
10669 reg_stat[i].last_set_value = 0;
10670 reg_stat[i].last_set_mode = 0;
10671 reg_stat[i].last_set_nonzero_bits = 0;
10672 reg_stat[i].last_set_sign_bit_copies = 0;
10673 reg_stat[i].last_death = 0;
10674 }
10675
10676 /* Mark registers that are being referenced in this value. */
10677 if (value)
10678 update_table_tick (value);
10679
10680 /* Now update the status of each register being set.
10681 If someone is using this register in this block, set this register
10682 to invalid since we will get confused between the two lives in this
10683 basic block. This makes using this register always invalid. In cse, we
10684 scan the table to invalidate all entries using this register, but this
10685 is too much work for us. */
10686
10687 for (i = regno; i < endregno; i++)
10688 {
10689 reg_stat[i].last_set_label = label_tick;
10690 if (value && reg_stat[i].last_set_table_tick == label_tick)
10691 reg_stat[i].last_set_invalid = 1;
10692 else
10693 reg_stat[i].last_set_invalid = 0;
10694 }
10695
10696 /* The value being assigned might refer to X (like in "x++;"). In that
10697 case, we must replace it with (clobber (const_int 0)) to prevent
10698 infinite loops. */
10699 if (value && ! get_last_value_validate (&value, insn,
10700 reg_stat[regno].last_set_label, 0))
10701 {
10702 value = copy_rtx (value);
10703 if (! get_last_value_validate (&value, insn,
10704 reg_stat[regno].last_set_label, 1))
10705 value = 0;
10706 }
10707
10708 /* For the main register being modified, update the value, the mode, the
10709 nonzero bits, and the number of sign bit copies. */
10710
10711 reg_stat[regno].last_set_value = value;
10712
10713 if (value)
10714 {
10715 enum machine_mode mode = GET_MODE (reg);
10716 subst_low_cuid = INSN_CUID (insn);
10717 reg_stat[regno].last_set_mode = mode;
10718 if (GET_MODE_CLASS (mode) == MODE_INT
10719 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10720 mode = nonzero_bits_mode;
10721 reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
10722 reg_stat[regno].last_set_sign_bit_copies
10723 = num_sign_bit_copies (value, GET_MODE (reg));
10724 }
10725 }
10726
10727 /* Called via note_stores from record_dead_and_set_regs to handle one
10728 SET or CLOBBER in an insn. DATA is the instruction in which the
10729 set is occurring. */
10730
10731 static void
10732 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
10733 {
10734 rtx record_dead_insn = (rtx) data;
10735
10736 if (GET_CODE (dest) == SUBREG)
10737 dest = SUBREG_REG (dest);
10738
10739 if (GET_CODE (dest) == REG)
10740 {
10741 /* If we are setting the whole register, we know its value. Otherwise
10742 show that we don't know the value. We can handle SUBREG in
10743 some cases. */
10744 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10745 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10746 else if (GET_CODE (setter) == SET
10747 && GET_CODE (SET_DEST (setter)) == SUBREG
10748 && SUBREG_REG (SET_DEST (setter)) == dest
10749 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10750 && subreg_lowpart_p (SET_DEST (setter)))
10751 record_value_for_reg (dest, record_dead_insn,
10752 gen_lowpart (GET_MODE (dest),
10753 SET_SRC (setter)));
10754 else
10755 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10756 }
10757 else if (GET_CODE (dest) == MEM
10758 /* Ignore pushes, they clobber nothing. */
10759 && ! push_operand (dest, GET_MODE (dest)))
10760 mem_last_set = INSN_CUID (record_dead_insn);
10761 }
10762
10763 /* Update the records of when each REG was most recently set or killed
10764 for the things done by INSN. This is the last thing done in processing
10765 INSN in the combiner loop.
10766
10767 We update reg_stat[], in particular fields last_set, last_set_value,
10768 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
10769 last_death, and also the similar information mem_last_set (which insn
10770 most recently modified memory) and last_call_cuid (which insn was the
10771 most recent subroutine call). */
10772
10773 static void
10774 record_dead_and_set_regs (rtx insn)
10775 {
10776 rtx link;
10777 unsigned int i;
10778
10779 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10780 {
10781 if (REG_NOTE_KIND (link) == REG_DEAD
10782 && GET_CODE (XEXP (link, 0)) == REG)
10783 {
10784 unsigned int regno = REGNO (XEXP (link, 0));
10785 unsigned int endregno
10786 = regno + (regno < FIRST_PSEUDO_REGISTER
10787 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
10788 : 1);
10789
10790 for (i = regno; i < endregno; i++)
10791 reg_stat[i].last_death = insn;
10792 }
10793 else if (REG_NOTE_KIND (link) == REG_INC)
10794 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10795 }
10796
10797 if (GET_CODE (insn) == CALL_INSN)
10798 {
10799 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10800 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
10801 {
10802 reg_stat[i].last_set_value = 0;
10803 reg_stat[i].last_set_mode = 0;
10804 reg_stat[i].last_set_nonzero_bits = 0;
10805 reg_stat[i].last_set_sign_bit_copies = 0;
10806 reg_stat[i].last_death = 0;
10807 }
10808
10809 last_call_cuid = mem_last_set = INSN_CUID (insn);
10810
10811 /* Don't bother recording what this insn does. It might set the
10812 return value register, but we can't combine into a call
10813 pattern anyway, so there's no point trying (and it may cause
10814 a crash, if e.g. we wind up asking for last_set_value of a
10815 SUBREG of the return value register). */
10816 return;
10817 }
10818
10819 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
10820 }
10821
10822 /* If a SUBREG has the promoted bit set, it is in fact a property of the
10823 register present in the SUBREG, so for each such SUBREG go back and
10824 adjust nonzero and sign bit information of the registers that are
10825 known to have some zero/sign bits set.
10826
10827 This is needed because when combine blows the SUBREGs away, the
10828 information on zero/sign bits is lost and further combines can be
10829 missed because of that. */
10830
10831 static void
10832 record_promoted_value (rtx insn, rtx subreg)
10833 {
10834 rtx links, set;
10835 unsigned int regno = REGNO (SUBREG_REG (subreg));
10836 enum machine_mode mode = GET_MODE (subreg);
10837
10838 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
10839 return;
10840
10841 for (links = LOG_LINKS (insn); links;)
10842 {
10843 insn = XEXP (links, 0);
10844 set = single_set (insn);
10845
10846 if (! set || GET_CODE (SET_DEST (set)) != REG
10847 || REGNO (SET_DEST (set)) != regno
10848 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
10849 {
10850 links = XEXP (links, 1);
10851 continue;
10852 }
10853
10854 if (reg_stat[regno].last_set == insn)
10855 {
10856 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
10857 reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
10858 }
10859
10860 if (GET_CODE (SET_SRC (set)) == REG)
10861 {
10862 regno = REGNO (SET_SRC (set));
10863 links = LOG_LINKS (insn);
10864 }
10865 else
10866 break;
10867 }
10868 }
10869
10870 /* Scan X for promoted SUBREGs. For each one found,
10871 note what it implies to the registers used in it. */
10872
10873 static void
10874 check_promoted_subreg (rtx insn, rtx x)
10875 {
10876 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
10877 && GET_CODE (SUBREG_REG (x)) == REG)
10878 record_promoted_value (insn, x);
10879 else
10880 {
10881 const char *format = GET_RTX_FORMAT (GET_CODE (x));
10882 int i, j;
10883
10884 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
10885 switch (format[i])
10886 {
10887 case 'e':
10888 check_promoted_subreg (insn, XEXP (x, i));
10889 break;
10890 case 'V':
10891 case 'E':
10892 if (XVEC (x, i) != 0)
10893 for (j = 0; j < XVECLEN (x, i); j++)
10894 check_promoted_subreg (insn, XVECEXP (x, i, j));
10895 break;
10896 }
10897 }
10898 }
10899 \f
10900 /* Utility routine for the following function. Verify that all the registers
10901 mentioned in *LOC are valid when *LOC was part of a value set when
10902 label_tick == TICK. Return 0 if some are not.
10903
10904 If REPLACE is nonzero, replace the invalid reference with
10905 (clobber (const_int 0)) and return 1. This replacement is useful because
10906 we often can get useful information about the form of a value (e.g., if
10907 it was produced by a shift that always produces -1 or 0) even though
10908 we don't know exactly what registers it was produced from. */
10909
10910 static int
10911 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
10912 {
10913 rtx x = *loc;
10914 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10915 int len = GET_RTX_LENGTH (GET_CODE (x));
10916 int i;
10917
10918 if (GET_CODE (x) == REG)
10919 {
10920 unsigned int regno = REGNO (x);
10921 unsigned int endregno
10922 = regno + (regno < FIRST_PSEUDO_REGISTER
10923 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10924 unsigned int j;
10925
10926 for (j = regno; j < endregno; j++)
10927 if (reg_stat[j].last_set_invalid
10928 /* If this is a pseudo-register that was only set once and not
10929 live at the beginning of the function, it is always valid. */
10930 || (! (regno >= FIRST_PSEUDO_REGISTER
10931 && REG_N_SETS (regno) == 1
10932 && (! REGNO_REG_SET_P
10933 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
10934 && reg_stat[j].last_set_label > tick))
10935 {
10936 if (replace)
10937 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10938 return replace;
10939 }
10940
10941 return 1;
10942 }
10943 /* If this is a memory reference, make sure that there were
10944 no stores after it that might have clobbered the value. We don't
10945 have alias info, so we assume any store invalidates it. */
10946 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10947 && INSN_CUID (insn) <= mem_last_set)
10948 {
10949 if (replace)
10950 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10951 return replace;
10952 }
10953
10954 for (i = 0; i < len; i++)
10955 {
10956 if (fmt[i] == 'e')
10957 {
10958 /* Check for identical subexpressions. If x contains
10959 identical subexpression we only have to traverse one of
10960 them. */
10961 if (i == 1 && ARITHMETIC_P (x))
10962 {
10963 /* Note that at this point x0 has already been checked
10964 and found valid. */
10965 rtx x0 = XEXP (x, 0);
10966 rtx x1 = XEXP (x, 1);
10967
10968 /* If x0 and x1 are identical then x is also valid. */
10969 if (x0 == x1)
10970 return 1;
10971
10972 /* If x1 is identical to a subexpression of x0 then
10973 while checking x0, x1 has already been checked. Thus
10974 it is valid and so as x. */
10975 if (ARITHMETIC_P (x0)
10976 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10977 return 1;
10978
10979 /* If x0 is identical to a subexpression of x1 then x is
10980 valid iff the rest of x1 is valid. */
10981 if (ARITHMETIC_P (x1)
10982 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10983 return
10984 get_last_value_validate (&XEXP (x1,
10985 x0 == XEXP (x1, 0) ? 1 : 0),
10986 insn, tick, replace);
10987 }
10988
10989 if (get_last_value_validate (&XEXP (x, i), insn, tick,
10990 replace) == 0)
10991 return 0;
10992 }
10993 /* Don't bother with these. They shouldn't occur anyway. */
10994 else if (fmt[i] == 'E')
10995 return 0;
10996 }
10997
10998 /* If we haven't found a reason for it to be invalid, it is valid. */
10999 return 1;
11000 }
11001
11002 /* Get the last value assigned to X, if known. Some registers
11003 in the value may be replaced with (clobber (const_int 0)) if their value
11004 is known longer known reliably. */
11005
11006 static rtx
11007 get_last_value (rtx x)
11008 {
11009 unsigned int regno;
11010 rtx value;
11011
11012 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11013 then convert it to the desired mode. If this is a paradoxical SUBREG,
11014 we cannot predict what values the "extra" bits might have. */
11015 if (GET_CODE (x) == SUBREG
11016 && subreg_lowpart_p (x)
11017 && (GET_MODE_SIZE (GET_MODE (x))
11018 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11019 && (value = get_last_value (SUBREG_REG (x))) != 0)
11020 return gen_lowpart (GET_MODE (x), value);
11021
11022 if (GET_CODE (x) != REG)
11023 return 0;
11024
11025 regno = REGNO (x);
11026 value = reg_stat[regno].last_set_value;
11027
11028 /* If we don't have a value, or if it isn't for this basic block and
11029 it's either a hard register, set more than once, or it's a live
11030 at the beginning of the function, return 0.
11031
11032 Because if it's not live at the beginning of the function then the reg
11033 is always set before being used (is never used without being set).
11034 And, if it's set only once, and it's always set before use, then all
11035 uses must have the same last value, even if it's not from this basic
11036 block. */
11037
11038 if (value == 0
11039 || (reg_stat[regno].last_set_label != label_tick
11040 && (regno < FIRST_PSEUDO_REGISTER
11041 || REG_N_SETS (regno) != 1
11042 || (REGNO_REG_SET_P
11043 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11044 return 0;
11045
11046 /* If the value was set in a later insn than the ones we are processing,
11047 we can't use it even if the register was only set once. */
11048 if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11049 return 0;
11050
11051 /* If the value has all its registers valid, return it. */
11052 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11053 reg_stat[regno].last_set_label, 0))
11054 return value;
11055
11056 /* Otherwise, make a copy and replace any invalid register with
11057 (clobber (const_int 0)). If that fails for some reason, return 0. */
11058
11059 value = copy_rtx (value);
11060 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11061 reg_stat[regno].last_set_label, 1))
11062 return value;
11063
11064 return 0;
11065 }
11066 \f
11067 /* Return nonzero if expression X refers to a REG or to memory
11068 that is set in an instruction more recent than FROM_CUID. */
11069
11070 static int
11071 use_crosses_set_p (rtx x, int from_cuid)
11072 {
11073 const char *fmt;
11074 int i;
11075 enum rtx_code code = GET_CODE (x);
11076
11077 if (code == REG)
11078 {
11079 unsigned int regno = REGNO (x);
11080 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11081 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11082
11083 #ifdef PUSH_ROUNDING
11084 /* Don't allow uses of the stack pointer to be moved,
11085 because we don't know whether the move crosses a push insn. */
11086 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11087 return 1;
11088 #endif
11089 for (; regno < endreg; regno++)
11090 if (reg_stat[regno].last_set
11091 && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11092 return 1;
11093 return 0;
11094 }
11095
11096 if (code == MEM && mem_last_set > from_cuid)
11097 return 1;
11098
11099 fmt = GET_RTX_FORMAT (code);
11100
11101 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11102 {
11103 if (fmt[i] == 'E')
11104 {
11105 int j;
11106 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11107 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11108 return 1;
11109 }
11110 else if (fmt[i] == 'e'
11111 && use_crosses_set_p (XEXP (x, i), from_cuid))
11112 return 1;
11113 }
11114 return 0;
11115 }
11116 \f
11117 /* Define three variables used for communication between the following
11118 routines. */
11119
11120 static unsigned int reg_dead_regno, reg_dead_endregno;
11121 static int reg_dead_flag;
11122
11123 /* Function called via note_stores from reg_dead_at_p.
11124
11125 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11126 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11127
11128 static void
11129 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11130 {
11131 unsigned int regno, endregno;
11132
11133 if (GET_CODE (dest) != REG)
11134 return;
11135
11136 regno = REGNO (dest);
11137 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11138 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11139
11140 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11141 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11142 }
11143
11144 /* Return nonzero if REG is known to be dead at INSN.
11145
11146 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11147 referencing REG, it is dead. If we hit a SET referencing REG, it is
11148 live. Otherwise, see if it is live or dead at the start of the basic
11149 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11150 must be assumed to be always live. */
11151
11152 static int
11153 reg_dead_at_p (rtx reg, rtx insn)
11154 {
11155 basic_block block;
11156 unsigned int i;
11157
11158 /* Set variables for reg_dead_at_p_1. */
11159 reg_dead_regno = REGNO (reg);
11160 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11161 ? hard_regno_nregs[reg_dead_regno]
11162 [GET_MODE (reg)]
11163 : 1);
11164
11165 reg_dead_flag = 0;
11166
11167 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11168 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11169 {
11170 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11171 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11172 return 0;
11173 }
11174
11175 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11176 beginning of function. */
11177 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11178 insn = prev_nonnote_insn (insn))
11179 {
11180 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11181 if (reg_dead_flag)
11182 return reg_dead_flag == 1 ? 1 : 0;
11183
11184 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11185 return 1;
11186 }
11187
11188 /* Get the basic block that we were in. */
11189 if (insn == 0)
11190 block = ENTRY_BLOCK_PTR->next_bb;
11191 else
11192 {
11193 FOR_EACH_BB (block)
11194 if (insn == BB_HEAD (block))
11195 break;
11196
11197 if (block == EXIT_BLOCK_PTR)
11198 return 0;
11199 }
11200
11201 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11202 if (REGNO_REG_SET_P (block->global_live_at_start, i))
11203 return 0;
11204
11205 return 1;
11206 }
11207 \f
11208 /* Note hard registers in X that are used. This code is similar to
11209 that in flow.c, but much simpler since we don't care about pseudos. */
11210
11211 static void
11212 mark_used_regs_combine (rtx x)
11213 {
11214 RTX_CODE code = GET_CODE (x);
11215 unsigned int regno;
11216 int i;
11217
11218 switch (code)
11219 {
11220 case LABEL_REF:
11221 case SYMBOL_REF:
11222 case CONST_INT:
11223 case CONST:
11224 case CONST_DOUBLE:
11225 case CONST_VECTOR:
11226 case PC:
11227 case ADDR_VEC:
11228 case ADDR_DIFF_VEC:
11229 case ASM_INPUT:
11230 #ifdef HAVE_cc0
11231 /* CC0 must die in the insn after it is set, so we don't need to take
11232 special note of it here. */
11233 case CC0:
11234 #endif
11235 return;
11236
11237 case CLOBBER:
11238 /* If we are clobbering a MEM, mark any hard registers inside the
11239 address as used. */
11240 if (GET_CODE (XEXP (x, 0)) == MEM)
11241 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11242 return;
11243
11244 case REG:
11245 regno = REGNO (x);
11246 /* A hard reg in a wide mode may really be multiple registers.
11247 If so, mark all of them just like the first. */
11248 if (regno < FIRST_PSEUDO_REGISTER)
11249 {
11250 unsigned int endregno, r;
11251
11252 /* None of this applies to the stack, frame or arg pointers. */
11253 if (regno == STACK_POINTER_REGNUM
11254 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11255 || regno == HARD_FRAME_POINTER_REGNUM
11256 #endif
11257 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11258 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11259 #endif
11260 || regno == FRAME_POINTER_REGNUM)
11261 return;
11262
11263 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11264 for (r = regno; r < endregno; r++)
11265 SET_HARD_REG_BIT (newpat_used_regs, r);
11266 }
11267 return;
11268
11269 case SET:
11270 {
11271 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11272 the address. */
11273 rtx testreg = SET_DEST (x);
11274
11275 while (GET_CODE (testreg) == SUBREG
11276 || GET_CODE (testreg) == ZERO_EXTRACT
11277 || GET_CODE (testreg) == SIGN_EXTRACT
11278 || GET_CODE (testreg) == STRICT_LOW_PART)
11279 testreg = XEXP (testreg, 0);
11280
11281 if (GET_CODE (testreg) == MEM)
11282 mark_used_regs_combine (XEXP (testreg, 0));
11283
11284 mark_used_regs_combine (SET_SRC (x));
11285 }
11286 return;
11287
11288 default:
11289 break;
11290 }
11291
11292 /* Recursively scan the operands of this expression. */
11293
11294 {
11295 const char *fmt = GET_RTX_FORMAT (code);
11296
11297 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11298 {
11299 if (fmt[i] == 'e')
11300 mark_used_regs_combine (XEXP (x, i));
11301 else if (fmt[i] == 'E')
11302 {
11303 int j;
11304
11305 for (j = 0; j < XVECLEN (x, i); j++)
11306 mark_used_regs_combine (XVECEXP (x, i, j));
11307 }
11308 }
11309 }
11310 }
11311 \f
11312 /* Remove register number REGNO from the dead registers list of INSN.
11313
11314 Return the note used to record the death, if there was one. */
11315
11316 rtx
11317 remove_death (unsigned int regno, rtx insn)
11318 {
11319 rtx note = find_regno_note (insn, REG_DEAD, regno);
11320
11321 if (note)
11322 {
11323 REG_N_DEATHS (regno)--;
11324 remove_note (insn, note);
11325 }
11326
11327 return note;
11328 }
11329
11330 /* For each register (hardware or pseudo) used within expression X, if its
11331 death is in an instruction with cuid between FROM_CUID (inclusive) and
11332 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11333 list headed by PNOTES.
11334
11335 That said, don't move registers killed by maybe_kill_insn.
11336
11337 This is done when X is being merged by combination into TO_INSN. These
11338 notes will then be distributed as needed. */
11339
11340 static void
11341 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11342 rtx *pnotes)
11343 {
11344 const char *fmt;
11345 int len, i;
11346 enum rtx_code code = GET_CODE (x);
11347
11348 if (code == REG)
11349 {
11350 unsigned int regno = REGNO (x);
11351 rtx where_dead = reg_stat[regno].last_death;
11352 rtx before_dead, after_dead;
11353
11354 /* Don't move the register if it gets killed in between from and to. */
11355 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11356 && ! reg_referenced_p (x, maybe_kill_insn))
11357 return;
11358
11359 /* WHERE_DEAD could be a USE insn made by combine, so first we
11360 make sure that we have insns with valid INSN_CUID values. */
11361 before_dead = where_dead;
11362 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11363 before_dead = PREV_INSN (before_dead);
11364
11365 after_dead = where_dead;
11366 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11367 after_dead = NEXT_INSN (after_dead);
11368
11369 if (before_dead && after_dead
11370 && INSN_CUID (before_dead) >= from_cuid
11371 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11372 || (where_dead != after_dead
11373 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11374 {
11375 rtx note = remove_death (regno, where_dead);
11376
11377 /* It is possible for the call above to return 0. This can occur
11378 when last_death points to I2 or I1 that we combined with.
11379 In that case make a new note.
11380
11381 We must also check for the case where X is a hard register
11382 and NOTE is a death note for a range of hard registers
11383 including X. In that case, we must put REG_DEAD notes for
11384 the remaining registers in place of NOTE. */
11385
11386 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11387 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11388 > GET_MODE_SIZE (GET_MODE (x))))
11389 {
11390 unsigned int deadregno = REGNO (XEXP (note, 0));
11391 unsigned int deadend
11392 = (deadregno + hard_regno_nregs[deadregno]
11393 [GET_MODE (XEXP (note, 0))]);
11394 unsigned int ourend
11395 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11396 unsigned int i;
11397
11398 for (i = deadregno; i < deadend; i++)
11399 if (i < regno || i >= ourend)
11400 REG_NOTES (where_dead)
11401 = gen_rtx_EXPR_LIST (REG_DEAD,
11402 regno_reg_rtx[i],
11403 REG_NOTES (where_dead));
11404 }
11405
11406 /* If we didn't find any note, or if we found a REG_DEAD note that
11407 covers only part of the given reg, and we have a multi-reg hard
11408 register, then to be safe we must check for REG_DEAD notes
11409 for each register other than the first. They could have
11410 their own REG_DEAD notes lying around. */
11411 else if ((note == 0
11412 || (note != 0
11413 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11414 < GET_MODE_SIZE (GET_MODE (x)))))
11415 && regno < FIRST_PSEUDO_REGISTER
11416 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11417 {
11418 unsigned int ourend
11419 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11420 unsigned int i, offset;
11421 rtx oldnotes = 0;
11422
11423 if (note)
11424 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11425 else
11426 offset = 1;
11427
11428 for (i = regno + offset; i < ourend; i++)
11429 move_deaths (regno_reg_rtx[i],
11430 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11431 }
11432
11433 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11434 {
11435 XEXP (note, 1) = *pnotes;
11436 *pnotes = note;
11437 }
11438 else
11439 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11440
11441 REG_N_DEATHS (regno)++;
11442 }
11443
11444 return;
11445 }
11446
11447 else if (GET_CODE (x) == SET)
11448 {
11449 rtx dest = SET_DEST (x);
11450
11451 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11452
11453 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11454 that accesses one word of a multi-word item, some
11455 piece of everything register in the expression is used by
11456 this insn, so remove any old death. */
11457 /* ??? So why do we test for equality of the sizes? */
11458
11459 if (GET_CODE (dest) == ZERO_EXTRACT
11460 || GET_CODE (dest) == STRICT_LOW_PART
11461 || (GET_CODE (dest) == SUBREG
11462 && (((GET_MODE_SIZE (GET_MODE (dest))
11463 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11464 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11465 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11466 {
11467 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11468 return;
11469 }
11470
11471 /* If this is some other SUBREG, we know it replaces the entire
11472 value, so use that as the destination. */
11473 if (GET_CODE (dest) == SUBREG)
11474 dest = SUBREG_REG (dest);
11475
11476 /* If this is a MEM, adjust deaths of anything used in the address.
11477 For a REG (the only other possibility), the entire value is
11478 being replaced so the old value is not used in this insn. */
11479
11480 if (GET_CODE (dest) == MEM)
11481 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11482 to_insn, pnotes);
11483 return;
11484 }
11485
11486 else if (GET_CODE (x) == CLOBBER)
11487 return;
11488
11489 len = GET_RTX_LENGTH (code);
11490 fmt = GET_RTX_FORMAT (code);
11491
11492 for (i = 0; i < len; i++)
11493 {
11494 if (fmt[i] == 'E')
11495 {
11496 int j;
11497 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11498 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11499 to_insn, pnotes);
11500 }
11501 else if (fmt[i] == 'e')
11502 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11503 }
11504 }
11505 \f
11506 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11507 pattern of an insn. X must be a REG. */
11508
11509 static int
11510 reg_bitfield_target_p (rtx x, rtx body)
11511 {
11512 int i;
11513
11514 if (GET_CODE (body) == SET)
11515 {
11516 rtx dest = SET_DEST (body);
11517 rtx target;
11518 unsigned int regno, tregno, endregno, endtregno;
11519
11520 if (GET_CODE (dest) == ZERO_EXTRACT)
11521 target = XEXP (dest, 0);
11522 else if (GET_CODE (dest) == STRICT_LOW_PART)
11523 target = SUBREG_REG (XEXP (dest, 0));
11524 else
11525 return 0;
11526
11527 if (GET_CODE (target) == SUBREG)
11528 target = SUBREG_REG (target);
11529
11530 if (GET_CODE (target) != REG)
11531 return 0;
11532
11533 tregno = REGNO (target), regno = REGNO (x);
11534 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11535 return target == x;
11536
11537 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11538 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11539
11540 return endregno > tregno && regno < endtregno;
11541 }
11542
11543 else if (GET_CODE (body) == PARALLEL)
11544 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11545 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11546 return 1;
11547
11548 return 0;
11549 }
11550 \f
11551 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11552 as appropriate. I3 and I2 are the insns resulting from the combination
11553 insns including FROM (I2 may be zero).
11554
11555 Each note in the list is either ignored or placed on some insns, depending
11556 on the type of note. */
11557
11558 static void
11559 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
11560 {
11561 rtx note, next_note;
11562 rtx tem;
11563
11564 for (note = notes; note; note = next_note)
11565 {
11566 rtx place = 0, place2 = 0;
11567
11568 /* If this NOTE references a pseudo register, ensure it references
11569 the latest copy of that register. */
11570 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11571 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11572 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11573
11574 next_note = XEXP (note, 1);
11575 switch (REG_NOTE_KIND (note))
11576 {
11577 case REG_BR_PROB:
11578 case REG_BR_PRED:
11579 /* Doesn't matter much where we put this, as long as it's somewhere.
11580 It is preferable to keep these notes on branches, which is most
11581 likely to be i3. */
11582 place = i3;
11583 break;
11584
11585 case REG_VALUE_PROFILE:
11586 /* Just get rid of this note, as it is unused later anyway. */
11587 break;
11588
11589 case REG_VTABLE_REF:
11590 /* ??? Should remain with *a particular* memory load. Given the
11591 nature of vtable data, the last insn seems relatively safe. */
11592 place = i3;
11593 break;
11594
11595 case REG_NON_LOCAL_GOTO:
11596 if (GET_CODE (i3) == JUMP_INSN)
11597 place = i3;
11598 else if (i2 && GET_CODE (i2) == JUMP_INSN)
11599 place = i2;
11600 else
11601 abort ();
11602 break;
11603
11604 case REG_EH_REGION:
11605 /* These notes must remain with the call or trapping instruction. */
11606 if (GET_CODE (i3) == CALL_INSN)
11607 place = i3;
11608 else if (i2 && GET_CODE (i2) == CALL_INSN)
11609 place = i2;
11610 else if (flag_non_call_exceptions)
11611 {
11612 if (may_trap_p (i3))
11613 place = i3;
11614 else if (i2 && may_trap_p (i2))
11615 place = i2;
11616 /* ??? Otherwise assume we've combined things such that we
11617 can now prove that the instructions can't trap. Drop the
11618 note in this case. */
11619 }
11620 else
11621 abort ();
11622 break;
11623
11624 case REG_ALWAYS_RETURN:
11625 case REG_NORETURN:
11626 case REG_SETJMP:
11627 /* These notes must remain with the call. It should not be
11628 possible for both I2 and I3 to be a call. */
11629 if (GET_CODE (i3) == CALL_INSN)
11630 place = i3;
11631 else if (i2 && GET_CODE (i2) == CALL_INSN)
11632 place = i2;
11633 else
11634 abort ();
11635 break;
11636
11637 case REG_UNUSED:
11638 /* Any clobbers for i3 may still exist, and so we must process
11639 REG_UNUSED notes from that insn.
11640
11641 Any clobbers from i2 or i1 can only exist if they were added by
11642 recog_for_combine. In that case, recog_for_combine created the
11643 necessary REG_UNUSED notes. Trying to keep any original
11644 REG_UNUSED notes from these insns can cause incorrect output
11645 if it is for the same register as the original i3 dest.
11646 In that case, we will notice that the register is set in i3,
11647 and then add a REG_UNUSED note for the destination of i3, which
11648 is wrong. However, it is possible to have REG_UNUSED notes from
11649 i2 or i1 for register which were both used and clobbered, so
11650 we keep notes from i2 or i1 if they will turn into REG_DEAD
11651 notes. */
11652
11653 /* If this register is set or clobbered in I3, put the note there
11654 unless there is one already. */
11655 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11656 {
11657 if (from_insn != i3)
11658 break;
11659
11660 if (! (GET_CODE (XEXP (note, 0)) == REG
11661 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11662 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11663 place = i3;
11664 }
11665 /* Otherwise, if this register is used by I3, then this register
11666 now dies here, so we must put a REG_DEAD note here unless there
11667 is one already. */
11668 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11669 && ! (GET_CODE (XEXP (note, 0)) == REG
11670 ? find_regno_note (i3, REG_DEAD,
11671 REGNO (XEXP (note, 0)))
11672 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11673 {
11674 PUT_REG_NOTE_KIND (note, REG_DEAD);
11675 place = i3;
11676 }
11677 break;
11678
11679 case REG_EQUAL:
11680 case REG_EQUIV:
11681 case REG_NOALIAS:
11682 /* These notes say something about results of an insn. We can
11683 only support them if they used to be on I3 in which case they
11684 remain on I3. Otherwise they are ignored.
11685
11686 If the note refers to an expression that is not a constant, we
11687 must also ignore the note since we cannot tell whether the
11688 equivalence is still true. It might be possible to do
11689 slightly better than this (we only have a problem if I2DEST
11690 or I1DEST is present in the expression), but it doesn't
11691 seem worth the trouble. */
11692
11693 if (from_insn == i3
11694 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11695 place = i3;
11696 break;
11697
11698 case REG_INC:
11699 case REG_NO_CONFLICT:
11700 /* These notes say something about how a register is used. They must
11701 be present on any use of the register in I2 or I3. */
11702 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11703 place = i3;
11704
11705 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11706 {
11707 if (place)
11708 place2 = i2;
11709 else
11710 place = i2;
11711 }
11712 break;
11713
11714 case REG_LABEL:
11715 /* This can show up in several ways -- either directly in the
11716 pattern, or hidden off in the constant pool with (or without?)
11717 a REG_EQUAL note. */
11718 /* ??? Ignore the without-reg_equal-note problem for now. */
11719 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11720 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11721 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11722 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11723 place = i3;
11724
11725 if (i2
11726 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11727 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11728 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11729 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11730 {
11731 if (place)
11732 place2 = i2;
11733 else
11734 place = i2;
11735 }
11736
11737 /* Don't attach REG_LABEL note to a JUMP_INSN which has
11738 JUMP_LABEL already. Instead, decrement LABEL_NUSES. */
11739 if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
11740 {
11741 if (JUMP_LABEL (place) != XEXP (note, 0))
11742 abort ();
11743 if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
11744 LABEL_NUSES (JUMP_LABEL (place))--;
11745 place = 0;
11746 }
11747 if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
11748 {
11749 if (JUMP_LABEL (place2) != XEXP (note, 0))
11750 abort ();
11751 if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
11752 LABEL_NUSES (JUMP_LABEL (place2))--;
11753 place2 = 0;
11754 }
11755 break;
11756
11757 case REG_NONNEG:
11758 /* This note says something about the value of a register prior
11759 to the execution of an insn. It is too much trouble to see
11760 if the note is still correct in all situations. It is better
11761 to simply delete it. */
11762 break;
11763
11764 case REG_RETVAL:
11765 /* If the insn previously containing this note still exists,
11766 put it back where it was. Otherwise move it to the previous
11767 insn. Adjust the corresponding REG_LIBCALL note. */
11768 if (GET_CODE (from_insn) != NOTE)
11769 place = from_insn;
11770 else
11771 {
11772 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11773 place = prev_real_insn (from_insn);
11774 if (tem && place)
11775 XEXP (tem, 0) = place;
11776 /* If we're deleting the last remaining instruction of a
11777 libcall sequence, don't add the notes. */
11778 else if (XEXP (note, 0) == from_insn)
11779 tem = place = 0;
11780 /* Don't add the dangling REG_RETVAL note. */
11781 else if (! tem)
11782 place = 0;
11783 }
11784 break;
11785
11786 case REG_LIBCALL:
11787 /* This is handled similarly to REG_RETVAL. */
11788 if (GET_CODE (from_insn) != NOTE)
11789 place = from_insn;
11790 else
11791 {
11792 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11793 place = next_real_insn (from_insn);
11794 if (tem && place)
11795 XEXP (tem, 0) = place;
11796 /* If we're deleting the last remaining instruction of a
11797 libcall sequence, don't add the notes. */
11798 else if (XEXP (note, 0) == from_insn)
11799 tem = place = 0;
11800 /* Don't add the dangling REG_LIBCALL note. */
11801 else if (! tem)
11802 place = 0;
11803 }
11804 break;
11805
11806 case REG_DEAD:
11807 /* If the register is used as an input in I3, it dies there.
11808 Similarly for I2, if it is nonzero and adjacent to I3.
11809
11810 If the register is not used as an input in either I3 or I2
11811 and it is not one of the registers we were supposed to eliminate,
11812 there are two possibilities. We might have a non-adjacent I2
11813 or we might have somehow eliminated an additional register
11814 from a computation. For example, we might have had A & B where
11815 we discover that B will always be zero. In this case we will
11816 eliminate the reference to A.
11817
11818 In both cases, we must search to see if we can find a previous
11819 use of A and put the death note there. */
11820
11821 if (from_insn
11822 && GET_CODE (from_insn) == CALL_INSN
11823 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11824 place = from_insn;
11825 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11826 place = i3;
11827 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11828 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11829 place = i2;
11830
11831 if (place == 0)
11832 {
11833 basic_block bb = this_basic_block;
11834
11835 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
11836 {
11837 if (! INSN_P (tem))
11838 {
11839 if (tem == BB_HEAD (bb))
11840 break;
11841 continue;
11842 }
11843
11844 /* If the register is being set at TEM, see if that is all
11845 TEM is doing. If so, delete TEM. Otherwise, make this
11846 into a REG_UNUSED note instead. */
11847 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11848 {
11849 rtx set = single_set (tem);
11850 rtx inner_dest = 0;
11851 #ifdef HAVE_cc0
11852 rtx cc0_setter = NULL_RTX;
11853 #endif
11854
11855 if (set != 0)
11856 for (inner_dest = SET_DEST (set);
11857 (GET_CODE (inner_dest) == STRICT_LOW_PART
11858 || GET_CODE (inner_dest) == SUBREG
11859 || GET_CODE (inner_dest) == ZERO_EXTRACT);
11860 inner_dest = XEXP (inner_dest, 0))
11861 ;
11862
11863 /* Verify that it was the set, and not a clobber that
11864 modified the register.
11865
11866 CC0 targets must be careful to maintain setter/user
11867 pairs. If we cannot delete the setter due to side
11868 effects, mark the user with an UNUSED note instead
11869 of deleting it. */
11870
11871 if (set != 0 && ! side_effects_p (SET_SRC (set))
11872 && rtx_equal_p (XEXP (note, 0), inner_dest)
11873 #ifdef HAVE_cc0
11874 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11875 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11876 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11877 #endif
11878 )
11879 {
11880 /* Move the notes and links of TEM elsewhere.
11881 This might delete other dead insns recursively.
11882 First set the pattern to something that won't use
11883 any register. */
11884 rtx old_notes = REG_NOTES (tem);
11885
11886 PATTERN (tem) = pc_rtx;
11887 REG_NOTES (tem) = NULL;
11888
11889 distribute_notes (old_notes, tem, tem, NULL_RTX);
11890 distribute_links (LOG_LINKS (tem));
11891
11892 PUT_CODE (tem, NOTE);
11893 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11894 NOTE_SOURCE_FILE (tem) = 0;
11895
11896 #ifdef HAVE_cc0
11897 /* Delete the setter too. */
11898 if (cc0_setter)
11899 {
11900 PATTERN (cc0_setter) = pc_rtx;
11901 old_notes = REG_NOTES (cc0_setter);
11902 REG_NOTES (cc0_setter) = NULL;
11903
11904 distribute_notes (old_notes, cc0_setter,
11905 cc0_setter, NULL_RTX);
11906 distribute_links (LOG_LINKS (cc0_setter));
11907
11908 PUT_CODE (cc0_setter, NOTE);
11909 NOTE_LINE_NUMBER (cc0_setter)
11910 = NOTE_INSN_DELETED;
11911 NOTE_SOURCE_FILE (cc0_setter) = 0;
11912 }
11913 #endif
11914 }
11915 else
11916 {
11917 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11918
11919 /* If there isn't already a REG_UNUSED note, put one
11920 here. Do not place a REG_DEAD note, even if
11921 the register is also used here; that would not
11922 match the algorithm used in lifetime analysis
11923 and can cause the consistency check in the
11924 scheduler to fail. */
11925 if (! find_regno_note (tem, REG_UNUSED,
11926 REGNO (XEXP (note, 0))))
11927 place = tem;
11928 break;
11929 }
11930 }
11931 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11932 || (GET_CODE (tem) == CALL_INSN
11933 && find_reg_fusage (tem, USE, XEXP (note, 0))))
11934 {
11935 place = tem;
11936
11937 /* If we are doing a 3->2 combination, and we have a
11938 register which formerly died in i3 and was not used
11939 by i2, which now no longer dies in i3 and is used in
11940 i2 but does not die in i2, and place is between i2
11941 and i3, then we may need to move a link from place to
11942 i2. */
11943 if (i2 && INSN_UID (place) <= max_uid_cuid
11944 && INSN_CUID (place) > INSN_CUID (i2)
11945 && from_insn
11946 && INSN_CUID (from_insn) > INSN_CUID (i2)
11947 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11948 {
11949 rtx links = LOG_LINKS (place);
11950 LOG_LINKS (place) = 0;
11951 distribute_links (links);
11952 }
11953 break;
11954 }
11955
11956 if (tem == BB_HEAD (bb))
11957 break;
11958 }
11959
11960 /* We haven't found an insn for the death note and it
11961 is still a REG_DEAD note, but we have hit the beginning
11962 of the block. If the existing life info says the reg
11963 was dead, there's nothing left to do. Otherwise, we'll
11964 need to do a global life update after combine. */
11965 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
11966 && REGNO_REG_SET_P (bb->global_live_at_start,
11967 REGNO (XEXP (note, 0))))
11968 SET_BIT (refresh_blocks, this_basic_block->index);
11969 }
11970
11971 /* If the register is set or already dead at PLACE, we needn't do
11972 anything with this note if it is still a REG_DEAD note.
11973 We can here if it is set at all, not if is it totally replace,
11974 which is what `dead_or_set_p' checks, so also check for it being
11975 set partially. */
11976
11977 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11978 {
11979 unsigned int regno = REGNO (XEXP (note, 0));
11980
11981 /* Similarly, if the instruction on which we want to place
11982 the note is a noop, we'll need do a global live update
11983 after we remove them in delete_noop_moves. */
11984 if (noop_move_p (place))
11985 SET_BIT (refresh_blocks, this_basic_block->index);
11986
11987 if (dead_or_set_p (place, XEXP (note, 0))
11988 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11989 {
11990 /* Unless the register previously died in PLACE, clear
11991 last_death. [I no longer understand why this is
11992 being done.] */
11993 if (reg_stat[regno].last_death != place)
11994 reg_stat[regno].last_death = 0;
11995 place = 0;
11996 }
11997 else
11998 reg_stat[regno].last_death = place;
11999
12000 /* If this is a death note for a hard reg that is occupying
12001 multiple registers, ensure that we are still using all
12002 parts of the object. If we find a piece of the object
12003 that is unused, we must arrange for an appropriate REG_DEAD
12004 note to be added for it. However, we can't just emit a USE
12005 and tag the note to it, since the register might actually
12006 be dead; so we recourse, and the recursive call then finds
12007 the previous insn that used this register. */
12008
12009 if (place && regno < FIRST_PSEUDO_REGISTER
12010 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12011 {
12012 unsigned int endregno
12013 = regno + hard_regno_nregs[regno]
12014 [GET_MODE (XEXP (note, 0))];
12015 int all_used = 1;
12016 unsigned int i;
12017
12018 for (i = regno; i < endregno; i++)
12019 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12020 && ! find_regno_fusage (place, USE, i))
12021 || dead_or_set_regno_p (place, i))
12022 all_used = 0;
12023
12024 if (! all_used)
12025 {
12026 /* Put only REG_DEAD notes for pieces that are
12027 not already dead or set. */
12028
12029 for (i = regno; i < endregno;
12030 i += hard_regno_nregs[i][reg_raw_mode[i]])
12031 {
12032 rtx piece = regno_reg_rtx[i];
12033 basic_block bb = this_basic_block;
12034
12035 if (! dead_or_set_p (place, piece)
12036 && ! reg_bitfield_target_p (piece,
12037 PATTERN (place)))
12038 {
12039 rtx new_note
12040 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12041
12042 distribute_notes (new_note, place, place,
12043 NULL_RTX);
12044 }
12045 else if (! refers_to_regno_p (i, i + 1,
12046 PATTERN (place), 0)
12047 && ! find_regno_fusage (place, USE, i))
12048 for (tem = PREV_INSN (place); ;
12049 tem = PREV_INSN (tem))
12050 {
12051 if (! INSN_P (tem))
12052 {
12053 if (tem == BB_HEAD (bb))
12054 {
12055 SET_BIT (refresh_blocks,
12056 this_basic_block->index);
12057 break;
12058 }
12059 continue;
12060 }
12061 if (dead_or_set_p (tem, piece)
12062 || reg_bitfield_target_p (piece,
12063 PATTERN (tem)))
12064 {
12065 REG_NOTES (tem)
12066 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12067 REG_NOTES (tem));
12068 break;
12069 }
12070 }
12071
12072 }
12073
12074 place = 0;
12075 }
12076 }
12077 }
12078 break;
12079
12080 default:
12081 /* Any other notes should not be present at this point in the
12082 compilation. */
12083 abort ();
12084 }
12085
12086 if (place)
12087 {
12088 XEXP (note, 1) = REG_NOTES (place);
12089 REG_NOTES (place) = note;
12090 }
12091 else if ((REG_NOTE_KIND (note) == REG_DEAD
12092 || REG_NOTE_KIND (note) == REG_UNUSED)
12093 && GET_CODE (XEXP (note, 0)) == REG)
12094 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12095
12096 if (place2)
12097 {
12098 if ((REG_NOTE_KIND (note) == REG_DEAD
12099 || REG_NOTE_KIND (note) == REG_UNUSED)
12100 && GET_CODE (XEXP (note, 0)) == REG)
12101 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12102
12103 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12104 REG_NOTE_KIND (note),
12105 XEXP (note, 0),
12106 REG_NOTES (place2));
12107 }
12108 }
12109 }
12110 \f
12111 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12112 I3, I2, and I1 to new locations. This is also called to add a link
12113 pointing at I3 when I3's destination is changed. */
12114
12115 static void
12116 distribute_links (rtx links)
12117 {
12118 rtx link, next_link;
12119
12120 for (link = links; link; link = next_link)
12121 {
12122 rtx place = 0;
12123 rtx insn;
12124 rtx set, reg;
12125
12126 next_link = XEXP (link, 1);
12127
12128 /* If the insn that this link points to is a NOTE or isn't a single
12129 set, ignore it. In the latter case, it isn't clear what we
12130 can do other than ignore the link, since we can't tell which
12131 register it was for. Such links wouldn't be used by combine
12132 anyway.
12133
12134 It is not possible for the destination of the target of the link to
12135 have been changed by combine. The only potential of this is if we
12136 replace I3, I2, and I1 by I3 and I2. But in that case the
12137 destination of I2 also remains unchanged. */
12138
12139 if (GET_CODE (XEXP (link, 0)) == NOTE
12140 || (set = single_set (XEXP (link, 0))) == 0)
12141 continue;
12142
12143 reg = SET_DEST (set);
12144 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12145 || GET_CODE (reg) == SIGN_EXTRACT
12146 || GET_CODE (reg) == STRICT_LOW_PART)
12147 reg = XEXP (reg, 0);
12148
12149 /* A LOG_LINK is defined as being placed on the first insn that uses
12150 a register and points to the insn that sets the register. Start
12151 searching at the next insn after the target of the link and stop
12152 when we reach a set of the register or the end of the basic block.
12153
12154 Note that this correctly handles the link that used to point from
12155 I3 to I2. Also note that not much searching is typically done here
12156 since most links don't point very far away. */
12157
12158 for (insn = NEXT_INSN (XEXP (link, 0));
12159 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12160 || BB_HEAD (this_basic_block->next_bb) != insn));
12161 insn = NEXT_INSN (insn))
12162 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12163 {
12164 if (reg_referenced_p (reg, PATTERN (insn)))
12165 place = insn;
12166 break;
12167 }
12168 else if (GET_CODE (insn) == CALL_INSN
12169 && find_reg_fusage (insn, USE, reg))
12170 {
12171 place = insn;
12172 break;
12173 }
12174 else if (INSN_P (insn) && reg_set_p (reg, insn))
12175 break;
12176
12177 /* If we found a place to put the link, place it there unless there
12178 is already a link to the same insn as LINK at that point. */
12179
12180 if (place)
12181 {
12182 rtx link2;
12183
12184 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12185 if (XEXP (link2, 0) == XEXP (link, 0))
12186 break;
12187
12188 if (link2 == 0)
12189 {
12190 XEXP (link, 1) = LOG_LINKS (place);
12191 LOG_LINKS (place) = link;
12192
12193 /* Set added_links_insn to the earliest insn we added a
12194 link to. */
12195 if (added_links_insn == 0
12196 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12197 added_links_insn = place;
12198 }
12199 }
12200 }
12201 }
12202 \f
12203 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12204 Check whether the expression pointer to by LOC is a register or
12205 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12206 Otherwise return zero. */
12207
12208 static int
12209 unmentioned_reg_p_1 (rtx *loc, void *expr)
12210 {
12211 rtx x = *loc;
12212
12213 if (x != NULL_RTX
12214 && (GET_CODE (x) == REG || GET_CODE (x) == MEM)
12215 && ! reg_mentioned_p (x, (rtx) expr))
12216 return 1;
12217 return 0;
12218 }
12219
12220 /* Check for any register or memory mentioned in EQUIV that is not
12221 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12222 of EXPR where some registers may have been replaced by constants. */
12223
12224 static bool
12225 unmentioned_reg_p (rtx equiv, rtx expr)
12226 {
12227 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12228 }
12229 \f
12230 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12231
12232 static int
12233 insn_cuid (rtx insn)
12234 {
12235 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12236 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12237 insn = NEXT_INSN (insn);
12238
12239 if (INSN_UID (insn) > max_uid_cuid)
12240 abort ();
12241
12242 return INSN_CUID (insn);
12243 }
12244 \f
12245 void
12246 dump_combine_stats (FILE *file)
12247 {
12248 fnotice
12249 (file,
12250 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12251 combine_attempts, combine_merges, combine_extras, combine_successes);
12252 }
12253
12254 void
12255 dump_combine_total_stats (FILE *file)
12256 {
12257 fnotice
12258 (file,
12259 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12260 total_attempts, total_merges, total_extras, total_successes);
12261 }