]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/combine.c
b0646c21b0b041748a05132b855f381c22419e23
[thirdparty/gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
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_regnotes) 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 "rtl.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
87 #include "expr.h"
88 #include "insn-flags.h"
89 #include "insn-codes.h"
90 #include "insn-attr.h"
91 #include "recog.h"
92 #include "real.h"
93 #include "toplev.h"
94
95 /* It is not safe to use ordinary gen_lowpart in combine.
96 Use gen_lowpart_for_combine instead. See comments there. */
97 #define gen_lowpart dont_use_gen_lowpart_you_dummy
98
99 /* Number of attempts to combine instructions in this function. */
100
101 static int combine_attempts;
102
103 /* Number of attempts that got as far as substitution in this function. */
104
105 static int combine_merges;
106
107 /* Number of instructions combined with added SETs in this function. */
108
109 static int combine_extras;
110
111 /* Number of instructions combined in this function. */
112
113 static int combine_successes;
114
115 /* Totals over entire compilation. */
116
117 static int total_attempts, total_merges, total_extras, total_successes;
118
119 /* Define a default value for REVERSIBLE_CC_MODE.
120 We can never assume that a condition code mode is safe to reverse unless
121 the md tells us so. */
122 #ifndef REVERSIBLE_CC_MODE
123 #define REVERSIBLE_CC_MODE(MODE) 0
124 #endif
125 \f
126 /* Vector mapping INSN_UIDs to cuids.
127 The cuids are like uids but increase monotonically always.
128 Combine always uses cuids so that it can compare them.
129 But actually renumbering the uids, which we used to do,
130 proves to be a bad idea because it makes it hard to compare
131 the dumps produced by earlier passes with those from later passes. */
132
133 static int *uid_cuid;
134 static int max_uid_cuid;
135
136 /* Get the cuid of an insn. */
137
138 #define INSN_CUID(INSN) \
139 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
140
141 /* Maximum register number, which is the size of the tables below. */
142
143 static int combine_max_regno;
144
145 /* Record last point of death of (hard or pseudo) register n. */
146
147 static rtx *reg_last_death;
148
149 /* Record last point of modification of (hard or pseudo) register n. */
150
151 static rtx *reg_last_set;
152
153 /* Record the cuid of the last insn that invalidated memory
154 (anything that writes memory, and subroutine calls, but not pushes). */
155
156 static int mem_last_set;
157
158 /* Record the cuid of the last CALL_INSN
159 so we can tell whether a potential combination crosses any calls. */
160
161 static int last_call_cuid;
162
163 /* When `subst' is called, this is the insn that is being modified
164 (by combining in a previous insn). The PATTERN of this insn
165 is still the old pattern partially modified and it should not be
166 looked at, but this may be used to examine the successors of the insn
167 to judge whether a simplification is valid. */
168
169 static rtx subst_insn;
170
171 /* This is an insn that belongs before subst_insn, but is not currently
172 on the insn chain. */
173
174 static rtx subst_prev_insn;
175
176 /* This is the lowest CUID that `subst' is currently dealing with.
177 get_last_value will not return a value if the register was set at or
178 after this CUID. If not for this mechanism, we could get confused if
179 I2 or I1 in try_combine were an insn that used the old value of a register
180 to obtain a new value. In that case, we might erroneously get the
181 new value of the register when we wanted the old one. */
182
183 static int subst_low_cuid;
184
185 /* This contains any hard registers that are used in newpat; reg_dead_at_p
186 must consider all these registers to be always live. */
187
188 static HARD_REG_SET newpat_used_regs;
189
190 /* This is an insn to which a LOG_LINKS entry has been added. If this
191 insn is the earlier than I2 or I3, combine should rescan starting at
192 that location. */
193
194 static rtx added_links_insn;
195
196 /* Basic block number of the block in which we are performing combines. */
197 static int this_basic_block;
198 \f
199 /* The next group of arrays allows the recording of the last value assigned
200 to (hard or pseudo) register n. We use this information to see if a
201 operation being processed is redundant given a prior operation performed
202 on the register. For example, an `and' with a constant is redundant if
203 all the zero bits are already known to be turned off.
204
205 We use an approach similar to that used by cse, but change it in the
206 following ways:
207
208 (1) We do not want to reinitialize at each label.
209 (2) It is useful, but not critical, to know the actual value assigned
210 to a register. Often just its form is helpful.
211
212 Therefore, we maintain the following arrays:
213
214 reg_last_set_value the last value assigned
215 reg_last_set_label records the value of label_tick when the
216 register was assigned
217 reg_last_set_table_tick records the value of label_tick when a
218 value using the register is assigned
219 reg_last_set_invalid set to non-zero when it is not valid
220 to use the value of this register in some
221 register's value
222
223 To understand the usage of these tables, it is important to understand
224 the distinction between the value in reg_last_set_value being valid
225 and the register being validly contained in some other expression in the
226 table.
227
228 Entry I in reg_last_set_value is valid if it is non-zero, and either
229 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
230
231 Register I may validly appear in any expression returned for the value
232 of another register if reg_n_sets[i] is 1. It may also appear in the
233 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
234 reg_last_set_invalid[j] is zero.
235
236 If an expression is found in the table containing a register which may
237 not validly appear in an expression, the register is replaced by
238 something that won't match, (clobber (const_int 0)).
239
240 reg_last_set_invalid[i] is set non-zero when register I is being assigned
241 to and reg_last_set_table_tick[i] == label_tick. */
242
243 /* Record last value assigned to (hard or pseudo) register n. */
244
245 static rtx *reg_last_set_value;
246
247 /* Record the value of label_tick when the value for register n is placed in
248 reg_last_set_value[n]. */
249
250 static int *reg_last_set_label;
251
252 /* Record the value of label_tick when an expression involving register n
253 is placed in reg_last_set_value. */
254
255 static int *reg_last_set_table_tick;
256
257 /* Set non-zero if references to register n in expressions should not be
258 used. */
259
260 static char *reg_last_set_invalid;
261
262 /* Incremented for each label. */
263
264 static int label_tick;
265
266 /* Some registers that are set more than once and used in more than one
267 basic block are nevertheless always set in similar ways. For example,
268 a QImode register may be loaded from memory in two places on a machine
269 where byte loads zero extend.
270
271 We record in the following array what we know about the nonzero
272 bits of a register, specifically which bits are known to be zero.
273
274 If an entry is zero, it means that we don't know anything special. */
275
276 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
277
278 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
279 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
280
281 static enum machine_mode nonzero_bits_mode;
282
283 /* Nonzero if we know that a register has some leading bits that are always
284 equal to the sign bit. */
285
286 static char *reg_sign_bit_copies;
287
288 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
289 It is zero while computing them and after combine has completed. This
290 former test prevents propagating values based on previously set values,
291 which can be incorrect if a variable is modified in a loop. */
292
293 static int nonzero_sign_valid;
294
295 /* These arrays are maintained in parallel with reg_last_set_value
296 and are used to store the mode in which the register was last set,
297 the bits that were known to be zero when it was last set, and the
298 number of sign bits copies it was known to have when it was last set. */
299
300 static enum machine_mode *reg_last_set_mode;
301 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
302 static char *reg_last_set_sign_bit_copies;
303 \f
304 /* Record one modification to rtl structure
305 to be undone by storing old_contents into *where.
306 is_int is 1 if the contents are an int. */
307
308 struct undo
309 {
310 struct undo *next;
311 int is_int;
312 union {rtx r; int i;} old_contents;
313 union {rtx *r; int *i;} where;
314 };
315
316 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
317 num_undo says how many are currently recorded.
318
319 storage is nonzero if we must undo the allocation of new storage.
320 The value of storage is what to pass to obfree.
321
322 other_insn is nonzero if we have modified some other insn in the process
323 of working on subst_insn. It must be verified too.
324
325 previous_undos is the value of undobuf.undos when we started processing
326 this substitution. This will prevent gen_rtx_combine from re-used a piece
327 from the previous expression. Doing so can produce circular rtl
328 structures. */
329
330 struct undobuf
331 {
332 char *storage;
333 struct undo *undos;
334 struct undo *frees;
335 struct undo *previous_undos;
336 rtx other_insn;
337 };
338
339 static struct undobuf undobuf;
340
341 /* Number of times the pseudo being substituted for
342 was found and replaced. */
343
344 static int n_occurrences;
345
346 static void do_SUBST PROTO((rtx *, rtx));
347 static void do_SUBST_INT PROTO((int *, int));
348 static void init_reg_last_arrays PROTO((void));
349 static void setup_incoming_promotions PROTO((void));
350 static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx));
351 static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
352 static int sets_function_arg_p PROTO((rtx));
353 static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
354 static rtx try_combine PROTO((rtx, rtx, rtx));
355 static void undo_all PROTO((void));
356 static rtx *find_split_point PROTO((rtx *, rtx));
357 static rtx subst PROTO((rtx, rtx, rtx, int, int));
358 static rtx simplify_rtx PROTO((rtx, enum machine_mode, int, int));
359 static rtx simplify_if_then_else PROTO((rtx));
360 static rtx simplify_set PROTO((rtx));
361 static rtx simplify_logical PROTO((rtx, int));
362 static rtx expand_compound_operation PROTO((rtx));
363 static rtx expand_field_assignment PROTO((rtx));
364 static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
365 int, int, int));
366 static rtx extract_left_shift PROTO((rtx, int));
367 static rtx make_compound_operation PROTO((rtx, enum rtx_code));
368 static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
369 static rtx force_to_mode PROTO((rtx, enum machine_mode,
370 unsigned HOST_WIDE_INT, rtx, int));
371 static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
372 static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
373 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
374 static rtx make_field_assignment PROTO((rtx));
375 static rtx apply_distributive_law PROTO((rtx));
376 static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
377 unsigned HOST_WIDE_INT));
378 static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
379 static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
380 static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
381 enum rtx_code, HOST_WIDE_INT,
382 enum machine_mode, int *));
383 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
384 rtx, int));
385 static int recog_for_combine PROTO((rtx *, rtx, rtx *));
386 static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
387 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
388 ...));
389 static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
390 rtx, rtx));
391 static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
392 enum machine_mode, rtx));
393 static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
394 static int reversible_comparison_p PROTO((rtx));
395 static void update_table_tick PROTO((rtx));
396 static void record_value_for_reg PROTO((rtx, rtx, rtx));
397 static void record_dead_and_set_regs_1 PROTO((rtx, rtx));
398 static void record_dead_and_set_regs PROTO((rtx));
399 static int get_last_value_validate PROTO((rtx *, rtx, int, int));
400 static rtx get_last_value PROTO((rtx));
401 static int use_crosses_set_p PROTO((rtx, int));
402 static void reg_dead_at_p_1 PROTO((rtx, rtx));
403 static int reg_dead_at_p PROTO((rtx, rtx));
404 static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
405 static int reg_bitfield_target_p PROTO((rtx, rtx));
406 static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
407 static void distribute_links PROTO((rtx));
408 static void mark_used_regs_combine PROTO((rtx));
409 static int insn_cuid PROTO((rtx));
410 \f
411 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
412 insn. The substitution can be undone by undo_all. If INTO is already
413 set to NEWVAL, do not record this change. Because computing NEWVAL might
414 also call SUBST, we have to compute it before we put anything into
415 the undo table. */
416
417 static void
418 do_SUBST(into, newval)
419 rtx *into, newval;
420 {
421 struct undo *buf;
422 rtx oldval = *into;
423
424 if (oldval == newval)
425 return;
426
427 if (undobuf.frees)
428 buf = undobuf.frees, undobuf.frees = buf->next;
429 else
430 buf = (struct undo *) xmalloc (sizeof (struct undo));
431
432 buf->is_int = 0;
433 buf->where.r = into;
434 buf->old_contents.r = oldval;
435 *into = newval;
436
437 buf->next = undobuf.undos, undobuf.undos = buf;
438 }
439
440 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
441
442 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
443 for the value of a HOST_WIDE_INT value (including CONST_INT) is
444 not safe. */
445
446 static void
447 do_SUBST_INT(into, newval)
448 int *into, newval;
449 {
450 struct undo *buf;
451 int oldval = *into;
452
453 if (oldval == newval)
454 return;
455
456 if (undobuf.frees)
457 buf = undobuf.frees, undobuf.frees = buf->next;
458 else
459 buf = (struct undo *) xmalloc (sizeof (struct undo));
460
461 buf->is_int = 1;
462 buf->where.i = into;
463 buf->old_contents.i = oldval;
464 *into = newval;
465
466 buf->next = undobuf.undos, undobuf.undos = buf;
467 }
468
469 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
470 \f
471 /* Main entry point for combiner. F is the first insn of the function.
472 NREGS is the first unused pseudo-reg number. */
473
474 void
475 combine_instructions (f, nregs)
476 rtx f;
477 int nregs;
478 {
479 register rtx insn, next;
480 #ifdef HAVE_cc0
481 register rtx prev;
482 #endif
483 register int i;
484 register rtx links, nextlinks;
485
486 combine_attempts = 0;
487 combine_merges = 0;
488 combine_extras = 0;
489 combine_successes = 0;
490 undobuf.undos = undobuf.previous_undos = 0;
491
492 combine_max_regno = nregs;
493
494 reg_nonzero_bits
495 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
496 reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
497
498 bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
499 bzero (reg_sign_bit_copies, nregs * sizeof (char));
500
501 reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
502 reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
503 reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
504 reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
505 reg_last_set_label = (int *) alloca (nregs * sizeof (int));
506 reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
507 reg_last_set_mode
508 = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
509 reg_last_set_nonzero_bits
510 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
511 reg_last_set_sign_bit_copies
512 = (char *) alloca (nregs * sizeof (char));
513
514 init_reg_last_arrays ();
515
516 init_recog_no_volatile ();
517
518 /* Compute maximum uid value so uid_cuid can be allocated. */
519
520 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
521 if (INSN_UID (insn) > i)
522 i = INSN_UID (insn);
523
524 uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
525 max_uid_cuid = i;
526
527 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
528
529 /* Don't use reg_nonzero_bits when computing it. This can cause problems
530 when, for example, we have j <<= 1 in a loop. */
531
532 nonzero_sign_valid = 0;
533
534 /* Compute the mapping from uids to cuids.
535 Cuids are numbers assigned to insns, like uids,
536 except that cuids increase monotonically through the code.
537
538 Scan all SETs and see if we can deduce anything about what
539 bits are known to be zero for some registers and how many copies
540 of the sign bit are known to exist for those registers.
541
542 Also set any known values so that we can use it while searching
543 for what bits are known to be set. */
544
545 label_tick = 1;
546
547 /* We need to initialize it here, because record_dead_and_set_regs may call
548 get_last_value. */
549 subst_prev_insn = NULL_RTX;
550
551 setup_incoming_promotions ();
552
553 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
554 {
555 uid_cuid[INSN_UID (insn)] = ++i;
556 subst_low_cuid = i;
557 subst_insn = insn;
558
559 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
560 {
561 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
562 record_dead_and_set_regs (insn);
563
564 #ifdef AUTO_INC_DEC
565 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
566 if (REG_NOTE_KIND (links) == REG_INC)
567 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
568 #endif
569 }
570
571 if (GET_CODE (insn) == CODE_LABEL)
572 label_tick++;
573 }
574
575 nonzero_sign_valid = 1;
576
577 /* Now scan all the insns in forward order. */
578
579 this_basic_block = -1;
580 label_tick = 1;
581 last_call_cuid = 0;
582 mem_last_set = 0;
583 init_reg_last_arrays ();
584 setup_incoming_promotions ();
585
586 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
587 {
588 next = 0;
589
590 /* If INSN starts a new basic block, update our basic block number. */
591 if (this_basic_block + 1 < n_basic_blocks
592 && BLOCK_HEAD (this_basic_block + 1) == insn)
593 this_basic_block++;
594
595 if (GET_CODE (insn) == CODE_LABEL)
596 label_tick++;
597
598 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
599 {
600 /* Try this insn with each insn it links back to. */
601
602 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
603 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
604 goto retry;
605
606 /* Try each sequence of three linked insns ending with this one. */
607
608 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
609 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
610 nextlinks = XEXP (nextlinks, 1))
611 if ((next = try_combine (insn, XEXP (links, 0),
612 XEXP (nextlinks, 0))) != 0)
613 goto retry;
614
615 #ifdef HAVE_cc0
616 /* Try to combine a jump insn that uses CC0
617 with a preceding insn that sets CC0, and maybe with its
618 logical predecessor as well.
619 This is how we make decrement-and-branch insns.
620 We need this special code because data flow connections
621 via CC0 do not get entered in LOG_LINKS. */
622
623 if (GET_CODE (insn) == JUMP_INSN
624 && (prev = prev_nonnote_insn (insn)) != 0
625 && GET_CODE (prev) == INSN
626 && sets_cc0_p (PATTERN (prev)))
627 {
628 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
629 goto retry;
630
631 for (nextlinks = LOG_LINKS (prev); nextlinks;
632 nextlinks = XEXP (nextlinks, 1))
633 if ((next = try_combine (insn, prev,
634 XEXP (nextlinks, 0))) != 0)
635 goto retry;
636 }
637
638 /* Do the same for an insn that explicitly references CC0. */
639 if (GET_CODE (insn) == INSN
640 && (prev = prev_nonnote_insn (insn)) != 0
641 && GET_CODE (prev) == INSN
642 && sets_cc0_p (PATTERN (prev))
643 && GET_CODE (PATTERN (insn)) == SET
644 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
645 {
646 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
647 goto retry;
648
649 for (nextlinks = LOG_LINKS (prev); nextlinks;
650 nextlinks = XEXP (nextlinks, 1))
651 if ((next = try_combine (insn, prev,
652 XEXP (nextlinks, 0))) != 0)
653 goto retry;
654 }
655
656 /* Finally, see if any of the insns that this insn links to
657 explicitly references CC0. If so, try this insn, that insn,
658 and its predecessor if it sets CC0. */
659 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
660 if (GET_CODE (XEXP (links, 0)) == INSN
661 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
662 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
663 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
664 && GET_CODE (prev) == INSN
665 && sets_cc0_p (PATTERN (prev))
666 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
667 goto retry;
668 #endif
669
670 /* Try combining an insn with two different insns whose results it
671 uses. */
672 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
673 for (nextlinks = XEXP (links, 1); nextlinks;
674 nextlinks = XEXP (nextlinks, 1))
675 if ((next = try_combine (insn, XEXP (links, 0),
676 XEXP (nextlinks, 0))) != 0)
677 goto retry;
678
679 if (GET_CODE (insn) != NOTE)
680 record_dead_and_set_regs (insn);
681
682 retry:
683 ;
684 }
685 }
686
687 total_attempts += combine_attempts;
688 total_merges += combine_merges;
689 total_extras += combine_extras;
690 total_successes += combine_successes;
691
692 nonzero_sign_valid = 0;
693
694 /* Make recognizer allow volatile MEMs again. */
695 init_recog ();
696 }
697
698 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
699
700 static void
701 init_reg_last_arrays ()
702 {
703 int nregs = combine_max_regno;
704
705 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
706 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
707 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
708 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
709 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
710 bzero (reg_last_set_invalid, nregs * sizeof (char));
711 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
712 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
713 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
714 }
715 \f
716 /* Set up any promoted values for incoming argument registers. */
717
718 static void
719 setup_incoming_promotions ()
720 {
721 #ifdef PROMOTE_FUNCTION_ARGS
722 int regno;
723 rtx reg;
724 enum machine_mode mode;
725 int unsignedp;
726 rtx first = get_insns ();
727
728 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
729 if (FUNCTION_ARG_REGNO_P (regno)
730 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
731 {
732 record_value_for_reg
733 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
734 : SIGN_EXTEND),
735 GET_MODE (reg),
736 gen_rtx_CLOBBER (mode, const0_rtx)));
737 }
738 #endif
739 }
740 \f
741 /* Called via note_stores. If X is a pseudo that is narrower than
742 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
743
744 If we are setting only a portion of X and we can't figure out what
745 portion, assume all bits will be used since we don't know what will
746 be happening.
747
748 Similarly, set how many bits of X are known to be copies of the sign bit
749 at all locations in the function. This is the smallest number implied
750 by any set of X. */
751
752 static void
753 set_nonzero_bits_and_sign_copies (x, set)
754 rtx x;
755 rtx set;
756 {
757 int num;
758
759 if (GET_CODE (x) == REG
760 && REGNO (x) >= FIRST_PSEUDO_REGISTER
761 /* If this register is undefined at the start of the file, we can't
762 say what its contents were. */
763 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, REGNO (x))
764 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
765 {
766 if (set == 0 || GET_CODE (set) == CLOBBER)
767 {
768 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
769 reg_sign_bit_copies[REGNO (x)] = 1;
770 return;
771 }
772
773 /* If this is a complex assignment, see if we can convert it into a
774 simple assignment. */
775 set = expand_field_assignment (set);
776
777 /* If this is a simple assignment, or we have a paradoxical SUBREG,
778 set what we know about X. */
779
780 if (SET_DEST (set) == x
781 || (GET_CODE (SET_DEST (set)) == SUBREG
782 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
783 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
784 && SUBREG_REG (SET_DEST (set)) == x))
785 {
786 rtx src = SET_SRC (set);
787
788 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
789 /* If X is narrower than a word and SRC is a non-negative
790 constant that would appear negative in the mode of X,
791 sign-extend it for use in reg_nonzero_bits because some
792 machines (maybe most) will actually do the sign-extension
793 and this is the conservative approach.
794
795 ??? For 2.5, try to tighten up the MD files in this regard
796 instead of this kludge. */
797
798 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
799 && GET_CODE (src) == CONST_INT
800 && INTVAL (src) > 0
801 && 0 != (INTVAL (src)
802 & ((HOST_WIDE_INT) 1
803 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
804 src = GEN_INT (INTVAL (src)
805 | ((HOST_WIDE_INT) (-1)
806 << GET_MODE_BITSIZE (GET_MODE (x))));
807 #endif
808
809 reg_nonzero_bits[REGNO (x)]
810 |= nonzero_bits (src, nonzero_bits_mode);
811 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
812 if (reg_sign_bit_copies[REGNO (x)] == 0
813 || reg_sign_bit_copies[REGNO (x)] > num)
814 reg_sign_bit_copies[REGNO (x)] = num;
815 }
816 else
817 {
818 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
819 reg_sign_bit_copies[REGNO (x)] = 1;
820 }
821 }
822 }
823 \f
824 /* See if INSN can be combined into I3. PRED and SUCC are optionally
825 insns that were previously combined into I3 or that will be combined
826 into the merger of INSN and I3.
827
828 Return 0 if the combination is not allowed for any reason.
829
830 If the combination is allowed, *PDEST will be set to the single
831 destination of INSN and *PSRC to the single source, and this function
832 will return 1. */
833
834 static int
835 can_combine_p (insn, i3, pred, succ, pdest, psrc)
836 rtx insn;
837 rtx i3;
838 rtx pred ATTRIBUTE_UNUSED;
839 rtx succ;
840 rtx *pdest, *psrc;
841 {
842 int i;
843 rtx set = 0, src, dest;
844 rtx p;
845 #ifdef AUTO_INC_DEC
846 rtx link;
847 #endif
848 int all_adjacent = (succ ? (next_active_insn (insn) == succ
849 && next_active_insn (succ) == i3)
850 : next_active_insn (insn) == i3);
851
852 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
853 or a PARALLEL consisting of such a SET and CLOBBERs.
854
855 If INSN has CLOBBER parallel parts, ignore them for our processing.
856 By definition, these happen during the execution of the insn. When it
857 is merged with another insn, all bets are off. If they are, in fact,
858 needed and aren't also supplied in I3, they may be added by
859 recog_for_combine. Otherwise, it won't match.
860
861 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
862 note.
863
864 Get the source and destination of INSN. If more than one, can't
865 combine. */
866
867 if (GET_CODE (PATTERN (insn)) == SET)
868 set = PATTERN (insn);
869 else if (GET_CODE (PATTERN (insn)) == PARALLEL
870 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
871 {
872 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
873 {
874 rtx elt = XVECEXP (PATTERN (insn), 0, i);
875
876 switch (GET_CODE (elt))
877 {
878 /* This is important to combine floating point insns
879 for the SH4 port. */
880 case USE:
881 /* Combining an isolated USE doesn't make sense.
882 We depend here on combinable_i3_pat to reject them. */
883 /* The code below this loop only verifies that the inputs of
884 the SET in INSN do not change. We call reg_set_between_p
885 to verify that the REG in the USE does not change betweeen
886 I3 and INSN.
887 If the USE in INSN was for a pseudo register, the matching
888 insn pattern will likely match any register; combining this
889 with any other USE would only be safe if we knew that the
890 used registers have identical values, or if there was
891 something to tell them apart, e.g. different modes. For
892 now, we forgo such compilcated tests and simply disallow
893 combining of USES of pseudo registers with any other USE. */
894 if (GET_CODE (XEXP (elt, 0)) == REG
895 && GET_CODE (PATTERN (i3)) == PARALLEL)
896 {
897 rtx i3pat = PATTERN (i3);
898 int i = XVECLEN (i3pat, 0) - 1;
899 int regno = REGNO (XEXP (elt, 0));
900 do
901 {
902 rtx i3elt = XVECEXP (i3pat, 0, i);
903 if (GET_CODE (i3elt) == USE
904 && GET_CODE (XEXP (i3elt, 0)) == REG
905 && (REGNO (XEXP (i3elt, 0)) == regno
906 ? reg_set_between_p (XEXP (elt, 0),
907 PREV_INSN (insn), i3)
908 : regno >= FIRST_PSEUDO_REGISTER))
909 return 0;
910 }
911 while (--i >= 0);
912 }
913 break;
914
915 /* We can ignore CLOBBERs. */
916 case CLOBBER:
917 break;
918
919 case SET:
920 /* Ignore SETs whose result isn't used but not those that
921 have side-effects. */
922 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
923 && ! side_effects_p (elt))
924 break;
925
926 /* If we have already found a SET, this is a second one and
927 so we cannot combine with this insn. */
928 if (set)
929 return 0;
930
931 set = elt;
932 break;
933
934 default:
935 /* Anything else means we can't combine. */
936 return 0;
937 }
938 }
939
940 if (set == 0
941 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
942 so don't do anything with it. */
943 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
944 return 0;
945 }
946 else
947 return 0;
948
949 if (set == 0)
950 return 0;
951
952 set = expand_field_assignment (set);
953 src = SET_SRC (set), dest = SET_DEST (set);
954
955 /* Don't eliminate a store in the stack pointer. */
956 if (dest == stack_pointer_rtx
957 /* If we couldn't eliminate a field assignment, we can't combine. */
958 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
959 /* Don't combine with an insn that sets a register to itself if it has
960 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
961 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
962 /* Can't merge a function call. */
963 || GET_CODE (src) == CALL
964 /* Don't eliminate a function call argument. */
965 || (GET_CODE (i3) == CALL_INSN
966 && (find_reg_fusage (i3, USE, dest)
967 || (GET_CODE (dest) == REG
968 && REGNO (dest) < FIRST_PSEUDO_REGISTER
969 && global_regs[REGNO (dest)])))
970 /* Don't substitute into an incremented register. */
971 || FIND_REG_INC_NOTE (i3, dest)
972 || (succ && FIND_REG_INC_NOTE (succ, dest))
973 #if 0
974 /* Don't combine the end of a libcall into anything. */
975 /* ??? This gives worse code, and appears to be unnecessary, since no
976 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
977 use REG_RETVAL notes for noconflict blocks, but other code here
978 makes sure that those insns don't disappear. */
979 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
980 #endif
981 /* Make sure that DEST is not used after SUCC but before I3. */
982 || (succ && ! all_adjacent
983 && reg_used_between_p (dest, succ, i3))
984 /* Make sure that the value that is to be substituted for the register
985 does not use any registers whose values alter in between. However,
986 If the insns are adjacent, a use can't cross a set even though we
987 think it might (this can happen for a sequence of insns each setting
988 the same destination; reg_last_set of that register might point to
989 a NOTE). If INSN has a REG_EQUIV note, the register is always
990 equivalent to the memory so the substitution is valid even if there
991 are intervening stores. Also, don't move a volatile asm or
992 UNSPEC_VOLATILE across any other insns. */
993 || (! all_adjacent
994 && (((GET_CODE (src) != MEM
995 || ! find_reg_note (insn, REG_EQUIV, src))
996 && use_crosses_set_p (src, INSN_CUID (insn)))
997 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
998 || GET_CODE (src) == UNSPEC_VOLATILE))
999 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1000 better register allocation by not doing the combine. */
1001 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1002 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1003 /* Don't combine across a CALL_INSN, because that would possibly
1004 change whether the life span of some REGs crosses calls or not,
1005 and it is a pain to update that information.
1006 Exception: if source is a constant, moving it later can't hurt.
1007 Accept that special case, because it helps -fforce-addr a lot. */
1008 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1009 return 0;
1010
1011 /* DEST must either be a REG or CC0. */
1012 if (GET_CODE (dest) == REG)
1013 {
1014 /* If register alignment is being enforced for multi-word items in all
1015 cases except for parameters, it is possible to have a register copy
1016 insn referencing a hard register that is not allowed to contain the
1017 mode being copied and which would not be valid as an operand of most
1018 insns. Eliminate this problem by not combining with such an insn.
1019
1020 Also, on some machines we don't want to extend the life of a hard
1021 register.
1022
1023 This is the same test done in can_combine except that we don't test
1024 if SRC is a CALL operation to permit a hard register with
1025 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1026 into account. */
1027
1028 if (GET_CODE (src) == REG
1029 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1030 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1031 /* Don't extend the life of a hard register unless it is
1032 user variable (if we have few registers) or it can't
1033 fit into the desired register (meaning something special
1034 is going on).
1035 Also avoid substituting a return register into I3, because
1036 reload can't handle a conflict with constraints of other
1037 inputs. */
1038 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1039 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
1040 || (SMALL_REGISTER_CLASSES
1041 && ((! all_adjacent && ! REG_USERVAR_P (src))
1042 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
1043 && ! REG_USERVAR_P (src))))))))
1044 return 0;
1045 }
1046 else if (GET_CODE (dest) != CC0)
1047 return 0;
1048
1049 /* Don't substitute for a register intended as a clobberable operand.
1050 Similarly, don't substitute an expression containing a register that
1051 will be clobbered in I3. */
1052 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1053 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1054 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1055 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1056 src)
1057 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1058 return 0;
1059
1060 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1061 or not), reject, unless nothing volatile comes between it and I3 */
1062
1063 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1064 {
1065 /* Make sure succ doesn't contain a volatile reference. */
1066 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1067 return 0;
1068
1069 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1070 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1071 && p != succ && volatile_refs_p (PATTERN (p)))
1072 return 0;
1073 }
1074
1075 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1076 to be an explicit register variable, and was chosen for a reason. */
1077
1078 if (GET_CODE (src) == ASM_OPERANDS
1079 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1080 return 0;
1081
1082 /* If there are any volatile insns between INSN and I3, reject, because
1083 they might affect machine state. */
1084
1085 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1086 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1087 && p != succ && volatile_insn_p (PATTERN (p)))
1088 return 0;
1089
1090 /* If INSN or I2 contains an autoincrement or autodecrement,
1091 make sure that register is not used between there and I3,
1092 and not already used in I3 either.
1093 Also insist that I3 not be a jump; if it were one
1094 and the incremented register were spilled, we would lose. */
1095
1096 #ifdef AUTO_INC_DEC
1097 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1098 if (REG_NOTE_KIND (link) == REG_INC
1099 && (GET_CODE (i3) == JUMP_INSN
1100 || reg_used_between_p (XEXP (link, 0), insn, i3)
1101 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1102 return 0;
1103 #endif
1104
1105 #ifdef HAVE_cc0
1106 /* Don't combine an insn that follows a CC0-setting insn.
1107 An insn that uses CC0 must not be separated from the one that sets it.
1108 We do, however, allow I2 to follow a CC0-setting insn if that insn
1109 is passed as I1; in that case it will be deleted also.
1110 We also allow combining in this case if all the insns are adjacent
1111 because that would leave the two CC0 insns adjacent as well.
1112 It would be more logical to test whether CC0 occurs inside I1 or I2,
1113 but that would be much slower, and this ought to be equivalent. */
1114
1115 p = prev_nonnote_insn (insn);
1116 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1117 && ! all_adjacent)
1118 return 0;
1119 #endif
1120
1121 /* If we get here, we have passed all the tests and the combination is
1122 to be allowed. */
1123
1124 *pdest = dest;
1125 *psrc = src;
1126
1127 return 1;
1128 }
1129 \f
1130 /* Check if PAT is an insn - or a part of it - used to set up an
1131 argument for a function in a hard register. */
1132
1133 static int
1134 sets_function_arg_p (pat)
1135 rtx pat;
1136 {
1137 int i;
1138 rtx inner_dest;
1139
1140 switch (GET_CODE (pat))
1141 {
1142 case INSN:
1143 return sets_function_arg_p (PATTERN (pat));
1144
1145 case PARALLEL:
1146 for (i = XVECLEN (pat, 0); --i >= 0;)
1147 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1148 return 1;
1149
1150 break;
1151
1152 case SET:
1153 inner_dest = SET_DEST (pat);
1154 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1155 || GET_CODE (inner_dest) == SUBREG
1156 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1157 inner_dest = XEXP (inner_dest, 0);
1158
1159 return (GET_CODE (inner_dest) == REG
1160 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1161 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1162
1163 default:
1164 break;
1165 }
1166
1167 return 0;
1168 }
1169
1170 /* LOC is the location within I3 that contains its pattern or the component
1171 of a PARALLEL of the pattern. We validate that it is valid for combining.
1172
1173 One problem is if I3 modifies its output, as opposed to replacing it
1174 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1175 so would produce an insn that is not equivalent to the original insns.
1176
1177 Consider:
1178
1179 (set (reg:DI 101) (reg:DI 100))
1180 (set (subreg:SI (reg:DI 101) 0) <foo>)
1181
1182 This is NOT equivalent to:
1183
1184 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1185 (set (reg:DI 101) (reg:DI 100))])
1186
1187 Not only does this modify 100 (in which case it might still be valid
1188 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1189
1190 We can also run into a problem if I2 sets a register that I1
1191 uses and I1 gets directly substituted into I3 (not via I2). In that
1192 case, we would be getting the wrong value of I2DEST into I3, so we
1193 must reject the combination. This case occurs when I2 and I1 both
1194 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1195 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1196 of a SET must prevent combination from occurring.
1197
1198 On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
1199 if the destination of a SET is a hard register that isn't a user
1200 variable.
1201
1202 Before doing the above check, we first try to expand a field assignment
1203 into a set of logical operations.
1204
1205 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1206 we place a register that is both set and used within I3. If more than one
1207 such register is detected, we fail.
1208
1209 Return 1 if the combination is valid, zero otherwise. */
1210
1211 static int
1212 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1213 rtx i3;
1214 rtx *loc;
1215 rtx i2dest;
1216 rtx i1dest;
1217 int i1_not_in_src;
1218 rtx *pi3dest_killed;
1219 {
1220 rtx x = *loc;
1221
1222 if (GET_CODE (x) == SET)
1223 {
1224 rtx set = expand_field_assignment (x);
1225 rtx dest = SET_DEST (set);
1226 rtx src = SET_SRC (set);
1227 rtx inner_dest = dest;
1228
1229 #if 0
1230 rtx inner_src = src;
1231 #endif
1232
1233 SUBST (*loc, set);
1234
1235 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1236 || GET_CODE (inner_dest) == SUBREG
1237 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1238 inner_dest = XEXP (inner_dest, 0);
1239
1240 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1241 was added. */
1242 #if 0
1243 while (GET_CODE (inner_src) == STRICT_LOW_PART
1244 || GET_CODE (inner_src) == SUBREG
1245 || GET_CODE (inner_src) == ZERO_EXTRACT)
1246 inner_src = XEXP (inner_src, 0);
1247
1248 /* If it is better that two different modes keep two different pseudos,
1249 avoid combining them. This avoids producing the following pattern
1250 on a 386:
1251 (set (subreg:SI (reg/v:QI 21) 0)
1252 (lshiftrt:SI (reg/v:SI 20)
1253 (const_int 24)))
1254 If that were made, reload could not handle the pair of
1255 reg 20/21, since it would try to get any GENERAL_REGS
1256 but some of them don't handle QImode. */
1257
1258 if (rtx_equal_p (inner_src, i2dest)
1259 && GET_CODE (inner_dest) == REG
1260 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1261 return 0;
1262 #endif
1263
1264 /* Check for the case where I3 modifies its output, as
1265 discussed above. */
1266 if ((inner_dest != dest
1267 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1268 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1269
1270 /* This is the same test done in can_combine_p except that we
1271 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1272 CALL operation. Moreover, we can't test all_adjacent; we don't
1273 have to, since this instruction will stay in place, thus we are
1274 not considering increasing the lifetime of INNER_DEST.
1275
1276 Also, if this insn sets a function argument, combining it with
1277 something that might need a spill could clobber a previous
1278 function argument; the all_adjacent test in can_combine_p also
1279 checks this; here, we do a more specific test for this case. */
1280
1281 || (GET_CODE (inner_dest) == REG
1282 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1283 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1284 GET_MODE (inner_dest))
1285 || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1286 && ! REG_USERVAR_P (inner_dest)
1287 && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1288 || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1289 && i3 != 0
1290 && sets_function_arg_p (prev_nonnote_insn (i3)))))))
1291 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1292 return 0;
1293
1294 /* If DEST is used in I3, it is being killed in this insn,
1295 so record that for later.
1296 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1297 STACK_POINTER_REGNUM, since these are always considered to be
1298 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1299 if (pi3dest_killed && GET_CODE (dest) == REG
1300 && reg_referenced_p (dest, PATTERN (i3))
1301 && REGNO (dest) != FRAME_POINTER_REGNUM
1302 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1303 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1304 #endif
1305 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1306 && (REGNO (dest) != ARG_POINTER_REGNUM
1307 || ! fixed_regs [REGNO (dest)])
1308 #endif
1309 && REGNO (dest) != STACK_POINTER_REGNUM)
1310 {
1311 if (*pi3dest_killed)
1312 return 0;
1313
1314 *pi3dest_killed = dest;
1315 }
1316 }
1317
1318 else if (GET_CODE (x) == PARALLEL)
1319 {
1320 int i;
1321
1322 for (i = 0; i < XVECLEN (x, 0); i++)
1323 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1324 i1_not_in_src, pi3dest_killed))
1325 return 0;
1326 }
1327
1328 return 1;
1329 }
1330 \f
1331 /* Try to combine the insns I1 and I2 into I3.
1332 Here I1 and I2 appear earlier than I3.
1333 I1 can be zero; then we combine just I2 into I3.
1334
1335 It we are combining three insns and the resulting insn is not recognized,
1336 try splitting it into two insns. If that happens, I2 and I3 are retained
1337 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1338 are pseudo-deleted.
1339
1340 Return 0 if the combination does not work. Then nothing is changed.
1341 If we did the combination, return the insn at which combine should
1342 resume scanning. */
1343
1344 static rtx
1345 try_combine (i3, i2, i1)
1346 register rtx i3, i2, i1;
1347 {
1348 /* New patterns for I3 and I3, respectively. */
1349 rtx newpat, newi2pat = 0;
1350 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1351 int added_sets_1, added_sets_2;
1352 /* Total number of SETs to put into I3. */
1353 int total_sets;
1354 /* Nonzero is I2's body now appears in I3. */
1355 int i2_is_used;
1356 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1357 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1358 /* Contains I3 if the destination of I3 is used in its source, which means
1359 that the old life of I3 is being killed. If that usage is placed into
1360 I2 and not in I3, a REG_DEAD note must be made. */
1361 rtx i3dest_killed = 0;
1362 /* SET_DEST and SET_SRC of I2 and I1. */
1363 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1364 /* PATTERN (I2), or a copy of it in certain cases. */
1365 rtx i2pat;
1366 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1367 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1368 int i1_feeds_i3 = 0;
1369 /* Notes that must be added to REG_NOTES in I3 and I2. */
1370 rtx new_i3_notes, new_i2_notes;
1371 /* Notes that we substituted I3 into I2 instead of the normal case. */
1372 int i3_subst_into_i2 = 0;
1373 /* Notes that I1, I2 or I3 is a MULT operation. */
1374 int have_mult = 0;
1375
1376 int maxreg;
1377 rtx temp;
1378 register rtx link;
1379 int i;
1380
1381 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1382 This can occur when flow deletes an insn that it has merged into an
1383 auto-increment address. We also can't do anything if I3 has a
1384 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1385 libcall. */
1386
1387 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1388 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1389 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1390 #if 0
1391 /* ??? This gives worse code, and appears to be unnecessary, since no
1392 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1393 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1394 #endif
1395 )
1396 return 0;
1397
1398 combine_attempts++;
1399
1400 undobuf.undos = undobuf.previous_undos = 0;
1401 undobuf.other_insn = 0;
1402
1403 /* Save the current high-water-mark so we can free storage if we didn't
1404 accept this combination. */
1405 undobuf.storage = (char *) oballoc (0);
1406
1407 /* Reset the hard register usage information. */
1408 CLEAR_HARD_REG_SET (newpat_used_regs);
1409
1410 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1411 code below, set I1 to be the earlier of the two insns. */
1412 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1413 temp = i1, i1 = i2, i2 = temp;
1414
1415 added_links_insn = 0;
1416
1417 /* First check for one important special-case that the code below will
1418 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1419 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1420 we may be able to replace that destination with the destination of I3.
1421 This occurs in the common code where we compute both a quotient and
1422 remainder into a structure, in which case we want to do the computation
1423 directly into the structure to avoid register-register copies.
1424
1425 We make very conservative checks below and only try to handle the
1426 most common cases of this. For example, we only handle the case
1427 where I2 and I3 are adjacent to avoid making difficult register
1428 usage tests. */
1429
1430 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1431 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1432 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1433 && (! SMALL_REGISTER_CLASSES
1434 || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1435 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1436 || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
1437 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1438 && GET_CODE (PATTERN (i2)) == PARALLEL
1439 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1440 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1441 below would need to check what is inside (and reg_overlap_mentioned_p
1442 doesn't support those codes anyway). Don't allow those destinations;
1443 the resulting insn isn't likely to be recognized anyway. */
1444 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1445 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1446 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1447 SET_DEST (PATTERN (i3)))
1448 && next_real_insn (i2) == i3)
1449 {
1450 rtx p2 = PATTERN (i2);
1451
1452 /* Make sure that the destination of I3,
1453 which we are going to substitute into one output of I2,
1454 is not used within another output of I2. We must avoid making this:
1455 (parallel [(set (mem (reg 69)) ...)
1456 (set (reg 69) ...)])
1457 which is not well-defined as to order of actions.
1458 (Besides, reload can't handle output reloads for this.)
1459
1460 The problem can also happen if the dest of I3 is a memory ref,
1461 if another dest in I2 is an indirect memory ref. */
1462 for (i = 0; i < XVECLEN (p2, 0); i++)
1463 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1464 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1465 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1466 SET_DEST (XVECEXP (p2, 0, i))))
1467 break;
1468
1469 if (i == XVECLEN (p2, 0))
1470 for (i = 0; i < XVECLEN (p2, 0); i++)
1471 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1472 {
1473 combine_merges++;
1474
1475 subst_insn = i3;
1476 subst_low_cuid = INSN_CUID (i2);
1477
1478 added_sets_2 = added_sets_1 = 0;
1479 i2dest = SET_SRC (PATTERN (i3));
1480
1481 /* Replace the dest in I2 with our dest and make the resulting
1482 insn the new pattern for I3. Then skip to where we
1483 validate the pattern. Everything was set up above. */
1484 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1485 SET_DEST (PATTERN (i3)));
1486
1487 newpat = p2;
1488 i3_subst_into_i2 = 1;
1489 goto validate_replacement;
1490 }
1491 }
1492
1493 #ifndef HAVE_cc0
1494 /* If we have no I1 and I2 looks like:
1495 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1496 (set Y OP)])
1497 make up a dummy I1 that is
1498 (set Y OP)
1499 and change I2 to be
1500 (set (reg:CC X) (compare:CC Y (const_int 0)))
1501
1502 (We can ignore any trailing CLOBBERs.)
1503
1504 This undoes a previous combination and allows us to match a branch-and-
1505 decrement insn. */
1506
1507 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1508 && XVECLEN (PATTERN (i2), 0) >= 2
1509 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1510 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1511 == MODE_CC)
1512 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1513 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1514 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1515 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1516 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1517 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1518 {
1519 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1520 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1521 break;
1522
1523 if (i == 1)
1524 {
1525 /* We make I1 with the same INSN_UID as I2. This gives it
1526 the same INSN_CUID for value tracking. Our fake I1 will
1527 never appear in the insn stream so giving it the same INSN_UID
1528 as I2 will not cause a problem. */
1529
1530 subst_prev_insn = i1
1531 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1532 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1533 NULL_RTX);
1534
1535 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1536 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1537 SET_DEST (PATTERN (i1)));
1538 }
1539 }
1540 #endif
1541
1542 /* Verify that I2 and I1 are valid for combining. */
1543 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1544 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1545 {
1546 undo_all ();
1547 return 0;
1548 }
1549
1550 /* Record whether I2DEST is used in I2SRC and similarly for the other
1551 cases. Knowing this will help in register status updating below. */
1552 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1553 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1554 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1555
1556 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1557 in I2SRC. */
1558 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1559
1560 /* Ensure that I3's pattern can be the destination of combines. */
1561 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1562 i1 && i2dest_in_i1src && i1_feeds_i3,
1563 &i3dest_killed))
1564 {
1565 undo_all ();
1566 return 0;
1567 }
1568
1569 /* See if any of the insns is a MULT operation. Unless one is, we will
1570 reject a combination that is, since it must be slower. Be conservative
1571 here. */
1572 if (GET_CODE (i2src) == MULT
1573 || (i1 != 0 && GET_CODE (i1src) == MULT)
1574 || (GET_CODE (PATTERN (i3)) == SET
1575 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1576 have_mult = 1;
1577
1578 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1579 We used to do this EXCEPT in one case: I3 has a post-inc in an
1580 output operand. However, that exception can give rise to insns like
1581 mov r3,(r3)+
1582 which is a famous insn on the PDP-11 where the value of r3 used as the
1583 source was model-dependent. Avoid this sort of thing. */
1584
1585 #if 0
1586 if (!(GET_CODE (PATTERN (i3)) == SET
1587 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1588 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1589 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1590 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1591 /* It's not the exception. */
1592 #endif
1593 #ifdef AUTO_INC_DEC
1594 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1595 if (REG_NOTE_KIND (link) == REG_INC
1596 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1597 || (i1 != 0
1598 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1599 {
1600 undo_all ();
1601 return 0;
1602 }
1603 #endif
1604
1605 /* See if the SETs in I1 or I2 need to be kept around in the merged
1606 instruction: whenever the value set there is still needed past I3.
1607 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1608
1609 For the SET in I1, we have two cases: If I1 and I2 independently
1610 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1611 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1612 in I1 needs to be kept around unless I1DEST dies or is set in either
1613 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1614 I1DEST. If so, we know I1 feeds into I2. */
1615
1616 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1617
1618 added_sets_1
1619 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1620 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1621
1622 /* If the set in I2 needs to be kept around, we must make a copy of
1623 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1624 PATTERN (I2), we are only substituting for the original I1DEST, not into
1625 an already-substituted copy. This also prevents making self-referential
1626 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1627 I2DEST. */
1628
1629 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1630 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1631 : PATTERN (i2));
1632
1633 if (added_sets_2)
1634 i2pat = copy_rtx (i2pat);
1635
1636 combine_merges++;
1637
1638 /* Substitute in the latest insn for the regs set by the earlier ones. */
1639
1640 maxreg = max_reg_num ();
1641
1642 subst_insn = i3;
1643
1644 /* It is possible that the source of I2 or I1 may be performing an
1645 unneeded operation, such as a ZERO_EXTEND of something that is known
1646 to have the high part zero. Handle that case by letting subst look at
1647 the innermost one of them.
1648
1649 Another way to do this would be to have a function that tries to
1650 simplify a single insn instead of merging two or more insns. We don't
1651 do this because of the potential of infinite loops and because
1652 of the potential extra memory required. However, doing it the way
1653 we are is a bit of a kludge and doesn't catch all cases.
1654
1655 But only do this if -fexpensive-optimizations since it slows things down
1656 and doesn't usually win. */
1657
1658 if (flag_expensive_optimizations)
1659 {
1660 /* Pass pc_rtx so no substitutions are done, just simplifications.
1661 The cases that we are interested in here do not involve the few
1662 cases were is_replaced is checked. */
1663 if (i1)
1664 {
1665 subst_low_cuid = INSN_CUID (i1);
1666 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1667 }
1668 else
1669 {
1670 subst_low_cuid = INSN_CUID (i2);
1671 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1672 }
1673
1674 undobuf.previous_undos = undobuf.undos;
1675 }
1676
1677 #ifndef HAVE_cc0
1678 /* Many machines that don't use CC0 have insns that can both perform an
1679 arithmetic operation and set the condition code. These operations will
1680 be represented as a PARALLEL with the first element of the vector
1681 being a COMPARE of an arithmetic operation with the constant zero.
1682 The second element of the vector will set some pseudo to the result
1683 of the same arithmetic operation. If we simplify the COMPARE, we won't
1684 match such a pattern and so will generate an extra insn. Here we test
1685 for this case, where both the comparison and the operation result are
1686 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1687 I2SRC. Later we will make the PARALLEL that contains I2. */
1688
1689 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1690 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1691 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1692 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1693 {
1694 #ifdef EXTRA_CC_MODES
1695 rtx *cc_use;
1696 enum machine_mode compare_mode;
1697 #endif
1698
1699 newpat = PATTERN (i3);
1700 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1701
1702 i2_is_used = 1;
1703
1704 #ifdef EXTRA_CC_MODES
1705 /* See if a COMPARE with the operand we substituted in should be done
1706 with the mode that is currently being used. If not, do the same
1707 processing we do in `subst' for a SET; namely, if the destination
1708 is used only once, try to replace it with a register of the proper
1709 mode and also replace the COMPARE. */
1710 if (undobuf.other_insn == 0
1711 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1712 &undobuf.other_insn))
1713 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1714 i2src, const0_rtx))
1715 != GET_MODE (SET_DEST (newpat))))
1716 {
1717 int regno = REGNO (SET_DEST (newpat));
1718 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1719
1720 if (regno < FIRST_PSEUDO_REGISTER
1721 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1722 && ! REG_USERVAR_P (SET_DEST (newpat))))
1723 {
1724 if (regno >= FIRST_PSEUDO_REGISTER)
1725 SUBST (regno_reg_rtx[regno], new_dest);
1726
1727 SUBST (SET_DEST (newpat), new_dest);
1728 SUBST (XEXP (*cc_use, 0), new_dest);
1729 SUBST (SET_SRC (newpat),
1730 gen_rtx_combine (COMPARE, compare_mode,
1731 i2src, const0_rtx));
1732 }
1733 else
1734 undobuf.other_insn = 0;
1735 }
1736 #endif
1737 }
1738 else
1739 #endif
1740 {
1741 n_occurrences = 0; /* `subst' counts here */
1742
1743 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1744 need to make a unique copy of I2SRC each time we substitute it
1745 to avoid self-referential rtl. */
1746
1747 subst_low_cuid = INSN_CUID (i2);
1748 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1749 ! i1_feeds_i3 && i1dest_in_i1src);
1750 undobuf.previous_undos = undobuf.undos;
1751
1752 /* Record whether i2's body now appears within i3's body. */
1753 i2_is_used = n_occurrences;
1754 }
1755
1756 /* If we already got a failure, don't try to do more. Otherwise,
1757 try to substitute in I1 if we have it. */
1758
1759 if (i1 && GET_CODE (newpat) != CLOBBER)
1760 {
1761 /* Before we can do this substitution, we must redo the test done
1762 above (see detailed comments there) that ensures that I1DEST
1763 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1764
1765 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1766 0, NULL_PTR))
1767 {
1768 undo_all ();
1769 return 0;
1770 }
1771
1772 n_occurrences = 0;
1773 subst_low_cuid = INSN_CUID (i1);
1774 newpat = subst (newpat, i1dest, i1src, 0, 0);
1775 undobuf.previous_undos = undobuf.undos;
1776 }
1777
1778 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1779 to count all the ways that I2SRC and I1SRC can be used. */
1780 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1781 && i2_is_used + added_sets_2 > 1)
1782 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1783 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1784 > 1))
1785 /* Fail if we tried to make a new register (we used to abort, but there's
1786 really no reason to). */
1787 || max_reg_num () != maxreg
1788 /* Fail if we couldn't do something and have a CLOBBER. */
1789 || GET_CODE (newpat) == CLOBBER
1790 /* Fail if this new pattern is a MULT and we didn't have one before
1791 at the outer level. */
1792 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1793 && ! have_mult))
1794 {
1795 undo_all ();
1796 return 0;
1797 }
1798
1799 /* If the actions of the earlier insns must be kept
1800 in addition to substituting them into the latest one,
1801 we must make a new PARALLEL for the latest insn
1802 to hold additional the SETs. */
1803
1804 if (added_sets_1 || added_sets_2)
1805 {
1806 combine_extras++;
1807
1808 if (GET_CODE (newpat) == PARALLEL)
1809 {
1810 rtvec old = XVEC (newpat, 0);
1811 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1812 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1813 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1814 sizeof (old->elem[0]) * old->num_elem);
1815 }
1816 else
1817 {
1818 rtx old = newpat;
1819 total_sets = 1 + added_sets_1 + added_sets_2;
1820 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1821 XVECEXP (newpat, 0, 0) = old;
1822 }
1823
1824 if (added_sets_1)
1825 XVECEXP (newpat, 0, --total_sets)
1826 = (GET_CODE (PATTERN (i1)) == PARALLEL
1827 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1828
1829 if (added_sets_2)
1830 {
1831 /* If there is no I1, use I2's body as is. We used to also not do
1832 the subst call below if I2 was substituted into I3,
1833 but that could lose a simplification. */
1834 if (i1 == 0)
1835 XVECEXP (newpat, 0, --total_sets) = i2pat;
1836 else
1837 /* See comment where i2pat is assigned. */
1838 XVECEXP (newpat, 0, --total_sets)
1839 = subst (i2pat, i1dest, i1src, 0, 0);
1840 }
1841 }
1842
1843 /* We come here when we are replacing a destination in I2 with the
1844 destination of I3. */
1845 validate_replacement:
1846
1847 /* Note which hard regs this insn has as inputs. */
1848 mark_used_regs_combine (newpat);
1849
1850 /* Is the result of combination a valid instruction? */
1851 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1852
1853 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1854 the second SET's destination is a register that is unused. In that case,
1855 we just need the first SET. This can occur when simplifying a divmod
1856 insn. We *must* test for this case here because the code below that
1857 splits two independent SETs doesn't handle this case correctly when it
1858 updates the register status. Also check the case where the first
1859 SET's destination is unused. That would not cause incorrect code, but
1860 does cause an unneeded insn to remain. */
1861
1862 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1863 && XVECLEN (newpat, 0) == 2
1864 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1865 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1866 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1867 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1868 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1869 && asm_noperands (newpat) < 0)
1870 {
1871 newpat = XVECEXP (newpat, 0, 0);
1872 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1873 }
1874
1875 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1876 && XVECLEN (newpat, 0) == 2
1877 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1878 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1879 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1880 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1881 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1882 && asm_noperands (newpat) < 0)
1883 {
1884 newpat = XVECEXP (newpat, 0, 1);
1885 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1886 }
1887
1888 /* If we were combining three insns and the result is a simple SET
1889 with no ASM_OPERANDS that wasn't recognized, try to split it into two
1890 insns. There are two ways to do this. It can be split using a
1891 machine-specific method (like when you have an addition of a large
1892 constant) or by combine in the function find_split_point. */
1893
1894 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1895 && asm_noperands (newpat) < 0)
1896 {
1897 rtx m_split, *split;
1898 rtx ni2dest = i2dest;
1899
1900 /* See if the MD file can split NEWPAT. If it can't, see if letting it
1901 use I2DEST as a scratch register will help. In the latter case,
1902 convert I2DEST to the mode of the source of NEWPAT if we can. */
1903
1904 m_split = split_insns (newpat, i3);
1905
1906 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1907 inputs of NEWPAT. */
1908
1909 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1910 possible to try that as a scratch reg. This would require adding
1911 more code to make it work though. */
1912
1913 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1914 {
1915 /* If I2DEST is a hard register or the only use of a pseudo,
1916 we can change its mode. */
1917 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1918 && GET_MODE (SET_DEST (newpat)) != VOIDmode
1919 && GET_CODE (i2dest) == REG
1920 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1921 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1922 && ! REG_USERVAR_P (i2dest))))
1923 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
1924 REGNO (i2dest));
1925
1926 m_split = split_insns (gen_rtx_PARALLEL
1927 (VOIDmode,
1928 gen_rtvec (2, newpat,
1929 gen_rtx_CLOBBER (VOIDmode,
1930 ni2dest))),
1931 i3);
1932 }
1933
1934 if (m_split && GET_CODE (m_split) == SEQUENCE
1935 && XVECLEN (m_split, 0) == 2
1936 && (next_real_insn (i2) == i3
1937 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1938 INSN_CUID (i2))))
1939 {
1940 rtx i2set, i3set;
1941 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1942 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1943
1944 i3set = single_set (XVECEXP (m_split, 0, 1));
1945 i2set = single_set (XVECEXP (m_split, 0, 0));
1946
1947 /* In case we changed the mode of I2DEST, replace it in the
1948 pseudo-register table here. We can't do it above in case this
1949 code doesn't get executed and we do a split the other way. */
1950
1951 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1952 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1953
1954 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1955
1956 /* If I2 or I3 has multiple SETs, we won't know how to track
1957 register status, so don't use these insns. If I2's destination
1958 is used between I2 and I3, we also can't use these insns. */
1959
1960 if (i2_code_number >= 0 && i2set && i3set
1961 && (next_real_insn (i2) == i3
1962 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1963 insn_code_number = recog_for_combine (&newi3pat, i3,
1964 &new_i3_notes);
1965 if (insn_code_number >= 0)
1966 newpat = newi3pat;
1967
1968 /* It is possible that both insns now set the destination of I3.
1969 If so, we must show an extra use of it. */
1970
1971 if (insn_code_number >= 0)
1972 {
1973 rtx new_i3_dest = SET_DEST (i3set);
1974 rtx new_i2_dest = SET_DEST (i2set);
1975
1976 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1977 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1978 || GET_CODE (new_i3_dest) == SUBREG)
1979 new_i3_dest = XEXP (new_i3_dest, 0);
1980
1981 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1982 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1983 || GET_CODE (new_i2_dest) == SUBREG)
1984 new_i2_dest = XEXP (new_i2_dest, 0);
1985
1986 if (GET_CODE (new_i3_dest) == REG
1987 && GET_CODE (new_i2_dest) == REG
1988 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1989 REG_N_SETS (REGNO (new_i2_dest))++;
1990 }
1991 }
1992
1993 /* If we can split it and use I2DEST, go ahead and see if that
1994 helps things be recognized. Verify that none of the registers
1995 are set between I2 and I3. */
1996 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1997 #ifdef HAVE_cc0
1998 && GET_CODE (i2dest) == REG
1999 #endif
2000 /* We need I2DEST in the proper mode. If it is a hard register
2001 or the only use of a pseudo, we can change its mode. */
2002 && (GET_MODE (*split) == GET_MODE (i2dest)
2003 || GET_MODE (*split) == VOIDmode
2004 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2005 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2006 && ! REG_USERVAR_P (i2dest)))
2007 && (next_real_insn (i2) == i3
2008 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2009 /* We can't overwrite I2DEST if its value is still used by
2010 NEWPAT. */
2011 && ! reg_referenced_p (i2dest, newpat))
2012 {
2013 rtx newdest = i2dest;
2014 enum rtx_code split_code = GET_CODE (*split);
2015 enum machine_mode split_mode = GET_MODE (*split);
2016
2017 /* Get NEWDEST as a register in the proper mode. We have already
2018 validated that we can do this. */
2019 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2020 {
2021 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2022
2023 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2024 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2025 }
2026
2027 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2028 an ASHIFT. This can occur if it was inside a PLUS and hence
2029 appeared to be a memory address. This is a kludge. */
2030 if (split_code == MULT
2031 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2032 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2033 {
2034 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2035 XEXP (*split, 0), GEN_INT (i)));
2036 /* Update split_code because we may not have a multiply
2037 anymore. */
2038 split_code = GET_CODE (*split);
2039 }
2040
2041 #ifdef INSN_SCHEDULING
2042 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2043 be written as a ZERO_EXTEND. */
2044 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2045 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2046 XEXP (*split, 0)));
2047 #endif
2048
2049 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2050 SUBST (*split, newdest);
2051 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2052
2053 /* If the split point was a MULT and we didn't have one before,
2054 don't use one now. */
2055 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2056 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2057 }
2058 }
2059
2060 /* Check for a case where we loaded from memory in a narrow mode and
2061 then sign extended it, but we need both registers. In that case,
2062 we have a PARALLEL with both loads from the same memory location.
2063 We can split this into a load from memory followed by a register-register
2064 copy. This saves at least one insn, more if register allocation can
2065 eliminate the copy.
2066
2067 We cannot do this if the destination of the second assignment is
2068 a register that we have already assumed is zero-extended. Similarly
2069 for a SUBREG of such a register. */
2070
2071 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2072 && GET_CODE (newpat) == PARALLEL
2073 && XVECLEN (newpat, 0) == 2
2074 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2075 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2076 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2077 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2078 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2079 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2080 INSN_CUID (i2))
2081 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2082 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2083 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2084 (GET_CODE (temp) == REG
2085 && reg_nonzero_bits[REGNO (temp)] != 0
2086 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2087 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2088 && (reg_nonzero_bits[REGNO (temp)]
2089 != GET_MODE_MASK (word_mode))))
2090 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2091 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2092 (GET_CODE (temp) == REG
2093 && reg_nonzero_bits[REGNO (temp)] != 0
2094 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2095 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2096 && (reg_nonzero_bits[REGNO (temp)]
2097 != GET_MODE_MASK (word_mode)))))
2098 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2099 SET_SRC (XVECEXP (newpat, 0, 1)))
2100 && ! find_reg_note (i3, REG_UNUSED,
2101 SET_DEST (XVECEXP (newpat, 0, 0))))
2102 {
2103 rtx ni2dest;
2104
2105 newi2pat = XVECEXP (newpat, 0, 0);
2106 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2107 newpat = XVECEXP (newpat, 0, 1);
2108 SUBST (SET_SRC (newpat),
2109 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2110 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2111
2112 if (i2_code_number >= 0)
2113 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2114
2115 if (insn_code_number >= 0)
2116 {
2117 rtx insn;
2118 rtx link;
2119
2120 /* If we will be able to accept this, we have made a change to the
2121 destination of I3. This can invalidate a LOG_LINKS pointing
2122 to I3. No other part of combine.c makes such a transformation.
2123
2124 The new I3 will have a destination that was previously the
2125 destination of I1 or I2 and which was used in i2 or I3. Call
2126 distribute_links to make a LOG_LINK from the next use of
2127 that destination. */
2128
2129 PATTERN (i3) = newpat;
2130 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2131
2132 /* I3 now uses what used to be its destination and which is
2133 now I2's destination. That means we need a LOG_LINK from
2134 I3 to I2. But we used to have one, so we still will.
2135
2136 However, some later insn might be using I2's dest and have
2137 a LOG_LINK pointing at I3. We must remove this link.
2138 The simplest way to remove the link is to point it at I1,
2139 which we know will be a NOTE. */
2140
2141 for (insn = NEXT_INSN (i3);
2142 insn && (this_basic_block == n_basic_blocks - 1
2143 || insn != BLOCK_HEAD (this_basic_block + 1));
2144 insn = NEXT_INSN (insn))
2145 {
2146 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2147 && reg_referenced_p (ni2dest, PATTERN (insn)))
2148 {
2149 for (link = LOG_LINKS (insn); link;
2150 link = XEXP (link, 1))
2151 if (XEXP (link, 0) == i3)
2152 XEXP (link, 0) = i1;
2153
2154 break;
2155 }
2156 }
2157 }
2158 }
2159
2160 /* Similarly, check for a case where we have a PARALLEL of two independent
2161 SETs but we started with three insns. In this case, we can do the sets
2162 as two separate insns. This case occurs when some SET allows two
2163 other insns to combine, but the destination of that SET is still live. */
2164
2165 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2166 && GET_CODE (newpat) == PARALLEL
2167 && XVECLEN (newpat, 0) == 2
2168 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2169 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2170 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2171 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2172 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2173 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2174 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2175 INSN_CUID (i2))
2176 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2177 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2178 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2179 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2180 XVECEXP (newpat, 0, 0))
2181 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2182 XVECEXP (newpat, 0, 1)))
2183 {
2184 /* Normally, it doesn't matter which of the two is done first,
2185 but it does if one references cc0. In that case, it has to
2186 be first. */
2187 #ifdef HAVE_cc0
2188 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2189 {
2190 newi2pat = XVECEXP (newpat, 0, 0);
2191 newpat = XVECEXP (newpat, 0, 1);
2192 }
2193 else
2194 #endif
2195 {
2196 newi2pat = XVECEXP (newpat, 0, 1);
2197 newpat = XVECEXP (newpat, 0, 0);
2198 }
2199
2200 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2201
2202 if (i2_code_number >= 0)
2203 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2204 }
2205
2206 /* If it still isn't recognized, fail and change things back the way they
2207 were. */
2208 if ((insn_code_number < 0
2209 /* Is the result a reasonable ASM_OPERANDS? */
2210 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2211 {
2212 undo_all ();
2213 return 0;
2214 }
2215
2216 /* If we had to change another insn, make sure it is valid also. */
2217 if (undobuf.other_insn)
2218 {
2219 rtx other_pat = PATTERN (undobuf.other_insn);
2220 rtx new_other_notes;
2221 rtx note, next;
2222
2223 CLEAR_HARD_REG_SET (newpat_used_regs);
2224
2225 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2226 &new_other_notes);
2227
2228 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2229 {
2230 undo_all ();
2231 return 0;
2232 }
2233
2234 PATTERN (undobuf.other_insn) = other_pat;
2235
2236 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2237 are still valid. Then add any non-duplicate notes added by
2238 recog_for_combine. */
2239 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2240 {
2241 next = XEXP (note, 1);
2242
2243 if (REG_NOTE_KIND (note) == REG_UNUSED
2244 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2245 {
2246 if (GET_CODE (XEXP (note, 0)) == REG)
2247 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2248
2249 remove_note (undobuf.other_insn, note);
2250 }
2251 }
2252
2253 for (note = new_other_notes; note; note = XEXP (note, 1))
2254 if (GET_CODE (XEXP (note, 0)) == REG)
2255 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2256
2257 distribute_notes (new_other_notes, undobuf.other_insn,
2258 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2259 }
2260
2261 /* We now know that we can do this combination. Merge the insns and
2262 update the status of registers and LOG_LINKS. */
2263
2264 {
2265 rtx i3notes, i2notes, i1notes = 0;
2266 rtx i3links, i2links, i1links = 0;
2267 rtx midnotes = 0;
2268 register int regno;
2269 /* Compute which registers we expect to eliminate. newi2pat may be setting
2270 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2271 same as i3dest, in which case newi2pat may be setting i1dest. */
2272 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2273 || i2dest_in_i2src || i2dest_in_i1src
2274 ? 0 : i2dest);
2275 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2276 || (newi2pat && reg_set_p (i1dest, newi2pat))
2277 ? 0 : i1dest);
2278
2279 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2280 clear them. */
2281 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2282 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2283 if (i1)
2284 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2285
2286 /* Ensure that we do not have something that should not be shared but
2287 occurs multiple times in the new insns. Check this by first
2288 resetting all the `used' flags and then copying anything is shared. */
2289
2290 reset_used_flags (i3notes);
2291 reset_used_flags (i2notes);
2292 reset_used_flags (i1notes);
2293 reset_used_flags (newpat);
2294 reset_used_flags (newi2pat);
2295 if (undobuf.other_insn)
2296 reset_used_flags (PATTERN (undobuf.other_insn));
2297
2298 i3notes = copy_rtx_if_shared (i3notes);
2299 i2notes = copy_rtx_if_shared (i2notes);
2300 i1notes = copy_rtx_if_shared (i1notes);
2301 newpat = copy_rtx_if_shared (newpat);
2302 newi2pat = copy_rtx_if_shared (newi2pat);
2303 if (undobuf.other_insn)
2304 reset_used_flags (PATTERN (undobuf.other_insn));
2305
2306 INSN_CODE (i3) = insn_code_number;
2307 PATTERN (i3) = newpat;
2308 if (undobuf.other_insn)
2309 INSN_CODE (undobuf.other_insn) = other_code_number;
2310
2311 /* We had one special case above where I2 had more than one set and
2312 we replaced a destination of one of those sets with the destination
2313 of I3. In that case, we have to update LOG_LINKS of insns later
2314 in this basic block. Note that this (expensive) case is rare.
2315
2316 Also, in this case, we must pretend that all REG_NOTEs for I2
2317 actually came from I3, so that REG_UNUSED notes from I2 will be
2318 properly handled. */
2319
2320 if (i3_subst_into_i2)
2321 {
2322 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2323 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2324 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2325 && ! find_reg_note (i2, REG_UNUSED,
2326 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2327 for (temp = NEXT_INSN (i2);
2328 temp && (this_basic_block == n_basic_blocks - 1
2329 || BLOCK_HEAD (this_basic_block) != temp);
2330 temp = NEXT_INSN (temp))
2331 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2332 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2333 if (XEXP (link, 0) == i2)
2334 XEXP (link, 0) = i3;
2335
2336 if (i3notes)
2337 {
2338 rtx link = i3notes;
2339 while (XEXP (link, 1))
2340 link = XEXP (link, 1);
2341 XEXP (link, 1) = i2notes;
2342 }
2343 else
2344 i3notes = i2notes;
2345 i2notes = 0;
2346 }
2347
2348 LOG_LINKS (i3) = 0;
2349 REG_NOTES (i3) = 0;
2350 LOG_LINKS (i2) = 0;
2351 REG_NOTES (i2) = 0;
2352
2353 if (newi2pat)
2354 {
2355 INSN_CODE (i2) = i2_code_number;
2356 PATTERN (i2) = newi2pat;
2357 }
2358 else
2359 {
2360 PUT_CODE (i2, NOTE);
2361 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2362 NOTE_SOURCE_FILE (i2) = 0;
2363 }
2364
2365 if (i1)
2366 {
2367 LOG_LINKS (i1) = 0;
2368 REG_NOTES (i1) = 0;
2369 PUT_CODE (i1, NOTE);
2370 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2371 NOTE_SOURCE_FILE (i1) = 0;
2372 }
2373
2374 /* Get death notes for everything that is now used in either I3 or
2375 I2 and used to die in a previous insn. If we built two new
2376 patterns, move from I1 to I2 then I2 to I3 so that we get the
2377 proper movement on registers that I2 modifies. */
2378
2379 if (newi2pat)
2380 {
2381 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2382 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2383 }
2384 else
2385 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2386 i3, &midnotes);
2387
2388 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2389 if (i3notes)
2390 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2391 elim_i2, elim_i1);
2392 if (i2notes)
2393 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2394 elim_i2, elim_i1);
2395 if (i1notes)
2396 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2397 elim_i2, elim_i1);
2398 if (midnotes)
2399 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2400 elim_i2, elim_i1);
2401
2402 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2403 know these are REG_UNUSED and want them to go to the desired insn,
2404 so we always pass it as i3. We have not counted the notes in
2405 reg_n_deaths yet, so we need to do so now. */
2406
2407 if (newi2pat && new_i2_notes)
2408 {
2409 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2410 if (GET_CODE (XEXP (temp, 0)) == REG)
2411 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2412
2413 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2414 }
2415
2416 if (new_i3_notes)
2417 {
2418 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2419 if (GET_CODE (XEXP (temp, 0)) == REG)
2420 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2421
2422 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2423 }
2424
2425 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2426 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2427 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2428 in that case, it might delete I2. Similarly for I2 and I1.
2429 Show an additional death due to the REG_DEAD note we make here. If
2430 we discard it in distribute_notes, we will decrement it again. */
2431
2432 if (i3dest_killed)
2433 {
2434 if (GET_CODE (i3dest_killed) == REG)
2435 REG_N_DEATHS (REGNO (i3dest_killed))++;
2436
2437 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2438 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2439 NULL_RTX),
2440 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2441 else
2442 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2443 NULL_RTX),
2444 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2445 elim_i2, elim_i1);
2446 }
2447
2448 if (i2dest_in_i2src)
2449 {
2450 if (GET_CODE (i2dest) == REG)
2451 REG_N_DEATHS (REGNO (i2dest))++;
2452
2453 if (newi2pat && reg_set_p (i2dest, newi2pat))
2454 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2455 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2456 else
2457 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2458 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2459 NULL_RTX, NULL_RTX);
2460 }
2461
2462 if (i1dest_in_i1src)
2463 {
2464 if (GET_CODE (i1dest) == REG)
2465 REG_N_DEATHS (REGNO (i1dest))++;
2466
2467 if (newi2pat && reg_set_p (i1dest, newi2pat))
2468 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2469 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2470 else
2471 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2472 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2473 NULL_RTX, NULL_RTX);
2474 }
2475
2476 distribute_links (i3links);
2477 distribute_links (i2links);
2478 distribute_links (i1links);
2479
2480 if (GET_CODE (i2dest) == REG)
2481 {
2482 rtx link;
2483 rtx i2_insn = 0, i2_val = 0, set;
2484
2485 /* The insn that used to set this register doesn't exist, and
2486 this life of the register may not exist either. See if one of
2487 I3's links points to an insn that sets I2DEST. If it does,
2488 that is now the last known value for I2DEST. If we don't update
2489 this and I2 set the register to a value that depended on its old
2490 contents, we will get confused. If this insn is used, thing
2491 will be set correctly in combine_instructions. */
2492
2493 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2494 if ((set = single_set (XEXP (link, 0))) != 0
2495 && rtx_equal_p (i2dest, SET_DEST (set)))
2496 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2497
2498 record_value_for_reg (i2dest, i2_insn, i2_val);
2499
2500 /* If the reg formerly set in I2 died only once and that was in I3,
2501 zero its use count so it won't make `reload' do any work. */
2502 if (! added_sets_2
2503 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2504 && ! i2dest_in_i2src)
2505 {
2506 regno = REGNO (i2dest);
2507 REG_N_SETS (regno)--;
2508 if (REG_N_SETS (regno) == 0
2509 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2510 regno))
2511 REG_N_REFS (regno) = 0;
2512 }
2513 }
2514
2515 if (i1 && GET_CODE (i1dest) == REG)
2516 {
2517 rtx link;
2518 rtx i1_insn = 0, i1_val = 0, set;
2519
2520 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2521 if ((set = single_set (XEXP (link, 0))) != 0
2522 && rtx_equal_p (i1dest, SET_DEST (set)))
2523 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2524
2525 record_value_for_reg (i1dest, i1_insn, i1_val);
2526
2527 regno = REGNO (i1dest);
2528 if (! added_sets_1 && ! i1dest_in_i1src)
2529 {
2530 REG_N_SETS (regno)--;
2531 if (REG_N_SETS (regno) == 0
2532 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2533 regno))
2534 REG_N_REFS (regno) = 0;
2535 }
2536 }
2537
2538 /* Update reg_nonzero_bits et al for any changes that may have been made
2539 to this insn. */
2540
2541 note_stores (newpat, set_nonzero_bits_and_sign_copies);
2542 if (newi2pat)
2543 note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2544
2545 /* If I3 is now an unconditional jump, ensure that it has a
2546 BARRIER following it since it may have initially been a
2547 conditional jump. It may also be the last nonnote insn. */
2548
2549 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2550 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2551 || GET_CODE (temp) != BARRIER))
2552 emit_barrier_after (i3);
2553 }
2554
2555 combine_successes++;
2556
2557 /* Clear this here, so that subsequent get_last_value calls are not
2558 affected. */
2559 subst_prev_insn = NULL_RTX;
2560
2561 if (added_links_insn
2562 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2563 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2564 return added_links_insn;
2565 else
2566 return newi2pat ? i2 : i3;
2567 }
2568 \f
2569 /* Undo all the modifications recorded in undobuf. */
2570
2571 static void
2572 undo_all ()
2573 {
2574 struct undo *undo, *next;
2575
2576 for (undo = undobuf.undos; undo; undo = next)
2577 {
2578 next = undo->next;
2579 if (undo->is_int)
2580 *undo->where.i = undo->old_contents.i;
2581 else
2582 *undo->where.r = undo->old_contents.r;
2583
2584 undo->next = undobuf.frees;
2585 undobuf.frees = undo;
2586 }
2587
2588 obfree (undobuf.storage);
2589 undobuf.undos = undobuf.previous_undos = 0;
2590
2591 /* Clear this here, so that subsequent get_last_value calls are not
2592 affected. */
2593 subst_prev_insn = NULL_RTX;
2594 }
2595 \f
2596 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2597 where we have an arithmetic expression and return that point. LOC will
2598 be inside INSN.
2599
2600 try_combine will call this function to see if an insn can be split into
2601 two insns. */
2602
2603 static rtx *
2604 find_split_point (loc, insn)
2605 rtx *loc;
2606 rtx insn;
2607 {
2608 rtx x = *loc;
2609 enum rtx_code code = GET_CODE (x);
2610 rtx *split;
2611 int len = 0, pos = 0, unsignedp = 0;
2612 rtx inner = NULL_RTX;
2613
2614 /* First special-case some codes. */
2615 switch (code)
2616 {
2617 case SUBREG:
2618 #ifdef INSN_SCHEDULING
2619 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2620 point. */
2621 if (GET_CODE (SUBREG_REG (x)) == MEM)
2622 return loc;
2623 #endif
2624 return find_split_point (&SUBREG_REG (x), insn);
2625
2626 case MEM:
2627 #ifdef HAVE_lo_sum
2628 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2629 using LO_SUM and HIGH. */
2630 if (GET_CODE (XEXP (x, 0)) == CONST
2631 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2632 {
2633 SUBST (XEXP (x, 0),
2634 gen_rtx_combine (LO_SUM, Pmode,
2635 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2636 XEXP (x, 0)));
2637 return &XEXP (XEXP (x, 0), 0);
2638 }
2639 #endif
2640
2641 /* If we have a PLUS whose second operand is a constant and the
2642 address is not valid, perhaps will can split it up using
2643 the machine-specific way to split large constants. We use
2644 the first pseudo-reg (one of the virtual regs) as a placeholder;
2645 it will not remain in the result. */
2646 if (GET_CODE (XEXP (x, 0)) == PLUS
2647 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2648 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2649 {
2650 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2651 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2652 subst_insn);
2653
2654 /* This should have produced two insns, each of which sets our
2655 placeholder. If the source of the second is a valid address,
2656 we can make put both sources together and make a split point
2657 in the middle. */
2658
2659 if (seq && XVECLEN (seq, 0) == 2
2660 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2661 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2662 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2663 && ! reg_mentioned_p (reg,
2664 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2665 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2666 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2667 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2668 && memory_address_p (GET_MODE (x),
2669 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2670 {
2671 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2672 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2673
2674 /* Replace the placeholder in SRC2 with SRC1. If we can
2675 find where in SRC2 it was placed, that can become our
2676 split point and we can replace this address with SRC2.
2677 Just try two obvious places. */
2678
2679 src2 = replace_rtx (src2, reg, src1);
2680 split = 0;
2681 if (XEXP (src2, 0) == src1)
2682 split = &XEXP (src2, 0);
2683 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2684 && XEXP (XEXP (src2, 0), 0) == src1)
2685 split = &XEXP (XEXP (src2, 0), 0);
2686
2687 if (split)
2688 {
2689 SUBST (XEXP (x, 0), src2);
2690 return split;
2691 }
2692 }
2693
2694 /* If that didn't work, perhaps the first operand is complex and
2695 needs to be computed separately, so make a split point there.
2696 This will occur on machines that just support REG + CONST
2697 and have a constant moved through some previous computation. */
2698
2699 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2700 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2701 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2702 == 'o')))
2703 return &XEXP (XEXP (x, 0), 0);
2704 }
2705 break;
2706
2707 case SET:
2708 #ifdef HAVE_cc0
2709 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2710 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2711 we need to put the operand into a register. So split at that
2712 point. */
2713
2714 if (SET_DEST (x) == cc0_rtx
2715 && GET_CODE (SET_SRC (x)) != COMPARE
2716 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2717 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2718 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2719 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2720 return &SET_SRC (x);
2721 #endif
2722
2723 /* See if we can split SET_SRC as it stands. */
2724 split = find_split_point (&SET_SRC (x), insn);
2725 if (split && split != &SET_SRC (x))
2726 return split;
2727
2728 /* See if we can split SET_DEST as it stands. */
2729 split = find_split_point (&SET_DEST (x), insn);
2730 if (split && split != &SET_DEST (x))
2731 return split;
2732
2733 /* See if this is a bitfield assignment with everything constant. If
2734 so, this is an IOR of an AND, so split it into that. */
2735 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2736 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2737 <= HOST_BITS_PER_WIDE_INT)
2738 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2739 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2740 && GET_CODE (SET_SRC (x)) == CONST_INT
2741 && ((INTVAL (XEXP (SET_DEST (x), 1))
2742 + INTVAL (XEXP (SET_DEST (x), 2)))
2743 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2744 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2745 {
2746 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2747 int len = INTVAL (XEXP (SET_DEST (x), 1));
2748 int src = INTVAL (SET_SRC (x));
2749 rtx dest = XEXP (SET_DEST (x), 0);
2750 enum machine_mode mode = GET_MODE (dest);
2751 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2752
2753 if (BITS_BIG_ENDIAN)
2754 pos = GET_MODE_BITSIZE (mode) - len - pos;
2755
2756 if ((unsigned HOST_WIDE_INT) src == mask)
2757 SUBST (SET_SRC (x),
2758 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2759 else
2760 SUBST (SET_SRC (x),
2761 gen_binary (IOR, mode,
2762 gen_binary (AND, mode, dest,
2763 GEN_INT (~ (mask << pos)
2764 & GET_MODE_MASK (mode))),
2765 GEN_INT (src << pos)));
2766
2767 SUBST (SET_DEST (x), dest);
2768
2769 split = find_split_point (&SET_SRC (x), insn);
2770 if (split && split != &SET_SRC (x))
2771 return split;
2772 }
2773
2774 /* Otherwise, see if this is an operation that we can split into two.
2775 If so, try to split that. */
2776 code = GET_CODE (SET_SRC (x));
2777
2778 switch (code)
2779 {
2780 case AND:
2781 /* If we are AND'ing with a large constant that is only a single
2782 bit and the result is only being used in a context where we
2783 need to know if it is zero or non-zero, replace it with a bit
2784 extraction. This will avoid the large constant, which might
2785 have taken more than one insn to make. If the constant were
2786 not a valid argument to the AND but took only one insn to make,
2787 this is no worse, but if it took more than one insn, it will
2788 be better. */
2789
2790 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2791 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2792 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2793 && GET_CODE (SET_DEST (x)) == REG
2794 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2795 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2796 && XEXP (*split, 0) == SET_DEST (x)
2797 && XEXP (*split, 1) == const0_rtx)
2798 {
2799 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2800 XEXP (SET_SRC (x), 0),
2801 pos, NULL_RTX, 1, 1, 0, 0);
2802 if (extraction != 0)
2803 {
2804 SUBST (SET_SRC (x), extraction);
2805 return find_split_point (loc, insn);
2806 }
2807 }
2808 break;
2809
2810 case NE:
2811 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2812 is known to be on, this can be converted into a NEG of a shift. */
2813 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2814 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2815 && 1 <= (pos = exact_log2
2816 (nonzero_bits (XEXP (SET_SRC (x), 0),
2817 GET_MODE (XEXP (SET_SRC (x), 0))))))
2818 {
2819 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2820
2821 SUBST (SET_SRC (x),
2822 gen_rtx_combine (NEG, mode,
2823 gen_rtx_combine (LSHIFTRT, mode,
2824 XEXP (SET_SRC (x), 0),
2825 GEN_INT (pos))));
2826
2827 split = find_split_point (&SET_SRC (x), insn);
2828 if (split && split != &SET_SRC (x))
2829 return split;
2830 }
2831 break;
2832
2833 case SIGN_EXTEND:
2834 inner = XEXP (SET_SRC (x), 0);
2835
2836 /* We can't optimize if either mode is a partial integer
2837 mode as we don't know how many bits are significant
2838 in those modes. */
2839 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2840 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2841 break;
2842
2843 pos = 0;
2844 len = GET_MODE_BITSIZE (GET_MODE (inner));
2845 unsignedp = 0;
2846 break;
2847
2848 case SIGN_EXTRACT:
2849 case ZERO_EXTRACT:
2850 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2851 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2852 {
2853 inner = XEXP (SET_SRC (x), 0);
2854 len = INTVAL (XEXP (SET_SRC (x), 1));
2855 pos = INTVAL (XEXP (SET_SRC (x), 2));
2856
2857 if (BITS_BIG_ENDIAN)
2858 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2859 unsignedp = (code == ZERO_EXTRACT);
2860 }
2861 break;
2862
2863 default:
2864 break;
2865 }
2866
2867 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2868 {
2869 enum machine_mode mode = GET_MODE (SET_SRC (x));
2870
2871 /* For unsigned, we have a choice of a shift followed by an
2872 AND or two shifts. Use two shifts for field sizes where the
2873 constant might be too large. We assume here that we can
2874 always at least get 8-bit constants in an AND insn, which is
2875 true for every current RISC. */
2876
2877 if (unsignedp && len <= 8)
2878 {
2879 SUBST (SET_SRC (x),
2880 gen_rtx_combine
2881 (AND, mode,
2882 gen_rtx_combine (LSHIFTRT, mode,
2883 gen_lowpart_for_combine (mode, inner),
2884 GEN_INT (pos)),
2885 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2886
2887 split = find_split_point (&SET_SRC (x), insn);
2888 if (split && split != &SET_SRC (x))
2889 return split;
2890 }
2891 else
2892 {
2893 SUBST (SET_SRC (x),
2894 gen_rtx_combine
2895 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2896 gen_rtx_combine (ASHIFT, mode,
2897 gen_lowpart_for_combine (mode, inner),
2898 GEN_INT (GET_MODE_BITSIZE (mode)
2899 - len - pos)),
2900 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2901
2902 split = find_split_point (&SET_SRC (x), insn);
2903 if (split && split != &SET_SRC (x))
2904 return split;
2905 }
2906 }
2907
2908 /* See if this is a simple operation with a constant as the second
2909 operand. It might be that this constant is out of range and hence
2910 could be used as a split point. */
2911 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2912 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2913 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2914 && CONSTANT_P (XEXP (SET_SRC (x), 1))
2915 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2916 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2917 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2918 == 'o'))))
2919 return &XEXP (SET_SRC (x), 1);
2920
2921 /* Finally, see if this is a simple operation with its first operand
2922 not in a register. The operation might require this operand in a
2923 register, so return it as a split point. We can always do this
2924 because if the first operand were another operation, we would have
2925 already found it as a split point. */
2926 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2927 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2928 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2929 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2930 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2931 return &XEXP (SET_SRC (x), 0);
2932
2933 return 0;
2934
2935 case AND:
2936 case IOR:
2937 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2938 it is better to write this as (not (ior A B)) so we can split it.
2939 Similarly for IOR. */
2940 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2941 {
2942 SUBST (*loc,
2943 gen_rtx_combine (NOT, GET_MODE (x),
2944 gen_rtx_combine (code == IOR ? AND : IOR,
2945 GET_MODE (x),
2946 XEXP (XEXP (x, 0), 0),
2947 XEXP (XEXP (x, 1), 0))));
2948 return find_split_point (loc, insn);
2949 }
2950
2951 /* Many RISC machines have a large set of logical insns. If the
2952 second operand is a NOT, put it first so we will try to split the
2953 other operand first. */
2954 if (GET_CODE (XEXP (x, 1)) == NOT)
2955 {
2956 rtx tem = XEXP (x, 0);
2957 SUBST (XEXP (x, 0), XEXP (x, 1));
2958 SUBST (XEXP (x, 1), tem);
2959 }
2960 break;
2961
2962 default:
2963 break;
2964 }
2965
2966 /* Otherwise, select our actions depending on our rtx class. */
2967 switch (GET_RTX_CLASS (code))
2968 {
2969 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
2970 case '3':
2971 split = find_split_point (&XEXP (x, 2), insn);
2972 if (split)
2973 return split;
2974 /* ... fall through ... */
2975 case '2':
2976 case 'c':
2977 case '<':
2978 split = find_split_point (&XEXP (x, 1), insn);
2979 if (split)
2980 return split;
2981 /* ... fall through ... */
2982 case '1':
2983 /* Some machines have (and (shift ...) ...) insns. If X is not
2984 an AND, but XEXP (X, 0) is, use it as our split point. */
2985 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2986 return &XEXP (x, 0);
2987
2988 split = find_split_point (&XEXP (x, 0), insn);
2989 if (split)
2990 return split;
2991 return loc;
2992 }
2993
2994 /* Otherwise, we don't have a split point. */
2995 return 0;
2996 }
2997 \f
2998 /* Throughout X, replace FROM with TO, and return the result.
2999 The result is TO if X is FROM;
3000 otherwise the result is X, but its contents may have been modified.
3001 If they were modified, a record was made in undobuf so that
3002 undo_all will (among other things) return X to its original state.
3003
3004 If the number of changes necessary is too much to record to undo,
3005 the excess changes are not made, so the result is invalid.
3006 The changes already made can still be undone.
3007 undobuf.num_undo is incremented for such changes, so by testing that
3008 the caller can tell whether the result is valid.
3009
3010 `n_occurrences' is incremented each time FROM is replaced.
3011
3012 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
3013
3014 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
3015 by copying if `n_occurrences' is non-zero. */
3016
3017 static rtx
3018 subst (x, from, to, in_dest, unique_copy)
3019 register rtx x, from, to;
3020 int in_dest;
3021 int unique_copy;
3022 {
3023 register enum rtx_code code = GET_CODE (x);
3024 enum machine_mode op0_mode = VOIDmode;
3025 register const char *fmt;
3026 register int len, i;
3027 rtx new;
3028
3029 /* Two expressions are equal if they are identical copies of a shared
3030 RTX or if they are both registers with the same register number
3031 and mode. */
3032
3033 #define COMBINE_RTX_EQUAL_P(X,Y) \
3034 ((X) == (Y) \
3035 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3036 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3037
3038 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3039 {
3040 n_occurrences++;
3041 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3042 }
3043
3044 /* If X and FROM are the same register but different modes, they will
3045 not have been seen as equal above. However, flow.c will make a
3046 LOG_LINKS entry for that case. If we do nothing, we will try to
3047 rerecognize our original insn and, when it succeeds, we will
3048 delete the feeding insn, which is incorrect.
3049
3050 So force this insn not to match in this (rare) case. */
3051 if (! in_dest && code == REG && GET_CODE (from) == REG
3052 && REGNO (x) == REGNO (from))
3053 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3054
3055 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3056 of which may contain things that can be combined. */
3057 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3058 return x;
3059
3060 /* It is possible to have a subexpression appear twice in the insn.
3061 Suppose that FROM is a register that appears within TO.
3062 Then, after that subexpression has been scanned once by `subst',
3063 the second time it is scanned, TO may be found. If we were
3064 to scan TO here, we would find FROM within it and create a
3065 self-referent rtl structure which is completely wrong. */
3066 if (COMBINE_RTX_EQUAL_P (x, to))
3067 return to;
3068
3069 /* Parallel asm_operands need special attention because all of the
3070 inputs are shared across the arms. Furthermore, unsharing the
3071 rtl results in recognition failures. Failure to handle this case
3072 specially can result in circular rtl.
3073
3074 Solve this by doing a normal pass across the first entry of the
3075 parallel, and only processing the SET_DESTs of the subsequent
3076 entries. Ug. */
3077
3078 if (code == PARALLEL
3079 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3080 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3081 {
3082 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3083
3084 /* If this substitution failed, this whole thing fails. */
3085 if (GET_CODE (new) == CLOBBER
3086 && XEXP (new, 0) == const0_rtx)
3087 return new;
3088
3089 SUBST (XVECEXP (x, 0, 0), new);
3090
3091 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3092 {
3093 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3094
3095 if (GET_CODE (dest) != REG
3096 && GET_CODE (dest) != CC0
3097 && GET_CODE (dest) != PC)
3098 {
3099 new = subst (dest, from, to, 0, unique_copy);
3100
3101 /* If this substitution failed, this whole thing fails. */
3102 if (GET_CODE (new) == CLOBBER
3103 && XEXP (new, 0) == const0_rtx)
3104 return new;
3105
3106 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3107 }
3108 }
3109 }
3110 else
3111 {
3112 len = GET_RTX_LENGTH (code);
3113 fmt = GET_RTX_FORMAT (code);
3114
3115 /* We don't need to process a SET_DEST that is a register, CC0,
3116 or PC, so set up to skip this common case. All other cases
3117 where we want to suppress replacing something inside a
3118 SET_SRC are handled via the IN_DEST operand. */
3119 if (code == SET
3120 && (GET_CODE (SET_DEST (x)) == REG
3121 || GET_CODE (SET_DEST (x)) == CC0
3122 || GET_CODE (SET_DEST (x)) == PC))
3123 fmt = "ie";
3124
3125 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3126 constant. */
3127 if (fmt[0] == 'e')
3128 op0_mode = GET_MODE (XEXP (x, 0));
3129
3130 for (i = 0; i < len; i++)
3131 {
3132 if (fmt[i] == 'E')
3133 {
3134 register int j;
3135 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3136 {
3137 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3138 {
3139 new = (unique_copy && n_occurrences
3140 ? copy_rtx (to) : to);
3141 n_occurrences++;
3142 }
3143 else
3144 {
3145 new = subst (XVECEXP (x, i, j), from, to, 0,
3146 unique_copy);
3147
3148 /* If this substitution failed, this whole thing
3149 fails. */
3150 if (GET_CODE (new) == CLOBBER
3151 && XEXP (new, 0) == const0_rtx)
3152 return new;
3153 }
3154
3155 SUBST (XVECEXP (x, i, j), new);
3156 }
3157 }
3158 else if (fmt[i] == 'e')
3159 {
3160 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3161 {
3162 /* In general, don't install a subreg involving two
3163 modes not tieable. It can worsen register
3164 allocation, and can even make invalid reload
3165 insns, since the reg inside may need to be copied
3166 from in the outside mode, and that may be invalid
3167 if it is an fp reg copied in integer mode.
3168
3169 We allow two exceptions to this: It is valid if
3170 it is inside another SUBREG and the mode of that
3171 SUBREG and the mode of the inside of TO is
3172 tieable and it is valid if X is a SET that copies
3173 FROM to CC0. */
3174
3175 if (GET_CODE (to) == SUBREG
3176 && ! MODES_TIEABLE_P (GET_MODE (to),
3177 GET_MODE (SUBREG_REG (to)))
3178 && ! (code == SUBREG
3179 && MODES_TIEABLE_P (GET_MODE (x),
3180 GET_MODE (SUBREG_REG (to))))
3181 #ifdef HAVE_cc0
3182 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3183 #endif
3184 )
3185 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3186
3187 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3188 n_occurrences++;
3189 }
3190 else
3191 /* If we are in a SET_DEST, suppress most cases unless we
3192 have gone inside a MEM, in which case we want to
3193 simplify the address. We assume here that things that
3194 are actually part of the destination have their inner
3195 parts in the first expression. This is true for SUBREG,
3196 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3197 things aside from REG and MEM that should appear in a
3198 SET_DEST. */
3199 new = subst (XEXP (x, i), from, to,
3200 (((in_dest
3201 && (code == SUBREG || code == STRICT_LOW_PART
3202 || code == ZERO_EXTRACT))
3203 || code == SET)
3204 && i == 0), unique_copy);
3205
3206 /* If we found that we will have to reject this combination,
3207 indicate that by returning the CLOBBER ourselves, rather than
3208 an expression containing it. This will speed things up as
3209 well as prevent accidents where two CLOBBERs are considered
3210 to be equal, thus producing an incorrect simplification. */
3211
3212 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3213 return new;
3214
3215 SUBST (XEXP (x, i), new);
3216 }
3217 }
3218 }
3219
3220 /* Try to simplify X. If the simplification changed the code, it is likely
3221 that further simplification will help, so loop, but limit the number
3222 of repetitions that will be performed. */
3223
3224 for (i = 0; i < 4; i++)
3225 {
3226 /* If X is sufficiently simple, don't bother trying to do anything
3227 with it. */
3228 if (code != CONST_INT && code != REG && code != CLOBBER)
3229 x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3230
3231 if (GET_CODE (x) == code)
3232 break;
3233
3234 code = GET_CODE (x);
3235
3236 /* We no longer know the original mode of operand 0 since we
3237 have changed the form of X) */
3238 op0_mode = VOIDmode;
3239 }
3240
3241 return x;
3242 }
3243 \f
3244 /* Simplify X, a piece of RTL. We just operate on the expression at the
3245 outer level; call `subst' to simplify recursively. Return the new
3246 expression.
3247
3248 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3249 will be the iteration even if an expression with a code different from
3250 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3251
3252 static rtx
3253 simplify_rtx (x, op0_mode, last, in_dest)
3254 rtx x;
3255 enum machine_mode op0_mode;
3256 int last;
3257 int in_dest;
3258 {
3259 enum rtx_code code = GET_CODE (x);
3260 enum machine_mode mode = GET_MODE (x);
3261 rtx temp;
3262 int i;
3263
3264 /* If this is a commutative operation, put a constant last and a complex
3265 expression first. We don't need to do this for comparisons here. */
3266 if (GET_RTX_CLASS (code) == 'c'
3267 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3268 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3269 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3270 || (GET_CODE (XEXP (x, 0)) == SUBREG
3271 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3272 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3273 {
3274 temp = XEXP (x, 0);
3275 SUBST (XEXP (x, 0), XEXP (x, 1));
3276 SUBST (XEXP (x, 1), temp);
3277 }
3278
3279 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3280 sign extension of a PLUS with a constant, reverse the order of the sign
3281 extension and the addition. Note that this not the same as the original
3282 code, but overflow is undefined for signed values. Also note that the
3283 PLUS will have been partially moved "inside" the sign-extension, so that
3284 the first operand of X will really look like:
3285 (ashiftrt (plus (ashift A C4) C5) C4).
3286 We convert this to
3287 (plus (ashiftrt (ashift A C4) C2) C4)
3288 and replace the first operand of X with that expression. Later parts
3289 of this function may simplify the expression further.
3290
3291 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3292 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3293 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3294
3295 We do this to simplify address expressions. */
3296
3297 if ((code == PLUS || code == MINUS || code == MULT)
3298 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3299 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3300 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3301 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3302 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3303 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3304 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3305 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3306 XEXP (XEXP (XEXP (x, 0), 0), 1),
3307 XEXP (XEXP (x, 0), 1))) != 0)
3308 {
3309 rtx new
3310 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3311 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3312 INTVAL (XEXP (XEXP (x, 0), 1)));
3313
3314 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3315 INTVAL (XEXP (XEXP (x, 0), 1)));
3316
3317 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3318 }
3319
3320 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3321 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3322 things. Check for cases where both arms are testing the same
3323 condition.
3324
3325 Don't do anything if all operands are very simple. */
3326
3327 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3328 || GET_RTX_CLASS (code) == '<')
3329 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3330 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3331 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3332 == 'o')))
3333 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3334 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3335 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3336 == 'o')))))
3337 || (GET_RTX_CLASS (code) == '1'
3338 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3339 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3340 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3341 == 'o'))))))
3342 {
3343 rtx cond, true, false;
3344
3345 cond = if_then_else_cond (x, &true, &false);
3346 if (cond != 0
3347 /* If everything is a comparison, what we have is highly unlikely
3348 to be simpler, so don't use it. */
3349 && ! (GET_RTX_CLASS (code) == '<'
3350 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3351 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3352 {
3353 rtx cop1 = const0_rtx;
3354 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3355
3356 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3357 return x;
3358
3359 /* Simplify the alternative arms; this may collapse the true and
3360 false arms to store-flag values. */
3361 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3362 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3363
3364 /* Restarting if we generate a store-flag expression will cause
3365 us to loop. Just drop through in this case. */
3366
3367 /* If the result values are STORE_FLAG_VALUE and zero, we can
3368 just make the comparison operation. */
3369 if (true == const_true_rtx && false == const0_rtx)
3370 x = gen_binary (cond_code, mode, cond, cop1);
3371 else if (true == const0_rtx && false == const_true_rtx)
3372 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3373
3374 /* Likewise, we can make the negate of a comparison operation
3375 if the result values are - STORE_FLAG_VALUE and zero. */
3376 else if (GET_CODE (true) == CONST_INT
3377 && INTVAL (true) == - STORE_FLAG_VALUE
3378 && false == const0_rtx)
3379 x = gen_unary (NEG, mode, mode,
3380 gen_binary (cond_code, mode, cond, cop1));
3381 else if (GET_CODE (false) == CONST_INT
3382 && INTVAL (false) == - STORE_FLAG_VALUE
3383 && true == const0_rtx)
3384 x = gen_unary (NEG, mode, mode,
3385 gen_binary (reverse_condition (cond_code),
3386 mode, cond, cop1));
3387 else
3388 return gen_rtx_IF_THEN_ELSE (mode,
3389 gen_binary (cond_code, VOIDmode,
3390 cond, cop1),
3391 true, false);
3392
3393 code = GET_CODE (x);
3394 op0_mode = VOIDmode;
3395 }
3396 }
3397
3398 /* Try to fold this expression in case we have constants that weren't
3399 present before. */
3400 temp = 0;
3401 switch (GET_RTX_CLASS (code))
3402 {
3403 case '1':
3404 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3405 break;
3406 case '<':
3407 temp = simplify_relational_operation (code, op0_mode,
3408 XEXP (x, 0), XEXP (x, 1));
3409 #ifdef FLOAT_STORE_FLAG_VALUE
3410 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3411 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3412 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3413 #endif
3414 break;
3415 case 'c':
3416 case '2':
3417 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3418 break;
3419 case 'b':
3420 case '3':
3421 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3422 XEXP (x, 1), XEXP (x, 2));
3423 break;
3424 }
3425
3426 if (temp)
3427 x = temp, code = GET_CODE (temp);
3428
3429 /* First see if we can apply the inverse distributive law. */
3430 if (code == PLUS || code == MINUS
3431 || code == AND || code == IOR || code == XOR)
3432 {
3433 x = apply_distributive_law (x);
3434 code = GET_CODE (x);
3435 }
3436
3437 /* If CODE is an associative operation not otherwise handled, see if we
3438 can associate some operands. This can win if they are constants or
3439 if they are logically related (i.e. (a & b) & a. */
3440 if ((code == PLUS || code == MINUS
3441 || code == MULT || code == AND || code == IOR || code == XOR
3442 || code == DIV || code == UDIV
3443 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3444 && INTEGRAL_MODE_P (mode))
3445 {
3446 if (GET_CODE (XEXP (x, 0)) == code)
3447 {
3448 rtx other = XEXP (XEXP (x, 0), 0);
3449 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3450 rtx inner_op1 = XEXP (x, 1);
3451 rtx inner;
3452
3453 /* Make sure we pass the constant operand if any as the second
3454 one if this is a commutative operation. */
3455 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3456 {
3457 rtx tem = inner_op0;
3458 inner_op0 = inner_op1;
3459 inner_op1 = tem;
3460 }
3461 inner = simplify_binary_operation (code == MINUS ? PLUS
3462 : code == DIV ? MULT
3463 : code == UDIV ? MULT
3464 : code,
3465 mode, inner_op0, inner_op1);
3466
3467 /* For commutative operations, try the other pair if that one
3468 didn't simplify. */
3469 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3470 {
3471 other = XEXP (XEXP (x, 0), 1);
3472 inner = simplify_binary_operation (code, mode,
3473 XEXP (XEXP (x, 0), 0),
3474 XEXP (x, 1));
3475 }
3476
3477 if (inner)
3478 return gen_binary (code, mode, other, inner);
3479 }
3480 }
3481
3482 /* A little bit of algebraic simplification here. */
3483 switch (code)
3484 {
3485 case MEM:
3486 /* Ensure that our address has any ASHIFTs converted to MULT in case
3487 address-recognizing predicates are called later. */
3488 temp = make_compound_operation (XEXP (x, 0), MEM);
3489 SUBST (XEXP (x, 0), temp);
3490 break;
3491
3492 case SUBREG:
3493 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3494 is paradoxical. If we can't do that safely, then it becomes
3495 something nonsensical so that this combination won't take place. */
3496
3497 if (GET_CODE (SUBREG_REG (x)) == MEM
3498 && (GET_MODE_SIZE (mode)
3499 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3500 {
3501 rtx inner = SUBREG_REG (x);
3502 int endian_offset = 0;
3503 /* Don't change the mode of the MEM
3504 if that would change the meaning of the address. */
3505 if (MEM_VOLATILE_P (SUBREG_REG (x))
3506 || mode_dependent_address_p (XEXP (inner, 0)))
3507 return gen_rtx_CLOBBER (mode, const0_rtx);
3508
3509 if (BYTES_BIG_ENDIAN)
3510 {
3511 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3512 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3513 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3514 endian_offset -= (UNITS_PER_WORD
3515 - GET_MODE_SIZE (GET_MODE (inner)));
3516 }
3517 /* Note if the plus_constant doesn't make a valid address
3518 then this combination won't be accepted. */
3519 x = gen_rtx_MEM (mode,
3520 plus_constant (XEXP (inner, 0),
3521 (SUBREG_WORD (x) * UNITS_PER_WORD
3522 + endian_offset)));
3523 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3524 MEM_COPY_ATTRIBUTES (x, inner);
3525 return x;
3526 }
3527
3528 /* If we are in a SET_DEST, these other cases can't apply. */
3529 if (in_dest)
3530 return x;
3531
3532 /* Changing mode twice with SUBREG => just change it once,
3533 or not at all if changing back to starting mode. */
3534 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3535 {
3536 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3537 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3538 return SUBREG_REG (SUBREG_REG (x));
3539
3540 SUBST_INT (SUBREG_WORD (x),
3541 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3542 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3543 }
3544
3545 /* SUBREG of a hard register => just change the register number
3546 and/or mode. If the hard register is not valid in that mode,
3547 suppress this combination. If the hard register is the stack,
3548 frame, or argument pointer, leave this as a SUBREG. */
3549
3550 if (GET_CODE (SUBREG_REG (x)) == REG
3551 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3552 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3553 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3554 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3555 #endif
3556 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3557 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3558 #endif
3559 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3560 {
3561 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3562 mode))
3563 return gen_rtx_REG (mode,
3564 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3565 else
3566 return gen_rtx_CLOBBER (mode, const0_rtx);
3567 }
3568
3569 /* For a constant, try to pick up the part we want. Handle a full
3570 word and low-order part. Only do this if we are narrowing
3571 the constant; if it is being widened, we have no idea what
3572 the extra bits will have been set to. */
3573
3574 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3575 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3576 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3577 && GET_MODE_CLASS (mode) == MODE_INT)
3578 {
3579 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3580 0, op0_mode);
3581 if (temp)
3582 return temp;
3583 }
3584
3585 /* If we want a subreg of a constant, at offset 0,
3586 take the low bits. On a little-endian machine, that's
3587 always valid. On a big-endian machine, it's valid
3588 only if the constant's mode fits in one word. Note that we
3589 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3590 if (CONSTANT_P (SUBREG_REG (x))
3591 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3592 || ! WORDS_BIG_ENDIAN)
3593 ? SUBREG_WORD (x) == 0
3594 : (SUBREG_WORD (x)
3595 == ((GET_MODE_SIZE (op0_mode)
3596 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3597 / UNITS_PER_WORD)))
3598 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3599 && (! WORDS_BIG_ENDIAN
3600 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3601 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3602
3603 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3604 since we are saying that the high bits don't matter. */
3605 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3606 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3607 return SUBREG_REG (x);
3608
3609 /* Note that we cannot do any narrowing for non-constants since
3610 we might have been counting on using the fact that some bits were
3611 zero. We now do this in the SET. */
3612
3613 break;
3614
3615 case NOT:
3616 /* (not (plus X -1)) can become (neg X). */
3617 if (GET_CODE (XEXP (x, 0)) == PLUS
3618 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3619 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3620
3621 /* Similarly, (not (neg X)) is (plus X -1). */
3622 if (GET_CODE (XEXP (x, 0)) == NEG)
3623 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3624 constm1_rtx);
3625
3626 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3627 if (GET_CODE (XEXP (x, 0)) == XOR
3628 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3629 && (temp = simplify_unary_operation (NOT, mode,
3630 XEXP (XEXP (x, 0), 1),
3631 mode)) != 0)
3632 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3633
3634 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3635 other than 1, but that is not valid. We could do a similar
3636 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3637 but this doesn't seem common enough to bother with. */
3638 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3639 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3640 return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3641 XEXP (XEXP (x, 0), 1));
3642
3643 if (GET_CODE (XEXP (x, 0)) == SUBREG
3644 && subreg_lowpart_p (XEXP (x, 0))
3645 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3646 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3647 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3648 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3649 {
3650 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3651
3652 x = gen_rtx_ROTATE (inner_mode,
3653 gen_unary (NOT, inner_mode, inner_mode,
3654 const1_rtx),
3655 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3656 return gen_lowpart_for_combine (mode, x);
3657 }
3658
3659 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3660 reversing the comparison code if valid. */
3661 if (STORE_FLAG_VALUE == -1
3662 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3663 && reversible_comparison_p (XEXP (x, 0)))
3664 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3665 mode, XEXP (XEXP (x, 0), 0),
3666 XEXP (XEXP (x, 0), 1));
3667
3668 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3669 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3670 perform the above simplification. */
3671
3672 if (STORE_FLAG_VALUE == -1
3673 && XEXP (x, 1) == const1_rtx
3674 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3675 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3676 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3677 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3678
3679 /* Apply De Morgan's laws to reduce number of patterns for machines
3680 with negating logical insns (and-not, nand, etc.). If result has
3681 only one NOT, put it first, since that is how the patterns are
3682 coded. */
3683
3684 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3685 {
3686 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3687
3688 if (GET_CODE (in1) == NOT)
3689 in1 = XEXP (in1, 0);
3690 else
3691 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3692
3693 if (GET_CODE (in2) == NOT)
3694 in2 = XEXP (in2, 0);
3695 else if (GET_CODE (in2) == CONST_INT
3696 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3697 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3698 else
3699 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3700
3701 if (GET_CODE (in2) == NOT)
3702 {
3703 rtx tem = in2;
3704 in2 = in1; in1 = tem;
3705 }
3706
3707 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3708 mode, in1, in2);
3709 }
3710 break;
3711
3712 case NEG:
3713 /* (neg (plus X 1)) can become (not X). */
3714 if (GET_CODE (XEXP (x, 0)) == PLUS
3715 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3716 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3717
3718 /* Similarly, (neg (not X)) is (plus X 1). */
3719 if (GET_CODE (XEXP (x, 0)) == NOT)
3720 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3721
3722 /* (neg (minus X Y)) can become (minus Y X). */
3723 if (GET_CODE (XEXP (x, 0)) == MINUS
3724 && (! FLOAT_MODE_P (mode)
3725 /* x-y != -(y-x) with IEEE floating point. */
3726 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3727 || flag_fast_math))
3728 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3729 XEXP (XEXP (x, 0), 0));
3730
3731 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3732 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3733 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3734 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3735
3736 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3737 if we can then eliminate the NEG (e.g.,
3738 if the operand is a constant). */
3739
3740 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3741 {
3742 temp = simplify_unary_operation (NEG, mode,
3743 XEXP (XEXP (x, 0), 0), mode);
3744 if (temp)
3745 {
3746 SUBST (XEXP (XEXP (x, 0), 0), temp);
3747 return XEXP (x, 0);
3748 }
3749 }
3750
3751 temp = expand_compound_operation (XEXP (x, 0));
3752
3753 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3754 replaced by (lshiftrt X C). This will convert
3755 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3756
3757 if (GET_CODE (temp) == ASHIFTRT
3758 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3759 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3760 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3761 INTVAL (XEXP (temp, 1)));
3762
3763 /* If X has only a single bit that might be nonzero, say, bit I, convert
3764 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3765 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3766 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3767 or a SUBREG of one since we'd be making the expression more
3768 complex if it was just a register. */
3769
3770 if (GET_CODE (temp) != REG
3771 && ! (GET_CODE (temp) == SUBREG
3772 && GET_CODE (SUBREG_REG (temp)) == REG)
3773 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3774 {
3775 rtx temp1 = simplify_shift_const
3776 (NULL_RTX, ASHIFTRT, mode,
3777 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3778 GET_MODE_BITSIZE (mode) - 1 - i),
3779 GET_MODE_BITSIZE (mode) - 1 - i);
3780
3781 /* If all we did was surround TEMP with the two shifts, we
3782 haven't improved anything, so don't use it. Otherwise,
3783 we are better off with TEMP1. */
3784 if (GET_CODE (temp1) != ASHIFTRT
3785 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3786 || XEXP (XEXP (temp1, 0), 0) != temp)
3787 return temp1;
3788 }
3789 break;
3790
3791 case TRUNCATE:
3792 /* We can't handle truncation to a partial integer mode here
3793 because we don't know the real bitsize of the partial
3794 integer mode. */
3795 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3796 break;
3797
3798 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3799 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3800 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3801 SUBST (XEXP (x, 0),
3802 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3803 GET_MODE_MASK (mode), NULL_RTX, 0));
3804
3805 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3806 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3807 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3808 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3809 return XEXP (XEXP (x, 0), 0);
3810
3811 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3812 (OP:SI foo:SI) if OP is NEG or ABS. */
3813 if ((GET_CODE (XEXP (x, 0)) == ABS
3814 || GET_CODE (XEXP (x, 0)) == NEG)
3815 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3816 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3817 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3818 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3819 XEXP (XEXP (XEXP (x, 0), 0), 0));
3820
3821 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3822 (truncate:SI x). */
3823 if (GET_CODE (XEXP (x, 0)) == SUBREG
3824 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3825 && subreg_lowpart_p (XEXP (x, 0)))
3826 return SUBREG_REG (XEXP (x, 0));
3827
3828 /* If we know that the value is already truncated, we can
3829 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION is
3830 nonzero for the corresponding modes. */
3831 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3832 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3833 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3834 >= GET_MODE_BITSIZE (mode) + 1)
3835 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3836
3837 /* A truncate of a comparison can be replaced with a subreg if
3838 STORE_FLAG_VALUE permits. This is like the previous test,
3839 but it works even if the comparison is done in a mode larger
3840 than HOST_BITS_PER_WIDE_INT. */
3841 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3842 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3843 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3844 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3845
3846 /* Similarly, a truncate of a register whose value is a
3847 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3848 permits. */
3849 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3850 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3851 && (temp = get_last_value (XEXP (x, 0)))
3852 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3853 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3854
3855 break;
3856
3857 case FLOAT_TRUNCATE:
3858 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3859 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3860 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3861 return XEXP (XEXP (x, 0), 0);
3862
3863 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3864 (OP:SF foo:SF) if OP is NEG or ABS. */
3865 if ((GET_CODE (XEXP (x, 0)) == ABS
3866 || GET_CODE (XEXP (x, 0)) == NEG)
3867 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3868 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3869 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3870 XEXP (XEXP (XEXP (x, 0), 0), 0));
3871
3872 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3873 is (float_truncate:SF x). */
3874 if (GET_CODE (XEXP (x, 0)) == SUBREG
3875 && subreg_lowpart_p (XEXP (x, 0))
3876 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3877 return SUBREG_REG (XEXP (x, 0));
3878 break;
3879
3880 #ifdef HAVE_cc0
3881 case COMPARE:
3882 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3883 using cc0, in which case we want to leave it as a COMPARE
3884 so we can distinguish it from a register-register-copy. */
3885 if (XEXP (x, 1) == const0_rtx)
3886 return XEXP (x, 0);
3887
3888 /* In IEEE floating point, x-0 is not the same as x. */
3889 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3890 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3891 || flag_fast_math)
3892 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3893 return XEXP (x, 0);
3894 break;
3895 #endif
3896
3897 case CONST:
3898 /* (const (const X)) can become (const X). Do it this way rather than
3899 returning the inner CONST since CONST can be shared with a
3900 REG_EQUAL note. */
3901 if (GET_CODE (XEXP (x, 0)) == CONST)
3902 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3903 break;
3904
3905 #ifdef HAVE_lo_sum
3906 case LO_SUM:
3907 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
3908 can add in an offset. find_split_point will split this address up
3909 again if it doesn't match. */
3910 if (GET_CODE (XEXP (x, 0)) == HIGH
3911 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3912 return XEXP (x, 1);
3913 break;
3914 #endif
3915
3916 case PLUS:
3917 /* If we have (plus (plus (A const) B)), associate it so that CONST is
3918 outermost. That's because that's the way indexed addresses are
3919 supposed to appear. This code used to check many more cases, but
3920 they are now checked elsewhere. */
3921 if (GET_CODE (XEXP (x, 0)) == PLUS
3922 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3923 return gen_binary (PLUS, mode,
3924 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3925 XEXP (x, 1)),
3926 XEXP (XEXP (x, 0), 1));
3927
3928 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3929 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3930 bit-field and can be replaced by either a sign_extend or a
3931 sign_extract. The `and' may be a zero_extend and the two
3932 <c>, -<c> constants may be reversed. */
3933 if (GET_CODE (XEXP (x, 0)) == XOR
3934 && GET_CODE (XEXP (x, 1)) == CONST_INT
3935 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3936 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3937 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3938 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
3939 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3940 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3941 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3942 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3943 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3944 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3945 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3946 == i + 1))))
3947 return simplify_shift_const
3948 (NULL_RTX, ASHIFTRT, mode,
3949 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3950 XEXP (XEXP (XEXP (x, 0), 0), 0),
3951 GET_MODE_BITSIZE (mode) - (i + 1)),
3952 GET_MODE_BITSIZE (mode) - (i + 1));
3953
3954 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3955 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3956 is 1. This produces better code than the alternative immediately
3957 below. */
3958 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3959 && reversible_comparison_p (XEXP (x, 0))
3960 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3961 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3962 return
3963 gen_unary (NEG, mode, mode,
3964 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3965 mode, XEXP (XEXP (x, 0), 0),
3966 XEXP (XEXP (x, 0), 1)));
3967
3968 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3969 can become (ashiftrt (ashift (xor x 1) C) C) where C is
3970 the bitsize of the mode - 1. This allows simplification of
3971 "a = (b & 8) == 0;" */
3972 if (XEXP (x, 1) == constm1_rtx
3973 && GET_CODE (XEXP (x, 0)) != REG
3974 && ! (GET_CODE (XEXP (x,0)) == SUBREG
3975 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3976 && nonzero_bits (XEXP (x, 0), mode) == 1)
3977 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3978 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3979 gen_rtx_combine (XOR, mode,
3980 XEXP (x, 0), const1_rtx),
3981 GET_MODE_BITSIZE (mode) - 1),
3982 GET_MODE_BITSIZE (mode) - 1);
3983
3984 /* If we are adding two things that have no bits in common, convert
3985 the addition into an IOR. This will often be further simplified,
3986 for example in cases like ((a & 1) + (a & 2)), which can
3987 become a & 3. */
3988
3989 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3990 && (nonzero_bits (XEXP (x, 0), mode)
3991 & nonzero_bits (XEXP (x, 1), mode)) == 0)
3992 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3993 break;
3994
3995 case MINUS:
3996 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3997 by reversing the comparison code if valid. */
3998 if (STORE_FLAG_VALUE == 1
3999 && XEXP (x, 0) == const1_rtx
4000 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4001 && reversible_comparison_p (XEXP (x, 1)))
4002 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
4003 mode, XEXP (XEXP (x, 1), 0),
4004 XEXP (XEXP (x, 1), 1));
4005
4006 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4007 (and <foo> (const_int pow2-1)) */
4008 if (GET_CODE (XEXP (x, 1)) == AND
4009 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4010 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4011 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4012 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4013 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4014
4015 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4016 integers. */
4017 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4018 return gen_binary (MINUS, mode,
4019 gen_binary (MINUS, mode, XEXP (x, 0),
4020 XEXP (XEXP (x, 1), 0)),
4021 XEXP (XEXP (x, 1), 1));
4022 break;
4023
4024 case MULT:
4025 /* If we have (mult (plus A B) C), apply the distributive law and then
4026 the inverse distributive law to see if things simplify. This
4027 occurs mostly in addresses, often when unrolling loops. */
4028
4029 if (GET_CODE (XEXP (x, 0)) == PLUS)
4030 {
4031 x = apply_distributive_law
4032 (gen_binary (PLUS, mode,
4033 gen_binary (MULT, mode,
4034 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4035 gen_binary (MULT, mode,
4036 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4037
4038 if (GET_CODE (x) != MULT)
4039 return x;
4040 }
4041 break;
4042
4043 case UDIV:
4044 /* If this is a divide by a power of two, treat it as a shift if
4045 its first operand is a shift. */
4046 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4047 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4048 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4049 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4050 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4051 || GET_CODE (XEXP (x, 0)) == ROTATE
4052 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4053 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4054 break;
4055
4056 case EQ: case NE:
4057 case GT: case GTU: case GE: case GEU:
4058 case LT: case LTU: case LE: case LEU:
4059 /* If the first operand is a condition code, we can't do anything
4060 with it. */
4061 if (GET_CODE (XEXP (x, 0)) == COMPARE
4062 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4063 #ifdef HAVE_cc0
4064 && XEXP (x, 0) != cc0_rtx
4065 #endif
4066 ))
4067 {
4068 rtx op0 = XEXP (x, 0);
4069 rtx op1 = XEXP (x, 1);
4070 enum rtx_code new_code;
4071
4072 if (GET_CODE (op0) == COMPARE)
4073 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4074
4075 /* Simplify our comparison, if possible. */
4076 new_code = simplify_comparison (code, &op0, &op1);
4077
4078 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4079 if only the low-order bit is possibly nonzero in X (such as when
4080 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4081 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4082 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4083 (plus X 1).
4084
4085 Remove any ZERO_EXTRACT we made when thinking this was a
4086 comparison. It may now be simpler to use, e.g., an AND. If a
4087 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4088 the call to make_compound_operation in the SET case. */
4089
4090 if (STORE_FLAG_VALUE == 1
4091 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4092 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
4093 return gen_lowpart_for_combine (mode,
4094 expand_compound_operation (op0));
4095
4096 else if (STORE_FLAG_VALUE == 1
4097 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4098 && op1 == const0_rtx
4099 && (num_sign_bit_copies (op0, mode)
4100 == GET_MODE_BITSIZE (mode)))
4101 {
4102 op0 = expand_compound_operation (op0);
4103 return gen_unary (NEG, mode, mode,
4104 gen_lowpart_for_combine (mode, op0));
4105 }
4106
4107 else if (STORE_FLAG_VALUE == 1
4108 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4109 && op1 == const0_rtx
4110 && nonzero_bits (op0, mode) == 1)
4111 {
4112 op0 = expand_compound_operation (op0);
4113 return gen_binary (XOR, mode,
4114 gen_lowpart_for_combine (mode, op0),
4115 const1_rtx);
4116 }
4117
4118 else if (STORE_FLAG_VALUE == 1
4119 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4120 && op1 == const0_rtx
4121 && (num_sign_bit_copies (op0, mode)
4122 == GET_MODE_BITSIZE (mode)))
4123 {
4124 op0 = expand_compound_operation (op0);
4125 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4126 }
4127
4128 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4129 those above. */
4130 if (STORE_FLAG_VALUE == -1
4131 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4132 && op1 == const0_rtx
4133 && (num_sign_bit_copies (op0, mode)
4134 == GET_MODE_BITSIZE (mode)))
4135 return gen_lowpart_for_combine (mode,
4136 expand_compound_operation (op0));
4137
4138 else if (STORE_FLAG_VALUE == -1
4139 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4140 && op1 == const0_rtx
4141 && nonzero_bits (op0, mode) == 1)
4142 {
4143 op0 = expand_compound_operation (op0);
4144 return gen_unary (NEG, mode, mode,
4145 gen_lowpart_for_combine (mode, op0));
4146 }
4147
4148 else if (STORE_FLAG_VALUE == -1
4149 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4150 && op1 == const0_rtx
4151 && (num_sign_bit_copies (op0, mode)
4152 == GET_MODE_BITSIZE (mode)))
4153 {
4154 op0 = expand_compound_operation (op0);
4155 return gen_unary (NOT, mode, mode,
4156 gen_lowpart_for_combine (mode, op0));
4157 }
4158
4159 /* If X is 0/1, (eq X 0) is X-1. */
4160 else if (STORE_FLAG_VALUE == -1
4161 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4162 && op1 == const0_rtx
4163 && nonzero_bits (op0, mode) == 1)
4164 {
4165 op0 = expand_compound_operation (op0);
4166 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4167 }
4168
4169 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4170 one bit that might be nonzero, we can convert (ne x 0) to
4171 (ashift x c) where C puts the bit in the sign bit. Remove any
4172 AND with STORE_FLAG_VALUE when we are done, since we are only
4173 going to test the sign bit. */
4174 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4175 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4176 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4177 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE(mode)-1))
4178 && op1 == const0_rtx
4179 && mode == GET_MODE (op0)
4180 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4181 {
4182 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4183 expand_compound_operation (op0),
4184 GET_MODE_BITSIZE (mode) - 1 - i);
4185 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4186 return XEXP (x, 0);
4187 else
4188 return x;
4189 }
4190
4191 /* If the code changed, return a whole new comparison. */
4192 if (new_code != code)
4193 return gen_rtx_combine (new_code, mode, op0, op1);
4194
4195 /* Otherwise, keep this operation, but maybe change its operands.
4196 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4197 SUBST (XEXP (x, 0), op0);
4198 SUBST (XEXP (x, 1), op1);
4199 }
4200 break;
4201
4202 case IF_THEN_ELSE:
4203 return simplify_if_then_else (x);
4204
4205 case ZERO_EXTRACT:
4206 case SIGN_EXTRACT:
4207 case ZERO_EXTEND:
4208 case SIGN_EXTEND:
4209 /* If we are processing SET_DEST, we are done. */
4210 if (in_dest)
4211 return x;
4212
4213 return expand_compound_operation (x);
4214
4215 case SET:
4216 return simplify_set (x);
4217
4218 case AND:
4219 case IOR:
4220 case XOR:
4221 return simplify_logical (x, last);
4222
4223 case ABS:
4224 /* (abs (neg <foo>)) -> (abs <foo>) */
4225 if (GET_CODE (XEXP (x, 0)) == NEG)
4226 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4227
4228 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4229 do nothing. */
4230 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4231 break;
4232
4233 /* If operand is something known to be positive, ignore the ABS. */
4234 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4235 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4236 <= HOST_BITS_PER_WIDE_INT)
4237 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4238 & ((HOST_WIDE_INT) 1
4239 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4240 == 0)))
4241 return XEXP (x, 0);
4242
4243
4244 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4245 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4246 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4247
4248 break;
4249
4250 case FFS:
4251 /* (ffs (*_extend <X>)) = (ffs <X>) */
4252 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4253 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4254 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4255 break;
4256
4257 case FLOAT:
4258 /* (float (sign_extend <X>)) = (float <X>). */
4259 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4260 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4261 break;
4262
4263 case ASHIFT:
4264 case LSHIFTRT:
4265 case ASHIFTRT:
4266 case ROTATE:
4267 case ROTATERT:
4268 /* If this is a shift by a constant amount, simplify it. */
4269 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4270 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4271 INTVAL (XEXP (x, 1)));
4272
4273 #ifdef SHIFT_COUNT_TRUNCATED
4274 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4275 SUBST (XEXP (x, 1),
4276 force_to_mode (XEXP (x, 1), GET_MODE (x),
4277 ((HOST_WIDE_INT) 1
4278 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4279 - 1,
4280 NULL_RTX, 0));
4281 #endif
4282
4283 break;
4284
4285 default:
4286 break;
4287 }
4288
4289 return x;
4290 }
4291 \f
4292 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4293
4294 static rtx
4295 simplify_if_then_else (x)
4296 rtx x;
4297 {
4298 enum machine_mode mode = GET_MODE (x);
4299 rtx cond = XEXP (x, 0);
4300 rtx true = XEXP (x, 1);
4301 rtx false = XEXP (x, 2);
4302 enum rtx_code true_code = GET_CODE (cond);
4303 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4304 rtx temp;
4305 int i;
4306
4307 /* Simplify storing of the truth value. */
4308 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4309 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4310
4311 /* Also when the truth value has to be reversed. */
4312 if (comparison_p && reversible_comparison_p (cond)
4313 && true == const0_rtx && false == const_true_rtx)
4314 return gen_binary (reverse_condition (true_code),
4315 mode, XEXP (cond, 0), XEXP (cond, 1));
4316
4317 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4318 in it is being compared against certain values. Get the true and false
4319 comparisons and see if that says anything about the value of each arm. */
4320
4321 if (comparison_p && reversible_comparison_p (cond)
4322 && GET_CODE (XEXP (cond, 0)) == REG)
4323 {
4324 HOST_WIDE_INT nzb;
4325 rtx from = XEXP (cond, 0);
4326 enum rtx_code false_code = reverse_condition (true_code);
4327 rtx true_val = XEXP (cond, 1);
4328 rtx false_val = true_val;
4329 int swapped = 0;
4330
4331 /* If FALSE_CODE is EQ, swap the codes and arms. */
4332
4333 if (false_code == EQ)
4334 {
4335 swapped = 1, true_code = EQ, false_code = NE;
4336 temp = true, true = false, false = temp;
4337 }
4338
4339 /* If we are comparing against zero and the expression being tested has
4340 only a single bit that might be nonzero, that is its value when it is
4341 not equal to zero. Similarly if it is known to be -1 or 0. */
4342
4343 if (true_code == EQ && true_val == const0_rtx
4344 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4345 false_code = EQ, false_val = GEN_INT (nzb);
4346 else if (true_code == EQ && true_val == const0_rtx
4347 && (num_sign_bit_copies (from, GET_MODE (from))
4348 == GET_MODE_BITSIZE (GET_MODE (from))))
4349 false_code = EQ, false_val = constm1_rtx;
4350
4351 /* Now simplify an arm if we know the value of the register in the
4352 branch and it is used in the arm. Be careful due to the potential
4353 of locally-shared RTL. */
4354
4355 if (reg_mentioned_p (from, true))
4356 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4357 pc_rtx, pc_rtx, 0, 0);
4358 if (reg_mentioned_p (from, false))
4359 false = subst (known_cond (copy_rtx (false), false_code,
4360 from, false_val),
4361 pc_rtx, pc_rtx, 0, 0);
4362
4363 SUBST (XEXP (x, 1), swapped ? false : true);
4364 SUBST (XEXP (x, 2), swapped ? true : false);
4365
4366 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4367 }
4368
4369 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4370 reversed, do so to avoid needing two sets of patterns for
4371 subtract-and-branch insns. Similarly if we have a constant in the true
4372 arm, the false arm is the same as the first operand of the comparison, or
4373 the false arm is more complicated than the true arm. */
4374
4375 if (comparison_p && reversible_comparison_p (cond)
4376 && (true == pc_rtx
4377 || (CONSTANT_P (true)
4378 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4379 || true == const0_rtx
4380 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4381 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4382 || (GET_CODE (true) == SUBREG
4383 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4384 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4385 || reg_mentioned_p (true, false)
4386 || rtx_equal_p (false, XEXP (cond, 0))))
4387 {
4388 true_code = reverse_condition (true_code);
4389 SUBST (XEXP (x, 0),
4390 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4391 XEXP (cond, 1)));
4392
4393 SUBST (XEXP (x, 1), false);
4394 SUBST (XEXP (x, 2), true);
4395
4396 temp = true, true = false, false = temp, cond = XEXP (x, 0);
4397
4398 /* It is possible that the conditional has been simplified out. */
4399 true_code = GET_CODE (cond);
4400 comparison_p = GET_RTX_CLASS (true_code) == '<';
4401 }
4402
4403 /* If the two arms are identical, we don't need the comparison. */
4404
4405 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4406 return true;
4407
4408 /* Convert a == b ? b : a to "a". */
4409 if (true_code == EQ && ! side_effects_p (cond)
4410 && rtx_equal_p (XEXP (cond, 0), false)
4411 && rtx_equal_p (XEXP (cond, 1), true))
4412 return false;
4413 else if (true_code == NE && ! side_effects_p (cond)
4414 && rtx_equal_p (XEXP (cond, 0), true)
4415 && rtx_equal_p (XEXP (cond, 1), false))
4416 return true;
4417
4418 /* Look for cases where we have (abs x) or (neg (abs X)). */
4419
4420 if (GET_MODE_CLASS (mode) == MODE_INT
4421 && GET_CODE (false) == NEG
4422 && rtx_equal_p (true, XEXP (false, 0))
4423 && comparison_p
4424 && rtx_equal_p (true, XEXP (cond, 0))
4425 && ! side_effects_p (true))
4426 switch (true_code)
4427 {
4428 case GT:
4429 case GE:
4430 return gen_unary (ABS, mode, mode, true);
4431 case LT:
4432 case LE:
4433 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4434 default:
4435 break;
4436 }
4437
4438 /* Look for MIN or MAX. */
4439
4440 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4441 && comparison_p
4442 && rtx_equal_p (XEXP (cond, 0), true)
4443 && rtx_equal_p (XEXP (cond, 1), false)
4444 && ! side_effects_p (cond))
4445 switch (true_code)
4446 {
4447 case GE:
4448 case GT:
4449 return gen_binary (SMAX, mode, true, false);
4450 case LE:
4451 case LT:
4452 return gen_binary (SMIN, mode, true, false);
4453 case GEU:
4454 case GTU:
4455 return gen_binary (UMAX, mode, true, false);
4456 case LEU:
4457 case LTU:
4458 return gen_binary (UMIN, mode, true, false);
4459 default:
4460 break;
4461 }
4462
4463 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4464 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4465 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4466 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4467 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4468 neither 1 or -1, but it isn't worth checking for. */
4469
4470 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4471 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4472 {
4473 rtx t = make_compound_operation (true, SET);
4474 rtx f = make_compound_operation (false, SET);
4475 rtx cond_op0 = XEXP (cond, 0);
4476 rtx cond_op1 = XEXP (cond, 1);
4477 enum rtx_code op = NIL, extend_op = NIL;
4478 enum machine_mode m = mode;
4479 rtx z = 0, c1 = NULL_RTX;
4480
4481 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4482 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4483 || GET_CODE (t) == ASHIFT
4484 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4485 && rtx_equal_p (XEXP (t, 0), f))
4486 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4487
4488 /* If an identity-zero op is commutative, check whether there
4489 would be a match if we swapped the operands. */
4490 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4491 || GET_CODE (t) == XOR)
4492 && rtx_equal_p (XEXP (t, 1), f))
4493 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4494 else if (GET_CODE (t) == SIGN_EXTEND
4495 && (GET_CODE (XEXP (t, 0)) == PLUS
4496 || GET_CODE (XEXP (t, 0)) == MINUS
4497 || GET_CODE (XEXP (t, 0)) == IOR
4498 || GET_CODE (XEXP (t, 0)) == XOR
4499 || GET_CODE (XEXP (t, 0)) == ASHIFT
4500 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4501 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4502 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4503 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4504 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4505 && (num_sign_bit_copies (f, GET_MODE (f))
4506 > (GET_MODE_BITSIZE (mode)
4507 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4508 {
4509 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4510 extend_op = SIGN_EXTEND;
4511 m = GET_MODE (XEXP (t, 0));
4512 }
4513 else if (GET_CODE (t) == SIGN_EXTEND
4514 && (GET_CODE (XEXP (t, 0)) == PLUS
4515 || GET_CODE (XEXP (t, 0)) == IOR
4516 || GET_CODE (XEXP (t, 0)) == XOR)
4517 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4518 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4519 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4520 && (num_sign_bit_copies (f, GET_MODE (f))
4521 > (GET_MODE_BITSIZE (mode)
4522 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4523 {
4524 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4525 extend_op = SIGN_EXTEND;
4526 m = GET_MODE (XEXP (t, 0));
4527 }
4528 else if (GET_CODE (t) == ZERO_EXTEND
4529 && (GET_CODE (XEXP (t, 0)) == PLUS
4530 || GET_CODE (XEXP (t, 0)) == MINUS
4531 || GET_CODE (XEXP (t, 0)) == IOR
4532 || GET_CODE (XEXP (t, 0)) == XOR
4533 || GET_CODE (XEXP (t, 0)) == ASHIFT
4534 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4535 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4536 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4537 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4538 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4539 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4540 && ((nonzero_bits (f, GET_MODE (f))
4541 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4542 == 0))
4543 {
4544 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4545 extend_op = ZERO_EXTEND;
4546 m = GET_MODE (XEXP (t, 0));
4547 }
4548 else if (GET_CODE (t) == ZERO_EXTEND
4549 && (GET_CODE (XEXP (t, 0)) == PLUS
4550 || GET_CODE (XEXP (t, 0)) == IOR
4551 || GET_CODE (XEXP (t, 0)) == XOR)
4552 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4553 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4554 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4555 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4556 && ((nonzero_bits (f, GET_MODE (f))
4557 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4558 == 0))
4559 {
4560 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4561 extend_op = ZERO_EXTEND;
4562 m = GET_MODE (XEXP (t, 0));
4563 }
4564
4565 if (z)
4566 {
4567 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4568 pc_rtx, pc_rtx, 0, 0);
4569 temp = gen_binary (MULT, m, temp,
4570 gen_binary (MULT, m, c1, const_true_rtx));
4571 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4572 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4573
4574 if (extend_op != NIL)
4575 temp = gen_unary (extend_op, mode, m, temp);
4576
4577 return temp;
4578 }
4579 }
4580
4581 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4582 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4583 negation of a single bit, we can convert this operation to a shift. We
4584 can actually do this more generally, but it doesn't seem worth it. */
4585
4586 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4587 && false == const0_rtx && GET_CODE (true) == CONST_INT
4588 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4589 && (i = exact_log2 (INTVAL (true))) >= 0)
4590 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4591 == GET_MODE_BITSIZE (mode))
4592 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4593 return
4594 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4595 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4596
4597 return x;
4598 }
4599 \f
4600 /* Simplify X, a SET expression. Return the new expression. */
4601
4602 static rtx
4603 simplify_set (x)
4604 rtx x;
4605 {
4606 rtx src = SET_SRC (x);
4607 rtx dest = SET_DEST (x);
4608 enum machine_mode mode
4609 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4610 rtx other_insn;
4611 rtx *cc_use;
4612
4613 /* (set (pc) (return)) gets written as (return). */
4614 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4615 return src;
4616
4617 /* Now that we know for sure which bits of SRC we are using, see if we can
4618 simplify the expression for the object knowing that we only need the
4619 low-order bits. */
4620
4621 if (GET_MODE_CLASS (mode) == MODE_INT)
4622 {
4623 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4624 SUBST (SET_SRC (x), src);
4625 }
4626
4627 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4628 the comparison result and try to simplify it unless we already have used
4629 undobuf.other_insn. */
4630 if ((GET_CODE (src) == COMPARE
4631 #ifdef HAVE_cc0
4632 || dest == cc0_rtx
4633 #endif
4634 )
4635 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4636 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4637 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4638 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4639 {
4640 enum rtx_code old_code = GET_CODE (*cc_use);
4641 enum rtx_code new_code;
4642 rtx op0, op1;
4643 int other_changed = 0;
4644 enum machine_mode compare_mode = GET_MODE (dest);
4645
4646 if (GET_CODE (src) == COMPARE)
4647 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4648 else
4649 op0 = src, op1 = const0_rtx;
4650
4651 /* Simplify our comparison, if possible. */
4652 new_code = simplify_comparison (old_code, &op0, &op1);
4653
4654 #ifdef EXTRA_CC_MODES
4655 /* If this machine has CC modes other than CCmode, check to see if we
4656 need to use a different CC mode here. */
4657 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4658 #endif /* EXTRA_CC_MODES */
4659
4660 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4661 /* If the mode changed, we have to change SET_DEST, the mode in the
4662 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4663 a hard register, just build new versions with the proper mode. If it
4664 is a pseudo, we lose unless it is only time we set the pseudo, in
4665 which case we can safely change its mode. */
4666 if (compare_mode != GET_MODE (dest))
4667 {
4668 int regno = REGNO (dest);
4669 rtx new_dest = gen_rtx_REG (compare_mode, regno);
4670
4671 if (regno < FIRST_PSEUDO_REGISTER
4672 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4673 {
4674 if (regno >= FIRST_PSEUDO_REGISTER)
4675 SUBST (regno_reg_rtx[regno], new_dest);
4676
4677 SUBST (SET_DEST (x), new_dest);
4678 SUBST (XEXP (*cc_use, 0), new_dest);
4679 other_changed = 1;
4680
4681 dest = new_dest;
4682 }
4683 }
4684 #endif
4685
4686 /* If the code changed, we have to build a new comparison in
4687 undobuf.other_insn. */
4688 if (new_code != old_code)
4689 {
4690 unsigned HOST_WIDE_INT mask;
4691
4692 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4693 dest, const0_rtx));
4694
4695 /* If the only change we made was to change an EQ into an NE or
4696 vice versa, OP0 has only one bit that might be nonzero, and OP1
4697 is zero, check if changing the user of the condition code will
4698 produce a valid insn. If it won't, we can keep the original code
4699 in that insn by surrounding our operation with an XOR. */
4700
4701 if (((old_code == NE && new_code == EQ)
4702 || (old_code == EQ && new_code == NE))
4703 && ! other_changed && op1 == const0_rtx
4704 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4705 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4706 {
4707 rtx pat = PATTERN (other_insn), note = 0;
4708
4709 if ((recog_for_combine (&pat, other_insn, &note) < 0
4710 && ! check_asm_operands (pat)))
4711 {
4712 PUT_CODE (*cc_use, old_code);
4713 other_insn = 0;
4714
4715 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4716 }
4717 }
4718
4719 other_changed = 1;
4720 }
4721
4722 if (other_changed)
4723 undobuf.other_insn = other_insn;
4724
4725 #ifdef HAVE_cc0
4726 /* If we are now comparing against zero, change our source if
4727 needed. If we do not use cc0, we always have a COMPARE. */
4728 if (op1 == const0_rtx && dest == cc0_rtx)
4729 {
4730 SUBST (SET_SRC (x), op0);
4731 src = op0;
4732 }
4733 else
4734 #endif
4735
4736 /* Otherwise, if we didn't previously have a COMPARE in the
4737 correct mode, we need one. */
4738 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4739 {
4740 SUBST (SET_SRC (x),
4741 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4742 src = SET_SRC (x);
4743 }
4744 else
4745 {
4746 /* Otherwise, update the COMPARE if needed. */
4747 SUBST (XEXP (src, 0), op0);
4748 SUBST (XEXP (src, 1), op1);
4749 }
4750 }
4751 else
4752 {
4753 /* Get SET_SRC in a form where we have placed back any
4754 compound expressions. Then do the checks below. */
4755 src = make_compound_operation (src, SET);
4756 SUBST (SET_SRC (x), src);
4757 }
4758
4759 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4760 and X being a REG or (subreg (reg)), we may be able to convert this to
4761 (set (subreg:m2 x) (op)).
4762
4763 We can always do this if M1 is narrower than M2 because that means that
4764 we only care about the low bits of the result.
4765
4766 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4767 perform a narrower operation than requested since the high-order bits will
4768 be undefined. On machine where it is defined, this transformation is safe
4769 as long as M1 and M2 have the same number of words. */
4770
4771 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4772 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4773 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4774 / UNITS_PER_WORD)
4775 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4776 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4777 #ifndef WORD_REGISTER_OPERATIONS
4778 && (GET_MODE_SIZE (GET_MODE (src))
4779 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4780 #endif
4781 #ifdef CLASS_CANNOT_CHANGE_SIZE
4782 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4783 && (TEST_HARD_REG_BIT
4784 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4785 REGNO (dest)))
4786 && (GET_MODE_SIZE (GET_MODE (src))
4787 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4788 #endif
4789 && (GET_CODE (dest) == REG
4790 || (GET_CODE (dest) == SUBREG
4791 && GET_CODE (SUBREG_REG (dest)) == REG)))
4792 {
4793 SUBST (SET_DEST (x),
4794 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4795 dest));
4796 SUBST (SET_SRC (x), SUBREG_REG (src));
4797
4798 src = SET_SRC (x), dest = SET_DEST (x);
4799 }
4800
4801 #ifdef LOAD_EXTEND_OP
4802 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4803 would require a paradoxical subreg. Replace the subreg with a
4804 zero_extend to avoid the reload that would otherwise be required. */
4805
4806 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4807 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4808 && SUBREG_WORD (src) == 0
4809 && (GET_MODE_SIZE (GET_MODE (src))
4810 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4811 && GET_CODE (SUBREG_REG (src)) == MEM)
4812 {
4813 SUBST (SET_SRC (x),
4814 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4815 GET_MODE (src), XEXP (src, 0)));
4816
4817 src = SET_SRC (x);
4818 }
4819 #endif
4820
4821 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4822 are comparing an item known to be 0 or -1 against 0, use a logical
4823 operation instead. Check for one of the arms being an IOR of the other
4824 arm with some value. We compute three terms to be IOR'ed together. In
4825 practice, at most two will be nonzero. Then we do the IOR's. */
4826
4827 if (GET_CODE (dest) != PC
4828 && GET_CODE (src) == IF_THEN_ELSE
4829 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4830 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4831 && XEXP (XEXP (src, 0), 1) == const0_rtx
4832 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4833 #ifdef HAVE_conditional_move
4834 && ! can_conditionally_move_p (GET_MODE (src))
4835 #endif
4836 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4837 GET_MODE (XEXP (XEXP (src, 0), 0)))
4838 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4839 && ! side_effects_p (src))
4840 {
4841 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4842 ? XEXP (src, 1) : XEXP (src, 2));
4843 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4844 ? XEXP (src, 2) : XEXP (src, 1));
4845 rtx term1 = const0_rtx, term2, term3;
4846
4847 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4848 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4849 else if (GET_CODE (true) == IOR
4850 && rtx_equal_p (XEXP (true, 1), false))
4851 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4852 else if (GET_CODE (false) == IOR
4853 && rtx_equal_p (XEXP (false, 0), true))
4854 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4855 else if (GET_CODE (false) == IOR
4856 && rtx_equal_p (XEXP (false, 1), true))
4857 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4858
4859 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4860 term3 = gen_binary (AND, GET_MODE (src),
4861 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4862 XEXP (XEXP (src, 0), 0)),
4863 false);
4864
4865 SUBST (SET_SRC (x),
4866 gen_binary (IOR, GET_MODE (src),
4867 gen_binary (IOR, GET_MODE (src), term1, term2),
4868 term3));
4869
4870 src = SET_SRC (x);
4871 }
4872
4873 #ifdef HAVE_conditional_arithmetic
4874 /* If we have conditional arithmetic and the operand of a SET is
4875 a conditional expression, replace this with an IF_THEN_ELSE.
4876 We can either have a conditional expression or a MULT of that expression
4877 with a constant. */
4878 if ((GET_RTX_CLASS (GET_CODE (src)) == '1'
4879 || GET_RTX_CLASS (GET_CODE (src)) == '2'
4880 || GET_RTX_CLASS (GET_CODE (src)) == 'c')
4881 && (GET_RTX_CLASS (GET_CODE (XEXP (src, 0))) == '<'
4882 || (GET_CODE (XEXP (src, 0)) == MULT
4883 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (src, 0), 0))) == '<'
4884 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT)))
4885 {
4886 rtx cond = XEXP (src, 0);
4887 rtx true_val = const1_rtx;
4888 rtx false_arm, true_arm;
4889
4890 if (GET_CODE (cond) == MULT)
4891 {
4892 true_val = XEXP (cond, 1);
4893 cond = XEXP (cond, 0);
4894 }
4895
4896 if (GET_RTX_CLASS (GET_CODE (src)) == '1')
4897 {
4898 true_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4899 GET_MODE (XEXP (src, 0)), true_val);
4900 false_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4901 GET_MODE (XEXP (src, 0)), const0_rtx);
4902 }
4903 else
4904 {
4905 true_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4906 true_val, XEXP (src, 1));
4907 false_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4908 const0_rtx, XEXP (src, 1));
4909 }
4910
4911 /* Canonicalize if true_arm is the simpler one. */
4912 if (GET_RTX_CLASS (GET_CODE (true_arm)) == 'o'
4913 && GET_RTX_CLASS (GET_CODE (false_arm)) != 'o'
4914 && reversible_comparison_p (cond))
4915 {
4916 rtx temp = true_arm;
4917
4918 true_arm = false_arm;
4919 false_arm = temp;
4920
4921 cond = gen_rtx_combine (reverse_condition (GET_CODE (cond)),
4922 GET_MODE (cond), XEXP (cond, 0),
4923 XEXP (cond, 1));
4924 }
4925
4926 src = gen_rtx_combine (IF_THEN_ELSE, GET_MODE (src),
4927 gen_rtx_combine (GET_CODE (cond), VOIDmode,
4928 XEXP (cond, 0),
4929 XEXP (cond, 1)),
4930 true_arm, false_arm);
4931 SUBST (SET_SRC (x), src);
4932 }
4933 #endif
4934
4935 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4936 whole thing fail. */
4937 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4938 return src;
4939 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4940 return dest;
4941 else
4942 /* Convert this into a field assignment operation, if possible. */
4943 return make_field_assignment (x);
4944 }
4945 \f
4946 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4947 result. LAST is nonzero if this is the last retry. */
4948
4949 static rtx
4950 simplify_logical (x, last)
4951 rtx x;
4952 int last;
4953 {
4954 enum machine_mode mode = GET_MODE (x);
4955 rtx op0 = XEXP (x, 0);
4956 rtx op1 = XEXP (x, 1);
4957
4958 switch (GET_CODE (x))
4959 {
4960 case AND:
4961 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4962 insn (and may simplify more). */
4963 if (GET_CODE (op0) == XOR
4964 && rtx_equal_p (XEXP (op0, 0), op1)
4965 && ! side_effects_p (op1))
4966 x = gen_binary (AND, mode,
4967 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4968
4969 if (GET_CODE (op0) == XOR
4970 && rtx_equal_p (XEXP (op0, 1), op1)
4971 && ! side_effects_p (op1))
4972 x = gen_binary (AND, mode,
4973 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4974
4975 /* Similarly for (~ (A ^ B)) & A. */
4976 if (GET_CODE (op0) == NOT
4977 && GET_CODE (XEXP (op0, 0)) == XOR
4978 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4979 && ! side_effects_p (op1))
4980 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4981
4982 if (GET_CODE (op0) == NOT
4983 && GET_CODE (XEXP (op0, 0)) == XOR
4984 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4985 && ! side_effects_p (op1))
4986 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4987
4988 /* We can call simplify_and_const_int only if we don't lose
4989 any (sign) bits when converting INTVAL (op1) to
4990 "unsigned HOST_WIDE_INT". */
4991 if (GET_CODE (op1) == CONST_INT
4992 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4993 || INTVAL (op1) > 0))
4994 {
4995 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4996
4997 /* If we have (ior (and (X C1) C2)) and the next restart would be
4998 the last, simplify this by making C1 as small as possible
4999 and then exit. */
5000 if (last
5001 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5002 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5003 && GET_CODE (op1) == CONST_INT)
5004 return gen_binary (IOR, mode,
5005 gen_binary (AND, mode, XEXP (op0, 0),
5006 GEN_INT (INTVAL (XEXP (op0, 1))
5007 & ~ INTVAL (op1))), op1);
5008
5009 if (GET_CODE (x) != AND)
5010 return x;
5011
5012 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5013 || GET_RTX_CLASS (GET_CODE (x)) == '2')
5014 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5015 }
5016
5017 /* Convert (A | B) & A to A. */
5018 if (GET_CODE (op0) == IOR
5019 && (rtx_equal_p (XEXP (op0, 0), op1)
5020 || rtx_equal_p (XEXP (op0, 1), op1))
5021 && ! side_effects_p (XEXP (op0, 0))
5022 && ! side_effects_p (XEXP (op0, 1)))
5023 return op1;
5024
5025 /* In the following group of tests (and those in case IOR below),
5026 we start with some combination of logical operations and apply
5027 the distributive law followed by the inverse distributive law.
5028 Most of the time, this results in no change. However, if some of
5029 the operands are the same or inverses of each other, simplifications
5030 will result.
5031
5032 For example, (and (ior A B) (not B)) can occur as the result of
5033 expanding a bit field assignment. When we apply the distributive
5034 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5035 which then simplifies to (and (A (not B))).
5036
5037 If we have (and (ior A B) C), apply the distributive law and then
5038 the inverse distributive law to see if things simplify. */
5039
5040 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5041 {
5042 x = apply_distributive_law
5043 (gen_binary (GET_CODE (op0), mode,
5044 gen_binary (AND, mode, XEXP (op0, 0), op1),
5045 gen_binary (AND, mode, XEXP (op0, 1), op1)));
5046 if (GET_CODE (x) != AND)
5047 return x;
5048 }
5049
5050 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5051 return apply_distributive_law
5052 (gen_binary (GET_CODE (op1), mode,
5053 gen_binary (AND, mode, XEXP (op1, 0), op0),
5054 gen_binary (AND, mode, XEXP (op1, 1), op0)));
5055
5056 /* Similarly, taking advantage of the fact that
5057 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5058
5059 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5060 return apply_distributive_law
5061 (gen_binary (XOR, mode,
5062 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5063 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
5064
5065 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5066 return apply_distributive_law
5067 (gen_binary (XOR, mode,
5068 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5069 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
5070 break;
5071
5072 case IOR:
5073 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5074 if (GET_CODE (op1) == CONST_INT
5075 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5076 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
5077 return op1;
5078
5079 /* Convert (A & B) | A to A. */
5080 if (GET_CODE (op0) == AND
5081 && (rtx_equal_p (XEXP (op0, 0), op1)
5082 || rtx_equal_p (XEXP (op0, 1), op1))
5083 && ! side_effects_p (XEXP (op0, 0))
5084 && ! side_effects_p (XEXP (op0, 1)))
5085 return op1;
5086
5087 /* If we have (ior (and A B) C), apply the distributive law and then
5088 the inverse distributive law to see if things simplify. */
5089
5090 if (GET_CODE (op0) == AND)
5091 {
5092 x = apply_distributive_law
5093 (gen_binary (AND, mode,
5094 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5095 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
5096
5097 if (GET_CODE (x) != IOR)
5098 return x;
5099 }
5100
5101 if (GET_CODE (op1) == AND)
5102 {
5103 x = apply_distributive_law
5104 (gen_binary (AND, mode,
5105 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5106 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
5107
5108 if (GET_CODE (x) != IOR)
5109 return x;
5110 }
5111
5112 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5113 mode size to (rotate A CX). */
5114
5115 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5116 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5117 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5118 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5119 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5120 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5121 == GET_MODE_BITSIZE (mode)))
5122 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5123 (GET_CODE (op0) == ASHIFT
5124 ? XEXP (op0, 1) : XEXP (op1, 1)));
5125
5126 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5127 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5128 does not affect any of the bits in OP1, it can really be done
5129 as a PLUS and we can associate. We do this by seeing if OP1
5130 can be safely shifted left C bits. */
5131 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5132 && GET_CODE (XEXP (op0, 0)) == PLUS
5133 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5134 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5135 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5136 {
5137 int count = INTVAL (XEXP (op0, 1));
5138 HOST_WIDE_INT mask = INTVAL (op1) << count;
5139
5140 if (mask >> count == INTVAL (op1)
5141 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5142 {
5143 SUBST (XEXP (XEXP (op0, 0), 1),
5144 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5145 return op0;
5146 }
5147 }
5148 break;
5149
5150 case XOR:
5151 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5152 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5153 (NOT y). */
5154 {
5155 int num_negated = 0;
5156
5157 if (GET_CODE (op0) == NOT)
5158 num_negated++, op0 = XEXP (op0, 0);
5159 if (GET_CODE (op1) == NOT)
5160 num_negated++, op1 = XEXP (op1, 0);
5161
5162 if (num_negated == 2)
5163 {
5164 SUBST (XEXP (x, 0), op0);
5165 SUBST (XEXP (x, 1), op1);
5166 }
5167 else if (num_negated == 1)
5168 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5169 }
5170
5171 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5172 correspond to a machine insn or result in further simplifications
5173 if B is a constant. */
5174
5175 if (GET_CODE (op0) == AND
5176 && rtx_equal_p (XEXP (op0, 1), op1)
5177 && ! side_effects_p (op1))
5178 return gen_binary (AND, mode,
5179 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5180 op1);
5181
5182 else if (GET_CODE (op0) == AND
5183 && rtx_equal_p (XEXP (op0, 0), op1)
5184 && ! side_effects_p (op1))
5185 return gen_binary (AND, mode,
5186 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5187 op1);
5188
5189 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5190 comparison if STORE_FLAG_VALUE is 1. */
5191 if (STORE_FLAG_VALUE == 1
5192 && op1 == const1_rtx
5193 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5194 && reversible_comparison_p (op0))
5195 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5196 mode, XEXP (op0, 0), XEXP (op0, 1));
5197
5198 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5199 is (lt foo (const_int 0)), so we can perform the above
5200 simplification if STORE_FLAG_VALUE is 1. */
5201
5202 if (STORE_FLAG_VALUE == 1
5203 && op1 == const1_rtx
5204 && GET_CODE (op0) == LSHIFTRT
5205 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5206 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5207 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5208
5209 /* (xor (comparison foo bar) (const_int sign-bit))
5210 when STORE_FLAG_VALUE is the sign bit. */
5211 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5212 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5213 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5214 && op1 == const_true_rtx
5215 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5216 && reversible_comparison_p (op0))
5217 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5218 mode, XEXP (op0, 0), XEXP (op0, 1));
5219 break;
5220
5221 default:
5222 abort ();
5223 }
5224
5225 return x;
5226 }
5227 \f
5228 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5229 operations" because they can be replaced with two more basic operations.
5230 ZERO_EXTEND is also considered "compound" because it can be replaced with
5231 an AND operation, which is simpler, though only one operation.
5232
5233 The function expand_compound_operation is called with an rtx expression
5234 and will convert it to the appropriate shifts and AND operations,
5235 simplifying at each stage.
5236
5237 The function make_compound_operation is called to convert an expression
5238 consisting of shifts and ANDs into the equivalent compound expression.
5239 It is the inverse of this function, loosely speaking. */
5240
5241 static rtx
5242 expand_compound_operation (x)
5243 rtx x;
5244 {
5245 int pos = 0, len;
5246 int unsignedp = 0;
5247 int modewidth;
5248 rtx tem;
5249
5250 switch (GET_CODE (x))
5251 {
5252 case ZERO_EXTEND:
5253 unsignedp = 1;
5254 case SIGN_EXTEND:
5255 /* We can't necessarily use a const_int for a multiword mode;
5256 it depends on implicitly extending the value.
5257 Since we don't know the right way to extend it,
5258 we can't tell whether the implicit way is right.
5259
5260 Even for a mode that is no wider than a const_int,
5261 we can't win, because we need to sign extend one of its bits through
5262 the rest of it, and we don't know which bit. */
5263 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5264 return x;
5265
5266 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5267 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5268 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5269 reloaded. If not for that, MEM's would very rarely be safe.
5270
5271 Reject MODEs bigger than a word, because we might not be able
5272 to reference a two-register group starting with an arbitrary register
5273 (and currently gen_lowpart might crash for a SUBREG). */
5274
5275 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5276 return x;
5277
5278 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5279 /* If the inner object has VOIDmode (the only way this can happen
5280 is if it is a ASM_OPERANDS), we can't do anything since we don't
5281 know how much masking to do. */
5282 if (len == 0)
5283 return x;
5284
5285 break;
5286
5287 case ZERO_EXTRACT:
5288 unsignedp = 1;
5289 case SIGN_EXTRACT:
5290 /* If the operand is a CLOBBER, just return it. */
5291 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5292 return XEXP (x, 0);
5293
5294 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5295 || GET_CODE (XEXP (x, 2)) != CONST_INT
5296 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5297 return x;
5298
5299 len = INTVAL (XEXP (x, 1));
5300 pos = INTVAL (XEXP (x, 2));
5301
5302 /* If this goes outside the object being extracted, replace the object
5303 with a (use (mem ...)) construct that only combine understands
5304 and is used only for this purpose. */
5305 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5306 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5307
5308 if (BITS_BIG_ENDIAN)
5309 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5310
5311 break;
5312
5313 default:
5314 return x;
5315 }
5316
5317 /* We can optimize some special cases of ZERO_EXTEND. */
5318 if (GET_CODE (x) == ZERO_EXTEND)
5319 {
5320 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5321 know that the last value didn't have any inappropriate bits
5322 set. */
5323 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5324 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5325 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5326 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5327 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5328 return XEXP (XEXP (x, 0), 0);
5329
5330 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5331 if (GET_CODE (XEXP (x, 0)) == SUBREG
5332 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5333 && subreg_lowpart_p (XEXP (x, 0))
5334 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5335 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5336 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5337 return SUBREG_REG (XEXP (x, 0));
5338
5339 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5340 is a comparison and STORE_FLAG_VALUE permits. This is like
5341 the first case, but it works even when GET_MODE (x) is larger
5342 than HOST_WIDE_INT. */
5343 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5344 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5345 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5346 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5347 <= HOST_BITS_PER_WIDE_INT)
5348 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5349 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5350 return XEXP (XEXP (x, 0), 0);
5351
5352 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5353 if (GET_CODE (XEXP (x, 0)) == SUBREG
5354 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5355 && subreg_lowpart_p (XEXP (x, 0))
5356 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5357 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5358 <= HOST_BITS_PER_WIDE_INT)
5359 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5360 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5361 return SUBREG_REG (XEXP (x, 0));
5362
5363 /* If sign extension is cheaper than zero extension, then use it
5364 if we know that no extraneous bits are set, and that the high
5365 bit is not set. */
5366 if (flag_expensive_optimizations
5367 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5368 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5369 & ~ (((unsigned HOST_WIDE_INT)
5370 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5371 >> 1))
5372 == 0))
5373 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5374 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5375 <= HOST_BITS_PER_WIDE_INT)
5376 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5377 & ~ (((unsigned HOST_WIDE_INT)
5378 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5379 >> 1))
5380 == 0))))
5381 {
5382 rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
5383
5384 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5385 return expand_compound_operation (temp);
5386 }
5387 }
5388
5389 /* If we reach here, we want to return a pair of shifts. The inner
5390 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5391 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5392 logical depending on the value of UNSIGNEDP.
5393
5394 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5395 converted into an AND of a shift.
5396
5397 We must check for the case where the left shift would have a negative
5398 count. This can happen in a case like (x >> 31) & 255 on machines
5399 that can't shift by a constant. On those machines, we would first
5400 combine the shift with the AND to produce a variable-position
5401 extraction. Then the constant of 31 would be substituted in to produce
5402 a such a position. */
5403
5404 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5405 if (modewidth >= pos - len)
5406 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5407 GET_MODE (x),
5408 simplify_shift_const (NULL_RTX, ASHIFT,
5409 GET_MODE (x),
5410 XEXP (x, 0),
5411 modewidth - pos - len),
5412 modewidth - len);
5413
5414 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5415 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5416 simplify_shift_const (NULL_RTX, LSHIFTRT,
5417 GET_MODE (x),
5418 XEXP (x, 0), pos),
5419 ((HOST_WIDE_INT) 1 << len) - 1);
5420 else
5421 /* Any other cases we can't handle. */
5422 return x;
5423
5424
5425 /* If we couldn't do this for some reason, return the original
5426 expression. */
5427 if (GET_CODE (tem) == CLOBBER)
5428 return x;
5429
5430 return tem;
5431 }
5432 \f
5433 /* X is a SET which contains an assignment of one object into
5434 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5435 or certain SUBREGS). If possible, convert it into a series of
5436 logical operations.
5437
5438 We half-heartedly support variable positions, but do not at all
5439 support variable lengths. */
5440
5441 static rtx
5442 expand_field_assignment (x)
5443 rtx x;
5444 {
5445 rtx inner;
5446 rtx pos; /* Always counts from low bit. */
5447 int len;
5448 rtx mask;
5449 enum machine_mode compute_mode;
5450
5451 /* Loop until we find something we can't simplify. */
5452 while (1)
5453 {
5454 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5455 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5456 {
5457 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5458 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5459 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5460 }
5461 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5462 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5463 {
5464 inner = XEXP (SET_DEST (x), 0);
5465 len = INTVAL (XEXP (SET_DEST (x), 1));
5466 pos = XEXP (SET_DEST (x), 2);
5467
5468 /* If the position is constant and spans the width of INNER,
5469 surround INNER with a USE to indicate this. */
5470 if (GET_CODE (pos) == CONST_INT
5471 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5472 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5473
5474 if (BITS_BIG_ENDIAN)
5475 {
5476 if (GET_CODE (pos) == CONST_INT)
5477 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5478 - INTVAL (pos));
5479 else if (GET_CODE (pos) == MINUS
5480 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5481 && (INTVAL (XEXP (pos, 1))
5482 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5483 /* If position is ADJUST - X, new position is X. */
5484 pos = XEXP (pos, 0);
5485 else
5486 pos = gen_binary (MINUS, GET_MODE (pos),
5487 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5488 - len),
5489 pos);
5490 }
5491 }
5492
5493 /* A SUBREG between two modes that occupy the same numbers of words
5494 can be done by moving the SUBREG to the source. */
5495 else if (GET_CODE (SET_DEST (x)) == SUBREG
5496 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5497 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5498 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5499 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5500 {
5501 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5502 gen_lowpart_for_combine
5503 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5504 SET_SRC (x)));
5505 continue;
5506 }
5507 else
5508 break;
5509
5510 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5511 inner = SUBREG_REG (inner);
5512
5513 compute_mode = GET_MODE (inner);
5514
5515 /* Don't attempt bitwise arithmetic on non-integral modes. */
5516 if (! INTEGRAL_MODE_P (compute_mode))
5517 {
5518 enum machine_mode imode;
5519
5520 /* Something is probably seriously wrong if this matches. */
5521 if (! FLOAT_MODE_P (compute_mode))
5522 break;
5523
5524 /* Try to find an integral mode to pun with. */
5525 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5526 if (imode == BLKmode)
5527 break;
5528
5529 compute_mode = imode;
5530 inner = gen_lowpart_for_combine (imode, inner);
5531 }
5532
5533 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5534 if (len < HOST_BITS_PER_WIDE_INT)
5535 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5536 else
5537 break;
5538
5539 /* Now compute the equivalent expression. Make a copy of INNER
5540 for the SET_DEST in case it is a MEM into which we will substitute;
5541 we don't want shared RTL in that case. */
5542 x = gen_rtx_SET
5543 (VOIDmode, copy_rtx (inner),
5544 gen_binary (IOR, compute_mode,
5545 gen_binary (AND, compute_mode,
5546 gen_unary (NOT, compute_mode,
5547 compute_mode,
5548 gen_binary (ASHIFT,
5549 compute_mode,
5550 mask, pos)),
5551 inner),
5552 gen_binary (ASHIFT, compute_mode,
5553 gen_binary (AND, compute_mode,
5554 gen_lowpart_for_combine
5555 (compute_mode, SET_SRC (x)),
5556 mask),
5557 pos)));
5558 }
5559
5560 return x;
5561 }
5562 \f
5563 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5564 it is an RTX that represents a variable starting position; otherwise,
5565 POS is the (constant) starting bit position (counted from the LSB).
5566
5567 INNER may be a USE. This will occur when we started with a bitfield
5568 that went outside the boundary of the object in memory, which is
5569 allowed on most machines. To isolate this case, we produce a USE
5570 whose mode is wide enough and surround the MEM with it. The only
5571 code that understands the USE is this routine. If it is not removed,
5572 it will cause the resulting insn not to match.
5573
5574 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5575 signed reference.
5576
5577 IN_DEST is non-zero if this is a reference in the destination of a
5578 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5579 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5580 be used.
5581
5582 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5583 ZERO_EXTRACT should be built even for bits starting at bit 0.
5584
5585 MODE is the desired mode of the result (if IN_DEST == 0).
5586
5587 The result is an RTX for the extraction or NULL_RTX if the target
5588 can't handle it. */
5589
5590 static rtx
5591 make_extraction (mode, inner, pos, pos_rtx, len,
5592 unsignedp, in_dest, in_compare)
5593 enum machine_mode mode;
5594 rtx inner;
5595 int pos;
5596 rtx pos_rtx;
5597 int len;
5598 int unsignedp;
5599 int in_dest, in_compare;
5600 {
5601 /* This mode describes the size of the storage area
5602 to fetch the overall value from. Within that, we
5603 ignore the POS lowest bits, etc. */
5604 enum machine_mode is_mode = GET_MODE (inner);
5605 enum machine_mode inner_mode;
5606 enum machine_mode wanted_inner_mode = byte_mode;
5607 enum machine_mode wanted_inner_reg_mode = word_mode;
5608 enum machine_mode pos_mode = word_mode;
5609 enum machine_mode extraction_mode = word_mode;
5610 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5611 int spans_byte = 0;
5612 rtx new = 0;
5613 rtx orig_pos_rtx = pos_rtx;
5614 int orig_pos;
5615
5616 /* Get some information about INNER and get the innermost object. */
5617 if (GET_CODE (inner) == USE)
5618 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5619 /* We don't need to adjust the position because we set up the USE
5620 to pretend that it was a full-word object. */
5621 spans_byte = 1, inner = XEXP (inner, 0);
5622 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5623 {
5624 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5625 consider just the QI as the memory to extract from.
5626 The subreg adds or removes high bits; its mode is
5627 irrelevant to the meaning of this extraction,
5628 since POS and LEN count from the lsb. */
5629 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5630 is_mode = GET_MODE (SUBREG_REG (inner));
5631 inner = SUBREG_REG (inner);
5632 }
5633
5634 inner_mode = GET_MODE (inner);
5635
5636 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5637 pos = INTVAL (pos_rtx), pos_rtx = 0;
5638
5639 /* See if this can be done without an extraction. We never can if the
5640 width of the field is not the same as that of some integer mode. For
5641 registers, we can only avoid the extraction if the position is at the
5642 low-order bit and this is either not in the destination or we have the
5643 appropriate STRICT_LOW_PART operation available.
5644
5645 For MEM, we can avoid an extract if the field starts on an appropriate
5646 boundary and we can change the mode of the memory reference. However,
5647 we cannot directly access the MEM if we have a USE and the underlying
5648 MEM is not TMODE. This combination means that MEM was being used in a
5649 context where bits outside its mode were being referenced; that is only
5650 valid in bit-field insns. */
5651
5652 if (tmode != BLKmode
5653 && ! (spans_byte && inner_mode != tmode)
5654 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5655 && GET_CODE (inner) != MEM
5656 && (! in_dest
5657 || (GET_CODE (inner) == REG
5658 && (movstrict_optab->handlers[(int) tmode].insn_code
5659 != CODE_FOR_nothing))))
5660 || (GET_CODE (inner) == MEM && pos_rtx == 0
5661 && (pos
5662 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5663 : BITS_PER_UNIT)) == 0
5664 /* We can't do this if we are widening INNER_MODE (it
5665 may not be aligned, for one thing). */
5666 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5667 && (inner_mode == tmode
5668 || (! mode_dependent_address_p (XEXP (inner, 0))
5669 && ! MEM_VOLATILE_P (inner))))))
5670 {
5671 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5672 field. If the original and current mode are the same, we need not
5673 adjust the offset. Otherwise, we do if bytes big endian.
5674
5675 If INNER is not a MEM, get a piece consisting of just the field
5676 of interest (in this case POS % BITS_PER_WORD must be 0). */
5677
5678 if (GET_CODE (inner) == MEM)
5679 {
5680 int offset;
5681 /* POS counts from lsb, but make OFFSET count in memory order. */
5682 if (BYTES_BIG_ENDIAN)
5683 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5684 else
5685 offset = pos / BITS_PER_UNIT;
5686
5687 new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
5688 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5689 MEM_COPY_ATTRIBUTES (new, inner);
5690 }
5691 else if (GET_CODE (inner) == REG)
5692 {
5693 /* We can't call gen_lowpart_for_combine here since we always want
5694 a SUBREG and it would sometimes return a new hard register. */
5695 if (tmode != inner_mode)
5696 new = gen_rtx_SUBREG (tmode, inner,
5697 (WORDS_BIG_ENDIAN
5698 && (GET_MODE_SIZE (inner_mode)
5699 > UNITS_PER_WORD)
5700 ? (((GET_MODE_SIZE (inner_mode)
5701 - GET_MODE_SIZE (tmode))
5702 / UNITS_PER_WORD)
5703 - pos / BITS_PER_WORD)
5704 : pos / BITS_PER_WORD));
5705 else
5706 new = inner;
5707 }
5708 else
5709 new = force_to_mode (inner, tmode,
5710 len >= HOST_BITS_PER_WIDE_INT
5711 ? GET_MODE_MASK (tmode)
5712 : ((HOST_WIDE_INT) 1 << len) - 1,
5713 NULL_RTX, 0);
5714
5715 /* If this extraction is going into the destination of a SET,
5716 make a STRICT_LOW_PART unless we made a MEM. */
5717
5718 if (in_dest)
5719 return (GET_CODE (new) == MEM ? new
5720 : (GET_CODE (new) != SUBREG
5721 ? gen_rtx_CLOBBER (tmode, const0_rtx)
5722 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5723
5724 /* Otherwise, sign- or zero-extend unless we already are in the
5725 proper mode. */
5726
5727 return (mode == tmode ? new
5728 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5729 mode, new));
5730 }
5731
5732 /* Unless this is a COMPARE or we have a funny memory reference,
5733 don't do anything with zero-extending field extracts starting at
5734 the low-order bit since they are simple AND operations. */
5735 if (pos_rtx == 0 && pos == 0 && ! in_dest
5736 && ! in_compare && ! spans_byte && unsignedp)
5737 return 0;
5738
5739 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
5740 we would be spanning bytes or if the position is not a constant and the
5741 length is not 1. In all other cases, we would only be going outside
5742 our object in cases when an original shift would have been
5743 undefined. */
5744 if (! spans_byte && GET_CODE (inner) == MEM
5745 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5746 || (pos_rtx != 0 && len != 1)))
5747 return 0;
5748
5749 /* Get the mode to use should INNER not be a MEM, the mode for the position,
5750 and the mode for the result. */
5751 #ifdef HAVE_insv
5752 if (in_dest)
5753 {
5754 wanted_inner_reg_mode
5755 = (insn_operand_mode[(int) CODE_FOR_insv][0] == VOIDmode
5756 ? word_mode
5757 : insn_operand_mode[(int) CODE_FOR_insv][0]);
5758 pos_mode = (insn_operand_mode[(int) CODE_FOR_insv][2] == VOIDmode
5759 ? word_mode : insn_operand_mode[(int) CODE_FOR_insv][2]);
5760 extraction_mode = (insn_operand_mode[(int) CODE_FOR_insv][3] == VOIDmode
5761 ? word_mode
5762 : insn_operand_mode[(int) CODE_FOR_insv][3]);
5763 }
5764 #endif
5765
5766 #ifdef HAVE_extzv
5767 if (! in_dest && unsignedp)
5768 {
5769 wanted_inner_reg_mode
5770 = (insn_operand_mode[(int) CODE_FOR_extzv][1] == VOIDmode
5771 ? word_mode
5772 : insn_operand_mode[(int) CODE_FOR_extzv][1]);
5773 pos_mode = (insn_operand_mode[(int) CODE_FOR_extzv][3] == VOIDmode
5774 ? word_mode : insn_operand_mode[(int) CODE_FOR_extzv][3]);
5775 extraction_mode = (insn_operand_mode[(int) CODE_FOR_extzv][0] == VOIDmode
5776 ? word_mode
5777 : insn_operand_mode[(int) CODE_FOR_extzv][0]);
5778 }
5779 #endif
5780
5781 #ifdef HAVE_extv
5782 if (! in_dest && ! unsignedp)
5783 {
5784 wanted_inner_reg_mode
5785 = (insn_operand_mode[(int) CODE_FOR_extv][1] == VOIDmode
5786 ? word_mode
5787 : insn_operand_mode[(int) CODE_FOR_extv][1]);
5788 pos_mode = (insn_operand_mode[(int) CODE_FOR_extv][3] == VOIDmode
5789 ? word_mode : insn_operand_mode[(int) CODE_FOR_extv][3]);
5790 extraction_mode = (insn_operand_mode[(int) CODE_FOR_extv][0] == VOIDmode
5791 ? word_mode
5792 : insn_operand_mode[(int) CODE_FOR_extv][0]);
5793 }
5794 #endif
5795
5796 /* Never narrow an object, since that might not be safe. */
5797
5798 if (mode != VOIDmode
5799 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5800 extraction_mode = mode;
5801
5802 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5803 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5804 pos_mode = GET_MODE (pos_rtx);
5805
5806 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5807 if we have to change the mode of memory and cannot, the desired mode is
5808 EXTRACTION_MODE. */
5809 if (GET_CODE (inner) != MEM)
5810 wanted_inner_mode = wanted_inner_reg_mode;
5811 else if (inner_mode != wanted_inner_mode
5812 && (mode_dependent_address_p (XEXP (inner, 0))
5813 || MEM_VOLATILE_P (inner)))
5814 wanted_inner_mode = extraction_mode;
5815
5816 orig_pos = pos;
5817
5818 if (BITS_BIG_ENDIAN)
5819 {
5820 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5821 BITS_BIG_ENDIAN style. If position is constant, compute new
5822 position. Otherwise, build subtraction.
5823 Note that POS is relative to the mode of the original argument.
5824 If it's a MEM we need to recompute POS relative to that.
5825 However, if we're extracting from (or inserting into) a register,
5826 we want to recompute POS relative to wanted_inner_mode. */
5827 int width = (GET_CODE (inner) == MEM
5828 ? GET_MODE_BITSIZE (is_mode)
5829 : GET_MODE_BITSIZE (wanted_inner_mode));
5830
5831 if (pos_rtx == 0)
5832 pos = width - len - pos;
5833 else
5834 pos_rtx
5835 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5836 GEN_INT (width - len), pos_rtx);
5837 /* POS may be less than 0 now, but we check for that below.
5838 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
5839 }
5840
5841 /* If INNER has a wider mode, make it smaller. If this is a constant
5842 extract, try to adjust the byte to point to the byte containing
5843 the value. */
5844 if (wanted_inner_mode != VOIDmode
5845 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5846 && ((GET_CODE (inner) == MEM
5847 && (inner_mode == wanted_inner_mode
5848 || (! mode_dependent_address_p (XEXP (inner, 0))
5849 && ! MEM_VOLATILE_P (inner))))))
5850 {
5851 int offset = 0;
5852
5853 /* The computations below will be correct if the machine is big
5854 endian in both bits and bytes or little endian in bits and bytes.
5855 If it is mixed, we must adjust. */
5856
5857 /* If bytes are big endian and we had a paradoxical SUBREG, we must
5858 adjust OFFSET to compensate. */
5859 if (BYTES_BIG_ENDIAN
5860 && ! spans_byte
5861 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5862 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5863
5864 /* If this is a constant position, we can move to the desired byte. */
5865 if (pos_rtx == 0)
5866 {
5867 offset += pos / BITS_PER_UNIT;
5868 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5869 }
5870
5871 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5872 && ! spans_byte
5873 && is_mode != wanted_inner_mode)
5874 offset = (GET_MODE_SIZE (is_mode)
5875 - GET_MODE_SIZE (wanted_inner_mode) - offset);
5876
5877 if (offset != 0 || inner_mode != wanted_inner_mode)
5878 {
5879 rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5880 plus_constant (XEXP (inner, 0), offset));
5881 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5882 MEM_COPY_ATTRIBUTES (newmem, inner);
5883 inner = newmem;
5884 }
5885 }
5886
5887 /* If INNER is not memory, we can always get it into the proper mode. If we
5888 are changing its mode, POS must be a constant and smaller than the size
5889 of the new mode. */
5890 else if (GET_CODE (inner) != MEM)
5891 {
5892 if (GET_MODE (inner) != wanted_inner_mode
5893 && (pos_rtx != 0
5894 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5895 return 0;
5896
5897 inner = force_to_mode (inner, wanted_inner_mode,
5898 pos_rtx
5899 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5900 ? GET_MODE_MASK (wanted_inner_mode)
5901 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5902 NULL_RTX, 0);
5903 }
5904
5905 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
5906 have to zero extend. Otherwise, we can just use a SUBREG. */
5907 if (pos_rtx != 0
5908 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5909 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5910 else if (pos_rtx != 0
5911 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5912 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5913
5914 /* Make POS_RTX unless we already have it and it is correct. If we don't
5915 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5916 be a CONST_INT. */
5917 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5918 pos_rtx = orig_pos_rtx;
5919
5920 else if (pos_rtx == 0)
5921 pos_rtx = GEN_INT (pos);
5922
5923 /* Make the required operation. See if we can use existing rtx. */
5924 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5925 extraction_mode, inner, GEN_INT (len), pos_rtx);
5926 if (! in_dest)
5927 new = gen_lowpart_for_combine (mode, new);
5928
5929 return new;
5930 }
5931 \f
5932 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5933 with any other operations in X. Return X without that shift if so. */
5934
5935 static rtx
5936 extract_left_shift (x, count)
5937 rtx x;
5938 int count;
5939 {
5940 enum rtx_code code = GET_CODE (x);
5941 enum machine_mode mode = GET_MODE (x);
5942 rtx tem;
5943
5944 switch (code)
5945 {
5946 case ASHIFT:
5947 /* This is the shift itself. If it is wide enough, we will return
5948 either the value being shifted if the shift count is equal to
5949 COUNT or a shift for the difference. */
5950 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5951 && INTVAL (XEXP (x, 1)) >= count)
5952 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5953 INTVAL (XEXP (x, 1)) - count);
5954 break;
5955
5956 case NEG: case NOT:
5957 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5958 return gen_unary (code, mode, mode, tem);
5959
5960 break;
5961
5962 case PLUS: case IOR: case XOR: case AND:
5963 /* If we can safely shift this constant and we find the inner shift,
5964 make a new operation. */
5965 if (GET_CODE (XEXP (x,1)) == CONST_INT
5966 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
5967 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5968 return gen_binary (code, mode, tem,
5969 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5970
5971 break;
5972
5973 default:
5974 break;
5975 }
5976
5977 return 0;
5978 }
5979 \f
5980 /* Look at the expression rooted at X. Look for expressions
5981 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5982 Form these expressions.
5983
5984 Return the new rtx, usually just X.
5985
5986 Also, for machines like the Vax that don't have logical shift insns,
5987 try to convert logical to arithmetic shift operations in cases where
5988 they are equivalent. This undoes the canonicalizations to logical
5989 shifts done elsewhere.
5990
5991 We try, as much as possible, to re-use rtl expressions to save memory.
5992
5993 IN_CODE says what kind of expression we are processing. Normally, it is
5994 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
5995 being kludges), it is MEM. When processing the arguments of a comparison
5996 or a COMPARE against zero, it is COMPARE. */
5997
5998 static rtx
5999 make_compound_operation (x, in_code)
6000 rtx x;
6001 enum rtx_code in_code;
6002 {
6003 enum rtx_code code = GET_CODE (x);
6004 enum machine_mode mode = GET_MODE (x);
6005 int mode_width = GET_MODE_BITSIZE (mode);
6006 rtx rhs, lhs;
6007 enum rtx_code next_code;
6008 int i;
6009 rtx new = 0;
6010 rtx tem;
6011 const char *fmt;
6012
6013 /* Select the code to be used in recursive calls. Once we are inside an
6014 address, we stay there. If we have a comparison, set to COMPARE,
6015 but once inside, go back to our default of SET. */
6016
6017 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6018 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6019 && XEXP (x, 1) == const0_rtx) ? COMPARE
6020 : in_code == COMPARE ? SET : in_code);
6021
6022 /* Process depending on the code of this operation. If NEW is set
6023 non-zero, it will be returned. */
6024
6025 switch (code)
6026 {
6027 case ASHIFT:
6028 /* Convert shifts by constants into multiplications if inside
6029 an address. */
6030 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6031 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6032 && INTVAL (XEXP (x, 1)) >= 0)
6033 {
6034 new = make_compound_operation (XEXP (x, 0), next_code);
6035 new = gen_rtx_combine (MULT, mode, new,
6036 GEN_INT ((HOST_WIDE_INT) 1
6037 << INTVAL (XEXP (x, 1))));
6038 }
6039 break;
6040
6041 case AND:
6042 /* If the second operand is not a constant, we can't do anything
6043 with it. */
6044 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6045 break;
6046
6047 /* If the constant is a power of two minus one and the first operand
6048 is a logical right shift, make an extraction. */
6049 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6050 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6051 {
6052 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6053 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6054 0, in_code == COMPARE);
6055 }
6056
6057 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6058 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6059 && subreg_lowpart_p (XEXP (x, 0))
6060 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6061 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6062 {
6063 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6064 next_code);
6065 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6066 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6067 0, in_code == COMPARE);
6068 }
6069 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6070 else if ((GET_CODE (XEXP (x, 0)) == XOR
6071 || GET_CODE (XEXP (x, 0)) == IOR)
6072 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6073 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6074 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6075 {
6076 /* Apply the distributive law, and then try to make extractions. */
6077 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
6078 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6079 XEXP (x, 1)),
6080 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6081 XEXP (x, 1)));
6082 new = make_compound_operation (new, in_code);
6083 }
6084
6085 /* If we are have (and (rotate X C) M) and C is larger than the number
6086 of bits in M, this is an extraction. */
6087
6088 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6089 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6090 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6091 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6092 {
6093 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6094 new = make_extraction (mode, new,
6095 (GET_MODE_BITSIZE (mode)
6096 - INTVAL (XEXP (XEXP (x, 0), 1))),
6097 NULL_RTX, i, 1, 0, in_code == COMPARE);
6098 }
6099
6100 /* On machines without logical shifts, if the operand of the AND is
6101 a logical shift and our mask turns off all the propagated sign
6102 bits, we can replace the logical shift with an arithmetic shift. */
6103 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6104 && (lshr_optab->handlers[(int) mode].insn_code
6105 == CODE_FOR_nothing)
6106 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6107 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6108 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6109 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6110 && mode_width <= HOST_BITS_PER_WIDE_INT)
6111 {
6112 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6113
6114 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6115 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6116 SUBST (XEXP (x, 0),
6117 gen_rtx_combine (ASHIFTRT, mode,
6118 make_compound_operation (XEXP (XEXP (x, 0), 0),
6119 next_code),
6120 XEXP (XEXP (x, 0), 1)));
6121 }
6122
6123 /* If the constant is one less than a power of two, this might be
6124 representable by an extraction even if no shift is present.
6125 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6126 we are in a COMPARE. */
6127 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6128 new = make_extraction (mode,
6129 make_compound_operation (XEXP (x, 0),
6130 next_code),
6131 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6132
6133 /* If we are in a comparison and this is an AND with a power of two,
6134 convert this into the appropriate bit extract. */
6135 else if (in_code == COMPARE
6136 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6137 new = make_extraction (mode,
6138 make_compound_operation (XEXP (x, 0),
6139 next_code),
6140 i, NULL_RTX, 1, 1, 0, 1);
6141
6142 break;
6143
6144 case LSHIFTRT:
6145 /* If the sign bit is known to be zero, replace this with an
6146 arithmetic shift. */
6147 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
6148 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6149 && mode_width <= HOST_BITS_PER_WIDE_INT
6150 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6151 {
6152 new = gen_rtx_combine (ASHIFTRT, mode,
6153 make_compound_operation (XEXP (x, 0),
6154 next_code),
6155 XEXP (x, 1));
6156 break;
6157 }
6158
6159 /* ... fall through ... */
6160
6161 case ASHIFTRT:
6162 lhs = XEXP (x, 0);
6163 rhs = XEXP (x, 1);
6164
6165 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6166 this is a SIGN_EXTRACT. */
6167 if (GET_CODE (rhs) == CONST_INT
6168 && GET_CODE (lhs) == ASHIFT
6169 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6170 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6171 {
6172 new = make_compound_operation (XEXP (lhs, 0), next_code);
6173 new = make_extraction (mode, new,
6174 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6175 NULL_RTX, mode_width - INTVAL (rhs),
6176 code == LSHIFTRT, 0, in_code == COMPARE);
6177 }
6178
6179 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6180 If so, try to merge the shifts into a SIGN_EXTEND. We could
6181 also do this for some cases of SIGN_EXTRACT, but it doesn't
6182 seem worth the effort; the case checked for occurs on Alpha. */
6183
6184 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6185 && ! (GET_CODE (lhs) == SUBREG
6186 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6187 && GET_CODE (rhs) == CONST_INT
6188 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6189 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6190 new = make_extraction (mode, make_compound_operation (new, next_code),
6191 0, NULL_RTX, mode_width - INTVAL (rhs),
6192 code == LSHIFTRT, 0, in_code == COMPARE);
6193
6194 break;
6195
6196 case SUBREG:
6197 /* Call ourselves recursively on the inner expression. If we are
6198 narrowing the object and it has a different RTL code from
6199 what it originally did, do this SUBREG as a force_to_mode. */
6200
6201 tem = make_compound_operation (SUBREG_REG (x), in_code);
6202 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6203 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6204 && subreg_lowpart_p (x))
6205 {
6206 rtx newer = force_to_mode (tem, mode,
6207 GET_MODE_MASK (mode), NULL_RTX, 0);
6208
6209 /* If we have something other than a SUBREG, we might have
6210 done an expansion, so rerun outselves. */
6211 if (GET_CODE (newer) != SUBREG)
6212 newer = make_compound_operation (newer, in_code);
6213
6214 return newer;
6215 }
6216
6217 /* If this is a paradoxical subreg, and the new code is a sign or
6218 zero extension, omit the subreg and widen the extension. If it
6219 is a regular subreg, we can still get rid of the subreg by not
6220 widening so much, or in fact removing the extension entirely. */
6221 if ((GET_CODE (tem) == SIGN_EXTEND
6222 || GET_CODE (tem) == ZERO_EXTEND)
6223 && subreg_lowpart_p (x))
6224 {
6225 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6226 || (GET_MODE_SIZE (mode) >
6227 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6228 tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6229 else
6230 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6231 return tem;
6232 }
6233 break;
6234
6235 default:
6236 break;
6237 }
6238
6239 if (new)
6240 {
6241 x = gen_lowpart_for_combine (mode, new);
6242 code = GET_CODE (x);
6243 }
6244
6245 /* Now recursively process each operand of this operation. */
6246 fmt = GET_RTX_FORMAT (code);
6247 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6248 if (fmt[i] == 'e')
6249 {
6250 new = make_compound_operation (XEXP (x, i), next_code);
6251 SUBST (XEXP (x, i), new);
6252 }
6253
6254 return x;
6255 }
6256 \f
6257 /* Given M see if it is a value that would select a field of bits
6258 within an item, but not the entire word. Return -1 if not.
6259 Otherwise, return the starting position of the field, where 0 is the
6260 low-order bit.
6261
6262 *PLEN is set to the length of the field. */
6263
6264 static int
6265 get_pos_from_mask (m, plen)
6266 unsigned HOST_WIDE_INT m;
6267 int *plen;
6268 {
6269 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6270 int pos = exact_log2 (m & - m);
6271
6272 if (pos < 0)
6273 return -1;
6274
6275 /* Now shift off the low-order zero bits and see if we have a power of
6276 two minus 1. */
6277 *plen = exact_log2 ((m >> pos) + 1);
6278
6279 if (*plen <= 0)
6280 return -1;
6281
6282 return pos;
6283 }
6284 \f
6285 /* See if X can be simplified knowing that we will only refer to it in
6286 MODE and will only refer to those bits that are nonzero in MASK.
6287 If other bits are being computed or if masking operations are done
6288 that select a superset of the bits in MASK, they can sometimes be
6289 ignored.
6290
6291 Return a possibly simplified expression, but always convert X to
6292 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6293
6294 Also, if REG is non-zero and X is a register equal in value to REG,
6295 replace X with REG.
6296
6297 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6298 are all off in X. This is used when X will be complemented, by either
6299 NOT, NEG, or XOR. */
6300
6301 static rtx
6302 force_to_mode (x, mode, mask, reg, just_select)
6303 rtx x;
6304 enum machine_mode mode;
6305 unsigned HOST_WIDE_INT mask;
6306 rtx reg;
6307 int just_select;
6308 {
6309 enum rtx_code code = GET_CODE (x);
6310 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6311 enum machine_mode op_mode;
6312 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6313 rtx op0, op1, temp;
6314
6315 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6316 code below will do the wrong thing since the mode of such an
6317 expression is VOIDmode.
6318
6319 Also do nothing if X is a CLOBBER; this can happen if X was
6320 the return value from a call to gen_lowpart_for_combine. */
6321 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6322 return x;
6323
6324 /* We want to perform the operation is its present mode unless we know
6325 that the operation is valid in MODE, in which case we do the operation
6326 in MODE. */
6327 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6328 && code_to_optab[(int) code] != 0
6329 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6330 != CODE_FOR_nothing))
6331 ? mode : GET_MODE (x));
6332
6333 /* It is not valid to do a right-shift in a narrower mode
6334 than the one it came in with. */
6335 if ((code == LSHIFTRT || code == ASHIFTRT)
6336 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6337 op_mode = GET_MODE (x);
6338
6339 /* Truncate MASK to fit OP_MODE. */
6340 if (op_mode)
6341 mask &= GET_MODE_MASK (op_mode);
6342
6343 /* When we have an arithmetic operation, or a shift whose count we
6344 do not know, we need to assume that all bit the up to the highest-order
6345 bit in MASK will be needed. This is how we form such a mask. */
6346 if (op_mode)
6347 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6348 ? GET_MODE_MASK (op_mode)
6349 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6350 else
6351 fuller_mask = ~ (HOST_WIDE_INT) 0;
6352
6353 /* Determine what bits of X are guaranteed to be (non)zero. */
6354 nonzero = nonzero_bits (x, mode);
6355
6356 /* If none of the bits in X are needed, return a zero. */
6357 if (! just_select && (nonzero & mask) == 0)
6358 return const0_rtx;
6359
6360 /* If X is a CONST_INT, return a new one. Do this here since the
6361 test below will fail. */
6362 if (GET_CODE (x) == CONST_INT)
6363 {
6364 HOST_WIDE_INT cval = INTVAL (x) & mask;
6365 int width = GET_MODE_BITSIZE (mode);
6366
6367 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6368 number, sign extend it. */
6369 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6370 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6371 cval |= (HOST_WIDE_INT) -1 << width;
6372
6373 return GEN_INT (cval);
6374 }
6375
6376 /* If X is narrower than MODE and we want all the bits in X's mode, just
6377 get X in the proper mode. */
6378 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6379 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6380 return gen_lowpart_for_combine (mode, x);
6381
6382 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6383 MASK are already known to be zero in X, we need not do anything. */
6384 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6385 return x;
6386
6387 switch (code)
6388 {
6389 case CLOBBER:
6390 /* If X is a (clobber (const_int)), return it since we know we are
6391 generating something that won't match. */
6392 return x;
6393
6394 case USE:
6395 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6396 spanned the boundary of the MEM. If we are now masking so it is
6397 within that boundary, we don't need the USE any more. */
6398 if (! BITS_BIG_ENDIAN
6399 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6400 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6401 break;
6402
6403 case SIGN_EXTEND:
6404 case ZERO_EXTEND:
6405 case ZERO_EXTRACT:
6406 case SIGN_EXTRACT:
6407 x = expand_compound_operation (x);
6408 if (GET_CODE (x) != code)
6409 return force_to_mode (x, mode, mask, reg, next_select);
6410 break;
6411
6412 case REG:
6413 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6414 || rtx_equal_p (reg, get_last_value (x))))
6415 x = reg;
6416 break;
6417
6418 case SUBREG:
6419 if (subreg_lowpart_p (x)
6420 /* We can ignore the effect of this SUBREG if it narrows the mode or
6421 if the constant masks to zero all the bits the mode doesn't
6422 have. */
6423 && ((GET_MODE_SIZE (GET_MODE (x))
6424 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6425 || (0 == (mask
6426 & GET_MODE_MASK (GET_MODE (x))
6427 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6428 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6429 break;
6430
6431 case AND:
6432 /* If this is an AND with a constant, convert it into an AND
6433 whose constant is the AND of that constant with MASK. If it
6434 remains an AND of MASK, delete it since it is redundant. */
6435
6436 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6437 {
6438 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6439 mask & INTVAL (XEXP (x, 1)));
6440
6441 /* If X is still an AND, see if it is an AND with a mask that
6442 is just some low-order bits. If so, and it is MASK, we don't
6443 need it. */
6444
6445 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6446 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == mask)
6447 x = XEXP (x, 0);
6448
6449 /* If it remains an AND, try making another AND with the bits
6450 in the mode mask that aren't in MASK turned on. If the
6451 constant in the AND is wide enough, this might make a
6452 cheaper constant. */
6453
6454 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6455 && GET_MODE_MASK (GET_MODE (x)) != mask
6456 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6457 {
6458 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6459 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6460 int width = GET_MODE_BITSIZE (GET_MODE (x));
6461 rtx y;
6462
6463 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6464 number, sign extend it. */
6465 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6466 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6467 cval |= (HOST_WIDE_INT) -1 << width;
6468
6469 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6470 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6471 x = y;
6472 }
6473
6474 break;
6475 }
6476
6477 goto binop;
6478
6479 case PLUS:
6480 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6481 low-order bits (as in an alignment operation) and FOO is already
6482 aligned to that boundary, mask C1 to that boundary as well.
6483 This may eliminate that PLUS and, later, the AND. */
6484
6485 {
6486 int width = GET_MODE_BITSIZE (mode);
6487 unsigned HOST_WIDE_INT smask = mask;
6488
6489 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6490 number, sign extend it. */
6491
6492 if (width < HOST_BITS_PER_WIDE_INT
6493 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6494 smask |= (HOST_WIDE_INT) -1 << width;
6495
6496 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6497 && exact_log2 (- smask) >= 0)
6498 {
6499 #ifdef STACK_BIAS
6500 if (STACK_BIAS
6501 && (XEXP (x, 0) == stack_pointer_rtx
6502 || XEXP (x, 0) == frame_pointer_rtx))
6503 {
6504 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6505 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6506
6507 sp_mask &= ~ (sp_alignment - 1);
6508 if ((sp_mask & ~ smask) == 0
6509 && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ smask) != 0)
6510 return force_to_mode (plus_constant (XEXP (x, 0),
6511 ((INTVAL (XEXP (x, 1)) -
6512 STACK_BIAS) & smask)
6513 + STACK_BIAS),
6514 mode, smask, reg, next_select);
6515 }
6516 #endif
6517 if ((nonzero_bits (XEXP (x, 0), mode) & ~ smask) == 0
6518 && (INTVAL (XEXP (x, 1)) & ~ smask) != 0)
6519 return force_to_mode (plus_constant (XEXP (x, 0),
6520 (INTVAL (XEXP (x, 1))
6521 & smask)),
6522 mode, smask, reg, next_select);
6523 }
6524 }
6525
6526 /* ... fall through ... */
6527
6528 case MINUS:
6529 case MULT:
6530 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6531 most significant bit in MASK since carries from those bits will
6532 affect the bits we are interested in. */
6533 mask = fuller_mask;
6534 goto binop;
6535
6536 case IOR:
6537 case XOR:
6538 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6539 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6540 operation which may be a bitfield extraction. Ensure that the
6541 constant we form is not wider than the mode of X. */
6542
6543 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6544 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6545 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6546 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6547 && GET_CODE (XEXP (x, 1)) == CONST_INT
6548 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6549 + floor_log2 (INTVAL (XEXP (x, 1))))
6550 < GET_MODE_BITSIZE (GET_MODE (x)))
6551 && (INTVAL (XEXP (x, 1))
6552 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6553 {
6554 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6555 << INTVAL (XEXP (XEXP (x, 0), 1)));
6556 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6557 XEXP (XEXP (x, 0), 0), temp);
6558 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6559 XEXP (XEXP (x, 0), 1));
6560 return force_to_mode (x, mode, mask, reg, next_select);
6561 }
6562
6563 binop:
6564 /* For most binary operations, just propagate into the operation and
6565 change the mode if we have an operation of that mode. */
6566
6567 op0 = gen_lowpart_for_combine (op_mode,
6568 force_to_mode (XEXP (x, 0), mode, mask,
6569 reg, next_select));
6570 op1 = gen_lowpart_for_combine (op_mode,
6571 force_to_mode (XEXP (x, 1), mode, mask,
6572 reg, next_select));
6573
6574 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6575 MASK since OP1 might have been sign-extended but we never want
6576 to turn on extra bits, since combine might have previously relied
6577 on them being off. */
6578 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6579 && (INTVAL (op1) & mask) != 0)
6580 op1 = GEN_INT (INTVAL (op1) & mask);
6581
6582 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6583 x = gen_binary (code, op_mode, op0, op1);
6584 break;
6585
6586 case ASHIFT:
6587 /* For left shifts, do the same, but just for the first operand.
6588 However, we cannot do anything with shifts where we cannot
6589 guarantee that the counts are smaller than the size of the mode
6590 because such a count will have a different meaning in a
6591 wider mode. */
6592
6593 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6594 && INTVAL (XEXP (x, 1)) >= 0
6595 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6596 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6597 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6598 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6599 break;
6600
6601 /* If the shift count is a constant and we can do arithmetic in
6602 the mode of the shift, refine which bits we need. Otherwise, use the
6603 conservative form of the mask. */
6604 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6605 && INTVAL (XEXP (x, 1)) >= 0
6606 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6607 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6608 mask >>= INTVAL (XEXP (x, 1));
6609 else
6610 mask = fuller_mask;
6611
6612 op0 = gen_lowpart_for_combine (op_mode,
6613 force_to_mode (XEXP (x, 0), op_mode,
6614 mask, reg, next_select));
6615
6616 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6617 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
6618 break;
6619
6620 case LSHIFTRT:
6621 /* Here we can only do something if the shift count is a constant,
6622 this shift constant is valid for the host, and we can do arithmetic
6623 in OP_MODE. */
6624
6625 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6626 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6627 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6628 {
6629 rtx inner = XEXP (x, 0);
6630
6631 /* Select the mask of the bits we need for the shift operand. */
6632 mask <<= INTVAL (XEXP (x, 1));
6633
6634 /* We can only change the mode of the shift if we can do arithmetic
6635 in the mode of the shift and MASK is no wider than the width of
6636 OP_MODE. */
6637 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6638 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6639 op_mode = GET_MODE (x);
6640
6641 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6642
6643 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6644 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6645 }
6646
6647 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6648 shift and AND produces only copies of the sign bit (C2 is one less
6649 than a power of two), we can do this with just a shift. */
6650
6651 if (GET_CODE (x) == LSHIFTRT
6652 && GET_CODE (XEXP (x, 1)) == CONST_INT
6653 && ((INTVAL (XEXP (x, 1))
6654 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6655 >= GET_MODE_BITSIZE (GET_MODE (x)))
6656 && exact_log2 (mask + 1) >= 0
6657 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6658 >= exact_log2 (mask + 1)))
6659 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6660 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6661 - exact_log2 (mask + 1)));
6662
6663 goto shiftrt;
6664
6665 case ASHIFTRT:
6666 /* If we are just looking for the sign bit, we don't need this shift at
6667 all, even if it has a variable count. */
6668 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6669 && (mask == ((unsigned HOST_WIDE_INT) 1
6670 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6671 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6672
6673 /* If this is a shift by a constant, get a mask that contains those bits
6674 that are not copies of the sign bit. We then have two cases: If
6675 MASK only includes those bits, this can be a logical shift, which may
6676 allow simplifications. If MASK is a single-bit field not within
6677 those bits, we are requesting a copy of the sign bit and hence can
6678 shift the sign bit to the appropriate location. */
6679
6680 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6681 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6682 {
6683 int i = -1;
6684
6685 /* If the considered data is wider then HOST_WIDE_INT, we can't
6686 represent a mask for all its bits in a single scalar.
6687 But we only care about the lower bits, so calculate these. */
6688
6689 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6690 {
6691 nonzero = ~ (HOST_WIDE_INT) 0;
6692
6693 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6694 is the number of bits a full-width mask would have set.
6695 We need only shift if these are fewer than nonzero can
6696 hold. If not, we must keep all bits set in nonzero. */
6697
6698 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6699 < HOST_BITS_PER_WIDE_INT)
6700 nonzero >>= INTVAL (XEXP (x, 1))
6701 + HOST_BITS_PER_WIDE_INT
6702 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6703 }
6704 else
6705 {
6706 nonzero = GET_MODE_MASK (GET_MODE (x));
6707 nonzero >>= INTVAL (XEXP (x, 1));
6708 }
6709
6710 if ((mask & ~ nonzero) == 0
6711 || (i = exact_log2 (mask)) >= 0)
6712 {
6713 x = simplify_shift_const
6714 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6715 i < 0 ? INTVAL (XEXP (x, 1))
6716 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6717
6718 if (GET_CODE (x) != ASHIFTRT)
6719 return force_to_mode (x, mode, mask, reg, next_select);
6720 }
6721 }
6722
6723 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6724 even if the shift count isn't a constant. */
6725 if (mask == 1)
6726 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6727
6728 shiftrt:
6729
6730 /* If this is a zero- or sign-extension operation that just affects bits
6731 we don't care about, remove it. Be sure the call above returned
6732 something that is still a shift. */
6733
6734 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6735 && GET_CODE (XEXP (x, 1)) == CONST_INT
6736 && INTVAL (XEXP (x, 1)) >= 0
6737 && (INTVAL (XEXP (x, 1))
6738 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6739 && GET_CODE (XEXP (x, 0)) == ASHIFT
6740 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6741 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6742 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6743 reg, next_select);
6744
6745 break;
6746
6747 case ROTATE:
6748 case ROTATERT:
6749 /* If the shift count is constant and we can do computations
6750 in the mode of X, compute where the bits we care about are.
6751 Otherwise, we can't do anything. Don't change the mode of
6752 the shift or propagate MODE into the shift, though. */
6753 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6754 && INTVAL (XEXP (x, 1)) >= 0)
6755 {
6756 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6757 GET_MODE (x), GEN_INT (mask),
6758 XEXP (x, 1));
6759 if (temp && GET_CODE(temp) == CONST_INT)
6760 SUBST (XEXP (x, 0),
6761 force_to_mode (XEXP (x, 0), GET_MODE (x),
6762 INTVAL (temp), reg, next_select));
6763 }
6764 break;
6765
6766 case NEG:
6767 /* If we just want the low-order bit, the NEG isn't needed since it
6768 won't change the low-order bit. */
6769 if (mask == 1)
6770 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6771
6772 /* We need any bits less significant than the most significant bit in
6773 MASK since carries from those bits will affect the bits we are
6774 interested in. */
6775 mask = fuller_mask;
6776 goto unop;
6777
6778 case NOT:
6779 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6780 same as the XOR case above. Ensure that the constant we form is not
6781 wider than the mode of X. */
6782
6783 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6784 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6785 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6786 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6787 < GET_MODE_BITSIZE (GET_MODE (x)))
6788 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6789 {
6790 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6791 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6792 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6793
6794 return force_to_mode (x, mode, mask, reg, next_select);
6795 }
6796
6797 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6798 use the full mask inside the NOT. */
6799 mask = fuller_mask;
6800
6801 unop:
6802 op0 = gen_lowpart_for_combine (op_mode,
6803 force_to_mode (XEXP (x, 0), mode, mask,
6804 reg, next_select));
6805 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6806 x = gen_unary (code, op_mode, op_mode, op0);
6807 break;
6808
6809 case NE:
6810 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6811 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6812 which is equal to STORE_FLAG_VALUE. */
6813 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6814 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6815 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6816 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6817
6818 break;
6819
6820 case IF_THEN_ELSE:
6821 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6822 written in a narrower mode. We play it safe and do not do so. */
6823
6824 SUBST (XEXP (x, 1),
6825 gen_lowpart_for_combine (GET_MODE (x),
6826 force_to_mode (XEXP (x, 1), mode,
6827 mask, reg, next_select)));
6828 SUBST (XEXP (x, 2),
6829 gen_lowpart_for_combine (GET_MODE (x),
6830 force_to_mode (XEXP (x, 2), mode,
6831 mask, reg,next_select)));
6832 break;
6833
6834 default:
6835 break;
6836 }
6837
6838 /* Ensure we return a value of the proper mode. */
6839 return gen_lowpart_for_combine (mode, x);
6840 }
6841 \f
6842 /* Return nonzero if X is an expression that has one of two values depending on
6843 whether some other value is zero or nonzero. In that case, we return the
6844 value that is being tested, *PTRUE is set to the value if the rtx being
6845 returned has a nonzero value, and *PFALSE is set to the other alternative.
6846
6847 If we return zero, we set *PTRUE and *PFALSE to X. */
6848
6849 static rtx
6850 if_then_else_cond (x, ptrue, pfalse)
6851 rtx x;
6852 rtx *ptrue, *pfalse;
6853 {
6854 enum machine_mode mode = GET_MODE (x);
6855 enum rtx_code code = GET_CODE (x);
6856 int size = GET_MODE_BITSIZE (mode);
6857 rtx cond0, cond1, true0, true1, false0, false1;
6858 unsigned HOST_WIDE_INT nz;
6859
6860 /* If this is a unary operation whose operand has one of two values, apply
6861 our opcode to compute those values. */
6862 if (GET_RTX_CLASS (code) == '1'
6863 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6864 {
6865 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6866 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6867 return cond0;
6868 }
6869
6870 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6871 make can't possibly match and would suppress other optimizations. */
6872 else if (code == COMPARE)
6873 ;
6874
6875 /* If this is a binary operation, see if either side has only one of two
6876 values. If either one does or if both do and they are conditional on
6877 the same value, compute the new true and false values. */
6878 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6879 || GET_RTX_CLASS (code) == '<')
6880 {
6881 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6882 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6883
6884 if ((cond0 != 0 || cond1 != 0)
6885 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6886 {
6887 /* If if_then_else_cond returned zero, then true/false are the
6888 same rtl. We must copy one of them to prevent invalid rtl
6889 sharing. */
6890 if (cond0 == 0)
6891 true0 = copy_rtx (true0);
6892 else if (cond1 == 0)
6893 true1 = copy_rtx (true1);
6894
6895 *ptrue = gen_binary (code, mode, true0, true1);
6896 *pfalse = gen_binary (code, mode, false0, false1);
6897 return cond0 ? cond0 : cond1;
6898 }
6899
6900 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6901 operands is zero when the other is non-zero, and vice-versa,
6902 and STORE_FLAG_VALUE is 1 or -1. */
6903
6904 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6905 && (code == PLUS || code == IOR || code == XOR || code == MINUS
6906 || code == UMAX)
6907 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6908 {
6909 rtx op0 = XEXP (XEXP (x, 0), 1);
6910 rtx op1 = XEXP (XEXP (x, 1), 1);
6911
6912 cond0 = XEXP (XEXP (x, 0), 0);
6913 cond1 = XEXP (XEXP (x, 1), 0);
6914
6915 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6916 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6917 && reversible_comparison_p (cond1)
6918 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6919 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6920 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6921 || ((swap_condition (GET_CODE (cond0))
6922 == reverse_condition (GET_CODE (cond1)))
6923 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6924 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6925 && ! side_effects_p (x))
6926 {
6927 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6928 *pfalse = gen_binary (MULT, mode,
6929 (code == MINUS
6930 ? gen_unary (NEG, mode, mode, op1) : op1),
6931 const_true_rtx);
6932 return cond0;
6933 }
6934 }
6935
6936 /* Similarly for MULT, AND and UMIN, execpt that for these the result
6937 is always zero. */
6938 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6939 && (code == MULT || code == AND || code == UMIN)
6940 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6941 {
6942 cond0 = XEXP (XEXP (x, 0), 0);
6943 cond1 = XEXP (XEXP (x, 1), 0);
6944
6945 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6946 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6947 && reversible_comparison_p (cond1)
6948 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6949 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6950 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6951 || ((swap_condition (GET_CODE (cond0))
6952 == reverse_condition (GET_CODE (cond1)))
6953 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6954 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6955 && ! side_effects_p (x))
6956 {
6957 *ptrue = *pfalse = const0_rtx;
6958 return cond0;
6959 }
6960 }
6961 }
6962
6963 else if (code == IF_THEN_ELSE)
6964 {
6965 /* If we have IF_THEN_ELSE already, extract the condition and
6966 canonicalize it if it is NE or EQ. */
6967 cond0 = XEXP (x, 0);
6968 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6969 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6970 return XEXP (cond0, 0);
6971 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6972 {
6973 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6974 return XEXP (cond0, 0);
6975 }
6976 else
6977 return cond0;
6978 }
6979
6980 /* If X is a normal SUBREG with both inner and outer modes integral,
6981 we can narrow both the true and false values of the inner expression,
6982 if there is a condition. */
6983 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6984 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6985 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6986 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6987 &true0, &false0)))
6988 {
6989 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6990 *pfalse
6991 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6992
6993 return cond0;
6994 }
6995
6996 /* If X is a constant, this isn't special and will cause confusions
6997 if we treat it as such. Likewise if it is equivalent to a constant. */
6998 else if (CONSTANT_P (x)
6999 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7000 ;
7001
7002 /* If X is known to be either 0 or -1, those are the true and
7003 false values when testing X. */
7004 else if (num_sign_bit_copies (x, mode) == size)
7005 {
7006 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7007 return x;
7008 }
7009
7010 /* Likewise for 0 or a single bit. */
7011 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7012 {
7013 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
7014 return x;
7015 }
7016
7017 /* Otherwise fail; show no condition with true and false values the same. */
7018 *ptrue = *pfalse = x;
7019 return 0;
7020 }
7021 \f
7022 /* Return the value of expression X given the fact that condition COND
7023 is known to be true when applied to REG as its first operand and VAL
7024 as its second. X is known to not be shared and so can be modified in
7025 place.
7026
7027 We only handle the simplest cases, and specifically those cases that
7028 arise with IF_THEN_ELSE expressions. */
7029
7030 static rtx
7031 known_cond (x, cond, reg, val)
7032 rtx x;
7033 enum rtx_code cond;
7034 rtx reg, val;
7035 {
7036 enum rtx_code code = GET_CODE (x);
7037 rtx temp;
7038 const char *fmt;
7039 int i, j;
7040
7041 if (side_effects_p (x))
7042 return x;
7043
7044 if (cond == EQ && rtx_equal_p (x, reg))
7045 return val;
7046
7047 /* If X is (abs REG) and we know something about REG's relationship
7048 with zero, we may be able to simplify this. */
7049
7050 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7051 switch (cond)
7052 {
7053 case GE: case GT: case EQ:
7054 return XEXP (x, 0);
7055 case LT: case LE:
7056 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
7057 XEXP (x, 0));
7058 default:
7059 break;
7060 }
7061
7062 /* The only other cases we handle are MIN, MAX, and comparisons if the
7063 operands are the same as REG and VAL. */
7064
7065 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7066 {
7067 if (rtx_equal_p (XEXP (x, 0), val))
7068 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7069
7070 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7071 {
7072 if (GET_RTX_CLASS (code) == '<')
7073 return (comparison_dominates_p (cond, code) ? const_true_rtx
7074 : (comparison_dominates_p (cond,
7075 reverse_condition (code))
7076 ? const0_rtx : x));
7077
7078 else if (code == SMAX || code == SMIN
7079 || code == UMIN || code == UMAX)
7080 {
7081 int unsignedp = (code == UMIN || code == UMAX);
7082
7083 if (code == SMAX || code == UMAX)
7084 cond = reverse_condition (cond);
7085
7086 switch (cond)
7087 {
7088 case GE: case GT:
7089 return unsignedp ? x : XEXP (x, 1);
7090 case LE: case LT:
7091 return unsignedp ? x : XEXP (x, 0);
7092 case GEU: case GTU:
7093 return unsignedp ? XEXP (x, 1) : x;
7094 case LEU: case LTU:
7095 return unsignedp ? XEXP (x, 0) : x;
7096 default:
7097 break;
7098 }
7099 }
7100 }
7101 }
7102
7103 fmt = GET_RTX_FORMAT (code);
7104 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7105 {
7106 if (fmt[i] == 'e')
7107 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7108 else if (fmt[i] == 'E')
7109 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7110 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7111 cond, reg, val));
7112 }
7113
7114 return x;
7115 }
7116 \f
7117 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7118 assignment as a field assignment. */
7119
7120 static int
7121 rtx_equal_for_field_assignment_p (x, y)
7122 rtx x;
7123 rtx y;
7124 {
7125 if (x == y || rtx_equal_p (x, y))
7126 return 1;
7127
7128 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7129 return 0;
7130
7131 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7132 Note that all SUBREGs of MEM are paradoxical; otherwise they
7133 would have been rewritten. */
7134 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7135 && GET_CODE (SUBREG_REG (y)) == MEM
7136 && rtx_equal_p (SUBREG_REG (y),
7137 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7138 return 1;
7139
7140 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7141 && GET_CODE (SUBREG_REG (x)) == MEM
7142 && rtx_equal_p (SUBREG_REG (x),
7143 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7144 return 1;
7145
7146 /* We used to see if get_last_value of X and Y were the same but that's
7147 not correct. In one direction, we'll cause the assignment to have
7148 the wrong destination and in the case, we'll import a register into this
7149 insn that might have already have been dead. So fail if none of the
7150 above cases are true. */
7151 return 0;
7152 }
7153 \f
7154 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7155 Return that assignment if so.
7156
7157 We only handle the most common cases. */
7158
7159 static rtx
7160 make_field_assignment (x)
7161 rtx x;
7162 {
7163 rtx dest = SET_DEST (x);
7164 rtx src = SET_SRC (x);
7165 rtx assign;
7166 rtx rhs, lhs;
7167 HOST_WIDE_INT c1;
7168 int pos, len;
7169 rtx other;
7170 enum machine_mode mode;
7171
7172 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7173 a clear of a one-bit field. We will have changed it to
7174 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7175 for a SUBREG. */
7176
7177 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7178 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7179 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7180 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7181 {
7182 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7183 1, 1, 1, 0);
7184 if (assign != 0)
7185 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7186 return x;
7187 }
7188
7189 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7190 && subreg_lowpart_p (XEXP (src, 0))
7191 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7192 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7193 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7194 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7195 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7196 {
7197 assign = make_extraction (VOIDmode, dest, 0,
7198 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7199 1, 1, 1, 0);
7200 if (assign != 0)
7201 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7202 return x;
7203 }
7204
7205 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7206 one-bit field. */
7207 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7208 && XEXP (XEXP (src, 0), 0) == const1_rtx
7209 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7210 {
7211 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7212 1, 1, 1, 0);
7213 if (assign != 0)
7214 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7215 return x;
7216 }
7217
7218 /* The other case we handle is assignments into a constant-position
7219 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7220 a mask that has all one bits except for a group of zero bits and
7221 OTHER is known to have zeros where C1 has ones, this is such an
7222 assignment. Compute the position and length from C1. Shift OTHER
7223 to the appropriate position, force it to the required mode, and
7224 make the extraction. Check for the AND in both operands. */
7225
7226 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7227 return x;
7228
7229 rhs = expand_compound_operation (XEXP (src, 0));
7230 lhs = expand_compound_operation (XEXP (src, 1));
7231
7232 if (GET_CODE (rhs) == AND
7233 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7234 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7235 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7236 else if (GET_CODE (lhs) == AND
7237 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7238 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7239 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7240 else
7241 return x;
7242
7243 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7244 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7245 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7246 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7247 return x;
7248
7249 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7250 if (assign == 0)
7251 return x;
7252
7253 /* The mode to use for the source is the mode of the assignment, or of
7254 what is inside a possible STRICT_LOW_PART. */
7255 mode = (GET_CODE (assign) == STRICT_LOW_PART
7256 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7257
7258 /* Shift OTHER right POS places and make it the source, restricting it
7259 to the proper length and mode. */
7260
7261 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7262 GET_MODE (src), other, pos),
7263 mode,
7264 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7265 ? GET_MODE_MASK (mode)
7266 : ((HOST_WIDE_INT) 1 << len) - 1,
7267 dest, 0);
7268
7269 return gen_rtx_combine (SET, VOIDmode, assign, src);
7270 }
7271 \f
7272 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7273 if so. */
7274
7275 static rtx
7276 apply_distributive_law (x)
7277 rtx x;
7278 {
7279 enum rtx_code code = GET_CODE (x);
7280 rtx lhs, rhs, other;
7281 rtx tem;
7282 enum rtx_code inner_code;
7283
7284 /* Distributivity is not true for floating point.
7285 It can change the value. So don't do it.
7286 -- rms and moshier@world.std.com. */
7287 if (FLOAT_MODE_P (GET_MODE (x)))
7288 return x;
7289
7290 /* The outer operation can only be one of the following: */
7291 if (code != IOR && code != AND && code != XOR
7292 && code != PLUS && code != MINUS)
7293 return x;
7294
7295 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7296
7297 /* If either operand is a primitive we can't do anything, so get out
7298 fast. */
7299 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7300 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7301 return x;
7302
7303 lhs = expand_compound_operation (lhs);
7304 rhs = expand_compound_operation (rhs);
7305 inner_code = GET_CODE (lhs);
7306 if (inner_code != GET_CODE (rhs))
7307 return x;
7308
7309 /* See if the inner and outer operations distribute. */
7310 switch (inner_code)
7311 {
7312 case LSHIFTRT:
7313 case ASHIFTRT:
7314 case AND:
7315 case IOR:
7316 /* These all distribute except over PLUS. */
7317 if (code == PLUS || code == MINUS)
7318 return x;
7319 break;
7320
7321 case MULT:
7322 if (code != PLUS && code != MINUS)
7323 return x;
7324 break;
7325
7326 case ASHIFT:
7327 /* This is also a multiply, so it distributes over everything. */
7328 break;
7329
7330 case SUBREG:
7331 /* Non-paradoxical SUBREGs distributes over all operations, provided
7332 the inner modes and word numbers are the same, this is an extraction
7333 of a low-order part, we don't convert an fp operation to int or
7334 vice versa, and we would not be converting a single-word
7335 operation into a multi-word operation. The latter test is not
7336 required, but it prevents generating unneeded multi-word operations.
7337 Some of the previous tests are redundant given the latter test, but
7338 are retained because they are required for correctness.
7339
7340 We produce the result slightly differently in this case. */
7341
7342 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7343 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7344 || ! subreg_lowpart_p (lhs)
7345 || (GET_MODE_CLASS (GET_MODE (lhs))
7346 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7347 || (GET_MODE_SIZE (GET_MODE (lhs))
7348 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7349 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7350 return x;
7351
7352 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7353 SUBREG_REG (lhs), SUBREG_REG (rhs));
7354 return gen_lowpart_for_combine (GET_MODE (x), tem);
7355
7356 default:
7357 return x;
7358 }
7359
7360 /* Set LHS and RHS to the inner operands (A and B in the example
7361 above) and set OTHER to the common operand (C in the example).
7362 These is only one way to do this unless the inner operation is
7363 commutative. */
7364 if (GET_RTX_CLASS (inner_code) == 'c'
7365 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7366 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7367 else if (GET_RTX_CLASS (inner_code) == 'c'
7368 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7369 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7370 else if (GET_RTX_CLASS (inner_code) == 'c'
7371 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7372 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7373 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7374 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7375 else
7376 return x;
7377
7378 /* Form the new inner operation, seeing if it simplifies first. */
7379 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7380
7381 /* There is one exception to the general way of distributing:
7382 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7383 if (code == XOR && inner_code == IOR)
7384 {
7385 inner_code = AND;
7386 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7387 }
7388
7389 /* We may be able to continuing distributing the result, so call
7390 ourselves recursively on the inner operation before forming the
7391 outer operation, which we return. */
7392 return gen_binary (inner_code, GET_MODE (x),
7393 apply_distributive_law (tem), other);
7394 }
7395 \f
7396 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7397 in MODE.
7398
7399 Return an equivalent form, if different from X. Otherwise, return X. If
7400 X is zero, we are to always construct the equivalent form. */
7401
7402 static rtx
7403 simplify_and_const_int (x, mode, varop, constop)
7404 rtx x;
7405 enum machine_mode mode;
7406 rtx varop;
7407 unsigned HOST_WIDE_INT constop;
7408 {
7409 unsigned HOST_WIDE_INT nonzero;
7410 int i;
7411
7412 /* Simplify VAROP knowing that we will be only looking at some of the
7413 bits in it. */
7414 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7415
7416 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7417 CONST_INT, we are done. */
7418 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7419 return varop;
7420
7421 /* See what bits may be nonzero in VAROP. Unlike the general case of
7422 a call to nonzero_bits, here we don't care about bits outside
7423 MODE. */
7424
7425 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7426 nonzero = trunc_int_for_mode (nonzero, mode);
7427
7428 /* Turn off all bits in the constant that are known to already be zero.
7429 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7430 which is tested below. */
7431
7432 constop &= nonzero;
7433
7434 /* If we don't have any bits left, return zero. */
7435 if (constop == 0)
7436 return const0_rtx;
7437
7438 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7439 a power of two, we can replace this with a ASHIFT. */
7440 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7441 && (i = exact_log2 (constop)) >= 0)
7442 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7443
7444 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7445 or XOR, then try to apply the distributive law. This may eliminate
7446 operations if either branch can be simplified because of the AND.
7447 It may also make some cases more complex, but those cases probably
7448 won't match a pattern either with or without this. */
7449
7450 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7451 return
7452 gen_lowpart_for_combine
7453 (mode,
7454 apply_distributive_law
7455 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7456 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7457 XEXP (varop, 0), constop),
7458 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7459 XEXP (varop, 1), constop))));
7460
7461 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7462 if we already had one (just check for the simplest cases). */
7463 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7464 && GET_MODE (XEXP (x, 0)) == mode
7465 && SUBREG_REG (XEXP (x, 0)) == varop)
7466 varop = XEXP (x, 0);
7467 else
7468 varop = gen_lowpart_for_combine (mode, varop);
7469
7470 /* If we can't make the SUBREG, try to return what we were given. */
7471 if (GET_CODE (varop) == CLOBBER)
7472 return x ? x : varop;
7473
7474 /* If we are only masking insignificant bits, return VAROP. */
7475 if (constop == nonzero)
7476 x = varop;
7477
7478 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7479 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7480 x = gen_binary (AND, mode, varop, GEN_INT (constop));
7481
7482 else
7483 {
7484 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7485 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
7486 SUBST (XEXP (x, 1), GEN_INT (constop));
7487
7488 SUBST (XEXP (x, 0), varop);
7489 }
7490
7491 return x;
7492 }
7493 \f
7494 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7495 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7496 is less useful. We can't allow both, because that results in exponential
7497 run time recursion. There is a nullstone testcase that triggered
7498 this. This macro avoids accidental uses of num_sign_bit_copies. */
7499 #define num_sign_bit_copies()
7500
7501 /* Given an expression, X, compute which bits in X can be non-zero.
7502 We don't care about bits outside of those defined in MODE.
7503
7504 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7505 a shift, AND, or zero_extract, we can do better. */
7506
7507 static unsigned HOST_WIDE_INT
7508 nonzero_bits (x, mode)
7509 rtx x;
7510 enum machine_mode mode;
7511 {
7512 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7513 unsigned HOST_WIDE_INT inner_nz;
7514 enum rtx_code code;
7515 int mode_width = GET_MODE_BITSIZE (mode);
7516 rtx tem;
7517
7518 /* For floating-point values, assume all bits are needed. */
7519 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7520 return nonzero;
7521
7522 /* If X is wider than MODE, use its mode instead. */
7523 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7524 {
7525 mode = GET_MODE (x);
7526 nonzero = GET_MODE_MASK (mode);
7527 mode_width = GET_MODE_BITSIZE (mode);
7528 }
7529
7530 if (mode_width > HOST_BITS_PER_WIDE_INT)
7531 /* Our only callers in this case look for single bit values. So
7532 just return the mode mask. Those tests will then be false. */
7533 return nonzero;
7534
7535 #ifndef WORD_REGISTER_OPERATIONS
7536 /* If MODE is wider than X, but both are a single word for both the host
7537 and target machines, we can compute this from which bits of the
7538 object might be nonzero in its own mode, taking into account the fact
7539 that on many CISC machines, accessing an object in a wider mode
7540 causes the high-order bits to become undefined. So they are
7541 not known to be zero. */
7542
7543 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7544 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7545 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7546 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7547 {
7548 nonzero &= nonzero_bits (x, GET_MODE (x));
7549 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7550 return nonzero;
7551 }
7552 #endif
7553
7554 code = GET_CODE (x);
7555 switch (code)
7556 {
7557 case REG:
7558 #ifdef POINTERS_EXTEND_UNSIGNED
7559 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7560 all the bits above ptr_mode are known to be zero. */
7561 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7562 && REGNO_POINTER_FLAG (REGNO (x)))
7563 nonzero &= GET_MODE_MASK (ptr_mode);
7564 #endif
7565
7566 #ifdef STACK_BOUNDARY
7567 /* If this is the stack pointer, we may know something about its
7568 alignment. If PUSH_ROUNDING is defined, it is possible for the
7569 stack to be momentarily aligned only to that amount, so we pick
7570 the least alignment. */
7571
7572 /* We can't check for arg_pointer_rtx here, because it is not
7573 guaranteed to have as much alignment as the stack pointer.
7574 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7575 alignment but the argument pointer has only 64 bit alignment. */
7576
7577 if ((x == frame_pointer_rtx
7578 || x == stack_pointer_rtx
7579 || x == hard_frame_pointer_rtx
7580 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7581 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7582 #ifdef STACK_BIAS
7583 && !STACK_BIAS
7584 #endif
7585 )
7586 {
7587 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7588
7589 #ifdef PUSH_ROUNDING
7590 if (REGNO (x) == STACK_POINTER_REGNUM)
7591 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7592 #endif
7593
7594 /* We must return here, otherwise we may get a worse result from
7595 one of the choices below. There is nothing useful below as
7596 far as the stack pointer is concerned. */
7597 return nonzero &= ~ (sp_alignment - 1);
7598 }
7599 #endif
7600
7601 /* If X is a register whose nonzero bits value is current, use it.
7602 Otherwise, if X is a register whose value we can find, use that
7603 value. Otherwise, use the previously-computed global nonzero bits
7604 for this register. */
7605
7606 if (reg_last_set_value[REGNO (x)] != 0
7607 && reg_last_set_mode[REGNO (x)] == mode
7608 && (reg_last_set_label[REGNO (x)] == label_tick
7609 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
7610 && REG_N_SETS (REGNO (x)) == 1
7611 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
7612 REGNO (x))))
7613 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7614 return reg_last_set_nonzero_bits[REGNO (x)];
7615
7616 tem = get_last_value (x);
7617
7618 if (tem)
7619 {
7620 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7621 /* If X is narrower than MODE and TEM is a non-negative
7622 constant that would appear negative in the mode of X,
7623 sign-extend it for use in reg_nonzero_bits because some
7624 machines (maybe most) will actually do the sign-extension
7625 and this is the conservative approach.
7626
7627 ??? For 2.5, try to tighten up the MD files in this regard
7628 instead of this kludge. */
7629
7630 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7631 && GET_CODE (tem) == CONST_INT
7632 && INTVAL (tem) > 0
7633 && 0 != (INTVAL (tem)
7634 & ((HOST_WIDE_INT) 1
7635 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7636 tem = GEN_INT (INTVAL (tem)
7637 | ((HOST_WIDE_INT) (-1)
7638 << GET_MODE_BITSIZE (GET_MODE (x))));
7639 #endif
7640 return nonzero_bits (tem, mode);
7641 }
7642 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7643 return reg_nonzero_bits[REGNO (x)] & nonzero;
7644 else
7645 return nonzero;
7646
7647 case CONST_INT:
7648 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7649 /* If X is negative in MODE, sign-extend the value. */
7650 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7651 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7652 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7653 #endif
7654
7655 return INTVAL (x);
7656
7657 case MEM:
7658 #ifdef LOAD_EXTEND_OP
7659 /* In many, if not most, RISC machines, reading a byte from memory
7660 zeros the rest of the register. Noticing that fact saves a lot
7661 of extra zero-extends. */
7662 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7663 nonzero &= GET_MODE_MASK (GET_MODE (x));
7664 #endif
7665 break;
7666
7667 case EQ: case NE:
7668 case GT: case GTU:
7669 case LT: case LTU:
7670 case GE: case GEU:
7671 case LE: case LEU:
7672
7673 /* If this produces an integer result, we know which bits are set.
7674 Code here used to clear bits outside the mode of X, but that is
7675 now done above. */
7676
7677 if (GET_MODE_CLASS (mode) == MODE_INT
7678 && mode_width <= HOST_BITS_PER_WIDE_INT)
7679 nonzero = STORE_FLAG_VALUE;
7680 break;
7681
7682 case NEG:
7683 #if 0
7684 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7685 and num_sign_bit_copies. */
7686 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7687 == GET_MODE_BITSIZE (GET_MODE (x)))
7688 nonzero = 1;
7689 #endif
7690
7691 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7692 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7693 break;
7694
7695 case ABS:
7696 #if 0
7697 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7698 and num_sign_bit_copies. */
7699 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7700 == GET_MODE_BITSIZE (GET_MODE (x)))
7701 nonzero = 1;
7702 #endif
7703 break;
7704
7705 case TRUNCATE:
7706 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7707 break;
7708
7709 case ZERO_EXTEND:
7710 nonzero &= nonzero_bits (XEXP (x, 0), mode);
7711 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7712 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7713 break;
7714
7715 case SIGN_EXTEND:
7716 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7717 Otherwise, show all the bits in the outer mode but not the inner
7718 may be non-zero. */
7719 inner_nz = nonzero_bits (XEXP (x, 0), mode);
7720 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7721 {
7722 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7723 if (inner_nz
7724 & (((HOST_WIDE_INT) 1
7725 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7726 inner_nz |= (GET_MODE_MASK (mode)
7727 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7728 }
7729
7730 nonzero &= inner_nz;
7731 break;
7732
7733 case AND:
7734 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7735 & nonzero_bits (XEXP (x, 1), mode));
7736 break;
7737
7738 case XOR: case IOR:
7739 case UMIN: case UMAX: case SMIN: case SMAX:
7740 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7741 | nonzero_bits (XEXP (x, 1), mode));
7742 break;
7743
7744 case PLUS: case MINUS:
7745 case MULT:
7746 case DIV: case UDIV:
7747 case MOD: case UMOD:
7748 /* We can apply the rules of arithmetic to compute the number of
7749 high- and low-order zero bits of these operations. We start by
7750 computing the width (position of the highest-order non-zero bit)
7751 and the number of low-order zero bits for each value. */
7752 {
7753 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7754 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7755 int width0 = floor_log2 (nz0) + 1;
7756 int width1 = floor_log2 (nz1) + 1;
7757 int low0 = floor_log2 (nz0 & -nz0);
7758 int low1 = floor_log2 (nz1 & -nz1);
7759 HOST_WIDE_INT op0_maybe_minusp
7760 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7761 HOST_WIDE_INT op1_maybe_minusp
7762 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7763 int result_width = mode_width;
7764 int result_low = 0;
7765
7766 switch (code)
7767 {
7768 case PLUS:
7769 #ifdef STACK_BIAS
7770 if (STACK_BIAS
7771 && (XEXP (x, 0) == stack_pointer_rtx
7772 || XEXP (x, 0) == frame_pointer_rtx)
7773 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7774 {
7775 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7776
7777 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7778 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7779 width0 = floor_log2 (nz0) + 1;
7780 width1 = floor_log2 (nz1) + 1;
7781 low0 = floor_log2 (nz0 & -nz0);
7782 low1 = floor_log2 (nz1 & -nz1);
7783 }
7784 #endif
7785 result_width = MAX (width0, width1) + 1;
7786 result_low = MIN (low0, low1);
7787 break;
7788 case MINUS:
7789 result_low = MIN (low0, low1);
7790 break;
7791 case MULT:
7792 result_width = width0 + width1;
7793 result_low = low0 + low1;
7794 break;
7795 case DIV:
7796 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7797 result_width = width0;
7798 break;
7799 case UDIV:
7800 result_width = width0;
7801 break;
7802 case MOD:
7803 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7804 result_width = MIN (width0, width1);
7805 result_low = MIN (low0, low1);
7806 break;
7807 case UMOD:
7808 result_width = MIN (width0, width1);
7809 result_low = MIN (low0, low1);
7810 break;
7811 default:
7812 abort ();
7813 }
7814
7815 if (result_width < mode_width)
7816 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7817
7818 if (result_low > 0)
7819 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7820 }
7821 break;
7822
7823 case ZERO_EXTRACT:
7824 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7825 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7826 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7827 break;
7828
7829 case SUBREG:
7830 /* If this is a SUBREG formed for a promoted variable that has
7831 been zero-extended, we know that at least the high-order bits
7832 are zero, though others might be too. */
7833
7834 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7835 nonzero = (GET_MODE_MASK (GET_MODE (x))
7836 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7837
7838 /* If the inner mode is a single word for both the host and target
7839 machines, we can compute this from which bits of the inner
7840 object might be nonzero. */
7841 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7842 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7843 <= HOST_BITS_PER_WIDE_INT))
7844 {
7845 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7846
7847 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
7848 /* If this is a typical RISC machine, we only have to worry
7849 about the way loads are extended. */
7850 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
7851 ? (nonzero
7852 & (1L << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1)))
7853 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
7854 #endif
7855 {
7856 /* On many CISC machines, accessing an object in a wider mode
7857 causes the high-order bits to become undefined. So they are
7858 not known to be zero. */
7859 if (GET_MODE_SIZE (GET_MODE (x))
7860 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7861 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7862 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7863 }
7864 }
7865 break;
7866
7867 case ASHIFTRT:
7868 case LSHIFTRT:
7869 case ASHIFT:
7870 case ROTATE:
7871 /* The nonzero bits are in two classes: any bits within MODE
7872 that aren't in GET_MODE (x) are always significant. The rest of the
7873 nonzero bits are those that are significant in the operand of
7874 the shift when shifted the appropriate number of bits. This
7875 shows that high-order bits are cleared by the right shift and
7876 low-order bits by left shifts. */
7877 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7878 && INTVAL (XEXP (x, 1)) >= 0
7879 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7880 {
7881 enum machine_mode inner_mode = GET_MODE (x);
7882 int width = GET_MODE_BITSIZE (inner_mode);
7883 int count = INTVAL (XEXP (x, 1));
7884 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7885 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7886 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7887 unsigned HOST_WIDE_INT outer = 0;
7888
7889 if (mode_width > width)
7890 outer = (op_nonzero & nonzero & ~ mode_mask);
7891
7892 if (code == LSHIFTRT)
7893 inner >>= count;
7894 else if (code == ASHIFTRT)
7895 {
7896 inner >>= count;
7897
7898 /* If the sign bit may have been nonzero before the shift, we
7899 need to mark all the places it could have been copied to
7900 by the shift as possibly nonzero. */
7901 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7902 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7903 }
7904 else if (code == ASHIFT)
7905 inner <<= count;
7906 else
7907 inner = ((inner << (count % width)
7908 | (inner >> (width - (count % width)))) & mode_mask);
7909
7910 nonzero &= (outer | inner);
7911 }
7912 break;
7913
7914 case FFS:
7915 /* This is at most the number of bits in the mode. */
7916 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7917 break;
7918
7919 case IF_THEN_ELSE:
7920 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7921 | nonzero_bits (XEXP (x, 2), mode));
7922 break;
7923
7924 default:
7925 break;
7926 }
7927
7928 return nonzero;
7929 }
7930
7931 /* See the macro definition above. */
7932 #undef num_sign_bit_copies
7933 \f
7934 /* Return the number of bits at the high-order end of X that are known to
7935 be equal to the sign bit. X will be used in mode MODE; if MODE is
7936 VOIDmode, X will be used in its own mode. The returned value will always
7937 be between 1 and the number of bits in MODE. */
7938
7939 static int
7940 num_sign_bit_copies (x, mode)
7941 rtx x;
7942 enum machine_mode mode;
7943 {
7944 enum rtx_code code = GET_CODE (x);
7945 int bitwidth;
7946 int num0, num1, result;
7947 unsigned HOST_WIDE_INT nonzero;
7948 rtx tem;
7949
7950 /* If we weren't given a mode, use the mode of X. If the mode is still
7951 VOIDmode, we don't know anything. Likewise if one of the modes is
7952 floating-point. */
7953
7954 if (mode == VOIDmode)
7955 mode = GET_MODE (x);
7956
7957 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7958 return 1;
7959
7960 bitwidth = GET_MODE_BITSIZE (mode);
7961
7962 /* For a smaller object, just ignore the high bits. */
7963 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7964 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7965 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7966
7967 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7968 {
7969 #ifndef WORD_REGISTER_OPERATIONS
7970 /* If this machine does not do all register operations on the entire
7971 register and MODE is wider than the mode of X, we can say nothing
7972 at all about the high-order bits. */
7973 return 1;
7974 #else
7975 /* Likewise on machines that do, if the mode of the object is smaller
7976 than a word and loads of that size don't sign extend, we can say
7977 nothing about the high order bits. */
7978 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
7979 #ifdef LOAD_EXTEND_OP
7980 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
7981 #endif
7982 )
7983 return 1;
7984 #endif
7985 }
7986
7987 switch (code)
7988 {
7989 case REG:
7990
7991 #ifdef POINTERS_EXTEND_UNSIGNED
7992 /* If pointers extend signed and this is a pointer in Pmode, say that
7993 all the bits above ptr_mode are known to be sign bit copies. */
7994 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7995 && REGNO_POINTER_FLAG (REGNO (x)))
7996 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7997 #endif
7998
7999 if (reg_last_set_value[REGNO (x)] != 0
8000 && reg_last_set_mode[REGNO (x)] == mode
8001 && (reg_last_set_label[REGNO (x)] == label_tick
8002 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8003 && REG_N_SETS (REGNO (x)) == 1
8004 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
8005 REGNO (x))))
8006 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8007 return reg_last_set_sign_bit_copies[REGNO (x)];
8008
8009 tem = get_last_value (x);
8010 if (tem != 0)
8011 return num_sign_bit_copies (tem, mode);
8012
8013 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
8014 return reg_sign_bit_copies[REGNO (x)];
8015 break;
8016
8017 case MEM:
8018 #ifdef LOAD_EXTEND_OP
8019 /* Some RISC machines sign-extend all loads of smaller than a word. */
8020 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8021 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
8022 #endif
8023 break;
8024
8025 case CONST_INT:
8026 /* If the constant is negative, take its 1's complement and remask.
8027 Then see how many zero bits we have. */
8028 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8029 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8030 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8031 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
8032
8033 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8034
8035 case SUBREG:
8036 /* If this is a SUBREG for a promoted object that is sign-extended
8037 and we are looking at it in a wider mode, we know that at least the
8038 high-order bits are known to be sign bit copies. */
8039
8040 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8041 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8042 num_sign_bit_copies (SUBREG_REG (x), mode));
8043
8044 /* For a smaller object, just ignore the high bits. */
8045 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8046 {
8047 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
8048 return MAX (1, (num0
8049 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8050 - bitwidth)));
8051 }
8052
8053 #ifdef WORD_REGISTER_OPERATIONS
8054 #ifdef LOAD_EXTEND_OP
8055 /* For paradoxical SUBREGs on machines where all register operations
8056 affect the entire register, just look inside. Note that we are
8057 passing MODE to the recursive call, so the number of sign bit copies
8058 will remain relative to that mode, not the inner mode. */
8059
8060 /* This works only if loads sign extend. Otherwise, if we get a
8061 reload for the inner part, it may be loaded from the stack, and
8062 then we lose all sign bit copies that existed before the store
8063 to the stack. */
8064
8065 if ((GET_MODE_SIZE (GET_MODE (x))
8066 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8067 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
8068 return num_sign_bit_copies (SUBREG_REG (x), mode);
8069 #endif
8070 #endif
8071 break;
8072
8073 case SIGN_EXTRACT:
8074 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8075 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
8076 break;
8077
8078 case SIGN_EXTEND:
8079 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8080 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
8081
8082 case TRUNCATE:
8083 /* For a smaller object, just ignore the high bits. */
8084 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
8085 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8086 - bitwidth)));
8087
8088 case NOT:
8089 return num_sign_bit_copies (XEXP (x, 0), mode);
8090
8091 case ROTATE: case ROTATERT:
8092 /* If we are rotating left by a number of bits less than the number
8093 of sign bit copies, we can just subtract that amount from the
8094 number. */
8095 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8096 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
8097 {
8098 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8099 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8100 : bitwidth - INTVAL (XEXP (x, 1))));
8101 }
8102 break;
8103
8104 case NEG:
8105 /* In general, this subtracts one sign bit copy. But if the value
8106 is known to be positive, the number of sign bit copies is the
8107 same as that of the input. Finally, if the input has just one bit
8108 that might be nonzero, all the bits are copies of the sign bit. */
8109 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8110 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8111 return num0 > 1 ? num0 - 1 : 1;
8112
8113 nonzero = nonzero_bits (XEXP (x, 0), mode);
8114 if (nonzero == 1)
8115 return bitwidth;
8116
8117 if (num0 > 1
8118 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8119 num0--;
8120
8121 return num0;
8122
8123 case IOR: case AND: case XOR:
8124 case SMIN: case SMAX: case UMIN: case UMAX:
8125 /* Logical operations will preserve the number of sign-bit copies.
8126 MIN and MAX operations always return one of the operands. */
8127 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8128 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8129 return MIN (num0, num1);
8130
8131 case PLUS: case MINUS:
8132 /* For addition and subtraction, we can have a 1-bit carry. However,
8133 if we are subtracting 1 from a positive number, there will not
8134 be such a carry. Furthermore, if the positive number is known to
8135 be 0 or 1, we know the result is either -1 or 0. */
8136
8137 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8138 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8139 {
8140 nonzero = nonzero_bits (XEXP (x, 0), mode);
8141 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8142 return (nonzero == 1 || nonzero == 0 ? bitwidth
8143 : bitwidth - floor_log2 (nonzero) - 1);
8144 }
8145
8146 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8147 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8148 return MAX (1, MIN (num0, num1) - 1);
8149
8150 case MULT:
8151 /* The number of bits of the product is the sum of the number of
8152 bits of both terms. However, unless one of the terms if known
8153 to be positive, we must allow for an additional bit since negating
8154 a negative number can remove one sign bit copy. */
8155
8156 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8157 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8158
8159 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8160 if (result > 0
8161 && (bitwidth > HOST_BITS_PER_WIDE_INT
8162 || (((nonzero_bits (XEXP (x, 0), mode)
8163 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8164 && ((nonzero_bits (XEXP (x, 1), mode)
8165 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8166 result--;
8167
8168 return MAX (1, result);
8169
8170 case UDIV:
8171 /* The result must be <= the first operand. If the first operand
8172 has the high bit set, we know nothing about the number of sign
8173 bit copies. */
8174 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8175 return 1;
8176 else if ((nonzero_bits (XEXP (x, 0), mode)
8177 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8178 return 1;
8179 else
8180 return num_sign_bit_copies (XEXP (x, 0), mode);
8181
8182 case UMOD:
8183 /* The result must be <= the scond operand. */
8184 return num_sign_bit_copies (XEXP (x, 1), mode);
8185
8186 case DIV:
8187 /* Similar to unsigned division, except that we have to worry about
8188 the case where the divisor is negative, in which case we have
8189 to add 1. */
8190 result = num_sign_bit_copies (XEXP (x, 0), mode);
8191 if (result > 1
8192 && (bitwidth > HOST_BITS_PER_WIDE_INT
8193 || (nonzero_bits (XEXP (x, 1), mode)
8194 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8195 result--;
8196
8197 return result;
8198
8199 case MOD:
8200 result = num_sign_bit_copies (XEXP (x, 1), mode);
8201 if (result > 1
8202 && (bitwidth > HOST_BITS_PER_WIDE_INT
8203 || (nonzero_bits (XEXP (x, 1), mode)
8204 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8205 result--;
8206
8207 return result;
8208
8209 case ASHIFTRT:
8210 /* Shifts by a constant add to the number of bits equal to the
8211 sign bit. */
8212 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8213 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8214 && INTVAL (XEXP (x, 1)) > 0)
8215 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8216
8217 return num0;
8218
8219 case ASHIFT:
8220 /* Left shifts destroy copies. */
8221 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8222 || INTVAL (XEXP (x, 1)) < 0
8223 || INTVAL (XEXP (x, 1)) >= bitwidth)
8224 return 1;
8225
8226 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8227 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8228
8229 case IF_THEN_ELSE:
8230 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8231 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8232 return MIN (num0, num1);
8233
8234 case EQ: case NE: case GE: case GT: case LE: case LT:
8235 case GEU: case GTU: case LEU: case LTU:
8236 if (STORE_FLAG_VALUE == -1)
8237 return bitwidth;
8238 break;
8239
8240 default:
8241 break;
8242 }
8243
8244 /* If we haven't been able to figure it out by one of the above rules,
8245 see if some of the high-order bits are known to be zero. If so,
8246 count those bits and return one less than that amount. If we can't
8247 safely compute the mask for this mode, always return BITWIDTH. */
8248
8249 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8250 return 1;
8251
8252 nonzero = nonzero_bits (x, mode);
8253 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8254 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8255 }
8256 \f
8257 /* Return the number of "extended" bits there are in X, when interpreted
8258 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8259 unsigned quantities, this is the number of high-order zero bits.
8260 For signed quantities, this is the number of copies of the sign bit
8261 minus 1. In both case, this function returns the number of "spare"
8262 bits. For example, if two quantities for which this function returns
8263 at least 1 are added, the addition is known not to overflow.
8264
8265 This function will always return 0 unless called during combine, which
8266 implies that it must be called from a define_split. */
8267
8268 int
8269 extended_count (x, mode, unsignedp)
8270 rtx x;
8271 enum machine_mode mode;
8272 int unsignedp;
8273 {
8274 if (nonzero_sign_valid == 0)
8275 return 0;
8276
8277 return (unsignedp
8278 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8279 && (GET_MODE_BITSIZE (mode) - 1
8280 - floor_log2 (nonzero_bits (x, mode))))
8281 : num_sign_bit_copies (x, mode) - 1);
8282 }
8283 \f
8284 /* This function is called from `simplify_shift_const' to merge two
8285 outer operations. Specifically, we have already found that we need
8286 to perform operation *POP0 with constant *PCONST0 at the outermost
8287 position. We would now like to also perform OP1 with constant CONST1
8288 (with *POP0 being done last).
8289
8290 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8291 the resulting operation. *PCOMP_P is set to 1 if we would need to
8292 complement the innermost operand, otherwise it is unchanged.
8293
8294 MODE is the mode in which the operation will be done. No bits outside
8295 the width of this mode matter. It is assumed that the width of this mode
8296 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8297
8298 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8299 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8300 result is simply *PCONST0.
8301
8302 If the resulting operation cannot be expressed as one operation, we
8303 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8304
8305 static int
8306 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8307 enum rtx_code *pop0;
8308 HOST_WIDE_INT *pconst0;
8309 enum rtx_code op1;
8310 HOST_WIDE_INT const1;
8311 enum machine_mode mode;
8312 int *pcomp_p;
8313 {
8314 enum rtx_code op0 = *pop0;
8315 HOST_WIDE_INT const0 = *pconst0;
8316
8317 const0 &= GET_MODE_MASK (mode);
8318 const1 &= GET_MODE_MASK (mode);
8319
8320 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8321 if (op0 == AND)
8322 const1 &= const0;
8323
8324 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8325 if OP0 is SET. */
8326
8327 if (op1 == NIL || op0 == SET)
8328 return 1;
8329
8330 else if (op0 == NIL)
8331 op0 = op1, const0 = const1;
8332
8333 else if (op0 == op1)
8334 {
8335 switch (op0)
8336 {
8337 case AND:
8338 const0 &= const1;
8339 break;
8340 case IOR:
8341 const0 |= const1;
8342 break;
8343 case XOR:
8344 const0 ^= const1;
8345 break;
8346 case PLUS:
8347 const0 += const1;
8348 break;
8349 case NEG:
8350 op0 = NIL;
8351 break;
8352 default:
8353 break;
8354 }
8355 }
8356
8357 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8358 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8359 return 0;
8360
8361 /* If the two constants aren't the same, we can't do anything. The
8362 remaining six cases can all be done. */
8363 else if (const0 != const1)
8364 return 0;
8365
8366 else
8367 switch (op0)
8368 {
8369 case IOR:
8370 if (op1 == AND)
8371 /* (a & b) | b == b */
8372 op0 = SET;
8373 else /* op1 == XOR */
8374 /* (a ^ b) | b == a | b */
8375 {;}
8376 break;
8377
8378 case XOR:
8379 if (op1 == AND)
8380 /* (a & b) ^ b == (~a) & b */
8381 op0 = AND, *pcomp_p = 1;
8382 else /* op1 == IOR */
8383 /* (a | b) ^ b == a & ~b */
8384 op0 = AND, *pconst0 = ~ const0;
8385 break;
8386
8387 case AND:
8388 if (op1 == IOR)
8389 /* (a | b) & b == b */
8390 op0 = SET;
8391 else /* op1 == XOR */
8392 /* (a ^ b) & b) == (~a) & b */
8393 *pcomp_p = 1;
8394 break;
8395 default:
8396 break;
8397 }
8398
8399 /* Check for NO-OP cases. */
8400 const0 &= GET_MODE_MASK (mode);
8401 if (const0 == 0
8402 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8403 op0 = NIL;
8404 else if (const0 == 0 && op0 == AND)
8405 op0 = SET;
8406 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8407 && op0 == AND)
8408 op0 = NIL;
8409
8410 /* ??? Slightly redundant with the above mask, but not entirely.
8411 Moving this above means we'd have to sign-extend the mode mask
8412 for the final test. */
8413 const0 = trunc_int_for_mode (const0, mode);
8414
8415 *pop0 = op0;
8416 *pconst0 = const0;
8417
8418 return 1;
8419 }
8420 \f
8421 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8422 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8423 that we started with.
8424
8425 The shift is normally computed in the widest mode we find in VAROP, as
8426 long as it isn't a different number of words than RESULT_MODE. Exceptions
8427 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8428
8429 static rtx
8430 simplify_shift_const (x, code, result_mode, varop, count)
8431 rtx x;
8432 enum rtx_code code;
8433 enum machine_mode result_mode;
8434 rtx varop;
8435 int count;
8436 {
8437 enum rtx_code orig_code = code;
8438 int orig_count = count;
8439 enum machine_mode mode = result_mode;
8440 enum machine_mode shift_mode, tmode;
8441 int mode_words
8442 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8443 /* We form (outer_op (code varop count) (outer_const)). */
8444 enum rtx_code outer_op = NIL;
8445 HOST_WIDE_INT outer_const = 0;
8446 rtx const_rtx;
8447 int complement_p = 0;
8448 rtx new;
8449
8450 /* If we were given an invalid count, don't do anything except exactly
8451 what was requested. */
8452
8453 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8454 {
8455 if (x)
8456 return x;
8457
8458 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
8459 }
8460
8461 /* Unless one of the branches of the `if' in this loop does a `continue',
8462 we will `break' the loop after the `if'. */
8463
8464 while (count != 0)
8465 {
8466 /* If we have an operand of (clobber (const_int 0)), just return that
8467 value. */
8468 if (GET_CODE (varop) == CLOBBER)
8469 return varop;
8470
8471 /* If we discovered we had to complement VAROP, leave. Making a NOT
8472 here would cause an infinite loop. */
8473 if (complement_p)
8474 break;
8475
8476 /* Convert ROTATERT to ROTATE. */
8477 if (code == ROTATERT)
8478 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8479
8480 /* We need to determine what mode we will do the shift in. If the
8481 shift is a right shift or a ROTATE, we must always do it in the mode
8482 it was originally done in. Otherwise, we can do it in MODE, the
8483 widest mode encountered. */
8484 shift_mode
8485 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8486 ? result_mode : mode);
8487
8488 /* Handle cases where the count is greater than the size of the mode
8489 minus 1. For ASHIFT, use the size minus one as the count (this can
8490 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8491 take the count modulo the size. For other shifts, the result is
8492 zero.
8493
8494 Since these shifts are being produced by the compiler by combining
8495 multiple operations, each of which are defined, we know what the
8496 result is supposed to be. */
8497
8498 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8499 {
8500 if (code == ASHIFTRT)
8501 count = GET_MODE_BITSIZE (shift_mode) - 1;
8502 else if (code == ROTATE || code == ROTATERT)
8503 count %= GET_MODE_BITSIZE (shift_mode);
8504 else
8505 {
8506 /* We can't simply return zero because there may be an
8507 outer op. */
8508 varop = const0_rtx;
8509 count = 0;
8510 break;
8511 }
8512 }
8513
8514 /* Negative counts are invalid and should not have been made (a
8515 programmer-specified negative count should have been handled
8516 above). */
8517 else if (count < 0)
8518 abort ();
8519
8520 /* An arithmetic right shift of a quantity known to be -1 or 0
8521 is a no-op. */
8522 if (code == ASHIFTRT
8523 && (num_sign_bit_copies (varop, shift_mode)
8524 == GET_MODE_BITSIZE (shift_mode)))
8525 {
8526 count = 0;
8527 break;
8528 }
8529
8530 /* If we are doing an arithmetic right shift and discarding all but
8531 the sign bit copies, this is equivalent to doing a shift by the
8532 bitsize minus one. Convert it into that shift because it will often
8533 allow other simplifications. */
8534
8535 if (code == ASHIFTRT
8536 && (count + num_sign_bit_copies (varop, shift_mode)
8537 >= GET_MODE_BITSIZE (shift_mode)))
8538 count = GET_MODE_BITSIZE (shift_mode) - 1;
8539
8540 /* We simplify the tests below and elsewhere by converting
8541 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8542 `make_compound_operation' will convert it to a ASHIFTRT for
8543 those machines (such as Vax) that don't have a LSHIFTRT. */
8544 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8545 && code == ASHIFTRT
8546 && ((nonzero_bits (varop, shift_mode)
8547 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8548 == 0))
8549 code = LSHIFTRT;
8550
8551 switch (GET_CODE (varop))
8552 {
8553 case SIGN_EXTEND:
8554 case ZERO_EXTEND:
8555 case SIGN_EXTRACT:
8556 case ZERO_EXTRACT:
8557 new = expand_compound_operation (varop);
8558 if (new != varop)
8559 {
8560 varop = new;
8561 continue;
8562 }
8563 break;
8564
8565 case MEM:
8566 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8567 minus the width of a smaller mode, we can do this with a
8568 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8569 if ((code == ASHIFTRT || code == LSHIFTRT)
8570 && ! mode_dependent_address_p (XEXP (varop, 0))
8571 && ! MEM_VOLATILE_P (varop)
8572 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8573 MODE_INT, 1)) != BLKmode)
8574 {
8575 if (BYTES_BIG_ENDIAN)
8576 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
8577 else
8578 new = gen_rtx_MEM (tmode,
8579 plus_constant (XEXP (varop, 0),
8580 count / BITS_PER_UNIT));
8581 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8582 MEM_COPY_ATTRIBUTES (new, varop);
8583 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8584 : ZERO_EXTEND, mode, new);
8585 count = 0;
8586 continue;
8587 }
8588 break;
8589
8590 case USE:
8591 /* Similar to the case above, except that we can only do this if
8592 the resulting mode is the same as that of the underlying
8593 MEM and adjust the address depending on the *bits* endianness
8594 because of the way that bit-field extract insns are defined. */
8595 if ((code == ASHIFTRT || code == LSHIFTRT)
8596 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8597 MODE_INT, 1)) != BLKmode
8598 && tmode == GET_MODE (XEXP (varop, 0)))
8599 {
8600 if (BITS_BIG_ENDIAN)
8601 new = XEXP (varop, 0);
8602 else
8603 {
8604 new = copy_rtx (XEXP (varop, 0));
8605 SUBST (XEXP (new, 0),
8606 plus_constant (XEXP (new, 0),
8607 count / BITS_PER_UNIT));
8608 }
8609
8610 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8611 : ZERO_EXTEND, mode, new);
8612 count = 0;
8613 continue;
8614 }
8615 break;
8616
8617 case SUBREG:
8618 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8619 the same number of words as what we've seen so far. Then store
8620 the widest mode in MODE. */
8621 if (subreg_lowpart_p (varop)
8622 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8623 > GET_MODE_SIZE (GET_MODE (varop)))
8624 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8625 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8626 == mode_words))
8627 {
8628 varop = SUBREG_REG (varop);
8629 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8630 mode = GET_MODE (varop);
8631 continue;
8632 }
8633 break;
8634
8635 case MULT:
8636 /* Some machines use MULT instead of ASHIFT because MULT
8637 is cheaper. But it is still better on those machines to
8638 merge two shifts into one. */
8639 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8640 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8641 {
8642 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8643 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8644 continue;
8645 }
8646 break;
8647
8648 case UDIV:
8649 /* Similar, for when divides are cheaper. */
8650 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8651 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8652 {
8653 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8654 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8655 continue;
8656 }
8657 break;
8658
8659 case ASHIFTRT:
8660 /* If we are extracting just the sign bit of an arithmetic right
8661 shift, that shift is not needed. */
8662 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8663 {
8664 varop = XEXP (varop, 0);
8665 continue;
8666 }
8667
8668 /* ... fall through ... */
8669
8670 case LSHIFTRT:
8671 case ASHIFT:
8672 case ROTATE:
8673 /* Here we have two nested shifts. The result is usually the
8674 AND of a new shift with a mask. We compute the result below. */
8675 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8676 && INTVAL (XEXP (varop, 1)) >= 0
8677 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8678 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8679 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8680 {
8681 enum rtx_code first_code = GET_CODE (varop);
8682 int first_count = INTVAL (XEXP (varop, 1));
8683 unsigned HOST_WIDE_INT mask;
8684 rtx mask_rtx;
8685
8686 /* We have one common special case. We can't do any merging if
8687 the inner code is an ASHIFTRT of a smaller mode. However, if
8688 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8689 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8690 we can convert it to
8691 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8692 This simplifies certain SIGN_EXTEND operations. */
8693 if (code == ASHIFT && first_code == ASHIFTRT
8694 && (GET_MODE_BITSIZE (result_mode)
8695 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8696 {
8697 /* C3 has the low-order C1 bits zero. */
8698
8699 mask = (GET_MODE_MASK (mode)
8700 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8701
8702 varop = simplify_and_const_int (NULL_RTX, result_mode,
8703 XEXP (varop, 0), mask);
8704 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8705 varop, count);
8706 count = first_count;
8707 code = ASHIFTRT;
8708 continue;
8709 }
8710
8711 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8712 than C1 high-order bits equal to the sign bit, we can convert
8713 this to either an ASHIFT or a ASHIFTRT depending on the
8714 two counts.
8715
8716 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8717
8718 if (code == ASHIFTRT && first_code == ASHIFT
8719 && GET_MODE (varop) == shift_mode
8720 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8721 > first_count))
8722 {
8723 count -= first_count;
8724 if (count < 0)
8725 count = - count, code = ASHIFT;
8726 varop = XEXP (varop, 0);
8727 continue;
8728 }
8729
8730 /* There are some cases we can't do. If CODE is ASHIFTRT,
8731 we can only do this if FIRST_CODE is also ASHIFTRT.
8732
8733 We can't do the case when CODE is ROTATE and FIRST_CODE is
8734 ASHIFTRT.
8735
8736 If the mode of this shift is not the mode of the outer shift,
8737 we can't do this if either shift is a right shift or ROTATE.
8738
8739 Finally, we can't do any of these if the mode is too wide
8740 unless the codes are the same.
8741
8742 Handle the case where the shift codes are the same
8743 first. */
8744
8745 if (code == first_code)
8746 {
8747 if (GET_MODE (varop) != result_mode
8748 && (code == ASHIFTRT || code == LSHIFTRT
8749 || code == ROTATE))
8750 break;
8751
8752 count += first_count;
8753 varop = XEXP (varop, 0);
8754 continue;
8755 }
8756
8757 if (code == ASHIFTRT
8758 || (code == ROTATE && first_code == ASHIFTRT)
8759 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8760 || (GET_MODE (varop) != result_mode
8761 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8762 || first_code == ROTATE
8763 || code == ROTATE)))
8764 break;
8765
8766 /* To compute the mask to apply after the shift, shift the
8767 nonzero bits of the inner shift the same way the
8768 outer shift will. */
8769
8770 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8771
8772 mask_rtx
8773 = simplify_binary_operation (code, result_mode, mask_rtx,
8774 GEN_INT (count));
8775
8776 /* Give up if we can't compute an outer operation to use. */
8777 if (mask_rtx == 0
8778 || GET_CODE (mask_rtx) != CONST_INT
8779 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8780 INTVAL (mask_rtx),
8781 result_mode, &complement_p))
8782 break;
8783
8784 /* If the shifts are in the same direction, we add the
8785 counts. Otherwise, we subtract them. */
8786 if ((code == ASHIFTRT || code == LSHIFTRT)
8787 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8788 count += first_count;
8789 else
8790 count -= first_count;
8791
8792 /* If COUNT is positive, the new shift is usually CODE,
8793 except for the two exceptions below, in which case it is
8794 FIRST_CODE. If the count is negative, FIRST_CODE should
8795 always be used */
8796 if (count > 0
8797 && ((first_code == ROTATE && code == ASHIFT)
8798 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8799 code = first_code;
8800 else if (count < 0)
8801 code = first_code, count = - count;
8802
8803 varop = XEXP (varop, 0);
8804 continue;
8805 }
8806
8807 /* If we have (A << B << C) for any shift, we can convert this to
8808 (A << C << B). This wins if A is a constant. Only try this if
8809 B is not a constant. */
8810
8811 else if (GET_CODE (varop) == code
8812 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8813 && 0 != (new
8814 = simplify_binary_operation (code, mode,
8815 XEXP (varop, 0),
8816 GEN_INT (count))))
8817 {
8818 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8819 count = 0;
8820 continue;
8821 }
8822 break;
8823
8824 case NOT:
8825 /* Make this fit the case below. */
8826 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8827 GEN_INT (GET_MODE_MASK (mode)));
8828 continue;
8829
8830 case IOR:
8831 case AND:
8832 case XOR:
8833 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8834 with C the size of VAROP - 1 and the shift is logical if
8835 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8836 we have an (le X 0) operation. If we have an arithmetic shift
8837 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8838 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8839
8840 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8841 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8842 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8843 && (code == LSHIFTRT || code == ASHIFTRT)
8844 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8845 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8846 {
8847 count = 0;
8848 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8849 const0_rtx);
8850
8851 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8852 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8853
8854 continue;
8855 }
8856
8857 /* If we have (shift (logical)), move the logical to the outside
8858 to allow it to possibly combine with another logical and the
8859 shift to combine with another shift. This also canonicalizes to
8860 what a ZERO_EXTRACT looks like. Also, some machines have
8861 (and (shift)) insns. */
8862
8863 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8864 && (new = simplify_binary_operation (code, result_mode,
8865 XEXP (varop, 1),
8866 GEN_INT (count))) != 0
8867 && GET_CODE(new) == CONST_INT
8868 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8869 INTVAL (new), result_mode, &complement_p))
8870 {
8871 varop = XEXP (varop, 0);
8872 continue;
8873 }
8874
8875 /* If we can't do that, try to simplify the shift in each arm of the
8876 logical expression, make a new logical expression, and apply
8877 the inverse distributive law. */
8878 {
8879 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8880 XEXP (varop, 0), count);
8881 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8882 XEXP (varop, 1), count);
8883
8884 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8885 varop = apply_distributive_law (varop);
8886
8887 count = 0;
8888 }
8889 break;
8890
8891 case EQ:
8892 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8893 says that the sign bit can be tested, FOO has mode MODE, C is
8894 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8895 that may be nonzero. */
8896 if (code == LSHIFTRT
8897 && XEXP (varop, 1) == const0_rtx
8898 && GET_MODE (XEXP (varop, 0)) == result_mode
8899 && count == GET_MODE_BITSIZE (result_mode) - 1
8900 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8901 && ((STORE_FLAG_VALUE
8902 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8903 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8904 && merge_outer_ops (&outer_op, &outer_const, XOR,
8905 (HOST_WIDE_INT) 1, result_mode,
8906 &complement_p))
8907 {
8908 varop = XEXP (varop, 0);
8909 count = 0;
8910 continue;
8911 }
8912 break;
8913
8914 case NEG:
8915 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8916 than the number of bits in the mode is equivalent to A. */
8917 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8918 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8919 {
8920 varop = XEXP (varop, 0);
8921 count = 0;
8922 continue;
8923 }
8924
8925 /* NEG commutes with ASHIFT since it is multiplication. Move the
8926 NEG outside to allow shifts to combine. */
8927 if (code == ASHIFT
8928 && merge_outer_ops (&outer_op, &outer_const, NEG,
8929 (HOST_WIDE_INT) 0, result_mode,
8930 &complement_p))
8931 {
8932 varop = XEXP (varop, 0);
8933 continue;
8934 }
8935 break;
8936
8937 case PLUS:
8938 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8939 is one less than the number of bits in the mode is
8940 equivalent to (xor A 1). */
8941 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8942 && XEXP (varop, 1) == constm1_rtx
8943 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8944 && merge_outer_ops (&outer_op, &outer_const, XOR,
8945 (HOST_WIDE_INT) 1, result_mode,
8946 &complement_p))
8947 {
8948 count = 0;
8949 varop = XEXP (varop, 0);
8950 continue;
8951 }
8952
8953 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8954 that might be nonzero in BAR are those being shifted out and those
8955 bits are known zero in FOO, we can replace the PLUS with FOO.
8956 Similarly in the other operand order. This code occurs when
8957 we are computing the size of a variable-size array. */
8958
8959 if ((code == ASHIFTRT || code == LSHIFTRT)
8960 && count < HOST_BITS_PER_WIDE_INT
8961 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8962 && (nonzero_bits (XEXP (varop, 1), result_mode)
8963 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8964 {
8965 varop = XEXP (varop, 0);
8966 continue;
8967 }
8968 else if ((code == ASHIFTRT || code == LSHIFTRT)
8969 && count < HOST_BITS_PER_WIDE_INT
8970 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8971 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8972 >> count)
8973 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8974 & nonzero_bits (XEXP (varop, 1),
8975 result_mode)))
8976 {
8977 varop = XEXP (varop, 1);
8978 continue;
8979 }
8980
8981 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
8982 if (code == ASHIFT
8983 && GET_CODE (XEXP (varop, 1)) == CONST_INT
8984 && (new = simplify_binary_operation (ASHIFT, result_mode,
8985 XEXP (varop, 1),
8986 GEN_INT (count))) != 0
8987 && GET_CODE(new) == CONST_INT
8988 && merge_outer_ops (&outer_op, &outer_const, PLUS,
8989 INTVAL (new), result_mode, &complement_p))
8990 {
8991 varop = XEXP (varop, 0);
8992 continue;
8993 }
8994 break;
8995
8996 case MINUS:
8997 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8998 with C the size of VAROP - 1 and the shift is logical if
8999 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9000 we have a (gt X 0) operation. If the shift is arithmetic with
9001 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9002 we have a (neg (gt X 0)) operation. */
9003
9004 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9005 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9006 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
9007 && (code == LSHIFTRT || code == ASHIFTRT)
9008 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9009 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9010 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9011 {
9012 count = 0;
9013 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
9014 const0_rtx);
9015
9016 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9017 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9018
9019 continue;
9020 }
9021 break;
9022
9023 case TRUNCATE:
9024 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9025 if the truncate does not affect the value. */
9026 if (code == LSHIFTRT
9027 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9028 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9029 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9030 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9031 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9032 {
9033 rtx varop_inner = XEXP (varop, 0);
9034
9035 varop_inner = gen_rtx_combine (LSHIFTRT,
9036 GET_MODE (varop_inner),
9037 XEXP (varop_inner, 0),
9038 GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
9039 varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
9040 varop_inner);
9041 count = 0;
9042 continue;
9043 }
9044 break;
9045
9046 default:
9047 break;
9048 }
9049
9050 break;
9051 }
9052
9053 /* We need to determine what mode to do the shift in. If the shift is
9054 a right shift or ROTATE, we must always do it in the mode it was
9055 originally done in. Otherwise, we can do it in MODE, the widest mode
9056 encountered. The code we care about is that of the shift that will
9057 actually be done, not the shift that was originally requested. */
9058 shift_mode
9059 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9060 ? result_mode : mode);
9061
9062 /* We have now finished analyzing the shift. The result should be
9063 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9064 OUTER_OP is non-NIL, it is an operation that needs to be applied
9065 to the result of the shift. OUTER_CONST is the relevant constant,
9066 but we must turn off all bits turned off in the shift.
9067
9068 If we were passed a value for X, see if we can use any pieces of
9069 it. If not, make new rtx. */
9070
9071 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9072 && GET_CODE (XEXP (x, 1)) == CONST_INT
9073 && INTVAL (XEXP (x, 1)) == count)
9074 const_rtx = XEXP (x, 1);
9075 else
9076 const_rtx = GEN_INT (count);
9077
9078 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9079 && GET_MODE (XEXP (x, 0)) == shift_mode
9080 && SUBREG_REG (XEXP (x, 0)) == varop)
9081 varop = XEXP (x, 0);
9082 else if (GET_MODE (varop) != shift_mode)
9083 varop = gen_lowpart_for_combine (shift_mode, varop);
9084
9085 /* If we can't make the SUBREG, try to return what we were given. */
9086 if (GET_CODE (varop) == CLOBBER)
9087 return x ? x : varop;
9088
9089 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9090 if (new != 0)
9091 x = new;
9092 else
9093 {
9094 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
9095 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
9096
9097 SUBST (XEXP (x, 0), varop);
9098 SUBST (XEXP (x, 1), const_rtx);
9099 }
9100
9101 /* If we have an outer operation and we just made a shift, it is
9102 possible that we could have simplified the shift were it not
9103 for the outer operation. So try to do the simplification
9104 recursively. */
9105
9106 if (outer_op != NIL && GET_CODE (x) == code
9107 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9108 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9109 INTVAL (XEXP (x, 1)));
9110
9111 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
9112 turn off all the bits that the shift would have turned off. */
9113 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9114 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9115 GET_MODE_MASK (result_mode) >> orig_count);
9116
9117 /* Do the remainder of the processing in RESULT_MODE. */
9118 x = gen_lowpart_for_combine (result_mode, x);
9119
9120 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9121 operation. */
9122 if (complement_p)
9123 x = gen_unary (NOT, result_mode, result_mode, x);
9124
9125 if (outer_op != NIL)
9126 {
9127 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9128 outer_const = trunc_int_for_mode (outer_const, result_mode);
9129
9130 if (outer_op == AND)
9131 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9132 else if (outer_op == SET)
9133 /* This means that we have determined that the result is
9134 equivalent to a constant. This should be rare. */
9135 x = GEN_INT (outer_const);
9136 else if (GET_RTX_CLASS (outer_op) == '1')
9137 x = gen_unary (outer_op, result_mode, result_mode, x);
9138 else
9139 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9140 }
9141
9142 return x;
9143 }
9144 \f
9145 /* Like recog, but we receive the address of a pointer to a new pattern.
9146 We try to match the rtx that the pointer points to.
9147 If that fails, we may try to modify or replace the pattern,
9148 storing the replacement into the same pointer object.
9149
9150 Modifications include deletion or addition of CLOBBERs.
9151
9152 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9153 the CLOBBERs are placed.
9154
9155 The value is the final insn code from the pattern ultimately matched,
9156 or -1. */
9157
9158 static int
9159 recog_for_combine (pnewpat, insn, pnotes)
9160 rtx *pnewpat;
9161 rtx insn;
9162 rtx *pnotes;
9163 {
9164 register rtx pat = *pnewpat;
9165 int insn_code_number;
9166 int num_clobbers_to_add = 0;
9167 int i;
9168 rtx notes = 0;
9169
9170 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9171 we use to indicate that something didn't match. If we find such a
9172 thing, force rejection. */
9173 if (GET_CODE (pat) == PARALLEL)
9174 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9175 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9176 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9177 return -1;
9178
9179 /* Is the result of combination a valid instruction? */
9180 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9181
9182 /* If it isn't, there is the possibility that we previously had an insn
9183 that clobbered some register as a side effect, but the combined
9184 insn doesn't need to do that. So try once more without the clobbers
9185 unless this represents an ASM insn. */
9186
9187 if (insn_code_number < 0 && ! check_asm_operands (pat)
9188 && GET_CODE (pat) == PARALLEL)
9189 {
9190 int pos;
9191
9192 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9193 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9194 {
9195 if (i != pos)
9196 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9197 pos++;
9198 }
9199
9200 SUBST_INT (XVECLEN (pat, 0), pos);
9201
9202 if (pos == 1)
9203 pat = XVECEXP (pat, 0, 0);
9204
9205 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9206 }
9207
9208 /* If we had any clobbers to add, make a new pattern than contains
9209 them. Then check to make sure that all of them are dead. */
9210 if (num_clobbers_to_add)
9211 {
9212 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9213 gen_rtvec (GET_CODE (pat) == PARALLEL
9214 ? (XVECLEN (pat, 0)
9215 + num_clobbers_to_add)
9216 : num_clobbers_to_add + 1));
9217
9218 if (GET_CODE (pat) == PARALLEL)
9219 for (i = 0; i < XVECLEN (pat, 0); i++)
9220 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9221 else
9222 XVECEXP (newpat, 0, 0) = pat;
9223
9224 add_clobbers (newpat, insn_code_number);
9225
9226 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9227 i < XVECLEN (newpat, 0); i++)
9228 {
9229 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9230 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9231 return -1;
9232 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9233 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9234 }
9235 pat = newpat;
9236 }
9237
9238 *pnewpat = pat;
9239 *pnotes = notes;
9240
9241 return insn_code_number;
9242 }
9243 \f
9244 /* Like gen_lowpart but for use by combine. In combine it is not possible
9245 to create any new pseudoregs. However, it is safe to create
9246 invalid memory addresses, because combine will try to recognize
9247 them and all they will do is make the combine attempt fail.
9248
9249 If for some reason this cannot do its job, an rtx
9250 (clobber (const_int 0)) is returned.
9251 An insn containing that will not be recognized. */
9252
9253 #undef gen_lowpart
9254
9255 static rtx
9256 gen_lowpart_for_combine (mode, x)
9257 enum machine_mode mode;
9258 register rtx x;
9259 {
9260 rtx result;
9261
9262 if (GET_MODE (x) == mode)
9263 return x;
9264
9265 /* We can only support MODE being wider than a word if X is a
9266 constant integer or has a mode the same size. */
9267
9268 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9269 && ! ((GET_MODE (x) == VOIDmode
9270 && (GET_CODE (x) == CONST_INT
9271 || GET_CODE (x) == CONST_DOUBLE))
9272 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9273 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9274
9275 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9276 won't know what to do. So we will strip off the SUBREG here and
9277 process normally. */
9278 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9279 {
9280 x = SUBREG_REG (x);
9281 if (GET_MODE (x) == mode)
9282 return x;
9283 }
9284
9285 result = gen_lowpart_common (mode, x);
9286 if (result != 0
9287 && GET_CODE (result) == SUBREG
9288 && GET_CODE (SUBREG_REG (result)) == REG
9289 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9290 && (GET_MODE_SIZE (GET_MODE (result))
9291 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
9292 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
9293
9294 if (result)
9295 return result;
9296
9297 if (GET_CODE (x) == MEM)
9298 {
9299 register int offset = 0;
9300 rtx new;
9301
9302 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9303 address. */
9304 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9305 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9306
9307 /* If we want to refer to something bigger than the original memref,
9308 generate a perverse subreg instead. That will force a reload
9309 of the original memref X. */
9310 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9311 return gen_rtx_SUBREG (mode, x, 0);
9312
9313 if (WORDS_BIG_ENDIAN)
9314 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9315 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9316
9317 if (BYTES_BIG_ENDIAN)
9318 {
9319 /* Adjust the address so that the address-after-the-data is
9320 unchanged. */
9321 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9322 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9323 }
9324 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9325 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9326 MEM_COPY_ATTRIBUTES (new, x);
9327 return new;
9328 }
9329
9330 /* If X is a comparison operator, rewrite it in a new mode. This
9331 probably won't match, but may allow further simplifications. */
9332 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9333 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9334
9335 /* If we couldn't simplify X any other way, just enclose it in a
9336 SUBREG. Normally, this SUBREG won't match, but some patterns may
9337 include an explicit SUBREG or we may simplify it further in combine. */
9338 else
9339 {
9340 int word = 0;
9341
9342 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9343 word = ((GET_MODE_SIZE (GET_MODE (x))
9344 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9345 / UNITS_PER_WORD);
9346 return gen_rtx_SUBREG (mode, x, word);
9347 }
9348 }
9349 \f
9350 /* Make an rtx expression. This is a subset of gen_rtx and only supports
9351 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9352
9353 If the identical expression was previously in the insn (in the undobuf),
9354 it will be returned. Only if it is not found will a new expression
9355 be made. */
9356
9357 /*VARARGS2*/
9358 static rtx
9359 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
9360 {
9361 #ifndef ANSI_PROTOTYPES
9362 enum rtx_code code;
9363 enum machine_mode mode;
9364 #endif
9365 va_list p;
9366 int n_args;
9367 rtx args[3];
9368 int j;
9369 const char *fmt;
9370 rtx rt;
9371 struct undo *undo;
9372
9373 VA_START (p, mode);
9374
9375 #ifndef ANSI_PROTOTYPES
9376 code = va_arg (p, enum rtx_code);
9377 mode = va_arg (p, enum machine_mode);
9378 #endif
9379
9380 n_args = GET_RTX_LENGTH (code);
9381 fmt = GET_RTX_FORMAT (code);
9382
9383 if (n_args == 0 || n_args > 3)
9384 abort ();
9385
9386 /* Get each arg and verify that it is supposed to be an expression. */
9387 for (j = 0; j < n_args; j++)
9388 {
9389 if (*fmt++ != 'e')
9390 abort ();
9391
9392 args[j] = va_arg (p, rtx);
9393 }
9394
9395 /* See if this is in undobuf. Be sure we don't use objects that came
9396 from another insn; this could produce circular rtl structures. */
9397
9398 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9399 if (!undo->is_int
9400 && GET_CODE (undo->old_contents.r) == code
9401 && GET_MODE (undo->old_contents.r) == mode)
9402 {
9403 for (j = 0; j < n_args; j++)
9404 if (XEXP (undo->old_contents.r, j) != args[j])
9405 break;
9406
9407 if (j == n_args)
9408 return undo->old_contents.r;
9409 }
9410
9411 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9412 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9413 rt = rtx_alloc (code);
9414 PUT_MODE (rt, mode);
9415 XEXP (rt, 0) = args[0];
9416 if (n_args > 1)
9417 {
9418 XEXP (rt, 1) = args[1];
9419 if (n_args > 2)
9420 XEXP (rt, 2) = args[2];
9421 }
9422 return rt;
9423 }
9424
9425 /* These routines make binary and unary operations by first seeing if they
9426 fold; if not, a new expression is allocated. */
9427
9428 static rtx
9429 gen_binary (code, mode, op0, op1)
9430 enum rtx_code code;
9431 enum machine_mode mode;
9432 rtx op0, op1;
9433 {
9434 rtx result;
9435 rtx tem;
9436
9437 if (GET_RTX_CLASS (code) == 'c'
9438 && (GET_CODE (op0) == CONST_INT
9439 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9440 tem = op0, op0 = op1, op1 = tem;
9441
9442 if (GET_RTX_CLASS (code) == '<')
9443 {
9444 enum machine_mode op_mode = GET_MODE (op0);
9445
9446 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9447 just (REL_OP X Y). */
9448 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9449 {
9450 op1 = XEXP (op0, 1);
9451 op0 = XEXP (op0, 0);
9452 op_mode = GET_MODE (op0);
9453 }
9454
9455 if (op_mode == VOIDmode)
9456 op_mode = GET_MODE (op1);
9457 result = simplify_relational_operation (code, op_mode, op0, op1);
9458 }
9459 else
9460 result = simplify_binary_operation (code, mode, op0, op1);
9461
9462 if (result)
9463 return result;
9464
9465 /* Put complex operands first and constants second. */
9466 if (GET_RTX_CLASS (code) == 'c'
9467 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9468 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9469 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9470 || (GET_CODE (op0) == SUBREG
9471 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9472 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9473 return gen_rtx_combine (code, mode, op1, op0);
9474
9475 /* If we are turning off bits already known off in OP0, we need not do
9476 an AND. */
9477 else if (code == AND && GET_CODE (op1) == CONST_INT
9478 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9479 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9480 return op0;
9481
9482 return gen_rtx_combine (code, mode, op0, op1);
9483 }
9484
9485 static rtx
9486 gen_unary (code, mode, op0_mode, op0)
9487 enum rtx_code code;
9488 enum machine_mode mode, op0_mode;
9489 rtx op0;
9490 {
9491 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9492
9493 if (result)
9494 return result;
9495
9496 return gen_rtx_combine (code, mode, op0);
9497 }
9498 \f
9499 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9500 comparison code that will be tested.
9501
9502 The result is a possibly different comparison code to use. *POP0 and
9503 *POP1 may be updated.
9504
9505 It is possible that we might detect that a comparison is either always
9506 true or always false. However, we do not perform general constant
9507 folding in combine, so this knowledge isn't useful. Such tautologies
9508 should have been detected earlier. Hence we ignore all such cases. */
9509
9510 static enum rtx_code
9511 simplify_comparison (code, pop0, pop1)
9512 enum rtx_code code;
9513 rtx *pop0;
9514 rtx *pop1;
9515 {
9516 rtx op0 = *pop0;
9517 rtx op1 = *pop1;
9518 rtx tem, tem1;
9519 int i;
9520 enum machine_mode mode, tmode;
9521
9522 /* Try a few ways of applying the same transformation to both operands. */
9523 while (1)
9524 {
9525 #ifndef WORD_REGISTER_OPERATIONS
9526 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9527 so check specially. */
9528 if (code != GTU && code != GEU && code != LTU && code != LEU
9529 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9530 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9531 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9532 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9533 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9534 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9535 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9536 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9537 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9538 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9539 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9540 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9541 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9542 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9543 && (INTVAL (XEXP (op0, 1))
9544 == (GET_MODE_BITSIZE (GET_MODE (op0))
9545 - (GET_MODE_BITSIZE
9546 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9547 {
9548 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9549 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9550 }
9551 #endif
9552
9553 /* If both operands are the same constant shift, see if we can ignore the
9554 shift. We can if the shift is a rotate or if the bits shifted out of
9555 this shift are known to be zero for both inputs and if the type of
9556 comparison is compatible with the shift. */
9557 if (GET_CODE (op0) == GET_CODE (op1)
9558 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9559 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9560 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9561 && (code != GT && code != LT && code != GE && code != LE))
9562 || (GET_CODE (op0) == ASHIFTRT
9563 && (code != GTU && code != LTU
9564 && code != GEU && code != GEU)))
9565 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9566 && INTVAL (XEXP (op0, 1)) >= 0
9567 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9568 && XEXP (op0, 1) == XEXP (op1, 1))
9569 {
9570 enum machine_mode mode = GET_MODE (op0);
9571 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9572 int shift_count = INTVAL (XEXP (op0, 1));
9573
9574 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9575 mask &= (mask >> shift_count) << shift_count;
9576 else if (GET_CODE (op0) == ASHIFT)
9577 mask = (mask & (mask << shift_count)) >> shift_count;
9578
9579 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9580 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9581 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9582 else
9583 break;
9584 }
9585
9586 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9587 SUBREGs are of the same mode, and, in both cases, the AND would
9588 be redundant if the comparison was done in the narrower mode,
9589 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9590 and the operand's possibly nonzero bits are 0xffffff01; in that case
9591 if we only care about QImode, we don't need the AND). This case
9592 occurs if the output mode of an scc insn is not SImode and
9593 STORE_FLAG_VALUE == 1 (e.g., the 386).
9594
9595 Similarly, check for a case where the AND's are ZERO_EXTEND
9596 operations from some narrower mode even though a SUBREG is not
9597 present. */
9598
9599 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9600 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9601 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9602 {
9603 rtx inner_op0 = XEXP (op0, 0);
9604 rtx inner_op1 = XEXP (op1, 0);
9605 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9606 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9607 int changed = 0;
9608
9609 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9610 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9611 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9612 && (GET_MODE (SUBREG_REG (inner_op0))
9613 == GET_MODE (SUBREG_REG (inner_op1)))
9614 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9615 <= HOST_BITS_PER_WIDE_INT)
9616 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9617 GET_MODE (SUBREG_REG (inner_op0)))))
9618 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9619 GET_MODE (SUBREG_REG (inner_op1))))))
9620 {
9621 op0 = SUBREG_REG (inner_op0);
9622 op1 = SUBREG_REG (inner_op1);
9623
9624 /* The resulting comparison is always unsigned since we masked
9625 off the original sign bit. */
9626 code = unsigned_condition (code);
9627
9628 changed = 1;
9629 }
9630
9631 else if (c0 == c1)
9632 for (tmode = GET_CLASS_NARROWEST_MODE
9633 (GET_MODE_CLASS (GET_MODE (op0)));
9634 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9635 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9636 {
9637 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9638 op1 = gen_lowpart_for_combine (tmode, inner_op1);
9639 code = unsigned_condition (code);
9640 changed = 1;
9641 break;
9642 }
9643
9644 if (! changed)
9645 break;
9646 }
9647
9648 /* If both operands are NOT, we can strip off the outer operation
9649 and adjust the comparison code for swapped operands; similarly for
9650 NEG, except that this must be an equality comparison. */
9651 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9652 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9653 && (code == EQ || code == NE)))
9654 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9655
9656 else
9657 break;
9658 }
9659
9660 /* If the first operand is a constant, swap the operands and adjust the
9661 comparison code appropriately, but don't do this if the second operand
9662 is already a constant integer. */
9663 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9664 {
9665 tem = op0, op0 = op1, op1 = tem;
9666 code = swap_condition (code);
9667 }
9668
9669 /* We now enter a loop during which we will try to simplify the comparison.
9670 For the most part, we only are concerned with comparisons with zero,
9671 but some things may really be comparisons with zero but not start
9672 out looking that way. */
9673
9674 while (GET_CODE (op1) == CONST_INT)
9675 {
9676 enum machine_mode mode = GET_MODE (op0);
9677 int mode_width = GET_MODE_BITSIZE (mode);
9678 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9679 int equality_comparison_p;
9680 int sign_bit_comparison_p;
9681 int unsigned_comparison_p;
9682 HOST_WIDE_INT const_op;
9683
9684 /* We only want to handle integral modes. This catches VOIDmode,
9685 CCmode, and the floating-point modes. An exception is that we
9686 can handle VOIDmode if OP0 is a COMPARE or a comparison
9687 operation. */
9688
9689 if (GET_MODE_CLASS (mode) != MODE_INT
9690 && ! (mode == VOIDmode
9691 && (GET_CODE (op0) == COMPARE
9692 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9693 break;
9694
9695 /* Get the constant we are comparing against and turn off all bits
9696 not on in our mode. */
9697 const_op = INTVAL (op1);
9698 if (mode_width <= HOST_BITS_PER_WIDE_INT)
9699 const_op &= mask;
9700
9701 /* If we are comparing against a constant power of two and the value
9702 being compared can only have that single bit nonzero (e.g., it was
9703 `and'ed with that bit), we can replace this with a comparison
9704 with zero. */
9705 if (const_op
9706 && (code == EQ || code == NE || code == GE || code == GEU
9707 || code == LT || code == LTU)
9708 && mode_width <= HOST_BITS_PER_WIDE_INT
9709 && exact_log2 (const_op) >= 0
9710 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9711 {
9712 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9713 op1 = const0_rtx, const_op = 0;
9714 }
9715
9716 /* Similarly, if we are comparing a value known to be either -1 or
9717 0 with -1, change it to the opposite comparison against zero. */
9718
9719 if (const_op == -1
9720 && (code == EQ || code == NE || code == GT || code == LE
9721 || code == GEU || code == LTU)
9722 && num_sign_bit_copies (op0, mode) == mode_width)
9723 {
9724 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9725 op1 = const0_rtx, const_op = 0;
9726 }
9727
9728 /* Do some canonicalizations based on the comparison code. We prefer
9729 comparisons against zero and then prefer equality comparisons.
9730 If we can reduce the size of a constant, we will do that too. */
9731
9732 switch (code)
9733 {
9734 case LT:
9735 /* < C is equivalent to <= (C - 1) */
9736 if (const_op > 0)
9737 {
9738 const_op -= 1;
9739 op1 = GEN_INT (const_op);
9740 code = LE;
9741 /* ... fall through to LE case below. */
9742 }
9743 else
9744 break;
9745
9746 case LE:
9747 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9748 if (const_op < 0)
9749 {
9750 const_op += 1;
9751 op1 = GEN_INT (const_op);
9752 code = LT;
9753 }
9754
9755 /* If we are doing a <= 0 comparison on a value known to have
9756 a zero sign bit, we can replace this with == 0. */
9757 else if (const_op == 0
9758 && mode_width <= HOST_BITS_PER_WIDE_INT
9759 && (nonzero_bits (op0, mode)
9760 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9761 code = EQ;
9762 break;
9763
9764 case GE:
9765 /* >= C is equivalent to > (C - 1). */
9766 if (const_op > 0)
9767 {
9768 const_op -= 1;
9769 op1 = GEN_INT (const_op);
9770 code = GT;
9771 /* ... fall through to GT below. */
9772 }
9773 else
9774 break;
9775
9776 case GT:
9777 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9778 if (const_op < 0)
9779 {
9780 const_op += 1;
9781 op1 = GEN_INT (const_op);
9782 code = GE;
9783 }
9784
9785 /* If we are doing a > 0 comparison on a value known to have
9786 a zero sign bit, we can replace this with != 0. */
9787 else if (const_op == 0
9788 && mode_width <= HOST_BITS_PER_WIDE_INT
9789 && (nonzero_bits (op0, mode)
9790 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9791 code = NE;
9792 break;
9793
9794 case LTU:
9795 /* < C is equivalent to <= (C - 1). */
9796 if (const_op > 0)
9797 {
9798 const_op -= 1;
9799 op1 = GEN_INT (const_op);
9800 code = LEU;
9801 /* ... fall through ... */
9802 }
9803
9804 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9805 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9806 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9807 {
9808 const_op = 0, op1 = const0_rtx;
9809 code = GE;
9810 break;
9811 }
9812 else
9813 break;
9814
9815 case LEU:
9816 /* unsigned <= 0 is equivalent to == 0 */
9817 if (const_op == 0)
9818 code = EQ;
9819
9820 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9821 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9822 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9823 {
9824 const_op = 0, op1 = const0_rtx;
9825 code = GE;
9826 }
9827 break;
9828
9829 case GEU:
9830 /* >= C is equivalent to < (C - 1). */
9831 if (const_op > 1)
9832 {
9833 const_op -= 1;
9834 op1 = GEN_INT (const_op);
9835 code = GTU;
9836 /* ... fall through ... */
9837 }
9838
9839 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9840 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9841 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9842 {
9843 const_op = 0, op1 = const0_rtx;
9844 code = LT;
9845 break;
9846 }
9847 else
9848 break;
9849
9850 case GTU:
9851 /* unsigned > 0 is equivalent to != 0 */
9852 if (const_op == 0)
9853 code = NE;
9854
9855 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9856 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9857 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9858 {
9859 const_op = 0, op1 = const0_rtx;
9860 code = LT;
9861 }
9862 break;
9863
9864 default:
9865 break;
9866 }
9867
9868 /* Compute some predicates to simplify code below. */
9869
9870 equality_comparison_p = (code == EQ || code == NE);
9871 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9872 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9873 || code == LEU);
9874
9875 /* If this is a sign bit comparison and we can do arithmetic in
9876 MODE, say that we will only be needing the sign bit of OP0. */
9877 if (sign_bit_comparison_p
9878 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9879 op0 = force_to_mode (op0, mode,
9880 ((HOST_WIDE_INT) 1
9881 << (GET_MODE_BITSIZE (mode) - 1)),
9882 NULL_RTX, 0);
9883
9884 /* Now try cases based on the opcode of OP0. If none of the cases
9885 does a "continue", we exit this loop immediately after the
9886 switch. */
9887
9888 switch (GET_CODE (op0))
9889 {
9890 case ZERO_EXTRACT:
9891 /* If we are extracting a single bit from a variable position in
9892 a constant that has only a single bit set and are comparing it
9893 with zero, we can convert this into an equality comparison
9894 between the position and the location of the single bit. */
9895
9896 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9897 && XEXP (op0, 1) == const1_rtx
9898 && equality_comparison_p && const_op == 0
9899 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9900 {
9901 if (BITS_BIG_ENDIAN)
9902 {
9903 #ifdef HAVE_extzv
9904 mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
9905 if (mode == VOIDmode)
9906 mode = word_mode;
9907 i = (GET_MODE_BITSIZE (mode) - 1 - i);
9908 #else
9909 i = BITS_PER_WORD - 1 - i;
9910 #endif
9911 }
9912
9913 op0 = XEXP (op0, 2);
9914 op1 = GEN_INT (i);
9915 const_op = i;
9916
9917 /* Result is nonzero iff shift count is equal to I. */
9918 code = reverse_condition (code);
9919 continue;
9920 }
9921
9922 /* ... fall through ... */
9923
9924 case SIGN_EXTRACT:
9925 tem = expand_compound_operation (op0);
9926 if (tem != op0)
9927 {
9928 op0 = tem;
9929 continue;
9930 }
9931 break;
9932
9933 case NOT:
9934 /* If testing for equality, we can take the NOT of the constant. */
9935 if (equality_comparison_p
9936 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9937 {
9938 op0 = XEXP (op0, 0);
9939 op1 = tem;
9940 continue;
9941 }
9942
9943 /* If just looking at the sign bit, reverse the sense of the
9944 comparison. */
9945 if (sign_bit_comparison_p)
9946 {
9947 op0 = XEXP (op0, 0);
9948 code = (code == GE ? LT : GE);
9949 continue;
9950 }
9951 break;
9952
9953 case NEG:
9954 /* If testing for equality, we can take the NEG of the constant. */
9955 if (equality_comparison_p
9956 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9957 {
9958 op0 = XEXP (op0, 0);
9959 op1 = tem;
9960 continue;
9961 }
9962
9963 /* The remaining cases only apply to comparisons with zero. */
9964 if (const_op != 0)
9965 break;
9966
9967 /* When X is ABS or is known positive,
9968 (neg X) is < 0 if and only if X != 0. */
9969
9970 if (sign_bit_comparison_p
9971 && (GET_CODE (XEXP (op0, 0)) == ABS
9972 || (mode_width <= HOST_BITS_PER_WIDE_INT
9973 && (nonzero_bits (XEXP (op0, 0), mode)
9974 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9975 {
9976 op0 = XEXP (op0, 0);
9977 code = (code == LT ? NE : EQ);
9978 continue;
9979 }
9980
9981 /* If we have NEG of something whose two high-order bits are the
9982 same, we know that "(-a) < 0" is equivalent to "a > 0". */
9983 if (num_sign_bit_copies (op0, mode) >= 2)
9984 {
9985 op0 = XEXP (op0, 0);
9986 code = swap_condition (code);
9987 continue;
9988 }
9989 break;
9990
9991 case ROTATE:
9992 /* If we are testing equality and our count is a constant, we
9993 can perform the inverse operation on our RHS. */
9994 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9995 && (tem = simplify_binary_operation (ROTATERT, mode,
9996 op1, XEXP (op0, 1))) != 0)
9997 {
9998 op0 = XEXP (op0, 0);
9999 op1 = tem;
10000 continue;
10001 }
10002
10003 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10004 a particular bit. Convert it to an AND of a constant of that
10005 bit. This will be converted into a ZERO_EXTRACT. */
10006 if (const_op == 0 && sign_bit_comparison_p
10007 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10008 && mode_width <= HOST_BITS_PER_WIDE_INT)
10009 {
10010 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10011 ((HOST_WIDE_INT) 1
10012 << (mode_width - 1
10013 - INTVAL (XEXP (op0, 1)))));
10014 code = (code == LT ? NE : EQ);
10015 continue;
10016 }
10017
10018 /* ... fall through ... */
10019
10020 case ABS:
10021 /* ABS is ignorable inside an equality comparison with zero. */
10022 if (const_op == 0 && equality_comparison_p)
10023 {
10024 op0 = XEXP (op0, 0);
10025 continue;
10026 }
10027 break;
10028
10029
10030 case SIGN_EXTEND:
10031 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10032 to (compare FOO CONST) if CONST fits in FOO's mode and we
10033 are either testing inequality or have an unsigned comparison
10034 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10035 if (! unsigned_comparison_p
10036 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10037 <= HOST_BITS_PER_WIDE_INT)
10038 && ((unsigned HOST_WIDE_INT) const_op
10039 < (((unsigned HOST_WIDE_INT) 1
10040 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10041 {
10042 op0 = XEXP (op0, 0);
10043 continue;
10044 }
10045 break;
10046
10047 case SUBREG:
10048 /* Check for the case where we are comparing A - C1 with C2,
10049 both constants are smaller than 1/2 the maximum positive
10050 value in MODE, and the comparison is equality or unsigned.
10051 In that case, if A is either zero-extended to MODE or has
10052 sufficient sign bits so that the high-order bit in MODE
10053 is a copy of the sign in the inner mode, we can prove that it is
10054 safe to do the operation in the wider mode. This simplifies
10055 many range checks. */
10056
10057 if (mode_width <= HOST_BITS_PER_WIDE_INT
10058 && subreg_lowpart_p (op0)
10059 && GET_CODE (SUBREG_REG (op0)) == PLUS
10060 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10061 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10062 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
10063 < (HOST_WIDE_INT)(GET_MODE_MASK (mode) / 2))
10064 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10065 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10066 GET_MODE (SUBREG_REG (op0)))
10067 & ~ GET_MODE_MASK (mode))
10068 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10069 GET_MODE (SUBREG_REG (op0)))
10070 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10071 - GET_MODE_BITSIZE (mode)))))
10072 {
10073 op0 = SUBREG_REG (op0);
10074 continue;
10075 }
10076
10077 /* If the inner mode is narrower and we are extracting the low part,
10078 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10079 if (subreg_lowpart_p (op0)
10080 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10081 /* Fall through */ ;
10082 else
10083 break;
10084
10085 /* ... fall through ... */
10086
10087 case ZERO_EXTEND:
10088 if ((unsigned_comparison_p || equality_comparison_p)
10089 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10090 <= HOST_BITS_PER_WIDE_INT)
10091 && ((unsigned HOST_WIDE_INT) const_op
10092 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10093 {
10094 op0 = XEXP (op0, 0);
10095 continue;
10096 }
10097 break;
10098
10099 case PLUS:
10100 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10101 this for equality comparisons due to pathological cases involving
10102 overflows. */
10103 if (equality_comparison_p
10104 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10105 op1, XEXP (op0, 1))))
10106 {
10107 op0 = XEXP (op0, 0);
10108 op1 = tem;
10109 continue;
10110 }
10111
10112 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10113 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10114 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10115 {
10116 op0 = XEXP (XEXP (op0, 0), 0);
10117 code = (code == LT ? EQ : NE);
10118 continue;
10119 }
10120 break;
10121
10122 case MINUS:
10123 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10124 (eq B (minus A C)), whichever simplifies. We can only do
10125 this for equality comparisons due to pathological cases involving
10126 overflows. */
10127 if (equality_comparison_p
10128 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10129 XEXP (op0, 1), op1)))
10130 {
10131 op0 = XEXP (op0, 0);
10132 op1 = tem;
10133 continue;
10134 }
10135
10136 if (equality_comparison_p
10137 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10138 XEXP (op0, 0), op1)))
10139 {
10140 op0 = XEXP (op0, 1);
10141 op1 = tem;
10142 continue;
10143 }
10144
10145 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10146 of bits in X minus 1, is one iff X > 0. */
10147 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10148 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10149 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10150 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10151 {
10152 op0 = XEXP (op0, 1);
10153 code = (code == GE ? LE : GT);
10154 continue;
10155 }
10156 break;
10157
10158 case XOR:
10159 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10160 if C is zero or B is a constant. */
10161 if (equality_comparison_p
10162 && 0 != (tem = simplify_binary_operation (XOR, mode,
10163 XEXP (op0, 1), op1)))
10164 {
10165 op0 = XEXP (op0, 0);
10166 op1 = tem;
10167 continue;
10168 }
10169 break;
10170
10171 case EQ: case NE:
10172 case LT: case LTU: case LE: case LEU:
10173 case GT: case GTU: case GE: case GEU:
10174 /* We can't do anything if OP0 is a condition code value, rather
10175 than an actual data value. */
10176 if (const_op != 0
10177 #ifdef HAVE_cc0
10178 || XEXP (op0, 0) == cc0_rtx
10179 #endif
10180 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10181 break;
10182
10183 /* Get the two operands being compared. */
10184 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10185 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10186 else
10187 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10188
10189 /* Check for the cases where we simply want the result of the
10190 earlier test or the opposite of that result. */
10191 if (code == NE
10192 || (code == EQ && reversible_comparison_p (op0))
10193 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10194 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10195 && (STORE_FLAG_VALUE
10196 & (((HOST_WIDE_INT) 1
10197 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10198 && (code == LT
10199 || (code == GE && reversible_comparison_p (op0)))))
10200 {
10201 code = (code == LT || code == NE
10202 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10203 op0 = tem, op1 = tem1;
10204 continue;
10205 }
10206 break;
10207
10208 case IOR:
10209 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10210 iff X <= 0. */
10211 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10212 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10213 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10214 {
10215 op0 = XEXP (op0, 1);
10216 code = (code == GE ? GT : LE);
10217 continue;
10218 }
10219 break;
10220
10221 case AND:
10222 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10223 will be converted to a ZERO_EXTRACT later. */
10224 if (const_op == 0 && equality_comparison_p
10225 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10226 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10227 {
10228 op0 = simplify_and_const_int
10229 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10230 XEXP (op0, 1),
10231 XEXP (XEXP (op0, 0), 1)),
10232 (HOST_WIDE_INT) 1);
10233 continue;
10234 }
10235
10236 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10237 zero and X is a comparison and C1 and C2 describe only bits set
10238 in STORE_FLAG_VALUE, we can compare with X. */
10239 if (const_op == 0 && equality_comparison_p
10240 && mode_width <= HOST_BITS_PER_WIDE_INT
10241 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10242 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10243 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10244 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10245 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10246 {
10247 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10248 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10249 if ((~ STORE_FLAG_VALUE & mask) == 0
10250 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10251 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10252 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10253 {
10254 op0 = XEXP (XEXP (op0, 0), 0);
10255 continue;
10256 }
10257 }
10258
10259 /* If we are doing an equality comparison of an AND of a bit equal
10260 to the sign bit, replace this with a LT or GE comparison of
10261 the underlying value. */
10262 if (equality_comparison_p
10263 && const_op == 0
10264 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10265 && mode_width <= HOST_BITS_PER_WIDE_INT
10266 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10267 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10268 {
10269 op0 = XEXP (op0, 0);
10270 code = (code == EQ ? GE : LT);
10271 continue;
10272 }
10273
10274 /* If this AND operation is really a ZERO_EXTEND from a narrower
10275 mode, the constant fits within that mode, and this is either an
10276 equality or unsigned comparison, try to do this comparison in
10277 the narrower mode. */
10278 if ((equality_comparison_p || unsigned_comparison_p)
10279 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10280 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10281 & GET_MODE_MASK (mode))
10282 + 1)) >= 0
10283 && const_op >> i == 0
10284 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10285 {
10286 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10287 continue;
10288 }
10289
10290 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10291 in both M1 and M2 and the SUBREG is either paradoxical or
10292 represents the low part, permute the SUBREG and the AND and
10293 try again. */
10294 if (GET_CODE (XEXP (op0, 0)) == SUBREG
10295 && (0
10296 #ifdef WORD_REGISTER_OPERATIONS
10297 || ((mode_width
10298 > (GET_MODE_BITSIZE
10299 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10300 && mode_width <= BITS_PER_WORD)
10301 #endif
10302 || ((mode_width
10303 <= (GET_MODE_BITSIZE
10304 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10305 && subreg_lowpart_p (XEXP (op0, 0))))
10306 #ifndef WORD_REGISTER_OPERATIONS
10307 /* It is unsafe to commute the AND into the SUBREG if the SUBREG
10308 is paradoxical and WORD_REGISTER_OPERATIONS is not defined.
10309 As originally written the upper bits have a defined value
10310 due to the AND operation. However, if we commute the AND
10311 inside the SUBREG then they no longer have defined values
10312 and the meaning of the code has been changed. */
10313 && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
10314 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10315 #endif
10316 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10317 && mode_width <= HOST_BITS_PER_WIDE_INT
10318 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10319 <= HOST_BITS_PER_WIDE_INT)
10320 && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10321 && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10322 & INTVAL (XEXP (op0, 1)))
10323 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1)) != mask
10324 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10325 != GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10326
10327 {
10328 op0
10329 = gen_lowpart_for_combine
10330 (mode,
10331 gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10332 SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10333 continue;
10334 }
10335
10336 break;
10337
10338 case ASHIFT:
10339 /* If we have (compare (ashift FOO N) (const_int C)) and
10340 the high order N bits of FOO (N+1 if an inequality comparison)
10341 are known to be zero, we can do this by comparing FOO with C
10342 shifted right N bits so long as the low-order N bits of C are
10343 zero. */
10344 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10345 && INTVAL (XEXP (op0, 1)) >= 0
10346 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10347 < HOST_BITS_PER_WIDE_INT)
10348 && ((const_op
10349 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10350 && mode_width <= HOST_BITS_PER_WIDE_INT
10351 && (nonzero_bits (XEXP (op0, 0), mode)
10352 & ~ (mask >> (INTVAL (XEXP (op0, 1))
10353 + ! equality_comparison_p))) == 0)
10354 {
10355 const_op >>= INTVAL (XEXP (op0, 1));
10356 op1 = GEN_INT (const_op);
10357 op0 = XEXP (op0, 0);
10358 continue;
10359 }
10360
10361 /* If we are doing a sign bit comparison, it means we are testing
10362 a particular bit. Convert it to the appropriate AND. */
10363 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10364 && mode_width <= HOST_BITS_PER_WIDE_INT)
10365 {
10366 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10367 ((HOST_WIDE_INT) 1
10368 << (mode_width - 1
10369 - INTVAL (XEXP (op0, 1)))));
10370 code = (code == LT ? NE : EQ);
10371 continue;
10372 }
10373
10374 /* If this an equality comparison with zero and we are shifting
10375 the low bit to the sign bit, we can convert this to an AND of the
10376 low-order bit. */
10377 if (const_op == 0 && equality_comparison_p
10378 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10379 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10380 {
10381 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10382 (HOST_WIDE_INT) 1);
10383 continue;
10384 }
10385 break;
10386
10387 case ASHIFTRT:
10388 /* If this is an equality comparison with zero, we can do this
10389 as a logical shift, which might be much simpler. */
10390 if (equality_comparison_p && const_op == 0
10391 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10392 {
10393 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10394 XEXP (op0, 0),
10395 INTVAL (XEXP (op0, 1)));
10396 continue;
10397 }
10398
10399 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10400 do the comparison in a narrower mode. */
10401 if (! unsigned_comparison_p
10402 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10403 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10404 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10405 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10406 MODE_INT, 1)) != BLKmode
10407 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10408 || ((unsigned HOST_WIDE_INT) - const_op
10409 <= GET_MODE_MASK (tmode))))
10410 {
10411 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10412 continue;
10413 }
10414
10415 /* ... fall through ... */
10416 case LSHIFTRT:
10417 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10418 the low order N bits of FOO are known to be zero, we can do this
10419 by comparing FOO with C shifted left N bits so long as no
10420 overflow occurs. */
10421 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10422 && INTVAL (XEXP (op0, 1)) >= 0
10423 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10424 && mode_width <= HOST_BITS_PER_WIDE_INT
10425 && (nonzero_bits (XEXP (op0, 0), mode)
10426 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10427 && (const_op == 0
10428 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10429 < mode_width)))
10430 {
10431 const_op <<= INTVAL (XEXP (op0, 1));
10432 op1 = GEN_INT (const_op);
10433 op0 = XEXP (op0, 0);
10434 continue;
10435 }
10436
10437 /* If we are using this shift to extract just the sign bit, we
10438 can replace this with an LT or GE comparison. */
10439 if (const_op == 0
10440 && (equality_comparison_p || sign_bit_comparison_p)
10441 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10442 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10443 {
10444 op0 = XEXP (op0, 0);
10445 code = (code == NE || code == GT ? LT : GE);
10446 continue;
10447 }
10448 break;
10449
10450 default:
10451 break;
10452 }
10453
10454 break;
10455 }
10456
10457 /* Now make any compound operations involved in this comparison. Then,
10458 check for an outmost SUBREG on OP0 that is not doing anything or is
10459 paradoxical. The latter case can only occur when it is known that the
10460 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
10461 We can never remove a SUBREG for a non-equality comparison because the
10462 sign bit is in a different place in the underlying object. */
10463
10464 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10465 op1 = make_compound_operation (op1, SET);
10466
10467 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10468 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10469 && (code == NE || code == EQ)
10470 && ((GET_MODE_SIZE (GET_MODE (op0))
10471 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10472 {
10473 op0 = SUBREG_REG (op0);
10474 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10475 }
10476
10477 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10478 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10479 && (code == NE || code == EQ)
10480 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10481 <= HOST_BITS_PER_WIDE_INT)
10482 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10483 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10484 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10485 op1),
10486 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10487 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10488 op0 = SUBREG_REG (op0), op1 = tem;
10489
10490 /* We now do the opposite procedure: Some machines don't have compare
10491 insns in all modes. If OP0's mode is an integer mode smaller than a
10492 word and we can't do a compare in that mode, see if there is a larger
10493 mode for which we can do the compare. There are a number of cases in
10494 which we can use the wider mode. */
10495
10496 mode = GET_MODE (op0);
10497 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10498 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10499 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10500 for (tmode = GET_MODE_WIDER_MODE (mode);
10501 (tmode != VOIDmode
10502 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10503 tmode = GET_MODE_WIDER_MODE (tmode))
10504 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10505 {
10506 /* If the only nonzero bits in OP0 and OP1 are those in the
10507 narrower mode and this is an equality or unsigned comparison,
10508 we can use the wider mode. Similarly for sign-extended
10509 values, in which case it is true for all comparisons. */
10510 if (((code == EQ || code == NE
10511 || code == GEU || code == GTU || code == LEU || code == LTU)
10512 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10513 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10514 || ((num_sign_bit_copies (op0, tmode)
10515 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10516 && (num_sign_bit_copies (op1, tmode)
10517 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10518 {
10519 op0 = gen_lowpart_for_combine (tmode, op0);
10520 op1 = gen_lowpart_for_combine (tmode, op1);
10521 break;
10522 }
10523
10524 /* If this is a test for negative, we can make an explicit
10525 test of the sign bit. */
10526
10527 if (op1 == const0_rtx && (code == LT || code == GE)
10528 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10529 {
10530 op0 = gen_binary (AND, tmode,
10531 gen_lowpart_for_combine (tmode, op0),
10532 GEN_INT ((HOST_WIDE_INT) 1
10533 << (GET_MODE_BITSIZE (mode) - 1)));
10534 code = (code == LT) ? NE : EQ;
10535 break;
10536 }
10537 }
10538
10539 #ifdef CANONICALIZE_COMPARISON
10540 /* If this machine only supports a subset of valid comparisons, see if we
10541 can convert an unsupported one into a supported one. */
10542 CANONICALIZE_COMPARISON (code, op0, op1);
10543 #endif
10544
10545 *pop0 = op0;
10546 *pop1 = op1;
10547
10548 return code;
10549 }
10550 \f
10551 /* Return 1 if we know that X, a comparison operation, is not operating
10552 on a floating-point value or is EQ or NE, meaning that we can safely
10553 reverse it. */
10554
10555 static int
10556 reversible_comparison_p (x)
10557 rtx x;
10558 {
10559 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10560 || flag_fast_math
10561 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10562 return 1;
10563
10564 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10565 {
10566 case MODE_INT:
10567 case MODE_PARTIAL_INT:
10568 case MODE_COMPLEX_INT:
10569 return 1;
10570
10571 case MODE_CC:
10572 /* If the mode of the condition codes tells us that this is safe,
10573 we need look no further. */
10574 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10575 return 1;
10576
10577 /* Otherwise try and find where the condition codes were last set and
10578 use that. */
10579 x = get_last_value (XEXP (x, 0));
10580 return (x && GET_CODE (x) == COMPARE
10581 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10582
10583 default:
10584 return 0;
10585 }
10586 }
10587 \f
10588 /* Utility function for following routine. Called when X is part of a value
10589 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10590 for each register mentioned. Similar to mention_regs in cse.c */
10591
10592 static void
10593 update_table_tick (x)
10594 rtx x;
10595 {
10596 register enum rtx_code code = GET_CODE (x);
10597 register const char *fmt = GET_RTX_FORMAT (code);
10598 register int i;
10599
10600 if (code == REG)
10601 {
10602 int regno = REGNO (x);
10603 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10604 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10605
10606 for (i = regno; i < endregno; i++)
10607 reg_last_set_table_tick[i] = label_tick;
10608
10609 return;
10610 }
10611
10612 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10613 /* Note that we can't have an "E" in values stored; see
10614 get_last_value_validate. */
10615 if (fmt[i] == 'e')
10616 update_table_tick (XEXP (x, i));
10617 }
10618
10619 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10620 are saying that the register is clobbered and we no longer know its
10621 value. If INSN is zero, don't update reg_last_set; this is only permitted
10622 with VALUE also zero and is used to invalidate the register. */
10623
10624 static void
10625 record_value_for_reg (reg, insn, value)
10626 rtx reg;
10627 rtx insn;
10628 rtx value;
10629 {
10630 int regno = REGNO (reg);
10631 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10632 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10633 int i;
10634
10635 /* If VALUE contains REG and we have a previous value for REG, substitute
10636 the previous value. */
10637 if (value && insn && reg_overlap_mentioned_p (reg, value))
10638 {
10639 rtx tem;
10640
10641 /* Set things up so get_last_value is allowed to see anything set up to
10642 our insn. */
10643 subst_low_cuid = INSN_CUID (insn);
10644 tem = get_last_value (reg);
10645
10646 if (tem)
10647 value = replace_rtx (copy_rtx (value), reg, tem);
10648 }
10649
10650 /* For each register modified, show we don't know its value, that
10651 we don't know about its bitwise content, that its value has been
10652 updated, and that we don't know the location of the death of the
10653 register. */
10654 for (i = regno; i < endregno; i ++)
10655 {
10656 if (insn)
10657 reg_last_set[i] = insn;
10658 reg_last_set_value[i] = 0;
10659 reg_last_set_mode[i] = 0;
10660 reg_last_set_nonzero_bits[i] = 0;
10661 reg_last_set_sign_bit_copies[i] = 0;
10662 reg_last_death[i] = 0;
10663 }
10664
10665 /* Mark registers that are being referenced in this value. */
10666 if (value)
10667 update_table_tick (value);
10668
10669 /* Now update the status of each register being set.
10670 If someone is using this register in this block, set this register
10671 to invalid since we will get confused between the two lives in this
10672 basic block. This makes using this register always invalid. In cse, we
10673 scan the table to invalidate all entries using this register, but this
10674 is too much work for us. */
10675
10676 for (i = regno; i < endregno; i++)
10677 {
10678 reg_last_set_label[i] = label_tick;
10679 if (value && reg_last_set_table_tick[i] == label_tick)
10680 reg_last_set_invalid[i] = 1;
10681 else
10682 reg_last_set_invalid[i] = 0;
10683 }
10684
10685 /* The value being assigned might refer to X (like in "x++;"). In that
10686 case, we must replace it with (clobber (const_int 0)) to prevent
10687 infinite loops. */
10688 if (value && ! get_last_value_validate (&value, insn,
10689 reg_last_set_label[regno], 0))
10690 {
10691 value = copy_rtx (value);
10692 if (! get_last_value_validate (&value, insn,
10693 reg_last_set_label[regno], 1))
10694 value = 0;
10695 }
10696
10697 /* For the main register being modified, update the value, the mode, the
10698 nonzero bits, and the number of sign bit copies. */
10699
10700 reg_last_set_value[regno] = value;
10701
10702 if (value)
10703 {
10704 subst_low_cuid = INSN_CUID (insn);
10705 reg_last_set_mode[regno] = GET_MODE (reg);
10706 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10707 reg_last_set_sign_bit_copies[regno]
10708 = num_sign_bit_copies (value, GET_MODE (reg));
10709 }
10710 }
10711
10712 /* Used for communication between the following two routines. */
10713 static rtx record_dead_insn;
10714
10715 /* Called via note_stores from record_dead_and_set_regs to handle one
10716 SET or CLOBBER in an insn. */
10717
10718 static void
10719 record_dead_and_set_regs_1 (dest, setter)
10720 rtx dest, setter;
10721 {
10722 if (GET_CODE (dest) == SUBREG)
10723 dest = SUBREG_REG (dest);
10724
10725 if (GET_CODE (dest) == REG)
10726 {
10727 /* If we are setting the whole register, we know its value. Otherwise
10728 show that we don't know the value. We can handle SUBREG in
10729 some cases. */
10730 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10731 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10732 else if (GET_CODE (setter) == SET
10733 && GET_CODE (SET_DEST (setter)) == SUBREG
10734 && SUBREG_REG (SET_DEST (setter)) == dest
10735 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10736 && subreg_lowpart_p (SET_DEST (setter)))
10737 record_value_for_reg (dest, record_dead_insn,
10738 gen_lowpart_for_combine (GET_MODE (dest),
10739 SET_SRC (setter)));
10740 else
10741 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10742 }
10743 else if (GET_CODE (dest) == MEM
10744 /* Ignore pushes, they clobber nothing. */
10745 && ! push_operand (dest, GET_MODE (dest)))
10746 mem_last_set = INSN_CUID (record_dead_insn);
10747 }
10748
10749 /* Update the records of when each REG was most recently set or killed
10750 for the things done by INSN. This is the last thing done in processing
10751 INSN in the combiner loop.
10752
10753 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10754 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10755 and also the similar information mem_last_set (which insn most recently
10756 modified memory) and last_call_cuid (which insn was the most recent
10757 subroutine call). */
10758
10759 static void
10760 record_dead_and_set_regs (insn)
10761 rtx insn;
10762 {
10763 register rtx link;
10764 int i;
10765
10766 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10767 {
10768 if (REG_NOTE_KIND (link) == REG_DEAD
10769 && GET_CODE (XEXP (link, 0)) == REG)
10770 {
10771 int regno = REGNO (XEXP (link, 0));
10772 int endregno
10773 = regno + (regno < FIRST_PSEUDO_REGISTER
10774 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10775 : 1);
10776
10777 for (i = regno; i < endregno; i++)
10778 reg_last_death[i] = insn;
10779 }
10780 else if (REG_NOTE_KIND (link) == REG_INC)
10781 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10782 }
10783
10784 if (GET_CODE (insn) == CALL_INSN)
10785 {
10786 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10787 if (call_used_regs[i])
10788 {
10789 reg_last_set_value[i] = 0;
10790 reg_last_set_mode[i] = 0;
10791 reg_last_set_nonzero_bits[i] = 0;
10792 reg_last_set_sign_bit_copies[i] = 0;
10793 reg_last_death[i] = 0;
10794 }
10795
10796 last_call_cuid = mem_last_set = INSN_CUID (insn);
10797 }
10798
10799 record_dead_insn = insn;
10800 note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10801 }
10802 \f
10803 /* Utility routine for the following function. Verify that all the registers
10804 mentioned in *LOC are valid when *LOC was part of a value set when
10805 label_tick == TICK. Return 0 if some are not.
10806
10807 If REPLACE is non-zero, replace the invalid reference with
10808 (clobber (const_int 0)) and return 1. This replacement is useful because
10809 we often can get useful information about the form of a value (e.g., if
10810 it was produced by a shift that always produces -1 or 0) even though
10811 we don't know exactly what registers it was produced from. */
10812
10813 static int
10814 get_last_value_validate (loc, insn, tick, replace)
10815 rtx *loc;
10816 rtx insn;
10817 int tick;
10818 int replace;
10819 {
10820 rtx x = *loc;
10821 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10822 int len = GET_RTX_LENGTH (GET_CODE (x));
10823 int i;
10824
10825 if (GET_CODE (x) == REG)
10826 {
10827 int regno = REGNO (x);
10828 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10829 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10830 int j;
10831
10832 for (j = regno; j < endregno; j++)
10833 if (reg_last_set_invalid[j]
10834 /* If this is a pseudo-register that was only set once and not
10835 live at the beginning of the function, it is always valid. */
10836 || (! (regno >= FIRST_PSEUDO_REGISTER
10837 && REG_N_SETS (regno) == 1
10838 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
10839 && reg_last_set_label[j] > tick))
10840 {
10841 if (replace)
10842 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10843 return replace;
10844 }
10845
10846 return 1;
10847 }
10848 /* If this is a memory reference, make sure that there were
10849 no stores after it that might have clobbered the value. We don't
10850 have alias info, so we assume any store invalidates it. */
10851 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10852 && INSN_CUID (insn) <= mem_last_set)
10853 {
10854 if (replace)
10855 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10856 return replace;
10857 }
10858
10859 for (i = 0; i < len; i++)
10860 if ((fmt[i] == 'e'
10861 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10862 /* Don't bother with these. They shouldn't occur anyway. */
10863 || fmt[i] == 'E')
10864 return 0;
10865
10866 /* If we haven't found a reason for it to be invalid, it is valid. */
10867 return 1;
10868 }
10869
10870 /* Get the last value assigned to X, if known. Some registers
10871 in the value may be replaced with (clobber (const_int 0)) if their value
10872 is known longer known reliably. */
10873
10874 static rtx
10875 get_last_value (x)
10876 rtx x;
10877 {
10878 int regno;
10879 rtx value;
10880
10881 /* If this is a non-paradoxical SUBREG, get the value of its operand and
10882 then convert it to the desired mode. If this is a paradoxical SUBREG,
10883 we cannot predict what values the "extra" bits might have. */
10884 if (GET_CODE (x) == SUBREG
10885 && subreg_lowpart_p (x)
10886 && (GET_MODE_SIZE (GET_MODE (x))
10887 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10888 && (value = get_last_value (SUBREG_REG (x))) != 0)
10889 return gen_lowpart_for_combine (GET_MODE (x), value);
10890
10891 if (GET_CODE (x) != REG)
10892 return 0;
10893
10894 regno = REGNO (x);
10895 value = reg_last_set_value[regno];
10896
10897 /* If we don't have a value, or if it isn't for this basic block and
10898 it's either a hard register, set more than once, or it's a live
10899 at the beginning of the function, return 0.
10900
10901 Because if it's not live at the beginnning of the function then the reg
10902 is always set before being used (is never used without being set).
10903 And, if it's set only once, and it's always set before use, then all
10904 uses must have the same last value, even if it's not from this basic
10905 block. */
10906
10907 if (value == 0
10908 || (reg_last_set_label[regno] != label_tick
10909 && (regno < FIRST_PSEUDO_REGISTER
10910 || REG_N_SETS (regno) != 1
10911 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))))
10912 return 0;
10913
10914 /* If the value was set in a later insn than the ones we are processing,
10915 we can't use it even if the register was only set once, but make a quick
10916 check to see if the previous insn set it to something. This is commonly
10917 the case when the same pseudo is used by repeated insns.
10918
10919 This does not work if there exists an instruction which is temporarily
10920 not on the insn chain. */
10921
10922 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10923 {
10924 rtx insn, set;
10925
10926 /* We can't do anything if the value is set in between the insns we are
10927 processing. */
10928 if (INSN_CUID (reg_last_set[regno]) <= INSN_CUID (subst_insn))
10929 return 0;
10930
10931 /* We can not do anything useful in this case, because there is
10932 an instruction which is not on the insn chain. */
10933 if (subst_prev_insn)
10934 return 0;
10935
10936 /* Skip over USE insns. They are not useful here, and they may have
10937 been made by combine, in which case they do not have a INSN_CUID
10938 value. We can't use prev_real_insn, because that would incorrectly
10939 take us backwards across labels. Skip over BARRIERs also, since
10940 they could have been made by combine. If we see one, we must be
10941 optimizing dead code, so it doesn't matter what we do. */
10942 for (insn = prev_nonnote_insn (subst_insn);
10943 insn && ((GET_CODE (insn) == INSN
10944 && GET_CODE (PATTERN (insn)) == USE)
10945 || GET_CODE (insn) == BARRIER
10946 || INSN_CUID (insn) >= subst_low_cuid);
10947 insn = prev_nonnote_insn (insn))
10948 ;
10949
10950 if (insn
10951 && (set = single_set (insn)) != 0
10952 && rtx_equal_p (SET_DEST (set), x))
10953 {
10954 value = SET_SRC (set);
10955
10956 /* Make sure that VALUE doesn't reference X. Replace any
10957 explicit references with a CLOBBER. If there are any remaining
10958 references (rare), don't use the value. */
10959
10960 if (reg_mentioned_p (x, value))
10961 value = replace_rtx (copy_rtx (value), x,
10962 gen_rtx_CLOBBER (GET_MODE (x), const0_rtx));
10963
10964 if (reg_overlap_mentioned_p (x, value))
10965 return 0;
10966 }
10967 else
10968 return 0;
10969 }
10970
10971 /* If the value has all its registers valid, return it. */
10972 if (get_last_value_validate (&value, reg_last_set[regno],
10973 reg_last_set_label[regno], 0))
10974 return value;
10975
10976 /* Otherwise, make a copy and replace any invalid register with
10977 (clobber (const_int 0)). If that fails for some reason, return 0. */
10978
10979 value = copy_rtx (value);
10980 if (get_last_value_validate (&value, reg_last_set[regno],
10981 reg_last_set_label[regno], 1))
10982 return value;
10983
10984 return 0;
10985 }
10986 \f
10987 /* Return nonzero if expression X refers to a REG or to memory
10988 that is set in an instruction more recent than FROM_CUID. */
10989
10990 static int
10991 use_crosses_set_p (x, from_cuid)
10992 register rtx x;
10993 int from_cuid;
10994 {
10995 register const char *fmt;
10996 register int i;
10997 register enum rtx_code code = GET_CODE (x);
10998
10999 if (code == REG)
11000 {
11001 register int regno = REGNO (x);
11002 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11003 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11004
11005 #ifdef PUSH_ROUNDING
11006 /* Don't allow uses of the stack pointer to be moved,
11007 because we don't know whether the move crosses a push insn. */
11008 if (regno == STACK_POINTER_REGNUM)
11009 return 1;
11010 #endif
11011 for (;regno < endreg; regno++)
11012 if (reg_last_set[regno]
11013 && INSN_CUID (reg_last_set[regno]) > from_cuid)
11014 return 1;
11015 return 0;
11016 }
11017
11018 if (code == MEM && mem_last_set > from_cuid)
11019 return 1;
11020
11021 fmt = GET_RTX_FORMAT (code);
11022
11023 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11024 {
11025 if (fmt[i] == 'E')
11026 {
11027 register int j;
11028 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11029 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11030 return 1;
11031 }
11032 else if (fmt[i] == 'e'
11033 && use_crosses_set_p (XEXP (x, i), from_cuid))
11034 return 1;
11035 }
11036 return 0;
11037 }
11038 \f
11039 /* Define three variables used for communication between the following
11040 routines. */
11041
11042 static int reg_dead_regno, reg_dead_endregno;
11043 static int reg_dead_flag;
11044
11045 /* Function called via note_stores from reg_dead_at_p.
11046
11047 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11048 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11049
11050 static void
11051 reg_dead_at_p_1 (dest, x)
11052 rtx dest;
11053 rtx x;
11054 {
11055 int regno, endregno;
11056
11057 if (GET_CODE (dest) != REG)
11058 return;
11059
11060 regno = REGNO (dest);
11061 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11062 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11063
11064 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11065 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11066 }
11067
11068 /* Return non-zero if REG is known to be dead at INSN.
11069
11070 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11071 referencing REG, it is dead. If we hit a SET referencing REG, it is
11072 live. Otherwise, see if it is live or dead at the start of the basic
11073 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11074 must be assumed to be always live. */
11075
11076 static int
11077 reg_dead_at_p (reg, insn)
11078 rtx reg;
11079 rtx insn;
11080 {
11081 int block, i;
11082
11083 /* Set variables for reg_dead_at_p_1. */
11084 reg_dead_regno = REGNO (reg);
11085 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11086 ? HARD_REGNO_NREGS (reg_dead_regno,
11087 GET_MODE (reg))
11088 : 1);
11089
11090 reg_dead_flag = 0;
11091
11092 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11093 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11094 {
11095 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11096 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11097 return 0;
11098 }
11099
11100 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11101 beginning of function. */
11102 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11103 insn = prev_nonnote_insn (insn))
11104 {
11105 note_stores (PATTERN (insn), reg_dead_at_p_1);
11106 if (reg_dead_flag)
11107 return reg_dead_flag == 1 ? 1 : 0;
11108
11109 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11110 return 1;
11111 }
11112
11113 /* Get the basic block number that we were in. */
11114 if (insn == 0)
11115 block = 0;
11116 else
11117 {
11118 for (block = 0; block < n_basic_blocks; block++)
11119 if (insn == BLOCK_HEAD (block))
11120 break;
11121
11122 if (block == n_basic_blocks)
11123 return 0;
11124 }
11125
11126 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11127 if (REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start, i))
11128 return 0;
11129
11130 return 1;
11131 }
11132 \f
11133 /* Note hard registers in X that are used. This code is similar to
11134 that in flow.c, but much simpler since we don't care about pseudos. */
11135
11136 static void
11137 mark_used_regs_combine (x)
11138 rtx x;
11139 {
11140 register RTX_CODE code = GET_CODE (x);
11141 register int regno;
11142 int i;
11143
11144 switch (code)
11145 {
11146 case LABEL_REF:
11147 case SYMBOL_REF:
11148 case CONST_INT:
11149 case CONST:
11150 case CONST_DOUBLE:
11151 case PC:
11152 case ADDR_VEC:
11153 case ADDR_DIFF_VEC:
11154 case ASM_INPUT:
11155 #ifdef HAVE_cc0
11156 /* CC0 must die in the insn after it is set, so we don't need to take
11157 special note of it here. */
11158 case CC0:
11159 #endif
11160 return;
11161
11162 case CLOBBER:
11163 /* If we are clobbering a MEM, mark any hard registers inside the
11164 address as used. */
11165 if (GET_CODE (XEXP (x, 0)) == MEM)
11166 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11167 return;
11168
11169 case REG:
11170 regno = REGNO (x);
11171 /* A hard reg in a wide mode may really be multiple registers.
11172 If so, mark all of them just like the first. */
11173 if (regno < FIRST_PSEUDO_REGISTER)
11174 {
11175 /* None of this applies to the stack, frame or arg pointers */
11176 if (regno == STACK_POINTER_REGNUM
11177 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11178 || regno == HARD_FRAME_POINTER_REGNUM
11179 #endif
11180 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11181 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11182 #endif
11183 || regno == FRAME_POINTER_REGNUM)
11184 return;
11185
11186 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
11187 while (i-- > 0)
11188 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11189 }
11190 return;
11191
11192 case SET:
11193 {
11194 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11195 the address. */
11196 register rtx testreg = SET_DEST (x);
11197
11198 while (GET_CODE (testreg) == SUBREG
11199 || GET_CODE (testreg) == ZERO_EXTRACT
11200 || GET_CODE (testreg) == SIGN_EXTRACT
11201 || GET_CODE (testreg) == STRICT_LOW_PART)
11202 testreg = XEXP (testreg, 0);
11203
11204 if (GET_CODE (testreg) == MEM)
11205 mark_used_regs_combine (XEXP (testreg, 0));
11206
11207 mark_used_regs_combine (SET_SRC (x));
11208 }
11209 return;
11210
11211 default:
11212 break;
11213 }
11214
11215 /* Recursively scan the operands of this expression. */
11216
11217 {
11218 register const char *fmt = GET_RTX_FORMAT (code);
11219
11220 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11221 {
11222 if (fmt[i] == 'e')
11223 mark_used_regs_combine (XEXP (x, i));
11224 else if (fmt[i] == 'E')
11225 {
11226 register int j;
11227
11228 for (j = 0; j < XVECLEN (x, i); j++)
11229 mark_used_regs_combine (XVECEXP (x, i, j));
11230 }
11231 }
11232 }
11233 }
11234
11235 \f
11236 /* Remove register number REGNO from the dead registers list of INSN.
11237
11238 Return the note used to record the death, if there was one. */
11239
11240 rtx
11241 remove_death (regno, insn)
11242 int regno;
11243 rtx insn;
11244 {
11245 register rtx note = find_regno_note (insn, REG_DEAD, regno);
11246
11247 if (note)
11248 {
11249 REG_N_DEATHS (regno)--;
11250 remove_note (insn, note);
11251 }
11252
11253 return note;
11254 }
11255
11256 /* For each register (hardware or pseudo) used within expression X, if its
11257 death is in an instruction with cuid between FROM_CUID (inclusive) and
11258 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11259 list headed by PNOTES.
11260
11261 That said, don't move registers killed by maybe_kill_insn.
11262
11263 This is done when X is being merged by combination into TO_INSN. These
11264 notes will then be distributed as needed. */
11265
11266 static void
11267 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11268 rtx x;
11269 rtx maybe_kill_insn;
11270 int from_cuid;
11271 rtx to_insn;
11272 rtx *pnotes;
11273 {
11274 register const char *fmt;
11275 register int len, i;
11276 register enum rtx_code code = GET_CODE (x);
11277
11278 if (code == REG)
11279 {
11280 register int regno = REGNO (x);
11281 register rtx where_dead = reg_last_death[regno];
11282 register rtx before_dead, after_dead;
11283
11284 /* Don't move the register if it gets killed in between from and to */
11285 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11286 && !reg_referenced_p (x, maybe_kill_insn))
11287 return;
11288
11289 /* WHERE_DEAD could be a USE insn made by combine, so first we
11290 make sure that we have insns with valid INSN_CUID values. */
11291 before_dead = where_dead;
11292 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11293 before_dead = PREV_INSN (before_dead);
11294 after_dead = where_dead;
11295 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11296 after_dead = NEXT_INSN (after_dead);
11297
11298 if (before_dead && after_dead
11299 && INSN_CUID (before_dead) >= from_cuid
11300 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11301 || (where_dead != after_dead
11302 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11303 {
11304 rtx note = remove_death (regno, where_dead);
11305
11306 /* It is possible for the call above to return 0. This can occur
11307 when reg_last_death points to I2 or I1 that we combined with.
11308 In that case make a new note.
11309
11310 We must also check for the case where X is a hard register
11311 and NOTE is a death note for a range of hard registers
11312 including X. In that case, we must put REG_DEAD notes for
11313 the remaining registers in place of NOTE. */
11314
11315 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11316 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11317 > GET_MODE_SIZE (GET_MODE (x))))
11318 {
11319 int deadregno = REGNO (XEXP (note, 0));
11320 int deadend
11321 = (deadregno + HARD_REGNO_NREGS (deadregno,
11322 GET_MODE (XEXP (note, 0))));
11323 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11324 int i;
11325
11326 for (i = deadregno; i < deadend; i++)
11327 if (i < regno || i >= ourend)
11328 REG_NOTES (where_dead)
11329 = gen_rtx_EXPR_LIST (REG_DEAD,
11330 gen_rtx_REG (reg_raw_mode[i], i),
11331 REG_NOTES (where_dead));
11332 }
11333 /* If we didn't find any note, or if we found a REG_DEAD note that
11334 covers only part of the given reg, and we have a multi-reg hard
11335 register, then to be safe we must check for REG_DEAD notes
11336 for each register other than the first. They could have
11337 their own REG_DEAD notes lying around. */
11338 else if ((note == 0
11339 || (note != 0
11340 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11341 < GET_MODE_SIZE (GET_MODE (x)))))
11342 && regno < FIRST_PSEUDO_REGISTER
11343 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11344 {
11345 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11346 int i, offset;
11347 rtx oldnotes = 0;
11348
11349 if (note)
11350 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11351 else
11352 offset = 1;
11353
11354 for (i = regno + offset; i < ourend; i++)
11355 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11356 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11357 }
11358
11359 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11360 {
11361 XEXP (note, 1) = *pnotes;
11362 *pnotes = note;
11363 }
11364 else
11365 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11366
11367 REG_N_DEATHS (regno)++;
11368 }
11369
11370 return;
11371 }
11372
11373 else if (GET_CODE (x) == SET)
11374 {
11375 rtx dest = SET_DEST (x);
11376
11377 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11378
11379 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11380 that accesses one word of a multi-word item, some
11381 piece of everything register in the expression is used by
11382 this insn, so remove any old death. */
11383
11384 if (GET_CODE (dest) == ZERO_EXTRACT
11385 || GET_CODE (dest) == STRICT_LOW_PART
11386 || (GET_CODE (dest) == SUBREG
11387 && (((GET_MODE_SIZE (GET_MODE (dest))
11388 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11389 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11390 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11391 {
11392 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11393 return;
11394 }
11395
11396 /* If this is some other SUBREG, we know it replaces the entire
11397 value, so use that as the destination. */
11398 if (GET_CODE (dest) == SUBREG)
11399 dest = SUBREG_REG (dest);
11400
11401 /* If this is a MEM, adjust deaths of anything used in the address.
11402 For a REG (the only other possibility), the entire value is
11403 being replaced so the old value is not used in this insn. */
11404
11405 if (GET_CODE (dest) == MEM)
11406 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11407 to_insn, pnotes);
11408 return;
11409 }
11410
11411 else if (GET_CODE (x) == CLOBBER)
11412 return;
11413
11414 len = GET_RTX_LENGTH (code);
11415 fmt = GET_RTX_FORMAT (code);
11416
11417 for (i = 0; i < len; i++)
11418 {
11419 if (fmt[i] == 'E')
11420 {
11421 register int j;
11422 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11423 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11424 to_insn, pnotes);
11425 }
11426 else if (fmt[i] == 'e')
11427 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11428 }
11429 }
11430 \f
11431 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11432 pattern of an insn. X must be a REG. */
11433
11434 static int
11435 reg_bitfield_target_p (x, body)
11436 rtx x;
11437 rtx body;
11438 {
11439 int i;
11440
11441 if (GET_CODE (body) == SET)
11442 {
11443 rtx dest = SET_DEST (body);
11444 rtx target;
11445 int regno, tregno, endregno, endtregno;
11446
11447 if (GET_CODE (dest) == ZERO_EXTRACT)
11448 target = XEXP (dest, 0);
11449 else if (GET_CODE (dest) == STRICT_LOW_PART)
11450 target = SUBREG_REG (XEXP (dest, 0));
11451 else
11452 return 0;
11453
11454 if (GET_CODE (target) == SUBREG)
11455 target = SUBREG_REG (target);
11456
11457 if (GET_CODE (target) != REG)
11458 return 0;
11459
11460 tregno = REGNO (target), regno = REGNO (x);
11461 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11462 return target == x;
11463
11464 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11465 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11466
11467 return endregno > tregno && regno < endtregno;
11468 }
11469
11470 else if (GET_CODE (body) == PARALLEL)
11471 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11472 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11473 return 1;
11474
11475 return 0;
11476 }
11477 \f
11478 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11479 as appropriate. I3 and I2 are the insns resulting from the combination
11480 insns including FROM (I2 may be zero).
11481
11482 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11483 not need REG_DEAD notes because they are being substituted for. This
11484 saves searching in the most common cases.
11485
11486 Each note in the list is either ignored or placed on some insns, depending
11487 on the type of note. */
11488
11489 static void
11490 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11491 rtx notes;
11492 rtx from_insn;
11493 rtx i3, i2;
11494 rtx elim_i2, elim_i1;
11495 {
11496 rtx note, next_note;
11497 rtx tem;
11498
11499 for (note = notes; note; note = next_note)
11500 {
11501 rtx place = 0, place2 = 0;
11502
11503 /* If this NOTE references a pseudo register, ensure it references
11504 the latest copy of that register. */
11505 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11506 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11507 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11508
11509 next_note = XEXP (note, 1);
11510 switch (REG_NOTE_KIND (note))
11511 {
11512 case REG_BR_PROB:
11513 case REG_EXEC_COUNT:
11514 /* Doesn't matter much where we put this, as long as it's somewhere.
11515 It is preferable to keep these notes on branches, which is most
11516 likely to be i3. */
11517 place = i3;
11518 break;
11519
11520 case REG_EH_REGION:
11521 case REG_EH_RETHROW:
11522 /* These notes must remain with the call. It should not be
11523 possible for both I2 and I3 to be a call. */
11524 if (GET_CODE (i3) == CALL_INSN)
11525 place = i3;
11526 else if (i2 && GET_CODE (i2) == CALL_INSN)
11527 place = i2;
11528 else
11529 abort ();
11530 break;
11531
11532 case REG_UNUSED:
11533 /* Any clobbers for i3 may still exist, and so we must process
11534 REG_UNUSED notes from that insn.
11535
11536 Any clobbers from i2 or i1 can only exist if they were added by
11537 recog_for_combine. In that case, recog_for_combine created the
11538 necessary REG_UNUSED notes. Trying to keep any original
11539 REG_UNUSED notes from these insns can cause incorrect output
11540 if it is for the same register as the original i3 dest.
11541 In that case, we will notice that the register is set in i3,
11542 and then add a REG_UNUSED note for the destination of i3, which
11543 is wrong. However, it is possible to have REG_UNUSED notes from
11544 i2 or i1 for register which were both used and clobbered, so
11545 we keep notes from i2 or i1 if they will turn into REG_DEAD
11546 notes. */
11547
11548 /* If this register is set or clobbered in I3, put the note there
11549 unless there is one already. */
11550 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11551 {
11552 if (from_insn != i3)
11553 break;
11554
11555 if (! (GET_CODE (XEXP (note, 0)) == REG
11556 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11557 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11558 place = i3;
11559 }
11560 /* Otherwise, if this register is used by I3, then this register
11561 now dies here, so we must put a REG_DEAD note here unless there
11562 is one already. */
11563 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11564 && ! (GET_CODE (XEXP (note, 0)) == REG
11565 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11566 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11567 {
11568 PUT_REG_NOTE_KIND (note, REG_DEAD);
11569 place = i3;
11570 }
11571 break;
11572
11573 case REG_EQUAL:
11574 case REG_EQUIV:
11575 case REG_NONNEG:
11576 case REG_NOALIAS:
11577 /* These notes say something about results of an insn. We can
11578 only support them if they used to be on I3 in which case they
11579 remain on I3. Otherwise they are ignored.
11580
11581 If the note refers to an expression that is not a constant, we
11582 must also ignore the note since we cannot tell whether the
11583 equivalence is still true. It might be possible to do
11584 slightly better than this (we only have a problem if I2DEST
11585 or I1DEST is present in the expression), but it doesn't
11586 seem worth the trouble. */
11587
11588 if (from_insn == i3
11589 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11590 place = i3;
11591 break;
11592
11593 case REG_INC:
11594 case REG_NO_CONFLICT:
11595 /* These notes say something about how a register is used. They must
11596 be present on any use of the register in I2 or I3. */
11597 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11598 place = i3;
11599
11600 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11601 {
11602 if (place)
11603 place2 = i2;
11604 else
11605 place = i2;
11606 }
11607 break;
11608
11609 case REG_LABEL:
11610 /* This can show up in several ways -- either directly in the
11611 pattern, or hidden off in the constant pool with (or without?)
11612 a REG_EQUAL note. */
11613 /* ??? Ignore the without-reg_equal-note problem for now. */
11614 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11615 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11616 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11617 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11618 place = i3;
11619
11620 if (i2
11621 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11622 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11623 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11624 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11625 {
11626 if (place)
11627 place2 = i2;
11628 else
11629 place = i2;
11630 }
11631 break;
11632
11633 case REG_WAS_0:
11634 /* It is too much trouble to try to see if this note is still
11635 correct in all situations. It is better to simply delete it. */
11636 break;
11637
11638 case REG_RETVAL:
11639 /* If the insn previously containing this note still exists,
11640 put it back where it was. Otherwise move it to the previous
11641 insn. Adjust the corresponding REG_LIBCALL note. */
11642 if (GET_CODE (from_insn) != NOTE)
11643 place = from_insn;
11644 else
11645 {
11646 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11647 place = prev_real_insn (from_insn);
11648 if (tem && place)
11649 XEXP (tem, 0) = place;
11650 }
11651 break;
11652
11653 case REG_LIBCALL:
11654 /* This is handled similarly to REG_RETVAL. */
11655 if (GET_CODE (from_insn) != NOTE)
11656 place = from_insn;
11657 else
11658 {
11659 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11660 place = next_real_insn (from_insn);
11661 if (tem && place)
11662 XEXP (tem, 0) = place;
11663 }
11664 break;
11665
11666 case REG_DEAD:
11667 /* If the register is used as an input in I3, it dies there.
11668 Similarly for I2, if it is non-zero and adjacent to I3.
11669
11670 If the register is not used as an input in either I3 or I2
11671 and it is not one of the registers we were supposed to eliminate,
11672 there are two possibilities. We might have a non-adjacent I2
11673 or we might have somehow eliminated an additional register
11674 from a computation. For example, we might have had A & B where
11675 we discover that B will always be zero. In this case we will
11676 eliminate the reference to A.
11677
11678 In both cases, we must search to see if we can find a previous
11679 use of A and put the death note there. */
11680
11681 if (from_insn
11682 && GET_CODE (from_insn) == CALL_INSN
11683 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11684 place = from_insn;
11685 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11686 place = i3;
11687 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11688 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11689 place = i2;
11690
11691 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11692 break;
11693
11694 /* If the register is used in both I2 and I3 and it dies in I3,
11695 we might have added another reference to it. If reg_n_refs
11696 was 2, bump it to 3. This has to be correct since the
11697 register must have been set somewhere. The reason this is
11698 done is because local-alloc.c treats 2 references as a
11699 special case. */
11700
11701 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11702 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11703 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11704 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11705
11706 if (place == 0)
11707 {
11708 for (tem = prev_nonnote_insn (i3);
11709 place == 0 && tem
11710 && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11711 tem = prev_nonnote_insn (tem))
11712 {
11713 /* If the register is being set at TEM, see if that is all
11714 TEM is doing. If so, delete TEM. Otherwise, make this
11715 into a REG_UNUSED note instead. */
11716 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11717 {
11718 rtx set = single_set (tem);
11719 rtx inner_dest = 0;
11720 #ifdef HAVE_cc0
11721 rtx cc0_setter = NULL_RTX;
11722 #endif
11723
11724 if (set != 0)
11725 for (inner_dest = SET_DEST (set);
11726 GET_CODE (inner_dest) == STRICT_LOW_PART
11727 || GET_CODE (inner_dest) == SUBREG
11728 || GET_CODE (inner_dest) == ZERO_EXTRACT;
11729 inner_dest = XEXP (inner_dest, 0))
11730 ;
11731
11732 /* Verify that it was the set, and not a clobber that
11733 modified the register.
11734
11735 CC0 targets must be careful to maintain setter/user
11736 pairs. If we cannot delete the setter due to side
11737 effects, mark the user with an UNUSED note instead
11738 of deleting it. */
11739
11740 if (set != 0 && ! side_effects_p (SET_SRC (set))
11741 && rtx_equal_p (XEXP (note, 0), inner_dest)
11742 #ifdef HAVE_cc0
11743 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11744 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11745 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11746 #endif
11747 )
11748 {
11749 /* Move the notes and links of TEM elsewhere.
11750 This might delete other dead insns recursively.
11751 First set the pattern to something that won't use
11752 any register. */
11753
11754 PATTERN (tem) = pc_rtx;
11755
11756 distribute_notes (REG_NOTES (tem), tem, tem,
11757 NULL_RTX, NULL_RTX, NULL_RTX);
11758 distribute_links (LOG_LINKS (tem));
11759
11760 PUT_CODE (tem, NOTE);
11761 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11762 NOTE_SOURCE_FILE (tem) = 0;
11763
11764 #ifdef HAVE_cc0
11765 /* Delete the setter too. */
11766 if (cc0_setter)
11767 {
11768 PATTERN (cc0_setter) = pc_rtx;
11769
11770 distribute_notes (REG_NOTES (cc0_setter),
11771 cc0_setter, cc0_setter,
11772 NULL_RTX, NULL_RTX, NULL_RTX);
11773 distribute_links (LOG_LINKS (cc0_setter));
11774
11775 PUT_CODE (cc0_setter, NOTE);
11776 NOTE_LINE_NUMBER (cc0_setter) = NOTE_INSN_DELETED;
11777 NOTE_SOURCE_FILE (cc0_setter) = 0;
11778 }
11779 #endif
11780 }
11781 /* If the register is both set and used here, put the
11782 REG_DEAD note here, but place a REG_UNUSED note
11783 here too unless there already is one. */
11784 else if (reg_referenced_p (XEXP (note, 0),
11785 PATTERN (tem)))
11786 {
11787 place = tem;
11788
11789 if (! find_regno_note (tem, REG_UNUSED,
11790 REGNO (XEXP (note, 0))))
11791 REG_NOTES (tem)
11792 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
11793 REG_NOTES (tem));
11794 }
11795 else
11796 {
11797 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11798
11799 /* If there isn't already a REG_UNUSED note, put one
11800 here. */
11801 if (! find_regno_note (tem, REG_UNUSED,
11802 REGNO (XEXP (note, 0))))
11803 place = tem;
11804 break;
11805 }
11806 }
11807 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11808 || (GET_CODE (tem) == CALL_INSN
11809 && find_reg_fusage (tem, USE, XEXP (note, 0))))
11810 {
11811 place = tem;
11812
11813 /* If we are doing a 3->2 combination, and we have a
11814 register which formerly died in i3 and was not used
11815 by i2, which now no longer dies in i3 and is used in
11816 i2 but does not die in i2, and place is between i2
11817 and i3, then we may need to move a link from place to
11818 i2. */
11819 if (i2 && INSN_UID (place) <= max_uid_cuid
11820 && INSN_CUID (place) > INSN_CUID (i2)
11821 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11822 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11823 {
11824 rtx links = LOG_LINKS (place);
11825 LOG_LINKS (place) = 0;
11826 distribute_links (links);
11827 }
11828 break;
11829 }
11830 }
11831
11832 /* If we haven't found an insn for the death note and it
11833 is still a REG_DEAD note, but we have hit a CODE_LABEL,
11834 insert a USE insn for the register at that label and
11835 put the death node there. This prevents problems with
11836 call-state tracking in caller-save.c. */
11837 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
11838 {
11839 place
11840 = emit_insn_after (gen_rtx_USE (VOIDmode, XEXP (note, 0)),
11841 tem);
11842
11843 /* If this insn was emitted between blocks, then update
11844 BLOCK_HEAD of the current block to include it. */
11845 if (BLOCK_END (this_basic_block - 1) == tem)
11846 BLOCK_HEAD (this_basic_block) = place;
11847 }
11848 }
11849
11850 /* If the register is set or already dead at PLACE, we needn't do
11851 anything with this note if it is still a REG_DEAD note.
11852 We can here if it is set at all, not if is it totally replace,
11853 which is what `dead_or_set_p' checks, so also check for it being
11854 set partially. */
11855
11856
11857 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11858 {
11859 int regno = REGNO (XEXP (note, 0));
11860
11861 if (dead_or_set_p (place, XEXP (note, 0))
11862 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11863 {
11864 /* Unless the register previously died in PLACE, clear
11865 reg_last_death. [I no longer understand why this is
11866 being done.] */
11867 if (reg_last_death[regno] != place)
11868 reg_last_death[regno] = 0;
11869 place = 0;
11870 }
11871 else
11872 reg_last_death[regno] = place;
11873
11874 /* If this is a death note for a hard reg that is occupying
11875 multiple registers, ensure that we are still using all
11876 parts of the object. If we find a piece of the object
11877 that is unused, we must add a USE for that piece before
11878 PLACE and put the appropriate REG_DEAD note on it.
11879
11880 An alternative would be to put a REG_UNUSED for the pieces
11881 on the insn that set the register, but that can't be done if
11882 it is not in the same block. It is simpler, though less
11883 efficient, to add the USE insns. */
11884
11885 if (place && regno < FIRST_PSEUDO_REGISTER
11886 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11887 {
11888 int endregno
11889 = regno + HARD_REGNO_NREGS (regno,
11890 GET_MODE (XEXP (note, 0)));
11891 int all_used = 1;
11892 int i;
11893
11894 for (i = regno; i < endregno; i++)
11895 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11896 && ! find_regno_fusage (place, USE, i))
11897 {
11898 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11899 rtx p;
11900
11901 /* See if we already placed a USE note for this
11902 register in front of PLACE. */
11903 for (p = place;
11904 GET_CODE (PREV_INSN (p)) == INSN
11905 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11906 p = PREV_INSN (p))
11907 if (rtx_equal_p (piece,
11908 XEXP (PATTERN (PREV_INSN (p)), 0)))
11909 {
11910 p = 0;
11911 break;
11912 }
11913
11914 if (p)
11915 {
11916 rtx use_insn
11917 = emit_insn_before (gen_rtx_USE (VOIDmode,
11918 piece),
11919 p);
11920 REG_NOTES (use_insn)
11921 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11922 REG_NOTES (use_insn));
11923 }
11924
11925 all_used = 0;
11926 }
11927
11928 /* Check for the case where the register dying partially
11929 overlaps the register set by this insn. */
11930 if (all_used)
11931 for (i = regno; i < endregno; i++)
11932 if (dead_or_set_regno_p (place, i))
11933 {
11934 all_used = 0;
11935 break;
11936 }
11937
11938 if (! all_used)
11939 {
11940 /* Put only REG_DEAD notes for pieces that are
11941 still used and that are not already dead or set. */
11942
11943 for (i = regno; i < endregno; i++)
11944 {
11945 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11946
11947 if ((reg_referenced_p (piece, PATTERN (place))
11948 || (GET_CODE (place) == CALL_INSN
11949 && find_reg_fusage (place, USE, piece)))
11950 && ! dead_or_set_p (place, piece)
11951 && ! reg_bitfield_target_p (piece,
11952 PATTERN (place)))
11953 REG_NOTES (place)
11954 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11955 REG_NOTES (place));
11956 }
11957
11958 place = 0;
11959 }
11960 }
11961 }
11962 break;
11963
11964 default:
11965 /* Any other notes should not be present at this point in the
11966 compilation. */
11967 abort ();
11968 }
11969
11970 if (place)
11971 {
11972 XEXP (note, 1) = REG_NOTES (place);
11973 REG_NOTES (place) = note;
11974 }
11975 else if ((REG_NOTE_KIND (note) == REG_DEAD
11976 || REG_NOTE_KIND (note) == REG_UNUSED)
11977 && GET_CODE (XEXP (note, 0)) == REG)
11978 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
11979
11980 if (place2)
11981 {
11982 if ((REG_NOTE_KIND (note) == REG_DEAD
11983 || REG_NOTE_KIND (note) == REG_UNUSED)
11984 && GET_CODE (XEXP (note, 0)) == REG)
11985 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
11986
11987 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
11988 REG_NOTE_KIND (note),
11989 XEXP (note, 0),
11990 REG_NOTES (place2));
11991 }
11992 }
11993 }
11994 \f
11995 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11996 I3, I2, and I1 to new locations. This is also called in one case to
11997 add a link pointing at I3 when I3's destination is changed. */
11998
11999 static void
12000 distribute_links (links)
12001 rtx links;
12002 {
12003 rtx link, next_link;
12004
12005 for (link = links; link; link = next_link)
12006 {
12007 rtx place = 0;
12008 rtx insn;
12009 rtx set, reg;
12010
12011 next_link = XEXP (link, 1);
12012
12013 /* If the insn that this link points to is a NOTE or isn't a single
12014 set, ignore it. In the latter case, it isn't clear what we
12015 can do other than ignore the link, since we can't tell which
12016 register it was for. Such links wouldn't be used by combine
12017 anyway.
12018
12019 It is not possible for the destination of the target of the link to
12020 have been changed by combine. The only potential of this is if we
12021 replace I3, I2, and I1 by I3 and I2. But in that case the
12022 destination of I2 also remains unchanged. */
12023
12024 if (GET_CODE (XEXP (link, 0)) == NOTE
12025 || (set = single_set (XEXP (link, 0))) == 0)
12026 continue;
12027
12028 reg = SET_DEST (set);
12029 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12030 || GET_CODE (reg) == SIGN_EXTRACT
12031 || GET_CODE (reg) == STRICT_LOW_PART)
12032 reg = XEXP (reg, 0);
12033
12034 /* A LOG_LINK is defined as being placed on the first insn that uses
12035 a register and points to the insn that sets the register. Start
12036 searching at the next insn after the target of the link and stop
12037 when we reach a set of the register or the end of the basic block.
12038
12039 Note that this correctly handles the link that used to point from
12040 I3 to I2. Also note that not much searching is typically done here
12041 since most links don't point very far away. */
12042
12043 for (insn = NEXT_INSN (XEXP (link, 0));
12044 (insn && (this_basic_block == n_basic_blocks - 1
12045 || BLOCK_HEAD (this_basic_block + 1) != insn));
12046 insn = NEXT_INSN (insn))
12047 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
12048 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12049 {
12050 if (reg_referenced_p (reg, PATTERN (insn)))
12051 place = insn;
12052 break;
12053 }
12054 else if (GET_CODE (insn) == CALL_INSN
12055 && find_reg_fusage (insn, USE, reg))
12056 {
12057 place = insn;
12058 break;
12059 }
12060
12061 /* If we found a place to put the link, place it there unless there
12062 is already a link to the same insn as LINK at that point. */
12063
12064 if (place)
12065 {
12066 rtx link2;
12067
12068 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12069 if (XEXP (link2, 0) == XEXP (link, 0))
12070 break;
12071
12072 if (link2 == 0)
12073 {
12074 XEXP (link, 1) = LOG_LINKS (place);
12075 LOG_LINKS (place) = link;
12076
12077 /* Set added_links_insn to the earliest insn we added a
12078 link to. */
12079 if (added_links_insn == 0
12080 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12081 added_links_insn = place;
12082 }
12083 }
12084 }
12085 }
12086 \f
12087 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12088
12089 static int
12090 insn_cuid (insn)
12091 rtx insn;
12092 {
12093 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12094 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12095 insn = NEXT_INSN (insn);
12096
12097 if (INSN_UID (insn) > max_uid_cuid)
12098 abort ();
12099
12100 return INSN_CUID (insn);
12101 }
12102 \f
12103 void
12104 dump_combine_stats (file)
12105 FILE *file;
12106 {
12107 fnotice
12108 (file,
12109 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12110 combine_attempts, combine_merges, combine_extras, combine_successes);
12111 }
12112
12113 void
12114 dump_combine_total_stats (file)
12115 FILE *file;
12116 {
12117 fnotice
12118 (file,
12119 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12120 total_attempts, total_merges, total_extras, total_successes);
12121 }