]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/combine.c
re PR rtl-optimization/27761 (combine miscompiles)
[thirdparty/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, 2006 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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 - reg_n_refs is not adjusted in the rare case when a register is
57 no longer required in a computation
58 - there are extremely rare cases (see distribute_notes) when a
59 REG_DEAD note is lost
60 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61 removed because there is no way to know which register it was
62 linking
63
64 To simplify substitution, we combine only when the earlier insn(s)
65 consist of only a single assignment. To simplify updating afterward,
66 we never combine when a subroutine call appears in the middle.
67
68 Since we do not represent assignments to CC0 explicitly except when that
69 is all an insn does, there is no LOG_LINKS entry in an insn that uses
70 the condition code for the insn that set the condition code.
71 Fortunately, these two insns must be consecutive.
72 Therefore, every JUMP_INSN is taken to have an implicit logical link
73 to the preceding insn. This is not quite right, since non-jumps can
74 also use the condition code; but in practice such insns would not
75 combine anyway. */
76
77 #include "config.h"
78 #include "system.h"
79 #include "coretypes.h"
80 #include "tm.h"
81 #include "rtl.h"
82 #include "tree.h"
83 #include "tm_p.h"
84 #include "flags.h"
85 #include "regs.h"
86 #include "hard-reg-set.h"
87 #include "basic-block.h"
88 #include "insn-config.h"
89 #include "function.h"
90 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
91 #include "expr.h"
92 #include "insn-attr.h"
93 #include "recog.h"
94 #include "real.h"
95 #include "toplev.h"
96 #include "target.h"
97 #include "optabs.h"
98 #include "insn-codes.h"
99 #include "rtlhooks-def.h"
100 /* Include output.h for dump_file. */
101 #include "output.h"
102 #include "params.h"
103 #include "timevar.h"
104 #include "tree-pass.h"
105
106 /* Number of attempts to combine instructions in this function. */
107
108 static int combine_attempts;
109
110 /* Number of attempts that got as far as substitution in this function. */
111
112 static int combine_merges;
113
114 /* Number of instructions combined with added SETs in this function. */
115
116 static int combine_extras;
117
118 /* Number of instructions combined in this function. */
119
120 static int combine_successes;
121
122 /* Totals over entire compilation. */
123
124 static int total_attempts, total_merges, total_extras, total_successes;
125
126 /* Sometimes combine tries to replace the right hand side of an insn
127 with the value of a REG_EQUAL note. This is the insn that has been
128 so modified, or null if none. */
129
130 static rtx replaced_rhs_insn;
131
132 /* When REPLACED_RHS_INSN is nonnull, this is a copy of the new right
133 hand side. */
134
135 static rtx replaced_rhs_value;
136 \f
137 /* Vector mapping INSN_UIDs to cuids.
138 The cuids are like uids but increase monotonically always.
139 Combine always uses cuids so that it can compare them.
140 But actually renumbering the uids, which we used to do,
141 proves to be a bad idea because it makes it hard to compare
142 the dumps produced by earlier passes with those from later passes. */
143
144 static int *uid_cuid;
145 static int max_uid_cuid;
146
147 /* Get the cuid of an insn. */
148
149 #define INSN_CUID(INSN) \
150 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
151
152 /* Maximum register number, which is the size of the tables below. */
153
154 static unsigned int combine_max_regno;
155
156 struct reg_stat {
157 /* Record last point of death of (hard or pseudo) register n. */
158 rtx last_death;
159
160 /* Record last point of modification of (hard or pseudo) register n. */
161 rtx last_set;
162
163 /* The next group of fields allows the recording of the last value assigned
164 to (hard or pseudo) register n. We use this information to see if an
165 operation being processed is redundant given a prior operation performed
166 on the register. For example, an `and' with a constant is redundant if
167 all the zero bits are already known to be turned off.
168
169 We use an approach similar to that used by cse, but change it in the
170 following ways:
171
172 (1) We do not want to reinitialize at each label.
173 (2) It is useful, but not critical, to know the actual value assigned
174 to a register. Often just its form is helpful.
175
176 Therefore, we maintain the following fields:
177
178 last_set_value the last value assigned
179 last_set_label records the value of label_tick when the
180 register was assigned
181 last_set_table_tick records the value of label_tick when a
182 value using the register is assigned
183 last_set_invalid set to nonzero when it is not valid
184 to use the value of this register in some
185 register's value
186
187 To understand the usage of these tables, it is important to understand
188 the distinction between the value in last_set_value being valid and
189 the register being validly contained in some other expression in the
190 table.
191
192 (The next two parameters are out of date).
193
194 reg_stat[i].last_set_value is valid if it is nonzero, and either
195 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
196
197 Register I may validly appear in any expression returned for the value
198 of another register if reg_n_sets[i] is 1. It may also appear in the
199 value for register J if reg_stat[j].last_set_invalid is zero, or
200 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
201
202 If an expression is found in the table containing a register which may
203 not validly appear in an expression, the register is replaced by
204 something that won't match, (clobber (const_int 0)). */
205
206 /* Record last value assigned to (hard or pseudo) register n. */
207
208 rtx last_set_value;
209
210 /* Record the value of label_tick when an expression involving register n
211 is placed in last_set_value. */
212
213 int last_set_table_tick;
214
215 /* Record the value of label_tick when the value for register n is placed in
216 last_set_value. */
217
218 int last_set_label;
219
220 /* These fields are maintained in parallel with last_set_value and are
221 used to store the mode in which the register was last set, the bits
222 that were known to be zero when it was last set, and the number of
223 sign bits copies it was known to have when it was last set. */
224
225 unsigned HOST_WIDE_INT last_set_nonzero_bits;
226 char last_set_sign_bit_copies;
227 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
228
229 /* Set nonzero if references to register n in expressions should not be
230 used. last_set_invalid is set nonzero when this register is being
231 assigned to and last_set_table_tick == label_tick. */
232
233 char last_set_invalid;
234
235 /* Some registers that are set more than once and used in more than one
236 basic block are nevertheless always set in similar ways. For example,
237 a QImode register may be loaded from memory in two places on a machine
238 where byte loads zero extend.
239
240 We record in the following fields if a register has some leading bits
241 that are always equal to the sign bit, and what we know about the
242 nonzero bits of a register, specifically which bits are known to be
243 zero.
244
245 If an entry is zero, it means that we don't know anything special. */
246
247 unsigned char sign_bit_copies;
248
249 unsigned HOST_WIDE_INT nonzero_bits;
250
251 /* Record the value of the label_tick when the last truncation
252 happened. The field truncated_to_mode is only valid if
253 truncation_label == label_tick. */
254
255 int truncation_label;
256
257 /* Record the last truncation seen for this register. If truncation
258 is not a nop to this mode we might be able to save an explicit
259 truncation if we know that value already contains a truncated
260 value. */
261
262 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
263 };
264
265 static struct reg_stat *reg_stat;
266
267 /* Record the cuid of the last insn that invalidated memory
268 (anything that writes memory, and subroutine calls, but not pushes). */
269
270 static int mem_last_set;
271
272 /* Record the cuid of the last CALL_INSN
273 so we can tell whether a potential combination crosses any calls. */
274
275 static int last_call_cuid;
276
277 /* When `subst' is called, this is the insn that is being modified
278 (by combining in a previous insn). The PATTERN of this insn
279 is still the old pattern partially modified and it should not be
280 looked at, but this may be used to examine the successors of the insn
281 to judge whether a simplification is valid. */
282
283 static rtx subst_insn;
284
285 /* This is the lowest CUID that `subst' is currently dealing with.
286 get_last_value will not return a value if the register was set at or
287 after this CUID. If not for this mechanism, we could get confused if
288 I2 or I1 in try_combine were an insn that used the old value of a register
289 to obtain a new value. In that case, we might erroneously get the
290 new value of the register when we wanted the old one. */
291
292 static int subst_low_cuid;
293
294 /* This contains any hard registers that are used in newpat; reg_dead_at_p
295 must consider all these registers to be always live. */
296
297 static HARD_REG_SET newpat_used_regs;
298
299 /* This is an insn to which a LOG_LINKS entry has been added. If this
300 insn is the earlier than I2 or I3, combine should rescan starting at
301 that location. */
302
303 static rtx added_links_insn;
304
305 /* Basic block in which we are performing combines. */
306 static basic_block this_basic_block;
307
308 /* A bitmap indicating which blocks had registers go dead at entry.
309 After combine, we'll need to re-do global life analysis with
310 those blocks as starting points. */
311 static sbitmap refresh_blocks;
312 \f
313 /* The following array records the insn_rtx_cost for every insn
314 in the instruction stream. */
315
316 static int *uid_insn_cost;
317
318 /* Length of the currently allocated uid_insn_cost array. */
319
320 static int last_insn_cost;
321
322 /* Incremented for each label. */
323
324 static int label_tick;
325
326 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
327 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
328
329 static enum machine_mode nonzero_bits_mode;
330
331 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
332 be safely used. It is zero while computing them and after combine has
333 completed. This former test prevents propagating values based on
334 previously set values, which can be incorrect if a variable is modified
335 in a loop. */
336
337 static int nonzero_sign_valid;
338
339 \f
340 /* Record one modification to rtl structure
341 to be undone by storing old_contents into *where. */
342
343 struct undo
344 {
345 struct undo *next;
346 enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
347 union { rtx r; int i; enum machine_mode m; } old_contents;
348 union { rtx *r; int *i; } where;
349 };
350
351 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
352 num_undo says how many are currently recorded.
353
354 other_insn is nonzero if we have modified some other insn in the process
355 of working on subst_insn. It must be verified too. */
356
357 struct undobuf
358 {
359 struct undo *undos;
360 struct undo *frees;
361 rtx other_insn;
362 };
363
364 static struct undobuf undobuf;
365
366 /* Number of times the pseudo being substituted for
367 was found and replaced. */
368
369 static int n_occurrences;
370
371 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
372 enum machine_mode,
373 unsigned HOST_WIDE_INT,
374 unsigned HOST_WIDE_INT *);
375 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
376 enum machine_mode,
377 unsigned int, unsigned int *);
378 static void do_SUBST (rtx *, rtx);
379 static void do_SUBST_INT (int *, int);
380 static void init_reg_last (void);
381 static void setup_incoming_promotions (void);
382 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
383 static int cant_combine_insn_p (rtx);
384 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
385 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
386 static int contains_muldiv (rtx);
387 static rtx try_combine (rtx, rtx, rtx, int *);
388 static void undo_all (void);
389 static void undo_commit (void);
390 static rtx *find_split_point (rtx *, rtx);
391 static rtx subst (rtx, rtx, rtx, int, int);
392 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
393 static rtx simplify_if_then_else (rtx);
394 static rtx simplify_set (rtx);
395 static rtx simplify_logical (rtx);
396 static rtx expand_compound_operation (rtx);
397 static rtx expand_field_assignment (rtx);
398 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
399 rtx, unsigned HOST_WIDE_INT, int, int, int);
400 static rtx extract_left_shift (rtx, int);
401 static rtx make_compound_operation (rtx, enum rtx_code);
402 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
403 unsigned HOST_WIDE_INT *);
404 static rtx canon_reg_for_combine (rtx, rtx);
405 static rtx force_to_mode (rtx, enum machine_mode,
406 unsigned HOST_WIDE_INT, int);
407 static rtx if_then_else_cond (rtx, rtx *, rtx *);
408 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
409 static int rtx_equal_for_field_assignment_p (rtx, rtx);
410 static rtx make_field_assignment (rtx);
411 static rtx apply_distributive_law (rtx);
412 static rtx distribute_and_simplify_rtx (rtx, int);
413 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
414 unsigned HOST_WIDE_INT);
415 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
416 unsigned HOST_WIDE_INT);
417 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
418 HOST_WIDE_INT, enum machine_mode, int *);
419 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
420 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
421 int);
422 static int recog_for_combine (rtx *, rtx, rtx *);
423 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
424 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
425 static void update_table_tick (rtx);
426 static void record_value_for_reg (rtx, rtx, rtx);
427 static void check_conversions (rtx, rtx);
428 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
429 static void record_dead_and_set_regs (rtx);
430 static int get_last_value_validate (rtx *, rtx, int, int);
431 static rtx get_last_value (rtx);
432 static int use_crosses_set_p (rtx, int);
433 static void reg_dead_at_p_1 (rtx, rtx, void *);
434 static int reg_dead_at_p (rtx, rtx);
435 static void move_deaths (rtx, rtx, int, rtx, rtx *);
436 static int reg_bitfield_target_p (rtx, rtx);
437 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
438 static void distribute_links (rtx);
439 static void mark_used_regs_combine (rtx);
440 static int insn_cuid (rtx);
441 static void record_promoted_value (rtx, rtx);
442 static int unmentioned_reg_p_1 (rtx *, void *);
443 static bool unmentioned_reg_p (rtx, rtx);
444 static void record_truncated_value (rtx);
445 static bool reg_truncated_to_mode (enum machine_mode, rtx);
446 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
447 \f
448
449 /* It is not safe to use ordinary gen_lowpart in combine.
450 See comments in gen_lowpart_for_combine. */
451 #undef RTL_HOOKS_GEN_LOWPART
452 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
453
454 /* Our implementation of gen_lowpart never emits a new pseudo. */
455 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
456 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
457
458 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
459 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
460
461 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
462 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
463
464 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
465 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
466
467 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
468
469 \f
470 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
471 insn. The substitution can be undone by undo_all. If INTO is already
472 set to NEWVAL, do not record this change. Because computing NEWVAL might
473 also call SUBST, we have to compute it before we put anything into
474 the undo table. */
475
476 static void
477 do_SUBST (rtx *into, rtx newval)
478 {
479 struct undo *buf;
480 rtx oldval = *into;
481
482 if (oldval == newval)
483 return;
484
485 /* We'd like to catch as many invalid transformations here as
486 possible. Unfortunately, there are way too many mode changes
487 that are perfectly valid, so we'd waste too much effort for
488 little gain doing the checks here. Focus on catching invalid
489 transformations involving integer constants. */
490 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
491 && GET_CODE (newval) == CONST_INT)
492 {
493 /* Sanity check that we're replacing oldval with a CONST_INT
494 that is a valid sign-extension for the original mode. */
495 gcc_assert (INTVAL (newval)
496 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
497
498 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
499 CONST_INT is not valid, because after the replacement, the
500 original mode would be gone. Unfortunately, we can't tell
501 when do_SUBST is called to replace the operand thereof, so we
502 perform this test on oldval instead, checking whether an
503 invalid replacement took place before we got here. */
504 gcc_assert (!(GET_CODE (oldval) == SUBREG
505 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
506 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
507 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
508 }
509
510 if (undobuf.frees)
511 buf = undobuf.frees, undobuf.frees = buf->next;
512 else
513 buf = XNEW (struct undo);
514
515 buf->kind = UNDO_RTX;
516 buf->where.r = into;
517 buf->old_contents.r = oldval;
518 *into = newval;
519
520 buf->next = undobuf.undos, undobuf.undos = buf;
521 }
522
523 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
524
525 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
526 for the value of a HOST_WIDE_INT value (including CONST_INT) is
527 not safe. */
528
529 static void
530 do_SUBST_INT (int *into, int newval)
531 {
532 struct undo *buf;
533 int oldval = *into;
534
535 if (oldval == newval)
536 return;
537
538 if (undobuf.frees)
539 buf = undobuf.frees, undobuf.frees = buf->next;
540 else
541 buf = XNEW (struct undo);
542
543 buf->kind = UNDO_INT;
544 buf->where.i = into;
545 buf->old_contents.i = oldval;
546 *into = newval;
547
548 buf->next = undobuf.undos, undobuf.undos = buf;
549 }
550
551 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
552
553 /* Similar to SUBST, but just substitute the mode. This is used when
554 changing the mode of a pseudo-register, so that any other
555 references to the entry in the regno_reg_rtx array will change as
556 well. */
557
558 static void
559 do_SUBST_MODE (rtx *into, enum machine_mode newval)
560 {
561 struct undo *buf;
562 enum machine_mode oldval = GET_MODE (*into);
563
564 if (oldval == newval)
565 return;
566
567 if (undobuf.frees)
568 buf = undobuf.frees, undobuf.frees = buf->next;
569 else
570 buf = XNEW (struct undo);
571
572 buf->kind = UNDO_MODE;
573 buf->where.r = into;
574 buf->old_contents.m = oldval;
575 PUT_MODE (*into, newval);
576
577 buf->next = undobuf.undos, undobuf.undos = buf;
578 }
579
580 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
581 \f
582 /* Subroutine of try_combine. Determine whether the combine replacement
583 patterns NEWPAT and NEWI2PAT are cheaper according to insn_rtx_cost
584 that the original instruction sequence I1, I2 and I3. Note that I1
585 and/or NEWI2PAT may be NULL_RTX. This function returns false, if the
586 costs of all instructions can be estimated, and the replacements are
587 more expensive than the original sequence. */
588
589 static bool
590 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat)
591 {
592 int i1_cost, i2_cost, i3_cost;
593 int new_i2_cost, new_i3_cost;
594 int old_cost, new_cost;
595
596 /* Lookup the original insn_rtx_costs. */
597 i2_cost = INSN_UID (i2) <= last_insn_cost
598 ? uid_insn_cost[INSN_UID (i2)] : 0;
599 i3_cost = INSN_UID (i3) <= last_insn_cost
600 ? uid_insn_cost[INSN_UID (i3)] : 0;
601
602 if (i1)
603 {
604 i1_cost = INSN_UID (i1) <= last_insn_cost
605 ? uid_insn_cost[INSN_UID (i1)] : 0;
606 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
607 ? i1_cost + i2_cost + i3_cost : 0;
608 }
609 else
610 {
611 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
612 i1_cost = 0;
613 }
614
615 /* Calculate the replacement insn_rtx_costs. */
616 new_i3_cost = insn_rtx_cost (newpat);
617 if (newi2pat)
618 {
619 new_i2_cost = insn_rtx_cost (newi2pat);
620 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
621 ? new_i2_cost + new_i3_cost : 0;
622 }
623 else
624 {
625 new_cost = new_i3_cost;
626 new_i2_cost = 0;
627 }
628
629 if (undobuf.other_insn)
630 {
631 int old_other_cost, new_other_cost;
632
633 old_other_cost = (INSN_UID (undobuf.other_insn) <= last_insn_cost
634 ? uid_insn_cost[INSN_UID (undobuf.other_insn)] : 0);
635 new_other_cost = insn_rtx_cost (PATTERN (undobuf.other_insn));
636 if (old_other_cost > 0 && new_other_cost > 0)
637 {
638 old_cost += old_other_cost;
639 new_cost += new_other_cost;
640 }
641 else
642 old_cost = 0;
643 }
644
645 /* Disallow this recombination if both new_cost and old_cost are
646 greater than zero, and new_cost is greater than old cost. */
647 if (old_cost > 0
648 && new_cost > old_cost)
649 {
650 if (dump_file)
651 {
652 if (i1)
653 {
654 fprintf (dump_file,
655 "rejecting combination of insns %d, %d and %d\n",
656 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
657 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
658 i1_cost, i2_cost, i3_cost, old_cost);
659 }
660 else
661 {
662 fprintf (dump_file,
663 "rejecting combination of insns %d and %d\n",
664 INSN_UID (i2), INSN_UID (i3));
665 fprintf (dump_file, "original costs %d + %d = %d\n",
666 i2_cost, i3_cost, old_cost);
667 }
668
669 if (newi2pat)
670 {
671 fprintf (dump_file, "replacement costs %d + %d = %d\n",
672 new_i2_cost, new_i3_cost, new_cost);
673 }
674 else
675 fprintf (dump_file, "replacement cost %d\n", new_cost);
676 }
677
678 return false;
679 }
680
681 /* Update the uid_insn_cost array with the replacement costs. */
682 uid_insn_cost[INSN_UID (i2)] = new_i2_cost;
683 uid_insn_cost[INSN_UID (i3)] = new_i3_cost;
684 if (i1)
685 uid_insn_cost[INSN_UID (i1)] = 0;
686
687 return true;
688 }
689 \f
690 /* Main entry point for combiner. F is the first insn of the function.
691 NREGS is the first unused pseudo-reg number.
692
693 Return nonzero if the combiner has turned an indirect jump
694 instruction into a direct jump. */
695 static int
696 combine_instructions (rtx f, unsigned int nregs)
697 {
698 rtx insn, next;
699 #ifdef HAVE_cc0
700 rtx prev;
701 #endif
702 int i;
703 unsigned int j = 0;
704 rtx links, nextlinks;
705 sbitmap_iterator sbi;
706
707 int new_direct_jump_p = 0;
708
709 combine_attempts = 0;
710 combine_merges = 0;
711 combine_extras = 0;
712 combine_successes = 0;
713
714 combine_max_regno = nregs;
715
716 rtl_hooks = combine_rtl_hooks;
717
718 reg_stat = XCNEWVEC (struct reg_stat, nregs);
719
720 init_recog_no_volatile ();
721
722 /* Compute maximum uid value so uid_cuid can be allocated. */
723
724 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
725 if (INSN_UID (insn) > i)
726 i = INSN_UID (insn);
727
728 uid_cuid = XNEWVEC (int, i + 1);
729 max_uid_cuid = i;
730
731 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
732
733 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
734 problems when, for example, we have j <<= 1 in a loop. */
735
736 nonzero_sign_valid = 0;
737
738 /* Compute the mapping from uids to cuids.
739 Cuids are numbers assigned to insns, like uids,
740 except that cuids increase monotonically through the code.
741
742 Scan all SETs and see if we can deduce anything about what
743 bits are known to be zero for some registers and how many copies
744 of the sign bit are known to exist for those registers.
745
746 Also set any known values so that we can use it while searching
747 for what bits are known to be set. */
748
749 label_tick = 1;
750
751 setup_incoming_promotions ();
752
753 refresh_blocks = sbitmap_alloc (last_basic_block);
754 sbitmap_zero (refresh_blocks);
755
756 /* Allocate array of current insn_rtx_costs. */
757 uid_insn_cost = XCNEWVEC (int, max_uid_cuid + 1);
758 last_insn_cost = max_uid_cuid;
759
760 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
761 {
762 uid_cuid[INSN_UID (insn)] = ++i;
763 subst_low_cuid = i;
764 subst_insn = insn;
765
766 if (INSN_P (insn))
767 {
768 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
769 NULL);
770 record_dead_and_set_regs (insn);
771
772 #ifdef AUTO_INC_DEC
773 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
774 if (REG_NOTE_KIND (links) == REG_INC)
775 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
776 NULL);
777 #endif
778
779 /* Record the current insn_rtx_cost of this instruction. */
780 if (NONJUMP_INSN_P (insn))
781 uid_insn_cost[INSN_UID (insn)] = insn_rtx_cost (PATTERN (insn));
782 if (dump_file)
783 fprintf(dump_file, "insn_cost %d: %d\n",
784 INSN_UID (insn), uid_insn_cost[INSN_UID (insn)]);
785 }
786
787 if (LABEL_P (insn))
788 label_tick++;
789 }
790
791 nonzero_sign_valid = 1;
792
793 /* Now scan all the insns in forward order. */
794
795 label_tick = 1;
796 last_call_cuid = 0;
797 mem_last_set = 0;
798 init_reg_last ();
799 setup_incoming_promotions ();
800
801 FOR_EACH_BB (this_basic_block)
802 {
803 for (insn = BB_HEAD (this_basic_block);
804 insn != NEXT_INSN (BB_END (this_basic_block));
805 insn = next ? next : NEXT_INSN (insn))
806 {
807 next = 0;
808
809 if (LABEL_P (insn))
810 label_tick++;
811
812 else if (INSN_P (insn))
813 {
814 /* See if we know about function return values before this
815 insn based upon SUBREG flags. */
816 check_conversions (insn, PATTERN (insn));
817
818 /* Try this insn with each insn it links back to. */
819
820 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
821 if ((next = try_combine (insn, XEXP (links, 0),
822 NULL_RTX, &new_direct_jump_p)) != 0)
823 goto retry;
824
825 /* Try each sequence of three linked insns ending with this one. */
826
827 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
828 {
829 rtx link = XEXP (links, 0);
830
831 /* If the linked insn has been replaced by a note, then there
832 is no point in pursuing this chain any further. */
833 if (NOTE_P (link))
834 continue;
835
836 for (nextlinks = LOG_LINKS (link);
837 nextlinks;
838 nextlinks = XEXP (nextlinks, 1))
839 if ((next = try_combine (insn, link,
840 XEXP (nextlinks, 0),
841 &new_direct_jump_p)) != 0)
842 goto retry;
843 }
844
845 #ifdef HAVE_cc0
846 /* Try to combine a jump insn that uses CC0
847 with a preceding insn that sets CC0, and maybe with its
848 logical predecessor as well.
849 This is how we make decrement-and-branch insns.
850 We need this special code because data flow connections
851 via CC0 do not get entered in LOG_LINKS. */
852
853 if (JUMP_P (insn)
854 && (prev = prev_nonnote_insn (insn)) != 0
855 && NONJUMP_INSN_P (prev)
856 && sets_cc0_p (PATTERN (prev)))
857 {
858 if ((next = try_combine (insn, prev,
859 NULL_RTX, &new_direct_jump_p)) != 0)
860 goto retry;
861
862 for (nextlinks = LOG_LINKS (prev); nextlinks;
863 nextlinks = XEXP (nextlinks, 1))
864 if ((next = try_combine (insn, prev,
865 XEXP (nextlinks, 0),
866 &new_direct_jump_p)) != 0)
867 goto retry;
868 }
869
870 /* Do the same for an insn that explicitly references CC0. */
871 if (NONJUMP_INSN_P (insn)
872 && (prev = prev_nonnote_insn (insn)) != 0
873 && NONJUMP_INSN_P (prev)
874 && sets_cc0_p (PATTERN (prev))
875 && GET_CODE (PATTERN (insn)) == SET
876 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
877 {
878 if ((next = try_combine (insn, prev,
879 NULL_RTX, &new_direct_jump_p)) != 0)
880 goto retry;
881
882 for (nextlinks = LOG_LINKS (prev); nextlinks;
883 nextlinks = XEXP (nextlinks, 1))
884 if ((next = try_combine (insn, prev,
885 XEXP (nextlinks, 0),
886 &new_direct_jump_p)) != 0)
887 goto retry;
888 }
889
890 /* Finally, see if any of the insns that this insn links to
891 explicitly references CC0. If so, try this insn, that insn,
892 and its predecessor if it sets CC0. */
893 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
894 if (NONJUMP_INSN_P (XEXP (links, 0))
895 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
896 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
897 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
898 && NONJUMP_INSN_P (prev)
899 && sets_cc0_p (PATTERN (prev))
900 && (next = try_combine (insn, XEXP (links, 0),
901 prev, &new_direct_jump_p)) != 0)
902 goto retry;
903 #endif
904
905 /* Try combining an insn with two different insns whose results it
906 uses. */
907 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
908 for (nextlinks = XEXP (links, 1); nextlinks;
909 nextlinks = XEXP (nextlinks, 1))
910 if ((next = try_combine (insn, XEXP (links, 0),
911 XEXP (nextlinks, 0),
912 &new_direct_jump_p)) != 0)
913 goto retry;
914
915 /* Try this insn with each REG_EQUAL note it links back to. */
916 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
917 {
918 rtx set, note;
919 rtx temp = XEXP (links, 0);
920 if ((set = single_set (temp)) != 0
921 && (note = find_reg_equal_equiv_note (temp)) != 0
922 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
923 /* Avoid using a register that may already been marked
924 dead by an earlier instruction. */
925 && ! unmentioned_reg_p (note, SET_SRC (set))
926 && (GET_MODE (note) == VOIDmode
927 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
928 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
929 {
930 /* Temporarily replace the set's source with the
931 contents of the REG_EQUAL note. The insn will
932 be deleted or recognized by try_combine. */
933 rtx orig = SET_SRC (set);
934 SET_SRC (set) = note;
935 replaced_rhs_insn = temp;
936 replaced_rhs_value = copy_rtx (note);
937 next = try_combine (insn, temp, NULL_RTX,
938 &new_direct_jump_p);
939 replaced_rhs_insn = NULL;
940 if (next)
941 goto retry;
942 SET_SRC (set) = orig;
943 }
944 }
945
946 if (!NOTE_P (insn))
947 record_dead_and_set_regs (insn);
948
949 retry:
950 ;
951 }
952 }
953 }
954 clear_bb_flags ();
955
956 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, j, sbi)
957 BASIC_BLOCK (j)->flags |= BB_DIRTY;
958 new_direct_jump_p |= purge_all_dead_edges ();
959 delete_noop_moves ();
960
961 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
962 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
963 | PROP_KILL_DEAD_CODE);
964
965 /* Clean up. */
966 sbitmap_free (refresh_blocks);
967 free (uid_insn_cost);
968 free (reg_stat);
969 free (uid_cuid);
970
971 {
972 struct undo *undo, *next;
973 for (undo = undobuf.frees; undo; undo = next)
974 {
975 next = undo->next;
976 free (undo);
977 }
978 undobuf.frees = 0;
979 }
980
981 total_attempts += combine_attempts;
982 total_merges += combine_merges;
983 total_extras += combine_extras;
984 total_successes += combine_successes;
985
986 nonzero_sign_valid = 0;
987 rtl_hooks = general_rtl_hooks;
988
989 /* Make recognizer allow volatile MEMs again. */
990 init_recog ();
991
992 return new_direct_jump_p;
993 }
994
995 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
996
997 static void
998 init_reg_last (void)
999 {
1000 unsigned int i;
1001 for (i = 0; i < combine_max_regno; i++)
1002 memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
1003 }
1004 \f
1005 /* Set up any promoted values for incoming argument registers. */
1006
1007 static void
1008 setup_incoming_promotions (void)
1009 {
1010 unsigned int regno;
1011 rtx reg;
1012 enum machine_mode mode;
1013 int unsignedp;
1014 rtx first = get_insns ();
1015
1016 if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1017 {
1018 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1019 /* Check whether this register can hold an incoming pointer
1020 argument. FUNCTION_ARG_REGNO_P tests outgoing register
1021 numbers, so translate if necessary due to register windows. */
1022 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
1023 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
1024 {
1025 record_value_for_reg
1026 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
1027 : SIGN_EXTEND),
1028 GET_MODE (reg),
1029 gen_rtx_CLOBBER (mode, const0_rtx)));
1030 }
1031 }
1032 }
1033 \f
1034 /* Called via note_stores. If X is a pseudo that is narrower than
1035 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1036
1037 If we are setting only a portion of X and we can't figure out what
1038 portion, assume all bits will be used since we don't know what will
1039 be happening.
1040
1041 Similarly, set how many bits of X are known to be copies of the sign bit
1042 at all locations in the function. This is the smallest number implied
1043 by any set of X. */
1044
1045 static void
1046 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
1047 void *data ATTRIBUTE_UNUSED)
1048 {
1049 unsigned int num;
1050
1051 if (REG_P (x)
1052 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1053 /* If this register is undefined at the start of the file, we can't
1054 say what its contents were. */
1055 && ! REGNO_REG_SET_P
1056 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start, REGNO (x))
1057 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1058 {
1059 if (set == 0 || GET_CODE (set) == CLOBBER)
1060 {
1061 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1062 reg_stat[REGNO (x)].sign_bit_copies = 1;
1063 return;
1064 }
1065
1066 /* If this is a complex assignment, see if we can convert it into a
1067 simple assignment. */
1068 set = expand_field_assignment (set);
1069
1070 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1071 set what we know about X. */
1072
1073 if (SET_DEST (set) == x
1074 || (GET_CODE (SET_DEST (set)) == SUBREG
1075 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1076 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1077 && SUBREG_REG (SET_DEST (set)) == x))
1078 {
1079 rtx src = SET_SRC (set);
1080
1081 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1082 /* If X is narrower than a word and SRC is a non-negative
1083 constant that would appear negative in the mode of X,
1084 sign-extend it for use in reg_stat[].nonzero_bits because some
1085 machines (maybe most) will actually do the sign-extension
1086 and this is the conservative approach.
1087
1088 ??? For 2.5, try to tighten up the MD files in this regard
1089 instead of this kludge. */
1090
1091 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1092 && GET_CODE (src) == CONST_INT
1093 && INTVAL (src) > 0
1094 && 0 != (INTVAL (src)
1095 & ((HOST_WIDE_INT) 1
1096 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1097 src = GEN_INT (INTVAL (src)
1098 | ((HOST_WIDE_INT) (-1)
1099 << GET_MODE_BITSIZE (GET_MODE (x))));
1100 #endif
1101
1102 /* Don't call nonzero_bits if it cannot change anything. */
1103 if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1104 reg_stat[REGNO (x)].nonzero_bits
1105 |= nonzero_bits (src, nonzero_bits_mode);
1106 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1107 if (reg_stat[REGNO (x)].sign_bit_copies == 0
1108 || reg_stat[REGNO (x)].sign_bit_copies > num)
1109 reg_stat[REGNO (x)].sign_bit_copies = num;
1110 }
1111 else
1112 {
1113 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1114 reg_stat[REGNO (x)].sign_bit_copies = 1;
1115 }
1116 }
1117 }
1118 \f
1119 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1120 insns that were previously combined into I3 or that will be combined
1121 into the merger of INSN and I3.
1122
1123 Return 0 if the combination is not allowed for any reason.
1124
1125 If the combination is allowed, *PDEST will be set to the single
1126 destination of INSN and *PSRC to the single source, and this function
1127 will return 1. */
1128
1129 static int
1130 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1131 rtx *pdest, rtx *psrc)
1132 {
1133 int i;
1134 rtx set = 0, src, dest;
1135 rtx p;
1136 #ifdef AUTO_INC_DEC
1137 rtx link;
1138 #endif
1139 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1140 && next_active_insn (succ) == i3)
1141 : next_active_insn (insn) == i3);
1142
1143 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1144 or a PARALLEL consisting of such a SET and CLOBBERs.
1145
1146 If INSN has CLOBBER parallel parts, ignore them for our processing.
1147 By definition, these happen during the execution of the insn. When it
1148 is merged with another insn, all bets are off. If they are, in fact,
1149 needed and aren't also supplied in I3, they may be added by
1150 recog_for_combine. Otherwise, it won't match.
1151
1152 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1153 note.
1154
1155 Get the source and destination of INSN. If more than one, can't
1156 combine. */
1157
1158 if (GET_CODE (PATTERN (insn)) == SET)
1159 set = PATTERN (insn);
1160 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1161 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1162 {
1163 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1164 {
1165 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1166 rtx note;
1167
1168 switch (GET_CODE (elt))
1169 {
1170 /* This is important to combine floating point insns
1171 for the SH4 port. */
1172 case USE:
1173 /* Combining an isolated USE doesn't make sense.
1174 We depend here on combinable_i3pat to reject them. */
1175 /* The code below this loop only verifies that the inputs of
1176 the SET in INSN do not change. We call reg_set_between_p
1177 to verify that the REG in the USE does not change between
1178 I3 and INSN.
1179 If the USE in INSN was for a pseudo register, the matching
1180 insn pattern will likely match any register; combining this
1181 with any other USE would only be safe if we knew that the
1182 used registers have identical values, or if there was
1183 something to tell them apart, e.g. different modes. For
1184 now, we forgo such complicated tests and simply disallow
1185 combining of USES of pseudo registers with any other USE. */
1186 if (REG_P (XEXP (elt, 0))
1187 && GET_CODE (PATTERN (i3)) == PARALLEL)
1188 {
1189 rtx i3pat = PATTERN (i3);
1190 int i = XVECLEN (i3pat, 0) - 1;
1191 unsigned int regno = REGNO (XEXP (elt, 0));
1192
1193 do
1194 {
1195 rtx i3elt = XVECEXP (i3pat, 0, i);
1196
1197 if (GET_CODE (i3elt) == USE
1198 && REG_P (XEXP (i3elt, 0))
1199 && (REGNO (XEXP (i3elt, 0)) == regno
1200 ? reg_set_between_p (XEXP (elt, 0),
1201 PREV_INSN (insn), i3)
1202 : regno >= FIRST_PSEUDO_REGISTER))
1203 return 0;
1204 }
1205 while (--i >= 0);
1206 }
1207 break;
1208
1209 /* We can ignore CLOBBERs. */
1210 case CLOBBER:
1211 break;
1212
1213 case SET:
1214 /* Ignore SETs whose result isn't used but not those that
1215 have side-effects. */
1216 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1217 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1218 || INTVAL (XEXP (note, 0)) <= 0)
1219 && ! side_effects_p (elt))
1220 break;
1221
1222 /* If we have already found a SET, this is a second one and
1223 so we cannot combine with this insn. */
1224 if (set)
1225 return 0;
1226
1227 set = elt;
1228 break;
1229
1230 default:
1231 /* Anything else means we can't combine. */
1232 return 0;
1233 }
1234 }
1235
1236 if (set == 0
1237 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1238 so don't do anything with it. */
1239 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1240 return 0;
1241 }
1242 else
1243 return 0;
1244
1245 if (set == 0)
1246 return 0;
1247
1248 set = expand_field_assignment (set);
1249 src = SET_SRC (set), dest = SET_DEST (set);
1250
1251 /* Don't eliminate a store in the stack pointer. */
1252 if (dest == stack_pointer_rtx
1253 /* Don't combine with an insn that sets a register to itself if it has
1254 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1255 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1256 /* Can't merge an ASM_OPERANDS. */
1257 || GET_CODE (src) == ASM_OPERANDS
1258 /* Can't merge a function call. */
1259 || GET_CODE (src) == CALL
1260 /* Don't eliminate a function call argument. */
1261 || (CALL_P (i3)
1262 && (find_reg_fusage (i3, USE, dest)
1263 || (REG_P (dest)
1264 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1265 && global_regs[REGNO (dest)])))
1266 /* Don't substitute into an incremented register. */
1267 || FIND_REG_INC_NOTE (i3, dest)
1268 || (succ && FIND_REG_INC_NOTE (succ, dest))
1269 /* Don't substitute into a non-local goto, this confuses CFG. */
1270 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1271 #if 0
1272 /* Don't combine the end of a libcall into anything. */
1273 /* ??? This gives worse code, and appears to be unnecessary, since no
1274 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1275 use REG_RETVAL notes for noconflict blocks, but other code here
1276 makes sure that those insns don't disappear. */
1277 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1278 #endif
1279 /* Make sure that DEST is not used after SUCC but before I3. */
1280 || (succ && ! all_adjacent
1281 && reg_used_between_p (dest, succ, i3))
1282 /* Make sure that the value that is to be substituted for the register
1283 does not use any registers whose values alter in between. However,
1284 If the insns are adjacent, a use can't cross a set even though we
1285 think it might (this can happen for a sequence of insns each setting
1286 the same destination; last_set of that register might point to
1287 a NOTE). If INSN has a REG_EQUIV note, the register is always
1288 equivalent to the memory so the substitution is valid even if there
1289 are intervening stores. Also, don't move a volatile asm or
1290 UNSPEC_VOLATILE across any other insns. */
1291 || (! all_adjacent
1292 && (((!MEM_P (src)
1293 || ! find_reg_note (insn, REG_EQUIV, src))
1294 && use_crosses_set_p (src, INSN_CUID (insn)))
1295 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1296 || GET_CODE (src) == UNSPEC_VOLATILE))
1297 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1298 better register allocation by not doing the combine. */
1299 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1300 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1301 /* Don't combine across a CALL_INSN, because that would possibly
1302 change whether the life span of some REGs crosses calls or not,
1303 and it is a pain to update that information.
1304 Exception: if source is a constant, moving it later can't hurt.
1305 Accept that special case, because it helps -fforce-addr a lot. */
1306 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1307 return 0;
1308
1309 /* DEST must either be a REG or CC0. */
1310 if (REG_P (dest))
1311 {
1312 /* If register alignment is being enforced for multi-word items in all
1313 cases except for parameters, it is possible to have a register copy
1314 insn referencing a hard register that is not allowed to contain the
1315 mode being copied and which would not be valid as an operand of most
1316 insns. Eliminate this problem by not combining with such an insn.
1317
1318 Also, on some machines we don't want to extend the life of a hard
1319 register. */
1320
1321 if (REG_P (src)
1322 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1323 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1324 /* Don't extend the life of a hard register unless it is
1325 user variable (if we have few registers) or it can't
1326 fit into the desired register (meaning something special
1327 is going on).
1328 Also avoid substituting a return register into I3, because
1329 reload can't handle a conflict with constraints of other
1330 inputs. */
1331 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1332 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1333 return 0;
1334 }
1335 else if (GET_CODE (dest) != CC0)
1336 return 0;
1337
1338
1339 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1340 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1341 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1342 {
1343 /* Don't substitute for a register intended as a clobberable
1344 operand. */
1345 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1346 if (rtx_equal_p (reg, dest))
1347 return 0;
1348
1349 /* If the clobber represents an earlyclobber operand, we must not
1350 substitute an expression containing the clobbered register.
1351 As we do not analyze the constraint strings here, we have to
1352 make the conservative assumption. However, if the register is
1353 a fixed hard reg, the clobber cannot represent any operand;
1354 we leave it up to the machine description to either accept or
1355 reject use-and-clobber patterns. */
1356 if (!REG_P (reg)
1357 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1358 || !fixed_regs[REGNO (reg)])
1359 if (reg_overlap_mentioned_p (reg, src))
1360 return 0;
1361 }
1362
1363 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1364 or not), reject, unless nothing volatile comes between it and I3 */
1365
1366 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1367 {
1368 /* Make sure succ doesn't contain a volatile reference. */
1369 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1370 return 0;
1371
1372 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1373 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1374 return 0;
1375 }
1376
1377 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1378 to be an explicit register variable, and was chosen for a reason. */
1379
1380 if (GET_CODE (src) == ASM_OPERANDS
1381 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1382 return 0;
1383
1384 /* If there are any volatile insns between INSN and I3, reject, because
1385 they might affect machine state. */
1386
1387 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1388 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1389 return 0;
1390
1391 /* If INSN contains an autoincrement or autodecrement, make sure that
1392 register is not used between there and I3, and not already used in
1393 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1394 Also insist that I3 not be a jump; if it were one
1395 and the incremented register were spilled, we would lose. */
1396
1397 #ifdef AUTO_INC_DEC
1398 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1399 if (REG_NOTE_KIND (link) == REG_INC
1400 && (JUMP_P (i3)
1401 || reg_used_between_p (XEXP (link, 0), insn, i3)
1402 || (pred != NULL_RTX
1403 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1404 || (succ != NULL_RTX
1405 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1406 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1407 return 0;
1408 #endif
1409
1410 #ifdef HAVE_cc0
1411 /* Don't combine an insn that follows a CC0-setting insn.
1412 An insn that uses CC0 must not be separated from the one that sets it.
1413 We do, however, allow I2 to follow a CC0-setting insn if that insn
1414 is passed as I1; in that case it will be deleted also.
1415 We also allow combining in this case if all the insns are adjacent
1416 because that would leave the two CC0 insns adjacent as well.
1417 It would be more logical to test whether CC0 occurs inside I1 or I2,
1418 but that would be much slower, and this ought to be equivalent. */
1419
1420 p = prev_nonnote_insn (insn);
1421 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1422 && ! all_adjacent)
1423 return 0;
1424 #endif
1425
1426 /* If we get here, we have passed all the tests and the combination is
1427 to be allowed. */
1428
1429 *pdest = dest;
1430 *psrc = src;
1431
1432 return 1;
1433 }
1434 \f
1435 /* LOC is the location within I3 that contains its pattern or the component
1436 of a PARALLEL of the pattern. We validate that it is valid for combining.
1437
1438 One problem is if I3 modifies its output, as opposed to replacing it
1439 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1440 so would produce an insn that is not equivalent to the original insns.
1441
1442 Consider:
1443
1444 (set (reg:DI 101) (reg:DI 100))
1445 (set (subreg:SI (reg:DI 101) 0) <foo>)
1446
1447 This is NOT equivalent to:
1448
1449 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1450 (set (reg:DI 101) (reg:DI 100))])
1451
1452 Not only does this modify 100 (in which case it might still be valid
1453 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1454
1455 We can also run into a problem if I2 sets a register that I1
1456 uses and I1 gets directly substituted into I3 (not via I2). In that
1457 case, we would be getting the wrong value of I2DEST into I3, so we
1458 must reject the combination. This case occurs when I2 and I1 both
1459 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1460 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1461 of a SET must prevent combination from occurring.
1462
1463 Before doing the above check, we first try to expand a field assignment
1464 into a set of logical operations.
1465
1466 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1467 we place a register that is both set and used within I3. If more than one
1468 such register is detected, we fail.
1469
1470 Return 1 if the combination is valid, zero otherwise. */
1471
1472 static int
1473 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1474 int i1_not_in_src, rtx *pi3dest_killed)
1475 {
1476 rtx x = *loc;
1477
1478 if (GET_CODE (x) == SET)
1479 {
1480 rtx set = x ;
1481 rtx dest = SET_DEST (set);
1482 rtx src = SET_SRC (set);
1483 rtx inner_dest = dest;
1484 rtx subdest;
1485
1486 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1487 || GET_CODE (inner_dest) == SUBREG
1488 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1489 inner_dest = XEXP (inner_dest, 0);
1490
1491 /* Check for the case where I3 modifies its output, as discussed
1492 above. We don't want to prevent pseudos from being combined
1493 into the address of a MEM, so only prevent the combination if
1494 i1 or i2 set the same MEM. */
1495 if ((inner_dest != dest &&
1496 (!MEM_P (inner_dest)
1497 || rtx_equal_p (i2dest, inner_dest)
1498 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1499 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1500 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1501
1502 /* This is the same test done in can_combine_p except we can't test
1503 all_adjacent; we don't have to, since this instruction will stay
1504 in place, thus we are not considering increasing the lifetime of
1505 INNER_DEST.
1506
1507 Also, if this insn sets a function argument, combining it with
1508 something that might need a spill could clobber a previous
1509 function argument; the all_adjacent test in can_combine_p also
1510 checks this; here, we do a more specific test for this case. */
1511
1512 || (REG_P (inner_dest)
1513 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1514 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1515 GET_MODE (inner_dest))))
1516 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1517 return 0;
1518
1519 /* If DEST is used in I3, it is being killed in this insn, so
1520 record that for later. We have to consider paradoxical
1521 subregs here, since they kill the whole register, but we
1522 ignore partial subregs, STRICT_LOW_PART, etc.
1523 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1524 STACK_POINTER_REGNUM, since these are always considered to be
1525 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1526 subdest = dest;
1527 if (GET_CODE (subdest) == SUBREG
1528 && (GET_MODE_SIZE (GET_MODE (subdest))
1529 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1530 subdest = SUBREG_REG (subdest);
1531 if (pi3dest_killed
1532 && REG_P (subdest)
1533 && reg_referenced_p (subdest, PATTERN (i3))
1534 && REGNO (subdest) != FRAME_POINTER_REGNUM
1535 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1536 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1537 #endif
1538 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1539 && (REGNO (subdest) != ARG_POINTER_REGNUM
1540 || ! fixed_regs [REGNO (subdest)])
1541 #endif
1542 && REGNO (subdest) != STACK_POINTER_REGNUM)
1543 {
1544 if (*pi3dest_killed)
1545 return 0;
1546
1547 *pi3dest_killed = subdest;
1548 }
1549 }
1550
1551 else if (GET_CODE (x) == PARALLEL)
1552 {
1553 int i;
1554
1555 for (i = 0; i < XVECLEN (x, 0); i++)
1556 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1557 i1_not_in_src, pi3dest_killed))
1558 return 0;
1559 }
1560
1561 return 1;
1562 }
1563 \f
1564 /* Return 1 if X is an arithmetic expression that contains a multiplication
1565 and division. We don't count multiplications by powers of two here. */
1566
1567 static int
1568 contains_muldiv (rtx x)
1569 {
1570 switch (GET_CODE (x))
1571 {
1572 case MOD: case DIV: case UMOD: case UDIV:
1573 return 1;
1574
1575 case MULT:
1576 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1577 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1578 default:
1579 if (BINARY_P (x))
1580 return contains_muldiv (XEXP (x, 0))
1581 || contains_muldiv (XEXP (x, 1));
1582
1583 if (UNARY_P (x))
1584 return contains_muldiv (XEXP (x, 0));
1585
1586 return 0;
1587 }
1588 }
1589 \f
1590 /* Determine whether INSN can be used in a combination. Return nonzero if
1591 not. This is used in try_combine to detect early some cases where we
1592 can't perform combinations. */
1593
1594 static int
1595 cant_combine_insn_p (rtx insn)
1596 {
1597 rtx set;
1598 rtx src, dest;
1599
1600 /* If this isn't really an insn, we can't do anything.
1601 This can occur when flow deletes an insn that it has merged into an
1602 auto-increment address. */
1603 if (! INSN_P (insn))
1604 return 1;
1605
1606 /* Never combine loads and stores involving hard regs that are likely
1607 to be spilled. The register allocator can usually handle such
1608 reg-reg moves by tying. If we allow the combiner to make
1609 substitutions of likely-spilled regs, reload might die.
1610 As an exception, we allow combinations involving fixed regs; these are
1611 not available to the register allocator so there's no risk involved. */
1612
1613 set = single_set (insn);
1614 if (! set)
1615 return 0;
1616 src = SET_SRC (set);
1617 dest = SET_DEST (set);
1618 if (GET_CODE (src) == SUBREG)
1619 src = SUBREG_REG (src);
1620 if (GET_CODE (dest) == SUBREG)
1621 dest = SUBREG_REG (dest);
1622 if (REG_P (src) && REG_P (dest)
1623 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1624 && ! fixed_regs[REGNO (src)]
1625 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1626 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1627 && ! fixed_regs[REGNO (dest)]
1628 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1629 return 1;
1630
1631 return 0;
1632 }
1633
1634 struct likely_spilled_retval_info
1635 {
1636 unsigned regno, nregs;
1637 unsigned mask;
1638 };
1639
1640 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
1641 hard registers that are known to be written to / clobbered in full. */
1642 static void
1643 likely_spilled_retval_1 (rtx x, rtx set, void *data)
1644 {
1645 struct likely_spilled_retval_info *info = data;
1646 unsigned regno, nregs;
1647 unsigned new_mask;
1648
1649 if (!REG_P (XEXP (set, 0)))
1650 return;
1651 regno = REGNO (x);
1652 if (regno >= info->regno + info->nregs)
1653 return;
1654 nregs = hard_regno_nregs[regno][GET_MODE (x)];
1655 if (regno + nregs <= info->regno)
1656 return;
1657 new_mask = (2U << (nregs - 1)) - 1;
1658 if (regno < info->regno)
1659 new_mask >>= info->regno - regno;
1660 else
1661 new_mask <<= regno - info->regno;
1662 info->mask &= ~new_mask;
1663 }
1664
1665 /* Return nonzero iff part of the return value is live during INSN, and
1666 it is likely spilled. This can happen when more than one insn is needed
1667 to copy the return value, e.g. when we consider to combine into the
1668 second copy insn for a complex value. */
1669
1670 static int
1671 likely_spilled_retval_p (rtx insn)
1672 {
1673 rtx use = BB_END (this_basic_block);
1674 rtx reg, p;
1675 unsigned regno, nregs;
1676 /* We assume here that no machine mode needs more than
1677 32 hard registers when the value overlaps with a register
1678 for which FUNCTION_VALUE_REGNO_P is true. */
1679 unsigned mask;
1680 struct likely_spilled_retval_info info;
1681
1682 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
1683 return 0;
1684 reg = XEXP (PATTERN (use), 0);
1685 if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
1686 return 0;
1687 regno = REGNO (reg);
1688 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
1689 if (nregs == 1)
1690 return 0;
1691 mask = (2U << (nregs - 1)) - 1;
1692
1693 /* Disregard parts of the return value that are set later. */
1694 info.regno = regno;
1695 info.nregs = nregs;
1696 info.mask = mask;
1697 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
1698 if (INSN_P (p))
1699 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
1700 mask = info.mask;
1701
1702 /* Check if any of the (probably) live return value registers is
1703 likely spilled. */
1704 nregs --;
1705 do
1706 {
1707 if ((mask & 1 << nregs)
1708 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
1709 return 1;
1710 } while (nregs--);
1711 return 0;
1712 }
1713
1714 /* Adjust INSN after we made a change to its destination.
1715
1716 Changing the destination can invalidate notes that say something about
1717 the results of the insn and a LOG_LINK pointing to the insn. */
1718
1719 static void
1720 adjust_for_new_dest (rtx insn)
1721 {
1722 rtx *loc;
1723
1724 /* For notes, be conservative and simply remove them. */
1725 loc = &REG_NOTES (insn);
1726 while (*loc)
1727 {
1728 enum reg_note kind = REG_NOTE_KIND (*loc);
1729 if (kind == REG_EQUAL || kind == REG_EQUIV)
1730 *loc = XEXP (*loc, 1);
1731 else
1732 loc = &XEXP (*loc, 1);
1733 }
1734
1735 /* The new insn will have a destination that was previously the destination
1736 of an insn just above it. Call distribute_links to make a LOG_LINK from
1737 the next use of that destination. */
1738 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1739 }
1740
1741 /* Return TRUE if combine can reuse reg X in mode MODE.
1742 ADDED_SETS is nonzero if the original set is still required. */
1743 static bool
1744 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
1745 {
1746 unsigned int regno;
1747
1748 if (!REG_P(x))
1749 return false;
1750
1751 regno = REGNO (x);
1752 /* Allow hard registers if the new mode is legal, and occupies no more
1753 registers than the old mode. */
1754 if (regno < FIRST_PSEUDO_REGISTER)
1755 return (HARD_REGNO_MODE_OK (regno, mode)
1756 && (hard_regno_nregs[regno][GET_MODE (x)]
1757 >= hard_regno_nregs[regno][mode]));
1758
1759 /* Or a pseudo that is only used once. */
1760 return (REG_N_SETS (regno) == 1 && !added_sets
1761 && !REG_USERVAR_P (x));
1762 }
1763
1764
1765 /* Check whether X, the destination of a set, refers to part of
1766 the register specified by REG. */
1767
1768 static bool
1769 reg_subword_p (rtx x, rtx reg)
1770 {
1771 /* Check that reg is an integer mode register. */
1772 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
1773 return false;
1774
1775 if (GET_CODE (x) == STRICT_LOW_PART
1776 || GET_CODE (x) == ZERO_EXTRACT)
1777 x = XEXP (x, 0);
1778
1779 return GET_CODE (x) == SUBREG
1780 && SUBREG_REG (x) == reg
1781 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
1782 }
1783
1784
1785 /* Try to combine the insns I1 and I2 into I3.
1786 Here I1 and I2 appear earlier than I3.
1787 I1 can be zero; then we combine just I2 into I3.
1788
1789 If we are combining three insns and the resulting insn is not recognized,
1790 try splitting it into two insns. If that happens, I2 and I3 are retained
1791 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1792 are pseudo-deleted.
1793
1794 Return 0 if the combination does not work. Then nothing is changed.
1795 If we did the combination, return the insn at which combine should
1796 resume scanning.
1797
1798 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1799 new direct jump instruction. */
1800
1801 static rtx
1802 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1803 {
1804 /* New patterns for I3 and I2, respectively. */
1805 rtx newpat, newi2pat = 0;
1806 rtvec newpat_vec_with_clobbers = 0;
1807 int substed_i2 = 0, substed_i1 = 0;
1808 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1809 int added_sets_1, added_sets_2;
1810 /* Total number of SETs to put into I3. */
1811 int total_sets;
1812 /* Nonzero if I2's body now appears in I3. */
1813 int i2_is_used;
1814 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1815 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1816 /* Contains I3 if the destination of I3 is used in its source, which means
1817 that the old life of I3 is being killed. If that usage is placed into
1818 I2 and not in I3, a REG_DEAD note must be made. */
1819 rtx i3dest_killed = 0;
1820 /* SET_DEST and SET_SRC of I2 and I1. */
1821 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1822 /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases. */
1823 rtx i1pat = 0, i2pat = 0;
1824 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1825 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1826 int i2dest_killed = 0, i1dest_killed = 0;
1827 int i1_feeds_i3 = 0;
1828 /* Notes that must be added to REG_NOTES in I3 and I2. */
1829 rtx new_i3_notes, new_i2_notes;
1830 /* Notes that we substituted I3 into I2 instead of the normal case. */
1831 int i3_subst_into_i2 = 0;
1832 /* Notes that I1, I2 or I3 is a MULT operation. */
1833 int have_mult = 0;
1834 int swap_i2i3 = 0;
1835
1836 int maxreg;
1837 rtx temp;
1838 rtx link;
1839 int i;
1840
1841 /* Exit early if one of the insns involved can't be used for
1842 combinations. */
1843 if (cant_combine_insn_p (i3)
1844 || cant_combine_insn_p (i2)
1845 || (i1 && cant_combine_insn_p (i1))
1846 || likely_spilled_retval_p (i3)
1847 /* We also can't do anything if I3 has a
1848 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1849 libcall. */
1850 #if 0
1851 /* ??? This gives worse code, and appears to be unnecessary, since no
1852 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1853 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1854 #endif
1855 )
1856 return 0;
1857
1858 combine_attempts++;
1859 undobuf.other_insn = 0;
1860
1861 /* Reset the hard register usage information. */
1862 CLEAR_HARD_REG_SET (newpat_used_regs);
1863
1864 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1865 code below, set I1 to be the earlier of the two insns. */
1866 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1867 temp = i1, i1 = i2, i2 = temp;
1868
1869 added_links_insn = 0;
1870
1871 /* First check for one important special-case that the code below will
1872 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1873 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1874 we may be able to replace that destination with the destination of I3.
1875 This occurs in the common code where we compute both a quotient and
1876 remainder into a structure, in which case we want to do the computation
1877 directly into the structure to avoid register-register copies.
1878
1879 Note that this case handles both multiple sets in I2 and also
1880 cases where I2 has a number of CLOBBER or PARALLELs.
1881
1882 We make very conservative checks below and only try to handle the
1883 most common cases of this. For example, we only handle the case
1884 where I2 and I3 are adjacent to avoid making difficult register
1885 usage tests. */
1886
1887 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
1888 && REG_P (SET_SRC (PATTERN (i3)))
1889 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1890 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1891 && GET_CODE (PATTERN (i2)) == PARALLEL
1892 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1893 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1894 below would need to check what is inside (and reg_overlap_mentioned_p
1895 doesn't support those codes anyway). Don't allow those destinations;
1896 the resulting insn isn't likely to be recognized anyway. */
1897 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1898 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1899 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1900 SET_DEST (PATTERN (i3)))
1901 && next_real_insn (i2) == i3)
1902 {
1903 rtx p2 = PATTERN (i2);
1904
1905 /* Make sure that the destination of I3,
1906 which we are going to substitute into one output of I2,
1907 is not used within another output of I2. We must avoid making this:
1908 (parallel [(set (mem (reg 69)) ...)
1909 (set (reg 69) ...)])
1910 which is not well-defined as to order of actions.
1911 (Besides, reload can't handle output reloads for this.)
1912
1913 The problem can also happen if the dest of I3 is a memory ref,
1914 if another dest in I2 is an indirect memory ref. */
1915 for (i = 0; i < XVECLEN (p2, 0); i++)
1916 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1917 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1918 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1919 SET_DEST (XVECEXP (p2, 0, i))))
1920 break;
1921
1922 if (i == XVECLEN (p2, 0))
1923 for (i = 0; i < XVECLEN (p2, 0); i++)
1924 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1925 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1926 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1927 {
1928 combine_merges++;
1929
1930 subst_insn = i3;
1931 subst_low_cuid = INSN_CUID (i2);
1932
1933 added_sets_2 = added_sets_1 = 0;
1934 i2dest = SET_SRC (PATTERN (i3));
1935 i2dest_killed = dead_or_set_p (i2, i2dest);
1936
1937 /* Replace the dest in I2 with our dest and make the resulting
1938 insn the new pattern for I3. Then skip to where we
1939 validate the pattern. Everything was set up above. */
1940 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1941 SET_DEST (PATTERN (i3)));
1942
1943 newpat = p2;
1944 i3_subst_into_i2 = 1;
1945 goto validate_replacement;
1946 }
1947 }
1948
1949 /* If I2 is setting a pseudo to a constant and I3 is setting some
1950 sub-part of it to another constant, merge them by making a new
1951 constant. */
1952 if (i1 == 0
1953 && (temp = single_set (i2)) != 0
1954 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1955 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1956 && GET_CODE (PATTERN (i3)) == SET
1957 && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
1958 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
1959 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
1960 {
1961 rtx dest = SET_DEST (PATTERN (i3));
1962 int offset = -1;
1963 int width = 0;
1964
1965 if (GET_CODE (dest) == ZERO_EXTRACT)
1966 {
1967 if (GET_CODE (XEXP (dest, 1)) == CONST_INT
1968 && GET_CODE (XEXP (dest, 2)) == CONST_INT)
1969 {
1970 width = INTVAL (XEXP (dest, 1));
1971 offset = INTVAL (XEXP (dest, 2));
1972 dest = XEXP (dest, 0);
1973 if (BITS_BIG_ENDIAN)
1974 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
1975 }
1976 }
1977 else
1978 {
1979 if (GET_CODE (dest) == STRICT_LOW_PART)
1980 dest = XEXP (dest, 0);
1981 width = GET_MODE_BITSIZE (GET_MODE (dest));
1982 offset = 0;
1983 }
1984
1985 if (offset >= 0)
1986 {
1987 /* If this is the low part, we're done. */
1988 if (subreg_lowpart_p (dest))
1989 ;
1990 /* Handle the case where inner is twice the size of outer. */
1991 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
1992 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
1993 offset += GET_MODE_BITSIZE (GET_MODE (dest));
1994 /* Otherwise give up for now. */
1995 else
1996 offset = -1;
1997 }
1998
1999 if (offset >= 0)
2000 {
2001 HOST_WIDE_INT mhi, ohi, ihi;
2002 HOST_WIDE_INT mlo, olo, ilo;
2003 rtx inner = SET_SRC (PATTERN (i3));
2004 rtx outer = SET_SRC (temp);
2005
2006 if (GET_CODE (outer) == CONST_INT)
2007 {
2008 olo = INTVAL (outer);
2009 ohi = olo < 0 ? -1 : 0;
2010 }
2011 else
2012 {
2013 olo = CONST_DOUBLE_LOW (outer);
2014 ohi = CONST_DOUBLE_HIGH (outer);
2015 }
2016
2017 if (GET_CODE (inner) == CONST_INT)
2018 {
2019 ilo = INTVAL (inner);
2020 ihi = ilo < 0 ? -1 : 0;
2021 }
2022 else
2023 {
2024 ilo = CONST_DOUBLE_LOW (inner);
2025 ihi = CONST_DOUBLE_HIGH (inner);
2026 }
2027
2028 if (width < HOST_BITS_PER_WIDE_INT)
2029 {
2030 mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2031 mhi = 0;
2032 }
2033 else if (width < HOST_BITS_PER_WIDE_INT * 2)
2034 {
2035 mhi = ((unsigned HOST_WIDE_INT) 1
2036 << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2037 mlo = -1;
2038 }
2039 else
2040 {
2041 mlo = -1;
2042 mhi = -1;
2043 }
2044
2045 ilo &= mlo;
2046 ihi &= mhi;
2047
2048 if (offset >= HOST_BITS_PER_WIDE_INT)
2049 {
2050 mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2051 mlo = 0;
2052 ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2053 ilo = 0;
2054 }
2055 else if (offset > 0)
2056 {
2057 mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2058 >> (HOST_BITS_PER_WIDE_INT - offset));
2059 mlo = mlo << offset;
2060 ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2061 >> (HOST_BITS_PER_WIDE_INT - offset));
2062 ilo = ilo << offset;
2063 }
2064
2065 olo = (olo & ~mlo) | ilo;
2066 ohi = (ohi & ~mhi) | ihi;
2067
2068 combine_merges++;
2069 subst_insn = i3;
2070 subst_low_cuid = INSN_CUID (i2);
2071 added_sets_2 = added_sets_1 = 0;
2072 i2dest = SET_DEST (temp);
2073 i2dest_killed = dead_or_set_p (i2, i2dest);
2074
2075 SUBST (SET_SRC (temp),
2076 immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2077
2078 newpat = PATTERN (i2);
2079 goto validate_replacement;
2080 }
2081 }
2082
2083 #ifndef HAVE_cc0
2084 /* If we have no I1 and I2 looks like:
2085 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2086 (set Y OP)])
2087 make up a dummy I1 that is
2088 (set Y OP)
2089 and change I2 to be
2090 (set (reg:CC X) (compare:CC Y (const_int 0)))
2091
2092 (We can ignore any trailing CLOBBERs.)
2093
2094 This undoes a previous combination and allows us to match a branch-and-
2095 decrement insn. */
2096
2097 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2098 && XVECLEN (PATTERN (i2), 0) >= 2
2099 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2100 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2101 == MODE_CC)
2102 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2103 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2104 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2105 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2106 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2107 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2108 {
2109 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2110 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2111 break;
2112
2113 if (i == 1)
2114 {
2115 /* We make I1 with the same INSN_UID as I2. This gives it
2116 the same INSN_CUID for value tracking. Our fake I1 will
2117 never appear in the insn stream so giving it the same INSN_UID
2118 as I2 will not cause a problem. */
2119
2120 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2121 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2122 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
2123 NULL_RTX);
2124
2125 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2126 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2127 SET_DEST (PATTERN (i1)));
2128 }
2129 }
2130 #endif
2131
2132 /* Verify that I2 and I1 are valid for combining. */
2133 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2134 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2135 {
2136 undo_all ();
2137 return 0;
2138 }
2139
2140 /* Record whether I2DEST is used in I2SRC and similarly for the other
2141 cases. Knowing this will help in register status updating below. */
2142 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2143 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2144 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2145 i2dest_killed = dead_or_set_p (i2, i2dest);
2146 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2147
2148 /* See if I1 directly feeds into I3. It does if I1DEST is not used
2149 in I2SRC. */
2150 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2151
2152 /* Ensure that I3's pattern can be the destination of combines. */
2153 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2154 i1 && i2dest_in_i1src && i1_feeds_i3,
2155 &i3dest_killed))
2156 {
2157 undo_all ();
2158 return 0;
2159 }
2160
2161 /* See if any of the insns is a MULT operation. Unless one is, we will
2162 reject a combination that is, since it must be slower. Be conservative
2163 here. */
2164 if (GET_CODE (i2src) == MULT
2165 || (i1 != 0 && GET_CODE (i1src) == MULT)
2166 || (GET_CODE (PATTERN (i3)) == SET
2167 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2168 have_mult = 1;
2169
2170 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2171 We used to do this EXCEPT in one case: I3 has a post-inc in an
2172 output operand. However, that exception can give rise to insns like
2173 mov r3,(r3)+
2174 which is a famous insn on the PDP-11 where the value of r3 used as the
2175 source was model-dependent. Avoid this sort of thing. */
2176
2177 #if 0
2178 if (!(GET_CODE (PATTERN (i3)) == SET
2179 && REG_P (SET_SRC (PATTERN (i3)))
2180 && MEM_P (SET_DEST (PATTERN (i3)))
2181 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2182 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2183 /* It's not the exception. */
2184 #endif
2185 #ifdef AUTO_INC_DEC
2186 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2187 if (REG_NOTE_KIND (link) == REG_INC
2188 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2189 || (i1 != 0
2190 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2191 {
2192 undo_all ();
2193 return 0;
2194 }
2195 #endif
2196
2197 /* See if the SETs in I1 or I2 need to be kept around in the merged
2198 instruction: whenever the value set there is still needed past I3.
2199 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2200
2201 For the SET in I1, we have two cases: If I1 and I2 independently
2202 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2203 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2204 in I1 needs to be kept around unless I1DEST dies or is set in either
2205 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2206 I1DEST. If so, we know I1 feeds into I2. */
2207
2208 added_sets_2 = ! dead_or_set_p (i3, i2dest);
2209
2210 added_sets_1
2211 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2212 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2213
2214 /* If the set in I2 needs to be kept around, we must make a copy of
2215 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2216 PATTERN (I2), we are only substituting for the original I1DEST, not into
2217 an already-substituted copy. This also prevents making self-referential
2218 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2219 I2DEST. */
2220
2221 if (added_sets_2)
2222 {
2223 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2224 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2225 else
2226 i2pat = copy_rtx (PATTERN (i2));
2227 }
2228
2229 if (added_sets_1)
2230 {
2231 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2232 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2233 else
2234 i1pat = copy_rtx (PATTERN (i1));
2235 }
2236
2237 combine_merges++;
2238
2239 /* Substitute in the latest insn for the regs set by the earlier ones. */
2240
2241 maxreg = max_reg_num ();
2242
2243 subst_insn = i3;
2244
2245 #ifndef HAVE_cc0
2246 /* Many machines that don't use CC0 have insns that can both perform an
2247 arithmetic operation and set the condition code. These operations will
2248 be represented as a PARALLEL with the first element of the vector
2249 being a COMPARE of an arithmetic operation with the constant zero.
2250 The second element of the vector will set some pseudo to the result
2251 of the same arithmetic operation. If we simplify the COMPARE, we won't
2252 match such a pattern and so will generate an extra insn. Here we test
2253 for this case, where both the comparison and the operation result are
2254 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2255 I2SRC. Later we will make the PARALLEL that contains I2. */
2256
2257 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2258 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2259 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2260 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2261 {
2262 #ifdef SELECT_CC_MODE
2263 rtx *cc_use;
2264 enum machine_mode compare_mode;
2265 #endif
2266
2267 newpat = PATTERN (i3);
2268 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2269
2270 i2_is_used = 1;
2271
2272 #ifdef SELECT_CC_MODE
2273 /* See if a COMPARE with the operand we substituted in should be done
2274 with the mode that is currently being used. If not, do the same
2275 processing we do in `subst' for a SET; namely, if the destination
2276 is used only once, try to replace it with a register of the proper
2277 mode and also replace the COMPARE. */
2278 if (undobuf.other_insn == 0
2279 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2280 &undobuf.other_insn))
2281 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2282 i2src, const0_rtx))
2283 != GET_MODE (SET_DEST (newpat))))
2284 {
2285 if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2286 compare_mode))
2287 {
2288 unsigned int regno = REGNO (SET_DEST (newpat));
2289 rtx new_dest;
2290
2291 if (regno < FIRST_PSEUDO_REGISTER)
2292 new_dest = gen_rtx_REG (compare_mode, regno);
2293 else
2294 {
2295 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2296 new_dest = regno_reg_rtx[regno];
2297 }
2298
2299 SUBST (SET_DEST (newpat), new_dest);
2300 SUBST (XEXP (*cc_use, 0), new_dest);
2301 SUBST (SET_SRC (newpat),
2302 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2303 }
2304 else
2305 undobuf.other_insn = 0;
2306 }
2307 #endif
2308 }
2309 else
2310 #endif
2311 {
2312 /* It is possible that the source of I2 or I1 may be performing
2313 an unneeded operation, such as a ZERO_EXTEND of something
2314 that is known to have the high part zero. Handle that case
2315 by letting subst look at the innermost one of them.
2316
2317 Another way to do this would be to have a function that tries
2318 to simplify a single insn instead of merging two or more
2319 insns. We don't do this because of the potential of infinite
2320 loops and because of the potential extra memory required.
2321 However, doing it the way we are is a bit of a kludge and
2322 doesn't catch all cases.
2323
2324 But only do this if -fexpensive-optimizations since it slows
2325 things down and doesn't usually win.
2326
2327 This is not done in the COMPARE case above because the
2328 unmodified I2PAT is used in the PARALLEL and so a pattern
2329 with a modified I2SRC would not match. */
2330
2331 if (flag_expensive_optimizations)
2332 {
2333 /* Pass pc_rtx so no substitutions are done, just
2334 simplifications. */
2335 if (i1)
2336 {
2337 subst_low_cuid = INSN_CUID (i1);
2338 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2339 }
2340 else
2341 {
2342 subst_low_cuid = INSN_CUID (i2);
2343 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2344 }
2345 }
2346
2347 n_occurrences = 0; /* `subst' counts here */
2348
2349 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2350 need to make a unique copy of I2SRC each time we substitute it
2351 to avoid self-referential rtl. */
2352
2353 subst_low_cuid = INSN_CUID (i2);
2354 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2355 ! i1_feeds_i3 && i1dest_in_i1src);
2356 substed_i2 = 1;
2357
2358 /* Record whether i2's body now appears within i3's body. */
2359 i2_is_used = n_occurrences;
2360 }
2361
2362 /* If we already got a failure, don't try to do more. Otherwise,
2363 try to substitute in I1 if we have it. */
2364
2365 if (i1 && GET_CODE (newpat) != CLOBBER)
2366 {
2367 /* Before we can do this substitution, we must redo the test done
2368 above (see detailed comments there) that ensures that I1DEST
2369 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2370
2371 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2372 0, (rtx*) 0))
2373 {
2374 undo_all ();
2375 return 0;
2376 }
2377
2378 n_occurrences = 0;
2379 subst_low_cuid = INSN_CUID (i1);
2380 newpat = subst (newpat, i1dest, i1src, 0, 0);
2381 substed_i1 = 1;
2382 }
2383
2384 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2385 to count all the ways that I2SRC and I1SRC can be used. */
2386 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2387 && i2_is_used + added_sets_2 > 1)
2388 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2389 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2390 > 1))
2391 /* Fail if we tried to make a new register. */
2392 || max_reg_num () != maxreg
2393 /* Fail if we couldn't do something and have a CLOBBER. */
2394 || GET_CODE (newpat) == CLOBBER
2395 /* Fail if this new pattern is a MULT and we didn't have one before
2396 at the outer level. */
2397 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2398 && ! have_mult))
2399 {
2400 undo_all ();
2401 return 0;
2402 }
2403
2404 /* If the actions of the earlier insns must be kept
2405 in addition to substituting them into the latest one,
2406 we must make a new PARALLEL for the latest insn
2407 to hold additional the SETs. */
2408
2409 if (added_sets_1 || added_sets_2)
2410 {
2411 combine_extras++;
2412
2413 if (GET_CODE (newpat) == PARALLEL)
2414 {
2415 rtvec old = XVEC (newpat, 0);
2416 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2417 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2418 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2419 sizeof (old->elem[0]) * old->num_elem);
2420 }
2421 else
2422 {
2423 rtx old = newpat;
2424 total_sets = 1 + added_sets_1 + added_sets_2;
2425 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2426 XVECEXP (newpat, 0, 0) = old;
2427 }
2428
2429 if (added_sets_1)
2430 XVECEXP (newpat, 0, --total_sets) = i1pat;
2431
2432 if (added_sets_2)
2433 {
2434 /* If there is no I1, use I2's body as is. We used to also not do
2435 the subst call below if I2 was substituted into I3,
2436 but that could lose a simplification. */
2437 if (i1 == 0)
2438 XVECEXP (newpat, 0, --total_sets) = i2pat;
2439 else
2440 /* See comment where i2pat is assigned. */
2441 XVECEXP (newpat, 0, --total_sets)
2442 = subst (i2pat, i1dest, i1src, 0, 0);
2443 }
2444 }
2445
2446 /* We come here when we are replacing a destination in I2 with the
2447 destination of I3. */
2448 validate_replacement:
2449
2450 /* Note which hard regs this insn has as inputs. */
2451 mark_used_regs_combine (newpat);
2452
2453 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2454 consider splitting this pattern, we might need these clobbers. */
2455 if (i1 && GET_CODE (newpat) == PARALLEL
2456 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2457 {
2458 int len = XVECLEN (newpat, 0);
2459
2460 newpat_vec_with_clobbers = rtvec_alloc (len);
2461 for (i = 0; i < len; i++)
2462 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2463 }
2464
2465 /* Is the result of combination a valid instruction? */
2466 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2467
2468 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2469 the second SET's destination is a register that is unused and isn't
2470 marked as an instruction that might trap in an EH region. In that case,
2471 we just need the first SET. This can occur when simplifying a divmod
2472 insn. We *must* test for this case here because the code below that
2473 splits two independent SETs doesn't handle this case correctly when it
2474 updates the register status.
2475
2476 It's pointless doing this if we originally had two sets, one from
2477 i3, and one from i2. Combining then splitting the parallel results
2478 in the original i2 again plus an invalid insn (which we delete).
2479 The net effect is only to move instructions around, which makes
2480 debug info less accurate.
2481
2482 Also check the case where the first SET's destination is unused.
2483 That would not cause incorrect code, but does cause an unneeded
2484 insn to remain. */
2485
2486 if (insn_code_number < 0
2487 && !(added_sets_2 && i1 == 0)
2488 && GET_CODE (newpat) == PARALLEL
2489 && XVECLEN (newpat, 0) == 2
2490 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2491 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2492 && asm_noperands (newpat) < 0)
2493 {
2494 rtx set0 = XVECEXP (newpat, 0, 0);
2495 rtx set1 = XVECEXP (newpat, 0, 1);
2496 rtx note;
2497
2498 if (((REG_P (SET_DEST (set1))
2499 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2500 || (GET_CODE (SET_DEST (set1)) == SUBREG
2501 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2502 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2503 || INTVAL (XEXP (note, 0)) <= 0)
2504 && ! side_effects_p (SET_SRC (set1)))
2505 {
2506 newpat = set0;
2507 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2508 }
2509
2510 else if (((REG_P (SET_DEST (set0))
2511 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2512 || (GET_CODE (SET_DEST (set0)) == SUBREG
2513 && find_reg_note (i3, REG_UNUSED,
2514 SUBREG_REG (SET_DEST (set0)))))
2515 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2516 || INTVAL (XEXP (note, 0)) <= 0)
2517 && ! side_effects_p (SET_SRC (set0)))
2518 {
2519 newpat = set1;
2520 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2521
2522 if (insn_code_number >= 0)
2523 {
2524 /* If we will be able to accept this, we have made a
2525 change to the destination of I3. This requires us to
2526 do a few adjustments. */
2527
2528 PATTERN (i3) = newpat;
2529 adjust_for_new_dest (i3);
2530 }
2531 }
2532 }
2533
2534 /* If we were combining three insns and the result is a simple SET
2535 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2536 insns. There are two ways to do this. It can be split using a
2537 machine-specific method (like when you have an addition of a large
2538 constant) or by combine in the function find_split_point. */
2539
2540 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2541 && asm_noperands (newpat) < 0)
2542 {
2543 rtx m_split, *split;
2544
2545 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2546 use I2DEST as a scratch register will help. In the latter case,
2547 convert I2DEST to the mode of the source of NEWPAT if we can. */
2548
2549 m_split = split_insns (newpat, i3);
2550
2551 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2552 inputs of NEWPAT. */
2553
2554 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2555 possible to try that as a scratch reg. This would require adding
2556 more code to make it work though. */
2557
2558 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2559 {
2560 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2561
2562 /* First try to split using the original register as a
2563 scratch register. */
2564 m_split = split_insns (gen_rtx_PARALLEL
2565 (VOIDmode,
2566 gen_rtvec (2, newpat,
2567 gen_rtx_CLOBBER (VOIDmode,
2568 i2dest))),
2569 i3);
2570
2571 /* If that didn't work, try changing the mode of I2DEST if
2572 we can. */
2573 if (m_split == 0
2574 && new_mode != GET_MODE (i2dest)
2575 && new_mode != VOIDmode
2576 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2577 {
2578 enum machine_mode old_mode = GET_MODE (i2dest);
2579 rtx ni2dest;
2580
2581 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2582 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2583 else
2584 {
2585 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2586 ni2dest = regno_reg_rtx[REGNO (i2dest)];
2587 }
2588
2589 m_split = split_insns (gen_rtx_PARALLEL
2590 (VOIDmode,
2591 gen_rtvec (2, newpat,
2592 gen_rtx_CLOBBER (VOIDmode,
2593 ni2dest))),
2594 i3);
2595
2596 if (m_split == 0
2597 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2598 {
2599 struct undo *buf;
2600
2601 PUT_MODE (regno_reg_rtx[REGNO (i2dest)], old_mode);
2602 buf = undobuf.undos;
2603 undobuf.undos = buf->next;
2604 buf->next = undobuf.frees;
2605 undobuf.frees = buf;
2606 }
2607 }
2608 }
2609
2610 /* If recog_for_combine has discarded clobbers, try to use them
2611 again for the split. */
2612 if (m_split == 0 && newpat_vec_with_clobbers)
2613 m_split
2614 = split_insns (gen_rtx_PARALLEL (VOIDmode,
2615 newpat_vec_with_clobbers), i3);
2616
2617 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2618 {
2619 m_split = PATTERN (m_split);
2620 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2621 if (insn_code_number >= 0)
2622 newpat = m_split;
2623 }
2624 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2625 && (next_real_insn (i2) == i3
2626 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2627 {
2628 rtx i2set, i3set;
2629 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2630 newi2pat = PATTERN (m_split);
2631
2632 i3set = single_set (NEXT_INSN (m_split));
2633 i2set = single_set (m_split);
2634
2635 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2636
2637 /* If I2 or I3 has multiple SETs, we won't know how to track
2638 register status, so don't use these insns. If I2's destination
2639 is used between I2 and I3, we also can't use these insns. */
2640
2641 if (i2_code_number >= 0 && i2set && i3set
2642 && (next_real_insn (i2) == i3
2643 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2644 insn_code_number = recog_for_combine (&newi3pat, i3,
2645 &new_i3_notes);
2646 if (insn_code_number >= 0)
2647 newpat = newi3pat;
2648
2649 /* It is possible that both insns now set the destination of I3.
2650 If so, we must show an extra use of it. */
2651
2652 if (insn_code_number >= 0)
2653 {
2654 rtx new_i3_dest = SET_DEST (i3set);
2655 rtx new_i2_dest = SET_DEST (i2set);
2656
2657 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2658 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2659 || GET_CODE (new_i3_dest) == SUBREG)
2660 new_i3_dest = XEXP (new_i3_dest, 0);
2661
2662 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2663 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2664 || GET_CODE (new_i2_dest) == SUBREG)
2665 new_i2_dest = XEXP (new_i2_dest, 0);
2666
2667 if (REG_P (new_i3_dest)
2668 && REG_P (new_i2_dest)
2669 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2670 REG_N_SETS (REGNO (new_i2_dest))++;
2671 }
2672 }
2673
2674 /* If we can split it and use I2DEST, go ahead and see if that
2675 helps things be recognized. Verify that none of the registers
2676 are set between I2 and I3. */
2677 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2678 #ifdef HAVE_cc0
2679 && REG_P (i2dest)
2680 #endif
2681 /* We need I2DEST in the proper mode. If it is a hard register
2682 or the only use of a pseudo, we can change its mode.
2683 Make sure we don't change a hard register to have a mode that
2684 isn't valid for it, or change the number of registers. */
2685 && (GET_MODE (*split) == GET_MODE (i2dest)
2686 || GET_MODE (*split) == VOIDmode
2687 || can_change_dest_mode (i2dest, added_sets_2,
2688 GET_MODE (*split)))
2689 && (next_real_insn (i2) == i3
2690 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2691 /* We can't overwrite I2DEST if its value is still used by
2692 NEWPAT. */
2693 && ! reg_referenced_p (i2dest, newpat))
2694 {
2695 rtx newdest = i2dest;
2696 enum rtx_code split_code = GET_CODE (*split);
2697 enum machine_mode split_mode = GET_MODE (*split);
2698 bool subst_done = false;
2699 newi2pat = NULL_RTX;
2700
2701 /* Get NEWDEST as a register in the proper mode. We have already
2702 validated that we can do this. */
2703 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2704 {
2705 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2706 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2707 else
2708 {
2709 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
2710 newdest = regno_reg_rtx[REGNO (i2dest)];
2711 }
2712 }
2713
2714 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2715 an ASHIFT. This can occur if it was inside a PLUS and hence
2716 appeared to be a memory address. This is a kludge. */
2717 if (split_code == MULT
2718 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2719 && INTVAL (XEXP (*split, 1)) > 0
2720 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2721 {
2722 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2723 XEXP (*split, 0), GEN_INT (i)));
2724 /* Update split_code because we may not have a multiply
2725 anymore. */
2726 split_code = GET_CODE (*split);
2727 }
2728
2729 #ifdef INSN_SCHEDULING
2730 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2731 be written as a ZERO_EXTEND. */
2732 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
2733 {
2734 #ifdef LOAD_EXTEND_OP
2735 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2736 what it really is. */
2737 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2738 == SIGN_EXTEND)
2739 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2740 SUBREG_REG (*split)));
2741 else
2742 #endif
2743 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2744 SUBREG_REG (*split)));
2745 }
2746 #endif
2747
2748 /* Attempt to split binary operators using arithmetic identities. */
2749 if (BINARY_P (SET_SRC (newpat))
2750 && split_mode == GET_MODE (SET_SRC (newpat))
2751 && ! side_effects_p (SET_SRC (newpat)))
2752 {
2753 rtx setsrc = SET_SRC (newpat);
2754 enum machine_mode mode = GET_MODE (setsrc);
2755 enum rtx_code code = GET_CODE (setsrc);
2756 rtx src_op0 = XEXP (setsrc, 0);
2757 rtx src_op1 = XEXP (setsrc, 1);
2758
2759 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
2760 if (rtx_equal_p (src_op0, src_op1))
2761 {
2762 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
2763 SUBST (XEXP (setsrc, 0), newdest);
2764 SUBST (XEXP (setsrc, 1), newdest);
2765 subst_done = true;
2766 }
2767 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
2768 else if ((code == PLUS || code == MULT)
2769 && GET_CODE (src_op0) == code
2770 && GET_CODE (XEXP (src_op0, 0)) == code
2771 && (INTEGRAL_MODE_P (mode)
2772 || (FLOAT_MODE_P (mode)
2773 && flag_unsafe_math_optimizations)))
2774 {
2775 rtx p = XEXP (XEXP (src_op0, 0), 0);
2776 rtx q = XEXP (XEXP (src_op0, 0), 1);
2777 rtx r = XEXP (src_op0, 1);
2778 rtx s = src_op1;
2779
2780 /* Split both "((X op Y) op X) op Y" and
2781 "((X op Y) op Y) op X" as "T op T" where T is
2782 "X op Y". */
2783 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
2784 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
2785 {
2786 newi2pat = gen_rtx_SET (VOIDmode, newdest,
2787 XEXP (src_op0, 0));
2788 SUBST (XEXP (setsrc, 0), newdest);
2789 SUBST (XEXP (setsrc, 1), newdest);
2790 subst_done = true;
2791 }
2792 /* Split "((X op X) op Y) op Y)" as "T op T" where
2793 T is "X op Y". */
2794 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
2795 {
2796 rtx tmp = simplify_gen_binary (code, mode, p, r);
2797 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
2798 SUBST (XEXP (setsrc, 0), newdest);
2799 SUBST (XEXP (setsrc, 1), newdest);
2800 subst_done = true;
2801 }
2802 }
2803 }
2804
2805 if (!subst_done)
2806 {
2807 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2808 SUBST (*split, newdest);
2809 }
2810
2811 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2812
2813 /* recog_for_combine might have added CLOBBERs to newi2pat.
2814 Make sure NEWPAT does not depend on the clobbered regs. */
2815 if (GET_CODE (newi2pat) == PARALLEL)
2816 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
2817 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
2818 {
2819 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
2820 if (reg_overlap_mentioned_p (reg, newpat))
2821 {
2822 undo_all ();
2823 return 0;
2824 }
2825 }
2826
2827 /* If the split point was a MULT and we didn't have one before,
2828 don't use one now. */
2829 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2830 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2831 }
2832 }
2833
2834 /* Check for a case where we loaded from memory in a narrow mode and
2835 then sign extended it, but we need both registers. In that case,
2836 we have a PARALLEL with both loads from the same memory location.
2837 We can split this into a load from memory followed by a register-register
2838 copy. This saves at least one insn, more if register allocation can
2839 eliminate the copy.
2840
2841 We cannot do this if the destination of the first assignment is a
2842 condition code register or cc0. We eliminate this case by making sure
2843 the SET_DEST and SET_SRC have the same mode.
2844
2845 We cannot do this if the destination of the second assignment is
2846 a register that we have already assumed is zero-extended. Similarly
2847 for a SUBREG of such a register. */
2848
2849 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2850 && GET_CODE (newpat) == PARALLEL
2851 && XVECLEN (newpat, 0) == 2
2852 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2853 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2854 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2855 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2856 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2857 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2858 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2859 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2860 INSN_CUID (i2))
2861 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2862 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2863 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2864 (REG_P (temp)
2865 && reg_stat[REGNO (temp)].nonzero_bits != 0
2866 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2867 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2868 && (reg_stat[REGNO (temp)].nonzero_bits
2869 != GET_MODE_MASK (word_mode))))
2870 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2871 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2872 (REG_P (temp)
2873 && reg_stat[REGNO (temp)].nonzero_bits != 0
2874 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2875 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2876 && (reg_stat[REGNO (temp)].nonzero_bits
2877 != GET_MODE_MASK (word_mode)))))
2878 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2879 SET_SRC (XVECEXP (newpat, 0, 1)))
2880 && ! find_reg_note (i3, REG_UNUSED,
2881 SET_DEST (XVECEXP (newpat, 0, 0))))
2882 {
2883 rtx ni2dest;
2884
2885 newi2pat = XVECEXP (newpat, 0, 0);
2886 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2887 newpat = XVECEXP (newpat, 0, 1);
2888 SUBST (SET_SRC (newpat),
2889 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2890 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2891
2892 if (i2_code_number >= 0)
2893 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2894
2895 if (insn_code_number >= 0)
2896 swap_i2i3 = 1;
2897 }
2898
2899 /* Similarly, check for a case where we have a PARALLEL of two independent
2900 SETs but we started with three insns. In this case, we can do the sets
2901 as two separate insns. This case occurs when some SET allows two
2902 other insns to combine, but the destination of that SET is still live. */
2903
2904 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2905 && GET_CODE (newpat) == PARALLEL
2906 && XVECLEN (newpat, 0) == 2
2907 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2908 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2909 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2910 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2911 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2912 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2913 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2914 INSN_CUID (i2))
2915 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2916 XVECEXP (newpat, 0, 0))
2917 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2918 XVECEXP (newpat, 0, 1))
2919 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2920 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
2921 #ifdef HAVE_cc0
2922 /* We cannot split the parallel into two sets if both sets
2923 reference cc0. */
2924 && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
2925 && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
2926 #endif
2927 )
2928 {
2929 /* Normally, it doesn't matter which of the two is done first,
2930 but it does if one references cc0. In that case, it has to
2931 be first. */
2932 #ifdef HAVE_cc0
2933 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2934 {
2935 newi2pat = XVECEXP (newpat, 0, 0);
2936 newpat = XVECEXP (newpat, 0, 1);
2937 }
2938 else
2939 #endif
2940 {
2941 newi2pat = XVECEXP (newpat, 0, 1);
2942 newpat = XVECEXP (newpat, 0, 0);
2943 }
2944
2945 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2946
2947 if (i2_code_number >= 0)
2948 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2949 }
2950
2951 /* If it still isn't recognized, fail and change things back the way they
2952 were. */
2953 if ((insn_code_number < 0
2954 /* Is the result a reasonable ASM_OPERANDS? */
2955 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2956 {
2957 undo_all ();
2958 return 0;
2959 }
2960
2961 /* If we had to change another insn, make sure it is valid also. */
2962 if (undobuf.other_insn)
2963 {
2964 rtx other_pat = PATTERN (undobuf.other_insn);
2965 rtx new_other_notes;
2966 rtx note, next;
2967
2968 CLEAR_HARD_REG_SET (newpat_used_regs);
2969
2970 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2971 &new_other_notes);
2972
2973 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2974 {
2975 undo_all ();
2976 return 0;
2977 }
2978
2979 PATTERN (undobuf.other_insn) = other_pat;
2980
2981 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2982 are still valid. Then add any non-duplicate notes added by
2983 recog_for_combine. */
2984 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2985 {
2986 next = XEXP (note, 1);
2987
2988 if (REG_NOTE_KIND (note) == REG_UNUSED
2989 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2990 {
2991 if (REG_P (XEXP (note, 0)))
2992 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2993
2994 remove_note (undobuf.other_insn, note);
2995 }
2996 }
2997
2998 for (note = new_other_notes; note; note = XEXP (note, 1))
2999 if (REG_P (XEXP (note, 0)))
3000 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
3001
3002 distribute_notes (new_other_notes, undobuf.other_insn,
3003 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3004 }
3005 #ifdef HAVE_cc0
3006 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3007 they are adjacent to each other or not. */
3008 {
3009 rtx p = prev_nonnote_insn (i3);
3010 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3011 && sets_cc0_p (newi2pat))
3012 {
3013 undo_all ();
3014 return 0;
3015 }
3016 }
3017 #endif
3018
3019 /* Only allow this combination if insn_rtx_costs reports that the
3020 replacement instructions are cheaper than the originals. */
3021 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat))
3022 {
3023 undo_all ();
3024 return 0;
3025 }
3026
3027 /* We now know that we can do this combination. Merge the insns and
3028 update the status of registers and LOG_LINKS. */
3029
3030 if (swap_i2i3)
3031 {
3032 rtx insn;
3033 rtx link;
3034 rtx ni2dest;
3035
3036 /* I3 now uses what used to be its destination and which is now
3037 I2's destination. This requires us to do a few adjustments. */
3038 PATTERN (i3) = newpat;
3039 adjust_for_new_dest (i3);
3040
3041 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3042 so we still will.
3043
3044 However, some later insn might be using I2's dest and have
3045 a LOG_LINK pointing at I3. We must remove this link.
3046 The simplest way to remove the link is to point it at I1,
3047 which we know will be a NOTE. */
3048
3049 /* newi2pat is usually a SET here; however, recog_for_combine might
3050 have added some clobbers. */
3051 if (GET_CODE (newi2pat) == PARALLEL)
3052 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3053 else
3054 ni2dest = SET_DEST (newi2pat);
3055
3056 for (insn = NEXT_INSN (i3);
3057 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3058 || insn != BB_HEAD (this_basic_block->next_bb));
3059 insn = NEXT_INSN (insn))
3060 {
3061 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3062 {
3063 for (link = LOG_LINKS (insn); link;
3064 link = XEXP (link, 1))
3065 if (XEXP (link, 0) == i3)
3066 XEXP (link, 0) = i1;
3067
3068 break;
3069 }
3070 }
3071 }
3072
3073 {
3074 rtx i3notes, i2notes, i1notes = 0;
3075 rtx i3links, i2links, i1links = 0;
3076 rtx midnotes = 0;
3077 unsigned int regno;
3078 /* Compute which registers we expect to eliminate. newi2pat may be setting
3079 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3080 same as i3dest, in which case newi2pat may be setting i1dest. */
3081 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3082 || i2dest_in_i2src || i2dest_in_i1src
3083 || !i2dest_killed
3084 ? 0 : i2dest);
3085 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3086 || (newi2pat && reg_set_p (i1dest, newi2pat))
3087 || !i1dest_killed
3088 ? 0 : i1dest);
3089
3090 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3091 clear them. */
3092 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3093 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3094 if (i1)
3095 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3096
3097 /* Ensure that we do not have something that should not be shared but
3098 occurs multiple times in the new insns. Check this by first
3099 resetting all the `used' flags and then copying anything is shared. */
3100
3101 reset_used_flags (i3notes);
3102 reset_used_flags (i2notes);
3103 reset_used_flags (i1notes);
3104 reset_used_flags (newpat);
3105 reset_used_flags (newi2pat);
3106 if (undobuf.other_insn)
3107 reset_used_flags (PATTERN (undobuf.other_insn));
3108
3109 i3notes = copy_rtx_if_shared (i3notes);
3110 i2notes = copy_rtx_if_shared (i2notes);
3111 i1notes = copy_rtx_if_shared (i1notes);
3112 newpat = copy_rtx_if_shared (newpat);
3113 newi2pat = copy_rtx_if_shared (newi2pat);
3114 if (undobuf.other_insn)
3115 reset_used_flags (PATTERN (undobuf.other_insn));
3116
3117 INSN_CODE (i3) = insn_code_number;
3118 PATTERN (i3) = newpat;
3119
3120 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3121 {
3122 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3123
3124 reset_used_flags (call_usage);
3125 call_usage = copy_rtx (call_usage);
3126
3127 if (substed_i2)
3128 replace_rtx (call_usage, i2dest, i2src);
3129
3130 if (substed_i1)
3131 replace_rtx (call_usage, i1dest, i1src);
3132
3133 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3134 }
3135
3136 if (undobuf.other_insn)
3137 INSN_CODE (undobuf.other_insn) = other_code_number;
3138
3139 /* We had one special case above where I2 had more than one set and
3140 we replaced a destination of one of those sets with the destination
3141 of I3. In that case, we have to update LOG_LINKS of insns later
3142 in this basic block. Note that this (expensive) case is rare.
3143
3144 Also, in this case, we must pretend that all REG_NOTEs for I2
3145 actually came from I3, so that REG_UNUSED notes from I2 will be
3146 properly handled. */
3147
3148 if (i3_subst_into_i2)
3149 {
3150 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3151 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3152 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3153 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3154 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3155 && ! find_reg_note (i2, REG_UNUSED,
3156 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3157 for (temp = NEXT_INSN (i2);
3158 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3159 || BB_HEAD (this_basic_block) != temp);
3160 temp = NEXT_INSN (temp))
3161 if (temp != i3 && INSN_P (temp))
3162 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3163 if (XEXP (link, 0) == i2)
3164 XEXP (link, 0) = i3;
3165
3166 if (i3notes)
3167 {
3168 rtx link = i3notes;
3169 while (XEXP (link, 1))
3170 link = XEXP (link, 1);
3171 XEXP (link, 1) = i2notes;
3172 }
3173 else
3174 i3notes = i2notes;
3175 i2notes = 0;
3176 }
3177
3178 LOG_LINKS (i3) = 0;
3179 REG_NOTES (i3) = 0;
3180 LOG_LINKS (i2) = 0;
3181 REG_NOTES (i2) = 0;
3182
3183 if (newi2pat)
3184 {
3185 INSN_CODE (i2) = i2_code_number;
3186 PATTERN (i2) = newi2pat;
3187 }
3188 else
3189 SET_INSN_DELETED (i2);
3190
3191 if (i1)
3192 {
3193 LOG_LINKS (i1) = 0;
3194 REG_NOTES (i1) = 0;
3195 SET_INSN_DELETED (i1);
3196 }
3197
3198 /* Get death notes for everything that is now used in either I3 or
3199 I2 and used to die in a previous insn. If we built two new
3200 patterns, move from I1 to I2 then I2 to I3 so that we get the
3201 proper movement on registers that I2 modifies. */
3202
3203 if (newi2pat)
3204 {
3205 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
3206 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
3207 }
3208 else
3209 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
3210 i3, &midnotes);
3211
3212 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
3213 if (i3notes)
3214 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3215 elim_i2, elim_i1);
3216 if (i2notes)
3217 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3218 elim_i2, elim_i1);
3219 if (i1notes)
3220 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3221 elim_i2, elim_i1);
3222 if (midnotes)
3223 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3224 elim_i2, elim_i1);
3225
3226 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
3227 know these are REG_UNUSED and want them to go to the desired insn,
3228 so we always pass it as i3. We have not counted the notes in
3229 reg_n_deaths yet, so we need to do so now. */
3230
3231 if (newi2pat && new_i2_notes)
3232 {
3233 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
3234 if (REG_P (XEXP (temp, 0)))
3235 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
3236
3237 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3238 }
3239
3240 if (new_i3_notes)
3241 {
3242 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
3243 if (REG_P (XEXP (temp, 0)))
3244 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
3245
3246 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3247 }
3248
3249 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
3250 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
3251 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
3252 in that case, it might delete I2. Similarly for I2 and I1.
3253 Show an additional death due to the REG_DEAD note we make here. If
3254 we discard it in distribute_notes, we will decrement it again. */
3255
3256 if (i3dest_killed)
3257 {
3258 if (REG_P (i3dest_killed))
3259 REG_N_DEATHS (REGNO (i3dest_killed))++;
3260
3261 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3262 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3263 NULL_RTX),
3264 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3265 else
3266 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3267 NULL_RTX),
3268 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3269 elim_i2, elim_i1);
3270 }
3271
3272 if (i2dest_in_i2src)
3273 {
3274 if (REG_P (i2dest))
3275 REG_N_DEATHS (REGNO (i2dest))++;
3276
3277 if (newi2pat && reg_set_p (i2dest, newi2pat))
3278 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3279 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3280 else
3281 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3282 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3283 NULL_RTX, NULL_RTX);
3284 }
3285
3286 if (i1dest_in_i1src)
3287 {
3288 if (REG_P (i1dest))
3289 REG_N_DEATHS (REGNO (i1dest))++;
3290
3291 if (newi2pat && reg_set_p (i1dest, newi2pat))
3292 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3293 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3294 else
3295 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3296 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3297 NULL_RTX, NULL_RTX);
3298 }
3299
3300 distribute_links (i3links);
3301 distribute_links (i2links);
3302 distribute_links (i1links);
3303
3304 if (REG_P (i2dest))
3305 {
3306 rtx link;
3307 rtx i2_insn = 0, i2_val = 0, set;
3308
3309 /* The insn that used to set this register doesn't exist, and
3310 this life of the register may not exist either. See if one of
3311 I3's links points to an insn that sets I2DEST. If it does,
3312 that is now the last known value for I2DEST. If we don't update
3313 this and I2 set the register to a value that depended on its old
3314 contents, we will get confused. If this insn is used, thing
3315 will be set correctly in combine_instructions. */
3316
3317 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3318 if ((set = single_set (XEXP (link, 0))) != 0
3319 && rtx_equal_p (i2dest, SET_DEST (set)))
3320 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3321
3322 record_value_for_reg (i2dest, i2_insn, i2_val);
3323
3324 /* If the reg formerly set in I2 died only once and that was in I3,
3325 zero its use count so it won't make `reload' do any work. */
3326 if (! added_sets_2
3327 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3328 && ! i2dest_in_i2src)
3329 {
3330 regno = REGNO (i2dest);
3331 REG_N_SETS (regno)--;
3332 }
3333 }
3334
3335 if (i1 && REG_P (i1dest))
3336 {
3337 rtx link;
3338 rtx i1_insn = 0, i1_val = 0, set;
3339
3340 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3341 if ((set = single_set (XEXP (link, 0))) != 0
3342 && rtx_equal_p (i1dest, SET_DEST (set)))
3343 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3344
3345 record_value_for_reg (i1dest, i1_insn, i1_val);
3346
3347 regno = REGNO (i1dest);
3348 if (! added_sets_1 && ! i1dest_in_i1src)
3349 REG_N_SETS (regno)--;
3350 }
3351
3352 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3353 been made to this insn. The order of
3354 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3355 can affect nonzero_bits of newpat */
3356 if (newi2pat)
3357 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3358 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3359
3360 /* Set new_direct_jump_p if a new return or simple jump instruction
3361 has been created.
3362
3363 If I3 is now an unconditional jump, ensure that it has a
3364 BARRIER following it since it may have initially been a
3365 conditional jump. It may also be the last nonnote insn. */
3366
3367 if (returnjump_p (i3) || any_uncondjump_p (i3))
3368 {
3369 *new_direct_jump_p = 1;
3370 mark_jump_label (PATTERN (i3), i3, 0);
3371
3372 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3373 || !BARRIER_P (temp))
3374 emit_barrier_after (i3);
3375 }
3376
3377 if (undobuf.other_insn != NULL_RTX
3378 && (returnjump_p (undobuf.other_insn)
3379 || any_uncondjump_p (undobuf.other_insn)))
3380 {
3381 *new_direct_jump_p = 1;
3382
3383 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3384 || !BARRIER_P (temp))
3385 emit_barrier_after (undobuf.other_insn);
3386 }
3387
3388 /* An NOOP jump does not need barrier, but it does need cleaning up
3389 of CFG. */
3390 if (GET_CODE (newpat) == SET
3391 && SET_SRC (newpat) == pc_rtx
3392 && SET_DEST (newpat) == pc_rtx)
3393 *new_direct_jump_p = 1;
3394 }
3395
3396 combine_successes++;
3397 undo_commit ();
3398
3399 if (added_links_insn
3400 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
3401 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
3402 return added_links_insn;
3403 else
3404 return newi2pat ? i2 : i3;
3405 }
3406 \f
3407 /* Undo all the modifications recorded in undobuf. */
3408
3409 static void
3410 undo_all (void)
3411 {
3412 struct undo *undo, *next;
3413
3414 for (undo = undobuf.undos; undo; undo = next)
3415 {
3416 next = undo->next;
3417 switch (undo->kind)
3418 {
3419 case UNDO_RTX:
3420 *undo->where.r = undo->old_contents.r;
3421 break;
3422 case UNDO_INT:
3423 *undo->where.i = undo->old_contents.i;
3424 break;
3425 case UNDO_MODE:
3426 PUT_MODE (*undo->where.r, undo->old_contents.m);
3427 break;
3428 default:
3429 gcc_unreachable ();
3430 }
3431
3432 undo->next = undobuf.frees;
3433 undobuf.frees = undo;
3434 }
3435
3436 undobuf.undos = 0;
3437 }
3438
3439 /* We've committed to accepting the changes we made. Move all
3440 of the undos to the free list. */
3441
3442 static void
3443 undo_commit (void)
3444 {
3445 struct undo *undo, *next;
3446
3447 for (undo = undobuf.undos; undo; undo = next)
3448 {
3449 next = undo->next;
3450 undo->next = undobuf.frees;
3451 undobuf.frees = undo;
3452 }
3453 undobuf.undos = 0;
3454 }
3455 \f
3456 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3457 where we have an arithmetic expression and return that point. LOC will
3458 be inside INSN.
3459
3460 try_combine will call this function to see if an insn can be split into
3461 two insns. */
3462
3463 static rtx *
3464 find_split_point (rtx *loc, rtx insn)
3465 {
3466 rtx x = *loc;
3467 enum rtx_code code = GET_CODE (x);
3468 rtx *split;
3469 unsigned HOST_WIDE_INT len = 0;
3470 HOST_WIDE_INT pos = 0;
3471 int unsignedp = 0;
3472 rtx inner = NULL_RTX;
3473
3474 /* First special-case some codes. */
3475 switch (code)
3476 {
3477 case SUBREG:
3478 #ifdef INSN_SCHEDULING
3479 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3480 point. */
3481 if (MEM_P (SUBREG_REG (x)))
3482 return loc;
3483 #endif
3484 return find_split_point (&SUBREG_REG (x), insn);
3485
3486 case MEM:
3487 #ifdef HAVE_lo_sum
3488 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3489 using LO_SUM and HIGH. */
3490 if (GET_CODE (XEXP (x, 0)) == CONST
3491 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3492 {
3493 SUBST (XEXP (x, 0),
3494 gen_rtx_LO_SUM (Pmode,
3495 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3496 XEXP (x, 0)));
3497 return &XEXP (XEXP (x, 0), 0);
3498 }
3499 #endif
3500
3501 /* If we have a PLUS whose second operand is a constant and the
3502 address is not valid, perhaps will can split it up using
3503 the machine-specific way to split large constants. We use
3504 the first pseudo-reg (one of the virtual regs) as a placeholder;
3505 it will not remain in the result. */
3506 if (GET_CODE (XEXP (x, 0)) == PLUS
3507 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3508 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3509 {
3510 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3511 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3512 subst_insn);
3513
3514 /* This should have produced two insns, each of which sets our
3515 placeholder. If the source of the second is a valid address,
3516 we can make put both sources together and make a split point
3517 in the middle. */
3518
3519 if (seq
3520 && NEXT_INSN (seq) != NULL_RTX
3521 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3522 && NONJUMP_INSN_P (seq)
3523 && GET_CODE (PATTERN (seq)) == SET
3524 && SET_DEST (PATTERN (seq)) == reg
3525 && ! reg_mentioned_p (reg,
3526 SET_SRC (PATTERN (seq)))
3527 && NONJUMP_INSN_P (NEXT_INSN (seq))
3528 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3529 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3530 && memory_address_p (GET_MODE (x),
3531 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3532 {
3533 rtx src1 = SET_SRC (PATTERN (seq));
3534 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3535
3536 /* Replace the placeholder in SRC2 with SRC1. If we can
3537 find where in SRC2 it was placed, that can become our
3538 split point and we can replace this address with SRC2.
3539 Just try two obvious places. */
3540
3541 src2 = replace_rtx (src2, reg, src1);
3542 split = 0;
3543 if (XEXP (src2, 0) == src1)
3544 split = &XEXP (src2, 0);
3545 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3546 && XEXP (XEXP (src2, 0), 0) == src1)
3547 split = &XEXP (XEXP (src2, 0), 0);
3548
3549 if (split)
3550 {
3551 SUBST (XEXP (x, 0), src2);
3552 return split;
3553 }
3554 }
3555
3556 /* If that didn't work, perhaps the first operand is complex and
3557 needs to be computed separately, so make a split point there.
3558 This will occur on machines that just support REG + CONST
3559 and have a constant moved through some previous computation. */
3560
3561 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3562 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3563 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3564 return &XEXP (XEXP (x, 0), 0);
3565 }
3566 break;
3567
3568 case SET:
3569 #ifdef HAVE_cc0
3570 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3571 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3572 we need to put the operand into a register. So split at that
3573 point. */
3574
3575 if (SET_DEST (x) == cc0_rtx
3576 && GET_CODE (SET_SRC (x)) != COMPARE
3577 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3578 && !OBJECT_P (SET_SRC (x))
3579 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3580 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3581 return &SET_SRC (x);
3582 #endif
3583
3584 /* See if we can split SET_SRC as it stands. */
3585 split = find_split_point (&SET_SRC (x), insn);
3586 if (split && split != &SET_SRC (x))
3587 return split;
3588
3589 /* See if we can split SET_DEST as it stands. */
3590 split = find_split_point (&SET_DEST (x), insn);
3591 if (split && split != &SET_DEST (x))
3592 return split;
3593
3594 /* See if this is a bitfield assignment with everything constant. If
3595 so, this is an IOR of an AND, so split it into that. */
3596 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3597 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3598 <= HOST_BITS_PER_WIDE_INT)
3599 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3600 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3601 && GET_CODE (SET_SRC (x)) == CONST_INT
3602 && ((INTVAL (XEXP (SET_DEST (x), 1))
3603 + INTVAL (XEXP (SET_DEST (x), 2)))
3604 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3605 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3606 {
3607 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3608 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3609 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3610 rtx dest = XEXP (SET_DEST (x), 0);
3611 enum machine_mode mode = GET_MODE (dest);
3612 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3613 rtx or_mask;
3614
3615 if (BITS_BIG_ENDIAN)
3616 pos = GET_MODE_BITSIZE (mode) - len - pos;
3617
3618 or_mask = gen_int_mode (src << pos, mode);
3619 if (src == mask)
3620 SUBST (SET_SRC (x),
3621 simplify_gen_binary (IOR, mode, dest, or_mask));
3622 else
3623 {
3624 rtx negmask = gen_int_mode (~(mask << pos), mode);
3625 SUBST (SET_SRC (x),
3626 simplify_gen_binary (IOR, mode,
3627 simplify_gen_binary (AND, mode,
3628 dest, negmask),
3629 or_mask));
3630 }
3631
3632 SUBST (SET_DEST (x), dest);
3633
3634 split = find_split_point (&SET_SRC (x), insn);
3635 if (split && split != &SET_SRC (x))
3636 return split;
3637 }
3638
3639 /* Otherwise, see if this is an operation that we can split into two.
3640 If so, try to split that. */
3641 code = GET_CODE (SET_SRC (x));
3642
3643 switch (code)
3644 {
3645 case AND:
3646 /* If we are AND'ing with a large constant that is only a single
3647 bit and the result is only being used in a context where we
3648 need to know if it is zero or nonzero, replace it with a bit
3649 extraction. This will avoid the large constant, which might
3650 have taken more than one insn to make. If the constant were
3651 not a valid argument to the AND but took only one insn to make,
3652 this is no worse, but if it took more than one insn, it will
3653 be better. */
3654
3655 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3656 && REG_P (XEXP (SET_SRC (x), 0))
3657 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3658 && REG_P (SET_DEST (x))
3659 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3660 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3661 && XEXP (*split, 0) == SET_DEST (x)
3662 && XEXP (*split, 1) == const0_rtx)
3663 {
3664 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3665 XEXP (SET_SRC (x), 0),
3666 pos, NULL_RTX, 1, 1, 0, 0);
3667 if (extraction != 0)
3668 {
3669 SUBST (SET_SRC (x), extraction);
3670 return find_split_point (loc, insn);
3671 }
3672 }
3673 break;
3674
3675 case NE:
3676 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3677 is known to be on, this can be converted into a NEG of a shift. */
3678 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3679 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3680 && 1 <= (pos = exact_log2
3681 (nonzero_bits (XEXP (SET_SRC (x), 0),
3682 GET_MODE (XEXP (SET_SRC (x), 0))))))
3683 {
3684 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3685
3686 SUBST (SET_SRC (x),
3687 gen_rtx_NEG (mode,
3688 gen_rtx_LSHIFTRT (mode,
3689 XEXP (SET_SRC (x), 0),
3690 GEN_INT (pos))));
3691
3692 split = find_split_point (&SET_SRC (x), insn);
3693 if (split && split != &SET_SRC (x))
3694 return split;
3695 }
3696 break;
3697
3698 case SIGN_EXTEND:
3699 inner = XEXP (SET_SRC (x), 0);
3700
3701 /* We can't optimize if either mode is a partial integer
3702 mode as we don't know how many bits are significant
3703 in those modes. */
3704 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3705 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3706 break;
3707
3708 pos = 0;
3709 len = GET_MODE_BITSIZE (GET_MODE (inner));
3710 unsignedp = 0;
3711 break;
3712
3713 case SIGN_EXTRACT:
3714 case ZERO_EXTRACT:
3715 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3716 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3717 {
3718 inner = XEXP (SET_SRC (x), 0);
3719 len = INTVAL (XEXP (SET_SRC (x), 1));
3720 pos = INTVAL (XEXP (SET_SRC (x), 2));
3721
3722 if (BITS_BIG_ENDIAN)
3723 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3724 unsignedp = (code == ZERO_EXTRACT);
3725 }
3726 break;
3727
3728 default:
3729 break;
3730 }
3731
3732 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3733 {
3734 enum machine_mode mode = GET_MODE (SET_SRC (x));
3735
3736 /* For unsigned, we have a choice of a shift followed by an
3737 AND or two shifts. Use two shifts for field sizes where the
3738 constant might be too large. We assume here that we can
3739 always at least get 8-bit constants in an AND insn, which is
3740 true for every current RISC. */
3741
3742 if (unsignedp && len <= 8)
3743 {
3744 SUBST (SET_SRC (x),
3745 gen_rtx_AND (mode,
3746 gen_rtx_LSHIFTRT
3747 (mode, gen_lowpart (mode, inner),
3748 GEN_INT (pos)),
3749 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3750
3751 split = find_split_point (&SET_SRC (x), insn);
3752 if (split && split != &SET_SRC (x))
3753 return split;
3754 }
3755 else
3756 {
3757 SUBST (SET_SRC (x),
3758 gen_rtx_fmt_ee
3759 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3760 gen_rtx_ASHIFT (mode,
3761 gen_lowpart (mode, inner),
3762 GEN_INT (GET_MODE_BITSIZE (mode)
3763 - len - pos)),
3764 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3765
3766 split = find_split_point (&SET_SRC (x), insn);
3767 if (split && split != &SET_SRC (x))
3768 return split;
3769 }
3770 }
3771
3772 /* See if this is a simple operation with a constant as the second
3773 operand. It might be that this constant is out of range and hence
3774 could be used as a split point. */
3775 if (BINARY_P (SET_SRC (x))
3776 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3777 && (OBJECT_P (XEXP (SET_SRC (x), 0))
3778 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3779 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3780 return &XEXP (SET_SRC (x), 1);
3781
3782 /* Finally, see if this is a simple operation with its first operand
3783 not in a register. The operation might require this operand in a
3784 register, so return it as a split point. We can always do this
3785 because if the first operand were another operation, we would have
3786 already found it as a split point. */
3787 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3788 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3789 return &XEXP (SET_SRC (x), 0);
3790
3791 return 0;
3792
3793 case AND:
3794 case IOR:
3795 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3796 it is better to write this as (not (ior A B)) so we can split it.
3797 Similarly for IOR. */
3798 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3799 {
3800 SUBST (*loc,
3801 gen_rtx_NOT (GET_MODE (x),
3802 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3803 GET_MODE (x),
3804 XEXP (XEXP (x, 0), 0),
3805 XEXP (XEXP (x, 1), 0))));
3806 return find_split_point (loc, insn);
3807 }
3808
3809 /* Many RISC machines have a large set of logical insns. If the
3810 second operand is a NOT, put it first so we will try to split the
3811 other operand first. */
3812 if (GET_CODE (XEXP (x, 1)) == NOT)
3813 {
3814 rtx tem = XEXP (x, 0);
3815 SUBST (XEXP (x, 0), XEXP (x, 1));
3816 SUBST (XEXP (x, 1), tem);
3817 }
3818 break;
3819
3820 default:
3821 break;
3822 }
3823
3824 /* Otherwise, select our actions depending on our rtx class. */
3825 switch (GET_RTX_CLASS (code))
3826 {
3827 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3828 case RTX_TERNARY:
3829 split = find_split_point (&XEXP (x, 2), insn);
3830 if (split)
3831 return split;
3832 /* ... fall through ... */
3833 case RTX_BIN_ARITH:
3834 case RTX_COMM_ARITH:
3835 case RTX_COMPARE:
3836 case RTX_COMM_COMPARE:
3837 split = find_split_point (&XEXP (x, 1), insn);
3838 if (split)
3839 return split;
3840 /* ... fall through ... */
3841 case RTX_UNARY:
3842 /* Some machines have (and (shift ...) ...) insns. If X is not
3843 an AND, but XEXP (X, 0) is, use it as our split point. */
3844 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3845 return &XEXP (x, 0);
3846
3847 split = find_split_point (&XEXP (x, 0), insn);
3848 if (split)
3849 return split;
3850 return loc;
3851
3852 default:
3853 /* Otherwise, we don't have a split point. */
3854 return 0;
3855 }
3856 }
3857 \f
3858 /* Throughout X, replace FROM with TO, and return the result.
3859 The result is TO if X is FROM;
3860 otherwise the result is X, but its contents may have been modified.
3861 If they were modified, a record was made in undobuf so that
3862 undo_all will (among other things) return X to its original state.
3863
3864 If the number of changes necessary is too much to record to undo,
3865 the excess changes are not made, so the result is invalid.
3866 The changes already made can still be undone.
3867 undobuf.num_undo is incremented for such changes, so by testing that
3868 the caller can tell whether the result is valid.
3869
3870 `n_occurrences' is incremented each time FROM is replaced.
3871
3872 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3873
3874 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3875 by copying if `n_occurrences' is nonzero. */
3876
3877 static rtx
3878 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3879 {
3880 enum rtx_code code = GET_CODE (x);
3881 enum machine_mode op0_mode = VOIDmode;
3882 const char *fmt;
3883 int len, i;
3884 rtx new;
3885
3886 /* Two expressions are equal if they are identical copies of a shared
3887 RTX or if they are both registers with the same register number
3888 and mode. */
3889
3890 #define COMBINE_RTX_EQUAL_P(X,Y) \
3891 ((X) == (Y) \
3892 || (REG_P (X) && REG_P (Y) \
3893 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3894
3895 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3896 {
3897 n_occurrences++;
3898 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3899 }
3900
3901 /* If X and FROM are the same register but different modes, they will
3902 not have been seen as equal above. However, flow.c will make a
3903 LOG_LINKS entry for that case. If we do nothing, we will try to
3904 rerecognize our original insn and, when it succeeds, we will
3905 delete the feeding insn, which is incorrect.
3906
3907 So force this insn not to match in this (rare) case. */
3908 if (! in_dest && code == REG && REG_P (from)
3909 && REGNO (x) == REGNO (from))
3910 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3911
3912 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3913 of which may contain things that can be combined. */
3914 if (code != MEM && code != LO_SUM && OBJECT_P (x))
3915 return x;
3916
3917 /* It is possible to have a subexpression appear twice in the insn.
3918 Suppose that FROM is a register that appears within TO.
3919 Then, after that subexpression has been scanned once by `subst',
3920 the second time it is scanned, TO may be found. If we were
3921 to scan TO here, we would find FROM within it and create a
3922 self-referent rtl structure which is completely wrong. */
3923 if (COMBINE_RTX_EQUAL_P (x, to))
3924 return to;
3925
3926 /* Parallel asm_operands need special attention because all of the
3927 inputs are shared across the arms. Furthermore, unsharing the
3928 rtl results in recognition failures. Failure to handle this case
3929 specially can result in circular rtl.
3930
3931 Solve this by doing a normal pass across the first entry of the
3932 parallel, and only processing the SET_DESTs of the subsequent
3933 entries. Ug. */
3934
3935 if (code == PARALLEL
3936 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3937 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3938 {
3939 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3940
3941 /* If this substitution failed, this whole thing fails. */
3942 if (GET_CODE (new) == CLOBBER
3943 && XEXP (new, 0) == const0_rtx)
3944 return new;
3945
3946 SUBST (XVECEXP (x, 0, 0), new);
3947
3948 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3949 {
3950 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3951
3952 if (!REG_P (dest)
3953 && GET_CODE (dest) != CC0
3954 && GET_CODE (dest) != PC)
3955 {
3956 new = subst (dest, from, to, 0, unique_copy);
3957
3958 /* If this substitution failed, this whole thing fails. */
3959 if (GET_CODE (new) == CLOBBER
3960 && XEXP (new, 0) == const0_rtx)
3961 return new;
3962
3963 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3964 }
3965 }
3966 }
3967 else
3968 {
3969 len = GET_RTX_LENGTH (code);
3970 fmt = GET_RTX_FORMAT (code);
3971
3972 /* We don't need to process a SET_DEST that is a register, CC0,
3973 or PC, so set up to skip this common case. All other cases
3974 where we want to suppress replacing something inside a
3975 SET_SRC are handled via the IN_DEST operand. */
3976 if (code == SET
3977 && (REG_P (SET_DEST (x))
3978 || GET_CODE (SET_DEST (x)) == CC0
3979 || GET_CODE (SET_DEST (x)) == PC))
3980 fmt = "ie";
3981
3982 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3983 constant. */
3984 if (fmt[0] == 'e')
3985 op0_mode = GET_MODE (XEXP (x, 0));
3986
3987 for (i = 0; i < len; i++)
3988 {
3989 if (fmt[i] == 'E')
3990 {
3991 int j;
3992 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3993 {
3994 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3995 {
3996 new = (unique_copy && n_occurrences
3997 ? copy_rtx (to) : to);
3998 n_occurrences++;
3999 }
4000 else
4001 {
4002 new = subst (XVECEXP (x, i, j), from, to, 0,
4003 unique_copy);
4004
4005 /* If this substitution failed, this whole thing
4006 fails. */
4007 if (GET_CODE (new) == CLOBBER
4008 && XEXP (new, 0) == const0_rtx)
4009 return new;
4010 }
4011
4012 SUBST (XVECEXP (x, i, j), new);
4013 }
4014 }
4015 else if (fmt[i] == 'e')
4016 {
4017 /* If this is a register being set, ignore it. */
4018 new = XEXP (x, i);
4019 if (in_dest
4020 && i == 0
4021 && (((code == SUBREG || code == ZERO_EXTRACT)
4022 && REG_P (new))
4023 || code == STRICT_LOW_PART))
4024 ;
4025
4026 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4027 {
4028 /* In general, don't install a subreg involving two
4029 modes not tieable. It can worsen register
4030 allocation, and can even make invalid reload
4031 insns, since the reg inside may need to be copied
4032 from in the outside mode, and that may be invalid
4033 if it is an fp reg copied in integer mode.
4034
4035 We allow two exceptions to this: It is valid if
4036 it is inside another SUBREG and the mode of that
4037 SUBREG and the mode of the inside of TO is
4038 tieable and it is valid if X is a SET that copies
4039 FROM to CC0. */
4040
4041 if (GET_CODE (to) == SUBREG
4042 && ! MODES_TIEABLE_P (GET_MODE (to),
4043 GET_MODE (SUBREG_REG (to)))
4044 && ! (code == SUBREG
4045 && MODES_TIEABLE_P (GET_MODE (x),
4046 GET_MODE (SUBREG_REG (to))))
4047 #ifdef HAVE_cc0
4048 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4049 #endif
4050 )
4051 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4052
4053 #ifdef CANNOT_CHANGE_MODE_CLASS
4054 if (code == SUBREG
4055 && REG_P (to)
4056 && REGNO (to) < FIRST_PSEUDO_REGISTER
4057 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4058 GET_MODE (to),
4059 GET_MODE (x)))
4060 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4061 #endif
4062
4063 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4064 n_occurrences++;
4065 }
4066 else
4067 /* If we are in a SET_DEST, suppress most cases unless we
4068 have gone inside a MEM, in which case we want to
4069 simplify the address. We assume here that things that
4070 are actually part of the destination have their inner
4071 parts in the first expression. This is true for SUBREG,
4072 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4073 things aside from REG and MEM that should appear in a
4074 SET_DEST. */
4075 new = subst (XEXP (x, i), from, to,
4076 (((in_dest
4077 && (code == SUBREG || code == STRICT_LOW_PART
4078 || code == ZERO_EXTRACT))
4079 || code == SET)
4080 && i == 0), unique_copy);
4081
4082 /* If we found that we will have to reject this combination,
4083 indicate that by returning the CLOBBER ourselves, rather than
4084 an expression containing it. This will speed things up as
4085 well as prevent accidents where two CLOBBERs are considered
4086 to be equal, thus producing an incorrect simplification. */
4087
4088 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
4089 return new;
4090
4091 if (GET_CODE (x) == SUBREG
4092 && (GET_CODE (new) == CONST_INT
4093 || GET_CODE (new) == CONST_DOUBLE))
4094 {
4095 enum machine_mode mode = GET_MODE (x);
4096
4097 x = simplify_subreg (GET_MODE (x), new,
4098 GET_MODE (SUBREG_REG (x)),
4099 SUBREG_BYTE (x));
4100 if (! x)
4101 x = gen_rtx_CLOBBER (mode, const0_rtx);
4102 }
4103 else if (GET_CODE (new) == CONST_INT
4104 && GET_CODE (x) == ZERO_EXTEND)
4105 {
4106 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4107 new, GET_MODE (XEXP (x, 0)));
4108 gcc_assert (x);
4109 }
4110 else
4111 SUBST (XEXP (x, i), new);
4112 }
4113 }
4114 }
4115
4116 /* Try to simplify X. If the simplification changed the code, it is likely
4117 that further simplification will help, so loop, but limit the number
4118 of repetitions that will be performed. */
4119
4120 for (i = 0; i < 4; i++)
4121 {
4122 /* If X is sufficiently simple, don't bother trying to do anything
4123 with it. */
4124 if (code != CONST_INT && code != REG && code != CLOBBER)
4125 x = combine_simplify_rtx (x, op0_mode, in_dest);
4126
4127 if (GET_CODE (x) == code)
4128 break;
4129
4130 code = GET_CODE (x);
4131
4132 /* We no longer know the original mode of operand 0 since we
4133 have changed the form of X) */
4134 op0_mode = VOIDmode;
4135 }
4136
4137 return x;
4138 }
4139 \f
4140 /* Simplify X, a piece of RTL. We just operate on the expression at the
4141 outer level; call `subst' to simplify recursively. Return the new
4142 expression.
4143
4144 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4145 if we are inside a SET_DEST. */
4146
4147 static rtx
4148 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4149 {
4150 enum rtx_code code = GET_CODE (x);
4151 enum machine_mode mode = GET_MODE (x);
4152 rtx temp;
4153 int i;
4154
4155 /* If this is a commutative operation, put a constant last and a complex
4156 expression first. We don't need to do this for comparisons here. */
4157 if (COMMUTATIVE_ARITH_P (x)
4158 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4159 {
4160 temp = XEXP (x, 0);
4161 SUBST (XEXP (x, 0), XEXP (x, 1));
4162 SUBST (XEXP (x, 1), temp);
4163 }
4164
4165 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4166 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4167 things. Check for cases where both arms are testing the same
4168 condition.
4169
4170 Don't do anything if all operands are very simple. */
4171
4172 if ((BINARY_P (x)
4173 && ((!OBJECT_P (XEXP (x, 0))
4174 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4175 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4176 || (!OBJECT_P (XEXP (x, 1))
4177 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4178 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4179 || (UNARY_P (x)
4180 && (!OBJECT_P (XEXP (x, 0))
4181 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4182 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4183 {
4184 rtx cond, true_rtx, false_rtx;
4185
4186 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4187 if (cond != 0
4188 /* If everything is a comparison, what we have is highly unlikely
4189 to be simpler, so don't use it. */
4190 && ! (COMPARISON_P (x)
4191 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4192 {
4193 rtx cop1 = const0_rtx;
4194 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4195
4196 if (cond_code == NE && COMPARISON_P (cond))
4197 return x;
4198
4199 /* Simplify the alternative arms; this may collapse the true and
4200 false arms to store-flag values. Be careful to use copy_rtx
4201 here since true_rtx or false_rtx might share RTL with x as a
4202 result of the if_then_else_cond call above. */
4203 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4204 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4205
4206 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4207 is unlikely to be simpler. */
4208 if (general_operand (true_rtx, VOIDmode)
4209 && general_operand (false_rtx, VOIDmode))
4210 {
4211 enum rtx_code reversed;
4212
4213 /* Restarting if we generate a store-flag expression will cause
4214 us to loop. Just drop through in this case. */
4215
4216 /* If the result values are STORE_FLAG_VALUE and zero, we can
4217 just make the comparison operation. */
4218 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4219 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4220 cond, cop1);
4221 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4222 && ((reversed = reversed_comparison_code_parts
4223 (cond_code, cond, cop1, NULL))
4224 != UNKNOWN))
4225 x = simplify_gen_relational (reversed, mode, VOIDmode,
4226 cond, cop1);
4227
4228 /* Likewise, we can make the negate of a comparison operation
4229 if the result values are - STORE_FLAG_VALUE and zero. */
4230 else if (GET_CODE (true_rtx) == CONST_INT
4231 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4232 && false_rtx == const0_rtx)
4233 x = simplify_gen_unary (NEG, mode,
4234 simplify_gen_relational (cond_code,
4235 mode, VOIDmode,
4236 cond, cop1),
4237 mode);
4238 else if (GET_CODE (false_rtx) == CONST_INT
4239 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4240 && true_rtx == const0_rtx
4241 && ((reversed = reversed_comparison_code_parts
4242 (cond_code, cond, cop1, NULL))
4243 != UNKNOWN))
4244 x = simplify_gen_unary (NEG, mode,
4245 simplify_gen_relational (reversed,
4246 mode, VOIDmode,
4247 cond, cop1),
4248 mode);
4249 else
4250 return gen_rtx_IF_THEN_ELSE (mode,
4251 simplify_gen_relational (cond_code,
4252 mode,
4253 VOIDmode,
4254 cond,
4255 cop1),
4256 true_rtx, false_rtx);
4257
4258 code = GET_CODE (x);
4259 op0_mode = VOIDmode;
4260 }
4261 }
4262 }
4263
4264 /* Try to fold this expression in case we have constants that weren't
4265 present before. */
4266 temp = 0;
4267 switch (GET_RTX_CLASS (code))
4268 {
4269 case RTX_UNARY:
4270 if (op0_mode == VOIDmode)
4271 op0_mode = GET_MODE (XEXP (x, 0));
4272 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4273 break;
4274 case RTX_COMPARE:
4275 case RTX_COMM_COMPARE:
4276 {
4277 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4278 if (cmp_mode == VOIDmode)
4279 {
4280 cmp_mode = GET_MODE (XEXP (x, 1));
4281 if (cmp_mode == VOIDmode)
4282 cmp_mode = op0_mode;
4283 }
4284 temp = simplify_relational_operation (code, mode, cmp_mode,
4285 XEXP (x, 0), XEXP (x, 1));
4286 }
4287 break;
4288 case RTX_COMM_ARITH:
4289 case RTX_BIN_ARITH:
4290 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4291 break;
4292 case RTX_BITFIELD_OPS:
4293 case RTX_TERNARY:
4294 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4295 XEXP (x, 1), XEXP (x, 2));
4296 break;
4297 default:
4298 break;
4299 }
4300
4301 if (temp)
4302 {
4303 x = temp;
4304 code = GET_CODE (temp);
4305 op0_mode = VOIDmode;
4306 mode = GET_MODE (temp);
4307 }
4308
4309 /* First see if we can apply the inverse distributive law. */
4310 if (code == PLUS || code == MINUS
4311 || code == AND || code == IOR || code == XOR)
4312 {
4313 x = apply_distributive_law (x);
4314 code = GET_CODE (x);
4315 op0_mode = VOIDmode;
4316 }
4317
4318 /* If CODE is an associative operation not otherwise handled, see if we
4319 can associate some operands. This can win if they are constants or
4320 if they are logically related (i.e. (a & b) & a). */
4321 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4322 || code == AND || code == IOR || code == XOR
4323 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4324 && ((INTEGRAL_MODE_P (mode) && code != DIV)
4325 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
4326 {
4327 if (GET_CODE (XEXP (x, 0)) == code)
4328 {
4329 rtx other = XEXP (XEXP (x, 0), 0);
4330 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4331 rtx inner_op1 = XEXP (x, 1);
4332 rtx inner;
4333
4334 /* Make sure we pass the constant operand if any as the second
4335 one if this is a commutative operation. */
4336 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4337 {
4338 rtx tem = inner_op0;
4339 inner_op0 = inner_op1;
4340 inner_op1 = tem;
4341 }
4342 inner = simplify_binary_operation (code == MINUS ? PLUS
4343 : code == DIV ? MULT
4344 : code,
4345 mode, inner_op0, inner_op1);
4346
4347 /* For commutative operations, try the other pair if that one
4348 didn't simplify. */
4349 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4350 {
4351 other = XEXP (XEXP (x, 0), 1);
4352 inner = simplify_binary_operation (code, mode,
4353 XEXP (XEXP (x, 0), 0),
4354 XEXP (x, 1));
4355 }
4356
4357 if (inner)
4358 return simplify_gen_binary (code, mode, other, inner);
4359 }
4360 }
4361
4362 /* A little bit of algebraic simplification here. */
4363 switch (code)
4364 {
4365 case MEM:
4366 /* Ensure that our address has any ASHIFTs converted to MULT in case
4367 address-recognizing predicates are called later. */
4368 temp = make_compound_operation (XEXP (x, 0), MEM);
4369 SUBST (XEXP (x, 0), temp);
4370 break;
4371
4372 case SUBREG:
4373 if (op0_mode == VOIDmode)
4374 op0_mode = GET_MODE (SUBREG_REG (x));
4375
4376 /* See if this can be moved to simplify_subreg. */
4377 if (CONSTANT_P (SUBREG_REG (x))
4378 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4379 /* Don't call gen_lowpart if the inner mode
4380 is VOIDmode and we cannot simplify it, as SUBREG without
4381 inner mode is invalid. */
4382 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4383 || gen_lowpart_common (mode, SUBREG_REG (x))))
4384 return gen_lowpart (mode, SUBREG_REG (x));
4385
4386 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4387 break;
4388 {
4389 rtx temp;
4390 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4391 SUBREG_BYTE (x));
4392 if (temp)
4393 return temp;
4394 }
4395
4396 /* Don't change the mode of the MEM if that would change the meaning
4397 of the address. */
4398 if (MEM_P (SUBREG_REG (x))
4399 && (MEM_VOLATILE_P (SUBREG_REG (x))
4400 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4401 return gen_rtx_CLOBBER (mode, const0_rtx);
4402
4403 /* Note that we cannot do any narrowing for non-constants since
4404 we might have been counting on using the fact that some bits were
4405 zero. We now do this in the SET. */
4406
4407 break;
4408
4409 case NEG:
4410 temp = expand_compound_operation (XEXP (x, 0));
4411
4412 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4413 replaced by (lshiftrt X C). This will convert
4414 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4415
4416 if (GET_CODE (temp) == ASHIFTRT
4417 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4418 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4419 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4420 INTVAL (XEXP (temp, 1)));
4421
4422 /* If X has only a single bit that might be nonzero, say, bit I, convert
4423 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4424 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4425 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4426 or a SUBREG of one since we'd be making the expression more
4427 complex if it was just a register. */
4428
4429 if (!REG_P (temp)
4430 && ! (GET_CODE (temp) == SUBREG
4431 && REG_P (SUBREG_REG (temp)))
4432 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4433 {
4434 rtx temp1 = simplify_shift_const
4435 (NULL_RTX, ASHIFTRT, mode,
4436 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4437 GET_MODE_BITSIZE (mode) - 1 - i),
4438 GET_MODE_BITSIZE (mode) - 1 - i);
4439
4440 /* If all we did was surround TEMP with the two shifts, we
4441 haven't improved anything, so don't use it. Otherwise,
4442 we are better off with TEMP1. */
4443 if (GET_CODE (temp1) != ASHIFTRT
4444 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4445 || XEXP (XEXP (temp1, 0), 0) != temp)
4446 return temp1;
4447 }
4448 break;
4449
4450 case TRUNCATE:
4451 /* We can't handle truncation to a partial integer mode here
4452 because we don't know the real bitsize of the partial
4453 integer mode. */
4454 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4455 break;
4456
4457 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4458 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4459 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4460 SUBST (XEXP (x, 0),
4461 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4462 GET_MODE_MASK (mode), 0));
4463
4464 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4465 whose value is a comparison can be replaced with a subreg if
4466 STORE_FLAG_VALUE permits. */
4467 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4468 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4469 && (temp = get_last_value (XEXP (x, 0)))
4470 && COMPARISON_P (temp))
4471 return gen_lowpart (mode, XEXP (x, 0));
4472 break;
4473
4474 #ifdef HAVE_cc0
4475 case COMPARE:
4476 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4477 using cc0, in which case we want to leave it as a COMPARE
4478 so we can distinguish it from a register-register-copy. */
4479 if (XEXP (x, 1) == const0_rtx)
4480 return XEXP (x, 0);
4481
4482 /* x - 0 is the same as x unless x's mode has signed zeros and
4483 allows rounding towards -infinity. Under those conditions,
4484 0 - 0 is -0. */
4485 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4486 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4487 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4488 return XEXP (x, 0);
4489 break;
4490 #endif
4491
4492 case CONST:
4493 /* (const (const X)) can become (const X). Do it this way rather than
4494 returning the inner CONST since CONST can be shared with a
4495 REG_EQUAL note. */
4496 if (GET_CODE (XEXP (x, 0)) == CONST)
4497 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4498 break;
4499
4500 #ifdef HAVE_lo_sum
4501 case LO_SUM:
4502 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4503 can add in an offset. find_split_point will split this address up
4504 again if it doesn't match. */
4505 if (GET_CODE (XEXP (x, 0)) == HIGH
4506 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4507 return XEXP (x, 1);
4508 break;
4509 #endif
4510
4511 case PLUS:
4512 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4513 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4514 bit-field and can be replaced by either a sign_extend or a
4515 sign_extract. The `and' may be a zero_extend and the two
4516 <c>, -<c> constants may be reversed. */
4517 if (GET_CODE (XEXP (x, 0)) == XOR
4518 && GET_CODE (XEXP (x, 1)) == CONST_INT
4519 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4520 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4521 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4522 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4523 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4524 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4525 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4526 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4527 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4528 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4529 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4530 == (unsigned int) i + 1))))
4531 return simplify_shift_const
4532 (NULL_RTX, ASHIFTRT, mode,
4533 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4534 XEXP (XEXP (XEXP (x, 0), 0), 0),
4535 GET_MODE_BITSIZE (mode) - (i + 1)),
4536 GET_MODE_BITSIZE (mode) - (i + 1));
4537
4538 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4539 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4540 the bitsize of the mode - 1. This allows simplification of
4541 "a = (b & 8) == 0;" */
4542 if (XEXP (x, 1) == constm1_rtx
4543 && !REG_P (XEXP (x, 0))
4544 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4545 && REG_P (SUBREG_REG (XEXP (x, 0))))
4546 && nonzero_bits (XEXP (x, 0), mode) == 1)
4547 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4548 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4549 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4550 GET_MODE_BITSIZE (mode) - 1),
4551 GET_MODE_BITSIZE (mode) - 1);
4552
4553 /* If we are adding two things that have no bits in common, convert
4554 the addition into an IOR. This will often be further simplified,
4555 for example in cases like ((a & 1) + (a & 2)), which can
4556 become a & 3. */
4557
4558 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4559 && (nonzero_bits (XEXP (x, 0), mode)
4560 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4561 {
4562 /* Try to simplify the expression further. */
4563 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4564 temp = combine_simplify_rtx (tor, mode, in_dest);
4565
4566 /* If we could, great. If not, do not go ahead with the IOR
4567 replacement, since PLUS appears in many special purpose
4568 address arithmetic instructions. */
4569 if (GET_CODE (temp) != CLOBBER && temp != tor)
4570 return temp;
4571 }
4572 break;
4573
4574 case MINUS:
4575 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4576 (and <foo> (const_int pow2-1)) */
4577 if (GET_CODE (XEXP (x, 1)) == AND
4578 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4579 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4580 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4581 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4582 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4583 break;
4584
4585 case MULT:
4586 /* If we have (mult (plus A B) C), apply the distributive law and then
4587 the inverse distributive law to see if things simplify. This
4588 occurs mostly in addresses, often when unrolling loops. */
4589
4590 if (GET_CODE (XEXP (x, 0)) == PLUS)
4591 {
4592 rtx result = distribute_and_simplify_rtx (x, 0);
4593 if (result)
4594 return result;
4595 }
4596
4597 /* Try simplify a*(b/c) as (a*b)/c. */
4598 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4599 && GET_CODE (XEXP (x, 0)) == DIV)
4600 {
4601 rtx tem = simplify_binary_operation (MULT, mode,
4602 XEXP (XEXP (x, 0), 0),
4603 XEXP (x, 1));
4604 if (tem)
4605 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4606 }
4607 break;
4608
4609 case UDIV:
4610 /* If this is a divide by a power of two, treat it as a shift if
4611 its first operand is a shift. */
4612 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4613 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4614 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4615 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4616 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4617 || GET_CODE (XEXP (x, 0)) == ROTATE
4618 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4619 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4620 break;
4621
4622 case EQ: case NE:
4623 case GT: case GTU: case GE: case GEU:
4624 case LT: case LTU: case LE: case LEU:
4625 case UNEQ: case LTGT:
4626 case UNGT: case UNGE:
4627 case UNLT: case UNLE:
4628 case UNORDERED: case ORDERED:
4629 /* If the first operand is a condition code, we can't do anything
4630 with it. */
4631 if (GET_CODE (XEXP (x, 0)) == COMPARE
4632 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4633 && ! CC0_P (XEXP (x, 0))))
4634 {
4635 rtx op0 = XEXP (x, 0);
4636 rtx op1 = XEXP (x, 1);
4637 enum rtx_code new_code;
4638
4639 if (GET_CODE (op0) == COMPARE)
4640 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4641
4642 /* Simplify our comparison, if possible. */
4643 new_code = simplify_comparison (code, &op0, &op1);
4644
4645 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4646 if only the low-order bit is possibly nonzero in X (such as when
4647 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4648 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4649 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4650 (plus X 1).
4651
4652 Remove any ZERO_EXTRACT we made when thinking this was a
4653 comparison. It may now be simpler to use, e.g., an AND. If a
4654 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4655 the call to make_compound_operation in the SET case. */
4656
4657 if (STORE_FLAG_VALUE == 1
4658 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4659 && op1 == const0_rtx
4660 && mode == GET_MODE (op0)
4661 && nonzero_bits (op0, mode) == 1)
4662 return gen_lowpart (mode,
4663 expand_compound_operation (op0));
4664
4665 else if (STORE_FLAG_VALUE == 1
4666 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4667 && op1 == const0_rtx
4668 && mode == GET_MODE (op0)
4669 && (num_sign_bit_copies (op0, mode)
4670 == GET_MODE_BITSIZE (mode)))
4671 {
4672 op0 = expand_compound_operation (op0);
4673 return simplify_gen_unary (NEG, mode,
4674 gen_lowpart (mode, op0),
4675 mode);
4676 }
4677
4678 else if (STORE_FLAG_VALUE == 1
4679 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4680 && op1 == const0_rtx
4681 && mode == GET_MODE (op0)
4682 && nonzero_bits (op0, mode) == 1)
4683 {
4684 op0 = expand_compound_operation (op0);
4685 return simplify_gen_binary (XOR, mode,
4686 gen_lowpart (mode, op0),
4687 const1_rtx);
4688 }
4689
4690 else if (STORE_FLAG_VALUE == 1
4691 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4692 && op1 == const0_rtx
4693 && mode == GET_MODE (op0)
4694 && (num_sign_bit_copies (op0, mode)
4695 == GET_MODE_BITSIZE (mode)))
4696 {
4697 op0 = expand_compound_operation (op0);
4698 return plus_constant (gen_lowpart (mode, op0), 1);
4699 }
4700
4701 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4702 those above. */
4703 if (STORE_FLAG_VALUE == -1
4704 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4705 && op1 == const0_rtx
4706 && (num_sign_bit_copies (op0, mode)
4707 == GET_MODE_BITSIZE (mode)))
4708 return gen_lowpart (mode,
4709 expand_compound_operation (op0));
4710
4711 else if (STORE_FLAG_VALUE == -1
4712 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4713 && op1 == const0_rtx
4714 && mode == GET_MODE (op0)
4715 && nonzero_bits (op0, mode) == 1)
4716 {
4717 op0 = expand_compound_operation (op0);
4718 return simplify_gen_unary (NEG, mode,
4719 gen_lowpart (mode, op0),
4720 mode);
4721 }
4722
4723 else if (STORE_FLAG_VALUE == -1
4724 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4725 && op1 == const0_rtx
4726 && mode == GET_MODE (op0)
4727 && (num_sign_bit_copies (op0, mode)
4728 == GET_MODE_BITSIZE (mode)))
4729 {
4730 op0 = expand_compound_operation (op0);
4731 return simplify_gen_unary (NOT, mode,
4732 gen_lowpart (mode, op0),
4733 mode);
4734 }
4735
4736 /* If X is 0/1, (eq X 0) is X-1. */
4737 else if (STORE_FLAG_VALUE == -1
4738 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4739 && op1 == const0_rtx
4740 && mode == GET_MODE (op0)
4741 && nonzero_bits (op0, mode) == 1)
4742 {
4743 op0 = expand_compound_operation (op0);
4744 return plus_constant (gen_lowpart (mode, op0), -1);
4745 }
4746
4747 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4748 one bit that might be nonzero, we can convert (ne x 0) to
4749 (ashift x c) where C puts the bit in the sign bit. Remove any
4750 AND with STORE_FLAG_VALUE when we are done, since we are only
4751 going to test the sign bit. */
4752 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4753 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4754 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4755 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4756 && op1 == const0_rtx
4757 && mode == GET_MODE (op0)
4758 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4759 {
4760 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4761 expand_compound_operation (op0),
4762 GET_MODE_BITSIZE (mode) - 1 - i);
4763 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4764 return XEXP (x, 0);
4765 else
4766 return x;
4767 }
4768
4769 /* If the code changed, return a whole new comparison. */
4770 if (new_code != code)
4771 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4772
4773 /* Otherwise, keep this operation, but maybe change its operands.
4774 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4775 SUBST (XEXP (x, 0), op0);
4776 SUBST (XEXP (x, 1), op1);
4777 }
4778 break;
4779
4780 case IF_THEN_ELSE:
4781 return simplify_if_then_else (x);
4782
4783 case ZERO_EXTRACT:
4784 case SIGN_EXTRACT:
4785 case ZERO_EXTEND:
4786 case SIGN_EXTEND:
4787 /* If we are processing SET_DEST, we are done. */
4788 if (in_dest)
4789 return x;
4790
4791 return expand_compound_operation (x);
4792
4793 case SET:
4794 return simplify_set (x);
4795
4796 case AND:
4797 case IOR:
4798 return simplify_logical (x);
4799
4800 case ASHIFT:
4801 case LSHIFTRT:
4802 case ASHIFTRT:
4803 case ROTATE:
4804 case ROTATERT:
4805 /* If this is a shift by a constant amount, simplify it. */
4806 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4807 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4808 INTVAL (XEXP (x, 1)));
4809
4810 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
4811 SUBST (XEXP (x, 1),
4812 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4813 ((HOST_WIDE_INT) 1
4814 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4815 - 1,
4816 0));
4817 break;
4818
4819 default:
4820 break;
4821 }
4822
4823 return x;
4824 }
4825 \f
4826 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4827
4828 static rtx
4829 simplify_if_then_else (rtx x)
4830 {
4831 enum machine_mode mode = GET_MODE (x);
4832 rtx cond = XEXP (x, 0);
4833 rtx true_rtx = XEXP (x, 1);
4834 rtx false_rtx = XEXP (x, 2);
4835 enum rtx_code true_code = GET_CODE (cond);
4836 int comparison_p = COMPARISON_P (cond);
4837 rtx temp;
4838 int i;
4839 enum rtx_code false_code;
4840 rtx reversed;
4841
4842 /* Simplify storing of the truth value. */
4843 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4844 return simplify_gen_relational (true_code, mode, VOIDmode,
4845 XEXP (cond, 0), XEXP (cond, 1));
4846
4847 /* Also when the truth value has to be reversed. */
4848 if (comparison_p
4849 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4850 && (reversed = reversed_comparison (cond, mode)))
4851 return reversed;
4852
4853 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4854 in it is being compared against certain values. Get the true and false
4855 comparisons and see if that says anything about the value of each arm. */
4856
4857 if (comparison_p
4858 && ((false_code = reversed_comparison_code (cond, NULL))
4859 != UNKNOWN)
4860 && REG_P (XEXP (cond, 0)))
4861 {
4862 HOST_WIDE_INT nzb;
4863 rtx from = XEXP (cond, 0);
4864 rtx true_val = XEXP (cond, 1);
4865 rtx false_val = true_val;
4866 int swapped = 0;
4867
4868 /* If FALSE_CODE is EQ, swap the codes and arms. */
4869
4870 if (false_code == EQ)
4871 {
4872 swapped = 1, true_code = EQ, false_code = NE;
4873 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4874 }
4875
4876 /* If we are comparing against zero and the expression being tested has
4877 only a single bit that might be nonzero, that is its value when it is
4878 not equal to zero. Similarly if it is known to be -1 or 0. */
4879
4880 if (true_code == EQ && true_val == const0_rtx
4881 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4882 false_code = EQ, false_val = GEN_INT (nzb);
4883 else if (true_code == EQ && true_val == const0_rtx
4884 && (num_sign_bit_copies (from, GET_MODE (from))
4885 == GET_MODE_BITSIZE (GET_MODE (from))))
4886 false_code = EQ, false_val = constm1_rtx;
4887
4888 /* Now simplify an arm if we know the value of the register in the
4889 branch and it is used in the arm. Be careful due to the potential
4890 of locally-shared RTL. */
4891
4892 if (reg_mentioned_p (from, true_rtx))
4893 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4894 from, true_val),
4895 pc_rtx, pc_rtx, 0, 0);
4896 if (reg_mentioned_p (from, false_rtx))
4897 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4898 from, false_val),
4899 pc_rtx, pc_rtx, 0, 0);
4900
4901 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4902 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4903
4904 true_rtx = XEXP (x, 1);
4905 false_rtx = XEXP (x, 2);
4906 true_code = GET_CODE (cond);
4907 }
4908
4909 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4910 reversed, do so to avoid needing two sets of patterns for
4911 subtract-and-branch insns. Similarly if we have a constant in the true
4912 arm, the false arm is the same as the first operand of the comparison, or
4913 the false arm is more complicated than the true arm. */
4914
4915 if (comparison_p
4916 && reversed_comparison_code (cond, NULL) != UNKNOWN
4917 && (true_rtx == pc_rtx
4918 || (CONSTANT_P (true_rtx)
4919 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4920 || true_rtx == const0_rtx
4921 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4922 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4923 && !OBJECT_P (false_rtx))
4924 || reg_mentioned_p (true_rtx, false_rtx)
4925 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4926 {
4927 true_code = reversed_comparison_code (cond, NULL);
4928 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
4929 SUBST (XEXP (x, 1), false_rtx);
4930 SUBST (XEXP (x, 2), true_rtx);
4931
4932 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4933 cond = XEXP (x, 0);
4934
4935 /* It is possible that the conditional has been simplified out. */
4936 true_code = GET_CODE (cond);
4937 comparison_p = COMPARISON_P (cond);
4938 }
4939
4940 /* If the two arms are identical, we don't need the comparison. */
4941
4942 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4943 return true_rtx;
4944
4945 /* Convert a == b ? b : a to "a". */
4946 if (true_code == EQ && ! side_effects_p (cond)
4947 && !HONOR_NANS (mode)
4948 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4949 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4950 return false_rtx;
4951 else if (true_code == NE && ! side_effects_p (cond)
4952 && !HONOR_NANS (mode)
4953 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4954 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4955 return true_rtx;
4956
4957 /* Look for cases where we have (abs x) or (neg (abs X)). */
4958
4959 if (GET_MODE_CLASS (mode) == MODE_INT
4960 && GET_CODE (false_rtx) == NEG
4961 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4962 && comparison_p
4963 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4964 && ! side_effects_p (true_rtx))
4965 switch (true_code)
4966 {
4967 case GT:
4968 case GE:
4969 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4970 case LT:
4971 case LE:
4972 return
4973 simplify_gen_unary (NEG, mode,
4974 simplify_gen_unary (ABS, mode, true_rtx, mode),
4975 mode);
4976 default:
4977 break;
4978 }
4979
4980 /* Look for MIN or MAX. */
4981
4982 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4983 && comparison_p
4984 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4985 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4986 && ! side_effects_p (cond))
4987 switch (true_code)
4988 {
4989 case GE:
4990 case GT:
4991 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
4992 case LE:
4993 case LT:
4994 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
4995 case GEU:
4996 case GTU:
4997 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
4998 case LEU:
4999 case LTU:
5000 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5001 default:
5002 break;
5003 }
5004
5005 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5006 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5007 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5008 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5009 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5010 neither 1 or -1, but it isn't worth checking for. */
5011
5012 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5013 && comparison_p
5014 && GET_MODE_CLASS (mode) == MODE_INT
5015 && ! side_effects_p (x))
5016 {
5017 rtx t = make_compound_operation (true_rtx, SET);
5018 rtx f = make_compound_operation (false_rtx, SET);
5019 rtx cond_op0 = XEXP (cond, 0);
5020 rtx cond_op1 = XEXP (cond, 1);
5021 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5022 enum machine_mode m = mode;
5023 rtx z = 0, c1 = NULL_RTX;
5024
5025 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5026 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5027 || GET_CODE (t) == ASHIFT
5028 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5029 && rtx_equal_p (XEXP (t, 0), f))
5030 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5031
5032 /* If an identity-zero op is commutative, check whether there
5033 would be a match if we swapped the operands. */
5034 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5035 || GET_CODE (t) == XOR)
5036 && rtx_equal_p (XEXP (t, 1), f))
5037 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5038 else if (GET_CODE (t) == SIGN_EXTEND
5039 && (GET_CODE (XEXP (t, 0)) == PLUS
5040 || GET_CODE (XEXP (t, 0)) == MINUS
5041 || GET_CODE (XEXP (t, 0)) == IOR
5042 || GET_CODE (XEXP (t, 0)) == XOR
5043 || GET_CODE (XEXP (t, 0)) == ASHIFT
5044 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5045 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5046 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5047 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5048 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5049 && (num_sign_bit_copies (f, GET_MODE (f))
5050 > (unsigned int)
5051 (GET_MODE_BITSIZE (mode)
5052 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5053 {
5054 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5055 extend_op = SIGN_EXTEND;
5056 m = GET_MODE (XEXP (t, 0));
5057 }
5058 else if (GET_CODE (t) == SIGN_EXTEND
5059 && (GET_CODE (XEXP (t, 0)) == PLUS
5060 || GET_CODE (XEXP (t, 0)) == IOR
5061 || GET_CODE (XEXP (t, 0)) == XOR)
5062 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5063 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5064 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5065 && (num_sign_bit_copies (f, GET_MODE (f))
5066 > (unsigned int)
5067 (GET_MODE_BITSIZE (mode)
5068 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5069 {
5070 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5071 extend_op = SIGN_EXTEND;
5072 m = GET_MODE (XEXP (t, 0));
5073 }
5074 else if (GET_CODE (t) == ZERO_EXTEND
5075 && (GET_CODE (XEXP (t, 0)) == PLUS
5076 || GET_CODE (XEXP (t, 0)) == MINUS
5077 || GET_CODE (XEXP (t, 0)) == IOR
5078 || GET_CODE (XEXP (t, 0)) == XOR
5079 || GET_CODE (XEXP (t, 0)) == ASHIFT
5080 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5081 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5082 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5083 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5084 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5085 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5086 && ((nonzero_bits (f, GET_MODE (f))
5087 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5088 == 0))
5089 {
5090 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5091 extend_op = ZERO_EXTEND;
5092 m = GET_MODE (XEXP (t, 0));
5093 }
5094 else if (GET_CODE (t) == ZERO_EXTEND
5095 && (GET_CODE (XEXP (t, 0)) == PLUS
5096 || GET_CODE (XEXP (t, 0)) == IOR
5097 || GET_CODE (XEXP (t, 0)) == XOR)
5098 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5099 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5100 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5101 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5102 && ((nonzero_bits (f, GET_MODE (f))
5103 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5104 == 0))
5105 {
5106 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5107 extend_op = ZERO_EXTEND;
5108 m = GET_MODE (XEXP (t, 0));
5109 }
5110
5111 if (z)
5112 {
5113 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5114 cond_op0, cond_op1),
5115 pc_rtx, pc_rtx, 0, 0);
5116 temp = simplify_gen_binary (MULT, m, temp,
5117 simplify_gen_binary (MULT, m, c1,
5118 const_true_rtx));
5119 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5120 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5121
5122 if (extend_op != UNKNOWN)
5123 temp = simplify_gen_unary (extend_op, mode, temp, m);
5124
5125 return temp;
5126 }
5127 }
5128
5129 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5130 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5131 negation of a single bit, we can convert this operation to a shift. We
5132 can actually do this more generally, but it doesn't seem worth it. */
5133
5134 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5135 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5136 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5137 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5138 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5139 == GET_MODE_BITSIZE (mode))
5140 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5141 return
5142 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5143 gen_lowpart (mode, XEXP (cond, 0)), i);
5144
5145 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5146 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5147 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5148 && GET_MODE (XEXP (cond, 0)) == mode
5149 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5150 == nonzero_bits (XEXP (cond, 0), mode)
5151 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5152 return XEXP (cond, 0);
5153
5154 return x;
5155 }
5156 \f
5157 /* Simplify X, a SET expression. Return the new expression. */
5158
5159 static rtx
5160 simplify_set (rtx x)
5161 {
5162 rtx src = SET_SRC (x);
5163 rtx dest = SET_DEST (x);
5164 enum machine_mode mode
5165 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5166 rtx other_insn;
5167 rtx *cc_use;
5168
5169 /* (set (pc) (return)) gets written as (return). */
5170 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5171 return src;
5172
5173 /* Now that we know for sure which bits of SRC we are using, see if we can
5174 simplify the expression for the object knowing that we only need the
5175 low-order bits. */
5176
5177 if (GET_MODE_CLASS (mode) == MODE_INT
5178 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5179 {
5180 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5181 SUBST (SET_SRC (x), src);
5182 }
5183
5184 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5185 the comparison result and try to simplify it unless we already have used
5186 undobuf.other_insn. */
5187 if ((GET_MODE_CLASS (mode) == MODE_CC
5188 || GET_CODE (src) == COMPARE
5189 || CC0_P (dest))
5190 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5191 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5192 && COMPARISON_P (*cc_use)
5193 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5194 {
5195 enum rtx_code old_code = GET_CODE (*cc_use);
5196 enum rtx_code new_code;
5197 rtx op0, op1, tmp;
5198 int other_changed = 0;
5199 enum machine_mode compare_mode = GET_MODE (dest);
5200
5201 if (GET_CODE (src) == COMPARE)
5202 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5203 else
5204 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5205
5206 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5207 op0, op1);
5208 if (!tmp)
5209 new_code = old_code;
5210 else if (!CONSTANT_P (tmp))
5211 {
5212 new_code = GET_CODE (tmp);
5213 op0 = XEXP (tmp, 0);
5214 op1 = XEXP (tmp, 1);
5215 }
5216 else
5217 {
5218 rtx pat = PATTERN (other_insn);
5219 undobuf.other_insn = other_insn;
5220 SUBST (*cc_use, tmp);
5221
5222 /* Attempt to simplify CC user. */
5223 if (GET_CODE (pat) == SET)
5224 {
5225 rtx new = simplify_rtx (SET_SRC (pat));
5226 if (new != NULL_RTX)
5227 SUBST (SET_SRC (pat), new);
5228 }
5229
5230 /* Convert X into a no-op move. */
5231 SUBST (SET_DEST (x), pc_rtx);
5232 SUBST (SET_SRC (x), pc_rtx);
5233 return x;
5234 }
5235
5236 /* Simplify our comparison, if possible. */
5237 new_code = simplify_comparison (new_code, &op0, &op1);
5238
5239 #ifdef SELECT_CC_MODE
5240 /* If this machine has CC modes other than CCmode, check to see if we
5241 need to use a different CC mode here. */
5242 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5243 compare_mode = GET_MODE (op0);
5244 else
5245 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5246
5247 #ifndef HAVE_cc0
5248 /* If the mode changed, we have to change SET_DEST, the mode in the
5249 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5250 a hard register, just build new versions with the proper mode. If it
5251 is a pseudo, we lose unless it is only time we set the pseudo, in
5252 which case we can safely change its mode. */
5253 if (compare_mode != GET_MODE (dest))
5254 {
5255 if (can_change_dest_mode (dest, 0, compare_mode))
5256 {
5257 unsigned int regno = REGNO (dest);
5258 rtx new_dest;
5259
5260 if (regno < FIRST_PSEUDO_REGISTER)
5261 new_dest = gen_rtx_REG (compare_mode, regno);
5262 else
5263 {
5264 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5265 new_dest = regno_reg_rtx[regno];
5266 }
5267
5268 SUBST (SET_DEST (x), new_dest);
5269 SUBST (XEXP (*cc_use, 0), new_dest);
5270 other_changed = 1;
5271
5272 dest = new_dest;
5273 }
5274 }
5275 #endif /* cc0 */
5276 #endif /* SELECT_CC_MODE */
5277
5278 /* If the code changed, we have to build a new comparison in
5279 undobuf.other_insn. */
5280 if (new_code != old_code)
5281 {
5282 int other_changed_previously = other_changed;
5283 unsigned HOST_WIDE_INT mask;
5284
5285 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5286 dest, const0_rtx));
5287 other_changed = 1;
5288
5289 /* If the only change we made was to change an EQ into an NE or
5290 vice versa, OP0 has only one bit that might be nonzero, and OP1
5291 is zero, check if changing the user of the condition code will
5292 produce a valid insn. If it won't, we can keep the original code
5293 in that insn by surrounding our operation with an XOR. */
5294
5295 if (((old_code == NE && new_code == EQ)
5296 || (old_code == EQ && new_code == NE))
5297 && ! other_changed_previously && op1 == const0_rtx
5298 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5299 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5300 {
5301 rtx pat = PATTERN (other_insn), note = 0;
5302
5303 if ((recog_for_combine (&pat, other_insn, &note) < 0
5304 && ! check_asm_operands (pat)))
5305 {
5306 PUT_CODE (*cc_use, old_code);
5307 other_changed = 0;
5308
5309 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5310 op0, GEN_INT (mask));
5311 }
5312 }
5313 }
5314
5315 if (other_changed)
5316 undobuf.other_insn = other_insn;
5317
5318 #ifdef HAVE_cc0
5319 /* If we are now comparing against zero, change our source if
5320 needed. If we do not use cc0, we always have a COMPARE. */
5321 if (op1 == const0_rtx && dest == cc0_rtx)
5322 {
5323 SUBST (SET_SRC (x), op0);
5324 src = op0;
5325 }
5326 else
5327 #endif
5328
5329 /* Otherwise, if we didn't previously have a COMPARE in the
5330 correct mode, we need one. */
5331 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5332 {
5333 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5334 src = SET_SRC (x);
5335 }
5336 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5337 {
5338 SUBST(SET_SRC (x), op0);
5339 src = SET_SRC (x);
5340 }
5341 else
5342 {
5343 /* Otherwise, update the COMPARE if needed. */
5344 SUBST (XEXP (src, 0), op0);
5345 SUBST (XEXP (src, 1), op1);
5346 }
5347 }
5348 else
5349 {
5350 /* Get SET_SRC in a form where we have placed back any
5351 compound expressions. Then do the checks below. */
5352 src = make_compound_operation (src, SET);
5353 SUBST (SET_SRC (x), src);
5354 }
5355
5356 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5357 and X being a REG or (subreg (reg)), we may be able to convert this to
5358 (set (subreg:m2 x) (op)).
5359
5360 We can always do this if M1 is narrower than M2 because that means that
5361 we only care about the low bits of the result.
5362
5363 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5364 perform a narrower operation than requested since the high-order bits will
5365 be undefined. On machine where it is defined, this transformation is safe
5366 as long as M1 and M2 have the same number of words. */
5367
5368 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5369 && !OBJECT_P (SUBREG_REG (src))
5370 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5371 / UNITS_PER_WORD)
5372 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5373 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5374 #ifndef WORD_REGISTER_OPERATIONS
5375 && (GET_MODE_SIZE (GET_MODE (src))
5376 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5377 #endif
5378 #ifdef CANNOT_CHANGE_MODE_CLASS
5379 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5380 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5381 GET_MODE (SUBREG_REG (src)),
5382 GET_MODE (src)))
5383 #endif
5384 && (REG_P (dest)
5385 || (GET_CODE (dest) == SUBREG
5386 && REG_P (SUBREG_REG (dest)))))
5387 {
5388 SUBST (SET_DEST (x),
5389 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5390 dest));
5391 SUBST (SET_SRC (x), SUBREG_REG (src));
5392
5393 src = SET_SRC (x), dest = SET_DEST (x);
5394 }
5395
5396 #ifdef HAVE_cc0
5397 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5398 in SRC. */
5399 if (dest == cc0_rtx
5400 && GET_CODE (src) == SUBREG
5401 && subreg_lowpart_p (src)
5402 && (GET_MODE_BITSIZE (GET_MODE (src))
5403 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5404 {
5405 rtx inner = SUBREG_REG (src);
5406 enum machine_mode inner_mode = GET_MODE (inner);
5407
5408 /* Here we make sure that we don't have a sign bit on. */
5409 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5410 && (nonzero_bits (inner, inner_mode)
5411 < ((unsigned HOST_WIDE_INT) 1
5412 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5413 {
5414 SUBST (SET_SRC (x), inner);
5415 src = SET_SRC (x);
5416 }
5417 }
5418 #endif
5419
5420 #ifdef LOAD_EXTEND_OP
5421 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5422 would require a paradoxical subreg. Replace the subreg with a
5423 zero_extend to avoid the reload that would otherwise be required. */
5424
5425 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5426 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5427 && SUBREG_BYTE (src) == 0
5428 && (GET_MODE_SIZE (GET_MODE (src))
5429 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5430 && MEM_P (SUBREG_REG (src)))
5431 {
5432 SUBST (SET_SRC (x),
5433 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5434 GET_MODE (src), SUBREG_REG (src)));
5435
5436 src = SET_SRC (x);
5437 }
5438 #endif
5439
5440 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5441 are comparing an item known to be 0 or -1 against 0, use a logical
5442 operation instead. Check for one of the arms being an IOR of the other
5443 arm with some value. We compute three terms to be IOR'ed together. In
5444 practice, at most two will be nonzero. Then we do the IOR's. */
5445
5446 if (GET_CODE (dest) != PC
5447 && GET_CODE (src) == IF_THEN_ELSE
5448 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5449 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5450 && XEXP (XEXP (src, 0), 1) == const0_rtx
5451 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5452 #ifdef HAVE_conditional_move
5453 && ! can_conditionally_move_p (GET_MODE (src))
5454 #endif
5455 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5456 GET_MODE (XEXP (XEXP (src, 0), 0)))
5457 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5458 && ! side_effects_p (src))
5459 {
5460 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5461 ? XEXP (src, 1) : XEXP (src, 2));
5462 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5463 ? XEXP (src, 2) : XEXP (src, 1));
5464 rtx term1 = const0_rtx, term2, term3;
5465
5466 if (GET_CODE (true_rtx) == IOR
5467 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5468 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5469 else if (GET_CODE (true_rtx) == IOR
5470 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5471 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5472 else if (GET_CODE (false_rtx) == IOR
5473 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5474 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5475 else if (GET_CODE (false_rtx) == IOR
5476 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5477 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5478
5479 term2 = simplify_gen_binary (AND, GET_MODE (src),
5480 XEXP (XEXP (src, 0), 0), true_rtx);
5481 term3 = simplify_gen_binary (AND, GET_MODE (src),
5482 simplify_gen_unary (NOT, GET_MODE (src),
5483 XEXP (XEXP (src, 0), 0),
5484 GET_MODE (src)),
5485 false_rtx);
5486
5487 SUBST (SET_SRC (x),
5488 simplify_gen_binary (IOR, GET_MODE (src),
5489 simplify_gen_binary (IOR, GET_MODE (src),
5490 term1, term2),
5491 term3));
5492
5493 src = SET_SRC (x);
5494 }
5495
5496 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5497 whole thing fail. */
5498 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5499 return src;
5500 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5501 return dest;
5502 else
5503 /* Convert this into a field assignment operation, if possible. */
5504 return make_field_assignment (x);
5505 }
5506 \f
5507 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5508 result. */
5509
5510 static rtx
5511 simplify_logical (rtx x)
5512 {
5513 enum machine_mode mode = GET_MODE (x);
5514 rtx op0 = XEXP (x, 0);
5515 rtx op1 = XEXP (x, 1);
5516
5517 switch (GET_CODE (x))
5518 {
5519 case AND:
5520 /* We can call simplify_and_const_int only if we don't lose
5521 any (sign) bits when converting INTVAL (op1) to
5522 "unsigned HOST_WIDE_INT". */
5523 if (GET_CODE (op1) == CONST_INT
5524 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5525 || INTVAL (op1) > 0))
5526 {
5527 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5528 if (GET_CODE (x) != AND)
5529 return x;
5530
5531 op0 = XEXP (x, 0);
5532 op1 = XEXP (x, 1);
5533 }
5534
5535 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5536 apply the distributive law and then the inverse distributive
5537 law to see if things simplify. */
5538 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5539 {
5540 rtx result = distribute_and_simplify_rtx (x, 0);
5541 if (result)
5542 return result;
5543 }
5544 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5545 {
5546 rtx result = distribute_and_simplify_rtx (x, 1);
5547 if (result)
5548 return result;
5549 }
5550 break;
5551
5552 case IOR:
5553 /* If we have (ior (and A B) C), apply the distributive law and then
5554 the inverse distributive law to see if things simplify. */
5555
5556 if (GET_CODE (op0) == AND)
5557 {
5558 rtx result = distribute_and_simplify_rtx (x, 0);
5559 if (result)
5560 return result;
5561 }
5562
5563 if (GET_CODE (op1) == AND)
5564 {
5565 rtx result = distribute_and_simplify_rtx (x, 1);
5566 if (result)
5567 return result;
5568 }
5569 break;
5570
5571 default:
5572 gcc_unreachable ();
5573 }
5574
5575 return x;
5576 }
5577 \f
5578 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5579 operations" because they can be replaced with two more basic operations.
5580 ZERO_EXTEND is also considered "compound" because it can be replaced with
5581 an AND operation, which is simpler, though only one operation.
5582
5583 The function expand_compound_operation is called with an rtx expression
5584 and will convert it to the appropriate shifts and AND operations,
5585 simplifying at each stage.
5586
5587 The function make_compound_operation is called to convert an expression
5588 consisting of shifts and ANDs into the equivalent compound expression.
5589 It is the inverse of this function, loosely speaking. */
5590
5591 static rtx
5592 expand_compound_operation (rtx x)
5593 {
5594 unsigned HOST_WIDE_INT pos = 0, len;
5595 int unsignedp = 0;
5596 unsigned int modewidth;
5597 rtx tem;
5598
5599 switch (GET_CODE (x))
5600 {
5601 case ZERO_EXTEND:
5602 unsignedp = 1;
5603 case SIGN_EXTEND:
5604 /* We can't necessarily use a const_int for a multiword mode;
5605 it depends on implicitly extending the value.
5606 Since we don't know the right way to extend it,
5607 we can't tell whether the implicit way is right.
5608
5609 Even for a mode that is no wider than a const_int,
5610 we can't win, because we need to sign extend one of its bits through
5611 the rest of it, and we don't know which bit. */
5612 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5613 return x;
5614
5615 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5616 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5617 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5618 reloaded. If not for that, MEM's would very rarely be safe.
5619
5620 Reject MODEs bigger than a word, because we might not be able
5621 to reference a two-register group starting with an arbitrary register
5622 (and currently gen_lowpart might crash for a SUBREG). */
5623
5624 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5625 return x;
5626
5627 /* Reject MODEs that aren't scalar integers because turning vector
5628 or complex modes into shifts causes problems. */
5629
5630 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5631 return x;
5632
5633 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5634 /* If the inner object has VOIDmode (the only way this can happen
5635 is if it is an ASM_OPERANDS), we can't do anything since we don't
5636 know how much masking to do. */
5637 if (len == 0)
5638 return x;
5639
5640 break;
5641
5642 case ZERO_EXTRACT:
5643 unsignedp = 1;
5644
5645 /* ... fall through ... */
5646
5647 case SIGN_EXTRACT:
5648 /* If the operand is a CLOBBER, just return it. */
5649 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5650 return XEXP (x, 0);
5651
5652 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5653 || GET_CODE (XEXP (x, 2)) != CONST_INT
5654 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5655 return x;
5656
5657 /* Reject MODEs that aren't scalar integers because turning vector
5658 or complex modes into shifts causes problems. */
5659
5660 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5661 return x;
5662
5663 len = INTVAL (XEXP (x, 1));
5664 pos = INTVAL (XEXP (x, 2));
5665
5666 /* This should stay within the object being extracted, fail otherwise. */
5667 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5668 return x;
5669
5670 if (BITS_BIG_ENDIAN)
5671 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5672
5673 break;
5674
5675 default:
5676 return x;
5677 }
5678 /* Convert sign extension to zero extension, if we know that the high
5679 bit is not set, as this is easier to optimize. It will be converted
5680 back to cheaper alternative in make_extraction. */
5681 if (GET_CODE (x) == SIGN_EXTEND
5682 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5683 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5684 & ~(((unsigned HOST_WIDE_INT)
5685 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5686 >> 1))
5687 == 0)))
5688 {
5689 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5690 rtx temp2 = expand_compound_operation (temp);
5691
5692 /* Make sure this is a profitable operation. */
5693 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5694 return temp2;
5695 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5696 return temp;
5697 else
5698 return x;
5699 }
5700
5701 /* We can optimize some special cases of ZERO_EXTEND. */
5702 if (GET_CODE (x) == ZERO_EXTEND)
5703 {
5704 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5705 know that the last value didn't have any inappropriate bits
5706 set. */
5707 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5708 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5709 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5710 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5711 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5712 return XEXP (XEXP (x, 0), 0);
5713
5714 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5715 if (GET_CODE (XEXP (x, 0)) == SUBREG
5716 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5717 && subreg_lowpart_p (XEXP (x, 0))
5718 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5719 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5720 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5721 return SUBREG_REG (XEXP (x, 0));
5722
5723 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5724 is a comparison and STORE_FLAG_VALUE permits. This is like
5725 the first case, but it works even when GET_MODE (x) is larger
5726 than HOST_WIDE_INT. */
5727 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5728 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5729 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5730 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5731 <= HOST_BITS_PER_WIDE_INT)
5732 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5733 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5734 return XEXP (XEXP (x, 0), 0);
5735
5736 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5737 if (GET_CODE (XEXP (x, 0)) == SUBREG
5738 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5739 && subreg_lowpart_p (XEXP (x, 0))
5740 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5741 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5742 <= HOST_BITS_PER_WIDE_INT)
5743 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5744 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5745 return SUBREG_REG (XEXP (x, 0));
5746
5747 }
5748
5749 /* If we reach here, we want to return a pair of shifts. The inner
5750 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5751 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5752 logical depending on the value of UNSIGNEDP.
5753
5754 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5755 converted into an AND of a shift.
5756
5757 We must check for the case where the left shift would have a negative
5758 count. This can happen in a case like (x >> 31) & 255 on machines
5759 that can't shift by a constant. On those machines, we would first
5760 combine the shift with the AND to produce a variable-position
5761 extraction. Then the constant of 31 would be substituted in to produce
5762 a such a position. */
5763
5764 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5765 if (modewidth + len >= pos)
5766 {
5767 enum machine_mode mode = GET_MODE (x);
5768 tem = gen_lowpart (mode, XEXP (x, 0));
5769 if (!tem || GET_CODE (tem) == CLOBBER)
5770 return x;
5771 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5772 tem, modewidth - pos - len);
5773 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5774 mode, tem, modewidth - len);
5775 }
5776 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5777 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5778 simplify_shift_const (NULL_RTX, LSHIFTRT,
5779 GET_MODE (x),
5780 XEXP (x, 0), pos),
5781 ((HOST_WIDE_INT) 1 << len) - 1);
5782 else
5783 /* Any other cases we can't handle. */
5784 return x;
5785
5786 /* If we couldn't do this for some reason, return the original
5787 expression. */
5788 if (GET_CODE (tem) == CLOBBER)
5789 return x;
5790
5791 return tem;
5792 }
5793 \f
5794 /* X is a SET which contains an assignment of one object into
5795 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5796 or certain SUBREGS). If possible, convert it into a series of
5797 logical operations.
5798
5799 We half-heartedly support variable positions, but do not at all
5800 support variable lengths. */
5801
5802 static rtx
5803 expand_field_assignment (rtx x)
5804 {
5805 rtx inner;
5806 rtx pos; /* Always counts from low bit. */
5807 int len;
5808 rtx mask, cleared, masked;
5809 enum machine_mode compute_mode;
5810
5811 /* Loop until we find something we can't simplify. */
5812 while (1)
5813 {
5814 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5815 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5816 {
5817 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5818 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5819 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5820 }
5821 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5822 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5823 {
5824 inner = XEXP (SET_DEST (x), 0);
5825 len = INTVAL (XEXP (SET_DEST (x), 1));
5826 pos = XEXP (SET_DEST (x), 2);
5827
5828 /* A constant position should stay within the width of INNER. */
5829 if (GET_CODE (pos) == CONST_INT
5830 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5831 break;
5832
5833 if (BITS_BIG_ENDIAN)
5834 {
5835 if (GET_CODE (pos) == CONST_INT)
5836 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5837 - INTVAL (pos));
5838 else if (GET_CODE (pos) == MINUS
5839 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5840 && (INTVAL (XEXP (pos, 1))
5841 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5842 /* If position is ADJUST - X, new position is X. */
5843 pos = XEXP (pos, 0);
5844 else
5845 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
5846 GEN_INT (GET_MODE_BITSIZE (
5847 GET_MODE (inner))
5848 - len),
5849 pos);
5850 }
5851 }
5852
5853 /* A SUBREG between two modes that occupy the same numbers of words
5854 can be done by moving the SUBREG to the source. */
5855 else if (GET_CODE (SET_DEST (x)) == SUBREG
5856 /* We need SUBREGs to compute nonzero_bits properly. */
5857 && nonzero_sign_valid
5858 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5859 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5860 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5861 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5862 {
5863 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5864 gen_lowpart
5865 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5866 SET_SRC (x)));
5867 continue;
5868 }
5869 else
5870 break;
5871
5872 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5873 inner = SUBREG_REG (inner);
5874
5875 compute_mode = GET_MODE (inner);
5876
5877 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
5878 if (! SCALAR_INT_MODE_P (compute_mode))
5879 {
5880 enum machine_mode imode;
5881
5882 /* Don't do anything for vector or complex integral types. */
5883 if (! FLOAT_MODE_P (compute_mode))
5884 break;
5885
5886 /* Try to find an integral mode to pun with. */
5887 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5888 if (imode == BLKmode)
5889 break;
5890
5891 compute_mode = imode;
5892 inner = gen_lowpart (imode, inner);
5893 }
5894
5895 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5896 if (len >= HOST_BITS_PER_WIDE_INT)
5897 break;
5898
5899 /* Now compute the equivalent expression. Make a copy of INNER
5900 for the SET_DEST in case it is a MEM into which we will substitute;
5901 we don't want shared RTL in that case. */
5902 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5903 cleared = simplify_gen_binary (AND, compute_mode,
5904 simplify_gen_unary (NOT, compute_mode,
5905 simplify_gen_binary (ASHIFT,
5906 compute_mode,
5907 mask, pos),
5908 compute_mode),
5909 inner);
5910 masked = simplify_gen_binary (ASHIFT, compute_mode,
5911 simplify_gen_binary (
5912 AND, compute_mode,
5913 gen_lowpart (compute_mode, SET_SRC (x)),
5914 mask),
5915 pos);
5916
5917 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
5918 simplify_gen_binary (IOR, compute_mode,
5919 cleared, masked));
5920 }
5921
5922 return x;
5923 }
5924 \f
5925 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5926 it is an RTX that represents a variable starting position; otherwise,
5927 POS is the (constant) starting bit position (counted from the LSB).
5928
5929 UNSIGNEDP is nonzero for an unsigned reference and zero for a
5930 signed reference.
5931
5932 IN_DEST is nonzero if this is a reference in the destination of a
5933 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
5934 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5935 be used.
5936
5937 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
5938 ZERO_EXTRACT should be built even for bits starting at bit 0.
5939
5940 MODE is the desired mode of the result (if IN_DEST == 0).
5941
5942 The result is an RTX for the extraction or NULL_RTX if the target
5943 can't handle it. */
5944
5945 static rtx
5946 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5947 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5948 int in_dest, int in_compare)
5949 {
5950 /* This mode describes the size of the storage area
5951 to fetch the overall value from. Within that, we
5952 ignore the POS lowest bits, etc. */
5953 enum machine_mode is_mode = GET_MODE (inner);
5954 enum machine_mode inner_mode;
5955 enum machine_mode wanted_inner_mode;
5956 enum machine_mode wanted_inner_reg_mode = word_mode;
5957 enum machine_mode pos_mode = word_mode;
5958 enum machine_mode extraction_mode = word_mode;
5959 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5960 rtx new = 0;
5961 rtx orig_pos_rtx = pos_rtx;
5962 HOST_WIDE_INT orig_pos;
5963
5964 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5965 {
5966 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5967 consider just the QI as the memory to extract from.
5968 The subreg adds or removes high bits; its mode is
5969 irrelevant to the meaning of this extraction,
5970 since POS and LEN count from the lsb. */
5971 if (MEM_P (SUBREG_REG (inner)))
5972 is_mode = GET_MODE (SUBREG_REG (inner));
5973 inner = SUBREG_REG (inner);
5974 }
5975 else if (GET_CODE (inner) == ASHIFT
5976 && GET_CODE (XEXP (inner, 1)) == CONST_INT
5977 && pos_rtx == 0 && pos == 0
5978 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
5979 {
5980 /* We're extracting the least significant bits of an rtx
5981 (ashift X (const_int C)), where LEN > C. Extract the
5982 least significant (LEN - C) bits of X, giving an rtx
5983 whose mode is MODE, then shift it left C times. */
5984 new = make_extraction (mode, XEXP (inner, 0),
5985 0, 0, len - INTVAL (XEXP (inner, 1)),
5986 unsignedp, in_dest, in_compare);
5987 if (new != 0)
5988 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
5989 }
5990
5991 inner_mode = GET_MODE (inner);
5992
5993 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5994 pos = INTVAL (pos_rtx), pos_rtx = 0;
5995
5996 /* See if this can be done without an extraction. We never can if the
5997 width of the field is not the same as that of some integer mode. For
5998 registers, we can only avoid the extraction if the position is at the
5999 low-order bit and this is either not in the destination or we have the
6000 appropriate STRICT_LOW_PART operation available.
6001
6002 For MEM, we can avoid an extract if the field starts on an appropriate
6003 boundary and we can change the mode of the memory reference. */
6004
6005 if (tmode != BLKmode
6006 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6007 && !MEM_P (inner)
6008 && (inner_mode == tmode
6009 || !REG_P (inner)
6010 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6011 GET_MODE_BITSIZE (inner_mode))
6012 || reg_truncated_to_mode (tmode, inner))
6013 && (! in_dest
6014 || (REG_P (inner)
6015 && have_insn_for (STRICT_LOW_PART, tmode))))
6016 || (MEM_P (inner) && pos_rtx == 0
6017 && (pos
6018 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6019 : BITS_PER_UNIT)) == 0
6020 /* We can't do this if we are widening INNER_MODE (it
6021 may not be aligned, for one thing). */
6022 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6023 && (inner_mode == tmode
6024 || (! mode_dependent_address_p (XEXP (inner, 0))
6025 && ! MEM_VOLATILE_P (inner))))))
6026 {
6027 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6028 field. If the original and current mode are the same, we need not
6029 adjust the offset. Otherwise, we do if bytes big endian.
6030
6031 If INNER is not a MEM, get a piece consisting of just the field
6032 of interest (in this case POS % BITS_PER_WORD must be 0). */
6033
6034 if (MEM_P (inner))
6035 {
6036 HOST_WIDE_INT offset;
6037
6038 /* POS counts from lsb, but make OFFSET count in memory order. */
6039 if (BYTES_BIG_ENDIAN)
6040 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6041 else
6042 offset = pos / BITS_PER_UNIT;
6043
6044 new = adjust_address_nv (inner, tmode, offset);
6045 }
6046 else if (REG_P (inner))
6047 {
6048 if (tmode != inner_mode)
6049 {
6050 /* We can't call gen_lowpart in a DEST since we
6051 always want a SUBREG (see below) and it would sometimes
6052 return a new hard register. */
6053 if (pos || in_dest)
6054 {
6055 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6056
6057 if (WORDS_BIG_ENDIAN
6058 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6059 final_word = ((GET_MODE_SIZE (inner_mode)
6060 - GET_MODE_SIZE (tmode))
6061 / UNITS_PER_WORD) - final_word;
6062
6063 final_word *= UNITS_PER_WORD;
6064 if (BYTES_BIG_ENDIAN &&
6065 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6066 final_word += (GET_MODE_SIZE (inner_mode)
6067 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6068
6069 /* Avoid creating invalid subregs, for example when
6070 simplifying (x>>32)&255. */
6071 if (!validate_subreg (tmode, inner_mode, inner, final_word))
6072 return NULL_RTX;
6073
6074 new = gen_rtx_SUBREG (tmode, inner, final_word);
6075 }
6076 else
6077 new = gen_lowpart (tmode, inner);
6078 }
6079 else
6080 new = inner;
6081 }
6082 else
6083 new = force_to_mode (inner, tmode,
6084 len >= HOST_BITS_PER_WIDE_INT
6085 ? ~(unsigned HOST_WIDE_INT) 0
6086 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6087 0);
6088
6089 /* If this extraction is going into the destination of a SET,
6090 make a STRICT_LOW_PART unless we made a MEM. */
6091
6092 if (in_dest)
6093 return (MEM_P (new) ? new
6094 : (GET_CODE (new) != SUBREG
6095 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6096 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6097
6098 if (mode == tmode)
6099 return new;
6100
6101 if (GET_CODE (new) == CONST_INT)
6102 return gen_int_mode (INTVAL (new), mode);
6103
6104 /* If we know that no extraneous bits are set, and that the high
6105 bit is not set, convert the extraction to the cheaper of
6106 sign and zero extension, that are equivalent in these cases. */
6107 if (flag_expensive_optimizations
6108 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6109 && ((nonzero_bits (new, tmode)
6110 & ~(((unsigned HOST_WIDE_INT)
6111 GET_MODE_MASK (tmode))
6112 >> 1))
6113 == 0)))
6114 {
6115 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6116 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6117
6118 /* Prefer ZERO_EXTENSION, since it gives more information to
6119 backends. */
6120 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6121 return temp;
6122 return temp1;
6123 }
6124
6125 /* Otherwise, sign- or zero-extend unless we already are in the
6126 proper mode. */
6127
6128 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6129 mode, new));
6130 }
6131
6132 /* Unless this is a COMPARE or we have a funny memory reference,
6133 don't do anything with zero-extending field extracts starting at
6134 the low-order bit since they are simple AND operations. */
6135 if (pos_rtx == 0 && pos == 0 && ! in_dest
6136 && ! in_compare && unsignedp)
6137 return 0;
6138
6139 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6140 if the position is not a constant and the length is not 1. In all
6141 other cases, we would only be going outside our object in cases when
6142 an original shift would have been undefined. */
6143 if (MEM_P (inner)
6144 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6145 || (pos_rtx != 0 && len != 1)))
6146 return 0;
6147
6148 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6149 and the mode for the result. */
6150 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6151 {
6152 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6153 pos_mode = mode_for_extraction (EP_insv, 2);
6154 extraction_mode = mode_for_extraction (EP_insv, 3);
6155 }
6156
6157 if (! in_dest && unsignedp
6158 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6159 {
6160 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6161 pos_mode = mode_for_extraction (EP_extzv, 3);
6162 extraction_mode = mode_for_extraction (EP_extzv, 0);
6163 }
6164
6165 if (! in_dest && ! unsignedp
6166 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6167 {
6168 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6169 pos_mode = mode_for_extraction (EP_extv, 3);
6170 extraction_mode = mode_for_extraction (EP_extv, 0);
6171 }
6172
6173 /* Never narrow an object, since that might not be safe. */
6174
6175 if (mode != VOIDmode
6176 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6177 extraction_mode = mode;
6178
6179 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6180 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6181 pos_mode = GET_MODE (pos_rtx);
6182
6183 /* If this is not from memory, the desired mode is the preferred mode
6184 for an extraction pattern's first input operand, or word_mode if there
6185 is none. */
6186 if (!MEM_P (inner))
6187 wanted_inner_mode = wanted_inner_reg_mode;
6188 else
6189 {
6190 /* Be careful not to go beyond the extracted object and maintain the
6191 natural alignment of the memory. */
6192 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6193 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6194 > GET_MODE_BITSIZE (wanted_inner_mode))
6195 {
6196 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6197 gcc_assert (wanted_inner_mode != VOIDmode);
6198 }
6199
6200 /* If we have to change the mode of memory and cannot, the desired mode
6201 is EXTRACTION_MODE. */
6202 if (inner_mode != wanted_inner_mode
6203 && (mode_dependent_address_p (XEXP (inner, 0))
6204 || MEM_VOLATILE_P (inner)
6205 || pos_rtx))
6206 wanted_inner_mode = extraction_mode;
6207 }
6208
6209 orig_pos = pos;
6210
6211 if (BITS_BIG_ENDIAN)
6212 {
6213 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6214 BITS_BIG_ENDIAN style. If position is constant, compute new
6215 position. Otherwise, build subtraction.
6216 Note that POS is relative to the mode of the original argument.
6217 If it's a MEM we need to recompute POS relative to that.
6218 However, if we're extracting from (or inserting into) a register,
6219 we want to recompute POS relative to wanted_inner_mode. */
6220 int width = (MEM_P (inner)
6221 ? GET_MODE_BITSIZE (is_mode)
6222 : GET_MODE_BITSIZE (wanted_inner_mode));
6223
6224 if (pos_rtx == 0)
6225 pos = width - len - pos;
6226 else
6227 pos_rtx
6228 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6229 /* POS may be less than 0 now, but we check for that below.
6230 Note that it can only be less than 0 if !MEM_P (inner). */
6231 }
6232
6233 /* If INNER has a wider mode, and this is a constant extraction, try to
6234 make it smaller and adjust the byte to point to the byte containing
6235 the value. */
6236 if (wanted_inner_mode != VOIDmode
6237 && inner_mode != wanted_inner_mode
6238 && ! pos_rtx
6239 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6240 && MEM_P (inner)
6241 && ! mode_dependent_address_p (XEXP (inner, 0))
6242 && ! MEM_VOLATILE_P (inner))
6243 {
6244 int offset = 0;
6245
6246 /* The computations below will be correct if the machine is big
6247 endian in both bits and bytes or little endian in bits and bytes.
6248 If it is mixed, we must adjust. */
6249
6250 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6251 adjust OFFSET to compensate. */
6252 if (BYTES_BIG_ENDIAN
6253 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6254 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6255
6256 /* We can now move to the desired byte. */
6257 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6258 * GET_MODE_SIZE (wanted_inner_mode);
6259 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6260
6261 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6262 && is_mode != wanted_inner_mode)
6263 offset = (GET_MODE_SIZE (is_mode)
6264 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6265
6266 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6267 }
6268
6269 /* If INNER is not memory, we can always get it into the proper mode. If we
6270 are changing its mode, POS must be a constant and smaller than the size
6271 of the new mode. */
6272 else if (!MEM_P (inner))
6273 {
6274 if (GET_MODE (inner) != wanted_inner_mode
6275 && (pos_rtx != 0
6276 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6277 return 0;
6278
6279 if (orig_pos < 0)
6280 return 0;
6281
6282 inner = force_to_mode (inner, wanted_inner_mode,
6283 pos_rtx
6284 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6285 ? ~(unsigned HOST_WIDE_INT) 0
6286 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6287 << orig_pos),
6288 0);
6289 }
6290
6291 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6292 have to zero extend. Otherwise, we can just use a SUBREG. */
6293 if (pos_rtx != 0
6294 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6295 {
6296 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6297
6298 /* If we know that no extraneous bits are set, and that the high
6299 bit is not set, convert extraction to cheaper one - either
6300 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6301 cases. */
6302 if (flag_expensive_optimizations
6303 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6304 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6305 & ~(((unsigned HOST_WIDE_INT)
6306 GET_MODE_MASK (GET_MODE (pos_rtx)))
6307 >> 1))
6308 == 0)))
6309 {
6310 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6311
6312 /* Prefer ZERO_EXTENSION, since it gives more information to
6313 backends. */
6314 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6315 temp = temp1;
6316 }
6317 pos_rtx = temp;
6318 }
6319 else if (pos_rtx != 0
6320 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6321 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6322
6323 /* Make POS_RTX unless we already have it and it is correct. If we don't
6324 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6325 be a CONST_INT. */
6326 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6327 pos_rtx = orig_pos_rtx;
6328
6329 else if (pos_rtx == 0)
6330 pos_rtx = GEN_INT (pos);
6331
6332 /* Make the required operation. See if we can use existing rtx. */
6333 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6334 extraction_mode, inner, GEN_INT (len), pos_rtx);
6335 if (! in_dest)
6336 new = gen_lowpart (mode, new);
6337
6338 return new;
6339 }
6340 \f
6341 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6342 with any other operations in X. Return X without that shift if so. */
6343
6344 static rtx
6345 extract_left_shift (rtx x, int count)
6346 {
6347 enum rtx_code code = GET_CODE (x);
6348 enum machine_mode mode = GET_MODE (x);
6349 rtx tem;
6350
6351 switch (code)
6352 {
6353 case ASHIFT:
6354 /* This is the shift itself. If it is wide enough, we will return
6355 either the value being shifted if the shift count is equal to
6356 COUNT or a shift for the difference. */
6357 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6358 && INTVAL (XEXP (x, 1)) >= count)
6359 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6360 INTVAL (XEXP (x, 1)) - count);
6361 break;
6362
6363 case NEG: case NOT:
6364 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6365 return simplify_gen_unary (code, mode, tem, mode);
6366
6367 break;
6368
6369 case PLUS: case IOR: case XOR: case AND:
6370 /* If we can safely shift this constant and we find the inner shift,
6371 make a new operation. */
6372 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6373 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6374 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6375 return simplify_gen_binary (code, mode, tem,
6376 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6377
6378 break;
6379
6380 default:
6381 break;
6382 }
6383
6384 return 0;
6385 }
6386 \f
6387 /* Look at the expression rooted at X. Look for expressions
6388 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6389 Form these expressions.
6390
6391 Return the new rtx, usually just X.
6392
6393 Also, for machines like the VAX that don't have logical shift insns,
6394 try to convert logical to arithmetic shift operations in cases where
6395 they are equivalent. This undoes the canonicalizations to logical
6396 shifts done elsewhere.
6397
6398 We try, as much as possible, to re-use rtl expressions to save memory.
6399
6400 IN_CODE says what kind of expression we are processing. Normally, it is
6401 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6402 being kludges), it is MEM. When processing the arguments of a comparison
6403 or a COMPARE against zero, it is COMPARE. */
6404
6405 static rtx
6406 make_compound_operation (rtx x, enum rtx_code in_code)
6407 {
6408 enum rtx_code code = GET_CODE (x);
6409 enum machine_mode mode = GET_MODE (x);
6410 int mode_width = GET_MODE_BITSIZE (mode);
6411 rtx rhs, lhs;
6412 enum rtx_code next_code;
6413 int i;
6414 rtx new = 0;
6415 rtx tem;
6416 const char *fmt;
6417
6418 /* Select the code to be used in recursive calls. Once we are inside an
6419 address, we stay there. If we have a comparison, set to COMPARE,
6420 but once inside, go back to our default of SET. */
6421
6422 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6423 : ((code == COMPARE || COMPARISON_P (x))
6424 && XEXP (x, 1) == const0_rtx) ? COMPARE
6425 : in_code == COMPARE ? SET : in_code);
6426
6427 /* Process depending on the code of this operation. If NEW is set
6428 nonzero, it will be returned. */
6429
6430 switch (code)
6431 {
6432 case ASHIFT:
6433 /* Convert shifts by constants into multiplications if inside
6434 an address. */
6435 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6436 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6437 && INTVAL (XEXP (x, 1)) >= 0)
6438 {
6439 new = make_compound_operation (XEXP (x, 0), next_code);
6440 new = gen_rtx_MULT (mode, new,
6441 GEN_INT ((HOST_WIDE_INT) 1
6442 << INTVAL (XEXP (x, 1))));
6443 }
6444 break;
6445
6446 case AND:
6447 /* If the second operand is not a constant, we can't do anything
6448 with it. */
6449 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6450 break;
6451
6452 /* If the constant is a power of two minus one and the first operand
6453 is a logical right shift, make an extraction. */
6454 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6455 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6456 {
6457 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6458 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6459 0, in_code == COMPARE);
6460 }
6461
6462 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6463 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6464 && subreg_lowpart_p (XEXP (x, 0))
6465 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6466 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6467 {
6468 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6469 next_code);
6470 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6471 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6472 0, in_code == COMPARE);
6473 }
6474 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6475 else if ((GET_CODE (XEXP (x, 0)) == XOR
6476 || GET_CODE (XEXP (x, 0)) == IOR)
6477 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6478 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6479 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6480 {
6481 /* Apply the distributive law, and then try to make extractions. */
6482 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6483 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6484 XEXP (x, 1)),
6485 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6486 XEXP (x, 1)));
6487 new = make_compound_operation (new, in_code);
6488 }
6489
6490 /* If we are have (and (rotate X C) M) and C is larger than the number
6491 of bits in M, this is an extraction. */
6492
6493 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6494 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6495 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6496 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6497 {
6498 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6499 new = make_extraction (mode, new,
6500 (GET_MODE_BITSIZE (mode)
6501 - INTVAL (XEXP (XEXP (x, 0), 1))),
6502 NULL_RTX, i, 1, 0, in_code == COMPARE);
6503 }
6504
6505 /* On machines without logical shifts, if the operand of the AND is
6506 a logical shift and our mask turns off all the propagated sign
6507 bits, we can replace the logical shift with an arithmetic shift. */
6508 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6509 && !have_insn_for (LSHIFTRT, mode)
6510 && have_insn_for (ASHIFTRT, mode)
6511 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6512 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6513 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6514 && mode_width <= HOST_BITS_PER_WIDE_INT)
6515 {
6516 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6517
6518 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6519 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6520 SUBST (XEXP (x, 0),
6521 gen_rtx_ASHIFTRT (mode,
6522 make_compound_operation
6523 (XEXP (XEXP (x, 0), 0), next_code),
6524 XEXP (XEXP (x, 0), 1)));
6525 }
6526
6527 /* If the constant is one less than a power of two, this might be
6528 representable by an extraction even if no shift is present.
6529 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6530 we are in a COMPARE. */
6531 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6532 new = make_extraction (mode,
6533 make_compound_operation (XEXP (x, 0),
6534 next_code),
6535 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6536
6537 /* If we are in a comparison and this is an AND with a power of two,
6538 convert this into the appropriate bit extract. */
6539 else if (in_code == COMPARE
6540 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6541 new = make_extraction (mode,
6542 make_compound_operation (XEXP (x, 0),
6543 next_code),
6544 i, NULL_RTX, 1, 1, 0, 1);
6545
6546 break;
6547
6548 case LSHIFTRT:
6549 /* If the sign bit is known to be zero, replace this with an
6550 arithmetic shift. */
6551 if (have_insn_for (ASHIFTRT, mode)
6552 && ! have_insn_for (LSHIFTRT, mode)
6553 && mode_width <= HOST_BITS_PER_WIDE_INT
6554 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6555 {
6556 new = gen_rtx_ASHIFTRT (mode,
6557 make_compound_operation (XEXP (x, 0),
6558 next_code),
6559 XEXP (x, 1));
6560 break;
6561 }
6562
6563 /* ... fall through ... */
6564
6565 case ASHIFTRT:
6566 lhs = XEXP (x, 0);
6567 rhs = XEXP (x, 1);
6568
6569 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6570 this is a SIGN_EXTRACT. */
6571 if (GET_CODE (rhs) == CONST_INT
6572 && GET_CODE (lhs) == ASHIFT
6573 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6574 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6575 {
6576 new = make_compound_operation (XEXP (lhs, 0), next_code);
6577 new = make_extraction (mode, new,
6578 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6579 NULL_RTX, mode_width - INTVAL (rhs),
6580 code == LSHIFTRT, 0, in_code == COMPARE);
6581 break;
6582 }
6583
6584 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6585 If so, try to merge the shifts into a SIGN_EXTEND. We could
6586 also do this for some cases of SIGN_EXTRACT, but it doesn't
6587 seem worth the effort; the case checked for occurs on Alpha. */
6588
6589 if (!OBJECT_P (lhs)
6590 && ! (GET_CODE (lhs) == SUBREG
6591 && (OBJECT_P (SUBREG_REG (lhs))))
6592 && GET_CODE (rhs) == CONST_INT
6593 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6594 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6595 new = make_extraction (mode, make_compound_operation (new, next_code),
6596 0, NULL_RTX, mode_width - INTVAL (rhs),
6597 code == LSHIFTRT, 0, in_code == COMPARE);
6598
6599 break;
6600
6601 case SUBREG:
6602 /* Call ourselves recursively on the inner expression. If we are
6603 narrowing the object and it has a different RTL code from
6604 what it originally did, do this SUBREG as a force_to_mode. */
6605
6606 tem = make_compound_operation (SUBREG_REG (x), in_code);
6607
6608 {
6609 rtx simplified;
6610 simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6611 SUBREG_BYTE (x));
6612
6613 if (simplified)
6614 tem = simplified;
6615
6616 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6617 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6618 && subreg_lowpart_p (x))
6619 {
6620 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6621 0);
6622
6623 /* If we have something other than a SUBREG, we might have
6624 done an expansion, so rerun ourselves. */
6625 if (GET_CODE (newer) != SUBREG)
6626 newer = make_compound_operation (newer, in_code);
6627
6628 return newer;
6629 }
6630
6631 if (simplified)
6632 return tem;
6633 }
6634 break;
6635
6636 default:
6637 break;
6638 }
6639
6640 if (new)
6641 {
6642 x = gen_lowpart (mode, new);
6643 code = GET_CODE (x);
6644 }
6645
6646 /* Now recursively process each operand of this operation. */
6647 fmt = GET_RTX_FORMAT (code);
6648 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6649 if (fmt[i] == 'e')
6650 {
6651 new = make_compound_operation (XEXP (x, i), next_code);
6652 SUBST (XEXP (x, i), new);
6653 }
6654
6655 /* If this is a commutative operation, the changes to the operands
6656 may have made it noncanonical. */
6657 if (COMMUTATIVE_ARITH_P (x)
6658 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
6659 {
6660 tem = XEXP (x, 0);
6661 SUBST (XEXP (x, 0), XEXP (x, 1));
6662 SUBST (XEXP (x, 1), tem);
6663 }
6664
6665 return x;
6666 }
6667 \f
6668 /* Given M see if it is a value that would select a field of bits
6669 within an item, but not the entire word. Return -1 if not.
6670 Otherwise, return the starting position of the field, where 0 is the
6671 low-order bit.
6672
6673 *PLEN is set to the length of the field. */
6674
6675 static int
6676 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6677 {
6678 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6679 int pos = exact_log2 (m & -m);
6680 int len = 0;
6681
6682 if (pos >= 0)
6683 /* Now shift off the low-order zero bits and see if we have a
6684 power of two minus 1. */
6685 len = exact_log2 ((m >> pos) + 1);
6686
6687 if (len <= 0)
6688 pos = -1;
6689
6690 *plen = len;
6691 return pos;
6692 }
6693 \f
6694 /* If X refers to a register that equals REG in value, replace these
6695 references with REG. */
6696 static rtx
6697 canon_reg_for_combine (rtx x, rtx reg)
6698 {
6699 rtx op0, op1, op2;
6700 const char *fmt;
6701 int i;
6702 bool copied;
6703
6704 enum rtx_code code = GET_CODE (x);
6705 switch (GET_RTX_CLASS (code))
6706 {
6707 case RTX_UNARY:
6708 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6709 if (op0 != XEXP (x, 0))
6710 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
6711 GET_MODE (reg));
6712 break;
6713
6714 case RTX_BIN_ARITH:
6715 case RTX_COMM_ARITH:
6716 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6717 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
6718 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6719 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
6720 break;
6721
6722 case RTX_COMPARE:
6723 case RTX_COMM_COMPARE:
6724 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6725 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
6726 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6727 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
6728 GET_MODE (op0), op0, op1);
6729 break;
6730
6731 case RTX_TERNARY:
6732 case RTX_BITFIELD_OPS:
6733 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
6734 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
6735 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
6736 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
6737 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
6738 GET_MODE (op0), op0, op1, op2);
6739
6740 case RTX_OBJ:
6741 if (REG_P (x))
6742 {
6743 if (rtx_equal_p (get_last_value (reg), x)
6744 || rtx_equal_p (reg, get_last_value (x)))
6745 return reg;
6746 else
6747 break;
6748 }
6749
6750 /* fall through */
6751
6752 default:
6753 fmt = GET_RTX_FORMAT (code);
6754 copied = false;
6755 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6756 if (fmt[i] == 'e')
6757 {
6758 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
6759 if (op != XEXP (x, i))
6760 {
6761 if (!copied)
6762 {
6763 copied = true;
6764 x = copy_rtx (x);
6765 }
6766 XEXP (x, i) = op;
6767 }
6768 }
6769 else if (fmt[i] == 'E')
6770 {
6771 int j;
6772 for (j = 0; j < XVECLEN (x, i); j++)
6773 {
6774 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
6775 if (op != XVECEXP (x, i, j))
6776 {
6777 if (!copied)
6778 {
6779 copied = true;
6780 x = copy_rtx (x);
6781 }
6782 XVECEXP (x, i, j) = op;
6783 }
6784 }
6785 }
6786
6787 break;
6788 }
6789
6790 return x;
6791 }
6792
6793 /* Return X converted to MODE. If the value is already truncated to
6794 MODE we can just return a subreg even though in the general case we
6795 would need an explicit truncation. */
6796
6797 static rtx
6798 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
6799 {
6800 if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
6801 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
6802 GET_MODE_BITSIZE (GET_MODE (x)))
6803 || (REG_P (x) && reg_truncated_to_mode (mode, x)))
6804 return gen_lowpart (mode, x);
6805 else
6806 return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
6807 }
6808
6809 /* See if X can be simplified knowing that we will only refer to it in
6810 MODE and will only refer to those bits that are nonzero in MASK.
6811 If other bits are being computed or if masking operations are done
6812 that select a superset of the bits in MASK, they can sometimes be
6813 ignored.
6814
6815 Return a possibly simplified expression, but always convert X to
6816 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6817
6818 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6819 are all off in X. This is used when X will be complemented, by either
6820 NOT, NEG, or XOR. */
6821
6822 static rtx
6823 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6824 int just_select)
6825 {
6826 enum rtx_code code = GET_CODE (x);
6827 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6828 enum machine_mode op_mode;
6829 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6830 rtx op0, op1, temp;
6831
6832 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6833 code below will do the wrong thing since the mode of such an
6834 expression is VOIDmode.
6835
6836 Also do nothing if X is a CLOBBER; this can happen if X was
6837 the return value from a call to gen_lowpart. */
6838 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6839 return x;
6840
6841 /* We want to perform the operation is its present mode unless we know
6842 that the operation is valid in MODE, in which case we do the operation
6843 in MODE. */
6844 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6845 && have_insn_for (code, mode))
6846 ? mode : GET_MODE (x));
6847
6848 /* It is not valid to do a right-shift in a narrower mode
6849 than the one it came in with. */
6850 if ((code == LSHIFTRT || code == ASHIFTRT)
6851 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6852 op_mode = GET_MODE (x);
6853
6854 /* Truncate MASK to fit OP_MODE. */
6855 if (op_mode)
6856 mask &= GET_MODE_MASK (op_mode);
6857
6858 /* When we have an arithmetic operation, or a shift whose count we
6859 do not know, we need to assume that all bits up to the highest-order
6860 bit in MASK will be needed. This is how we form such a mask. */
6861 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6862 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6863 else
6864 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6865 - 1);
6866
6867 /* Determine what bits of X are guaranteed to be (non)zero. */
6868 nonzero = nonzero_bits (x, mode);
6869
6870 /* If none of the bits in X are needed, return a zero. */
6871 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
6872 x = const0_rtx;
6873
6874 /* If X is a CONST_INT, return a new one. Do this here since the
6875 test below will fail. */
6876 if (GET_CODE (x) == CONST_INT)
6877 {
6878 if (SCALAR_INT_MODE_P (mode))
6879 return gen_int_mode (INTVAL (x) & mask, mode);
6880 else
6881 {
6882 x = GEN_INT (INTVAL (x) & mask);
6883 return gen_lowpart_common (mode, x);
6884 }
6885 }
6886
6887 /* If X is narrower than MODE and we want all the bits in X's mode, just
6888 get X in the proper mode. */
6889 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6890 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6891 return gen_lowpart (mode, x);
6892
6893 switch (code)
6894 {
6895 case CLOBBER:
6896 /* If X is a (clobber (const_int)), return it since we know we are
6897 generating something that won't match. */
6898 return x;
6899
6900 case SIGN_EXTEND:
6901 case ZERO_EXTEND:
6902 case ZERO_EXTRACT:
6903 case SIGN_EXTRACT:
6904 x = expand_compound_operation (x);
6905 if (GET_CODE (x) != code)
6906 return force_to_mode (x, mode, mask, next_select);
6907 break;
6908
6909 case SUBREG:
6910 if (subreg_lowpart_p (x)
6911 /* We can ignore the effect of this SUBREG if it narrows the mode or
6912 if the constant masks to zero all the bits the mode doesn't
6913 have. */
6914 && ((GET_MODE_SIZE (GET_MODE (x))
6915 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6916 || (0 == (mask
6917 & GET_MODE_MASK (GET_MODE (x))
6918 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6919 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
6920 break;
6921
6922 case AND:
6923 /* If this is an AND with a constant, convert it into an AND
6924 whose constant is the AND of that constant with MASK. If it
6925 remains an AND of MASK, delete it since it is redundant. */
6926
6927 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6928 {
6929 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6930 mask & INTVAL (XEXP (x, 1)));
6931
6932 /* If X is still an AND, see if it is an AND with a mask that
6933 is just some low-order bits. If so, and it is MASK, we don't
6934 need it. */
6935
6936 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6937 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6938 == mask))
6939 x = XEXP (x, 0);
6940
6941 /* If it remains an AND, try making another AND with the bits
6942 in the mode mask that aren't in MASK turned on. If the
6943 constant in the AND is wide enough, this might make a
6944 cheaper constant. */
6945
6946 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6947 && GET_MODE_MASK (GET_MODE (x)) != mask
6948 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6949 {
6950 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6951 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6952 int width = GET_MODE_BITSIZE (GET_MODE (x));
6953 rtx y;
6954
6955 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6956 number, sign extend it. */
6957 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6958 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6959 cval |= (HOST_WIDE_INT) -1 << width;
6960
6961 y = simplify_gen_binary (AND, GET_MODE (x),
6962 XEXP (x, 0), GEN_INT (cval));
6963 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6964 x = y;
6965 }
6966
6967 break;
6968 }
6969
6970 goto binop;
6971
6972 case PLUS:
6973 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6974 low-order bits (as in an alignment operation) and FOO is already
6975 aligned to that boundary, mask C1 to that boundary as well.
6976 This may eliminate that PLUS and, later, the AND. */
6977
6978 {
6979 unsigned int width = GET_MODE_BITSIZE (mode);
6980 unsigned HOST_WIDE_INT smask = mask;
6981
6982 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6983 number, sign extend it. */
6984
6985 if (width < HOST_BITS_PER_WIDE_INT
6986 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6987 smask |= (HOST_WIDE_INT) -1 << width;
6988
6989 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6990 && exact_log2 (- smask) >= 0
6991 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6992 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6993 return force_to_mode (plus_constant (XEXP (x, 0),
6994 (INTVAL (XEXP (x, 1)) & smask)),
6995 mode, smask, next_select);
6996 }
6997
6998 /* ... fall through ... */
6999
7000 case MULT:
7001 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7002 most significant bit in MASK since carries from those bits will
7003 affect the bits we are interested in. */
7004 mask = fuller_mask;
7005 goto binop;
7006
7007 case MINUS:
7008 /* If X is (minus C Y) where C's least set bit is larger than any bit
7009 in the mask, then we may replace with (neg Y). */
7010 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7011 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7012 & -INTVAL (XEXP (x, 0))))
7013 > mask))
7014 {
7015 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7016 GET_MODE (x));
7017 return force_to_mode (x, mode, mask, next_select);
7018 }
7019
7020 /* Similarly, if C contains every bit in the fuller_mask, then we may
7021 replace with (not Y). */
7022 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7023 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7024 == INTVAL (XEXP (x, 0))))
7025 {
7026 x = simplify_gen_unary (NOT, GET_MODE (x),
7027 XEXP (x, 1), GET_MODE (x));
7028 return force_to_mode (x, mode, mask, next_select);
7029 }
7030
7031 mask = fuller_mask;
7032 goto binop;
7033
7034 case IOR:
7035 case XOR:
7036 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7037 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7038 operation which may be a bitfield extraction. Ensure that the
7039 constant we form is not wider than the mode of X. */
7040
7041 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7042 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7043 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7044 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7045 && GET_CODE (XEXP (x, 1)) == CONST_INT
7046 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7047 + floor_log2 (INTVAL (XEXP (x, 1))))
7048 < GET_MODE_BITSIZE (GET_MODE (x)))
7049 && (INTVAL (XEXP (x, 1))
7050 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7051 {
7052 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7053 << INTVAL (XEXP (XEXP (x, 0), 1)));
7054 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7055 XEXP (XEXP (x, 0), 0), temp);
7056 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7057 XEXP (XEXP (x, 0), 1));
7058 return force_to_mode (x, mode, mask, next_select);
7059 }
7060
7061 binop:
7062 /* For most binary operations, just propagate into the operation and
7063 change the mode if we have an operation of that mode. */
7064
7065 op0 = gen_lowpart_or_truncate (op_mode,
7066 force_to_mode (XEXP (x, 0), mode, mask,
7067 next_select));
7068 op1 = gen_lowpart_or_truncate (op_mode,
7069 force_to_mode (XEXP (x, 1), mode, mask,
7070 next_select));
7071
7072 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7073 x = simplify_gen_binary (code, op_mode, op0, op1);
7074 break;
7075
7076 case ASHIFT:
7077 /* For left shifts, do the same, but just for the first operand.
7078 However, we cannot do anything with shifts where we cannot
7079 guarantee that the counts are smaller than the size of the mode
7080 because such a count will have a different meaning in a
7081 wider mode. */
7082
7083 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7084 && INTVAL (XEXP (x, 1)) >= 0
7085 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7086 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7087 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7088 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7089 break;
7090
7091 /* If the shift count is a constant and we can do arithmetic in
7092 the mode of the shift, refine which bits we need. Otherwise, use the
7093 conservative form of the mask. */
7094 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7095 && INTVAL (XEXP (x, 1)) >= 0
7096 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7097 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7098 mask >>= INTVAL (XEXP (x, 1));
7099 else
7100 mask = fuller_mask;
7101
7102 op0 = gen_lowpart_or_truncate (op_mode,
7103 force_to_mode (XEXP (x, 0), op_mode,
7104 mask, next_select));
7105
7106 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7107 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7108 break;
7109
7110 case LSHIFTRT:
7111 /* Here we can only do something if the shift count is a constant,
7112 this shift constant is valid for the host, and we can do arithmetic
7113 in OP_MODE. */
7114
7115 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7116 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7117 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7118 {
7119 rtx inner = XEXP (x, 0);
7120 unsigned HOST_WIDE_INT inner_mask;
7121
7122 /* Select the mask of the bits we need for the shift operand. */
7123 inner_mask = mask << INTVAL (XEXP (x, 1));
7124
7125 /* We can only change the mode of the shift if we can do arithmetic
7126 in the mode of the shift and INNER_MASK is no wider than the
7127 width of X's mode. */
7128 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7129 op_mode = GET_MODE (x);
7130
7131 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7132
7133 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7134 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7135 }
7136
7137 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7138 shift and AND produces only copies of the sign bit (C2 is one less
7139 than a power of two), we can do this with just a shift. */
7140
7141 if (GET_CODE (x) == LSHIFTRT
7142 && GET_CODE (XEXP (x, 1)) == CONST_INT
7143 /* The shift puts one of the sign bit copies in the least significant
7144 bit. */
7145 && ((INTVAL (XEXP (x, 1))
7146 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7147 >= GET_MODE_BITSIZE (GET_MODE (x)))
7148 && exact_log2 (mask + 1) >= 0
7149 /* Number of bits left after the shift must be more than the mask
7150 needs. */
7151 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7152 <= GET_MODE_BITSIZE (GET_MODE (x)))
7153 /* Must be more sign bit copies than the mask needs. */
7154 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7155 >= exact_log2 (mask + 1)))
7156 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7157 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7158 - exact_log2 (mask + 1)));
7159
7160 goto shiftrt;
7161
7162 case ASHIFTRT:
7163 /* If we are just looking for the sign bit, we don't need this shift at
7164 all, even if it has a variable count. */
7165 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7166 && (mask == ((unsigned HOST_WIDE_INT) 1
7167 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7168 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7169
7170 /* If this is a shift by a constant, get a mask that contains those bits
7171 that are not copies of the sign bit. We then have two cases: If
7172 MASK only includes those bits, this can be a logical shift, which may
7173 allow simplifications. If MASK is a single-bit field not within
7174 those bits, we are requesting a copy of the sign bit and hence can
7175 shift the sign bit to the appropriate location. */
7176
7177 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7178 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7179 {
7180 int i;
7181
7182 /* If the considered data is wider than HOST_WIDE_INT, we can't
7183 represent a mask for all its bits in a single scalar.
7184 But we only care about the lower bits, so calculate these. */
7185
7186 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7187 {
7188 nonzero = ~(HOST_WIDE_INT) 0;
7189
7190 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7191 is the number of bits a full-width mask would have set.
7192 We need only shift if these are fewer than nonzero can
7193 hold. If not, we must keep all bits set in nonzero. */
7194
7195 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7196 < HOST_BITS_PER_WIDE_INT)
7197 nonzero >>= INTVAL (XEXP (x, 1))
7198 + HOST_BITS_PER_WIDE_INT
7199 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7200 }
7201 else
7202 {
7203 nonzero = GET_MODE_MASK (GET_MODE (x));
7204 nonzero >>= INTVAL (XEXP (x, 1));
7205 }
7206
7207 if ((mask & ~nonzero) == 0)
7208 {
7209 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7210 XEXP (x, 0), INTVAL (XEXP (x, 1)));
7211 if (GET_CODE (x) != ASHIFTRT)
7212 return force_to_mode (x, mode, mask, next_select);
7213 }
7214
7215 else if ((i = exact_log2 (mask)) >= 0)
7216 {
7217 x = simplify_shift_const
7218 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7219 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7220
7221 if (GET_CODE (x) != ASHIFTRT)
7222 return force_to_mode (x, mode, mask, next_select);
7223 }
7224 }
7225
7226 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7227 even if the shift count isn't a constant. */
7228 if (mask == 1)
7229 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7230 XEXP (x, 0), XEXP (x, 1));
7231
7232 shiftrt:
7233
7234 /* If this is a zero- or sign-extension operation that just affects bits
7235 we don't care about, remove it. Be sure the call above returned
7236 something that is still a shift. */
7237
7238 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7239 && GET_CODE (XEXP (x, 1)) == CONST_INT
7240 && INTVAL (XEXP (x, 1)) >= 0
7241 && (INTVAL (XEXP (x, 1))
7242 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7243 && GET_CODE (XEXP (x, 0)) == ASHIFT
7244 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7245 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7246 next_select);
7247
7248 break;
7249
7250 case ROTATE:
7251 case ROTATERT:
7252 /* If the shift count is constant and we can do computations
7253 in the mode of X, compute where the bits we care about are.
7254 Otherwise, we can't do anything. Don't change the mode of
7255 the shift or propagate MODE into the shift, though. */
7256 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7257 && INTVAL (XEXP (x, 1)) >= 0)
7258 {
7259 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7260 GET_MODE (x), GEN_INT (mask),
7261 XEXP (x, 1));
7262 if (temp && GET_CODE (temp) == CONST_INT)
7263 SUBST (XEXP (x, 0),
7264 force_to_mode (XEXP (x, 0), GET_MODE (x),
7265 INTVAL (temp), next_select));
7266 }
7267 break;
7268
7269 case NEG:
7270 /* If we just want the low-order bit, the NEG isn't needed since it
7271 won't change the low-order bit. */
7272 if (mask == 1)
7273 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7274
7275 /* We need any bits less significant than the most significant bit in
7276 MASK since carries from those bits will affect the bits we are
7277 interested in. */
7278 mask = fuller_mask;
7279 goto unop;
7280
7281 case NOT:
7282 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7283 same as the XOR case above. Ensure that the constant we form is not
7284 wider than the mode of X. */
7285
7286 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7287 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7288 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7289 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7290 < GET_MODE_BITSIZE (GET_MODE (x)))
7291 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7292 {
7293 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7294 GET_MODE (x));
7295 temp = simplify_gen_binary (XOR, GET_MODE (x),
7296 XEXP (XEXP (x, 0), 0), temp);
7297 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7298 temp, XEXP (XEXP (x, 0), 1));
7299
7300 return force_to_mode (x, mode, mask, next_select);
7301 }
7302
7303 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7304 use the full mask inside the NOT. */
7305 mask = fuller_mask;
7306
7307 unop:
7308 op0 = gen_lowpart_or_truncate (op_mode,
7309 force_to_mode (XEXP (x, 0), mode, mask,
7310 next_select));
7311 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7312 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7313 break;
7314
7315 case NE:
7316 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7317 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7318 which is equal to STORE_FLAG_VALUE. */
7319 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7320 && GET_MODE (XEXP (x, 0)) == mode
7321 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7322 && (nonzero_bits (XEXP (x, 0), mode)
7323 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7324 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7325
7326 break;
7327
7328 case IF_THEN_ELSE:
7329 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7330 written in a narrower mode. We play it safe and do not do so. */
7331
7332 SUBST (XEXP (x, 1),
7333 gen_lowpart_or_truncate (GET_MODE (x),
7334 force_to_mode (XEXP (x, 1), mode,
7335 mask, next_select)));
7336 SUBST (XEXP (x, 2),
7337 gen_lowpart_or_truncate (GET_MODE (x),
7338 force_to_mode (XEXP (x, 2), mode,
7339 mask, next_select)));
7340 break;
7341
7342 default:
7343 break;
7344 }
7345
7346 /* Ensure we return a value of the proper mode. */
7347 return gen_lowpart_or_truncate (mode, x);
7348 }
7349 \f
7350 /* Return nonzero if X is an expression that has one of two values depending on
7351 whether some other value is zero or nonzero. In that case, we return the
7352 value that is being tested, *PTRUE is set to the value if the rtx being
7353 returned has a nonzero value, and *PFALSE is set to the other alternative.
7354
7355 If we return zero, we set *PTRUE and *PFALSE to X. */
7356
7357 static rtx
7358 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7359 {
7360 enum machine_mode mode = GET_MODE (x);
7361 enum rtx_code code = GET_CODE (x);
7362 rtx cond0, cond1, true0, true1, false0, false1;
7363 unsigned HOST_WIDE_INT nz;
7364
7365 /* If we are comparing a value against zero, we are done. */
7366 if ((code == NE || code == EQ)
7367 && XEXP (x, 1) == const0_rtx)
7368 {
7369 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7370 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7371 return XEXP (x, 0);
7372 }
7373
7374 /* If this is a unary operation whose operand has one of two values, apply
7375 our opcode to compute those values. */
7376 else if (UNARY_P (x)
7377 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7378 {
7379 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7380 *pfalse = simplify_gen_unary (code, mode, false0,
7381 GET_MODE (XEXP (x, 0)));
7382 return cond0;
7383 }
7384
7385 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7386 make can't possibly match and would suppress other optimizations. */
7387 else if (code == COMPARE)
7388 ;
7389
7390 /* If this is a binary operation, see if either side has only one of two
7391 values. If either one does or if both do and they are conditional on
7392 the same value, compute the new true and false values. */
7393 else if (BINARY_P (x))
7394 {
7395 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7396 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7397
7398 if ((cond0 != 0 || cond1 != 0)
7399 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7400 {
7401 /* If if_then_else_cond returned zero, then true/false are the
7402 same rtl. We must copy one of them to prevent invalid rtl
7403 sharing. */
7404 if (cond0 == 0)
7405 true0 = copy_rtx (true0);
7406 else if (cond1 == 0)
7407 true1 = copy_rtx (true1);
7408
7409 if (COMPARISON_P (x))
7410 {
7411 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7412 true0, true1);
7413 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7414 false0, false1);
7415 }
7416 else
7417 {
7418 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7419 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7420 }
7421
7422 return cond0 ? cond0 : cond1;
7423 }
7424
7425 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7426 operands is zero when the other is nonzero, and vice-versa,
7427 and STORE_FLAG_VALUE is 1 or -1. */
7428
7429 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7430 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7431 || code == UMAX)
7432 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7433 {
7434 rtx op0 = XEXP (XEXP (x, 0), 1);
7435 rtx op1 = XEXP (XEXP (x, 1), 1);
7436
7437 cond0 = XEXP (XEXP (x, 0), 0);
7438 cond1 = XEXP (XEXP (x, 1), 0);
7439
7440 if (COMPARISON_P (cond0)
7441 && COMPARISON_P (cond1)
7442 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7443 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7444 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7445 || ((swap_condition (GET_CODE (cond0))
7446 == reversed_comparison_code (cond1, NULL))
7447 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7448 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7449 && ! side_effects_p (x))
7450 {
7451 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7452 *pfalse = simplify_gen_binary (MULT, mode,
7453 (code == MINUS
7454 ? simplify_gen_unary (NEG, mode,
7455 op1, mode)
7456 : op1),
7457 const_true_rtx);
7458 return cond0;
7459 }
7460 }
7461
7462 /* Similarly for MULT, AND and UMIN, except that for these the result
7463 is always zero. */
7464 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7465 && (code == MULT || code == AND || code == UMIN)
7466 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7467 {
7468 cond0 = XEXP (XEXP (x, 0), 0);
7469 cond1 = XEXP (XEXP (x, 1), 0);
7470
7471 if (COMPARISON_P (cond0)
7472 && COMPARISON_P (cond1)
7473 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7474 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7475 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7476 || ((swap_condition (GET_CODE (cond0))
7477 == reversed_comparison_code (cond1, NULL))
7478 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7479 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7480 && ! side_effects_p (x))
7481 {
7482 *ptrue = *pfalse = const0_rtx;
7483 return cond0;
7484 }
7485 }
7486 }
7487
7488 else if (code == IF_THEN_ELSE)
7489 {
7490 /* If we have IF_THEN_ELSE already, extract the condition and
7491 canonicalize it if it is NE or EQ. */
7492 cond0 = XEXP (x, 0);
7493 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7494 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7495 return XEXP (cond0, 0);
7496 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7497 {
7498 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7499 return XEXP (cond0, 0);
7500 }
7501 else
7502 return cond0;
7503 }
7504
7505 /* If X is a SUBREG, we can narrow both the true and false values
7506 if the inner expression, if there is a condition. */
7507 else if (code == SUBREG
7508 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7509 &true0, &false0)))
7510 {
7511 true0 = simplify_gen_subreg (mode, true0,
7512 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7513 false0 = simplify_gen_subreg (mode, false0,
7514 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7515 if (true0 && false0)
7516 {
7517 *ptrue = true0;
7518 *pfalse = false0;
7519 return cond0;
7520 }
7521 }
7522
7523 /* If X is a constant, this isn't special and will cause confusions
7524 if we treat it as such. Likewise if it is equivalent to a constant. */
7525 else if (CONSTANT_P (x)
7526 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7527 ;
7528
7529 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7530 will be least confusing to the rest of the compiler. */
7531 else if (mode == BImode)
7532 {
7533 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7534 return x;
7535 }
7536
7537 /* If X is known to be either 0 or -1, those are the true and
7538 false values when testing X. */
7539 else if (x == constm1_rtx || x == const0_rtx
7540 || (mode != VOIDmode
7541 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7542 {
7543 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7544 return x;
7545 }
7546
7547 /* Likewise for 0 or a single bit. */
7548 else if (SCALAR_INT_MODE_P (mode)
7549 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7550 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7551 {
7552 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7553 return x;
7554 }
7555
7556 /* Otherwise fail; show no condition with true and false values the same. */
7557 *ptrue = *pfalse = x;
7558 return 0;
7559 }
7560 \f
7561 /* Return the value of expression X given the fact that condition COND
7562 is known to be true when applied to REG as its first operand and VAL
7563 as its second. X is known to not be shared and so can be modified in
7564 place.
7565
7566 We only handle the simplest cases, and specifically those cases that
7567 arise with IF_THEN_ELSE expressions. */
7568
7569 static rtx
7570 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7571 {
7572 enum rtx_code code = GET_CODE (x);
7573 rtx temp;
7574 const char *fmt;
7575 int i, j;
7576
7577 if (side_effects_p (x))
7578 return x;
7579
7580 /* If either operand of the condition is a floating point value,
7581 then we have to avoid collapsing an EQ comparison. */
7582 if (cond == EQ
7583 && rtx_equal_p (x, reg)
7584 && ! FLOAT_MODE_P (GET_MODE (x))
7585 && ! FLOAT_MODE_P (GET_MODE (val)))
7586 return val;
7587
7588 if (cond == UNEQ && rtx_equal_p (x, reg))
7589 return val;
7590
7591 /* If X is (abs REG) and we know something about REG's relationship
7592 with zero, we may be able to simplify this. */
7593
7594 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7595 switch (cond)
7596 {
7597 case GE: case GT: case EQ:
7598 return XEXP (x, 0);
7599 case LT: case LE:
7600 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7601 XEXP (x, 0),
7602 GET_MODE (XEXP (x, 0)));
7603 default:
7604 break;
7605 }
7606
7607 /* The only other cases we handle are MIN, MAX, and comparisons if the
7608 operands are the same as REG and VAL. */
7609
7610 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7611 {
7612 if (rtx_equal_p (XEXP (x, 0), val))
7613 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7614
7615 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7616 {
7617 if (COMPARISON_P (x))
7618 {
7619 if (comparison_dominates_p (cond, code))
7620 return const_true_rtx;
7621
7622 code = reversed_comparison_code (x, NULL);
7623 if (code != UNKNOWN
7624 && comparison_dominates_p (cond, code))
7625 return const0_rtx;
7626 else
7627 return x;
7628 }
7629 else if (code == SMAX || code == SMIN
7630 || code == UMIN || code == UMAX)
7631 {
7632 int unsignedp = (code == UMIN || code == UMAX);
7633
7634 /* Do not reverse the condition when it is NE or EQ.
7635 This is because we cannot conclude anything about
7636 the value of 'SMAX (x, y)' when x is not equal to y,
7637 but we can when x equals y. */
7638 if ((code == SMAX || code == UMAX)
7639 && ! (cond == EQ || cond == NE))
7640 cond = reverse_condition (cond);
7641
7642 switch (cond)
7643 {
7644 case GE: case GT:
7645 return unsignedp ? x : XEXP (x, 1);
7646 case LE: case LT:
7647 return unsignedp ? x : XEXP (x, 0);
7648 case GEU: case GTU:
7649 return unsignedp ? XEXP (x, 1) : x;
7650 case LEU: case LTU:
7651 return unsignedp ? XEXP (x, 0) : x;
7652 default:
7653 break;
7654 }
7655 }
7656 }
7657 }
7658 else if (code == SUBREG)
7659 {
7660 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7661 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7662
7663 if (SUBREG_REG (x) != r)
7664 {
7665 /* We must simplify subreg here, before we lose track of the
7666 original inner_mode. */
7667 new = simplify_subreg (GET_MODE (x), r,
7668 inner_mode, SUBREG_BYTE (x));
7669 if (new)
7670 return new;
7671 else
7672 SUBST (SUBREG_REG (x), r);
7673 }
7674
7675 return x;
7676 }
7677 /* We don't have to handle SIGN_EXTEND here, because even in the
7678 case of replacing something with a modeless CONST_INT, a
7679 CONST_INT is already (supposed to be) a valid sign extension for
7680 its narrower mode, which implies it's already properly
7681 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7682 story is different. */
7683 else if (code == ZERO_EXTEND)
7684 {
7685 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7686 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7687
7688 if (XEXP (x, 0) != r)
7689 {
7690 /* We must simplify the zero_extend here, before we lose
7691 track of the original inner_mode. */
7692 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7693 r, inner_mode);
7694 if (new)
7695 return new;
7696 else
7697 SUBST (XEXP (x, 0), r);
7698 }
7699
7700 return x;
7701 }
7702
7703 fmt = GET_RTX_FORMAT (code);
7704 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7705 {
7706 if (fmt[i] == 'e')
7707 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7708 else if (fmt[i] == 'E')
7709 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7710 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7711 cond, reg, val));
7712 }
7713
7714 return x;
7715 }
7716 \f
7717 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7718 assignment as a field assignment. */
7719
7720 static int
7721 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7722 {
7723 if (x == y || rtx_equal_p (x, y))
7724 return 1;
7725
7726 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7727 return 0;
7728
7729 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7730 Note that all SUBREGs of MEM are paradoxical; otherwise they
7731 would have been rewritten. */
7732 if (MEM_P (x) && GET_CODE (y) == SUBREG
7733 && MEM_P (SUBREG_REG (y))
7734 && rtx_equal_p (SUBREG_REG (y),
7735 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7736 return 1;
7737
7738 if (MEM_P (y) && GET_CODE (x) == SUBREG
7739 && MEM_P (SUBREG_REG (x))
7740 && rtx_equal_p (SUBREG_REG (x),
7741 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7742 return 1;
7743
7744 /* We used to see if get_last_value of X and Y were the same but that's
7745 not correct. In one direction, we'll cause the assignment to have
7746 the wrong destination and in the case, we'll import a register into this
7747 insn that might have already have been dead. So fail if none of the
7748 above cases are true. */
7749 return 0;
7750 }
7751 \f
7752 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7753 Return that assignment if so.
7754
7755 We only handle the most common cases. */
7756
7757 static rtx
7758 make_field_assignment (rtx x)
7759 {
7760 rtx dest = SET_DEST (x);
7761 rtx src = SET_SRC (x);
7762 rtx assign;
7763 rtx rhs, lhs;
7764 HOST_WIDE_INT c1;
7765 HOST_WIDE_INT pos;
7766 unsigned HOST_WIDE_INT len;
7767 rtx other;
7768 enum machine_mode mode;
7769
7770 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7771 a clear of a one-bit field. We will have changed it to
7772 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7773 for a SUBREG. */
7774
7775 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7776 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7777 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7778 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7779 {
7780 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7781 1, 1, 1, 0);
7782 if (assign != 0)
7783 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7784 return x;
7785 }
7786
7787 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7788 && subreg_lowpart_p (XEXP (src, 0))
7789 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7790 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7791 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7792 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7793 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7794 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7795 {
7796 assign = make_extraction (VOIDmode, dest, 0,
7797 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7798 1, 1, 1, 0);
7799 if (assign != 0)
7800 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7801 return x;
7802 }
7803
7804 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7805 one-bit field. */
7806 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7807 && XEXP (XEXP (src, 0), 0) == const1_rtx
7808 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7809 {
7810 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7811 1, 1, 1, 0);
7812 if (assign != 0)
7813 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7814 return x;
7815 }
7816
7817 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
7818 SRC is an AND with all bits of that field set, then we can discard
7819 the AND. */
7820 if (GET_CODE (dest) == ZERO_EXTRACT
7821 && GET_CODE (XEXP (dest, 1)) == CONST_INT
7822 && GET_CODE (src) == AND
7823 && GET_CODE (XEXP (src, 1)) == CONST_INT)
7824 {
7825 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
7826 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
7827 unsigned HOST_WIDE_INT ze_mask;
7828
7829 if (width >= HOST_BITS_PER_WIDE_INT)
7830 ze_mask = -1;
7831 else
7832 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
7833
7834 /* Complete overlap. We can remove the source AND. */
7835 if ((and_mask & ze_mask) == ze_mask)
7836 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
7837
7838 /* Partial overlap. We can reduce the source AND. */
7839 if ((and_mask & ze_mask) != and_mask)
7840 {
7841 mode = GET_MODE (src);
7842 src = gen_rtx_AND (mode, XEXP (src, 0),
7843 gen_int_mode (and_mask & ze_mask, mode));
7844 return gen_rtx_SET (VOIDmode, dest, src);
7845 }
7846 }
7847
7848 /* The other case we handle is assignments into a constant-position
7849 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7850 a mask that has all one bits except for a group of zero bits and
7851 OTHER is known to have zeros where C1 has ones, this is such an
7852 assignment. Compute the position and length from C1. Shift OTHER
7853 to the appropriate position, force it to the required mode, and
7854 make the extraction. Check for the AND in both operands. */
7855
7856 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7857 return x;
7858
7859 rhs = expand_compound_operation (XEXP (src, 0));
7860 lhs = expand_compound_operation (XEXP (src, 1));
7861
7862 if (GET_CODE (rhs) == AND
7863 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7864 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7865 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7866 else if (GET_CODE (lhs) == AND
7867 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7868 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7869 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7870 else
7871 return x;
7872
7873 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7874 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7875 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7876 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7877 return x;
7878
7879 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7880 if (assign == 0)
7881 return x;
7882
7883 /* The mode to use for the source is the mode of the assignment, or of
7884 what is inside a possible STRICT_LOW_PART. */
7885 mode = (GET_CODE (assign) == STRICT_LOW_PART
7886 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7887
7888 /* Shift OTHER right POS places and make it the source, restricting it
7889 to the proper length and mode. */
7890
7891 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
7892 GET_MODE (src),
7893 other, pos),
7894 dest);
7895 src = force_to_mode (src, mode,
7896 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7897 ? ~(unsigned HOST_WIDE_INT) 0
7898 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7899 0);
7900
7901 /* If SRC is masked by an AND that does not make a difference in
7902 the value being stored, strip it. */
7903 if (GET_CODE (assign) == ZERO_EXTRACT
7904 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7905 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7906 && GET_CODE (src) == AND
7907 && GET_CODE (XEXP (src, 1)) == CONST_INT
7908 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7909 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7910 src = XEXP (src, 0);
7911
7912 return gen_rtx_SET (VOIDmode, assign, src);
7913 }
7914 \f
7915 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7916 if so. */
7917
7918 static rtx
7919 apply_distributive_law (rtx x)
7920 {
7921 enum rtx_code code = GET_CODE (x);
7922 enum rtx_code inner_code;
7923 rtx lhs, rhs, other;
7924 rtx tem;
7925
7926 /* Distributivity is not true for floating point as it can change the
7927 value. So we don't do it unless -funsafe-math-optimizations. */
7928 if (FLOAT_MODE_P (GET_MODE (x))
7929 && ! flag_unsafe_math_optimizations)
7930 return x;
7931
7932 /* The outer operation can only be one of the following: */
7933 if (code != IOR && code != AND && code != XOR
7934 && code != PLUS && code != MINUS)
7935 return x;
7936
7937 lhs = XEXP (x, 0);
7938 rhs = XEXP (x, 1);
7939
7940 /* If either operand is a primitive we can't do anything, so get out
7941 fast. */
7942 if (OBJECT_P (lhs) || OBJECT_P (rhs))
7943 return x;
7944
7945 lhs = expand_compound_operation (lhs);
7946 rhs = expand_compound_operation (rhs);
7947 inner_code = GET_CODE (lhs);
7948 if (inner_code != GET_CODE (rhs))
7949 return x;
7950
7951 /* See if the inner and outer operations distribute. */
7952 switch (inner_code)
7953 {
7954 case LSHIFTRT:
7955 case ASHIFTRT:
7956 case AND:
7957 case IOR:
7958 /* These all distribute except over PLUS. */
7959 if (code == PLUS || code == MINUS)
7960 return x;
7961 break;
7962
7963 case MULT:
7964 if (code != PLUS && code != MINUS)
7965 return x;
7966 break;
7967
7968 case ASHIFT:
7969 /* This is also a multiply, so it distributes over everything. */
7970 break;
7971
7972 case SUBREG:
7973 /* Non-paradoxical SUBREGs distributes over all operations,
7974 provided the inner modes and byte offsets are the same, this
7975 is an extraction of a low-order part, we don't convert an fp
7976 operation to int or vice versa, this is not a vector mode,
7977 and we would not be converting a single-word operation into a
7978 multi-word operation. The latter test is not required, but
7979 it prevents generating unneeded multi-word operations. Some
7980 of the previous tests are redundant given the latter test,
7981 but are retained because they are required for correctness.
7982
7983 We produce the result slightly differently in this case. */
7984
7985 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7986 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7987 || ! subreg_lowpart_p (lhs)
7988 || (GET_MODE_CLASS (GET_MODE (lhs))
7989 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7990 || (GET_MODE_SIZE (GET_MODE (lhs))
7991 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7992 || VECTOR_MODE_P (GET_MODE (lhs))
7993 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
7994 /* Result might need to be truncated. Don't change mode if
7995 explicit truncation is needed. */
7996 || !TRULY_NOOP_TRUNCATION
7997 (GET_MODE_BITSIZE (GET_MODE (x)),
7998 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
7999 return x;
8000
8001 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8002 SUBREG_REG (lhs), SUBREG_REG (rhs));
8003 return gen_lowpart (GET_MODE (x), tem);
8004
8005 default:
8006 return x;
8007 }
8008
8009 /* Set LHS and RHS to the inner operands (A and B in the example
8010 above) and set OTHER to the common operand (C in the example).
8011 There is only one way to do this unless the inner operation is
8012 commutative. */
8013 if (COMMUTATIVE_ARITH_P (lhs)
8014 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8015 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8016 else if (COMMUTATIVE_ARITH_P (lhs)
8017 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8018 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8019 else if (COMMUTATIVE_ARITH_P (lhs)
8020 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8021 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8022 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8023 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8024 else
8025 return x;
8026
8027 /* Form the new inner operation, seeing if it simplifies first. */
8028 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8029
8030 /* There is one exception to the general way of distributing:
8031 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8032 if (code == XOR && inner_code == IOR)
8033 {
8034 inner_code = AND;
8035 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8036 }
8037
8038 /* We may be able to continuing distributing the result, so call
8039 ourselves recursively on the inner operation before forming the
8040 outer operation, which we return. */
8041 return simplify_gen_binary (inner_code, GET_MODE (x),
8042 apply_distributive_law (tem), other);
8043 }
8044
8045 /* See if X is of the form (* (+ A B) C), and if so convert to
8046 (+ (* A C) (* B C)) and try to simplify.
8047
8048 Most of the time, this results in no change. However, if some of
8049 the operands are the same or inverses of each other, simplifications
8050 will result.
8051
8052 For example, (and (ior A B) (not B)) can occur as the result of
8053 expanding a bit field assignment. When we apply the distributive
8054 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8055 which then simplifies to (and (A (not B))).
8056
8057 Note that no checks happen on the validity of applying the inverse
8058 distributive law. This is pointless since we can do it in the
8059 few places where this routine is called.
8060
8061 N is the index of the term that is decomposed (the arithmetic operation,
8062 i.e. (+ A B) in the first example above). !N is the index of the term that
8063 is distributed, i.e. of C in the first example above. */
8064 static rtx
8065 distribute_and_simplify_rtx (rtx x, int n)
8066 {
8067 enum machine_mode mode;
8068 enum rtx_code outer_code, inner_code;
8069 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8070
8071 decomposed = XEXP (x, n);
8072 if (!ARITHMETIC_P (decomposed))
8073 return NULL_RTX;
8074
8075 mode = GET_MODE (x);
8076 outer_code = GET_CODE (x);
8077 distributed = XEXP (x, !n);
8078
8079 inner_code = GET_CODE (decomposed);
8080 inner_op0 = XEXP (decomposed, 0);
8081 inner_op1 = XEXP (decomposed, 1);
8082
8083 /* Special case (and (xor B C) (not A)), which is equivalent to
8084 (xor (ior A B) (ior A C)) */
8085 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8086 {
8087 distributed = XEXP (distributed, 0);
8088 outer_code = IOR;
8089 }
8090
8091 if (n == 0)
8092 {
8093 /* Distribute the second term. */
8094 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8095 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8096 }
8097 else
8098 {
8099 /* Distribute the first term. */
8100 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8101 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8102 }
8103
8104 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8105 new_op0, new_op1));
8106 if (GET_CODE (tmp) != outer_code
8107 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8108 return tmp;
8109
8110 return NULL_RTX;
8111 }
8112 \f
8113 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8114 in MODE. Return an equivalent form, if different from (and VAROP
8115 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8116
8117 static rtx
8118 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8119 unsigned HOST_WIDE_INT constop)
8120 {
8121 unsigned HOST_WIDE_INT nonzero;
8122 unsigned HOST_WIDE_INT orig_constop;
8123 rtx orig_varop;
8124 int i;
8125
8126 orig_varop = varop;
8127 orig_constop = constop;
8128 if (GET_CODE (varop) == CLOBBER)
8129 return NULL_RTX;
8130
8131 /* Simplify VAROP knowing that we will be only looking at some of the
8132 bits in it.
8133
8134 Note by passing in CONSTOP, we guarantee that the bits not set in
8135 CONSTOP are not significant and will never be examined. We must
8136 ensure that is the case by explicitly masking out those bits
8137 before returning. */
8138 varop = force_to_mode (varop, mode, constop, 0);
8139
8140 /* If VAROP is a CLOBBER, we will fail so return it. */
8141 if (GET_CODE (varop) == CLOBBER)
8142 return varop;
8143
8144 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8145 to VAROP and return the new constant. */
8146 if (GET_CODE (varop) == CONST_INT)
8147 return gen_int_mode (INTVAL (varop) & constop, mode);
8148
8149 /* See what bits may be nonzero in VAROP. Unlike the general case of
8150 a call to nonzero_bits, here we don't care about bits outside
8151 MODE. */
8152
8153 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8154
8155 /* Turn off all bits in the constant that are known to already be zero.
8156 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8157 which is tested below. */
8158
8159 constop &= nonzero;
8160
8161 /* If we don't have any bits left, return zero. */
8162 if (constop == 0)
8163 return const0_rtx;
8164
8165 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8166 a power of two, we can replace this with an ASHIFT. */
8167 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8168 && (i = exact_log2 (constop)) >= 0)
8169 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8170
8171 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8172 or XOR, then try to apply the distributive law. This may eliminate
8173 operations if either branch can be simplified because of the AND.
8174 It may also make some cases more complex, but those cases probably
8175 won't match a pattern either with or without this. */
8176
8177 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8178 return
8179 gen_lowpart
8180 (mode,
8181 apply_distributive_law
8182 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8183 simplify_and_const_int (NULL_RTX,
8184 GET_MODE (varop),
8185 XEXP (varop, 0),
8186 constop),
8187 simplify_and_const_int (NULL_RTX,
8188 GET_MODE (varop),
8189 XEXP (varop, 1),
8190 constop))));
8191
8192 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8193 the AND and see if one of the operands simplifies to zero. If so, we
8194 may eliminate it. */
8195
8196 if (GET_CODE (varop) == PLUS
8197 && exact_log2 (constop + 1) >= 0)
8198 {
8199 rtx o0, o1;
8200
8201 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8202 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8203 if (o0 == const0_rtx)
8204 return o1;
8205 if (o1 == const0_rtx)
8206 return o0;
8207 }
8208
8209 /* Make a SUBREG if necessary. If we can't make it, fail. */
8210 varop = gen_lowpart (mode, varop);
8211 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8212 return NULL_RTX;
8213
8214 /* If we are only masking insignificant bits, return VAROP. */
8215 if (constop == nonzero)
8216 return varop;
8217
8218 if (varop == orig_varop && constop == orig_constop)
8219 return NULL_RTX;
8220
8221 /* Otherwise, return an AND. */
8222 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8223 }
8224
8225
8226 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8227 in MODE.
8228
8229 Return an equivalent form, if different from X. Otherwise, return X. If
8230 X is zero, we are to always construct the equivalent form. */
8231
8232 static rtx
8233 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8234 unsigned HOST_WIDE_INT constop)
8235 {
8236 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8237 if (tem)
8238 return tem;
8239
8240 if (!x)
8241 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8242 gen_int_mode (constop, mode));
8243 if (GET_MODE (x) != mode)
8244 x = gen_lowpart (mode, x);
8245 return x;
8246 }
8247 \f
8248 /* Given a REG, X, compute which bits in X can be nonzero.
8249 We don't care about bits outside of those defined in MODE.
8250
8251 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8252 a shift, AND, or zero_extract, we can do better. */
8253
8254 static rtx
8255 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8256 rtx known_x ATTRIBUTE_UNUSED,
8257 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8258 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8259 unsigned HOST_WIDE_INT *nonzero)
8260 {
8261 rtx tem;
8262
8263 /* If X is a register whose nonzero bits value is current, use it.
8264 Otherwise, if X is a register whose value we can find, use that
8265 value. Otherwise, use the previously-computed global nonzero bits
8266 for this register. */
8267
8268 if (reg_stat[REGNO (x)].last_set_value != 0
8269 && (reg_stat[REGNO (x)].last_set_mode == mode
8270 || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8271 && GET_MODE_CLASS (mode) == MODE_INT))
8272 && (reg_stat[REGNO (x)].last_set_label == label_tick
8273 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8274 && REG_N_SETS (REGNO (x)) == 1
8275 && ! REGNO_REG_SET_P
8276 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8277 REGNO (x))))
8278 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8279 {
8280 *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8281 return NULL;
8282 }
8283
8284 tem = get_last_value (x);
8285
8286 if (tem)
8287 {
8288 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8289 /* If X is narrower than MODE and TEM is a non-negative
8290 constant that would appear negative in the mode of X,
8291 sign-extend it for use in reg_nonzero_bits because some
8292 machines (maybe most) will actually do the sign-extension
8293 and this is the conservative approach.
8294
8295 ??? For 2.5, try to tighten up the MD files in this regard
8296 instead of this kludge. */
8297
8298 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8299 && GET_CODE (tem) == CONST_INT
8300 && INTVAL (tem) > 0
8301 && 0 != (INTVAL (tem)
8302 & ((HOST_WIDE_INT) 1
8303 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8304 tem = GEN_INT (INTVAL (tem)
8305 | ((HOST_WIDE_INT) (-1)
8306 << GET_MODE_BITSIZE (GET_MODE (x))));
8307 #endif
8308 return tem;
8309 }
8310 else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8311 {
8312 unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8313
8314 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8315 /* We don't know anything about the upper bits. */
8316 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8317 *nonzero &= mask;
8318 }
8319
8320 return NULL;
8321 }
8322
8323 /* Return the number of bits at the high-order end of X that are known to
8324 be equal to the sign bit. X will be used in mode MODE; if MODE is
8325 VOIDmode, X will be used in its own mode. The returned value will always
8326 be between 1 and the number of bits in MODE. */
8327
8328 static rtx
8329 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8330 rtx known_x ATTRIBUTE_UNUSED,
8331 enum machine_mode known_mode
8332 ATTRIBUTE_UNUSED,
8333 unsigned int known_ret ATTRIBUTE_UNUSED,
8334 unsigned int *result)
8335 {
8336 rtx tem;
8337
8338 if (reg_stat[REGNO (x)].last_set_value != 0
8339 && reg_stat[REGNO (x)].last_set_mode == mode
8340 && (reg_stat[REGNO (x)].last_set_label == label_tick
8341 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8342 && REG_N_SETS (REGNO (x)) == 1
8343 && ! REGNO_REG_SET_P
8344 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
8345 REGNO (x))))
8346 && INSN_CUID (reg_stat[REGNO (x)].last_set) < subst_low_cuid)
8347 {
8348 *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8349 return NULL;
8350 }
8351
8352 tem = get_last_value (x);
8353 if (tem != 0)
8354 return tem;
8355
8356 if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8357 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8358 *result = reg_stat[REGNO (x)].sign_bit_copies;
8359
8360 return NULL;
8361 }
8362 \f
8363 /* Return the number of "extended" bits there are in X, when interpreted
8364 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8365 unsigned quantities, this is the number of high-order zero bits.
8366 For signed quantities, this is the number of copies of the sign bit
8367 minus 1. In both case, this function returns the number of "spare"
8368 bits. For example, if two quantities for which this function returns
8369 at least 1 are added, the addition is known not to overflow.
8370
8371 This function will always return 0 unless called during combine, which
8372 implies that it must be called from a define_split. */
8373
8374 unsigned int
8375 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8376 {
8377 if (nonzero_sign_valid == 0)
8378 return 0;
8379
8380 return (unsignedp
8381 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8382 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8383 - floor_log2 (nonzero_bits (x, mode)))
8384 : 0)
8385 : num_sign_bit_copies (x, mode) - 1);
8386 }
8387 \f
8388 /* This function is called from `simplify_shift_const' to merge two
8389 outer operations. Specifically, we have already found that we need
8390 to perform operation *POP0 with constant *PCONST0 at the outermost
8391 position. We would now like to also perform OP1 with constant CONST1
8392 (with *POP0 being done last).
8393
8394 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8395 the resulting operation. *PCOMP_P is set to 1 if we would need to
8396 complement the innermost operand, otherwise it is unchanged.
8397
8398 MODE is the mode in which the operation will be done. No bits outside
8399 the width of this mode matter. It is assumed that the width of this mode
8400 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8401
8402 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8403 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8404 result is simply *PCONST0.
8405
8406 If the resulting operation cannot be expressed as one operation, we
8407 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8408
8409 static int
8410 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)
8411 {
8412 enum rtx_code op0 = *pop0;
8413 HOST_WIDE_INT const0 = *pconst0;
8414
8415 const0 &= GET_MODE_MASK (mode);
8416 const1 &= GET_MODE_MASK (mode);
8417
8418 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8419 if (op0 == AND)
8420 const1 &= const0;
8421
8422 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8423 if OP0 is SET. */
8424
8425 if (op1 == UNKNOWN || op0 == SET)
8426 return 1;
8427
8428 else if (op0 == UNKNOWN)
8429 op0 = op1, const0 = const1;
8430
8431 else if (op0 == op1)
8432 {
8433 switch (op0)
8434 {
8435 case AND:
8436 const0 &= const1;
8437 break;
8438 case IOR:
8439 const0 |= const1;
8440 break;
8441 case XOR:
8442 const0 ^= const1;
8443 break;
8444 case PLUS:
8445 const0 += const1;
8446 break;
8447 case NEG:
8448 op0 = UNKNOWN;
8449 break;
8450 default:
8451 break;
8452 }
8453 }
8454
8455 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8456 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8457 return 0;
8458
8459 /* If the two constants aren't the same, we can't do anything. The
8460 remaining six cases can all be done. */
8461 else if (const0 != const1)
8462 return 0;
8463
8464 else
8465 switch (op0)
8466 {
8467 case IOR:
8468 if (op1 == AND)
8469 /* (a & b) | b == b */
8470 op0 = SET;
8471 else /* op1 == XOR */
8472 /* (a ^ b) | b == a | b */
8473 {;}
8474 break;
8475
8476 case XOR:
8477 if (op1 == AND)
8478 /* (a & b) ^ b == (~a) & b */
8479 op0 = AND, *pcomp_p = 1;
8480 else /* op1 == IOR */
8481 /* (a | b) ^ b == a & ~b */
8482 op0 = AND, const0 = ~const0;
8483 break;
8484
8485 case AND:
8486 if (op1 == IOR)
8487 /* (a | b) & b == b */
8488 op0 = SET;
8489 else /* op1 == XOR */
8490 /* (a ^ b) & b) == (~a) & b */
8491 *pcomp_p = 1;
8492 break;
8493 default:
8494 break;
8495 }
8496
8497 /* Check for NO-OP cases. */
8498 const0 &= GET_MODE_MASK (mode);
8499 if (const0 == 0
8500 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8501 op0 = UNKNOWN;
8502 else if (const0 == 0 && op0 == AND)
8503 op0 = SET;
8504 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8505 && op0 == AND)
8506 op0 = UNKNOWN;
8507
8508 /* ??? Slightly redundant with the above mask, but not entirely.
8509 Moving this above means we'd have to sign-extend the mode mask
8510 for the final test. */
8511 const0 = trunc_int_for_mode (const0, mode);
8512
8513 *pop0 = op0;
8514 *pconst0 = const0;
8515
8516 return 1;
8517 }
8518 \f
8519 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8520 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
8521 simplify it. Otherwise, return a simplified value.
8522
8523 The shift is normally computed in the widest mode we find in VAROP, as
8524 long as it isn't a different number of words than RESULT_MODE. Exceptions
8525 are ASHIFTRT and ROTATE, which are always done in their original mode. */
8526
8527 static rtx
8528 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8529 rtx varop, int orig_count)
8530 {
8531 enum rtx_code orig_code = code;
8532 rtx orig_varop = varop;
8533 int count;
8534 enum machine_mode mode = result_mode;
8535 enum machine_mode shift_mode, tmode;
8536 unsigned int mode_words
8537 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8538 /* We form (outer_op (code varop count) (outer_const)). */
8539 enum rtx_code outer_op = UNKNOWN;
8540 HOST_WIDE_INT outer_const = 0;
8541 int complement_p = 0;
8542 rtx new, x;
8543
8544 /* Make sure and truncate the "natural" shift on the way in. We don't
8545 want to do this inside the loop as it makes it more difficult to
8546 combine shifts. */
8547 if (SHIFT_COUNT_TRUNCATED)
8548 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8549
8550 /* If we were given an invalid count, don't do anything except exactly
8551 what was requested. */
8552
8553 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8554 return NULL_RTX;
8555
8556 count = orig_count;
8557
8558 /* Unless one of the branches of the `if' in this loop does a `continue',
8559 we will `break' the loop after the `if'. */
8560
8561 while (count != 0)
8562 {
8563 /* If we have an operand of (clobber (const_int 0)), fail. */
8564 if (GET_CODE (varop) == CLOBBER)
8565 return NULL_RTX;
8566
8567 /* If we discovered we had to complement VAROP, leave. Making a NOT
8568 here would cause an infinite loop. */
8569 if (complement_p)
8570 break;
8571
8572 /* Convert ROTATERT to ROTATE. */
8573 if (code == ROTATERT)
8574 {
8575 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8576 code = ROTATE;
8577 if (VECTOR_MODE_P (result_mode))
8578 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8579 else
8580 count = bitsize - count;
8581 }
8582
8583 /* We need to determine what mode we will do the shift in. If the
8584 shift is a right shift or a ROTATE, we must always do it in the mode
8585 it was originally done in. Otherwise, we can do it in MODE, the
8586 widest mode encountered. */
8587 shift_mode
8588 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8589 ? result_mode : mode);
8590
8591 /* Handle cases where the count is greater than the size of the mode
8592 minus 1. For ASHIFT, use the size minus one as the count (this can
8593 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8594 take the count modulo the size. For other shifts, the result is
8595 zero.
8596
8597 Since these shifts are being produced by the compiler by combining
8598 multiple operations, each of which are defined, we know what the
8599 result is supposed to be. */
8600
8601 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
8602 {
8603 if (code == ASHIFTRT)
8604 count = GET_MODE_BITSIZE (shift_mode) - 1;
8605 else if (code == ROTATE || code == ROTATERT)
8606 count %= GET_MODE_BITSIZE (shift_mode);
8607 else
8608 {
8609 /* We can't simply return zero because there may be an
8610 outer op. */
8611 varop = const0_rtx;
8612 count = 0;
8613 break;
8614 }
8615 }
8616
8617 /* An arithmetic right shift of a quantity known to be -1 or 0
8618 is a no-op. */
8619 if (code == ASHIFTRT
8620 && (num_sign_bit_copies (varop, shift_mode)
8621 == GET_MODE_BITSIZE (shift_mode)))
8622 {
8623 count = 0;
8624 break;
8625 }
8626
8627 /* If we are doing an arithmetic right shift and discarding all but
8628 the sign bit copies, this is equivalent to doing a shift by the
8629 bitsize minus one. Convert it into that shift because it will often
8630 allow other simplifications. */
8631
8632 if (code == ASHIFTRT
8633 && (count + num_sign_bit_copies (varop, shift_mode)
8634 >= GET_MODE_BITSIZE (shift_mode)))
8635 count = GET_MODE_BITSIZE (shift_mode) - 1;
8636
8637 /* We simplify the tests below and elsewhere by converting
8638 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8639 `make_compound_operation' will convert it to an ASHIFTRT for
8640 those machines (such as VAX) that don't have an LSHIFTRT. */
8641 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8642 && code == ASHIFTRT
8643 && ((nonzero_bits (varop, shift_mode)
8644 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8645 == 0))
8646 code = LSHIFTRT;
8647
8648 if (((code == LSHIFTRT
8649 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8650 && !(nonzero_bits (varop, shift_mode) >> count))
8651 || (code == ASHIFT
8652 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8653 && !((nonzero_bits (varop, shift_mode) << count)
8654 & GET_MODE_MASK (shift_mode))))
8655 && !side_effects_p (varop))
8656 varop = const0_rtx;
8657
8658 switch (GET_CODE (varop))
8659 {
8660 case SIGN_EXTEND:
8661 case ZERO_EXTEND:
8662 case SIGN_EXTRACT:
8663 case ZERO_EXTRACT:
8664 new = expand_compound_operation (varop);
8665 if (new != varop)
8666 {
8667 varop = new;
8668 continue;
8669 }
8670 break;
8671
8672 case MEM:
8673 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8674 minus the width of a smaller mode, we can do this with a
8675 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8676 if ((code == ASHIFTRT || code == LSHIFTRT)
8677 && ! mode_dependent_address_p (XEXP (varop, 0))
8678 && ! MEM_VOLATILE_P (varop)
8679 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8680 MODE_INT, 1)) != BLKmode)
8681 {
8682 new = adjust_address_nv (varop, tmode,
8683 BYTES_BIG_ENDIAN ? 0
8684 : count / BITS_PER_UNIT);
8685
8686 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
8687 : ZERO_EXTEND, mode, new);
8688 count = 0;
8689 continue;
8690 }
8691 break;
8692
8693 case SUBREG:
8694 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8695 the same number of words as what we've seen so far. Then store
8696 the widest mode in MODE. */
8697 if (subreg_lowpart_p (varop)
8698 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8699 > GET_MODE_SIZE (GET_MODE (varop)))
8700 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8701 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8702 == mode_words)
8703 {
8704 varop = SUBREG_REG (varop);
8705 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8706 mode = GET_MODE (varop);
8707 continue;
8708 }
8709 break;
8710
8711 case MULT:
8712 /* Some machines use MULT instead of ASHIFT because MULT
8713 is cheaper. But it is still better on those machines to
8714 merge two shifts into one. */
8715 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8716 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8717 {
8718 varop
8719 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
8720 XEXP (varop, 0),
8721 GEN_INT (exact_log2 (
8722 INTVAL (XEXP (varop, 1)))));
8723 continue;
8724 }
8725 break;
8726
8727 case UDIV:
8728 /* Similar, for when divides are cheaper. */
8729 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8730 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8731 {
8732 varop
8733 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
8734 XEXP (varop, 0),
8735 GEN_INT (exact_log2 (
8736 INTVAL (XEXP (varop, 1)))));
8737 continue;
8738 }
8739 break;
8740
8741 case ASHIFTRT:
8742 /* If we are extracting just the sign bit of an arithmetic
8743 right shift, that shift is not needed. However, the sign
8744 bit of a wider mode may be different from what would be
8745 interpreted as the sign bit in a narrower mode, so, if
8746 the result is narrower, don't discard the shift. */
8747 if (code == LSHIFTRT
8748 && count == (GET_MODE_BITSIZE (result_mode) - 1)
8749 && (GET_MODE_BITSIZE (result_mode)
8750 >= GET_MODE_BITSIZE (GET_MODE (varop))))
8751 {
8752 varop = XEXP (varop, 0);
8753 continue;
8754 }
8755
8756 /* ... fall through ... */
8757
8758 case LSHIFTRT:
8759 case ASHIFT:
8760 case ROTATE:
8761 /* Here we have two nested shifts. The result is usually the
8762 AND of a new shift with a mask. We compute the result below. */
8763 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8764 && INTVAL (XEXP (varop, 1)) >= 0
8765 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8766 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8767 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8768 && !VECTOR_MODE_P (result_mode))
8769 {
8770 enum rtx_code first_code = GET_CODE (varop);
8771 unsigned int first_count = INTVAL (XEXP (varop, 1));
8772 unsigned HOST_WIDE_INT mask;
8773 rtx mask_rtx;
8774
8775 /* We have one common special case. We can't do any merging if
8776 the inner code is an ASHIFTRT of a smaller mode. However, if
8777 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8778 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8779 we can convert it to
8780 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8781 This simplifies certain SIGN_EXTEND operations. */
8782 if (code == ASHIFT && first_code == ASHIFTRT
8783 && count == (GET_MODE_BITSIZE (result_mode)
8784 - GET_MODE_BITSIZE (GET_MODE (varop))))
8785 {
8786 /* C3 has the low-order C1 bits zero. */
8787
8788 mask = (GET_MODE_MASK (mode)
8789 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
8790
8791 varop = simplify_and_const_int (NULL_RTX, result_mode,
8792 XEXP (varop, 0), mask);
8793 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8794 varop, count);
8795 count = first_count;
8796 code = ASHIFTRT;
8797 continue;
8798 }
8799
8800 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8801 than C1 high-order bits equal to the sign bit, we can convert
8802 this to either an ASHIFT or an ASHIFTRT depending on the
8803 two counts.
8804
8805 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8806
8807 if (code == ASHIFTRT && first_code == ASHIFT
8808 && GET_MODE (varop) == shift_mode
8809 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8810 > first_count))
8811 {
8812 varop = XEXP (varop, 0);
8813 count -= first_count;
8814 if (count < 0)
8815 {
8816 count = -count;
8817 code = ASHIFT;
8818 }
8819
8820 continue;
8821 }
8822
8823 /* There are some cases we can't do. If CODE is ASHIFTRT,
8824 we can only do this if FIRST_CODE is also ASHIFTRT.
8825
8826 We can't do the case when CODE is ROTATE and FIRST_CODE is
8827 ASHIFTRT.
8828
8829 If the mode of this shift is not the mode of the outer shift,
8830 we can't do this if either shift is a right shift or ROTATE.
8831
8832 Finally, we can't do any of these if the mode is too wide
8833 unless the codes are the same.
8834
8835 Handle the case where the shift codes are the same
8836 first. */
8837
8838 if (code == first_code)
8839 {
8840 if (GET_MODE (varop) != result_mode
8841 && (code == ASHIFTRT || code == LSHIFTRT
8842 || code == ROTATE))
8843 break;
8844
8845 count += first_count;
8846 varop = XEXP (varop, 0);
8847 continue;
8848 }
8849
8850 if (code == ASHIFTRT
8851 || (code == ROTATE && first_code == ASHIFTRT)
8852 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8853 || (GET_MODE (varop) != result_mode
8854 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8855 || first_code == ROTATE
8856 || code == ROTATE)))
8857 break;
8858
8859 /* To compute the mask to apply after the shift, shift the
8860 nonzero bits of the inner shift the same way the
8861 outer shift will. */
8862
8863 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8864
8865 mask_rtx
8866 = simplify_const_binary_operation (code, result_mode, mask_rtx,
8867 GEN_INT (count));
8868
8869 /* Give up if we can't compute an outer operation to use. */
8870 if (mask_rtx == 0
8871 || GET_CODE (mask_rtx) != CONST_INT
8872 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8873 INTVAL (mask_rtx),
8874 result_mode, &complement_p))
8875 break;
8876
8877 /* If the shifts are in the same direction, we add the
8878 counts. Otherwise, we subtract them. */
8879 if ((code == ASHIFTRT || code == LSHIFTRT)
8880 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8881 count += first_count;
8882 else
8883 count -= first_count;
8884
8885 /* If COUNT is positive, the new shift is usually CODE,
8886 except for the two exceptions below, in which case it is
8887 FIRST_CODE. If the count is negative, FIRST_CODE should
8888 always be used */
8889 if (count > 0
8890 && ((first_code == ROTATE && code == ASHIFT)
8891 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8892 code = first_code;
8893 else if (count < 0)
8894 code = first_code, count = -count;
8895
8896 varop = XEXP (varop, 0);
8897 continue;
8898 }
8899
8900 /* If we have (A << B << C) for any shift, we can convert this to
8901 (A << C << B). This wins if A is a constant. Only try this if
8902 B is not a constant. */
8903
8904 else if (GET_CODE (varop) == code
8905 && GET_CODE (XEXP (varop, 0)) == CONST_INT
8906 && GET_CODE (XEXP (varop, 1)) != CONST_INT)
8907 {
8908 rtx new = simplify_const_binary_operation (code, mode,
8909 XEXP (varop, 0),
8910 GEN_INT (count));
8911 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
8912 count = 0;
8913 continue;
8914 }
8915 break;
8916
8917 case NOT:
8918 /* Make this fit the case below. */
8919 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
8920 GEN_INT (GET_MODE_MASK (mode)));
8921 continue;
8922
8923 case IOR:
8924 case AND:
8925 case XOR:
8926 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8927 with C the size of VAROP - 1 and the shift is logical if
8928 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8929 we have an (le X 0) operation. If we have an arithmetic shift
8930 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8931 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8932
8933 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8934 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8935 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8936 && (code == LSHIFTRT || code == ASHIFTRT)
8937 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
8938 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8939 {
8940 count = 0;
8941 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
8942 const0_rtx);
8943
8944 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8945 varop = gen_rtx_NEG (GET_MODE (varop), varop);
8946
8947 continue;
8948 }
8949
8950 /* If we have (shift (logical)), move the logical to the outside
8951 to allow it to possibly combine with another logical and the
8952 shift to combine with another shift. This also canonicalizes to
8953 what a ZERO_EXTRACT looks like. Also, some machines have
8954 (and (shift)) insns. */
8955
8956 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8957 /* We can't do this if we have (ashiftrt (xor)) and the
8958 constant has its sign bit set in shift_mode. */
8959 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8960 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8961 shift_mode))
8962 && (new = simplify_const_binary_operation (code, result_mode,
8963 XEXP (varop, 1),
8964 GEN_INT (count))) != 0
8965 && GET_CODE (new) == CONST_INT
8966 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8967 INTVAL (new), result_mode, &complement_p))
8968 {
8969 varop = XEXP (varop, 0);
8970 continue;
8971 }
8972
8973 /* If we can't do that, try to simplify the shift in each arm of the
8974 logical expression, make a new logical expression, and apply
8975 the inverse distributive law. This also can't be done
8976 for some (ashiftrt (xor)). */
8977 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8978 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
8979 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
8980 shift_mode)))
8981 {
8982 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8983 XEXP (varop, 0), count);
8984 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8985 XEXP (varop, 1), count);
8986
8987 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
8988 lhs, rhs);
8989 varop = apply_distributive_law (varop);
8990
8991 count = 0;
8992 continue;
8993 }
8994 break;
8995
8996 case EQ:
8997 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8998 says that the sign bit can be tested, FOO has mode MODE, C is
8999 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9000 that may be nonzero. */
9001 if (code == LSHIFTRT
9002 && XEXP (varop, 1) == const0_rtx
9003 && GET_MODE (XEXP (varop, 0)) == result_mode
9004 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9005 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9006 && STORE_FLAG_VALUE == -1
9007 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9008 && merge_outer_ops (&outer_op, &outer_const, XOR,
9009 (HOST_WIDE_INT) 1, result_mode,
9010 &complement_p))
9011 {
9012 varop = XEXP (varop, 0);
9013 count = 0;
9014 continue;
9015 }
9016 break;
9017
9018 case NEG:
9019 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9020 than the number of bits in the mode is equivalent to A. */
9021 if (code == LSHIFTRT
9022 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9023 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9024 {
9025 varop = XEXP (varop, 0);
9026 count = 0;
9027 continue;
9028 }
9029
9030 /* NEG commutes with ASHIFT since it is multiplication. Move the
9031 NEG outside to allow shifts to combine. */
9032 if (code == ASHIFT
9033 && merge_outer_ops (&outer_op, &outer_const, NEG,
9034 (HOST_WIDE_INT) 0, result_mode,
9035 &complement_p))
9036 {
9037 varop = XEXP (varop, 0);
9038 continue;
9039 }
9040 break;
9041
9042 case PLUS:
9043 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9044 is one less than the number of bits in the mode is
9045 equivalent to (xor A 1). */
9046 if (code == LSHIFTRT
9047 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9048 && XEXP (varop, 1) == constm1_rtx
9049 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9050 && merge_outer_ops (&outer_op, &outer_const, XOR,
9051 (HOST_WIDE_INT) 1, result_mode,
9052 &complement_p))
9053 {
9054 count = 0;
9055 varop = XEXP (varop, 0);
9056 continue;
9057 }
9058
9059 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9060 that might be nonzero in BAR are those being shifted out and those
9061 bits are known zero in FOO, we can replace the PLUS with FOO.
9062 Similarly in the other operand order. This code occurs when
9063 we are computing the size of a variable-size array. */
9064
9065 if ((code == ASHIFTRT || code == LSHIFTRT)
9066 && count < HOST_BITS_PER_WIDE_INT
9067 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9068 && (nonzero_bits (XEXP (varop, 1), result_mode)
9069 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9070 {
9071 varop = XEXP (varop, 0);
9072 continue;
9073 }
9074 else if ((code == ASHIFTRT || code == LSHIFTRT)
9075 && count < HOST_BITS_PER_WIDE_INT
9076 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9077 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9078 >> count)
9079 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9080 & nonzero_bits (XEXP (varop, 1),
9081 result_mode)))
9082 {
9083 varop = XEXP (varop, 1);
9084 continue;
9085 }
9086
9087 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9088 if (code == ASHIFT
9089 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9090 && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9091 XEXP (varop, 1),
9092 GEN_INT (count))) != 0
9093 && GET_CODE (new) == CONST_INT
9094 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9095 INTVAL (new), result_mode, &complement_p))
9096 {
9097 varop = XEXP (varop, 0);
9098 continue;
9099 }
9100
9101 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9102 signbit', and attempt to change the PLUS to an XOR and move it to
9103 the outer operation as is done above in the AND/IOR/XOR case
9104 leg for shift(logical). See details in logical handling above
9105 for reasoning in doing so. */
9106 if (code == LSHIFTRT
9107 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9108 && mode_signbit_p (result_mode, XEXP (varop, 1))
9109 && (new = simplify_const_binary_operation (code, result_mode,
9110 XEXP (varop, 1),
9111 GEN_INT (count))) != 0
9112 && GET_CODE (new) == CONST_INT
9113 && merge_outer_ops (&outer_op, &outer_const, XOR,
9114 INTVAL (new), result_mode, &complement_p))
9115 {
9116 varop = XEXP (varop, 0);
9117 continue;
9118 }
9119
9120 break;
9121
9122 case MINUS:
9123 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9124 with C the size of VAROP - 1 and the shift is logical if
9125 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9126 we have a (gt X 0) operation. If the shift is arithmetic with
9127 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9128 we have a (neg (gt X 0)) operation. */
9129
9130 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9131 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9132 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9133 && (code == LSHIFTRT || code == ASHIFTRT)
9134 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9135 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9136 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9137 {
9138 count = 0;
9139 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9140 const0_rtx);
9141
9142 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9143 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9144
9145 continue;
9146 }
9147 break;
9148
9149 case TRUNCATE:
9150 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9151 if the truncate does not affect the value. */
9152 if (code == LSHIFTRT
9153 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9154 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9155 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9156 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9157 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9158 {
9159 rtx varop_inner = XEXP (varop, 0);
9160
9161 varop_inner
9162 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9163 XEXP (varop_inner, 0),
9164 GEN_INT
9165 (count + INTVAL (XEXP (varop_inner, 1))));
9166 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9167 count = 0;
9168 continue;
9169 }
9170 break;
9171
9172 default:
9173 break;
9174 }
9175
9176 break;
9177 }
9178
9179 /* We need to determine what mode to do the shift in. If the shift is
9180 a right shift or ROTATE, we must always do it in the mode it was
9181 originally done in. Otherwise, we can do it in MODE, the widest mode
9182 encountered. The code we care about is that of the shift that will
9183 actually be done, not the shift that was originally requested. */
9184 shift_mode
9185 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9186 ? result_mode : mode);
9187
9188 /* We have now finished analyzing the shift. The result should be
9189 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9190 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9191 to the result of the shift. OUTER_CONST is the relevant constant,
9192 but we must turn off all bits turned off in the shift. */
9193
9194 if (outer_op == UNKNOWN
9195 && orig_code == code && orig_count == count
9196 && varop == orig_varop
9197 && shift_mode == GET_MODE (varop))
9198 return NULL_RTX;
9199
9200 /* Make a SUBREG if necessary. If we can't make it, fail. */
9201 varop = gen_lowpart (shift_mode, varop);
9202 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9203 return NULL_RTX;
9204
9205 /* If we have an outer operation and we just made a shift, it is
9206 possible that we could have simplified the shift were it not
9207 for the outer operation. So try to do the simplification
9208 recursively. */
9209
9210 if (outer_op != UNKNOWN)
9211 x = simplify_shift_const_1 (code, shift_mode, varop, count);
9212 else
9213 x = NULL_RTX;
9214
9215 if (x == NULL_RTX)
9216 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9217
9218 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9219 turn off all the bits that the shift would have turned off. */
9220 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9221 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9222 GET_MODE_MASK (result_mode) >> orig_count);
9223
9224 /* Do the remainder of the processing in RESULT_MODE. */
9225 x = gen_lowpart_or_truncate (result_mode, x);
9226
9227 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9228 operation. */
9229 if (complement_p)
9230 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9231
9232 if (outer_op != UNKNOWN)
9233 {
9234 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9235 outer_const = trunc_int_for_mode (outer_const, result_mode);
9236
9237 if (outer_op == AND)
9238 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9239 else if (outer_op == SET)
9240 {
9241 /* This means that we have determined that the result is
9242 equivalent to a constant. This should be rare. */
9243 if (!side_effects_p (x))
9244 x = GEN_INT (outer_const);
9245 }
9246 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9247 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9248 else
9249 x = simplify_gen_binary (outer_op, result_mode, x,
9250 GEN_INT (outer_const));
9251 }
9252
9253 return x;
9254 }
9255
9256 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9257 The result of the shift is RESULT_MODE. If we cannot simplify it,
9258 return X or, if it is NULL, synthesize the expression with
9259 simplify_gen_binary. Otherwise, return a simplified value.
9260
9261 The shift is normally computed in the widest mode we find in VAROP, as
9262 long as it isn't a different number of words than RESULT_MODE. Exceptions
9263 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9264
9265 static rtx
9266 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9267 rtx varop, int count)
9268 {
9269 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9270 if (tem)
9271 return tem;
9272
9273 if (!x)
9274 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9275 if (GET_MODE (x) != result_mode)
9276 x = gen_lowpart (result_mode, x);
9277 return x;
9278 }
9279
9280 \f
9281 /* Like recog, but we receive the address of a pointer to a new pattern.
9282 We try to match the rtx that the pointer points to.
9283 If that fails, we may try to modify or replace the pattern,
9284 storing the replacement into the same pointer object.
9285
9286 Modifications include deletion or addition of CLOBBERs.
9287
9288 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9289 the CLOBBERs are placed.
9290
9291 The value is the final insn code from the pattern ultimately matched,
9292 or -1. */
9293
9294 static int
9295 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9296 {
9297 rtx pat = *pnewpat;
9298 int insn_code_number;
9299 int num_clobbers_to_add = 0;
9300 int i;
9301 rtx notes = 0;
9302 rtx old_notes, old_pat;
9303
9304 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9305 we use to indicate that something didn't match. If we find such a
9306 thing, force rejection. */
9307 if (GET_CODE (pat) == PARALLEL)
9308 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9309 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9310 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9311 return -1;
9312
9313 old_pat = PATTERN (insn);
9314 old_notes = REG_NOTES (insn);
9315 PATTERN (insn) = pat;
9316 REG_NOTES (insn) = 0;
9317
9318 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9319
9320 /* If it isn't, there is the possibility that we previously had an insn
9321 that clobbered some register as a side effect, but the combined
9322 insn doesn't need to do that. So try once more without the clobbers
9323 unless this represents an ASM insn. */
9324
9325 if (insn_code_number < 0 && ! check_asm_operands (pat)
9326 && GET_CODE (pat) == PARALLEL)
9327 {
9328 int pos;
9329
9330 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9331 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9332 {
9333 if (i != pos)
9334 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9335 pos++;
9336 }
9337
9338 SUBST_INT (XVECLEN (pat, 0), pos);
9339
9340 if (pos == 1)
9341 pat = XVECEXP (pat, 0, 0);
9342
9343 PATTERN (insn) = pat;
9344 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9345 }
9346 PATTERN (insn) = old_pat;
9347 REG_NOTES (insn) = old_notes;
9348
9349 /* Recognize all noop sets, these will be killed by followup pass. */
9350 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9351 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9352
9353 /* If we had any clobbers to add, make a new pattern than contains
9354 them. Then check to make sure that all of them are dead. */
9355 if (num_clobbers_to_add)
9356 {
9357 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9358 rtvec_alloc (GET_CODE (pat) == PARALLEL
9359 ? (XVECLEN (pat, 0)
9360 + num_clobbers_to_add)
9361 : num_clobbers_to_add + 1));
9362
9363 if (GET_CODE (pat) == PARALLEL)
9364 for (i = 0; i < XVECLEN (pat, 0); i++)
9365 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9366 else
9367 XVECEXP (newpat, 0, 0) = pat;
9368
9369 add_clobbers (newpat, insn_code_number);
9370
9371 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9372 i < XVECLEN (newpat, 0); i++)
9373 {
9374 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9375 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9376 return -1;
9377 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9378 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9379 }
9380 pat = newpat;
9381 }
9382
9383 *pnewpat = pat;
9384 *pnotes = notes;
9385
9386 return insn_code_number;
9387 }
9388 \f
9389 /* Like gen_lowpart_general but for use by combine. In combine it
9390 is not possible to create any new pseudoregs. However, it is
9391 safe to create invalid memory addresses, because combine will
9392 try to recognize them and all they will do is make the combine
9393 attempt fail.
9394
9395 If for some reason this cannot do its job, an rtx
9396 (clobber (const_int 0)) is returned.
9397 An insn containing that will not be recognized. */
9398
9399 static rtx
9400 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9401 {
9402 enum machine_mode imode = GET_MODE (x);
9403 unsigned int osize = GET_MODE_SIZE (omode);
9404 unsigned int isize = GET_MODE_SIZE (imode);
9405 rtx result;
9406
9407 if (omode == imode)
9408 return x;
9409
9410 /* Return identity if this is a CONST or symbolic reference. */
9411 if (omode == Pmode
9412 && (GET_CODE (x) == CONST
9413 || GET_CODE (x) == SYMBOL_REF
9414 || GET_CODE (x) == LABEL_REF))
9415 return x;
9416
9417 /* We can only support MODE being wider than a word if X is a
9418 constant integer or has a mode the same size. */
9419 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9420 && ! ((imode == VOIDmode
9421 && (GET_CODE (x) == CONST_INT
9422 || GET_CODE (x) == CONST_DOUBLE))
9423 || isize == osize))
9424 goto fail;
9425
9426 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9427 won't know what to do. So we will strip off the SUBREG here and
9428 process normally. */
9429 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9430 {
9431 x = SUBREG_REG (x);
9432
9433 /* For use in case we fall down into the address adjustments
9434 further below, we need to adjust the known mode and size of
9435 x; imode and isize, since we just adjusted x. */
9436 imode = GET_MODE (x);
9437
9438 if (imode == omode)
9439 return x;
9440
9441 isize = GET_MODE_SIZE (imode);
9442 }
9443
9444 result = gen_lowpart_common (omode, x);
9445
9446 #ifdef CANNOT_CHANGE_MODE_CLASS
9447 if (result != 0 && GET_CODE (result) == SUBREG)
9448 record_subregs_of_mode (result);
9449 #endif
9450
9451 if (result)
9452 return result;
9453
9454 if (MEM_P (x))
9455 {
9456 int offset = 0;
9457
9458 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9459 address. */
9460 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9461 goto fail;
9462
9463 /* If we want to refer to something bigger than the original memref,
9464 generate a paradoxical subreg instead. That will force a reload
9465 of the original memref X. */
9466 if (isize < osize)
9467 return gen_rtx_SUBREG (omode, x, 0);
9468
9469 if (WORDS_BIG_ENDIAN)
9470 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9471
9472 /* Adjust the address so that the address-after-the-data is
9473 unchanged. */
9474 if (BYTES_BIG_ENDIAN)
9475 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9476
9477 return adjust_address_nv (x, omode, offset);
9478 }
9479
9480 /* If X is a comparison operator, rewrite it in a new mode. This
9481 probably won't match, but may allow further simplifications. */
9482 else if (COMPARISON_P (x))
9483 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9484
9485 /* If we couldn't simplify X any other way, just enclose it in a
9486 SUBREG. Normally, this SUBREG won't match, but some patterns may
9487 include an explicit SUBREG or we may simplify it further in combine. */
9488 else
9489 {
9490 int offset = 0;
9491 rtx res;
9492
9493 offset = subreg_lowpart_offset (omode, imode);
9494 if (imode == VOIDmode)
9495 {
9496 imode = int_mode_for_mode (omode);
9497 x = gen_lowpart_common (imode, x);
9498 if (x == NULL)
9499 goto fail;
9500 }
9501 res = simplify_gen_subreg (omode, x, imode, offset);
9502 if (res)
9503 return res;
9504 }
9505
9506 fail:
9507 return gen_rtx_CLOBBER (imode, const0_rtx);
9508 }
9509 \f
9510 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9511 comparison code that will be tested.
9512
9513 The result is a possibly different comparison code to use. *POP0 and
9514 *POP1 may be updated.
9515
9516 It is possible that we might detect that a comparison is either always
9517 true or always false. However, we do not perform general constant
9518 folding in combine, so this knowledge isn't useful. Such tautologies
9519 should have been detected earlier. Hence we ignore all such cases. */
9520
9521 static enum rtx_code
9522 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9523 {
9524 rtx op0 = *pop0;
9525 rtx op1 = *pop1;
9526 rtx tem, tem1;
9527 int i;
9528 enum machine_mode mode, tmode;
9529
9530 /* Try a few ways of applying the same transformation to both operands. */
9531 while (1)
9532 {
9533 #ifndef WORD_REGISTER_OPERATIONS
9534 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9535 so check specially. */
9536 if (code != GTU && code != GEU && code != LTU && code != LEU
9537 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9538 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9539 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9540 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9541 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9542 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9543 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9544 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9545 && XEXP (op0, 1) == XEXP (op1, 1)
9546 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9547 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9548 && (INTVAL (XEXP (op0, 1))
9549 == (GET_MODE_BITSIZE (GET_MODE (op0))
9550 - (GET_MODE_BITSIZE
9551 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9552 {
9553 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9554 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9555 }
9556 #endif
9557
9558 /* If both operands are the same constant shift, see if we can ignore the
9559 shift. We can if the shift is a rotate or if the bits shifted out of
9560 this shift are known to be zero for both inputs and if the type of
9561 comparison is compatible with the shift. */
9562 if (GET_CODE (op0) == GET_CODE (op1)
9563 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9564 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9565 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9566 && (code != GT && code != LT && code != GE && code != LE))
9567 || (GET_CODE (op0) == ASHIFTRT
9568 && (code != GTU && code != LTU
9569 && code != GEU && code != LEU)))
9570 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9571 && INTVAL (XEXP (op0, 1)) >= 0
9572 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9573 && XEXP (op0, 1) == XEXP (op1, 1))
9574 {
9575 enum machine_mode mode = GET_MODE (op0);
9576 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9577 int shift_count = INTVAL (XEXP (op0, 1));
9578
9579 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9580 mask &= (mask >> shift_count) << shift_count;
9581 else if (GET_CODE (op0) == ASHIFT)
9582 mask = (mask & (mask << shift_count)) >> shift_count;
9583
9584 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9585 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9586 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9587 else
9588 break;
9589 }
9590
9591 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9592 SUBREGs are of the same mode, and, in both cases, the AND would
9593 be redundant if the comparison was done in the narrower mode,
9594 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9595 and the operand's possibly nonzero bits are 0xffffff01; in that case
9596 if we only care about QImode, we don't need the AND). This case
9597 occurs if the output mode of an scc insn is not SImode and
9598 STORE_FLAG_VALUE == 1 (e.g., the 386).
9599
9600 Similarly, check for a case where the AND's are ZERO_EXTEND
9601 operations from some narrower mode even though a SUBREG is not
9602 present. */
9603
9604 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9605 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9606 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9607 {
9608 rtx inner_op0 = XEXP (op0, 0);
9609 rtx inner_op1 = XEXP (op1, 0);
9610 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9611 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9612 int changed = 0;
9613
9614 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9615 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9616 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9617 && (GET_MODE (SUBREG_REG (inner_op0))
9618 == GET_MODE (SUBREG_REG (inner_op1)))
9619 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9620 <= HOST_BITS_PER_WIDE_INT)
9621 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9622 GET_MODE (SUBREG_REG (inner_op0)))))
9623 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9624 GET_MODE (SUBREG_REG (inner_op1))))))
9625 {
9626 op0 = SUBREG_REG (inner_op0);
9627 op1 = SUBREG_REG (inner_op1);
9628
9629 /* The resulting comparison is always unsigned since we masked
9630 off the original sign bit. */
9631 code = unsigned_condition (code);
9632
9633 changed = 1;
9634 }
9635
9636 else if (c0 == c1)
9637 for (tmode = GET_CLASS_NARROWEST_MODE
9638 (GET_MODE_CLASS (GET_MODE (op0)));
9639 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9640 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9641 {
9642 op0 = gen_lowpart (tmode, inner_op0);
9643 op1 = gen_lowpart (tmode, inner_op1);
9644 code = unsigned_condition (code);
9645 changed = 1;
9646 break;
9647 }
9648
9649 if (! changed)
9650 break;
9651 }
9652
9653 /* If both operands are NOT, we can strip off the outer operation
9654 and adjust the comparison code for swapped operands; similarly for
9655 NEG, except that this must be an equality comparison. */
9656 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9657 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9658 && (code == EQ || code == NE)))
9659 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9660
9661 else
9662 break;
9663 }
9664
9665 /* If the first operand is a constant, swap the operands and adjust the
9666 comparison code appropriately, but don't do this if the second operand
9667 is already a constant integer. */
9668 if (swap_commutative_operands_p (op0, op1))
9669 {
9670 tem = op0, op0 = op1, op1 = tem;
9671 code = swap_condition (code);
9672 }
9673
9674 /* We now enter a loop during which we will try to simplify the comparison.
9675 For the most part, we only are concerned with comparisons with zero,
9676 but some things may really be comparisons with zero but not start
9677 out looking that way. */
9678
9679 while (GET_CODE (op1) == CONST_INT)
9680 {
9681 enum machine_mode mode = GET_MODE (op0);
9682 unsigned int mode_width = GET_MODE_BITSIZE (mode);
9683 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9684 int equality_comparison_p;
9685 int sign_bit_comparison_p;
9686 int unsigned_comparison_p;
9687 HOST_WIDE_INT const_op;
9688
9689 /* We only want to handle integral modes. This catches VOIDmode,
9690 CCmode, and the floating-point modes. An exception is that we
9691 can handle VOIDmode if OP0 is a COMPARE or a comparison
9692 operation. */
9693
9694 if (GET_MODE_CLASS (mode) != MODE_INT
9695 && ! (mode == VOIDmode
9696 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
9697 break;
9698
9699 /* Get the constant we are comparing against and turn off all bits
9700 not on in our mode. */
9701 const_op = INTVAL (op1);
9702 if (mode != VOIDmode)
9703 const_op = trunc_int_for_mode (const_op, mode);
9704 op1 = GEN_INT (const_op);
9705
9706 /* If we are comparing against a constant power of two and the value
9707 being compared can only have that single bit nonzero (e.g., it was
9708 `and'ed with that bit), we can replace this with a comparison
9709 with zero. */
9710 if (const_op
9711 && (code == EQ || code == NE || code == GE || code == GEU
9712 || code == LT || code == LTU)
9713 && mode_width <= HOST_BITS_PER_WIDE_INT
9714 && exact_log2 (const_op) >= 0
9715 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9716 {
9717 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9718 op1 = const0_rtx, const_op = 0;
9719 }
9720
9721 /* Similarly, if we are comparing a value known to be either -1 or
9722 0 with -1, change it to the opposite comparison against zero. */
9723
9724 if (const_op == -1
9725 && (code == EQ || code == NE || code == GT || code == LE
9726 || code == GEU || code == LTU)
9727 && num_sign_bit_copies (op0, mode) == mode_width)
9728 {
9729 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9730 op1 = const0_rtx, const_op = 0;
9731 }
9732
9733 /* Do some canonicalizations based on the comparison code. We prefer
9734 comparisons against zero and then prefer equality comparisons.
9735 If we can reduce the size of a constant, we will do that too. */
9736
9737 switch (code)
9738 {
9739 case LT:
9740 /* < C is equivalent to <= (C - 1) */
9741 if (const_op > 0)
9742 {
9743 const_op -= 1;
9744 op1 = GEN_INT (const_op);
9745 code = LE;
9746 /* ... fall through to LE case below. */
9747 }
9748 else
9749 break;
9750
9751 case LE:
9752 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9753 if (const_op < 0)
9754 {
9755 const_op += 1;
9756 op1 = GEN_INT (const_op);
9757 code = LT;
9758 }
9759
9760 /* If we are doing a <= 0 comparison on a value known to have
9761 a zero sign bit, we can replace this with == 0. */
9762 else if (const_op == 0
9763 && mode_width <= HOST_BITS_PER_WIDE_INT
9764 && (nonzero_bits (op0, mode)
9765 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9766 code = EQ;
9767 break;
9768
9769 case GE:
9770 /* >= C is equivalent to > (C - 1). */
9771 if (const_op > 0)
9772 {
9773 const_op -= 1;
9774 op1 = GEN_INT (const_op);
9775 code = GT;
9776 /* ... fall through to GT below. */
9777 }
9778 else
9779 break;
9780
9781 case GT:
9782 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
9783 if (const_op < 0)
9784 {
9785 const_op += 1;
9786 op1 = GEN_INT (const_op);
9787 code = GE;
9788 }
9789
9790 /* If we are doing a > 0 comparison on a value known to have
9791 a zero sign bit, we can replace this with != 0. */
9792 else if (const_op == 0
9793 && mode_width <= HOST_BITS_PER_WIDE_INT
9794 && (nonzero_bits (op0, mode)
9795 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9796 code = NE;
9797 break;
9798
9799 case LTU:
9800 /* < C is equivalent to <= (C - 1). */
9801 if (const_op > 0)
9802 {
9803 const_op -= 1;
9804 op1 = GEN_INT (const_op);
9805 code = LEU;
9806 /* ... fall through ... */
9807 }
9808
9809 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9810 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9811 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9812 {
9813 const_op = 0, op1 = const0_rtx;
9814 code = GE;
9815 break;
9816 }
9817 else
9818 break;
9819
9820 case LEU:
9821 /* unsigned <= 0 is equivalent to == 0 */
9822 if (const_op == 0)
9823 code = EQ;
9824
9825 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9826 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9827 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9828 {
9829 const_op = 0, op1 = const0_rtx;
9830 code = GE;
9831 }
9832 break;
9833
9834 case GEU:
9835 /* >= C is equivalent to > (C - 1). */
9836 if (const_op > 1)
9837 {
9838 const_op -= 1;
9839 op1 = GEN_INT (const_op);
9840 code = GTU;
9841 /* ... fall through ... */
9842 }
9843
9844 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9845 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9846 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9847 {
9848 const_op = 0, op1 = const0_rtx;
9849 code = LT;
9850 break;
9851 }
9852 else
9853 break;
9854
9855 case GTU:
9856 /* unsigned > 0 is equivalent to != 0 */
9857 if (const_op == 0)
9858 code = NE;
9859
9860 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9861 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9862 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9863 {
9864 const_op = 0, op1 = const0_rtx;
9865 code = LT;
9866 }
9867 break;
9868
9869 default:
9870 break;
9871 }
9872
9873 /* Compute some predicates to simplify code below. */
9874
9875 equality_comparison_p = (code == EQ || code == NE);
9876 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9877 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9878 || code == GEU);
9879
9880 /* If this is a sign bit comparison and we can do arithmetic in
9881 MODE, say that we will only be needing the sign bit of OP0. */
9882 if (sign_bit_comparison_p
9883 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9884 op0 = force_to_mode (op0, mode,
9885 ((HOST_WIDE_INT) 1
9886 << (GET_MODE_BITSIZE (mode) - 1)),
9887 0);
9888
9889 /* Now try cases based on the opcode of OP0. If none of the cases
9890 does a "continue", we exit this loop immediately after the
9891 switch. */
9892
9893 switch (GET_CODE (op0))
9894 {
9895 case ZERO_EXTRACT:
9896 /* If we are extracting a single bit from a variable position in
9897 a constant that has only a single bit set and are comparing it
9898 with zero, we can convert this into an equality comparison
9899 between the position and the location of the single bit. */
9900 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
9901 have already reduced the shift count modulo the word size. */
9902 if (!SHIFT_COUNT_TRUNCATED
9903 && GET_CODE (XEXP (op0, 0)) == CONST_INT
9904 && XEXP (op0, 1) == const1_rtx
9905 && equality_comparison_p && const_op == 0
9906 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9907 {
9908 if (BITS_BIG_ENDIAN)
9909 {
9910 enum machine_mode new_mode
9911 = mode_for_extraction (EP_extzv, 1);
9912 if (new_mode == MAX_MACHINE_MODE)
9913 i = BITS_PER_WORD - 1 - i;
9914 else
9915 {
9916 mode = new_mode;
9917 i = (GET_MODE_BITSIZE (mode) - 1 - i);
9918 }
9919 }
9920
9921 op0 = XEXP (op0, 2);
9922 op1 = GEN_INT (i);
9923 const_op = i;
9924
9925 /* Result is nonzero iff shift count is equal to I. */
9926 code = reverse_condition (code);
9927 continue;
9928 }
9929
9930 /* ... fall through ... */
9931
9932 case SIGN_EXTRACT:
9933 tem = expand_compound_operation (op0);
9934 if (tem != op0)
9935 {
9936 op0 = tem;
9937 continue;
9938 }
9939 break;
9940
9941 case NOT:
9942 /* If testing for equality, we can take the NOT of the constant. */
9943 if (equality_comparison_p
9944 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9945 {
9946 op0 = XEXP (op0, 0);
9947 op1 = tem;
9948 continue;
9949 }
9950
9951 /* If just looking at the sign bit, reverse the sense of the
9952 comparison. */
9953 if (sign_bit_comparison_p)
9954 {
9955 op0 = XEXP (op0, 0);
9956 code = (code == GE ? LT : GE);
9957 continue;
9958 }
9959 break;
9960
9961 case NEG:
9962 /* If testing for equality, we can take the NEG of the constant. */
9963 if (equality_comparison_p
9964 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9965 {
9966 op0 = XEXP (op0, 0);
9967 op1 = tem;
9968 continue;
9969 }
9970
9971 /* The remaining cases only apply to comparisons with zero. */
9972 if (const_op != 0)
9973 break;
9974
9975 /* When X is ABS or is known positive,
9976 (neg X) is < 0 if and only if X != 0. */
9977
9978 if (sign_bit_comparison_p
9979 && (GET_CODE (XEXP (op0, 0)) == ABS
9980 || (mode_width <= HOST_BITS_PER_WIDE_INT
9981 && (nonzero_bits (XEXP (op0, 0), mode)
9982 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9983 {
9984 op0 = XEXP (op0, 0);
9985 code = (code == LT ? NE : EQ);
9986 continue;
9987 }
9988
9989 /* If we have NEG of something whose two high-order bits are the
9990 same, we know that "(-a) < 0" is equivalent to "a > 0". */
9991 if (num_sign_bit_copies (op0, mode) >= 2)
9992 {
9993 op0 = XEXP (op0, 0);
9994 code = swap_condition (code);
9995 continue;
9996 }
9997 break;
9998
9999 case ROTATE:
10000 /* If we are testing equality and our count is a constant, we
10001 can perform the inverse operation on our RHS. */
10002 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10003 && (tem = simplify_binary_operation (ROTATERT, mode,
10004 op1, XEXP (op0, 1))) != 0)
10005 {
10006 op0 = XEXP (op0, 0);
10007 op1 = tem;
10008 continue;
10009 }
10010
10011 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10012 a particular bit. Convert it to an AND of a constant of that
10013 bit. This will be converted into a ZERO_EXTRACT. */
10014 if (const_op == 0 && sign_bit_comparison_p
10015 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10016 && mode_width <= HOST_BITS_PER_WIDE_INT)
10017 {
10018 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10019 ((HOST_WIDE_INT) 1
10020 << (mode_width - 1
10021 - INTVAL (XEXP (op0, 1)))));
10022 code = (code == LT ? NE : EQ);
10023 continue;
10024 }
10025
10026 /* Fall through. */
10027
10028 case ABS:
10029 /* ABS is ignorable inside an equality comparison with zero. */
10030 if (const_op == 0 && equality_comparison_p)
10031 {
10032 op0 = XEXP (op0, 0);
10033 continue;
10034 }
10035 break;
10036
10037 case SIGN_EXTEND:
10038 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10039 (compare FOO CONST) if CONST fits in FOO's mode and we
10040 are either testing inequality or have an unsigned
10041 comparison with ZERO_EXTEND or a signed comparison with
10042 SIGN_EXTEND. But don't do it if we don't have a compare
10043 insn of the given mode, since we'd have to revert it
10044 later on, and then we wouldn't know whether to sign- or
10045 zero-extend. */
10046 mode = GET_MODE (XEXP (op0, 0));
10047 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10048 && ! unsigned_comparison_p
10049 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10050 && ((unsigned HOST_WIDE_INT) const_op
10051 < (((unsigned HOST_WIDE_INT) 1
10052 << (GET_MODE_BITSIZE (mode) - 1))))
10053 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10054 {
10055 op0 = XEXP (op0, 0);
10056 continue;
10057 }
10058 break;
10059
10060 case SUBREG:
10061 /* Check for the case where we are comparing A - C1 with C2, that is
10062
10063 (subreg:MODE (plus (A) (-C1))) op (C2)
10064
10065 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10066 comparison in the wider mode. One of the following two conditions
10067 must be true in order for this to be valid:
10068
10069 1. The mode extension results in the same bit pattern being added
10070 on both sides and the comparison is equality or unsigned. As
10071 C2 has been truncated to fit in MODE, the pattern can only be
10072 all 0s or all 1s.
10073
10074 2. The mode extension results in the sign bit being copied on
10075 each side.
10076
10077 The difficulty here is that we have predicates for A but not for
10078 (A - C1) so we need to check that C1 is within proper bounds so
10079 as to perturbate A as little as possible. */
10080
10081 if (mode_width <= HOST_BITS_PER_WIDE_INT
10082 && subreg_lowpart_p (op0)
10083 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10084 && GET_CODE (SUBREG_REG (op0)) == PLUS
10085 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10086 {
10087 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10088 rtx a = XEXP (SUBREG_REG (op0), 0);
10089 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10090
10091 if ((c1 > 0
10092 && (unsigned HOST_WIDE_INT) c1
10093 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10094 && (equality_comparison_p || unsigned_comparison_p)
10095 /* (A - C1) zero-extends if it is positive and sign-extends
10096 if it is negative, C2 both zero- and sign-extends. */
10097 && ((0 == (nonzero_bits (a, inner_mode)
10098 & ~GET_MODE_MASK (mode))
10099 && const_op >= 0)
10100 /* (A - C1) sign-extends if it is positive and 1-extends
10101 if it is negative, C2 both sign- and 1-extends. */
10102 || (num_sign_bit_copies (a, inner_mode)
10103 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10104 - mode_width)
10105 && const_op < 0)))
10106 || ((unsigned HOST_WIDE_INT) c1
10107 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10108 /* (A - C1) always sign-extends, like C2. */
10109 && num_sign_bit_copies (a, inner_mode)
10110 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10111 - (mode_width - 1))))
10112 {
10113 op0 = SUBREG_REG (op0);
10114 continue;
10115 }
10116 }
10117
10118 /* If the inner mode is narrower and we are extracting the low part,
10119 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10120 if (subreg_lowpart_p (op0)
10121 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10122 /* Fall through */ ;
10123 else
10124 break;
10125
10126 /* ... fall through ... */
10127
10128 case ZERO_EXTEND:
10129 mode = GET_MODE (XEXP (op0, 0));
10130 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10131 && (unsigned_comparison_p || equality_comparison_p)
10132 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10133 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10134 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10135 {
10136 op0 = XEXP (op0, 0);
10137 continue;
10138 }
10139 break;
10140
10141 case PLUS:
10142 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10143 this for equality comparisons due to pathological cases involving
10144 overflows. */
10145 if (equality_comparison_p
10146 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10147 op1, XEXP (op0, 1))))
10148 {
10149 op0 = XEXP (op0, 0);
10150 op1 = tem;
10151 continue;
10152 }
10153
10154 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10155 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10156 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10157 {
10158 op0 = XEXP (XEXP (op0, 0), 0);
10159 code = (code == LT ? EQ : NE);
10160 continue;
10161 }
10162 break;
10163
10164 case MINUS:
10165 /* We used to optimize signed comparisons against zero, but that
10166 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10167 arrive here as equality comparisons, or (GEU, LTU) are
10168 optimized away. No need to special-case them. */
10169
10170 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10171 (eq B (minus A C)), whichever simplifies. We can only do
10172 this for equality comparisons due to pathological cases involving
10173 overflows. */
10174 if (equality_comparison_p
10175 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10176 XEXP (op0, 1), op1)))
10177 {
10178 op0 = XEXP (op0, 0);
10179 op1 = tem;
10180 continue;
10181 }
10182
10183 if (equality_comparison_p
10184 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10185 XEXP (op0, 0), op1)))
10186 {
10187 op0 = XEXP (op0, 1);
10188 op1 = tem;
10189 continue;
10190 }
10191
10192 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10193 of bits in X minus 1, is one iff X > 0. */
10194 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10195 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10196 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10197 == mode_width - 1
10198 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10199 {
10200 op0 = XEXP (op0, 1);
10201 code = (code == GE ? LE : GT);
10202 continue;
10203 }
10204 break;
10205
10206 case XOR:
10207 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10208 if C is zero or B is a constant. */
10209 if (equality_comparison_p
10210 && 0 != (tem = simplify_binary_operation (XOR, mode,
10211 XEXP (op0, 1), op1)))
10212 {
10213 op0 = XEXP (op0, 0);
10214 op1 = tem;
10215 continue;
10216 }
10217 break;
10218
10219 case EQ: case NE:
10220 case UNEQ: case LTGT:
10221 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10222 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10223 case UNORDERED: case ORDERED:
10224 /* We can't do anything if OP0 is a condition code value, rather
10225 than an actual data value. */
10226 if (const_op != 0
10227 || CC0_P (XEXP (op0, 0))
10228 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10229 break;
10230
10231 /* Get the two operands being compared. */
10232 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10233 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10234 else
10235 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10236
10237 /* Check for the cases where we simply want the result of the
10238 earlier test or the opposite of that result. */
10239 if (code == NE || code == EQ
10240 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10241 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10242 && (STORE_FLAG_VALUE
10243 & (((HOST_WIDE_INT) 1
10244 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10245 && (code == LT || code == GE)))
10246 {
10247 enum rtx_code new_code;
10248 if (code == LT || code == NE)
10249 new_code = GET_CODE (op0);
10250 else
10251 new_code = reversed_comparison_code (op0, NULL);
10252
10253 if (new_code != UNKNOWN)
10254 {
10255 code = new_code;
10256 op0 = tem;
10257 op1 = tem1;
10258 continue;
10259 }
10260 }
10261 break;
10262
10263 case IOR:
10264 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10265 iff X <= 0. */
10266 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10267 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10268 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10269 {
10270 op0 = XEXP (op0, 1);
10271 code = (code == GE ? GT : LE);
10272 continue;
10273 }
10274 break;
10275
10276 case AND:
10277 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10278 will be converted to a ZERO_EXTRACT later. */
10279 if (const_op == 0 && equality_comparison_p
10280 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10281 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10282 {
10283 op0 = simplify_and_const_int
10284 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10285 XEXP (op0, 1),
10286 XEXP (XEXP (op0, 0), 1)),
10287 (HOST_WIDE_INT) 1);
10288 continue;
10289 }
10290
10291 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10292 zero and X is a comparison and C1 and C2 describe only bits set
10293 in STORE_FLAG_VALUE, we can compare with X. */
10294 if (const_op == 0 && equality_comparison_p
10295 && mode_width <= HOST_BITS_PER_WIDE_INT
10296 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10297 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10298 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10299 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10300 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10301 {
10302 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10303 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10304 if ((~STORE_FLAG_VALUE & mask) == 0
10305 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10306 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10307 && COMPARISON_P (tem))))
10308 {
10309 op0 = XEXP (XEXP (op0, 0), 0);
10310 continue;
10311 }
10312 }
10313
10314 /* If we are doing an equality comparison of an AND of a bit equal
10315 to the sign bit, replace this with a LT or GE comparison of
10316 the underlying value. */
10317 if (equality_comparison_p
10318 && const_op == 0
10319 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10320 && mode_width <= HOST_BITS_PER_WIDE_INT
10321 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10322 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10323 {
10324 op0 = XEXP (op0, 0);
10325 code = (code == EQ ? GE : LT);
10326 continue;
10327 }
10328
10329 /* If this AND operation is really a ZERO_EXTEND from a narrower
10330 mode, the constant fits within that mode, and this is either an
10331 equality or unsigned comparison, try to do this comparison in
10332 the narrower mode.
10333
10334 Note that in:
10335
10336 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10337 -> (ne:DI (reg:SI 4) (const_int 0))
10338
10339 unless TRULY_NOOP_TRUNCATION allows it or the register is
10340 known to hold a value of the required mode the
10341 transformation is invalid. */
10342 if ((equality_comparison_p || unsigned_comparison_p)
10343 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10344 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10345 & GET_MODE_MASK (mode))
10346 + 1)) >= 0
10347 && const_op >> i == 0
10348 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10349 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10350 GET_MODE_BITSIZE (GET_MODE (op0)))
10351 || (REG_P (XEXP (op0, 0))
10352 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10353 {
10354 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10355 continue;
10356 }
10357
10358 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10359 fits in both M1 and M2 and the SUBREG is either paradoxical
10360 or represents the low part, permute the SUBREG and the AND
10361 and try again. */
10362 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10363 {
10364 unsigned HOST_WIDE_INT c1;
10365 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10366 /* Require an integral mode, to avoid creating something like
10367 (AND:SF ...). */
10368 if (SCALAR_INT_MODE_P (tmode)
10369 /* It is unsafe to commute the AND into the SUBREG if the
10370 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10371 not defined. As originally written the upper bits
10372 have a defined value due to the AND operation.
10373 However, if we commute the AND inside the SUBREG then
10374 they no longer have defined values and the meaning of
10375 the code has been changed. */
10376 && (0
10377 #ifdef WORD_REGISTER_OPERATIONS
10378 || (mode_width > GET_MODE_BITSIZE (tmode)
10379 && mode_width <= BITS_PER_WORD)
10380 #endif
10381 || (mode_width <= GET_MODE_BITSIZE (tmode)
10382 && subreg_lowpart_p (XEXP (op0, 0))))
10383 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10384 && mode_width <= HOST_BITS_PER_WIDE_INT
10385 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10386 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10387 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10388 && c1 != mask
10389 && c1 != GET_MODE_MASK (tmode))
10390 {
10391 op0 = simplify_gen_binary (AND, tmode,
10392 SUBREG_REG (XEXP (op0, 0)),
10393 gen_int_mode (c1, tmode));
10394 op0 = gen_lowpart (mode, op0);
10395 continue;
10396 }
10397 }
10398
10399 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10400 if (const_op == 0 && equality_comparison_p
10401 && XEXP (op0, 1) == const1_rtx
10402 && GET_CODE (XEXP (op0, 0)) == NOT)
10403 {
10404 op0 = simplify_and_const_int
10405 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10406 code = (code == NE ? EQ : NE);
10407 continue;
10408 }
10409
10410 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10411 (eq (and (lshiftrt X) 1) 0).
10412 Also handle the case where (not X) is expressed using xor. */
10413 if (const_op == 0 && equality_comparison_p
10414 && XEXP (op0, 1) == const1_rtx
10415 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10416 {
10417 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10418 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10419
10420 if (GET_CODE (shift_op) == NOT
10421 || (GET_CODE (shift_op) == XOR
10422 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10423 && GET_CODE (shift_count) == CONST_INT
10424 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10425 && (INTVAL (XEXP (shift_op, 1))
10426 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10427 {
10428 op0 = simplify_and_const_int
10429 (NULL_RTX, mode,
10430 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10431 (HOST_WIDE_INT) 1);
10432 code = (code == NE ? EQ : NE);
10433 continue;
10434 }
10435 }
10436 break;
10437
10438 case ASHIFT:
10439 /* If we have (compare (ashift FOO N) (const_int C)) and
10440 the high order N bits of FOO (N+1 if an inequality comparison)
10441 are known to be zero, we can do this by comparing FOO with C
10442 shifted right N bits so long as the low-order N bits of C are
10443 zero. */
10444 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10445 && INTVAL (XEXP (op0, 1)) >= 0
10446 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10447 < HOST_BITS_PER_WIDE_INT)
10448 && ((const_op
10449 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10450 && mode_width <= HOST_BITS_PER_WIDE_INT
10451 && (nonzero_bits (XEXP (op0, 0), mode)
10452 & ~(mask >> (INTVAL (XEXP (op0, 1))
10453 + ! equality_comparison_p))) == 0)
10454 {
10455 /* We must perform a logical shift, not an arithmetic one,
10456 as we want the top N bits of C to be zero. */
10457 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10458
10459 temp >>= INTVAL (XEXP (op0, 1));
10460 op1 = gen_int_mode (temp, mode);
10461 op0 = XEXP (op0, 0);
10462 continue;
10463 }
10464
10465 /* If we are doing a sign bit comparison, it means we are testing
10466 a particular bit. Convert it to the appropriate AND. */
10467 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10468 && mode_width <= HOST_BITS_PER_WIDE_INT)
10469 {
10470 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10471 ((HOST_WIDE_INT) 1
10472 << (mode_width - 1
10473 - INTVAL (XEXP (op0, 1)))));
10474 code = (code == LT ? NE : EQ);
10475 continue;
10476 }
10477
10478 /* If this an equality comparison with zero and we are shifting
10479 the low bit to the sign bit, we can convert this to an AND of the
10480 low-order bit. */
10481 if (const_op == 0 && equality_comparison_p
10482 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10483 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10484 == mode_width - 1)
10485 {
10486 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10487 (HOST_WIDE_INT) 1);
10488 continue;
10489 }
10490 break;
10491
10492 case ASHIFTRT:
10493 /* If this is an equality comparison with zero, we can do this
10494 as a logical shift, which might be much simpler. */
10495 if (equality_comparison_p && const_op == 0
10496 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10497 {
10498 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10499 XEXP (op0, 0),
10500 INTVAL (XEXP (op0, 1)));
10501 continue;
10502 }
10503
10504 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10505 do the comparison in a narrower mode. */
10506 if (! unsigned_comparison_p
10507 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10508 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10509 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10510 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10511 MODE_INT, 1)) != BLKmode
10512 && (((unsigned HOST_WIDE_INT) const_op
10513 + (GET_MODE_MASK (tmode) >> 1) + 1)
10514 <= GET_MODE_MASK (tmode)))
10515 {
10516 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10517 continue;
10518 }
10519
10520 /* Likewise if OP0 is a PLUS of a sign extension with a
10521 constant, which is usually represented with the PLUS
10522 between the shifts. */
10523 if (! unsigned_comparison_p
10524 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10525 && GET_CODE (XEXP (op0, 0)) == PLUS
10526 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10527 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10528 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10529 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10530 MODE_INT, 1)) != BLKmode
10531 && (((unsigned HOST_WIDE_INT) const_op
10532 + (GET_MODE_MASK (tmode) >> 1) + 1)
10533 <= GET_MODE_MASK (tmode)))
10534 {
10535 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10536 rtx add_const = XEXP (XEXP (op0, 0), 1);
10537 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10538 add_const, XEXP (op0, 1));
10539
10540 op0 = simplify_gen_binary (PLUS, tmode,
10541 gen_lowpart (tmode, inner),
10542 new_const);
10543 continue;
10544 }
10545
10546 /* ... fall through ... */
10547 case LSHIFTRT:
10548 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10549 the low order N bits of FOO are known to be zero, we can do this
10550 by comparing FOO with C shifted left N bits so long as no
10551 overflow occurs. */
10552 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10553 && INTVAL (XEXP (op0, 1)) >= 0
10554 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10555 && mode_width <= HOST_BITS_PER_WIDE_INT
10556 && (nonzero_bits (XEXP (op0, 0), mode)
10557 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10558 && (((unsigned HOST_WIDE_INT) const_op
10559 + (GET_CODE (op0) != LSHIFTRT
10560 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10561 + 1)
10562 : 0))
10563 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10564 {
10565 /* If the shift was logical, then we must make the condition
10566 unsigned. */
10567 if (GET_CODE (op0) == LSHIFTRT)
10568 code = unsigned_condition (code);
10569
10570 const_op <<= INTVAL (XEXP (op0, 1));
10571 op1 = GEN_INT (const_op);
10572 op0 = XEXP (op0, 0);
10573 continue;
10574 }
10575
10576 /* If we are using this shift to extract just the sign bit, we
10577 can replace this with an LT or GE comparison. */
10578 if (const_op == 0
10579 && (equality_comparison_p || sign_bit_comparison_p)
10580 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10581 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10582 == mode_width - 1)
10583 {
10584 op0 = XEXP (op0, 0);
10585 code = (code == NE || code == GT ? LT : GE);
10586 continue;
10587 }
10588 break;
10589
10590 default:
10591 break;
10592 }
10593
10594 break;
10595 }
10596
10597 /* Now make any compound operations involved in this comparison. Then,
10598 check for an outmost SUBREG on OP0 that is not doing anything or is
10599 paradoxical. The latter transformation must only be performed when
10600 it is known that the "extra" bits will be the same in op0 and op1 or
10601 that they don't matter. There are three cases to consider:
10602
10603 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10604 care bits and we can assume they have any convenient value. So
10605 making the transformation is safe.
10606
10607 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10608 In this case the upper bits of op0 are undefined. We should not make
10609 the simplification in that case as we do not know the contents of
10610 those bits.
10611
10612 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10613 UNKNOWN. In that case we know those bits are zeros or ones. We must
10614 also be sure that they are the same as the upper bits of op1.
10615
10616 We can never remove a SUBREG for a non-equality comparison because
10617 the sign bit is in a different place in the underlying object. */
10618
10619 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10620 op1 = make_compound_operation (op1, SET);
10621
10622 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10623 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10624 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10625 && (code == NE || code == EQ))
10626 {
10627 if (GET_MODE_SIZE (GET_MODE (op0))
10628 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10629 {
10630 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10631 implemented. */
10632 if (REG_P (SUBREG_REG (op0)))
10633 {
10634 op0 = SUBREG_REG (op0);
10635 op1 = gen_lowpart (GET_MODE (op0), op1);
10636 }
10637 }
10638 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10639 <= HOST_BITS_PER_WIDE_INT)
10640 && (nonzero_bits (SUBREG_REG (op0),
10641 GET_MODE (SUBREG_REG (op0)))
10642 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10643 {
10644 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
10645
10646 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10647 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
10648 op0 = SUBREG_REG (op0), op1 = tem;
10649 }
10650 }
10651
10652 /* We now do the opposite procedure: Some machines don't have compare
10653 insns in all modes. If OP0's mode is an integer mode smaller than a
10654 word and we can't do a compare in that mode, see if there is a larger
10655 mode for which we can do the compare. There are a number of cases in
10656 which we can use the wider mode. */
10657
10658 mode = GET_MODE (op0);
10659 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10660 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10661 && ! have_insn_for (COMPARE, mode))
10662 for (tmode = GET_MODE_WIDER_MODE (mode);
10663 (tmode != VOIDmode
10664 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10665 tmode = GET_MODE_WIDER_MODE (tmode))
10666 if (have_insn_for (COMPARE, tmode))
10667 {
10668 int zero_extended;
10669
10670 /* If the only nonzero bits in OP0 and OP1 are those in the
10671 narrower mode and this is an equality or unsigned comparison,
10672 we can use the wider mode. Similarly for sign-extended
10673 values, in which case it is true for all comparisons. */
10674 zero_extended = ((code == EQ || code == NE
10675 || code == GEU || code == GTU
10676 || code == LEU || code == LTU)
10677 && (nonzero_bits (op0, tmode)
10678 & ~GET_MODE_MASK (mode)) == 0
10679 && ((GET_CODE (op1) == CONST_INT
10680 || (nonzero_bits (op1, tmode)
10681 & ~GET_MODE_MASK (mode)) == 0)));
10682
10683 if (zero_extended
10684 || ((num_sign_bit_copies (op0, tmode)
10685 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10686 - GET_MODE_BITSIZE (mode)))
10687 && (num_sign_bit_copies (op1, tmode)
10688 > (unsigned int) (GET_MODE_BITSIZE (tmode)
10689 - GET_MODE_BITSIZE (mode)))))
10690 {
10691 /* If OP0 is an AND and we don't have an AND in MODE either,
10692 make a new AND in the proper mode. */
10693 if (GET_CODE (op0) == AND
10694 && !have_insn_for (AND, mode))
10695 op0 = simplify_gen_binary (AND, tmode,
10696 gen_lowpart (tmode,
10697 XEXP (op0, 0)),
10698 gen_lowpart (tmode,
10699 XEXP (op0, 1)));
10700
10701 op0 = gen_lowpart (tmode, op0);
10702 if (zero_extended && GET_CODE (op1) == CONST_INT)
10703 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
10704 op1 = gen_lowpart (tmode, op1);
10705 break;
10706 }
10707
10708 /* If this is a test for negative, we can make an explicit
10709 test of the sign bit. */
10710
10711 if (op1 == const0_rtx && (code == LT || code == GE)
10712 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10713 {
10714 op0 = simplify_gen_binary (AND, tmode,
10715 gen_lowpart (tmode, op0),
10716 GEN_INT ((HOST_WIDE_INT) 1
10717 << (GET_MODE_BITSIZE (mode)
10718 - 1)));
10719 code = (code == LT) ? NE : EQ;
10720 break;
10721 }
10722 }
10723
10724 #ifdef CANONICALIZE_COMPARISON
10725 /* If this machine only supports a subset of valid comparisons, see if we
10726 can convert an unsupported one into a supported one. */
10727 CANONICALIZE_COMPARISON (code, op0, op1);
10728 #endif
10729
10730 *pop0 = op0;
10731 *pop1 = op1;
10732
10733 return code;
10734 }
10735 \f
10736 /* Utility function for record_value_for_reg. Count number of
10737 rtxs in X. */
10738 static int
10739 count_rtxs (rtx x)
10740 {
10741 enum rtx_code code = GET_CODE (x);
10742 const char *fmt;
10743 int i, ret = 1;
10744
10745 if (GET_RTX_CLASS (code) == '2'
10746 || GET_RTX_CLASS (code) == 'c')
10747 {
10748 rtx x0 = XEXP (x, 0);
10749 rtx x1 = XEXP (x, 1);
10750
10751 if (x0 == x1)
10752 return 1 + 2 * count_rtxs (x0);
10753
10754 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
10755 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
10756 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10757 return 2 + 2 * count_rtxs (x0)
10758 + count_rtxs (x == XEXP (x1, 0)
10759 ? XEXP (x1, 1) : XEXP (x1, 0));
10760
10761 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
10762 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
10763 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10764 return 2 + 2 * count_rtxs (x1)
10765 + count_rtxs (x == XEXP (x0, 0)
10766 ? XEXP (x0, 1) : XEXP (x0, 0));
10767 }
10768
10769 fmt = GET_RTX_FORMAT (code);
10770 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10771 if (fmt[i] == 'e')
10772 ret += count_rtxs (XEXP (x, i));
10773
10774 return ret;
10775 }
10776 \f
10777 /* Utility function for following routine. Called when X is part of a value
10778 being stored into last_set_value. Sets last_set_table_tick
10779 for each register mentioned. Similar to mention_regs in cse.c */
10780
10781 static void
10782 update_table_tick (rtx x)
10783 {
10784 enum rtx_code code = GET_CODE (x);
10785 const char *fmt = GET_RTX_FORMAT (code);
10786 int i;
10787
10788 if (code == REG)
10789 {
10790 unsigned int regno = REGNO (x);
10791 unsigned int endregno
10792 = regno + (regno < FIRST_PSEUDO_REGISTER
10793 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
10794 unsigned int r;
10795
10796 for (r = regno; r < endregno; r++)
10797 reg_stat[r].last_set_table_tick = label_tick;
10798
10799 return;
10800 }
10801
10802 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10803 /* Note that we can't have an "E" in values stored; see
10804 get_last_value_validate. */
10805 if (fmt[i] == 'e')
10806 {
10807 /* Check for identical subexpressions. If x contains
10808 identical subexpression we only have to traverse one of
10809 them. */
10810 if (i == 0 && ARITHMETIC_P (x))
10811 {
10812 /* Note that at this point x1 has already been
10813 processed. */
10814 rtx x0 = XEXP (x, 0);
10815 rtx x1 = XEXP (x, 1);
10816
10817 /* If x0 and x1 are identical then there is no need to
10818 process x0. */
10819 if (x0 == x1)
10820 break;
10821
10822 /* If x0 is identical to a subexpression of x1 then while
10823 processing x1, x0 has already been processed. Thus we
10824 are done with x. */
10825 if (ARITHMETIC_P (x1)
10826 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
10827 break;
10828
10829 /* If x1 is identical to a subexpression of x0 then we
10830 still have to process the rest of x0. */
10831 if (ARITHMETIC_P (x0)
10832 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
10833 {
10834 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
10835 break;
10836 }
10837 }
10838
10839 update_table_tick (XEXP (x, i));
10840 }
10841 }
10842
10843 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10844 are saying that the register is clobbered and we no longer know its
10845 value. If INSN is zero, don't update reg_stat[].last_set; this is
10846 only permitted with VALUE also zero and is used to invalidate the
10847 register. */
10848
10849 static void
10850 record_value_for_reg (rtx reg, rtx insn, rtx value)
10851 {
10852 unsigned int regno = REGNO (reg);
10853 unsigned int endregno
10854 = regno + (regno < FIRST_PSEUDO_REGISTER
10855 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
10856 unsigned int i;
10857
10858 /* If VALUE contains REG and we have a previous value for REG, substitute
10859 the previous value. */
10860 if (value && insn && reg_overlap_mentioned_p (reg, value))
10861 {
10862 rtx tem;
10863
10864 /* Set things up so get_last_value is allowed to see anything set up to
10865 our insn. */
10866 subst_low_cuid = INSN_CUID (insn);
10867 tem = get_last_value (reg);
10868
10869 /* If TEM is simply a binary operation with two CLOBBERs as operands,
10870 it isn't going to be useful and will take a lot of time to process,
10871 so just use the CLOBBER. */
10872
10873 if (tem)
10874 {
10875 if (ARITHMETIC_P (tem)
10876 && GET_CODE (XEXP (tem, 0)) == CLOBBER
10877 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10878 tem = XEXP (tem, 0);
10879 else if (count_occurrences (value, reg, 1) >= 2)
10880 {
10881 /* If there are two or more occurrences of REG in VALUE,
10882 prevent the value from growing too much. */
10883 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
10884 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
10885 }
10886
10887 value = replace_rtx (copy_rtx (value), reg, tem);
10888 }
10889 }
10890
10891 /* For each register modified, show we don't know its value, that
10892 we don't know about its bitwise content, that its value has been
10893 updated, and that we don't know the location of the death of the
10894 register. */
10895 for (i = regno; i < endregno; i++)
10896 {
10897 if (insn)
10898 reg_stat[i].last_set = insn;
10899
10900 reg_stat[i].last_set_value = 0;
10901 reg_stat[i].last_set_mode = 0;
10902 reg_stat[i].last_set_nonzero_bits = 0;
10903 reg_stat[i].last_set_sign_bit_copies = 0;
10904 reg_stat[i].last_death = 0;
10905 reg_stat[i].truncated_to_mode = 0;
10906 }
10907
10908 /* Mark registers that are being referenced in this value. */
10909 if (value)
10910 update_table_tick (value);
10911
10912 /* Now update the status of each register being set.
10913 If someone is using this register in this block, set this register
10914 to invalid since we will get confused between the two lives in this
10915 basic block. This makes using this register always invalid. In cse, we
10916 scan the table to invalidate all entries using this register, but this
10917 is too much work for us. */
10918
10919 for (i = regno; i < endregno; i++)
10920 {
10921 reg_stat[i].last_set_label = label_tick;
10922 if (!insn || (value && reg_stat[i].last_set_table_tick == label_tick))
10923 reg_stat[i].last_set_invalid = 1;
10924 else
10925 reg_stat[i].last_set_invalid = 0;
10926 }
10927
10928 /* The value being assigned might refer to X (like in "x++;"). In that
10929 case, we must replace it with (clobber (const_int 0)) to prevent
10930 infinite loops. */
10931 if (value && ! get_last_value_validate (&value, insn,
10932 reg_stat[regno].last_set_label, 0))
10933 {
10934 value = copy_rtx (value);
10935 if (! get_last_value_validate (&value, insn,
10936 reg_stat[regno].last_set_label, 1))
10937 value = 0;
10938 }
10939
10940 /* For the main register being modified, update the value, the mode, the
10941 nonzero bits, and the number of sign bit copies. */
10942
10943 reg_stat[regno].last_set_value = value;
10944
10945 if (value)
10946 {
10947 enum machine_mode mode = GET_MODE (reg);
10948 subst_low_cuid = INSN_CUID (insn);
10949 reg_stat[regno].last_set_mode = mode;
10950 if (GET_MODE_CLASS (mode) == MODE_INT
10951 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10952 mode = nonzero_bits_mode;
10953 reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
10954 reg_stat[regno].last_set_sign_bit_copies
10955 = num_sign_bit_copies (value, GET_MODE (reg));
10956 }
10957 }
10958
10959 /* Called via note_stores from record_dead_and_set_regs to handle one
10960 SET or CLOBBER in an insn. DATA is the instruction in which the
10961 set is occurring. */
10962
10963 static void
10964 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
10965 {
10966 rtx record_dead_insn = (rtx) data;
10967
10968 if (GET_CODE (dest) == SUBREG)
10969 dest = SUBREG_REG (dest);
10970
10971 if (!record_dead_insn)
10972 {
10973 if (REG_P (dest))
10974 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
10975 return;
10976 }
10977
10978 if (REG_P (dest))
10979 {
10980 /* If we are setting the whole register, we know its value. Otherwise
10981 show that we don't know the value. We can handle SUBREG in
10982 some cases. */
10983 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10984 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10985 else if (GET_CODE (setter) == SET
10986 && GET_CODE (SET_DEST (setter)) == SUBREG
10987 && SUBREG_REG (SET_DEST (setter)) == dest
10988 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10989 && subreg_lowpart_p (SET_DEST (setter)))
10990 record_value_for_reg (dest, record_dead_insn,
10991 gen_lowpart (GET_MODE (dest),
10992 SET_SRC (setter)));
10993 else
10994 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10995 }
10996 else if (MEM_P (dest)
10997 /* Ignore pushes, they clobber nothing. */
10998 && ! push_operand (dest, GET_MODE (dest)))
10999 mem_last_set = INSN_CUID (record_dead_insn);
11000 }
11001
11002 /* Update the records of when each REG was most recently set or killed
11003 for the things done by INSN. This is the last thing done in processing
11004 INSN in the combiner loop.
11005
11006 We update reg_stat[], in particular fields last_set, last_set_value,
11007 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11008 last_death, and also the similar information mem_last_set (which insn
11009 most recently modified memory) and last_call_cuid (which insn was the
11010 most recent subroutine call). */
11011
11012 static void
11013 record_dead_and_set_regs (rtx insn)
11014 {
11015 rtx link;
11016 unsigned int i;
11017
11018 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11019 {
11020 if (REG_NOTE_KIND (link) == REG_DEAD
11021 && REG_P (XEXP (link, 0)))
11022 {
11023 unsigned int regno = REGNO (XEXP (link, 0));
11024 unsigned int endregno
11025 = regno + (regno < FIRST_PSEUDO_REGISTER
11026 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11027 : 1);
11028
11029 for (i = regno; i < endregno; i++)
11030 reg_stat[i].last_death = insn;
11031 }
11032 else if (REG_NOTE_KIND (link) == REG_INC)
11033 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11034 }
11035
11036 if (CALL_P (insn))
11037 {
11038 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11039 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11040 {
11041 reg_stat[i].last_set_value = 0;
11042 reg_stat[i].last_set_mode = 0;
11043 reg_stat[i].last_set_nonzero_bits = 0;
11044 reg_stat[i].last_set_sign_bit_copies = 0;
11045 reg_stat[i].last_death = 0;
11046 reg_stat[i].truncated_to_mode = 0;
11047 }
11048
11049 last_call_cuid = mem_last_set = INSN_CUID (insn);
11050
11051 /* We can't combine into a call pattern. Remember, though, that
11052 the return value register is set at this CUID. We could
11053 still replace a register with the return value from the
11054 wrong subroutine call! */
11055 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11056 }
11057 else
11058 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11059 }
11060
11061 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11062 register present in the SUBREG, so for each such SUBREG go back and
11063 adjust nonzero and sign bit information of the registers that are
11064 known to have some zero/sign bits set.
11065
11066 This is needed because when combine blows the SUBREGs away, the
11067 information on zero/sign bits is lost and further combines can be
11068 missed because of that. */
11069
11070 static void
11071 record_promoted_value (rtx insn, rtx subreg)
11072 {
11073 rtx links, set;
11074 unsigned int regno = REGNO (SUBREG_REG (subreg));
11075 enum machine_mode mode = GET_MODE (subreg);
11076
11077 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11078 return;
11079
11080 for (links = LOG_LINKS (insn); links;)
11081 {
11082 insn = XEXP (links, 0);
11083 set = single_set (insn);
11084
11085 if (! set || !REG_P (SET_DEST (set))
11086 || REGNO (SET_DEST (set)) != regno
11087 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11088 {
11089 links = XEXP (links, 1);
11090 continue;
11091 }
11092
11093 if (reg_stat[regno].last_set == insn)
11094 {
11095 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11096 reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11097 }
11098
11099 if (REG_P (SET_SRC (set)))
11100 {
11101 regno = REGNO (SET_SRC (set));
11102 links = LOG_LINKS (insn);
11103 }
11104 else
11105 break;
11106 }
11107 }
11108
11109 /* Check if X, a register, is known to contain a value already
11110 truncated to MODE. In this case we can use a subreg to refer to
11111 the truncated value even though in the generic case we would need
11112 an explicit truncation. */
11113
11114 static bool
11115 reg_truncated_to_mode (enum machine_mode mode, rtx x)
11116 {
11117 enum machine_mode truncated = reg_stat[REGNO (x)].truncated_to_mode;
11118
11119 if (truncated == 0 || reg_stat[REGNO (x)].truncation_label != label_tick)
11120 return false;
11121 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11122 return true;
11123 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11124 GET_MODE_BITSIZE (truncated)))
11125 return true;
11126 return false;
11127 }
11128
11129 /* X is a REG or a SUBREG. If X is some sort of a truncation record
11130 it. For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11131 a truncate into a subreg using this information. */
11132
11133 static void
11134 record_truncated_value (rtx x)
11135 {
11136 enum machine_mode truncated_mode;
11137
11138 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11139 {
11140 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11141 truncated_mode = GET_MODE (x);
11142
11143 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11144 return;
11145
11146 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11147 GET_MODE_BITSIZE (original_mode)))
11148 return;
11149
11150 x = SUBREG_REG (x);
11151 }
11152 /* ??? For hard-regs we now record everything. We might be able to
11153 optimize this using last_set_mode. */
11154 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11155 truncated_mode = GET_MODE (x);
11156 else
11157 return;
11158
11159 if (reg_stat[REGNO (x)].truncated_to_mode == 0
11160 || reg_stat[REGNO (x)].truncation_label < label_tick
11161 || (GET_MODE_SIZE (truncated_mode)
11162 < GET_MODE_SIZE (reg_stat[REGNO (x)].truncated_to_mode)))
11163 {
11164 reg_stat[REGNO (x)].truncated_to_mode = truncated_mode;
11165 reg_stat[REGNO (x)].truncation_label = label_tick;
11166 }
11167 }
11168
11169 /* Scan X for promoted SUBREGs and truncated REGs. For each one
11170 found, note what it implies to the registers used in it. */
11171
11172 static void
11173 check_conversions (rtx insn, rtx x)
11174 {
11175 if (GET_CODE (x) == SUBREG || REG_P (x))
11176 {
11177 if (GET_CODE (x) == SUBREG
11178 && SUBREG_PROMOTED_VAR_P (x)
11179 && REG_P (SUBREG_REG (x)))
11180 record_promoted_value (insn, x);
11181
11182 record_truncated_value (x);
11183 }
11184 else
11185 {
11186 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11187 int i, j;
11188
11189 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11190 switch (format[i])
11191 {
11192 case 'e':
11193 check_conversions (insn, XEXP (x, i));
11194 break;
11195 case 'V':
11196 case 'E':
11197 if (XVEC (x, i) != 0)
11198 for (j = 0; j < XVECLEN (x, i); j++)
11199 check_conversions (insn, XVECEXP (x, i, j));
11200 break;
11201 }
11202 }
11203 }
11204 \f
11205 /* Utility routine for the following function. Verify that all the registers
11206 mentioned in *LOC are valid when *LOC was part of a value set when
11207 label_tick == TICK. Return 0 if some are not.
11208
11209 If REPLACE is nonzero, replace the invalid reference with
11210 (clobber (const_int 0)) and return 1. This replacement is useful because
11211 we often can get useful information about the form of a value (e.g., if
11212 it was produced by a shift that always produces -1 or 0) even though
11213 we don't know exactly what registers it was produced from. */
11214
11215 static int
11216 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11217 {
11218 rtx x = *loc;
11219 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11220 int len = GET_RTX_LENGTH (GET_CODE (x));
11221 int i;
11222
11223 if (REG_P (x))
11224 {
11225 unsigned int regno = REGNO (x);
11226 unsigned int endregno
11227 = regno + (regno < FIRST_PSEUDO_REGISTER
11228 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11229 unsigned int j;
11230
11231 for (j = regno; j < endregno; j++)
11232 if (reg_stat[j].last_set_invalid
11233 /* If this is a pseudo-register that was only set once and not
11234 live at the beginning of the function, it is always valid. */
11235 || (! (regno >= FIRST_PSEUDO_REGISTER
11236 && REG_N_SETS (regno) == 1
11237 && (! REGNO_REG_SET_P
11238 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11239 regno)))
11240 && reg_stat[j].last_set_label > tick))
11241 {
11242 if (replace)
11243 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11244 return replace;
11245 }
11246
11247 return 1;
11248 }
11249 /* If this is a memory reference, make sure that there were
11250 no stores after it that might have clobbered the value. We don't
11251 have alias info, so we assume any store invalidates it. */
11252 else if (MEM_P (x) && !MEM_READONLY_P (x)
11253 && INSN_CUID (insn) <= mem_last_set)
11254 {
11255 if (replace)
11256 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11257 return replace;
11258 }
11259
11260 for (i = 0; i < len; i++)
11261 {
11262 if (fmt[i] == 'e')
11263 {
11264 /* Check for identical subexpressions. If x contains
11265 identical subexpression we only have to traverse one of
11266 them. */
11267 if (i == 1 && ARITHMETIC_P (x))
11268 {
11269 /* Note that at this point x0 has already been checked
11270 and found valid. */
11271 rtx x0 = XEXP (x, 0);
11272 rtx x1 = XEXP (x, 1);
11273
11274 /* If x0 and x1 are identical then x is also valid. */
11275 if (x0 == x1)
11276 return 1;
11277
11278 /* If x1 is identical to a subexpression of x0 then
11279 while checking x0, x1 has already been checked. Thus
11280 it is valid and so as x. */
11281 if (ARITHMETIC_P (x0)
11282 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11283 return 1;
11284
11285 /* If x0 is identical to a subexpression of x1 then x is
11286 valid iff the rest of x1 is valid. */
11287 if (ARITHMETIC_P (x1)
11288 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11289 return
11290 get_last_value_validate (&XEXP (x1,
11291 x0 == XEXP (x1, 0) ? 1 : 0),
11292 insn, tick, replace);
11293 }
11294
11295 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11296 replace) == 0)
11297 return 0;
11298 }
11299 /* Don't bother with these. They shouldn't occur anyway. */
11300 else if (fmt[i] == 'E')
11301 return 0;
11302 }
11303
11304 /* If we haven't found a reason for it to be invalid, it is valid. */
11305 return 1;
11306 }
11307
11308 /* Get the last value assigned to X, if known. Some registers
11309 in the value may be replaced with (clobber (const_int 0)) if their value
11310 is known longer known reliably. */
11311
11312 static rtx
11313 get_last_value (rtx x)
11314 {
11315 unsigned int regno;
11316 rtx value;
11317
11318 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11319 then convert it to the desired mode. If this is a paradoxical SUBREG,
11320 we cannot predict what values the "extra" bits might have. */
11321 if (GET_CODE (x) == SUBREG
11322 && subreg_lowpart_p (x)
11323 && (GET_MODE_SIZE (GET_MODE (x))
11324 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11325 && (value = get_last_value (SUBREG_REG (x))) != 0)
11326 return gen_lowpart (GET_MODE (x), value);
11327
11328 if (!REG_P (x))
11329 return 0;
11330
11331 regno = REGNO (x);
11332 value = reg_stat[regno].last_set_value;
11333
11334 /* If we don't have a value, or if it isn't for this basic block and
11335 it's either a hard register, set more than once, or it's a live
11336 at the beginning of the function, return 0.
11337
11338 Because if it's not live at the beginning of the function then the reg
11339 is always set before being used (is never used without being set).
11340 And, if it's set only once, and it's always set before use, then all
11341 uses must have the same last value, even if it's not from this basic
11342 block. */
11343
11344 if (value == 0
11345 || (reg_stat[regno].last_set_label != label_tick
11346 && (regno < FIRST_PSEUDO_REGISTER
11347 || REG_N_SETS (regno) != 1
11348 || (REGNO_REG_SET_P
11349 (ENTRY_BLOCK_PTR->next_bb->il.rtl->global_live_at_start,
11350 regno)))))
11351 return 0;
11352
11353 /* If the value was set in a later insn than the ones we are processing,
11354 we can't use it even if the register was only set once. */
11355 if (INSN_CUID (reg_stat[regno].last_set) >= subst_low_cuid)
11356 return 0;
11357
11358 /* If the value has all its registers valid, return it. */
11359 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11360 reg_stat[regno].last_set_label, 0))
11361 return value;
11362
11363 /* Otherwise, make a copy and replace any invalid register with
11364 (clobber (const_int 0)). If that fails for some reason, return 0. */
11365
11366 value = copy_rtx (value);
11367 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11368 reg_stat[regno].last_set_label, 1))
11369 return value;
11370
11371 return 0;
11372 }
11373 \f
11374 /* Return nonzero if expression X refers to a REG or to memory
11375 that is set in an instruction more recent than FROM_CUID. */
11376
11377 static int
11378 use_crosses_set_p (rtx x, int from_cuid)
11379 {
11380 const char *fmt;
11381 int i;
11382 enum rtx_code code = GET_CODE (x);
11383
11384 if (code == REG)
11385 {
11386 unsigned int regno = REGNO (x);
11387 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11388 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11389
11390 #ifdef PUSH_ROUNDING
11391 /* Don't allow uses of the stack pointer to be moved,
11392 because we don't know whether the move crosses a push insn. */
11393 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11394 return 1;
11395 #endif
11396 for (; regno < endreg; regno++)
11397 if (reg_stat[regno].last_set
11398 && INSN_CUID (reg_stat[regno].last_set) > from_cuid)
11399 return 1;
11400 return 0;
11401 }
11402
11403 if (code == MEM && mem_last_set > from_cuid)
11404 return 1;
11405
11406 fmt = GET_RTX_FORMAT (code);
11407
11408 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11409 {
11410 if (fmt[i] == 'E')
11411 {
11412 int j;
11413 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11414 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11415 return 1;
11416 }
11417 else if (fmt[i] == 'e'
11418 && use_crosses_set_p (XEXP (x, i), from_cuid))
11419 return 1;
11420 }
11421 return 0;
11422 }
11423 \f
11424 /* Define three variables used for communication between the following
11425 routines. */
11426
11427 static unsigned int reg_dead_regno, reg_dead_endregno;
11428 static int reg_dead_flag;
11429
11430 /* Function called via note_stores from reg_dead_at_p.
11431
11432 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11433 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11434
11435 static void
11436 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11437 {
11438 unsigned int regno, endregno;
11439
11440 if (!REG_P (dest))
11441 return;
11442
11443 regno = REGNO (dest);
11444 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11445 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11446
11447 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11448 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11449 }
11450
11451 /* Return nonzero if REG is known to be dead at INSN.
11452
11453 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11454 referencing REG, it is dead. If we hit a SET referencing REG, it is
11455 live. Otherwise, see if it is live or dead at the start of the basic
11456 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11457 must be assumed to be always live. */
11458
11459 static int
11460 reg_dead_at_p (rtx reg, rtx insn)
11461 {
11462 basic_block block;
11463 unsigned int i;
11464
11465 /* Set variables for reg_dead_at_p_1. */
11466 reg_dead_regno = REGNO (reg);
11467 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11468 ? hard_regno_nregs[reg_dead_regno]
11469 [GET_MODE (reg)]
11470 : 1);
11471
11472 reg_dead_flag = 0;
11473
11474 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11475 we allow the machine description to decide whether use-and-clobber
11476 patterns are OK. */
11477 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11478 {
11479 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11480 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11481 return 0;
11482 }
11483
11484 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11485 beginning of function. */
11486 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11487 insn = prev_nonnote_insn (insn))
11488 {
11489 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11490 if (reg_dead_flag)
11491 return reg_dead_flag == 1 ? 1 : 0;
11492
11493 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11494 return 1;
11495 }
11496
11497 /* Get the basic block that we were in. */
11498 if (insn == 0)
11499 block = ENTRY_BLOCK_PTR->next_bb;
11500 else
11501 {
11502 FOR_EACH_BB (block)
11503 if (insn == BB_HEAD (block))
11504 break;
11505
11506 if (block == EXIT_BLOCK_PTR)
11507 return 0;
11508 }
11509
11510 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11511 if (REGNO_REG_SET_P (block->il.rtl->global_live_at_start, i))
11512 return 0;
11513
11514 return 1;
11515 }
11516 \f
11517 /* Note hard registers in X that are used. This code is similar to
11518 that in flow.c, but much simpler since we don't care about pseudos. */
11519
11520 static void
11521 mark_used_regs_combine (rtx x)
11522 {
11523 RTX_CODE code = GET_CODE (x);
11524 unsigned int regno;
11525 int i;
11526
11527 switch (code)
11528 {
11529 case LABEL_REF:
11530 case SYMBOL_REF:
11531 case CONST_INT:
11532 case CONST:
11533 case CONST_DOUBLE:
11534 case CONST_VECTOR:
11535 case PC:
11536 case ADDR_VEC:
11537 case ADDR_DIFF_VEC:
11538 case ASM_INPUT:
11539 #ifdef HAVE_cc0
11540 /* CC0 must die in the insn after it is set, so we don't need to take
11541 special note of it here. */
11542 case CC0:
11543 #endif
11544 return;
11545
11546 case CLOBBER:
11547 /* If we are clobbering a MEM, mark any hard registers inside the
11548 address as used. */
11549 if (MEM_P (XEXP (x, 0)))
11550 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11551 return;
11552
11553 case REG:
11554 regno = REGNO (x);
11555 /* A hard reg in a wide mode may really be multiple registers.
11556 If so, mark all of them just like the first. */
11557 if (regno < FIRST_PSEUDO_REGISTER)
11558 {
11559 unsigned int endregno, r;
11560
11561 /* None of this applies to the stack, frame or arg pointers. */
11562 if (regno == STACK_POINTER_REGNUM
11563 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11564 || regno == HARD_FRAME_POINTER_REGNUM
11565 #endif
11566 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11567 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11568 #endif
11569 || regno == FRAME_POINTER_REGNUM)
11570 return;
11571
11572 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11573 for (r = regno; r < endregno; r++)
11574 SET_HARD_REG_BIT (newpat_used_regs, r);
11575 }
11576 return;
11577
11578 case SET:
11579 {
11580 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11581 the address. */
11582 rtx testreg = SET_DEST (x);
11583
11584 while (GET_CODE (testreg) == SUBREG
11585 || GET_CODE (testreg) == ZERO_EXTRACT
11586 || GET_CODE (testreg) == STRICT_LOW_PART)
11587 testreg = XEXP (testreg, 0);
11588
11589 if (MEM_P (testreg))
11590 mark_used_regs_combine (XEXP (testreg, 0));
11591
11592 mark_used_regs_combine (SET_SRC (x));
11593 }
11594 return;
11595
11596 default:
11597 break;
11598 }
11599
11600 /* Recursively scan the operands of this expression. */
11601
11602 {
11603 const char *fmt = GET_RTX_FORMAT (code);
11604
11605 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11606 {
11607 if (fmt[i] == 'e')
11608 mark_used_regs_combine (XEXP (x, i));
11609 else if (fmt[i] == 'E')
11610 {
11611 int j;
11612
11613 for (j = 0; j < XVECLEN (x, i); j++)
11614 mark_used_regs_combine (XVECEXP (x, i, j));
11615 }
11616 }
11617 }
11618 }
11619 \f
11620 /* Remove register number REGNO from the dead registers list of INSN.
11621
11622 Return the note used to record the death, if there was one. */
11623
11624 rtx
11625 remove_death (unsigned int regno, rtx insn)
11626 {
11627 rtx note = find_regno_note (insn, REG_DEAD, regno);
11628
11629 if (note)
11630 {
11631 REG_N_DEATHS (regno)--;
11632 remove_note (insn, note);
11633 }
11634
11635 return note;
11636 }
11637
11638 /* For each register (hardware or pseudo) used within expression X, if its
11639 death is in an instruction with cuid between FROM_CUID (inclusive) and
11640 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11641 list headed by PNOTES.
11642
11643 That said, don't move registers killed by maybe_kill_insn.
11644
11645 This is done when X is being merged by combination into TO_INSN. These
11646 notes will then be distributed as needed. */
11647
11648 static void
11649 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
11650 rtx *pnotes)
11651 {
11652 const char *fmt;
11653 int len, i;
11654 enum rtx_code code = GET_CODE (x);
11655
11656 if (code == REG)
11657 {
11658 unsigned int regno = REGNO (x);
11659 rtx where_dead = reg_stat[regno].last_death;
11660 rtx before_dead, after_dead;
11661
11662 /* Don't move the register if it gets killed in between from and to. */
11663 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11664 && ! reg_referenced_p (x, maybe_kill_insn))
11665 return;
11666
11667 /* WHERE_DEAD could be a USE insn made by combine, so first we
11668 make sure that we have insns with valid INSN_CUID values. */
11669 before_dead = where_dead;
11670 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11671 before_dead = PREV_INSN (before_dead);
11672
11673 after_dead = where_dead;
11674 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11675 after_dead = NEXT_INSN (after_dead);
11676
11677 if (before_dead && after_dead
11678 && INSN_CUID (before_dead) >= from_cuid
11679 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11680 || (where_dead != after_dead
11681 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11682 {
11683 rtx note = remove_death (regno, where_dead);
11684
11685 /* It is possible for the call above to return 0. This can occur
11686 when last_death points to I2 or I1 that we combined with.
11687 In that case make a new note.
11688
11689 We must also check for the case where X is a hard register
11690 and NOTE is a death note for a range of hard registers
11691 including X. In that case, we must put REG_DEAD notes for
11692 the remaining registers in place of NOTE. */
11693
11694 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11695 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11696 > GET_MODE_SIZE (GET_MODE (x))))
11697 {
11698 unsigned int deadregno = REGNO (XEXP (note, 0));
11699 unsigned int deadend
11700 = (deadregno + hard_regno_nregs[deadregno]
11701 [GET_MODE (XEXP (note, 0))]);
11702 unsigned int ourend
11703 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11704 unsigned int i;
11705
11706 for (i = deadregno; i < deadend; i++)
11707 if (i < regno || i >= ourend)
11708 REG_NOTES (where_dead)
11709 = gen_rtx_EXPR_LIST (REG_DEAD,
11710 regno_reg_rtx[i],
11711 REG_NOTES (where_dead));
11712 }
11713
11714 /* If we didn't find any note, or if we found a REG_DEAD note that
11715 covers only part of the given reg, and we have a multi-reg hard
11716 register, then to be safe we must check for REG_DEAD notes
11717 for each register other than the first. They could have
11718 their own REG_DEAD notes lying around. */
11719 else if ((note == 0
11720 || (note != 0
11721 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11722 < GET_MODE_SIZE (GET_MODE (x)))))
11723 && regno < FIRST_PSEUDO_REGISTER
11724 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
11725 {
11726 unsigned int ourend
11727 = regno + hard_regno_nregs[regno][GET_MODE (x)];
11728 unsigned int i, offset;
11729 rtx oldnotes = 0;
11730
11731 if (note)
11732 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
11733 else
11734 offset = 1;
11735
11736 for (i = regno + offset; i < ourend; i++)
11737 move_deaths (regno_reg_rtx[i],
11738 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11739 }
11740
11741 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11742 {
11743 XEXP (note, 1) = *pnotes;
11744 *pnotes = note;
11745 }
11746 else
11747 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11748
11749 REG_N_DEATHS (regno)++;
11750 }
11751
11752 return;
11753 }
11754
11755 else if (GET_CODE (x) == SET)
11756 {
11757 rtx dest = SET_DEST (x);
11758
11759 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11760
11761 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11762 that accesses one word of a multi-word item, some
11763 piece of everything register in the expression is used by
11764 this insn, so remove any old death. */
11765 /* ??? So why do we test for equality of the sizes? */
11766
11767 if (GET_CODE (dest) == ZERO_EXTRACT
11768 || GET_CODE (dest) == STRICT_LOW_PART
11769 || (GET_CODE (dest) == SUBREG
11770 && (((GET_MODE_SIZE (GET_MODE (dest))
11771 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11772 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11773 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11774 {
11775 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11776 return;
11777 }
11778
11779 /* If this is some other SUBREG, we know it replaces the entire
11780 value, so use that as the destination. */
11781 if (GET_CODE (dest) == SUBREG)
11782 dest = SUBREG_REG (dest);
11783
11784 /* If this is a MEM, adjust deaths of anything used in the address.
11785 For a REG (the only other possibility), the entire value is
11786 being replaced so the old value is not used in this insn. */
11787
11788 if (MEM_P (dest))
11789 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11790 to_insn, pnotes);
11791 return;
11792 }
11793
11794 else if (GET_CODE (x) == CLOBBER)
11795 return;
11796
11797 len = GET_RTX_LENGTH (code);
11798 fmt = GET_RTX_FORMAT (code);
11799
11800 for (i = 0; i < len; i++)
11801 {
11802 if (fmt[i] == 'E')
11803 {
11804 int j;
11805 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11806 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11807 to_insn, pnotes);
11808 }
11809 else if (fmt[i] == 'e')
11810 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11811 }
11812 }
11813 \f
11814 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11815 pattern of an insn. X must be a REG. */
11816
11817 static int
11818 reg_bitfield_target_p (rtx x, rtx body)
11819 {
11820 int i;
11821
11822 if (GET_CODE (body) == SET)
11823 {
11824 rtx dest = SET_DEST (body);
11825 rtx target;
11826 unsigned int regno, tregno, endregno, endtregno;
11827
11828 if (GET_CODE (dest) == ZERO_EXTRACT)
11829 target = XEXP (dest, 0);
11830 else if (GET_CODE (dest) == STRICT_LOW_PART)
11831 target = SUBREG_REG (XEXP (dest, 0));
11832 else
11833 return 0;
11834
11835 if (GET_CODE (target) == SUBREG)
11836 target = SUBREG_REG (target);
11837
11838 if (!REG_P (target))
11839 return 0;
11840
11841 tregno = REGNO (target), regno = REGNO (x);
11842 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11843 return target == x;
11844
11845 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
11846 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
11847
11848 return endregno > tregno && regno < endtregno;
11849 }
11850
11851 else if (GET_CODE (body) == PARALLEL)
11852 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11853 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11854 return 1;
11855
11856 return 0;
11857 }
11858 \f
11859 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11860 as appropriate. I3 and I2 are the insns resulting from the combination
11861 insns including FROM (I2 may be zero).
11862
11863 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11864 not need REG_DEAD notes because they are being substituted for. This
11865 saves searching in the most common cases.
11866
11867 Each note in the list is either ignored or placed on some insns, depending
11868 on the type of note. */
11869
11870 static void
11871 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
11872 rtx elim_i1)
11873 {
11874 rtx note, next_note;
11875 rtx tem;
11876
11877 for (note = notes; note; note = next_note)
11878 {
11879 rtx place = 0, place2 = 0;
11880
11881 next_note = XEXP (note, 1);
11882 switch (REG_NOTE_KIND (note))
11883 {
11884 case REG_BR_PROB:
11885 case REG_BR_PRED:
11886 /* Doesn't matter much where we put this, as long as it's somewhere.
11887 It is preferable to keep these notes on branches, which is most
11888 likely to be i3. */
11889 place = i3;
11890 break;
11891
11892 case REG_VALUE_PROFILE:
11893 /* Just get rid of this note, as it is unused later anyway. */
11894 break;
11895
11896 case REG_NON_LOCAL_GOTO:
11897 if (JUMP_P (i3))
11898 place = i3;
11899 else
11900 {
11901 gcc_assert (i2 && JUMP_P (i2));
11902 place = i2;
11903 }
11904 break;
11905
11906 case REG_EH_REGION:
11907 /* These notes must remain with the call or trapping instruction. */
11908 if (CALL_P (i3))
11909 place = i3;
11910 else if (i2 && CALL_P (i2))
11911 place = i2;
11912 else
11913 {
11914 gcc_assert (flag_non_call_exceptions);
11915 if (may_trap_p (i3))
11916 place = i3;
11917 else if (i2 && may_trap_p (i2))
11918 place = i2;
11919 /* ??? Otherwise assume we've combined things such that we
11920 can now prove that the instructions can't trap. Drop the
11921 note in this case. */
11922 }
11923 break;
11924
11925 case REG_NORETURN:
11926 case REG_SETJMP:
11927 /* These notes must remain with the call. It should not be
11928 possible for both I2 and I3 to be a call. */
11929 if (CALL_P (i3))
11930 place = i3;
11931 else
11932 {
11933 gcc_assert (i2 && CALL_P (i2));
11934 place = i2;
11935 }
11936 break;
11937
11938 case REG_UNUSED:
11939 /* Any clobbers for i3 may still exist, and so we must process
11940 REG_UNUSED notes from that insn.
11941
11942 Any clobbers from i2 or i1 can only exist if they were added by
11943 recog_for_combine. In that case, recog_for_combine created the
11944 necessary REG_UNUSED notes. Trying to keep any original
11945 REG_UNUSED notes from these insns can cause incorrect output
11946 if it is for the same register as the original i3 dest.
11947 In that case, we will notice that the register is set in i3,
11948 and then add a REG_UNUSED note for the destination of i3, which
11949 is wrong. However, it is possible to have REG_UNUSED notes from
11950 i2 or i1 for register which were both used and clobbered, so
11951 we keep notes from i2 or i1 if they will turn into REG_DEAD
11952 notes. */
11953
11954 /* If this register is set or clobbered in I3, put the note there
11955 unless there is one already. */
11956 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11957 {
11958 if (from_insn != i3)
11959 break;
11960
11961 if (! (REG_P (XEXP (note, 0))
11962 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11963 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11964 place = i3;
11965 }
11966 /* Otherwise, if this register is used by I3, then this register
11967 now dies here, so we must put a REG_DEAD note here unless there
11968 is one already. */
11969 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11970 && ! (REG_P (XEXP (note, 0))
11971 ? find_regno_note (i3, REG_DEAD,
11972 REGNO (XEXP (note, 0)))
11973 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11974 {
11975 PUT_REG_NOTE_KIND (note, REG_DEAD);
11976 place = i3;
11977 }
11978 break;
11979
11980 case REG_EQUAL:
11981 case REG_EQUIV:
11982 case REG_NOALIAS:
11983 /* These notes say something about results of an insn. We can
11984 only support them if they used to be on I3 in which case they
11985 remain on I3. Otherwise they are ignored.
11986
11987 If the note refers to an expression that is not a constant, we
11988 must also ignore the note since we cannot tell whether the
11989 equivalence is still true. It might be possible to do
11990 slightly better than this (we only have a problem if I2DEST
11991 or I1DEST is present in the expression), but it doesn't
11992 seem worth the trouble. */
11993
11994 if (from_insn == i3
11995 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11996 place = i3;
11997 break;
11998
11999 case REG_INC:
12000 case REG_NO_CONFLICT:
12001 /* These notes say something about how a register is used. They must
12002 be present on any use of the register in I2 or I3. */
12003 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12004 place = i3;
12005
12006 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12007 {
12008 if (place)
12009 place2 = i2;
12010 else
12011 place = i2;
12012 }
12013 break;
12014
12015 case REG_LABEL:
12016 /* This can show up in several ways -- either directly in the
12017 pattern, or hidden off in the constant pool with (or without?)
12018 a REG_EQUAL note. */
12019 /* ??? Ignore the without-reg_equal-note problem for now. */
12020 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12021 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12022 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12023 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12024 place = i3;
12025
12026 if (i2
12027 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12028 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12029 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12030 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12031 {
12032 if (place)
12033 place2 = i2;
12034 else
12035 place = i2;
12036 }
12037
12038 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
12039 a JUMP_LABEL instead or decrement LABEL_NUSES. */
12040 if (place && JUMP_P (place))
12041 {
12042 rtx label = JUMP_LABEL (place);
12043
12044 if (!label)
12045 JUMP_LABEL (place) = XEXP (note, 0);
12046 else
12047 {
12048 gcc_assert (label == XEXP (note, 0));
12049 if (LABEL_P (label))
12050 LABEL_NUSES (label)--;
12051 }
12052 place = 0;
12053 }
12054 if (place2 && JUMP_P (place2))
12055 {
12056 rtx label = JUMP_LABEL (place2);
12057
12058 if (!label)
12059 JUMP_LABEL (place2) = XEXP (note, 0);
12060 else
12061 {
12062 gcc_assert (label == XEXP (note, 0));
12063 if (LABEL_P (label))
12064 LABEL_NUSES (label)--;
12065 }
12066 place2 = 0;
12067 }
12068 break;
12069
12070 case REG_NONNEG:
12071 /* This note says something about the value of a register prior
12072 to the execution of an insn. It is too much trouble to see
12073 if the note is still correct in all situations. It is better
12074 to simply delete it. */
12075 break;
12076
12077 case REG_RETVAL:
12078 /* If the insn previously containing this note still exists,
12079 put it back where it was. Otherwise move it to the previous
12080 insn. Adjust the corresponding REG_LIBCALL note. */
12081 if (!NOTE_P (from_insn))
12082 place = from_insn;
12083 else
12084 {
12085 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12086 place = prev_real_insn (from_insn);
12087 if (tem && place)
12088 XEXP (tem, 0) = place;
12089 /* If we're deleting the last remaining instruction of a
12090 libcall sequence, don't add the notes. */
12091 else if (XEXP (note, 0) == from_insn)
12092 tem = place = 0;
12093 /* Don't add the dangling REG_RETVAL note. */
12094 else if (! tem)
12095 place = 0;
12096 }
12097 break;
12098
12099 case REG_LIBCALL:
12100 /* This is handled similarly to REG_RETVAL. */
12101 if (!NOTE_P (from_insn))
12102 place = from_insn;
12103 else
12104 {
12105 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12106 place = next_real_insn (from_insn);
12107 if (tem && place)
12108 XEXP (tem, 0) = place;
12109 /* If we're deleting the last remaining instruction of a
12110 libcall sequence, don't add the notes. */
12111 else if (XEXP (note, 0) == from_insn)
12112 tem = place = 0;
12113 /* Don't add the dangling REG_LIBCALL note. */
12114 else if (! tem)
12115 place = 0;
12116 }
12117 break;
12118
12119 case REG_DEAD:
12120 /* If we replaced the right hand side of FROM_INSN with a
12121 REG_EQUAL note, the original use of the dying register
12122 will not have been combined into I3 and I2. In such cases,
12123 FROM_INSN is guaranteed to be the first of the combined
12124 instructions, so we simply need to search back before
12125 FROM_INSN for the previous use or set of this register,
12126 then alter the notes there appropriately.
12127
12128 If the register is used as an input in I3, it dies there.
12129 Similarly for I2, if it is nonzero and adjacent to I3.
12130
12131 If the register is not used as an input in either I3 or I2
12132 and it is not one of the registers we were supposed to eliminate,
12133 there are two possibilities. We might have a non-adjacent I2
12134 or we might have somehow eliminated an additional register
12135 from a computation. For example, we might have had A & B where
12136 we discover that B will always be zero. In this case we will
12137 eliminate the reference to A.
12138
12139 In both cases, we must search to see if we can find a previous
12140 use of A and put the death note there. */
12141
12142 if (from_insn
12143 && from_insn == replaced_rhs_insn
12144 && !reg_overlap_mentioned_p (XEXP (note, 0), replaced_rhs_value))
12145 tem = from_insn;
12146 else
12147 {
12148 if (from_insn
12149 && CALL_P (from_insn)
12150 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12151 place = from_insn;
12152 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12153 place = i3;
12154 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12155 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12156 place = i2;
12157 else if (rtx_equal_p (XEXP (note, 0), elim_i2)
12158 || rtx_equal_p (XEXP (note, 0), elim_i1))
12159 break;
12160 tem = i3;
12161 }
12162
12163 if (place == 0)
12164 {
12165 basic_block bb = this_basic_block;
12166
12167 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12168 {
12169 if (! INSN_P (tem))
12170 {
12171 if (tem == BB_HEAD (bb))
12172 break;
12173 continue;
12174 }
12175
12176 /* If TEM is a (reaching) definition of the use to which the
12177 note was attached, see if that is all TEM is doing. If so,
12178 delete TEM. Otherwise, make this into a REG_UNUSED note
12179 instead. Don't delete sets to global register vars. */
12180 if ((!from_insn
12181 || INSN_CUID (tem) < INSN_CUID (from_insn))
12182 && (REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12183 || !global_regs[REGNO (XEXP (note, 0))])
12184 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12185 {
12186 rtx set = single_set (tem);
12187 rtx inner_dest = 0;
12188 #ifdef HAVE_cc0
12189 rtx cc0_setter = NULL_RTX;
12190 #endif
12191
12192 if (set != 0)
12193 for (inner_dest = SET_DEST (set);
12194 (GET_CODE (inner_dest) == STRICT_LOW_PART
12195 || GET_CODE (inner_dest) == SUBREG
12196 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12197 inner_dest = XEXP (inner_dest, 0))
12198 ;
12199
12200 /* Verify that it was the set, and not a clobber that
12201 modified the register.
12202
12203 CC0 targets must be careful to maintain setter/user
12204 pairs. If we cannot delete the setter due to side
12205 effects, mark the user with an UNUSED note instead
12206 of deleting it. */
12207
12208 if (set != 0 && ! side_effects_p (SET_SRC (set))
12209 && rtx_equal_p (XEXP (note, 0), inner_dest)
12210 #ifdef HAVE_cc0
12211 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12212 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12213 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12214 #endif
12215 )
12216 {
12217 /* Move the notes and links of TEM elsewhere.
12218 This might delete other dead insns recursively.
12219 First set the pattern to something that won't use
12220 any register. */
12221 rtx old_notes = REG_NOTES (tem);
12222
12223 PATTERN (tem) = pc_rtx;
12224 REG_NOTES (tem) = NULL;
12225
12226 distribute_notes (old_notes, tem, tem, NULL_RTX,
12227 NULL_RTX, NULL_RTX);
12228 distribute_links (LOG_LINKS (tem));
12229
12230 SET_INSN_DELETED (tem);
12231
12232 #ifdef HAVE_cc0
12233 /* Delete the setter too. */
12234 if (cc0_setter)
12235 {
12236 PATTERN (cc0_setter) = pc_rtx;
12237 old_notes = REG_NOTES (cc0_setter);
12238 REG_NOTES (cc0_setter) = NULL;
12239
12240 distribute_notes (old_notes, cc0_setter,
12241 cc0_setter, NULL_RTX,
12242 NULL_RTX, NULL_RTX);
12243 distribute_links (LOG_LINKS (cc0_setter));
12244
12245 SET_INSN_DELETED (cc0_setter);
12246 }
12247 #endif
12248 }
12249 else
12250 {
12251 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12252
12253 /* If there isn't already a REG_UNUSED note, put one
12254 here. Do not place a REG_DEAD note, even if
12255 the register is also used here; that would not
12256 match the algorithm used in lifetime analysis
12257 and can cause the consistency check in the
12258 scheduler to fail. */
12259 if (! find_regno_note (tem, REG_UNUSED,
12260 REGNO (XEXP (note, 0))))
12261 place = tem;
12262 break;
12263 }
12264 }
12265 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12266 || (CALL_P (tem)
12267 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12268 {
12269 place = tem;
12270
12271 /* If we are doing a 3->2 combination, and we have a
12272 register which formerly died in i3 and was not used
12273 by i2, which now no longer dies in i3 and is used in
12274 i2 but does not die in i2, and place is between i2
12275 and i3, then we may need to move a link from place to
12276 i2. */
12277 if (i2 && INSN_UID (place) <= max_uid_cuid
12278 && INSN_CUID (place) > INSN_CUID (i2)
12279 && from_insn
12280 && INSN_CUID (from_insn) > INSN_CUID (i2)
12281 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12282 {
12283 rtx links = LOG_LINKS (place);
12284 LOG_LINKS (place) = 0;
12285 distribute_links (links);
12286 }
12287 break;
12288 }
12289
12290 if (tem == BB_HEAD (bb))
12291 break;
12292 }
12293
12294 /* We haven't found an insn for the death note and it
12295 is still a REG_DEAD note, but we have hit the beginning
12296 of the block. If the existing life info says the reg
12297 was dead, there's nothing left to do. Otherwise, we'll
12298 need to do a global life update after combine. */
12299 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12300 && REGNO_REG_SET_P (bb->il.rtl->global_live_at_start,
12301 REGNO (XEXP (note, 0))))
12302 SET_BIT (refresh_blocks, this_basic_block->index);
12303 }
12304
12305 /* If the register is set or already dead at PLACE, we needn't do
12306 anything with this note if it is still a REG_DEAD note.
12307 We check here if it is set at all, not if is it totally replaced,
12308 which is what `dead_or_set_p' checks, so also check for it being
12309 set partially. */
12310
12311 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12312 {
12313 unsigned int regno = REGNO (XEXP (note, 0));
12314
12315 /* Similarly, if the instruction on which we want to place
12316 the note is a noop, we'll need do a global live update
12317 after we remove them in delete_noop_moves. */
12318 if (noop_move_p (place))
12319 SET_BIT (refresh_blocks, this_basic_block->index);
12320
12321 if (dead_or_set_p (place, XEXP (note, 0))
12322 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12323 {
12324 /* Unless the register previously died in PLACE, clear
12325 last_death. [I no longer understand why this is
12326 being done.] */
12327 if (reg_stat[regno].last_death != place)
12328 reg_stat[regno].last_death = 0;
12329 place = 0;
12330 }
12331 else
12332 reg_stat[regno].last_death = place;
12333
12334 /* If this is a death note for a hard reg that is occupying
12335 multiple registers, ensure that we are still using all
12336 parts of the object. If we find a piece of the object
12337 that is unused, we must arrange for an appropriate REG_DEAD
12338 note to be added for it. However, we can't just emit a USE
12339 and tag the note to it, since the register might actually
12340 be dead; so we recourse, and the recursive call then finds
12341 the previous insn that used this register. */
12342
12343 if (place && regno < FIRST_PSEUDO_REGISTER
12344 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12345 {
12346 unsigned int endregno
12347 = regno + hard_regno_nregs[regno]
12348 [GET_MODE (XEXP (note, 0))];
12349 int all_used = 1;
12350 unsigned int i;
12351
12352 for (i = regno; i < endregno; i++)
12353 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12354 && ! find_regno_fusage (place, USE, i))
12355 || dead_or_set_regno_p (place, i))
12356 all_used = 0;
12357
12358 if (! all_used)
12359 {
12360 /* Put only REG_DEAD notes for pieces that are
12361 not already dead or set. */
12362
12363 for (i = regno; i < endregno;
12364 i += hard_regno_nregs[i][reg_raw_mode[i]])
12365 {
12366 rtx piece = regno_reg_rtx[i];
12367 basic_block bb = this_basic_block;
12368
12369 if (! dead_or_set_p (place, piece)
12370 && ! reg_bitfield_target_p (piece,
12371 PATTERN (place)))
12372 {
12373 rtx new_note
12374 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12375
12376 distribute_notes (new_note, place, place,
12377 NULL_RTX, NULL_RTX, NULL_RTX);
12378 }
12379 else if (! refers_to_regno_p (i, i + 1,
12380 PATTERN (place), 0)
12381 && ! find_regno_fusage (place, USE, i))
12382 for (tem = PREV_INSN (place); ;
12383 tem = PREV_INSN (tem))
12384 {
12385 if (! INSN_P (tem))
12386 {
12387 if (tem == BB_HEAD (bb))
12388 {
12389 SET_BIT (refresh_blocks,
12390 this_basic_block->index);
12391 break;
12392 }
12393 continue;
12394 }
12395 if (dead_or_set_p (tem, piece)
12396 || reg_bitfield_target_p (piece,
12397 PATTERN (tem)))
12398 {
12399 REG_NOTES (tem)
12400 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12401 REG_NOTES (tem));
12402 break;
12403 }
12404 }
12405
12406 }
12407
12408 place = 0;
12409 }
12410 }
12411 }
12412 break;
12413
12414 default:
12415 /* Any other notes should not be present at this point in the
12416 compilation. */
12417 gcc_unreachable ();
12418 }
12419
12420 if (place)
12421 {
12422 XEXP (note, 1) = REG_NOTES (place);
12423 REG_NOTES (place) = note;
12424 }
12425 else if ((REG_NOTE_KIND (note) == REG_DEAD
12426 || REG_NOTE_KIND (note) == REG_UNUSED)
12427 && REG_P (XEXP (note, 0)))
12428 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12429
12430 if (place2)
12431 {
12432 if ((REG_NOTE_KIND (note) == REG_DEAD
12433 || REG_NOTE_KIND (note) == REG_UNUSED)
12434 && REG_P (XEXP (note, 0)))
12435 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12436
12437 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12438 REG_NOTE_KIND (note),
12439 XEXP (note, 0),
12440 REG_NOTES (place2));
12441 }
12442 }
12443 }
12444 \f
12445 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12446 I3, I2, and I1 to new locations. This is also called to add a link
12447 pointing at I3 when I3's destination is changed. */
12448
12449 static void
12450 distribute_links (rtx links)
12451 {
12452 rtx link, next_link;
12453
12454 for (link = links; link; link = next_link)
12455 {
12456 rtx place = 0;
12457 rtx insn;
12458 rtx set, reg;
12459
12460 next_link = XEXP (link, 1);
12461
12462 /* If the insn that this link points to is a NOTE or isn't a single
12463 set, ignore it. In the latter case, it isn't clear what we
12464 can do other than ignore the link, since we can't tell which
12465 register it was for. Such links wouldn't be used by combine
12466 anyway.
12467
12468 It is not possible for the destination of the target of the link to
12469 have been changed by combine. The only potential of this is if we
12470 replace I3, I2, and I1 by I3 and I2. But in that case the
12471 destination of I2 also remains unchanged. */
12472
12473 if (NOTE_P (XEXP (link, 0))
12474 || (set = single_set (XEXP (link, 0))) == 0)
12475 continue;
12476
12477 reg = SET_DEST (set);
12478 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12479 || GET_CODE (reg) == STRICT_LOW_PART)
12480 reg = XEXP (reg, 0);
12481
12482 /* A LOG_LINK is defined as being placed on the first insn that uses
12483 a register and points to the insn that sets the register. Start
12484 searching at the next insn after the target of the link and stop
12485 when we reach a set of the register or the end of the basic block.
12486
12487 Note that this correctly handles the link that used to point from
12488 I3 to I2. Also note that not much searching is typically done here
12489 since most links don't point very far away. */
12490
12491 for (insn = NEXT_INSN (XEXP (link, 0));
12492 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12493 || BB_HEAD (this_basic_block->next_bb) != insn));
12494 insn = NEXT_INSN (insn))
12495 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12496 {
12497 if (reg_referenced_p (reg, PATTERN (insn)))
12498 place = insn;
12499 break;
12500 }
12501 else if (CALL_P (insn)
12502 && find_reg_fusage (insn, USE, reg))
12503 {
12504 place = insn;
12505 break;
12506 }
12507 else if (INSN_P (insn) && reg_set_p (reg, insn))
12508 break;
12509
12510 /* If we found a place to put the link, place it there unless there
12511 is already a link to the same insn as LINK at that point. */
12512
12513 if (place)
12514 {
12515 rtx link2;
12516
12517 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12518 if (XEXP (link2, 0) == XEXP (link, 0))
12519 break;
12520
12521 if (link2 == 0)
12522 {
12523 XEXP (link, 1) = LOG_LINKS (place);
12524 LOG_LINKS (place) = link;
12525
12526 /* Set added_links_insn to the earliest insn we added a
12527 link to. */
12528 if (added_links_insn == 0
12529 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12530 added_links_insn = place;
12531 }
12532 }
12533 }
12534 }
12535 \f
12536 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12537 Check whether the expression pointer to by LOC is a register or
12538 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12539 Otherwise return zero. */
12540
12541 static int
12542 unmentioned_reg_p_1 (rtx *loc, void *expr)
12543 {
12544 rtx x = *loc;
12545
12546 if (x != NULL_RTX
12547 && (REG_P (x) || MEM_P (x))
12548 && ! reg_mentioned_p (x, (rtx) expr))
12549 return 1;
12550 return 0;
12551 }
12552
12553 /* Check for any register or memory mentioned in EQUIV that is not
12554 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12555 of EXPR where some registers may have been replaced by constants. */
12556
12557 static bool
12558 unmentioned_reg_p (rtx equiv, rtx expr)
12559 {
12560 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12561 }
12562 \f
12563 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12564
12565 static int
12566 insn_cuid (rtx insn)
12567 {
12568 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12569 && NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE)
12570 insn = NEXT_INSN (insn);
12571
12572 gcc_assert (INSN_UID (insn) <= max_uid_cuid);
12573
12574 return INSN_CUID (insn);
12575 }
12576 \f
12577 void
12578 dump_combine_stats (FILE *file)
12579 {
12580 fprintf
12581 (file,
12582 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12583 combine_attempts, combine_merges, combine_extras, combine_successes);
12584 }
12585
12586 void
12587 dump_combine_total_stats (FILE *file)
12588 {
12589 fprintf
12590 (file,
12591 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12592 total_attempts, total_merges, total_extras, total_successes);
12593 }
12594 \f
12595
12596 static bool
12597 gate_handle_combine (void)
12598 {
12599 return (optimize > 0);
12600 }
12601
12602 /* Try combining insns through substitution. */
12603 static unsigned int
12604 rest_of_handle_combine (void)
12605 {
12606 int rebuild_jump_labels_after_combine
12607 = combine_instructions (get_insns (), max_reg_num ());
12608
12609 /* Combining insns may have turned an indirect jump into a
12610 direct jump. Rebuild the JUMP_LABEL fields of jumping
12611 instructions. */
12612 if (rebuild_jump_labels_after_combine)
12613 {
12614 timevar_push (TV_JUMP);
12615 rebuild_jump_labels (get_insns ());
12616 timevar_pop (TV_JUMP);
12617
12618 delete_dead_jumptables ();
12619 cleanup_cfg (CLEANUP_EXPENSIVE | CLEANUP_UPDATE_LIFE);
12620 }
12621 return 0;
12622 }
12623
12624 struct tree_opt_pass pass_combine =
12625 {
12626 "combine", /* name */
12627 gate_handle_combine, /* gate */
12628 rest_of_handle_combine, /* execute */
12629 NULL, /* sub */
12630 NULL, /* next */
12631 0, /* static_pass_number */
12632 TV_COMBINE, /* tv_id */
12633 0, /* properties_required */
12634 0, /* properties_provided */
12635 0, /* properties_destroyed */
12636 0, /* todo_flags_start */
12637 TODO_dump_func |
12638 TODO_ggc_collect, /* todo_flags_finish */
12639 'c' /* letter */
12640 };
12641