]> gcc.gnu.org Git - gcc.git/blob - gcc/combine.c
re PR target/20322 (Miscompilation of libcpp/expr.c at -O2+)
[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, 2005 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 "optabs.h"
94 #include "insn-codes.h"
95 #include "rtlhooks-def.h"
96 /* Include output.h for dump_file. */
97 #include "output.h"
98 #include "params.h"
99
100 /* Number of attempts to combine instructions in this function. */
101
102 static int combine_attempts;
103
104 /* Number of attempts that got as far as substitution in this function. */
105
106 static int combine_merges;
107
108 /* Number of instructions combined with added SETs in this function. */
109
110 static int combine_extras;
111
112 /* Number of instructions combined in this function. */
113
114 static int combine_successes;
115
116 /* Totals over entire compilation. */
117
118 static int total_attempts, total_merges, total_extras, total_successes;
119
120 \f
121 /* Vector mapping INSN_UIDs to cuids.
122 The cuids are like uids but increase monotonically always.
123 Combine always uses cuids so that it can compare them.
124 But actually renumbering the uids, which we used to do,
125 proves to be a bad idea because it makes it hard to compare
126 the dumps produced by earlier passes with those from later passes. */
127
128 static int *uid_cuid;
129 static int max_uid_cuid;
130
131 /* Get the cuid of an insn. */
132
133 #define INSN_CUID(INSN) \
134 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
135
136 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
137 BITS_PER_WORD would invoke undefined behavior. Work around it. */
138
139 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
140 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
141
142 /* Maximum register number, which is the size of the tables below. */
143
144 static unsigned int combine_max_regno;
145
146 struct reg_stat {
147 /* Record last point of death of (hard or pseudo) register n. */
148 rtx last_death;
149
150 /* Record last point of modification of (hard or pseudo) register n. */
151 rtx last_set;
152
153 /* The next group of fields allows the recording of the last value assigned
154 to (hard or pseudo) register n. We use this information to see if an
155 operation being processed is redundant given a prior operation performed
156 on the register. For example, an `and' with a constant is redundant if
157 all the zero bits are already known to be turned off.
158
159 We use an approach similar to that used by cse, but change it in the
160 following ways:
161
162 (1) We do not want to reinitialize at each label.
163 (2) It is useful, but not critical, to know the actual value assigned
164 to a register. Often just its form is helpful.
165
166 Therefore, we maintain the following fields:
167
168 last_set_value the last value assigned
169 last_set_label records the value of label_tick when the
170 register was assigned
171 last_set_table_tick records the value of label_tick when a
172 value using the register is assigned
173 last_set_invalid set to nonzero when it is not valid
174 to use the value of this register in some
175 register's value
176
177 To understand the usage of these tables, it is important to understand
178 the distinction between the value in last_set_value being valid and
179 the register being validly contained in some other expression in the
180 table.
181
182 (The next two parameters are out of date).
183
184 reg_stat[i].last_set_value is valid if it is nonzero, and either
185 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186
187 Register I may validly appear in any expression returned for the value
188 of another register if reg_n_sets[i] is 1. It may also appear in the
189 value for register J if reg_stat[j].last_set_invalid is zero, or
190 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191
192 If an expression is found in the table containing a register which may
193 not validly appear in an expression, the register is replaced by
194 something that won't match, (clobber (const_int 0)). */
195
196 /* Record last value assigned to (hard or pseudo) register n. */
197
198 rtx last_set_value;
199
200 /* Record the value of label_tick when an expression involving register n
201 is placed in last_set_value. */
202
203 int last_set_table_tick;
204
205 /* Record the value of label_tick when the value for register n is placed in
206 last_set_value. */
207
208 int last_set_label;
209
210 /* These fields are maintained in parallel with last_set_value and are
211 used to store the mode in which the register was last set, the bits
212 that were known to be zero when it was last set, and the number of
213 sign bits copies it was known to have when it was last set. */
214
215 unsigned HOST_WIDE_INT last_set_nonzero_bits;
216 char last_set_sign_bit_copies;
217 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
218
219 /* Set nonzero if references to register n in expressions should not be
220 used. last_set_invalid is set nonzero when this register is being
221 assigned to and last_set_table_tick == label_tick. */
222
223 char last_set_invalid;
224
225 /* Some registers that are set more than once and used in more than one
226 basic block are nevertheless always set in similar ways. For example,
227 a QImode register may be loaded from memory in two places on a machine
228 where byte loads zero extend.
229
230 We record in the following fields if a register has some leading bits
231 that are always equal to the sign bit, and what we know about the
232 nonzero bits of a register, specifically which bits are known to be
233 zero.
234
235 If an entry is zero, it means that we don't know anything special. */
236
237 unsigned char sign_bit_copies;
238
239 unsigned HOST_WIDE_INT nonzero_bits;
240 };
241
242 static struct reg_stat *reg_stat;
243
244 /* Record the cuid of the last insn that invalidated memory
245 (anything that writes memory, and subroutine calls, but not pushes). */
246
247 static int mem_last_set;
248
249 /* Record the cuid of the last CALL_INSN
250 so we can tell whether a potential combination crosses any calls. */
251
252 static int last_call_cuid;
253
254 /* When `subst' is called, this is the insn that is being modified
255 (by combining in a previous insn). The PATTERN of this insn
256 is still the old pattern partially modified and it should not be
257 looked at, but this may be used to examine the successors of the insn
258 to judge whether a simplification is valid. */
259
260 static rtx subst_insn;
261
262 /* This is the lowest CUID that `subst' is currently dealing with.
263 get_last_value will not return a value if the register was set at or
264 after this CUID. If not for this mechanism, we could get confused if
265 I2 or I1 in try_combine were an insn that used the old value of a register
266 to obtain a new value. In that case, we might erroneously get the
267 new value of the register when we wanted the old one. */
268
269 static int subst_low_cuid;
270
271 /* This contains any hard registers that are used in newpat; reg_dead_at_p
272 must consider all these registers to be always live. */
273
274 static HARD_REG_SET newpat_used_regs;
275
276 /* This is an insn to which a LOG_LINKS entry has been added. If this
277 insn is the earlier than I2 or I3, combine should rescan starting at
278 that location. */
279
280 static rtx added_links_insn;
281
282 /* Basic block in which we are performing combines. */
283 static basic_block this_basic_block;
284
285 /* A bitmap indicating which blocks had registers go dead at entry.
286 After combine, we'll need to re-do global life analysis with
287 those blocks as starting points. */
288 static sbitmap refresh_blocks;
289 \f
290 /* The following array records the insn_rtx_cost for every insn
291 in the instruction stream. */
292
293 static int *uid_insn_cost;
294
295 /* Length of the currently allocated uid_insn_cost array. */
296
297 static int last_insn_cost;
298
299 /* Incremented for each label. */
300
301 static int label_tick;
302
303 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
304 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
305
306 static enum machine_mode nonzero_bits_mode;
307
308 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
309 be safely used. It is zero while computing them and after combine has
310 completed. This former test prevents propagating values based on
311 previously set values, which can be incorrect if a variable is modified
312 in a loop. */
313
314 static int nonzero_sign_valid;
315
316 \f
317 /* Record one modification to rtl structure
318 to be undone by storing old_contents into *where.
319 is_int is 1 if the contents are an int. */
320
321 struct undo
322 {
323 struct undo *next;
324 int is_int;
325 union {rtx r; int i;} old_contents;
326 union {rtx *r; int *i;} where;
327 };
328
329 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
330 num_undo says how many are currently recorded.
331
332 other_insn is nonzero if we have modified some other insn in the process
333 of working on subst_insn. It must be verified too. */
334
335 struct undobuf
336 {
337 struct undo *undos;
338 struct undo *frees;
339 rtx other_insn;
340 };
341
342 static struct undobuf undobuf;
343
344 /* Number of times the pseudo being substituted for
345 was found and replaced. */
346
347 static int n_occurrences;
348
349 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
350 enum machine_mode,
351 unsigned HOST_WIDE_INT,
352 unsigned HOST_WIDE_INT *);
353 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
354 enum machine_mode,
355 unsigned int, unsigned int *);
356 static void do_SUBST (rtx *, rtx);
357 static void do_SUBST_INT (int *, int);
358 static void init_reg_last (void);
359 static void setup_incoming_promotions (void);
360 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
361 static int cant_combine_insn_p (rtx);
362 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
363 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
364 static int contains_muldiv (rtx);
365 static rtx try_combine (rtx, rtx, rtx, int *);
366 static void undo_all (void);
367 static void undo_commit (void);
368 static rtx *find_split_point (rtx *, rtx);
369 static rtx subst (rtx, rtx, rtx, int, int);
370 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
371 static rtx simplify_if_then_else (rtx);
372 static rtx simplify_set (rtx);
373 static rtx simplify_logical (rtx);
374 static rtx expand_compound_operation (rtx);
375 static rtx expand_field_assignment (rtx);
376 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
377 rtx, unsigned HOST_WIDE_INT, int, int, int);
378 static rtx extract_left_shift (rtx, int);
379 static rtx make_compound_operation (rtx, enum rtx_code);
380 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
381 unsigned HOST_WIDE_INT *);
382 static rtx force_to_mode (rtx, enum machine_mode,
383 unsigned HOST_WIDE_INT, rtx, int);
384 static rtx if_then_else_cond (rtx, rtx *, rtx *);
385 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
386 static int rtx_equal_for_field_assignment_p (rtx, rtx);
387 static rtx make_field_assignment (rtx);
388 static rtx apply_distributive_law (rtx);
389 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
390 unsigned HOST_WIDE_INT);
391 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
392 HOST_WIDE_INT, enum machine_mode, int *);
393 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
394 int);
395 static int recog_for_combine (rtx *, rtx, rtx *);
396 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
397 static rtx gen_binary (enum rtx_code, enum machine_mode, rtx, rtx);
398 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
399 static void update_table_tick (rtx);
400 static void record_value_for_reg (rtx, rtx, rtx);
401 static void check_promoted_subreg (rtx, rtx);
402 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
403 static void record_dead_and_set_regs (rtx);
404 static int get_last_value_validate (rtx *, rtx, int, int);
405 static rtx get_last_value (rtx);
406 static int use_crosses_set_p (rtx, int);
407 static void reg_dead_at_p_1 (rtx, rtx, void *);
408 static int reg_dead_at_p (rtx, rtx);
409 static void move_deaths (rtx, rtx, int, rtx, rtx *);
410 static int reg_bitfield_target_p (rtx, rtx);
411 static void distribute_notes (rtx, rtx, rtx, rtx);
412 static void distribute_links (rtx);
413 static void mark_used_regs_combine (rtx);
414 static int insn_cuid (rtx);
415 static void record_promoted_value (rtx, rtx);
416 static rtx reversed_comparison (rtx, enum machine_mode, rtx, rtx);
417 static enum rtx_code combine_reversed_comparison_code (rtx);
418 static int unmentioned_reg_p_1 (rtx *, void *);
419 static bool unmentioned_reg_p (rtx, rtx);
420 \f
421
422 /* It is not safe to use ordinary gen_lowpart in combine.
423 See comments in gen_lowpart_for_combine. */
424 #undef RTL_HOOKS_GEN_LOWPART
425 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
426
427 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
428 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
429
430 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
431 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
432
433 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
434
435 \f
436 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
437 insn. The substitution can be undone by undo_all. If INTO is already
438 set to NEWVAL, do not record this change. Because computing NEWVAL might
439 also call SUBST, we have to compute it before we put anything into
440 the undo table. */
441
442 static void
443 do_SUBST (rtx *into, rtx newval)
444 {
445 struct undo *buf;
446 rtx oldval = *into;
447
448 if (oldval == newval)
449 return;
450
451 /* We'd like to catch as many invalid transformations here as
452 possible. Unfortunately, there are way too many mode changes
453 that are perfectly valid, so we'd waste too much effort for
454 little gain doing the checks here. Focus on catching invalid
455 transformations involving integer constants. */
456 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
457 && GET_CODE (newval) == CONST_INT)
458 {
459 /* Sanity check that we're replacing oldval with a CONST_INT
460 that is a valid sign-extension for the original mode. */
461 gcc_assert (INTVAL (newval)
462 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
463
464 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
465 CONST_INT is not valid, because after the replacement, the
466 original mode would be gone. Unfortunately, we can't tell
467 when do_SUBST is called to replace the operand thereof, so we
468 perform this test on oldval instead, checking whether an
469 invalid replacement took place before we got here. */
470 gcc_assert (!(GET_CODE (oldval) == SUBREG
471 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
472 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
473 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
474 }
475
476 if (undobuf.frees)
477 buf = undobuf.frees, undobuf.frees = buf->next;
478 else
479 buf = xmalloc (sizeof (struct undo));
480
481 buf->is_int = 0;
482 buf->where.r = into;
483 buf->old_contents.r = oldval;
484 *into = newval;
485
486 buf->next = undobuf.undos, undobuf.undos = buf;
487 }
488
489 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
490
491 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
492 for the value of a HOST_WIDE_INT value (including CONST_INT) is
493 not safe. */
494
495 static void
496 do_SUBST_INT (int *into, int newval)
497 {
498 struct undo *buf;
499 int oldval = *into;
500
501 if (oldval == newval)
502 return;
503
504 if (undobuf.frees)
505 buf = undobuf.frees, undobuf.frees = buf->next;
506 else
507 buf = xmalloc (sizeof (struct undo));
508
509 buf->is_int = 1;
510 buf->where.i = into;
511 buf->old_contents.i = oldval;
512 *into = newval;
513
514 buf->next = undobuf.undos, undobuf.undos = buf;
515 }
516
517 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
518 \f
519 /* Subroutine of try_combine. Determine whether the combine replacement
520 patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
521 that the original instruction sequence I1, I2 and I3. Note that I1
522 and/or NEWI2PAT may be NULL_RTX. This function returns false, if the
523 costs of all instructions can be estimated, and the replacements are
524 more expensive than the original sequence. */
525
526 static bool
527 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
528 {
529 int i1_cost, i2_cost, i3_cost;
530 int new_i2_cost, new_i3_cost;
531 int old_cost, new_cost;
532
533 /* Lookup the original insn_rtx_costs. */
534 i2_cost = INSN_UID (i2) <= last_insn_cost
535 ? uid_insn_cost[INSN_UID (i2)] : 0;
536 i3_cost = INSN_UID (i3) <= last_insn_cost
537 ? uid_insn_cost[INSN_UID (i3)] : 0;
538
539 if (i1)
540 {
541 i1_cost = INSN_UID (i1) <= last_insn_cost
542 ? uid_insn_cost[INSN_UID (i1)] : 0;
543 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
544 ? i1_cost + i2_cost + i3_cost : 0;
545 }
546 else
547 {
548 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
549 i1_cost = 0;
550 }
551
552 /* Calculate the replacement insn_rtx_costs. */
553 new_i3_cost = insn_rtx_cost (newpat);
554 if (newi2pat)
555 {
556 new_i2_cost = insn_rtx_cost (newi2pat);
557 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
558 ? new_i2_cost + new_i3_cost : 0;
559 }
560 else
561 {
562 new_cost = new_i3_cost;
563 new_i2_cost = 0;
564 }
565
566 if (undobuf.other_insn)
567 {
568 int old_other_cost, new_other_cost;
569
570 old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
571 ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
572 new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
573 if (old_other_cost > 0 && new_other_cost > 0)
574 {
575 old_cost += old_other_cost;
576 new_cost += new_other_cost;
577 }
578 else
579 old_cost = 0;
580 }
581
582 /* Disallow this recombination if both new_cost and old_cost are
583 greater than zero, and new_cost is greater than old cost. */
584 if (old_cost > 0
585 && new_cost > old_cost)
586 {
587 if (dump_file)
588 {
589 if (i1)
590 {
591 fprintf (dump_file,
592 "rejecting combination of insns %d, %d and %d\n",
593 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
594 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
595 i1_cost, i2_cost, i3_cost, old_cost);
596 }
597 else
598 {
599 fprintf (dump_file,
600 "rejecting combination of insns %d and %d\n",
601 INSN_UID (i2), INSN_UID (i3));
602 fprintf (dump_file, "original costs %d + %d = %d\n",
603 i2_cost, i3_cost, old_cost);
604 }
605
606 if (newi2pat)
607 {
608 fprintf (dump_file, "replacement costs %d + %d = %d\n",
609 new_i2_cost, new_i3_cost, new_cost);
610 }
611 else
612 fprintf (dump_file, "replacement cost %d\n", new_cost);
613 }
614
615 return false;
616 }
617
618 /* Update the uid_insn_cost array with the replacement costs. */
619 uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
620 uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
621 if (i1)
622 uid_insn_cost[INSN_UID (i1)] = 0;
623
624 return true;
625 }
626 \f
627 /* Main entry point for combiner. F is the first insn of the function.
628 NREGS is the first unused pseudo-reg number.
629
630 Return nonzero if the combiner has turned an indirect jump
631 instruction into a direct jump. */
632 int
633 combine_instructions (rtx f, unsigned int nregs)
634 {
635 rtx insn, next;
636 #ifdef HAVE_cc0
637 rtx prev;
638 #endif
639 int i;
640 rtx links, nextlinks;
641
642 int new_direct_jump_p = 0;
643
644 combine_attempts = 0;
645 combine_merges = 0;
646 combine_extras = 0;
647 combine_successes = 0;
648
649 combine_max_regno = nregs;
650
651 rtl_hooks = combine_rtl_hooks;
652
653 reg_stat = xcalloc (nregs, sizeof (struct reg_stat));
654
655 init_recog_no_volatile ();
656
657 /* Compute maximum uid value so uid_cuid can be allocated. */
658
659 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
660 if (INSN_UID (insn) > i)
661 i = INSN_UID (insn);
662
663 uid_cuid = xmalloc ((i + 1) * sizeof (int));
664 max_uid_cuid = i;
665
666 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
667
668 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
669 problems when, for example, we have j <<= 1 in a loop. */
670
671 nonzero_sign_valid = 0;
672
673 /* Compute the mapping from uids to cuids.
674 Cuids are numbers assigned to insns, like uids,
675 except that cuids increase monotonically through the code.
676
677 Scan all SETs and see if we can deduce anything about what
678 bits are known to be zero for some registers and how many copies
679 of the sign bit are known to exist for those registers.
680
681 Also set any known values so that we can use it while searching
682 for what bits are known to be set. */
683
684 label_tick = 1;
685
686 setup_incoming_promotions ();
687
688 refresh_blocks = sbitmap_alloc (last_basic_block);
689 sbitmap_zero (refresh_blocks);
690
691 /* Allocate array of current insn_rtx_costs. */
692 uid_insn_cost = xcalloc (max_uid_cuid + 1, sizeof (int));
693 last_insn_cost = max_uid_cuid;
694
695 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
696 {
697 uid_cuid[INSN_UID (insn)] = ++i;
698 subst_low_cuid = i;
699 subst_insn = insn;
700
701 if (INSN_P (insn))
702 {
703 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
704 NULL);
705 record_dead_and_set_regs (insn);
706
707 #ifdef AUTO_INC_DEC
708 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
709 if (REG_NOTE_KIND (links) == REG_INC)
710 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
711 NULL);
712 #endif
713
714 /* Record the current insn_rtx_cost of this instruction. */
715 if (NONJUMP_INSN_P (insn))
716 uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
717 if (dump_file)
718 fprintf(dump_file, "insn_cost %d: %d\n",
719 INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
720 }
721
722 if (LABEL_P (insn))
723 label_tick++;
724 }
725
726 nonzero_sign_valid = 1;
727
728 /* Now scan all the insns in forward order. */
729
730 label_tick = 1;
731 last_call_cuid = 0;
732 mem_last_set = 0;
733 init_reg_last ();
734 setup_incoming_promotions ();
735
736 FOR_EACH_BB (this_basic_block)
737 {
738 for (insn = BB_HEAD (this_basic_block);
739 insn != NEXT_INSN (BB_END (this_basic_block));
740 insn = next ? next : NEXT_INSN (insn))
741 {
742 next = 0;
743
744 if (LABEL_P (insn))
745 label_tick++;
746
747 else if (INSN_P (insn))
748 {
749 /* See if we know about function return values before this
750 insn based upon SUBREG flags. */
751 check_promoted_subreg (insn, PATTERN (insn));
752
753 /* Try this insn with each insn it links back to. */
754
755 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
756 if ((next = try_combine (insn, XEXP (links, 0),
757 NULL_RTX, &new_direct_jump_p)) != 0)
758 goto retry;
759
760 /* Try each sequence of three linked insns ending with this one. */
761
762 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
763 {
764 rtx link = XEXP (links, 0);
765
766 /* If the linked insn has been replaced by a note, then there
767 is no point in pursuing this chain any further. */
768 if (NOTE_P (link))
769 continue;
770
771 for (nextlinks = LOG_LINKS (link);
772 nextlinks;
773 nextlinks = XEXP (nextlinks, 1))
774 if ((next = try_combine (insn, link,
775 XEXP (nextlinks, 0),
776 &new_direct_jump_p)) != 0)
777 goto retry;
778 }
779
780 #ifdef HAVE_cc0
781 /* Try to combine a jump insn that uses CC0
782 with a preceding insn that sets CC0, and maybe with its
783 logical predecessor as well.
784 This is how we make decrement-and-branch insns.
785 We need this special code because data flow connections
786 via CC0 do not get entered in LOG_LINKS. */
787
788 if (JUMP_P (insn)
789 && (prev = prev_nonnote_insn (insn)) != 0
790 && NONJUMP_INSN_P (prev)
791 && sets_cc0_p (PATTERN (prev)))
792 {
793 if ((next = try_combine (insn, prev,
794 NULL_RTX, &new_direct_jump_p)) != 0)
795 goto retry;
796
797 for (nextlinks = LOG_LINKS (prev); nextlinks;
798 nextlinks = XEXP (nextlinks, 1))
799 if ((next = try_combine (insn, prev,
800 XEXP (nextlinks, 0),
801 &new_direct_jump_p)) != 0)
802 goto retry;
803 }
804
805 /* Do the same for an insn that explicitly references CC0. */
806 if (NONJUMP_INSN_P (insn)
807 && (prev = prev_nonnote_insn (insn)) != 0
808 && NONJUMP_INSN_P (prev)
809 && sets_cc0_p (PATTERN (prev))
810 && GET_CODE (PATTERN (insn)) == SET
811 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
812 {
813 if ((next = try_combine (insn, prev,
814 NULL_RTX, &new_direct_jump_p)) != 0)
815 goto retry;
816
817 for (nextlinks = LOG_LINKS (prev); nextlinks;
818 nextlinks = XEXP (nextlinks, 1))
819 if ((next = try_combine (insn, prev,
820 XEXP (nextlinks, 0),
821 &new_direct_jump_p)) != 0)
822 goto retry;
823 }
824
825 /* Finally, see if any of the insns that this insn links to
826 explicitly references CC0. If so, try this insn, that insn,
827 and its predecessor if it sets CC0. */
828 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
829 if (NONJUMP_INSN_P (XEXP (links, 0))
830 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
831 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
832 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
833 && NONJUMP_INSN_P (prev)
834 && sets_cc0_p (PATTERN (prev))
835 && (next = try_combine (insn, XEXP (links, 0),
836 prev, &new_direct_jump_p)) != 0)
837 goto retry;
838 #endif
839
840 /* Try combining an insn with two different insns whose results it
841 uses. */
842 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
843 for (nextlinks = XEXP (links, 1); nextlinks;
844 nextlinks = XEXP (nextlinks, 1))
845 if ((next = try_combine (insn, XEXP (links, 0),
846 XEXP (nextlinks, 0),
847 &new_direct_jump_p)) != 0)
848 goto retry;
849
850 /* Try this insn with each REG_EQUAL note it links back to. */
851 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
852 {
853 rtx set, note;
854 rtx temp = XEXP (links, 0);
855 if ((set = single_set (temp)) != 0
856 && (note = find_reg_equal_equiv_note (temp)) != 0
857 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
858 /* Avoid using a register that may already been marked
859 dead by an earlier instruction. */
860 && ! unmentioned_reg_p (XEXP (note, 0), SET_SRC (set)))
861 {
862 /* Temporarily replace the set's source with the
863 contents of the REG_EQUAL note. The insn will
864 be deleted or recognized by try_combine. */
865 rtx orig = SET_SRC (set);
866 SET_SRC (set) = XEXP (note, 0);
867 next = try_combine (insn, temp, NULL_RTX,
868 &new_direct_jump_p);
869 if (next)
870 goto retry;
871 SET_SRC (set) = orig;
872 }
873 }
874
875 if (!NOTE_P (insn))
876 record_dead_and_set_regs (insn);
877
878 retry:
879 ;
880 }
881 }
882 }
883 clear_bb_flags ();
884
885 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
886 BASIC_BLOCK (i)->flags |= BB_DIRTY);
887 new_direct_jump_p |= purge_all_dead_edges (0);
888 delete_noop_moves ();
889
890 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
891 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
892 | PROP_KILL_DEAD_CODE);
893
894 /* Clean up. */
895 sbitmap_free (refresh_blocks);
896 free (uid_insn_cost);
897 free (reg_stat);
898 free (uid_cuid);
899
900 {
901 struct undo *undo, *next;
902 for (undo = undobuf.frees; undo; undo = next)
903 {
904 next = undo->next;
905 free (undo);
906 }
907 undobuf.frees = 0;
908 }
909
910 total_attempts += combine_attempts;
911 total_merges += combine_merges;
912 total_extras += combine_extras;
913 total_successes += combine_successes;
914
915 nonzero_sign_valid = 0;
916 rtl_hooks = general_rtl_hooks;
917
918 /* Make recognizer allow volatile MEMs again. */
919 init_recog ();
920
921 return new_direct_jump_p;
922 }
923
924 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
925
926 static void
927 init_reg_last (void)
928 {
929 unsigned int i;
930 for (i = 0; i < combine_max_regno; i++)
931 memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
932 }
933 \f
934 /* Set up any promoted values for incoming argument registers. */
935
936 static void
937 setup_incoming_promotions (void)
938 {
939 unsigned int regno;
940 rtx reg;
941 enum machine_mode mode;
942 int unsignedp;
943 rtx first = get_insns ();
944
945 if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
946 {
947 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
948 /* Check whether this register can hold an incoming pointer
949 argument. FUNCTION_ARG_REGNO_P tests outgoing register
950 numbers, so translate if necessary due to register windows. */
951 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
952 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
953 {
954 record_value_for_reg
955 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
956 : SIGN_EXTEND),
957 GET_MODE (reg),
958 gen_rtx_CLOBBER (mode, const0_rtx)));
959 }
960 }
961 }
962 \f
963 /* Called via note_stores. If X is a pseudo that is narrower than
964 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
965
966 If we are setting only a portion of X and we can't figure out what
967 portion, assume all bits will be used since we don't know what will
968 be happening.
969
970 Similarly, set how many bits of X are known to be copies of the sign bit
971 at all locations in the function. This is the smallest number implied
972 by any set of X. */
973
974 static void
975 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
976 void *data ATTRIBUTE_UNUSED)
977 {
978 unsigned int num;
979
980 if (REG_P (x)
981 && REGNO (x) >= FIRST_PSEUDO_REGISTER
982 /* If this register is undefined at the start of the file, we can't
983 say what its contents were. */
984 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
985 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
986 {
987 if (set == 0 || GET_CODE (set) == CLOBBER)
988 {
989 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
990 reg_stat[REGNO (x)].sign_bit_copies = 1;
991 return;
992 }
993
994 /* If this is a complex assignment, see if we can convert it into a
995 simple assignment. */
996 set = expand_field_assignment (set);
997
998 /* If this is a simple assignment, or we have a paradoxical SUBREG,
999 set what we know about X. */
1000
1001 if (SET_DEST (set) == x
1002 || (GET_CODE (SET_DEST (set)) == SUBREG
1003 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1004 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1005 && SUBREG_REG (SET_DEST (set)) == x))
1006 {
1007 rtx src = SET_SRC (set);
1008
1009 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1010 /* If X is narrower than a word and SRC is a non-negative
1011 constant that would appear negative in the mode of X,
1012 sign-extend it for use in reg_stat[].nonzero_bits because some
1013 machines (maybe most) will actually do the sign-extension
1014 and this is the conservative approach.
1015
1016 ??? For 2.5, try to tighten up the MD files in this regard
1017 instead of this kludge. */
1018
1019 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1020 && GET_CODE (src) == CONST_INT
1021 && INTVAL (src) > 0
1022 && 0 != (INTVAL (src)
1023 & ((HOST_WIDE_INT) 1
1024 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1025 src = GEN_INT (INTVAL (src)
1026 | ((HOST_WIDE_INT) (-1)
1027 << GET_MODE_BITSIZE (GET_MODE (x))));
1028 #endif
1029
1030 /* Don't call nonzero_bits if it cannot change anything. */
1031 if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1032 reg_stat[REGNO (x)].nonzero_bits
1033 |= nonzero_bits (src, nonzero_bits_mode);
1034 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1035 if (reg_stat[REGNO (x)].sign_bit_copies == 0
1036 || reg_stat[REGNO (x)].sign_bit_copies > num)
1037 reg_stat[REGNO (x)].sign_bit_copies = num;
1038 }
1039 else
1040 {
1041 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1042 reg_stat[REGNO (x)].sign_bit_copies = 1;
1043 }
1044 }
1045 }
1046 \f
1047 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1048 insns that were previously combined into I3 or that will be combined
1049 into the merger of INSN and I3.
1050
1051 Return 0 if the combination is not allowed for any reason.
1052
1053 If the combination is allowed, *PDEST will be set to the single
1054 destination of INSN and *PSRC to the single source, and this function
1055 will return 1. */
1056
1057 static int
1058 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1059 rtx *pdest, rtx *psrc)
1060 {
1061 int i;
1062 rtx set = 0, src, dest;
1063 rtx p;
1064 #ifdef AUTO_INC_DEC
1065 rtx link;
1066 #endif
1067 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1068 && next_active_insn (succ) == i3)
1069 : next_active_insn (insn) == i3);
1070
1071 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1072 or a PARALLEL consisting of such a SET and CLOBBERs.
1073
1074 If INSN has CLOBBER parallel parts, ignore them for our processing.
1075 By definition, these happen during the execution of the insn. When it
1076 is merged with another insn, all bets are off. If they are, in fact,
1077 needed and aren't also supplied in I3, they may be added by
1078 recog_for_combine. Otherwise, it won't match.
1079
1080 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1081 note.
1082
1083 Get the source and destination of INSN. If more than one, can't
1084 combine. */
1085
1086 if (GET_CODE (PATTERN (insn)) == SET)
1087 set = PATTERN (insn);
1088 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1089 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1090 {
1091 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1092 {
1093 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1094 rtx note;
1095
1096 switch (GET_CODE (elt))
1097 {
1098 /* This is important to combine floating point insns
1099 for the SH4 port. */
1100 case USE:
1101 /* Combining an isolated USE doesn't make sense.
1102 We depend here on combinable_i3pat to reject them. */
1103 /* The code below this loop only verifies that the inputs of
1104 the SET in INSN do not change. We call reg_set_between_p
1105 to verify that the REG in the USE does not change between
1106 I3 and INSN.
1107 If the USE in INSN was for a pseudo register, the matching
1108 insn pattern will likely match any register; combining this
1109 with any other USE would only be safe if we knew that the
1110 used registers have identical values, or if there was
1111 something to tell them apart, e.g. different modes. For
1112 now, we forgo such complicated tests and simply disallow
1113 combining of USES of pseudo registers with any other USE. */
1114 if (REG_P (XEXP (elt, 0))
1115 && GET_CODE (PATTERN (i3)) == PARALLEL)
1116 {
1117 rtx i3pat = PATTERN (i3);
1118 int i = XVECLEN (i3pat, 0) - 1;
1119 unsigned int regno = REGNO (XEXP (elt, 0));
1120
1121 do
1122 {
1123 rtx i3elt = XVECEXP (i3pat, 0, i);
1124
1125 if (GET_CODE (i3elt) == USE
1126 && REG_P (XEXP (i3elt, 0))
1127 && (REGNO (XEXP (i3elt, 0)) == regno
1128 ? reg_set_between_p (XEXP (elt, 0),
1129 PREV_INSN (insn), i3)
1130 : regno >= FIRST_PSEUDO_REGISTER))
1131 return 0;
1132 }
1133 while (--i >= 0);
1134 }
1135 break;
1136
1137 /* We can ignore CLOBBERs. */
1138 case CLOBBER:
1139 break;
1140
1141 case SET:
1142 /* Ignore SETs whose result isn't used but not those that
1143 have side-effects. */
1144 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1145 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1146 || INTVAL (XEXP (note, 0)) <= 0)
1147 && ! side_effects_p (elt))
1148 break;
1149
1150 /* If we have already found a SET, this is a second one and
1151 so we cannot combine with this insn. */
1152 if (set)
1153 return 0;
1154
1155 set = elt;
1156 break;
1157
1158 default:
1159 /* Anything else means we can't combine. */
1160 return 0;
1161 }
1162 }
1163
1164 if (set == 0
1165 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1166 so don't do anything with it. */
1167 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1168 return 0;
1169 }
1170 else
1171 return 0;
1172
1173 if (set == 0)
1174 return 0;
1175
1176 set = expand_field_assignment (set);
1177 src = SET_SRC (set), dest = SET_DEST (set);
1178
1179 /* Don't eliminate a store in the stack pointer. */
1180 if (dest == stack_pointer_rtx
1181 /* Don't combine with an insn that sets a register to itself if it has
1182 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1183 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1184 /* Can't merge an ASM_OPERANDS. */
1185 || GET_CODE (src) == ASM_OPERANDS
1186 /* Can't merge a function call. */
1187 || GET_CODE (src) == CALL
1188 /* Don't eliminate a function call argument. */
1189 || (CALL_P (i3)
1190 && (find_reg_fusage (i3, USE, dest)
1191 || (REG_P (dest)
1192 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1193 && global_regs[REGNO (dest)])))
1194 /* Don't substitute into an incremented register. */
1195 || FIND_REG_INC_NOTE (i3, dest)
1196 || (succ && FIND_REG_INC_NOTE (succ, dest))
1197 /* Don't substitute into a non-local goto, this confuses CFG. */
1198 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1199 #if 0
1200 /* Don't combine the end of a libcall into anything. */
1201 /* ??? This gives worse code, and appears to be unnecessary, since no
1202 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1203 use REG_RETVAL notes for noconflict blocks, but other code here
1204 makes sure that those insns don't disappear. */
1205 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1206 #endif
1207 /* Make sure that DEST is not used after SUCC but before I3. */
1208 || (succ && ! all_adjacent
1209 && reg_used_between_p (dest, succ, i3))
1210 /* Make sure that the value that is to be substituted for the register
1211 does not use any registers whose values alter in between. However,
1212 If the insns are adjacent, a use can't cross a set even though we
1213 think it might (this can happen for a sequence of insns each setting
1214 the same destination; last_set of that register might point to
1215 a NOTE). If INSN has a REG_EQUIV note, the register is always
1216 equivalent to the memory so the substitution is valid even if there
1217 are intervening stores. Also, don't move a volatile asm or
1218 UNSPEC_VOLATILE across any other insns. */
1219 || (! all_adjacent
1220 && (((!MEM_P (src)
1221 || ! find_reg_note (insn, REG_EQUIV, src))
1222 && use_crosses_set_p (src, INSN_CUID (insn)))
1223 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1224 || GET_CODE (src) == UNSPEC_VOLATILE))
1225 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1226 better register allocation by not doing the combine. */
1227 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1228 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1229 /* Don't combine across a CALL_INSN, because that would possibly
1230 change whether the life span of some REGs crosses calls or not,
1231 and it is a pain to update that information.
1232 Exception: if source is a constant, moving it later can't hurt.
1233 Accept that special case, because it helps -fforce-addr a lot. */
1234 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1235 return 0;
1236
1237 /* DEST must either be a REG or CC0. */
1238 if (REG_P (dest))
1239 {
1240 /* If register alignment is being enforced for multi-word items in all
1241 cases except for parameters, it is possible to have a register copy
1242 insn referencing a hard register that is not allowed to contain the
1243 mode being copied and which would not be valid as an operand of most
1244 insns. Eliminate this problem by not combining with such an insn.
1245
1246 Also, on some machines we don't want to extend the life of a hard
1247 register. */
1248
1249 if (REG_P (src)
1250 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1251 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1252 /* Don't extend the life of a hard register unless it is
1253 user variable (if we have few registers) or it can't
1254 fit into the desired register (meaning something special
1255 is going on).
1256 Also avoid substituting a return register into I3, because
1257 reload can't handle a conflict with constraints of other
1258 inputs. */
1259 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1260 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1261 return 0;
1262 }
1263 else if (GET_CODE (dest) != CC0)
1264 return 0;
1265
1266
1267 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1268 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1269 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1270 {
1271 /* Don't substitute for a register intended as a clobberable
1272 operand. */
1273 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1274 if (rtx_equal_p (reg, dest))
1275 return 0;
1276
1277 /* If the clobber represents an earlyclobber operand, we must not
1278 substitute an expression containing the clobbered register.
1279 As we do not analyze the constraint strings here, we have to
1280 make the conservative assumption. However, if the register is
1281 a fixed hard reg, the clobber cannot represent any operand;
1282 we leave it up to the machine description to either accept or
1283 reject use-and-clobber patterns. */
1284 if (!REG_P (reg)
1285 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1286 || !fixed_regs[REGNO (reg)])
1287 if (reg_overlap_mentioned_p (reg, src))
1288 return 0;
1289 }
1290
1291 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1292 or not), reject, unless nothing volatile comes between it and I3 */
1293
1294 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1295 {
1296 /* Make sure succ doesn't contain a volatile reference. */
1297 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1298 return 0;
1299
1300 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1301 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1302 return 0;
1303 }
1304
1305 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1306 to be an explicit register variable, and was chosen for a reason. */
1307
1308 if (GET_CODE (src) == ASM_OPERANDS
1309 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1310 return 0;
1311
1312 /* If there are any volatile insns between INSN and I3, reject, because
1313 they might affect machine state. */
1314
1315 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1316 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1317 return 0;
1318
1319 /* If INSN or I2 contains an autoincrement or autodecrement,
1320 make sure that register is not used between there and I3,
1321 and not already used in I3 either.
1322 Also insist that I3 not be a jump; if it were one
1323 and the incremented register were spilled, we would lose. */
1324
1325 #ifdef AUTO_INC_DEC
1326 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1327 if (REG_NOTE_KIND (link) == REG_INC
1328 && (JUMP_P (i3)
1329 || reg_used_between_p (XEXP (link, 0), insn, i3)
1330 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1331 return 0;
1332 #endif
1333
1334 #ifdef HAVE_cc0
1335 /* Don't combine an insn that follows a CC0-setting insn.
1336 An insn that uses CC0 must not be separated from the one that sets it.
1337 We do, however, allow I2 to follow a CC0-setting insn if that insn
1338 is passed as I1; in that case it will be deleted also.
1339 We also allow combining in this case if all the insns are adjacent
1340 because that would leave the two CC0 insns adjacent as well.
1341 It would be more logical to test whether CC0 occurs inside I1 or I2,
1342 but that would be much slower, and this ought to be equivalent. */
1343
1344 p = prev_nonnote_insn (insn);
1345 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1346 && ! all_adjacent)
1347 return 0;
1348 #endif
1349
1350 /* If we get here, we have passed all the tests and the combination is
1351 to be allowed. */
1352
1353 *pdest = dest;
1354 *psrc = src;
1355
1356 return 1;
1357 }
1358 \f
1359 /* LOC is the location within I3 that contains its pattern or the component
1360 of a PARALLEL of the pattern. We validate that it is valid for combining.
1361
1362 One problem is if I3 modifies its output, as opposed to replacing it
1363 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1364 so would produce an insn that is not equivalent to the original insns.
1365
1366 Consider:
1367
1368 (set (reg:DI 101) (reg:DI 100))
1369 (set (subreg:SI (reg:DI 101) 0) <foo>)
1370
1371 This is NOT equivalent to:
1372
1373 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1374 (set (reg:DI 101) (reg:DI 100))])
1375
1376 Not only does this modify 100 (in which case it might still be valid
1377 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1378
1379 We can also run into a problem if I2 sets a register that I1
1380 uses and I1 gets directly substituted into I3 (not via I2). In that
1381 case, we would be getting the wrong value of I2DEST into I3, so we
1382 must reject the combination. This case occurs when I2 and I1 both
1383 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1384 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1385 of a SET must prevent combination from occurring.
1386
1387 Before doing the above check, we first try to expand a field assignment
1388 into a set of logical operations.
1389
1390 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1391 we place a register that is both set and used within I3. If more than one
1392 such register is detected, we fail.
1393
1394 Return 1 if the combination is valid, zero otherwise. */
1395
1396 static int
1397 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1398 int i1_not_in_src, rtx *pi3dest_killed)
1399 {
1400 rtx x = *loc;
1401
1402 if (GET_CODE (x) == SET)
1403 {
1404 rtx set = x ;
1405 rtx dest = SET_DEST (set);
1406 rtx src = SET_SRC (set);
1407 rtx inner_dest = dest;
1408
1409 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1410 || GET_CODE (inner_dest) == SUBREG
1411 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1412 inner_dest = XEXP (inner_dest, 0);
1413
1414 /* Check for the case where I3 modifies its output, as discussed
1415 above. We don't want to prevent pseudos from being combined
1416 into the address of a MEM, so only prevent the combination if
1417 i1 or i2 set the same MEM. */
1418 if ((inner_dest != dest &&
1419 (!MEM_P (inner_dest)
1420 || rtx_equal_p (i2dest, inner_dest)
1421 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1422 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1423 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1424
1425 /* This is the same test done in can_combine_p except we can't test
1426 all_adjacent; we don't have to, since this instruction will stay
1427 in place, thus we are not considering increasing the lifetime of
1428 INNER_DEST.
1429
1430 Also, if this insn sets a function argument, combining it with
1431 something that might need a spill could clobber a previous
1432 function argument; the all_adjacent test in can_combine_p also
1433 checks this; here, we do a more specific test for this case. */
1434
1435 || (REG_P (inner_dest)
1436 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1437 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1438 GET_MODE (inner_dest))))
1439 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1440 return 0;
1441
1442 /* If DEST is used in I3, it is being killed in this insn,
1443 so record that for later.
1444 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1445 STACK_POINTER_REGNUM, since these are always considered to be
1446 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1447 if (pi3dest_killed && REG_P (dest)
1448 && reg_referenced_p (dest, PATTERN (i3))
1449 && REGNO (dest) != FRAME_POINTER_REGNUM
1450 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1451 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1452 #endif
1453 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1454 && (REGNO (dest) != ARG_POINTER_REGNUM
1455 || ! fixed_regs [REGNO (dest)])
1456 #endif
1457 && REGNO (dest) != STACK_POINTER_REGNUM)
1458 {
1459 if (*pi3dest_killed)
1460 return 0;
1461
1462 *pi3dest_killed = dest;
1463 }
1464 }
1465
1466 else if (GET_CODE (x) == PARALLEL)
1467 {
1468 int i;
1469
1470 for (i = 0; i < XVECLEN (x, 0); i++)
1471 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1472 i1_not_in_src, pi3dest_killed))
1473 return 0;
1474 }
1475
1476 return 1;
1477 }
1478 \f
1479 /* Return 1 if X is an arithmetic expression that contains a multiplication
1480 and division. We don't count multiplications by powers of two here. */
1481
1482 static int
1483 contains_muldiv (rtx x)
1484 {
1485 switch (GET_CODE (x))
1486 {
1487 case MOD: case DIV: case UMOD: case UDIV:
1488 return 1;
1489
1490 case MULT:
1491 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1492 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1493 default:
1494 if (BINARY_P (x))
1495 return contains_muldiv (XEXP (x, 0))
1496 || contains_muldiv (XEXP (x, 1));
1497
1498 if (UNARY_P (x))
1499 return contains_muldiv (XEXP (x, 0));
1500
1501 return 0;
1502 }
1503 }
1504 \f
1505 /* Determine whether INSN can be used in a combination. Return nonzero if
1506 not. This is used in try_combine to detect early some cases where we
1507 can't perform combinations. */
1508
1509 static int
1510 cant_combine_insn_p (rtx insn)
1511 {
1512 rtx set;
1513 rtx src, dest;
1514
1515 /* If this isn't really an insn, we can't do anything.
1516 This can occur when flow deletes an insn that it has merged into an
1517 auto-increment address. */
1518 if (! INSN_P (insn))
1519 return 1;
1520
1521 /* Never combine loads and stores involving hard regs that are likely
1522 to be spilled. The register allocator can usually handle such
1523 reg-reg moves by tying. If we allow the combiner to make
1524 substitutions of likely-spilled regs, we may abort in reload.
1525 As an exception, we allow combinations involving fixed regs; these are
1526 not available to the register allocator so there's no risk involved. */
1527
1528 set = single_set (insn);
1529 if (! set)
1530 return 0;
1531 src = SET_SRC (set);
1532 dest = SET_DEST (set);
1533 if (GET_CODE (src) == SUBREG)
1534 src = SUBREG_REG (src);
1535 if (GET_CODE (dest) == SUBREG)
1536 dest = SUBREG_REG (dest);
1537 if (REG_P (src) && REG_P (dest)
1538 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1539 && ! fixed_regs[REGNO (src)]
1540 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1541 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1542 && ! fixed_regs[REGNO (dest)]
1543 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1544 return 1;
1545
1546 return 0;
1547 }
1548
1549 /* Adjust INSN after we made a change to its destination.
1550
1551 Changing the destination can invalidate notes that say something about
1552 the results of the insn and a LOG_LINK pointing to the insn. */
1553
1554 static void
1555 adjust_for_new_dest (rtx insn)
1556 {
1557 rtx *loc;
1558
1559 /* For notes, be conservative and simply remove them. */
1560 loc = &REG_NOTES (insn);
1561 while (*loc)
1562 {
1563 enum reg_note kind = REG_NOTE_KIND (*loc);
1564 if (kind == REG_EQUAL || kind == REG_EQUIV)
1565 *loc = XEXP (*loc, 1);
1566 else
1567 loc = &XEXP (*loc, 1);
1568 }
1569
1570 /* The new insn will have a destination that was previously the destination
1571 of an insn just above it. Call distribute_links to make a LOG_LINK from
1572 the next use of that destination. */
1573 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1574 }
1575
1576 /* Try to combine the insns I1 and I2 into I3.
1577 Here I1 and I2 appear earlier than I3.
1578 I1 can be zero; then we combine just I2 into I3.
1579
1580 If we are combining three insns and the resulting insn is not recognized,
1581 try splitting it into two insns. If that happens, I2 and I3 are retained
1582 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1583 are pseudo-deleted.
1584
1585 Return 0 if the combination does not work. Then nothing is changed.
1586 If we did the combination, return the insn at which combine should
1587 resume scanning.
1588
1589 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1590 new direct jump instruction. */
1591
1592 static rtx
1593 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1594 {
1595 /* New patterns for I3 and I2, respectively. */
1596 rtx newpat, newi2pat = 0;
1597 int substed_i2 = 0, substed_i1 = 0;
1598 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1599 int added_sets_1, added_sets_2;
1600 /* Total number of SETs to put into I3. */
1601 int total_sets;
1602 /* Nonzero if I2's body now appears in I3. */
1603 int i2_is_used;
1604 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1605 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1606 /* Contains I3 if the destination of I3 is used in its source, which means
1607 that the old life of I3 is being killed. If that usage is placed into
1608 I2 and not in I3, a REG_DEAD note must be made. */
1609 rtx i3dest_killed = 0;
1610 /* SET_DEST and SET_SRC of I2 and I1. */
1611 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1612 /* PATTERN (I2), or a copy of it in certain cases. */
1613 rtx i2pat;
1614 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1615 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1616 int i1_feeds_i3 = 0;
1617 /* Notes that must be added to REG_NOTES in I3 and I2. */
1618 rtx new_i3_notes, new_i2_notes;
1619 /* Notes that we substituted I3 into I2 instead of the normal case. */
1620 int i3_subst_into_i2 = 0;
1621 /* Notes that I1, I2 or I3 is a MULT operation. */
1622 int have_mult = 0;
1623 int swap_i2i3 = 0;
1624
1625 int maxreg;
1626 rtx temp;
1627 rtx link;
1628 int i;
1629
1630 /* Exit early if one of the insns involved can't be used for
1631 combinations. */
1632 if (cant_combine_insn_p (i3)
1633 || cant_combine_insn_p (i2)
1634 || (i1 && cant_combine_insn_p (i1))
1635 /* We also can't do anything if I3 has a
1636 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1637 libcall. */
1638 #if 0
1639 /* ??? This gives worse code, and appears to be unnecessary, since no
1640 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1641 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1642 #endif
1643 )
1644 return 0;
1645
1646 combine_attempts++;
1647 undobuf.other_insn = 0;
1648
1649 /* Reset the hard register usage information. */
1650 CLEAR_HARD_REG_SET (newpat_used_regs);
1651
1652 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1653 code below, set I1 to be the earlier of the two insns. */
1654 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1655 temp = i1, i1 = i2, i2 = temp;
1656
1657 added_links_insn = 0;
1658
1659 /* First check for one important special-case that the code below will
1660 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1661 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1662 we may be able to replace that destination with the destination of I3.
1663 This occurs in the common code where we compute both a quotient and
1664 remainder into a structure, in which case we want to do the computation
1665 directly into the structure to avoid register-register copies.
1666
1667 Note that this case handles both multiple sets in I2 and also
1668 cases where I2 has a number of CLOBBER or PARALLELs.
1669
1670 We make very conservative checks below and only try to handle the
1671 most common cases of this. For example, we only handle the case
1672 where I2 and I3 are adjacent to avoid making difficult register
1673 usage tests. */
1674
1675 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1676 && REG_P (SET_SRC (PATTERN (i3)))
1677 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1678 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1679 && GET_CODE (PATTERN (i2)) == PARALLEL
1680 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1681 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1682 below would need to check what is inside (and reg_overlap_mentioned_p
1683 doesn't support those codes anyway). Don't allow those destinations;
1684 the resulting insn isn't likely to be recognized anyway. */
1685 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1686 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1687 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1688 SET_DEST (PATTERN (i3)))
1689 && next_real_insn (i2) == i3)
1690 {
1691 rtx p2 = PATTERN (i2);
1692
1693 /* Make sure that the destination of I3,
1694 which we are going to substitute into one output of I2,
1695 is not used within another output of I2. We must avoid making this:
1696 (parallel [(set (mem (reg 69)) ...)
1697 (set (reg 69) ...)])
1698 which is not well-defined as to order of actions.
1699 (Besides, reload can't handle output reloads for this.)
1700
1701 The problem can also happen if the dest of I3 is a memory ref,
1702 if another dest in I2 is an indirect memory ref. */
1703 for (i = 0; i < XVECLEN (p2, 0); i++)
1704 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1705 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1706 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1707 SET_DEST (XVECEXP (p2, 0, i))))
1708 break;
1709
1710 if (i == XVECLEN (p2, 0))
1711 for (i = 0; i < XVECLEN (p2, 0); i++)
1712 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1713 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1714 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1715 {
1716 combine_merges++;
1717
1718 subst_insn = i3;
1719 subst_low_cuid = INSN_CUID (i2);
1720
1721 added_sets_2 = added_sets_1 = 0;
1722 i2dest = SET_SRC (PATTERN (i3));
1723
1724 /* Replace the dest in I2 with our dest and make the resulting
1725 insn the new pattern for I3. Then skip to where we
1726 validate the pattern. Everything was set up above. */
1727 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1728 SET_DEST (PATTERN (i3)));
1729
1730 newpat = p2;
1731 i3_subst_into_i2 = 1;
1732 goto validate_replacement;
1733 }
1734 }
1735
1736 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1737 one of those words to another constant, merge them by making a new
1738 constant. */
1739 if (i1 == 0
1740 && (temp = single_set (i2)) != 0
1741 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1742 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1743 && REG_P (SET_DEST (temp))
1744 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1745 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1746 && GET_CODE (PATTERN (i3)) == SET
1747 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1748 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1749 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1750 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1751 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1752 {
1753 HOST_WIDE_INT lo, hi;
1754
1755 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1756 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1757 else
1758 {
1759 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1760 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1761 }
1762
1763 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1764 {
1765 /* We don't handle the case of the target word being wider
1766 than a host wide int. */
1767 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD);
1768
1769 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1770 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1771 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1772 }
1773 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1774 hi = INTVAL (SET_SRC (PATTERN (i3)));
1775 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1776 {
1777 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1778 >> (HOST_BITS_PER_WIDE_INT - 1));
1779
1780 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1781 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1782 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1783 (INTVAL (SET_SRC (PATTERN (i3)))));
1784 if (hi == sign)
1785 hi = lo < 0 ? -1 : 0;
1786 }
1787 else
1788 /* We don't handle the case of the higher word not fitting
1789 entirely in either hi or lo. */
1790 gcc_unreachable ();
1791
1792 combine_merges++;
1793 subst_insn = i3;
1794 subst_low_cuid = INSN_CUID (i2);
1795 added_sets_2 = added_sets_1 = 0;
1796 i2dest = SET_DEST (temp);
1797
1798 SUBST (SET_SRC (temp),
1799 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1800
1801 newpat = PATTERN (i2);
1802 goto validate_replacement;
1803 }
1804
1805 #ifndef HAVE_cc0
1806 /* If we have no I1 and I2 looks like:
1807 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1808 (set Y OP)])
1809 make up a dummy I1 that is
1810 (set Y OP)
1811 and change I2 to be
1812 (set (reg:CC X) (compare:CC Y (const_int 0)))
1813
1814 (We can ignore any trailing CLOBBERs.)
1815
1816 This undoes a previous combination and allows us to match a branch-and-
1817 decrement insn. */
1818
1819 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1820 && XVECLEN (PATTERN (i2), 0) >= 2
1821 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1822 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1823 == MODE_CC)
1824 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1825 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1826 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1827 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
1828 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1829 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1830 {
1831 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1832 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1833 break;
1834
1835 if (i == 1)
1836 {
1837 /* We make I1 with the same INSN_UID as I2. This gives it
1838 the same INSN_CUID for value tracking. Our fake I1 will
1839 never appear in the insn stream so giving it the same INSN_UID
1840 as I2 will not cause a problem. */
1841
1842 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1843 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1844 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1845 NULL_RTX);
1846
1847 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1848 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1849 SET_DEST (PATTERN (i1)));
1850 }
1851 }
1852 #endif
1853
1854 /* Verify that I2 and I1 are valid for combining. */
1855 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1856 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1857 {
1858 undo_all ();
1859 return 0;
1860 }
1861
1862 /* Record whether I2DEST is used in I2SRC and similarly for the other
1863 cases. Knowing this will help in register status updating below. */
1864 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1865 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1866 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1867
1868 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1869 in I2SRC. */
1870 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1871
1872 /* Ensure that I3's pattern can be the destination of combines. */
1873 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1874 i1 && i2dest_in_i1src && i1_feeds_i3,
1875 &i3dest_killed))
1876 {
1877 undo_all ();
1878 return 0;
1879 }
1880
1881 /* See if any of the insns is a MULT operation. Unless one is, we will
1882 reject a combination that is, since it must be slower. Be conservative
1883 here. */
1884 if (GET_CODE (i2src) == MULT
1885 || (i1 != 0 && GET_CODE (i1src) == MULT)
1886 || (GET_CODE (PATTERN (i3)) == SET
1887 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1888 have_mult = 1;
1889
1890 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1891 We used to do this EXCEPT in one case: I3 has a post-inc in an
1892 output operand. However, that exception can give rise to insns like
1893 mov r3,(r3)+
1894 which is a famous insn on the PDP-11 where the value of r3 used as the
1895 source was model-dependent. Avoid this sort of thing. */
1896
1897 #if 0
1898 if (!(GET_CODE (PATTERN (i3)) == SET
1899 && REG_P (SET_SRC (PATTERN (i3)))
1900 && MEM_P (SET_DEST (PATTERN (i3)))
1901 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1902 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1903 /* It's not the exception. */
1904 #endif
1905 #ifdef AUTO_INC_DEC
1906 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1907 if (REG_NOTE_KIND (link) == REG_INC
1908 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1909 || (i1 != 0
1910 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1911 {
1912 undo_all ();
1913 return 0;
1914 }
1915 #endif
1916
1917 /* See if the SETs in I1 or I2 need to be kept around in the merged
1918 instruction: whenever the value set there is still needed past I3.
1919 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1920
1921 For the SET in I1, we have two cases: If I1 and I2 independently
1922 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1923 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1924 in I1 needs to be kept around unless I1DEST dies or is set in either
1925 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1926 I1DEST. If so, we know I1 feeds into I2. */
1927
1928 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1929
1930 added_sets_1
1931 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1932 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1933
1934 /* If the set in I2 needs to be kept around, we must make a copy of
1935 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1936 PATTERN (I2), we are only substituting for the original I1DEST, not into
1937 an already-substituted copy. This also prevents making self-referential
1938 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1939 I2DEST. */
1940
1941 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1942 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1943 : PATTERN (i2));
1944
1945 if (added_sets_2)
1946 i2pat = copy_rtx (i2pat);
1947
1948 combine_merges++;
1949
1950 /* Substitute in the latest insn for the regs set by the earlier ones. */
1951
1952 maxreg = max_reg_num ();
1953
1954 subst_insn = i3;
1955
1956 /* It is possible that the source of I2 or I1 may be performing an
1957 unneeded operation, such as a ZERO_EXTEND of something that is known
1958 to have the high part zero. Handle that case by letting subst look at
1959 the innermost one of them.
1960
1961 Another way to do this would be to have a function that tries to
1962 simplify a single insn instead of merging two or more insns. We don't
1963 do this because of the potential of infinite loops and because
1964 of the potential extra memory required. However, doing it the way
1965 we are is a bit of a kludge and doesn't catch all cases.
1966
1967 But only do this if -fexpensive-optimizations since it slows things down
1968 and doesn't usually win. */
1969
1970 if (flag_expensive_optimizations)
1971 {
1972 /* Pass pc_rtx so no substitutions are done, just simplifications. */
1973 if (i1)
1974 {
1975 subst_low_cuid = INSN_CUID (i1);
1976 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1977 }
1978 else
1979 {
1980 subst_low_cuid = INSN_CUID (i2);
1981 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1982 }
1983 }
1984
1985 #ifndef HAVE_cc0
1986 /* Many machines that don't use CC0 have insns that can both perform an
1987 arithmetic operation and set the condition code. These operations will
1988 be represented as a PARALLEL with the first element of the vector
1989 being a COMPARE of an arithmetic operation with the constant zero.
1990 The second element of the vector will set some pseudo to the result
1991 of the same arithmetic operation. If we simplify the COMPARE, we won't
1992 match such a pattern and so will generate an extra insn. Here we test
1993 for this case, where both the comparison and the operation result are
1994 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1995 I2SRC. Later we will make the PARALLEL that contains I2. */
1996
1997 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1998 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1999 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2000 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2001 {
2002 #ifdef SELECT_CC_MODE
2003 rtx *cc_use;
2004 enum machine_mode compare_mode;
2005 #endif
2006
2007 newpat = PATTERN (i3);
2008 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2009
2010 i2_is_used = 1;
2011
2012 #ifdef SELECT_CC_MODE
2013 /* See if a COMPARE with the operand we substituted in should be done
2014 with the mode that is currently being used. If not, do the same
2015 processing we do in `subst' for a SET; namely, if the destination
2016 is used only once, try to replace it with a register of the proper
2017 mode and also replace the COMPARE. */
2018 if (undobuf.other_insn == 0
2019 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2020 &undobuf.other_insn))
2021 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2022 i2src, const0_rtx))
2023 != GET_MODE (SET_DEST (newpat))))
2024 {
2025 unsigned int regno = REGNO (SET_DEST (newpat));
2026 rtx new_dest = gen_rtx_REG (compare_mode, regno);
2027
2028 if (regno < FIRST_PSEUDO_REGISTER
2029 || (REG_N_SETS (regno) == 1 && ! added_sets_2
2030 && ! REG_USERVAR_P (SET_DEST (newpat))))
2031 {
2032 if (regno >= FIRST_PSEUDO_REGISTER)
2033 SUBST (regno_reg_rtx[regno], new_dest);
2034
2035 SUBST (SET_DEST (newpat), new_dest);
2036 SUBST (XEXP (*cc_use, 0), new_dest);
2037 SUBST (SET_SRC (newpat),
2038 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2039 }
2040 else
2041 undobuf.other_insn = 0;
2042 }
2043 #endif
2044 }
2045 else
2046 #endif
2047 {
2048 n_occurrences = 0; /* `subst' counts here */
2049
2050 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2051 need to make a unique copy of I2SRC each time we substitute it
2052 to avoid self-referential rtl. */
2053
2054 subst_low_cuid = INSN_CUID (i2);
2055 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2056 ! i1_feeds_i3 && i1dest_in_i1src);
2057 substed_i2 = 1;
2058
2059 /* Record whether i2's body now appears within i3's body. */
2060 i2_is_used = n_occurrences;
2061 }
2062
2063 /* If we already got a failure, don't try to do more. Otherwise,
2064 try to substitute in I1 if we have it. */
2065
2066 if (i1 && GET_CODE (newpat) != CLOBBER)
2067 {
2068 /* Before we can do this substitution, we must redo the test done
2069 above (see detailed comments there) that ensures that I1DEST
2070 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2071
2072 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2073 0, (rtx*) 0))
2074 {
2075 undo_all ();
2076 return 0;
2077 }
2078
2079 n_occurrences = 0;
2080 subst_low_cuid = INSN_CUID (i1);
2081 newpat = subst (newpat, i1dest, i1src, 0, 0);
2082 substed_i1 = 1;
2083 }
2084
2085 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2086 to count all the ways that I2SRC and I1SRC can be used. */
2087 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2088 && i2_is_used + added_sets_2 > 1)
2089 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2090 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2091 > 1))
2092 /* Fail if we tried to make a new register (we used to abort, but there's
2093 really no reason to). */
2094 || max_reg_num () != maxreg
2095 /* Fail if we couldn't do something and have a CLOBBER. */
2096 || GET_CODE (newpat) == CLOBBER
2097 /* Fail if this new pattern is a MULT and we didn't have one before
2098 at the outer level. */
2099 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2100 && ! have_mult))
2101 {
2102 undo_all ();
2103 return 0;
2104 }
2105
2106 /* If the actions of the earlier insns must be kept
2107 in addition to substituting them into the latest one,
2108 we must make a new PARALLEL for the latest insn
2109 to hold additional the SETs. */
2110
2111 if (added_sets_1 || added_sets_2)
2112 {
2113 combine_extras++;
2114
2115 if (GET_CODE (newpat) == PARALLEL)
2116 {
2117 rtvec old = XVEC (newpat, 0);
2118 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2119 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2120 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2121 sizeof (old->elem[0]) * old->num_elem);
2122 }
2123 else
2124 {
2125 rtx old = newpat;
2126 total_sets = 1 + added_sets_1 + added_sets_2;
2127 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2128 XVECEXP (newpat, 0, 0) = old;
2129 }
2130
2131 if (added_sets_1)
2132 XVECEXP (newpat, 0, --total_sets)
2133 = (GET_CODE (PATTERN (i1)) == PARALLEL
2134 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2135
2136 if (added_sets_2)
2137 {
2138 /* If there is no I1, use I2's body as is. We used to also not do
2139 the subst call below if I2 was substituted into I3,
2140 but that could lose a simplification. */
2141 if (i1 == 0)
2142 XVECEXP (newpat, 0, --total_sets) = i2pat;
2143 else
2144 /* See comment where i2pat is assigned. */
2145 XVECEXP (newpat, 0, --total_sets)
2146 = subst (i2pat, i1dest, i1src, 0, 0);
2147 }
2148 }
2149
2150 /* We come here when we are replacing a destination in I2 with the
2151 destination of I3. */
2152 validate_replacement:
2153
2154 /* Note which hard regs this insn has as inputs. */
2155 mark_used_regs_combine (newpat);
2156
2157 /* Is the result of combination a valid instruction? */
2158 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2159
2160 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2161 the second SET's destination is a register that is unused and isn't
2162 marked as an instruction that might trap in an EH region. In that case,
2163 we just need the first SET. This can occur when simplifying a divmod
2164 insn. We *must* test for this case here because the code below that
2165 splits two independent SETs doesn't handle this case correctly when it
2166 updates the register status.
2167
2168 It's pointless doing this if we originally had two sets, one from
2169 i3, and one from i2. Combining then splitting the parallel results
2170 in the original i2 again plus an invalid insn (which we delete).
2171 The net effect is only to move instructions around, which makes
2172 debug info less accurate.
2173
2174 Also check the case where the first SET's destination is unused.
2175 That would not cause incorrect code, but does cause an unneeded
2176 insn to remain. */
2177
2178 if (insn_code_number < 0
2179 && !(added_sets_2 && i1 == 0)
2180 && GET_CODE (newpat) == PARALLEL
2181 && XVECLEN (newpat, 0) == 2
2182 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2183 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2184 && asm_noperands (newpat) < 0)
2185 {
2186 rtx set0 = XVECEXP (newpat, 0, 0);
2187 rtx set1 = XVECEXP (newpat, 0, 1);
2188 rtx note;
2189
2190 if (((REG_P (SET_DEST (set1))
2191 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2192 || (GET_CODE (SET_DEST (set1)) == SUBREG
2193 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2194 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2195 || INTVAL (XEXP (note, 0)) <= 0)
2196 && ! side_effects_p (SET_SRC (set1)))
2197 {
2198 newpat = set0;
2199 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2200 }
2201
2202 else if (((REG_P (SET_DEST (set0))
2203 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2204 || (GET_CODE (SET_DEST (set0)) == SUBREG
2205 && find_reg_note (i3, REG_UNUSED,
2206 SUBREG_REG (SET_DEST (set0)))))
2207 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2208 || INTVAL (XEXP (note, 0)) <= 0)
2209 && ! side_effects_p (SET_SRC (set0)))
2210 {
2211 newpat = set1;
2212 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2213
2214 if (insn_code_number >= 0)
2215 {
2216 /* If we will be able to accept this, we have made a
2217 change to the destination of I3. This requires us to
2218 do a few adjustments. */
2219
2220 PATTERN (i3) = newpat;
2221 adjust_for_new_dest (i3);
2222 }
2223 }
2224 }
2225
2226 /* If we were combining three insns and the result is a simple SET
2227 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2228 insns. There are two ways to do this. It can be split using a
2229 machine-specific method (like when you have an addition of a large
2230 constant) or by combine in the function find_split_point. */
2231
2232 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2233 && asm_noperands (newpat) < 0)
2234 {
2235 rtx m_split, *split;
2236 rtx ni2dest = i2dest;
2237
2238 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2239 use I2DEST as a scratch register will help. In the latter case,
2240 convert I2DEST to the mode of the source of NEWPAT if we can. */
2241
2242 m_split = split_insns (newpat, i3);
2243
2244 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2245 inputs of NEWPAT. */
2246
2247 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2248 possible to try that as a scratch reg. This would require adding
2249 more code to make it work though. */
2250
2251 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2252 {
2253 /* If I2DEST is a hard register or the only use of a pseudo,
2254 we can change its mode. */
2255 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2256 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2257 && REG_P (i2dest)
2258 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2259 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2260 && ! REG_USERVAR_P (i2dest))))
2261 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2262 REGNO (i2dest));
2263
2264 m_split = split_insns (gen_rtx_PARALLEL
2265 (VOIDmode,
2266 gen_rtvec (2, newpat,
2267 gen_rtx_CLOBBER (VOIDmode,
2268 ni2dest))),
2269 i3);
2270 /* If the split with the mode-changed register didn't work, try
2271 the original register. */
2272 if (! m_split && ni2dest != i2dest)
2273 {
2274 ni2dest = i2dest;
2275 m_split = split_insns (gen_rtx_PARALLEL
2276 (VOIDmode,
2277 gen_rtvec (2, newpat,
2278 gen_rtx_CLOBBER (VOIDmode,
2279 i2dest))),
2280 i3);
2281 }
2282 }
2283
2284 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2285 {
2286 m_split = PATTERN (m_split);
2287 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2288 if (insn_code_number >= 0)
2289 newpat = m_split;
2290 }
2291 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2292 && (next_real_insn (i2) == i3
2293 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2294 {
2295 rtx i2set, i3set;
2296 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2297 newi2pat = PATTERN (m_split);
2298
2299 i3set = single_set (NEXT_INSN (m_split));
2300 i2set = single_set (m_split);
2301
2302 /* In case we changed the mode of I2DEST, replace it in the
2303 pseudo-register table here. We can't do it above in case this
2304 code doesn't get executed and we do a split the other way. */
2305
2306 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2307 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2308
2309 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2310
2311 /* If I2 or I3 has multiple SETs, we won't know how to track
2312 register status, so don't use these insns. If I2's destination
2313 is used between I2 and I3, we also can't use these insns. */
2314
2315 if (i2_code_number >= 0 && i2set && i3set
2316 && (next_real_insn (i2) == i3
2317 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2318 insn_code_number = recog_for_combine (&newi3pat, i3,
2319 &new_i3_notes);
2320 if (insn_code_number >= 0)
2321 newpat = newi3pat;
2322
2323 /* It is possible that both insns now set the destination of I3.
2324 If so, we must show an extra use of it. */
2325
2326 if (insn_code_number >= 0)
2327 {
2328 rtx new_i3_dest = SET_DEST (i3set);
2329 rtx new_i2_dest = SET_DEST (i2set);
2330
2331 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2332 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2333 || GET_CODE (new_i3_dest) == SUBREG)
2334 new_i3_dest = XEXP (new_i3_dest, 0);
2335
2336 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2337 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2338 || GET_CODE (new_i2_dest) == SUBREG)
2339 new_i2_dest = XEXP (new_i2_dest, 0);
2340
2341 if (REG_P (new_i3_dest)
2342 && REG_P (new_i2_dest)
2343 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2344 REG_N_SETS (REGNO (new_i2_dest))++;
2345 }
2346 }
2347
2348 /* If we can split it and use I2DEST, go ahead and see if that
2349 helps things be recognized. Verify that none of the registers
2350 are set between I2 and I3. */
2351 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2352 #ifdef HAVE_cc0
2353 && REG_P (i2dest)
2354 #endif
2355 /* We need I2DEST in the proper mode. If it is a hard register
2356 or the only use of a pseudo, we can change its mode. */
2357 && (GET_MODE (*split) == GET_MODE (i2dest)
2358 || GET_MODE (*split) == VOIDmode
2359 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2360 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2361 && ! REG_USERVAR_P (i2dest)))
2362 && (next_real_insn (i2) == i3
2363 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2364 /* We can't overwrite I2DEST if its value is still used by
2365 NEWPAT. */
2366 && ! reg_referenced_p (i2dest, newpat))
2367 {
2368 rtx newdest = i2dest;
2369 enum rtx_code split_code = GET_CODE (*split);
2370 enum machine_mode split_mode = GET_MODE (*split);
2371
2372 /* Get NEWDEST as a register in the proper mode. We have already
2373 validated that we can do this. */
2374 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2375 {
2376 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2377
2378 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2379 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2380 }
2381
2382 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2383 an ASHIFT. This can occur if it was inside a PLUS and hence
2384 appeared to be a memory address. This is a kludge. */
2385 if (split_code == MULT
2386 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2387 && INTVAL (XEXP (*split, 1)) > 0
2388 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2389 {
2390 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2391 XEXP (*split, 0), GEN_INT (i)));
2392 /* Update split_code because we may not have a multiply
2393 anymore. */
2394 split_code = GET_CODE (*split);
2395 }
2396
2397 #ifdef INSN_SCHEDULING
2398 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2399 be written as a ZERO_EXTEND. */
2400 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2401 {
2402 #ifdef LOAD_EXTEND_OP
2403 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2404 what it really is. */
2405 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2406 == SIGN_EXTEND)
2407 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2408 SUBREG_REG (*split)));
2409 else
2410 #endif
2411 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2412 SUBREG_REG (*split)));
2413 }
2414 #endif
2415
2416 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2417 SUBST (*split, newdest);
2418 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2419
2420 /* recog_for_combine might have added CLOBBERs to newi2pat.
2421 Make sure NEWPAT does not depend on the clobbered regs. */
2422 if (GET_CODE (newi2pat) == PARALLEL)
2423 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2424 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2425 {
2426 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2427 if (reg_overlap_mentioned_p (reg, newpat))
2428 {
2429 undo_all ();
2430 return 0;
2431 }
2432 }
2433
2434 /* If the split point was a MULT and we didn't have one before,
2435 don't use one now. */
2436 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2437 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2438 }
2439 }
2440
2441 /* Check for a case where we loaded from memory in a narrow mode and
2442 then sign extended it, but we need both registers. In that case,
2443 we have a PARALLEL with both loads from the same memory location.
2444 We can split this into a load from memory followed by a register-register
2445 copy. This saves at least one insn, more if register allocation can
2446 eliminate the copy.
2447
2448 We cannot do this if the destination of the first assignment is a
2449 condition code register or cc0. We eliminate this case by making sure
2450 the SET_DEST and SET_SRC have the same mode.
2451
2452 We cannot do this if the destination of the second assignment is
2453 a register that we have already assumed is zero-extended. Similarly
2454 for a SUBREG of such a register. */
2455
2456 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2457 && GET_CODE (newpat) == PARALLEL
2458 && XVECLEN (newpat, 0) == 2
2459 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2460 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2461 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2462 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2463 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2464 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2465 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2466 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2467 INSN_CUID (i2))
2468 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2469 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2470 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2471 (REG_P (temp)
2472 && reg_stat[REGNO (temp)].nonzero_bits != 0
2473 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2474 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2475 && (reg_stat[REGNO (temp)].nonzero_bits
2476 != GET_MODE_MASK (word_mode))))
2477 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2478 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2479 (REG_P (temp)
2480 && reg_stat[REGNO (temp)].nonzero_bits != 0
2481 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2482 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2483 && (reg_stat[REGNO (temp)].nonzero_bits
2484 != GET_MODE_MASK (word_mode)))))
2485 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2486 SET_SRC (XVECEXP (newpat, 0, 1)))
2487 && ! find_reg_note (i3, REG_UNUSED,
2488 SET_DEST (XVECEXP (newpat, 0, 0))))
2489 {
2490 rtx ni2dest;
2491
2492 newi2pat = XVECEXP (newpat, 0, 0);
2493 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2494 newpat = XVECEXP (newpat, 0, 1);
2495 SUBST (SET_SRC (newpat),
2496 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2497 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2498
2499 if (i2_code_number >= 0)
2500 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2501
2502 if (insn_code_number >= 0)
2503 swap_i2i3 = 1;
2504 }
2505
2506 /* Similarly, check for a case where we have a PARALLEL of two independent
2507 SETs but we started with three insns. In this case, we can do the sets
2508 as two separate insns. This case occurs when some SET allows two
2509 other insns to combine, but the destination of that SET is still live. */
2510
2511 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2512 && GET_CODE (newpat) == PARALLEL
2513 && XVECLEN (newpat, 0) == 2
2514 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2515 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2516 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2517 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2518 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2519 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2520 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2521 INSN_CUID (i2))
2522 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2523 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2524 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2525 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2526 XVECEXP (newpat, 0, 0))
2527 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2528 XVECEXP (newpat, 0, 1))
2529 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2530 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2531 {
2532 /* Normally, it doesn't matter which of the two is done first,
2533 but it does if one references cc0. In that case, it has to
2534 be first. */
2535 #ifdef HAVE_cc0
2536 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2537 {
2538 newi2pat = XVECEXP (newpat, 0, 0);
2539 newpat = XVECEXP (newpat, 0, 1);
2540 }
2541 else
2542 #endif
2543 {
2544 newi2pat = XVECEXP (newpat, 0, 1);
2545 newpat = XVECEXP (newpat, 0, 0);
2546 }
2547
2548 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2549
2550 if (i2_code_number >= 0)
2551 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2552 }
2553
2554 /* If it still isn't recognized, fail and change things back the way they
2555 were. */
2556 if ((insn_code_number < 0
2557 /* Is the result a reasonable ASM_OPERANDS? */
2558 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2559 {
2560 undo_all ();
2561 return 0;
2562 }
2563
2564 /* If we had to change another insn, make sure it is valid also. */
2565 if (undobuf.other_insn)
2566 {
2567 rtx other_pat = PATTERN (undobuf.other_insn);
2568 rtx new_other_notes;
2569 rtx note, next;
2570
2571 CLEAR_HARD_REG_SET (newpat_used_regs);
2572
2573 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2574 &new_other_notes);
2575
2576 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2577 {
2578 undo_all ();
2579 return 0;
2580 }
2581
2582 PATTERN (undobuf.other_insn) = other_pat;
2583
2584 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2585 are still valid. Then add any non-duplicate notes added by
2586 recog_for_combine. */
2587 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2588 {
2589 next = XEXP (note, 1);
2590
2591 if (REG_NOTE_KIND (note) == REG_UNUSED
2592 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2593 {
2594 if (REG_P (XEXP (note, 0)))
2595 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2596
2597 remove_note (undobuf.other_insn, note);
2598 }
2599 }
2600
2601 for (note = new_other_notes; note; note = XEXP (note, 1))
2602 if (REG_P (XEXP (note, 0)))
2603 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2604
2605 distribute_notes (new_other_notes, undobuf.other_insn,
2606 undobuf.other_insn, NULL_RTX);
2607 }
2608 #ifdef HAVE_cc0
2609 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2610 they are adjacent to each other or not. */
2611 {
2612 rtx p = prev_nonnote_insn (i3);
2613 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
2614 && sets_cc0_p (newi2pat))
2615 {
2616 undo_all ();
2617 return 0;
2618 }
2619 }
2620 #endif
2621
2622 /* Only allow this combination if insn_rtx_costs reports that the
2623 replacement instructions are cheaper than the originals. */
2624 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
2625 {
2626 undo_all ();
2627 return 0;
2628 }
2629
2630 /* We now know that we can do this combination. Merge the insns and
2631 update the status of registers and LOG_LINKS. */
2632
2633 if (swap_i2i3)
2634 {
2635 rtx insn;
2636 rtx link;
2637 rtx ni2dest;
2638
2639 /* I3 now uses what used to be its destination and which is now
2640 I2's destination. This requires us to do a few adjustments. */
2641 PATTERN (i3) = newpat;
2642 adjust_for_new_dest (i3);
2643
2644 /* We need a LOG_LINK from I3 to I2. But we used to have one,
2645 so we still will.
2646
2647 However, some later insn might be using I2's dest and have
2648 a LOG_LINK pointing at I3. We must remove this link.
2649 The simplest way to remove the link is to point it at I1,
2650 which we know will be a NOTE. */
2651
2652 /* newi2pat is usually a SET here; however, recog_for_combine might
2653 have added some clobbers. */
2654 if (GET_CODE (newi2pat) == PARALLEL)
2655 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
2656 else
2657 ni2dest = SET_DEST (newi2pat);
2658
2659 for (insn = NEXT_INSN (i3);
2660 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2661 || insn != BB_HEAD (this_basic_block->next_bb));
2662 insn = NEXT_INSN (insn))
2663 {
2664 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2665 {
2666 for (link = LOG_LINKS (insn); link;
2667 link = XEXP (link, 1))
2668 if (XEXP (link, 0) == i3)
2669 XEXP (link, 0) = i1;
2670
2671 break;
2672 }
2673 }
2674 }
2675
2676 {
2677 rtx i3notes, i2notes, i1notes = 0;
2678 rtx i3links, i2links, i1links = 0;
2679 rtx midnotes = 0;
2680 unsigned int regno;
2681
2682 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2683 clear them. */
2684 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2685 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2686 if (i1)
2687 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2688
2689 /* Ensure that we do not have something that should not be shared but
2690 occurs multiple times in the new insns. Check this by first
2691 resetting all the `used' flags and then copying anything is shared. */
2692
2693 reset_used_flags (i3notes);
2694 reset_used_flags (i2notes);
2695 reset_used_flags (i1notes);
2696 reset_used_flags (newpat);
2697 reset_used_flags (newi2pat);
2698 if (undobuf.other_insn)
2699 reset_used_flags (PATTERN (undobuf.other_insn));
2700
2701 i3notes = copy_rtx_if_shared (i3notes);
2702 i2notes = copy_rtx_if_shared (i2notes);
2703 i1notes = copy_rtx_if_shared (i1notes);
2704 newpat = copy_rtx_if_shared (newpat);
2705 newi2pat = copy_rtx_if_shared (newi2pat);
2706 if (undobuf.other_insn)
2707 reset_used_flags (PATTERN (undobuf.other_insn));
2708
2709 INSN_CODE (i3) = insn_code_number;
2710 PATTERN (i3) = newpat;
2711
2712 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
2713 {
2714 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2715
2716 reset_used_flags (call_usage);
2717 call_usage = copy_rtx (call_usage);
2718
2719 if (substed_i2)
2720 replace_rtx (call_usage, i2dest, i2src);
2721
2722 if (substed_i1)
2723 replace_rtx (call_usage, i1dest, i1src);
2724
2725 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2726 }
2727
2728 if (undobuf.other_insn)
2729 INSN_CODE (undobuf.other_insn) = other_code_number;
2730
2731 /* We had one special case above where I2 had more than one set and
2732 we replaced a destination of one of those sets with the destination
2733 of I3. In that case, we have to update LOG_LINKS of insns later
2734 in this basic block. Note that this (expensive) case is rare.
2735
2736 Also, in this case, we must pretend that all REG_NOTEs for I2
2737 actually came from I3, so that REG_UNUSED notes from I2 will be
2738 properly handled. */
2739
2740 if (i3_subst_into_i2)
2741 {
2742 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2743 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2744 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
2745 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2746 && ! find_reg_note (i2, REG_UNUSED,
2747 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2748 for (temp = NEXT_INSN (i2);
2749 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2750 || BB_HEAD (this_basic_block) != temp);
2751 temp = NEXT_INSN (temp))
2752 if (temp != i3 && INSN_P (temp))
2753 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2754 if (XEXP (link, 0) == i2)
2755 XEXP (link, 0) = i3;
2756
2757 if (i3notes)
2758 {
2759 rtx link = i3notes;
2760 while (XEXP (link, 1))
2761 link = XEXP (link, 1);
2762 XEXP (link, 1) = i2notes;
2763 }
2764 else
2765 i3notes = i2notes;
2766 i2notes = 0;
2767 }
2768
2769 LOG_LINKS (i3) = 0;
2770 REG_NOTES (i3) = 0;
2771 LOG_LINKS (i2) = 0;
2772 REG_NOTES (i2) = 0;
2773
2774 if (newi2pat)
2775 {
2776 INSN_CODE (i2) = i2_code_number;
2777 PATTERN (i2) = newi2pat;
2778 }
2779 else
2780 SET_INSN_DELETED (i2);
2781
2782 if (i1)
2783 {
2784 LOG_LINKS (i1) = 0;
2785 REG_NOTES (i1) = 0;
2786 SET_INSN_DELETED (i1);
2787 }
2788
2789 /* Get death notes for everything that is now used in either I3 or
2790 I2 and used to die in a previous insn. If we built two new
2791 patterns, move from I1 to I2 then I2 to I3 so that we get the
2792 proper movement on registers that I2 modifies. */
2793
2794 if (newi2pat)
2795 {
2796 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2797 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2798 }
2799 else
2800 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2801 i3, &midnotes);
2802
2803 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2804 if (i3notes)
2805 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2806 if (i2notes)
2807 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2808 if (i1notes)
2809 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2810 if (midnotes)
2811 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2812
2813 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2814 know these are REG_UNUSED and want them to go to the desired insn,
2815 so we always pass it as i3. We have not counted the notes in
2816 reg_n_deaths yet, so we need to do so now. */
2817
2818 if (newi2pat && new_i2_notes)
2819 {
2820 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2821 if (REG_P (XEXP (temp, 0)))
2822 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2823
2824 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2825 }
2826
2827 if (new_i3_notes)
2828 {
2829 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2830 if (REG_P (XEXP (temp, 0)))
2831 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2832
2833 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2834 }
2835
2836 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2837 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2838 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2839 in that case, it might delete I2. Similarly for I2 and I1.
2840 Show an additional death due to the REG_DEAD note we make here. If
2841 we discard it in distribute_notes, we will decrement it again. */
2842
2843 if (i3dest_killed)
2844 {
2845 if (REG_P (i3dest_killed))
2846 REG_N_DEATHS (REGNO (i3dest_killed))++;
2847
2848 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2849 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2850 NULL_RTX),
2851 NULL_RTX, i2, NULL_RTX);
2852 else
2853 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2854 NULL_RTX),
2855 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2856 }
2857
2858 if (i2dest_in_i2src)
2859 {
2860 if (REG_P (i2dest))
2861 REG_N_DEATHS (REGNO (i2dest))++;
2862
2863 if (newi2pat && reg_set_p (i2dest, newi2pat))
2864 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2865 NULL_RTX, i2, NULL_RTX);
2866 else
2867 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2868 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2869 }
2870
2871 if (i1dest_in_i1src)
2872 {
2873 if (REG_P (i1dest))
2874 REG_N_DEATHS (REGNO (i1dest))++;
2875
2876 if (newi2pat && reg_set_p (i1dest, newi2pat))
2877 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2878 NULL_RTX, i2, NULL_RTX);
2879 else
2880 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2881 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2882 }
2883
2884 distribute_links (i3links);
2885 distribute_links (i2links);
2886 distribute_links (i1links);
2887
2888 if (REG_P (i2dest))
2889 {
2890 rtx link;
2891 rtx i2_insn = 0, i2_val = 0, set;
2892
2893 /* The insn that used to set this register doesn't exist, and
2894 this life of the register may not exist either. See if one of
2895 I3's links points to an insn that sets I2DEST. If it does,
2896 that is now the last known value for I2DEST. If we don't update
2897 this and I2 set the register to a value that depended on its old
2898 contents, we will get confused. If this insn is used, thing
2899 will be set correctly in combine_instructions. */
2900
2901 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2902 if ((set = single_set (XEXP (link, 0))) != 0
2903 && rtx_equal_p (i2dest, SET_DEST (set)))
2904 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2905
2906 record_value_for_reg (i2dest, i2_insn, i2_val);
2907
2908 /* If the reg formerly set in I2 died only once and that was in I3,
2909 zero its use count so it won't make `reload' do any work. */
2910 if (! added_sets_2
2911 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2912 && ! i2dest_in_i2src)
2913 {
2914 regno = REGNO (i2dest);
2915 REG_N_SETS (regno)--;
2916 }
2917 }
2918
2919 if (i1 && REG_P (i1dest))
2920 {
2921 rtx link;
2922 rtx i1_insn = 0, i1_val = 0, set;
2923
2924 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2925 if ((set = single_set (XEXP (link, 0))) != 0
2926 && rtx_equal_p (i1dest, SET_DEST (set)))
2927 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2928
2929 record_value_for_reg (i1dest, i1_insn, i1_val);
2930
2931 regno = REGNO (i1dest);
2932 if (! added_sets_1 && ! i1dest_in_i1src)
2933 REG_N_SETS (regno)--;
2934 }
2935
2936 /* Update reg_stat[].nonzero_bits et al for any changes that may have
2937 been made to this insn. The order of
2938 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
2939 can affect nonzero_bits of newpat */
2940 if (newi2pat)
2941 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2942 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2943
2944 /* Set new_direct_jump_p if a new return or simple jump instruction
2945 has been created.
2946
2947 If I3 is now an unconditional jump, ensure that it has a
2948 BARRIER following it since it may have initially been a
2949 conditional jump. It may also be the last nonnote insn. */
2950
2951 if (returnjump_p (i3) || any_uncondjump_p (i3))
2952 {
2953 *new_direct_jump_p = 1;
2954 mark_jump_label (PATTERN (i3), i3, 0);
2955
2956 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2957 || !BARRIER_P (temp))
2958 emit_barrier_after (i3);
2959 }
2960
2961 if (undobuf.other_insn != NULL_RTX
2962 && (returnjump_p (undobuf.other_insn)
2963 || any_uncondjump_p (undobuf.other_insn)))
2964 {
2965 *new_direct_jump_p = 1;
2966
2967 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2968 || !BARRIER_P (temp))
2969 emit_barrier_after (undobuf.other_insn);
2970 }
2971
2972 /* An NOOP jump does not need barrier, but it does need cleaning up
2973 of CFG. */
2974 if (GET_CODE (newpat) == SET
2975 && SET_SRC (newpat) == pc_rtx
2976 && SET_DEST (newpat) == pc_rtx)
2977 *new_direct_jump_p = 1;
2978 }
2979
2980 combine_successes++;
2981 undo_commit ();
2982
2983 if (added_links_insn
2984 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2985 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2986 return added_links_insn;
2987 else
2988 return newi2pat ? i2 : i3;
2989 }
2990 \f
2991 /* Undo all the modifications recorded in undobuf. */
2992
2993 static void
2994 undo_all (void)
2995 {
2996 struct undo *undo, *next;
2997
2998 for (undo = undobuf.undos; undo; undo = next)
2999 {
3000 next = undo->next;
3001 if (undo->is_int)
3002 *undo->where.i = undo->old_contents.i;
3003 else
3004 *undo->where.r = undo->old_contents.r;
3005
3006 undo->next = undobuf.frees;
3007 undobuf.frees = undo;
3008 }
3009
3010 undobuf.undos = 0;
3011 }
3012
3013 /* We've committed to accepting the changes we made. Move all
3014 of the undos to the free list. */
3015
3016 static void
3017 undo_commit (void)
3018 {
3019 struct undo *undo, *next;
3020
3021 for (undo = undobuf.undos; undo; undo = next)
3022 {
3023 next = undo->next;
3024 undo->next = undobuf.frees;
3025 undobuf.frees = undo;
3026 }
3027 undobuf.undos = 0;
3028 }
3029
3030 \f
3031 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3032 where we have an arithmetic expression and return that point. LOC will
3033 be inside INSN.
3034
3035 try_combine will call this function to see if an insn can be split into
3036 two insns. */
3037
3038 static rtx *
3039 find_split_point (rtx *loc, rtx insn)
3040 {
3041 rtx x = *loc;
3042 enum rtx_code code = GET_CODE (x);
3043 rtx *split;
3044 unsigned HOST_WIDE_INT len = 0;
3045 HOST_WIDE_INT pos = 0;
3046 int unsignedp = 0;
3047 rtx inner = NULL_RTX;
3048
3049 /* First special-case some codes. */
3050 switch (code)
3051 {
3052 case SUBREG:
3053 #ifdef INSN_SCHEDULING
3054 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3055 point. */
3056 if (MEM_P (SUBREG_REG (x)))
3057 return loc;
3058 #endif
3059 return find_split_point (&SUBREG_REG (x), insn);
3060
3061 case MEM:
3062 #ifdef HAVE_lo_sum
3063 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3064 using LO_SUM and HIGH. */
3065 if (GET_CODE (XEXP (x, 0)) == CONST
3066 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3067 {
3068 SUBST (XEXP (x, 0),
3069 gen_rtx_LO_SUM (Pmode,
3070 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3071 XEXP (x, 0)));
3072 return &XEXP (XEXP (x, 0), 0);
3073 }
3074 #endif
3075
3076 /* If we have a PLUS whose second operand is a constant and the
3077 address is not valid, perhaps will can split it up using
3078 the machine-specific way to split large constants. We use
3079 the first pseudo-reg (one of the virtual regs) as a placeholder;
3080 it will not remain in the result. */
3081 if (GET_CODE (XEXP (x, 0)) == PLUS
3082 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3083 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3084 {
3085 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3086 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3087 subst_insn);
3088
3089 /* This should have produced two insns, each of which sets our
3090 placeholder. If the source of the second is a valid address,
3091 we can make put both sources together and make a split point
3092 in the middle. */
3093
3094 if (seq
3095 && NEXT_INSN (seq) != NULL_RTX
3096 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3097 && NONJUMP_INSN_P (seq)
3098 && GET_CODE (PATTERN (seq)) == SET
3099 && SET_DEST (PATTERN (seq)) == reg
3100 && ! reg_mentioned_p (reg,
3101 SET_SRC (PATTERN (seq)))
3102 && NONJUMP_INSN_P (NEXT_INSN (seq))
3103 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3104 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3105 && memory_address_p (GET_MODE (x),
3106 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3107 {
3108 rtx src1 = SET_SRC (PATTERN (seq));
3109 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3110
3111 /* Replace the placeholder in SRC2 with SRC1. If we can
3112 find where in SRC2 it was placed, that can become our
3113 split point and we can replace this address with SRC2.
3114 Just try two obvious places. */
3115
3116 src2 = replace_rtx (src2, reg, src1);
3117 split = 0;
3118 if (XEXP (src2, 0) == src1)
3119 split = &XEXP (src2, 0);
3120 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3121 && XEXP (XEXP (src2, 0), 0) == src1)
3122 split = &XEXP (XEXP (src2, 0), 0);
3123
3124 if (split)
3125 {
3126 SUBST (XEXP (x, 0), src2);
3127 return split;
3128 }
3129 }
3130
3131 /* If that didn't work, perhaps the first operand is complex and
3132 needs to be computed separately, so make a split point there.
3133 This will occur on machines that just support REG + CONST
3134 and have a constant moved through some previous computation. */
3135
3136 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3137 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3138 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3139 return &XEXP (XEXP (x, 0), 0);
3140 }
3141 break;
3142
3143 case SET:
3144 #ifdef HAVE_cc0
3145 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3146 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3147 we need to put the operand into a register. So split at that
3148 point. */
3149
3150 if (SET_DEST (x) == cc0_rtx
3151 && GET_CODE (SET_SRC (x)) != COMPARE
3152 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3153 && !OBJECT_P (SET_SRC (x))
3154 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3155 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3156 return &SET_SRC (x);
3157 #endif
3158
3159 /* See if we can split SET_SRC as it stands. */
3160 split = find_split_point (&SET_SRC (x), insn);
3161 if (split && split != &SET_SRC (x))
3162 return split;
3163
3164 /* See if we can split SET_DEST as it stands. */
3165 split = find_split_point (&SET_DEST (x), insn);
3166 if (split && split != &SET_DEST (x))
3167 return split;
3168
3169 /* See if this is a bitfield assignment with everything constant. If
3170 so, this is an IOR of an AND, so split it into that. */
3171 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3172 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3173 <= HOST_BITS_PER_WIDE_INT)
3174 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3175 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3176 && GET_CODE (SET_SRC (x)) == CONST_INT
3177 && ((INTVAL (XEXP (SET_DEST (x), 1))
3178 + INTVAL (XEXP (SET_DEST (x), 2)))
3179 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3180 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3181 {
3182 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3183 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3184 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3185 rtx dest = XEXP (SET_DEST (x), 0);
3186 enum machine_mode mode = GET_MODE (dest);
3187 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3188
3189 if (BITS_BIG_ENDIAN)
3190 pos = GET_MODE_BITSIZE (mode) - len - pos;
3191
3192 if (src == mask)
3193 SUBST (SET_SRC (x),
3194 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3195 else
3196 SUBST (SET_SRC (x),
3197 gen_binary (IOR, mode,
3198 gen_binary (AND, mode, dest,
3199 gen_int_mode (~(mask << pos),
3200 mode)),
3201 GEN_INT (src << pos)));
3202
3203 SUBST (SET_DEST (x), dest);
3204
3205 split = find_split_point (&SET_SRC (x), insn);
3206 if (split && split != &SET_SRC (x))
3207 return split;
3208 }
3209
3210 /* Otherwise, see if this is an operation that we can split into two.
3211 If so, try to split that. */
3212 code = GET_CODE (SET_SRC (x));
3213
3214 switch (code)
3215 {
3216 case AND:
3217 /* If we are AND'ing with a large constant that is only a single
3218 bit and the result is only being used in a context where we
3219 need to know if it is zero or nonzero, replace it with a bit
3220 extraction. This will avoid the large constant, which might
3221 have taken more than one insn to make. If the constant were
3222 not a valid argument to the AND but took only one insn to make,
3223 this is no worse, but if it took more than one insn, it will
3224 be better. */
3225
3226 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3227 && REG_P (XEXP (SET_SRC (x), 0))
3228 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3229 && REG_P (SET_DEST (x))
3230 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3231 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3232 && XEXP (*split, 0) == SET_DEST (x)
3233 && XEXP (*split, 1) == const0_rtx)
3234 {
3235 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3236 XEXP (SET_SRC (x), 0),
3237 pos, NULL_RTX, 1, 1, 0, 0);
3238 if (extraction != 0)
3239 {
3240 SUBST (SET_SRC (x), extraction);
3241 return find_split_point (loc, insn);
3242 }
3243 }
3244 break;
3245
3246 case NE:
3247 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3248 is known to be on, this can be converted into a NEG of a shift. */
3249 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3250 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3251 && 1 <= (pos = exact_log2
3252 (nonzero_bits (XEXP (SET_SRC (x), 0),
3253 GET_MODE (XEXP (SET_SRC (x), 0))))))
3254 {
3255 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3256
3257 SUBST (SET_SRC (x),
3258 gen_rtx_NEG (mode,
3259 gen_rtx_LSHIFTRT (mode,
3260 XEXP (SET_SRC (x), 0),
3261 GEN_INT (pos))));
3262
3263 split = find_split_point (&SET_SRC (x), insn);
3264 if (split && split != &SET_SRC (x))
3265 return split;
3266 }
3267 break;
3268
3269 case SIGN_EXTEND:
3270 inner = XEXP (SET_SRC (x), 0);
3271
3272 /* We can't optimize if either mode is a partial integer
3273 mode as we don't know how many bits are significant
3274 in those modes. */
3275 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3276 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3277 break;
3278
3279 pos = 0;
3280 len = GET_MODE_BITSIZE (GET_MODE (inner));
3281 unsignedp = 0;
3282 break;
3283
3284 case SIGN_EXTRACT:
3285 case ZERO_EXTRACT:
3286 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3287 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3288 {
3289 inner = XEXP (SET_SRC (x), 0);
3290 len = INTVAL (XEXP (SET_SRC (x), 1));
3291 pos = INTVAL (XEXP (SET_SRC (x), 2));
3292
3293 if (BITS_BIG_ENDIAN)
3294 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3295 unsignedp = (code == ZERO_EXTRACT);
3296 }
3297 break;
3298
3299 default:
3300 break;
3301 }
3302
3303 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3304 {
3305 enum machine_mode mode = GET_MODE (SET_SRC (x));
3306
3307 /* For unsigned, we have a choice of a shift followed by an
3308 AND or two shifts. Use two shifts for field sizes where the
3309 constant might be too large. We assume here that we can
3310 always at least get 8-bit constants in an AND insn, which is
3311 true for every current RISC. */
3312
3313 if (unsignedp && len <= 8)
3314 {
3315 SUBST (SET_SRC (x),
3316 gen_rtx_AND (mode,
3317 gen_rtx_LSHIFTRT
3318 (mode, gen_lowpart (mode, inner),
3319 GEN_INT (pos)),
3320 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3321
3322 split = find_split_point (&SET_SRC (x), insn);
3323 if (split && split != &SET_SRC (x))
3324 return split;
3325 }
3326 else
3327 {
3328 SUBST (SET_SRC (x),
3329 gen_rtx_fmt_ee
3330 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3331 gen_rtx_ASHIFT (mode,
3332 gen_lowpart (mode, inner),
3333 GEN_INT (GET_MODE_BITSIZE (mode)
3334 - len - pos)),
3335 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3336
3337 split = find_split_point (&SET_SRC (x), insn);
3338 if (split && split != &SET_SRC (x))
3339 return split;
3340 }
3341 }
3342
3343 /* See if this is a simple operation with a constant as the second
3344 operand. It might be that this constant is out of range and hence
3345 could be used as a split point. */
3346 if (BINARY_P (SET_SRC (x))
3347 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3348 && (OBJECT_P (XEXP (SET_SRC (x), 0))
3349 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3350 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3351 return &XEXP (SET_SRC (x), 1);
3352
3353 /* Finally, see if this is a simple operation with its first operand
3354 not in a register. The operation might require this operand in a
3355 register, so return it as a split point. We can always do this
3356 because if the first operand were another operation, we would have
3357 already found it as a split point. */
3358 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3359 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3360 return &XEXP (SET_SRC (x), 0);
3361
3362 return 0;
3363
3364 case AND:
3365 case IOR:
3366 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3367 it is better to write this as (not (ior A B)) so we can split it.
3368 Similarly for IOR. */
3369 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3370 {
3371 SUBST (*loc,
3372 gen_rtx_NOT (GET_MODE (x),
3373 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3374 GET_MODE (x),
3375 XEXP (XEXP (x, 0), 0),
3376 XEXP (XEXP (x, 1), 0))));
3377 return find_split_point (loc, insn);
3378 }
3379
3380 /* Many RISC machines have a large set of logical insns. If the
3381 second operand is a NOT, put it first so we will try to split the
3382 other operand first. */
3383 if (GET_CODE (XEXP (x, 1)) == NOT)
3384 {
3385 rtx tem = XEXP (x, 0);
3386 SUBST (XEXP (x, 0), XEXP (x, 1));
3387 SUBST (XEXP (x, 1), tem);
3388 }
3389 break;
3390
3391 default:
3392 break;
3393 }
3394
3395 /* Otherwise, select our actions depending on our rtx class. */
3396 switch (GET_RTX_CLASS (code))
3397 {
3398 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3399 case RTX_TERNARY:
3400 split = find_split_point (&XEXP (x, 2), insn);
3401 if (split)
3402 return split;
3403 /* ... fall through ... */
3404 case RTX_BIN_ARITH:
3405 case RTX_COMM_ARITH:
3406 case RTX_COMPARE:
3407 case RTX_COMM_COMPARE:
3408 split = find_split_point (&XEXP (x, 1), insn);
3409 if (split)
3410 return split;
3411 /* ... fall through ... */
3412 case RTX_UNARY:
3413 /* Some machines have (and (shift ...) ...) insns. If X is not
3414 an AND, but XEXP (X, 0) is, use it as our split point. */
3415 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3416 return &XEXP (x, 0);
3417
3418 split = find_split_point (&XEXP (x, 0), insn);
3419 if (split)
3420 return split;
3421 return loc;
3422
3423 default:
3424 /* Otherwise, we don't have a split point. */
3425 return 0;
3426 }
3427 }
3428 \f
3429 /* Throughout X, replace FROM with TO, and return the result.
3430 The result is TO if X is FROM;
3431 otherwise the result is X, but its contents may have been modified.
3432 If they were modified, a record was made in undobuf so that
3433 undo_all will (among other things) return X to its original state.
3434
3435 If the number of changes necessary is too much to record to undo,
3436 the excess changes are not made, so the result is invalid.
3437 The changes already made can still be undone.
3438 undobuf.num_undo is incremented for such changes, so by testing that
3439 the caller can tell whether the result is valid.
3440
3441 `n_occurrences' is incremented each time FROM is replaced.
3442
3443 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3444
3445 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3446 by copying if `n_occurrences' is nonzero. */
3447
3448 static rtx
3449 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3450 {
3451 enum rtx_code code = GET_CODE (x);
3452 enum machine_mode op0_mode = VOIDmode;
3453 const char *fmt;
3454 int len, i;
3455 rtx new;
3456
3457 /* Two expressions are equal if they are identical copies of a shared
3458 RTX or if they are both registers with the same register number
3459 and mode. */
3460
3461 #define COMBINE_RTX_EQUAL_P(X,Y) \
3462 ((X) == (Y) \
3463 || (REG_P (X) && REG_P (Y) \
3464 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3465
3466 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3467 {
3468 n_occurrences++;
3469 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3470 }
3471
3472 /* If X and FROM are the same register but different modes, they will
3473 not have been seen as equal above. However, flow.c will make a
3474 LOG_LINKS entry for that case. If we do nothing, we will try to
3475 rerecognize our original insn and, when it succeeds, we will
3476 delete the feeding insn, which is incorrect.
3477
3478 So force this insn not to match in this (rare) case. */
3479 if (! in_dest && code == REG && REG_P (from)
3480 && REGNO (x) == REGNO (from))
3481 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3482
3483 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3484 of which may contain things that can be combined. */
3485 if (code != MEM && code != LO_SUM && OBJECT_P (x))
3486 return x;
3487
3488 /* It is possible to have a subexpression appear twice in the insn.
3489 Suppose that FROM is a register that appears within TO.
3490 Then, after that subexpression has been scanned once by `subst',
3491 the second time it is scanned, TO may be found. If we were
3492 to scan TO here, we would find FROM within it and create a
3493 self-referent rtl structure which is completely wrong. */
3494 if (COMBINE_RTX_EQUAL_P (x, to))
3495 return to;
3496
3497 /* Parallel asm_operands need special attention because all of the
3498 inputs are shared across the arms. Furthermore, unsharing the
3499 rtl results in recognition failures. Failure to handle this case
3500 specially can result in circular rtl.
3501
3502 Solve this by doing a normal pass across the first entry of the
3503 parallel, and only processing the SET_DESTs of the subsequent
3504 entries. Ug. */
3505
3506 if (code == PARALLEL
3507 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3508 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3509 {
3510 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3511
3512 /* If this substitution failed, this whole thing fails. */
3513 if (GET_CODE (new) == CLOBBER
3514 && XEXP (new, 0) == const0_rtx)
3515 return new;
3516
3517 SUBST (XVECEXP (x, 0, 0), new);
3518
3519 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3520 {
3521 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3522
3523 if (!REG_P (dest)
3524 && GET_CODE (dest) != CC0
3525 && GET_CODE (dest) != PC)
3526 {
3527 new = subst (dest, from, to, 0, unique_copy);
3528
3529 /* If this substitution failed, this whole thing fails. */
3530 if (GET_CODE (new) == CLOBBER
3531 && XEXP (new, 0) == const0_rtx)
3532 return new;
3533
3534 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3535 }
3536 }
3537 }
3538 else
3539 {
3540 len = GET_RTX_LENGTH (code);
3541 fmt = GET_RTX_FORMAT (code);
3542
3543 /* We don't need to process a SET_DEST that is a register, CC0,
3544 or PC, so set up to skip this common case. All other cases
3545 where we want to suppress replacing something inside a
3546 SET_SRC are handled via the IN_DEST operand. */
3547 if (code == SET
3548 && (REG_P (SET_DEST (x))
3549 || GET_CODE (SET_DEST (x)) == CC0
3550 || GET_CODE (SET_DEST (x)) == PC))
3551 fmt = "ie";
3552
3553 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3554 constant. */
3555 if (fmt[0] == 'e')
3556 op0_mode = GET_MODE (XEXP (x, 0));
3557
3558 for (i = 0; i < len; i++)
3559 {
3560 if (fmt[i] == 'E')
3561 {
3562 int j;
3563 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3564 {
3565 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3566 {
3567 new = (unique_copy && n_occurrences
3568 ? copy_rtx (to) : to);
3569 n_occurrences++;
3570 }
3571 else
3572 {
3573 new = subst (XVECEXP (x, i, j), from, to, 0,
3574 unique_copy);
3575
3576 /* If this substitution failed, this whole thing
3577 fails. */
3578 if (GET_CODE (new) == CLOBBER
3579 && XEXP (new, 0) == const0_rtx)
3580 return new;
3581 }
3582
3583 SUBST (XVECEXP (x, i, j), new);
3584 }
3585 }
3586 else if (fmt[i] == 'e')
3587 {
3588 /* If this is a register being set, ignore it. */
3589 new = XEXP (x, i);
3590 if (in_dest
3591 && i == 0
3592 && (((code == SUBREG || code == ZERO_EXTRACT)
3593 && REG_P (new))
3594 || code == STRICT_LOW_PART))
3595 ;
3596
3597 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3598 {
3599 /* In general, don't install a subreg involving two
3600 modes not tieable. It can worsen register
3601 allocation, and can even make invalid reload
3602 insns, since the reg inside may need to be copied
3603 from in the outside mode, and that may be invalid
3604 if it is an fp reg copied in integer mode.
3605
3606 We allow two exceptions to this: It is valid if
3607 it is inside another SUBREG and the mode of that
3608 SUBREG and the mode of the inside of TO is
3609 tieable and it is valid if X is a SET that copies
3610 FROM to CC0. */
3611
3612 if (GET_CODE (to) == SUBREG
3613 && ! MODES_TIEABLE_P (GET_MODE (to),
3614 GET_MODE (SUBREG_REG (to)))
3615 && ! (code == SUBREG
3616 && MODES_TIEABLE_P (GET_MODE (x),
3617 GET_MODE (SUBREG_REG (to))))
3618 #ifdef HAVE_cc0
3619 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3620 #endif
3621 )
3622 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3623
3624 #ifdef CANNOT_CHANGE_MODE_CLASS
3625 if (code == SUBREG
3626 && REG_P (to)
3627 && REGNO (to) < FIRST_PSEUDO_REGISTER
3628 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3629 GET_MODE (to),
3630 GET_MODE (x)))
3631 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3632 #endif
3633
3634 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3635 n_occurrences++;
3636 }
3637 else
3638 /* If we are in a SET_DEST, suppress most cases unless we
3639 have gone inside a MEM, in which case we want to
3640 simplify the address. We assume here that things that
3641 are actually part of the destination have their inner
3642 parts in the first expression. This is true for SUBREG,
3643 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3644 things aside from REG and MEM that should appear in a
3645 SET_DEST. */
3646 new = subst (XEXP (x, i), from, to,
3647 (((in_dest
3648 && (code == SUBREG || code == STRICT_LOW_PART
3649 || code == ZERO_EXTRACT))
3650 || code == SET)
3651 && i == 0), unique_copy);
3652
3653 /* If we found that we will have to reject this combination,
3654 indicate that by returning the CLOBBER ourselves, rather than
3655 an expression containing it. This will speed things up as
3656 well as prevent accidents where two CLOBBERs are considered
3657 to be equal, thus producing an incorrect simplification. */
3658
3659 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3660 return new;
3661
3662 if (GET_CODE (x) == SUBREG
3663 && (GET_CODE (new) == CONST_INT
3664 || GET_CODE (new) == CONST_DOUBLE))
3665 {
3666 enum machine_mode mode = GET_MODE (x);
3667
3668 x = simplify_subreg (GET_MODE (x), new,
3669 GET_MODE (SUBREG_REG (x)),
3670 SUBREG_BYTE (x));
3671 if (! x)
3672 x = gen_rtx_CLOBBER (mode, const0_rtx);
3673 }
3674 else if (GET_CODE (new) == CONST_INT
3675 && GET_CODE (x) == ZERO_EXTEND)
3676 {
3677 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3678 new, GET_MODE (XEXP (x, 0)));
3679 gcc_assert (x);
3680 }
3681 else
3682 SUBST (XEXP (x, i), new);
3683 }
3684 }
3685 }
3686
3687 /* Try to simplify X. If the simplification changed the code, it is likely
3688 that further simplification will help, so loop, but limit the number
3689 of repetitions that will be performed. */
3690
3691 for (i = 0; i < 4; i++)
3692 {
3693 /* If X is sufficiently simple, don't bother trying to do anything
3694 with it. */
3695 if (code != CONST_INT && code != REG && code != CLOBBER)
3696 x = combine_simplify_rtx (x, op0_mode, in_dest);
3697
3698 if (GET_CODE (x) == code)
3699 break;
3700
3701 code = GET_CODE (x);
3702
3703 /* We no longer know the original mode of operand 0 since we
3704 have changed the form of X) */
3705 op0_mode = VOIDmode;
3706 }
3707
3708 return x;
3709 }
3710 \f
3711 /* Simplify X, a piece of RTL. We just operate on the expression at the
3712 outer level; call `subst' to simplify recursively. Return the new
3713 expression.
3714
3715 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
3716 if we are inside a SET_DEST. */
3717
3718 static rtx
3719 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3720 {
3721 enum rtx_code code = GET_CODE (x);
3722 enum machine_mode mode = GET_MODE (x);
3723 rtx temp;
3724 rtx reversed;
3725 int i;
3726
3727 /* If this is a commutative operation, put a constant last and a complex
3728 expression first. We don't need to do this for comparisons here. */
3729 if (COMMUTATIVE_ARITH_P (x)
3730 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3731 {
3732 temp = XEXP (x, 0);
3733 SUBST (XEXP (x, 0), XEXP (x, 1));
3734 SUBST (XEXP (x, 1), temp);
3735 }
3736
3737 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3738 sign extension of a PLUS with a constant, reverse the order of the sign
3739 extension and the addition. Note that this not the same as the original
3740 code, but overflow is undefined for signed values. Also note that the
3741 PLUS will have been partially moved "inside" the sign-extension, so that
3742 the first operand of X will really look like:
3743 (ashiftrt (plus (ashift A C4) C5) C4).
3744 We convert this to
3745 (plus (ashiftrt (ashift A C4) C2) C4)
3746 and replace the first operand of X with that expression. Later parts
3747 of this function may simplify the expression further.
3748
3749 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3750 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3751 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3752
3753 We do this to simplify address expressions. */
3754
3755 if ((code == PLUS || code == MINUS || code == MULT)
3756 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3757 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3758 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3759 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3760 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3761 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3762 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3763 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3764 XEXP (XEXP (XEXP (x, 0), 0), 1),
3765 XEXP (XEXP (x, 0), 1))) != 0)
3766 {
3767 rtx new
3768 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3769 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3770 INTVAL (XEXP (XEXP (x, 0), 1)));
3771
3772 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3773 INTVAL (XEXP (XEXP (x, 0), 1)));
3774
3775 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3776 }
3777
3778 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3779 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3780 things. Check for cases where both arms are testing the same
3781 condition.
3782
3783 Don't do anything if all operands are very simple. */
3784
3785 if ((BINARY_P (x)
3786 && ((!OBJECT_P (XEXP (x, 0))
3787 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3788 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3789 || (!OBJECT_P (XEXP (x, 1))
3790 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3791 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3792 || (UNARY_P (x)
3793 && (!OBJECT_P (XEXP (x, 0))
3794 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3795 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3796 {
3797 rtx cond, true_rtx, false_rtx;
3798
3799 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3800 if (cond != 0
3801 /* If everything is a comparison, what we have is highly unlikely
3802 to be simpler, so don't use it. */
3803 && ! (COMPARISON_P (x)
3804 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3805 {
3806 rtx cop1 = const0_rtx;
3807 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3808
3809 if (cond_code == NE && COMPARISON_P (cond))
3810 return x;
3811
3812 /* Simplify the alternative arms; this may collapse the true and
3813 false arms to store-flag values. Be careful to use copy_rtx
3814 here since true_rtx or false_rtx might share RTL with x as a
3815 result of the if_then_else_cond call above. */
3816 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3817 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3818
3819 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3820 is unlikely to be simpler. */
3821 if (general_operand (true_rtx, VOIDmode)
3822 && general_operand (false_rtx, VOIDmode))
3823 {
3824 enum rtx_code reversed;
3825
3826 /* Restarting if we generate a store-flag expression will cause
3827 us to loop. Just drop through in this case. */
3828
3829 /* If the result values are STORE_FLAG_VALUE and zero, we can
3830 just make the comparison operation. */
3831 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3832 x = gen_binary (cond_code, mode, cond, cop1);
3833 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3834 && ((reversed = reversed_comparison_code_parts
3835 (cond_code, cond, cop1, NULL))
3836 != UNKNOWN))
3837 x = gen_binary (reversed, mode, cond, cop1);
3838
3839 /* Likewise, we can make the negate of a comparison operation
3840 if the result values are - STORE_FLAG_VALUE and zero. */
3841 else if (GET_CODE (true_rtx) == CONST_INT
3842 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3843 && false_rtx == const0_rtx)
3844 x = simplify_gen_unary (NEG, mode,
3845 gen_binary (cond_code, mode, cond,
3846 cop1),
3847 mode);
3848 else if (GET_CODE (false_rtx) == CONST_INT
3849 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3850 && true_rtx == const0_rtx
3851 && ((reversed = reversed_comparison_code_parts
3852 (cond_code, cond, cop1, NULL))
3853 != UNKNOWN))
3854 x = simplify_gen_unary (NEG, mode,
3855 gen_binary (reversed, mode,
3856 cond, cop1),
3857 mode);
3858 else
3859 return gen_rtx_IF_THEN_ELSE (mode,
3860 gen_binary (cond_code, VOIDmode,
3861 cond, cop1),
3862 true_rtx, false_rtx);
3863
3864 code = GET_CODE (x);
3865 op0_mode = VOIDmode;
3866 }
3867 }
3868 }
3869
3870 /* Try to fold this expression in case we have constants that weren't
3871 present before. */
3872 temp = 0;
3873 switch (GET_RTX_CLASS (code))
3874 {
3875 case RTX_UNARY:
3876 if (op0_mode == VOIDmode)
3877 op0_mode = GET_MODE (XEXP (x, 0));
3878 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3879 break;
3880 case RTX_COMPARE:
3881 case RTX_COMM_COMPARE:
3882 {
3883 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3884 if (cmp_mode == VOIDmode)
3885 {
3886 cmp_mode = GET_MODE (XEXP (x, 1));
3887 if (cmp_mode == VOIDmode)
3888 cmp_mode = op0_mode;
3889 }
3890 temp = simplify_relational_operation (code, mode, cmp_mode,
3891 XEXP (x, 0), XEXP (x, 1));
3892 }
3893 break;
3894 case RTX_COMM_ARITH:
3895 case RTX_BIN_ARITH:
3896 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3897 break;
3898 case RTX_BITFIELD_OPS:
3899 case RTX_TERNARY:
3900 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3901 XEXP (x, 1), XEXP (x, 2));
3902 break;
3903 default:
3904 break;
3905 }
3906
3907 if (temp)
3908 {
3909 x = temp;
3910 code = GET_CODE (temp);
3911 op0_mode = VOIDmode;
3912 mode = GET_MODE (temp);
3913 }
3914
3915 /* First see if we can apply the inverse distributive law. */
3916 if (code == PLUS || code == MINUS
3917 || code == AND || code == IOR || code == XOR)
3918 {
3919 x = apply_distributive_law (x);
3920 code = GET_CODE (x);
3921 op0_mode = VOIDmode;
3922 }
3923
3924 /* If CODE is an associative operation not otherwise handled, see if we
3925 can associate some operands. This can win if they are constants or
3926 if they are logically related (i.e. (a & b) & a). */
3927 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3928 || code == AND || code == IOR || code == XOR
3929 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3930 && ((INTEGRAL_MODE_P (mode) && code != DIV)
3931 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3932 {
3933 if (GET_CODE (XEXP (x, 0)) == code)
3934 {
3935 rtx other = XEXP (XEXP (x, 0), 0);
3936 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3937 rtx inner_op1 = XEXP (x, 1);
3938 rtx inner;
3939
3940 /* Make sure we pass the constant operand if any as the second
3941 one if this is a commutative operation. */
3942 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
3943 {
3944 rtx tem = inner_op0;
3945 inner_op0 = inner_op1;
3946 inner_op1 = tem;
3947 }
3948 inner = simplify_binary_operation (code == MINUS ? PLUS
3949 : code == DIV ? MULT
3950 : code,
3951 mode, inner_op0, inner_op1);
3952
3953 /* For commutative operations, try the other pair if that one
3954 didn't simplify. */
3955 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
3956 {
3957 other = XEXP (XEXP (x, 0), 1);
3958 inner = simplify_binary_operation (code, mode,
3959 XEXP (XEXP (x, 0), 0),
3960 XEXP (x, 1));
3961 }
3962
3963 if (inner)
3964 return gen_binary (code, mode, other, inner);
3965 }
3966 }
3967
3968 /* A little bit of algebraic simplification here. */
3969 switch (code)
3970 {
3971 case MEM:
3972 /* Ensure that our address has any ASHIFTs converted to MULT in case
3973 address-recognizing predicates are called later. */
3974 temp = make_compound_operation (XEXP (x, 0), MEM);
3975 SUBST (XEXP (x, 0), temp);
3976 break;
3977
3978 case SUBREG:
3979 if (op0_mode == VOIDmode)
3980 op0_mode = GET_MODE (SUBREG_REG (x));
3981
3982 /* See if this can be moved to simplify_subreg. */
3983 if (CONSTANT_P (SUBREG_REG (x))
3984 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3985 /* Don't call gen_lowpart if the inner mode
3986 is VOIDmode and we cannot simplify it, as SUBREG without
3987 inner mode is invalid. */
3988 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3989 || gen_lowpart_common (mode, SUBREG_REG (x))))
3990 return gen_lowpart (mode, SUBREG_REG (x));
3991
3992 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3993 break;
3994 {
3995 rtx temp;
3996 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3997 SUBREG_BYTE (x));
3998 if (temp)
3999 return temp;
4000 }
4001
4002 /* Don't change the mode of the MEM if that would change the meaning
4003 of the address. */
4004 if (MEM_P (SUBREG_REG (x))
4005 && (MEM_VOLATILE_P (SUBREG_REG (x))
4006 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4007 return gen_rtx_CLOBBER (mode, const0_rtx);
4008
4009 /* Note that we cannot do any narrowing for non-constants since
4010 we might have been counting on using the fact that some bits were
4011 zero. We now do this in the SET. */
4012
4013 break;
4014
4015 case NOT:
4016 if (GET_CODE (XEXP (x, 0)) == SUBREG
4017 && subreg_lowpart_p (XEXP (x, 0))
4018 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
4019 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
4020 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
4021 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
4022 {
4023 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
4024
4025 x = gen_rtx_ROTATE (inner_mode,
4026 simplify_gen_unary (NOT, inner_mode, const1_rtx,
4027 inner_mode),
4028 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
4029 return gen_lowpart (mode, x);
4030 }
4031
4032 /* Apply De Morgan's laws to reduce number of patterns for machines
4033 with negating logical insns (and-not, nand, etc.). If result has
4034 only one NOT, put it first, since that is how the patterns are
4035 coded. */
4036
4037 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
4038 {
4039 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
4040 enum machine_mode op_mode;
4041
4042 op_mode = GET_MODE (in1);
4043 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
4044
4045 op_mode = GET_MODE (in2);
4046 if (op_mode == VOIDmode)
4047 op_mode = mode;
4048 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
4049
4050 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
4051 {
4052 rtx tem = in2;
4053 in2 = in1; in1 = tem;
4054 }
4055
4056 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
4057 mode, in1, in2);
4058 }
4059 break;
4060
4061 case NEG:
4062 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
4063 if (GET_CODE (XEXP (x, 0)) == XOR
4064 && XEXP (XEXP (x, 0), 1) == const1_rtx
4065 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4066 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
4067
4068 temp = expand_compound_operation (XEXP (x, 0));
4069
4070 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4071 replaced by (lshiftrt X C). This will convert
4072 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4073
4074 if (GET_CODE (temp) == ASHIFTRT
4075 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4076 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4077 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4078 INTVAL (XEXP (temp, 1)));
4079
4080 /* If X has only a single bit that might be nonzero, say, bit I, convert
4081 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4082 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4083 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4084 or a SUBREG of one since we'd be making the expression more
4085 complex if it was just a register. */
4086
4087 if (!REG_P (temp)
4088 && ! (GET_CODE (temp) == SUBREG
4089 && REG_P (SUBREG_REG (temp)))
4090 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4091 {
4092 rtx temp1 = simplify_shift_const
4093 (NULL_RTX, ASHIFTRT, mode,
4094 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4095 GET_MODE_BITSIZE (mode) - 1 - i),
4096 GET_MODE_BITSIZE (mode) - 1 - i);
4097
4098 /* If all we did was surround TEMP with the two shifts, we
4099 haven't improved anything, so don't use it. Otherwise,
4100 we are better off with TEMP1. */
4101 if (GET_CODE (temp1) != ASHIFTRT
4102 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4103 || XEXP (XEXP (temp1, 0), 0) != temp)
4104 return temp1;
4105 }
4106 break;
4107
4108 case TRUNCATE:
4109 /* We can't handle truncation to a partial integer mode here
4110 because we don't know the real bitsize of the partial
4111 integer mode. */
4112 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4113 break;
4114
4115 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4116 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4117 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4118 SUBST (XEXP (x, 0),
4119 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4120 GET_MODE_MASK (mode), NULL_RTX, 0));
4121
4122 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
4123 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4124 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4125 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4126 return XEXP (XEXP (x, 0), 0);
4127
4128 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4129 (OP:SI foo:SI) if OP is NEG or ABS. */
4130 if ((GET_CODE (XEXP (x, 0)) == ABS
4131 || GET_CODE (XEXP (x, 0)) == NEG)
4132 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4133 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4134 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4135 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4136 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4137
4138 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4139 (truncate:SI x). */
4140 if (GET_CODE (XEXP (x, 0)) == SUBREG
4141 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4142 && subreg_lowpart_p (XEXP (x, 0)))
4143 return SUBREG_REG (XEXP (x, 0));
4144
4145 /* If we know that the value is already truncated, we can
4146 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4147 is nonzero for the corresponding modes. But don't do this
4148 for an (LSHIFTRT (MULT ...)) since this will cause problems
4149 with the umulXi3_highpart patterns. */
4150 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4151 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4152 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4153 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4154 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4155 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4156 return gen_lowpart (mode, XEXP (x, 0));
4157
4158 /* A truncate of a comparison can be replaced with a subreg if
4159 STORE_FLAG_VALUE permits. This is like the previous test,
4160 but it works even if the comparison is done in a mode larger
4161 than HOST_BITS_PER_WIDE_INT. */
4162 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4163 && COMPARISON_P (XEXP (x, 0))
4164 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4165 return gen_lowpart (mode, XEXP (x, 0));
4166
4167 /* Similarly, a truncate of a register whose value is a
4168 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4169 permits. */
4170 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4171 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4172 && (temp = get_last_value (XEXP (x, 0)))
4173 && COMPARISON_P (temp))
4174 return gen_lowpart (mode, XEXP (x, 0));
4175
4176 break;
4177
4178 case FLOAT_TRUNCATE:
4179 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4180 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4181 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4182 return XEXP (XEXP (x, 0), 0);
4183
4184 /* (float_truncate:SF (float_truncate:DF foo:XF))
4185 = (float_truncate:SF foo:XF).
4186 This may eliminate double rounding, so it is unsafe.
4187
4188 (float_truncate:SF (float_extend:XF foo:DF))
4189 = (float_truncate:SF foo:DF).
4190
4191 (float_truncate:DF (float_extend:XF foo:SF))
4192 = (float_extend:SF foo:DF). */
4193 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4194 && flag_unsafe_math_optimizations)
4195 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4196 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4197 0)))
4198 > GET_MODE_SIZE (mode)
4199 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4200 mode,
4201 XEXP (XEXP (x, 0), 0), mode);
4202
4203 /* (float_truncate (float x)) is (float x) */
4204 if (GET_CODE (XEXP (x, 0)) == FLOAT
4205 && (flag_unsafe_math_optimizations
4206 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4207 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4208 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4209 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4210 return simplify_gen_unary (FLOAT, mode,
4211 XEXP (XEXP (x, 0), 0),
4212 GET_MODE (XEXP (XEXP (x, 0), 0)));
4213
4214 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4215 (OP:SF foo:SF) if OP is NEG or ABS. */
4216 if ((GET_CODE (XEXP (x, 0)) == ABS
4217 || GET_CODE (XEXP (x, 0)) == NEG)
4218 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4219 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4220 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4221 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4222
4223 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4224 is (float_truncate:SF x). */
4225 if (GET_CODE (XEXP (x, 0)) == SUBREG
4226 && subreg_lowpart_p (XEXP (x, 0))
4227 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4228 return SUBREG_REG (XEXP (x, 0));
4229 break;
4230 case FLOAT_EXTEND:
4231 /* (float_extend (float_extend x)) is (float_extend x)
4232
4233 (float_extend (float x)) is (float x) assuming that double
4234 rounding can't happen.
4235 */
4236 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4237 || (GET_CODE (XEXP (x, 0)) == FLOAT
4238 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4239 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4240 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4241 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4242 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4243 XEXP (XEXP (x, 0), 0),
4244 GET_MODE (XEXP (XEXP (x, 0), 0)));
4245
4246 break;
4247 #ifdef HAVE_cc0
4248 case COMPARE:
4249 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4250 using cc0, in which case we want to leave it as a COMPARE
4251 so we can distinguish it from a register-register-copy. */
4252 if (XEXP (x, 1) == const0_rtx)
4253 return XEXP (x, 0);
4254
4255 /* x - 0 is the same as x unless x's mode has signed zeros and
4256 allows rounding towards -infinity. Under those conditions,
4257 0 - 0 is -0. */
4258 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4259 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4260 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4261 return XEXP (x, 0);
4262 break;
4263 #endif
4264
4265 case CONST:
4266 /* (const (const X)) can become (const X). Do it this way rather than
4267 returning the inner CONST since CONST can be shared with a
4268 REG_EQUAL note. */
4269 if (GET_CODE (XEXP (x, 0)) == CONST)
4270 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4271 break;
4272
4273 #ifdef HAVE_lo_sum
4274 case LO_SUM:
4275 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4276 can add in an offset. find_split_point will split this address up
4277 again if it doesn't match. */
4278 if (GET_CODE (XEXP (x, 0)) == HIGH
4279 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4280 return XEXP (x, 1);
4281 break;
4282 #endif
4283
4284 case PLUS:
4285 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4286 */
4287 if (GET_CODE (XEXP (x, 0)) == MULT
4288 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4289 {
4290 rtx in1, in2;
4291
4292 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4293 in2 = XEXP (XEXP (x, 0), 1);
4294 return gen_binary (MINUS, mode, XEXP (x, 1),
4295 gen_binary (MULT, mode, in1, in2));
4296 }
4297
4298 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4299 outermost. That's because that's the way indexed addresses are
4300 supposed to appear. This code used to check many more cases, but
4301 they are now checked elsewhere. */
4302 if (GET_CODE (XEXP (x, 0)) == PLUS
4303 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4304 return gen_binary (PLUS, mode,
4305 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4306 XEXP (x, 1)),
4307 XEXP (XEXP (x, 0), 1));
4308
4309 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4310 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4311 bit-field and can be replaced by either a sign_extend or a
4312 sign_extract. The `and' may be a zero_extend and the two
4313 <c>, -<c> constants may be reversed. */
4314 if (GET_CODE (XEXP (x, 0)) == XOR
4315 && GET_CODE (XEXP (x, 1)) == CONST_INT
4316 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4317 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4318 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4319 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4320 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4321 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4322 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4323 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4324 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4325 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4326 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4327 == (unsigned int) i + 1))))
4328 return simplify_shift_const
4329 (NULL_RTX, ASHIFTRT, mode,
4330 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4331 XEXP (XEXP (XEXP (x, 0), 0), 0),
4332 GET_MODE_BITSIZE (mode) - (i + 1)),
4333 GET_MODE_BITSIZE (mode) - (i + 1));
4334
4335 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4336 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4337 is 1. This produces better code than the alternative immediately
4338 below. */
4339 if (COMPARISON_P (XEXP (x, 0))
4340 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4341 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4342 && (reversed = reversed_comparison (XEXP (x, 0), mode,
4343 XEXP (XEXP (x, 0), 0),
4344 XEXP (XEXP (x, 0), 1))))
4345 return
4346 simplify_gen_unary (NEG, mode, reversed, mode);
4347
4348 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4349 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4350 the bitsize of the mode - 1. This allows simplification of
4351 "a = (b & 8) == 0;" */
4352 if (XEXP (x, 1) == constm1_rtx
4353 && !REG_P (XEXP (x, 0))
4354 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4355 && REG_P (SUBREG_REG (XEXP (x, 0))))
4356 && nonzero_bits (XEXP (x, 0), mode) == 1)
4357 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4358 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4359 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4360 GET_MODE_BITSIZE (mode) - 1),
4361 GET_MODE_BITSIZE (mode) - 1);
4362
4363 /* If we are adding two things that have no bits in common, convert
4364 the addition into an IOR. This will often be further simplified,
4365 for example in cases like ((a & 1) + (a & 2)), which can
4366 become a & 3. */
4367
4368 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4369 && (nonzero_bits (XEXP (x, 0), mode)
4370 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4371 {
4372 /* Try to simplify the expression further. */
4373 rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4374 temp = combine_simplify_rtx (tor, mode, in_dest);
4375
4376 /* If we could, great. If not, do not go ahead with the IOR
4377 replacement, since PLUS appears in many special purpose
4378 address arithmetic instructions. */
4379 if (GET_CODE (temp) != CLOBBER && temp != tor)
4380 return temp;
4381 }
4382 break;
4383
4384 case MINUS:
4385 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4386 by reversing the comparison code if valid. */
4387 if (STORE_FLAG_VALUE == 1
4388 && XEXP (x, 0) == const1_rtx
4389 && COMPARISON_P (XEXP (x, 1))
4390 && (reversed = reversed_comparison (XEXP (x, 1), mode,
4391 XEXP (XEXP (x, 1), 0),
4392 XEXP (XEXP (x, 1), 1))))
4393 return reversed;
4394
4395 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4396 (and <foo> (const_int pow2-1)) */
4397 if (GET_CODE (XEXP (x, 1)) == AND
4398 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4399 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4400 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4401 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4402 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4403
4404 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4405 */
4406 if (GET_CODE (XEXP (x, 1)) == MULT
4407 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4408 {
4409 rtx in1, in2;
4410
4411 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4412 in2 = XEXP (XEXP (x, 1), 1);
4413 return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4414 XEXP (x, 0));
4415 }
4416
4417 /* Canonicalize (minus (neg A) (mult B C)) to
4418 (minus (mult (neg B) C) A). */
4419 if (GET_CODE (XEXP (x, 1)) == MULT
4420 && GET_CODE (XEXP (x, 0)) == NEG)
4421 {
4422 rtx in1, in2;
4423
4424 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4425 in2 = XEXP (XEXP (x, 1), 1);
4426 return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4427 XEXP (XEXP (x, 0), 0));
4428 }
4429
4430 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4431 integers. */
4432 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4433 return gen_binary (MINUS, mode,
4434 gen_binary (MINUS, mode, XEXP (x, 0),
4435 XEXP (XEXP (x, 1), 0)),
4436 XEXP (XEXP (x, 1), 1));
4437 break;
4438
4439 case MULT:
4440 /* If we have (mult (plus A B) C), apply the distributive law and then
4441 the inverse distributive law to see if things simplify. This
4442 occurs mostly in addresses, often when unrolling loops. */
4443
4444 if (GET_CODE (XEXP (x, 0)) == PLUS)
4445 {
4446 x = apply_distributive_law
4447 (gen_binary (PLUS, mode,
4448 gen_binary (MULT, mode,
4449 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4450 gen_binary (MULT, mode,
4451 XEXP (XEXP (x, 0), 1),
4452 copy_rtx (XEXP (x, 1)))));
4453
4454 if (GET_CODE (x) != MULT)
4455 return x;
4456 }
4457 /* Try simplify a*(b/c) as (a*b)/c. */
4458 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4459 && GET_CODE (XEXP (x, 0)) == DIV)
4460 {
4461 rtx tem = simplify_binary_operation (MULT, mode,
4462 XEXP (XEXP (x, 0), 0),
4463 XEXP (x, 1));
4464 if (tem)
4465 return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4466 }
4467 break;
4468
4469 case UDIV:
4470 /* If this is a divide by a power of two, treat it as a shift if
4471 its first operand is a shift. */
4472 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4473 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4474 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4475 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4476 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4477 || GET_CODE (XEXP (x, 0)) == ROTATE
4478 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4479 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4480 break;
4481
4482 case EQ: case NE:
4483 case GT: case GTU: case GE: case GEU:
4484 case LT: case LTU: case LE: case LEU:
4485 case UNEQ: case LTGT:
4486 case UNGT: case UNGE:
4487 case UNLT: case UNLE:
4488 case UNORDERED: case ORDERED:
4489 /* If the first operand is a condition code, we can't do anything
4490 with it. */
4491 if (GET_CODE (XEXP (x, 0)) == COMPARE
4492 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4493 && ! CC0_P (XEXP (x, 0))))
4494 {
4495 rtx op0 = XEXP (x, 0);
4496 rtx op1 = XEXP (x, 1);
4497 enum rtx_code new_code;
4498
4499 if (GET_CODE (op0) == COMPARE)
4500 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4501
4502 /* Simplify our comparison, if possible. */
4503 new_code = simplify_comparison (code, &op0, &op1);
4504
4505 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4506 if only the low-order bit is possibly nonzero in X (such as when
4507 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4508 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4509 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4510 (plus X 1).
4511
4512 Remove any ZERO_EXTRACT we made when thinking this was a
4513 comparison. It may now be simpler to use, e.g., an AND. If a
4514 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4515 the call to make_compound_operation in the SET case. */
4516
4517 if (STORE_FLAG_VALUE == 1
4518 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4519 && op1 == const0_rtx
4520 && mode == GET_MODE (op0)
4521 && nonzero_bits (op0, mode) == 1)
4522 return gen_lowpart (mode,
4523 expand_compound_operation (op0));
4524
4525 else if (STORE_FLAG_VALUE == 1
4526 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4527 && op1 == const0_rtx
4528 && mode == GET_MODE (op0)
4529 && (num_sign_bit_copies (op0, mode)
4530 == GET_MODE_BITSIZE (mode)))
4531 {
4532 op0 = expand_compound_operation (op0);
4533 return simplify_gen_unary (NEG, mode,
4534 gen_lowpart (mode, op0),
4535 mode);
4536 }
4537
4538 else if (STORE_FLAG_VALUE == 1
4539 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4540 && op1 == const0_rtx
4541 && mode == GET_MODE (op0)
4542 && nonzero_bits (op0, mode) == 1)
4543 {
4544 op0 = expand_compound_operation (op0);
4545 return gen_binary (XOR, mode,
4546 gen_lowpart (mode, op0),
4547 const1_rtx);
4548 }
4549
4550 else if (STORE_FLAG_VALUE == 1
4551 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4552 && op1 == const0_rtx
4553 && mode == GET_MODE (op0)
4554 && (num_sign_bit_copies (op0, mode)
4555 == GET_MODE_BITSIZE (mode)))
4556 {
4557 op0 = expand_compound_operation (op0);
4558 return plus_constant (gen_lowpart (mode, op0), 1);
4559 }
4560
4561 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4562 those above. */
4563 if (STORE_FLAG_VALUE == -1
4564 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4565 && op1 == const0_rtx
4566 && (num_sign_bit_copies (op0, mode)
4567 == GET_MODE_BITSIZE (mode)))
4568 return gen_lowpart (mode,
4569 expand_compound_operation (op0));
4570
4571 else if (STORE_FLAG_VALUE == -1
4572 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4573 && op1 == const0_rtx
4574 && mode == GET_MODE (op0)
4575 && nonzero_bits (op0, mode) == 1)
4576 {
4577 op0 = expand_compound_operation (op0);
4578 return simplify_gen_unary (NEG, mode,
4579 gen_lowpart (mode, op0),
4580 mode);
4581 }
4582
4583 else if (STORE_FLAG_VALUE == -1
4584 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4585 && op1 == const0_rtx
4586 && mode == GET_MODE (op0)
4587 && (num_sign_bit_copies (op0, mode)
4588 == GET_MODE_BITSIZE (mode)))
4589 {
4590 op0 = expand_compound_operation (op0);
4591 return simplify_gen_unary (NOT, mode,
4592 gen_lowpart (mode, op0),
4593 mode);
4594 }
4595
4596 /* If X is 0/1, (eq X 0) is X-1. */
4597 else if (STORE_FLAG_VALUE == -1
4598 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4599 && op1 == const0_rtx
4600 && mode == GET_MODE (op0)
4601 && nonzero_bits (op0, mode) == 1)
4602 {
4603 op0 = expand_compound_operation (op0);
4604 return plus_constant (gen_lowpart (mode, op0), -1);
4605 }
4606
4607 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4608 one bit that might be nonzero, we can convert (ne x 0) to
4609 (ashift x c) where C puts the bit in the sign bit. Remove any
4610 AND with STORE_FLAG_VALUE when we are done, since we are only
4611 going to test the sign bit. */
4612 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4613 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4614 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4615 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4616 && op1 == const0_rtx
4617 && mode == GET_MODE (op0)
4618 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4619 {
4620 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4621 expand_compound_operation (op0),
4622 GET_MODE_BITSIZE (mode) - 1 - i);
4623 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4624 return XEXP (x, 0);
4625 else
4626 return x;
4627 }
4628
4629 /* If the code changed, return a whole new comparison. */
4630 if (new_code != code)
4631 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4632
4633 /* Otherwise, keep this operation, but maybe change its operands.
4634 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4635 SUBST (XEXP (x, 0), op0);
4636 SUBST (XEXP (x, 1), op1);
4637 }
4638 break;
4639
4640 case IF_THEN_ELSE:
4641 return simplify_if_then_else (x);
4642
4643 case ZERO_EXTRACT:
4644 case SIGN_EXTRACT:
4645 case ZERO_EXTEND:
4646 case SIGN_EXTEND:
4647 /* If we are processing SET_DEST, we are done. */
4648 if (in_dest)
4649 return x;
4650
4651 return expand_compound_operation (x);
4652
4653 case SET:
4654 return simplify_set (x);
4655
4656 case AND:
4657 case IOR:
4658 case XOR:
4659 return simplify_logical (x);
4660
4661 case ABS:
4662 /* (abs (neg <foo>)) -> (abs <foo>) */
4663 if (GET_CODE (XEXP (x, 0)) == NEG)
4664 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4665
4666 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4667 do nothing. */
4668 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4669 break;
4670
4671 /* If operand is something known to be positive, ignore the ABS. */
4672 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4673 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4674 <= HOST_BITS_PER_WIDE_INT)
4675 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4676 & ((HOST_WIDE_INT) 1
4677 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4678 == 0)))
4679 return XEXP (x, 0);
4680
4681 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4682 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4683 return gen_rtx_NEG (mode, XEXP (x, 0));
4684
4685 break;
4686
4687 case FFS:
4688 /* (ffs (*_extend <X>)) = (ffs <X>) */
4689 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4690 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4691 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4692 break;
4693
4694 case POPCOUNT:
4695 case PARITY:
4696 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4697 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4698 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4699 break;
4700
4701 case FLOAT:
4702 /* (float (sign_extend <X>)) = (float <X>). */
4703 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4704 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4705 break;
4706
4707 case ASHIFT:
4708 case LSHIFTRT:
4709 case ASHIFTRT:
4710 case ROTATE:
4711 case ROTATERT:
4712 /* If this is a shift by a constant amount, simplify it. */
4713 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4714 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4715 INTVAL (XEXP (x, 1)));
4716
4717 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4718 SUBST (XEXP (x, 1),
4719 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4720 ((HOST_WIDE_INT) 1
4721 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4722 - 1,
4723 NULL_RTX, 0));
4724 break;
4725
4726 case VEC_SELECT:
4727 {
4728 rtx op0 = XEXP (x, 0);
4729 rtx op1 = XEXP (x, 1);
4730 int len;
4731
4732 gcc_assert (GET_CODE (op1) == PARALLEL);
4733 len = XVECLEN (op1, 0);
4734 if (len == 1
4735 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4736 && GET_CODE (op0) == VEC_CONCAT)
4737 {
4738 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4739
4740 /* Try to find the element in the VEC_CONCAT. */
4741 for (;;)
4742 {
4743 if (GET_MODE (op0) == GET_MODE (x))
4744 return op0;
4745 if (GET_CODE (op0) == VEC_CONCAT)
4746 {
4747 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4748 if (op0_size < offset)
4749 op0 = XEXP (op0, 0);
4750 else
4751 {
4752 offset -= op0_size;
4753 op0 = XEXP (op0, 1);
4754 }
4755 }
4756 else
4757 break;
4758 }
4759 }
4760 }
4761
4762 break;
4763
4764 default:
4765 break;
4766 }
4767
4768 return x;
4769 }
4770 \f
4771 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4772
4773 static rtx
4774 simplify_if_then_else (rtx x)
4775 {
4776 enum machine_mode mode = GET_MODE (x);
4777 rtx cond = XEXP (x, 0);
4778 rtx true_rtx = XEXP (x, 1);
4779 rtx false_rtx = XEXP (x, 2);
4780 enum rtx_code true_code = GET_CODE (cond);
4781 int comparison_p = COMPARISON_P (cond);
4782 rtx temp;
4783 int i;
4784 enum rtx_code false_code;
4785 rtx reversed;
4786
4787 /* Simplify storing of the truth value. */
4788 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4789 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4790
4791 /* Also when the truth value has to be reversed. */
4792 if (comparison_p
4793 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4794 && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4795 XEXP (cond, 1))))
4796 return reversed;
4797
4798 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4799 in it is being compared against certain values. Get the true and false
4800 comparisons and see if that says anything about the value of each arm. */
4801
4802 if (comparison_p
4803 && ((false_code = combine_reversed_comparison_code (cond))
4804 != UNKNOWN)
4805 && REG_P (XEXP (cond, 0)))
4806 {
4807 HOST_WIDE_INT nzb;
4808 rtx from = XEXP (cond, 0);
4809 rtx true_val = XEXP (cond, 1);
4810 rtx false_val = true_val;
4811 int swapped = 0;
4812
4813 /* If FALSE_CODE is EQ, swap the codes and arms. */
4814
4815 if (false_code == EQ)
4816 {
4817 swapped = 1, true_code = EQ, false_code = NE;
4818 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4819 }
4820
4821 /* If we are comparing against zero and the expression being tested has
4822 only a single bit that might be nonzero, that is its value when it is
4823 not equal to zero. Similarly if it is known to be -1 or 0. */
4824
4825 if (true_code == EQ && true_val == const0_rtx
4826 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4827 false_code = EQ, false_val = GEN_INT (nzb);
4828 else if (true_code == EQ && true_val == const0_rtx
4829 && (num_sign_bit_copies (from, GET_MODE (from))
4830 == GET_MODE_BITSIZE (GET_MODE (from))))
4831 false_code = EQ, false_val = constm1_rtx;
4832
4833 /* Now simplify an arm if we know the value of the register in the
4834 branch and it is used in the arm. Be careful due to the potential
4835 of locally-shared RTL. */
4836
4837 if (reg_mentioned_p (from, true_rtx))
4838 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4839 from, true_val),
4840 pc_rtx, pc_rtx, 0, 0);
4841 if (reg_mentioned_p (from, false_rtx))
4842 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4843 from, false_val),
4844 pc_rtx, pc_rtx, 0, 0);
4845
4846 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4847 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4848
4849 true_rtx = XEXP (x, 1);
4850 false_rtx = XEXP (x, 2);
4851 true_code = GET_CODE (cond);
4852 }
4853
4854 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4855 reversed, do so to avoid needing two sets of patterns for
4856 subtract-and-branch insns. Similarly if we have a constant in the true
4857 arm, the false arm is the same as the first operand of the comparison, or
4858 the false arm is more complicated than the true arm. */
4859
4860 if (comparison_p
4861 && combine_reversed_comparison_code (cond) != UNKNOWN
4862 && (true_rtx == pc_rtx
4863 || (CONSTANT_P (true_rtx)
4864 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4865 || true_rtx == const0_rtx
4866 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4867 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4868 && !OBJECT_P (false_rtx))
4869 || reg_mentioned_p (true_rtx, false_rtx)
4870 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4871 {
4872 true_code = reversed_comparison_code (cond, NULL);
4873 SUBST (XEXP (x, 0),
4874 reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4875 XEXP (cond, 1)));
4876
4877 SUBST (XEXP (x, 1), false_rtx);
4878 SUBST (XEXP (x, 2), true_rtx);
4879
4880 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4881 cond = XEXP (x, 0);
4882
4883 /* It is possible that the conditional has been simplified out. */
4884 true_code = GET_CODE (cond);
4885 comparison_p = COMPARISON_P (cond);
4886 }
4887
4888 /* If the two arms are identical, we don't need the comparison. */
4889
4890 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4891 return true_rtx;
4892
4893 /* Convert a == b ? b : a to "a". */
4894 if (true_code == EQ && ! side_effects_p (cond)
4895 && !HONOR_NANS (mode)
4896 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4897 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4898 return false_rtx;
4899 else if (true_code == NE && ! side_effects_p (cond)
4900 && !HONOR_NANS (mode)
4901 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4902 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4903 return true_rtx;
4904
4905 /* Look for cases where we have (abs x) or (neg (abs X)). */
4906
4907 if (GET_MODE_CLASS (mode) == MODE_INT
4908 && GET_CODE (false_rtx) == NEG
4909 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4910 && comparison_p
4911 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4912 && ! side_effects_p (true_rtx))
4913 switch (true_code)
4914 {
4915 case GT:
4916 case GE:
4917 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4918 case LT:
4919 case LE:
4920 return
4921 simplify_gen_unary (NEG, mode,
4922 simplify_gen_unary (ABS, mode, true_rtx, mode),
4923 mode);
4924 default:
4925 break;
4926 }
4927
4928 /* Look for MIN or MAX. */
4929
4930 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4931 && comparison_p
4932 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4933 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4934 && ! side_effects_p (cond))
4935 switch (true_code)
4936 {
4937 case GE:
4938 case GT:
4939 return gen_binary (SMAX, mode, true_rtx, false_rtx);
4940 case LE:
4941 case LT:
4942 return gen_binary (SMIN, mode, true_rtx, false_rtx);
4943 case GEU:
4944 case GTU:
4945 return gen_binary (UMAX, mode, true_rtx, false_rtx);
4946 case LEU:
4947 case LTU:
4948 return gen_binary (UMIN, mode, true_rtx, false_rtx);
4949 default:
4950 break;
4951 }
4952
4953 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4954 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4955 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4956 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4957 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4958 neither 1 or -1, but it isn't worth checking for. */
4959
4960 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4961 && comparison_p
4962 && GET_MODE_CLASS (mode) == MODE_INT
4963 && ! side_effects_p (x))
4964 {
4965 rtx t = make_compound_operation (true_rtx, SET);
4966 rtx f = make_compound_operation (false_rtx, SET);
4967 rtx cond_op0 = XEXP (cond, 0);
4968 rtx cond_op1 = XEXP (cond, 1);
4969 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
4970 enum machine_mode m = mode;
4971 rtx z = 0, c1 = NULL_RTX;
4972
4973 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4974 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4975 || GET_CODE (t) == ASHIFT
4976 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4977 && rtx_equal_p (XEXP (t, 0), f))
4978 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4979
4980 /* If an identity-zero op is commutative, check whether there
4981 would be a match if we swapped the operands. */
4982 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4983 || GET_CODE (t) == XOR)
4984 && rtx_equal_p (XEXP (t, 1), f))
4985 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4986 else if (GET_CODE (t) == SIGN_EXTEND
4987 && (GET_CODE (XEXP (t, 0)) == PLUS
4988 || GET_CODE (XEXP (t, 0)) == MINUS
4989 || GET_CODE (XEXP (t, 0)) == IOR
4990 || GET_CODE (XEXP (t, 0)) == XOR
4991 || GET_CODE (XEXP (t, 0)) == ASHIFT
4992 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4993 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4994 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4995 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4996 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4997 && (num_sign_bit_copies (f, GET_MODE (f))
4998 > (unsigned int)
4999 (GET_MODE_BITSIZE (mode)
5000 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5001 {
5002 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5003 extend_op = SIGN_EXTEND;
5004 m = GET_MODE (XEXP (t, 0));
5005 }
5006 else if (GET_CODE (t) == SIGN_EXTEND
5007 && (GET_CODE (XEXP (t, 0)) == PLUS
5008 || GET_CODE (XEXP (t, 0)) == IOR
5009 || GET_CODE (XEXP (t, 0)) == XOR)
5010 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5011 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5012 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5013 && (num_sign_bit_copies (f, GET_MODE (f))
5014 > (unsigned int)
5015 (GET_MODE_BITSIZE (mode)
5016 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5017 {
5018 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5019 extend_op = SIGN_EXTEND;
5020 m = GET_MODE (XEXP (t, 0));
5021 }
5022 else if (GET_CODE (t) == ZERO_EXTEND
5023 && (GET_CODE (XEXP (t, 0)) == PLUS
5024 || GET_CODE (XEXP (t, 0)) == MINUS
5025 || GET_CODE (XEXP (t, 0)) == IOR
5026 || GET_CODE (XEXP (t, 0)) == XOR
5027 || GET_CODE (XEXP (t, 0)) == ASHIFT
5028 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5029 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5030 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5031 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5032 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5033 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5034 && ((nonzero_bits (f, GET_MODE (f))
5035 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5036 == 0))
5037 {
5038 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5039 extend_op = ZERO_EXTEND;
5040 m = GET_MODE (XEXP (t, 0));
5041 }
5042 else if (GET_CODE (t) == ZERO_EXTEND
5043 && (GET_CODE (XEXP (t, 0)) == PLUS
5044 || GET_CODE (XEXP (t, 0)) == IOR
5045 || GET_CODE (XEXP (t, 0)) == XOR)
5046 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5047 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5048 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5049 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5050 && ((nonzero_bits (f, GET_MODE (f))
5051 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5052 == 0))
5053 {
5054 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5055 extend_op = ZERO_EXTEND;
5056 m = GET_MODE (XEXP (t, 0));
5057 }
5058
5059 if (z)
5060 {
5061 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
5062 pc_rtx, pc_rtx, 0, 0);
5063 temp = gen_binary (MULT, m, temp,
5064 gen_binary (MULT, m, c1, const_true_rtx));
5065 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5066 temp = gen_binary (op, m, gen_lowpart (m, z), temp);
5067
5068 if (extend_op != UNKNOWN)
5069 temp = simplify_gen_unary (extend_op, mode, temp, m);
5070
5071 return temp;
5072 }
5073 }
5074
5075 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5076 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5077 negation of a single bit, we can convert this operation to a shift. We
5078 can actually do this more generally, but it doesn't seem worth it. */
5079
5080 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5081 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5082 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5083 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5084 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5085 == GET_MODE_BITSIZE (mode))
5086 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5087 return
5088 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5089 gen_lowpart (mode, XEXP (cond, 0)), i);
5090
5091 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5092 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5093 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5094 && GET_MODE (XEXP (cond, 0)) == mode
5095 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5096 == nonzero_bits (XEXP (cond, 0), mode)
5097 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5098 return XEXP (cond, 0);
5099
5100 return x;
5101 }
5102 \f
5103 /* Simplify X, a SET expression. Return the new expression. */
5104
5105 static rtx
5106 simplify_set (rtx x)
5107 {
5108 rtx src = SET_SRC (x);
5109 rtx dest = SET_DEST (x);
5110 enum machine_mode mode
5111 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5112 rtx other_insn;
5113 rtx *cc_use;
5114
5115 /* (set (pc) (return)) gets written as (return). */
5116 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5117 return src;
5118
5119 /* Now that we know for sure which bits of SRC we are using, see if we can
5120 simplify the expression for the object knowing that we only need the
5121 low-order bits. */
5122
5123 if (GET_MODE_CLASS (mode) == MODE_INT
5124 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5125 {
5126 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5127 SUBST (SET_SRC (x), src);
5128 }
5129
5130 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5131 the comparison result and try to simplify it unless we already have used
5132 undobuf.other_insn. */
5133 if ((GET_MODE_CLASS (mode) == MODE_CC
5134 || GET_CODE (src) == COMPARE
5135 || CC0_P (dest))
5136 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5137 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5138 && COMPARISON_P (*cc_use)
5139 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5140 {
5141 enum rtx_code old_code = GET_CODE (*cc_use);
5142 enum rtx_code new_code;
5143 rtx op0, op1, tmp;
5144 int other_changed = 0;
5145 enum machine_mode compare_mode = GET_MODE (dest);
5146
5147 if (GET_CODE (src) == COMPARE)
5148 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5149 else
5150 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5151
5152 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5153 op0, op1);
5154 if (!tmp)
5155 new_code = old_code;
5156 else if (!CONSTANT_P (tmp))
5157 {
5158 new_code = GET_CODE (tmp);
5159 op0 = XEXP (tmp, 0);
5160 op1 = XEXP (tmp, 1);
5161 }
5162 else
5163 {
5164 rtx pat = PATTERN (other_insn);
5165 undobuf.other_insn = other_insn;
5166 SUBST (*cc_use, tmp);
5167
5168 /* Attempt to simplify CC user. */
5169 if (GET_CODE (pat) == SET)
5170 {
5171 rtx new = simplify_rtx (SET_SRC (pat));
5172 if (new != NULL_RTX)
5173 SUBST (SET_SRC (pat), new);
5174 }
5175
5176 /* Convert X into a no-op move. */
5177 SUBST (SET_DEST (x), pc_rtx);
5178 SUBST (SET_SRC (x), pc_rtx);
5179 return x;
5180 }
5181
5182 /* Simplify our comparison, if possible. */
5183 new_code = simplify_comparison (new_code, &op0, &op1);
5184
5185 #ifdef SELECT_CC_MODE
5186 /* If this machine has CC modes other than CCmode, check to see if we
5187 need to use a different CC mode here. */
5188 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5189 compare_mode = GET_MODE (op0);
5190 else
5191 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5192
5193 #ifndef HAVE_cc0
5194 /* If the mode changed, we have to change SET_DEST, the mode in the
5195 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5196 a hard register, just build new versions with the proper mode. If it
5197 is a pseudo, we lose unless it is only time we set the pseudo, in
5198 which case we can safely change its mode. */
5199 if (compare_mode != GET_MODE (dest))
5200 {
5201 unsigned int regno = REGNO (dest);
5202 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5203
5204 if (regno < FIRST_PSEUDO_REGISTER
5205 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5206 {
5207 if (regno >= FIRST_PSEUDO_REGISTER)
5208 SUBST (regno_reg_rtx[regno], new_dest);
5209
5210 SUBST (SET_DEST (x), new_dest);
5211 SUBST (XEXP (*cc_use, 0), new_dest);
5212 other_changed = 1;
5213
5214 dest = new_dest;
5215 }
5216 }
5217 #endif /* cc0 */
5218 #endif /* SELECT_CC_MODE */
5219
5220 /* If the code changed, we have to build a new comparison in
5221 undobuf.other_insn. */
5222 if (new_code != old_code)
5223 {
5224 int other_changed_previously = other_changed;
5225 unsigned HOST_WIDE_INT mask;
5226
5227 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5228 dest, const0_rtx));
5229 other_changed = 1;
5230
5231 /* If the only change we made was to change an EQ into an NE or
5232 vice versa, OP0 has only one bit that might be nonzero, and OP1
5233 is zero, check if changing the user of the condition code will
5234 produce a valid insn. If it won't, we can keep the original code
5235 in that insn by surrounding our operation with an XOR. */
5236
5237 if (((old_code == NE && new_code == EQ)
5238 || (old_code == EQ && new_code == NE))
5239 && ! other_changed_previously && op1 == const0_rtx
5240 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5241 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5242 {
5243 rtx pat = PATTERN (other_insn), note = 0;
5244
5245 if ((recog_for_combine (&pat, other_insn, &note) < 0
5246 && ! check_asm_operands (pat)))
5247 {
5248 PUT_CODE (*cc_use, old_code);
5249 other_changed = 0;
5250
5251 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5252 }
5253 }
5254 }
5255
5256 if (other_changed)
5257 undobuf.other_insn = other_insn;
5258
5259 #ifdef HAVE_cc0
5260 /* If we are now comparing against zero, change our source if
5261 needed. If we do not use cc0, we always have a COMPARE. */
5262 if (op1 == const0_rtx && dest == cc0_rtx)
5263 {
5264 SUBST (SET_SRC (x), op0);
5265 src = op0;
5266 }
5267 else
5268 #endif
5269
5270 /* Otherwise, if we didn't previously have a COMPARE in the
5271 correct mode, we need one. */
5272 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5273 {
5274 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5275 src = SET_SRC (x);
5276 }
5277 else
5278 {
5279 /* Otherwise, update the COMPARE if needed. */
5280 SUBST (XEXP (src, 0), op0);
5281 SUBST (XEXP (src, 1), op1);
5282 }
5283 }
5284 else
5285 {
5286 /* Get SET_SRC in a form where we have placed back any
5287 compound expressions. Then do the checks below. */
5288 src = make_compound_operation (src, SET);
5289 SUBST (SET_SRC (x), src);
5290 }
5291
5292 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5293 and X being a REG or (subreg (reg)), we may be able to convert this to
5294 (set (subreg:m2 x) (op)).
5295
5296 We can always do this if M1 is narrower than M2 because that means that
5297 we only care about the low bits of the result.
5298
5299 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5300 perform a narrower operation than requested since the high-order bits will
5301 be undefined. On machine where it is defined, this transformation is safe
5302 as long as M1 and M2 have the same number of words. */
5303
5304 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5305 && !OBJECT_P (SUBREG_REG (src))
5306 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5307 / UNITS_PER_WORD)
5308 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5309 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5310 #ifndef WORD_REGISTER_OPERATIONS
5311 && (GET_MODE_SIZE (GET_MODE (src))
5312 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5313 #endif
5314 #ifdef CANNOT_CHANGE_MODE_CLASS
5315 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5316 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5317 GET_MODE (SUBREG_REG (src)),
5318 GET_MODE (src)))
5319 #endif
5320 && (REG_P (dest)
5321 || (GET_CODE (dest) == SUBREG
5322 && REG_P (SUBREG_REG (dest)))))
5323 {
5324 SUBST (SET_DEST (x),
5325 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5326 dest));
5327 SUBST (SET_SRC (x), SUBREG_REG (src));
5328
5329 src = SET_SRC (x), dest = SET_DEST (x);
5330 }
5331
5332 #ifdef HAVE_cc0
5333 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5334 in SRC. */
5335 if (dest == cc0_rtx
5336 && GET_CODE (src) == SUBREG
5337 && subreg_lowpart_p (src)
5338 && (GET_MODE_BITSIZE (GET_MODE (src))
5339 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5340 {
5341 rtx inner = SUBREG_REG (src);
5342 enum machine_mode inner_mode = GET_MODE (inner);
5343
5344 /* Here we make sure that we don't have a sign bit on. */
5345 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5346 && (nonzero_bits (inner, inner_mode)
5347 < ((unsigned HOST_WIDE_INT) 1
5348 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5349 {
5350 SUBST (SET_SRC (x), inner);
5351 src = SET_SRC (x);
5352 }
5353 }
5354 #endif
5355
5356 #ifdef LOAD_EXTEND_OP
5357 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5358 would require a paradoxical subreg. Replace the subreg with a
5359 zero_extend to avoid the reload that would otherwise be required. */
5360
5361 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5362 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5363 && SUBREG_BYTE (src) == 0
5364 && (GET_MODE_SIZE (GET_MODE (src))
5365 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5366 && MEM_P (SUBREG_REG (src)))
5367 {
5368 SUBST (SET_SRC (x),
5369 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5370 GET_MODE (src), SUBREG_REG (src)));
5371
5372 src = SET_SRC (x);
5373 }
5374 #endif
5375
5376 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5377 are comparing an item known to be 0 or -1 against 0, use a logical
5378 operation instead. Check for one of the arms being an IOR of the other
5379 arm with some value. We compute three terms to be IOR'ed together. In
5380 practice, at most two will be nonzero. Then we do the IOR's. */
5381
5382 if (GET_CODE (dest) != PC
5383 && GET_CODE (src) == IF_THEN_ELSE
5384 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5385 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5386 && XEXP (XEXP (src, 0), 1) == const0_rtx
5387 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5388 #ifdef HAVE_conditional_move
5389 && ! can_conditionally_move_p (GET_MODE (src))
5390 #endif
5391 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5392 GET_MODE (XEXP (XEXP (src, 0), 0)))
5393 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5394 && ! side_effects_p (src))
5395 {
5396 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5397 ? XEXP (src, 1) : XEXP (src, 2));
5398 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5399 ? XEXP (src, 2) : XEXP (src, 1));
5400 rtx term1 = const0_rtx, term2, term3;
5401
5402 if (GET_CODE (true_rtx) == IOR
5403 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5404 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5405 else if (GET_CODE (true_rtx) == IOR
5406 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5407 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5408 else if (GET_CODE (false_rtx) == IOR
5409 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5410 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5411 else if (GET_CODE (false_rtx) == IOR
5412 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5413 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5414
5415 term2 = gen_binary (AND, GET_MODE (src),
5416 XEXP (XEXP (src, 0), 0), true_rtx);
5417 term3 = gen_binary (AND, GET_MODE (src),
5418 simplify_gen_unary (NOT, GET_MODE (src),
5419 XEXP (XEXP (src, 0), 0),
5420 GET_MODE (src)),
5421 false_rtx);
5422
5423 SUBST (SET_SRC (x),
5424 gen_binary (IOR, GET_MODE (src),
5425 gen_binary (IOR, GET_MODE (src), term1, term2),
5426 term3));
5427
5428 src = SET_SRC (x);
5429 }
5430
5431 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5432 whole thing fail. */
5433 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5434 return src;
5435 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5436 return dest;
5437 else
5438 /* Convert this into a field assignment operation, if possible. */
5439 return make_field_assignment (x);
5440 }
5441 \f
5442 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5443 result. */
5444
5445 static rtx
5446 simplify_logical (rtx x)
5447 {
5448 enum machine_mode mode = GET_MODE (x);
5449 rtx op0 = XEXP (x, 0);
5450 rtx op1 = XEXP (x, 1);
5451 rtx reversed;
5452
5453 switch (GET_CODE (x))
5454 {
5455 case AND:
5456 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5457 insn (and may simplify more). */
5458 if (GET_CODE (op0) == XOR
5459 && rtx_equal_p (XEXP (op0, 0), op1)
5460 && ! side_effects_p (op1))
5461 x = gen_binary (AND, mode,
5462 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5463 op1);
5464
5465 if (GET_CODE (op0) == XOR
5466 && rtx_equal_p (XEXP (op0, 1), op1)
5467 && ! side_effects_p (op1))
5468 x = gen_binary (AND, mode,
5469 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5470 op1);
5471
5472 /* Similarly for (~(A ^ B)) & A. */
5473 if (GET_CODE (op0) == NOT
5474 && GET_CODE (XEXP (op0, 0)) == XOR
5475 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5476 && ! side_effects_p (op1))
5477 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5478
5479 if (GET_CODE (op0) == NOT
5480 && GET_CODE (XEXP (op0, 0)) == XOR
5481 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5482 && ! side_effects_p (op1))
5483 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5484
5485 /* We can call simplify_and_const_int only if we don't lose
5486 any (sign) bits when converting INTVAL (op1) to
5487 "unsigned HOST_WIDE_INT". */
5488 if (GET_CODE (op1) == CONST_INT
5489 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5490 || INTVAL (op1) > 0))
5491 {
5492 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5493
5494 /* If we have (ior (and (X C1) C2)) and the next restart would be
5495 the last, simplify this by making C1 as small as possible
5496 and then exit. Only do this if C1 actually changes: for now
5497 this only saves memory but, should this transformation be
5498 moved to simplify-rtx.c, we'd risk unbounded recursion there. */
5499 if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5500 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5501 && GET_CODE (op1) == CONST_INT
5502 && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5503 return gen_binary (IOR, mode,
5504 gen_binary (AND, mode, XEXP (op0, 0),
5505 GEN_INT (INTVAL (XEXP (op0, 1))
5506 & ~INTVAL (op1))), op1);
5507
5508 if (GET_CODE (x) != AND)
5509 return x;
5510
5511 op0 = XEXP (x, 0);
5512 op1 = XEXP (x, 1);
5513 }
5514
5515 /* Convert (A | B) & A to A. */
5516 if (GET_CODE (op0) == IOR
5517 && (rtx_equal_p (XEXP (op0, 0), op1)
5518 || rtx_equal_p (XEXP (op0, 1), op1))
5519 && ! side_effects_p (XEXP (op0, 0))
5520 && ! side_effects_p (XEXP (op0, 1)))
5521 return op1;
5522
5523 /* In the following group of tests (and those in case IOR below),
5524 we start with some combination of logical operations and apply
5525 the distributive law followed by the inverse distributive law.
5526 Most of the time, this results in no change. However, if some of
5527 the operands are the same or inverses of each other, simplifications
5528 will result.
5529
5530 For example, (and (ior A B) (not B)) can occur as the result of
5531 expanding a bit field assignment. When we apply the distributive
5532 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5533 which then simplifies to (and (A (not B))).
5534
5535 If we have (and (ior A B) C), apply the distributive law and then
5536 the inverse distributive law to see if things simplify. */
5537
5538 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5539 {
5540 x = apply_distributive_law
5541 (gen_binary (GET_CODE (op0), mode,
5542 gen_binary (AND, mode, XEXP (op0, 0), op1),
5543 gen_binary (AND, mode, XEXP (op0, 1),
5544 copy_rtx (op1))));
5545 if (GET_CODE (x) != AND)
5546 return x;
5547 }
5548
5549 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5550 return apply_distributive_law
5551 (gen_binary (GET_CODE (op1), mode,
5552 gen_binary (AND, mode, XEXP (op1, 0), op0),
5553 gen_binary (AND, mode, XEXP (op1, 1),
5554 copy_rtx (op0))));
5555
5556 /* Similarly, taking advantage of the fact that
5557 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5558
5559 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5560 return apply_distributive_law
5561 (gen_binary (XOR, mode,
5562 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5563 gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5564 XEXP (op1, 1))));
5565
5566 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5567 return apply_distributive_law
5568 (gen_binary (XOR, mode,
5569 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5570 gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5571 break;
5572
5573 case IOR:
5574 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5575 if (GET_CODE (op1) == CONST_INT
5576 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5577 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5578 return op1;
5579
5580 /* Convert (A & B) | A to A. */
5581 if (GET_CODE (op0) == AND
5582 && (rtx_equal_p (XEXP (op0, 0), op1)
5583 || rtx_equal_p (XEXP (op0, 1), op1))
5584 && ! side_effects_p (XEXP (op0, 0))
5585 && ! side_effects_p (XEXP (op0, 1)))
5586 return op1;
5587
5588 /* If we have (ior (and A B) C), apply the distributive law and then
5589 the inverse distributive law to see if things simplify. */
5590
5591 if (GET_CODE (op0) == AND)
5592 {
5593 rtx tmp = apply_distributive_law
5594 (gen_binary (AND, mode,
5595 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5596 gen_binary (IOR, mode, XEXP (op0, 1),
5597 copy_rtx (op1))));
5598
5599 if (GET_CODE (tmp) != IOR
5600 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
5601 return tmp;
5602 }
5603
5604 if (GET_CODE (op1) == AND)
5605 {
5606 rtx tmp = apply_distributive_law
5607 (gen_binary (AND, mode,
5608 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5609 gen_binary (IOR, mode, XEXP (op1, 1),
5610 copy_rtx (op0))));
5611
5612 if (GET_CODE (tmp) != IOR
5613 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
5614 return tmp;
5615 }
5616
5617 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5618 mode size to (rotate A CX). */
5619
5620 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5621 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5622 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5623 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5624 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5625 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5626 == GET_MODE_BITSIZE (mode)))
5627 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5628 (GET_CODE (op0) == ASHIFT
5629 ? XEXP (op0, 1) : XEXP (op1, 1)));
5630
5631 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5632 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5633 does not affect any of the bits in OP1, it can really be done
5634 as a PLUS and we can associate. We do this by seeing if OP1
5635 can be safely shifted left C bits. */
5636 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5637 && GET_CODE (XEXP (op0, 0)) == PLUS
5638 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5639 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5640 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5641 {
5642 int count = INTVAL (XEXP (op0, 1));
5643 HOST_WIDE_INT mask = INTVAL (op1) << count;
5644
5645 if (mask >> count == INTVAL (op1)
5646 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5647 {
5648 SUBST (XEXP (XEXP (op0, 0), 1),
5649 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5650 return op0;
5651 }
5652 }
5653 break;
5654
5655 case XOR:
5656 /* If we are XORing two things that have no bits in common,
5657 convert them into an IOR. This helps to detect rotation encoded
5658 using those methods and possibly other simplifications. */
5659
5660 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5661 && (nonzero_bits (op0, mode)
5662 & nonzero_bits (op1, mode)) == 0)
5663 return (gen_binary (IOR, mode, op0, op1));
5664
5665 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5666 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5667 (NOT y). */
5668 {
5669 int num_negated = 0;
5670
5671 if (GET_CODE (op0) == NOT)
5672 num_negated++, op0 = XEXP (op0, 0);
5673 if (GET_CODE (op1) == NOT)
5674 num_negated++, op1 = XEXP (op1, 0);
5675
5676 if (num_negated == 2)
5677 {
5678 SUBST (XEXP (x, 0), op0);
5679 SUBST (XEXP (x, 1), op1);
5680 }
5681 else if (num_negated == 1)
5682 return
5683 simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5684 mode);
5685 }
5686
5687 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5688 correspond to a machine insn or result in further simplifications
5689 if B is a constant. */
5690
5691 if (GET_CODE (op0) == AND
5692 && rtx_equal_p (XEXP (op0, 1), op1)
5693 && ! side_effects_p (op1))
5694 return gen_binary (AND, mode,
5695 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5696 op1);
5697
5698 else if (GET_CODE (op0) == AND
5699 && rtx_equal_p (XEXP (op0, 0), op1)
5700 && ! side_effects_p (op1))
5701 return gen_binary (AND, mode,
5702 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5703 op1);
5704
5705 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5706 comparison if STORE_FLAG_VALUE is 1. */
5707 if (STORE_FLAG_VALUE == 1
5708 && op1 == const1_rtx
5709 && COMPARISON_P (op0)
5710 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5711 XEXP (op0, 1))))
5712 return reversed;
5713
5714 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5715 is (lt foo (const_int 0)), so we can perform the above
5716 simplification if STORE_FLAG_VALUE is 1. */
5717
5718 if (STORE_FLAG_VALUE == 1
5719 && op1 == const1_rtx
5720 && GET_CODE (op0) == LSHIFTRT
5721 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5722 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5723 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5724
5725 /* (xor (comparison foo bar) (const_int sign-bit))
5726 when STORE_FLAG_VALUE is the sign bit. */
5727 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5728 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5729 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5730 && op1 == const_true_rtx
5731 && COMPARISON_P (op0)
5732 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5733 XEXP (op0, 1))))
5734 return reversed;
5735
5736 break;
5737
5738 default:
5739 gcc_unreachable ();
5740 }
5741
5742 return x;
5743 }
5744 \f
5745 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5746 operations" because they can be replaced with two more basic operations.
5747 ZERO_EXTEND is also considered "compound" because it can be replaced with
5748 an AND operation, which is simpler, though only one operation.
5749
5750 The function expand_compound_operation is called with an rtx expression
5751 and will convert it to the appropriate shifts and AND operations,
5752 simplifying at each stage.
5753
5754 The function make_compound_operation is called to convert an expression
5755 consisting of shifts and ANDs into the equivalent compound expression.
5756 It is the inverse of this function, loosely speaking. */
5757
5758 static rtx
5759 expand_compound_operation (rtx x)
5760 {
5761 unsigned HOST_WIDE_INT pos = 0, len;
5762 int unsignedp = 0;
5763 unsigned int modewidth;
5764 rtx tem;
5765
5766 switch (GET_CODE (x))
5767 {
5768 case ZERO_EXTEND:
5769 unsignedp = 1;
5770 case SIGN_EXTEND:
5771 /* We can't necessarily use a const_int for a multiword mode;
5772 it depends on implicitly extending the value.
5773 Since we don't know the right way to extend it,
5774 we can't tell whether the implicit way is right.
5775
5776 Even for a mode that is no wider than a const_int,
5777 we can't win, because we need to sign extend one of its bits through
5778 the rest of it, and we don't know which bit. */
5779 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5780 return x;
5781
5782 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5783 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5784 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5785 reloaded. If not for that, MEM's would very rarely be safe.
5786
5787 Reject MODEs bigger than a word, because we might not be able
5788 to reference a two-register group starting with an arbitrary register
5789 (and currently gen_lowpart might crash for a SUBREG). */
5790
5791 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5792 return x;
5793
5794 /* Reject MODEs that aren't scalar integers because turning vector
5795 or complex modes into shifts causes problems. */
5796
5797 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5798 return x;
5799
5800 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5801 /* If the inner object has VOIDmode (the only way this can happen
5802 is if it is an ASM_OPERANDS), we can't do anything since we don't
5803 know how much masking to do. */
5804 if (len == 0)
5805 return x;
5806
5807 break;
5808
5809 case ZERO_EXTRACT:
5810 unsignedp = 1;
5811
5812 /* ... fall through ... */
5813
5814 case SIGN_EXTRACT:
5815 /* If the operand is a CLOBBER, just return it. */
5816 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5817 return XEXP (x, 0);
5818
5819 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5820 || GET_CODE (XEXP (x, 2)) != CONST_INT
5821 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5822 return x;
5823
5824 /* Reject MODEs that aren't scalar integers because turning vector
5825 or complex modes into shifts causes problems. */
5826
5827 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5828 return x;
5829
5830 len = INTVAL (XEXP (x, 1));
5831 pos = INTVAL (XEXP (x, 2));
5832
5833 /* If this goes outside the object being extracted, replace the object
5834 with a (use (mem ...)) construct that only combine understands
5835 and is used only for this purpose. */
5836 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5837 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5838
5839 if (BITS_BIG_ENDIAN)
5840 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5841
5842 break;
5843
5844 default:
5845 return x;
5846 }
5847 /* Convert sign extension to zero extension, if we know that the high
5848 bit is not set, as this is easier to optimize. It will be converted
5849 back to cheaper alternative in make_extraction. */
5850 if (GET_CODE (x) == SIGN_EXTEND
5851 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5852 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5853 & ~(((unsigned HOST_WIDE_INT)
5854 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5855 >> 1))
5856 == 0)))
5857 {
5858 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5859 rtx temp2 = expand_compound_operation (temp);
5860
5861 /* Make sure this is a profitable operation. */
5862 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5863 return temp2;
5864 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5865 return temp;
5866 else
5867 return x;
5868 }
5869
5870 /* We can optimize some special cases of ZERO_EXTEND. */
5871 if (GET_CODE (x) == ZERO_EXTEND)
5872 {
5873 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5874 know that the last value didn't have any inappropriate bits
5875 set. */
5876 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5877 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5878 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5879 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5880 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5881 return XEXP (XEXP (x, 0), 0);
5882
5883 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5884 if (GET_CODE (XEXP (x, 0)) == SUBREG
5885 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5886 && subreg_lowpart_p (XEXP (x, 0))
5887 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5888 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5889 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5890 return SUBREG_REG (XEXP (x, 0));
5891
5892 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5893 is a comparison and STORE_FLAG_VALUE permits. This is like
5894 the first case, but it works even when GET_MODE (x) is larger
5895 than HOST_WIDE_INT. */
5896 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5897 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5898 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5899 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5900 <= HOST_BITS_PER_WIDE_INT)
5901 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5902 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5903 return XEXP (XEXP (x, 0), 0);
5904
5905 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5906 if (GET_CODE (XEXP (x, 0)) == SUBREG
5907 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5908 && subreg_lowpart_p (XEXP (x, 0))
5909 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5910 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5911 <= HOST_BITS_PER_WIDE_INT)
5912 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5913 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5914 return SUBREG_REG (XEXP (x, 0));
5915
5916 }
5917
5918 /* If we reach here, we want to return a pair of shifts. The inner
5919 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5920 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5921 logical depending on the value of UNSIGNEDP.
5922
5923 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5924 converted into an AND of a shift.
5925
5926 We must check for the case where the left shift would have a negative
5927 count. This can happen in a case like (x >> 31) & 255 on machines
5928 that can't shift by a constant. On those machines, we would first
5929 combine the shift with the AND to produce a variable-position
5930 extraction. Then the constant of 31 would be substituted in to produce
5931 a such a position. */
5932
5933 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5934 if (modewidth + len >= pos)
5935 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5936 GET_MODE (x),
5937 simplify_shift_const (NULL_RTX, ASHIFT,
5938 GET_MODE (x),
5939 XEXP (x, 0),
5940 modewidth - pos - len),
5941 modewidth - len);
5942
5943 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5944 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5945 simplify_shift_const (NULL_RTX, LSHIFTRT,
5946 GET_MODE (x),
5947 XEXP (x, 0), pos),
5948 ((HOST_WIDE_INT) 1 << len) - 1);
5949 else
5950 /* Any other cases we can't handle. */
5951 return x;
5952
5953 /* If we couldn't do this for some reason, return the original
5954 expression. */
5955 if (GET_CODE (tem) == CLOBBER)
5956 return x;
5957
5958 return tem;
5959 }
5960 \f
5961 /* X is a SET which contains an assignment of one object into
5962 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5963 or certain SUBREGS). If possible, convert it into a series of
5964 logical operations.
5965
5966 We half-heartedly support variable positions, but do not at all
5967 support variable lengths. */
5968
5969 static rtx
5970 expand_field_assignment (rtx x)
5971 {
5972 rtx inner;
5973 rtx pos; /* Always counts from low bit. */
5974 int len;
5975 rtx mask;
5976 enum machine_mode compute_mode;
5977
5978 /* Loop until we find something we can't simplify. */
5979 while (1)
5980 {
5981 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5982 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5983 {
5984 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5985 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5986 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5987 }
5988 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5989 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5990 {
5991 inner = XEXP (SET_DEST (x), 0);
5992 len = INTVAL (XEXP (SET_DEST (x), 1));
5993 pos = XEXP (SET_DEST (x), 2);
5994
5995 /* If the position is constant and spans the width of INNER,
5996 surround INNER with a USE to indicate this. */
5997 if (GET_CODE (pos) == CONST_INT
5998 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5999 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
6000
6001 if (BITS_BIG_ENDIAN)
6002 {
6003 if (GET_CODE (pos) == CONST_INT)
6004 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6005 - INTVAL (pos));
6006 else if (GET_CODE (pos) == MINUS
6007 && GET_CODE (XEXP (pos, 1)) == CONST_INT
6008 && (INTVAL (XEXP (pos, 1))
6009 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6010 /* If position is ADJUST - X, new position is X. */
6011 pos = XEXP (pos, 0);
6012 else
6013 pos = gen_binary (MINUS, GET_MODE (pos),
6014 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
6015 - len),
6016 pos);
6017 }
6018 }
6019
6020 /* A SUBREG between two modes that occupy the same numbers of words
6021 can be done by moving the SUBREG to the source. */
6022 else if (GET_CODE (SET_DEST (x)) == SUBREG
6023 /* We need SUBREGs to compute nonzero_bits properly. */
6024 && nonzero_sign_valid
6025 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6026 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6027 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6028 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6029 {
6030 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6031 gen_lowpart
6032 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6033 SET_SRC (x)));
6034 continue;
6035 }
6036 else
6037 break;
6038
6039 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6040 inner = SUBREG_REG (inner);
6041
6042 compute_mode = GET_MODE (inner);
6043
6044 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6045 if (! SCALAR_INT_MODE_P (compute_mode))
6046 {
6047 enum machine_mode imode;
6048
6049 /* Don't do anything for vector or complex integral types. */
6050 if (! FLOAT_MODE_P (compute_mode))
6051 break;
6052
6053 /* Try to find an integral mode to pun with. */
6054 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6055 if (imode == BLKmode)
6056 break;
6057
6058 compute_mode = imode;
6059 inner = gen_lowpart (imode, inner);
6060 }
6061
6062 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6063 if (len < HOST_BITS_PER_WIDE_INT)
6064 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6065 else
6066 break;
6067
6068 /* Now compute the equivalent expression. Make a copy of INNER
6069 for the SET_DEST in case it is a MEM into which we will substitute;
6070 we don't want shared RTL in that case. */
6071 x = gen_rtx_SET
6072 (VOIDmode, copy_rtx (inner),
6073 gen_binary (IOR, compute_mode,
6074 gen_binary (AND, compute_mode,
6075 simplify_gen_unary (NOT, compute_mode,
6076 gen_binary (ASHIFT,
6077 compute_mode,
6078 mask, pos),
6079 compute_mode),
6080 inner),
6081 gen_binary (ASHIFT, compute_mode,
6082 gen_binary (AND, compute_mode,
6083 gen_lowpart
6084 (compute_mode, SET_SRC (x)),
6085 mask),
6086 pos)));
6087 }
6088
6089 return x;
6090 }
6091 \f
6092 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6093 it is an RTX that represents a variable starting position; otherwise,
6094 POS is the (constant) starting bit position (counted from the LSB).
6095
6096 INNER may be a USE. This will occur when we started with a bitfield
6097 that went outside the boundary of the object in memory, which is
6098 allowed on most machines. To isolate this case, we produce a USE
6099 whose mode is wide enough and surround the MEM with it. The only
6100 code that understands the USE is this routine. If it is not removed,
6101 it will cause the resulting insn not to match.
6102
6103 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6104 signed reference.
6105
6106 IN_DEST is nonzero if this is a reference in the destination of a
6107 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6108 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6109 be used.
6110
6111 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6112 ZERO_EXTRACT should be built even for bits starting at bit 0.
6113
6114 MODE is the desired mode of the result (if IN_DEST == 0).
6115
6116 The result is an RTX for the extraction or NULL_RTX if the target
6117 can't handle it. */
6118
6119 static rtx
6120 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6121 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6122 int in_dest, int in_compare)
6123 {
6124 /* This mode describes the size of the storage area
6125 to fetch the overall value from. Within that, we
6126 ignore the POS lowest bits, etc. */
6127 enum machine_mode is_mode = GET_MODE (inner);
6128 enum machine_mode inner_mode;
6129 enum machine_mode wanted_inner_mode = byte_mode;
6130 enum machine_mode wanted_inner_reg_mode = word_mode;
6131 enum machine_mode pos_mode = word_mode;
6132 enum machine_mode extraction_mode = word_mode;
6133 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6134 int spans_byte = 0;
6135 rtx new = 0;
6136 rtx orig_pos_rtx = pos_rtx;
6137 HOST_WIDE_INT orig_pos;
6138
6139 /* Get some information about INNER and get the innermost object. */
6140 if (GET_CODE (inner) == USE)
6141 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
6142 /* We don't need to adjust the position because we set up the USE
6143 to pretend that it was a full-word object. */
6144 spans_byte = 1, inner = XEXP (inner, 0);
6145 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6146 {
6147 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6148 consider just the QI as the memory to extract from.
6149 The subreg adds or removes high bits; its mode is
6150 irrelevant to the meaning of this extraction,
6151 since POS and LEN count from the lsb. */
6152 if (MEM_P (SUBREG_REG (inner)))
6153 is_mode = GET_MODE (SUBREG_REG (inner));
6154 inner = SUBREG_REG (inner);
6155 }
6156 else if (GET_CODE (inner) == ASHIFT
6157 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6158 && pos_rtx == 0 && pos == 0
6159 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6160 {
6161 /* We're extracting the least significant bits of an rtx
6162 (ashift X (const_int C)), where LEN > C. Extract the
6163 least significant (LEN - C) bits of X, giving an rtx
6164 whose mode is MODE, then shift it left C times. */
6165 new = make_extraction (mode, XEXP (inner, 0),
6166 0, 0, len - INTVAL (XEXP (inner, 1)),
6167 unsignedp, in_dest, in_compare);
6168 if (new != 0)
6169 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6170 }
6171
6172 inner_mode = GET_MODE (inner);
6173
6174 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6175 pos = INTVAL (pos_rtx), pos_rtx = 0;
6176
6177 /* See if this can be done without an extraction. We never can if the
6178 width of the field is not the same as that of some integer mode. For
6179 registers, we can only avoid the extraction if the position is at the
6180 low-order bit and this is either not in the destination or we have the
6181 appropriate STRICT_LOW_PART operation available.
6182
6183 For MEM, we can avoid an extract if the field starts on an appropriate
6184 boundary and we can change the mode of the memory reference. However,
6185 we cannot directly access the MEM if we have a USE and the underlying
6186 MEM is not TMODE. This combination means that MEM was being used in a
6187 context where bits outside its mode were being referenced; that is only
6188 valid in bit-field insns. */
6189
6190 if (tmode != BLKmode
6191 && ! (spans_byte && inner_mode != tmode)
6192 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6193 && !MEM_P (inner)
6194 && (! in_dest
6195 || (REG_P (inner)
6196 && have_insn_for (STRICT_LOW_PART, tmode))))
6197 || (MEM_P (inner) && pos_rtx == 0
6198 && (pos
6199 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6200 : BITS_PER_UNIT)) == 0
6201 /* We can't do this if we are widening INNER_MODE (it
6202 may not be aligned, for one thing). */
6203 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6204 && (inner_mode == tmode
6205 || (! mode_dependent_address_p (XEXP (inner, 0))
6206 && ! MEM_VOLATILE_P (inner))))))
6207 {
6208 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6209 field. If the original and current mode are the same, we need not
6210 adjust the offset. Otherwise, we do if bytes big endian.
6211
6212 If INNER is not a MEM, get a piece consisting of just the field
6213 of interest (in this case POS % BITS_PER_WORD must be 0). */
6214
6215 if (MEM_P (inner))
6216 {
6217 HOST_WIDE_INT offset;
6218
6219 /* POS counts from lsb, but make OFFSET count in memory order. */
6220 if (BYTES_BIG_ENDIAN)
6221 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6222 else
6223 offset = pos / BITS_PER_UNIT;
6224
6225 new = adjust_address_nv (inner, tmode, offset);
6226 }
6227 else if (REG_P (inner))
6228 {
6229 if (tmode != inner_mode)
6230 {
6231 /* We can't call gen_lowpart in a DEST since we
6232 always want a SUBREG (see below) and it would sometimes
6233 return a new hard register. */
6234 if (pos || in_dest)
6235 {
6236 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6237
6238 if (WORDS_BIG_ENDIAN
6239 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6240 final_word = ((GET_MODE_SIZE (inner_mode)
6241 - GET_MODE_SIZE (tmode))
6242 / UNITS_PER_WORD) - final_word;
6243
6244 final_word *= UNITS_PER_WORD;
6245 if (BYTES_BIG_ENDIAN &&
6246 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6247 final_word += (GET_MODE_SIZE (inner_mode)
6248 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6249
6250 /* Avoid creating invalid subregs, for example when
6251 simplifying (x>>32)&255. */
6252 if (final_word >= GET_MODE_SIZE (inner_mode))
6253 return NULL_RTX;
6254
6255 new = gen_rtx_SUBREG (tmode, inner, final_word);
6256 }
6257 else
6258 new = gen_lowpart (tmode, inner);
6259 }
6260 else
6261 new = inner;
6262 }
6263 else
6264 new = force_to_mode (inner, tmode,
6265 len >= HOST_BITS_PER_WIDE_INT
6266 ? ~(unsigned HOST_WIDE_INT) 0
6267 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6268 NULL_RTX, 0);
6269
6270 /* If this extraction is going into the destination of a SET,
6271 make a STRICT_LOW_PART unless we made a MEM. */
6272
6273 if (in_dest)
6274 return (MEM_P (new) ? new
6275 : (GET_CODE (new) != SUBREG
6276 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6277 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6278
6279 if (mode == tmode)
6280 return new;
6281
6282 if (GET_CODE (new) == CONST_INT)
6283 return gen_int_mode (INTVAL (new), mode);
6284
6285 /* If we know that no extraneous bits are set, and that the high
6286 bit is not set, convert the extraction to the cheaper of
6287 sign and zero extension, that are equivalent in these cases. */
6288 if (flag_expensive_optimizations
6289 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6290 && ((nonzero_bits (new, tmode)
6291 & ~(((unsigned HOST_WIDE_INT)
6292 GET_MODE_MASK (tmode))
6293 >> 1))
6294 == 0)))
6295 {
6296 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6297 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6298
6299 /* Prefer ZERO_EXTENSION, since it gives more information to
6300 backends. */
6301 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6302 return temp;
6303 return temp1;
6304 }
6305
6306 /* Otherwise, sign- or zero-extend unless we already are in the
6307 proper mode. */
6308
6309 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6310 mode, new));
6311 }
6312
6313 /* Unless this is a COMPARE or we have a funny memory reference,
6314 don't do anything with zero-extending field extracts starting at
6315 the low-order bit since they are simple AND operations. */
6316 if (pos_rtx == 0 && pos == 0 && ! in_dest
6317 && ! in_compare && ! spans_byte && unsignedp)
6318 return 0;
6319
6320 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6321 we would be spanning bytes or if the position is not a constant and the
6322 length is not 1. In all other cases, we would only be going outside
6323 our object in cases when an original shift would have been
6324 undefined. */
6325 if (! spans_byte && MEM_P (inner)
6326 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6327 || (pos_rtx != 0 && len != 1)))
6328 return 0;
6329
6330 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6331 and the mode for the result. */
6332 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6333 {
6334 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6335 pos_mode = mode_for_extraction (EP_insv, 2);
6336 extraction_mode = mode_for_extraction (EP_insv, 3);
6337 }
6338
6339 if (! in_dest && unsignedp
6340 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6341 {
6342 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6343 pos_mode = mode_for_extraction (EP_extzv, 3);
6344 extraction_mode = mode_for_extraction (EP_extzv, 0);
6345 }
6346
6347 if (! in_dest && ! unsignedp
6348 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6349 {
6350 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6351 pos_mode = mode_for_extraction (EP_extv, 3);
6352 extraction_mode = mode_for_extraction (EP_extv, 0);
6353 }
6354
6355 /* Never narrow an object, since that might not be safe. */
6356
6357 if (mode != VOIDmode
6358 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6359 extraction_mode = mode;
6360
6361 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6362 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6363 pos_mode = GET_MODE (pos_rtx);
6364
6365 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6366 if we have to change the mode of memory and cannot, the desired mode is
6367 EXTRACTION_MODE. */
6368 if (!MEM_P (inner))
6369 wanted_inner_mode = wanted_inner_reg_mode;
6370 else if (inner_mode != wanted_inner_mode
6371 && (mode_dependent_address_p (XEXP (inner, 0))
6372 || MEM_VOLATILE_P (inner)))
6373 wanted_inner_mode = extraction_mode;
6374
6375 orig_pos = pos;
6376
6377 if (BITS_BIG_ENDIAN)
6378 {
6379 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6380 BITS_BIG_ENDIAN style. If position is constant, compute new
6381 position. Otherwise, build subtraction.
6382 Note that POS is relative to the mode of the original argument.
6383 If it's a MEM we need to recompute POS relative to that.
6384 However, if we're extracting from (or inserting into) a register,
6385 we want to recompute POS relative to wanted_inner_mode. */
6386 int width = (MEM_P (inner)
6387 ? GET_MODE_BITSIZE (is_mode)
6388 : GET_MODE_BITSIZE (wanted_inner_mode));
6389
6390 if (pos_rtx == 0)
6391 pos = width - len - pos;
6392 else
6393 pos_rtx
6394 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6395 /* POS may be less than 0 now, but we check for that below.
6396 Note that it can only be less than 0 if !MEM_P (inner). */
6397 }
6398
6399 /* If INNER has a wider mode, make it smaller. If this is a constant
6400 extract, try to adjust the byte to point to the byte containing
6401 the value. */
6402 if (wanted_inner_mode != VOIDmode
6403 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6404 && ((MEM_P (inner)
6405 && (inner_mode == wanted_inner_mode
6406 || (! mode_dependent_address_p (XEXP (inner, 0))
6407 && ! MEM_VOLATILE_P (inner))))))
6408 {
6409 int offset = 0;
6410
6411 /* The computations below will be correct if the machine is big
6412 endian in both bits and bytes or little endian in bits and bytes.
6413 If it is mixed, we must adjust. */
6414
6415 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6416 adjust OFFSET to compensate. */
6417 if (BYTES_BIG_ENDIAN
6418 && ! spans_byte
6419 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6420 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6421
6422 /* If this is a constant position, we can move to the desired byte. */
6423 if (pos_rtx == 0)
6424 {
6425 offset += pos / BITS_PER_UNIT;
6426 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6427 }
6428
6429 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6430 && ! spans_byte
6431 && is_mode != wanted_inner_mode)
6432 offset = (GET_MODE_SIZE (is_mode)
6433 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6434
6435 if (offset != 0 || inner_mode != wanted_inner_mode)
6436 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6437 }
6438
6439 /* If INNER is not memory, we can always get it into the proper mode. If we
6440 are changing its mode, POS must be a constant and smaller than the size
6441 of the new mode. */
6442 else if (!MEM_P (inner))
6443 {
6444 if (GET_MODE (inner) != wanted_inner_mode
6445 && (pos_rtx != 0
6446 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6447 return 0;
6448
6449 inner = force_to_mode (inner, wanted_inner_mode,
6450 pos_rtx
6451 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6452 ? ~(unsigned HOST_WIDE_INT) 0
6453 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6454 << orig_pos),
6455 NULL_RTX, 0);
6456 }
6457
6458 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6459 have to zero extend. Otherwise, we can just use a SUBREG. */
6460 if (pos_rtx != 0
6461 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6462 {
6463 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6464
6465 /* If we know that no extraneous bits are set, and that the high
6466 bit is not set, convert extraction to cheaper one - either
6467 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6468 cases. */
6469 if (flag_expensive_optimizations
6470 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6471 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6472 & ~(((unsigned HOST_WIDE_INT)
6473 GET_MODE_MASK (GET_MODE (pos_rtx)))
6474 >> 1))
6475 == 0)))
6476 {
6477 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6478
6479 /* Prefer ZERO_EXTENSION, since it gives more information to
6480 backends. */
6481 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6482 temp = temp1;
6483 }
6484 pos_rtx = temp;
6485 }
6486 else if (pos_rtx != 0
6487 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6488 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6489
6490 /* Make POS_RTX unless we already have it and it is correct. If we don't
6491 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6492 be a CONST_INT. */
6493 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6494 pos_rtx = orig_pos_rtx;
6495
6496 else if (pos_rtx == 0)
6497 pos_rtx = GEN_INT (pos);
6498
6499 /* Make the required operation. See if we can use existing rtx. */
6500 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6501 extraction_mode, inner, GEN_INT (len), pos_rtx);
6502 if (! in_dest)
6503 new = gen_lowpart (mode, new);
6504
6505 return new;
6506 }
6507 \f
6508 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6509 with any other operations in X. Return X without that shift if so. */
6510
6511 static rtx
6512 extract_left_shift (rtx x, int count)
6513 {
6514 enum rtx_code code = GET_CODE (x);
6515 enum machine_mode mode = GET_MODE (x);
6516 rtx tem;
6517
6518 switch (code)
6519 {
6520 case ASHIFT:
6521 /* This is the shift itself. If it is wide enough, we will return
6522 either the value being shifted if the shift count is equal to
6523 COUNT or a shift for the difference. */
6524 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6525 && INTVAL (XEXP (x, 1)) >= count)
6526 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6527 INTVAL (XEXP (x, 1)) - count);
6528 break;
6529
6530 case NEG: case NOT:
6531 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6532 return simplify_gen_unary (code, mode, tem, mode);
6533
6534 break;
6535
6536 case PLUS: case IOR: case XOR: case AND:
6537 /* If we can safely shift this constant and we find the inner shift,
6538 make a new operation. */
6539 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6540 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6541 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6542 return gen_binary (code, mode, tem,
6543 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6544
6545 break;
6546
6547 default:
6548 break;
6549 }
6550
6551 return 0;
6552 }
6553 \f
6554 /* Look at the expression rooted at X. Look for expressions
6555 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6556 Form these expressions.
6557
6558 Return the new rtx, usually just X.
6559
6560 Also, for machines like the VAX that don't have logical shift insns,
6561 try to convert logical to arithmetic shift operations in cases where
6562 they are equivalent. This undoes the canonicalizations to logical
6563 shifts done elsewhere.
6564
6565 We try, as much as possible, to re-use rtl expressions to save memory.
6566
6567 IN_CODE says what kind of expression we are processing. Normally, it is
6568 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6569 being kludges), it is MEM. When processing the arguments of a comparison
6570 or a COMPARE against zero, it is COMPARE. */
6571
6572 static rtx
6573 make_compound_operation (rtx x, enum rtx_code in_code)
6574 {
6575 enum rtx_code code = GET_CODE (x);
6576 enum machine_mode mode = GET_MODE (x);
6577 int mode_width = GET_MODE_BITSIZE (mode);
6578 rtx rhs, lhs;
6579 enum rtx_code next_code;
6580 int i;
6581 rtx new = 0;
6582 rtx tem;
6583 const char *fmt;
6584
6585 /* Select the code to be used in recursive calls. Once we are inside an
6586 address, we stay there. If we have a comparison, set to COMPARE,
6587 but once inside, go back to our default of SET. */
6588
6589 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6590 : ((code == COMPARE || COMPARISON_P (x))
6591 && XEXP (x, 1) == const0_rtx) ? COMPARE
6592 : in_code == COMPARE ? SET : in_code);
6593
6594 /* Process depending on the code of this operation. If NEW is set
6595 nonzero, it will be returned. */
6596
6597 switch (code)
6598 {
6599 case ASHIFT:
6600 /* Convert shifts by constants into multiplications if inside
6601 an address. */
6602 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6603 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6604 && INTVAL (XEXP (x, 1)) >= 0)
6605 {
6606 new = make_compound_operation (XEXP (x, 0), next_code);
6607 new = gen_rtx_MULT (mode, new,
6608 GEN_INT ((HOST_WIDE_INT) 1
6609 << INTVAL (XEXP (x, 1))));
6610 }
6611 break;
6612
6613 case AND:
6614 /* If the second operand is not a constant, we can't do anything
6615 with it. */
6616 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6617 break;
6618
6619 /* If the constant is a power of two minus one and the first operand
6620 is a logical right shift, make an extraction. */
6621 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6622 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6623 {
6624 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6625 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6626 0, in_code == COMPARE);
6627 }
6628
6629 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6630 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6631 && subreg_lowpart_p (XEXP (x, 0))
6632 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6633 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6634 {
6635 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6636 next_code);
6637 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6638 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6639 0, in_code == COMPARE);
6640 }
6641 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6642 else if ((GET_CODE (XEXP (x, 0)) == XOR
6643 || GET_CODE (XEXP (x, 0)) == IOR)
6644 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6645 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6646 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6647 {
6648 /* Apply the distributive law, and then try to make extractions. */
6649 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6650 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6651 XEXP (x, 1)),
6652 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6653 XEXP (x, 1)));
6654 new = make_compound_operation (new, in_code);
6655 }
6656
6657 /* If we are have (and (rotate X C) M) and C is larger than the number
6658 of bits in M, this is an extraction. */
6659
6660 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6661 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6662 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6663 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6664 {
6665 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6666 new = make_extraction (mode, new,
6667 (GET_MODE_BITSIZE (mode)
6668 - INTVAL (XEXP (XEXP (x, 0), 1))),
6669 NULL_RTX, i, 1, 0, in_code == COMPARE);
6670 }
6671
6672 /* On machines without logical shifts, if the operand of the AND is
6673 a logical shift and our mask turns off all the propagated sign
6674 bits, we can replace the logical shift with an arithmetic shift. */
6675 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6676 && !have_insn_for (LSHIFTRT, mode)
6677 && have_insn_for (ASHIFTRT, mode)
6678 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6679 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6680 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6681 && mode_width <= HOST_BITS_PER_WIDE_INT)
6682 {
6683 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6684
6685 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6686 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6687 SUBST (XEXP (x, 0),
6688 gen_rtx_ASHIFTRT (mode,
6689 make_compound_operation
6690 (XEXP (XEXP (x, 0), 0), next_code),
6691 XEXP (XEXP (x, 0), 1)));
6692 }
6693
6694 /* If the constant is one less than a power of two, this might be
6695 representable by an extraction even if no shift is present.
6696 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6697 we are in a COMPARE. */
6698 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6699 new = make_extraction (mode,
6700 make_compound_operation (XEXP (x, 0),
6701 next_code),
6702 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6703
6704 /* If we are in a comparison and this is an AND with a power of two,
6705 convert this into the appropriate bit extract. */
6706 else if (in_code == COMPARE
6707 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6708 new = make_extraction (mode,
6709 make_compound_operation (XEXP (x, 0),
6710 next_code),
6711 i, NULL_RTX, 1, 1, 0, 1);
6712
6713 break;
6714
6715 case LSHIFTRT:
6716 /* If the sign bit is known to be zero, replace this with an
6717 arithmetic shift. */
6718 if (have_insn_for (ASHIFTRT, mode)
6719 && ! have_insn_for (LSHIFTRT, mode)
6720 && mode_width <= HOST_BITS_PER_WIDE_INT
6721 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6722 {
6723 new = gen_rtx_ASHIFTRT (mode,
6724 make_compound_operation (XEXP (x, 0),
6725 next_code),
6726 XEXP (x, 1));
6727 break;
6728 }
6729
6730 /* ... fall through ... */
6731
6732 case ASHIFTRT:
6733 lhs = XEXP (x, 0);
6734 rhs = XEXP (x, 1);
6735
6736 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6737 this is a SIGN_EXTRACT. */
6738 if (GET_CODE (rhs) == CONST_INT
6739 && GET_CODE (lhs) == ASHIFT
6740 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6741 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6742 {
6743 new = make_compound_operation (XEXP (lhs, 0), next_code);
6744 new = make_extraction (mode, new,
6745 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6746 NULL_RTX, mode_width - INTVAL (rhs),
6747 code == LSHIFTRT, 0, in_code == COMPARE);
6748 break;
6749 }
6750
6751 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6752 If so, try to merge the shifts into a SIGN_EXTEND. We could
6753 also do this for some cases of SIGN_EXTRACT, but it doesn't
6754 seem worth the effort; the case checked for occurs on Alpha. */
6755
6756 if (!OBJECT_P (lhs)
6757 && ! (GET_CODE (lhs) == SUBREG
6758 && (OBJECT_P (SUBREG_REG (lhs))))
6759 && GET_CODE (rhs) == CONST_INT
6760 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6761 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6762 new = make_extraction (mode, make_compound_operation (new, next_code),
6763 0, NULL_RTX, mode_width - INTVAL (rhs),
6764 code == LSHIFTRT, 0, in_code == COMPARE);
6765
6766 break;
6767
6768 case SUBREG:
6769 /* Call ourselves recursively on the inner expression. If we are
6770 narrowing the object and it has a different RTL code from
6771 what it originally did, do this SUBREG as a force_to_mode. */
6772
6773 tem = make_compound_operation (SUBREG_REG (x), in_code);
6774 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6775 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6776 && subreg_lowpart_p (x))
6777 {
6778 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6779 NULL_RTX, 0);
6780
6781 /* If we have something other than a SUBREG, we might have
6782 done an expansion, so rerun ourselves. */
6783 if (GET_CODE (newer) != SUBREG)
6784 newer = make_compound_operation (newer, in_code);
6785
6786 return newer;
6787 }
6788
6789 /* If this is a paradoxical subreg, and the new code is a sign or
6790 zero extension, omit the subreg and widen the extension. If it
6791 is a regular subreg, we can still get rid of the subreg by not
6792 widening so much, or in fact removing the extension entirely. */
6793 if ((GET_CODE (tem) == SIGN_EXTEND
6794 || GET_CODE (tem) == ZERO_EXTEND)
6795 && subreg_lowpart_p (x))
6796 {
6797 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6798 || (GET_MODE_SIZE (mode) >
6799 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6800 {
6801 if (! SCALAR_INT_MODE_P (mode))
6802 break;
6803 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6804 }
6805 else
6806 tem = gen_lowpart (mode, XEXP (tem, 0));
6807 return tem;
6808 }
6809 break;
6810
6811 default:
6812 break;
6813 }
6814
6815 if (new)
6816 {
6817 x = gen_lowpart (mode, new);
6818 code = GET_CODE (x);
6819 }
6820
6821 /* Now recursively process each operand of this operation. */
6822 fmt = GET_RTX_FORMAT (code);
6823 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6824 if (fmt[i] == 'e')
6825 {
6826 new = make_compound_operation (XEXP (x, i), next_code);
6827 SUBST (XEXP (x, i), new);
6828 }
6829
6830 return x;
6831 }
6832 \f
6833 /* Given M see if it is a value that would select a field of bits
6834 within an item, but not the entire word. Return -1 if not.
6835 Otherwise, return the starting position of the field, where 0 is the
6836 low-order bit.
6837
6838 *PLEN is set to the length of the field. */
6839
6840 static int
6841 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6842 {
6843 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6844 int pos = exact_log2 (m & -m);
6845 int len = 0;
6846
6847 if (pos >= 0)
6848 /* Now shift off the low-order zero bits and see if we have a
6849 power of two minus 1. */
6850 len = exact_log2 ((m >> pos) + 1);
6851
6852 if (len <= 0)
6853 pos = -1;
6854
6855 *plen = len;
6856 return pos;
6857 }
6858 \f
6859 /* See if X can be simplified knowing that we will only refer to it in
6860 MODE and will only refer to those bits that are nonzero in MASK.
6861 If other bits are being computed or if masking operations are done
6862 that select a superset of the bits in MASK, they can sometimes be
6863 ignored.
6864
6865 Return a possibly simplified expression, but always convert X to
6866 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6867
6868 Also, if REG is nonzero and X is a register equal in value to REG,
6869 replace X with REG.
6870
6871 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6872 are all off in X. This is used when X will be complemented, by either
6873 NOT, NEG, or XOR. */
6874
6875 static rtx
6876 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6877 rtx reg, int just_select)
6878 {
6879 enum rtx_code code = GET_CODE (x);
6880 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6881 enum machine_mode op_mode;
6882 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6883 rtx op0, op1, temp;
6884
6885 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6886 code below will do the wrong thing since the mode of such an
6887 expression is VOIDmode.
6888
6889 Also do nothing if X is a CLOBBER; this can happen if X was
6890 the return value from a call to gen_lowpart. */
6891 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6892 return x;
6893
6894 /* We want to perform the operation is its present mode unless we know
6895 that the operation is valid in MODE, in which case we do the operation
6896 in MODE. */
6897 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6898 && have_insn_for (code, mode))
6899 ? mode : GET_MODE (x));
6900
6901 /* It is not valid to do a right-shift in a narrower mode
6902 than the one it came in with. */
6903 if ((code == LSHIFTRT || code == ASHIFTRT)
6904 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6905 op_mode = GET_MODE (x);
6906
6907 /* Truncate MASK to fit OP_MODE. */
6908 if (op_mode)
6909 mask &= GET_MODE_MASK (op_mode);
6910
6911 /* When we have an arithmetic operation, or a shift whose count we
6912 do not know, we need to assume that all bits up to the highest-order
6913 bit in MASK will be needed. This is how we form such a mask. */
6914 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6915 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6916 else
6917 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6918 - 1);
6919
6920 /* Determine what bits of X are guaranteed to be (non)zero. */
6921 nonzero = nonzero_bits (x, mode);
6922
6923 /* If none of the bits in X are needed, return a zero. */
6924 if (! just_select && (nonzero & mask) == 0)
6925 x = const0_rtx;
6926
6927 /* If X is a CONST_INT, return a new one. Do this here since the
6928 test below will fail. */
6929 if (GET_CODE (x) == CONST_INT)
6930 {
6931 if (SCALAR_INT_MODE_P (mode))
6932 return gen_int_mode (INTVAL (x) & mask, mode);
6933 else
6934 {
6935 x = GEN_INT (INTVAL (x) & mask);
6936 return gen_lowpart_common (mode, x);
6937 }
6938 }
6939
6940 /* If X is narrower than MODE and we want all the bits in X's mode, just
6941 get X in the proper mode. */
6942 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6943 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6944 return gen_lowpart (mode, x);
6945
6946 switch (code)
6947 {
6948 case CLOBBER:
6949 /* If X is a (clobber (const_int)), return it since we know we are
6950 generating something that won't match. */
6951 return x;
6952
6953 case USE:
6954 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6955 spanned the boundary of the MEM. If we are now masking so it is
6956 within that boundary, we don't need the USE any more. */
6957 if (! BITS_BIG_ENDIAN
6958 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6959 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6960 break;
6961
6962 case SIGN_EXTEND:
6963 case ZERO_EXTEND:
6964 case ZERO_EXTRACT:
6965 case SIGN_EXTRACT:
6966 x = expand_compound_operation (x);
6967 if (GET_CODE (x) != code)
6968 return force_to_mode (x, mode, mask, reg, next_select);
6969 break;
6970
6971 case REG:
6972 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6973 || rtx_equal_p (reg, get_last_value (x))))
6974 x = reg;
6975 break;
6976
6977 case SUBREG:
6978 if (subreg_lowpart_p (x)
6979 /* We can ignore the effect of this SUBREG if it narrows the mode or
6980 if the constant masks to zero all the bits the mode doesn't
6981 have. */
6982 && ((GET_MODE_SIZE (GET_MODE (x))
6983 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6984 || (0 == (mask
6985 & GET_MODE_MASK (GET_MODE (x))
6986 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6987 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6988 break;
6989
6990 case AND:
6991 /* If this is an AND with a constant, convert it into an AND
6992 whose constant is the AND of that constant with MASK. If it
6993 remains an AND of MASK, delete it since it is redundant. */
6994
6995 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6996 {
6997 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6998 mask & INTVAL (XEXP (x, 1)));
6999
7000 /* If X is still an AND, see if it is an AND with a mask that
7001 is just some low-order bits. If so, and it is MASK, we don't
7002 need it. */
7003
7004 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7005 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7006 == mask))
7007 x = XEXP (x, 0);
7008
7009 /* If it remains an AND, try making another AND with the bits
7010 in the mode mask that aren't in MASK turned on. If the
7011 constant in the AND is wide enough, this might make a
7012 cheaper constant. */
7013
7014 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7015 && GET_MODE_MASK (GET_MODE (x)) != mask
7016 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7017 {
7018 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7019 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7020 int width = GET_MODE_BITSIZE (GET_MODE (x));
7021 rtx y;
7022
7023 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7024 number, sign extend it. */
7025 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7026 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7027 cval |= (HOST_WIDE_INT) -1 << width;
7028
7029 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
7030 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7031 x = y;
7032 }
7033
7034 break;
7035 }
7036
7037 goto binop;
7038
7039 case PLUS:
7040 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7041 low-order bits (as in an alignment operation) and FOO is already
7042 aligned to that boundary, mask C1 to that boundary as well.
7043 This may eliminate that PLUS and, later, the AND. */
7044
7045 {
7046 unsigned int width = GET_MODE_BITSIZE (mode);
7047 unsigned HOST_WIDE_INT smask = mask;
7048
7049 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7050 number, sign extend it. */
7051
7052 if (width < HOST_BITS_PER_WIDE_INT
7053 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7054 smask |= (HOST_WIDE_INT) -1 << width;
7055
7056 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7057 && exact_log2 (- smask) >= 0
7058 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7059 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7060 return force_to_mode (plus_constant (XEXP (x, 0),
7061 (INTVAL (XEXP (x, 1)) & smask)),
7062 mode, smask, reg, next_select);
7063 }
7064
7065 /* ... fall through ... */
7066
7067 case MULT:
7068 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7069 most significant bit in MASK since carries from those bits will
7070 affect the bits we are interested in. */
7071 mask = fuller_mask;
7072 goto binop;
7073
7074 case MINUS:
7075 /* If X is (minus C Y) where C's least set bit is larger than any bit
7076 in the mask, then we may replace with (neg Y). */
7077 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7078 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7079 & -INTVAL (XEXP (x, 0))))
7080 > mask))
7081 {
7082 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7083 GET_MODE (x));
7084 return force_to_mode (x, mode, mask, reg, next_select);
7085 }
7086
7087 /* Similarly, if C contains every bit in the fuller_mask, then we may
7088 replace with (not Y). */
7089 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7090 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7091 == INTVAL (XEXP (x, 0))))
7092 {
7093 x = simplify_gen_unary (NOT, GET_MODE (x),
7094 XEXP (x, 1), GET_MODE (x));
7095 return force_to_mode (x, mode, mask, reg, next_select);
7096 }
7097
7098 mask = fuller_mask;
7099 goto binop;
7100
7101 case IOR:
7102 case XOR:
7103 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7104 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7105 operation which may be a bitfield extraction. Ensure that the
7106 constant we form is not wider than the mode of X. */
7107
7108 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7109 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7110 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7111 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7112 && GET_CODE (XEXP (x, 1)) == CONST_INT
7113 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7114 + floor_log2 (INTVAL (XEXP (x, 1))))
7115 < GET_MODE_BITSIZE (GET_MODE (x)))
7116 && (INTVAL (XEXP (x, 1))
7117 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7118 {
7119 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7120 << INTVAL (XEXP (XEXP (x, 0), 1)));
7121 temp = gen_binary (GET_CODE (x), GET_MODE (x),
7122 XEXP (XEXP (x, 0), 0), temp);
7123 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
7124 XEXP (XEXP (x, 0), 1));
7125 return force_to_mode (x, mode, mask, reg, next_select);
7126 }
7127
7128 binop:
7129 /* For most binary operations, just propagate into the operation and
7130 change the mode if we have an operation of that mode. */
7131
7132 op0 = gen_lowpart (op_mode,
7133 force_to_mode (XEXP (x, 0), mode, mask,
7134 reg, next_select));
7135 op1 = gen_lowpart (op_mode,
7136 force_to_mode (XEXP (x, 1), mode, mask,
7137 reg, next_select));
7138
7139 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7140 x = gen_binary (code, op_mode, op0, op1);
7141 break;
7142
7143 case ASHIFT:
7144 /* For left shifts, do the same, but just for the first operand.
7145 However, we cannot do anything with shifts where we cannot
7146 guarantee that the counts are smaller than the size of the mode
7147 because such a count will have a different meaning in a
7148 wider mode. */
7149
7150 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7151 && INTVAL (XEXP (x, 1)) >= 0
7152 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7153 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7154 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7155 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7156 break;
7157
7158 /* If the shift count is a constant and we can do arithmetic in
7159 the mode of the shift, refine which bits we need. Otherwise, use the
7160 conservative form of the mask. */
7161 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7162 && INTVAL (XEXP (x, 1)) >= 0
7163 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7164 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7165 mask >>= INTVAL (XEXP (x, 1));
7166 else
7167 mask = fuller_mask;
7168
7169 op0 = gen_lowpart (op_mode,
7170 force_to_mode (XEXP (x, 0), op_mode,
7171 mask, reg, next_select));
7172
7173 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7174 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7175 break;
7176
7177 case LSHIFTRT:
7178 /* Here we can only do something if the shift count is a constant,
7179 this shift constant is valid for the host, and we can do arithmetic
7180 in OP_MODE. */
7181
7182 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7183 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7184 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7185 {
7186 rtx inner = XEXP (x, 0);
7187 unsigned HOST_WIDE_INT inner_mask;
7188
7189 /* Select the mask of the bits we need for the shift operand. */
7190 inner_mask = mask << INTVAL (XEXP (x, 1));
7191
7192 /* We can only change the mode of the shift if we can do arithmetic
7193 in the mode of the shift and INNER_MASK is no wider than the
7194 width of X's mode. */
7195 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7196 op_mode = GET_MODE (x);
7197
7198 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7199
7200 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7201 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7202 }
7203
7204 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7205 shift and AND produces only copies of the sign bit (C2 is one less
7206 than a power of two), we can do this with just a shift. */
7207
7208 if (GET_CODE (x) == LSHIFTRT
7209 && GET_CODE (XEXP (x, 1)) == CONST_INT
7210 /* The shift puts one of the sign bit copies in the least significant
7211 bit. */
7212 && ((INTVAL (XEXP (x, 1))
7213 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7214 >= GET_MODE_BITSIZE (GET_MODE (x)))
7215 && exact_log2 (mask + 1) >= 0
7216 /* Number of bits left after the shift must be more than the mask
7217 needs. */
7218 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7219 <= GET_MODE_BITSIZE (GET_MODE (x)))
7220 /* Must be more sign bit copies than the mask needs. */
7221 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7222 >= exact_log2 (mask + 1)))
7223 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7224 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7225 - exact_log2 (mask + 1)));
7226
7227 goto shiftrt;
7228
7229 case ASHIFTRT:
7230 /* If we are just looking for the sign bit, we don't need this shift at
7231 all, even if it has a variable count. */
7232 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7233 && (mask == ((unsigned HOST_WIDE_INT) 1
7234 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7235 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7236
7237 /* If this is a shift by a constant, get a mask that contains those bits
7238 that are not copies of the sign bit. We then have two cases: If
7239 MASK only includes those bits, this can be a logical shift, which may
7240 allow simplifications. If MASK is a single-bit field not within
7241 those bits, we are requesting a copy of the sign bit and hence can
7242 shift the sign bit to the appropriate location. */
7243
7244 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7245 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7246 {
7247 int i = -1;
7248
7249 /* If the considered data is wider than HOST_WIDE_INT, we can't
7250 represent a mask for all its bits in a single scalar.
7251 But we only care about the lower bits, so calculate these. */
7252
7253 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7254 {
7255 nonzero = ~(HOST_WIDE_INT) 0;
7256
7257 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7258 is the number of bits a full-width mask would have set.
7259 We need only shift if these are fewer than nonzero can
7260 hold. If not, we must keep all bits set in nonzero. */
7261
7262 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7263 < HOST_BITS_PER_WIDE_INT)
7264 nonzero >>= INTVAL (XEXP (x, 1))
7265 + HOST_BITS_PER_WIDE_INT
7266 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7267 }
7268 else
7269 {
7270 nonzero = GET_MODE_MASK (GET_MODE (x));
7271 nonzero >>= INTVAL (XEXP (x, 1));
7272 }
7273
7274 if ((mask & ~nonzero) == 0
7275 || (i = exact_log2 (mask)) >= 0)
7276 {
7277 x = simplify_shift_const
7278 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7279 i < 0 ? INTVAL (XEXP (x, 1))
7280 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7281
7282 if (GET_CODE (x) != ASHIFTRT)
7283 return force_to_mode (x, mode, mask, reg, next_select);
7284 }
7285 }
7286
7287 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7288 even if the shift count isn't a constant. */
7289 if (mask == 1)
7290 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7291
7292 shiftrt:
7293
7294 /* If this is a zero- or sign-extension operation that just affects bits
7295 we don't care about, remove it. Be sure the call above returned
7296 something that is still a shift. */
7297
7298 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7299 && GET_CODE (XEXP (x, 1)) == CONST_INT
7300 && INTVAL (XEXP (x, 1)) >= 0
7301 && (INTVAL (XEXP (x, 1))
7302 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7303 && GET_CODE (XEXP (x, 0)) == ASHIFT
7304 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7305 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7306 reg, next_select);
7307
7308 break;
7309
7310 case ROTATE:
7311 case ROTATERT:
7312 /* If the shift count is constant and we can do computations
7313 in the mode of X, compute where the bits we care about are.
7314 Otherwise, we can't do anything. Don't change the mode of
7315 the shift or propagate MODE into the shift, though. */
7316 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7317 && INTVAL (XEXP (x, 1)) >= 0)
7318 {
7319 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7320 GET_MODE (x), GEN_INT (mask),
7321 XEXP (x, 1));
7322 if (temp && GET_CODE (temp) == CONST_INT)
7323 SUBST (XEXP (x, 0),
7324 force_to_mode (XEXP (x, 0), GET_MODE (x),
7325 INTVAL (temp), reg, next_select));
7326 }
7327 break;
7328
7329 case NEG:
7330 /* If we just want the low-order bit, the NEG isn't needed since it
7331 won't change the low-order bit. */
7332 if (mask == 1)
7333 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7334
7335 /* We need any bits less significant than the most significant bit in
7336 MASK since carries from those bits will affect the bits we are
7337 interested in. */
7338 mask = fuller_mask;
7339 goto unop;
7340
7341 case NOT:
7342 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7343 same as the XOR case above. Ensure that the constant we form is not
7344 wider than the mode of X. */
7345
7346 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7347 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7348 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7349 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7350 < GET_MODE_BITSIZE (GET_MODE (x)))
7351 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7352 {
7353 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7354 GET_MODE (x));
7355 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7356 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7357
7358 return force_to_mode (x, mode, mask, reg, next_select);
7359 }
7360
7361 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7362 use the full mask inside the NOT. */
7363 mask = fuller_mask;
7364
7365 unop:
7366 op0 = gen_lowpart (op_mode,
7367 force_to_mode (XEXP (x, 0), mode, mask,
7368 reg, next_select));
7369 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7370 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7371 break;
7372
7373 case NE:
7374 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7375 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7376 which is equal to STORE_FLAG_VALUE. */
7377 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7378 && GET_MODE (XEXP (x, 0)) == mode
7379 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7380 && (nonzero_bits (XEXP (x, 0), mode)
7381 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7382 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7383
7384 break;
7385
7386 case IF_THEN_ELSE:
7387 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7388 written in a narrower mode. We play it safe and do not do so. */
7389
7390 SUBST (XEXP (x, 1),
7391 gen_lowpart (GET_MODE (x),
7392 force_to_mode (XEXP (x, 1), mode,
7393 mask, reg, next_select)));
7394 SUBST (XEXP (x, 2),
7395 gen_lowpart (GET_MODE (x),
7396 force_to_mode (XEXP (x, 2), mode,
7397 mask, reg, next_select)));
7398 break;
7399
7400 default:
7401 break;
7402 }
7403
7404 /* Ensure we return a value of the proper mode. */
7405 return gen_lowpart (mode, x);
7406 }
7407 \f
7408 /* Return nonzero if X is an expression that has one of two values depending on
7409 whether some other value is zero or nonzero. In that case, we return the
7410 value that is being tested, *PTRUE is set to the value if the rtx being
7411 returned has a nonzero value, and *PFALSE is set to the other alternative.
7412
7413 If we return zero, we set *PTRUE and *PFALSE to X. */
7414
7415 static rtx
7416 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7417 {
7418 enum machine_mode mode = GET_MODE (x);
7419 enum rtx_code code = GET_CODE (x);
7420 rtx cond0, cond1, true0, true1, false0, false1;
7421 unsigned HOST_WIDE_INT nz;
7422
7423 /* If we are comparing a value against zero, we are done. */
7424 if ((code == NE || code == EQ)
7425 && XEXP (x, 1) == const0_rtx)
7426 {
7427 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7428 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7429 return XEXP (x, 0);
7430 }
7431
7432 /* If this is a unary operation whose operand has one of two values, apply
7433 our opcode to compute those values. */
7434 else if (UNARY_P (x)
7435 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7436 {
7437 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7438 *pfalse = simplify_gen_unary (code, mode, false0,
7439 GET_MODE (XEXP (x, 0)));
7440 return cond0;
7441 }
7442
7443 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7444 make can't possibly match and would suppress other optimizations. */
7445 else if (code == COMPARE)
7446 ;
7447
7448 /* If this is a binary operation, see if either side has only one of two
7449 values. If either one does or if both do and they are conditional on
7450 the same value, compute the new true and false values. */
7451 else if (BINARY_P (x))
7452 {
7453 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7454 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7455
7456 if ((cond0 != 0 || cond1 != 0)
7457 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7458 {
7459 /* If if_then_else_cond returned zero, then true/false are the
7460 same rtl. We must copy one of them to prevent invalid rtl
7461 sharing. */
7462 if (cond0 == 0)
7463 true0 = copy_rtx (true0);
7464 else if (cond1 == 0)
7465 true1 = copy_rtx (true1);
7466
7467 *ptrue = gen_binary (code, mode, true0, true1);
7468 *pfalse = gen_binary (code, mode, false0, false1);
7469 return cond0 ? cond0 : cond1;
7470 }
7471
7472 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7473 operands is zero when the other is nonzero, and vice-versa,
7474 and STORE_FLAG_VALUE is 1 or -1. */
7475
7476 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7477 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7478 || code == UMAX)
7479 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7480 {
7481 rtx op0 = XEXP (XEXP (x, 0), 1);
7482 rtx op1 = XEXP (XEXP (x, 1), 1);
7483
7484 cond0 = XEXP (XEXP (x, 0), 0);
7485 cond1 = XEXP (XEXP (x, 1), 0);
7486
7487 if (COMPARISON_P (cond0)
7488 && COMPARISON_P (cond1)
7489 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7490 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7491 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7492 || ((swap_condition (GET_CODE (cond0))
7493 == combine_reversed_comparison_code (cond1))
7494 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7495 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7496 && ! side_effects_p (x))
7497 {
7498 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7499 *pfalse = gen_binary (MULT, mode,
7500 (code == MINUS
7501 ? simplify_gen_unary (NEG, mode, op1,
7502 mode)
7503 : op1),
7504 const_true_rtx);
7505 return cond0;
7506 }
7507 }
7508
7509 /* Similarly for MULT, AND and UMIN, except that for these the result
7510 is always zero. */
7511 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7512 && (code == MULT || code == AND || code == UMIN)
7513 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7514 {
7515 cond0 = XEXP (XEXP (x, 0), 0);
7516 cond1 = XEXP (XEXP (x, 1), 0);
7517
7518 if (COMPARISON_P (cond0)
7519 && COMPARISON_P (cond1)
7520 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7521 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7522 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7523 || ((swap_condition (GET_CODE (cond0))
7524 == combine_reversed_comparison_code (cond1))
7525 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7526 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7527 && ! side_effects_p (x))
7528 {
7529 *ptrue = *pfalse = const0_rtx;
7530 return cond0;
7531 }
7532 }
7533 }
7534
7535 else if (code == IF_THEN_ELSE)
7536 {
7537 /* If we have IF_THEN_ELSE already, extract the condition and
7538 canonicalize it if it is NE or EQ. */
7539 cond0 = XEXP (x, 0);
7540 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7541 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7542 return XEXP (cond0, 0);
7543 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7544 {
7545 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7546 return XEXP (cond0, 0);
7547 }
7548 else
7549 return cond0;
7550 }
7551
7552 /* If X is a SUBREG, we can narrow both the true and false values
7553 if the inner expression, if there is a condition. */
7554 else if (code == SUBREG
7555 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7556 &true0, &false0)))
7557 {
7558 true0 = simplify_gen_subreg (mode, true0,
7559 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7560 false0 = simplify_gen_subreg (mode, false0,
7561 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7562 if (true0 && false0)
7563 {
7564 *ptrue = true0;
7565 *pfalse = false0;
7566 return cond0;
7567 }
7568 }
7569
7570 /* If X is a constant, this isn't special and will cause confusions
7571 if we treat it as such. Likewise if it is equivalent to a constant. */
7572 else if (CONSTANT_P (x)
7573 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7574 ;
7575
7576 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7577 will be least confusing to the rest of the compiler. */
7578 else if (mode == BImode)
7579 {
7580 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7581 return x;
7582 }
7583
7584 /* If X is known to be either 0 or -1, those are the true and
7585 false values when testing X. */
7586 else if (x == constm1_rtx || x == const0_rtx
7587 || (mode != VOIDmode
7588 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7589 {
7590 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7591 return x;
7592 }
7593
7594 /* Likewise for 0 or a single bit. */
7595 else if (SCALAR_INT_MODE_P (mode)
7596 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7597 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7598 {
7599 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7600 return x;
7601 }
7602
7603 /* Otherwise fail; show no condition with true and false values the same. */
7604 *ptrue = *pfalse = x;
7605 return 0;
7606 }
7607 \f
7608 /* Return the value of expression X given the fact that condition COND
7609 is known to be true when applied to REG as its first operand and VAL
7610 as its second. X is known to not be shared and so can be modified in
7611 place.
7612
7613 We only handle the simplest cases, and specifically those cases that
7614 arise with IF_THEN_ELSE expressions. */
7615
7616 static rtx
7617 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7618 {
7619 enum rtx_code code = GET_CODE (x);
7620 rtx temp;
7621 const char *fmt;
7622 int i, j;
7623
7624 if (side_effects_p (x))
7625 return x;
7626
7627 /* If either operand of the condition is a floating point value,
7628 then we have to avoid collapsing an EQ comparison. */
7629 if (cond == EQ
7630 && rtx_equal_p (x, reg)
7631 && ! FLOAT_MODE_P (GET_MODE (x))
7632 && ! FLOAT_MODE_P (GET_MODE (val)))
7633 return val;
7634
7635 if (cond == UNEQ && rtx_equal_p (x, reg))
7636 return val;
7637
7638 /* If X is (abs REG) and we know something about REG's relationship
7639 with zero, we may be able to simplify this. */
7640
7641 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7642 switch (cond)
7643 {
7644 case GE: case GT: case EQ:
7645 return XEXP (x, 0);
7646 case LT: case LE:
7647 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7648 XEXP (x, 0),
7649 GET_MODE (XEXP (x, 0)));
7650 default:
7651 break;
7652 }
7653
7654 /* The only other cases we handle are MIN, MAX, and comparisons if the
7655 operands are the same as REG and VAL. */
7656
7657 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7658 {
7659 if (rtx_equal_p (XEXP (x, 0), val))
7660 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7661
7662 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7663 {
7664 if (COMPARISON_P (x))
7665 {
7666 if (comparison_dominates_p (cond, code))
7667 return const_true_rtx;
7668
7669 code = combine_reversed_comparison_code (x);
7670 if (code != UNKNOWN
7671 && comparison_dominates_p (cond, code))
7672 return const0_rtx;
7673 else
7674 return x;
7675 }
7676 else if (code == SMAX || code == SMIN
7677 || code == UMIN || code == UMAX)
7678 {
7679 int unsignedp = (code == UMIN || code == UMAX);
7680
7681 /* Do not reverse the condition when it is NE or EQ.
7682 This is because we cannot conclude anything about
7683 the value of 'SMAX (x, y)' when x is not equal to y,
7684 but we can when x equals y. */
7685 if ((code == SMAX || code == UMAX)
7686 && ! (cond == EQ || cond == NE))
7687 cond = reverse_condition (cond);
7688
7689 switch (cond)
7690 {
7691 case GE: case GT:
7692 return unsignedp ? x : XEXP (x, 1);
7693 case LE: case LT:
7694 return unsignedp ? x : XEXP (x, 0);
7695 case GEU: case GTU:
7696 return unsignedp ? XEXP (x, 1) : x;
7697 case LEU: case LTU:
7698 return unsignedp ? XEXP (x, 0) : x;
7699 default:
7700 break;
7701 }
7702 }
7703 }
7704 }
7705 else if (code == SUBREG)
7706 {
7707 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7708 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7709
7710 if (SUBREG_REG (x) != r)
7711 {
7712 /* We must simplify subreg here, before we lose track of the
7713 original inner_mode. */
7714 new = simplify_subreg (GET_MODE (x), r,
7715 inner_mode, SUBREG_BYTE (x));
7716 if (new)
7717 return new;
7718 else
7719 SUBST (SUBREG_REG (x), r);
7720 }
7721
7722 return x;
7723 }
7724 /* We don't have to handle SIGN_EXTEND here, because even in the
7725 case of replacing something with a modeless CONST_INT, a
7726 CONST_INT is already (supposed to be) a valid sign extension for
7727 its narrower mode, which implies it's already properly
7728 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7729 story is different. */
7730 else if (code == ZERO_EXTEND)
7731 {
7732 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7733 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7734
7735 if (XEXP (x, 0) != r)
7736 {
7737 /* We must simplify the zero_extend here, before we lose
7738 track of the original inner_mode. */
7739 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7740 r, inner_mode);
7741 if (new)
7742 return new;
7743 else
7744 SUBST (XEXP (x, 0), r);
7745 }
7746
7747 return x;
7748 }
7749
7750 fmt = GET_RTX_FORMAT (code);
7751 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7752 {
7753 if (fmt[i] == 'e')
7754 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7755 else if (fmt[i] == 'E')
7756 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7757 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7758 cond, reg, val));
7759 }
7760
7761 return x;
7762 }
7763 \f
7764 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7765 assignment as a field assignment. */
7766
7767 static int
7768 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7769 {
7770 if (x == y || rtx_equal_p (x, y))
7771 return 1;
7772
7773 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7774 return 0;
7775
7776 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7777 Note that all SUBREGs of MEM are paradoxical; otherwise they
7778 would have been rewritten. */
7779 if (MEM_P (x) && GET_CODE (y) == SUBREG
7780 && MEM_P (SUBREG_REG (y))
7781 && rtx_equal_p (SUBREG_REG (y),
7782 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7783 return 1;
7784
7785 if (MEM_P (y) && GET_CODE (x) == SUBREG
7786 && MEM_P (SUBREG_REG (x))
7787 && rtx_equal_p (SUBREG_REG (x),
7788 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7789 return 1;
7790
7791 /* We used to see if get_last_value of X and Y were the same but that's
7792 not correct. In one direction, we'll cause the assignment to have
7793 the wrong destination and in the case, we'll import a register into this
7794 insn that might have already have been dead. So fail if none of the
7795 above cases are true. */
7796 return 0;
7797 }
7798 \f
7799 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7800 Return that assignment if so.
7801
7802 We only handle the most common cases. */
7803
7804 static rtx
7805 make_field_assignment (rtx x)
7806 {
7807 rtx dest = SET_DEST (x);
7808 rtx src = SET_SRC (x);
7809 rtx assign;
7810 rtx rhs, lhs;
7811 HOST_WIDE_INT c1;
7812 HOST_WIDE_INT pos;
7813 unsigned HOST_WIDE_INT len;
7814 rtx other;
7815 enum machine_mode mode;
7816
7817 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7818 a clear of a one-bit field. We will have changed it to
7819 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7820 for a SUBREG. */
7821
7822 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7823 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7824 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7825 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7826 {
7827 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7828 1, 1, 1, 0);
7829 if (assign != 0)
7830 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7831 return x;
7832 }
7833
7834 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7835 && subreg_lowpart_p (XEXP (src, 0))
7836 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7837 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7838 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7839 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7840 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7841 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7842 {
7843 assign = make_extraction (VOIDmode, dest, 0,
7844 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7845 1, 1, 1, 0);
7846 if (assign != 0)
7847 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7848 return x;
7849 }
7850
7851 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7852 one-bit field. */
7853 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7854 && XEXP (XEXP (src, 0), 0) == const1_rtx
7855 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7856 {
7857 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7858 1, 1, 1, 0);
7859 if (assign != 0)
7860 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7861 return x;
7862 }
7863
7864 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7865 SRC is an AND with all bits of that field set, then we can discard
7866 the AND. */
7867 if (GET_CODE (dest) == ZERO_EXTRACT
7868 && GET_CODE (XEXP (dest, 1)) == CONST_INT
7869 && GET_CODE (src) == AND
7870 && GET_CODE (XEXP (src, 1)) == CONST_INT)
7871 {
7872 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7873 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7874 unsigned HOST_WIDE_INT ze_mask;
7875
7876 if (width >= HOST_BITS_PER_WIDE_INT)
7877 ze_mask = -1;
7878 else
7879 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7880
7881 /* Complete overlap. We can remove the source AND. */
7882 if ((and_mask & ze_mask) == ze_mask)
7883 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7884
7885 /* Partial overlap. We can reduce the source AND. */
7886 if ((and_mask & ze_mask) != and_mask)
7887 {
7888 mode = GET_MODE (src);
7889 src = gen_rtx_AND (mode, XEXP (src, 0),
7890 gen_int_mode (and_mask & ze_mask, mode));
7891 return gen_rtx_SET (VOIDmode, dest, src);
7892 }
7893 }
7894
7895 /* The other case we handle is assignments into a constant-position
7896 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7897 a mask that has all one bits except for a group of zero bits and
7898 OTHER is known to have zeros where C1 has ones, this is such an
7899 assignment. Compute the position and length from C1. Shift OTHER
7900 to the appropriate position, force it to the required mode, and
7901 make the extraction. Check for the AND in both operands. */
7902
7903 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7904 return x;
7905
7906 rhs = expand_compound_operation (XEXP (src, 0));
7907 lhs = expand_compound_operation (XEXP (src, 1));
7908
7909 if (GET_CODE (rhs) == AND
7910 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7911 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7912 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7913 else if (GET_CODE (lhs) == AND
7914 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7915 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7916 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7917 else
7918 return x;
7919
7920 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7921 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7922 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7923 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7924 return x;
7925
7926 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7927 if (assign == 0)
7928 return x;
7929
7930 /* The mode to use for the source is the mode of the assignment, or of
7931 what is inside a possible STRICT_LOW_PART. */
7932 mode = (GET_CODE (assign) == STRICT_LOW_PART
7933 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7934
7935 /* Shift OTHER right POS places and make it the source, restricting it
7936 to the proper length and mode. */
7937
7938 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7939 GET_MODE (src), other, pos),
7940 mode,
7941 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7942 ? ~(unsigned HOST_WIDE_INT) 0
7943 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7944 dest, 0);
7945
7946 /* If SRC is masked by an AND that does not make a difference in
7947 the value being stored, strip it. */
7948 if (GET_CODE (assign) == ZERO_EXTRACT
7949 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7950 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7951 && GET_CODE (src) == AND
7952 && GET_CODE (XEXP (src, 1)) == CONST_INT
7953 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7954 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7955 src = XEXP (src, 0);
7956
7957 return gen_rtx_SET (VOIDmode, assign, src);
7958 }
7959 \f
7960 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7961 if so. */
7962
7963 static rtx
7964 apply_distributive_law (rtx x)
7965 {
7966 enum rtx_code code = GET_CODE (x);
7967 enum rtx_code inner_code;
7968 rtx lhs, rhs, other;
7969 rtx tem;
7970
7971 /* Distributivity is not true for floating point as it can change the
7972 value. So we don't do it unless -funsafe-math-optimizations. */
7973 if (FLOAT_MODE_P (GET_MODE (x))
7974 && ! flag_unsafe_math_optimizations)
7975 return x;
7976
7977 /* The outer operation can only be one of the following: */
7978 if (code != IOR && code != AND && code != XOR
7979 && code != PLUS && code != MINUS)
7980 return x;
7981
7982 lhs = XEXP (x, 0);
7983 rhs = XEXP (x, 1);
7984
7985 /* If either operand is a primitive we can't do anything, so get out
7986 fast. */
7987 if (OBJECT_P (lhs) || OBJECT_P (rhs))
7988 return x;
7989
7990 lhs = expand_compound_operation (lhs);
7991 rhs = expand_compound_operation (rhs);
7992 inner_code = GET_CODE (lhs);
7993 if (inner_code != GET_CODE (rhs))
7994 return x;
7995
7996 /* See if the inner and outer operations distribute. */
7997 switch (inner_code)
7998 {
7999 case LSHIFTRT:
8000 case ASHIFTRT:
8001 case AND:
8002 case IOR:
8003 /* These all distribute except over PLUS. */
8004 if (code == PLUS || code == MINUS)
8005 return x;
8006 break;
8007
8008 case MULT:
8009 if (code != PLUS && code != MINUS)
8010 return x;
8011 break;
8012
8013 case ASHIFT:
8014 /* This is also a multiply, so it distributes over everything. */
8015 break;
8016
8017 case SUBREG:
8018 /* Non-paradoxical SUBREGs distributes over all operations, provided
8019 the inner modes and byte offsets are the same, this is an extraction
8020 of a low-order part, we don't convert an fp operation to int or
8021 vice versa, and we would not be converting a single-word
8022 operation into a multi-word operation. The latter test is not
8023 required, but it prevents generating unneeded multi-word operations.
8024 Some of the previous tests are redundant given the latter test, but
8025 are retained because they are required for correctness.
8026
8027 We produce the result slightly differently in this case. */
8028
8029 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8030 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8031 || ! subreg_lowpart_p (lhs)
8032 || (GET_MODE_CLASS (GET_MODE (lhs))
8033 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8034 || (GET_MODE_SIZE (GET_MODE (lhs))
8035 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8036 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
8037 return x;
8038
8039 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8040 SUBREG_REG (lhs), SUBREG_REG (rhs));
8041 return gen_lowpart (GET_MODE (x), tem);
8042
8043 default:
8044 return x;
8045 }
8046
8047 /* Set LHS and RHS to the inner operands (A and B in the example
8048 above) and set OTHER to the common operand (C in the example).
8049 There is only one way to do this unless the inner operation is
8050 commutative. */
8051 if (COMMUTATIVE_ARITH_P (lhs)
8052 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8053 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8054 else if (COMMUTATIVE_ARITH_P (lhs)
8055 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8056 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8057 else if (COMMUTATIVE_ARITH_P (lhs)
8058 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8059 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8060 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8061 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8062 else
8063 return x;
8064
8065 /* Form the new inner operation, seeing if it simplifies first. */
8066 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
8067
8068 /* There is one exception to the general way of distributing:
8069 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8070 if (code == XOR && inner_code == IOR)
8071 {
8072 inner_code = AND;
8073 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8074 }
8075
8076 /* We may be able to continuing distributing the result, so call
8077 ourselves recursively on the inner operation before forming the
8078 outer operation, which we return. */
8079 return gen_binary (inner_code, GET_MODE (x),
8080 apply_distributive_law (tem), other);
8081 }
8082 \f
8083 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8084 in MODE.
8085
8086 Return an equivalent form, if different from X. Otherwise, return X. If
8087 X is zero, we are to always construct the equivalent form. */
8088
8089 static rtx
8090 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8091 unsigned HOST_WIDE_INT constop)
8092 {
8093 unsigned HOST_WIDE_INT nonzero;
8094 int i;
8095
8096 /* Simplify VAROP knowing that we will be only looking at some of the
8097 bits in it.
8098
8099 Note by passing in CONSTOP, we guarantee that the bits not set in
8100 CONSTOP are not significant and will never be examined. We must
8101 ensure that is the case by explicitly masking out those bits
8102 before returning. */
8103 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8104
8105 /* If VAROP is a CLOBBER, we will fail so return it. */
8106 if (GET_CODE (varop) == CLOBBER)
8107 return varop;
8108
8109 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8110 to VAROP and return the new constant. */
8111 if (GET_CODE (varop) == CONST_INT)
8112 return gen_int_mode (INTVAL (varop) & constop, mode);
8113
8114 /* See what bits may be nonzero in VAROP. Unlike the general case of
8115 a call to nonzero_bits, here we don't care about bits outside
8116 MODE. */
8117
8118 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8119
8120 /* Turn off all bits in the constant that are known to already be zero.
8121 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8122 which is tested below. */
8123
8124 constop &= nonzero;
8125
8126 /* If we don't have any bits left, return zero. */
8127 if (constop == 0)
8128 return const0_rtx;
8129
8130 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8131 a power of two, we can replace this with an ASHIFT. */
8132 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8133 && (i = exact_log2 (constop)) >= 0)
8134 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8135
8136 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8137 or XOR, then try to apply the distributive law. This may eliminate
8138 operations if either branch can be simplified because of the AND.
8139 It may also make some cases more complex, but those cases probably
8140 won't match a pattern either with or without this. */
8141
8142 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8143 return
8144 gen_lowpart
8145 (mode,
8146 apply_distributive_law
8147 (gen_binary (GET_CODE (varop), GET_MODE (varop),
8148 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8149 XEXP (varop, 0), constop),
8150 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8151 XEXP (varop, 1), constop))));
8152
8153 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8154 the AND and see if one of the operands simplifies to zero. If so, we
8155 may eliminate it. */
8156
8157 if (GET_CODE (varop) == PLUS
8158 && exact_log2 (constop + 1) >= 0)
8159 {
8160 rtx o0, o1;
8161
8162 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8163 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8164 if (o0 == const0_rtx)
8165 return o1;
8166 if (o1 == const0_rtx)
8167 return o0;
8168 }
8169
8170 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
8171 if we already had one (just check for the simplest cases). */
8172 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8173 && GET_MODE (XEXP (x, 0)) == mode
8174 && SUBREG_REG (XEXP (x, 0)) == varop)
8175 varop = XEXP (x, 0);
8176 else
8177 varop = gen_lowpart (mode, varop);
8178
8179 /* If we can't make the SUBREG, try to return what we were given. */
8180 if (GET_CODE (varop) == CLOBBER)
8181 return x ? x : varop;
8182
8183 /* If we are only masking insignificant bits, return VAROP. */
8184 if (constop == nonzero)
8185 x = varop;
8186 else
8187 {
8188 /* Otherwise, return an AND. */
8189 constop = trunc_int_for_mode (constop, mode);
8190 /* See how much, if any, of X we can use. */
8191 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8192 x = gen_binary (AND, mode, varop, GEN_INT (constop));
8193
8194 else
8195 {
8196 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8197 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8198 SUBST (XEXP (x, 1), GEN_INT (constop));
8199
8200 SUBST (XEXP (x, 0), varop);
8201 }
8202 }
8203
8204 return x;
8205 }
8206 \f
8207 /* Given a REG, X, compute which bits in X can be nonzero.
8208 We don't care about bits outside of those defined in MODE.
8209
8210 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8211 a shift, AND, or zero_extract, we can do better. */
8212
8213 static rtx
8214 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8215 rtx known_x ATTRIBUTE_UNUSED,
8216 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8217 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8218 unsigned HOST_WIDE_INT *nonzero)
8219 {
8220 rtx tem;
8221
8222 /* If X is a register whose nonzero bits value is current, use it.
8223 Otherwise, if X is a register whose value we can find, use that
8224 value. Otherwise, use the previously-computed global nonzero bits
8225 for this register. */
8226
8227 if (reg_stat[REGNO (x)].last_set_value != 0
8228 && (reg_stat[REGNO (x)].last_set_mode == mode
8229 || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8230 && GET_MODE_CLASS (mode) == MODE_INT))
8231 && (reg_stat[REGNO (x)].last_set_label == label_tick
8232 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8233 && REG_N_SETS (REGNO (x)) == 1
8234 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8235 REGNO (x))))
8236 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8237 {
8238 *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8239 return NULL;
8240 }
8241
8242 tem = get_last_value (x);
8243
8244 if (tem)
8245 {
8246 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8247 /* If X is narrower than MODE and TEM is a non-negative
8248 constant that would appear negative in the mode of X,
8249 sign-extend it for use in reg_nonzero_bits because some
8250 machines (maybe most) will actually do the sign-extension
8251 and this is the conservative approach.
8252
8253 ??? For 2.5, try to tighten up the MD files in this regard
8254 instead of this kludge. */
8255
8256 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8257 && GET_CODE (tem) == CONST_INT
8258 && INTVAL (tem) > 0
8259 && 0 != (INTVAL (tem)
8260 & ((HOST_WIDE_INT) 1
8261 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8262 tem = GEN_INT (INTVAL (tem)
8263 | ((HOST_WIDE_INT) (-1)
8264 << GET_MODE_BITSIZE (GET_MODE (x))));
8265 #endif
8266 return tem;
8267 }
8268 else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8269 {
8270 unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8271
8272 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8273 /* We don't know anything about the upper bits. */
8274 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8275 *nonzero &= mask;
8276 }
8277
8278 return NULL;
8279 }
8280
8281 /* Return the number of bits at the high-order end of X that are known to
8282 be equal to the sign bit. X will be used in mode MODE; if MODE is
8283 VOIDmode, X will be used in its own mode. The returned value will always
8284 be between 1 and the number of bits in MODE. */
8285
8286 static rtx
8287 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8288 rtx known_x ATTRIBUTE_UNUSED,
8289 enum machine_mode known_mode
8290 ATTRIBUTE_UNUSED,
8291 unsigned int known_ret ATTRIBUTE_UNUSED,
8292 unsigned int *result)
8293 {
8294 rtx tem;
8295
8296 if (reg_stat[REGNO (x)].last_set_value != 0
8297 && reg_stat[REGNO (x)].last_set_mode == mode
8298 && (reg_stat[REGNO (x)].last_set_label == label_tick
8299 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8300 && REG_N_SETS (REGNO (x)) == 1
8301 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8302 REGNO (x))))
8303 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8304 {
8305 *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8306 return NULL;
8307 }
8308
8309 tem = get_last_value (x);
8310 if (tem != 0)
8311 return tem;
8312
8313 if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8314 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8315 *result = reg_stat[REGNO (x)].sign_bit_copies;
8316
8317 return NULL;
8318 }
8319 \f
8320 /* Return the number of "extended" bits there are in X, when interpreted
8321 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8322 unsigned quantities, this is the number of high-order zero bits.
8323 For signed quantities, this is the number of copies of the sign bit
8324 minus 1. In both case, this function returns the number of "spare"
8325 bits. For example, if two quantities for which this function returns
8326 at least 1 are added, the addition is known not to overflow.
8327
8328 This function will always return 0 unless called during combine, which
8329 implies that it must be called from a define_split. */
8330
8331 unsigned int
8332 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8333 {
8334 if (nonzero_sign_valid == 0)
8335 return 0;
8336
8337 return (unsignedp
8338 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8339 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8340 - floor_log2 (nonzero_bits (x, mode)))
8341 : 0)
8342 : num_sign_bit_copies (x, mode) - 1);
8343 }
8344 \f
8345 /* This function is called from `simplify_shift_const' to merge two
8346 outer operations. Specifically, we have already found that we need
8347 to perform operation *POP0 with constant *PCONST0 at the outermost
8348 position. We would now like to also perform OP1 with constant CONST1
8349 (with *POP0 being done last).
8350
8351 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8352 the resulting operation. *PCOMP_P is set to 1 if we would need to
8353 complement the innermost operand, otherwise it is unchanged.
8354
8355 MODE is the mode in which the operation will be done. No bits outside
8356 the width of this mode matter. It is assumed that the width of this mode
8357 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8358
8359 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8360 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8361 result is simply *PCONST0.
8362
8363 If the resulting operation cannot be expressed as one operation, we
8364 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8365
8366 static int
8367 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)
8368 {
8369 enum rtx_code op0 = *pop0;
8370 HOST_WIDE_INT const0 = *pconst0;
8371
8372 const0 &= GET_MODE_MASK (mode);
8373 const1 &= GET_MODE_MASK (mode);
8374
8375 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8376 if (op0 == AND)
8377 const1 &= const0;
8378
8379 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8380 if OP0 is SET. */
8381
8382 if (op1 == UNKNOWN || op0 == SET)
8383 return 1;
8384
8385 else if (op0 == UNKNOWN)
8386 op0 = op1, const0 = const1;
8387
8388 else if (op0 == op1)
8389 {
8390 switch (op0)
8391 {
8392 case AND:
8393 const0 &= const1;
8394 break;
8395 case IOR:
8396 const0 |= const1;
8397 break;
8398 case XOR:
8399 const0 ^= const1;
8400 break;
8401 case PLUS:
8402 const0 += const1;
8403 break;
8404 case NEG:
8405 op0 = UNKNOWN;
8406 break;
8407 default:
8408 break;
8409 }
8410 }
8411
8412 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8413 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8414 return 0;
8415
8416 /* If the two constants aren't the same, we can't do anything. The
8417 remaining six cases can all be done. */
8418 else if (const0 != const1)
8419 return 0;
8420
8421 else
8422 switch (op0)
8423 {
8424 case IOR:
8425 if (op1 == AND)
8426 /* (a & b) | b == b */
8427 op0 = SET;
8428 else /* op1 == XOR */
8429 /* (a ^ b) | b == a | b */
8430 {;}
8431 break;
8432
8433 case XOR:
8434 if (op1 == AND)
8435 /* (a & b) ^ b == (~a) & b */
8436 op0 = AND, *pcomp_p = 1;
8437 else /* op1 == IOR */
8438 /* (a | b) ^ b == a & ~b */
8439 op0 = AND, const0 = ~const0;
8440 break;
8441
8442 case AND:
8443 if (op1 == IOR)
8444 /* (a | b) & b == b */
8445 op0 = SET;
8446 else /* op1 == XOR */
8447 /* (a ^ b) & b) == (~a) & b */
8448 *pcomp_p = 1;
8449 break;
8450 default:
8451 break;
8452 }
8453
8454 /* Check for NO-OP cases. */
8455 const0 &= GET_MODE_MASK (mode);
8456 if (const0 == 0
8457 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8458 op0 = UNKNOWN;
8459 else if (const0 == 0 && op0 == AND)
8460 op0 = SET;
8461 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8462 && op0 == AND)
8463 op0 = UNKNOWN;
8464
8465 /* ??? Slightly redundant with the above mask, but not entirely.
8466 Moving this above means we'd have to sign-extend the mode mask
8467 for the final test. */
8468 const0 = trunc_int_for_mode (const0, mode);
8469
8470 *pop0 = op0;
8471 *pconst0 = const0;
8472
8473 return 1;
8474 }
8475 \f
8476 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8477 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
8478 that we started with.
8479
8480 The shift is normally computed in the widest mode we find in VAROP, as
8481 long as it isn't a different number of words than RESULT_MODE. Exceptions
8482 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8483
8484 static rtx
8485 simplify_shift_const (rtx x, enum rtx_code code,
8486 enum machine_mode result_mode, rtx varop,
8487 int orig_count)
8488 {
8489 enum rtx_code orig_code = code;
8490 unsigned int count;
8491 int signed_count;
8492 enum machine_mode mode = result_mode;
8493 enum machine_mode shift_mode, tmode;
8494 unsigned int mode_words
8495 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8496 /* We form (outer_op (code varop count) (outer_const)). */
8497 enum rtx_code outer_op = UNKNOWN;
8498 HOST_WIDE_INT outer_const = 0;
8499 rtx const_rtx;
8500 int complement_p = 0;
8501 rtx new;
8502
8503 /* Make sure and truncate the "natural" shift on the way in. We don't
8504 want to do this inside the loop as it makes it more difficult to
8505 combine shifts. */
8506 if (SHIFT_COUNT_TRUNCATED)
8507 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8508
8509 /* If we were given an invalid count, don't do anything except exactly
8510 what was requested. */
8511
8512 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8513 {
8514 if (x)
8515 return x;
8516
8517 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
8518 }
8519
8520 count = orig_count;
8521
8522 /* Unless one of the branches of the `if' in this loop does a `continue',
8523 we will `break' the loop after the `if'. */
8524
8525 while (count != 0)
8526 {
8527 /* If we have an operand of (clobber (const_int 0)), just return that
8528 value. */
8529 if (GET_CODE (varop) == CLOBBER)
8530 return varop;
8531
8532 /* If we discovered we had to complement VAROP, leave. Making a NOT
8533 here would cause an infinite loop. */
8534 if (complement_p)
8535 break;
8536
8537 /* Convert ROTATERT to ROTATE. */
8538 if (code == ROTATERT)
8539 {
8540 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8541 code = ROTATE;
8542 if (VECTOR_MODE_P (result_mode))
8543 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8544 else
8545 count = bitsize - count;
8546 }
8547
8548 /* We need to determine what mode we will do the shift in. If the
8549 shift is a right shift or a ROTATE, we must always do it in the mode
8550 it was originally done in. Otherwise, we can do it in MODE, the
8551 widest mode encountered. */
8552 shift_mode
8553 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8554 ? result_mode : mode);
8555
8556 /* Handle cases where the count is greater than the size of the mode
8557 minus 1. For ASHIFT, use the size minus one as the count (this can
8558 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8559 take the count modulo the size. For other shifts, the result is
8560 zero.
8561
8562 Since these shifts are being produced by the compiler by combining
8563 multiple operations, each of which are defined, we know what the
8564 result is supposed to be. */
8565
8566 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
8567 {
8568 if (code == ASHIFTRT)
8569 count = GET_MODE_BITSIZE (shift_mode) - 1;
8570 else if (code == ROTATE || code == ROTATERT)
8571 count %= GET_MODE_BITSIZE (shift_mode);
8572 else
8573 {
8574 /* We can't simply return zero because there may be an
8575 outer op. */
8576 varop = const0_rtx;
8577 count = 0;
8578 break;
8579 }
8580 }
8581
8582 /* An arithmetic right shift of a quantity known to be -1 or 0
8583 is a no-op. */
8584 if (code == ASHIFTRT
8585 && (num_sign_bit_copies (varop, shift_mode)
8586 == GET_MODE_BITSIZE (shift_mode)))
8587 {
8588 count = 0;
8589 break;
8590 }
8591
8592 /* If we are doing an arithmetic right shift and discarding all but
8593 the sign bit copies, this is equivalent to doing a shift by the
8594 bitsize minus one. Convert it into that shift because it will often
8595 allow other simplifications. */
8596
8597 if (code == ASHIFTRT
8598 && (count + num_sign_bit_copies (varop, shift_mode)
8599 >= GET_MODE_BITSIZE (shift_mode)))
8600 count = GET_MODE_BITSIZE (shift_mode) - 1;
8601
8602 /* We simplify the tests below and elsewhere by converting
8603 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8604 `make_compound_operation' will convert it to an ASHIFTRT for
8605 those machines (such as VAX) that don't have an LSHIFTRT. */
8606 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8607 && code == ASHIFTRT
8608 && ((nonzero_bits (varop, shift_mode)
8609 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8610 == 0))
8611 code = LSHIFTRT;
8612
8613 if (code == LSHIFTRT
8614 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8615 && !(nonzero_bits (varop, shift_mode) >> count))
8616 varop = const0_rtx;
8617 if (code == ASHIFT
8618 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8619 && !((nonzero_bits (varop, shift_mode) << count)
8620 & GET_MODE_MASK (shift_mode)))
8621 varop = const0_rtx;
8622
8623 switch (GET_CODE (varop))
8624 {
8625 case SIGN_EXTEND:
8626 case ZERO_EXTEND:
8627 case SIGN_EXTRACT:
8628 case ZERO_EXTRACT:
8629 new = expand_compound_operation (varop);
8630 if (new != varop)
8631 {
8632 varop = new;
8633 continue;
8634 }
8635 break;
8636
8637 case MEM:
8638 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8639 minus the width of a smaller mode, we can do this with a
8640 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8641 if ((code == ASHIFTRT || code == LSHIFTRT)
8642 && ! mode_dependent_address_p (XEXP (varop, 0))
8643 && ! MEM_VOLATILE_P (varop)
8644 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8645 MODE_INT, 1)) != BLKmode)
8646 {
8647 new = adjust_address_nv (varop, tmode,
8648 BYTES_BIG_ENDIAN ? 0
8649 : count / BITS_PER_UNIT);
8650
8651 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8652 : ZERO_EXTEND, mode, new);
8653 count = 0;
8654 continue;
8655 }
8656 break;
8657
8658 case USE:
8659 /* Similar to the case above, except that we can only do this if
8660 the resulting mode is the same as that of the underlying
8661 MEM and adjust the address depending on the *bits* endianness
8662 because of the way that bit-field extract insns are defined. */
8663 if ((code == ASHIFTRT || code == LSHIFTRT)
8664 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8665 MODE_INT, 1)) != BLKmode
8666 && tmode == GET_MODE (XEXP (varop, 0)))
8667 {
8668 if (BITS_BIG_ENDIAN)
8669 new = XEXP (varop, 0);
8670 else
8671 {
8672 new = copy_rtx (XEXP (varop, 0));
8673 SUBST (XEXP (new, 0),
8674 plus_constant (XEXP (new, 0),
8675 count / BITS_PER_UNIT));
8676 }
8677
8678 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8679 : ZERO_EXTEND, mode, new);
8680 count = 0;
8681 continue;
8682 }
8683 break;
8684
8685 case SUBREG:
8686 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8687 the same number of words as what we've seen so far. Then store
8688 the widest mode in MODE. */
8689 if (subreg_lowpart_p (varop)
8690 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8691 > GET_MODE_SIZE (GET_MODE (varop)))
8692 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8693 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8694 == mode_words)
8695 {
8696 varop = SUBREG_REG (varop);
8697 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8698 mode = GET_MODE (varop);
8699 continue;
8700 }
8701 break;
8702
8703 case MULT:
8704 /* Some machines use MULT instead of ASHIFT because MULT
8705 is cheaper. But it is still better on those machines to
8706 merge two shifts into one. */
8707 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8708 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8709 {
8710 varop
8711 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8712 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8713 continue;
8714 }
8715 break;
8716
8717 case UDIV:
8718 /* Similar, for when divides are cheaper. */
8719 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8720 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8721 {
8722 varop
8723 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8724 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8725 continue;
8726 }
8727 break;
8728
8729 case ASHIFTRT:
8730 /* If we are extracting just the sign bit of an arithmetic
8731 right shift, that shift is not needed. However, the sign
8732 bit of a wider mode may be different from what would be
8733 interpreted as the sign bit in a narrower mode, so, if
8734 the result is narrower, don't discard the shift. */
8735 if (code == LSHIFTRT
8736 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8737 && (GET_MODE_BITSIZE (result_mode)
8738 >= GET_MODE_BITSIZE (GET_MODE (varop))))
8739 {
8740 varop = XEXP (varop, 0);
8741 continue;
8742 }
8743
8744 /* ... fall through ... */
8745
8746 case LSHIFTRT:
8747 case ASHIFT:
8748 case ROTATE:
8749 /* Here we have two nested shifts. The result is usually the
8750 AND of a new shift with a mask. We compute the result below. */
8751 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8752 && INTVAL (XEXP (varop, 1)) >= 0
8753 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8754 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8755 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8756 {
8757 enum rtx_code first_code = GET_CODE (varop);
8758 unsigned int first_count = INTVAL (XEXP (varop, 1));
8759 unsigned HOST_WIDE_INT mask;
8760 rtx mask_rtx;
8761
8762 /* We have one common special case. We can't do any merging if
8763 the inner code is an ASHIFTRT of a smaller mode. However, if
8764 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8765 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8766 we can convert it to
8767 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8768 This simplifies certain SIGN_EXTEND operations. */
8769 if (code == ASHIFT && first_code == ASHIFTRT
8770 && count == (unsigned int)
8771 (GET_MODE_BITSIZE (result_mode)
8772 - GET_MODE_BITSIZE (GET_MODE (varop))))
8773 {
8774 /* C3 has the low-order C1 bits zero. */
8775
8776 mask = (GET_MODE_MASK (mode)
8777 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8778
8779 varop = simplify_and_const_int (NULL_RTX, result_mode,
8780 XEXP (varop, 0), mask);
8781 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8782 varop, count);
8783 count = first_count;
8784 code = ASHIFTRT;
8785 continue;
8786 }
8787
8788 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8789 than C1 high-order bits equal to the sign bit, we can convert
8790 this to either an ASHIFT or an ASHIFTRT depending on the
8791 two counts.
8792
8793 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8794
8795 if (code == ASHIFTRT && first_code == ASHIFT
8796 && GET_MODE (varop) == shift_mode
8797 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8798 > first_count))
8799 {
8800 varop = XEXP (varop, 0);
8801
8802 signed_count = count - first_count;
8803 if (signed_count < 0)
8804 count = -signed_count, code = ASHIFT;
8805 else
8806 count = signed_count;
8807
8808 continue;
8809 }
8810
8811 /* There are some cases we can't do. If CODE is ASHIFTRT,
8812 we can only do this if FIRST_CODE is also ASHIFTRT.
8813
8814 We can't do the case when CODE is ROTATE and FIRST_CODE is
8815 ASHIFTRT.
8816
8817 If the mode of this shift is not the mode of the outer shift,
8818 we can't do this if either shift is a right shift or ROTATE.
8819
8820 Finally, we can't do any of these if the mode is too wide
8821 unless the codes are the same.
8822
8823 Handle the case where the shift codes are the same
8824 first. */
8825
8826 if (code == first_code)
8827 {
8828 if (GET_MODE (varop) != result_mode
8829 && (code == ASHIFTRT || code == LSHIFTRT
8830 || code == ROTATE))
8831 break;
8832
8833 count += first_count;
8834 varop = XEXP (varop, 0);
8835 continue;
8836 }
8837
8838 if (code == ASHIFTRT
8839 || (code == ROTATE && first_code == ASHIFTRT)
8840 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8841 || (GET_MODE (varop) != result_mode
8842 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8843 || first_code == ROTATE
8844 || code == ROTATE)))
8845 break;
8846
8847 /* To compute the mask to apply after the shift, shift the
8848 nonzero bits of the inner shift the same way the
8849 outer shift will. */
8850
8851 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8852
8853 mask_rtx
8854 = simplify_binary_operation (code, result_mode, mask_rtx,
8855 GEN_INT (count));
8856
8857 /* Give up if we can't compute an outer operation to use. */
8858 if (mask_rtx == 0
8859 || GET_CODE (mask_rtx) != CONST_INT
8860 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8861 INTVAL (mask_rtx),
8862 result_mode, &complement_p))
8863 break;
8864
8865 /* If the shifts are in the same direction, we add the
8866 counts. Otherwise, we subtract them. */
8867 signed_count = count;
8868 if ((code == ASHIFTRT || code == LSHIFTRT)
8869 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8870 signed_count += first_count;
8871 else
8872 signed_count -= first_count;
8873
8874 /* If COUNT is positive, the new shift is usually CODE,
8875 except for the two exceptions below, in which case it is
8876 FIRST_CODE. If the count is negative, FIRST_CODE should
8877 always be used */
8878 if (signed_count > 0
8879 && ((first_code == ROTATE && code == ASHIFT)
8880 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8881 code = first_code, count = signed_count;
8882 else if (signed_count < 0)
8883 code = first_code, count = -signed_count;
8884 else
8885 count = signed_count;
8886
8887 varop = XEXP (varop, 0);
8888 continue;
8889 }
8890
8891 /* If we have (A << B << C) for any shift, we can convert this to
8892 (A << C << B). This wins if A is a constant. Only try this if
8893 B is not a constant. */
8894
8895 else if (GET_CODE (varop) == code
8896 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8897 && 0 != (new
8898 = simplify_binary_operation (code, mode,
8899 XEXP (varop, 0),
8900 GEN_INT (count))))
8901 {
8902 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8903 count = 0;
8904 continue;
8905 }
8906 break;
8907
8908 case NOT:
8909 /* Make this fit the case below. */
8910 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8911 GEN_INT (GET_MODE_MASK (mode)));
8912 continue;
8913
8914 case IOR:
8915 case AND:
8916 case XOR:
8917 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8918 with C the size of VAROP - 1 and the shift is logical if
8919 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8920 we have an (le X 0) operation. If we have an arithmetic shift
8921 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8922 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8923
8924 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8925 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8926 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8927 && (code == LSHIFTRT || code == ASHIFTRT)
8928 && count == (unsigned int)
8929 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8930 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8931 {
8932 count = 0;
8933 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
8934 const0_rtx);
8935
8936 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8937 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8938
8939 continue;
8940 }
8941
8942 /* If we have (shift (logical)), move the logical to the outside
8943 to allow it to possibly combine with another logical and the
8944 shift to combine with another shift. This also canonicalizes to
8945 what a ZERO_EXTRACT looks like. Also, some machines have
8946 (and (shift)) insns. */
8947
8948 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8949 /* We can't do this if we have (ashiftrt (xor)) and the
8950 constant has its sign bit set in shift_mode. */
8951 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8952 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8953 shift_mode))
8954 && (new = simplify_binary_operation (code, result_mode,
8955 XEXP (varop, 1),
8956 GEN_INT (count))) != 0
8957 && GET_CODE (new) == CONST_INT
8958 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8959 INTVAL (new), result_mode, &complement_p))
8960 {
8961 varop = XEXP (varop, 0);
8962 continue;
8963 }
8964
8965 /* If we can't do that, try to simplify the shift in each arm of the
8966 logical expression, make a new logical expression, and apply
8967 the inverse distributive law. This also can't be done
8968 for some (ashiftrt (xor)). */
8969 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8970 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8971 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8972 shift_mode)))
8973 {
8974 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8975 XEXP (varop, 0), count);
8976 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8977 XEXP (varop, 1), count);
8978
8979 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8980 varop = apply_distributive_law (varop);
8981
8982 count = 0;
8983 continue;
8984 }
8985 break;
8986
8987 case EQ:
8988 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8989 says that the sign bit can be tested, FOO has mode MODE, C is
8990 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8991 that may be nonzero. */
8992 if (code == LSHIFTRT
8993 && XEXP (varop, 1) == const0_rtx
8994 && GET_MODE (XEXP (varop, 0)) == result_mode
8995 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
8996 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8997 && ((STORE_FLAG_VALUE
8998 & ((HOST_WIDE_INT) 1
8999 < (GET_MODE_BITSIZE (result_mode) - 1))))
9000 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9001 && merge_outer_ops (&outer_op, &outer_const, XOR,
9002 (HOST_WIDE_INT) 1, result_mode,
9003 &complement_p))
9004 {
9005 varop = XEXP (varop, 0);
9006 count = 0;
9007 continue;
9008 }
9009 break;
9010
9011 case NEG:
9012 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9013 than the number of bits in the mode is equivalent to A. */
9014 if (code == LSHIFTRT
9015 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9016 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9017 {
9018 varop = XEXP (varop, 0);
9019 count = 0;
9020 continue;
9021 }
9022
9023 /* NEG commutes with ASHIFT since it is multiplication. Move the
9024 NEG outside to allow shifts to combine. */
9025 if (code == ASHIFT
9026 && merge_outer_ops (&outer_op, &outer_const, NEG,
9027 (HOST_WIDE_INT) 0, result_mode,
9028 &complement_p))
9029 {
9030 varop = XEXP (varop, 0);
9031 continue;
9032 }
9033 break;
9034
9035 case PLUS:
9036 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9037 is one less than the number of bits in the mode is
9038 equivalent to (xor A 1). */
9039 if (code == LSHIFTRT
9040 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9041 && XEXP (varop, 1) == constm1_rtx
9042 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9043 && merge_outer_ops (&outer_op, &outer_const, XOR,
9044 (HOST_WIDE_INT) 1, result_mode,
9045 &complement_p))
9046 {
9047 count = 0;
9048 varop = XEXP (varop, 0);
9049 continue;
9050 }
9051
9052 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9053 that might be nonzero in BAR are those being shifted out and those
9054 bits are known zero in FOO, we can replace the PLUS with FOO.
9055 Similarly in the other operand order. This code occurs when
9056 we are computing the size of a variable-size array. */
9057
9058 if ((code == ASHIFTRT || code == LSHIFTRT)
9059 && count < HOST_BITS_PER_WIDE_INT
9060 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9061 && (nonzero_bits (XEXP (varop, 1), result_mode)
9062 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9063 {
9064 varop = XEXP (varop, 0);
9065 continue;
9066 }
9067 else if ((code == ASHIFTRT || code == LSHIFTRT)
9068 && count < HOST_BITS_PER_WIDE_INT
9069 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9070 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9071 >> count)
9072 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9073 & nonzero_bits (XEXP (varop, 1),
9074 result_mode)))
9075 {
9076 varop = XEXP (varop, 1);
9077 continue;
9078 }
9079
9080 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9081 if (code == ASHIFT
9082 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9083 && (new = simplify_binary_operation (ASHIFT, result_mode,
9084 XEXP (varop, 1),
9085 GEN_INT (count))) != 0
9086 && GET_CODE (new) == CONST_INT
9087 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9088 INTVAL (new), result_mode, &complement_p))
9089 {
9090 varop = XEXP (varop, 0);
9091 continue;
9092 }
9093
9094 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9095 signbit', and attempt to change the PLUS to an XOR and move it to
9096 the outer operation as is done above in the AND/IOR/XOR case
9097 leg for shift(logical). See details in logical handling above
9098 for reasoning in doing so. */
9099 if (code == LSHIFTRT
9100 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9101 && mode_signbit_p (result_mode, XEXP (varop, 1))
9102 && (new = simplify_binary_operation (code, result_mode,
9103 XEXP (varop, 1),
9104 GEN_INT (count))) != 0
9105 && GET_CODE (new) == CONST_INT
9106 && merge_outer_ops (&outer_op, &outer_const, XOR,
9107 INTVAL (new), result_mode, &complement_p))
9108 {
9109 varop = XEXP (varop, 0);
9110 continue;
9111 }
9112
9113 break;
9114
9115 case MINUS:
9116 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9117 with C the size of VAROP - 1 and the shift is logical if
9118 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9119 we have a (gt X 0) operation. If the shift is arithmetic with
9120 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9121 we have a (neg (gt X 0)) operation. */
9122
9123 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9124 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9125 && count == (unsigned int)
9126 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9127 && (code == LSHIFTRT || code == ASHIFTRT)
9128 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9129 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9130 == count
9131 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9132 {
9133 count = 0;
9134 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9135 const0_rtx);
9136
9137 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9138 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9139
9140 continue;
9141 }
9142 break;
9143
9144 case TRUNCATE:
9145 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9146 if the truncate does not affect the value. */
9147 if (code == LSHIFTRT
9148 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9149 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9150 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9151 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9152 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9153 {
9154 rtx varop_inner = XEXP (varop, 0);
9155
9156 varop_inner
9157 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9158 XEXP (varop_inner, 0),
9159 GEN_INT
9160 (count + INTVAL (XEXP (varop_inner, 1))));
9161 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9162 count = 0;
9163 continue;
9164 }
9165 break;
9166
9167 default:
9168 break;
9169 }
9170
9171 break;
9172 }
9173
9174 /* We need to determine what mode to do the shift in. If the shift is
9175 a right shift or ROTATE, we must always do it in the mode it was
9176 originally done in. Otherwise, we can do it in MODE, the widest mode
9177 encountered. The code we care about is that of the shift that will
9178 actually be done, not the shift that was originally requested. */
9179 shift_mode
9180 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9181 ? result_mode : mode);
9182
9183 /* We have now finished analyzing the shift. The result should be
9184 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9185 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9186 to the result of the shift. OUTER_CONST is the relevant constant,
9187 but we must turn off all bits turned off in the shift.
9188
9189 If we were passed a value for X, see if we can use any pieces of
9190 it. If not, make new rtx. */
9191
9192 if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9193 && GET_CODE (XEXP (x, 1)) == CONST_INT
9194 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9195 const_rtx = XEXP (x, 1);
9196 else
9197 const_rtx = GEN_INT (count);
9198
9199 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9200 && GET_MODE (XEXP (x, 0)) == shift_mode
9201 && SUBREG_REG (XEXP (x, 0)) == varop)
9202 varop = XEXP (x, 0);
9203 else if (GET_MODE (varop) != shift_mode)
9204 varop = gen_lowpart (shift_mode, varop);
9205
9206 /* If we can't make the SUBREG, try to return what we were given. */
9207 if (GET_CODE (varop) == CLOBBER)
9208 return x ? x : varop;
9209
9210 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9211 if (new != 0)
9212 x = new;
9213 else
9214 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9215
9216 /* If we have an outer operation and we just made a shift, it is
9217 possible that we could have simplified the shift were it not
9218 for the outer operation. So try to do the simplification
9219 recursively. */
9220
9221 if (outer_op != UNKNOWN && GET_CODE (x) == code
9222 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9223 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9224 INTVAL (XEXP (x, 1)));
9225
9226 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9227 turn off all the bits that the shift would have turned off. */
9228 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9229 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9230 GET_MODE_MASK (result_mode) >> orig_count);
9231
9232 /* Do the remainder of the processing in RESULT_MODE. */
9233 x = gen_lowpart (result_mode, x);
9234
9235 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9236 operation. */
9237 if (complement_p)
9238 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9239
9240 if (outer_op != UNKNOWN)
9241 {
9242 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9243 outer_const = trunc_int_for_mode (outer_const, result_mode);
9244
9245 if (outer_op == AND)
9246 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9247 else if (outer_op == SET)
9248 /* This means that we have determined that the result is
9249 equivalent to a constant. This should be rare. */
9250 x = GEN_INT (outer_const);
9251 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9252 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9253 else
9254 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9255 }
9256
9257 return x;
9258 }
9259 \f
9260 /* Like recog, but we receive the address of a pointer to a new pattern.
9261 We try to match the rtx that the pointer points to.
9262 If that fails, we may try to modify or replace the pattern,
9263 storing the replacement into the same pointer object.
9264
9265 Modifications include deletion or addition of CLOBBERs.
9266
9267 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9268 the CLOBBERs are placed.
9269
9270 The value is the final insn code from the pattern ultimately matched,
9271 or -1. */
9272
9273 static int
9274 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9275 {
9276 rtx pat = *pnewpat;
9277 int insn_code_number;
9278 int num_clobbers_to_add = 0;
9279 int i;
9280 rtx notes = 0;
9281 rtx old_notes, old_pat;
9282
9283 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9284 we use to indicate that something didn't match. If we find such a
9285 thing, force rejection. */
9286 if (GET_CODE (pat) == PARALLEL)
9287 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9288 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9289 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9290 return -1;
9291
9292 old_pat = PATTERN (insn);
9293 old_notes = REG_NOTES (insn);
9294 PATTERN (insn) = pat;
9295 REG_NOTES (insn) = 0;
9296
9297 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9298
9299 /* If it isn't, there is the possibility that we previously had an insn
9300 that clobbered some register as a side effect, but the combined
9301 insn doesn't need to do that. So try once more without the clobbers
9302 unless this represents an ASM insn. */
9303
9304 if (insn_code_number < 0 && ! check_asm_operands (pat)
9305 && GET_CODE (pat) == PARALLEL)
9306 {
9307 int pos;
9308
9309 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9310 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9311 {
9312 if (i != pos)
9313 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9314 pos++;
9315 }
9316
9317 SUBST_INT (XVECLEN (pat, 0), pos);
9318
9319 if (pos == 1)
9320 pat = XVECEXP (pat, 0, 0);
9321
9322 PATTERN (insn) = pat;
9323 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9324 }
9325 PATTERN (insn) = old_pat;
9326 REG_NOTES (insn) = old_notes;
9327
9328 /* Recognize all noop sets, these will be killed by followup pass. */
9329 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9330 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9331
9332 /* If we had any clobbers to add, make a new pattern than contains
9333 them. Then check to make sure that all of them are dead. */
9334 if (num_clobbers_to_add)
9335 {
9336 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9337 rtvec_alloc (GET_CODE (pat) == PARALLEL
9338 ? (XVECLEN (pat, 0)
9339 + num_clobbers_to_add)
9340 : num_clobbers_to_add + 1));
9341
9342 if (GET_CODE (pat) == PARALLEL)
9343 for (i = 0; i < XVECLEN (pat, 0); i++)
9344 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9345 else
9346 XVECEXP (newpat, 0, 0) = pat;
9347
9348 add_clobbers (newpat, insn_code_number);
9349
9350 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9351 i < XVECLEN (newpat, 0); i++)
9352 {
9353 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9354 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9355 return -1;
9356 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9357 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9358 }
9359 pat = newpat;
9360 }
9361
9362 *pnewpat = pat;
9363 *pnotes = notes;
9364
9365 return insn_code_number;
9366 }
9367 \f
9368 /* Like gen_lowpart_general but for use by combine. In combine it
9369 is not possible to create any new pseudoregs. However, it is
9370 safe to create invalid memory addresses, because combine will
9371 try to recognize them and all they will do is make the combine
9372 attempt fail.
9373
9374 If for some reason this cannot do its job, an rtx
9375 (clobber (const_int 0)) is returned.
9376 An insn containing that will not be recognized. */
9377
9378 static rtx
9379 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9380 {
9381 enum machine_mode imode = GET_MODE (x);
9382 unsigned int osize = GET_MODE_SIZE (omode);
9383 unsigned int isize = GET_MODE_SIZE (imode);
9384 rtx result;
9385
9386 if (omode == imode)
9387 return x;
9388
9389 /* Return identity if this is a CONST or symbolic reference. */
9390 if (omode == Pmode
9391 && (GET_CODE (x) == CONST
9392 || GET_CODE (x) == SYMBOL_REF
9393 || GET_CODE (x) == LABEL_REF))
9394 return x;
9395
9396 /* We can only support MODE being wider than a word if X is a
9397 constant integer or has a mode the same size. */
9398 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9399 && ! ((imode == VOIDmode
9400 && (GET_CODE (x) == CONST_INT
9401 || GET_CODE (x) == CONST_DOUBLE))
9402 || isize == osize))
9403 goto fail;
9404
9405 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9406 won't know what to do. So we will strip off the SUBREG here and
9407 process normally. */
9408 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9409 {
9410 x = SUBREG_REG (x);
9411
9412 /* For use in case we fall down into the address adjustments
9413 further below, we need to adjust the known mode and size of
9414 x; imode and isize, since we just adjusted x. */
9415 imode = GET_MODE (x);
9416
9417 if (imode == omode)
9418 return x;
9419
9420 isize = GET_MODE_SIZE (imode);
9421 }
9422
9423 result = gen_lowpart_common (omode, x);
9424
9425 #ifdef CANNOT_CHANGE_MODE_CLASS
9426 if (result != 0 && GET_CODE (result) == SUBREG)
9427 record_subregs_of_mode (result);
9428 #endif
9429
9430 if (result)
9431 return result;
9432
9433 if (MEM_P (x))
9434 {
9435 int offset = 0;
9436
9437 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9438 address. */
9439 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9440 goto fail;
9441
9442 /* If we want to refer to something bigger than the original memref,
9443 generate a paradoxical subreg instead. That will force a reload
9444 of the original memref X. */
9445 if (isize < osize)
9446 return gen_rtx_SUBREG (omode, x, 0);
9447
9448 if (WORDS_BIG_ENDIAN)
9449 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9450
9451 /* Adjust the address so that the address-after-the-data is
9452 unchanged. */
9453 if (BYTES_BIG_ENDIAN)
9454 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9455
9456 return adjust_address_nv (x, omode, offset);
9457 }
9458
9459 /* If X is a comparison operator, rewrite it in a new mode. This
9460 probably won't match, but may allow further simplifications. */
9461 else if (COMPARISON_P (x))
9462 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9463
9464 /* If we couldn't simplify X any other way, just enclose it in a
9465 SUBREG. Normally, this SUBREG won't match, but some patterns may
9466 include an explicit SUBREG or we may simplify it further in combine. */
9467 else
9468 {
9469 int offset = 0;
9470 rtx res;
9471
9472 offset = subreg_lowpart_offset (omode, imode);
9473 if (imode == VOIDmode)
9474 {
9475 imode = int_mode_for_mode (omode);
9476 x = gen_lowpart_common (imode, x);
9477 if (x == NULL)
9478 goto fail;
9479 }
9480 res = simplify_gen_subreg (omode, x, imode, offset);
9481 if (res)
9482 return res;
9483 }
9484
9485 fail:
9486 return gen_rtx_CLOBBER (imode, const0_rtx);
9487 }
9488 \f
9489 /* These routines make binary and unary operations by first seeing if they
9490 fold; if not, a new expression is allocated. */
9491
9492 static rtx
9493 gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0, rtx op1)
9494 {
9495 rtx result;
9496 rtx tem;
9497
9498 if (GET_CODE (op0) == CLOBBER)
9499 return op0;
9500 else if (GET_CODE (op1) == CLOBBER)
9501 return op1;
9502
9503 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9504 && swap_commutative_operands_p (op0, op1))
9505 tem = op0, op0 = op1, op1 = tem;
9506
9507 if (GET_RTX_CLASS (code) == RTX_COMPARE
9508 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
9509 {
9510 enum machine_mode op_mode = GET_MODE (op0);
9511
9512 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9513 just (REL_OP X Y). */
9514 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9515 {
9516 op1 = XEXP (op0, 1);
9517 op0 = XEXP (op0, 0);
9518 op_mode = GET_MODE (op0);
9519 }
9520
9521 if (op_mode == VOIDmode)
9522 op_mode = GET_MODE (op1);
9523 result = simplify_relational_operation (code, mode, op_mode, op0, op1);
9524 }
9525 else
9526 result = simplify_binary_operation (code, mode, op0, op1);
9527
9528 if (result)
9529 return result;
9530
9531 /* Put complex operands first and constants second. */
9532 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9533 && swap_commutative_operands_p (op0, op1))
9534 return gen_rtx_fmt_ee (code, mode, op1, op0);
9535
9536 /* If we are turning off bits already known off in OP0, we need not do
9537 an AND. */
9538 else if (code == AND && GET_CODE (op1) == CONST_INT
9539 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9540 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
9541 return op0;
9542
9543 return gen_rtx_fmt_ee (code, mode, op0, op1);
9544 }
9545 \f
9546 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9547 comparison code that will be tested.
9548
9549 The result is a possibly different comparison code to use. *POP0 and
9550 *POP1 may be updated.
9551
9552 It is possible that we might detect that a comparison is either always
9553 true or always false. However, we do not perform general constant
9554 folding in combine, so this knowledge isn't useful. Such tautologies
9555 should have been detected earlier. Hence we ignore all such cases. */
9556
9557 static enum rtx_code
9558 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9559 {
9560 rtx op0 = *pop0;
9561 rtx op1 = *pop1;
9562 rtx tem, tem1;
9563 int i;
9564 enum machine_mode mode, tmode;
9565
9566 /* Try a few ways of applying the same transformation to both operands. */
9567 while (1)
9568 {
9569 #ifndef WORD_REGISTER_OPERATIONS
9570 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9571 so check specially. */
9572 if (code != GTU && code != GEU && code != LTU && code != LEU
9573 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9574 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9575 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9576 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9577 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9578 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9579 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9580 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9581 && XEXP (op0, 1) == XEXP (op1, 1)
9582 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9583 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9584 && (INTVAL (XEXP (op0, 1))
9585 == (GET_MODE_BITSIZE (GET_MODE (op0))
9586 - (GET_MODE_BITSIZE
9587 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9588 {
9589 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9590 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9591 }
9592 #endif
9593
9594 /* If both operands are the same constant shift, see if we can ignore the
9595 shift. We can if the shift is a rotate or if the bits shifted out of
9596 this shift are known to be zero for both inputs and if the type of
9597 comparison is compatible with the shift. */
9598 if (GET_CODE (op0) == GET_CODE (op1)
9599 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9600 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9601 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9602 && (code != GT && code != LT && code != GE && code != LE))
9603 || (GET_CODE (op0) == ASHIFTRT
9604 && (code != GTU && code != LTU
9605 && code != GEU && code != LEU)))
9606 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9607 && INTVAL (XEXP (op0, 1)) >= 0
9608 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9609 && XEXP (op0, 1) == XEXP (op1, 1))
9610 {
9611 enum machine_mode mode = GET_MODE (op0);
9612 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9613 int shift_count = INTVAL (XEXP (op0, 1));
9614
9615 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9616 mask &= (mask >> shift_count) << shift_count;
9617 else if (GET_CODE (op0) == ASHIFT)
9618 mask = (mask & (mask << shift_count)) >> shift_count;
9619
9620 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9621 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9622 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9623 else
9624 break;
9625 }
9626
9627 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9628 SUBREGs are of the same mode, and, in both cases, the AND would
9629 be redundant if the comparison was done in the narrower mode,
9630 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9631 and the operand's possibly nonzero bits are 0xffffff01; in that case
9632 if we only care about QImode, we don't need the AND). This case
9633 occurs if the output mode of an scc insn is not SImode and
9634 STORE_FLAG_VALUE == 1 (e.g., the 386).
9635
9636 Similarly, check for a case where the AND's are ZERO_EXTEND
9637 operations from some narrower mode even though a SUBREG is not
9638 present. */
9639
9640 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9641 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9642 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9643 {
9644 rtx inner_op0 = XEXP (op0, 0);
9645 rtx inner_op1 = XEXP (op1, 0);
9646 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9647 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9648 int changed = 0;
9649
9650 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9651 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9652 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9653 && (GET_MODE (SUBREG_REG (inner_op0))
9654 == GET_MODE (SUBREG_REG (inner_op1)))
9655 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9656 <= HOST_BITS_PER_WIDE_INT)
9657 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9658 GET_MODE (SUBREG_REG (inner_op0)))))
9659 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9660 GET_MODE (SUBREG_REG (inner_op1))))))
9661 {
9662 op0 = SUBREG_REG (inner_op0);
9663 op1 = SUBREG_REG (inner_op1);
9664
9665 /* The resulting comparison is always unsigned since we masked
9666 off the original sign bit. */
9667 code = unsigned_condition (code);
9668
9669 changed = 1;
9670 }
9671
9672 else if (c0 == c1)
9673 for (tmode = GET_CLASS_NARROWEST_MODE
9674 (GET_MODE_CLASS (GET_MODE (op0)));
9675 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9676 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9677 {
9678 op0 = gen_lowpart (tmode, inner_op0);
9679 op1 = gen_lowpart (tmode, inner_op1);
9680 code = unsigned_condition (code);
9681 changed = 1;
9682 break;
9683 }
9684
9685 if (! changed)
9686 break;
9687 }
9688
9689 /* If both operands are NOT, we can strip off the outer operation
9690 and adjust the comparison code for swapped operands; similarly for
9691 NEG, except that this must be an equality comparison. */
9692 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9693 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9694 && (code == EQ || code == NE)))
9695 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9696
9697 else
9698 break;
9699 }
9700
9701 /* If the first operand is a constant, swap the operands and adjust the
9702 comparison code appropriately, but don't do this if the second operand
9703 is already a constant integer. */
9704 if (swap_commutative_operands_p (op0, op1))
9705 {
9706 tem = op0, op0 = op1, op1 = tem;
9707 code = swap_condition (code);
9708 }
9709
9710 /* We now enter a loop during which we will try to simplify the comparison.
9711 For the most part, we only are concerned with comparisons with zero,
9712 but some things may really be comparisons with zero but not start
9713 out looking that way. */
9714
9715 while (GET_CODE (op1) == CONST_INT)
9716 {
9717 enum machine_mode mode = GET_MODE (op0);
9718 unsigned int mode_width = GET_MODE_BITSIZE (mode);
9719 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9720 int equality_comparison_p;
9721 int sign_bit_comparison_p;
9722 int unsigned_comparison_p;
9723 HOST_WIDE_INT const_op;
9724
9725 /* We only want to handle integral modes. This catches VOIDmode,
9726 CCmode, and the floating-point modes. An exception is that we
9727 can handle VOIDmode if OP0 is a COMPARE or a comparison
9728 operation. */
9729
9730 if (GET_MODE_CLASS (mode) != MODE_INT
9731 && ! (mode == VOIDmode
9732 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9733 break;
9734
9735 /* Get the constant we are comparing against and turn off all bits
9736 not on in our mode. */
9737 const_op = INTVAL (op1);
9738 if (mode != VOIDmode)
9739 const_op = trunc_int_for_mode (const_op, mode);
9740 op1 = GEN_INT (const_op);
9741
9742 /* If we are comparing against a constant power of two and the value
9743 being compared can only have that single bit nonzero (e.g., it was
9744 `and'ed with that bit), we can replace this with a comparison
9745 with zero. */
9746 if (const_op
9747 && (code == EQ || code == NE || code == GE || code == GEU
9748 || code == LT || code == LTU)
9749 && mode_width <= HOST_BITS_PER_WIDE_INT
9750 && exact_log2 (const_op) >= 0
9751 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9752 {
9753 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9754 op1 = const0_rtx, const_op = 0;
9755 }
9756
9757 /* Similarly, if we are comparing a value known to be either -1 or
9758 0 with -1, change it to the opposite comparison against zero. */
9759
9760 if (const_op == -1
9761 && (code == EQ || code == NE || code == GT || code == LE
9762 || code == GEU || code == LTU)
9763 && num_sign_bit_copies (op0, mode) == mode_width)
9764 {
9765 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9766 op1 = const0_rtx, const_op = 0;
9767 }
9768
9769 /* Do some canonicalizations based on the comparison code. We prefer
9770 comparisons against zero and then prefer equality comparisons.
9771 If we can reduce the size of a constant, we will do that too. */
9772
9773 switch (code)
9774 {
9775 case LT:
9776 /* < C is equivalent to <= (C - 1) */
9777 if (const_op > 0)
9778 {
9779 const_op -= 1;
9780 op1 = GEN_INT (const_op);
9781 code = LE;
9782 /* ... fall through to LE case below. */
9783 }
9784 else
9785 break;
9786
9787 case LE:
9788 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9789 if (const_op < 0)
9790 {
9791 const_op += 1;
9792 op1 = GEN_INT (const_op);
9793 code = LT;
9794 }
9795
9796 /* If we are doing a <= 0 comparison on a value known to have
9797 a zero sign bit, we can replace this with == 0. */
9798 else if (const_op == 0
9799 && mode_width <= HOST_BITS_PER_WIDE_INT
9800 && (nonzero_bits (op0, mode)
9801 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9802 code = EQ;
9803 break;
9804
9805 case GE:
9806 /* >= C is equivalent to > (C - 1). */
9807 if (const_op > 0)
9808 {
9809 const_op -= 1;
9810 op1 = GEN_INT (const_op);
9811 code = GT;
9812 /* ... fall through to GT below. */
9813 }
9814 else
9815 break;
9816
9817 case GT:
9818 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
9819 if (const_op < 0)
9820 {
9821 const_op += 1;
9822 op1 = GEN_INT (const_op);
9823 code = GE;
9824 }
9825
9826 /* If we are doing a > 0 comparison on a value known to have
9827 a zero sign bit, we can replace this with != 0. */
9828 else if (const_op == 0
9829 && mode_width <= HOST_BITS_PER_WIDE_INT
9830 && (nonzero_bits (op0, mode)
9831 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9832 code = NE;
9833 break;
9834
9835 case LTU:
9836 /* < C is equivalent to <= (C - 1). */
9837 if (const_op > 0)
9838 {
9839 const_op -= 1;
9840 op1 = GEN_INT (const_op);
9841 code = LEU;
9842 /* ... fall through ... */
9843 }
9844
9845 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9846 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9847 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9848 {
9849 const_op = 0, op1 = const0_rtx;
9850 code = GE;
9851 break;
9852 }
9853 else
9854 break;
9855
9856 case LEU:
9857 /* unsigned <= 0 is equivalent to == 0 */
9858 if (const_op == 0)
9859 code = EQ;
9860
9861 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9862 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9863 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9864 {
9865 const_op = 0, op1 = const0_rtx;
9866 code = GE;
9867 }
9868 break;
9869
9870 case GEU:
9871 /* >= C is equivalent to > (C - 1). */
9872 if (const_op > 1)
9873 {
9874 const_op -= 1;
9875 op1 = GEN_INT (const_op);
9876 code = GTU;
9877 /* ... fall through ... */
9878 }
9879
9880 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9881 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9882 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9883 {
9884 const_op = 0, op1 = const0_rtx;
9885 code = LT;
9886 break;
9887 }
9888 else
9889 break;
9890
9891 case GTU:
9892 /* unsigned > 0 is equivalent to != 0 */
9893 if (const_op == 0)
9894 code = NE;
9895
9896 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9897 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9898 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9899 {
9900 const_op = 0, op1 = const0_rtx;
9901 code = LT;
9902 }
9903 break;
9904
9905 default:
9906 break;
9907 }
9908
9909 /* Compute some predicates to simplify code below. */
9910
9911 equality_comparison_p = (code == EQ || code == NE);
9912 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9913 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9914 || code == GEU);
9915
9916 /* If this is a sign bit comparison and we can do arithmetic in
9917 MODE, say that we will only be needing the sign bit of OP0. */
9918 if (sign_bit_comparison_p
9919 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9920 op0 = force_to_mode (op0, mode,
9921 ((HOST_WIDE_INT) 1
9922 << (GET_MODE_BITSIZE (mode) - 1)),
9923 NULL_RTX, 0);
9924
9925 /* Now try cases based on the opcode of OP0. If none of the cases
9926 does a "continue", we exit this loop immediately after the
9927 switch. */
9928
9929 switch (GET_CODE (op0))
9930 {
9931 case ZERO_EXTRACT:
9932 /* If we are extracting a single bit from a variable position in
9933 a constant that has only a single bit set and are comparing it
9934 with zero, we can convert this into an equality comparison
9935 between the position and the location of the single bit. */
9936 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9937 have already reduced the shift count modulo the word size. */
9938 if (!SHIFT_COUNT_TRUNCATED
9939 && GET_CODE (XEXP (op0, 0)) == CONST_INT
9940 && XEXP (op0, 1) == const1_rtx
9941 && equality_comparison_p && const_op == 0
9942 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9943 {
9944 if (BITS_BIG_ENDIAN)
9945 {
9946 enum machine_mode new_mode
9947 = mode_for_extraction (EP_extzv, 1);
9948 if (new_mode == MAX_MACHINE_MODE)
9949 i = BITS_PER_WORD - 1 - i;
9950 else
9951 {
9952 mode = new_mode;
9953 i = (GET_MODE_BITSIZE (mode) - 1 - i);
9954 }
9955 }
9956
9957 op0 = XEXP (op0, 2);
9958 op1 = GEN_INT (i);
9959 const_op = i;
9960
9961 /* Result is nonzero iff shift count is equal to I. */
9962 code = reverse_condition (code);
9963 continue;
9964 }
9965
9966 /* ... fall through ... */
9967
9968 case SIGN_EXTRACT:
9969 tem = expand_compound_operation (op0);
9970 if (tem != op0)
9971 {
9972 op0 = tem;
9973 continue;
9974 }
9975 break;
9976
9977 case NOT:
9978 /* If testing for equality, we can take the NOT of the constant. */
9979 if (equality_comparison_p
9980 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9981 {
9982 op0 = XEXP (op0, 0);
9983 op1 = tem;
9984 continue;
9985 }
9986
9987 /* If just looking at the sign bit, reverse the sense of the
9988 comparison. */
9989 if (sign_bit_comparison_p)
9990 {
9991 op0 = XEXP (op0, 0);
9992 code = (code == GE ? LT : GE);
9993 continue;
9994 }
9995 break;
9996
9997 case NEG:
9998 /* If testing for equality, we can take the NEG of the constant. */
9999 if (equality_comparison_p
10000 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10001 {
10002 op0 = XEXP (op0, 0);
10003 op1 = tem;
10004 continue;
10005 }
10006
10007 /* The remaining cases only apply to comparisons with zero. */
10008 if (const_op != 0)
10009 break;
10010
10011 /* When X is ABS or is known positive,
10012 (neg X) is < 0 if and only if X != 0. */
10013
10014 if (sign_bit_comparison_p
10015 && (GET_CODE (XEXP (op0, 0)) == ABS
10016 || (mode_width <= HOST_BITS_PER_WIDE_INT
10017 && (nonzero_bits (XEXP (op0, 0), mode)
10018 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10019 {
10020 op0 = XEXP (op0, 0);
10021 code = (code == LT ? NE : EQ);
10022 continue;
10023 }
10024
10025 /* If we have NEG of something whose two high-order bits are the
10026 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10027 if (num_sign_bit_copies (op0, mode) >= 2)
10028 {
10029 op0 = XEXP (op0, 0);
10030 code = swap_condition (code);
10031 continue;
10032 }
10033 break;
10034
10035 case ROTATE:
10036 /* If we are testing equality and our count is a constant, we
10037 can perform the inverse operation on our RHS. */
10038 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10039 && (tem = simplify_binary_operation (ROTATERT, mode,
10040 op1, XEXP (op0, 1))) != 0)
10041 {
10042 op0 = XEXP (op0, 0);
10043 op1 = tem;
10044 continue;
10045 }
10046
10047 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10048 a particular bit. Convert it to an AND of a constant of that
10049 bit. This will be converted into a ZERO_EXTRACT. */
10050 if (const_op == 0 && sign_bit_comparison_p
10051 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10052 && mode_width <= HOST_BITS_PER_WIDE_INT)
10053 {
10054 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10055 ((HOST_WIDE_INT) 1
10056 << (mode_width - 1
10057 - INTVAL (XEXP (op0, 1)))));
10058 code = (code == LT ? NE : EQ);
10059 continue;
10060 }
10061
10062 /* Fall through. */
10063
10064 case ABS:
10065 /* ABS is ignorable inside an equality comparison with zero. */
10066 if (const_op == 0 && equality_comparison_p)
10067 {
10068 op0 = XEXP (op0, 0);
10069 continue;
10070 }
10071 break;
10072
10073 case SIGN_EXTEND:
10074 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10075 (compare FOO CONST) if CONST fits in FOO's mode and we
10076 are either testing inequality or have an unsigned
10077 comparison with ZERO_EXTEND or a signed comparison with
10078 SIGN_EXTEND. But don't do it if we don't have a compare
10079 insn of the given mode, since we'd have to revert it
10080 later on, and then we wouldn't know whether to sign- or
10081 zero-extend. */
10082 mode = GET_MODE (XEXP (op0, 0));
10083 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10084 && ! unsigned_comparison_p
10085 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10086 && ((unsigned HOST_WIDE_INT) const_op
10087 < (((unsigned HOST_WIDE_INT) 1
10088 << (GET_MODE_BITSIZE (mode) - 1))))
10089 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10090 {
10091 op0 = XEXP (op0, 0);
10092 continue;
10093 }
10094 break;
10095
10096 case SUBREG:
10097 /* Check for the case where we are comparing A - C1 with C2, that is
10098
10099 (subreg:MODE (plus (A) (-C1))) op (C2)
10100
10101 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10102 comparison in the wider mode. One of the following two conditions
10103 must be true in order for this to be valid:
10104
10105 1. The mode extension results in the same bit pattern being added
10106 on both sides and the comparison is equality or unsigned. As
10107 C2 has been truncated to fit in MODE, the pattern can only be
10108 all 0s or all 1s.
10109
10110 2. The mode extension results in the sign bit being copied on
10111 each side.
10112
10113 The difficulty here is that we have predicates for A but not for
10114 (A - C1) so we need to check that C1 is within proper bounds so
10115 as to perturbate A as little as possible. */
10116
10117 if (mode_width <= HOST_BITS_PER_WIDE_INT
10118 && subreg_lowpart_p (op0)
10119 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10120 && GET_CODE (SUBREG_REG (op0)) == PLUS
10121 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10122 {
10123 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10124 rtx a = XEXP (SUBREG_REG (op0), 0);
10125 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10126
10127 if ((c1 > 0
10128 && (unsigned HOST_WIDE_INT) c1
10129 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10130 && (equality_comparison_p || unsigned_comparison_p)
10131 /* (A - C1) zero-extends if it is positive and sign-extends
10132 if it is negative, C2 both zero- and sign-extends. */
10133 && ((0 == (nonzero_bits (a, inner_mode)
10134 & ~GET_MODE_MASK (mode))
10135 && const_op >= 0)
10136 /* (A - C1) sign-extends if it is positive and 1-extends
10137 if it is negative, C2 both sign- and 1-extends. */
10138 || (num_sign_bit_copies (a, inner_mode)
10139 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10140 - mode_width)
10141 && const_op < 0)))
10142 || ((unsigned HOST_WIDE_INT) c1
10143 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10144 /* (A - C1) always sign-extends, like C2. */
10145 && num_sign_bit_copies (a, inner_mode)
10146 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10147 - mode_width - 1)))
10148 {
10149 op0 = SUBREG_REG (op0);
10150 continue;
10151 }
10152 }
10153
10154 /* If the inner mode is narrower and we are extracting the low part,
10155 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10156 if (subreg_lowpart_p (op0)
10157 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10158 /* Fall through */ ;
10159 else
10160 break;
10161
10162 /* ... fall through ... */
10163
10164 case ZERO_EXTEND:
10165 mode = GET_MODE (XEXP (op0, 0));
10166 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10167 && (unsigned_comparison_p || equality_comparison_p)
10168 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10169 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10170 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10171 {
10172 op0 = XEXP (op0, 0);
10173 continue;
10174 }
10175 break;
10176
10177 case PLUS:
10178 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10179 this for equality comparisons due to pathological cases involving
10180 overflows. */
10181 if (equality_comparison_p
10182 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10183 op1, XEXP (op0, 1))))
10184 {
10185 op0 = XEXP (op0, 0);
10186 op1 = tem;
10187 continue;
10188 }
10189
10190 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10191 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10192 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10193 {
10194 op0 = XEXP (XEXP (op0, 0), 0);
10195 code = (code == LT ? EQ : NE);
10196 continue;
10197 }
10198 break;
10199
10200 case MINUS:
10201 /* We used to optimize signed comparisons against zero, but that
10202 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10203 arrive here as equality comparisons, or (GEU, LTU) are
10204 optimized away. No need to special-case them. */
10205
10206 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10207 (eq B (minus A C)), whichever simplifies. We can only do
10208 this for equality comparisons due to pathological cases involving
10209 overflows. */
10210 if (equality_comparison_p
10211 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10212 XEXP (op0, 1), op1)))
10213 {
10214 op0 = XEXP (op0, 0);
10215 op1 = tem;
10216 continue;
10217 }
10218
10219 if (equality_comparison_p
10220 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10221 XEXP (op0, 0), op1)))
10222 {
10223 op0 = XEXP (op0, 1);
10224 op1 = tem;
10225 continue;
10226 }
10227
10228 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10229 of bits in X minus 1, is one iff X > 0. */
10230 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10231 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10232 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10233 == mode_width - 1
10234 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10235 {
10236 op0 = XEXP (op0, 1);
10237 code = (code == GE ? LE : GT);
10238 continue;
10239 }
10240 break;
10241
10242 case XOR:
10243 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10244 if C is zero or B is a constant. */
10245 if (equality_comparison_p
10246 && 0 != (tem = simplify_binary_operation (XOR, mode,
10247 XEXP (op0, 1), op1)))
10248 {
10249 op0 = XEXP (op0, 0);
10250 op1 = tem;
10251 continue;
10252 }
10253 break;
10254
10255 case EQ: case NE:
10256 case UNEQ: case LTGT:
10257 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10258 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10259 case UNORDERED: case ORDERED:
10260 /* We can't do anything if OP0 is a condition code value, rather
10261 than an actual data value. */
10262 if (const_op != 0
10263 || CC0_P (XEXP (op0, 0))
10264 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10265 break;
10266
10267 /* Get the two operands being compared. */
10268 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10269 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10270 else
10271 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10272
10273 /* Check for the cases where we simply want the result of the
10274 earlier test or the opposite of that result. */
10275 if (code == NE || code == EQ
10276 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10277 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10278 && (STORE_FLAG_VALUE
10279 & (((HOST_WIDE_INT) 1
10280 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10281 && (code == LT || code == GE)))
10282 {
10283 enum rtx_code new_code;
10284 if (code == LT || code == NE)
10285 new_code = GET_CODE (op0);
10286 else
10287 new_code = combine_reversed_comparison_code (op0);
10288
10289 if (new_code != UNKNOWN)
10290 {
10291 code = new_code;
10292 op0 = tem;
10293 op1 = tem1;
10294 continue;
10295 }
10296 }
10297 break;
10298
10299 case IOR:
10300 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10301 iff X <= 0. */
10302 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10303 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10304 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10305 {
10306 op0 = XEXP (op0, 1);
10307 code = (code == GE ? GT : LE);
10308 continue;
10309 }
10310 break;
10311
10312 case AND:
10313 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10314 will be converted to a ZERO_EXTRACT later. */
10315 if (const_op == 0 && equality_comparison_p
10316 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10317 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10318 {
10319 op0 = simplify_and_const_int
10320 (op0, mode, gen_rtx_LSHIFTRT (mode,
10321 XEXP (op0, 1),
10322 XEXP (XEXP (op0, 0), 1)),
10323 (HOST_WIDE_INT) 1);
10324 continue;
10325 }
10326
10327 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10328 zero and X is a comparison and C1 and C2 describe only bits set
10329 in STORE_FLAG_VALUE, we can compare with X. */
10330 if (const_op == 0 && equality_comparison_p
10331 && mode_width <= HOST_BITS_PER_WIDE_INT
10332 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10333 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10334 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10335 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10336 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10337 {
10338 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10339 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10340 if ((~STORE_FLAG_VALUE & mask) == 0
10341 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10342 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10343 && COMPARISON_P (tem))))
10344 {
10345 op0 = XEXP (XEXP (op0, 0), 0);
10346 continue;
10347 }
10348 }
10349
10350 /* If we are doing an equality comparison of an AND of a bit equal
10351 to the sign bit, replace this with a LT or GE comparison of
10352 the underlying value. */
10353 if (equality_comparison_p
10354 && const_op == 0
10355 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10356 && mode_width <= HOST_BITS_PER_WIDE_INT
10357 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10358 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10359 {
10360 op0 = XEXP (op0, 0);
10361 code = (code == EQ ? GE : LT);
10362 continue;
10363 }
10364
10365 /* If this AND operation is really a ZERO_EXTEND from a narrower
10366 mode, the constant fits within that mode, and this is either an
10367 equality or unsigned comparison, try to do this comparison in
10368 the narrower mode. */
10369 if ((equality_comparison_p || unsigned_comparison_p)
10370 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10371 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10372 & GET_MODE_MASK (mode))
10373 + 1)) >= 0
10374 && const_op >> i == 0
10375 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10376 {
10377 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10378 continue;
10379 }
10380
10381 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10382 fits in both M1 and M2 and the SUBREG is either paradoxical
10383 or represents the low part, permute the SUBREG and the AND
10384 and try again. */
10385 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10386 {
10387 unsigned HOST_WIDE_INT c1;
10388 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10389 /* Require an integral mode, to avoid creating something like
10390 (AND:SF ...). */
10391 if (SCALAR_INT_MODE_P (tmode)
10392 /* It is unsafe to commute the AND into the SUBREG if the
10393 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10394 not defined. As originally written the upper bits
10395 have a defined value due to the AND operation.
10396 However, if we commute the AND inside the SUBREG then
10397 they no longer have defined values and the meaning of
10398 the code has been changed. */
10399 && (0
10400 #ifdef WORD_REGISTER_OPERATIONS
10401 || (mode_width > GET_MODE_BITSIZE (tmode)
10402 && mode_width <= BITS_PER_WORD)
10403 #endif
10404 || (mode_width <= GET_MODE_BITSIZE (tmode)
10405 && subreg_lowpart_p (XEXP (op0, 0))))
10406 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10407 && mode_width <= HOST_BITS_PER_WIDE_INT
10408 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10409 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10410 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10411 && c1 != mask
10412 && c1 != GET_MODE_MASK (tmode))
10413 {
10414 op0 = gen_binary (AND, tmode,
10415 SUBREG_REG (XEXP (op0, 0)),
10416 gen_int_mode (c1, tmode));
10417 op0 = gen_lowpart (mode, op0);
10418 continue;
10419 }
10420 }
10421
10422 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10423 if (const_op == 0 && equality_comparison_p
10424 && XEXP (op0, 1) == const1_rtx
10425 && GET_CODE (XEXP (op0, 0)) == NOT)
10426 {
10427 op0 = simplify_and_const_int
10428 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10429 code = (code == NE ? EQ : NE);
10430 continue;
10431 }
10432
10433 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10434 (eq (and (lshiftrt X) 1) 0).
10435 Also handle the case where (not X) is expressed using xor. */
10436 if (const_op == 0 && equality_comparison_p
10437 && XEXP (op0, 1) == const1_rtx
10438 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10439 {
10440 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10441 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10442
10443 if (GET_CODE (shift_op) == NOT
10444 || (GET_CODE (shift_op) == XOR
10445 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10446 && GET_CODE (shift_count) == CONST_INT
10447 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10448 && (INTVAL (XEXP (shift_op, 1))
10449 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10450 {
10451 op0 = simplify_and_const_int
10452 (NULL_RTX, mode,
10453 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10454 (HOST_WIDE_INT) 1);
10455 code = (code == NE ? EQ : NE);
10456 continue;
10457 }
10458 }
10459 break;
10460
10461 case ASHIFT:
10462 /* If we have (compare (ashift FOO N) (const_int C)) and
10463 the high order N bits of FOO (N+1 if an inequality comparison)
10464 are known to be zero, we can do this by comparing FOO with C
10465 shifted right N bits so long as the low-order N bits of C are
10466 zero. */
10467 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10468 && INTVAL (XEXP (op0, 1)) >= 0
10469 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10470 < HOST_BITS_PER_WIDE_INT)
10471 && ((const_op
10472 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10473 && mode_width <= HOST_BITS_PER_WIDE_INT
10474 && (nonzero_bits (XEXP (op0, 0), mode)
10475 & ~(mask >> (INTVAL (XEXP (op0, 1))
10476 + ! equality_comparison_p))) == 0)
10477 {
10478 /* We must perform a logical shift, not an arithmetic one,
10479 as we want the top N bits of C to be zero. */
10480 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10481
10482 temp >>= INTVAL (XEXP (op0, 1));
10483 op1 = gen_int_mode (temp, mode);
10484 op0 = XEXP (op0, 0);
10485 continue;
10486 }
10487
10488 /* If we are doing a sign bit comparison, it means we are testing
10489 a particular bit. Convert it to the appropriate AND. */
10490 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10491 && mode_width <= HOST_BITS_PER_WIDE_INT)
10492 {
10493 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10494 ((HOST_WIDE_INT) 1
10495 << (mode_width - 1
10496 - INTVAL (XEXP (op0, 1)))));
10497 code = (code == LT ? NE : EQ);
10498 continue;
10499 }
10500
10501 /* If this an equality comparison with zero and we are shifting
10502 the low bit to the sign bit, we can convert this to an AND of the
10503 low-order bit. */
10504 if (const_op == 0 && equality_comparison_p
10505 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10506 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10507 == mode_width - 1)
10508 {
10509 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10510 (HOST_WIDE_INT) 1);
10511 continue;
10512 }
10513 break;
10514
10515 case ASHIFTRT:
10516 /* If this is an equality comparison with zero, we can do this
10517 as a logical shift, which might be much simpler. */
10518 if (equality_comparison_p && const_op == 0
10519 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10520 {
10521 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10522 XEXP (op0, 0),
10523 INTVAL (XEXP (op0, 1)));
10524 continue;
10525 }
10526
10527 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10528 do the comparison in a narrower mode. */
10529 if (! unsigned_comparison_p
10530 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10531 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10532 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10533 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10534 MODE_INT, 1)) != BLKmode
10535 && (((unsigned HOST_WIDE_INT) const_op
10536 + (GET_MODE_MASK (tmode) >> 1) + 1)
10537 <= GET_MODE_MASK (tmode)))
10538 {
10539 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10540 continue;
10541 }
10542
10543 /* Likewise if OP0 is a PLUS of a sign extension with a
10544 constant, which is usually represented with the PLUS
10545 between the shifts. */
10546 if (! unsigned_comparison_p
10547 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10548 && GET_CODE (XEXP (op0, 0)) == PLUS
10549 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10550 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10551 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10552 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10553 MODE_INT, 1)) != BLKmode
10554 && (((unsigned HOST_WIDE_INT) const_op
10555 + (GET_MODE_MASK (tmode) >> 1) + 1)
10556 <= GET_MODE_MASK (tmode)))
10557 {
10558 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10559 rtx add_const = XEXP (XEXP (op0, 0), 1);
10560 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
10561 XEXP (op0, 1));
10562
10563 op0 = gen_binary (PLUS, tmode,
10564 gen_lowpart (tmode, inner),
10565 new_const);
10566 continue;
10567 }
10568
10569 /* ... fall through ... */
10570 case LSHIFTRT:
10571 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10572 the low order N bits of FOO are known to be zero, we can do this
10573 by comparing FOO with C shifted left N bits so long as no
10574 overflow occurs. */
10575 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10576 && INTVAL (XEXP (op0, 1)) >= 0
10577 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10578 && mode_width <= HOST_BITS_PER_WIDE_INT
10579 && (nonzero_bits (XEXP (op0, 0), mode)
10580 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10581 && (((unsigned HOST_WIDE_INT) const_op
10582 + (GET_CODE (op0) != LSHIFTRT
10583 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10584 + 1)
10585 : 0))
10586 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10587 {
10588 /* If the shift was logical, then we must make the condition
10589 unsigned. */
10590 if (GET_CODE (op0) == LSHIFTRT)
10591 code = unsigned_condition (code);
10592
10593 const_op <<= INTVAL (XEXP (op0, 1));
10594 op1 = GEN_INT (const_op);
10595 op0 = XEXP (op0, 0);
10596 continue;
10597 }
10598
10599 /* If we are using this shift to extract just the sign bit, we
10600 can replace this with an LT or GE comparison. */
10601 if (const_op == 0
10602 && (equality_comparison_p || sign_bit_comparison_p)
10603 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10604 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10605 == mode_width - 1)
10606 {
10607 op0 = XEXP (op0, 0);
10608 code = (code == NE || code == GT ? LT : GE);
10609 continue;
10610 }
10611 break;
10612
10613 default:
10614 break;
10615 }
10616
10617 break;
10618 }
10619
10620 /* Now make any compound operations involved in this comparison. Then,
10621 check for an outmost SUBREG on OP0 that is not doing anything or is
10622 paradoxical. The latter transformation must only be performed when
10623 it is known that the "extra" bits will be the same in op0 and op1 or
10624 that they don't matter. There are three cases to consider:
10625
10626 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10627 care bits and we can assume they have any convenient value. So
10628 making the transformation is safe.
10629
10630 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10631 In this case the upper bits of op0 are undefined. We should not make
10632 the simplification in that case as we do not know the contents of
10633 those bits.
10634
10635 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10636 UNKNOWN. In that case we know those bits are zeros or ones. We must
10637 also be sure that they are the same as the upper bits of op1.
10638
10639 We can never remove a SUBREG for a non-equality comparison because
10640 the sign bit is in a different place in the underlying object. */
10641
10642 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10643 op1 = make_compound_operation (op1, SET);
10644
10645 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10646 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10647 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10648 && (code == NE || code == EQ))
10649 {
10650 if (GET_MODE_SIZE (GET_MODE (op0))
10651 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10652 {
10653 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10654 implemented. */
10655 if (REG_P (SUBREG_REG (op0)))
10656 {
10657 op0 = SUBREG_REG (op0);
10658 op1 = gen_lowpart (GET_MODE (op0), op1);
10659 }
10660 }
10661 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10662 <= HOST_BITS_PER_WIDE_INT)
10663 && (nonzero_bits (SUBREG_REG (op0),
10664 GET_MODE (SUBREG_REG (op0)))
10665 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10666 {
10667 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10668
10669 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10670 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10671 op0 = SUBREG_REG (op0), op1 = tem;
10672 }
10673 }
10674
10675 /* We now do the opposite procedure: Some machines don't have compare
10676 insns in all modes. If OP0's mode is an integer mode smaller than a
10677 word and we can't do a compare in that mode, see if there is a larger
10678 mode for which we can do the compare. There are a number of cases in
10679 which we can use the wider mode. */
10680
10681 mode = GET_MODE (op0);
10682 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10683 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10684 && ! have_insn_for (COMPARE, mode))
10685 for (tmode = GET_MODE_WIDER_MODE (mode);
10686 (tmode != VOIDmode
10687 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10688 tmode = GET_MODE_WIDER_MODE (tmode))
10689 if (have_insn_for (COMPARE, tmode))
10690 {
10691 int zero_extended;
10692
10693 /* If the only nonzero bits in OP0 and OP1 are those in the
10694 narrower mode and this is an equality or unsigned comparison,
10695 we can use the wider mode. Similarly for sign-extended
10696 values, in which case it is true for all comparisons. */
10697 zero_extended = ((code == EQ || code == NE
10698 || code == GEU || code == GTU
10699 || code == LEU || code == LTU)
10700 && (nonzero_bits (op0, tmode)
10701 & ~GET_MODE_MASK (mode)) == 0
10702 && ((GET_CODE (op1) == CONST_INT
10703 || (nonzero_bits (op1, tmode)
10704 & ~GET_MODE_MASK (mode)) == 0)));
10705
10706 if (zero_extended
10707 || ((num_sign_bit_copies (op0, tmode)
10708 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10709 - GET_MODE_BITSIZE (mode)))
10710 && (num_sign_bit_copies (op1, tmode)
10711 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10712 - GET_MODE_BITSIZE (mode)))))
10713 {
10714 /* If OP0 is an AND and we don't have an AND in MODE either,
10715 make a new AND in the proper mode. */
10716 if (GET_CODE (op0) == AND
10717 && !have_insn_for (AND, mode))
10718 op0 = gen_binary (AND, tmode,
10719 gen_lowpart (tmode,
10720 XEXP (op0, 0)),
10721 gen_lowpart (tmode,
10722 XEXP (op0, 1)));
10723
10724 op0 = gen_lowpart (tmode, op0);
10725 if (zero_extended && GET_CODE (op1) == CONST_INT)
10726 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10727 op1 = gen_lowpart (tmode, op1);
10728 break;
10729 }
10730
10731 /* If this is a test for negative, we can make an explicit
10732 test of the sign bit. */
10733
10734 if (op1 == const0_rtx && (code == LT || code == GE)
10735 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10736 {
10737 op0 = gen_binary (AND, tmode,
10738 gen_lowpart (tmode, op0),
10739 GEN_INT ((HOST_WIDE_INT) 1
10740 << (GET_MODE_BITSIZE (mode) - 1)));
10741 code = (code == LT) ? NE : EQ;
10742 break;
10743 }
10744 }
10745
10746 #ifdef CANONICALIZE_COMPARISON
10747 /* If this machine only supports a subset of valid comparisons, see if we
10748 can convert an unsupported one into a supported one. */
10749 CANONICALIZE_COMPARISON (code, op0, op1);
10750 #endif
10751
10752 *pop0 = op0;
10753 *pop1 = op1;
10754
10755 return code;
10756 }
10757 \f
10758 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
10759 searching backward. */
10760 static enum rtx_code
10761 combine_reversed_comparison_code (rtx exp)
10762 {
10763 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
10764 rtx x;
10765
10766 if (code1 != UNKNOWN
10767 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
10768 return code1;
10769 /* Otherwise try and find where the condition codes were last set and
10770 use that. */
10771 x = get_last_value (XEXP (exp, 0));
10772 if (!x || GET_CODE (x) != COMPARE)
10773 return UNKNOWN;
10774 return reversed_comparison_code_parts (GET_CODE (exp),
10775 XEXP (x, 0), XEXP (x, 1), NULL);
10776 }
10777
10778 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
10779 Return NULL_RTX in case we fail to do the reversal. */
10780 static rtx
10781 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
10782 {
10783 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
10784 if (reversed_code == UNKNOWN)
10785 return NULL_RTX;
10786 else
10787 return gen_binary (reversed_code, mode, op0, op1);
10788 }
10789 \f
10790 /* Utility function for record_value_for_reg. Count number of
10791 rtxs in X. */
10792 static int
10793 count_rtxs (rtx x)
10794 {
10795 enum rtx_code code = GET_CODE (x);
10796 const char *fmt;
10797 int i, ret = 1;
10798
10799 if (GET_RTX_CLASS (code) == '2'
10800 || GET_RTX_CLASS (code) == 'c')
10801 {
10802 rtx x0 = XEXP (x, 0);
10803 rtx x1 = XEXP (x, 1);
10804
10805 if (x0 == x1)
10806 return 1 + 2 * count_rtxs (x0);
10807
10808 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10809 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10810 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10811 return 2 + 2 * count_rtxs (x0)
10812 + count_rtxs (x == XEXP (x1, 0)
10813 ? XEXP (x1, 1) : XEXP (x1, 0));
10814
10815 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10816 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10817 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10818 return 2 + 2 * count_rtxs (x1)
10819 + count_rtxs (x == XEXP (x0, 0)
10820 ? XEXP (x0, 1) : XEXP (x0, 0));
10821 }
10822
10823 fmt = GET_RTX_FORMAT (code);
10824 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10825 if (fmt[i] == 'e')
10826 ret += count_rtxs (XEXP (x, i));
10827
10828 return ret;
10829 }
10830 \f
10831 /* Utility function for following routine. Called when X is part of a value
10832 being stored into last_set_value. Sets last_set_table_tick
10833 for each register mentioned. Similar to mention_regs in cse.c */
10834
10835 static void
10836 update_table_tick (rtx x)
10837 {
10838 enum rtx_code code = GET_CODE (x);
10839 const char *fmt = GET_RTX_FORMAT (code);
10840 int i;
10841
10842 if (code == REG)
10843 {
10844 unsigned int regno = REGNO (x);
10845 unsigned int endregno
10846 = regno + (regno < FIRST_PSEUDO_REGISTER
10847 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10848 unsigned int r;
10849
10850 for (r = regno; r < endregno; r++)
10851 reg_stat[r].last_set_table_tick = label_tick;
10852
10853 return;
10854 }
10855
10856 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10857 /* Note that we can't have an "E" in values stored; see
10858 get_last_value_validate. */
10859 if (fmt[i] == 'e')
10860 {
10861 /* Check for identical subexpressions. If x contains
10862 identical subexpression we only have to traverse one of
10863 them. */
10864 if (i == 0 && ARITHMETIC_P (x))
10865 {
10866 /* Note that at this point x1 has already been
10867 processed. */
10868 rtx x0 = XEXP (x, 0);
10869 rtx x1 = XEXP (x, 1);
10870
10871 /* If x0 and x1 are identical then there is no need to
10872 process x0. */
10873 if (x0 == x1)
10874 break;
10875
10876 /* If x0 is identical to a subexpression of x1 then while
10877 processing x1, x0 has already been processed. Thus we
10878 are done with x. */
10879 if (ARITHMETIC_P (x1)
10880 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10881 break;
10882
10883 /* If x1 is identical to a subexpression of x0 then we
10884 still have to process the rest of x0. */
10885 if (ARITHMETIC_P (x0)
10886 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10887 {
10888 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10889 break;
10890 }
10891 }
10892
10893 update_table_tick (XEXP (x, i));
10894 }
10895 }
10896
10897 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10898 are saying that the register is clobbered and we no longer know its
10899 value. If INSN is zero, don't update reg_stat[].last_set; this is
10900 only permitted with VALUE also zero and is used to invalidate the
10901 register. */
10902
10903 static void
10904 record_value_for_reg (rtx reg, rtx insn, rtx value)
10905 {
10906 unsigned int regno = REGNO (reg);
10907 unsigned int endregno
10908 = regno + (regno < FIRST_PSEUDO_REGISTER
10909 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10910 unsigned int i;
10911
10912 /* If VALUE contains REG and we have a previous value for REG, substitute
10913 the previous value. */
10914 if (value && insn && reg_overlap_mentioned_p (reg, value))
10915 {
10916 rtx tem;
10917
10918 /* Set things up so get_last_value is allowed to see anything set up to
10919 our insn. */
10920 subst_low_cuid = INSN_CUID (insn);
10921 tem = get_last_value (reg);
10922
10923 /* If TEM is simply a binary operation with two CLOBBERs as operands,
10924 it isn't going to be useful and will take a lot of time to process,
10925 so just use the CLOBBER. */
10926
10927 if (tem)
10928 {
10929 if (ARITHMETIC_P (tem)
10930 && GET_CODE (XEXP (tem, 0)) == CLOBBER
10931 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10932 tem = XEXP (tem, 0);
10933 else if (count_occurrences (value, reg, 1) >= 2)
10934 {
10935 /* If there are two or more occurrences of REG in VALUE,
10936 prevent the value from growing too much. */
10937 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
10938 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
10939 }
10940
10941 value = replace_rtx (copy_rtx (value), reg, tem);
10942 }
10943 }
10944
10945 /* For each register modified, show we don't know its value, that
10946 we don't know about its bitwise content, that its value has been
10947 updated, and that we don't know the location of the death of the
10948 register. */
10949 for (i = regno; i < endregno; i++)
10950 {
10951 if (insn)
10952 reg_stat[i].last_set = insn;
10953
10954 reg_stat[i].last_set_value = 0;
10955 reg_stat[i].last_set_mode = 0;
10956 reg_stat[i].last_set_nonzero_bits = 0;
10957 reg_stat[i].last_set_sign_bit_copies = 0;
10958 reg_stat[i].last_death = 0;
10959 }
10960
10961 /* Mark registers that are being referenced in this value. */
10962 if (value)
10963 update_table_tick (value);
10964
10965 /* Now update the status of each register being set.
10966 If someone is using this register in this block, set this register
10967 to invalid since we will get confused between the two lives in this
10968 basic block. This makes using this register always invalid. In cse, we
10969 scan the table to invalidate all entries using this register, but this
10970 is too much work for us. */
10971
10972 for (i = regno; i < endregno; i++)
10973 {
10974 reg_stat[i].last_set_label = label_tick;
10975 if (value && reg_stat[i].last_set_table_tick == label_tick)
10976 reg_stat[i].last_set_invalid = 1;
10977 else
10978 reg_stat[i].last_set_invalid = 0;
10979 }
10980
10981 /* The value being assigned might refer to X (like in "x++;"). In that
10982 case, we must replace it with (clobber (const_int 0)) to prevent
10983 infinite loops. */
10984 if (value && ! get_last_value_validate (&value, insn,
10985 reg_stat[regno].last_set_label, 0))
10986 {
10987 value = copy_rtx (value);
10988 if (! get_last_value_validate (&value, insn,
10989 reg_stat[regno].last_set_label, 1))
10990 value = 0;
10991 }
10992
10993 /* For the main register being modified, update the value, the mode, the
10994 nonzero bits, and the number of sign bit copies. */
10995
10996 reg_stat[regno].last_set_value = value;
10997
10998 if (value)
10999 {
11000 enum machine_mode mode = GET_MODE (reg);
11001 subst_low_cuid = INSN_CUID (insn);
11002 reg_stat[regno].last_set_mode = mode;
11003 if (GET_MODE_CLASS (mode) == MODE_INT
11004 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11005 mode = nonzero_bits_mode;
11006 reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
11007 reg_stat[regno].last_set_sign_bit_copies
11008 = num_sign_bit_copies (value, GET_MODE (reg));
11009 }
11010 }
11011
11012 /* Called via note_stores from record_dead_and_set_regs to handle one
11013 SET or CLOBBER in an insn. DATA is the instruction in which the
11014 set is occurring. */
11015
11016 static void
11017 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11018 {
11019 rtx record_dead_insn = (rtx) data;
11020
11021 if (GET_CODE (dest) == SUBREG)
11022 dest = SUBREG_REG (dest);
11023
11024 if (REG_P (dest))
11025 {
11026 /* If we are setting the whole register, we know its value. Otherwise
11027 show that we don't know the value. We can handle SUBREG in
11028 some cases. */
11029 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11030 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11031 else if (GET_CODE (setter) == SET
11032 && GET_CODE (SET_DEST (setter)) == SUBREG
11033 && SUBREG_REG (SET_DEST (setter)) == dest
11034 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11035 && subreg_lowpart_p (SET_DEST (setter)))
11036 record_value_for_reg (dest, record_dead_insn,
11037 gen_lowpart (GET_MODE (dest),
11038 SET_SRC (setter)));
11039 else
11040 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11041 }
11042 else if (MEM_P (dest)
11043 /* Ignore pushes, they clobber nothing. */
11044 && ! push_operand (dest, GET_MODE (dest)))
11045 mem_last_set = INSN_CUID (record_dead_insn);
11046 }
11047
11048 /* Update the records of when each REG was most recently set or killed
11049 for the things done by INSN. This is the last thing done in processing
11050 INSN in the combiner loop.
11051
11052 We update reg_stat[], in particular fields last_set, last_set_value,
11053 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11054 last_death, and also the similar information mem_last_set (which insn
11055 most recently modified memory) and last_call_cuid (which insn was the
11056 most recent subroutine call). */
11057
11058 static void
11059 record_dead_and_set_regs (rtx insn)
11060 {
11061 rtx link;
11062 unsigned int i;
11063
11064 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11065 {
11066 if (REG_NOTE_KIND (link) == REG_DEAD
11067 && REG_P (XEXP (link, 0)))
11068 {
11069 unsigned int regno = REGNO (XEXP (link, 0));
11070 unsigned int endregno
11071 = regno + (regno < FIRST_PSEUDO_REGISTER
11072 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11073 : 1);
11074
11075 for (i = regno; i < endregno; i++)
11076 reg_stat[i].last_death = insn;
11077 }
11078 else if (REG_NOTE_KIND (link) == REG_INC)
11079 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11080 }
11081
11082 if (CALL_P (insn))
11083 {
11084 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11085 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11086 {
11087 reg_stat[i].last_set_value = 0;
11088 reg_stat[i].last_set_mode = 0;
11089 reg_stat[i].last_set_nonzero_bits = 0;
11090 reg_stat[i].last_set_sign_bit_copies = 0;
11091 reg_stat[i].last_death = 0;
11092 }
11093
11094 last_call_cuid = mem_last_set = INSN_CUID (insn);
11095
11096 /* Don't bother recording what this insn does. It might set the
11097 return value register, but we can't combine into a call
11098 pattern anyway, so there's no point trying (and it may cause
11099 a crash, if e.g. we wind up asking for last_set_value of a
11100 SUBREG of the return value register). */
11101 return;
11102 }
11103
11104 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11105 }
11106
11107 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11108 register present in the SUBREG, so for each such SUBREG go back and
11109 adjust nonzero and sign bit information of the registers that are
11110 known to have some zero/sign bits set.
11111
11112 This is needed because when combine blows the SUBREGs away, the
11113 information on zero/sign bits is lost and further combines can be
11114 missed because of that. */
11115
11116 static void
11117 record_promoted_value (rtx insn, rtx subreg)
11118 {
11119 rtx links, set;
11120 unsigned int regno = REGNO (SUBREG_REG (subreg));
11121 enum machine_mode mode = GET_MODE (subreg);
11122
11123 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11124 return;
11125
11126 for (links = LOG_LINKS (insn); links;)
11127 {
11128 insn = XEXP (links, 0);
11129 set = single_set (insn);
11130
11131 if (! set || !REG_P (SET_DEST (set))
11132 || REGNO (SET_DEST (set)) != regno
11133 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11134 {
11135 links = XEXP (links, 1);
11136 continue;
11137 }
11138
11139 if (reg_stat[regno].last_set == insn)
11140 {
11141 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11142 reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11143 }
11144
11145 if (REG_P (SET_SRC (set)))
11146 {
11147 regno = REGNO (SET_SRC (set));
11148 links = LOG_LINKS (insn);
11149 }
11150 else
11151 break;
11152 }
11153 }
11154
11155 /* Scan X for promoted SUBREGs. For each one found,
11156 note what it implies to the registers used in it. */
11157
11158 static void
11159 check_promoted_subreg (rtx insn, rtx x)
11160 {
11161 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11162 && REG_P (SUBREG_REG (x)))
11163 record_promoted_value (insn, x);
11164 else
11165 {
11166 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11167 int i, j;
11168
11169 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11170 switch (format[i])
11171 {
11172 case 'e':
11173 check_promoted_subreg (insn, XEXP (x, i));
11174 break;
11175 case 'V':
11176 case 'E':
11177 if (XVEC (x, i) != 0)
11178 for (j = 0; j < XVECLEN (x, i); j++)
11179 check_promoted_subreg (insn, XVECEXP (x, i, j));
11180 break;
11181 }
11182 }
11183 }
11184 \f
11185 /* Utility routine for the following function. Verify that all the registers
11186 mentioned in *LOC are valid when *LOC was part of a value set when
11187 label_tick == TICK. Return 0 if some are not.
11188
11189 If REPLACE is nonzero, replace the invalid reference with
11190 (clobber (const_int 0)) and return 1. This replacement is useful because
11191 we often can get useful information about the form of a value (e.g., if
11192 it was produced by a shift that always produces -1 or 0) even though
11193 we don't know exactly what registers it was produced from. */
11194
11195 static int
11196 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11197 {
11198 rtx x = *loc;
11199 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11200 int len = GET_RTX_LENGTH (GET_CODE (x));
11201 int i;
11202
11203 if (REG_P (x))
11204 {
11205 unsigned int regno = REGNO (x);
11206 unsigned int endregno
11207 = regno + (regno < FIRST_PSEUDO_REGISTER
11208 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11209 unsigned int j;
11210
11211 for (j = regno; j < endregno; j++)
11212 if (reg_stat[j].last_set_invalid
11213 /* If this is a pseudo-register that was only set once and not
11214 live at the beginning of the function, it is always valid. */
11215 || (! (regno >= FIRST_PSEUDO_REGISTER
11216 && REG_N_SETS (regno) == 1
11217 && (! REGNO_REG_SET_P
11218 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11219 && reg_stat[j].last_set_label > tick))
11220 {
11221 if (replace)
11222 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11223 return replace;
11224 }
11225
11226 return 1;
11227 }
11228 /* If this is a memory reference, make sure that there were
11229 no stores after it that might have clobbered the value. We don't
11230 have alias info, so we assume any store invalidates it. */
11231 else if (MEM_P (x) && !MEM_READONLY_P (x)
11232 && INSN_CUID (insn) <= mem_last_set)
11233 {
11234 if (replace)
11235 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11236 return replace;
11237 }
11238
11239 for (i = 0; i < len; i++)
11240 {
11241 if (fmt[i] == 'e')
11242 {
11243 /* Check for identical subexpressions. If x contains
11244 identical subexpression we only have to traverse one of
11245 them. */
11246 if (i == 1 && ARITHMETIC_P (x))
11247 {
11248 /* Note that at this point x0 has already been checked
11249 and found valid. */
11250 rtx x0 = XEXP (x, 0);
11251 rtx x1 = XEXP (x, 1);
11252
11253 /* If x0 and x1 are identical then x is also valid. */
11254 if (x0 == x1)
11255 return 1;
11256
11257 /* If x1 is identical to a subexpression of x0 then
11258 while checking x0, x1 has already been checked. Thus
11259 it is valid and so as x. */
11260 if (ARITHMETIC_P (x0)
11261 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11262 return 1;
11263
11264 /* If x0 is identical to a subexpression of x1 then x is
11265 valid iff the rest of x1 is valid. */
11266 if (ARITHMETIC_P (x1)
11267 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11268 return
11269 get_last_value_validate (&XEXP (x1,
11270 x0 == XEXP (x1, 0) ? 1 : 0),
11271 insn, tick, replace);
11272 }
11273
11274 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11275 replace) == 0)
11276 return 0;
11277 }
11278 /* Don't bother with these. They shouldn't occur anyway. */
11279 else if (fmt[i] == 'E')
11280 return 0;
11281 }
11282
11283 /* If we haven't found a reason for it to be invalid, it is valid. */
11284 return 1;
11285 }
11286
11287 /* Get the last value assigned to X, if known. Some registers
11288 in the value may be replaced with (clobber (const_int 0)) if their value
11289 is known longer known reliably. */
11290
11291 static rtx
11292 get_last_value (rtx x)
11293 {
11294 unsigned int regno;
11295 rtx value;
11296
11297 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11298 then convert it to the desired mode. If this is a paradoxical SUBREG,
11299 we cannot predict what values the "extra" bits might have. */
11300 if (GET_CODE (x) == SUBREG
11301 && subreg_lowpart_p (x)
11302 && (GET_MODE_SIZE (GET_MODE (x))
11303 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11304 && (value = get_last_value (SUBREG_REG (x))) != 0)
11305 return gen_lowpart (GET_MODE (x), value);
11306
11307 if (!REG_P (x))
11308 return 0;
11309
11310 regno = REGNO (x);
11311 value = reg_stat[regno].last_set_value;
11312
11313 /* If we don't have a value, or if it isn't for this basic block and
11314 it's either a hard register, set more than once, or it's a live
11315 at the beginning of the function, return 0.
11316
11317 Because if it's not live at the beginning of the function then the reg
11318 is always set before being used (is never used without being set).
11319 And, if it's set only once, and it's always set before use, then all
11320 uses must have the same last value, even if it's not from this basic
11321 block. */
11322
11323 if (value == 0
11324 || (reg_stat[regno].last_set_label != label_tick
11325 && (regno < FIRST_PSEUDO_REGISTER
11326 || REG_N_SETS (regno) != 1
11327 || (REGNO_REG_SET_P
11328 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11329 return 0;
11330
11331 /* If the value was set in a later insn than the ones we are processing,
11332 we can't use it even if the register was only set once. */
11333 if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11334 return 0;
11335
11336 /* If the value has all its registers valid, return it. */
11337 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11338 reg_stat[regno].last_set_label, 0))
11339 return value;
11340
11341 /* Otherwise, make a copy and replace any invalid register with
11342 (clobber (const_int 0)). If that fails for some reason, return 0. */
11343
11344 value = copy_rtx (value);
11345 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11346 reg_stat[regno].last_set_label, 1))
11347 return value;
11348
11349 return 0;
11350 }
11351 \f
11352 /* Return nonzero if expression X refers to a REG or to memory
11353 that is set in an instruction more recent than FROM_CUID. */
11354
11355 static int
11356 use_crosses_set_p (rtx x, int from_cuid)
11357 {
11358 const char *fmt;
11359 int i;
11360 enum rtx_code code = GET_CODE (x);
11361
11362 if (code == REG)
11363 {
11364 unsigned int regno = REGNO (x);
11365 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11366 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11367
11368 #ifdef PUSH_ROUNDING
11369 /* Don't allow uses of the stack pointer to be moved,
11370 because we don't know whether the move crosses a push insn. */
11371 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11372 return 1;
11373 #endif
11374 for (; regno < endreg; regno++)
11375 if (reg_stat[regno].last_set
11376 && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11377 return 1;
11378 return 0;
11379 }
11380
11381 if (code == MEM && mem_last_set > from_cuid)
11382 return 1;
11383
11384 fmt = GET_RTX_FORMAT (code);
11385
11386 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11387 {
11388 if (fmt[i] == 'E')
11389 {
11390 int j;
11391 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11392 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11393 return 1;
11394 }
11395 else if (fmt[i] == 'e'
11396 && use_crosses_set_p (XEXP (x, i), from_cuid))
11397 return 1;
11398 }
11399 return 0;
11400 }
11401 \f
11402 /* Define three variables used for communication between the following
11403 routines. */
11404
11405 static unsigned int reg_dead_regno, reg_dead_endregno;
11406 static int reg_dead_flag;
11407
11408 /* Function called via note_stores from reg_dead_at_p.
11409
11410 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11411 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11412
11413 static void
11414 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11415 {
11416 unsigned int regno, endregno;
11417
11418 if (!REG_P (dest))
11419 return;
11420
11421 regno = REGNO (dest);
11422 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11423 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11424
11425 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11426 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11427 }
11428
11429 /* Return nonzero if REG is known to be dead at INSN.
11430
11431 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11432 referencing REG, it is dead. If we hit a SET referencing REG, it is
11433 live. Otherwise, see if it is live or dead at the start of the basic
11434 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11435 must be assumed to be always live. */
11436
11437 static int
11438 reg_dead_at_p (rtx reg, rtx insn)
11439 {
11440 basic_block block;
11441 unsigned int i;
11442
11443 /* Set variables for reg_dead_at_p_1. */
11444 reg_dead_regno = REGNO (reg);
11445 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11446 ? hard_regno_nregs[reg_dead_regno]
11447 [GET_MODE (reg)]
11448 : 1);
11449
11450 reg_dead_flag = 0;
11451
11452 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11453 we allow the machine description to decide whether use-and-clobber
11454 patterns are OK. */
11455 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11456 {
11457 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11458 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11459 return 0;
11460 }
11461
11462 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11463 beginning of function. */
11464 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11465 insn = prev_nonnote_insn (insn))
11466 {
11467 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11468 if (reg_dead_flag)
11469 return reg_dead_flag == 1 ? 1 : 0;
11470
11471 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11472 return 1;
11473 }
11474
11475 /* Get the basic block that we were in. */
11476 if (insn == 0)
11477 block = ENTRY_BLOCK_PTR->next_bb;
11478 else
11479 {
11480 FOR_EACH_BB (block)
11481 if (insn == BB_HEAD (block))
11482 break;
11483
11484 if (block == EXIT_BLOCK_PTR)
11485 return 0;
11486 }
11487
11488 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11489 if (REGNO_REG_SET_P (block->global_live_at_start, i))
11490 return 0;
11491
11492 return 1;
11493 }
11494 \f
11495 /* Note hard registers in X that are used. This code is similar to
11496 that in flow.c, but much simpler since we don't care about pseudos. */
11497
11498 static void
11499 mark_used_regs_combine (rtx x)
11500 {
11501 RTX_CODE code = GET_CODE (x);
11502 unsigned int regno;
11503 int i;
11504
11505 switch (code)
11506 {
11507 case LABEL_REF:
11508 case SYMBOL_REF:
11509 case CONST_INT:
11510 case CONST:
11511 case CONST_DOUBLE:
11512 case CONST_VECTOR:
11513 case PC:
11514 case ADDR_VEC:
11515 case ADDR_DIFF_VEC:
11516 case ASM_INPUT:
11517 #ifdef HAVE_cc0
11518 /* CC0 must die in the insn after it is set, so we don't need to take
11519 special note of it here. */
11520 case CC0:
11521 #endif
11522 return;
11523
11524 case CLOBBER:
11525 /* If we are clobbering a MEM, mark any hard registers inside the
11526 address as used. */
11527 if (MEM_P (XEXP (x, 0)))
11528 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11529 return;
11530
11531 case REG:
11532 regno = REGNO (x);
11533 /* A hard reg in a wide mode may really be multiple registers.
11534 If so, mark all of them just like the first. */
11535 if (regno < FIRST_PSEUDO_REGISTER)
11536 {
11537 unsigned int endregno, r;
11538
11539 /* None of this applies to the stack, frame or arg pointers. */
11540 if (regno == STACK_POINTER_REGNUM
11541 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11542 || regno == HARD_FRAME_POINTER_REGNUM
11543 #endif
11544 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11545 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11546 #endif
11547 || regno == FRAME_POINTER_REGNUM)
11548 return;
11549
11550 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11551 for (r = regno; r < endregno; r++)
11552 SET_HARD_REG_BIT (newpat_used_regs, r);
11553 }
11554 return;
11555
11556 case SET:
11557 {
11558 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11559 the address. */
11560 rtx testreg = SET_DEST (x);
11561
11562 while (GET_CODE (testreg) == SUBREG
11563 || GET_CODE (testreg) == ZERO_EXTRACT
11564 || GET_CODE (testreg) == STRICT_LOW_PART)
11565 testreg = XEXP (testreg, 0);
11566
11567 if (MEM_P (testreg))
11568 mark_used_regs_combine (XEXP (testreg, 0));
11569
11570 mark_used_regs_combine (SET_SRC (x));
11571 }
11572 return;
11573
11574 default:
11575 break;
11576 }
11577
11578 /* Recursively scan the operands of this expression. */
11579
11580 {
11581 const char *fmt = GET_RTX_FORMAT (code);
11582
11583 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11584 {
11585 if (fmt[i] == 'e')
11586 mark_used_regs_combine (XEXP (x, i));
11587 else if (fmt[i] == 'E')
11588 {
11589 int j;
11590
11591 for (j = 0; j < XVECLEN (x, i); j++)
11592 mark_used_regs_combine (XVECEXP (x, i, j));
11593 }
11594 }
11595 }
11596 }
11597 \f
11598 /* Remove register number REGNO from the dead registers list of INSN.
11599
11600 Return the note used to record the death, if there was one. */
11601
11602 rtx
11603 remove_death (unsigned int regno, rtx insn)
11604 {
11605 rtx note = find_regno_note (insn, REG_DEAD, regno);
11606
11607 if (note)
11608 {
11609 REG_N_DEATHS (regno)--;
11610 remove_note (insn, note);
11611 }
11612
11613 return note;
11614 }
11615
11616 /* For each register (hardware or pseudo) used within expression X, if its
11617 death is in an instruction with cuid between FROM_CUID (inclusive) and
11618 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11619 list headed by PNOTES.
11620
11621 That said, don't move registers killed by maybe_kill_insn.
11622
11623 This is done when X is being merged by combination into TO_INSN. These
11624 notes will then be distributed as needed. */
11625
11626 static void
11627 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11628 rtx *pnotes)
11629 {
11630 const char *fmt;
11631 int len, i;
11632 enum rtx_code code = GET_CODE (x);
11633
11634 if (code == REG)
11635 {
11636 unsigned int regno = REGNO (x);
11637 rtx where_dead = reg_stat[regno].last_death;
11638 rtx before_dead, after_dead;
11639
11640 /* Don't move the register if it gets killed in between from and to. */
11641 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11642 && ! reg_referenced_p (x, maybe_kill_insn))
11643 return;
11644
11645 /* WHERE_DEAD could be a USE insn made by combine, so first we
11646 make sure that we have insns with valid INSN_CUID values. */
11647 before_dead = where_dead;
11648 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11649 before_dead = PREV_INSN (before_dead);
11650
11651 after_dead = where_dead;
11652 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11653 after_dead = NEXT_INSN (after_dead);
11654
11655 if (before_dead && after_dead
11656 && INSN_CUID (before_dead) >= from_cuid
11657 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11658 || (where_dead != after_dead
11659 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11660 {
11661 rtx note = remove_death (regno, where_dead);
11662
11663 /* It is possible for the call above to return 0. This can occur
11664 when last_death points to I2 or I1 that we combined with.
11665 In that case make a new note.
11666
11667 We must also check for the case where X is a hard register
11668 and NOTE is a death note for a range of hard registers
11669 including X. In that case, we must put REG_DEAD notes for
11670 the remaining registers in place of NOTE. */
11671
11672 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11673 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11674 > GET_MODE_SIZE (GET_MODE (x))))
11675 {
11676 unsigned int deadregno = REGNO (XEXP (note, 0));
11677 unsigned int deadend
11678 = (deadregno + hard_regno_nregs[deadregno]
11679 [GET_MODE (XEXP (note, 0))]);
11680 unsigned int ourend
11681 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11682 unsigned int i;
11683
11684 for (i = deadregno; i < deadend; i++)
11685 if (i < regno || i >= ourend)
11686 REG_NOTES (where_dead)
11687 = gen_rtx_EXPR_LIST (REG_DEAD,
11688 regno_reg_rtx[i],
11689 REG_NOTES (where_dead));
11690 }
11691
11692 /* If we didn't find any note, or if we found a REG_DEAD note that
11693 covers only part of the given reg, and we have a multi-reg hard
11694 register, then to be safe we must check for REG_DEAD notes
11695 for each register other than the first. They could have
11696 their own REG_DEAD notes lying around. */
11697 else if ((note == 0
11698 || (note != 0
11699 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11700 < GET_MODE_SIZE (GET_MODE (x)))))
11701 && regno < FIRST_PSEUDO_REGISTER
11702 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11703 {
11704 unsigned int ourend
11705 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11706 unsigned int i, offset;
11707 rtx oldnotes = 0;
11708
11709 if (note)
11710 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11711 else
11712 offset = 1;
11713
11714 for (i = regno + offset; i < ourend; i++)
11715 move_deaths (regno_reg_rtx[i],
11716 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11717 }
11718
11719 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11720 {
11721 XEXP (note, 1) = *pnotes;
11722 *pnotes = note;
11723 }
11724 else
11725 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11726
11727 REG_N_DEATHS (regno)++;
11728 }
11729
11730 return;
11731 }
11732
11733 else if (GET_CODE (x) == SET)
11734 {
11735 rtx dest = SET_DEST (x);
11736
11737 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11738
11739 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11740 that accesses one word of a multi-word item, some
11741 piece of everything register in the expression is used by
11742 this insn, so remove any old death. */
11743 /* ??? So why do we test for equality of the sizes? */
11744
11745 if (GET_CODE (dest) == ZERO_EXTRACT
11746 || GET_CODE (dest) == STRICT_LOW_PART
11747 || (GET_CODE (dest) == SUBREG
11748 && (((GET_MODE_SIZE (GET_MODE (dest))
11749 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11750 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11751 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11752 {
11753 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11754 return;
11755 }
11756
11757 /* If this is some other SUBREG, we know it replaces the entire
11758 value, so use that as the destination. */
11759 if (GET_CODE (dest) == SUBREG)
11760 dest = SUBREG_REG (dest);
11761
11762 /* If this is a MEM, adjust deaths of anything used in the address.
11763 For a REG (the only other possibility), the entire value is
11764 being replaced so the old value is not used in this insn. */
11765
11766 if (MEM_P (dest))
11767 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11768 to_insn, pnotes);
11769 return;
11770 }
11771
11772 else if (GET_CODE (x) == CLOBBER)
11773 return;
11774
11775 len = GET_RTX_LENGTH (code);
11776 fmt = GET_RTX_FORMAT (code);
11777
11778 for (i = 0; i < len; i++)
11779 {
11780 if (fmt[i] == 'E')
11781 {
11782 int j;
11783 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11784 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11785 to_insn, pnotes);
11786 }
11787 else if (fmt[i] == 'e')
11788 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11789 }
11790 }
11791 \f
11792 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11793 pattern of an insn. X must be a REG. */
11794
11795 static int
11796 reg_bitfield_target_p (rtx x, rtx body)
11797 {
11798 int i;
11799
11800 if (GET_CODE (body) == SET)
11801 {
11802 rtx dest = SET_DEST (body);
11803 rtx target;
11804 unsigned int regno, tregno, endregno, endtregno;
11805
11806 if (GET_CODE (dest) == ZERO_EXTRACT)
11807 target = XEXP (dest, 0);
11808 else if (GET_CODE (dest) == STRICT_LOW_PART)
11809 target = SUBREG_REG (XEXP (dest, 0));
11810 else
11811 return 0;
11812
11813 if (GET_CODE (target) == SUBREG)
11814 target = SUBREG_REG (target);
11815
11816 if (!REG_P (target))
11817 return 0;
11818
11819 tregno = REGNO (target), regno = REGNO (x);
11820 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11821 return target == x;
11822
11823 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11824 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11825
11826 return endregno > tregno && regno < endtregno;
11827 }
11828
11829 else if (GET_CODE (body) == PARALLEL)
11830 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11831 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11832 return 1;
11833
11834 return 0;
11835 }
11836 \f
11837 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11838 as appropriate. I3 and I2 are the insns resulting from the combination
11839 insns including FROM (I2 may be zero).
11840
11841 Each note in the list is either ignored or placed on some insns, depending
11842 on the type of note. */
11843
11844 static void
11845 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
11846 {
11847 rtx note, next_note;
11848 rtx tem;
11849
11850 for (note = notes; note; note = next_note)
11851 {
11852 rtx place = 0, place2 = 0;
11853
11854 /* If this NOTE references a pseudo register, ensure it references
11855 the latest copy of that register. */
11856 if (XEXP (note, 0) && REG_P (XEXP (note, 0))
11857 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11858 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11859
11860 next_note = XEXP (note, 1);
11861 switch (REG_NOTE_KIND (note))
11862 {
11863 case REG_BR_PROB:
11864 case REG_BR_PRED:
11865 /* Doesn't matter much where we put this, as long as it's somewhere.
11866 It is preferable to keep these notes on branches, which is most
11867 likely to be i3. */
11868 place = i3;
11869 break;
11870
11871 case REG_VALUE_PROFILE:
11872 /* Just get rid of this note, as it is unused later anyway. */
11873 break;
11874
11875 case REG_NON_LOCAL_GOTO:
11876 if (JUMP_P (i3))
11877 place = i3;
11878 else
11879 {
11880 gcc_assert (i2 && JUMP_P (i2));
11881 place = i2;
11882 }
11883 break;
11884
11885 case REG_EH_REGION:
11886 /* These notes must remain with the call or trapping instruction. */
11887 if (CALL_P (i3))
11888 place = i3;
11889 else if (i2 && CALL_P (i2))
11890 place = i2;
11891 else
11892 {
11893 gcc_assert (flag_non_call_exceptions);
11894 if (may_trap_p (i3))
11895 place = i3;
11896 else if (i2 && may_trap_p (i2))
11897 place = i2;
11898 /* ??? Otherwise assume we've combined things such that we
11899 can now prove that the instructions can't trap. Drop the
11900 note in this case. */
11901 }
11902 break;
11903
11904 case REG_NORETURN:
11905 case REG_SETJMP:
11906 /* These notes must remain with the call. It should not be
11907 possible for both I2 and I3 to be a call. */
11908 if (CALL_P (i3))
11909 place = i3;
11910 else
11911 {
11912 gcc_assert (i2 && CALL_P (i2));
11913 place = i2;
11914 }
11915 break;
11916
11917 case REG_UNUSED:
11918 /* Any clobbers for i3 may still exist, and so we must process
11919 REG_UNUSED notes from that insn.
11920
11921 Any clobbers from i2 or i1 can only exist if they were added by
11922 recog_for_combine. In that case, recog_for_combine created the
11923 necessary REG_UNUSED notes. Trying to keep any original
11924 REG_UNUSED notes from these insns can cause incorrect output
11925 if it is for the same register as the original i3 dest.
11926 In that case, we will notice that the register is set in i3,
11927 and then add a REG_UNUSED note for the destination of i3, which
11928 is wrong. However, it is possible to have REG_UNUSED notes from
11929 i2 or i1 for register which were both used and clobbered, so
11930 we keep notes from i2 or i1 if they will turn into REG_DEAD
11931 notes. */
11932
11933 /* If this register is set or clobbered in I3, put the note there
11934 unless there is one already. */
11935 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11936 {
11937 if (from_insn != i3)
11938 break;
11939
11940 if (! (REG_P (XEXP (note, 0))
11941 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11942 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11943 place = i3;
11944 }
11945 /* Otherwise, if this register is used by I3, then this register
11946 now dies here, so we must put a REG_DEAD note here unless there
11947 is one already. */
11948 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11949 && ! (REG_P (XEXP (note, 0))
11950 ? find_regno_note (i3, REG_DEAD,
11951 REGNO (XEXP (note, 0)))
11952 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11953 {
11954 PUT_REG_NOTE_KIND (note, REG_DEAD);
11955 place = i3;
11956 }
11957 break;
11958
11959 case REG_EQUAL:
11960 case REG_EQUIV:
11961 case REG_NOALIAS:
11962 /* These notes say something about results of an insn. We can
11963 only support them if they used to be on I3 in which case they
11964 remain on I3. Otherwise they are ignored.
11965
11966 If the note refers to an expression that is not a constant, we
11967 must also ignore the note since we cannot tell whether the
11968 equivalence is still true. It might be possible to do
11969 slightly better than this (we only have a problem if I2DEST
11970 or I1DEST is present in the expression), but it doesn't
11971 seem worth the trouble. */
11972
11973 if (from_insn == i3
11974 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11975 place = i3;
11976 break;
11977
11978 case REG_INC:
11979 case REG_NO_CONFLICT:
11980 /* These notes say something about how a register is used. They must
11981 be present on any use of the register in I2 or I3. */
11982 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11983 place = i3;
11984
11985 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11986 {
11987 if (place)
11988 place2 = i2;
11989 else
11990 place = i2;
11991 }
11992 break;
11993
11994 case REG_LABEL:
11995 /* This can show up in several ways -- either directly in the
11996 pattern, or hidden off in the constant pool with (or without?)
11997 a REG_EQUAL note. */
11998 /* ??? Ignore the without-reg_equal-note problem for now. */
11999 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12000 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12001 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12002 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12003 place = i3;
12004
12005 if (i2
12006 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12007 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12008 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12009 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12010 {
12011 if (place)
12012 place2 = i2;
12013 else
12014 place = i2;
12015 }
12016
12017 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
12018 a JUMP_LABEL instead or decrement LABEL_NUSES. */
12019 if (place && JUMP_P (place))
12020 {
12021 rtx label = JUMP_LABEL (place);
12022
12023 if (!label)
12024 JUMP_LABEL (place) = XEXP (note, 0);
12025 else
12026 {
12027 gcc_assert (label == XEXP (note, 0));
12028 if (LABEL_P (label))
12029 LABEL_NUSES (label)--;
12030 }
12031 place = 0;
12032 }
12033 if (place2 && JUMP_P (place2))
12034 {
12035 rtx label = JUMP_LABEL (place2);
12036
12037 if (!label)
12038 JUMP_LABEL (place2) = XEXP (note, 0);
12039 else
12040 {
12041 gcc_assert (label == XEXP (note, 0));
12042 if (LABEL_P (label))
12043 LABEL_NUSES (label)--;
12044 }
12045 place2 = 0;
12046 }
12047 break;
12048
12049 case REG_NONNEG:
12050 /* This note says something about the value of a register prior
12051 to the execution of an insn. It is too much trouble to see
12052 if the note is still correct in all situations. It is better
12053 to simply delete it. */
12054 break;
12055
12056 case REG_RETVAL:
12057 /* If the insn previously containing this note still exists,
12058 put it back where it was. Otherwise move it to the previous
12059 insn. Adjust the corresponding REG_LIBCALL note. */
12060 if (!NOTE_P (from_insn))
12061 place = from_insn;
12062 else
12063 {
12064 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12065 place = prev_real_insn (from_insn);
12066 if (tem && place)
12067 XEXP (tem, 0) = place;
12068 /* If we're deleting the last remaining instruction of a
12069 libcall sequence, don't add the notes. */
12070 else if (XEXP (note, 0) == from_insn)
12071 tem = place = 0;
12072 /* Don't add the dangling REG_RETVAL note. */
12073 else if (! tem)
12074 place = 0;
12075 }
12076 break;
12077
12078 case REG_LIBCALL:
12079 /* This is handled similarly to REG_RETVAL. */
12080 if (!NOTE_P (from_insn))
12081 place = from_insn;
12082 else
12083 {
12084 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12085 place = next_real_insn (from_insn);
12086 if (tem && place)
12087 XEXP (tem, 0) = place;
12088 /* If we're deleting the last remaining instruction of a
12089 libcall sequence, don't add the notes. */
12090 else if (XEXP (note, 0) == from_insn)
12091 tem = place = 0;
12092 /* Don't add the dangling REG_LIBCALL note. */
12093 else if (! tem)
12094 place = 0;
12095 }
12096 break;
12097
12098 case REG_DEAD:
12099 /* If the register is used as an input in I3, it dies there.
12100 Similarly for I2, if it is nonzero and adjacent to I3.
12101
12102 If the register is not used as an input in either I3 or I2
12103 and it is not one of the registers we were supposed to eliminate,
12104 there are two possibilities. We might have a non-adjacent I2
12105 or we might have somehow eliminated an additional register
12106 from a computation. For example, we might have had A & B where
12107 we discover that B will always be zero. In this case we will
12108 eliminate the reference to A.
12109
12110 In both cases, we must search to see if we can find a previous
12111 use of A and put the death note there. */
12112
12113 if (from_insn
12114 && CALL_P (from_insn)
12115 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12116 place = from_insn;
12117 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12118 place = i3;
12119 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12120 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12121 place = i2;
12122
12123 if (place == 0)
12124 {
12125 basic_block bb = this_basic_block;
12126
12127 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12128 {
12129 if (! INSN_P (tem))
12130 {
12131 if (tem == BB_HEAD (bb))
12132 break;
12133 continue;
12134 }
12135
12136 /* If the register is being set at TEM, see if that is all
12137 TEM is doing. If so, delete TEM. Otherwise, make this
12138 into a REG_UNUSED note instead. Don't delete sets to
12139 global register vars. */
12140 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12141 || !global_regs[REGNO (XEXP (note, 0))])
12142 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12143 {
12144 rtx set = single_set (tem);
12145 rtx inner_dest = 0;
12146 #ifdef HAVE_cc0
12147 rtx cc0_setter = NULL_RTX;
12148 #endif
12149
12150 if (set != 0)
12151 for (inner_dest = SET_DEST (set);
12152 (GET_CODE (inner_dest) == STRICT_LOW_PART
12153 || GET_CODE (inner_dest) == SUBREG
12154 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12155 inner_dest = XEXP (inner_dest, 0))
12156 ;
12157
12158 /* Verify that it was the set, and not a clobber that
12159 modified the register.
12160
12161 CC0 targets must be careful to maintain setter/user
12162 pairs. If we cannot delete the setter due to side
12163 effects, mark the user with an UNUSED note instead
12164 of deleting it. */
12165
12166 if (set != 0 && ! side_effects_p (SET_SRC (set))
12167 && rtx_equal_p (XEXP (note, 0), inner_dest)
12168 #ifdef HAVE_cc0
12169 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12170 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12171 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12172 #endif
12173 )
12174 {
12175 /* Move the notes and links of TEM elsewhere.
12176 This might delete other dead insns recursively.
12177 First set the pattern to something that won't use
12178 any register. */
12179 rtx old_notes = REG_NOTES (tem);
12180
12181 PATTERN (tem) = pc_rtx;
12182 REG_NOTES (tem) = NULL;
12183
12184 distribute_notes (old_notes, tem, tem, NULL_RTX);
12185 distribute_links (LOG_LINKS (tem));
12186
12187 SET_INSN_DELETED (tem);
12188
12189 #ifdef HAVE_cc0
12190 /* Delete the setter too. */
12191 if (cc0_setter)
12192 {
12193 PATTERN (cc0_setter) = pc_rtx;
12194 old_notes = REG_NOTES (cc0_setter);
12195 REG_NOTES (cc0_setter) = NULL;
12196
12197 distribute_notes (old_notes, cc0_setter,
12198 cc0_setter, NULL_RTX);
12199 distribute_links (LOG_LINKS (cc0_setter));
12200
12201 SET_INSN_DELETED (cc0_setter);
12202 }
12203 #endif
12204 }
12205 else
12206 {
12207 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12208
12209 /* If there isn't already a REG_UNUSED note, put one
12210 here. Do not place a REG_DEAD note, even if
12211 the register is also used here; that would not
12212 match the algorithm used in lifetime analysis
12213 and can cause the consistency check in the
12214 scheduler to fail. */
12215 if (! find_regno_note (tem, REG_UNUSED,
12216 REGNO (XEXP (note, 0))))
12217 place = tem;
12218 break;
12219 }
12220 }
12221 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12222 || (CALL_P (tem)
12223 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12224 {
12225 place = tem;
12226
12227 /* If we are doing a 3->2 combination, and we have a
12228 register which formerly died in i3 and was not used
12229 by i2, which now no longer dies in i3 and is used in
12230 i2 but does not die in i2, and place is between i2
12231 and i3, then we may need to move a link from place to
12232 i2. */
12233 if (i2 && INSN_UID (place) <= max_uid_cuid
12234 && INSN_CUID (place) > INSN_CUID (i2)
12235 && from_insn
12236 && INSN_CUID (from_insn) > INSN_CUID (i2)
12237 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12238 {
12239 rtx links = LOG_LINKS (place);
12240 LOG_LINKS (place) = 0;
12241 distribute_links (links);
12242 }
12243 break;
12244 }
12245
12246 if (tem == BB_HEAD (bb))
12247 break;
12248 }
12249
12250 /* We haven't found an insn for the death note and it
12251 is still a REG_DEAD note, but we have hit the beginning
12252 of the block. If the existing life info says the reg
12253 was dead, there's nothing left to do. Otherwise, we'll
12254 need to do a global life update after combine. */
12255 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12256 && REGNO_REG_SET_P (bb->global_live_at_start,
12257 REGNO (XEXP (note, 0))))
12258 SET_BIT (refresh_blocks, this_basic_block->index);
12259 }
12260
12261 /* If the register is set or already dead at PLACE, we needn't do
12262 anything with this note if it is still a REG_DEAD note.
12263 We check here if it is set at all, not if is it totally replaced,
12264 which is what `dead_or_set_p' checks, so also check for it being
12265 set partially. */
12266
12267 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12268 {
12269 unsigned int regno = REGNO (XEXP (note, 0));
12270
12271 /* Similarly, if the instruction on which we want to place
12272 the note is a noop, we'll need do a global live update
12273 after we remove them in delete_noop_moves. */
12274 if (noop_move_p (place))
12275 SET_BIT (refresh_blocks, this_basic_block->index);
12276
12277 if (dead_or_set_p (place, XEXP (note, 0))
12278 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12279 {
12280 /* Unless the register previously died in PLACE, clear
12281 last_death. [I no longer understand why this is
12282 being done.] */
12283 if (reg_stat[regno].last_death != place)
12284 reg_stat[regno].last_death = 0;
12285 place = 0;
12286 }
12287 else
12288 reg_stat[regno].last_death = place;
12289
12290 /* If this is a death note for a hard reg that is occupying
12291 multiple registers, ensure that we are still using all
12292 parts of the object. If we find a piece of the object
12293 that is unused, we must arrange for an appropriate REG_DEAD
12294 note to be added for it. However, we can't just emit a USE
12295 and tag the note to it, since the register might actually
12296 be dead; so we recourse, and the recursive call then finds
12297 the previous insn that used this register. */
12298
12299 if (place && regno < FIRST_PSEUDO_REGISTER
12300 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12301 {
12302 unsigned int endregno
12303 = regno + hard_regno_nregs[regno]
12304 [GET_MODE (XEXP (note, 0))];
12305 int all_used = 1;
12306 unsigned int i;
12307
12308 for (i = regno; i < endregno; i++)
12309 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12310 && ! find_regno_fusage (place, USE, i))
12311 || dead_or_set_regno_p (place, i))
12312 all_used = 0;
12313
12314 if (! all_used)
12315 {
12316 /* Put only REG_DEAD notes for pieces that are
12317 not already dead or set. */
12318
12319 for (i = regno; i < endregno;
12320 i += hard_regno_nregs[i][reg_raw_mode[i]])
12321 {
12322 rtx piece = regno_reg_rtx[i];
12323 basic_block bb = this_basic_block;
12324
12325 if (! dead_or_set_p (place, piece)
12326 && ! reg_bitfield_target_p (piece,
12327 PATTERN (place)))
12328 {
12329 rtx new_note
12330 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12331
12332 distribute_notes (new_note, place, place,
12333 NULL_RTX);
12334 }
12335 else if (! refers_to_regno_p (i, i + 1,
12336 PATTERN (place), 0)
12337 && ! find_regno_fusage (place, USE, i))
12338 for (tem = PREV_INSN (place); ;
12339 tem = PREV_INSN (tem))
12340 {
12341 if (! INSN_P (tem))
12342 {
12343 if (tem == BB_HEAD (bb))
12344 {
12345 SET_BIT (refresh_blocks,
12346 this_basic_block->index);
12347 break;
12348 }
12349 continue;
12350 }
12351 if (dead_or_set_p (tem, piece)
12352 || reg_bitfield_target_p (piece,
12353 PATTERN (tem)))
12354 {
12355 REG_NOTES (tem)
12356 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12357 REG_NOTES (tem));
12358 break;
12359 }
12360 }
12361
12362 }
12363
12364 place = 0;
12365 }
12366 }
12367 }
12368 break;
12369
12370 default:
12371 /* Any other notes should not be present at this point in the
12372 compilation. */
12373 gcc_unreachable ();
12374 }
12375
12376 if (place)
12377 {
12378 XEXP (note, 1) = REG_NOTES (place);
12379 REG_NOTES (place) = note;
12380 }
12381 else if ((REG_NOTE_KIND (note) == REG_DEAD
12382 || REG_NOTE_KIND (note) == REG_UNUSED)
12383 && REG_P (XEXP (note, 0)))
12384 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12385
12386 if (place2)
12387 {
12388 if ((REG_NOTE_KIND (note) == REG_DEAD
12389 || REG_NOTE_KIND (note) == REG_UNUSED)
12390 && REG_P (XEXP (note, 0)))
12391 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12392
12393 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12394 REG_NOTE_KIND (note),
12395 XEXP (note, 0),
12396 REG_NOTES (place2));
12397 }
12398 }
12399 }
12400 \f
12401 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12402 I3, I2, and I1 to new locations. This is also called to add a link
12403 pointing at I3 when I3's destination is changed. */
12404
12405 static void
12406 distribute_links (rtx links)
12407 {
12408 rtx link, next_link;
12409
12410 for (link = links; link; link = next_link)
12411 {
12412 rtx place = 0;
12413 rtx insn;
12414 rtx set, reg;
12415
12416 next_link = XEXP (link, 1);
12417
12418 /* If the insn that this link points to is a NOTE or isn't a single
12419 set, ignore it. In the latter case, it isn't clear what we
12420 can do other than ignore the link, since we can't tell which
12421 register it was for. Such links wouldn't be used by combine
12422 anyway.
12423
12424 It is not possible for the destination of the target of the link to
12425 have been changed by combine. The only potential of this is if we
12426 replace I3, I2, and I1 by I3 and I2. But in that case the
12427 destination of I2 also remains unchanged. */
12428
12429 if (NOTE_P (XEXP (link, 0))
12430 || (set = single_set (XEXP (link, 0))) == 0)
12431 continue;
12432
12433 reg = SET_DEST (set);
12434 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12435 || GET_CODE (reg) == STRICT_LOW_PART)
12436 reg = XEXP (reg, 0);
12437
12438 /* A LOG_LINK is defined as being placed on the first insn that uses
12439 a register and points to the insn that sets the register. Start
12440 searching at the next insn after the target of the link and stop
12441 when we reach a set of the register or the end of the basic block.
12442
12443 Note that this correctly handles the link that used to point from
12444 I3 to I2. Also note that not much searching is typically done here
12445 since most links don't point very far away. */
12446
12447 for (insn = NEXT_INSN (XEXP (link, 0));
12448 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12449 || BB_HEAD (this_basic_block->next_bb) != insn));
12450 insn = NEXT_INSN (insn))
12451 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12452 {
12453 if (reg_referenced_p (reg, PATTERN (insn)))
12454 place = insn;
12455 break;
12456 }
12457 else if (CALL_P (insn)
12458 && find_reg_fusage (insn, USE, reg))
12459 {
12460 place = insn;
12461 break;
12462 }
12463 else if (INSN_P (insn) && reg_set_p (reg, insn))
12464 break;
12465
12466 /* If we found a place to put the link, place it there unless there
12467 is already a link to the same insn as LINK at that point. */
12468
12469 if (place)
12470 {
12471 rtx link2;
12472
12473 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12474 if (XEXP (link2, 0) == XEXP (link, 0))
12475 break;
12476
12477 if (link2 == 0)
12478 {
12479 XEXP (link, 1) = LOG_LINKS (place);
12480 LOG_LINKS (place) = link;
12481
12482 /* Set added_links_insn to the earliest insn we added a
12483 link to. */
12484 if (added_links_insn == 0
12485 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12486 added_links_insn = place;
12487 }
12488 }
12489 }
12490 }
12491 \f
12492 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12493 Check whether the expression pointer to by LOC is a register or
12494 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12495 Otherwise return zero. */
12496
12497 static int
12498 unmentioned_reg_p_1 (rtx *loc, void *expr)
12499 {
12500 rtx x = *loc;
12501
12502 if (x != NULL_RTX
12503 && (REG_P (x) || MEM_P (x))
12504 && ! reg_mentioned_p (x, (rtx) expr))
12505 return 1;
12506 return 0;
12507 }
12508
12509 /* Check for any register or memory mentioned in EQUIV that is not
12510 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12511 of EXPR where some registers may have been replaced by constants. */
12512
12513 static bool
12514 unmentioned_reg_p (rtx equiv, rtx expr)
12515 {
12516 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12517 }
12518 \f
12519 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12520
12521 static int
12522 insn_cuid (rtx insn)
12523 {
12524 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12525 && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12526 insn = NEXT_INSN (insn);
12527
12528 gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12529
12530 return INSN_CUID (insn);
12531 }
12532 \f
12533 void
12534 dump_combine_stats (FILE *file)
12535 {
12536 fnotice
12537 (file,
12538 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12539 combine_attempts, combine_merges, combine_extras, combine_successes);
12540 }
12541
12542 void
12543 dump_combine_total_stats (FILE *file)
12544 {
12545 fnotice
12546 (file,
12547 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12548 total_attempts, total_merges, total_extras, total_successes);
12549 }
This page took 0.707036 seconds and 6 git commands to generate.