]> gcc.gnu.org Git - gcc.git/blame - gcc/combine.c
Fixed typo.
[gcc.git] / gcc / combine.c
CommitLineData
230d793d 1/* Optimize by combining instructions for GNU compiler.
1a6ec070 2 Copyright (C) 1987, 88, 92-96, 1997 Free Software Foundation, Inc.
230d793d
RS
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
940d9d63
RK
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
230d793d
RS
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
230d793d 77#include "config.h"
4f90e4a0 78#ifdef __STDC__
04fe4385 79#include <stdarg.h>
4f90e4a0 80#else
04fe4385 81#include <varargs.h>
4f90e4a0 82#endif
dfa3449b 83
9c3b4c8b
RS
84/* Must precede rtl.h for FFS. */
85#include <stdio.h>
86
230d793d
RS
87#include "rtl.h"
88#include "flags.h"
89#include "regs.h"
55310dad 90#include "hard-reg-set.h"
230d793d
RS
91#include "expr.h"
92#include "basic-block.h"
93#include "insn-config.h"
94#include "insn-flags.h"
95#include "insn-codes.h"
96#include "insn-attr.h"
97#include "recog.h"
98#include "real.h"
99
100/* It is not safe to use ordinary gen_lowpart in combine.
101 Use gen_lowpart_for_combine instead. See comments there. */
102#define gen_lowpart dont_use_gen_lowpart_you_dummy
103
104/* Number of attempts to combine instructions in this function. */
105
106static int combine_attempts;
107
108/* Number of attempts that got as far as substitution in this function. */
109
110static int combine_merges;
111
112/* Number of instructions combined with added SETs in this function. */
113
114static int combine_extras;
115
116/* Number of instructions combined in this function. */
117
118static int combine_successes;
119
120/* Totals over entire compilation. */
121
122static int total_attempts, total_merges, total_extras, total_successes;
9210df58 123
ddd5a7c1 124/* Define a default value for REVERSIBLE_CC_MODE.
9210df58
RK
125 We can never assume that a condition code mode is safe to reverse unless
126 the md tells us so. */
127#ifndef REVERSIBLE_CC_MODE
128#define REVERSIBLE_CC_MODE(MODE) 0
129#endif
230d793d
RS
130\f
131/* Vector mapping INSN_UIDs to cuids.
5089e22e 132 The cuids are like uids but increase monotonically always.
230d793d
RS
133 Combine always uses cuids so that it can compare them.
134 But actually renumbering the uids, which we used to do,
135 proves to be a bad idea because it makes it hard to compare
136 the dumps produced by earlier passes with those from later passes. */
137
138static int *uid_cuid;
4255220d 139static int max_uid_cuid;
230d793d
RS
140
141/* Get the cuid of an insn. */
142
1427d6d2
RK
143#define INSN_CUID(INSN) \
144(INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
230d793d
RS
145
146/* Maximum register number, which is the size of the tables below. */
147
148static int combine_max_regno;
149
150/* Record last point of death of (hard or pseudo) register n. */
151
152static rtx *reg_last_death;
153
154/* Record last point of modification of (hard or pseudo) register n. */
155
156static rtx *reg_last_set;
157
158/* Record the cuid of the last insn that invalidated memory
159 (anything that writes memory, and subroutine calls, but not pushes). */
160
161static int mem_last_set;
162
163/* Record the cuid of the last CALL_INSN
164 so we can tell whether a potential combination crosses any calls. */
165
166static int last_call_cuid;
167
168/* When `subst' is called, this is the insn that is being modified
169 (by combining in a previous insn). The PATTERN of this insn
170 is still the old pattern partially modified and it should not be
171 looked at, but this may be used to examine the successors of the insn
172 to judge whether a simplification is valid. */
173
174static rtx subst_insn;
175
0d9641d1
JW
176/* This is an insn that belongs before subst_insn, but is not currently
177 on the insn chain. */
178
179static rtx subst_prev_insn;
180
230d793d
RS
181/* This is the lowest CUID that `subst' is currently dealing with.
182 get_last_value will not return a value if the register was set at or
183 after this CUID. If not for this mechanism, we could get confused if
184 I2 or I1 in try_combine were an insn that used the old value of a register
185 to obtain a new value. In that case, we might erroneously get the
186 new value of the register when we wanted the old one. */
187
188static int subst_low_cuid;
189
6e25d159
RK
190/* This contains any hard registers that are used in newpat; reg_dead_at_p
191 must consider all these registers to be always live. */
192
193static HARD_REG_SET newpat_used_regs;
194
abe6e52f
RK
195/* This is an insn to which a LOG_LINKS entry has been added. If this
196 insn is the earlier than I2 or I3, combine should rescan starting at
197 that location. */
198
199static rtx added_links_insn;
200
0d4d42c3
RK
201/* Basic block number of the block in which we are performing combines. */
202static int this_basic_block;
230d793d
RS
203\f
204/* The next group of arrays allows the recording of the last value assigned
205 to (hard or pseudo) register n. We use this information to see if a
5089e22e 206 operation being processed is redundant given a prior operation performed
230d793d
RS
207 on the register. For example, an `and' with a constant is redundant if
208 all the zero bits are already known to be turned off.
209
210 We use an approach similar to that used by cse, but change it in the
211 following ways:
212
213 (1) We do not want to reinitialize at each label.
214 (2) It is useful, but not critical, to know the actual value assigned
215 to a register. Often just its form is helpful.
216
217 Therefore, we maintain the following arrays:
218
219 reg_last_set_value the last value assigned
220 reg_last_set_label records the value of label_tick when the
221 register was assigned
222 reg_last_set_table_tick records the value of label_tick when a
223 value using the register is assigned
224 reg_last_set_invalid set to non-zero when it is not valid
225 to use the value of this register in some
226 register's value
227
228 To understand the usage of these tables, it is important to understand
229 the distinction between the value in reg_last_set_value being valid
230 and the register being validly contained in some other expression in the
231 table.
232
233 Entry I in reg_last_set_value is valid if it is non-zero, and either
234 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
235
236 Register I may validly appear in any expression returned for the value
237 of another register if reg_n_sets[i] is 1. It may also appear in the
238 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
239 reg_last_set_invalid[j] is zero.
240
241 If an expression is found in the table containing a register which may
242 not validly appear in an expression, the register is replaced by
243 something that won't match, (clobber (const_int 0)).
244
245 reg_last_set_invalid[i] is set non-zero when register I is being assigned
246 to and reg_last_set_table_tick[i] == label_tick. */
247
0f41302f 248/* Record last value assigned to (hard or pseudo) register n. */
230d793d
RS
249
250static rtx *reg_last_set_value;
251
252/* Record the value of label_tick when the value for register n is placed in
253 reg_last_set_value[n]. */
254
568356af 255static int *reg_last_set_label;
230d793d
RS
256
257/* Record the value of label_tick when an expression involving register n
0f41302f 258 is placed in reg_last_set_value. */
230d793d 259
568356af 260static int *reg_last_set_table_tick;
230d793d
RS
261
262/* Set non-zero if references to register n in expressions should not be
263 used. */
264
265static char *reg_last_set_invalid;
266
0f41302f 267/* Incremented for each label. */
230d793d 268
568356af 269static int label_tick;
230d793d
RS
270
271/* Some registers that are set more than once and used in more than one
272 basic block are nevertheless always set in similar ways. For example,
273 a QImode register may be loaded from memory in two places on a machine
274 where byte loads zero extend.
275
951553af 276 We record in the following array what we know about the nonzero
230d793d
RS
277 bits of a register, specifically which bits are known to be zero.
278
279 If an entry is zero, it means that we don't know anything special. */
280
55310dad 281static unsigned HOST_WIDE_INT *reg_nonzero_bits;
230d793d 282
951553af 283/* Mode used to compute significance in reg_nonzero_bits. It is the largest
5f4f0e22 284 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
230d793d 285
951553af 286static enum machine_mode nonzero_bits_mode;
230d793d 287
d0ab8cd3
RK
288/* Nonzero if we know that a register has some leading bits that are always
289 equal to the sign bit. */
290
291static char *reg_sign_bit_copies;
292
951553af 293/* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
1a26b032
RK
294 It is zero while computing them and after combine has completed. This
295 former test prevents propagating values based on previously set values,
296 which can be incorrect if a variable is modified in a loop. */
230d793d 297
951553af 298static int nonzero_sign_valid;
55310dad
RK
299
300/* These arrays are maintained in parallel with reg_last_set_value
301 and are used to store the mode in which the register was last set,
302 the bits that were known to be zero when it was last set, and the
303 number of sign bits copies it was known to have when it was last set. */
304
305static enum machine_mode *reg_last_set_mode;
306static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
307static char *reg_last_set_sign_bit_copies;
230d793d
RS
308\f
309/* Record one modification to rtl structure
310 to be undone by storing old_contents into *where.
311 is_int is 1 if the contents are an int. */
312
313struct undo
314{
241cea85 315 struct undo *next;
230d793d 316 int is_int;
f5393ab9
RS
317 union {rtx r; int i;} old_contents;
318 union {rtx *r; int *i;} where;
230d793d
RS
319};
320
321/* Record a bunch of changes to be undone, up to MAX_UNDO of them.
322 num_undo says how many are currently recorded.
323
324 storage is nonzero if we must undo the allocation of new storage.
325 The value of storage is what to pass to obfree.
326
327 other_insn is nonzero if we have modified some other insn in the process
241cea85 328 of working on subst_insn. It must be verified too.
230d793d 329
241cea85
RK
330 previous_undos is the value of undobuf.undos when we started processing
331 this substitution. This will prevent gen_rtx_combine from re-used a piece
332 from the previous expression. Doing so can produce circular rtl
333 structures. */
230d793d
RS
334
335struct undobuf
336{
230d793d 337 char *storage;
241cea85
RK
338 struct undo *undos;
339 struct undo *frees;
340 struct undo *previous_undos;
230d793d
RS
341 rtx other_insn;
342};
343
344static struct undobuf undobuf;
345
cc876596 346/* Substitute NEWVAL, an rtx expression, into INTO, a place in some
230d793d 347 insn. The substitution can be undone by undo_all. If INTO is already
cc876596
RK
348 set to NEWVAL, do not record this change. Because computing NEWVAL might
349 also call SUBST, we have to compute it before we put anything into
350 the undo table. */
230d793d
RS
351
352#define SUBST(INTO, NEWVAL) \
241cea85
RK
353 do { rtx _new = (NEWVAL); \
354 struct undo *_buf; \
355 \
356 if (undobuf.frees) \
357 _buf = undobuf.frees, undobuf.frees = _buf->next; \
358 else \
359 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
360 \
361 _buf->is_int = 0; \
362 _buf->where.r = &INTO; \
363 _buf->old_contents.r = INTO; \
364 INTO = _new; \
365 if (_buf->old_contents.r == INTO) \
366 _buf->next = undobuf.frees, undobuf.frees = _buf; \
367 else \
368 _buf->next = undobuf.undos, undobuf.undos = _buf; \
230d793d
RS
369 } while (0)
370
241cea85
RK
371/* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
372 for the value of a HOST_WIDE_INT value (including CONST_INT) is
373 not safe. */
230d793d
RS
374
375#define SUBST_INT(INTO, NEWVAL) \
241cea85
RK
376 do { struct undo *_buf; \
377 \
378 if (undobuf.frees) \
379 _buf = undobuf.frees, undobuf.frees = _buf->next; \
380 else \
381 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
382 \
383 _buf->is_int = 1; \
384 _buf->where.i = (int *) &INTO; \
385 _buf->old_contents.i = INTO; \
386 INTO = NEWVAL; \
387 if (_buf->old_contents.i == INTO) \
388 _buf->next = undobuf.frees, undobuf.frees = _buf; \
389 else \
390 _buf->next = undobuf.undos, undobuf.undos = _buf; \
230d793d
RS
391 } while (0)
392
393/* Number of times the pseudo being substituted for
394 was found and replaced. */
395
396static int n_occurrences;
397
c5ad722c
RK
398static void init_reg_last_arrays PROTO((void));
399static void setup_incoming_promotions PROTO((void));
fe2db4fb
RK
400static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx));
401static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
402static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
403static rtx try_combine PROTO((rtx, rtx, rtx));
404static void undo_all PROTO((void));
405static rtx *find_split_point PROTO((rtx *, rtx));
406static rtx subst PROTO((rtx, rtx, rtx, int, int));
8079805d
RK
407static rtx simplify_rtx PROTO((rtx, enum machine_mode, int, int));
408static rtx simplify_if_then_else PROTO((rtx));
409static rtx simplify_set PROTO((rtx));
410static rtx simplify_logical PROTO((rtx, int));
fe2db4fb
RK
411static rtx expand_compound_operation PROTO((rtx));
412static rtx expand_field_assignment PROTO((rtx));
413static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
414 int, int, int));
71923da7 415static rtx extract_left_shift PROTO((rtx, int));
fe2db4fb
RK
416static rtx make_compound_operation PROTO((rtx, enum rtx_code));
417static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
6139ff20 418static rtx force_to_mode PROTO((rtx, enum machine_mode,
e3d616e3 419 unsigned HOST_WIDE_INT, rtx, int));
abe6e52f 420static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
fe2db4fb 421static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
e11fa86f 422static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
fe2db4fb
RK
423static rtx make_field_assignment PROTO((rtx));
424static rtx apply_distributive_law PROTO((rtx));
425static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
426 unsigned HOST_WIDE_INT));
427static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
428static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
429static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
430 enum rtx_code, HOST_WIDE_INT,
431 enum machine_mode, int *));
432static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
433 rtx, int));
a29ca9db 434static int recog_for_combine PROTO((rtx *, rtx, rtx *, int *));
fe2db4fb 435static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
d18225c4 436static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
4f90e4a0 437 ...));
fe2db4fb
RK
438static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
439 rtx, rtx));
0c1c8ea6
RK
440static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
441 enum machine_mode, rtx));
fe2db4fb
RK
442static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
443static int reversible_comparison_p PROTO((rtx));
444static void update_table_tick PROTO((rtx));
445static void record_value_for_reg PROTO((rtx, rtx, rtx));
446static void record_dead_and_set_regs_1 PROTO((rtx, rtx));
447static void record_dead_and_set_regs PROTO((rtx));
9a893315 448static int get_last_value_validate PROTO((rtx *, rtx, int, int));
fe2db4fb
RK
449static rtx get_last_value PROTO((rtx));
450static int use_crosses_set_p PROTO((rtx, int));
451static void reg_dead_at_p_1 PROTO((rtx, rtx));
452static int reg_dead_at_p PROTO((rtx, rtx));
6eb12cef 453static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
fe2db4fb
RK
454static int reg_bitfield_target_p PROTO((rtx, rtx));
455static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
456static void distribute_links PROTO((rtx));
6e25d159 457static void mark_used_regs_combine PROTO((rtx));
1427d6d2 458static int insn_cuid PROTO((rtx));
230d793d
RS
459\f
460/* Main entry point for combiner. F is the first insn of the function.
461 NREGS is the first unused pseudo-reg number. */
462
463void
464combine_instructions (f, nregs)
465 rtx f;
466 int nregs;
467{
468 register rtx insn, next, prev;
469 register int i;
470 register rtx links, nextlinks;
471
472 combine_attempts = 0;
473 combine_merges = 0;
474 combine_extras = 0;
475 combine_successes = 0;
241cea85 476 undobuf.undos = undobuf.previous_undos = 0;
230d793d
RS
477
478 combine_max_regno = nregs;
479
ef026f91
RS
480 reg_nonzero_bits
481 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
482 reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
483
4c9a05bc 484 bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
ef026f91
RS
485 bzero (reg_sign_bit_copies, nregs * sizeof (char));
486
230d793d
RS
487 reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
488 reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
489 reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
568356af
RK
490 reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
491 reg_last_set_label = (int *) alloca (nregs * sizeof (int));
5f4f0e22 492 reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
55310dad
RK
493 reg_last_set_mode
494 = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
495 reg_last_set_nonzero_bits
496 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
497 reg_last_set_sign_bit_copies
498 = (char *) alloca (nregs * sizeof (char));
499
ef026f91 500 init_reg_last_arrays ();
230d793d
RS
501
502 init_recog_no_volatile ();
503
504 /* Compute maximum uid value so uid_cuid can be allocated. */
505
506 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
507 if (INSN_UID (insn) > i)
508 i = INSN_UID (insn);
509
510 uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
4255220d 511 max_uid_cuid = i;
230d793d 512
951553af 513 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
230d793d 514
951553af 515 /* Don't use reg_nonzero_bits when computing it. This can cause problems
230d793d
RS
516 when, for example, we have j <<= 1 in a loop. */
517
951553af 518 nonzero_sign_valid = 0;
230d793d
RS
519
520 /* Compute the mapping from uids to cuids.
521 Cuids are numbers assigned to insns, like uids,
522 except that cuids increase monotonically through the code.
523
524 Scan all SETs and see if we can deduce anything about what
951553af 525 bits are known to be zero for some registers and how many copies
d79f08e0
RK
526 of the sign bit are known to exist for those registers.
527
528 Also set any known values so that we can use it while searching
529 for what bits are known to be set. */
530
531 label_tick = 1;
230d793d 532
bcd49eb7
JW
533 /* We need to initialize it here, because record_dead_and_set_regs may call
534 get_last_value. */
535 subst_prev_insn = NULL_RTX;
536
7988fd36
RK
537 setup_incoming_promotions ();
538
230d793d
RS
539 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
540 {
4255220d 541 uid_cuid[INSN_UID (insn)] = ++i;
d79f08e0
RK
542 subst_low_cuid = i;
543 subst_insn = insn;
544
230d793d 545 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
d79f08e0
RK
546 {
547 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
548 record_dead_and_set_regs (insn);
2dab894a
RK
549
550#ifdef AUTO_INC_DEC
551 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
552 if (REG_NOTE_KIND (links) == REG_INC)
553 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
554#endif
d79f08e0
RK
555 }
556
557 if (GET_CODE (insn) == CODE_LABEL)
558 label_tick++;
230d793d
RS
559 }
560
951553af 561 nonzero_sign_valid = 1;
230d793d
RS
562
563 /* Now scan all the insns in forward order. */
564
0d4d42c3 565 this_basic_block = -1;
230d793d
RS
566 label_tick = 1;
567 last_call_cuid = 0;
568 mem_last_set = 0;
ef026f91 569 init_reg_last_arrays ();
7988fd36
RK
570 setup_incoming_promotions ();
571
230d793d
RS
572 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
573 {
574 next = 0;
575
0d4d42c3 576 /* If INSN starts a new basic block, update our basic block number. */
f085c9cd 577 if (this_basic_block + 1 < n_basic_blocks
0d4d42c3
RK
578 && basic_block_head[this_basic_block + 1] == insn)
579 this_basic_block++;
580
230d793d
RS
581 if (GET_CODE (insn) == CODE_LABEL)
582 label_tick++;
583
0d4d42c3 584 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
230d793d
RS
585 {
586 /* Try this insn with each insn it links back to. */
587
588 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
5f4f0e22 589 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
230d793d
RS
590 goto retry;
591
592 /* Try each sequence of three linked insns ending with this one. */
593
594 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
595 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
596 nextlinks = XEXP (nextlinks, 1))
597 if ((next = try_combine (insn, XEXP (links, 0),
598 XEXP (nextlinks, 0))) != 0)
599 goto retry;
600
601#ifdef HAVE_cc0
602 /* Try to combine a jump insn that uses CC0
603 with a preceding insn that sets CC0, and maybe with its
604 logical predecessor as well.
605 This is how we make decrement-and-branch insns.
606 We need this special code because data flow connections
607 via CC0 do not get entered in LOG_LINKS. */
608
609 if (GET_CODE (insn) == JUMP_INSN
610 && (prev = prev_nonnote_insn (insn)) != 0
611 && GET_CODE (prev) == INSN
612 && sets_cc0_p (PATTERN (prev)))
613 {
5f4f0e22 614 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
230d793d
RS
615 goto retry;
616
617 for (nextlinks = LOG_LINKS (prev); nextlinks;
618 nextlinks = XEXP (nextlinks, 1))
619 if ((next = try_combine (insn, prev,
620 XEXP (nextlinks, 0))) != 0)
621 goto retry;
622 }
623
624 /* Do the same for an insn that explicitly references CC0. */
625 if (GET_CODE (insn) == INSN
626 && (prev = prev_nonnote_insn (insn)) != 0
627 && GET_CODE (prev) == INSN
628 && sets_cc0_p (PATTERN (prev))
629 && GET_CODE (PATTERN (insn)) == SET
630 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
631 {
5f4f0e22 632 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
230d793d
RS
633 goto retry;
634
635 for (nextlinks = LOG_LINKS (prev); nextlinks;
636 nextlinks = XEXP (nextlinks, 1))
637 if ((next = try_combine (insn, prev,
638 XEXP (nextlinks, 0))) != 0)
639 goto retry;
640 }
641
642 /* Finally, see if any of the insns that this insn links to
643 explicitly references CC0. If so, try this insn, that insn,
5089e22e 644 and its predecessor if it sets CC0. */
230d793d
RS
645 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
646 if (GET_CODE (XEXP (links, 0)) == INSN
647 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
648 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
649 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
650 && GET_CODE (prev) == INSN
651 && sets_cc0_p (PATTERN (prev))
652 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
653 goto retry;
654#endif
655
656 /* Try combining an insn with two different insns whose results it
657 uses. */
658 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
659 for (nextlinks = XEXP (links, 1); nextlinks;
660 nextlinks = XEXP (nextlinks, 1))
661 if ((next = try_combine (insn, XEXP (links, 0),
662 XEXP (nextlinks, 0))) != 0)
663 goto retry;
664
665 if (GET_CODE (insn) != NOTE)
666 record_dead_and_set_regs (insn);
667
668 retry:
669 ;
670 }
671 }
672
673 total_attempts += combine_attempts;
674 total_merges += combine_merges;
675 total_extras += combine_extras;
676 total_successes += combine_successes;
1a26b032 677
951553af 678 nonzero_sign_valid = 0;
230d793d 679}
ef026f91
RS
680
681/* Wipe the reg_last_xxx arrays in preparation for another pass. */
682
683static void
684init_reg_last_arrays ()
685{
686 int nregs = combine_max_regno;
687
4c9a05bc
RK
688 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
689 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
690 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
691 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
692 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
ef026f91 693 bzero (reg_last_set_invalid, nregs * sizeof (char));
4c9a05bc
RK
694 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
695 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
ef026f91
RS
696 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
697}
230d793d 698\f
7988fd36
RK
699/* Set up any promoted values for incoming argument registers. */
700
ee791cc3 701static void
7988fd36
RK
702setup_incoming_promotions ()
703{
704#ifdef PROMOTE_FUNCTION_ARGS
705 int regno;
706 rtx reg;
707 enum machine_mode mode;
708 int unsignedp;
709 rtx first = get_insns ();
710
711 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
712 if (FUNCTION_ARG_REGNO_P (regno)
713 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
714 record_value_for_reg (reg, first,
715 gen_rtx (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
500c518b
RK
716 GET_MODE (reg),
717 gen_rtx (CLOBBER, mode, const0_rtx)));
7988fd36
RK
718#endif
719}
720\f
91102d5a
RK
721/* Called via note_stores. If X is a pseudo that is narrower than
722 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
230d793d
RS
723
724 If we are setting only a portion of X and we can't figure out what
725 portion, assume all bits will be used since we don't know what will
d0ab8cd3
RK
726 be happening.
727
728 Similarly, set how many bits of X are known to be copies of the sign bit
729 at all locations in the function. This is the smallest number implied
730 by any set of X. */
230d793d
RS
731
732static void
951553af 733set_nonzero_bits_and_sign_copies (x, set)
230d793d
RS
734 rtx x;
735 rtx set;
736{
d0ab8cd3
RK
737 int num;
738
230d793d
RS
739 if (GET_CODE (x) == REG
740 && REGNO (x) >= FIRST_PSEUDO_REGISTER
e8095e80
RK
741 /* If this register is undefined at the start of the file, we can't
742 say what its contents were. */
8e08106d 743 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], REGNO (x))
5f4f0e22 744 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
230d793d 745 {
2dab894a 746 if (set == 0 || GET_CODE (set) == CLOBBER)
e8095e80
RK
747 {
748 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
88306d12 749 reg_sign_bit_copies[REGNO (x)] = 1;
e8095e80
RK
750 return;
751 }
230d793d
RS
752
753 /* If this is a complex assignment, see if we can convert it into a
5089e22e 754 simple assignment. */
230d793d 755 set = expand_field_assignment (set);
d79f08e0
RK
756
757 /* If this is a simple assignment, or we have a paradoxical SUBREG,
758 set what we know about X. */
759
760 if (SET_DEST (set) == x
761 || (GET_CODE (SET_DEST (set)) == SUBREG
705c7b3b
JW
762 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
763 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
d79f08e0 764 && SUBREG_REG (SET_DEST (set)) == x))
d0ab8cd3 765 {
9afa3d54
RK
766 rtx src = SET_SRC (set);
767
768#ifdef SHORT_IMMEDIATES_SIGN_EXTEND
769 /* If X is narrower than a word and SRC is a non-negative
770 constant that would appear negative in the mode of X,
771 sign-extend it for use in reg_nonzero_bits because some
772 machines (maybe most) will actually do the sign-extension
773 and this is the conservative approach.
774
775 ??? For 2.5, try to tighten up the MD files in this regard
776 instead of this kludge. */
777
778 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
779 && GET_CODE (src) == CONST_INT
780 && INTVAL (src) > 0
781 && 0 != (INTVAL (src)
782 & ((HOST_WIDE_INT) 1
9e69be8c 783 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9afa3d54
RK
784 src = GEN_INT (INTVAL (src)
785 | ((HOST_WIDE_INT) (-1)
786 << GET_MODE_BITSIZE (GET_MODE (x))));
787#endif
788
951553af 789 reg_nonzero_bits[REGNO (x)]
9afa3d54 790 |= nonzero_bits (src, nonzero_bits_mode);
d0ab8cd3
RK
791 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
792 if (reg_sign_bit_copies[REGNO (x)] == 0
793 || reg_sign_bit_copies[REGNO (x)] > num)
794 reg_sign_bit_copies[REGNO (x)] = num;
795 }
230d793d 796 else
d0ab8cd3 797 {
951553af 798 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
88306d12 799 reg_sign_bit_copies[REGNO (x)] = 1;
d0ab8cd3 800 }
230d793d
RS
801 }
802}
803\f
804/* See if INSN can be combined into I3. PRED and SUCC are optionally
805 insns that were previously combined into I3 or that will be combined
806 into the merger of INSN and I3.
807
808 Return 0 if the combination is not allowed for any reason.
809
810 If the combination is allowed, *PDEST will be set to the single
811 destination of INSN and *PSRC to the single source, and this function
812 will return 1. */
813
814static int
815can_combine_p (insn, i3, pred, succ, pdest, psrc)
816 rtx insn;
817 rtx i3;
818 rtx pred, succ;
819 rtx *pdest, *psrc;
820{
821 int i;
822 rtx set = 0, src, dest;
823 rtx p, link;
824 int all_adjacent = (succ ? (next_active_insn (insn) == succ
825 && next_active_insn (succ) == i3)
826 : next_active_insn (insn) == i3);
827
828 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
829 or a PARALLEL consisting of such a SET and CLOBBERs.
830
831 If INSN has CLOBBER parallel parts, ignore them for our processing.
832 By definition, these happen during the execution of the insn. When it
833 is merged with another insn, all bets are off. If they are, in fact,
834 needed and aren't also supplied in I3, they may be added by
835 recog_for_combine. Otherwise, it won't match.
836
837 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
838 note.
839
840 Get the source and destination of INSN. If more than one, can't
841 combine. */
842
843 if (GET_CODE (PATTERN (insn)) == SET)
844 set = PATTERN (insn);
845 else if (GET_CODE (PATTERN (insn)) == PARALLEL
846 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
847 {
848 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
849 {
850 rtx elt = XVECEXP (PATTERN (insn), 0, i);
851
852 switch (GET_CODE (elt))
853 {
854 /* We can ignore CLOBBERs. */
855 case CLOBBER:
856 break;
857
858 case SET:
859 /* Ignore SETs whose result isn't used but not those that
860 have side-effects. */
861 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
862 && ! side_effects_p (elt))
863 break;
864
865 /* If we have already found a SET, this is a second one and
866 so we cannot combine with this insn. */
867 if (set)
868 return 0;
869
870 set = elt;
871 break;
872
873 default:
874 /* Anything else means we can't combine. */
875 return 0;
876 }
877 }
878
879 if (set == 0
880 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
881 so don't do anything with it. */
882 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
883 return 0;
884 }
885 else
886 return 0;
887
888 if (set == 0)
889 return 0;
890
891 set = expand_field_assignment (set);
892 src = SET_SRC (set), dest = SET_DEST (set);
893
894 /* Don't eliminate a store in the stack pointer. */
895 if (dest == stack_pointer_rtx
230d793d
RS
896 /* If we couldn't eliminate a field assignment, we can't combine. */
897 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
898 /* Don't combine with an insn that sets a register to itself if it has
899 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
5f4f0e22 900 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
230d793d
RS
901 /* Can't merge a function call. */
902 || GET_CODE (src) == CALL
cd5e8f1f 903 /* Don't eliminate a function call argument. */
4dca5ec5
RK
904 || (GET_CODE (i3) == CALL_INSN
905 && (find_reg_fusage (i3, USE, dest)
906 || (GET_CODE (dest) == REG
907 && REGNO (dest) < FIRST_PSEUDO_REGISTER
908 && global_regs[REGNO (dest)])))
230d793d
RS
909 /* Don't substitute into an incremented register. */
910 || FIND_REG_INC_NOTE (i3, dest)
911 || (succ && FIND_REG_INC_NOTE (succ, dest))
912 /* Don't combine the end of a libcall into anything. */
5f4f0e22 913 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
230d793d
RS
914 /* Make sure that DEST is not used after SUCC but before I3. */
915 || (succ && ! all_adjacent
916 && reg_used_between_p (dest, succ, i3))
917 /* Make sure that the value that is to be substituted for the register
918 does not use any registers whose values alter in between. However,
919 If the insns are adjacent, a use can't cross a set even though we
920 think it might (this can happen for a sequence of insns each setting
921 the same destination; reg_last_set of that register might point to
d81481d3
RK
922 a NOTE). If INSN has a REG_EQUIV note, the register is always
923 equivalent to the memory so the substitution is valid even if there
924 are intervening stores. Also, don't move a volatile asm or
925 UNSPEC_VOLATILE across any other insns. */
230d793d 926 || (! all_adjacent
d81481d3
RK
927 && (((GET_CODE (src) != MEM
928 || ! find_reg_note (insn, REG_EQUIV, src))
929 && use_crosses_set_p (src, INSN_CUID (insn)))
a66a10c7
RS
930 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
931 || GET_CODE (src) == UNSPEC_VOLATILE))
230d793d
RS
932 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
933 better register allocation by not doing the combine. */
934 || find_reg_note (i3, REG_NO_CONFLICT, dest)
935 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
936 /* Don't combine across a CALL_INSN, because that would possibly
937 change whether the life span of some REGs crosses calls or not,
938 and it is a pain to update that information.
939 Exception: if source is a constant, moving it later can't hurt.
940 Accept that special case, because it helps -fforce-addr a lot. */
941 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
942 return 0;
943
944 /* DEST must either be a REG or CC0. */
945 if (GET_CODE (dest) == REG)
946 {
947 /* If register alignment is being enforced for multi-word items in all
948 cases except for parameters, it is possible to have a register copy
949 insn referencing a hard register that is not allowed to contain the
950 mode being copied and which would not be valid as an operand of most
951 insns. Eliminate this problem by not combining with such an insn.
952
953 Also, on some machines we don't want to extend the life of a hard
4d2c432d
RK
954 register.
955
956 This is the same test done in can_combine except that we don't test
957 if SRC is a CALL operation to permit a hard register with
958 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
959 into account. */
230d793d
RS
960
961 if (GET_CODE (src) == REG
962 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
963 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
c448a43e
RK
964 /* Don't extend the life of a hard register unless it is
965 user variable (if we have few registers) or it can't
966 fit into the desired register (meaning something special
ecd40809
RK
967 is going on).
968 Also avoid substituting a return register into I3, because
969 reload can't handle a conflict with constraints of other
970 inputs. */
230d793d 971 || (REGNO (src) < FIRST_PSEUDO_REGISTER
c448a43e 972 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
f95182a4
ILT
973 || (SMALL_REGISTER_CLASSES
974 && ((! all_adjacent && ! REG_USERVAR_P (src))
975 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
e9a25f70 976 && ! REG_USERVAR_P (src))))))))
230d793d
RS
977 return 0;
978 }
979 else if (GET_CODE (dest) != CC0)
980 return 0;
981
5f96750d
RS
982 /* Don't substitute for a register intended as a clobberable operand.
983 Similarly, don't substitute an expression containing a register that
984 will be clobbered in I3. */
230d793d
RS
985 if (GET_CODE (PATTERN (i3)) == PARALLEL)
986 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
987 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
5f96750d
RS
988 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
989 src)
990 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
230d793d
RS
991 return 0;
992
993 /* If INSN contains anything volatile, or is an `asm' (whether volatile
994 or not), reject, unless nothing volatile comes between it and I3,
995 with the exception of SUCC. */
996
997 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
998 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
999 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1000 && p != succ && volatile_refs_p (PATTERN (p)))
1001 return 0;
1002
b79ee7eb
RH
1003 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1004 to be an explicit register variable, and was chosen for a reason. */
1005
1006 if (GET_CODE (src) == ASM_OPERANDS
1007 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1008 return 0;
1009
4b2cb4a2
RS
1010 /* If there are any volatile insns between INSN and I3, reject, because
1011 they might affect machine state. */
1012
1013 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1014 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1015 && p != succ && volatile_insn_p (PATTERN (p)))
1016 return 0;
1017
230d793d
RS
1018 /* If INSN or I2 contains an autoincrement or autodecrement,
1019 make sure that register is not used between there and I3,
1020 and not already used in I3 either.
1021 Also insist that I3 not be a jump; if it were one
1022 and the incremented register were spilled, we would lose. */
1023
1024#ifdef AUTO_INC_DEC
1025 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1026 if (REG_NOTE_KIND (link) == REG_INC
1027 && (GET_CODE (i3) == JUMP_INSN
1028 || reg_used_between_p (XEXP (link, 0), insn, i3)
1029 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1030 return 0;
1031#endif
1032
1033#ifdef HAVE_cc0
1034 /* Don't combine an insn that follows a CC0-setting insn.
1035 An insn that uses CC0 must not be separated from the one that sets it.
1036 We do, however, allow I2 to follow a CC0-setting insn if that insn
1037 is passed as I1; in that case it will be deleted also.
1038 We also allow combining in this case if all the insns are adjacent
1039 because that would leave the two CC0 insns adjacent as well.
1040 It would be more logical to test whether CC0 occurs inside I1 or I2,
1041 but that would be much slower, and this ought to be equivalent. */
1042
1043 p = prev_nonnote_insn (insn);
1044 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1045 && ! all_adjacent)
1046 return 0;
1047#endif
1048
1049 /* If we get here, we have passed all the tests and the combination is
1050 to be allowed. */
1051
1052 *pdest = dest;
1053 *psrc = src;
1054
1055 return 1;
1056}
1057\f
956d6950
JL
1058/* Check if PAT is an insn - or a part of it - used to set up an
1059 argument for a function in a hard register. */
1060
1061static int
1062sets_function_arg_p (pat)
1063 rtx pat;
1064{
1065 int i;
1066 rtx inner_dest;
1067
1068 switch (GET_CODE (pat))
1069 {
1070 case INSN:
1071 return sets_function_arg_p (PATTERN (pat));
1072
1073 case PARALLEL:
1074 for (i = XVECLEN (pat, 0); --i >= 0;)
1075 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1076 return 1;
1077
1078 break;
1079
1080 case SET:
1081 inner_dest = SET_DEST (pat);
1082 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1083 || GET_CODE (inner_dest) == SUBREG
1084 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1085 inner_dest = XEXP (inner_dest, 0);
1086
1087 return (GET_CODE (inner_dest) == REG
1088 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1089 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1090 }
1091
1092 return 0;
1093}
1094
230d793d
RS
1095/* LOC is the location within I3 that contains its pattern or the component
1096 of a PARALLEL of the pattern. We validate that it is valid for combining.
1097
1098 One problem is if I3 modifies its output, as opposed to replacing it
1099 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1100 so would produce an insn that is not equivalent to the original insns.
1101
1102 Consider:
1103
1104 (set (reg:DI 101) (reg:DI 100))
1105 (set (subreg:SI (reg:DI 101) 0) <foo>)
1106
1107 This is NOT equivalent to:
1108
1109 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1110 (set (reg:DI 101) (reg:DI 100))])
1111
1112 Not only does this modify 100 (in which case it might still be valid
1113 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1114
1115 We can also run into a problem if I2 sets a register that I1
1116 uses and I1 gets directly substituted into I3 (not via I2). In that
1117 case, we would be getting the wrong value of I2DEST into I3, so we
1118 must reject the combination. This case occurs when I2 and I1 both
1119 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1120 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1121 of a SET must prevent combination from occurring.
1122
e9a25f70 1123 On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
c448a43e
RK
1124 if the destination of a SET is a hard register that isn't a user
1125 variable.
230d793d
RS
1126
1127 Before doing the above check, we first try to expand a field assignment
1128 into a set of logical operations.
1129
1130 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1131 we place a register that is both set and used within I3. If more than one
1132 such register is detected, we fail.
1133
1134 Return 1 if the combination is valid, zero otherwise. */
1135
1136static int
1137combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1138 rtx i3;
1139 rtx *loc;
1140 rtx i2dest;
1141 rtx i1dest;
1142 int i1_not_in_src;
1143 rtx *pi3dest_killed;
1144{
1145 rtx x = *loc;
1146
1147 if (GET_CODE (x) == SET)
1148 {
1149 rtx set = expand_field_assignment (x);
1150 rtx dest = SET_DEST (set);
1151 rtx src = SET_SRC (set);
1152 rtx inner_dest = dest, inner_src = src;
1153
1154 SUBST (*loc, set);
1155
1156 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1157 || GET_CODE (inner_dest) == SUBREG
1158 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1159 inner_dest = XEXP (inner_dest, 0);
1160
1161 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1162 was added. */
1163#if 0
1164 while (GET_CODE (inner_src) == STRICT_LOW_PART
1165 || GET_CODE (inner_src) == SUBREG
1166 || GET_CODE (inner_src) == ZERO_EXTRACT)
1167 inner_src = XEXP (inner_src, 0);
1168
1169 /* If it is better that two different modes keep two different pseudos,
1170 avoid combining them. This avoids producing the following pattern
1171 on a 386:
1172 (set (subreg:SI (reg/v:QI 21) 0)
1173 (lshiftrt:SI (reg/v:SI 20)
1174 (const_int 24)))
1175 If that were made, reload could not handle the pair of
1176 reg 20/21, since it would try to get any GENERAL_REGS
1177 but some of them don't handle QImode. */
1178
1179 if (rtx_equal_p (inner_src, i2dest)
1180 && GET_CODE (inner_dest) == REG
1181 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1182 return 0;
1183#endif
1184
1185 /* Check for the case where I3 modifies its output, as
1186 discussed above. */
1187 if ((inner_dest != dest
1188 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1189 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
956d6950 1190
3f508eca
RK
1191 /* This is the same test done in can_combine_p except that we
1192 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
956d6950
JL
1193 CALL operation. Moreover, we can't test all_adjacent; we don't
1194 have to, since this instruction will stay in place, thus we are
1195 not considering increasing the lifetime of INNER_DEST.
1196
1197 Also, if this insn sets a function argument, combining it with
1198 something that might need a spill could clobber a previous
1199 function argument; the all_adjacent test in can_combine_p also
1200 checks this; here, we do a more specific test for this case. */
1201
230d793d 1202 || (GET_CODE (inner_dest) == REG
dfbe1b2f 1203 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
c448a43e
RK
1204 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1205 GET_MODE (inner_dest))
e9a25f70
JL
1206 || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1207 && ! REG_USERVAR_P (inner_dest)
956d6950
JL
1208 && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1209 || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1210 && i3 != 0
1211 && sets_function_arg_p (prev_nonnote_insn (i3)))))))
230d793d
RS
1212 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1213 return 0;
1214
1215 /* If DEST is used in I3, it is being killed in this insn,
36a9c2e9
JL
1216 so record that for later.
1217 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1218 STACK_POINTER_REGNUM, since these are always considered to be
1219 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
230d793d 1220 if (pi3dest_killed && GET_CODE (dest) == REG
36a9c2e9
JL
1221 && reg_referenced_p (dest, PATTERN (i3))
1222 && REGNO (dest) != FRAME_POINTER_REGNUM
6d7096b0
DE
1223#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1224 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1225#endif
36a9c2e9
JL
1226#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1227 && (REGNO (dest) != ARG_POINTER_REGNUM
1228 || ! fixed_regs [REGNO (dest)])
1229#endif
1230 && REGNO (dest) != STACK_POINTER_REGNUM)
230d793d
RS
1231 {
1232 if (*pi3dest_killed)
1233 return 0;
1234
1235 *pi3dest_killed = dest;
1236 }
1237 }
1238
1239 else if (GET_CODE (x) == PARALLEL)
1240 {
1241 int i;
1242
1243 for (i = 0; i < XVECLEN (x, 0); i++)
1244 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1245 i1_not_in_src, pi3dest_killed))
1246 return 0;
1247 }
1248
1249 return 1;
1250}
1251\f
1252/* Try to combine the insns I1 and I2 into I3.
1253 Here I1 and I2 appear earlier than I3.
1254 I1 can be zero; then we combine just I2 into I3.
1255
1256 It we are combining three insns and the resulting insn is not recognized,
1257 try splitting it into two insns. If that happens, I2 and I3 are retained
1258 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1259 are pseudo-deleted.
1260
abe6e52f
RK
1261 Return 0 if the combination does not work. Then nothing is changed.
1262 If we did the combination, return the insn at which combine should
1263 resume scanning. */
230d793d
RS
1264
1265static rtx
1266try_combine (i3, i2, i1)
1267 register rtx i3, i2, i1;
1268{
1269 /* New patterns for I3 and I3, respectively. */
1270 rtx newpat, newi2pat = 0;
1271 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1272 int added_sets_1, added_sets_2;
1273 /* Total number of SETs to put into I3. */
1274 int total_sets;
1275 /* Nonzero is I2's body now appears in I3. */
1276 int i2_is_used;
1277 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1278 int insn_code_number, i2_code_number, other_code_number;
1279 /* Contains I3 if the destination of I3 is used in its source, which means
1280 that the old life of I3 is being killed. If that usage is placed into
1281 I2 and not in I3, a REG_DEAD note must be made. */
1282 rtx i3dest_killed = 0;
1283 /* SET_DEST and SET_SRC of I2 and I1. */
1284 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1285 /* PATTERN (I2), or a copy of it in certain cases. */
1286 rtx i2pat;
1287 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
c4e861e8 1288 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
230d793d
RS
1289 int i1_feeds_i3 = 0;
1290 /* Notes that must be added to REG_NOTES in I3 and I2. */
1291 rtx new_i3_notes, new_i2_notes;
176c9e6b
JW
1292 /* Notes that we substituted I3 into I2 instead of the normal case. */
1293 int i3_subst_into_i2 = 0;
df7d75de
RK
1294 /* Notes that I1, I2 or I3 is a MULT operation. */
1295 int have_mult = 0;
a29ca9db
RK
1296 /* Number of clobbers of SCRATCH we had to add. */
1297 int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
230d793d
RS
1298
1299 int maxreg;
1300 rtx temp;
1301 register rtx link;
1302 int i;
1303
1304 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1305 This can occur when flow deletes an insn that it has merged into an
1306 auto-increment address. We also can't do anything if I3 has a
1307 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1308 libcall. */
1309
1310 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1311 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1312 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
5f4f0e22 1313 || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
230d793d
RS
1314 return 0;
1315
1316 combine_attempts++;
1317
241cea85 1318 undobuf.undos = undobuf.previous_undos = 0;
230d793d
RS
1319 undobuf.other_insn = 0;
1320
1321 /* Save the current high-water-mark so we can free storage if we didn't
1322 accept this combination. */
1323 undobuf.storage = (char *) oballoc (0);
1324
6e25d159
RK
1325 /* Reset the hard register usage information. */
1326 CLEAR_HARD_REG_SET (newpat_used_regs);
1327
230d793d
RS
1328 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1329 code below, set I1 to be the earlier of the two insns. */
1330 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1331 temp = i1, i1 = i2, i2 = temp;
1332
abe6e52f 1333 added_links_insn = 0;
137e889e 1334
230d793d
RS
1335 /* First check for one important special-case that the code below will
1336 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1337 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1338 we may be able to replace that destination with the destination of I3.
1339 This occurs in the common code where we compute both a quotient and
1340 remainder into a structure, in which case we want to do the computation
1341 directly into the structure to avoid register-register copies.
1342
1343 We make very conservative checks below and only try to handle the
1344 most common cases of this. For example, we only handle the case
1345 where I2 and I3 are adjacent to avoid making difficult register
1346 usage tests. */
1347
1348 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1349 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1350 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
f95182a4 1351 && (! SMALL_REGISTER_CLASSES
e9a25f70
JL
1352 || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1353 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1354 || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
230d793d
RS
1355 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1356 && GET_CODE (PATTERN (i2)) == PARALLEL
1357 && ! side_effects_p (SET_DEST (PATTERN (i3)))
5089e22e
RS
1358 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1359 below would need to check what is inside (and reg_overlap_mentioned_p
1360 doesn't support those codes anyway). Don't allow those destinations;
1361 the resulting insn isn't likely to be recognized anyway. */
1362 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1363 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
230d793d
RS
1364 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1365 SET_DEST (PATTERN (i3)))
1366 && next_real_insn (i2) == i3)
5089e22e
RS
1367 {
1368 rtx p2 = PATTERN (i2);
1369
1370 /* Make sure that the destination of I3,
1371 which we are going to substitute into one output of I2,
1372 is not used within another output of I2. We must avoid making this:
1373 (parallel [(set (mem (reg 69)) ...)
1374 (set (reg 69) ...)])
1375 which is not well-defined as to order of actions.
1376 (Besides, reload can't handle output reloads for this.)
1377
1378 The problem can also happen if the dest of I3 is a memory ref,
1379 if another dest in I2 is an indirect memory ref. */
1380 for (i = 0; i < XVECLEN (p2, 0); i++)
7ca919b7
RK
1381 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1382 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
5089e22e
RS
1383 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1384 SET_DEST (XVECEXP (p2, 0, i))))
1385 break;
230d793d 1386
5089e22e
RS
1387 if (i == XVECLEN (p2, 0))
1388 for (i = 0; i < XVECLEN (p2, 0); i++)
1389 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1390 {
1391 combine_merges++;
230d793d 1392
5089e22e
RS
1393 subst_insn = i3;
1394 subst_low_cuid = INSN_CUID (i2);
230d793d 1395
c4e861e8 1396 added_sets_2 = added_sets_1 = 0;
5089e22e 1397 i2dest = SET_SRC (PATTERN (i3));
230d793d 1398
5089e22e
RS
1399 /* Replace the dest in I2 with our dest and make the resulting
1400 insn the new pattern for I3. Then skip to where we
1401 validate the pattern. Everything was set up above. */
1402 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1403 SET_DEST (PATTERN (i3)));
1404
1405 newpat = p2;
176c9e6b 1406 i3_subst_into_i2 = 1;
5089e22e
RS
1407 goto validate_replacement;
1408 }
1409 }
230d793d
RS
1410
1411#ifndef HAVE_cc0
1412 /* If we have no I1 and I2 looks like:
1413 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1414 (set Y OP)])
1415 make up a dummy I1 that is
1416 (set Y OP)
1417 and change I2 to be
1418 (set (reg:CC X) (compare:CC Y (const_int 0)))
1419
1420 (We can ignore any trailing CLOBBERs.)
1421
1422 This undoes a previous combination and allows us to match a branch-and-
1423 decrement insn. */
1424
1425 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1426 && XVECLEN (PATTERN (i2), 0) >= 2
1427 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1428 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1429 == MODE_CC)
1430 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1431 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1432 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1433 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1434 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1435 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1436 {
1437 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1438 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1439 break;
1440
1441 if (i == 1)
1442 {
1443 /* We make I1 with the same INSN_UID as I2. This gives it
1444 the same INSN_CUID for value tracking. Our fake I1 will
1445 never appear in the insn stream so giving it the same INSN_UID
1446 as I2 will not cause a problem. */
1447
0d9641d1 1448 subst_prev_insn = i1
9524f769
DM
1449 = gen_rtx (INSN, VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1450 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX, NULL_RTX);
230d793d
RS
1451
1452 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1453 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1454 SET_DEST (PATTERN (i1)));
1455 }
1456 }
1457#endif
1458
1459 /* Verify that I2 and I1 are valid for combining. */
5f4f0e22
CH
1460 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1461 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
230d793d
RS
1462 {
1463 undo_all ();
1464 return 0;
1465 }
1466
1467 /* Record whether I2DEST is used in I2SRC and similarly for the other
1468 cases. Knowing this will help in register status updating below. */
1469 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1470 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1471 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1472
916f14f1 1473 /* See if I1 directly feeds into I3. It does if I1DEST is not used
230d793d
RS
1474 in I2SRC. */
1475 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1476
1477 /* Ensure that I3's pattern can be the destination of combines. */
1478 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1479 i1 && i2dest_in_i1src && i1_feeds_i3,
1480 &i3dest_killed))
1481 {
1482 undo_all ();
1483 return 0;
1484 }
1485
df7d75de
RK
1486 /* See if any of the insns is a MULT operation. Unless one is, we will
1487 reject a combination that is, since it must be slower. Be conservative
1488 here. */
1489 if (GET_CODE (i2src) == MULT
1490 || (i1 != 0 && GET_CODE (i1src) == MULT)
1491 || (GET_CODE (PATTERN (i3)) == SET
1492 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1493 have_mult = 1;
1494
230d793d
RS
1495 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1496 We used to do this EXCEPT in one case: I3 has a post-inc in an
1497 output operand. However, that exception can give rise to insns like
1498 mov r3,(r3)+
1499 which is a famous insn on the PDP-11 where the value of r3 used as the
5089e22e 1500 source was model-dependent. Avoid this sort of thing. */
230d793d
RS
1501
1502#if 0
1503 if (!(GET_CODE (PATTERN (i3)) == SET
1504 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1505 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1506 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1507 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1508 /* It's not the exception. */
1509#endif
1510#ifdef AUTO_INC_DEC
1511 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1512 if (REG_NOTE_KIND (link) == REG_INC
1513 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1514 || (i1 != 0
1515 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1516 {
1517 undo_all ();
1518 return 0;
1519 }
1520#endif
1521
1522 /* See if the SETs in I1 or I2 need to be kept around in the merged
1523 instruction: whenever the value set there is still needed past I3.
1524 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1525
1526 For the SET in I1, we have two cases: If I1 and I2 independently
1527 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1528 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1529 in I1 needs to be kept around unless I1DEST dies or is set in either
1530 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1531 I1DEST. If so, we know I1 feeds into I2. */
1532
1533 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1534
1535 added_sets_1
1536 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1537 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1538
1539 /* If the set in I2 needs to be kept around, we must make a copy of
1540 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
5089e22e 1541 PATTERN (I2), we are only substituting for the original I1DEST, not into
230d793d
RS
1542 an already-substituted copy. This also prevents making self-referential
1543 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1544 I2DEST. */
1545
1546 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1547 ? gen_rtx (SET, VOIDmode, i2dest, i2src)
1548 : PATTERN (i2));
1549
1550 if (added_sets_2)
1551 i2pat = copy_rtx (i2pat);
1552
1553 combine_merges++;
1554
1555 /* Substitute in the latest insn for the regs set by the earlier ones. */
1556
1557 maxreg = max_reg_num ();
1558
1559 subst_insn = i3;
230d793d
RS
1560
1561 /* It is possible that the source of I2 or I1 may be performing an
1562 unneeded operation, such as a ZERO_EXTEND of something that is known
1563 to have the high part zero. Handle that case by letting subst look at
1564 the innermost one of them.
1565
1566 Another way to do this would be to have a function that tries to
1567 simplify a single insn instead of merging two or more insns. We don't
1568 do this because of the potential of infinite loops and because
1569 of the potential extra memory required. However, doing it the way
1570 we are is a bit of a kludge and doesn't catch all cases.
1571
1572 But only do this if -fexpensive-optimizations since it slows things down
1573 and doesn't usually win. */
1574
1575 if (flag_expensive_optimizations)
1576 {
1577 /* Pass pc_rtx so no substitutions are done, just simplifications.
1578 The cases that we are interested in here do not involve the few
1579 cases were is_replaced is checked. */
1580 if (i1)
d0ab8cd3
RK
1581 {
1582 subst_low_cuid = INSN_CUID (i1);
1583 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1584 }
230d793d 1585 else
d0ab8cd3
RK
1586 {
1587 subst_low_cuid = INSN_CUID (i2);
1588 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1589 }
230d793d 1590
241cea85 1591 undobuf.previous_undos = undobuf.undos;
230d793d
RS
1592 }
1593
1594#ifndef HAVE_cc0
1595 /* Many machines that don't use CC0 have insns that can both perform an
1596 arithmetic operation and set the condition code. These operations will
1597 be represented as a PARALLEL with the first element of the vector
1598 being a COMPARE of an arithmetic operation with the constant zero.
1599 The second element of the vector will set some pseudo to the result
1600 of the same arithmetic operation. If we simplify the COMPARE, we won't
1601 match such a pattern and so will generate an extra insn. Here we test
1602 for this case, where both the comparison and the operation result are
1603 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1604 I2SRC. Later we will make the PARALLEL that contains I2. */
1605
1606 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1607 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1608 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1609 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1610 {
1611 rtx *cc_use;
1612 enum machine_mode compare_mode;
1613
1614 newpat = PATTERN (i3);
1615 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1616
1617 i2_is_used = 1;
1618
1619#ifdef EXTRA_CC_MODES
1620 /* See if a COMPARE with the operand we substituted in should be done
1621 with the mode that is currently being used. If not, do the same
1622 processing we do in `subst' for a SET; namely, if the destination
1623 is used only once, try to replace it with a register of the proper
1624 mode and also replace the COMPARE. */
1625 if (undobuf.other_insn == 0
1626 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1627 &undobuf.other_insn))
77fa0940
RK
1628 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1629 i2src, const0_rtx))
230d793d
RS
1630 != GET_MODE (SET_DEST (newpat))))
1631 {
1632 int regno = REGNO (SET_DEST (newpat));
1633 rtx new_dest = gen_rtx (REG, compare_mode, regno);
1634
1635 if (regno < FIRST_PSEUDO_REGISTER
b1f21e0a 1636 || (REG_N_SETS (regno) == 1 && ! added_sets_2
230d793d
RS
1637 && ! REG_USERVAR_P (SET_DEST (newpat))))
1638 {
1639 if (regno >= FIRST_PSEUDO_REGISTER)
1640 SUBST (regno_reg_rtx[regno], new_dest);
1641
1642 SUBST (SET_DEST (newpat), new_dest);
1643 SUBST (XEXP (*cc_use, 0), new_dest);
1644 SUBST (SET_SRC (newpat),
1645 gen_rtx_combine (COMPARE, compare_mode,
1646 i2src, const0_rtx));
1647 }
1648 else
1649 undobuf.other_insn = 0;
1650 }
1651#endif
1652 }
1653 else
1654#endif
1655 {
1656 n_occurrences = 0; /* `subst' counts here */
1657
1658 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1659 need to make a unique copy of I2SRC each time we substitute it
1660 to avoid self-referential rtl. */
1661
d0ab8cd3 1662 subst_low_cuid = INSN_CUID (i2);
230d793d
RS
1663 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1664 ! i1_feeds_i3 && i1dest_in_i1src);
241cea85 1665 undobuf.previous_undos = undobuf.undos;
230d793d
RS
1666
1667 /* Record whether i2's body now appears within i3's body. */
1668 i2_is_used = n_occurrences;
1669 }
1670
1671 /* If we already got a failure, don't try to do more. Otherwise,
1672 try to substitute in I1 if we have it. */
1673
1674 if (i1 && GET_CODE (newpat) != CLOBBER)
1675 {
1676 /* Before we can do this substitution, we must redo the test done
1677 above (see detailed comments there) that ensures that I1DEST
0f41302f 1678 isn't mentioned in any SETs in NEWPAT that are field assignments. */
230d793d 1679
5f4f0e22
CH
1680 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1681 0, NULL_PTR))
230d793d
RS
1682 {
1683 undo_all ();
1684 return 0;
1685 }
1686
1687 n_occurrences = 0;
d0ab8cd3 1688 subst_low_cuid = INSN_CUID (i1);
230d793d 1689 newpat = subst (newpat, i1dest, i1src, 0, 0);
241cea85 1690 undobuf.previous_undos = undobuf.undos;
230d793d
RS
1691 }
1692
916f14f1
RK
1693 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1694 to count all the ways that I2SRC and I1SRC can be used. */
5f4f0e22 1695 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
916f14f1 1696 && i2_is_used + added_sets_2 > 1)
5f4f0e22 1697 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
916f14f1
RK
1698 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1699 > 1))
230d793d
RS
1700 /* Fail if we tried to make a new register (we used to abort, but there's
1701 really no reason to). */
1702 || max_reg_num () != maxreg
1703 /* Fail if we couldn't do something and have a CLOBBER. */
df7d75de
RK
1704 || GET_CODE (newpat) == CLOBBER
1705 /* Fail if this new pattern is a MULT and we didn't have one before
1706 at the outer level. */
1707 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1708 && ! have_mult))
230d793d
RS
1709 {
1710 undo_all ();
1711 return 0;
1712 }
1713
1714 /* If the actions of the earlier insns must be kept
1715 in addition to substituting them into the latest one,
1716 we must make a new PARALLEL for the latest insn
1717 to hold additional the SETs. */
1718
1719 if (added_sets_1 || added_sets_2)
1720 {
1721 combine_extras++;
1722
1723 if (GET_CODE (newpat) == PARALLEL)
1724 {
1725 rtvec old = XVEC (newpat, 0);
1726 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1727 newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
59888de2 1728 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
230d793d
RS
1729 sizeof (old->elem[0]) * old->num_elem);
1730 }
1731 else
1732 {
1733 rtx old = newpat;
1734 total_sets = 1 + added_sets_1 + added_sets_2;
1735 newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1736 XVECEXP (newpat, 0, 0) = old;
1737 }
1738
1739 if (added_sets_1)
1740 XVECEXP (newpat, 0, --total_sets)
1741 = (GET_CODE (PATTERN (i1)) == PARALLEL
1742 ? gen_rtx (SET, VOIDmode, i1dest, i1src) : PATTERN (i1));
1743
1744 if (added_sets_2)
1745 {
1746 /* If there is no I1, use I2's body as is. We used to also not do
1747 the subst call below if I2 was substituted into I3,
1748 but that could lose a simplification. */
1749 if (i1 == 0)
1750 XVECEXP (newpat, 0, --total_sets) = i2pat;
1751 else
1752 /* See comment where i2pat is assigned. */
1753 XVECEXP (newpat, 0, --total_sets)
1754 = subst (i2pat, i1dest, i1src, 0, 0);
1755 }
1756 }
1757
1758 /* We come here when we are replacing a destination in I2 with the
1759 destination of I3. */
1760 validate_replacement:
1761
6e25d159
RK
1762 /* Note which hard regs this insn has as inputs. */
1763 mark_used_regs_combine (newpat);
1764
230d793d 1765 /* Is the result of combination a valid instruction? */
a29ca9db
RK
1766 insn_code_number
1767 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
230d793d
RS
1768
1769 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1770 the second SET's destination is a register that is unused. In that case,
1771 we just need the first SET. This can occur when simplifying a divmod
1772 insn. We *must* test for this case here because the code below that
1773 splits two independent SETs doesn't handle this case correctly when it
1774 updates the register status. Also check the case where the first
1775 SET's destination is unused. That would not cause incorrect code, but
1776 does cause an unneeded insn to remain. */
1777
1778 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1779 && XVECLEN (newpat, 0) == 2
1780 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1781 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1782 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1783 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1784 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1785 && asm_noperands (newpat) < 0)
1786 {
1787 newpat = XVECEXP (newpat, 0, 0);
a29ca9db
RK
1788 insn_code_number
1789 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
230d793d
RS
1790 }
1791
1792 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1793 && XVECLEN (newpat, 0) == 2
1794 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1795 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1796 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1797 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1798 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1799 && asm_noperands (newpat) < 0)
1800 {
1801 newpat = XVECEXP (newpat, 0, 1);
a29ca9db
RK
1802 insn_code_number
1803 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
230d793d
RS
1804 }
1805
1806 /* If we were combining three insns and the result is a simple SET
1807 with no ASM_OPERANDS that wasn't recognized, try to split it into two
916f14f1
RK
1808 insns. There are two ways to do this. It can be split using a
1809 machine-specific method (like when you have an addition of a large
1810 constant) or by combine in the function find_split_point. */
1811
230d793d
RS
1812 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1813 && asm_noperands (newpat) < 0)
1814 {
916f14f1 1815 rtx m_split, *split;
42495ca0 1816 rtx ni2dest = i2dest;
916f14f1
RK
1817
1818 /* See if the MD file can split NEWPAT. If it can't, see if letting it
42495ca0
RK
1819 use I2DEST as a scratch register will help. In the latter case,
1820 convert I2DEST to the mode of the source of NEWPAT if we can. */
916f14f1
RK
1821
1822 m_split = split_insns (newpat, i3);
a70c61d9
JW
1823
1824 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1825 inputs of NEWPAT. */
1826
1827 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1828 possible to try that as a scratch reg. This would require adding
1829 more code to make it work though. */
1830
1831 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
42495ca0
RK
1832 {
1833 /* If I2DEST is a hard register or the only use of a pseudo,
1834 we can change its mode. */
1835 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
02f4ada4 1836 && GET_MODE (SET_DEST (newpat)) != VOIDmode
60654f77 1837 && GET_CODE (i2dest) == REG
42495ca0 1838 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
b1f21e0a 1839 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
42495ca0
RK
1840 && ! REG_USERVAR_P (i2dest))))
1841 ni2dest = gen_rtx (REG, GET_MODE (SET_DEST (newpat)),
1842 REGNO (i2dest));
1843
1844 m_split = split_insns (gen_rtx (PARALLEL, VOIDmode,
1845 gen_rtvec (2, newpat,
1846 gen_rtx (CLOBBER,
1847 VOIDmode,
1848 ni2dest))),
1849 i3);
1850 }
916f14f1
RK
1851
1852 if (m_split && GET_CODE (m_split) == SEQUENCE
3f508eca
RK
1853 && XVECLEN (m_split, 0) == 2
1854 && (next_real_insn (i2) == i3
1855 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1856 INSN_CUID (i2))))
916f14f1 1857 {
1a26b032 1858 rtx i2set, i3set;
d0ab8cd3 1859 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
916f14f1 1860 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
916f14f1 1861
e4ba89be
RK
1862 i3set = single_set (XVECEXP (m_split, 0, 1));
1863 i2set = single_set (XVECEXP (m_split, 0, 0));
1a26b032 1864
42495ca0
RK
1865 /* In case we changed the mode of I2DEST, replace it in the
1866 pseudo-register table here. We can't do it above in case this
1867 code doesn't get executed and we do a split the other way. */
1868
1869 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1870 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1871
a29ca9db
RK
1872 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
1873 &i2_scratches);
1a26b032
RK
1874
1875 /* If I2 or I3 has multiple SETs, we won't know how to track
9cc96794
RK
1876 register status, so don't use these insns. If I2's destination
1877 is used between I2 and I3, we also can't use these insns. */
1a26b032 1878
9cc96794
RK
1879 if (i2_code_number >= 0 && i2set && i3set
1880 && (next_real_insn (i2) == i3
1881 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
a29ca9db
RK
1882 insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
1883 &i3_scratches);
d0ab8cd3
RK
1884 if (insn_code_number >= 0)
1885 newpat = newi3pat;
1886
c767f54b 1887 /* It is possible that both insns now set the destination of I3.
22609cbf 1888 If so, we must show an extra use of it. */
c767f54b 1889
393de53f
RK
1890 if (insn_code_number >= 0)
1891 {
1892 rtx new_i3_dest = SET_DEST (i3set);
1893 rtx new_i2_dest = SET_DEST (i2set);
1894
1895 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1896 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1897 || GET_CODE (new_i3_dest) == SUBREG)
1898 new_i3_dest = XEXP (new_i3_dest, 0);
1899
d4096689
RK
1900 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1901 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1902 || GET_CODE (new_i2_dest) == SUBREG)
1903 new_i2_dest = XEXP (new_i2_dest, 0);
1904
393de53f
RK
1905 if (GET_CODE (new_i3_dest) == REG
1906 && GET_CODE (new_i2_dest) == REG
1907 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
b1f21e0a 1908 REG_N_SETS (REGNO (new_i2_dest))++;
393de53f 1909 }
916f14f1 1910 }
230d793d
RS
1911
1912 /* If we can split it and use I2DEST, go ahead and see if that
1913 helps things be recognized. Verify that none of the registers
1914 are set between I2 and I3. */
d0ab8cd3 1915 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
230d793d
RS
1916#ifdef HAVE_cc0
1917 && GET_CODE (i2dest) == REG
1918#endif
1919 /* We need I2DEST in the proper mode. If it is a hard register
1920 or the only use of a pseudo, we can change its mode. */
1921 && (GET_MODE (*split) == GET_MODE (i2dest)
1922 || GET_MODE (*split) == VOIDmode
1923 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
b1f21e0a 1924 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
230d793d
RS
1925 && ! REG_USERVAR_P (i2dest)))
1926 && (next_real_insn (i2) == i3
1927 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1928 /* We can't overwrite I2DEST if its value is still used by
1929 NEWPAT. */
1930 && ! reg_referenced_p (i2dest, newpat))
1931 {
1932 rtx newdest = i2dest;
df7d75de
RK
1933 enum rtx_code split_code = GET_CODE (*split);
1934 enum machine_mode split_mode = GET_MODE (*split);
230d793d
RS
1935
1936 /* Get NEWDEST as a register in the proper mode. We have already
1937 validated that we can do this. */
df7d75de 1938 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
230d793d 1939 {
df7d75de 1940 newdest = gen_rtx (REG, split_mode, REGNO (i2dest));
230d793d
RS
1941
1942 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1943 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
1944 }
1945
1946 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
1947 an ASHIFT. This can occur if it was inside a PLUS and hence
1948 appeared to be a memory address. This is a kludge. */
df7d75de 1949 if (split_code == MULT
230d793d
RS
1950 && GET_CODE (XEXP (*split, 1)) == CONST_INT
1951 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
1dc8a823
JW
1952 {
1953 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
1954 XEXP (*split, 0), GEN_INT (i)));
1955 /* Update split_code because we may not have a multiply
1956 anymore. */
1957 split_code = GET_CODE (*split);
1958 }
230d793d
RS
1959
1960#ifdef INSN_SCHEDULING
1961 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
1962 be written as a ZERO_EXTEND. */
df7d75de
RK
1963 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
1964 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
230d793d
RS
1965 XEXP (*split, 0)));
1966#endif
1967
1968 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
1969 SUBST (*split, newdest);
a29ca9db
RK
1970 i2_code_number
1971 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
df7d75de
RK
1972
1973 /* If the split point was a MULT and we didn't have one before,
1974 don't use one now. */
1975 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
a29ca9db
RK
1976 insn_code_number
1977 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
230d793d
RS
1978 }
1979 }
1980
1981 /* Check for a case where we loaded from memory in a narrow mode and
1982 then sign extended it, but we need both registers. In that case,
1983 we have a PARALLEL with both loads from the same memory location.
1984 We can split this into a load from memory followed by a register-register
1985 copy. This saves at least one insn, more if register allocation can
f0343c74
RK
1986 eliminate the copy.
1987
1988 We cannot do this if the destination of the second assignment is
1989 a register that we have already assumed is zero-extended. Similarly
1990 for a SUBREG of such a register. */
230d793d
RS
1991
1992 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
1993 && GET_CODE (newpat) == PARALLEL
1994 && XVECLEN (newpat, 0) == 2
1995 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1996 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
1997 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1998 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1999 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2000 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2001 INSN_CUID (i2))
2002 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2003 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
f0343c74
RK
2004 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2005 (GET_CODE (temp) == REG
2006 && reg_nonzero_bits[REGNO (temp)] != 0
2007 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2008 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2009 && (reg_nonzero_bits[REGNO (temp)]
2010 != GET_MODE_MASK (word_mode))))
2011 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2012 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2013 (GET_CODE (temp) == REG
2014 && reg_nonzero_bits[REGNO (temp)] != 0
2015 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2016 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2017 && (reg_nonzero_bits[REGNO (temp)]
2018 != GET_MODE_MASK (word_mode)))))
230d793d
RS
2019 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2020 SET_SRC (XVECEXP (newpat, 0, 1)))
2021 && ! find_reg_note (i3, REG_UNUSED,
2022 SET_DEST (XVECEXP (newpat, 0, 0))))
2023 {
472fbdd1
RK
2024 rtx ni2dest;
2025
230d793d 2026 newi2pat = XVECEXP (newpat, 0, 0);
472fbdd1 2027 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
230d793d
RS
2028 newpat = XVECEXP (newpat, 0, 1);
2029 SUBST (SET_SRC (newpat),
472fbdd1 2030 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
a29ca9db
RK
2031 i2_code_number
2032 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2033
230d793d 2034 if (i2_code_number >= 0)
a29ca9db
RK
2035 insn_code_number
2036 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
5089e22e
RS
2037
2038 if (insn_code_number >= 0)
2039 {
2040 rtx insn;
2041 rtx link;
2042
2043 /* If we will be able to accept this, we have made a change to the
2044 destination of I3. This can invalidate a LOG_LINKS pointing
2045 to I3. No other part of combine.c makes such a transformation.
2046
2047 The new I3 will have a destination that was previously the
2048 destination of I1 or I2 and which was used in i2 or I3. Call
2049 distribute_links to make a LOG_LINK from the next use of
2050 that destination. */
2051
2052 PATTERN (i3) = newpat;
5f4f0e22 2053 distribute_links (gen_rtx (INSN_LIST, VOIDmode, i3, NULL_RTX));
5089e22e
RS
2054
2055 /* I3 now uses what used to be its destination and which is
2056 now I2's destination. That means we need a LOG_LINK from
2057 I3 to I2. But we used to have one, so we still will.
2058
2059 However, some later insn might be using I2's dest and have
2060 a LOG_LINK pointing at I3. We must remove this link.
2061 The simplest way to remove the link is to point it at I1,
2062 which we know will be a NOTE. */
2063
2064 for (insn = NEXT_INSN (i3);
0d4d42c3
RK
2065 insn && (this_basic_block == n_basic_blocks - 1
2066 || insn != basic_block_head[this_basic_block + 1]);
5089e22e
RS
2067 insn = NEXT_INSN (insn))
2068 {
2069 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
472fbdd1 2070 && reg_referenced_p (ni2dest, PATTERN (insn)))
5089e22e
RS
2071 {
2072 for (link = LOG_LINKS (insn); link;
2073 link = XEXP (link, 1))
2074 if (XEXP (link, 0) == i3)
2075 XEXP (link, 0) = i1;
2076
2077 break;
2078 }
2079 }
2080 }
230d793d
RS
2081 }
2082
2083 /* Similarly, check for a case where we have a PARALLEL of two independent
2084 SETs but we started with three insns. In this case, we can do the sets
2085 as two separate insns. This case occurs when some SET allows two
2086 other insns to combine, but the destination of that SET is still live. */
2087
2088 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2089 && GET_CODE (newpat) == PARALLEL
2090 && XVECLEN (newpat, 0) == 2
2091 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2092 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2093 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2094 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2095 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2096 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2097 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2098 INSN_CUID (i2))
2099 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2100 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2101 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2102 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2103 XVECEXP (newpat, 0, 0))
2104 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2105 XVECEXP (newpat, 0, 1)))
2106 {
e9a25f70
JL
2107 /* Normally, it doesn't matter which of the two is done first,
2108 but it does if one references cc0. In that case, it has to
2109 be first. */
2110#ifdef HAVE_cc0
2111 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2112 {
2113 newi2pat = XVECEXP (newpat, 0, 0);
2114 newpat = XVECEXP (newpat, 0, 1);
2115 }
2116 else
2117#endif
2118 {
2119 newi2pat = XVECEXP (newpat, 0, 1);
2120 newpat = XVECEXP (newpat, 0, 0);
2121 }
230d793d 2122
a29ca9db
RK
2123 i2_code_number
2124 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2125
230d793d 2126 if (i2_code_number >= 0)
a29ca9db
RK
2127 insn_code_number
2128 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
230d793d
RS
2129 }
2130
2131 /* If it still isn't recognized, fail and change things back the way they
2132 were. */
2133 if ((insn_code_number < 0
2134 /* Is the result a reasonable ASM_OPERANDS? */
2135 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2136 {
2137 undo_all ();
2138 return 0;
2139 }
2140
2141 /* If we had to change another insn, make sure it is valid also. */
2142 if (undobuf.other_insn)
2143 {
230d793d
RS
2144 rtx other_pat = PATTERN (undobuf.other_insn);
2145 rtx new_other_notes;
2146 rtx note, next;
2147
6e25d159
RK
2148 CLEAR_HARD_REG_SET (newpat_used_regs);
2149
a29ca9db
RK
2150 other_code_number
2151 = recog_for_combine (&other_pat, undobuf.other_insn,
2152 &new_other_notes, &other_scratches);
230d793d
RS
2153
2154 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2155 {
2156 undo_all ();
2157 return 0;
2158 }
2159
2160 PATTERN (undobuf.other_insn) = other_pat;
2161
2162 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2163 are still valid. Then add any non-duplicate notes added by
2164 recog_for_combine. */
2165 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2166 {
2167 next = XEXP (note, 1);
2168
2169 if (REG_NOTE_KIND (note) == REG_UNUSED
2170 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
1a26b032
RK
2171 {
2172 if (GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 2173 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
1a26b032
RK
2174
2175 remove_note (undobuf.other_insn, note);
2176 }
230d793d
RS
2177 }
2178
1a26b032
RK
2179 for (note = new_other_notes; note; note = XEXP (note, 1))
2180 if (GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 2181 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
1a26b032 2182
230d793d 2183 distribute_notes (new_other_notes, undobuf.other_insn,
5f4f0e22 2184 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
230d793d
RS
2185 }
2186
2187 /* We now know that we can do this combination. Merge the insns and
2188 update the status of registers and LOG_LINKS. */
2189
2190 {
2191 rtx i3notes, i2notes, i1notes = 0;
2192 rtx i3links, i2links, i1links = 0;
2193 rtx midnotes = 0;
230d793d 2194 register int regno;
ff3467a9
JW
2195 /* Compute which registers we expect to eliminate. newi2pat may be setting
2196 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2197 same as i3dest, in which case newi2pat may be setting i1dest. */
2198 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2199 || i2dest_in_i2src || i2dest_in_i1src
230d793d 2200 ? 0 : i2dest);
ff3467a9
JW
2201 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2202 || (newi2pat && reg_set_p (i1dest, newi2pat))
2203 ? 0 : i1dest);
230d793d
RS
2204
2205 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2206 clear them. */
2207 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2208 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2209 if (i1)
2210 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2211
2212 /* Ensure that we do not have something that should not be shared but
2213 occurs multiple times in the new insns. Check this by first
5089e22e 2214 resetting all the `used' flags and then copying anything is shared. */
230d793d
RS
2215
2216 reset_used_flags (i3notes);
2217 reset_used_flags (i2notes);
2218 reset_used_flags (i1notes);
2219 reset_used_flags (newpat);
2220 reset_used_flags (newi2pat);
2221 if (undobuf.other_insn)
2222 reset_used_flags (PATTERN (undobuf.other_insn));
2223
2224 i3notes = copy_rtx_if_shared (i3notes);
2225 i2notes = copy_rtx_if_shared (i2notes);
2226 i1notes = copy_rtx_if_shared (i1notes);
2227 newpat = copy_rtx_if_shared (newpat);
2228 newi2pat = copy_rtx_if_shared (newi2pat);
2229 if (undobuf.other_insn)
2230 reset_used_flags (PATTERN (undobuf.other_insn));
2231
2232 INSN_CODE (i3) = insn_code_number;
2233 PATTERN (i3) = newpat;
2234 if (undobuf.other_insn)
2235 INSN_CODE (undobuf.other_insn) = other_code_number;
2236
2237 /* We had one special case above where I2 had more than one set and
2238 we replaced a destination of one of those sets with the destination
2239 of I3. In that case, we have to update LOG_LINKS of insns later
176c9e6b
JW
2240 in this basic block. Note that this (expensive) case is rare.
2241
2242 Also, in this case, we must pretend that all REG_NOTEs for I2
2243 actually came from I3, so that REG_UNUSED notes from I2 will be
2244 properly handled. */
2245
2246 if (i3_subst_into_i2)
2247 {
2248 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2249 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2250 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2251 && ! find_reg_note (i2, REG_UNUSED,
2252 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2253 for (temp = NEXT_INSN (i2);
2254 temp && (this_basic_block == n_basic_blocks - 1
2255 || basic_block_head[this_basic_block] != temp);
2256 temp = NEXT_INSN (temp))
2257 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2258 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2259 if (XEXP (link, 0) == i2)
2260 XEXP (link, 0) = i3;
2261
2262 if (i3notes)
2263 {
2264 rtx link = i3notes;
2265 while (XEXP (link, 1))
2266 link = XEXP (link, 1);
2267 XEXP (link, 1) = i2notes;
2268 }
2269 else
2270 i3notes = i2notes;
2271 i2notes = 0;
2272 }
230d793d
RS
2273
2274 LOG_LINKS (i3) = 0;
2275 REG_NOTES (i3) = 0;
2276 LOG_LINKS (i2) = 0;
2277 REG_NOTES (i2) = 0;
2278
2279 if (newi2pat)
2280 {
2281 INSN_CODE (i2) = i2_code_number;
2282 PATTERN (i2) = newi2pat;
2283 }
2284 else
2285 {
2286 PUT_CODE (i2, NOTE);
2287 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2288 NOTE_SOURCE_FILE (i2) = 0;
2289 }
2290
2291 if (i1)
2292 {
2293 LOG_LINKS (i1) = 0;
2294 REG_NOTES (i1) = 0;
2295 PUT_CODE (i1, NOTE);
2296 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2297 NOTE_SOURCE_FILE (i1) = 0;
2298 }
2299
2300 /* Get death notes for everything that is now used in either I3 or
6eb12cef
RK
2301 I2 and used to die in a previous insn. If we built two new
2302 patterns, move from I1 to I2 then I2 to I3 so that we get the
2303 proper movement on registers that I2 modifies. */
230d793d 2304
230d793d 2305 if (newi2pat)
6eb12cef
RK
2306 {
2307 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2308 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2309 }
2310 else
2311 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2312 i3, &midnotes);
230d793d
RS
2313
2314 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2315 if (i3notes)
5f4f0e22
CH
2316 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2317 elim_i2, elim_i1);
230d793d 2318 if (i2notes)
5f4f0e22
CH
2319 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2320 elim_i2, elim_i1);
230d793d 2321 if (i1notes)
5f4f0e22
CH
2322 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2323 elim_i2, elim_i1);
230d793d 2324 if (midnotes)
5f4f0e22
CH
2325 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2326 elim_i2, elim_i1);
230d793d
RS
2327
2328 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2329 know these are REG_UNUSED and want them to go to the desired insn,
1a26b032
RK
2330 so we always pass it as i3. We have not counted the notes in
2331 reg_n_deaths yet, so we need to do so now. */
2332
230d793d 2333 if (newi2pat && new_i2_notes)
1a26b032
RK
2334 {
2335 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2336 if (GET_CODE (XEXP (temp, 0)) == REG)
b1f21e0a 2337 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
1a26b032
RK
2338
2339 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2340 }
2341
230d793d 2342 if (new_i3_notes)
1a26b032
RK
2343 {
2344 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2345 if (GET_CODE (XEXP (temp, 0)) == REG)
b1f21e0a 2346 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
1a26b032
RK
2347
2348 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2349 }
230d793d
RS
2350
2351 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
e9a25f70
JL
2352 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2353 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2354 in that case, it might delete I2. Similarly for I2 and I1.
1a26b032
RK
2355 Show an additional death due to the REG_DEAD note we make here. If
2356 we discard it in distribute_notes, we will decrement it again. */
d0ab8cd3 2357
230d793d 2358 if (i3dest_killed)
1a26b032
RK
2359 {
2360 if (GET_CODE (i3dest_killed) == REG)
b1f21e0a 2361 REG_N_DEATHS (REGNO (i3dest_killed))++;
1a26b032 2362
e9a25f70
JL
2363 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2364 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
2365 NULL_RTX),
ff3467a9 2366 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
e9a25f70
JL
2367 else
2368 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
2369 NULL_RTX),
2370 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
ff3467a9 2371 elim_i2, elim_i1);
1a26b032 2372 }
58c8c593 2373
230d793d 2374 if (i2dest_in_i2src)
58c8c593 2375 {
1a26b032 2376 if (GET_CODE (i2dest) == REG)
b1f21e0a 2377 REG_N_DEATHS (REGNO (i2dest))++;
1a26b032 2378
58c8c593
RK
2379 if (newi2pat && reg_set_p (i2dest, newi2pat))
2380 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2381 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2382 else
2383 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2384 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2385 NULL_RTX, NULL_RTX);
2386 }
2387
230d793d 2388 if (i1dest_in_i1src)
58c8c593 2389 {
1a26b032 2390 if (GET_CODE (i1dest) == REG)
b1f21e0a 2391 REG_N_DEATHS (REGNO (i1dest))++;
1a26b032 2392
58c8c593
RK
2393 if (newi2pat && reg_set_p (i1dest, newi2pat))
2394 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2395 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2396 else
2397 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2398 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2399 NULL_RTX, NULL_RTX);
2400 }
230d793d
RS
2401
2402 distribute_links (i3links);
2403 distribute_links (i2links);
2404 distribute_links (i1links);
2405
2406 if (GET_CODE (i2dest) == REG)
2407 {
d0ab8cd3
RK
2408 rtx link;
2409 rtx i2_insn = 0, i2_val = 0, set;
2410
2411 /* The insn that used to set this register doesn't exist, and
2412 this life of the register may not exist either. See if one of
2413 I3's links points to an insn that sets I2DEST. If it does,
2414 that is now the last known value for I2DEST. If we don't update
2415 this and I2 set the register to a value that depended on its old
230d793d
RS
2416 contents, we will get confused. If this insn is used, thing
2417 will be set correctly in combine_instructions. */
d0ab8cd3
RK
2418
2419 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2420 if ((set = single_set (XEXP (link, 0))) != 0
2421 && rtx_equal_p (i2dest, SET_DEST (set)))
2422 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2423
2424 record_value_for_reg (i2dest, i2_insn, i2_val);
230d793d
RS
2425
2426 /* If the reg formerly set in I2 died only once and that was in I3,
2427 zero its use count so it won't make `reload' do any work. */
538fe8cd
ILT
2428 if (! added_sets_2
2429 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2430 && ! i2dest_in_i2src)
230d793d
RS
2431 {
2432 regno = REGNO (i2dest);
b1f21e0a
MM
2433 REG_N_SETS (regno)--;
2434 if (REG_N_SETS (regno) == 0
8e08106d 2435 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
b1f21e0a 2436 REG_N_REFS (regno) = 0;
230d793d
RS
2437 }
2438 }
2439
2440 if (i1 && GET_CODE (i1dest) == REG)
2441 {
d0ab8cd3
RK
2442 rtx link;
2443 rtx i1_insn = 0, i1_val = 0, set;
2444
2445 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2446 if ((set = single_set (XEXP (link, 0))) != 0
2447 && rtx_equal_p (i1dest, SET_DEST (set)))
2448 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2449
2450 record_value_for_reg (i1dest, i1_insn, i1_val);
2451
230d793d 2452 regno = REGNO (i1dest);
5af91171 2453 if (! added_sets_1 && ! i1dest_in_i1src)
230d793d 2454 {
b1f21e0a
MM
2455 REG_N_SETS (regno)--;
2456 if (REG_N_SETS (regno) == 0
8e08106d 2457 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
b1f21e0a 2458 REG_N_REFS (regno) = 0;
230d793d
RS
2459 }
2460 }
2461
951553af 2462 /* Update reg_nonzero_bits et al for any changes that may have been made
22609cbf
RK
2463 to this insn. */
2464
951553af 2465 note_stores (newpat, set_nonzero_bits_and_sign_copies);
22609cbf 2466 if (newi2pat)
951553af 2467 note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
22609cbf 2468
a29ca9db
RK
2469 /* If we added any (clobber (scratch)), add them to the max for a
2470 block. This is a very pessimistic calculation, since we might
2471 have had them already and this might not be the worst block, but
2472 it's not worth doing any better. */
2473 max_scratch += i3_scratches + i2_scratches + other_scratches;
2474
230d793d
RS
2475 /* If I3 is now an unconditional jump, ensure that it has a
2476 BARRIER following it since it may have initially been a
381ee8af 2477 conditional jump. It may also be the last nonnote insn. */
230d793d
RS
2478
2479 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
381ee8af
TW
2480 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2481 || GET_CODE (temp) != BARRIER))
230d793d
RS
2482 emit_barrier_after (i3);
2483 }
2484
2485 combine_successes++;
2486
bcd49eb7
JW
2487 /* Clear this here, so that subsequent get_last_value calls are not
2488 affected. */
2489 subst_prev_insn = NULL_RTX;
2490
abe6e52f
RK
2491 if (added_links_insn
2492 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2493 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2494 return added_links_insn;
2495 else
2496 return newi2pat ? i2 : i3;
230d793d
RS
2497}
2498\f
2499/* Undo all the modifications recorded in undobuf. */
2500
2501static void
2502undo_all ()
2503{
241cea85
RK
2504 struct undo *undo, *next;
2505
2506 for (undo = undobuf.undos; undo; undo = next)
7c046e4e 2507 {
241cea85
RK
2508 next = undo->next;
2509 if (undo->is_int)
2510 *undo->where.i = undo->old_contents.i;
7c046e4e 2511 else
241cea85
RK
2512 *undo->where.r = undo->old_contents.r;
2513
2514 undo->next = undobuf.frees;
2515 undobuf.frees = undo;
7c046e4e 2516 }
230d793d
RS
2517
2518 obfree (undobuf.storage);
845fc875 2519 undobuf.undos = undobuf.previous_undos = 0;
bcd49eb7
JW
2520
2521 /* Clear this here, so that subsequent get_last_value calls are not
2522 affected. */
2523 subst_prev_insn = NULL_RTX;
230d793d
RS
2524}
2525\f
2526/* Find the innermost point within the rtx at LOC, possibly LOC itself,
d0ab8cd3
RK
2527 where we have an arithmetic expression and return that point. LOC will
2528 be inside INSN.
230d793d
RS
2529
2530 try_combine will call this function to see if an insn can be split into
2531 two insns. */
2532
2533static rtx *
d0ab8cd3 2534find_split_point (loc, insn)
230d793d 2535 rtx *loc;
d0ab8cd3 2536 rtx insn;
230d793d
RS
2537{
2538 rtx x = *loc;
2539 enum rtx_code code = GET_CODE (x);
2540 rtx *split;
2541 int len = 0, pos, unsignedp;
2542 rtx inner;
2543
2544 /* First special-case some codes. */
2545 switch (code)
2546 {
2547 case SUBREG:
2548#ifdef INSN_SCHEDULING
2549 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2550 point. */
2551 if (GET_CODE (SUBREG_REG (x)) == MEM)
2552 return loc;
2553#endif
d0ab8cd3 2554 return find_split_point (&SUBREG_REG (x), insn);
230d793d 2555
230d793d 2556 case MEM:
916f14f1 2557#ifdef HAVE_lo_sum
230d793d
RS
2558 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2559 using LO_SUM and HIGH. */
2560 if (GET_CODE (XEXP (x, 0)) == CONST
2561 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2562 {
2563 SUBST (XEXP (x, 0),
2564 gen_rtx_combine (LO_SUM, Pmode,
2565 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2566 XEXP (x, 0)));
2567 return &XEXP (XEXP (x, 0), 0);
2568 }
230d793d
RS
2569#endif
2570
916f14f1
RK
2571 /* If we have a PLUS whose second operand is a constant and the
2572 address is not valid, perhaps will can split it up using
2573 the machine-specific way to split large constants. We use
ddd5a7c1 2574 the first pseudo-reg (one of the virtual regs) as a placeholder;
916f14f1
RK
2575 it will not remain in the result. */
2576 if (GET_CODE (XEXP (x, 0)) == PLUS
2577 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2578 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2579 {
2580 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2581 rtx seq = split_insns (gen_rtx (SET, VOIDmode, reg, XEXP (x, 0)),
2582 subst_insn);
2583
2584 /* This should have produced two insns, each of which sets our
2585 placeholder. If the source of the second is a valid address,
2586 we can make put both sources together and make a split point
2587 in the middle. */
2588
2589 if (seq && XVECLEN (seq, 0) == 2
2590 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2591 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2592 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2593 && ! reg_mentioned_p (reg,
2594 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2595 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2596 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2597 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2598 && memory_address_p (GET_MODE (x),
2599 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2600 {
2601 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2602 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2603
2604 /* Replace the placeholder in SRC2 with SRC1. If we can
2605 find where in SRC2 it was placed, that can become our
2606 split point and we can replace this address with SRC2.
2607 Just try two obvious places. */
2608
2609 src2 = replace_rtx (src2, reg, src1);
2610 split = 0;
2611 if (XEXP (src2, 0) == src1)
2612 split = &XEXP (src2, 0);
2613 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2614 && XEXP (XEXP (src2, 0), 0) == src1)
2615 split = &XEXP (XEXP (src2, 0), 0);
2616
2617 if (split)
2618 {
2619 SUBST (XEXP (x, 0), src2);
2620 return split;
2621 }
2622 }
1a26b032
RK
2623
2624 /* If that didn't work, perhaps the first operand is complex and
2625 needs to be computed separately, so make a split point there.
2626 This will occur on machines that just support REG + CONST
2627 and have a constant moved through some previous computation. */
2628
2629 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2630 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2631 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2632 == 'o')))
2633 return &XEXP (XEXP (x, 0), 0);
916f14f1
RK
2634 }
2635 break;
2636
230d793d
RS
2637 case SET:
2638#ifdef HAVE_cc0
2639 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2640 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2641 we need to put the operand into a register. So split at that
2642 point. */
2643
2644 if (SET_DEST (x) == cc0_rtx
2645 && GET_CODE (SET_SRC (x)) != COMPARE
2646 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2647 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2648 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2649 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2650 return &SET_SRC (x);
2651#endif
2652
2653 /* See if we can split SET_SRC as it stands. */
d0ab8cd3 2654 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2655 if (split && split != &SET_SRC (x))
2656 return split;
2657
041d7180
JL
2658 /* See if we can split SET_DEST as it stands. */
2659 split = find_split_point (&SET_DEST (x), insn);
2660 if (split && split != &SET_DEST (x))
2661 return split;
2662
230d793d
RS
2663 /* See if this is a bitfield assignment with everything constant. If
2664 so, this is an IOR of an AND, so split it into that. */
2665 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2666 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
5f4f0e22 2667 <= HOST_BITS_PER_WIDE_INT)
230d793d
RS
2668 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2669 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2670 && GET_CODE (SET_SRC (x)) == CONST_INT
2671 && ((INTVAL (XEXP (SET_DEST (x), 1))
2672 + INTVAL (XEXP (SET_DEST (x), 2)))
2673 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2674 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2675 {
2676 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2677 int len = INTVAL (XEXP (SET_DEST (x), 1));
2678 int src = INTVAL (SET_SRC (x));
2679 rtx dest = XEXP (SET_DEST (x), 0);
2680 enum machine_mode mode = GET_MODE (dest);
5f4f0e22 2681 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
230d793d 2682
f76b9db2
ILT
2683 if (BITS_BIG_ENDIAN)
2684 pos = GET_MODE_BITSIZE (mode) - len - pos;
230d793d
RS
2685
2686 if (src == mask)
2687 SUBST (SET_SRC (x),
5f4f0e22 2688 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
230d793d
RS
2689 else
2690 SUBST (SET_SRC (x),
2691 gen_binary (IOR, mode,
2692 gen_binary (AND, mode, dest,
5f4f0e22
CH
2693 GEN_INT (~ (mask << pos)
2694 & GET_MODE_MASK (mode))),
2695 GEN_INT (src << pos)));
230d793d
RS
2696
2697 SUBST (SET_DEST (x), dest);
2698
d0ab8cd3 2699 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2700 if (split && split != &SET_SRC (x))
2701 return split;
2702 }
2703
2704 /* Otherwise, see if this is an operation that we can split into two.
2705 If so, try to split that. */
2706 code = GET_CODE (SET_SRC (x));
2707
2708 switch (code)
2709 {
d0ab8cd3
RK
2710 case AND:
2711 /* If we are AND'ing with a large constant that is only a single
2712 bit and the result is only being used in a context where we
2713 need to know if it is zero or non-zero, replace it with a bit
2714 extraction. This will avoid the large constant, which might
2715 have taken more than one insn to make. If the constant were
2716 not a valid argument to the AND but took only one insn to make,
2717 this is no worse, but if it took more than one insn, it will
2718 be better. */
2719
2720 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2721 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2722 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2723 && GET_CODE (SET_DEST (x)) == REG
2724 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2725 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2726 && XEXP (*split, 0) == SET_DEST (x)
2727 && XEXP (*split, 1) == const0_rtx)
2728 {
76184def
DE
2729 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2730 XEXP (SET_SRC (x), 0),
2731 pos, NULL_RTX, 1, 1, 0, 0);
2732 if (extraction != 0)
2733 {
2734 SUBST (SET_SRC (x), extraction);
2735 return find_split_point (loc, insn);
2736 }
d0ab8cd3
RK
2737 }
2738 break;
2739
1a6ec070
RK
2740 case NE:
2741 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2742 is known to be on, this can be converted into a NEG of a shift. */
2743 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2744 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4eb2cb10 2745 && 1 <= (pos = exact_log2
1a6ec070
RK
2746 (nonzero_bits (XEXP (SET_SRC (x), 0),
2747 GET_MODE (XEXP (SET_SRC (x), 0))))))
2748 {
2749 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2750
2751 SUBST (SET_SRC (x),
2752 gen_rtx_combine (NEG, mode,
2753 gen_rtx_combine (LSHIFTRT, mode,
2754 XEXP (SET_SRC (x), 0),
4eb2cb10 2755 GEN_INT (pos))));
1a6ec070
RK
2756
2757 split = find_split_point (&SET_SRC (x), insn);
2758 if (split && split != &SET_SRC (x))
2759 return split;
2760 }
2761 break;
2762
230d793d
RS
2763 case SIGN_EXTEND:
2764 inner = XEXP (SET_SRC (x), 0);
101c1a3d
JL
2765
2766 /* We can't optimize if either mode is a partial integer
2767 mode as we don't know how many bits are significant
2768 in those modes. */
2769 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2770 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2771 break;
2772
230d793d
RS
2773 pos = 0;
2774 len = GET_MODE_BITSIZE (GET_MODE (inner));
2775 unsignedp = 0;
2776 break;
2777
2778 case SIGN_EXTRACT:
2779 case ZERO_EXTRACT:
2780 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2781 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2782 {
2783 inner = XEXP (SET_SRC (x), 0);
2784 len = INTVAL (XEXP (SET_SRC (x), 1));
2785 pos = INTVAL (XEXP (SET_SRC (x), 2));
2786
f76b9db2
ILT
2787 if (BITS_BIG_ENDIAN)
2788 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
230d793d
RS
2789 unsignedp = (code == ZERO_EXTRACT);
2790 }
2791 break;
e9a25f70
JL
2792
2793 default:
2794 break;
230d793d
RS
2795 }
2796
2797 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2798 {
2799 enum machine_mode mode = GET_MODE (SET_SRC (x));
2800
d0ab8cd3
RK
2801 /* For unsigned, we have a choice of a shift followed by an
2802 AND or two shifts. Use two shifts for field sizes where the
2803 constant might be too large. We assume here that we can
2804 always at least get 8-bit constants in an AND insn, which is
2805 true for every current RISC. */
2806
2807 if (unsignedp && len <= 8)
230d793d
RS
2808 {
2809 SUBST (SET_SRC (x),
2810 gen_rtx_combine
2811 (AND, mode,
2812 gen_rtx_combine (LSHIFTRT, mode,
2813 gen_lowpart_for_combine (mode, inner),
5f4f0e22
CH
2814 GEN_INT (pos)),
2815 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
230d793d 2816
d0ab8cd3 2817 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2818 if (split && split != &SET_SRC (x))
2819 return split;
2820 }
2821 else
2822 {
2823 SUBST (SET_SRC (x),
2824 gen_rtx_combine
d0ab8cd3 2825 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
230d793d
RS
2826 gen_rtx_combine (ASHIFT, mode,
2827 gen_lowpart_for_combine (mode, inner),
5f4f0e22
CH
2828 GEN_INT (GET_MODE_BITSIZE (mode)
2829 - len - pos)),
2830 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
230d793d 2831
d0ab8cd3 2832 split = find_split_point (&SET_SRC (x), insn);
230d793d
RS
2833 if (split && split != &SET_SRC (x))
2834 return split;
2835 }
2836 }
2837
2838 /* See if this is a simple operation with a constant as the second
2839 operand. It might be that this constant is out of range and hence
2840 could be used as a split point. */
2841 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2842 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2843 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2844 && CONSTANT_P (XEXP (SET_SRC (x), 1))
2845 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2846 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2847 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2848 == 'o'))))
2849 return &XEXP (SET_SRC (x), 1);
2850
2851 /* Finally, see if this is a simple operation with its first operand
2852 not in a register. The operation might require this operand in a
2853 register, so return it as a split point. We can always do this
2854 because if the first operand were another operation, we would have
2855 already found it as a split point. */
2856 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2857 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2858 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2859 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2860 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2861 return &XEXP (SET_SRC (x), 0);
2862
2863 return 0;
2864
2865 case AND:
2866 case IOR:
2867 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2868 it is better to write this as (not (ior A B)) so we can split it.
2869 Similarly for IOR. */
2870 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2871 {
2872 SUBST (*loc,
2873 gen_rtx_combine (NOT, GET_MODE (x),
2874 gen_rtx_combine (code == IOR ? AND : IOR,
2875 GET_MODE (x),
2876 XEXP (XEXP (x, 0), 0),
2877 XEXP (XEXP (x, 1), 0))));
d0ab8cd3 2878 return find_split_point (loc, insn);
230d793d
RS
2879 }
2880
2881 /* Many RISC machines have a large set of logical insns. If the
2882 second operand is a NOT, put it first so we will try to split the
2883 other operand first. */
2884 if (GET_CODE (XEXP (x, 1)) == NOT)
2885 {
2886 rtx tem = XEXP (x, 0);
2887 SUBST (XEXP (x, 0), XEXP (x, 1));
2888 SUBST (XEXP (x, 1), tem);
2889 }
2890 break;
e9a25f70
JL
2891
2892 default:
2893 break;
230d793d
RS
2894 }
2895
2896 /* Otherwise, select our actions depending on our rtx class. */
2897 switch (GET_RTX_CLASS (code))
2898 {
2899 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
2900 case '3':
d0ab8cd3 2901 split = find_split_point (&XEXP (x, 2), insn);
230d793d
RS
2902 if (split)
2903 return split;
0f41302f 2904 /* ... fall through ... */
230d793d
RS
2905 case '2':
2906 case 'c':
2907 case '<':
d0ab8cd3 2908 split = find_split_point (&XEXP (x, 1), insn);
230d793d
RS
2909 if (split)
2910 return split;
0f41302f 2911 /* ... fall through ... */
230d793d
RS
2912 case '1':
2913 /* Some machines have (and (shift ...) ...) insns. If X is not
2914 an AND, but XEXP (X, 0) is, use it as our split point. */
2915 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2916 return &XEXP (x, 0);
2917
d0ab8cd3 2918 split = find_split_point (&XEXP (x, 0), insn);
230d793d
RS
2919 if (split)
2920 return split;
2921 return loc;
2922 }
2923
2924 /* Otherwise, we don't have a split point. */
2925 return 0;
2926}
2927\f
2928/* Throughout X, replace FROM with TO, and return the result.
2929 The result is TO if X is FROM;
2930 otherwise the result is X, but its contents may have been modified.
2931 If they were modified, a record was made in undobuf so that
2932 undo_all will (among other things) return X to its original state.
2933
2934 If the number of changes necessary is too much to record to undo,
2935 the excess changes are not made, so the result is invalid.
2936 The changes already made can still be undone.
2937 undobuf.num_undo is incremented for such changes, so by testing that
2938 the caller can tell whether the result is valid.
2939
2940 `n_occurrences' is incremented each time FROM is replaced.
2941
2942 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2943
5089e22e 2944 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
230d793d
RS
2945 by copying if `n_occurrences' is non-zero. */
2946
2947static rtx
2948subst (x, from, to, in_dest, unique_copy)
2949 register rtx x, from, to;
2950 int in_dest;
2951 int unique_copy;
2952{
f24ad0e4 2953 register enum rtx_code code = GET_CODE (x);
230d793d 2954 enum machine_mode op0_mode = VOIDmode;
8079805d
RK
2955 register char *fmt;
2956 register int len, i;
2957 rtx new;
230d793d
RS
2958
2959/* Two expressions are equal if they are identical copies of a shared
2960 RTX or if they are both registers with the same register number
2961 and mode. */
2962
2963#define COMBINE_RTX_EQUAL_P(X,Y) \
2964 ((X) == (Y) \
2965 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
2966 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
2967
2968 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
2969 {
2970 n_occurrences++;
2971 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
2972 }
2973
2974 /* If X and FROM are the same register but different modes, they will
2975 not have been seen as equal above. However, flow.c will make a
2976 LOG_LINKS entry for that case. If we do nothing, we will try to
2977 rerecognize our original insn and, when it succeeds, we will
2978 delete the feeding insn, which is incorrect.
2979
2980 So force this insn not to match in this (rare) case. */
2981 if (! in_dest && code == REG && GET_CODE (from) == REG
2982 && REGNO (x) == REGNO (from))
2983 return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
2984
2985 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
2986 of which may contain things that can be combined. */
2987 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
2988 return x;
2989
2990 /* It is possible to have a subexpression appear twice in the insn.
2991 Suppose that FROM is a register that appears within TO.
2992 Then, after that subexpression has been scanned once by `subst',
2993 the second time it is scanned, TO may be found. If we were
2994 to scan TO here, we would find FROM within it and create a
2995 self-referent rtl structure which is completely wrong. */
2996 if (COMBINE_RTX_EQUAL_P (x, to))
2997 return to;
2998
2999 len = GET_RTX_LENGTH (code);
3000 fmt = GET_RTX_FORMAT (code);
3001
3002 /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
3003 set up to skip this common case. All other cases where we want to
3004 suppress replacing something inside a SET_SRC are handled via the
3005 IN_DEST operand. */
3006 if (code == SET
3007 && (GET_CODE (SET_DEST (x)) == REG
3008 || GET_CODE (SET_DEST (x)) == CC0
3009 || GET_CODE (SET_DEST (x)) == PC))
3010 fmt = "ie";
3011
0f41302f
MS
3012 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3013 constant. */
230d793d
RS
3014 if (fmt[0] == 'e')
3015 op0_mode = GET_MODE (XEXP (x, 0));
3016
3017 for (i = 0; i < len; i++)
3018 {
3019 if (fmt[i] == 'E')
3020 {
3021 register int j;
3022 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3023 {
230d793d
RS
3024 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3025 {
3026 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3027 n_occurrences++;
3028 }
3029 else
3030 {
3031 new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
3032
3033 /* If this substitution failed, this whole thing fails. */
3034 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3035 return new;
3036 }
3037
3038 SUBST (XVECEXP (x, i, j), new);
3039 }
3040 }
3041 else if (fmt[i] == 'e')
3042 {
230d793d
RS
3043 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3044 {
42301240
RK
3045 /* In general, don't install a subreg involving two modes not
3046 tieable. It can worsen register allocation, and can even
3047 make invalid reload insns, since the reg inside may need to
3048 be copied from in the outside mode, and that may be invalid
3049 if it is an fp reg copied in integer mode.
3050
3051 We allow two exceptions to this: It is valid if it is inside
3052 another SUBREG and the mode of that SUBREG and the mode of
3053 the inside of TO is tieable and it is valid if X is a SET
3054 that copies FROM to CC0. */
3055 if (GET_CODE (to) == SUBREG
3056 && ! MODES_TIEABLE_P (GET_MODE (to),
3057 GET_MODE (SUBREG_REG (to)))
3058 && ! (code == SUBREG
8079805d
RK
3059 && MODES_TIEABLE_P (GET_MODE (x),
3060 GET_MODE (SUBREG_REG (to))))
42301240
RK
3061#ifdef HAVE_cc0
3062 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3063#endif
3064 )
3065 return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
3066
230d793d
RS
3067 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3068 n_occurrences++;
3069 }
3070 else
3071 /* If we are in a SET_DEST, suppress most cases unless we
3072 have gone inside a MEM, in which case we want to
3073 simplify the address. We assume here that things that
3074 are actually part of the destination have their inner
3075 parts in the first expression. This is true for SUBREG,
3076 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3077 things aside from REG and MEM that should appear in a
3078 SET_DEST. */
3079 new = subst (XEXP (x, i), from, to,
3080 (((in_dest
3081 && (code == SUBREG || code == STRICT_LOW_PART
3082 || code == ZERO_EXTRACT))
3083 || code == SET)
3084 && i == 0), unique_copy);
3085
3086 /* If we found that we will have to reject this combination,
3087 indicate that by returning the CLOBBER ourselves, rather than
3088 an expression containing it. This will speed things up as
3089 well as prevent accidents where two CLOBBERs are considered
3090 to be equal, thus producing an incorrect simplification. */
3091
3092 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3093 return new;
3094
3095 SUBST (XEXP (x, i), new);
3096 }
3097 }
3098
8079805d
RK
3099 /* Try to simplify X. If the simplification changed the code, it is likely
3100 that further simplification will help, so loop, but limit the number
3101 of repetitions that will be performed. */
3102
3103 for (i = 0; i < 4; i++)
3104 {
3105 /* If X is sufficiently simple, don't bother trying to do anything
3106 with it. */
3107 if (code != CONST_INT && code != REG && code != CLOBBER)
3108 x = simplify_rtx (x, op0_mode, i == 3, in_dest);
d0ab8cd3 3109
8079805d
RK
3110 if (GET_CODE (x) == code)
3111 break;
d0ab8cd3 3112
8079805d 3113 code = GET_CODE (x);
eeb43d32 3114
8079805d
RK
3115 /* We no longer know the original mode of operand 0 since we
3116 have changed the form of X) */
3117 op0_mode = VOIDmode;
3118 }
eeb43d32 3119
8079805d
RK
3120 return x;
3121}
3122\f
3123/* Simplify X, a piece of RTL. We just operate on the expression at the
3124 outer level; call `subst' to simplify recursively. Return the new
3125 expression.
3126
3127 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3128 will be the iteration even if an expression with a code different from
3129 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
eeb43d32 3130
8079805d
RK
3131static rtx
3132simplify_rtx (x, op0_mode, last, in_dest)
3133 rtx x;
3134 enum machine_mode op0_mode;
3135 int last;
3136 int in_dest;
3137{
3138 enum rtx_code code = GET_CODE (x);
3139 enum machine_mode mode = GET_MODE (x);
3140 rtx temp;
3141 int i;
d0ab8cd3 3142
230d793d
RS
3143 /* If this is a commutative operation, put a constant last and a complex
3144 expression first. We don't need to do this for comparisons here. */
3145 if (GET_RTX_CLASS (code) == 'c'
3146 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3147 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3148 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3149 || (GET_CODE (XEXP (x, 0)) == SUBREG
3150 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3151 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3152 {
3153 temp = XEXP (x, 0);
3154 SUBST (XEXP (x, 0), XEXP (x, 1));
3155 SUBST (XEXP (x, 1), temp);
3156 }
3157
22609cbf
RK
3158 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3159 sign extension of a PLUS with a constant, reverse the order of the sign
3160 extension and the addition. Note that this not the same as the original
3161 code, but overflow is undefined for signed values. Also note that the
3162 PLUS will have been partially moved "inside" the sign-extension, so that
3163 the first operand of X will really look like:
3164 (ashiftrt (plus (ashift A C4) C5) C4).
3165 We convert this to
3166 (plus (ashiftrt (ashift A C4) C2) C4)
3167 and replace the first operand of X with that expression. Later parts
3168 of this function may simplify the expression further.
3169
3170 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3171 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3172 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3173
3174 We do this to simplify address expressions. */
3175
3176 if ((code == PLUS || code == MINUS || code == MULT)
3177 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3178 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3179 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3180 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3181 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3182 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3183 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3184 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3185 XEXP (XEXP (XEXP (x, 0), 0), 1),
3186 XEXP (XEXP (x, 0), 1))) != 0)
3187 {
3188 rtx new
3189 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3190 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3191 INTVAL (XEXP (XEXP (x, 0), 1)));
3192
3193 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3194 INTVAL (XEXP (XEXP (x, 0), 1)));
3195
3196 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3197 }
3198
d0ab8cd3
RK
3199 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3200 applying it to the arms of the IF_THEN_ELSE. This often simplifies
abe6e52f
RK
3201 things. Check for cases where both arms are testing the same
3202 condition.
3203
3204 Don't do anything if all operands are very simple. */
3205
3206 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3207 || GET_RTX_CLASS (code) == '<')
3208 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3209 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3210 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3211 == 'o')))
3212 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3213 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3214 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3215 == 'o')))))
3216 || (GET_RTX_CLASS (code) == '1'
3217 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3218 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3219 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3220 == 'o'))))))
d0ab8cd3 3221 {
abe6e52f
RK
3222 rtx cond, true, false;
3223
3224 cond = if_then_else_cond (x, &true, &false);
0802d516
RK
3225 if (cond != 0
3226 /* If everything is a comparison, what we have is highly unlikely
3227 to be simpler, so don't use it. */
3228 && ! (GET_RTX_CLASS (code) == '<'
3229 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3230 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
abe6e52f
RK
3231 {
3232 rtx cop1 = const0_rtx;
3233 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3234
15448afc
RK
3235 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3236 return x;
3237
9210df58
RK
3238 /* Simplify the alternative arms; this may collapse the true and
3239 false arms to store-flag values. */
3240 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3241 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3242
3243 /* Restarting if we generate a store-flag expression will cause
3244 us to loop. Just drop through in this case. */
3245
abe6e52f
RK
3246 /* If the result values are STORE_FLAG_VALUE and zero, we can
3247 just make the comparison operation. */
3248 if (true == const_true_rtx && false == const0_rtx)
3249 x = gen_binary (cond_code, mode, cond, cop1);
3250 else if (true == const0_rtx && false == const_true_rtx)
3251 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3252
3253 /* Likewise, we can make the negate of a comparison operation
3254 if the result values are - STORE_FLAG_VALUE and zero. */
3255 else if (GET_CODE (true) == CONST_INT
3256 && INTVAL (true) == - STORE_FLAG_VALUE
3257 && false == const0_rtx)
0c1c8ea6 3258 x = gen_unary (NEG, mode, mode,
abe6e52f
RK
3259 gen_binary (cond_code, mode, cond, cop1));
3260 else if (GET_CODE (false) == CONST_INT
3261 && INTVAL (false) == - STORE_FLAG_VALUE
3262 && true == const0_rtx)
0c1c8ea6 3263 x = gen_unary (NEG, mode, mode,
abe6e52f
RK
3264 gen_binary (reverse_condition (cond_code),
3265 mode, cond, cop1));
3266 else
8079805d
RK
3267 return gen_rtx (IF_THEN_ELSE, mode,
3268 gen_binary (cond_code, VOIDmode, cond, cop1),
3269 true, false);
5109d49f 3270
9210df58
RK
3271 code = GET_CODE (x);
3272 op0_mode = VOIDmode;
abe6e52f 3273 }
d0ab8cd3
RK
3274 }
3275
230d793d
RS
3276 /* Try to fold this expression in case we have constants that weren't
3277 present before. */
3278 temp = 0;
3279 switch (GET_RTX_CLASS (code))
3280 {
3281 case '1':
3282 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3283 break;
3284 case '<':
3285 temp = simplify_relational_operation (code, op0_mode,
3286 XEXP (x, 0), XEXP (x, 1));
77fa0940
RK
3287#ifdef FLOAT_STORE_FLAG_VALUE
3288 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3289 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3290 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3291#endif
230d793d
RS
3292 break;
3293 case 'c':
3294 case '2':
3295 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3296 break;
3297 case 'b':
3298 case '3':
3299 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3300 XEXP (x, 1), XEXP (x, 2));
3301 break;
3302 }
3303
3304 if (temp)
d0ab8cd3 3305 x = temp, code = GET_CODE (temp);
230d793d 3306
230d793d 3307 /* First see if we can apply the inverse distributive law. */
224eeff2
RK
3308 if (code == PLUS || code == MINUS
3309 || code == AND || code == IOR || code == XOR)
230d793d
RS
3310 {
3311 x = apply_distributive_law (x);
3312 code = GET_CODE (x);
3313 }
3314
3315 /* If CODE is an associative operation not otherwise handled, see if we
3316 can associate some operands. This can win if they are constants or
3317 if they are logically related (i.e. (a & b) & a. */
3318 if ((code == PLUS || code == MINUS
3319 || code == MULT || code == AND || code == IOR || code == XOR
3320 || code == DIV || code == UDIV
3321 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3ad2180a 3322 && INTEGRAL_MODE_P (mode))
230d793d
RS
3323 {
3324 if (GET_CODE (XEXP (x, 0)) == code)
3325 {
3326 rtx other = XEXP (XEXP (x, 0), 0);
3327 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3328 rtx inner_op1 = XEXP (x, 1);
3329 rtx inner;
3330
3331 /* Make sure we pass the constant operand if any as the second
3332 one if this is a commutative operation. */
3333 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3334 {
3335 rtx tem = inner_op0;
3336 inner_op0 = inner_op1;
3337 inner_op1 = tem;
3338 }
3339 inner = simplify_binary_operation (code == MINUS ? PLUS
3340 : code == DIV ? MULT
3341 : code == UDIV ? MULT
3342 : code,
3343 mode, inner_op0, inner_op1);
3344
3345 /* For commutative operations, try the other pair if that one
3346 didn't simplify. */
3347 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3348 {
3349 other = XEXP (XEXP (x, 0), 1);
3350 inner = simplify_binary_operation (code, mode,
3351 XEXP (XEXP (x, 0), 0),
3352 XEXP (x, 1));
3353 }
3354
3355 if (inner)
8079805d 3356 return gen_binary (code, mode, other, inner);
230d793d
RS
3357 }
3358 }
3359
3360 /* A little bit of algebraic simplification here. */
3361 switch (code)
3362 {
3363 case MEM:
3364 /* Ensure that our address has any ASHIFTs converted to MULT in case
3365 address-recognizing predicates are called later. */
3366 temp = make_compound_operation (XEXP (x, 0), MEM);
3367 SUBST (XEXP (x, 0), temp);
3368 break;
3369
3370 case SUBREG:
3371 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3372 is paradoxical. If we can't do that safely, then it becomes
3373 something nonsensical so that this combination won't take place. */
3374
3375 if (GET_CODE (SUBREG_REG (x)) == MEM
3376 && (GET_MODE_SIZE (mode)
3377 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3378 {
3379 rtx inner = SUBREG_REG (x);
3380 int endian_offset = 0;
3381 /* Don't change the mode of the MEM
3382 if that would change the meaning of the address. */
3383 if (MEM_VOLATILE_P (SUBREG_REG (x))
3384 || mode_dependent_address_p (XEXP (inner, 0)))
3385 return gen_rtx (CLOBBER, mode, const0_rtx);
3386
f76b9db2
ILT
3387 if (BYTES_BIG_ENDIAN)
3388 {
3389 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3390 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3391 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3392 endian_offset -= (UNITS_PER_WORD
3393 - GET_MODE_SIZE (GET_MODE (inner)));
3394 }
230d793d
RS
3395 /* Note if the plus_constant doesn't make a valid address
3396 then this combination won't be accepted. */
3397 x = gen_rtx (MEM, mode,
3398 plus_constant (XEXP (inner, 0),
3399 (SUBREG_WORD (x) * UNITS_PER_WORD
3400 + endian_offset)));
3401 MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3402 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3403 MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3404 return x;
3405 }
3406
3407 /* If we are in a SET_DEST, these other cases can't apply. */
3408 if (in_dest)
3409 return x;
3410
3411 /* Changing mode twice with SUBREG => just change it once,
3412 or not at all if changing back to starting mode. */
3413 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3414 {
3415 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3416 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3417 return SUBREG_REG (SUBREG_REG (x));
3418
3419 SUBST_INT (SUBREG_WORD (x),
3420 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3421 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3422 }
3423
3424 /* SUBREG of a hard register => just change the register number
3425 and/or mode. If the hard register is not valid in that mode,
26ecfc76
RK
3426 suppress this combination. If the hard register is the stack,
3427 frame, or argument pointer, leave this as a SUBREG. */
230d793d
RS
3428
3429 if (GET_CODE (SUBREG_REG (x)) == REG
26ecfc76
RK
3430 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3431 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
6d7096b0
DE
3432#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3433 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3434#endif
26ecfc76
RK
3435#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3436 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3437#endif
3438 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
230d793d
RS
3439 {
3440 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3441 mode))
3442 return gen_rtx (REG, mode,
3443 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3444 else
3445 return gen_rtx (CLOBBER, mode, const0_rtx);
3446 }
3447
3448 /* For a constant, try to pick up the part we want. Handle a full
a4bde0b1
RK
3449 word and low-order part. Only do this if we are narrowing
3450 the constant; if it is being widened, we have no idea what
3451 the extra bits will have been set to. */
230d793d
RS
3452
3453 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3454 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3c99d5ff 3455 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
230d793d
RS
3456 && GET_MODE_CLASS (mode) == MODE_INT)
3457 {
3458 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
5f4f0e22 3459 0, op0_mode);
230d793d
RS
3460 if (temp)
3461 return temp;
3462 }
3463
19808e22
RS
3464 /* If we want a subreg of a constant, at offset 0,
3465 take the low bits. On a little-endian machine, that's
3466 always valid. On a big-endian machine, it's valid
3c99d5ff 3467 only if the constant's mode fits in one word. Note that we
61b1bece 3468 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3c99d5ff
RK
3469 if (CONSTANT_P (SUBREG_REG (x))
3470 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3471 || ! WORDS_BIG_ENDIAN)
3472 ? SUBREG_WORD (x) == 0
3473 : (SUBREG_WORD (x)
3474 == ((GET_MODE_SIZE (op0_mode)
3475 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3476 / UNITS_PER_WORD)))
f82da7d2 3477 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
f76b9db2
ILT
3478 && (! WORDS_BIG_ENDIAN
3479 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
230d793d
RS
3480 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3481
b65c1b5b
RK
3482 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3483 since we are saying that the high bits don't matter. */
3484 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3485 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3486 return SUBREG_REG (x);
3487
87e3e0c1
RK
3488 /* Note that we cannot do any narrowing for non-constants since
3489 we might have been counting on using the fact that some bits were
3490 zero. We now do this in the SET. */
3491
230d793d
RS
3492 break;
3493
3494 case NOT:
3495 /* (not (plus X -1)) can become (neg X). */
3496 if (GET_CODE (XEXP (x, 0)) == PLUS
3497 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
8079805d 3498 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
230d793d
RS
3499
3500 /* Similarly, (not (neg X)) is (plus X -1). */
3501 if (GET_CODE (XEXP (x, 0)) == NEG)
8079805d
RK
3502 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3503 constm1_rtx);
230d793d 3504
d0ab8cd3
RK
3505 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3506 if (GET_CODE (XEXP (x, 0)) == XOR
3507 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3508 && (temp = simplify_unary_operation (NOT, mode,
3509 XEXP (XEXP (x, 0), 1),
3510 mode)) != 0)
787745f5 3511 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
d0ab8cd3 3512
230d793d
RS
3513 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3514 other than 1, but that is not valid. We could do a similar
3515 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3516 but this doesn't seem common enough to bother with. */
3517 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3518 && XEXP (XEXP (x, 0), 0) == const1_rtx)
0c1c8ea6 3519 return gen_rtx (ROTATE, mode, gen_unary (NOT, mode, mode, const1_rtx),
8079805d 3520 XEXP (XEXP (x, 0), 1));
230d793d
RS
3521
3522 if (GET_CODE (XEXP (x, 0)) == SUBREG
3523 && subreg_lowpart_p (XEXP (x, 0))
3524 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3525 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3526 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3527 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3528 {
3529 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3530
3531 x = gen_rtx (ROTATE, inner_mode,
0c1c8ea6 3532 gen_unary (NOT, inner_mode, inner_mode, const1_rtx),
230d793d 3533 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
8079805d 3534 return gen_lowpart_for_combine (mode, x);
230d793d
RS
3535 }
3536
0802d516
RK
3537 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3538 reversing the comparison code if valid. */
3539 if (STORE_FLAG_VALUE == -1
3540 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
230d793d
RS
3541 && reversible_comparison_p (XEXP (x, 0)))
3542 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3543 mode, XEXP (XEXP (x, 0), 0),
3544 XEXP (XEXP (x, 0), 1));
500c518b
RK
3545
3546 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
0802d516
RK
3547 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3548 perform the above simplification. */
500c518b 3549
0802d516
RK
3550 if (STORE_FLAG_VALUE == -1
3551 && XEXP (x, 1) == const1_rtx
500c518b
RK
3552 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3553 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3554 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3555 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
230d793d
RS
3556
3557 /* Apply De Morgan's laws to reduce number of patterns for machines
3558 with negating logical insns (and-not, nand, etc.). If result has
3559 only one NOT, put it first, since that is how the patterns are
3560 coded. */
3561
3562 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3563 {
3564 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3565
3566 if (GET_CODE (in1) == NOT)
3567 in1 = XEXP (in1, 0);
3568 else
3569 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3570
3571 if (GET_CODE (in2) == NOT)
3572 in2 = XEXP (in2, 0);
3573 else if (GET_CODE (in2) == CONST_INT
5f4f0e22
CH
3574 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3575 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
230d793d
RS
3576 else
3577 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3578
3579 if (GET_CODE (in2) == NOT)
3580 {
3581 rtx tem = in2;
3582 in2 = in1; in1 = tem;
3583 }
3584
8079805d
RK
3585 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3586 mode, in1, in2);
230d793d
RS
3587 }
3588 break;
3589
3590 case NEG:
3591 /* (neg (plus X 1)) can become (not X). */
3592 if (GET_CODE (XEXP (x, 0)) == PLUS
3593 && XEXP (XEXP (x, 0), 1) == const1_rtx)
8079805d 3594 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
230d793d
RS
3595
3596 /* Similarly, (neg (not X)) is (plus X 1). */
3597 if (GET_CODE (XEXP (x, 0)) == NOT)
8079805d 3598 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
230d793d 3599
230d793d
RS
3600 /* (neg (minus X Y)) can become (minus Y X). */
3601 if (GET_CODE (XEXP (x, 0)) == MINUS
3ad2180a 3602 && (! FLOAT_MODE_P (mode)
0f41302f 3603 /* x-y != -(y-x) with IEEE floating point. */
7e2a0d8e
RK
3604 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3605 || flag_fast_math))
8079805d
RK
3606 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3607 XEXP (XEXP (x, 0), 0));
230d793d 3608
0f41302f 3609 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
d0ab8cd3 3610 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
951553af 3611 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
8079805d 3612 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
d0ab8cd3 3613
230d793d
RS
3614 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3615 if we can then eliminate the NEG (e.g.,
3616 if the operand is a constant). */
3617
3618 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3619 {
3620 temp = simplify_unary_operation (NEG, mode,
3621 XEXP (XEXP (x, 0), 0), mode);
3622 if (temp)
3623 {
3624 SUBST (XEXP (XEXP (x, 0), 0), temp);
3625 return XEXP (x, 0);
3626 }
3627 }
3628
3629 temp = expand_compound_operation (XEXP (x, 0));
3630
3631 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3632 replaced by (lshiftrt X C). This will convert
3633 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3634
3635 if (GET_CODE (temp) == ASHIFTRT
3636 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3637 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
8079805d
RK
3638 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3639 INTVAL (XEXP (temp, 1)));
230d793d 3640
951553af 3641 /* If X has only a single bit that might be nonzero, say, bit I, convert
230d793d
RS
3642 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3643 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3644 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3645 or a SUBREG of one since we'd be making the expression more
3646 complex if it was just a register. */
3647
3648 if (GET_CODE (temp) != REG
3649 && ! (GET_CODE (temp) == SUBREG
3650 && GET_CODE (SUBREG_REG (temp)) == REG)
951553af 3651 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
230d793d
RS
3652 {
3653 rtx temp1 = simplify_shift_const
5f4f0e22
CH
3654 (NULL_RTX, ASHIFTRT, mode,
3655 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
230d793d
RS
3656 GET_MODE_BITSIZE (mode) - 1 - i),
3657 GET_MODE_BITSIZE (mode) - 1 - i);
3658
3659 /* If all we did was surround TEMP with the two shifts, we
3660 haven't improved anything, so don't use it. Otherwise,
3661 we are better off with TEMP1. */
3662 if (GET_CODE (temp1) != ASHIFTRT
3663 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3664 || XEXP (XEXP (temp1, 0), 0) != temp)
8079805d 3665 return temp1;
230d793d
RS
3666 }
3667 break;
3668
2ca9ae17 3669 case TRUNCATE:
e30fb98f
JL
3670 /* We can't handle truncation to a partial integer mode here
3671 because we don't know the real bitsize of the partial
3672 integer mode. */
3673 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3674 break;
3675
2ca9ae17
JW
3676 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3677 SUBST (XEXP (x, 0),
3678 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3679 GET_MODE_MASK (mode), NULL_RTX, 0));
0f13a422
ILT
3680
3681 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3682 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3683 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3684 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3685 return XEXP (XEXP (x, 0), 0);
3686
3687 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3688 (OP:SI foo:SI) if OP is NEG or ABS. */
3689 if ((GET_CODE (XEXP (x, 0)) == ABS
3690 || GET_CODE (XEXP (x, 0)) == NEG)
3691 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3692 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3693 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3694 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3695 XEXP (XEXP (XEXP (x, 0), 0), 0));
3696
3697 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3698 (truncate:SI x). */
3699 if (GET_CODE (XEXP (x, 0)) == SUBREG
3700 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3701 && subreg_lowpart_p (XEXP (x, 0)))
3702 return SUBREG_REG (XEXP (x, 0));
3703
3704 /* If we know that the value is already truncated, we can
3705 replace the TRUNCATE with a SUBREG. */
3706 if (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) <= HOST_BITS_PER_WIDE_INT
3707 && (nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3708 &~ GET_MODE_MASK (mode)) == 0)
3709 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3710
3711 /* A truncate of a comparison can be replaced with a subreg if
3712 STORE_FLAG_VALUE permits. This is like the previous test,
3713 but it works even if the comparison is done in a mode larger
3714 than HOST_BITS_PER_WIDE_INT. */
3715 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3716 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3717 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3718 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3719
3720 /* Similarly, a truncate of a register whose value is a
3721 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3722 permits. */
3723 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3724 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3725 && (temp = get_last_value (XEXP (x, 0)))
3726 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3727 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3728
2ca9ae17
JW
3729 break;
3730
230d793d
RS
3731 case FLOAT_TRUNCATE:
3732 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3733 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3734 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3735 return XEXP (XEXP (x, 0), 0);
4635f748
RK
3736
3737 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3738 (OP:SF foo:SF) if OP is NEG or ABS. */
3739 if ((GET_CODE (XEXP (x, 0)) == ABS
3740 || GET_CODE (XEXP (x, 0)) == NEG)
3741 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3742 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
0c1c8ea6
RK
3743 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3744 XEXP (XEXP (XEXP (x, 0), 0), 0));
1d12df72
RK
3745
3746 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3747 is (float_truncate:SF x). */
3748 if (GET_CODE (XEXP (x, 0)) == SUBREG
3749 && subreg_lowpart_p (XEXP (x, 0))
3750 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3751 return SUBREG_REG (XEXP (x, 0));
230d793d
RS
3752 break;
3753
3754#ifdef HAVE_cc0
3755 case COMPARE:
3756 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3757 using cc0, in which case we want to leave it as a COMPARE
3758 so we can distinguish it from a register-register-copy. */
3759 if (XEXP (x, 1) == const0_rtx)
3760 return XEXP (x, 0);
3761
3762 /* In IEEE floating point, x-0 is not the same as x. */
3763 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7e2a0d8e
RK
3764 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3765 || flag_fast_math)
230d793d
RS
3766 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3767 return XEXP (x, 0);
3768 break;
3769#endif
3770
3771 case CONST:
3772 /* (const (const X)) can become (const X). Do it this way rather than
3773 returning the inner CONST since CONST can be shared with a
3774 REG_EQUAL note. */
3775 if (GET_CODE (XEXP (x, 0)) == CONST)
3776 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3777 break;
3778
3779#ifdef HAVE_lo_sum
3780 case LO_SUM:
3781 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
3782 can add in an offset. find_split_point will split this address up
3783 again if it doesn't match. */
3784 if (GET_CODE (XEXP (x, 0)) == HIGH
3785 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3786 return XEXP (x, 1);
3787 break;
3788#endif
3789
3790 case PLUS:
3791 /* If we have (plus (plus (A const) B)), associate it so that CONST is
3792 outermost. That's because that's the way indexed addresses are
3793 supposed to appear. This code used to check many more cases, but
3794 they are now checked elsewhere. */
3795 if (GET_CODE (XEXP (x, 0)) == PLUS
3796 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3797 return gen_binary (PLUS, mode,
3798 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3799 XEXP (x, 1)),
3800 XEXP (XEXP (x, 0), 1));
3801
3802 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3803 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3804 bit-field and can be replaced by either a sign_extend or a
3805 sign_extract. The `and' may be a zero_extend. */
3806 if (GET_CODE (XEXP (x, 0)) == XOR
3807 && GET_CODE (XEXP (x, 1)) == CONST_INT
3808 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3809 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3810 && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5f4f0e22 3811 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
230d793d
RS
3812 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3813 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3814 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5f4f0e22 3815 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
230d793d
RS
3816 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3817 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3818 == i + 1))))
8079805d
RK
3819 return simplify_shift_const
3820 (NULL_RTX, ASHIFTRT, mode,
3821 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3822 XEXP (XEXP (XEXP (x, 0), 0), 0),
3823 GET_MODE_BITSIZE (mode) - (i + 1)),
3824 GET_MODE_BITSIZE (mode) - (i + 1));
230d793d 3825
bc0776c6
RK
3826 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3827 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3828 is 1. This produces better code than the alternative immediately
3829 below. */
3830 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3831 && reversible_comparison_p (XEXP (x, 0))
3832 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3833 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
8079805d 3834 return
0c1c8ea6 3835 gen_unary (NEG, mode, mode,
8079805d
RK
3836 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3837 mode, XEXP (XEXP (x, 0), 0),
3838 XEXP (XEXP (x, 0), 1)));
bc0776c6
RK
3839
3840 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
230d793d
RS
3841 can become (ashiftrt (ashift (xor x 1) C) C) where C is
3842 the bitsize of the mode - 1. This allows simplification of
3843 "a = (b & 8) == 0;" */
3844 if (XEXP (x, 1) == constm1_rtx
3845 && GET_CODE (XEXP (x, 0)) != REG
3846 && ! (GET_CODE (XEXP (x,0)) == SUBREG
3847 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
951553af 3848 && nonzero_bits (XEXP (x, 0), mode) == 1)
8079805d
RK
3849 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3850 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3851 gen_rtx_combine (XOR, mode,
3852 XEXP (x, 0), const1_rtx),
3853 GET_MODE_BITSIZE (mode) - 1),
3854 GET_MODE_BITSIZE (mode) - 1);
02f4ada4
RK
3855
3856 /* If we are adding two things that have no bits in common, convert
3857 the addition into an IOR. This will often be further simplified,
3858 for example in cases like ((a & 1) + (a & 2)), which can
3859 become a & 3. */
3860
ac49a949 3861 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
951553af
RK
3862 && (nonzero_bits (XEXP (x, 0), mode)
3863 & nonzero_bits (XEXP (x, 1), mode)) == 0)
8079805d 3864 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
230d793d
RS
3865 break;
3866
3867 case MINUS:
0802d516
RK
3868 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3869 by reversing the comparison code if valid. */
3870 if (STORE_FLAG_VALUE == 1
3871 && XEXP (x, 0) == const1_rtx
5109d49f
RK
3872 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3873 && reversible_comparison_p (XEXP (x, 1)))
3874 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3875 mode, XEXP (XEXP (x, 1), 0),
3876 XEXP (XEXP (x, 1), 1));
5109d49f 3877
230d793d
RS
3878 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3879 (and <foo> (const_int pow2-1)) */
3880 if (GET_CODE (XEXP (x, 1)) == AND
3881 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3882 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3883 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
8079805d
RK
3884 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3885 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
7bef8680
RK
3886
3887 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
3888 integers. */
3889 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
8079805d
RK
3890 return gen_binary (MINUS, mode,
3891 gen_binary (MINUS, mode, XEXP (x, 0),
3892 XEXP (XEXP (x, 1), 0)),
3893 XEXP (XEXP (x, 1), 1));
230d793d
RS
3894 break;
3895
3896 case MULT:
3897 /* If we have (mult (plus A B) C), apply the distributive law and then
3898 the inverse distributive law to see if things simplify. This
3899 occurs mostly in addresses, often when unrolling loops. */
3900
3901 if (GET_CODE (XEXP (x, 0)) == PLUS)
3902 {
3903 x = apply_distributive_law
3904 (gen_binary (PLUS, mode,
3905 gen_binary (MULT, mode,
3906 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3907 gen_binary (MULT, mode,
3908 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3909
3910 if (GET_CODE (x) != MULT)
8079805d 3911 return x;
230d793d 3912 }
230d793d
RS
3913 break;
3914
3915 case UDIV:
3916 /* If this is a divide by a power of two, treat it as a shift if
3917 its first operand is a shift. */
3918 if (GET_CODE (XEXP (x, 1)) == CONST_INT
3919 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3920 && (GET_CODE (XEXP (x, 0)) == ASHIFT
3921 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3922 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3923 || GET_CODE (XEXP (x, 0)) == ROTATE
3924 || GET_CODE (XEXP (x, 0)) == ROTATERT))
8079805d 3925 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
230d793d
RS
3926 break;
3927
3928 case EQ: case NE:
3929 case GT: case GTU: case GE: case GEU:
3930 case LT: case LTU: case LE: case LEU:
3931 /* If the first operand is a condition code, we can't do anything
3932 with it. */
3933 if (GET_CODE (XEXP (x, 0)) == COMPARE
3934 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
3935#ifdef HAVE_cc0
3936 && XEXP (x, 0) != cc0_rtx
3937#endif
3938 ))
3939 {
3940 rtx op0 = XEXP (x, 0);
3941 rtx op1 = XEXP (x, 1);
3942 enum rtx_code new_code;
3943
3944 if (GET_CODE (op0) == COMPARE)
3945 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
3946
3947 /* Simplify our comparison, if possible. */
3948 new_code = simplify_comparison (code, &op0, &op1);
3949
230d793d 3950 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
951553af 3951 if only the low-order bit is possibly nonzero in X (such as when
5109d49f
RK
3952 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
3953 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
3954 known to be either 0 or -1, NE becomes a NEG and EQ becomes
3955 (plus X 1).
3956
3957 Remove any ZERO_EXTRACT we made when thinking this was a
3958 comparison. It may now be simpler to use, e.g., an AND. If a
3959 ZERO_EXTRACT is indeed appropriate, it will be placed back by
3960 the call to make_compound_operation in the SET case. */
3961
0802d516
RK
3962 if (STORE_FLAG_VALUE == 1
3963 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3964 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
818b11b9
RK
3965 return gen_lowpart_for_combine (mode,
3966 expand_compound_operation (op0));
5109d49f 3967
0802d516
RK
3968 else if (STORE_FLAG_VALUE == 1
3969 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
3970 && op1 == const0_rtx
3971 && (num_sign_bit_copies (op0, mode)
3972 == GET_MODE_BITSIZE (mode)))
3973 {
3974 op0 = expand_compound_operation (op0);
0c1c8ea6 3975 return gen_unary (NEG, mode, mode,
8079805d 3976 gen_lowpart_for_combine (mode, op0));
5109d49f
RK
3977 }
3978
0802d516
RK
3979 else if (STORE_FLAG_VALUE == 1
3980 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
230d793d 3981 && op1 == const0_rtx
5109d49f 3982 && nonzero_bits (op0, mode) == 1)
818b11b9
RK
3983 {
3984 op0 = expand_compound_operation (op0);
8079805d
RK
3985 return gen_binary (XOR, mode,
3986 gen_lowpart_for_combine (mode, op0),
3987 const1_rtx);
5109d49f 3988 }
818b11b9 3989
0802d516
RK
3990 else if (STORE_FLAG_VALUE == 1
3991 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
3992 && op1 == const0_rtx
3993 && (num_sign_bit_copies (op0, mode)
3994 == GET_MODE_BITSIZE (mode)))
3995 {
3996 op0 = expand_compound_operation (op0);
8079805d 3997 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
818b11b9 3998 }
230d793d 3999
5109d49f
RK
4000 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4001 those above. */
0802d516
RK
4002 if (STORE_FLAG_VALUE == -1
4003 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
230d793d 4004 && op1 == const0_rtx
5109d49f
RK
4005 && (num_sign_bit_copies (op0, mode)
4006 == GET_MODE_BITSIZE (mode)))
4007 return gen_lowpart_for_combine (mode,
4008 expand_compound_operation (op0));
4009
0802d516
RK
4010 else if (STORE_FLAG_VALUE == -1
4011 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4012 && op1 == const0_rtx
4013 && nonzero_bits (op0, mode) == 1)
4014 {
4015 op0 = expand_compound_operation (op0);
0c1c8ea6 4016 return gen_unary (NEG, mode, mode,
8079805d 4017 gen_lowpart_for_combine (mode, op0));
5109d49f
RK
4018 }
4019
0802d516
RK
4020 else if (STORE_FLAG_VALUE == -1
4021 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4022 && op1 == const0_rtx
4023 && (num_sign_bit_copies (op0, mode)
4024 == GET_MODE_BITSIZE (mode)))
230d793d 4025 {
818b11b9 4026 op0 = expand_compound_operation (op0);
0c1c8ea6 4027 return gen_unary (NOT, mode, mode,
8079805d 4028 gen_lowpart_for_combine (mode, op0));
5109d49f
RK
4029 }
4030
4031 /* If X is 0/1, (eq X 0) is X-1. */
0802d516
RK
4032 else if (STORE_FLAG_VALUE == -1
4033 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5109d49f
RK
4034 && op1 == const0_rtx
4035 && nonzero_bits (op0, mode) == 1)
4036 {
4037 op0 = expand_compound_operation (op0);
8079805d 4038 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
230d793d 4039 }
230d793d
RS
4040
4041 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
951553af
RK
4042 one bit that might be nonzero, we can convert (ne x 0) to
4043 (ashift x c) where C puts the bit in the sign bit. Remove any
4044 AND with STORE_FLAG_VALUE when we are done, since we are only
4045 going to test the sign bit. */
3f508eca 4046 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5f4f0e22 4047 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
0802d516 4048 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5f4f0e22 4049 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
230d793d
RS
4050 && op1 == const0_rtx
4051 && mode == GET_MODE (op0)
5109d49f 4052 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
230d793d 4053 {
818b11b9
RK
4054 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4055 expand_compound_operation (op0),
230d793d
RS
4056 GET_MODE_BITSIZE (mode) - 1 - i);
4057 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4058 return XEXP (x, 0);
4059 else
4060 return x;
4061 }
4062
4063 /* If the code changed, return a whole new comparison. */
4064 if (new_code != code)
4065 return gen_rtx_combine (new_code, mode, op0, op1);
4066
4067 /* Otherwise, keep this operation, but maybe change its operands.
4068 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4069 SUBST (XEXP (x, 0), op0);
4070 SUBST (XEXP (x, 1), op1);
4071 }
4072 break;
4073
4074 case IF_THEN_ELSE:
8079805d 4075 return simplify_if_then_else (x);
9210df58 4076
8079805d
RK
4077 case ZERO_EXTRACT:
4078 case SIGN_EXTRACT:
4079 case ZERO_EXTEND:
4080 case SIGN_EXTEND:
0f41302f 4081 /* If we are processing SET_DEST, we are done. */
8079805d
RK
4082 if (in_dest)
4083 return x;
d0ab8cd3 4084
8079805d 4085 return expand_compound_operation (x);
d0ab8cd3 4086
8079805d
RK
4087 case SET:
4088 return simplify_set (x);
1a26b032 4089
8079805d
RK
4090 case AND:
4091 case IOR:
4092 case XOR:
4093 return simplify_logical (x, last);
d0ab8cd3 4094
8079805d
RK
4095 case ABS:
4096 /* (abs (neg <foo>)) -> (abs <foo>) */
4097 if (GET_CODE (XEXP (x, 0)) == NEG)
4098 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
1a26b032 4099
8079805d
RK
4100 /* If operand is something known to be positive, ignore the ABS. */
4101 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4102 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4103 <= HOST_BITS_PER_WIDE_INT)
4104 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4105 & ((HOST_WIDE_INT) 1
4106 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4107 == 0)))
4108 return XEXP (x, 0);
1a26b032 4109
1a26b032 4110
8079805d
RK
4111 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4112 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4113 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
1a26b032 4114
8079805d 4115 break;
1a26b032 4116
8079805d
RK
4117 case FFS:
4118 /* (ffs (*_extend <X>)) = (ffs <X>) */
4119 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4120 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4121 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4122 break;
1a26b032 4123
8079805d
RK
4124 case FLOAT:
4125 /* (float (sign_extend <X>)) = (float <X>). */
4126 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4127 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4128 break;
1a26b032 4129
8079805d
RK
4130 case ASHIFT:
4131 case LSHIFTRT:
4132 case ASHIFTRT:
4133 case ROTATE:
4134 case ROTATERT:
4135 /* If this is a shift by a constant amount, simplify it. */
4136 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4137 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4138 INTVAL (XEXP (x, 1)));
4139
4140#ifdef SHIFT_COUNT_TRUNCATED
4141 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4142 SUBST (XEXP (x, 1),
4143 force_to_mode (XEXP (x, 1), GET_MODE (x),
4144 ((HOST_WIDE_INT) 1
4145 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4146 - 1,
4147 NULL_RTX, 0));
4148#endif
4149
4150 break;
e9a25f70
JL
4151
4152 default:
4153 break;
8079805d
RK
4154 }
4155
4156 return x;
4157}
4158\f
4159/* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5109d49f 4160
8079805d
RK
4161static rtx
4162simplify_if_then_else (x)
4163 rtx x;
4164{
4165 enum machine_mode mode = GET_MODE (x);
4166 rtx cond = XEXP (x, 0);
4167 rtx true = XEXP (x, 1);
4168 rtx false = XEXP (x, 2);
4169 enum rtx_code true_code = GET_CODE (cond);
4170 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4171 rtx temp;
4172 int i;
4173
0f41302f 4174 /* Simplify storing of the truth value. */
8079805d
RK
4175 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4176 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4177
0f41302f 4178 /* Also when the truth value has to be reversed. */
8079805d
RK
4179 if (comparison_p && reversible_comparison_p (cond)
4180 && true == const0_rtx && false == const_true_rtx)
4181 return gen_binary (reverse_condition (true_code),
4182 mode, XEXP (cond, 0), XEXP (cond, 1));
4183
4184 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4185 in it is being compared against certain values. Get the true and false
4186 comparisons and see if that says anything about the value of each arm. */
4187
4188 if (comparison_p && reversible_comparison_p (cond)
4189 && GET_CODE (XEXP (cond, 0)) == REG)
4190 {
4191 HOST_WIDE_INT nzb;
4192 rtx from = XEXP (cond, 0);
4193 enum rtx_code false_code = reverse_condition (true_code);
4194 rtx true_val = XEXP (cond, 1);
4195 rtx false_val = true_val;
4196 int swapped = 0;
9210df58 4197
8079805d 4198 /* If FALSE_CODE is EQ, swap the codes and arms. */
5109d49f 4199
8079805d 4200 if (false_code == EQ)
1a26b032 4201 {
8079805d
RK
4202 swapped = 1, true_code = EQ, false_code = NE;
4203 temp = true, true = false, false = temp;
4204 }
5109d49f 4205
8079805d
RK
4206 /* If we are comparing against zero and the expression being tested has
4207 only a single bit that might be nonzero, that is its value when it is
4208 not equal to zero. Similarly if it is known to be -1 or 0. */
4209
4210 if (true_code == EQ && true_val == const0_rtx
4211 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4212 false_code = EQ, false_val = GEN_INT (nzb);
4213 else if (true_code == EQ && true_val == const0_rtx
4214 && (num_sign_bit_copies (from, GET_MODE (from))
4215 == GET_MODE_BITSIZE (GET_MODE (from))))
4216 false_code = EQ, false_val = constm1_rtx;
4217
4218 /* Now simplify an arm if we know the value of the register in the
4219 branch and it is used in the arm. Be careful due to the potential
4220 of locally-shared RTL. */
4221
4222 if (reg_mentioned_p (from, true))
4223 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4224 pc_rtx, pc_rtx, 0, 0);
4225 if (reg_mentioned_p (from, false))
4226 false = subst (known_cond (copy_rtx (false), false_code,
4227 from, false_val),
4228 pc_rtx, pc_rtx, 0, 0);
4229
4230 SUBST (XEXP (x, 1), swapped ? false : true);
4231 SUBST (XEXP (x, 2), swapped ? true : false);
4232
4233 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4234 }
5109d49f 4235
8079805d
RK
4236 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4237 reversed, do so to avoid needing two sets of patterns for
4238 subtract-and-branch insns. Similarly if we have a constant in the true
4239 arm, the false arm is the same as the first operand of the comparison, or
4240 the false arm is more complicated than the true arm. */
4241
4242 if (comparison_p && reversible_comparison_p (cond)
4243 && (true == pc_rtx
4244 || (CONSTANT_P (true)
4245 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4246 || true == const0_rtx
4247 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4248 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4249 || (GET_CODE (true) == SUBREG
4250 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4251 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4252 || reg_mentioned_p (true, false)
4253 || rtx_equal_p (false, XEXP (cond, 0))))
4254 {
4255 true_code = reverse_condition (true_code);
4256 SUBST (XEXP (x, 0),
4257 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4258 XEXP (cond, 1)));
5109d49f 4259
8079805d
RK
4260 SUBST (XEXP (x, 1), false);
4261 SUBST (XEXP (x, 2), true);
1a26b032 4262
8079805d 4263 temp = true, true = false, false = temp, cond = XEXP (x, 0);
bb821298 4264
0f41302f 4265 /* It is possible that the conditional has been simplified out. */
bb821298
RK
4266 true_code = GET_CODE (cond);
4267 comparison_p = GET_RTX_CLASS (true_code) == '<';
8079805d 4268 }
abe6e52f 4269
8079805d 4270 /* If the two arms are identical, we don't need the comparison. */
1a26b032 4271
8079805d
RK
4272 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4273 return true;
1a26b032 4274
5be669c7
RK
4275 /* Convert a == b ? b : a to "a". */
4276 if (true_code == EQ && ! side_effects_p (cond)
4277 && rtx_equal_p (XEXP (cond, 0), false)
4278 && rtx_equal_p (XEXP (cond, 1), true))
4279 return false;
4280 else if (true_code == NE && ! side_effects_p (cond)
4281 && rtx_equal_p (XEXP (cond, 0), true)
4282 && rtx_equal_p (XEXP (cond, 1), false))
4283 return true;
4284
8079805d
RK
4285 /* Look for cases where we have (abs x) or (neg (abs X)). */
4286
4287 if (GET_MODE_CLASS (mode) == MODE_INT
4288 && GET_CODE (false) == NEG
4289 && rtx_equal_p (true, XEXP (false, 0))
4290 && comparison_p
4291 && rtx_equal_p (true, XEXP (cond, 0))
4292 && ! side_effects_p (true))
4293 switch (true_code)
4294 {
4295 case GT:
4296 case GE:
0c1c8ea6 4297 return gen_unary (ABS, mode, mode, true);
8079805d
RK
4298 case LT:
4299 case LE:
0c1c8ea6 4300 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
e9a25f70
JL
4301 default:
4302 break;
8079805d
RK
4303 }
4304
4305 /* Look for MIN or MAX. */
4306
34c8be72 4307 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
8079805d
RK
4308 && comparison_p
4309 && rtx_equal_p (XEXP (cond, 0), true)
4310 && rtx_equal_p (XEXP (cond, 1), false)
4311 && ! side_effects_p (cond))
4312 switch (true_code)
4313 {
4314 case GE:
4315 case GT:
4316 return gen_binary (SMAX, mode, true, false);
4317 case LE:
4318 case LT:
4319 return gen_binary (SMIN, mode, true, false);
4320 case GEU:
4321 case GTU:
4322 return gen_binary (UMAX, mode, true, false);
4323 case LEU:
4324 case LTU:
4325 return gen_binary (UMIN, mode, true, false);
e9a25f70
JL
4326 default:
4327 break;
8079805d
RK
4328 }
4329
8079805d
RK
4330 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4331 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4332 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4333 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4334 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
0802d516 4335 neither 1 or -1, but it isn't worth checking for. */
8079805d 4336
0802d516
RK
4337 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4338 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
8079805d
RK
4339 {
4340 rtx t = make_compound_operation (true, SET);
4341 rtx f = make_compound_operation (false, SET);
4342 rtx cond_op0 = XEXP (cond, 0);
4343 rtx cond_op1 = XEXP (cond, 1);
4344 enum rtx_code op, extend_op = NIL;
4345 enum machine_mode m = mode;
f24ad0e4 4346 rtx z = 0, c1;
8079805d 4347
8079805d
RK
4348 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4349 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4350 || GET_CODE (t) == ASHIFT
4351 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4352 && rtx_equal_p (XEXP (t, 0), f))
4353 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4354
4355 /* If an identity-zero op is commutative, check whether there
0f41302f 4356 would be a match if we swapped the operands. */
8079805d
RK
4357 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4358 || GET_CODE (t) == XOR)
4359 && rtx_equal_p (XEXP (t, 1), f))
4360 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4361 else if (GET_CODE (t) == SIGN_EXTEND
4362 && (GET_CODE (XEXP (t, 0)) == PLUS
4363 || GET_CODE (XEXP (t, 0)) == MINUS
4364 || GET_CODE (XEXP (t, 0)) == IOR
4365 || GET_CODE (XEXP (t, 0)) == XOR
4366 || GET_CODE (XEXP (t, 0)) == ASHIFT
4367 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4368 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4369 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4370 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4371 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4372 && (num_sign_bit_copies (f, GET_MODE (f))
4373 > (GET_MODE_BITSIZE (mode)
4374 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4375 {
4376 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4377 extend_op = SIGN_EXTEND;
4378 m = GET_MODE (XEXP (t, 0));
1a26b032 4379 }
8079805d
RK
4380 else if (GET_CODE (t) == SIGN_EXTEND
4381 && (GET_CODE (XEXP (t, 0)) == PLUS
4382 || GET_CODE (XEXP (t, 0)) == IOR
4383 || GET_CODE (XEXP (t, 0)) == XOR)
4384 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4385 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4386 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4387 && (num_sign_bit_copies (f, GET_MODE (f))
4388 > (GET_MODE_BITSIZE (mode)
4389 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4390 {
4391 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4392 extend_op = SIGN_EXTEND;
4393 m = GET_MODE (XEXP (t, 0));
4394 }
4395 else if (GET_CODE (t) == ZERO_EXTEND
4396 && (GET_CODE (XEXP (t, 0)) == PLUS
4397 || GET_CODE (XEXP (t, 0)) == MINUS
4398 || GET_CODE (XEXP (t, 0)) == IOR
4399 || GET_CODE (XEXP (t, 0)) == XOR
4400 || GET_CODE (XEXP (t, 0)) == ASHIFT
4401 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4402 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4403 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4404 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4405 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4406 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4407 && ((nonzero_bits (f, GET_MODE (f))
4408 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4409 == 0))
4410 {
4411 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4412 extend_op = ZERO_EXTEND;
4413 m = GET_MODE (XEXP (t, 0));
4414 }
4415 else if (GET_CODE (t) == ZERO_EXTEND
4416 && (GET_CODE (XEXP (t, 0)) == PLUS
4417 || GET_CODE (XEXP (t, 0)) == IOR
4418 || GET_CODE (XEXP (t, 0)) == XOR)
4419 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4420 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4421 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4422 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4423 && ((nonzero_bits (f, GET_MODE (f))
4424 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4425 == 0))
4426 {
4427 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4428 extend_op = ZERO_EXTEND;
4429 m = GET_MODE (XEXP (t, 0));
4430 }
4431
4432 if (z)
4433 {
4434 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4435 pc_rtx, pc_rtx, 0, 0);
4436 temp = gen_binary (MULT, m, temp,
4437 gen_binary (MULT, m, c1, const_true_rtx));
4438 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4439 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4440
4441 if (extend_op != NIL)
0c1c8ea6 4442 temp = gen_unary (extend_op, mode, m, temp);
8079805d
RK
4443
4444 return temp;
4445 }
4446 }
224eeff2 4447
8079805d
RK
4448 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4449 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4450 negation of a single bit, we can convert this operation to a shift. We
4451 can actually do this more generally, but it doesn't seem worth it. */
4452
4453 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4454 && false == const0_rtx && GET_CODE (true) == CONST_INT
4455 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4456 && (i = exact_log2 (INTVAL (true))) >= 0)
4457 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4458 == GET_MODE_BITSIZE (mode))
4459 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4460 return
4461 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4462 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
230d793d 4463
8079805d
RK
4464 return x;
4465}
4466\f
4467/* Simplify X, a SET expression. Return the new expression. */
230d793d 4468
8079805d
RK
4469static rtx
4470simplify_set (x)
4471 rtx x;
4472{
4473 rtx src = SET_SRC (x);
4474 rtx dest = SET_DEST (x);
4475 enum machine_mode mode
4476 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4477 rtx other_insn;
4478 rtx *cc_use;
4479
4480 /* (set (pc) (return)) gets written as (return). */
4481 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4482 return src;
230d793d 4483
87e3e0c1
RK
4484 /* Now that we know for sure which bits of SRC we are using, see if we can
4485 simplify the expression for the object knowing that we only need the
4486 low-order bits. */
4487
4488 if (GET_MODE_CLASS (mode) == MODE_INT)
4489 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4490
8079805d
RK
4491 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4492 the comparison result and try to simplify it unless we already have used
4493 undobuf.other_insn. */
4494 if ((GET_CODE (src) == COMPARE
230d793d 4495#ifdef HAVE_cc0
8079805d 4496 || dest == cc0_rtx
230d793d 4497#endif
8079805d
RK
4498 )
4499 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4500 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4501 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
c0d3ac4d 4502 && rtx_equal_p (XEXP (*cc_use, 0), dest))
8079805d
RK
4503 {
4504 enum rtx_code old_code = GET_CODE (*cc_use);
4505 enum rtx_code new_code;
4506 rtx op0, op1;
4507 int other_changed = 0;
4508 enum machine_mode compare_mode = GET_MODE (dest);
4509
4510 if (GET_CODE (src) == COMPARE)
4511 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4512 else
4513 op0 = src, op1 = const0_rtx;
230d793d 4514
8079805d
RK
4515 /* Simplify our comparison, if possible. */
4516 new_code = simplify_comparison (old_code, &op0, &op1);
230d793d 4517
c141a106 4518#ifdef EXTRA_CC_MODES
8079805d
RK
4519 /* If this machine has CC modes other than CCmode, check to see if we
4520 need to use a different CC mode here. */
4521 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
c141a106 4522#endif /* EXTRA_CC_MODES */
230d793d 4523
c141a106 4524#if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
8079805d
RK
4525 /* If the mode changed, we have to change SET_DEST, the mode in the
4526 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4527 a hard register, just build new versions with the proper mode. If it
4528 is a pseudo, we lose unless it is only time we set the pseudo, in
4529 which case we can safely change its mode. */
4530 if (compare_mode != GET_MODE (dest))
4531 {
4532 int regno = REGNO (dest);
4533 rtx new_dest = gen_rtx (REG, compare_mode, regno);
4534
4535 if (regno < FIRST_PSEUDO_REGISTER
b1f21e0a 4536 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
230d793d 4537 {
8079805d
RK
4538 if (regno >= FIRST_PSEUDO_REGISTER)
4539 SUBST (regno_reg_rtx[regno], new_dest);
230d793d 4540
8079805d
RK
4541 SUBST (SET_DEST (x), new_dest);
4542 SUBST (XEXP (*cc_use, 0), new_dest);
4543 other_changed = 1;
230d793d 4544
8079805d 4545 dest = new_dest;
230d793d 4546 }
8079805d 4547 }
230d793d
RS
4548#endif
4549
8079805d
RK
4550 /* If the code changed, we have to build a new comparison in
4551 undobuf.other_insn. */
4552 if (new_code != old_code)
4553 {
4554 unsigned HOST_WIDE_INT mask;
4555
4556 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4557 dest, const0_rtx));
4558
4559 /* If the only change we made was to change an EQ into an NE or
4560 vice versa, OP0 has only one bit that might be nonzero, and OP1
4561 is zero, check if changing the user of the condition code will
4562 produce a valid insn. If it won't, we can keep the original code
4563 in that insn by surrounding our operation with an XOR. */
4564
4565 if (((old_code == NE && new_code == EQ)
4566 || (old_code == EQ && new_code == NE))
4567 && ! other_changed && op1 == const0_rtx
4568 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4569 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
230d793d 4570 {
8079805d 4571 rtx pat = PATTERN (other_insn), note = 0;
a29ca9db 4572 int scratches;
230d793d 4573
a29ca9db 4574 if ((recog_for_combine (&pat, other_insn, &note, &scratches) < 0
8079805d
RK
4575 && ! check_asm_operands (pat)))
4576 {
4577 PUT_CODE (*cc_use, old_code);
4578 other_insn = 0;
230d793d 4579
8079805d 4580 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
230d793d 4581 }
230d793d
RS
4582 }
4583
8079805d
RK
4584 other_changed = 1;
4585 }
4586
4587 if (other_changed)
4588 undobuf.other_insn = other_insn;
230d793d
RS
4589
4590#ifdef HAVE_cc0
8079805d
RK
4591 /* If we are now comparing against zero, change our source if
4592 needed. If we do not use cc0, we always have a COMPARE. */
4593 if (op1 == const0_rtx && dest == cc0_rtx)
4594 {
4595 SUBST (SET_SRC (x), op0);
4596 src = op0;
4597 }
4598 else
230d793d
RS
4599#endif
4600
8079805d
RK
4601 /* Otherwise, if we didn't previously have a COMPARE in the
4602 correct mode, we need one. */
4603 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4604 {
4605 SUBST (SET_SRC (x),
4606 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4607 src = SET_SRC (x);
230d793d
RS
4608 }
4609 else
4610 {
8079805d
RK
4611 /* Otherwise, update the COMPARE if needed. */
4612 SUBST (XEXP (src, 0), op0);
4613 SUBST (XEXP (src, 1), op1);
230d793d 4614 }
8079805d
RK
4615 }
4616 else
4617 {
4618 /* Get SET_SRC in a form where we have placed back any
4619 compound expressions. Then do the checks below. */
4620 src = make_compound_operation (src, SET);
4621 SUBST (SET_SRC (x), src);
4622 }
230d793d 4623
8079805d
RK
4624 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4625 and X being a REG or (subreg (reg)), we may be able to convert this to
4626 (set (subreg:m2 x) (op)).
df62f951 4627
8079805d
RK
4628 We can always do this if M1 is narrower than M2 because that means that
4629 we only care about the low bits of the result.
df62f951 4630
8079805d
RK
4631 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4632 perform a narrower operation that requested since the high-order bits will
4633 be undefined. On machine where it is defined, this transformation is safe
4634 as long as M1 and M2 have the same number of words. */
df62f951 4635
8079805d
RK
4636 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4637 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4638 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4639 / UNITS_PER_WORD)
4640 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4641 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
8baf60bb 4642#ifndef WORD_REGISTER_OPERATIONS
8079805d
RK
4643 && (GET_MODE_SIZE (GET_MODE (src))
4644 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
df62f951 4645#endif
f507a070
RK
4646#ifdef CLASS_CANNOT_CHANGE_SIZE
4647 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4648 && (TEST_HARD_REG_BIT
4649 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4650 REGNO (dest)))
4651 && (GET_MODE_SIZE (GET_MODE (src))
4652 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4653#endif
8079805d
RK
4654 && (GET_CODE (dest) == REG
4655 || (GET_CODE (dest) == SUBREG
4656 && GET_CODE (SUBREG_REG (dest)) == REG)))
4657 {
4658 SUBST (SET_DEST (x),
4659 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4660 dest));
4661 SUBST (SET_SRC (x), SUBREG_REG (src));
4662
4663 src = SET_SRC (x), dest = SET_DEST (x);
4664 }
df62f951 4665
8baf60bb 4666#ifdef LOAD_EXTEND_OP
8079805d
RK
4667 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4668 would require a paradoxical subreg. Replace the subreg with a
0f41302f 4669 zero_extend to avoid the reload that would otherwise be required. */
8079805d
RK
4670
4671 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4672 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4673 && SUBREG_WORD (src) == 0
4674 && (GET_MODE_SIZE (GET_MODE (src))
4675 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4676 && GET_CODE (SUBREG_REG (src)) == MEM)
4677 {
4678 SUBST (SET_SRC (x),
4679 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4680 GET_MODE (src), XEXP (src, 0)));
4681
4682 src = SET_SRC (x);
4683 }
230d793d
RS
4684#endif
4685
8079805d
RK
4686 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4687 are comparing an item known to be 0 or -1 against 0, use a logical
4688 operation instead. Check for one of the arms being an IOR of the other
4689 arm with some value. We compute three terms to be IOR'ed together. In
4690 practice, at most two will be nonzero. Then we do the IOR's. */
4691
4692 if (GET_CODE (dest) != PC
4693 && GET_CODE (src) == IF_THEN_ELSE
36b8d792 4694 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
8079805d
RK
4695 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4696 && XEXP (XEXP (src, 0), 1) == const0_rtx
6dd49058 4697 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
ea414472
DE
4698#ifdef HAVE_conditional_move
4699 && ! can_conditionally_move_p (GET_MODE (src))
4700#endif
8079805d
RK
4701 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4702 GET_MODE (XEXP (XEXP (src, 0), 0)))
4703 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4704 && ! side_effects_p (src))
4705 {
4706 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4707 ? XEXP (src, 1) : XEXP (src, 2));
4708 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4709 ? XEXP (src, 2) : XEXP (src, 1));
4710 rtx term1 = const0_rtx, term2, term3;
4711
4712 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4713 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4714 else if (GET_CODE (true) == IOR
4715 && rtx_equal_p (XEXP (true, 1), false))
4716 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4717 else if (GET_CODE (false) == IOR
4718 && rtx_equal_p (XEXP (false, 0), true))
4719 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4720 else if (GET_CODE (false) == IOR
4721 && rtx_equal_p (XEXP (false, 1), true))
4722 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4723
4724 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4725 term3 = gen_binary (AND, GET_MODE (src),
0c1c8ea6 4726 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
8079805d
RK
4727 XEXP (XEXP (src, 0), 0)),
4728 false);
4729
4730 SUBST (SET_SRC (x),
4731 gen_binary (IOR, GET_MODE (src),
4732 gen_binary (IOR, GET_MODE (src), term1, term2),
4733 term3));
4734
4735 src = SET_SRC (x);
4736 }
230d793d 4737
246e00f2
RK
4738 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4739 whole thing fail. */
4740 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4741 return src;
4742 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4743 return dest;
4744 else
4745 /* Convert this into a field assignment operation, if possible. */
4746 return make_field_assignment (x);
8079805d
RK
4747}
4748\f
4749/* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4750 result. LAST is nonzero if this is the last retry. */
4751
4752static rtx
4753simplify_logical (x, last)
4754 rtx x;
4755 int last;
4756{
4757 enum machine_mode mode = GET_MODE (x);
4758 rtx op0 = XEXP (x, 0);
4759 rtx op1 = XEXP (x, 1);
4760
4761 switch (GET_CODE (x))
4762 {
230d793d 4763 case AND:
8079805d
RK
4764 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4765 insn (and may simplify more). */
4766 if (GET_CODE (op0) == XOR
4767 && rtx_equal_p (XEXP (op0, 0), op1)
4768 && ! side_effects_p (op1))
0c1c8ea6
RK
4769 x = gen_binary (AND, mode,
4770 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
8079805d
RK
4771
4772 if (GET_CODE (op0) == XOR
4773 && rtx_equal_p (XEXP (op0, 1), op1)
4774 && ! side_effects_p (op1))
0c1c8ea6
RK
4775 x = gen_binary (AND, mode,
4776 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
8079805d
RK
4777
4778 /* Similarly for (~ (A ^ B)) & A. */
4779 if (GET_CODE (op0) == NOT
4780 && GET_CODE (XEXP (op0, 0)) == XOR
4781 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4782 && ! side_effects_p (op1))
4783 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4784
4785 if (GET_CODE (op0) == NOT
4786 && GET_CODE (XEXP (op0, 0)) == XOR
4787 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4788 && ! side_effects_p (op1))
4789 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4790
4791 if (GET_CODE (op1) == CONST_INT)
230d793d 4792 {
8079805d 4793 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
230d793d
RS
4794
4795 /* If we have (ior (and (X C1) C2)) and the next restart would be
4796 the last, simplify this by making C1 as small as possible
0f41302f 4797 and then exit. */
8079805d
RK
4798 if (last
4799 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4800 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4801 && GET_CODE (op1) == CONST_INT)
4802 return gen_binary (IOR, mode,
4803 gen_binary (AND, mode, XEXP (op0, 0),
4804 GEN_INT (INTVAL (XEXP (op0, 1))
4805 & ~ INTVAL (op1))), op1);
230d793d
RS
4806
4807 if (GET_CODE (x) != AND)
8079805d 4808 return x;
0e32506c
RK
4809
4810 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
4811 || GET_RTX_CLASS (GET_CODE (x)) == '2')
4812 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
230d793d
RS
4813 }
4814
4815 /* Convert (A | B) & A to A. */
8079805d
RK
4816 if (GET_CODE (op0) == IOR
4817 && (rtx_equal_p (XEXP (op0, 0), op1)
4818 || rtx_equal_p (XEXP (op0, 1), op1))
4819 && ! side_effects_p (XEXP (op0, 0))
4820 && ! side_effects_p (XEXP (op0, 1)))
4821 return op1;
230d793d 4822
d0ab8cd3 4823 /* In the following group of tests (and those in case IOR below),
230d793d
RS
4824 we start with some combination of logical operations and apply
4825 the distributive law followed by the inverse distributive law.
4826 Most of the time, this results in no change. However, if some of
4827 the operands are the same or inverses of each other, simplifications
4828 will result.
4829
4830 For example, (and (ior A B) (not B)) can occur as the result of
4831 expanding a bit field assignment. When we apply the distributive
4832 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8079805d 4833 which then simplifies to (and (A (not B))).
230d793d 4834
8079805d 4835 If we have (and (ior A B) C), apply the distributive law and then
230d793d
RS
4836 the inverse distributive law to see if things simplify. */
4837
8079805d 4838 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
230d793d
RS
4839 {
4840 x = apply_distributive_law
8079805d
RK
4841 (gen_binary (GET_CODE (op0), mode,
4842 gen_binary (AND, mode, XEXP (op0, 0), op1),
4843 gen_binary (AND, mode, XEXP (op0, 1), op1)));
230d793d 4844 if (GET_CODE (x) != AND)
8079805d 4845 return x;
230d793d
RS
4846 }
4847
8079805d
RK
4848 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
4849 return apply_distributive_law
4850 (gen_binary (GET_CODE (op1), mode,
4851 gen_binary (AND, mode, XEXP (op1, 0), op0),
4852 gen_binary (AND, mode, XEXP (op1, 1), op0)));
230d793d
RS
4853
4854 /* Similarly, taking advantage of the fact that
4855 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
4856
8079805d
RK
4857 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
4858 return apply_distributive_law
4859 (gen_binary (XOR, mode,
4860 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
4861 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
230d793d 4862
8079805d
RK
4863 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
4864 return apply_distributive_law
4865 (gen_binary (XOR, mode,
4866 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
4867 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
230d793d
RS
4868 break;
4869
4870 case IOR:
951553af 4871 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
8079805d 4872 if (GET_CODE (op1) == CONST_INT
ac49a949 4873 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8079805d
RK
4874 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
4875 return op1;
d0ab8cd3 4876
230d793d 4877 /* Convert (A & B) | A to A. */
8079805d
RK
4878 if (GET_CODE (op0) == AND
4879 && (rtx_equal_p (XEXP (op0, 0), op1)
4880 || rtx_equal_p (XEXP (op0, 1), op1))
4881 && ! side_effects_p (XEXP (op0, 0))
4882 && ! side_effects_p (XEXP (op0, 1)))
4883 return op1;
230d793d
RS
4884
4885 /* If we have (ior (and A B) C), apply the distributive law and then
4886 the inverse distributive law to see if things simplify. */
4887
8079805d 4888 if (GET_CODE (op0) == AND)
230d793d
RS
4889 {
4890 x = apply_distributive_law
4891 (gen_binary (AND, mode,
8079805d
RK
4892 gen_binary (IOR, mode, XEXP (op0, 0), op1),
4893 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
230d793d
RS
4894
4895 if (GET_CODE (x) != IOR)
8079805d 4896 return x;
230d793d
RS
4897 }
4898
8079805d 4899 if (GET_CODE (op1) == AND)
230d793d
RS
4900 {
4901 x = apply_distributive_law
4902 (gen_binary (AND, mode,
8079805d
RK
4903 gen_binary (IOR, mode, XEXP (op1, 0), op0),
4904 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
230d793d
RS
4905
4906 if (GET_CODE (x) != IOR)
8079805d 4907 return x;
230d793d
RS
4908 }
4909
4910 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4911 mode size to (rotate A CX). */
4912
8079805d
RK
4913 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
4914 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
4915 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
4916 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4917 && GET_CODE (XEXP (op1, 1)) == CONST_INT
4918 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
230d793d 4919 == GET_MODE_BITSIZE (mode)))
8079805d
RK
4920 return gen_rtx (ROTATE, mode, XEXP (op0, 0),
4921 (GET_CODE (op0) == ASHIFT
4922 ? XEXP (op0, 1) : XEXP (op1, 1)));
230d793d 4923
71923da7
RK
4924 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4925 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
4926 does not affect any of the bits in OP1, it can really be done
4927 as a PLUS and we can associate. We do this by seeing if OP1
4928 can be safely shifted left C bits. */
4929 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
4930 && GET_CODE (XEXP (op0, 0)) == PLUS
4931 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
4932 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4933 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4934 {
4935 int count = INTVAL (XEXP (op0, 1));
4936 HOST_WIDE_INT mask = INTVAL (op1) << count;
4937
4938 if (mask >> count == INTVAL (op1)
4939 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
4940 {
4941 SUBST (XEXP (XEXP (op0, 0), 1),
4942 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
4943 return op0;
4944 }
4945 }
230d793d
RS
4946 break;
4947
4948 case XOR:
4949 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4950 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4951 (NOT y). */
4952 {
4953 int num_negated = 0;
230d793d 4954
8079805d
RK
4955 if (GET_CODE (op0) == NOT)
4956 num_negated++, op0 = XEXP (op0, 0);
4957 if (GET_CODE (op1) == NOT)
4958 num_negated++, op1 = XEXP (op1, 0);
230d793d
RS
4959
4960 if (num_negated == 2)
4961 {
8079805d
RK
4962 SUBST (XEXP (x, 0), op0);
4963 SUBST (XEXP (x, 1), op1);
230d793d
RS
4964 }
4965 else if (num_negated == 1)
0c1c8ea6 4966 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
230d793d
RS
4967 }
4968
4969 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
4970 correspond to a machine insn or result in further simplifications
4971 if B is a constant. */
4972
8079805d
RK
4973 if (GET_CODE (op0) == AND
4974 && rtx_equal_p (XEXP (op0, 1), op1)
4975 && ! side_effects_p (op1))
0c1c8ea6
RK
4976 return gen_binary (AND, mode,
4977 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
8079805d 4978 op1);
230d793d 4979
8079805d
RK
4980 else if (GET_CODE (op0) == AND
4981 && rtx_equal_p (XEXP (op0, 0), op1)
4982 && ! side_effects_p (op1))
0c1c8ea6
RK
4983 return gen_binary (AND, mode,
4984 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
8079805d 4985 op1);
230d793d 4986
230d793d 4987 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
0802d516
RK
4988 comparison if STORE_FLAG_VALUE is 1. */
4989 if (STORE_FLAG_VALUE == 1
4990 && op1 == const1_rtx
8079805d
RK
4991 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4992 && reversible_comparison_p (op0))
4993 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4994 mode, XEXP (op0, 0), XEXP (op0, 1));
500c518b
RK
4995
4996 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4997 is (lt foo (const_int 0)), so we can perform the above
0802d516 4998 simplification if STORE_FLAG_VALUE is 1. */
500c518b 4999
0802d516
RK
5000 if (STORE_FLAG_VALUE == 1
5001 && op1 == const1_rtx
8079805d
RK
5002 && GET_CODE (op0) == LSHIFTRT
5003 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5004 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5005 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
230d793d
RS
5006
5007 /* (xor (comparison foo bar) (const_int sign-bit))
5008 when STORE_FLAG_VALUE is the sign bit. */
5f4f0e22 5009 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
0802d516 5010 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5f4f0e22 5011 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
8079805d
RK
5012 && op1 == const_true_rtx
5013 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5014 && reversible_comparison_p (op0))
5015 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5016 mode, XEXP (op0, 0), XEXP (op0, 1));
230d793d 5017 break;
e9a25f70
JL
5018
5019 default:
5020 abort ();
230d793d
RS
5021 }
5022
5023 return x;
5024}
5025\f
5026/* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5027 operations" because they can be replaced with two more basic operations.
5028 ZERO_EXTEND is also considered "compound" because it can be replaced with
5029 an AND operation, which is simpler, though only one operation.
5030
5031 The function expand_compound_operation is called with an rtx expression
5032 and will convert it to the appropriate shifts and AND operations,
5033 simplifying at each stage.
5034
5035 The function make_compound_operation is called to convert an expression
5036 consisting of shifts and ANDs into the equivalent compound expression.
5037 It is the inverse of this function, loosely speaking. */
5038
5039static rtx
5040expand_compound_operation (x)
5041 rtx x;
5042{
5043 int pos = 0, len;
5044 int unsignedp = 0;
5045 int modewidth;
5046 rtx tem;
5047
5048 switch (GET_CODE (x))
5049 {
5050 case ZERO_EXTEND:
5051 unsignedp = 1;
5052 case SIGN_EXTEND:
75473182
RS
5053 /* We can't necessarily use a const_int for a multiword mode;
5054 it depends on implicitly extending the value.
5055 Since we don't know the right way to extend it,
5056 we can't tell whether the implicit way is right.
5057
5058 Even for a mode that is no wider than a const_int,
5059 we can't win, because we need to sign extend one of its bits through
5060 the rest of it, and we don't know which bit. */
230d793d 5061 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
75473182 5062 return x;
230d793d 5063
8079805d
RK
5064 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5065 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5066 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5067 reloaded. If not for that, MEM's would very rarely be safe.
5068
5069 Reject MODEs bigger than a word, because we might not be able
5070 to reference a two-register group starting with an arbitrary register
5071 (and currently gen_lowpart might crash for a SUBREG). */
5072
5073 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
230d793d
RS
5074 return x;
5075
5076 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5077 /* If the inner object has VOIDmode (the only way this can happen
5078 is if it is a ASM_OPERANDS), we can't do anything since we don't
5079 know how much masking to do. */
5080 if (len == 0)
5081 return x;
5082
5083 break;
5084
5085 case ZERO_EXTRACT:
5086 unsignedp = 1;
5087 case SIGN_EXTRACT:
5088 /* If the operand is a CLOBBER, just return it. */
5089 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5090 return XEXP (x, 0);
5091
5092 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5093 || GET_CODE (XEXP (x, 2)) != CONST_INT
5094 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5095 return x;
5096
5097 len = INTVAL (XEXP (x, 1));
5098 pos = INTVAL (XEXP (x, 2));
5099
5100 /* If this goes outside the object being extracted, replace the object
5101 with a (use (mem ...)) construct that only combine understands
5102 and is used only for this purpose. */
5103 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5104 SUBST (XEXP (x, 0), gen_rtx (USE, GET_MODE (x), XEXP (x, 0)));
5105
f76b9db2
ILT
5106 if (BITS_BIG_ENDIAN)
5107 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5108
230d793d
RS
5109 break;
5110
5111 default:
5112 return x;
5113 }
5114
0f13a422
ILT
5115 /* We can optimize some special cases of ZERO_EXTEND. */
5116 if (GET_CODE (x) == ZERO_EXTEND)
5117 {
5118 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5119 know that the last value didn't have any inappropriate bits
5120 set. */
5121 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5122 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5123 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5124 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5125 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5126 return XEXP (XEXP (x, 0), 0);
5127
5128 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5129 if (GET_CODE (XEXP (x, 0)) == SUBREG
5130 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5131 && subreg_lowpart_p (XEXP (x, 0))
5132 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5133 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5134 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0)
5135 return SUBREG_REG (XEXP (x, 0));
5136
5137 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5138 is a comparison and STORE_FLAG_VALUE permits. This is like
5139 the first case, but it works even when GET_MODE (x) is larger
5140 than HOST_WIDE_INT. */
5141 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5142 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5143 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5144 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5145 <= HOST_BITS_PER_WIDE_INT)
5146 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5147 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5148 return XEXP (XEXP (x, 0), 0);
5149
5150 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5151 if (GET_CODE (XEXP (x, 0)) == SUBREG
5152 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5153 && subreg_lowpart_p (XEXP (x, 0))
5154 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5155 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5156 <= HOST_BITS_PER_WIDE_INT)
5157 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5158 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5159 return SUBREG_REG (XEXP (x, 0));
5160
5161 /* If sign extension is cheaper than zero extension, then use it
5162 if we know that no extraneous bits are set, and that the high
5163 bit is not set. */
5164 if (flag_expensive_optimizations
5165 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5166 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5167 & ~ (((unsigned HOST_WIDE_INT)
5168 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5169 >> 1))
5170 == 0))
5171 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5172 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5173 <= HOST_BITS_PER_WIDE_INT)
5174 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5175 & ~ (((unsigned HOST_WIDE_INT)
5176 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5177 >> 1))
5178 == 0))))
5179 {
5180 rtx temp = gen_rtx (SIGN_EXTEND, GET_MODE (x), XEXP (x, 0));
5181
5182 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5183 return expand_compound_operation (temp);
5184 }
5185 }
5186
230d793d
RS
5187 /* If we reach here, we want to return a pair of shifts. The inner
5188 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5189 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5190 logical depending on the value of UNSIGNEDP.
5191
5192 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5193 converted into an AND of a shift.
5194
5195 We must check for the case where the left shift would have a negative
5196 count. This can happen in a case like (x >> 31) & 255 on machines
5197 that can't shift by a constant. On those machines, we would first
5198 combine the shift with the AND to produce a variable-position
5199 extraction. Then the constant of 31 would be substituted in to produce
5200 a such a position. */
5201
5202 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5203 if (modewidth >= pos - len)
5f4f0e22 5204 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
230d793d 5205 GET_MODE (x),
5f4f0e22
CH
5206 simplify_shift_const (NULL_RTX, ASHIFT,
5207 GET_MODE (x),
230d793d
RS
5208 XEXP (x, 0),
5209 modewidth - pos - len),
5210 modewidth - len);
5211
5f4f0e22
CH
5212 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5213 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5214 simplify_shift_const (NULL_RTX, LSHIFTRT,
230d793d
RS
5215 GET_MODE (x),
5216 XEXP (x, 0), pos),
5f4f0e22 5217 ((HOST_WIDE_INT) 1 << len) - 1);
230d793d
RS
5218 else
5219 /* Any other cases we can't handle. */
5220 return x;
5221
5222
5223 /* If we couldn't do this for some reason, return the original
5224 expression. */
5225 if (GET_CODE (tem) == CLOBBER)
5226 return x;
5227
5228 return tem;
5229}
5230\f
5231/* X is a SET which contains an assignment of one object into
5232 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5233 or certain SUBREGS). If possible, convert it into a series of
5234 logical operations.
5235
5236 We half-heartedly support variable positions, but do not at all
5237 support variable lengths. */
5238
5239static rtx
5240expand_field_assignment (x)
5241 rtx x;
5242{
5243 rtx inner;
0f41302f 5244 rtx pos; /* Always counts from low bit. */
230d793d
RS
5245 int len;
5246 rtx mask;
5247 enum machine_mode compute_mode;
5248
5249 /* Loop until we find something we can't simplify. */
5250 while (1)
5251 {
5252 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5253 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5254 {
5255 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5256 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
4d9cfc7b 5257 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
230d793d
RS
5258 }
5259 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5260 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5261 {
5262 inner = XEXP (SET_DEST (x), 0);
5263 len = INTVAL (XEXP (SET_DEST (x), 1));
5264 pos = XEXP (SET_DEST (x), 2);
5265
5266 /* If the position is constant and spans the width of INNER,
5267 surround INNER with a USE to indicate this. */
5268 if (GET_CODE (pos) == CONST_INT
5269 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5270 inner = gen_rtx (USE, GET_MODE (SET_DEST (x)), inner);
5271
f76b9db2
ILT
5272 if (BITS_BIG_ENDIAN)
5273 {
5274 if (GET_CODE (pos) == CONST_INT)
5275 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5276 - INTVAL (pos));
5277 else if (GET_CODE (pos) == MINUS
5278 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5279 && (INTVAL (XEXP (pos, 1))
5280 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5281 /* If position is ADJUST - X, new position is X. */
5282 pos = XEXP (pos, 0);
5283 else
5284 pos = gen_binary (MINUS, GET_MODE (pos),
5285 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5286 - len),
5287 pos);
5288 }
230d793d
RS
5289 }
5290
5291 /* A SUBREG between two modes that occupy the same numbers of words
5292 can be done by moving the SUBREG to the source. */
5293 else if (GET_CODE (SET_DEST (x)) == SUBREG
5294 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5295 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5296 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5297 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5298 {
5299 x = gen_rtx (SET, VOIDmode, SUBREG_REG (SET_DEST (x)),
5300 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
5301 SET_SRC (x)));
5302 continue;
5303 }
5304 else
5305 break;
5306
5307 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5308 inner = SUBREG_REG (inner);
5309
5310 compute_mode = GET_MODE (inner);
5311
5312 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5f4f0e22
CH
5313 if (len < HOST_BITS_PER_WIDE_INT)
5314 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
230d793d
RS
5315 else
5316 break;
5317
5318 /* Now compute the equivalent expression. Make a copy of INNER
5319 for the SET_DEST in case it is a MEM into which we will substitute;
5320 we don't want shared RTL in that case. */
5321 x = gen_rtx (SET, VOIDmode, copy_rtx (inner),
5322 gen_binary (IOR, compute_mode,
5323 gen_binary (AND, compute_mode,
5324 gen_unary (NOT, compute_mode,
0c1c8ea6 5325 compute_mode,
230d793d
RS
5326 gen_binary (ASHIFT,
5327 compute_mode,
5328 mask, pos)),
5329 inner),
5330 gen_binary (ASHIFT, compute_mode,
5331 gen_binary (AND, compute_mode,
5332 gen_lowpart_for_combine
5333 (compute_mode,
5334 SET_SRC (x)),
5335 mask),
5336 pos)));
5337 }
5338
5339 return x;
5340}
5341\f
8999a12e
RK
5342/* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5343 it is an RTX that represents a variable starting position; otherwise,
5344 POS is the (constant) starting bit position (counted from the LSB).
230d793d
RS
5345
5346 INNER may be a USE. This will occur when we started with a bitfield
5347 that went outside the boundary of the object in memory, which is
5348 allowed on most machines. To isolate this case, we produce a USE
5349 whose mode is wide enough and surround the MEM with it. The only
5350 code that understands the USE is this routine. If it is not removed,
5351 it will cause the resulting insn not to match.
5352
5353 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5354 signed reference.
5355
5356 IN_DEST is non-zero if this is a reference in the destination of a
5357 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5358 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5359 be used.
5360
5361 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5362 ZERO_EXTRACT should be built even for bits starting at bit 0.
5363
76184def
DE
5364 MODE is the desired mode of the result (if IN_DEST == 0).
5365
5366 The result is an RTX for the extraction or NULL_RTX if the target
5367 can't handle it. */
230d793d
RS
5368
5369static rtx
5370make_extraction (mode, inner, pos, pos_rtx, len,
5371 unsignedp, in_dest, in_compare)
5372 enum machine_mode mode;
5373 rtx inner;
5374 int pos;
5375 rtx pos_rtx;
5376 int len;
5377 int unsignedp;
5378 int in_dest, in_compare;
5379{
94b4b17a
RS
5380 /* This mode describes the size of the storage area
5381 to fetch the overall value from. Within that, we
5382 ignore the POS lowest bits, etc. */
230d793d
RS
5383 enum machine_mode is_mode = GET_MODE (inner);
5384 enum machine_mode inner_mode;
d7cd794f
RK
5385 enum machine_mode wanted_inner_mode = byte_mode;
5386 enum machine_mode wanted_inner_reg_mode = word_mode;
230d793d
RS
5387 enum machine_mode pos_mode = word_mode;
5388 enum machine_mode extraction_mode = word_mode;
5389 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5390 int spans_byte = 0;
5391 rtx new = 0;
8999a12e 5392 rtx orig_pos_rtx = pos_rtx;
6139ff20 5393 int orig_pos;
230d793d
RS
5394
5395 /* Get some information about INNER and get the innermost object. */
5396 if (GET_CODE (inner) == USE)
94b4b17a 5397 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
230d793d
RS
5398 /* We don't need to adjust the position because we set up the USE
5399 to pretend that it was a full-word object. */
5400 spans_byte = 1, inner = XEXP (inner, 0);
5401 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
94b4b17a
RS
5402 {
5403 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5404 consider just the QI as the memory to extract from.
5405 The subreg adds or removes high bits; its mode is
5406 irrelevant to the meaning of this extraction,
5407 since POS and LEN count from the lsb. */
5408 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5409 is_mode = GET_MODE (SUBREG_REG (inner));
5410 inner = SUBREG_REG (inner);
5411 }
230d793d
RS
5412
5413 inner_mode = GET_MODE (inner);
5414
5415 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
8999a12e 5416 pos = INTVAL (pos_rtx), pos_rtx = 0;
230d793d
RS
5417
5418 /* See if this can be done without an extraction. We never can if the
5419 width of the field is not the same as that of some integer mode. For
5420 registers, we can only avoid the extraction if the position is at the
5421 low-order bit and this is either not in the destination or we have the
5422 appropriate STRICT_LOW_PART operation available.
5423
5424 For MEM, we can avoid an extract if the field starts on an appropriate
5425 boundary and we can change the mode of the memory reference. However,
5426 we cannot directly access the MEM if we have a USE and the underlying
5427 MEM is not TMODE. This combination means that MEM was being used in a
5428 context where bits outside its mode were being referenced; that is only
5429 valid in bit-field insns. */
5430
5431 if (tmode != BLKmode
5432 && ! (spans_byte && inner_mode != tmode)
4d9cfc7b
RK
5433 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5434 && GET_CODE (inner) != MEM
230d793d 5435 && (! in_dest
df62f951
RK
5436 || (GET_CODE (inner) == REG
5437 && (movstrict_optab->handlers[(int) tmode].insn_code
5438 != CODE_FOR_nothing))))
8999a12e 5439 || (GET_CODE (inner) == MEM && pos_rtx == 0
dfbe1b2f
RK
5440 && (pos
5441 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5442 : BITS_PER_UNIT)) == 0
230d793d
RS
5443 /* We can't do this if we are widening INNER_MODE (it
5444 may not be aligned, for one thing). */
5445 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5446 && (inner_mode == tmode
5447 || (! mode_dependent_address_p (XEXP (inner, 0))
5448 && ! MEM_VOLATILE_P (inner))))))
5449 {
230d793d
RS
5450 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5451 field. If the original and current mode are the same, we need not
5452 adjust the offset. Otherwise, we do if bytes big endian.
5453
4d9cfc7b
RK
5454 If INNER is not a MEM, get a piece consisting of just the field
5455 of interest (in this case POS % BITS_PER_WORD must be 0). */
230d793d
RS
5456
5457 if (GET_CODE (inner) == MEM)
5458 {
94b4b17a
RS
5459 int offset;
5460 /* POS counts from lsb, but make OFFSET count in memory order. */
5461 if (BYTES_BIG_ENDIAN)
5462 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5463 else
5464 offset = pos / BITS_PER_UNIT;
230d793d
RS
5465
5466 new = gen_rtx (MEM, tmode, plus_constant (XEXP (inner, 0), offset));
5467 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5468 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
5469 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
5470 }
df62f951 5471 else if (GET_CODE (inner) == REG)
c0d3ac4d
RK
5472 {
5473 /* We can't call gen_lowpart_for_combine here since we always want
5474 a SUBREG and it would sometimes return a new hard register. */
5475 if (tmode != inner_mode)
5476 new = gen_rtx (SUBREG, tmode, inner,
5477 (WORDS_BIG_ENDIAN
5478 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
4d9cfc7b
RK
5479 ? (((GET_MODE_SIZE (inner_mode)
5480 - GET_MODE_SIZE (tmode))
5481 / UNITS_PER_WORD)
5482 - pos / BITS_PER_WORD)
5483 : pos / BITS_PER_WORD));
c0d3ac4d
RK
5484 else
5485 new = inner;
5486 }
230d793d 5487 else
6139ff20
RK
5488 new = force_to_mode (inner, tmode,
5489 len >= HOST_BITS_PER_WIDE_INT
5490 ? GET_MODE_MASK (tmode)
5491 : ((HOST_WIDE_INT) 1 << len) - 1,
e3d616e3 5492 NULL_RTX, 0);
230d793d
RS
5493
5494 /* If this extraction is going into the destination of a SET,
5495 make a STRICT_LOW_PART unless we made a MEM. */
5496
5497 if (in_dest)
5498 return (GET_CODE (new) == MEM ? new
77fa0940
RK
5499 : (GET_CODE (new) != SUBREG
5500 ? gen_rtx (CLOBBER, tmode, const0_rtx)
5501 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
230d793d
RS
5502
5503 /* Otherwise, sign- or zero-extend unless we already are in the
5504 proper mode. */
5505
5506 return (mode == tmode ? new
5507 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5508 mode, new));
5509 }
5510
cc471082
RS
5511 /* Unless this is a COMPARE or we have a funny memory reference,
5512 don't do anything with zero-extending field extracts starting at
5513 the low-order bit since they are simple AND operations. */
8999a12e
RK
5514 if (pos_rtx == 0 && pos == 0 && ! in_dest
5515 && ! in_compare && ! spans_byte && unsignedp)
230d793d
RS
5516 return 0;
5517
e7373556
RK
5518 /* Unless we are allowed to span bytes, reject this if we would be
5519 spanning bytes or if the position is not a constant and the length
5520 is not 1. In all other cases, we would only be going outside
5521 out object in cases when an original shift would have been
5522 undefined. */
5523 if (! spans_byte
5524 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5525 || (pos_rtx != 0 && len != 1)))
5526 return 0;
5527
d7cd794f 5528 /* Get the mode to use should INNER not be a MEM, the mode for the position,
230d793d
RS
5529 and the mode for the result. */
5530#ifdef HAVE_insv
5531 if (in_dest)
5532 {
d7cd794f 5533 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
230d793d
RS
5534 pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
5535 extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
5536 }
5537#endif
5538
5539#ifdef HAVE_extzv
5540 if (! in_dest && unsignedp)
5541 {
d7cd794f 5542 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
230d793d
RS
5543 pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
5544 extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
5545 }
5546#endif
5547
5548#ifdef HAVE_extv
5549 if (! in_dest && ! unsignedp)
5550 {
d7cd794f 5551 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
230d793d
RS
5552 pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
5553 extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
5554 }
5555#endif
5556
5557 /* Never narrow an object, since that might not be safe. */
5558
5559 if (mode != VOIDmode
5560 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5561 extraction_mode = mode;
5562
5563 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5564 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5565 pos_mode = GET_MODE (pos_rtx);
5566
d7cd794f
RK
5567 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5568 if we have to change the mode of memory and cannot, the desired mode is
5569 EXTRACTION_MODE. */
5570 if (GET_CODE (inner) != MEM)
5571 wanted_inner_mode = wanted_inner_reg_mode;
5572 else if (inner_mode != wanted_inner_mode
5573 && (mode_dependent_address_p (XEXP (inner, 0))
5574 || MEM_VOLATILE_P (inner)))
5575 wanted_inner_mode = extraction_mode;
230d793d 5576
6139ff20
RK
5577 orig_pos = pos;
5578
f76b9db2
ILT
5579 if (BITS_BIG_ENDIAN)
5580 {
cf54c2cd
DE
5581 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5582 BITS_BIG_ENDIAN style. If position is constant, compute new
5583 position. Otherwise, build subtraction.
5584 Note that POS is relative to the mode of the original argument.
5585 If it's a MEM we need to recompute POS relative to that.
5586 However, if we're extracting from (or inserting into) a register,
5587 we want to recompute POS relative to wanted_inner_mode. */
5588 int width = (GET_CODE (inner) == MEM
5589 ? GET_MODE_BITSIZE (is_mode)
5590 : GET_MODE_BITSIZE (wanted_inner_mode));
5591
f76b9db2 5592 if (pos_rtx == 0)
cf54c2cd 5593 pos = width - len - pos;
f76b9db2
ILT
5594 else
5595 pos_rtx
5596 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
cf54c2cd
DE
5597 GEN_INT (width - len), pos_rtx);
5598 /* POS may be less than 0 now, but we check for that below.
5599 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
f76b9db2 5600 }
230d793d
RS
5601
5602 /* If INNER has a wider mode, make it smaller. If this is a constant
5603 extract, try to adjust the byte to point to the byte containing
5604 the value. */
d7cd794f
RK
5605 if (wanted_inner_mode != VOIDmode
5606 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
230d793d 5607 && ((GET_CODE (inner) == MEM
d7cd794f 5608 && (inner_mode == wanted_inner_mode
230d793d
RS
5609 || (! mode_dependent_address_p (XEXP (inner, 0))
5610 && ! MEM_VOLATILE_P (inner))))))
5611 {
5612 int offset = 0;
5613
5614 /* The computations below will be correct if the machine is big
5615 endian in both bits and bytes or little endian in bits and bytes.
5616 If it is mixed, we must adjust. */
5617
230d793d 5618 /* If bytes are big endian and we had a paradoxical SUBREG, we must
0f41302f 5619 adjust OFFSET to compensate. */
f76b9db2
ILT
5620 if (BYTES_BIG_ENDIAN
5621 && ! spans_byte
230d793d
RS
5622 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5623 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
230d793d
RS
5624
5625 /* If this is a constant position, we can move to the desired byte. */
8999a12e 5626 if (pos_rtx == 0)
230d793d
RS
5627 {
5628 offset += pos / BITS_PER_UNIT;
d7cd794f 5629 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
230d793d
RS
5630 }
5631
f76b9db2
ILT
5632 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5633 && ! spans_byte
d7cd794f 5634 && is_mode != wanted_inner_mode)
c6b3f1f2 5635 offset = (GET_MODE_SIZE (is_mode)
d7cd794f 5636 - GET_MODE_SIZE (wanted_inner_mode) - offset);
c6b3f1f2 5637
d7cd794f 5638 if (offset != 0 || inner_mode != wanted_inner_mode)
230d793d 5639 {
d7cd794f 5640 rtx newmem = gen_rtx (MEM, wanted_inner_mode,
230d793d
RS
5641 plus_constant (XEXP (inner, 0), offset));
5642 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5643 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
5644 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
5645 inner = newmem;
5646 }
5647 }
5648
9e74dc41
RK
5649 /* If INNER is not memory, we can always get it into the proper mode. If we
5650 are changing its mode, POS must be a constant and smaller than the size
5651 of the new mode. */
230d793d 5652 else if (GET_CODE (inner) != MEM)
9e74dc41
RK
5653 {
5654 if (GET_MODE (inner) != wanted_inner_mode
5655 && (pos_rtx != 0
5656 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5657 return 0;
5658
5659 inner = force_to_mode (inner, wanted_inner_mode,
5660 pos_rtx
5661 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5662 ? GET_MODE_MASK (wanted_inner_mode)
5663 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5664 NULL_RTX, 0);
5665 }
230d793d
RS
5666
5667 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
5668 have to zero extend. Otherwise, we can just use a SUBREG. */
8999a12e 5669 if (pos_rtx != 0
230d793d
RS
5670 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5671 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
8999a12e 5672 else if (pos_rtx != 0
230d793d
RS
5673 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5674 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5675
8999a12e
RK
5676 /* Make POS_RTX unless we already have it and it is correct. If we don't
5677 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
0f41302f 5678 be a CONST_INT. */
8999a12e
RK
5679 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5680 pos_rtx = orig_pos_rtx;
5681
5682 else if (pos_rtx == 0)
5f4f0e22 5683 pos_rtx = GEN_INT (pos);
230d793d
RS
5684
5685 /* Make the required operation. See if we can use existing rtx. */
5686 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5f4f0e22 5687 extraction_mode, inner, GEN_INT (len), pos_rtx);
230d793d
RS
5688 if (! in_dest)
5689 new = gen_lowpart_for_combine (mode, new);
5690
5691 return new;
5692}
5693\f
71923da7
RK
5694/* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5695 with any other operations in X. Return X without that shift if so. */
5696
5697static rtx
5698extract_left_shift (x, count)
5699 rtx x;
5700 int count;
5701{
5702 enum rtx_code code = GET_CODE (x);
5703 enum machine_mode mode = GET_MODE (x);
5704 rtx tem;
5705
5706 switch (code)
5707 {
5708 case ASHIFT:
5709 /* This is the shift itself. If it is wide enough, we will return
5710 either the value being shifted if the shift count is equal to
5711 COUNT or a shift for the difference. */
5712 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5713 && INTVAL (XEXP (x, 1)) >= count)
5714 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5715 INTVAL (XEXP (x, 1)) - count);
5716 break;
5717
5718 case NEG: case NOT:
5719 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
0c1c8ea6 5720 return gen_unary (code, mode, mode, tem);
71923da7
RK
5721
5722 break;
5723
5724 case PLUS: case IOR: case XOR: case AND:
5725 /* If we can safely shift this constant and we find the inner shift,
5726 make a new operation. */
5727 if (GET_CODE (XEXP (x,1)) == CONST_INT
5728 && (INTVAL (XEXP (x, 1)) & (((HOST_WIDE_INT) 1 << count)) - 1) == 0
5729 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5730 return gen_binary (code, mode, tem,
5731 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5732
5733 break;
e9a25f70
JL
5734
5735 default:
5736 break;
71923da7
RK
5737 }
5738
5739 return 0;
5740}
5741\f
230d793d
RS
5742/* Look at the expression rooted at X. Look for expressions
5743 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5744 Form these expressions.
5745
5746 Return the new rtx, usually just X.
5747
5748 Also, for machines like the Vax that don't have logical shift insns,
5749 try to convert logical to arithmetic shift operations in cases where
5750 they are equivalent. This undoes the canonicalizations to logical
5751 shifts done elsewhere.
5752
5753 We try, as much as possible, to re-use rtl expressions to save memory.
5754
5755 IN_CODE says what kind of expression we are processing. Normally, it is
42495ca0
RK
5756 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
5757 being kludges), it is MEM. When processing the arguments of a comparison
230d793d
RS
5758 or a COMPARE against zero, it is COMPARE. */
5759
5760static rtx
5761make_compound_operation (x, in_code)
5762 rtx x;
5763 enum rtx_code in_code;
5764{
5765 enum rtx_code code = GET_CODE (x);
5766 enum machine_mode mode = GET_MODE (x);
5767 int mode_width = GET_MODE_BITSIZE (mode);
71923da7 5768 rtx rhs, lhs;
230d793d 5769 enum rtx_code next_code;
f24ad0e4 5770 int i;
230d793d 5771 rtx new = 0;
280f58ba 5772 rtx tem;
230d793d
RS
5773 char *fmt;
5774
5775 /* Select the code to be used in recursive calls. Once we are inside an
5776 address, we stay there. If we have a comparison, set to COMPARE,
5777 but once inside, go back to our default of SET. */
5778
42495ca0 5779 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
230d793d
RS
5780 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5781 && XEXP (x, 1) == const0_rtx) ? COMPARE
5782 : in_code == COMPARE ? SET : in_code);
5783
5784 /* Process depending on the code of this operation. If NEW is set
5785 non-zero, it will be returned. */
5786
5787 switch (code)
5788 {
5789 case ASHIFT:
230d793d
RS
5790 /* Convert shifts by constants into multiplications if inside
5791 an address. */
5792 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5f4f0e22 5793 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
230d793d 5794 && INTVAL (XEXP (x, 1)) >= 0)
280f58ba
RK
5795 {
5796 new = make_compound_operation (XEXP (x, 0), next_code);
5797 new = gen_rtx_combine (MULT, mode, new,
5798 GEN_INT ((HOST_WIDE_INT) 1
5799 << INTVAL (XEXP (x, 1))));
5800 }
230d793d
RS
5801 break;
5802
5803 case AND:
5804 /* If the second operand is not a constant, we can't do anything
5805 with it. */
5806 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5807 break;
5808
5809 /* If the constant is a power of two minus one and the first operand
5810 is a logical right shift, make an extraction. */
5811 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5812 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
280f58ba
RK
5813 {
5814 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5815 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5816 0, in_code == COMPARE);
5817 }
dfbe1b2f 5818
230d793d
RS
5819 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
5820 else if (GET_CODE (XEXP (x, 0)) == SUBREG
5821 && subreg_lowpart_p (XEXP (x, 0))
5822 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5823 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
280f58ba
RK
5824 {
5825 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5826 next_code);
2f99f437 5827 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
280f58ba
RK
5828 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5829 0, in_code == COMPARE);
5830 }
45620ed4 5831 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
c2f9f64e
JW
5832 else if ((GET_CODE (XEXP (x, 0)) == XOR
5833 || GET_CODE (XEXP (x, 0)) == IOR)
5834 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
5835 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
5836 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5837 {
5838 /* Apply the distributive law, and then try to make extractions. */
5839 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
5840 gen_rtx (AND, mode, XEXP (XEXP (x, 0), 0),
5841 XEXP (x, 1)),
5842 gen_rtx (AND, mode, XEXP (XEXP (x, 0), 1),
5843 XEXP (x, 1)));
5844 new = make_compound_operation (new, in_code);
5845 }
a7c99304
RK
5846
5847 /* If we are have (and (rotate X C) M) and C is larger than the number
5848 of bits in M, this is an extraction. */
5849
5850 else if (GET_CODE (XEXP (x, 0)) == ROTATE
5851 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5852 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5853 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
280f58ba
RK
5854 {
5855 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5856 new = make_extraction (mode, new,
5857 (GET_MODE_BITSIZE (mode)
5858 - INTVAL (XEXP (XEXP (x, 0), 1))),
5859 NULL_RTX, i, 1, 0, in_code == COMPARE);
5860 }
a7c99304
RK
5861
5862 /* On machines without logical shifts, if the operand of the AND is
230d793d
RS
5863 a logical shift and our mask turns off all the propagated sign
5864 bits, we can replace the logical shift with an arithmetic shift. */
d0ab8cd3
RK
5865 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5866 && (lshr_optab->handlers[(int) mode].insn_code
5867 == CODE_FOR_nothing)
230d793d
RS
5868 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5869 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5870 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5f4f0e22
CH
5871 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5872 && mode_width <= HOST_BITS_PER_WIDE_INT)
230d793d 5873 {
5f4f0e22 5874 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
230d793d
RS
5875
5876 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5877 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5878 SUBST (XEXP (x, 0),
280f58ba
RK
5879 gen_rtx_combine (ASHIFTRT, mode,
5880 make_compound_operation (XEXP (XEXP (x, 0), 0),
5881 next_code),
230d793d
RS
5882 XEXP (XEXP (x, 0), 1)));
5883 }
5884
5885 /* If the constant is one less than a power of two, this might be
5886 representable by an extraction even if no shift is present.
5887 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5888 we are in a COMPARE. */
5889 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
280f58ba
RK
5890 new = make_extraction (mode,
5891 make_compound_operation (XEXP (x, 0),
5892 next_code),
5893 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
230d793d
RS
5894
5895 /* If we are in a comparison and this is an AND with a power of two,
5896 convert this into the appropriate bit extract. */
5897 else if (in_code == COMPARE
5898 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
280f58ba
RK
5899 new = make_extraction (mode,
5900 make_compound_operation (XEXP (x, 0),
5901 next_code),
5902 i, NULL_RTX, 1, 1, 0, 1);
230d793d
RS
5903
5904 break;
5905
5906 case LSHIFTRT:
5907 /* If the sign bit is known to be zero, replace this with an
5908 arithmetic shift. */
d0ab8cd3
RK
5909 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5910 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5f4f0e22 5911 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 5912 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
230d793d 5913 {
280f58ba
RK
5914 new = gen_rtx_combine (ASHIFTRT, mode,
5915 make_compound_operation (XEXP (x, 0),
5916 next_code),
5917 XEXP (x, 1));
230d793d
RS
5918 break;
5919 }
5920
0f41302f 5921 /* ... fall through ... */
230d793d
RS
5922
5923 case ASHIFTRT:
71923da7
RK
5924 lhs = XEXP (x, 0);
5925 rhs = XEXP (x, 1);
5926
230d793d
RS
5927 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
5928 this is a SIGN_EXTRACT. */
71923da7
RK
5929 if (GET_CODE (rhs) == CONST_INT
5930 && GET_CODE (lhs) == ASHIFT
5931 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
5932 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
280f58ba 5933 {
71923da7 5934 new = make_compound_operation (XEXP (lhs, 0), next_code);
280f58ba 5935 new = make_extraction (mode, new,
71923da7
RK
5936 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
5937 NULL_RTX, mode_width - INTVAL (rhs),
d0ab8cd3
RK
5938 code == LSHIFTRT, 0, in_code == COMPARE);
5939 }
5940
71923da7
RK
5941 /* See if we have operations between an ASHIFTRT and an ASHIFT.
5942 If so, try to merge the shifts into a SIGN_EXTEND. We could
5943 also do this for some cases of SIGN_EXTRACT, but it doesn't
5944 seem worth the effort; the case checked for occurs on Alpha. */
5945
5946 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
5947 && ! (GET_CODE (lhs) == SUBREG
5948 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
5949 && GET_CODE (rhs) == CONST_INT
5950 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
5951 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
5952 new = make_extraction (mode, make_compound_operation (new, next_code),
5953 0, NULL_RTX, mode_width - INTVAL (rhs),
5954 code == LSHIFTRT, 0, in_code == COMPARE);
5955
230d793d 5956 break;
280f58ba
RK
5957
5958 case SUBREG:
5959 /* Call ourselves recursively on the inner expression. If we are
5960 narrowing the object and it has a different RTL code from
5961 what it originally did, do this SUBREG as a force_to_mode. */
5962
0a5cbff6 5963 tem = make_compound_operation (SUBREG_REG (x), in_code);
280f58ba
RK
5964 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
5965 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
5966 && subreg_lowpart_p (x))
0a5cbff6
RK
5967 {
5968 rtx newer = force_to_mode (tem, mode,
e3d616e3 5969 GET_MODE_MASK (mode), NULL_RTX, 0);
0a5cbff6
RK
5970
5971 /* If we have something other than a SUBREG, we might have
5972 done an expansion, so rerun outselves. */
5973 if (GET_CODE (newer) != SUBREG)
5974 newer = make_compound_operation (newer, in_code);
5975
5976 return newer;
5977 }
e9a25f70
JL
5978 break;
5979
5980 default:
5981 break;
230d793d
RS
5982 }
5983
5984 if (new)
5985 {
df62f951 5986 x = gen_lowpart_for_combine (mode, new);
230d793d
RS
5987 code = GET_CODE (x);
5988 }
5989
5990 /* Now recursively process each operand of this operation. */
5991 fmt = GET_RTX_FORMAT (code);
5992 for (i = 0; i < GET_RTX_LENGTH (code); i++)
5993 if (fmt[i] == 'e')
5994 {
5995 new = make_compound_operation (XEXP (x, i), next_code);
5996 SUBST (XEXP (x, i), new);
5997 }
5998
5999 return x;
6000}
6001\f
6002/* Given M see if it is a value that would select a field of bits
6003 within an item, but not the entire word. Return -1 if not.
6004 Otherwise, return the starting position of the field, where 0 is the
6005 low-order bit.
6006
6007 *PLEN is set to the length of the field. */
6008
6009static int
6010get_pos_from_mask (m, plen)
5f4f0e22 6011 unsigned HOST_WIDE_INT m;
230d793d
RS
6012 int *plen;
6013{
6014 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6015 int pos = exact_log2 (m & - m);
6016
6017 if (pos < 0)
6018 return -1;
6019
6020 /* Now shift off the low-order zero bits and see if we have a power of
6021 two minus 1. */
6022 *plen = exact_log2 ((m >> pos) + 1);
6023
6024 if (*plen <= 0)
6025 return -1;
6026
6027 return pos;
6028}
6029\f
6139ff20
RK
6030/* See if X can be simplified knowing that we will only refer to it in
6031 MODE and will only refer to those bits that are nonzero in MASK.
6032 If other bits are being computed or if masking operations are done
6033 that select a superset of the bits in MASK, they can sometimes be
6034 ignored.
6035
6036 Return a possibly simplified expression, but always convert X to
6037 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
dfbe1b2f
RK
6038
6039 Also, if REG is non-zero and X is a register equal in value to REG,
e3d616e3
RK
6040 replace X with REG.
6041
6042 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6043 are all off in X. This is used when X will be complemented, by either
180b8e4b 6044 NOT, NEG, or XOR. */
dfbe1b2f
RK
6045
6046static rtx
e3d616e3 6047force_to_mode (x, mode, mask, reg, just_select)
dfbe1b2f
RK
6048 rtx x;
6049 enum machine_mode mode;
6139ff20 6050 unsigned HOST_WIDE_INT mask;
dfbe1b2f 6051 rtx reg;
e3d616e3 6052 int just_select;
dfbe1b2f
RK
6053{
6054 enum rtx_code code = GET_CODE (x);
180b8e4b 6055 int next_select = just_select || code == XOR || code == NOT || code == NEG;
ef026f91
RS
6056 enum machine_mode op_mode;
6057 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6139ff20
RK
6058 rtx op0, op1, temp;
6059
132d2040
RK
6060 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6061 code below will do the wrong thing since the mode of such an
6062 expression is VOIDmode. */
6063 if (code == CALL || code == ASM_OPERANDS)
246e00f2
RK
6064 return x;
6065
6139ff20
RK
6066 /* We want to perform the operation is its present mode unless we know
6067 that the operation is valid in MODE, in which case we do the operation
6068 in MODE. */
1c75dfa4
RK
6069 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6070 && code_to_optab[(int) code] != 0
ef026f91
RS
6071 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6072 != CODE_FOR_nothing))
6073 ? mode : GET_MODE (x));
e3d616e3 6074
aa988991
RS
6075 /* It is not valid to do a right-shift in a narrower mode
6076 than the one it came in with. */
6077 if ((code == LSHIFTRT || code == ASHIFTRT)
6078 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6079 op_mode = GET_MODE (x);
ef026f91
RS
6080
6081 /* Truncate MASK to fit OP_MODE. */
6082 if (op_mode)
6083 mask &= GET_MODE_MASK (op_mode);
6139ff20
RK
6084
6085 /* When we have an arithmetic operation, or a shift whose count we
6086 do not know, we need to assume that all bit the up to the highest-order
6087 bit in MASK will be needed. This is how we form such a mask. */
ef026f91
RS
6088 if (op_mode)
6089 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6090 ? GET_MODE_MASK (op_mode)
6091 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6092 else
6093 fuller_mask = ~ (HOST_WIDE_INT) 0;
6094
6095 /* Determine what bits of X are guaranteed to be (non)zero. */
6096 nonzero = nonzero_bits (x, mode);
6139ff20
RK
6097
6098 /* If none of the bits in X are needed, return a zero. */
e3d616e3 6099 if (! just_select && (nonzero & mask) == 0)
6139ff20 6100 return const0_rtx;
dfbe1b2f 6101
6139ff20
RK
6102 /* If X is a CONST_INT, return a new one. Do this here since the
6103 test below will fail. */
6104 if (GET_CODE (x) == CONST_INT)
ceb7983c
RK
6105 {
6106 HOST_WIDE_INT cval = INTVAL (x) & mask;
6107 int width = GET_MODE_BITSIZE (mode);
6108
6109 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6110 number, sign extend it. */
6111 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6112 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6113 cval |= (HOST_WIDE_INT) -1 << width;
6114
6115 return GEN_INT (cval);
6116 }
dfbe1b2f 6117
180b8e4b
RK
6118 /* If X is narrower than MODE and we want all the bits in X's mode, just
6119 get X in the proper mode. */
6120 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6121 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
dfbe1b2f
RK
6122 return gen_lowpart_for_combine (mode, x);
6123
71923da7
RK
6124 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6125 MASK are already known to be zero in X, we need not do anything. */
6126 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6139ff20
RK
6127 return x;
6128
dfbe1b2f
RK
6129 switch (code)
6130 {
6139ff20
RK
6131 case CLOBBER:
6132 /* If X is a (clobber (const_int)), return it since we know we are
0f41302f 6133 generating something that won't match. */
6139ff20
RK
6134 return x;
6135
6139ff20
RK
6136 case USE:
6137 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6138 spanned the boundary of the MEM. If we are now masking so it is
6139 within that boundary, we don't need the USE any more. */
f76b9db2
ILT
6140 if (! BITS_BIG_ENDIAN
6141 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
e3d616e3 6142 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
f76b9db2 6143 break;
6139ff20 6144
dfbe1b2f
RK
6145 case SIGN_EXTEND:
6146 case ZERO_EXTEND:
6147 case ZERO_EXTRACT:
6148 case SIGN_EXTRACT:
6149 x = expand_compound_operation (x);
6150 if (GET_CODE (x) != code)
e3d616e3 6151 return force_to_mode (x, mode, mask, reg, next_select);
dfbe1b2f
RK
6152 break;
6153
6154 case REG:
6155 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6156 || rtx_equal_p (reg, get_last_value (x))))
6157 x = reg;
6158 break;
6159
dfbe1b2f 6160 case SUBREG:
6139ff20 6161 if (subreg_lowpart_p (x)
180b8e4b
RK
6162 /* We can ignore the effect of this SUBREG if it narrows the mode or
6163 if the constant masks to zero all the bits the mode doesn't
6164 have. */
6139ff20
RK
6165 && ((GET_MODE_SIZE (GET_MODE (x))
6166 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6139ff20
RK
6167 || (0 == (mask
6168 & GET_MODE_MASK (GET_MODE (x))
180b8e4b 6169 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
e3d616e3 6170 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
dfbe1b2f
RK
6171 break;
6172
6173 case AND:
6139ff20
RK
6174 /* If this is an AND with a constant, convert it into an AND
6175 whose constant is the AND of that constant with MASK. If it
6176 remains an AND of MASK, delete it since it is redundant. */
dfbe1b2f 6177
2ca9ae17 6178 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
dfbe1b2f 6179 {
6139ff20
RK
6180 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6181 mask & INTVAL (XEXP (x, 1)));
dfbe1b2f
RK
6182
6183 /* If X is still an AND, see if it is an AND with a mask that
71923da7
RK
6184 is just some low-order bits. If so, and it is MASK, we don't
6185 need it. */
dfbe1b2f
RK
6186
6187 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6139ff20 6188 && INTVAL (XEXP (x, 1)) == mask)
dfbe1b2f 6189 x = XEXP (x, 0);
d0ab8cd3 6190
71923da7
RK
6191 /* If it remains an AND, try making another AND with the bits
6192 in the mode mask that aren't in MASK turned on. If the
6193 constant in the AND is wide enough, this might make a
6194 cheaper constant. */
6195
6196 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
2ca9ae17
JW
6197 && GET_MODE_MASK (GET_MODE (x)) != mask
6198 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
71923da7
RK
6199 {
6200 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6201 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6202 int width = GET_MODE_BITSIZE (GET_MODE (x));
6203 rtx y;
6204
6205 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6206 number, sign extend it. */
6207 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6208 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6209 cval |= (HOST_WIDE_INT) -1 << width;
6210
6211 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6212 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6213 x = y;
6214 }
6215
d0ab8cd3 6216 break;
dfbe1b2f
RK
6217 }
6218
6139ff20 6219 goto binop;
dfbe1b2f
RK
6220
6221 case PLUS:
6139ff20
RK
6222 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6223 low-order bits (as in an alignment operation) and FOO is already
6224 aligned to that boundary, mask C1 to that boundary as well.
6225 This may eliminate that PLUS and, later, the AND. */
9fa6d012
TG
6226
6227 {
6228 int width = GET_MODE_BITSIZE (mode);
6229 unsigned HOST_WIDE_INT smask = mask;
6230
6231 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6232 number, sign extend it. */
6233
6234 if (width < HOST_BITS_PER_WIDE_INT
6235 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6236 smask |= (HOST_WIDE_INT) -1 << width;
6237
6238 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6239 && exact_log2 (- smask) >= 0
6240 && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
6241 && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
6242 return force_to_mode (plus_constant (XEXP (x, 0),
6243 INTVAL (XEXP (x, 1)) & mask),
6244 mode, mask, reg, next_select);
6245 }
6139ff20 6246
0f41302f 6247 /* ... fall through ... */
6139ff20 6248
dfbe1b2f
RK
6249 case MINUS:
6250 case MULT:
6139ff20
RK
6251 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6252 most significant bit in MASK since carries from those bits will
6253 affect the bits we are interested in. */
6254 mask = fuller_mask;
6255 goto binop;
6256
dfbe1b2f
RK
6257 case IOR:
6258 case XOR:
6139ff20
RK
6259 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6260 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6261 operation which may be a bitfield extraction. Ensure that the
6262 constant we form is not wider than the mode of X. */
6263
6264 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6265 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6266 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6267 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6268 && GET_CODE (XEXP (x, 1)) == CONST_INT
6269 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6270 + floor_log2 (INTVAL (XEXP (x, 1))))
6271 < GET_MODE_BITSIZE (GET_MODE (x)))
6272 && (INTVAL (XEXP (x, 1))
01c82bbb 6273 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6139ff20
RK
6274 {
6275 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6276 << INTVAL (XEXP (XEXP (x, 0), 1)));
6277 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6278 XEXP (XEXP (x, 0), 0), temp);
d4d2b13f
RK
6279 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6280 XEXP (XEXP (x, 0), 1));
e3d616e3 6281 return force_to_mode (x, mode, mask, reg, next_select);
6139ff20
RK
6282 }
6283
6284 binop:
dfbe1b2f 6285 /* For most binary operations, just propagate into the operation and
6139ff20
RK
6286 change the mode if we have an operation of that mode. */
6287
e3d616e3
RK
6288 op0 = gen_lowpart_for_combine (op_mode,
6289 force_to_mode (XEXP (x, 0), mode, mask,
6290 reg, next_select));
6291 op1 = gen_lowpart_for_combine (op_mode,
6292 force_to_mode (XEXP (x, 1), mode, mask,
6293 reg, next_select));
6139ff20 6294
2dd484ed
RK
6295 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6296 MASK since OP1 might have been sign-extended but we never want
6297 to turn on extra bits, since combine might have previously relied
6298 on them being off. */
6299 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6300 && (INTVAL (op1) & mask) != 0)
6301 op1 = GEN_INT (INTVAL (op1) & mask);
6302
6139ff20
RK
6303 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6304 x = gen_binary (code, op_mode, op0, op1);
d0ab8cd3 6305 break;
dfbe1b2f
RK
6306
6307 case ASHIFT:
dfbe1b2f 6308 /* For left shifts, do the same, but just for the first operand.
f6785026
RK
6309 However, we cannot do anything with shifts where we cannot
6310 guarantee that the counts are smaller than the size of the mode
6311 because such a count will have a different meaning in a
6139ff20 6312 wider mode. */
f6785026
RK
6313
6314 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6139ff20 6315 && INTVAL (XEXP (x, 1)) >= 0
f6785026
RK
6316 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6317 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6318 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
adb7a1cb 6319 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
f6785026
RK
6320 break;
6321
6139ff20
RK
6322 /* If the shift count is a constant and we can do arithmetic in
6323 the mode of the shift, refine which bits we need. Otherwise, use the
6324 conservative form of the mask. */
6325 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6326 && INTVAL (XEXP (x, 1)) >= 0
6327 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6328 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6329 mask >>= INTVAL (XEXP (x, 1));
6330 else
6331 mask = fuller_mask;
6332
6333 op0 = gen_lowpart_for_combine (op_mode,
6334 force_to_mode (XEXP (x, 0), op_mode,
e3d616e3 6335 mask, reg, next_select));
6139ff20
RK
6336
6337 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6338 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
d0ab8cd3 6339 break;
dfbe1b2f
RK
6340
6341 case LSHIFTRT:
1347292b
JW
6342 /* Here we can only do something if the shift count is a constant,
6343 this shift constant is valid for the host, and we can do arithmetic
6344 in OP_MODE. */
dfbe1b2f
RK
6345
6346 if (GET_CODE (XEXP (x, 1)) == CONST_INT
1347292b 6347 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6139ff20 6348 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
d0ab8cd3 6349 {
6139ff20
RK
6350 rtx inner = XEXP (x, 0);
6351
6352 /* Select the mask of the bits we need for the shift operand. */
6353 mask <<= INTVAL (XEXP (x, 1));
d0ab8cd3 6354
6139ff20
RK
6355 /* We can only change the mode of the shift if we can do arithmetic
6356 in the mode of the shift and MASK is no wider than the width of
6357 OP_MODE. */
6358 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6359 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
d0ab8cd3
RK
6360 op_mode = GET_MODE (x);
6361
e3d616e3 6362 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6139ff20
RK
6363
6364 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6365 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
d0ab8cd3 6366 }
6139ff20
RK
6367
6368 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6369 shift and AND produces only copies of the sign bit (C2 is one less
6370 than a power of two), we can do this with just a shift. */
6371
6372 if (GET_CODE (x) == LSHIFTRT
6373 && GET_CODE (XEXP (x, 1)) == CONST_INT
6374 && ((INTVAL (XEXP (x, 1))
6375 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6376 >= GET_MODE_BITSIZE (GET_MODE (x)))
6377 && exact_log2 (mask + 1) >= 0
6378 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6379 >= exact_log2 (mask + 1)))
6380 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6381 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6382 - exact_log2 (mask + 1)));
d0ab8cd3
RK
6383 break;
6384
6385 case ASHIFTRT:
6139ff20
RK
6386 /* If we are just looking for the sign bit, we don't need this shift at
6387 all, even if it has a variable count. */
9bf22b75
RK
6388 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6389 && (mask == ((HOST_WIDE_INT) 1
6390 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
e3d616e3 6391 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6139ff20
RK
6392
6393 /* If this is a shift by a constant, get a mask that contains those bits
6394 that are not copies of the sign bit. We then have two cases: If
6395 MASK only includes those bits, this can be a logical shift, which may
6396 allow simplifications. If MASK is a single-bit field not within
6397 those bits, we are requesting a copy of the sign bit and hence can
6398 shift the sign bit to the appropriate location. */
6399
6400 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6401 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6402 {
6403 int i = -1;
6404
b69960ac
RK
6405 /* If the considered data is wider then HOST_WIDE_INT, we can't
6406 represent a mask for all its bits in a single scalar.
6407 But we only care about the lower bits, so calculate these. */
6408
6a11342f 6409 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
b69960ac 6410 {
0f41302f 6411 nonzero = ~ (HOST_WIDE_INT) 0;
b69960ac
RK
6412
6413 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6414 is the number of bits a full-width mask would have set.
6415 We need only shift if these are fewer than nonzero can
6416 hold. If not, we must keep all bits set in nonzero. */
6417
6418 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6419 < HOST_BITS_PER_WIDE_INT)
6420 nonzero >>= INTVAL (XEXP (x, 1))
6421 + HOST_BITS_PER_WIDE_INT
6422 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6423 }
6424 else
6425 {
6426 nonzero = GET_MODE_MASK (GET_MODE (x));
6427 nonzero >>= INTVAL (XEXP (x, 1));
6428 }
6139ff20
RK
6429
6430 if ((mask & ~ nonzero) == 0
6431 || (i = exact_log2 (mask)) >= 0)
6432 {
6433 x = simplify_shift_const
6434 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6435 i < 0 ? INTVAL (XEXP (x, 1))
6436 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6437
6438 if (GET_CODE (x) != ASHIFTRT)
e3d616e3 6439 return force_to_mode (x, mode, mask, reg, next_select);
6139ff20
RK
6440 }
6441 }
6442
6443 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6444 even if the shift count isn't a constant. */
6445 if (mask == 1)
6446 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6447
d0ab8cd3 6448 /* If this is a sign-extension operation that just affects bits
4c002f29
RK
6449 we don't care about, remove it. Be sure the call above returned
6450 something that is still a shift. */
d0ab8cd3 6451
4c002f29
RK
6452 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6453 && GET_CODE (XEXP (x, 1)) == CONST_INT
d0ab8cd3 6454 && INTVAL (XEXP (x, 1)) >= 0
6139ff20
RK
6455 && (INTVAL (XEXP (x, 1))
6456 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
d0ab8cd3
RK
6457 && GET_CODE (XEXP (x, 0)) == ASHIFT
6458 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6459 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
e3d616e3
RK
6460 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6461 reg, next_select);
6139ff20 6462
dfbe1b2f
RK
6463 break;
6464
6139ff20
RK
6465 case ROTATE:
6466 case ROTATERT:
6467 /* If the shift count is constant and we can do computations
6468 in the mode of X, compute where the bits we care about are.
6469 Otherwise, we can't do anything. Don't change the mode of
6470 the shift or propagate MODE into the shift, though. */
6471 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6472 && INTVAL (XEXP (x, 1)) >= 0)
6473 {
6474 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6475 GET_MODE (x), GEN_INT (mask),
6476 XEXP (x, 1));
7d171a1e 6477 if (temp && GET_CODE(temp) == CONST_INT)
6139ff20
RK
6478 SUBST (XEXP (x, 0),
6479 force_to_mode (XEXP (x, 0), GET_MODE (x),
e3d616e3 6480 INTVAL (temp), reg, next_select));
6139ff20
RK
6481 }
6482 break;
6483
dfbe1b2f 6484 case NEG:
180b8e4b
RK
6485 /* If we just want the low-order bit, the NEG isn't needed since it
6486 won't change the low-order bit. */
6487 if (mask == 1)
6488 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6489
6139ff20
RK
6490 /* We need any bits less significant than the most significant bit in
6491 MASK since carries from those bits will affect the bits we are
6492 interested in. */
6493 mask = fuller_mask;
6494 goto unop;
6495
dfbe1b2f 6496 case NOT:
6139ff20
RK
6497 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6498 same as the XOR case above. Ensure that the constant we form is not
6499 wider than the mode of X. */
6500
6501 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6502 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6503 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6504 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6505 < GET_MODE_BITSIZE (GET_MODE (x)))
6506 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6507 {
6508 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6509 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6510 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6511
e3d616e3 6512 return force_to_mode (x, mode, mask, reg, next_select);
6139ff20
RK
6513 }
6514
f82da7d2
JW
6515 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6516 use the full mask inside the NOT. */
6517 mask = fuller_mask;
6518
6139ff20 6519 unop:
e3d616e3
RK
6520 op0 = gen_lowpart_for_combine (op_mode,
6521 force_to_mode (XEXP (x, 0), mode, mask,
6522 reg, next_select));
6139ff20 6523 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
0c1c8ea6 6524 x = gen_unary (code, op_mode, op_mode, op0);
6139ff20
RK
6525 break;
6526
6527 case NE:
6528 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
3aceff0d 6529 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
1a6ec070 6530 which is equal to STORE_FLAG_VALUE. */
3aceff0d
RK
6531 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6532 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
1a6ec070 6533 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
e3d616e3 6534 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6139ff20 6535
d0ab8cd3
RK
6536 break;
6537
6538 case IF_THEN_ELSE:
6539 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6540 written in a narrower mode. We play it safe and do not do so. */
6541
6542 SUBST (XEXP (x, 1),
6543 gen_lowpart_for_combine (GET_MODE (x),
6544 force_to_mode (XEXP (x, 1), mode,
e3d616e3 6545 mask, reg, next_select)));
d0ab8cd3
RK
6546 SUBST (XEXP (x, 2),
6547 gen_lowpart_for_combine (GET_MODE (x),
6548 force_to_mode (XEXP (x, 2), mode,
e3d616e3 6549 mask, reg,next_select)));
d0ab8cd3 6550 break;
e9a25f70
JL
6551
6552 default:
6553 break;
dfbe1b2f
RK
6554 }
6555
d0ab8cd3 6556 /* Ensure we return a value of the proper mode. */
dfbe1b2f
RK
6557 return gen_lowpart_for_combine (mode, x);
6558}
6559\f
abe6e52f
RK
6560/* Return nonzero if X is an expression that has one of two values depending on
6561 whether some other value is zero or nonzero. In that case, we return the
6562 value that is being tested, *PTRUE is set to the value if the rtx being
6563 returned has a nonzero value, and *PFALSE is set to the other alternative.
6564
6565 If we return zero, we set *PTRUE and *PFALSE to X. */
6566
6567static rtx
6568if_then_else_cond (x, ptrue, pfalse)
6569 rtx x;
6570 rtx *ptrue, *pfalse;
6571{
6572 enum machine_mode mode = GET_MODE (x);
6573 enum rtx_code code = GET_CODE (x);
6574 int size = GET_MODE_BITSIZE (mode);
6575 rtx cond0, cond1, true0, true1, false0, false1;
6576 unsigned HOST_WIDE_INT nz;
6577
6578 /* If this is a unary operation whose operand has one of two values, apply
6579 our opcode to compute those values. */
6580 if (GET_RTX_CLASS (code) == '1'
6581 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6582 {
0c1c8ea6
RK
6583 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6584 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
abe6e52f
RK
6585 return cond0;
6586 }
6587
3a19aabc 6588 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
ddd5a7c1 6589 make can't possibly match and would suppress other optimizations. */
3a19aabc
RK
6590 else if (code == COMPARE)
6591 ;
6592
abe6e52f
RK
6593 /* If this is a binary operation, see if either side has only one of two
6594 values. If either one does or if both do and they are conditional on
6595 the same value, compute the new true and false values. */
6596 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6597 || GET_RTX_CLASS (code) == '<')
6598 {
6599 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6600 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6601
6602 if ((cond0 != 0 || cond1 != 0)
6603 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6604 {
987e845a
JW
6605 /* If if_then_else_cond returned zero, then true/false are the
6606 same rtl. We must copy one of them to prevent invalid rtl
6607 sharing. */
6608 if (cond0 == 0)
6609 true0 = copy_rtx (true0);
6610 else if (cond1 == 0)
6611 true1 = copy_rtx (true1);
6612
abe6e52f
RK
6613 *ptrue = gen_binary (code, mode, true0, true1);
6614 *pfalse = gen_binary (code, mode, false0, false1);
6615 return cond0 ? cond0 : cond1;
6616 }
9210df58 6617
9210df58 6618 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
0802d516
RK
6619 operands is zero when the other is non-zero, and vice-versa,
6620 and STORE_FLAG_VALUE is 1 or -1. */
9210df58 6621
0802d516
RK
6622 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6623 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9210df58
RK
6624 || code == UMAX)
6625 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6626 {
6627 rtx op0 = XEXP (XEXP (x, 0), 1);
6628 rtx op1 = XEXP (XEXP (x, 1), 1);
6629
6630 cond0 = XEXP (XEXP (x, 0), 0);
6631 cond1 = XEXP (XEXP (x, 1), 0);
6632
6633 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6634 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6635 && reversible_comparison_p (cond1)
6636 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6637 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6638 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6639 || ((swap_condition (GET_CODE (cond0))
6640 == reverse_condition (GET_CODE (cond1)))
6641 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6642 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6643 && ! side_effects_p (x))
6644 {
6645 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6646 *pfalse = gen_binary (MULT, mode,
6647 (code == MINUS
0c1c8ea6 6648 ? gen_unary (NEG, mode, mode, op1) : op1),
9210df58
RK
6649 const_true_rtx);
6650 return cond0;
6651 }
6652 }
6653
6654 /* Similarly for MULT, AND and UMIN, execpt that for these the result
6655 is always zero. */
0802d516
RK
6656 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6657 && (code == MULT || code == AND || code == UMIN)
9210df58
RK
6658 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6659 {
6660 cond0 = XEXP (XEXP (x, 0), 0);
6661 cond1 = XEXP (XEXP (x, 1), 0);
6662
6663 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6664 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6665 && reversible_comparison_p (cond1)
6666 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6667 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6668 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6669 || ((swap_condition (GET_CODE (cond0))
6670 == reverse_condition (GET_CODE (cond1)))
6671 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6672 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6673 && ! side_effects_p (x))
6674 {
6675 *ptrue = *pfalse = const0_rtx;
6676 return cond0;
6677 }
6678 }
abe6e52f
RK
6679 }
6680
6681 else if (code == IF_THEN_ELSE)
6682 {
6683 /* If we have IF_THEN_ELSE already, extract the condition and
6684 canonicalize it if it is NE or EQ. */
6685 cond0 = XEXP (x, 0);
6686 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6687 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6688 return XEXP (cond0, 0);
6689 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6690 {
6691 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6692 return XEXP (cond0, 0);
6693 }
6694 else
6695 return cond0;
6696 }
6697
6698 /* If X is a normal SUBREG with both inner and outer modes integral,
6699 we can narrow both the true and false values of the inner expression,
6700 if there is a condition. */
6701 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6702 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6703 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6704 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6705 &true0, &false0)))
6706 {
00244e6b
RK
6707 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6708 *pfalse
6709 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
abe6e52f 6710
abe6e52f
RK
6711 return cond0;
6712 }
6713
6714 /* If X is a constant, this isn't special and will cause confusions
6715 if we treat it as such. Likewise if it is equivalent to a constant. */
6716 else if (CONSTANT_P (x)
6717 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6718 ;
6719
6720 /* If X is known to be either 0 or -1, those are the true and
6721 false values when testing X. */
6722 else if (num_sign_bit_copies (x, mode) == size)
6723 {
6724 *ptrue = constm1_rtx, *pfalse = const0_rtx;
6725 return x;
6726 }
6727
6728 /* Likewise for 0 or a single bit. */
6729 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6730 {
6731 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6732 return x;
6733 }
6734
6735 /* Otherwise fail; show no condition with true and false values the same. */
6736 *ptrue = *pfalse = x;
6737 return 0;
6738}
6739\f
1a26b032
RK
6740/* Return the value of expression X given the fact that condition COND
6741 is known to be true when applied to REG as its first operand and VAL
6742 as its second. X is known to not be shared and so can be modified in
6743 place.
6744
6745 We only handle the simplest cases, and specifically those cases that
6746 arise with IF_THEN_ELSE expressions. */
6747
6748static rtx
6749known_cond (x, cond, reg, val)
6750 rtx x;
6751 enum rtx_code cond;
6752 rtx reg, val;
6753{
6754 enum rtx_code code = GET_CODE (x);
f24ad0e4 6755 rtx temp;
1a26b032
RK
6756 char *fmt;
6757 int i, j;
6758
6759 if (side_effects_p (x))
6760 return x;
6761
6762 if (cond == EQ && rtx_equal_p (x, reg))
6763 return val;
6764
6765 /* If X is (abs REG) and we know something about REG's relationship
6766 with zero, we may be able to simplify this. */
6767
6768 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
6769 switch (cond)
6770 {
6771 case GE: case GT: case EQ:
6772 return XEXP (x, 0);
6773 case LT: case LE:
0c1c8ea6
RK
6774 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
6775 XEXP (x, 0));
e9a25f70
JL
6776 default:
6777 break;
1a26b032
RK
6778 }
6779
6780 /* The only other cases we handle are MIN, MAX, and comparisons if the
6781 operands are the same as REG and VAL. */
6782
6783 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
6784 {
6785 if (rtx_equal_p (XEXP (x, 0), val))
6786 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
6787
6788 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
6789 {
6790 if (GET_RTX_CLASS (code) == '<')
6791 return (comparison_dominates_p (cond, code) ? const_true_rtx
6792 : (comparison_dominates_p (cond,
6793 reverse_condition (code))
6794 ? const0_rtx : x));
6795
6796 else if (code == SMAX || code == SMIN
6797 || code == UMIN || code == UMAX)
6798 {
6799 int unsignedp = (code == UMIN || code == UMAX);
6800
6801 if (code == SMAX || code == UMAX)
6802 cond = reverse_condition (cond);
6803
6804 switch (cond)
6805 {
6806 case GE: case GT:
6807 return unsignedp ? x : XEXP (x, 1);
6808 case LE: case LT:
6809 return unsignedp ? x : XEXP (x, 0);
6810 case GEU: case GTU:
6811 return unsignedp ? XEXP (x, 1) : x;
6812 case LEU: case LTU:
6813 return unsignedp ? XEXP (x, 0) : x;
e9a25f70
JL
6814 default:
6815 break;
1a26b032
RK
6816 }
6817 }
6818 }
6819 }
6820
6821 fmt = GET_RTX_FORMAT (code);
6822 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6823 {
6824 if (fmt[i] == 'e')
6825 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
6826 else if (fmt[i] == 'E')
6827 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6828 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
6829 cond, reg, val));
6830 }
6831
6832 return x;
6833}
6834\f
e11fa86f
RK
6835/* See if X and Y are equal for the purposes of seeing if we can rewrite an
6836 assignment as a field assignment. */
6837
6838static int
6839rtx_equal_for_field_assignment_p (x, y)
6840 rtx x;
6841 rtx y;
6842{
6843 rtx last_x, last_y;
6844
6845 if (x == y || rtx_equal_p (x, y))
6846 return 1;
6847
6848 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
6849 return 0;
6850
6851 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
6852 Note that all SUBREGs of MEM are paradoxical; otherwise they
6853 would have been rewritten. */
6854 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
6855 && GET_CODE (SUBREG_REG (y)) == MEM
6856 && rtx_equal_p (SUBREG_REG (y),
6857 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
6858 return 1;
6859
6860 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
6861 && GET_CODE (SUBREG_REG (x)) == MEM
6862 && rtx_equal_p (SUBREG_REG (x),
6863 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
6864 return 1;
6865
6866 last_x = get_last_value (x);
6867 last_y = get_last_value (y);
6868
0f47edd3
JL
6869 return ((last_x != 0
6870 && GET_CODE (last_x) != CLOBBER
6871 && rtx_equal_for_field_assignment_p (last_x, y))
6872 || (last_y != 0
6873 && GET_CODE (last_y) != CLOBBER
6874 && rtx_equal_for_field_assignment_p (x, last_y))
e11fa86f 6875 || (last_x != 0 && last_y != 0
0f47edd3
JL
6876 && GET_CODE (last_x) != CLOBBER
6877 && GET_CODE (last_y) != CLOBBER
e11fa86f
RK
6878 && rtx_equal_for_field_assignment_p (last_x, last_y)));
6879}
6880\f
230d793d
RS
6881/* See if X, a SET operation, can be rewritten as a bit-field assignment.
6882 Return that assignment if so.
6883
6884 We only handle the most common cases. */
6885
6886static rtx
6887make_field_assignment (x)
6888 rtx x;
6889{
6890 rtx dest = SET_DEST (x);
6891 rtx src = SET_SRC (x);
dfbe1b2f 6892 rtx assign;
e11fa86f 6893 rtx rhs, lhs;
5f4f0e22
CH
6894 HOST_WIDE_INT c1;
6895 int pos, len;
dfbe1b2f
RK
6896 rtx other;
6897 enum machine_mode mode;
230d793d
RS
6898
6899 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
6900 a clear of a one-bit field. We will have changed it to
6901 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
6902 for a SUBREG. */
6903
6904 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
6905 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
6906 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
e11fa86f 6907 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
230d793d 6908 {
8999a12e 6909 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
230d793d 6910 1, 1, 1, 0);
76184def
DE
6911 if (assign != 0)
6912 return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6913 return x;
230d793d
RS
6914 }
6915
6916 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
6917 && subreg_lowpart_p (XEXP (src, 0))
6918 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
6919 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
6920 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
6921 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
e11fa86f 6922 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
230d793d 6923 {
8999a12e 6924 assign = make_extraction (VOIDmode, dest, 0,
230d793d
RS
6925 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
6926 1, 1, 1, 0);
76184def
DE
6927 if (assign != 0)
6928 return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6929 return x;
230d793d
RS
6930 }
6931
9dd11dcb 6932 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
230d793d
RS
6933 one-bit field. */
6934 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
6935 && XEXP (XEXP (src, 0), 0) == const1_rtx
e11fa86f 6936 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
230d793d 6937 {
8999a12e 6938 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
230d793d 6939 1, 1, 1, 0);
76184def
DE
6940 if (assign != 0)
6941 return gen_rtx (SET, VOIDmode, assign, const1_rtx);
6942 return x;
230d793d
RS
6943 }
6944
dfbe1b2f 6945 /* The other case we handle is assignments into a constant-position
9dd11dcb 6946 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
dfbe1b2f
RK
6947 a mask that has all one bits except for a group of zero bits and
6948 OTHER is known to have zeros where C1 has ones, this is such an
6949 assignment. Compute the position and length from C1. Shift OTHER
6950 to the appropriate position, force it to the required mode, and
6951 make the extraction. Check for the AND in both operands. */
6952
9dd11dcb 6953 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
e11fa86f
RK
6954 return x;
6955
6956 rhs = expand_compound_operation (XEXP (src, 0));
6957 lhs = expand_compound_operation (XEXP (src, 1));
6958
6959 if (GET_CODE (rhs) == AND
6960 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
6961 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
6962 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
6963 else if (GET_CODE (lhs) == AND
6964 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6965 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
6966 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
dfbe1b2f
RK
6967 else
6968 return x;
230d793d 6969
e11fa86f 6970 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
dfbe1b2f 6971 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
ac49a949 6972 || (GET_MODE_BITSIZE (GET_MODE (other)) <= HOST_BITS_PER_WIDE_INT
951553af 6973 && (c1 & nonzero_bits (other, GET_MODE (other))) != 0))
dfbe1b2f 6974 return x;
230d793d 6975
5f4f0e22 6976 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
76184def
DE
6977 if (assign == 0)
6978 return x;
230d793d 6979
dfbe1b2f
RK
6980 /* The mode to use for the source is the mode of the assignment, or of
6981 what is inside a possible STRICT_LOW_PART. */
6982 mode = (GET_CODE (assign) == STRICT_LOW_PART
6983 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
230d793d 6984
dfbe1b2f
RK
6985 /* Shift OTHER right POS places and make it the source, restricting it
6986 to the proper length and mode. */
230d793d 6987
5f4f0e22
CH
6988 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
6989 GET_MODE (src), other, pos),
6139ff20
RK
6990 mode,
6991 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
6992 ? GET_MODE_MASK (mode)
6993 : ((HOST_WIDE_INT) 1 << len) - 1,
e3d616e3 6994 dest, 0);
230d793d 6995
dfbe1b2f 6996 return gen_rtx_combine (SET, VOIDmode, assign, src);
230d793d
RS
6997}
6998\f
6999/* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7000 if so. */
7001
7002static rtx
7003apply_distributive_law (x)
7004 rtx x;
7005{
7006 enum rtx_code code = GET_CODE (x);
7007 rtx lhs, rhs, other;
7008 rtx tem;
7009 enum rtx_code inner_code;
7010
d8a8a4da
RS
7011 /* Distributivity is not true for floating point.
7012 It can change the value. So don't do it.
7013 -- rms and moshier@world.std.com. */
3ad2180a 7014 if (FLOAT_MODE_P (GET_MODE (x)))
d8a8a4da
RS
7015 return x;
7016
230d793d
RS
7017 /* The outer operation can only be one of the following: */
7018 if (code != IOR && code != AND && code != XOR
7019 && code != PLUS && code != MINUS)
7020 return x;
7021
7022 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7023
0f41302f
MS
7024 /* If either operand is a primitive we can't do anything, so get out
7025 fast. */
230d793d 7026 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
dfbe1b2f 7027 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
230d793d
RS
7028 return x;
7029
7030 lhs = expand_compound_operation (lhs);
7031 rhs = expand_compound_operation (rhs);
7032 inner_code = GET_CODE (lhs);
7033 if (inner_code != GET_CODE (rhs))
7034 return x;
7035
7036 /* See if the inner and outer operations distribute. */
7037 switch (inner_code)
7038 {
7039 case LSHIFTRT:
7040 case ASHIFTRT:
7041 case AND:
7042 case IOR:
7043 /* These all distribute except over PLUS. */
7044 if (code == PLUS || code == MINUS)
7045 return x;
7046 break;
7047
7048 case MULT:
7049 if (code != PLUS && code != MINUS)
7050 return x;
7051 break;
7052
7053 case ASHIFT:
45620ed4 7054 /* This is also a multiply, so it distributes over everything. */
230d793d
RS
7055 break;
7056
7057 case SUBREG:
dfbe1b2f
RK
7058 /* Non-paradoxical SUBREGs distributes over all operations, provided
7059 the inner modes and word numbers are the same, this is an extraction
2b4bd1bc
JW
7060 of a low-order part, we don't convert an fp operation to int or
7061 vice versa, and we would not be converting a single-word
dfbe1b2f 7062 operation into a multi-word operation. The latter test is not
2b4bd1bc 7063 required, but it prevents generating unneeded multi-word operations.
dfbe1b2f
RK
7064 Some of the previous tests are redundant given the latter test, but
7065 are retained because they are required for correctness.
7066
7067 We produce the result slightly differently in this case. */
7068
7069 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7070 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7071 || ! subreg_lowpart_p (lhs)
2b4bd1bc
JW
7072 || (GET_MODE_CLASS (GET_MODE (lhs))
7073 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
dfbe1b2f 7074 || (GET_MODE_SIZE (GET_MODE (lhs))
8af24e26 7075 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
dfbe1b2f 7076 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
230d793d
RS
7077 return x;
7078
7079 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7080 SUBREG_REG (lhs), SUBREG_REG (rhs));
7081 return gen_lowpart_for_combine (GET_MODE (x), tem);
7082
7083 default:
7084 return x;
7085 }
7086
7087 /* Set LHS and RHS to the inner operands (A and B in the example
7088 above) and set OTHER to the common operand (C in the example).
7089 These is only one way to do this unless the inner operation is
7090 commutative. */
7091 if (GET_RTX_CLASS (inner_code) == 'c'
7092 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7093 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7094 else if (GET_RTX_CLASS (inner_code) == 'c'
7095 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7096 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7097 else if (GET_RTX_CLASS (inner_code) == 'c'
7098 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7099 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7100 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7101 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7102 else
7103 return x;
7104
7105 /* Form the new inner operation, seeing if it simplifies first. */
7106 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7107
7108 /* There is one exception to the general way of distributing:
7109 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7110 if (code == XOR && inner_code == IOR)
7111 {
7112 inner_code = AND;
0c1c8ea6 7113 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
230d793d
RS
7114 }
7115
7116 /* We may be able to continuing distributing the result, so call
7117 ourselves recursively on the inner operation before forming the
7118 outer operation, which we return. */
7119 return gen_binary (inner_code, GET_MODE (x),
7120 apply_distributive_law (tem), other);
7121}
7122\f
7123/* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7124 in MODE.
7125
7126 Return an equivalent form, if different from X. Otherwise, return X. If
7127 X is zero, we are to always construct the equivalent form. */
7128
7129static rtx
7130simplify_and_const_int (x, mode, varop, constop)
7131 rtx x;
7132 enum machine_mode mode;
7133 rtx varop;
5f4f0e22 7134 unsigned HOST_WIDE_INT constop;
230d793d 7135{
951553af 7136 unsigned HOST_WIDE_INT nonzero;
9fa6d012 7137 int width = GET_MODE_BITSIZE (mode);
42301240 7138 int i;
230d793d 7139
6139ff20
RK
7140 /* Simplify VAROP knowing that we will be only looking at some of the
7141 bits in it. */
e3d616e3 7142 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
230d793d 7143
6139ff20
RK
7144 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7145 CONST_INT, we are done. */
7146 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7147 return varop;
230d793d 7148
fc06d7aa
RK
7149 /* See what bits may be nonzero in VAROP. Unlike the general case of
7150 a call to nonzero_bits, here we don't care about bits outside
7151 MODE. */
7152
7153 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
230d793d 7154
9fa6d012
TG
7155 /* If this would be an entire word for the target, but is not for
7156 the host, then sign-extend on the host so that the number will look
7157 the same way on the host that it would on the target.
7158
7159 For example, when building a 64 bit alpha hosted 32 bit sparc
7160 targeted compiler, then we want the 32 bit unsigned value -1 to be
7161 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7162 The later confuses the sparc backend. */
7163
7164 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7165 && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
7166 nonzero |= ((HOST_WIDE_INT) (-1) << width);
7167
230d793d 7168 /* Turn off all bits in the constant that are known to already be zero.
951553af 7169 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
230d793d
RS
7170 which is tested below. */
7171
951553af 7172 constop &= nonzero;
230d793d
RS
7173
7174 /* If we don't have any bits left, return zero. */
7175 if (constop == 0)
7176 return const0_rtx;
7177
42301240
RK
7178 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7179 a power of two, we can replace this with a ASHIFT. */
7180 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7181 && (i = exact_log2 (constop)) >= 0)
7182 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7183
6139ff20
RK
7184 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7185 or XOR, then try to apply the distributive law. This may eliminate
7186 operations if either branch can be simplified because of the AND.
7187 It may also make some cases more complex, but those cases probably
7188 won't match a pattern either with or without this. */
7189
7190 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7191 return
7192 gen_lowpart_for_combine
7193 (mode,
7194 apply_distributive_law
7195 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7196 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7197 XEXP (varop, 0), constop),
7198 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7199 XEXP (varop, 1), constop))));
7200
230d793d
RS
7201 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7202 if we already had one (just check for the simplest cases). */
7203 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7204 && GET_MODE (XEXP (x, 0)) == mode
7205 && SUBREG_REG (XEXP (x, 0)) == varop)
7206 varop = XEXP (x, 0);
7207 else
7208 varop = gen_lowpart_for_combine (mode, varop);
7209
0f41302f 7210 /* If we can't make the SUBREG, try to return what we were given. */
230d793d
RS
7211 if (GET_CODE (varop) == CLOBBER)
7212 return x ? x : varop;
7213
7214 /* If we are only masking insignificant bits, return VAROP. */
951553af 7215 if (constop == nonzero)
230d793d
RS
7216 x = varop;
7217
7218 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7219 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
6139ff20 7220 x = gen_binary (AND, mode, varop, GEN_INT (constop));
230d793d
RS
7221
7222 else
7223 {
7224 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7225 || INTVAL (XEXP (x, 1)) != constop)
5f4f0e22 7226 SUBST (XEXP (x, 1), GEN_INT (constop));
230d793d
RS
7227
7228 SUBST (XEXP (x, 0), varop);
7229 }
7230
7231 return x;
7232}
7233\f
b3728b0e
JW
7234/* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7235 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7236 is less useful. We can't allow both, because that results in exponential
956d6950 7237 run time recursion. There is a nullstone testcase that triggered
b3728b0e
JW
7238 this. This macro avoids accidental uses of num_sign_bit_copies. */
7239#define num_sign_bit_copies()
7240
230d793d
RS
7241/* Given an expression, X, compute which bits in X can be non-zero.
7242 We don't care about bits outside of those defined in MODE.
7243
7244 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7245 a shift, AND, or zero_extract, we can do better. */
7246
5f4f0e22 7247static unsigned HOST_WIDE_INT
951553af 7248nonzero_bits (x, mode)
230d793d
RS
7249 rtx x;
7250 enum machine_mode mode;
7251{
951553af
RK
7252 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7253 unsigned HOST_WIDE_INT inner_nz;
230d793d
RS
7254 enum rtx_code code;
7255 int mode_width = GET_MODE_BITSIZE (mode);
7256 rtx tem;
7257
1c75dfa4
RK
7258 /* For floating-point values, assume all bits are needed. */
7259 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7260 return nonzero;
7261
230d793d
RS
7262 /* If X is wider than MODE, use its mode instead. */
7263 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7264 {
7265 mode = GET_MODE (x);
951553af 7266 nonzero = GET_MODE_MASK (mode);
230d793d
RS
7267 mode_width = GET_MODE_BITSIZE (mode);
7268 }
7269
5f4f0e22 7270 if (mode_width > HOST_BITS_PER_WIDE_INT)
230d793d
RS
7271 /* Our only callers in this case look for single bit values. So
7272 just return the mode mask. Those tests will then be false. */
951553af 7273 return nonzero;
230d793d 7274
8baf60bb 7275#ifndef WORD_REGISTER_OPERATIONS
c6965c0f 7276 /* If MODE is wider than X, but both are a single word for both the host
0840fd91
RK
7277 and target machines, we can compute this from which bits of the
7278 object might be nonzero in its own mode, taking into account the fact
7279 that on many CISC machines, accessing an object in a wider mode
7280 causes the high-order bits to become undefined. So they are
7281 not known to be zero. */
7282
7283 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7284 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7285 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
c6965c0f 7286 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
0840fd91
RK
7287 {
7288 nonzero &= nonzero_bits (x, GET_MODE (x));
7289 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7290 return nonzero;
7291 }
7292#endif
7293
230d793d
RS
7294 code = GET_CODE (x);
7295 switch (code)
7296 {
7297 case REG:
320dd7a7
RK
7298#ifdef POINTERS_EXTEND_UNSIGNED
7299 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7300 all the bits above ptr_mode are known to be zero. */
7301 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7302 && REGNO_POINTER_FLAG (REGNO (x)))
7303 nonzero &= GET_MODE_MASK (ptr_mode);
7304#endif
7305
b0d71df9
RK
7306#ifdef STACK_BOUNDARY
7307 /* If this is the stack pointer, we may know something about its
7308 alignment. If PUSH_ROUNDING is defined, it is possible for the
230d793d
RS
7309 stack to be momentarily aligned only to that amount, so we pick
7310 the least alignment. */
7311
ee49a9c7
JW
7312 /* We can't check for arg_pointer_rtx here, because it is not
7313 guaranteed to have as much alignment as the stack pointer.
7314 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7315 alignment but the argument pointer has only 64 bit alignment. */
7316
b0d71df9 7317 if (x == stack_pointer_rtx || x == frame_pointer_rtx
ee49a9c7 7318 || x == hard_frame_pointer_rtx
b0d71df9
RK
7319 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7320 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
230d793d 7321 {
b0d71df9 7322 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
230d793d
RS
7323
7324#ifdef PUSH_ROUNDING
91102d5a 7325 if (REGNO (x) == STACK_POINTER_REGNUM)
b0d71df9 7326 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
230d793d
RS
7327#endif
7328
320dd7a7
RK
7329 /* We must return here, otherwise we may get a worse result from
7330 one of the choices below. There is nothing useful below as
7331 far as the stack pointer is concerned. */
b0d71df9 7332 return nonzero &= ~ (sp_alignment - 1);
230d793d 7333 }
b0d71df9 7334#endif
230d793d 7335
55310dad
RK
7336 /* If X is a register whose nonzero bits value is current, use it.
7337 Otherwise, if X is a register whose value we can find, use that
7338 value. Otherwise, use the previously-computed global nonzero bits
7339 for this register. */
7340
7341 if (reg_last_set_value[REGNO (x)] != 0
7342 && reg_last_set_mode[REGNO (x)] == mode
b1f21e0a 7343 && (REG_N_SETS (REGNO (x)) == 1
55310dad
RK
7344 || reg_last_set_label[REGNO (x)] == label_tick)
7345 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7346 return reg_last_set_nonzero_bits[REGNO (x)];
230d793d
RS
7347
7348 tem = get_last_value (x);
9afa3d54 7349
230d793d 7350 if (tem)
9afa3d54
RK
7351 {
7352#ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7353 /* If X is narrower than MODE and TEM is a non-negative
7354 constant that would appear negative in the mode of X,
7355 sign-extend it for use in reg_nonzero_bits because some
7356 machines (maybe most) will actually do the sign-extension
7357 and this is the conservative approach.
7358
7359 ??? For 2.5, try to tighten up the MD files in this regard
7360 instead of this kludge. */
7361
7362 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7363 && GET_CODE (tem) == CONST_INT
7364 && INTVAL (tem) > 0
7365 && 0 != (INTVAL (tem)
7366 & ((HOST_WIDE_INT) 1
9e69be8c 7367 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
9afa3d54
RK
7368 tem = GEN_INT (INTVAL (tem)
7369 | ((HOST_WIDE_INT) (-1)
7370 << GET_MODE_BITSIZE (GET_MODE (x))));
7371#endif
7372 return nonzero_bits (tem, mode);
7373 }
951553af
RK
7374 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7375 return reg_nonzero_bits[REGNO (x)] & nonzero;
230d793d 7376 else
951553af 7377 return nonzero;
230d793d
RS
7378
7379 case CONST_INT:
9afa3d54
RK
7380#ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7381 /* If X is negative in MODE, sign-extend the value. */
9e69be8c
RK
7382 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7383 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7384 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
9afa3d54
RK
7385#endif
7386
230d793d
RS
7387 return INTVAL (x);
7388
230d793d 7389 case MEM:
8baf60bb 7390#ifdef LOAD_EXTEND_OP
230d793d
RS
7391 /* In many, if not most, RISC machines, reading a byte from memory
7392 zeros the rest of the register. Noticing that fact saves a lot
7393 of extra zero-extends. */
8baf60bb
RK
7394 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7395 nonzero &= GET_MODE_MASK (GET_MODE (x));
230d793d 7396#endif
8baf60bb 7397 break;
230d793d 7398
230d793d
RS
7399 case EQ: case NE:
7400 case GT: case GTU:
7401 case LT: case LTU:
7402 case GE: case GEU:
7403 case LE: case LEU:
3f508eca 7404
c6965c0f
RK
7405 /* If this produces an integer result, we know which bits are set.
7406 Code here used to clear bits outside the mode of X, but that is
7407 now done above. */
230d793d 7408
c6965c0f
RK
7409 if (GET_MODE_CLASS (mode) == MODE_INT
7410 && mode_width <= HOST_BITS_PER_WIDE_INT)
7411 nonzero = STORE_FLAG_VALUE;
230d793d 7412 break;
230d793d 7413
230d793d 7414 case NEG:
b3728b0e
JW
7415#if 0
7416 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7417 and num_sign_bit_copies. */
d0ab8cd3
RK
7418 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7419 == GET_MODE_BITSIZE (GET_MODE (x)))
951553af 7420 nonzero = 1;
b3728b0e 7421#endif
230d793d
RS
7422
7423 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
951553af 7424 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
230d793d 7425 break;
d0ab8cd3
RK
7426
7427 case ABS:
b3728b0e
JW
7428#if 0
7429 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7430 and num_sign_bit_copies. */
d0ab8cd3
RK
7431 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7432 == GET_MODE_BITSIZE (GET_MODE (x)))
951553af 7433 nonzero = 1;
b3728b0e 7434#endif
d0ab8cd3 7435 break;
230d793d
RS
7436
7437 case TRUNCATE:
951553af 7438 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
230d793d
RS
7439 break;
7440
7441 case ZERO_EXTEND:
951553af 7442 nonzero &= nonzero_bits (XEXP (x, 0), mode);
230d793d 7443 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
951553af 7444 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
230d793d
RS
7445 break;
7446
7447 case SIGN_EXTEND:
7448 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7449 Otherwise, show all the bits in the outer mode but not the inner
7450 may be non-zero. */
951553af 7451 inner_nz = nonzero_bits (XEXP (x, 0), mode);
230d793d
RS
7452 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7453 {
951553af 7454 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
e3da301d
MS
7455 if (inner_nz
7456 & (((HOST_WIDE_INT) 1
7457 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
951553af 7458 inner_nz |= (GET_MODE_MASK (mode)
230d793d
RS
7459 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7460 }
7461
951553af 7462 nonzero &= inner_nz;
230d793d
RS
7463 break;
7464
7465 case AND:
951553af
RK
7466 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7467 & nonzero_bits (XEXP (x, 1), mode));
230d793d
RS
7468 break;
7469
d0ab8cd3
RK
7470 case XOR: case IOR:
7471 case UMIN: case UMAX: case SMIN: case SMAX:
951553af
RK
7472 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7473 | nonzero_bits (XEXP (x, 1), mode));
230d793d
RS
7474 break;
7475
7476 case PLUS: case MINUS:
7477 case MULT:
7478 case DIV: case UDIV:
7479 case MOD: case UMOD:
7480 /* We can apply the rules of arithmetic to compute the number of
7481 high- and low-order zero bits of these operations. We start by
7482 computing the width (position of the highest-order non-zero bit)
7483 and the number of low-order zero bits for each value. */
7484 {
951553af
RK
7485 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7486 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7487 int width0 = floor_log2 (nz0) + 1;
7488 int width1 = floor_log2 (nz1) + 1;
7489 int low0 = floor_log2 (nz0 & -nz0);
7490 int low1 = floor_log2 (nz1 & -nz1);
318b149c
RK
7491 HOST_WIDE_INT op0_maybe_minusp
7492 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7493 HOST_WIDE_INT op1_maybe_minusp
7494 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
230d793d
RS
7495 int result_width = mode_width;
7496 int result_low = 0;
7497
7498 switch (code)
7499 {
7500 case PLUS:
7501 result_width = MAX (width0, width1) + 1;
7502 result_low = MIN (low0, low1);
7503 break;
7504 case MINUS:
7505 result_low = MIN (low0, low1);
7506 break;
7507 case MULT:
7508 result_width = width0 + width1;
7509 result_low = low0 + low1;
7510 break;
7511 case DIV:
7512 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7513 result_width = width0;
7514 break;
7515 case UDIV:
7516 result_width = width0;
7517 break;
7518 case MOD:
7519 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7520 result_width = MIN (width0, width1);
7521 result_low = MIN (low0, low1);
7522 break;
7523 case UMOD:
7524 result_width = MIN (width0, width1);
7525 result_low = MIN (low0, low1);
7526 break;
e9a25f70
JL
7527 default:
7528 abort ();
230d793d
RS
7529 }
7530
7531 if (result_width < mode_width)
951553af 7532 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
230d793d
RS
7533
7534 if (result_low > 0)
951553af 7535 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
230d793d
RS
7536 }
7537 break;
7538
7539 case ZERO_EXTRACT:
7540 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5f4f0e22 7541 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
951553af 7542 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
230d793d
RS
7543 break;
7544
7545 case SUBREG:
c3c2cb37
RK
7546 /* If this is a SUBREG formed for a promoted variable that has
7547 been zero-extended, we know that at least the high-order bits
7548 are zero, though others might be too. */
7549
7550 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
951553af
RK
7551 nonzero = (GET_MODE_MASK (GET_MODE (x))
7552 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
c3c2cb37 7553
230d793d
RS
7554 /* If the inner mode is a single word for both the host and target
7555 machines, we can compute this from which bits of the inner
951553af 7556 object might be nonzero. */
230d793d 7557 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
5f4f0e22
CH
7558 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7559 <= HOST_BITS_PER_WIDE_INT))
230d793d 7560 {
951553af 7561 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
8baf60bb
RK
7562
7563#ifndef WORD_REGISTER_OPERATIONS
230d793d
RS
7564 /* On many CISC machines, accessing an object in a wider mode
7565 causes the high-order bits to become undefined. So they are
7566 not known to be zero. */
7567 if (GET_MODE_SIZE (GET_MODE (x))
7568 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
951553af
RK
7569 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7570 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
230d793d
RS
7571#endif
7572 }
7573 break;
7574
7575 case ASHIFTRT:
7576 case LSHIFTRT:
7577 case ASHIFT:
230d793d 7578 case ROTATE:
951553af 7579 /* The nonzero bits are in two classes: any bits within MODE
230d793d 7580 that aren't in GET_MODE (x) are always significant. The rest of the
951553af 7581 nonzero bits are those that are significant in the operand of
230d793d
RS
7582 the shift when shifted the appropriate number of bits. This
7583 shows that high-order bits are cleared by the right shift and
7584 low-order bits by left shifts. */
7585 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7586 && INTVAL (XEXP (x, 1)) >= 0
5f4f0e22 7587 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
230d793d
RS
7588 {
7589 enum machine_mode inner_mode = GET_MODE (x);
7590 int width = GET_MODE_BITSIZE (inner_mode);
7591 int count = INTVAL (XEXP (x, 1));
5f4f0e22 7592 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
951553af
RK
7593 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7594 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
5f4f0e22 7595 unsigned HOST_WIDE_INT outer = 0;
230d793d
RS
7596
7597 if (mode_width > width)
951553af 7598 outer = (op_nonzero & nonzero & ~ mode_mask);
230d793d
RS
7599
7600 if (code == LSHIFTRT)
7601 inner >>= count;
7602 else if (code == ASHIFTRT)
7603 {
7604 inner >>= count;
7605
951553af 7606 /* If the sign bit may have been nonzero before the shift, we
230d793d 7607 need to mark all the places it could have been copied to
951553af 7608 by the shift as possibly nonzero. */
5f4f0e22
CH
7609 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7610 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
230d793d 7611 }
45620ed4 7612 else if (code == ASHIFT)
230d793d
RS
7613 inner <<= count;
7614 else
7615 inner = ((inner << (count % width)
7616 | (inner >> (width - (count % width)))) & mode_mask);
7617
951553af 7618 nonzero &= (outer | inner);
230d793d
RS
7619 }
7620 break;
7621
7622 case FFS:
7623 /* This is at most the number of bits in the mode. */
951553af 7624 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
230d793d 7625 break;
d0ab8cd3
RK
7626
7627 case IF_THEN_ELSE:
951553af
RK
7628 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7629 | nonzero_bits (XEXP (x, 2), mode));
d0ab8cd3 7630 break;
e9a25f70
JL
7631
7632 default:
7633 break;
230d793d
RS
7634 }
7635
951553af 7636 return nonzero;
230d793d 7637}
b3728b0e
JW
7638
7639/* See the macro definition above. */
7640#undef num_sign_bit_copies
230d793d 7641\f
d0ab8cd3 7642/* Return the number of bits at the high-order end of X that are known to
5109d49f
RK
7643 be equal to the sign bit. X will be used in mode MODE; if MODE is
7644 VOIDmode, X will be used in its own mode. The returned value will always
7645 be between 1 and the number of bits in MODE. */
d0ab8cd3
RK
7646
7647static int
7648num_sign_bit_copies (x, mode)
7649 rtx x;
7650 enum machine_mode mode;
7651{
7652 enum rtx_code code = GET_CODE (x);
7653 int bitwidth;
7654 int num0, num1, result;
951553af 7655 unsigned HOST_WIDE_INT nonzero;
d0ab8cd3
RK
7656 rtx tem;
7657
7658 /* If we weren't given a mode, use the mode of X. If the mode is still
1c75dfa4
RK
7659 VOIDmode, we don't know anything. Likewise if one of the modes is
7660 floating-point. */
d0ab8cd3
RK
7661
7662 if (mode == VOIDmode)
7663 mode = GET_MODE (x);
7664
1c75dfa4 7665 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
6752e8d2 7666 return 1;
d0ab8cd3
RK
7667
7668 bitwidth = GET_MODE_BITSIZE (mode);
7669
0f41302f 7670 /* For a smaller object, just ignore the high bits. */
312def2e
RK
7671 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7672 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7673 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7674
e9a25f70
JL
7675 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7676 {
0c314d1a
RK
7677#ifndef WORD_REGISTER_OPERATIONS
7678 /* If this machine does not do all register operations on the entire
7679 register and MODE is wider than the mode of X, we can say nothing
7680 at all about the high-order bits. */
e9a25f70
JL
7681 return 1;
7682#else
7683 /* Likewise on machines that do, if the mode of the object is smaller
7684 than a word and loads of that size don't sign extend, we can say
7685 nothing about the high order bits. */
7686 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
7687#ifdef LOAD_EXTEND_OP
7688 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
7689#endif
7690 )
7691 return 1;
0c314d1a 7692#endif
e9a25f70 7693 }
0c314d1a 7694
d0ab8cd3
RK
7695 switch (code)
7696 {
7697 case REG:
55310dad 7698
ff0dbdd1
RK
7699#ifdef POINTERS_EXTEND_UNSIGNED
7700 /* If pointers extend signed and this is a pointer in Pmode, say that
7701 all the bits above ptr_mode are known to be sign bit copies. */
7702 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7703 && REGNO_POINTER_FLAG (REGNO (x)))
7704 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7705#endif
7706
55310dad
RK
7707 if (reg_last_set_value[REGNO (x)] != 0
7708 && reg_last_set_mode[REGNO (x)] == mode
b1f21e0a 7709 && (REG_N_SETS (REGNO (x)) == 1
55310dad
RK
7710 || reg_last_set_label[REGNO (x)] == label_tick)
7711 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7712 return reg_last_set_sign_bit_copies[REGNO (x)];
d0ab8cd3
RK
7713
7714 tem = get_last_value (x);
7715 if (tem != 0)
7716 return num_sign_bit_copies (tem, mode);
55310dad
RK
7717
7718 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7719 return reg_sign_bit_copies[REGNO (x)];
d0ab8cd3
RK
7720 break;
7721
457816e2 7722 case MEM:
8baf60bb 7723#ifdef LOAD_EXTEND_OP
457816e2 7724 /* Some RISC machines sign-extend all loads of smaller than a word. */
8baf60bb
RK
7725 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
7726 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
457816e2 7727#endif
8baf60bb 7728 break;
457816e2 7729
d0ab8cd3
RK
7730 case CONST_INT:
7731 /* If the constant is negative, take its 1's complement and remask.
7732 Then see how many zero bits we have. */
951553af 7733 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
ac49a949 7734 if (bitwidth <= HOST_BITS_PER_WIDE_INT
951553af
RK
7735 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7736 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
d0ab8cd3 7737
951553af 7738 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
d0ab8cd3
RK
7739
7740 case SUBREG:
c3c2cb37
RK
7741 /* If this is a SUBREG for a promoted object that is sign-extended
7742 and we are looking at it in a wider mode, we know that at least the
7743 high-order bits are known to be sign bit copies. */
7744
7745 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
dc3e17ad
RK
7746 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
7747 num_sign_bit_copies (SUBREG_REG (x), mode));
c3c2cb37 7748
0f41302f 7749 /* For a smaller object, just ignore the high bits. */
d0ab8cd3
RK
7750 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
7751 {
7752 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
7753 return MAX (1, (num0
7754 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7755 - bitwidth)));
7756 }
457816e2 7757
8baf60bb 7758#ifdef WORD_REGISTER_OPERATIONS
2aec5b7a 7759#ifdef LOAD_EXTEND_OP
8baf60bb
RK
7760 /* For paradoxical SUBREGs on machines where all register operations
7761 affect the entire register, just look inside. Note that we are
7762 passing MODE to the recursive call, so the number of sign bit copies
7763 will remain relative to that mode, not the inner mode. */
457816e2 7764
2aec5b7a
JW
7765 /* This works only if loads sign extend. Otherwise, if we get a
7766 reload for the inner part, it may be loaded from the stack, and
7767 then we lose all sign bit copies that existed before the store
7768 to the stack. */
7769
7770 if ((GET_MODE_SIZE (GET_MODE (x))
7771 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7772 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
457816e2 7773 return num_sign_bit_copies (SUBREG_REG (x), mode);
2aec5b7a 7774#endif
457816e2 7775#endif
d0ab8cd3
RK
7776 break;
7777
7778 case SIGN_EXTRACT:
7779 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7780 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
7781 break;
7782
7783 case SIGN_EXTEND:
7784 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7785 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
7786
7787 case TRUNCATE:
0f41302f 7788 /* For a smaller object, just ignore the high bits. */
d0ab8cd3
RK
7789 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
7790 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7791 - bitwidth)));
7792
7793 case NOT:
7794 return num_sign_bit_copies (XEXP (x, 0), mode);
7795
7796 case ROTATE: case ROTATERT:
7797 /* If we are rotating left by a number of bits less than the number
7798 of sign bit copies, we can just subtract that amount from the
7799 number. */
7800 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7801 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
7802 {
7803 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7804 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
7805 : bitwidth - INTVAL (XEXP (x, 1))));
7806 }
7807 break;
7808
7809 case NEG:
7810 /* In general, this subtracts one sign bit copy. But if the value
7811 is known to be positive, the number of sign bit copies is the
951553af
RK
7812 same as that of the input. Finally, if the input has just one bit
7813 that might be nonzero, all the bits are copies of the sign bit. */
7814 nonzero = nonzero_bits (XEXP (x, 0), mode);
7815 if (nonzero == 1)
d0ab8cd3
RK
7816 return bitwidth;
7817
7818 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7819 if (num0 > 1
ac49a949 7820 && bitwidth <= HOST_BITS_PER_WIDE_INT
951553af 7821 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
d0ab8cd3
RK
7822 num0--;
7823
7824 return num0;
7825
7826 case IOR: case AND: case XOR:
7827 case SMIN: case SMAX: case UMIN: case UMAX:
7828 /* Logical operations will preserve the number of sign-bit copies.
7829 MIN and MAX operations always return one of the operands. */
7830 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7831 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7832 return MIN (num0, num1);
7833
7834 case PLUS: case MINUS:
7835 /* For addition and subtraction, we can have a 1-bit carry. However,
7836 if we are subtracting 1 from a positive number, there will not
7837 be such a carry. Furthermore, if the positive number is known to
7838 be 0 or 1, we know the result is either -1 or 0. */
7839
3e3ea975 7840 if (code == PLUS && XEXP (x, 1) == constm1_rtx
9295e6af 7841 && bitwidth <= HOST_BITS_PER_WIDE_INT)
d0ab8cd3 7842 {
951553af
RK
7843 nonzero = nonzero_bits (XEXP (x, 0), mode);
7844 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
7845 return (nonzero == 1 || nonzero == 0 ? bitwidth
7846 : bitwidth - floor_log2 (nonzero) - 1);
d0ab8cd3
RK
7847 }
7848
7849 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7850 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7851 return MAX (1, MIN (num0, num1) - 1);
7852
7853 case MULT:
7854 /* The number of bits of the product is the sum of the number of
7855 bits of both terms. However, unless one of the terms if known
7856 to be positive, we must allow for an additional bit since negating
7857 a negative number can remove one sign bit copy. */
7858
7859 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7860 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7861
7862 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
7863 if (result > 0
9295e6af 7864 && bitwidth <= HOST_BITS_PER_WIDE_INT
951553af 7865 && ((nonzero_bits (XEXP (x, 0), mode)
d0ab8cd3 7866 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
01c82bbb
RK
7867 && ((nonzero_bits (XEXP (x, 1), mode)
7868 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
d0ab8cd3
RK
7869 result--;
7870
7871 return MAX (1, result);
7872
7873 case UDIV:
7874 /* The result must be <= the first operand. */
7875 return num_sign_bit_copies (XEXP (x, 0), mode);
7876
7877 case UMOD:
7878 /* The result must be <= the scond operand. */
7879 return num_sign_bit_copies (XEXP (x, 1), mode);
7880
7881 case DIV:
7882 /* Similar to unsigned division, except that we have to worry about
7883 the case where the divisor is negative, in which case we have
7884 to add 1. */
7885 result = num_sign_bit_copies (XEXP (x, 0), mode);
7886 if (result > 1
ac49a949 7887 && bitwidth <= HOST_BITS_PER_WIDE_INT
951553af 7888 && (nonzero_bits (XEXP (x, 1), mode)
d0ab8cd3
RK
7889 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7890 result --;
7891
7892 return result;
7893
7894 case MOD:
7895 result = num_sign_bit_copies (XEXP (x, 1), mode);
7896 if (result > 1
ac49a949 7897 && bitwidth <= HOST_BITS_PER_WIDE_INT
951553af 7898 && (nonzero_bits (XEXP (x, 1), mode)
d0ab8cd3
RK
7899 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7900 result --;
7901
7902 return result;
7903
7904 case ASHIFTRT:
7905 /* Shifts by a constant add to the number of bits equal to the
7906 sign bit. */
7907 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7908 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7909 && INTVAL (XEXP (x, 1)) > 0)
7910 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
7911
7912 return num0;
7913
7914 case ASHIFT:
d0ab8cd3
RK
7915 /* Left shifts destroy copies. */
7916 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7917 || INTVAL (XEXP (x, 1)) < 0
7918 || INTVAL (XEXP (x, 1)) >= bitwidth)
7919 return 1;
7920
7921 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7922 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
7923
7924 case IF_THEN_ELSE:
7925 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
7926 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
7927 return MIN (num0, num1);
7928
d0ab8cd3
RK
7929 case EQ: case NE: case GE: case GT: case LE: case LT:
7930 case GEU: case GTU: case LEU: case LTU:
0802d516
RK
7931 if (STORE_FLAG_VALUE == -1)
7932 return bitwidth;
e9a25f70
JL
7933 break;
7934
7935 default:
7936 break;
d0ab8cd3
RK
7937 }
7938
7939 /* If we haven't been able to figure it out by one of the above rules,
7940 see if some of the high-order bits are known to be zero. If so,
ac49a949
RS
7941 count those bits and return one less than that amount. If we can't
7942 safely compute the mask for this mode, always return BITWIDTH. */
7943
7944 if (bitwidth > HOST_BITS_PER_WIDE_INT)
6752e8d2 7945 return 1;
d0ab8cd3 7946
951553af 7947 nonzero = nonzero_bits (x, mode);
df6f4086 7948 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
951553af 7949 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
d0ab8cd3
RK
7950}
7951\f
1a26b032
RK
7952/* Return the number of "extended" bits there are in X, when interpreted
7953 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
7954 unsigned quantities, this is the number of high-order zero bits.
7955 For signed quantities, this is the number of copies of the sign bit
7956 minus 1. In both case, this function returns the number of "spare"
7957 bits. For example, if two quantities for which this function returns
7958 at least 1 are added, the addition is known not to overflow.
7959
7960 This function will always return 0 unless called during combine, which
7961 implies that it must be called from a define_split. */
7962
7963int
7964extended_count (x, mode, unsignedp)
7965 rtx x;
7966 enum machine_mode mode;
7967 int unsignedp;
7968{
951553af 7969 if (nonzero_sign_valid == 0)
1a26b032
RK
7970 return 0;
7971
7972 return (unsignedp
ac49a949
RS
7973 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7974 && (GET_MODE_BITSIZE (mode) - 1
951553af 7975 - floor_log2 (nonzero_bits (x, mode))))
1a26b032
RK
7976 : num_sign_bit_copies (x, mode) - 1);
7977}
7978\f
230d793d
RS
7979/* This function is called from `simplify_shift_const' to merge two
7980 outer operations. Specifically, we have already found that we need
7981 to perform operation *POP0 with constant *PCONST0 at the outermost
7982 position. We would now like to also perform OP1 with constant CONST1
7983 (with *POP0 being done last).
7984
7985 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
7986 the resulting operation. *PCOMP_P is set to 1 if we would need to
7987 complement the innermost operand, otherwise it is unchanged.
7988
7989 MODE is the mode in which the operation will be done. No bits outside
7990 the width of this mode matter. It is assumed that the width of this mode
5f4f0e22 7991 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
230d793d
RS
7992
7993 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
7994 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
7995 result is simply *PCONST0.
7996
7997 If the resulting operation cannot be expressed as one operation, we
7998 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
7999
8000static int
8001merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8002 enum rtx_code *pop0;
5f4f0e22 8003 HOST_WIDE_INT *pconst0;
230d793d 8004 enum rtx_code op1;
5f4f0e22 8005 HOST_WIDE_INT const1;
230d793d
RS
8006 enum machine_mode mode;
8007 int *pcomp_p;
8008{
8009 enum rtx_code op0 = *pop0;
5f4f0e22 8010 HOST_WIDE_INT const0 = *pconst0;
9fa6d012 8011 int width = GET_MODE_BITSIZE (mode);
230d793d
RS
8012
8013 const0 &= GET_MODE_MASK (mode);
8014 const1 &= GET_MODE_MASK (mode);
8015
8016 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8017 if (op0 == AND)
8018 const1 &= const0;
8019
8020 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8021 if OP0 is SET. */
8022
8023 if (op1 == NIL || op0 == SET)
8024 return 1;
8025
8026 else if (op0 == NIL)
8027 op0 = op1, const0 = const1;
8028
8029 else if (op0 == op1)
8030 {
8031 switch (op0)
8032 {
8033 case AND:
8034 const0 &= const1;
8035 break;
8036 case IOR:
8037 const0 |= const1;
8038 break;
8039 case XOR:
8040 const0 ^= const1;
8041 break;
8042 case PLUS:
8043 const0 += const1;
8044 break;
8045 case NEG:
8046 op0 = NIL;
8047 break;
e9a25f70
JL
8048 default:
8049 break;
230d793d
RS
8050 }
8051 }
8052
8053 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8054 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8055 return 0;
8056
8057 /* If the two constants aren't the same, we can't do anything. The
8058 remaining six cases can all be done. */
8059 else if (const0 != const1)
8060 return 0;
8061
8062 else
8063 switch (op0)
8064 {
8065 case IOR:
8066 if (op1 == AND)
8067 /* (a & b) | b == b */
8068 op0 = SET;
8069 else /* op1 == XOR */
8070 /* (a ^ b) | b == a | b */
8071 ;
8072 break;
8073
8074 case XOR:
8075 if (op1 == AND)
8076 /* (a & b) ^ b == (~a) & b */
8077 op0 = AND, *pcomp_p = 1;
8078 else /* op1 == IOR */
8079 /* (a | b) ^ b == a & ~b */
8080 op0 = AND, *pconst0 = ~ const0;
8081 break;
8082
8083 case AND:
8084 if (op1 == IOR)
8085 /* (a | b) & b == b */
8086 op0 = SET;
8087 else /* op1 == XOR */
8088 /* (a ^ b) & b) == (~a) & b */
8089 *pcomp_p = 1;
8090 break;
e9a25f70
JL
8091 default:
8092 break;
230d793d
RS
8093 }
8094
8095 /* Check for NO-OP cases. */
8096 const0 &= GET_MODE_MASK (mode);
8097 if (const0 == 0
8098 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8099 op0 = NIL;
8100 else if (const0 == 0 && op0 == AND)
8101 op0 = SET;
8102 else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
8103 op0 = NIL;
8104
9fa6d012
TG
8105 /* If this would be an entire word for the target, but is not for
8106 the host, then sign-extend on the host so that the number will look
8107 the same way on the host that it would on the target.
8108
8109 For example, when building a 64 bit alpha hosted 32 bit sparc
8110 targeted compiler, then we want the 32 bit unsigned value -1 to be
8111 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8112 The later confuses the sparc backend. */
8113
8114 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8115 && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
8116 const0 |= ((HOST_WIDE_INT) (-1) << width);
8117
230d793d
RS
8118 *pop0 = op0;
8119 *pconst0 = const0;
8120
8121 return 1;
8122}
8123\f
8124/* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8125 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8126 that we started with.
8127
8128 The shift is normally computed in the widest mode we find in VAROP, as
8129 long as it isn't a different number of words than RESULT_MODE. Exceptions
8130 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8131
8132static rtx
8133simplify_shift_const (x, code, result_mode, varop, count)
8134 rtx x;
8135 enum rtx_code code;
8136 enum machine_mode result_mode;
8137 rtx varop;
8138 int count;
8139{
8140 enum rtx_code orig_code = code;
8141 int orig_count = count;
8142 enum machine_mode mode = result_mode;
8143 enum machine_mode shift_mode, tmode;
8144 int mode_words
8145 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8146 /* We form (outer_op (code varop count) (outer_const)). */
8147 enum rtx_code outer_op = NIL;
c4e861e8 8148 HOST_WIDE_INT outer_const = 0;
230d793d
RS
8149 rtx const_rtx;
8150 int complement_p = 0;
8151 rtx new;
8152
8153 /* If we were given an invalid count, don't do anything except exactly
8154 what was requested. */
8155
8156 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8157 {
8158 if (x)
8159 return x;
8160
5f4f0e22 8161 return gen_rtx (code, mode, varop, GEN_INT (count));
230d793d
RS
8162 }
8163
8164 /* Unless one of the branches of the `if' in this loop does a `continue',
8165 we will `break' the loop after the `if'. */
8166
8167 while (count != 0)
8168 {
8169 /* If we have an operand of (clobber (const_int 0)), just return that
8170 value. */
8171 if (GET_CODE (varop) == CLOBBER)
8172 return varop;
8173
8174 /* If we discovered we had to complement VAROP, leave. Making a NOT
8175 here would cause an infinite loop. */
8176 if (complement_p)
8177 break;
8178
abc95ed3 8179 /* Convert ROTATERT to ROTATE. */
230d793d
RS
8180 if (code == ROTATERT)
8181 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8182
230d793d 8183 /* We need to determine what mode we will do the shift in. If the
f6789c77
RK
8184 shift is a right shift or a ROTATE, we must always do it in the mode
8185 it was originally done in. Otherwise, we can do it in MODE, the
0f41302f 8186 widest mode encountered. */
f6789c77
RK
8187 shift_mode
8188 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8189 ? result_mode : mode);
230d793d
RS
8190
8191 /* Handle cases where the count is greater than the size of the mode
8192 minus 1. For ASHIFT, use the size minus one as the count (this can
8193 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8194 take the count modulo the size. For other shifts, the result is
8195 zero.
8196
8197 Since these shifts are being produced by the compiler by combining
8198 multiple operations, each of which are defined, we know what the
8199 result is supposed to be. */
8200
8201 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8202 {
8203 if (code == ASHIFTRT)
8204 count = GET_MODE_BITSIZE (shift_mode) - 1;
8205 else if (code == ROTATE || code == ROTATERT)
8206 count %= GET_MODE_BITSIZE (shift_mode);
8207 else
8208 {
8209 /* We can't simply return zero because there may be an
8210 outer op. */
8211 varop = const0_rtx;
8212 count = 0;
8213 break;
8214 }
8215 }
8216
8217 /* Negative counts are invalid and should not have been made (a
8218 programmer-specified negative count should have been handled
0f41302f 8219 above). */
230d793d
RS
8220 else if (count < 0)
8221 abort ();
8222
312def2e
RK
8223 /* An arithmetic right shift of a quantity known to be -1 or 0
8224 is a no-op. */
8225 if (code == ASHIFTRT
8226 && (num_sign_bit_copies (varop, shift_mode)
8227 == GET_MODE_BITSIZE (shift_mode)))
d0ab8cd3 8228 {
312def2e
RK
8229 count = 0;
8230 break;
8231 }
d0ab8cd3 8232
312def2e
RK
8233 /* If we are doing an arithmetic right shift and discarding all but
8234 the sign bit copies, this is equivalent to doing a shift by the
8235 bitsize minus one. Convert it into that shift because it will often
8236 allow other simplifications. */
500c518b 8237
312def2e
RK
8238 if (code == ASHIFTRT
8239 && (count + num_sign_bit_copies (varop, shift_mode)
8240 >= GET_MODE_BITSIZE (shift_mode)))
8241 count = GET_MODE_BITSIZE (shift_mode) - 1;
500c518b 8242
230d793d
RS
8243 /* We simplify the tests below and elsewhere by converting
8244 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8245 `make_compound_operation' will convert it to a ASHIFTRT for
8246 those machines (such as Vax) that don't have a LSHIFTRT. */
5f4f0e22 8247 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
230d793d 8248 && code == ASHIFTRT
951553af 8249 && ((nonzero_bits (varop, shift_mode)
5f4f0e22
CH
8250 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8251 == 0))
230d793d
RS
8252 code = LSHIFTRT;
8253
8254 switch (GET_CODE (varop))
8255 {
8256 case SIGN_EXTEND:
8257 case ZERO_EXTEND:
8258 case SIGN_EXTRACT:
8259 case ZERO_EXTRACT:
8260 new = expand_compound_operation (varop);
8261 if (new != varop)
8262 {
8263 varop = new;
8264 continue;
8265 }
8266 break;
8267
8268 case MEM:
8269 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8270 minus the width of a smaller mode, we can do this with a
8271 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8272 if ((code == ASHIFTRT || code == LSHIFTRT)
8273 && ! mode_dependent_address_p (XEXP (varop, 0))
8274 && ! MEM_VOLATILE_P (varop)
8275 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8276 MODE_INT, 1)) != BLKmode)
8277 {
f76b9db2
ILT
8278 if (BYTES_BIG_ENDIAN)
8279 new = gen_rtx (MEM, tmode, XEXP (varop, 0));
8280 else
e24b00c8
ILT
8281 new = gen_rtx (MEM, tmode,
8282 plus_constant (XEXP (varop, 0),
8283 count / BITS_PER_UNIT));
8284 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8285 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
8286 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
230d793d
RS
8287 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8288 : ZERO_EXTEND, mode, new);
8289 count = 0;
8290 continue;
8291 }
8292 break;
8293
8294 case USE:
8295 /* Similar to the case above, except that we can only do this if
8296 the resulting mode is the same as that of the underlying
8297 MEM and adjust the address depending on the *bits* endianness
8298 because of the way that bit-field extract insns are defined. */
8299 if ((code == ASHIFTRT || code == LSHIFTRT)
8300 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8301 MODE_INT, 1)) != BLKmode
8302 && tmode == GET_MODE (XEXP (varop, 0)))
8303 {
f76b9db2
ILT
8304 if (BITS_BIG_ENDIAN)
8305 new = XEXP (varop, 0);
8306 else
8307 {
8308 new = copy_rtx (XEXP (varop, 0));
8309 SUBST (XEXP (new, 0),
8310 plus_constant (XEXP (new, 0),
8311 count / BITS_PER_UNIT));
8312 }
230d793d
RS
8313
8314 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8315 : ZERO_EXTEND, mode, new);
8316 count = 0;
8317 continue;
8318 }
8319 break;
8320
8321 case SUBREG:
8322 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8323 the same number of words as what we've seen so far. Then store
8324 the widest mode in MODE. */
f9e67232
RS
8325 if (subreg_lowpart_p (varop)
8326 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8327 > GET_MODE_SIZE (GET_MODE (varop)))
230d793d
RS
8328 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8329 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8330 == mode_words))
8331 {
8332 varop = SUBREG_REG (varop);
8333 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8334 mode = GET_MODE (varop);
8335 continue;
8336 }
8337 break;
8338
8339 case MULT:
8340 /* Some machines use MULT instead of ASHIFT because MULT
8341 is cheaper. But it is still better on those machines to
8342 merge two shifts into one. */
8343 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8344 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8345 {
8346 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
5f4f0e22 8347 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
230d793d
RS
8348 continue;
8349 }
8350 break;
8351
8352 case UDIV:
8353 /* Similar, for when divides are cheaper. */
8354 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8355 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8356 {
8357 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
5f4f0e22 8358 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
230d793d
RS
8359 continue;
8360 }
8361 break;
8362
8363 case ASHIFTRT:
8364 /* If we are extracting just the sign bit of an arithmetic right
8365 shift, that shift is not needed. */
8366 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8367 {
8368 varop = XEXP (varop, 0);
8369 continue;
8370 }
8371
0f41302f 8372 /* ... fall through ... */
230d793d
RS
8373
8374 case LSHIFTRT:
8375 case ASHIFT:
230d793d
RS
8376 case ROTATE:
8377 /* Here we have two nested shifts. The result is usually the
8378 AND of a new shift with a mask. We compute the result below. */
8379 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8380 && INTVAL (XEXP (varop, 1)) >= 0
8381 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
5f4f0e22
CH
8382 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8383 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
230d793d
RS
8384 {
8385 enum rtx_code first_code = GET_CODE (varop);
8386 int first_count = INTVAL (XEXP (varop, 1));
5f4f0e22 8387 unsigned HOST_WIDE_INT mask;
230d793d 8388 rtx mask_rtx;
230d793d 8389
230d793d
RS
8390 /* We have one common special case. We can't do any merging if
8391 the inner code is an ASHIFTRT of a smaller mode. However, if
8392 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8393 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8394 we can convert it to
8395 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8396 This simplifies certain SIGN_EXTEND operations. */
8397 if (code == ASHIFT && first_code == ASHIFTRT
8398 && (GET_MODE_BITSIZE (result_mode)
8399 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8400 {
8401 /* C3 has the low-order C1 bits zero. */
8402
5f4f0e22
CH
8403 mask = (GET_MODE_MASK (mode)
8404 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
230d793d 8405
5f4f0e22 8406 varop = simplify_and_const_int (NULL_RTX, result_mode,
230d793d 8407 XEXP (varop, 0), mask);
5f4f0e22 8408 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
230d793d
RS
8409 varop, count);
8410 count = first_count;
8411 code = ASHIFTRT;
8412 continue;
8413 }
8414
d0ab8cd3
RK
8415 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8416 than C1 high-order bits equal to the sign bit, we can convert
8417 this to either an ASHIFT or a ASHIFTRT depending on the
8418 two counts.
230d793d
RS
8419
8420 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8421
8422 if (code == ASHIFTRT && first_code == ASHIFT
8423 && GET_MODE (varop) == shift_mode
d0ab8cd3
RK
8424 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8425 > first_count))
230d793d 8426 {
d0ab8cd3
RK
8427 count -= first_count;
8428 if (count < 0)
8429 count = - count, code = ASHIFT;
8430 varop = XEXP (varop, 0);
8431 continue;
230d793d
RS
8432 }
8433
8434 /* There are some cases we can't do. If CODE is ASHIFTRT,
8435 we can only do this if FIRST_CODE is also ASHIFTRT.
8436
8437 We can't do the case when CODE is ROTATE and FIRST_CODE is
8438 ASHIFTRT.
8439
8440 If the mode of this shift is not the mode of the outer shift,
bdaae9a0 8441 we can't do this if either shift is a right shift or ROTATE.
230d793d
RS
8442
8443 Finally, we can't do any of these if the mode is too wide
8444 unless the codes are the same.
8445
8446 Handle the case where the shift codes are the same
8447 first. */
8448
8449 if (code == first_code)
8450 {
8451 if (GET_MODE (varop) != result_mode
bdaae9a0
RK
8452 && (code == ASHIFTRT || code == LSHIFTRT
8453 || code == ROTATE))
230d793d
RS
8454 break;
8455
8456 count += first_count;
8457 varop = XEXP (varop, 0);
8458 continue;
8459 }
8460
8461 if (code == ASHIFTRT
8462 || (code == ROTATE && first_code == ASHIFTRT)
5f4f0e22 8463 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
230d793d 8464 || (GET_MODE (varop) != result_mode
bdaae9a0
RK
8465 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8466 || first_code == ROTATE
230d793d
RS
8467 || code == ROTATE)))
8468 break;
8469
8470 /* To compute the mask to apply after the shift, shift the
951553af 8471 nonzero bits of the inner shift the same way the
230d793d
RS
8472 outer shift will. */
8473
951553af 8474 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
230d793d
RS
8475
8476 mask_rtx
8477 = simplify_binary_operation (code, result_mode, mask_rtx,
5f4f0e22 8478 GEN_INT (count));
230d793d
RS
8479
8480 /* Give up if we can't compute an outer operation to use. */
8481 if (mask_rtx == 0
8482 || GET_CODE (mask_rtx) != CONST_INT
8483 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8484 INTVAL (mask_rtx),
8485 result_mode, &complement_p))
8486 break;
8487
8488 /* If the shifts are in the same direction, we add the
8489 counts. Otherwise, we subtract them. */
8490 if ((code == ASHIFTRT || code == LSHIFTRT)
8491 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8492 count += first_count;
8493 else
8494 count -= first_count;
8495
8496 /* If COUNT is positive, the new shift is usually CODE,
8497 except for the two exceptions below, in which case it is
8498 FIRST_CODE. If the count is negative, FIRST_CODE should
8499 always be used */
8500 if (count > 0
8501 && ((first_code == ROTATE && code == ASHIFT)
8502 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8503 code = first_code;
8504 else if (count < 0)
8505 code = first_code, count = - count;
8506
8507 varop = XEXP (varop, 0);
8508 continue;
8509 }
8510
8511 /* If we have (A << B << C) for any shift, we can convert this to
8512 (A << C << B). This wins if A is a constant. Only try this if
8513 B is not a constant. */
8514
8515 else if (GET_CODE (varop) == code
8516 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8517 && 0 != (new
8518 = simplify_binary_operation (code, mode,
8519 XEXP (varop, 0),
5f4f0e22 8520 GEN_INT (count))))
230d793d
RS
8521 {
8522 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8523 count = 0;
8524 continue;
8525 }
8526 break;
8527
8528 case NOT:
8529 /* Make this fit the case below. */
8530 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
5f4f0e22 8531 GEN_INT (GET_MODE_MASK (mode)));
230d793d
RS
8532 continue;
8533
8534 case IOR:
8535 case AND:
8536 case XOR:
8537 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8538 with C the size of VAROP - 1 and the shift is logical if
8539 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8540 we have an (le X 0) operation. If we have an arithmetic shift
8541 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8542 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8543
8544 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8545 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8546 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8547 && (code == LSHIFTRT || code == ASHIFTRT)
8548 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8549 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8550 {
8551 count = 0;
8552 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8553 const0_rtx);
8554
8555 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8556 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8557
8558 continue;
8559 }
8560
8561 /* If we have (shift (logical)), move the logical to the outside
8562 to allow it to possibly combine with another logical and the
8563 shift to combine with another shift. This also canonicalizes to
8564 what a ZERO_EXTRACT looks like. Also, some machines have
8565 (and (shift)) insns. */
8566
8567 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8568 && (new = simplify_binary_operation (code, result_mode,
8569 XEXP (varop, 1),
5f4f0e22 8570 GEN_INT (count))) != 0
7d171a1e 8571 && GET_CODE(new) == CONST_INT
230d793d
RS
8572 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8573 INTVAL (new), result_mode, &complement_p))
8574 {
8575 varop = XEXP (varop, 0);
8576 continue;
8577 }
8578
8579 /* If we can't do that, try to simplify the shift in each arm of the
8580 logical expression, make a new logical expression, and apply
8581 the inverse distributive law. */
8582 {
00d4ca1c 8583 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
230d793d 8584 XEXP (varop, 0), count);
00d4ca1c 8585 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
230d793d
RS
8586 XEXP (varop, 1), count);
8587
21a64bf1 8588 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
230d793d
RS
8589 varop = apply_distributive_law (varop);
8590
8591 count = 0;
8592 }
8593 break;
8594
8595 case EQ:
45620ed4 8596 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
230d793d 8597 says that the sign bit can be tested, FOO has mode MODE, C is
45620ed4
RK
8598 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8599 that may be nonzero. */
8600 if (code == LSHIFTRT
230d793d
RS
8601 && XEXP (varop, 1) == const0_rtx
8602 && GET_MODE (XEXP (varop, 0)) == result_mode
8603 && count == GET_MODE_BITSIZE (result_mode) - 1
5f4f0e22 8604 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
230d793d 8605 && ((STORE_FLAG_VALUE
5f4f0e22 8606 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
951553af 8607 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
5f4f0e22
CH
8608 && merge_outer_ops (&outer_op, &outer_const, XOR,
8609 (HOST_WIDE_INT) 1, result_mode,
8610 &complement_p))
230d793d
RS
8611 {
8612 varop = XEXP (varop, 0);
8613 count = 0;
8614 continue;
8615 }
8616 break;
8617
8618 case NEG:
d0ab8cd3
RK
8619 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8620 than the number of bits in the mode is equivalent to A. */
8621 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
951553af 8622 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
230d793d 8623 {
d0ab8cd3 8624 varop = XEXP (varop, 0);
230d793d
RS
8625 count = 0;
8626 continue;
8627 }
8628
8629 /* NEG commutes with ASHIFT since it is multiplication. Move the
8630 NEG outside to allow shifts to combine. */
8631 if (code == ASHIFT
5f4f0e22
CH
8632 && merge_outer_ops (&outer_op, &outer_const, NEG,
8633 (HOST_WIDE_INT) 0, result_mode,
8634 &complement_p))
230d793d
RS
8635 {
8636 varop = XEXP (varop, 0);
8637 continue;
8638 }
8639 break;
8640
8641 case PLUS:
d0ab8cd3
RK
8642 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8643 is one less than the number of bits in the mode is
8644 equivalent to (xor A 1). */
230d793d
RS
8645 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8646 && XEXP (varop, 1) == constm1_rtx
951553af 8647 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
5f4f0e22
CH
8648 && merge_outer_ops (&outer_op, &outer_const, XOR,
8649 (HOST_WIDE_INT) 1, result_mode,
8650 &complement_p))
230d793d
RS
8651 {
8652 count = 0;
8653 varop = XEXP (varop, 0);
8654 continue;
8655 }
8656
3f508eca 8657 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
951553af 8658 that might be nonzero in BAR are those being shifted out and those
3f508eca
RK
8659 bits are known zero in FOO, we can replace the PLUS with FOO.
8660 Similarly in the other operand order. This code occurs when
8661 we are computing the size of a variable-size array. */
8662
8663 if ((code == ASHIFTRT || code == LSHIFTRT)
5f4f0e22 8664 && count < HOST_BITS_PER_WIDE_INT
951553af
RK
8665 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8666 && (nonzero_bits (XEXP (varop, 1), result_mode)
8667 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
3f508eca
RK
8668 {
8669 varop = XEXP (varop, 0);
8670 continue;
8671 }
8672 else if ((code == ASHIFTRT || code == LSHIFTRT)
5f4f0e22 8673 && count < HOST_BITS_PER_WIDE_INT
ac49a949 8674 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
951553af 8675 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
3f508eca 8676 >> count)
951553af
RK
8677 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8678 & nonzero_bits (XEXP (varop, 1),
3f508eca
RK
8679 result_mode)))
8680 {
8681 varop = XEXP (varop, 1);
8682 continue;
8683 }
8684
230d793d
RS
8685 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
8686 if (code == ASHIFT
8687 && GET_CODE (XEXP (varop, 1)) == CONST_INT
8688 && (new = simplify_binary_operation (ASHIFT, result_mode,
8689 XEXP (varop, 1),
5f4f0e22 8690 GEN_INT (count))) != 0
7d171a1e 8691 && GET_CODE(new) == CONST_INT
230d793d
RS
8692 && merge_outer_ops (&outer_op, &outer_const, PLUS,
8693 INTVAL (new), result_mode, &complement_p))
8694 {
8695 varop = XEXP (varop, 0);
8696 continue;
8697 }
8698 break;
8699
8700 case MINUS:
8701 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8702 with C the size of VAROP - 1 and the shift is logical if
8703 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8704 we have a (gt X 0) operation. If the shift is arithmetic with
8705 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8706 we have a (neg (gt X 0)) operation. */
8707
0802d516
RK
8708 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8709 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
230d793d 8710 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
230d793d
RS
8711 && (code == LSHIFTRT || code == ASHIFTRT)
8712 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8713 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8714 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8715 {
8716 count = 0;
8717 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8718 const0_rtx);
8719
8720 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8721 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8722
8723 continue;
8724 }
8725 break;
e9a25f70
JL
8726
8727 default:
8728 break;
230d793d
RS
8729 }
8730
8731 break;
8732 }
8733
8734 /* We need to determine what mode to do the shift in. If the shift is
f6789c77
RK
8735 a right shift or ROTATE, we must always do it in the mode it was
8736 originally done in. Otherwise, we can do it in MODE, the widest mode
8737 encountered. The code we care about is that of the shift that will
8738 actually be done, not the shift that was originally requested. */
8739 shift_mode
8740 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8741 ? result_mode : mode);
230d793d
RS
8742
8743 /* We have now finished analyzing the shift. The result should be
8744 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
8745 OUTER_OP is non-NIL, it is an operation that needs to be applied
8746 to the result of the shift. OUTER_CONST is the relevant constant,
8747 but we must turn off all bits turned off in the shift.
8748
8749 If we were passed a value for X, see if we can use any pieces of
8750 it. If not, make new rtx. */
8751
8752 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
8753 && GET_CODE (XEXP (x, 1)) == CONST_INT
8754 && INTVAL (XEXP (x, 1)) == count)
8755 const_rtx = XEXP (x, 1);
8756 else
5f4f0e22 8757 const_rtx = GEN_INT (count);
230d793d
RS
8758
8759 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8760 && GET_MODE (XEXP (x, 0)) == shift_mode
8761 && SUBREG_REG (XEXP (x, 0)) == varop)
8762 varop = XEXP (x, 0);
8763 else if (GET_MODE (varop) != shift_mode)
8764 varop = gen_lowpart_for_combine (shift_mode, varop);
8765
0f41302f 8766 /* If we can't make the SUBREG, try to return what we were given. */
230d793d
RS
8767 if (GET_CODE (varop) == CLOBBER)
8768 return x ? x : varop;
8769
8770 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
8771 if (new != 0)
8772 x = new;
8773 else
8774 {
8775 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
8776 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
8777
8778 SUBST (XEXP (x, 0), varop);
8779 SUBST (XEXP (x, 1), const_rtx);
8780 }
8781
224eeff2
RK
8782 /* If we have an outer operation and we just made a shift, it is
8783 possible that we could have simplified the shift were it not
8784 for the outer operation. So try to do the simplification
8785 recursively. */
8786
8787 if (outer_op != NIL && GET_CODE (x) == code
8788 && GET_CODE (XEXP (x, 1)) == CONST_INT)
8789 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
8790 INTVAL (XEXP (x, 1)));
8791
230d793d
RS
8792 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
8793 turn off all the bits that the shift would have turned off. */
8794 if (orig_code == LSHIFTRT && result_mode != shift_mode)
5f4f0e22 8795 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
230d793d
RS
8796 GET_MODE_MASK (result_mode) >> orig_count);
8797
8798 /* Do the remainder of the processing in RESULT_MODE. */
8799 x = gen_lowpart_for_combine (result_mode, x);
8800
8801 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
8802 operation. */
8803 if (complement_p)
0c1c8ea6 8804 x = gen_unary (NOT, result_mode, result_mode, x);
230d793d
RS
8805
8806 if (outer_op != NIL)
8807 {
5f4f0e22 8808 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9fa6d012
TG
8809 {
8810 int width = GET_MODE_BITSIZE (result_mode);
8811
8812 outer_const &= GET_MODE_MASK (result_mode);
8813
8814 /* If this would be an entire word for the target, but is not for
8815 the host, then sign-extend on the host so that the number will
8816 look the same way on the host that it would on the target.
8817
8818 For example, when building a 64 bit alpha hosted 32 bit sparc
8819 targeted compiler, then we want the 32 bit unsigned value -1 to be
8820 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8821 The later confuses the sparc backend. */
8822
8823 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8824 && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
8825 outer_const |= ((HOST_WIDE_INT) (-1) << width);
8826 }
230d793d
RS
8827
8828 if (outer_op == AND)
5f4f0e22 8829 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
230d793d
RS
8830 else if (outer_op == SET)
8831 /* This means that we have determined that the result is
8832 equivalent to a constant. This should be rare. */
5f4f0e22 8833 x = GEN_INT (outer_const);
230d793d 8834 else if (GET_RTX_CLASS (outer_op) == '1')
0c1c8ea6 8835 x = gen_unary (outer_op, result_mode, result_mode, x);
230d793d 8836 else
5f4f0e22 8837 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
230d793d
RS
8838 }
8839
8840 return x;
8841}
8842\f
8843/* Like recog, but we receive the address of a pointer to a new pattern.
8844 We try to match the rtx that the pointer points to.
8845 If that fails, we may try to modify or replace the pattern,
8846 storing the replacement into the same pointer object.
8847
8848 Modifications include deletion or addition of CLOBBERs.
8849
8850 PNOTES is a pointer to a location where any REG_UNUSED notes added for
8851 the CLOBBERs are placed.
8852
a29ca9db
RK
8853 PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
8854 we had to add.
8855
230d793d
RS
8856 The value is the final insn code from the pattern ultimately matched,
8857 or -1. */
8858
8859static int
a29ca9db 8860recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
230d793d
RS
8861 rtx *pnewpat;
8862 rtx insn;
8863 rtx *pnotes;
a29ca9db 8864 int *padded_scratches;
230d793d
RS
8865{
8866 register rtx pat = *pnewpat;
8867 int insn_code_number;
8868 int num_clobbers_to_add = 0;
8869 int i;
8870 rtx notes = 0;
8871
a29ca9db
RK
8872 *padded_scratches = 0;
8873
974f4146
RK
8874 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
8875 we use to indicate that something didn't match. If we find such a
8876 thing, force rejection. */
d96023cf 8877 if (GET_CODE (pat) == PARALLEL)
974f4146 8878 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
d96023cf
RK
8879 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
8880 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
974f4146
RK
8881 return -1;
8882
230d793d
RS
8883 /* Is the result of combination a valid instruction? */
8884 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8885
8886 /* If it isn't, there is the possibility that we previously had an insn
8887 that clobbered some register as a side effect, but the combined
8888 insn doesn't need to do that. So try once more without the clobbers
8889 unless this represents an ASM insn. */
8890
8891 if (insn_code_number < 0 && ! check_asm_operands (pat)
8892 && GET_CODE (pat) == PARALLEL)
8893 {
8894 int pos;
8895
8896 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
8897 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
8898 {
8899 if (i != pos)
8900 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
8901 pos++;
8902 }
8903
8904 SUBST_INT (XVECLEN (pat, 0), pos);
8905
8906 if (pos == 1)
8907 pat = XVECEXP (pat, 0, 0);
8908
8909 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8910 }
8911
8912 /* If we had any clobbers to add, make a new pattern than contains
8913 them. Then check to make sure that all of them are dead. */
8914 if (num_clobbers_to_add)
8915 {
8916 rtx newpat = gen_rtx (PARALLEL, VOIDmode,
8917 gen_rtvec (GET_CODE (pat) == PARALLEL
8918 ? XVECLEN (pat, 0) + num_clobbers_to_add
8919 : num_clobbers_to_add + 1));
8920
8921 if (GET_CODE (pat) == PARALLEL)
8922 for (i = 0; i < XVECLEN (pat, 0); i++)
8923 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
8924 else
8925 XVECEXP (newpat, 0, 0) = pat;
8926
8927 add_clobbers (newpat, insn_code_number);
8928
8929 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
8930 i < XVECLEN (newpat, 0); i++)
8931 {
8932 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
8933 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
8934 return -1;
a29ca9db
RK
8935 else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
8936 (*padded_scratches)++;
230d793d
RS
8937 notes = gen_rtx (EXPR_LIST, REG_UNUSED,
8938 XEXP (XVECEXP (newpat, 0, i), 0), notes);
8939 }
8940 pat = newpat;
8941 }
8942
8943 *pnewpat = pat;
8944 *pnotes = notes;
8945
8946 return insn_code_number;
8947}
8948\f
8949/* Like gen_lowpart but for use by combine. In combine it is not possible
8950 to create any new pseudoregs. However, it is safe to create
8951 invalid memory addresses, because combine will try to recognize
8952 them and all they will do is make the combine attempt fail.
8953
8954 If for some reason this cannot do its job, an rtx
8955 (clobber (const_int 0)) is returned.
8956 An insn containing that will not be recognized. */
8957
8958#undef gen_lowpart
8959
8960static rtx
8961gen_lowpart_for_combine (mode, x)
8962 enum machine_mode mode;
8963 register rtx x;
8964{
8965 rtx result;
8966
8967 if (GET_MODE (x) == mode)
8968 return x;
8969
eae957a8
RK
8970 /* We can only support MODE being wider than a word if X is a
8971 constant integer or has a mode the same size. */
8972
8973 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
8974 && ! ((GET_MODE (x) == VOIDmode
8975 && (GET_CODE (x) == CONST_INT
8976 || GET_CODE (x) == CONST_DOUBLE))
8977 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
230d793d
RS
8978 return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8979
8980 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
8981 won't know what to do. So we will strip off the SUBREG here and
8982 process normally. */
8983 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
8984 {
8985 x = SUBREG_REG (x);
8986 if (GET_MODE (x) == mode)
8987 return x;
8988 }
8989
8990 result = gen_lowpart_common (mode, x);
64bf47a2
RK
8991 if (result != 0
8992 && GET_CODE (result) == SUBREG
8993 && GET_CODE (SUBREG_REG (result)) == REG
8994 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
8995 && (GET_MODE_SIZE (GET_MODE (result))
8996 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
b1f21e0a 8997 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
64bf47a2 8998
230d793d
RS
8999 if (result)
9000 return result;
9001
9002 if (GET_CODE (x) == MEM)
9003 {
9004 register int offset = 0;
9005 rtx new;
9006
9007 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9008 address. */
9009 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9010 return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
9011
9012 /* If we want to refer to something bigger than the original memref,
9013 generate a perverse subreg instead. That will force a reload
9014 of the original memref X. */
9015 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9016 return gen_rtx (SUBREG, mode, x, 0);
9017
f76b9db2
ILT
9018 if (WORDS_BIG_ENDIAN)
9019 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9020 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9021 if (BYTES_BIG_ENDIAN)
9022 {
9023 /* Adjust the address so that the address-after-the-data is
9024 unchanged. */
9025 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9026 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9027 }
230d793d
RS
9028 new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
9029 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9030 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
9031 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
9032 return new;
9033 }
9034
9035 /* If X is a comparison operator, rewrite it in a new mode. This
9036 probably won't match, but may allow further simplifications. */
9037 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9038 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9039
9040 /* If we couldn't simplify X any other way, just enclose it in a
9041 SUBREG. Normally, this SUBREG won't match, but some patterns may
a7c99304 9042 include an explicit SUBREG or we may simplify it further in combine. */
230d793d 9043 else
dfbe1b2f
RK
9044 {
9045 int word = 0;
9046
9047 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9048 word = ((GET_MODE_SIZE (GET_MODE (x))
9049 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9050 / UNITS_PER_WORD);
9051 return gen_rtx (SUBREG, mode, x, word);
9052 }
230d793d
RS
9053}
9054\f
9055/* Make an rtx expression. This is a subset of gen_rtx and only supports
9056 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9057
9058 If the identical expression was previously in the insn (in the undobuf),
9059 it will be returned. Only if it is not found will a new expression
9060 be made. */
9061
9062/*VARARGS2*/
9063static rtx
4f90e4a0 9064gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
230d793d 9065{
4f90e4a0 9066#ifndef __STDC__
230d793d
RS
9067 enum rtx_code code;
9068 enum machine_mode mode;
4f90e4a0
RK
9069#endif
9070 va_list p;
230d793d
RS
9071 int n_args;
9072 rtx args[3];
9073 int i, j;
9074 char *fmt;
9075 rtx rt;
241cea85 9076 struct undo *undo;
230d793d 9077
4f90e4a0
RK
9078 VA_START (p, mode);
9079
9080#ifndef __STDC__
230d793d
RS
9081 code = va_arg (p, enum rtx_code);
9082 mode = va_arg (p, enum machine_mode);
4f90e4a0
RK
9083#endif
9084
230d793d
RS
9085 n_args = GET_RTX_LENGTH (code);
9086 fmt = GET_RTX_FORMAT (code);
9087
9088 if (n_args == 0 || n_args > 3)
9089 abort ();
9090
9091 /* Get each arg and verify that it is supposed to be an expression. */
9092 for (j = 0; j < n_args; j++)
9093 {
9094 if (*fmt++ != 'e')
9095 abort ();
9096
9097 args[j] = va_arg (p, rtx);
9098 }
9099
9100 /* See if this is in undobuf. Be sure we don't use objects that came
9101 from another insn; this could produce circular rtl structures. */
9102
241cea85
RK
9103 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9104 if (!undo->is_int
9105 && GET_CODE (undo->old_contents.r) == code
9106 && GET_MODE (undo->old_contents.r) == mode)
230d793d
RS
9107 {
9108 for (j = 0; j < n_args; j++)
241cea85 9109 if (XEXP (undo->old_contents.r, j) != args[j])
230d793d
RS
9110 break;
9111
9112 if (j == n_args)
241cea85 9113 return undo->old_contents.r;
230d793d
RS
9114 }
9115
9116 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9117 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9118 rt = rtx_alloc (code);
9119 PUT_MODE (rt, mode);
9120 XEXP (rt, 0) = args[0];
9121 if (n_args > 1)
9122 {
9123 XEXP (rt, 1) = args[1];
9124 if (n_args > 2)
9125 XEXP (rt, 2) = args[2];
9126 }
9127 return rt;
9128}
9129
9130/* These routines make binary and unary operations by first seeing if they
9131 fold; if not, a new expression is allocated. */
9132
9133static rtx
9134gen_binary (code, mode, op0, op1)
9135 enum rtx_code code;
9136 enum machine_mode mode;
9137 rtx op0, op1;
9138{
9139 rtx result;
1a26b032
RK
9140 rtx tem;
9141
9142 if (GET_RTX_CLASS (code) == 'c'
9143 && (GET_CODE (op0) == CONST_INT
9144 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9145 tem = op0, op0 = op1, op1 = tem;
230d793d
RS
9146
9147 if (GET_RTX_CLASS (code) == '<')
9148 {
9149 enum machine_mode op_mode = GET_MODE (op0);
9210df58
RK
9150
9151 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
0f41302f 9152 just (REL_OP X Y). */
9210df58
RK
9153 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9154 {
9155 op1 = XEXP (op0, 1);
9156 op0 = XEXP (op0, 0);
9157 op_mode = GET_MODE (op0);
9158 }
9159
230d793d
RS
9160 if (op_mode == VOIDmode)
9161 op_mode = GET_MODE (op1);
9162 result = simplify_relational_operation (code, op_mode, op0, op1);
9163 }
9164 else
9165 result = simplify_binary_operation (code, mode, op0, op1);
9166
9167 if (result)
9168 return result;
9169
9170 /* Put complex operands first and constants second. */
9171 if (GET_RTX_CLASS (code) == 'c'
9172 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9173 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9174 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9175 || (GET_CODE (op0) == SUBREG
9176 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9177 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9178 return gen_rtx_combine (code, mode, op1, op0);
9179
9180 return gen_rtx_combine (code, mode, op0, op1);
9181}
9182
9183static rtx
0c1c8ea6 9184gen_unary (code, mode, op0_mode, op0)
230d793d 9185 enum rtx_code code;
0c1c8ea6 9186 enum machine_mode mode, op0_mode;
230d793d
RS
9187 rtx op0;
9188{
0c1c8ea6 9189 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
230d793d
RS
9190
9191 if (result)
9192 return result;
9193
9194 return gen_rtx_combine (code, mode, op0);
9195}
9196\f
9197/* Simplify a comparison between *POP0 and *POP1 where CODE is the
9198 comparison code that will be tested.
9199
9200 The result is a possibly different comparison code to use. *POP0 and
9201 *POP1 may be updated.
9202
9203 It is possible that we might detect that a comparison is either always
9204 true or always false. However, we do not perform general constant
5089e22e 9205 folding in combine, so this knowledge isn't useful. Such tautologies
230d793d
RS
9206 should have been detected earlier. Hence we ignore all such cases. */
9207
9208static enum rtx_code
9209simplify_comparison (code, pop0, pop1)
9210 enum rtx_code code;
9211 rtx *pop0;
9212 rtx *pop1;
9213{
9214 rtx op0 = *pop0;
9215 rtx op1 = *pop1;
9216 rtx tem, tem1;
9217 int i;
9218 enum machine_mode mode, tmode;
9219
9220 /* Try a few ways of applying the same transformation to both operands. */
9221 while (1)
9222 {
3a19aabc
RK
9223#ifndef WORD_REGISTER_OPERATIONS
9224 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9225 so check specially. */
9226 if (code != GTU && code != GEU && code != LTU && code != LEU
9227 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9228 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9229 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9230 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9231 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9232 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
ad25ba17 9233 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
3a19aabc
RK
9234 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9235 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9236 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9237 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9238 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9239 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9240 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9241 && (INTVAL (XEXP (op0, 1))
9242 == (GET_MODE_BITSIZE (GET_MODE (op0))
9243 - (GET_MODE_BITSIZE
9244 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9245 {
9246 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9247 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9248 }
9249#endif
9250
230d793d
RS
9251 /* If both operands are the same constant shift, see if we can ignore the
9252 shift. We can if the shift is a rotate or if the bits shifted out of
951553af 9253 this shift are known to be zero for both inputs and if the type of
230d793d 9254 comparison is compatible with the shift. */
67232b23
RK
9255 if (GET_CODE (op0) == GET_CODE (op1)
9256 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9257 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
45620ed4 9258 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
67232b23
RK
9259 && (code != GT && code != LT && code != GE && code != LE))
9260 || (GET_CODE (op0) == ASHIFTRT
9261 && (code != GTU && code != LTU
9262 && code != GEU && code != GEU)))
9263 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9264 && INTVAL (XEXP (op0, 1)) >= 0
9265 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9266 && XEXP (op0, 1) == XEXP (op1, 1))
230d793d
RS
9267 {
9268 enum machine_mode mode = GET_MODE (op0);
5f4f0e22 9269 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
230d793d
RS
9270 int shift_count = INTVAL (XEXP (op0, 1));
9271
9272 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9273 mask &= (mask >> shift_count) << shift_count;
45620ed4 9274 else if (GET_CODE (op0) == ASHIFT)
230d793d
RS
9275 mask = (mask & (mask << shift_count)) >> shift_count;
9276
951553af
RK
9277 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9278 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
230d793d
RS
9279 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9280 else
9281 break;
9282 }
9283
9284 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9285 SUBREGs are of the same mode, and, in both cases, the AND would
9286 be redundant if the comparison was done in the narrower mode,
9287 do the comparison in the narrower mode (e.g., we are AND'ing with 1
951553af
RK
9288 and the operand's possibly nonzero bits are 0xffffff01; in that case
9289 if we only care about QImode, we don't need the AND). This case
9290 occurs if the output mode of an scc insn is not SImode and
7e4dc511
RK
9291 STORE_FLAG_VALUE == 1 (e.g., the 386).
9292
9293 Similarly, check for a case where the AND's are ZERO_EXTEND
9294 operations from some narrower mode even though a SUBREG is not
9295 present. */
230d793d
RS
9296
9297 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9298 && GET_CODE (XEXP (op0, 1)) == CONST_INT
7e4dc511 9299 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
230d793d 9300 {
7e4dc511
RK
9301 rtx inner_op0 = XEXP (op0, 0);
9302 rtx inner_op1 = XEXP (op1, 0);
9303 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9304 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9305 int changed = 0;
9306
9307 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9308 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9309 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9310 && (GET_MODE (SUBREG_REG (inner_op0))
9311 == GET_MODE (SUBREG_REG (inner_op1)))
9312 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9313 <= HOST_BITS_PER_WIDE_INT)
01c82bbb
RK
9314 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9315 GET_MODE (SUBREG_REG (op0)))))
9316 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9317 GET_MODE (SUBREG_REG (inner_op1))))))
7e4dc511
RK
9318 {
9319 op0 = SUBREG_REG (inner_op0);
9320 op1 = SUBREG_REG (inner_op1);
9321
9322 /* The resulting comparison is always unsigned since we masked
0f41302f 9323 off the original sign bit. */
7e4dc511
RK
9324 code = unsigned_condition (code);
9325
9326 changed = 1;
9327 }
230d793d 9328
7e4dc511
RK
9329 else if (c0 == c1)
9330 for (tmode = GET_CLASS_NARROWEST_MODE
9331 (GET_MODE_CLASS (GET_MODE (op0)));
9332 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9333 if (c0 == GET_MODE_MASK (tmode))
9334 {
9335 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9336 op1 = gen_lowpart_for_combine (tmode, inner_op1);
66415c8b 9337 code = unsigned_condition (code);
7e4dc511
RK
9338 changed = 1;
9339 break;
9340 }
9341
9342 if (! changed)
9343 break;
230d793d 9344 }
3a19aabc 9345
ad25ba17
RK
9346 /* If both operands are NOT, we can strip off the outer operation
9347 and adjust the comparison code for swapped operands; similarly for
9348 NEG, except that this must be an equality comparison. */
9349 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9350 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9351 && (code == EQ || code == NE)))
9352 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
3a19aabc 9353
230d793d
RS
9354 else
9355 break;
9356 }
9357
9358 /* If the first operand is a constant, swap the operands and adjust the
3aceff0d
RK
9359 comparison code appropriately, but don't do this if the second operand
9360 is already a constant integer. */
9361 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
230d793d
RS
9362 {
9363 tem = op0, op0 = op1, op1 = tem;
9364 code = swap_condition (code);
9365 }
9366
9367 /* We now enter a loop during which we will try to simplify the comparison.
9368 For the most part, we only are concerned with comparisons with zero,
9369 but some things may really be comparisons with zero but not start
9370 out looking that way. */
9371
9372 while (GET_CODE (op1) == CONST_INT)
9373 {
9374 enum machine_mode mode = GET_MODE (op0);
9375 int mode_width = GET_MODE_BITSIZE (mode);
5f4f0e22 9376 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
230d793d
RS
9377 int equality_comparison_p;
9378 int sign_bit_comparison_p;
9379 int unsigned_comparison_p;
5f4f0e22 9380 HOST_WIDE_INT const_op;
230d793d
RS
9381
9382 /* We only want to handle integral modes. This catches VOIDmode,
9383 CCmode, and the floating-point modes. An exception is that we
9384 can handle VOIDmode if OP0 is a COMPARE or a comparison
9385 operation. */
9386
9387 if (GET_MODE_CLASS (mode) != MODE_INT
9388 && ! (mode == VOIDmode
9389 && (GET_CODE (op0) == COMPARE
9390 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9391 break;
9392
9393 /* Get the constant we are comparing against and turn off all bits
9394 not on in our mode. */
9395 const_op = INTVAL (op1);
5f4f0e22 9396 if (mode_width <= HOST_BITS_PER_WIDE_INT)
4803a34a 9397 const_op &= mask;
230d793d
RS
9398
9399 /* If we are comparing against a constant power of two and the value
951553af 9400 being compared can only have that single bit nonzero (e.g., it was
230d793d
RS
9401 `and'ed with that bit), we can replace this with a comparison
9402 with zero. */
9403 if (const_op
9404 && (code == EQ || code == NE || code == GE || code == GEU
9405 || code == LT || code == LTU)
5f4f0e22 9406 && mode_width <= HOST_BITS_PER_WIDE_INT
230d793d 9407 && exact_log2 (const_op) >= 0
951553af 9408 && nonzero_bits (op0, mode) == const_op)
230d793d
RS
9409 {
9410 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9411 op1 = const0_rtx, const_op = 0;
9412 }
9413
d0ab8cd3
RK
9414 /* Similarly, if we are comparing a value known to be either -1 or
9415 0 with -1, change it to the opposite comparison against zero. */
9416
9417 if (const_op == -1
9418 && (code == EQ || code == NE || code == GT || code == LE
9419 || code == GEU || code == LTU)
9420 && num_sign_bit_copies (op0, mode) == mode_width)
9421 {
9422 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9423 op1 = const0_rtx, const_op = 0;
9424 }
9425
230d793d 9426 /* Do some canonicalizations based on the comparison code. We prefer
4803a34a
RK
9427 comparisons against zero and then prefer equality comparisons.
9428 If we can reduce the size of a constant, we will do that too. */
230d793d
RS
9429
9430 switch (code)
9431 {
9432 case LT:
4803a34a
RK
9433 /* < C is equivalent to <= (C - 1) */
9434 if (const_op > 0)
230d793d 9435 {
4803a34a 9436 const_op -= 1;
5f4f0e22 9437 op1 = GEN_INT (const_op);
230d793d
RS
9438 code = LE;
9439 /* ... fall through to LE case below. */
9440 }
9441 else
9442 break;
9443
9444 case LE:
4803a34a
RK
9445 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9446 if (const_op < 0)
9447 {
9448 const_op += 1;
5f4f0e22 9449 op1 = GEN_INT (const_op);
4803a34a
RK
9450 code = LT;
9451 }
230d793d
RS
9452
9453 /* If we are doing a <= 0 comparison on a value known to have
9454 a zero sign bit, we can replace this with == 0. */
9455 else if (const_op == 0
5f4f0e22 9456 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9457 && (nonzero_bits (op0, mode)
5f4f0e22 9458 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
230d793d
RS
9459 code = EQ;
9460 break;
9461
9462 case GE:
0f41302f 9463 /* >= C is equivalent to > (C - 1). */
4803a34a 9464 if (const_op > 0)
230d793d 9465 {
4803a34a 9466 const_op -= 1;
5f4f0e22 9467 op1 = GEN_INT (const_op);
230d793d
RS
9468 code = GT;
9469 /* ... fall through to GT below. */
9470 }
9471 else
9472 break;
9473
9474 case GT:
4803a34a
RK
9475 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9476 if (const_op < 0)
9477 {
9478 const_op += 1;
5f4f0e22 9479 op1 = GEN_INT (const_op);
4803a34a
RK
9480 code = GE;
9481 }
230d793d
RS
9482
9483 /* If we are doing a > 0 comparison on a value known to have
9484 a zero sign bit, we can replace this with != 0. */
9485 else if (const_op == 0
5f4f0e22 9486 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9487 && (nonzero_bits (op0, mode)
5f4f0e22 9488 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
230d793d
RS
9489 code = NE;
9490 break;
9491
230d793d 9492 case LTU:
4803a34a
RK
9493 /* < C is equivalent to <= (C - 1). */
9494 if (const_op > 0)
9495 {
9496 const_op -= 1;
5f4f0e22 9497 op1 = GEN_INT (const_op);
4803a34a 9498 code = LEU;
0f41302f 9499 /* ... fall through ... */
4803a34a 9500 }
d0ab8cd3
RK
9501
9502 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
f77aada2
JW
9503 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9504 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
d0ab8cd3
RK
9505 {
9506 const_op = 0, op1 = const0_rtx;
9507 code = GE;
9508 break;
9509 }
4803a34a
RK
9510 else
9511 break;
230d793d
RS
9512
9513 case LEU:
9514 /* unsigned <= 0 is equivalent to == 0 */
9515 if (const_op == 0)
9516 code = EQ;
d0ab8cd3 9517
0f41302f 9518 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
f77aada2
JW
9519 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9520 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
d0ab8cd3
RK
9521 {
9522 const_op = 0, op1 = const0_rtx;
9523 code = GE;
9524 }
230d793d
RS
9525 break;
9526
4803a34a
RK
9527 case GEU:
9528 /* >= C is equivalent to < (C - 1). */
9529 if (const_op > 1)
9530 {
9531 const_op -= 1;
5f4f0e22 9532 op1 = GEN_INT (const_op);
4803a34a 9533 code = GTU;
0f41302f 9534 /* ... fall through ... */
4803a34a 9535 }
d0ab8cd3
RK
9536
9537 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
f77aada2
JW
9538 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9539 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
d0ab8cd3
RK
9540 {
9541 const_op = 0, op1 = const0_rtx;
9542 code = LT;
8b2e69e1 9543 break;
d0ab8cd3 9544 }
4803a34a
RK
9545 else
9546 break;
9547
230d793d
RS
9548 case GTU:
9549 /* unsigned > 0 is equivalent to != 0 */
9550 if (const_op == 0)
9551 code = NE;
d0ab8cd3
RK
9552
9553 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
f77aada2
JW
9554 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9555 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
d0ab8cd3
RK
9556 {
9557 const_op = 0, op1 = const0_rtx;
9558 code = LT;
9559 }
230d793d 9560 break;
e9a25f70
JL
9561
9562 default:
9563 break;
230d793d
RS
9564 }
9565
9566 /* Compute some predicates to simplify code below. */
9567
9568 equality_comparison_p = (code == EQ || code == NE);
9569 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9570 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9571 || code == LEU);
9572
6139ff20
RK
9573 /* If this is a sign bit comparison and we can do arithmetic in
9574 MODE, say that we will only be needing the sign bit of OP0. */
9575 if (sign_bit_comparison_p
9576 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9577 op0 = force_to_mode (op0, mode,
9578 ((HOST_WIDE_INT) 1
9579 << (GET_MODE_BITSIZE (mode) - 1)),
e3d616e3 9580 NULL_RTX, 0);
6139ff20 9581
230d793d
RS
9582 /* Now try cases based on the opcode of OP0. If none of the cases
9583 does a "continue", we exit this loop immediately after the
9584 switch. */
9585
9586 switch (GET_CODE (op0))
9587 {
9588 case ZERO_EXTRACT:
9589 /* If we are extracting a single bit from a variable position in
9590 a constant that has only a single bit set and are comparing it
9591 with zero, we can convert this into an equality comparison
d7cd794f 9592 between the position and the location of the single bit. */
230d793d 9593
230d793d
RS
9594 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9595 && XEXP (op0, 1) == const1_rtx
9596 && equality_comparison_p && const_op == 0
d7cd794f 9597 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
230d793d 9598 {
f76b9db2 9599 if (BITS_BIG_ENDIAN)
d7cd794f 9600#ifdef HAVE_extzv
f76b9db2
ILT
9601 i = (GET_MODE_BITSIZE
9602 (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
d7cd794f
RK
9603#else
9604 i = BITS_PER_WORD - 1 - i;
230d793d
RS
9605#endif
9606
9607 op0 = XEXP (op0, 2);
5f4f0e22 9608 op1 = GEN_INT (i);
230d793d
RS
9609 const_op = i;
9610
9611 /* Result is nonzero iff shift count is equal to I. */
9612 code = reverse_condition (code);
9613 continue;
9614 }
230d793d 9615
0f41302f 9616 /* ... fall through ... */
230d793d
RS
9617
9618 case SIGN_EXTRACT:
9619 tem = expand_compound_operation (op0);
9620 if (tem != op0)
9621 {
9622 op0 = tem;
9623 continue;
9624 }
9625 break;
9626
9627 case NOT:
9628 /* If testing for equality, we can take the NOT of the constant. */
9629 if (equality_comparison_p
9630 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9631 {
9632 op0 = XEXP (op0, 0);
9633 op1 = tem;
9634 continue;
9635 }
9636
9637 /* If just looking at the sign bit, reverse the sense of the
9638 comparison. */
9639 if (sign_bit_comparison_p)
9640 {
9641 op0 = XEXP (op0, 0);
9642 code = (code == GE ? LT : GE);
9643 continue;
9644 }
9645 break;
9646
9647 case NEG:
9648 /* If testing for equality, we can take the NEG of the constant. */
9649 if (equality_comparison_p
9650 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9651 {
9652 op0 = XEXP (op0, 0);
9653 op1 = tem;
9654 continue;
9655 }
9656
9657 /* The remaining cases only apply to comparisons with zero. */
9658 if (const_op != 0)
9659 break;
9660
9661 /* When X is ABS or is known positive,
9662 (neg X) is < 0 if and only if X != 0. */
9663
9664 if (sign_bit_comparison_p
9665 && (GET_CODE (XEXP (op0, 0)) == ABS
5f4f0e22 9666 || (mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9667 && (nonzero_bits (XEXP (op0, 0), mode)
5f4f0e22 9668 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
230d793d
RS
9669 {
9670 op0 = XEXP (op0, 0);
9671 code = (code == LT ? NE : EQ);
9672 continue;
9673 }
9674
3bed8141 9675 /* If we have NEG of something whose two high-order bits are the
0f41302f 9676 same, we know that "(-a) < 0" is equivalent to "a > 0". */
3bed8141 9677 if (num_sign_bit_copies (op0, mode) >= 2)
230d793d
RS
9678 {
9679 op0 = XEXP (op0, 0);
9680 code = swap_condition (code);
9681 continue;
9682 }
9683 break;
9684
9685 case ROTATE:
9686 /* If we are testing equality and our count is a constant, we
9687 can perform the inverse operation on our RHS. */
9688 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9689 && (tem = simplify_binary_operation (ROTATERT, mode,
9690 op1, XEXP (op0, 1))) != 0)
9691 {
9692 op0 = XEXP (op0, 0);
9693 op1 = tem;
9694 continue;
9695 }
9696
9697 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9698 a particular bit. Convert it to an AND of a constant of that
9699 bit. This will be converted into a ZERO_EXTRACT. */
9700 if (const_op == 0 && sign_bit_comparison_p
9701 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5f4f0e22 9702 && mode_width <= HOST_BITS_PER_WIDE_INT)
230d793d 9703 {
5f4f0e22
CH
9704 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9705 ((HOST_WIDE_INT) 1
9706 << (mode_width - 1
9707 - INTVAL (XEXP (op0, 1)))));
230d793d
RS
9708 code = (code == LT ? NE : EQ);
9709 continue;
9710 }
9711
0f41302f 9712 /* ... fall through ... */
230d793d
RS
9713
9714 case ABS:
9715 /* ABS is ignorable inside an equality comparison with zero. */
9716 if (const_op == 0 && equality_comparison_p)
9717 {
9718 op0 = XEXP (op0, 0);
9719 continue;
9720 }
9721 break;
9722
9723
9724 case SIGN_EXTEND:
9725 /* Can simplify (compare (zero/sign_extend FOO) CONST)
9726 to (compare FOO CONST) if CONST fits in FOO's mode and we
9727 are either testing inequality or have an unsigned comparison
9728 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
9729 if (! unsigned_comparison_p
9730 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
5f4f0e22
CH
9731 <= HOST_BITS_PER_WIDE_INT)
9732 && ((unsigned HOST_WIDE_INT) const_op
9733 < (((HOST_WIDE_INT) 1
9734 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
230d793d
RS
9735 {
9736 op0 = XEXP (op0, 0);
9737 continue;
9738 }
9739 break;
9740
9741 case SUBREG:
a687e897 9742 /* Check for the case where we are comparing A - C1 with C2,
abc95ed3 9743 both constants are smaller than 1/2 the maximum positive
a687e897
RK
9744 value in MODE, and the comparison is equality or unsigned.
9745 In that case, if A is either zero-extended to MODE or has
9746 sufficient sign bits so that the high-order bit in MODE
9747 is a copy of the sign in the inner mode, we can prove that it is
9748 safe to do the operation in the wider mode. This simplifies
9749 many range checks. */
9750
9751 if (mode_width <= HOST_BITS_PER_WIDE_INT
9752 && subreg_lowpart_p (op0)
9753 && GET_CODE (SUBREG_REG (op0)) == PLUS
9754 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9755 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9756 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
9757 < GET_MODE_MASK (mode) / 2)
adb7a1cb 9758 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
951553af
RK
9759 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9760 GET_MODE (SUBREG_REG (op0)))
a687e897
RK
9761 & ~ GET_MODE_MASK (mode))
9762 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9763 GET_MODE (SUBREG_REG (op0)))
9764 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9765 - GET_MODE_BITSIZE (mode)))))
9766 {
9767 op0 = SUBREG_REG (op0);
9768 continue;
9769 }
9770
fe0cf571
RK
9771 /* If the inner mode is narrower and we are extracting the low part,
9772 we can treat the SUBREG as if it were a ZERO_EXTEND. */
9773 if (subreg_lowpart_p (op0)
89f1c7f2
RS
9774 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9775 /* Fall through */ ;
9776 else
230d793d
RS
9777 break;
9778
0f41302f 9779 /* ... fall through ... */
230d793d
RS
9780
9781 case ZERO_EXTEND:
9782 if ((unsigned_comparison_p || equality_comparison_p)
9783 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
5f4f0e22
CH
9784 <= HOST_BITS_PER_WIDE_INT)
9785 && ((unsigned HOST_WIDE_INT) const_op
230d793d
RS
9786 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9787 {
9788 op0 = XEXP (op0, 0);
9789 continue;
9790 }
9791 break;
9792
9793 case PLUS:
20fdd649 9794 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
5089e22e 9795 this for equality comparisons due to pathological cases involving
230d793d 9796 overflows. */
20fdd649
RK
9797 if (equality_comparison_p
9798 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9799 op1, XEXP (op0, 1))))
230d793d
RS
9800 {
9801 op0 = XEXP (op0, 0);
9802 op1 = tem;
9803 continue;
9804 }
9805
9806 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
9807 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9808 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9809 {
9810 op0 = XEXP (XEXP (op0, 0), 0);
9811 code = (code == LT ? EQ : NE);
9812 continue;
9813 }
9814 break;
9815
9816 case MINUS:
20fdd649
RK
9817 /* (eq (minus A B) C) -> (eq A (plus B C)) or
9818 (eq B (minus A C)), whichever simplifies. We can only do
9819 this for equality comparisons due to pathological cases involving
9820 overflows. */
9821 if (equality_comparison_p
9822 && 0 != (tem = simplify_binary_operation (PLUS, mode,
9823 XEXP (op0, 1), op1)))
9824 {
9825 op0 = XEXP (op0, 0);
9826 op1 = tem;
9827 continue;
9828 }
9829
9830 if (equality_comparison_p
9831 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9832 XEXP (op0, 0), op1)))
9833 {
9834 op0 = XEXP (op0, 1);
9835 op1 = tem;
9836 continue;
9837 }
9838
230d793d
RS
9839 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
9840 of bits in X minus 1, is one iff X > 0. */
9841 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
9842 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9843 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
9844 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9845 {
9846 op0 = XEXP (op0, 1);
9847 code = (code == GE ? LE : GT);
9848 continue;
9849 }
9850 break;
9851
9852 case XOR:
9853 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
9854 if C is zero or B is a constant. */
9855 if (equality_comparison_p
9856 && 0 != (tem = simplify_binary_operation (XOR, mode,
9857 XEXP (op0, 1), op1)))
9858 {
9859 op0 = XEXP (op0, 0);
9860 op1 = tem;
9861 continue;
9862 }
9863 break;
9864
9865 case EQ: case NE:
9866 case LT: case LTU: case LE: case LEU:
9867 case GT: case GTU: case GE: case GEU:
9868 /* We can't do anything if OP0 is a condition code value, rather
9869 than an actual data value. */
9870 if (const_op != 0
9871#ifdef HAVE_cc0
9872 || XEXP (op0, 0) == cc0_rtx
9873#endif
9874 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
9875 break;
9876
9877 /* Get the two operands being compared. */
9878 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
9879 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
9880 else
9881 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
9882
9883 /* Check for the cases where we simply want the result of the
9884 earlier test or the opposite of that result. */
9885 if (code == NE
9886 || (code == EQ && reversible_comparison_p (op0))
5f4f0e22 9887 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
3f508eca 9888 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
230d793d 9889 && (STORE_FLAG_VALUE
5f4f0e22
CH
9890 & (((HOST_WIDE_INT) 1
9891 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
230d793d
RS
9892 && (code == LT
9893 || (code == GE && reversible_comparison_p (op0)))))
9894 {
9895 code = (code == LT || code == NE
9896 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
9897 op0 = tem, op1 = tem1;
9898 continue;
9899 }
9900 break;
9901
9902 case IOR:
9903 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
9904 iff X <= 0. */
9905 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
9906 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
9907 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9908 {
9909 op0 = XEXP (op0, 1);
9910 code = (code == GE ? GT : LE);
9911 continue;
9912 }
9913 break;
9914
9915 case AND:
9916 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
9917 will be converted to a ZERO_EXTRACT later. */
9918 if (const_op == 0 && equality_comparison_p
45620ed4 9919 && GET_CODE (XEXP (op0, 0)) == ASHIFT
230d793d
RS
9920 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
9921 {
9922 op0 = simplify_and_const_int
9923 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
9924 XEXP (op0, 1),
9925 XEXP (XEXP (op0, 0), 1)),
5f4f0e22 9926 (HOST_WIDE_INT) 1);
230d793d
RS
9927 continue;
9928 }
9929
9930 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
9931 zero and X is a comparison and C1 and C2 describe only bits set
9932 in STORE_FLAG_VALUE, we can compare with X. */
9933 if (const_op == 0 && equality_comparison_p
5f4f0e22 9934 && mode_width <= HOST_BITS_PER_WIDE_INT
230d793d
RS
9935 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9936 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
9937 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9938 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
5f4f0e22 9939 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
230d793d
RS
9940 {
9941 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9942 << INTVAL (XEXP (XEXP (op0, 0), 1)));
9943 if ((~ STORE_FLAG_VALUE & mask) == 0
9944 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
9945 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
9946 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
9947 {
9948 op0 = XEXP (XEXP (op0, 0), 0);
9949 continue;
9950 }
9951 }
9952
9953 /* If we are doing an equality comparison of an AND of a bit equal
9954 to the sign bit, replace this with a LT or GE comparison of
9955 the underlying value. */
9956 if (equality_comparison_p
9957 && const_op == 0
9958 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5f4f0e22 9959 && mode_width <= HOST_BITS_PER_WIDE_INT
230d793d 9960 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
5f4f0e22 9961 == (HOST_WIDE_INT) 1 << (mode_width - 1)))
230d793d
RS
9962 {
9963 op0 = XEXP (op0, 0);
9964 code = (code == EQ ? GE : LT);
9965 continue;
9966 }
9967
9968 /* If this AND operation is really a ZERO_EXTEND from a narrower
9969 mode, the constant fits within that mode, and this is either an
9970 equality or unsigned comparison, try to do this comparison in
9971 the narrower mode. */
9972 if ((equality_comparison_p || unsigned_comparison_p)
9973 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9974 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
9975 & GET_MODE_MASK (mode))
9976 + 1)) >= 0
9977 && const_op >> i == 0
9978 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
9979 {
9980 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
9981 continue;
9982 }
9983 break;
9984
9985 case ASHIFT:
45620ed4 9986 /* If we have (compare (ashift FOO N) (const_int C)) and
230d793d 9987 the high order N bits of FOO (N+1 if an inequality comparison)
951553af 9988 are known to be zero, we can do this by comparing FOO with C
230d793d
RS
9989 shifted right N bits so long as the low-order N bits of C are
9990 zero. */
9991 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9992 && INTVAL (XEXP (op0, 1)) >= 0
9993 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
5f4f0e22
CH
9994 < HOST_BITS_PER_WIDE_INT)
9995 && ((const_op
34785d05 9996 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
5f4f0e22 9997 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 9998 && (nonzero_bits (XEXP (op0, 0), mode)
230d793d
RS
9999 & ~ (mask >> (INTVAL (XEXP (op0, 1))
10000 + ! equality_comparison_p))) == 0)
10001 {
10002 const_op >>= INTVAL (XEXP (op0, 1));
5f4f0e22 10003 op1 = GEN_INT (const_op);
230d793d
RS
10004 op0 = XEXP (op0, 0);
10005 continue;
10006 }
10007
dfbe1b2f 10008 /* If we are doing a sign bit comparison, it means we are testing
230d793d 10009 a particular bit. Convert it to the appropriate AND. */
dfbe1b2f 10010 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
5f4f0e22 10011 && mode_width <= HOST_BITS_PER_WIDE_INT)
230d793d 10012 {
5f4f0e22
CH
10013 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10014 ((HOST_WIDE_INT) 1
10015 << (mode_width - 1
10016 - INTVAL (XEXP (op0, 1)))));
230d793d
RS
10017 code = (code == LT ? NE : EQ);
10018 continue;
10019 }
dfbe1b2f
RK
10020
10021 /* If this an equality comparison with zero and we are shifting
10022 the low bit to the sign bit, we can convert this to an AND of the
10023 low-order bit. */
10024 if (const_op == 0 && equality_comparison_p
10025 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10026 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10027 {
5f4f0e22
CH
10028 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10029 (HOST_WIDE_INT) 1);
dfbe1b2f
RK
10030 continue;
10031 }
230d793d
RS
10032 break;
10033
10034 case ASHIFTRT:
d0ab8cd3
RK
10035 /* If this is an equality comparison with zero, we can do this
10036 as a logical shift, which might be much simpler. */
10037 if (equality_comparison_p && const_op == 0
10038 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10039 {
10040 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10041 XEXP (op0, 0),
10042 INTVAL (XEXP (op0, 1)));
10043 continue;
10044 }
10045
230d793d
RS
10046 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10047 do the comparison in a narrower mode. */
10048 if (! unsigned_comparison_p
10049 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10050 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10051 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10052 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
22331794 10053 MODE_INT, 1)) != BLKmode
5f4f0e22
CH
10054 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10055 || ((unsigned HOST_WIDE_INT) - const_op
10056 <= GET_MODE_MASK (tmode))))
230d793d
RS
10057 {
10058 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10059 continue;
10060 }
10061
0f41302f 10062 /* ... fall through ... */
230d793d
RS
10063 case LSHIFTRT:
10064 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
951553af 10065 the low order N bits of FOO are known to be zero, we can do this
230d793d
RS
10066 by comparing FOO with C shifted left N bits so long as no
10067 overflow occurs. */
10068 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10069 && INTVAL (XEXP (op0, 1)) >= 0
5f4f0e22
CH
10070 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10071 && mode_width <= HOST_BITS_PER_WIDE_INT
951553af 10072 && (nonzero_bits (XEXP (op0, 0), mode)
5f4f0e22 10073 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
230d793d
RS
10074 && (const_op == 0
10075 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10076 < mode_width)))
10077 {
10078 const_op <<= INTVAL (XEXP (op0, 1));
5f4f0e22 10079 op1 = GEN_INT (const_op);
230d793d
RS
10080 op0 = XEXP (op0, 0);
10081 continue;
10082 }
10083
10084 /* If we are using this shift to extract just the sign bit, we
10085 can replace this with an LT or GE comparison. */
10086 if (const_op == 0
10087 && (equality_comparison_p || sign_bit_comparison_p)
10088 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10089 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10090 {
10091 op0 = XEXP (op0, 0);
10092 code = (code == NE || code == GT ? LT : GE);
10093 continue;
10094 }
10095 break;
e9a25f70
JL
10096
10097 default:
10098 break;
230d793d
RS
10099 }
10100
10101 break;
10102 }
10103
10104 /* Now make any compound operations involved in this comparison. Then,
10105 check for an outmost SUBREG on OP0 that isn't doing anything or is
10106 paradoxical. The latter case can only occur when it is known that the
10107 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
10108 We can never remove a SUBREG for a non-equality comparison because the
10109 sign bit is in a different place in the underlying object. */
10110
10111 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10112 op1 = make_compound_operation (op1, SET);
10113
10114 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10115 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10116 && (code == NE || code == EQ)
10117 && ((GET_MODE_SIZE (GET_MODE (op0))
10118 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10119 {
10120 op0 = SUBREG_REG (op0);
10121 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10122 }
10123
10124 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10125 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10126 && (code == NE || code == EQ)
ac49a949
RS
10127 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10128 <= HOST_BITS_PER_WIDE_INT)
951553af 10129 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
230d793d
RS
10130 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10131 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10132 op1),
951553af 10133 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
230d793d
RS
10134 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10135 op0 = SUBREG_REG (op0), op1 = tem;
10136
10137 /* We now do the opposite procedure: Some machines don't have compare
10138 insns in all modes. If OP0's mode is an integer mode smaller than a
10139 word and we can't do a compare in that mode, see if there is a larger
a687e897
RK
10140 mode for which we can do the compare. There are a number of cases in
10141 which we can use the wider mode. */
230d793d
RS
10142
10143 mode = GET_MODE (op0);
10144 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10145 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10146 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10147 for (tmode = GET_MODE_WIDER_MODE (mode);
5f4f0e22
CH
10148 (tmode != VOIDmode
10149 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
230d793d 10150 tmode = GET_MODE_WIDER_MODE (tmode))
a687e897 10151 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
230d793d 10152 {
951553af 10153 /* If the only nonzero bits in OP0 and OP1 are those in the
a687e897
RK
10154 narrower mode and this is an equality or unsigned comparison,
10155 we can use the wider mode. Similarly for sign-extended
7e4dc511 10156 values, in which case it is true for all comparisons. */
a687e897
RK
10157 if (((code == EQ || code == NE
10158 || code == GEU || code == GTU || code == LEU || code == LTU)
951553af
RK
10159 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10160 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
7e4dc511
RK
10161 || ((num_sign_bit_copies (op0, tmode)
10162 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
a687e897 10163 && (num_sign_bit_copies (op1, tmode)
58744483 10164 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
a687e897
RK
10165 {
10166 op0 = gen_lowpart_for_combine (tmode, op0);
10167 op1 = gen_lowpart_for_combine (tmode, op1);
10168 break;
10169 }
230d793d 10170
a687e897
RK
10171 /* If this is a test for negative, we can make an explicit
10172 test of the sign bit. */
10173
10174 if (op1 == const0_rtx && (code == LT || code == GE)
10175 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
230d793d 10176 {
a687e897
RK
10177 op0 = gen_binary (AND, tmode,
10178 gen_lowpart_for_combine (tmode, op0),
5f4f0e22
CH
10179 GEN_INT ((HOST_WIDE_INT) 1
10180 << (GET_MODE_BITSIZE (mode) - 1)));
230d793d 10181 code = (code == LT) ? NE : EQ;
a687e897 10182 break;
230d793d 10183 }
230d793d
RS
10184 }
10185
b7a775b2
RK
10186#ifdef CANONICALIZE_COMPARISON
10187 /* If this machine only supports a subset of valid comparisons, see if we
10188 can convert an unsupported one into a supported one. */
10189 CANONICALIZE_COMPARISON (code, op0, op1);
10190#endif
10191
230d793d
RS
10192 *pop0 = op0;
10193 *pop1 = op1;
10194
10195 return code;
10196}
10197\f
10198/* Return 1 if we know that X, a comparison operation, is not operating
10199 on a floating-point value or is EQ or NE, meaning that we can safely
10200 reverse it. */
10201
10202static int
10203reversible_comparison_p (x)
10204 rtx x;
10205{
10206 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7e2a0d8e 10207 || flag_fast_math
230d793d
RS
10208 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10209 return 1;
10210
10211 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10212 {
10213 case MODE_INT:
3ad2180a
RK
10214 case MODE_PARTIAL_INT:
10215 case MODE_COMPLEX_INT:
230d793d
RS
10216 return 1;
10217
10218 case MODE_CC:
9210df58
RK
10219 /* If the mode of the condition codes tells us that this is safe,
10220 we need look no further. */
10221 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10222 return 1;
10223
10224 /* Otherwise try and find where the condition codes were last set and
10225 use that. */
230d793d
RS
10226 x = get_last_value (XEXP (x, 0));
10227 return (x && GET_CODE (x) == COMPARE
3ad2180a 10228 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
e9a25f70
JL
10229
10230 default:
10231 return 0;
230d793d 10232 }
230d793d
RS
10233}
10234\f
10235/* Utility function for following routine. Called when X is part of a value
10236 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10237 for each register mentioned. Similar to mention_regs in cse.c */
10238
10239static void
10240update_table_tick (x)
10241 rtx x;
10242{
10243 register enum rtx_code code = GET_CODE (x);
10244 register char *fmt = GET_RTX_FORMAT (code);
10245 register int i;
10246
10247 if (code == REG)
10248 {
10249 int regno = REGNO (x);
10250 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10251 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10252
10253 for (i = regno; i < endregno; i++)
10254 reg_last_set_table_tick[i] = label_tick;
10255
10256 return;
10257 }
10258
10259 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10260 /* Note that we can't have an "E" in values stored; see
10261 get_last_value_validate. */
10262 if (fmt[i] == 'e')
10263 update_table_tick (XEXP (x, i));
10264}
10265
10266/* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10267 are saying that the register is clobbered and we no longer know its
7988fd36
RK
10268 value. If INSN is zero, don't update reg_last_set; this is only permitted
10269 with VALUE also zero and is used to invalidate the register. */
230d793d
RS
10270
10271static void
10272record_value_for_reg (reg, insn, value)
10273 rtx reg;
10274 rtx insn;
10275 rtx value;
10276{
10277 int regno = REGNO (reg);
10278 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10279 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10280 int i;
10281
10282 /* If VALUE contains REG and we have a previous value for REG, substitute
10283 the previous value. */
10284 if (value && insn && reg_overlap_mentioned_p (reg, value))
10285 {
10286 rtx tem;
10287
10288 /* Set things up so get_last_value is allowed to see anything set up to
10289 our insn. */
10290 subst_low_cuid = INSN_CUID (insn);
10291 tem = get_last_value (reg);
10292
10293 if (tem)
10294 value = replace_rtx (copy_rtx (value), reg, tem);
10295 }
10296
10297 /* For each register modified, show we don't know its value, that
ef026f91
RS
10298 we don't know about its bitwise content, that its value has been
10299 updated, and that we don't know the location of the death of the
10300 register. */
230d793d
RS
10301 for (i = regno; i < endregno; i ++)
10302 {
10303 if (insn)
10304 reg_last_set[i] = insn;
10305 reg_last_set_value[i] = 0;
ef026f91
RS
10306 reg_last_set_mode[i] = 0;
10307 reg_last_set_nonzero_bits[i] = 0;
10308 reg_last_set_sign_bit_copies[i] = 0;
230d793d
RS
10309 reg_last_death[i] = 0;
10310 }
10311
10312 /* Mark registers that are being referenced in this value. */
10313 if (value)
10314 update_table_tick (value);
10315
10316 /* Now update the status of each register being set.
10317 If someone is using this register in this block, set this register
10318 to invalid since we will get confused between the two lives in this
10319 basic block. This makes using this register always invalid. In cse, we
10320 scan the table to invalidate all entries using this register, but this
10321 is too much work for us. */
10322
10323 for (i = regno; i < endregno; i++)
10324 {
10325 reg_last_set_label[i] = label_tick;
10326 if (value && reg_last_set_table_tick[i] == label_tick)
10327 reg_last_set_invalid[i] = 1;
10328 else
10329 reg_last_set_invalid[i] = 0;
10330 }
10331
10332 /* The value being assigned might refer to X (like in "x++;"). In that
10333 case, we must replace it with (clobber (const_int 0)) to prevent
10334 infinite loops. */
9a893315 10335 if (value && ! get_last_value_validate (&value, insn,
230d793d
RS
10336 reg_last_set_label[regno], 0))
10337 {
10338 value = copy_rtx (value);
9a893315
JW
10339 if (! get_last_value_validate (&value, insn,
10340 reg_last_set_label[regno], 1))
230d793d
RS
10341 value = 0;
10342 }
10343
55310dad
RK
10344 /* For the main register being modified, update the value, the mode, the
10345 nonzero bits, and the number of sign bit copies. */
10346
230d793d
RS
10347 reg_last_set_value[regno] = value;
10348
55310dad
RK
10349 if (value)
10350 {
2afabb48 10351 subst_low_cuid = INSN_CUID (insn);
55310dad
RK
10352 reg_last_set_mode[regno] = GET_MODE (reg);
10353 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10354 reg_last_set_sign_bit_copies[regno]
10355 = num_sign_bit_copies (value, GET_MODE (reg));
10356 }
230d793d
RS
10357}
10358
10359/* Used for communication between the following two routines. */
10360static rtx record_dead_insn;
10361
10362/* Called via note_stores from record_dead_and_set_regs to handle one
10363 SET or CLOBBER in an insn. */
10364
10365static void
10366record_dead_and_set_regs_1 (dest, setter)
10367 rtx dest, setter;
10368{
ca89d290
RK
10369 if (GET_CODE (dest) == SUBREG)
10370 dest = SUBREG_REG (dest);
10371
230d793d
RS
10372 if (GET_CODE (dest) == REG)
10373 {
10374 /* If we are setting the whole register, we know its value. Otherwise
10375 show that we don't know the value. We can handle SUBREG in
10376 some cases. */
10377 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10378 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10379 else if (GET_CODE (setter) == SET
10380 && GET_CODE (SET_DEST (setter)) == SUBREG
10381 && SUBREG_REG (SET_DEST (setter)) == dest
90bf8081 10382 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
230d793d 10383 && subreg_lowpart_p (SET_DEST (setter)))
d0ab8cd3
RK
10384 record_value_for_reg (dest, record_dead_insn,
10385 gen_lowpart_for_combine (GET_MODE (dest),
10386 SET_SRC (setter)));
230d793d 10387 else
5f4f0e22 10388 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
230d793d
RS
10389 }
10390 else if (GET_CODE (dest) == MEM
10391 /* Ignore pushes, they clobber nothing. */
10392 && ! push_operand (dest, GET_MODE (dest)))
10393 mem_last_set = INSN_CUID (record_dead_insn);
10394}
10395
10396/* Update the records of when each REG was most recently set or killed
10397 for the things done by INSN. This is the last thing done in processing
10398 INSN in the combiner loop.
10399
ef026f91
RS
10400 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10401 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10402 and also the similar information mem_last_set (which insn most recently
10403 modified memory) and last_call_cuid (which insn was the most recent
10404 subroutine call). */
230d793d
RS
10405
10406static void
10407record_dead_and_set_regs (insn)
10408 rtx insn;
10409{
10410 register rtx link;
55310dad
RK
10411 int i;
10412
230d793d
RS
10413 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10414 {
dbc131f3
RK
10415 if (REG_NOTE_KIND (link) == REG_DEAD
10416 && GET_CODE (XEXP (link, 0)) == REG)
10417 {
10418 int regno = REGNO (XEXP (link, 0));
10419 int endregno
10420 = regno + (regno < FIRST_PSEUDO_REGISTER
10421 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10422 : 1);
dbc131f3
RK
10423
10424 for (i = regno; i < endregno; i++)
10425 reg_last_death[i] = insn;
10426 }
230d793d 10427 else if (REG_NOTE_KIND (link) == REG_INC)
5f4f0e22 10428 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
230d793d
RS
10429 }
10430
10431 if (GET_CODE (insn) == CALL_INSN)
55310dad
RK
10432 {
10433 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10434 if (call_used_regs[i])
10435 {
10436 reg_last_set_value[i] = 0;
ef026f91
RS
10437 reg_last_set_mode[i] = 0;
10438 reg_last_set_nonzero_bits[i] = 0;
10439 reg_last_set_sign_bit_copies[i] = 0;
55310dad
RK
10440 reg_last_death[i] = 0;
10441 }
10442
10443 last_call_cuid = mem_last_set = INSN_CUID (insn);
10444 }
230d793d
RS
10445
10446 record_dead_insn = insn;
10447 note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10448}
10449\f
10450/* Utility routine for the following function. Verify that all the registers
10451 mentioned in *LOC are valid when *LOC was part of a value set when
10452 label_tick == TICK. Return 0 if some are not.
10453
10454 If REPLACE is non-zero, replace the invalid reference with
10455 (clobber (const_int 0)) and return 1. This replacement is useful because
10456 we often can get useful information about the form of a value (e.g., if
10457 it was produced by a shift that always produces -1 or 0) even though
10458 we don't know exactly what registers it was produced from. */
10459
10460static int
9a893315 10461get_last_value_validate (loc, insn, tick, replace)
230d793d 10462 rtx *loc;
9a893315 10463 rtx insn;
230d793d
RS
10464 int tick;
10465 int replace;
10466{
10467 rtx x = *loc;
10468 char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10469 int len = GET_RTX_LENGTH (GET_CODE (x));
10470 int i;
10471
10472 if (GET_CODE (x) == REG)
10473 {
10474 int regno = REGNO (x);
10475 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10476 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10477 int j;
10478
10479 for (j = regno; j < endregno; j++)
10480 if (reg_last_set_invalid[j]
10481 /* If this is a pseudo-register that was only set once, it is
10482 always valid. */
b1f21e0a 10483 || (! (regno >= FIRST_PSEUDO_REGISTER && REG_N_SETS (regno) == 1)
230d793d
RS
10484 && reg_last_set_label[j] > tick))
10485 {
10486 if (replace)
10487 *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10488 return replace;
10489 }
10490
10491 return 1;
10492 }
9a893315
JW
10493 /* If this is a memory reference, make sure that there were
10494 no stores after it that might have clobbered the value. We don't
10495 have alias info, so we assume any store invalidates it. */
10496 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10497 && INSN_CUID (insn) <= mem_last_set)
10498 {
10499 if (replace)
10500 *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10501 return replace;
10502 }
230d793d
RS
10503
10504 for (i = 0; i < len; i++)
10505 if ((fmt[i] == 'e'
9a893315 10506 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
230d793d
RS
10507 /* Don't bother with these. They shouldn't occur anyway. */
10508 || fmt[i] == 'E')
10509 return 0;
10510
10511 /* If we haven't found a reason for it to be invalid, it is valid. */
10512 return 1;
10513}
10514
10515/* Get the last value assigned to X, if known. Some registers
10516 in the value may be replaced with (clobber (const_int 0)) if their value
10517 is known longer known reliably. */
10518
10519static rtx
10520get_last_value (x)
10521 rtx x;
10522{
10523 int regno;
10524 rtx value;
10525
10526 /* If this is a non-paradoxical SUBREG, get the value of its operand and
10527 then convert it to the desired mode. If this is a paradoxical SUBREG,
0f41302f 10528 we cannot predict what values the "extra" bits might have. */
230d793d
RS
10529 if (GET_CODE (x) == SUBREG
10530 && subreg_lowpart_p (x)
10531 && (GET_MODE_SIZE (GET_MODE (x))
10532 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10533 && (value = get_last_value (SUBREG_REG (x))) != 0)
10534 return gen_lowpart_for_combine (GET_MODE (x), value);
10535
10536 if (GET_CODE (x) != REG)
10537 return 0;
10538
10539 regno = REGNO (x);
10540 value = reg_last_set_value[regno];
10541
0f41302f
MS
10542 /* If we don't have a value or if it isn't for this basic block,
10543 return 0. */
230d793d
RS
10544
10545 if (value == 0
b1f21e0a 10546 || (REG_N_SETS (regno) != 1
55310dad 10547 && reg_last_set_label[regno] != label_tick))
230d793d
RS
10548 return 0;
10549
4255220d 10550 /* If the value was set in a later insn than the ones we are processing,
4090a6b3
RK
10551 we can't use it even if the register was only set once, but make a quick
10552 check to see if the previous insn set it to something. This is commonly
0d9641d1
JW
10553 the case when the same pseudo is used by repeated insns.
10554
10555 This does not work if there exists an instruction which is temporarily
10556 not on the insn chain. */
d0ab8cd3 10557
bcd49eb7 10558 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
d0ab8cd3
RK
10559 {
10560 rtx insn, set;
10561
bcd49eb7
JW
10562 /* We can not do anything useful in this case, because there is
10563 an instruction which is not on the insn chain. */
10564 if (subst_prev_insn)
10565 return 0;
10566
4255220d
JW
10567 /* Skip over USE insns. They are not useful here, and they may have
10568 been made by combine, in which case they do not have a INSN_CUID
d6c80562 10569 value. We can't use prev_real_insn, because that would incorrectly
e340018d
JW
10570 take us backwards across labels. Skip over BARRIERs also, since
10571 they could have been made by combine. If we see one, we must be
10572 optimizing dead code, so it doesn't matter what we do. */
d6c80562
JW
10573 for (insn = prev_nonnote_insn (subst_insn);
10574 insn && ((GET_CODE (insn) == INSN
10575 && GET_CODE (PATTERN (insn)) == USE)
e340018d 10576 || GET_CODE (insn) == BARRIER
4255220d 10577 || INSN_CUID (insn) >= subst_low_cuid);
d6c80562 10578 insn = prev_nonnote_insn (insn))
3adde2a5 10579 ;
d0ab8cd3
RK
10580
10581 if (insn
10582 && (set = single_set (insn)) != 0
10583 && rtx_equal_p (SET_DEST (set), x))
10584 {
10585 value = SET_SRC (set);
10586
10587 /* Make sure that VALUE doesn't reference X. Replace any
ddd5a7c1 10588 explicit references with a CLOBBER. If there are any remaining
d0ab8cd3
RK
10589 references (rare), don't use the value. */
10590
10591 if (reg_mentioned_p (x, value))
10592 value = replace_rtx (copy_rtx (value), x,
10593 gen_rtx (CLOBBER, GET_MODE (x), const0_rtx));
10594
10595 if (reg_overlap_mentioned_p (x, value))
10596 return 0;
10597 }
10598 else
10599 return 0;
10600 }
10601
10602 /* If the value has all its registers valid, return it. */
9a893315
JW
10603 if (get_last_value_validate (&value, reg_last_set[regno],
10604 reg_last_set_label[regno], 0))
230d793d
RS
10605 return value;
10606
10607 /* Otherwise, make a copy and replace any invalid register with
10608 (clobber (const_int 0)). If that fails for some reason, return 0. */
10609
10610 value = copy_rtx (value);
9a893315
JW
10611 if (get_last_value_validate (&value, reg_last_set[regno],
10612 reg_last_set_label[regno], 1))
230d793d
RS
10613 return value;
10614
10615 return 0;
10616}
10617\f
10618/* Return nonzero if expression X refers to a REG or to memory
10619 that is set in an instruction more recent than FROM_CUID. */
10620
10621static int
10622use_crosses_set_p (x, from_cuid)
10623 register rtx x;
10624 int from_cuid;
10625{
10626 register char *fmt;
10627 register int i;
10628 register enum rtx_code code = GET_CODE (x);
10629
10630 if (code == REG)
10631 {
10632 register int regno = REGNO (x);
e28f5732
RK
10633 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10634 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10635
230d793d
RS
10636#ifdef PUSH_ROUNDING
10637 /* Don't allow uses of the stack pointer to be moved,
10638 because we don't know whether the move crosses a push insn. */
10639 if (regno == STACK_POINTER_REGNUM)
10640 return 1;
10641#endif
e28f5732
RK
10642 for (;regno < endreg; regno++)
10643 if (reg_last_set[regno]
10644 && INSN_CUID (reg_last_set[regno]) > from_cuid)
10645 return 1;
10646 return 0;
230d793d
RS
10647 }
10648
10649 if (code == MEM && mem_last_set > from_cuid)
10650 return 1;
10651
10652 fmt = GET_RTX_FORMAT (code);
10653
10654 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10655 {
10656 if (fmt[i] == 'E')
10657 {
10658 register int j;
10659 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10660 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
10661 return 1;
10662 }
10663 else if (fmt[i] == 'e'
10664 && use_crosses_set_p (XEXP (x, i), from_cuid))
10665 return 1;
10666 }
10667 return 0;
10668}
10669\f
10670/* Define three variables used for communication between the following
10671 routines. */
10672
10673static int reg_dead_regno, reg_dead_endregno;
10674static int reg_dead_flag;
10675
10676/* Function called via note_stores from reg_dead_at_p.
10677
ddd5a7c1 10678 If DEST is within [reg_dead_regno, reg_dead_endregno), set
230d793d
RS
10679 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
10680
10681static void
10682reg_dead_at_p_1 (dest, x)
10683 rtx dest;
10684 rtx x;
10685{
10686 int regno, endregno;
10687
10688 if (GET_CODE (dest) != REG)
10689 return;
10690
10691 regno = REGNO (dest);
10692 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10693 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
10694
10695 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
10696 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
10697}
10698
10699/* Return non-zero if REG is known to be dead at INSN.
10700
10701 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
10702 referencing REG, it is dead. If we hit a SET referencing REG, it is
10703 live. Otherwise, see if it is live or dead at the start of the basic
6e25d159
RK
10704 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
10705 must be assumed to be always live. */
230d793d
RS
10706
10707static int
10708reg_dead_at_p (reg, insn)
10709 rtx reg;
10710 rtx insn;
10711{
10712 int block, i;
10713
10714 /* Set variables for reg_dead_at_p_1. */
10715 reg_dead_regno = REGNO (reg);
10716 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
10717 ? HARD_REGNO_NREGS (reg_dead_regno,
10718 GET_MODE (reg))
10719 : 1);
10720
10721 reg_dead_flag = 0;
10722
6e25d159
RK
10723 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
10724 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
10725 {
10726 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10727 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
10728 return 0;
10729 }
10730
230d793d
RS
10731 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
10732 beginning of function. */
60715d0b 10733 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
230d793d
RS
10734 insn = prev_nonnote_insn (insn))
10735 {
10736 note_stores (PATTERN (insn), reg_dead_at_p_1);
10737 if (reg_dead_flag)
10738 return reg_dead_flag == 1 ? 1 : 0;
10739
10740 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
10741 return 1;
10742 }
10743
10744 /* Get the basic block number that we were in. */
10745 if (insn == 0)
10746 block = 0;
10747 else
10748 {
10749 for (block = 0; block < n_basic_blocks; block++)
10750 if (insn == basic_block_head[block])
10751 break;
10752
10753 if (block == n_basic_blocks)
10754 return 0;
10755 }
10756
10757 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
8e08106d 10758 if (REGNO_REG_SET_P (basic_block_live_at_start[block], i))
230d793d
RS
10759 return 0;
10760
10761 return 1;
10762}
6e25d159
RK
10763\f
10764/* Note hard registers in X that are used. This code is similar to
10765 that in flow.c, but much simpler since we don't care about pseudos. */
10766
10767static void
10768mark_used_regs_combine (x)
10769 rtx x;
10770{
10771 register RTX_CODE code = GET_CODE (x);
10772 register int regno;
10773 int i;
10774
10775 switch (code)
10776 {
10777 case LABEL_REF:
10778 case SYMBOL_REF:
10779 case CONST_INT:
10780 case CONST:
10781 case CONST_DOUBLE:
10782 case PC:
10783 case ADDR_VEC:
10784 case ADDR_DIFF_VEC:
10785 case ASM_INPUT:
10786#ifdef HAVE_cc0
10787 /* CC0 must die in the insn after it is set, so we don't need to take
10788 special note of it here. */
10789 case CC0:
10790#endif
10791 return;
10792
10793 case CLOBBER:
10794 /* If we are clobbering a MEM, mark any hard registers inside the
10795 address as used. */
10796 if (GET_CODE (XEXP (x, 0)) == MEM)
10797 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
10798 return;
10799
10800 case REG:
10801 regno = REGNO (x);
10802 /* A hard reg in a wide mode may really be multiple registers.
10803 If so, mark all of them just like the first. */
10804 if (regno < FIRST_PSEUDO_REGISTER)
10805 {
10806 /* None of this applies to the stack, frame or arg pointers */
10807 if (regno == STACK_POINTER_REGNUM
10808#if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10809 || regno == HARD_FRAME_POINTER_REGNUM
10810#endif
10811#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
10812 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
10813#endif
10814 || regno == FRAME_POINTER_REGNUM)
10815 return;
10816
10817 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
10818 while (i-- > 0)
10819 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
10820 }
10821 return;
10822
10823 case SET:
10824 {
10825 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
10826 the address. */
10827 register rtx testreg = SET_DEST (x);
10828
e048778f
RK
10829 while (GET_CODE (testreg) == SUBREG
10830 || GET_CODE (testreg) == ZERO_EXTRACT
10831 || GET_CODE (testreg) == SIGN_EXTRACT
10832 || GET_CODE (testreg) == STRICT_LOW_PART)
6e25d159
RK
10833 testreg = XEXP (testreg, 0);
10834
10835 if (GET_CODE (testreg) == MEM)
10836 mark_used_regs_combine (XEXP (testreg, 0));
10837
10838 mark_used_regs_combine (SET_SRC (x));
6e25d159 10839 }
e9a25f70
JL
10840 return;
10841
10842 default:
10843 break;
6e25d159
RK
10844 }
10845
10846 /* Recursively scan the operands of this expression. */
10847
10848 {
10849 register char *fmt = GET_RTX_FORMAT (code);
10850
10851 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10852 {
10853 if (fmt[i] == 'e')
10854 mark_used_regs_combine (XEXP (x, i));
10855 else if (fmt[i] == 'E')
10856 {
10857 register int j;
10858
10859 for (j = 0; j < XVECLEN (x, i); j++)
10860 mark_used_regs_combine (XVECEXP (x, i, j));
10861 }
10862 }
10863 }
10864}
10865
230d793d
RS
10866\f
10867/* Remove register number REGNO from the dead registers list of INSN.
10868
10869 Return the note used to record the death, if there was one. */
10870
10871rtx
10872remove_death (regno, insn)
10873 int regno;
10874 rtx insn;
10875{
10876 register rtx note = find_regno_note (insn, REG_DEAD, regno);
10877
10878 if (note)
1a26b032 10879 {
b1f21e0a 10880 REG_N_DEATHS (regno)--;
1a26b032
RK
10881 remove_note (insn, note);
10882 }
230d793d
RS
10883
10884 return note;
10885}
10886
10887/* For each register (hardware or pseudo) used within expression X, if its
10888 death is in an instruction with cuid between FROM_CUID (inclusive) and
10889 TO_INSN (exclusive), put a REG_DEAD note for that register in the
10890 list headed by PNOTES.
10891
6eb12cef
RK
10892 That said, don't move registers killed by maybe_kill_insn.
10893
230d793d
RS
10894 This is done when X is being merged by combination into TO_INSN. These
10895 notes will then be distributed as needed. */
10896
10897static void
6eb12cef 10898move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
230d793d 10899 rtx x;
6eb12cef 10900 rtx maybe_kill_insn;
230d793d
RS
10901 int from_cuid;
10902 rtx to_insn;
10903 rtx *pnotes;
10904{
10905 register char *fmt;
10906 register int len, i;
10907 register enum rtx_code code = GET_CODE (x);
10908
10909 if (code == REG)
10910 {
10911 register int regno = REGNO (x);
10912 register rtx where_dead = reg_last_death[regno];
e340018d
JW
10913 register rtx before_dead, after_dead;
10914
6eb12cef
RK
10915 /* Don't move the register if it gets killed in between from and to */
10916 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
10917 && !reg_referenced_p (x, maybe_kill_insn))
10918 return;
10919
e340018d
JW
10920 /* WHERE_DEAD could be a USE insn made by combine, so first we
10921 make sure that we have insns with valid INSN_CUID values. */
10922 before_dead = where_dead;
10923 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
10924 before_dead = PREV_INSN (before_dead);
10925 after_dead = where_dead;
10926 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
10927 after_dead = NEXT_INSN (after_dead);
10928
10929 if (before_dead && after_dead
10930 && INSN_CUID (before_dead) >= from_cuid
10931 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
10932 || (where_dead != after_dead
10933 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
230d793d 10934 {
dbc131f3 10935 rtx note = remove_death (regno, where_dead);
230d793d
RS
10936
10937 /* It is possible for the call above to return 0. This can occur
10938 when reg_last_death points to I2 or I1 that we combined with.
dbc131f3
RK
10939 In that case make a new note.
10940
10941 We must also check for the case where X is a hard register
10942 and NOTE is a death note for a range of hard registers
10943 including X. In that case, we must put REG_DEAD notes for
10944 the remaining registers in place of NOTE. */
10945
10946 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
10947 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
24e46fc4 10948 > GET_MODE_SIZE (GET_MODE (x))))
dbc131f3
RK
10949 {
10950 int deadregno = REGNO (XEXP (note, 0));
10951 int deadend
10952 = (deadregno + HARD_REGNO_NREGS (deadregno,
10953 GET_MODE (XEXP (note, 0))));
10954 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10955 int i;
10956
10957 for (i = deadregno; i < deadend; i++)
10958 if (i < regno || i >= ourend)
10959 REG_NOTES (where_dead)
10960 = gen_rtx (EXPR_LIST, REG_DEAD,
36b878d1 10961 gen_rtx (REG, reg_raw_mode[i], i),
dbc131f3
RK
10962 REG_NOTES (where_dead));
10963 }
24e46fc4
JW
10964 /* If we didn't find any note, or if we found a REG_DEAD note that
10965 covers only part of the given reg, and we have a multi-reg hard
fabd69e8
RK
10966 register, then to be safe we must check for REG_DEAD notes
10967 for each register other than the first. They could have
10968 their own REG_DEAD notes lying around. */
24e46fc4
JW
10969 else if ((note == 0
10970 || (note != 0
10971 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
10972 < GET_MODE_SIZE (GET_MODE (x)))))
10973 && regno < FIRST_PSEUDO_REGISTER
fabd69e8
RK
10974 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
10975 {
10976 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
24e46fc4 10977 int i, offset;
fabd69e8
RK
10978 rtx oldnotes = 0;
10979
24e46fc4
JW
10980 if (note)
10981 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
10982 else
10983 offset = 1;
10984
10985 for (i = regno + offset; i < ourend; i++)
fabd69e8 10986 move_deaths (gen_rtx (REG, reg_raw_mode[i], i),
6eb12cef 10987 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
fabd69e8 10988 }
230d793d 10989
dbc131f3 10990 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
230d793d
RS
10991 {
10992 XEXP (note, 1) = *pnotes;
10993 *pnotes = note;
10994 }
10995 else
10996 *pnotes = gen_rtx (EXPR_LIST, REG_DEAD, x, *pnotes);
1a26b032 10997
b1f21e0a 10998 REG_N_DEATHS (regno)++;
230d793d
RS
10999 }
11000
11001 return;
11002 }
11003
11004 else if (GET_CODE (x) == SET)
11005 {
11006 rtx dest = SET_DEST (x);
11007
6eb12cef 11008 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
230d793d 11009
a7c99304
RK
11010 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11011 that accesses one word of a multi-word item, some
11012 piece of everything register in the expression is used by
11013 this insn, so remove any old death. */
11014
11015 if (GET_CODE (dest) == ZERO_EXTRACT
11016 || GET_CODE (dest) == STRICT_LOW_PART
11017 || (GET_CODE (dest) == SUBREG
11018 && (((GET_MODE_SIZE (GET_MODE (dest))
11019 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11020 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11021 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
230d793d 11022 {
6eb12cef 11023 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
a7c99304 11024 return;
230d793d
RS
11025 }
11026
a7c99304
RK
11027 /* If this is some other SUBREG, we know it replaces the entire
11028 value, so use that as the destination. */
11029 if (GET_CODE (dest) == SUBREG)
11030 dest = SUBREG_REG (dest);
11031
11032 /* If this is a MEM, adjust deaths of anything used in the address.
11033 For a REG (the only other possibility), the entire value is
11034 being replaced so the old value is not used in this insn. */
230d793d
RS
11035
11036 if (GET_CODE (dest) == MEM)
6eb12cef
RK
11037 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11038 to_insn, pnotes);
230d793d
RS
11039 return;
11040 }
11041
11042 else if (GET_CODE (x) == CLOBBER)
11043 return;
11044
11045 len = GET_RTX_LENGTH (code);
11046 fmt = GET_RTX_FORMAT (code);
11047
11048 for (i = 0; i < len; i++)
11049 {
11050 if (fmt[i] == 'E')
11051 {
11052 register int j;
11053 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6eb12cef
RK
11054 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11055 to_insn, pnotes);
230d793d
RS
11056 }
11057 else if (fmt[i] == 'e')
6eb12cef 11058 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
230d793d
RS
11059 }
11060}
11061\f
a7c99304
RK
11062/* Return 1 if X is the target of a bit-field assignment in BODY, the
11063 pattern of an insn. X must be a REG. */
230d793d
RS
11064
11065static int
a7c99304
RK
11066reg_bitfield_target_p (x, body)
11067 rtx x;
230d793d
RS
11068 rtx body;
11069{
11070 int i;
11071
11072 if (GET_CODE (body) == SET)
a7c99304
RK
11073 {
11074 rtx dest = SET_DEST (body);
11075 rtx target;
11076 int regno, tregno, endregno, endtregno;
11077
11078 if (GET_CODE (dest) == ZERO_EXTRACT)
11079 target = XEXP (dest, 0);
11080 else if (GET_CODE (dest) == STRICT_LOW_PART)
11081 target = SUBREG_REG (XEXP (dest, 0));
11082 else
11083 return 0;
11084
11085 if (GET_CODE (target) == SUBREG)
11086 target = SUBREG_REG (target);
11087
11088 if (GET_CODE (target) != REG)
11089 return 0;
11090
11091 tregno = REGNO (target), regno = REGNO (x);
11092 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11093 return target == x;
11094
11095 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11096 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11097
11098 return endregno > tregno && regno < endtregno;
11099 }
230d793d
RS
11100
11101 else if (GET_CODE (body) == PARALLEL)
11102 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
a7c99304 11103 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
230d793d
RS
11104 return 1;
11105
11106 return 0;
11107}
11108\f
11109/* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11110 as appropriate. I3 and I2 are the insns resulting from the combination
11111 insns including FROM (I2 may be zero).
11112
11113 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11114 not need REG_DEAD notes because they are being substituted for. This
11115 saves searching in the most common cases.
11116
11117 Each note in the list is either ignored or placed on some insns, depending
11118 on the type of note. */
11119
11120static void
11121distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11122 rtx notes;
11123 rtx from_insn;
11124 rtx i3, i2;
11125 rtx elim_i2, elim_i1;
11126{
11127 rtx note, next_note;
11128 rtx tem;
11129
11130 for (note = notes; note; note = next_note)
11131 {
11132 rtx place = 0, place2 = 0;
11133
11134 /* If this NOTE references a pseudo register, ensure it references
11135 the latest copy of that register. */
11136 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11137 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11138 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11139
11140 next_note = XEXP (note, 1);
11141 switch (REG_NOTE_KIND (note))
11142 {
c9903b44
DE
11143 case REG_BR_PROB:
11144 case REG_EXEC_COUNT:
11145 /* Doesn't matter much where we put this, as long as it's somewhere.
11146 It is preferable to keep these notes on branches, which is most
11147 likely to be i3. */
11148 place = i3;
11149 break;
11150
230d793d 11151 case REG_UNUSED:
07d0cbdd 11152 /* Any clobbers for i3 may still exist, and so we must process
176c9e6b
JW
11153 REG_UNUSED notes from that insn.
11154
11155 Any clobbers from i2 or i1 can only exist if they were added by
11156 recog_for_combine. In that case, recog_for_combine created the
11157 necessary REG_UNUSED notes. Trying to keep any original
11158 REG_UNUSED notes from these insns can cause incorrect output
11159 if it is for the same register as the original i3 dest.
11160 In that case, we will notice that the register is set in i3,
11161 and then add a REG_UNUSED note for the destination of i3, which
07d0cbdd
JW
11162 is wrong. However, it is possible to have REG_UNUSED notes from
11163 i2 or i1 for register which were both used and clobbered, so
11164 we keep notes from i2 or i1 if they will turn into REG_DEAD
11165 notes. */
176c9e6b 11166
230d793d
RS
11167 /* If this register is set or clobbered in I3, put the note there
11168 unless there is one already. */
07d0cbdd 11169 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
230d793d 11170 {
07d0cbdd
JW
11171 if (from_insn != i3)
11172 break;
11173
230d793d
RS
11174 if (! (GET_CODE (XEXP (note, 0)) == REG
11175 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11176 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11177 place = i3;
11178 }
11179 /* Otherwise, if this register is used by I3, then this register
11180 now dies here, so we must put a REG_DEAD note here unless there
11181 is one already. */
11182 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11183 && ! (GET_CODE (XEXP (note, 0)) == REG
11184 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11185 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11186 {
11187 PUT_REG_NOTE_KIND (note, REG_DEAD);
11188 place = i3;
11189 }
11190 break;
11191
11192 case REG_EQUAL:
11193 case REG_EQUIV:
11194 case REG_NONNEG:
9ae8ffe7 11195 case REG_NOALIAS:
230d793d
RS
11196 /* These notes say something about results of an insn. We can
11197 only support them if they used to be on I3 in which case they
a687e897
RK
11198 remain on I3. Otherwise they are ignored.
11199
11200 If the note refers to an expression that is not a constant, we
11201 must also ignore the note since we cannot tell whether the
11202 equivalence is still true. It might be possible to do
11203 slightly better than this (we only have a problem if I2DEST
11204 or I1DEST is present in the expression), but it doesn't
11205 seem worth the trouble. */
11206
11207 if (from_insn == i3
11208 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
230d793d
RS
11209 place = i3;
11210 break;
11211
11212 case REG_INC:
11213 case REG_NO_CONFLICT:
11214 case REG_LABEL:
11215 /* These notes say something about how a register is used. They must
11216 be present on any use of the register in I2 or I3. */
11217 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11218 place = i3;
11219
11220 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11221 {
11222 if (place)
11223 place2 = i2;
11224 else
11225 place = i2;
11226 }
11227 break;
11228
11229 case REG_WAS_0:
11230 /* It is too much trouble to try to see if this note is still
11231 correct in all situations. It is better to simply delete it. */
11232 break;
11233
11234 case REG_RETVAL:
11235 /* If the insn previously containing this note still exists,
11236 put it back where it was. Otherwise move it to the previous
11237 insn. Adjust the corresponding REG_LIBCALL note. */
11238 if (GET_CODE (from_insn) != NOTE)
11239 place = from_insn;
11240 else
11241 {
5f4f0e22 11242 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
230d793d
RS
11243 place = prev_real_insn (from_insn);
11244 if (tem && place)
11245 XEXP (tem, 0) = place;
11246 }
11247 break;
11248
11249 case REG_LIBCALL:
11250 /* This is handled similarly to REG_RETVAL. */
11251 if (GET_CODE (from_insn) != NOTE)
11252 place = from_insn;
11253 else
11254 {
5f4f0e22 11255 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
230d793d
RS
11256 place = next_real_insn (from_insn);
11257 if (tem && place)
11258 XEXP (tem, 0) = place;
11259 }
11260 break;
11261
11262 case REG_DEAD:
11263 /* If the register is used as an input in I3, it dies there.
11264 Similarly for I2, if it is non-zero and adjacent to I3.
11265
11266 If the register is not used as an input in either I3 or I2
11267 and it is not one of the registers we were supposed to eliminate,
11268 there are two possibilities. We might have a non-adjacent I2
11269 or we might have somehow eliminated an additional register
11270 from a computation. For example, we might have had A & B where
11271 we discover that B will always be zero. In this case we will
11272 eliminate the reference to A.
11273
11274 In both cases, we must search to see if we can find a previous
11275 use of A and put the death note there. */
11276
6e2d1486
RK
11277 if (from_insn
11278 && GET_CODE (from_insn) == CALL_INSN
11279 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11280 place = from_insn;
11281 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
230d793d
RS
11282 place = i3;
11283 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11284 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11285 place = i2;
11286
11287 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11288 break;
11289
510dd77e
RK
11290 /* If the register is used in both I2 and I3 and it dies in I3,
11291 we might have added another reference to it. If reg_n_refs
11292 was 2, bump it to 3. This has to be correct since the
11293 register must have been set somewhere. The reason this is
11294 done is because local-alloc.c treats 2 references as a
11295 special case. */
11296
11297 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
b1f21e0a 11298 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
510dd77e 11299 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
b1f21e0a 11300 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
510dd77e 11301
230d793d 11302 if (place == 0)
38d8473f
RK
11303 {
11304 for (tem = prev_nonnote_insn (i3);
11305 place == 0 && tem
11306 && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11307 tem = prev_nonnote_insn (tem))
11308 {
11309 /* If the register is being set at TEM, see if that is all
11310 TEM is doing. If so, delete TEM. Otherwise, make this
11311 into a REG_UNUSED note instead. */
11312 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11313 {
11314 rtx set = single_set (tem);
11315
11316 /* Verify that it was the set, and not a clobber that
11317 modified the register. */
11318
11319 if (set != 0 && ! side_effects_p (SET_SRC (set))
d02089a5
RK
11320 && (rtx_equal_p (XEXP (note, 0), SET_DEST (set))
11321 || (GET_CODE (SET_DEST (set)) == SUBREG
11322 && rtx_equal_p (XEXP (note, 0),
11323 XEXP (SET_DEST (set), 0)))))
38d8473f
RK
11324 {
11325 /* Move the notes and links of TEM elsewhere.
11326 This might delete other dead insns recursively.
11327 First set the pattern to something that won't use
11328 any register. */
11329
11330 PATTERN (tem) = pc_rtx;
11331
11332 distribute_notes (REG_NOTES (tem), tem, tem,
11333 NULL_RTX, NULL_RTX, NULL_RTX);
11334 distribute_links (LOG_LINKS (tem));
11335
11336 PUT_CODE (tem, NOTE);
11337 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11338 NOTE_SOURCE_FILE (tem) = 0;
11339 }
11340 else
11341 {
11342 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11343
11344 /* If there isn't already a REG_UNUSED note, put one
11345 here. */
11346 if (! find_regno_note (tem, REG_UNUSED,
11347 REGNO (XEXP (note, 0))))
11348 place = tem;
11349 break;
230d793d
RS
11350 }
11351 }
13018fad
RE
11352 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11353 || (GET_CODE (tem) == CALL_INSN
11354 && find_reg_fusage (tem, USE, XEXP (note, 0))))
230d793d
RS
11355 {
11356 place = tem;
932d1119
RK
11357
11358 /* If we are doing a 3->2 combination, and we have a
11359 register which formerly died in i3 and was not used
11360 by i2, which now no longer dies in i3 and is used in
11361 i2 but does not die in i2, and place is between i2
11362 and i3, then we may need to move a link from place to
11363 i2. */
a8908849
RK
11364 if (i2 && INSN_UID (place) <= max_uid_cuid
11365 && INSN_CUID (place) > INSN_CUID (i2)
932d1119
RK
11366 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11367 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11368 {
11369 rtx links = LOG_LINKS (place);
11370 LOG_LINKS (place) = 0;
11371 distribute_links (links);
11372 }
230d793d
RS
11373 break;
11374 }
38d8473f
RK
11375 }
11376
11377 /* If we haven't found an insn for the death note and it
11378 is still a REG_DEAD note, but we have hit a CODE_LABEL,
11379 insert a USE insn for the register at that label and
11380 put the death node there. This prevents problems with
11381 call-state tracking in caller-save.c. */
11382 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
e2cce0cf
RK
11383 {
11384 place
11385 = emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (note, 0)),
11386 tem);
11387
11388 /* If this insn was emitted between blocks, then update
11389 basic_block_head of the current block to include it. */
11390 if (basic_block_end[this_basic_block - 1] == tem)
11391 basic_block_head[this_basic_block] = place;
11392 }
38d8473f 11393 }
230d793d
RS
11394
11395 /* If the register is set or already dead at PLACE, we needn't do
11396 anything with this note if it is still a REG_DEAD note.
11397
11398 Note that we cannot use just `dead_or_set_p' here since we can
11399 convert an assignment to a register into a bit-field assignment.
11400 Therefore, we must also omit the note if the register is the
11401 target of a bitfield assignment. */
11402
11403 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11404 {
11405 int regno = REGNO (XEXP (note, 0));
11406
11407 if (dead_or_set_p (place, XEXP (note, 0))
11408 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11409 {
11410 /* Unless the register previously died in PLACE, clear
11411 reg_last_death. [I no longer understand why this is
11412 being done.] */
11413 if (reg_last_death[regno] != place)
11414 reg_last_death[regno] = 0;
11415 place = 0;
11416 }
11417 else
11418 reg_last_death[regno] = place;
11419
11420 /* If this is a death note for a hard reg that is occupying
11421 multiple registers, ensure that we are still using all
11422 parts of the object. If we find a piece of the object
11423 that is unused, we must add a USE for that piece before
11424 PLACE and put the appropriate REG_DEAD note on it.
11425
11426 An alternative would be to put a REG_UNUSED for the pieces
11427 on the insn that set the register, but that can't be done if
11428 it is not in the same block. It is simpler, though less
11429 efficient, to add the USE insns. */
11430
11431 if (place && regno < FIRST_PSEUDO_REGISTER
11432 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11433 {
11434 int endregno
11435 = regno + HARD_REGNO_NREGS (regno,
11436 GET_MODE (XEXP (note, 0)));
11437 int all_used = 1;
11438 int i;
11439
11440 for (i = regno; i < endregno; i++)
9fd5bb62
JW
11441 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11442 && ! find_regno_fusage (place, USE, i))
230d793d 11443 {
485eeec4 11444 rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
28f6d3af
RK
11445 rtx p;
11446
11447 /* See if we already placed a USE note for this
11448 register in front of PLACE. */
11449 for (p = place;
11450 GET_CODE (PREV_INSN (p)) == INSN
11451 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11452 p = PREV_INSN (p))
11453 if (rtx_equal_p (piece,
11454 XEXP (PATTERN (PREV_INSN (p)), 0)))
11455 {
11456 p = 0;
11457 break;
11458 }
11459
11460 if (p)
11461 {
11462 rtx use_insn
11463 = emit_insn_before (gen_rtx (USE, VOIDmode,
11464 piece),
11465 p);
11466 REG_NOTES (use_insn)
11467 = gen_rtx (EXPR_LIST, REG_DEAD, piece,
11468 REG_NOTES (use_insn));
11469 }
230d793d 11470
5089e22e 11471 all_used = 0;
230d793d
RS
11472 }
11473
a394b17b
JW
11474 /* Check for the case where the register dying partially
11475 overlaps the register set by this insn. */
11476 if (all_used)
11477 for (i = regno; i < endregno; i++)
11478 if (dead_or_set_regno_p (place, i))
11479 {
11480 all_used = 0;
11481 break;
11482 }
11483
230d793d
RS
11484 if (! all_used)
11485 {
11486 /* Put only REG_DEAD notes for pieces that are
11487 still used and that are not already dead or set. */
11488
11489 for (i = regno; i < endregno; i++)
11490 {
485eeec4 11491 rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
230d793d 11492
17cbf358
JW
11493 if ((reg_referenced_p (piece, PATTERN (place))
11494 || (GET_CODE (place) == CALL_INSN
11495 && find_reg_fusage (place, USE, piece)))
230d793d
RS
11496 && ! dead_or_set_p (place, piece)
11497 && ! reg_bitfield_target_p (piece,
11498 PATTERN (place)))
11499 REG_NOTES (place) = gen_rtx (EXPR_LIST, REG_DEAD,
11500 piece,
11501 REG_NOTES (place));
11502 }
11503
11504 place = 0;
11505 }
11506 }
11507 }
11508 break;
11509
11510 default:
11511 /* Any other notes should not be present at this point in the
11512 compilation. */
11513 abort ();
11514 }
11515
11516 if (place)
11517 {
11518 XEXP (note, 1) = REG_NOTES (place);
11519 REG_NOTES (place) = note;
11520 }
1a26b032
RK
11521 else if ((REG_NOTE_KIND (note) == REG_DEAD
11522 || REG_NOTE_KIND (note) == REG_UNUSED)
11523 && GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 11524 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
230d793d
RS
11525
11526 if (place2)
1a26b032
RK
11527 {
11528 if ((REG_NOTE_KIND (note) == REG_DEAD
11529 || REG_NOTE_KIND (note) == REG_UNUSED)
11530 && GET_CODE (XEXP (note, 0)) == REG)
b1f21e0a 11531 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
1a26b032
RK
11532
11533 REG_NOTES (place2) = gen_rtx (GET_CODE (note), REG_NOTE_KIND (note),
11534 XEXP (note, 0), REG_NOTES (place2));
11535 }
230d793d
RS
11536 }
11537}
11538\f
11539/* Similarly to above, distribute the LOG_LINKS that used to be present on
5089e22e
RS
11540 I3, I2, and I1 to new locations. This is also called in one case to
11541 add a link pointing at I3 when I3's destination is changed. */
230d793d
RS
11542
11543static void
11544distribute_links (links)
11545 rtx links;
11546{
11547 rtx link, next_link;
11548
11549 for (link = links; link; link = next_link)
11550 {
11551 rtx place = 0;
11552 rtx insn;
11553 rtx set, reg;
11554
11555 next_link = XEXP (link, 1);
11556
11557 /* If the insn that this link points to is a NOTE or isn't a single
11558 set, ignore it. In the latter case, it isn't clear what we
11559 can do other than ignore the link, since we can't tell which
11560 register it was for. Such links wouldn't be used by combine
11561 anyway.
11562
11563 It is not possible for the destination of the target of the link to
11564 have been changed by combine. The only potential of this is if we
11565 replace I3, I2, and I1 by I3 and I2. But in that case the
11566 destination of I2 also remains unchanged. */
11567
11568 if (GET_CODE (XEXP (link, 0)) == NOTE
11569 || (set = single_set (XEXP (link, 0))) == 0)
11570 continue;
11571
11572 reg = SET_DEST (set);
11573 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
11574 || GET_CODE (reg) == SIGN_EXTRACT
11575 || GET_CODE (reg) == STRICT_LOW_PART)
11576 reg = XEXP (reg, 0);
11577
11578 /* A LOG_LINK is defined as being placed on the first insn that uses
11579 a register and points to the insn that sets the register. Start
11580 searching at the next insn after the target of the link and stop
11581 when we reach a set of the register or the end of the basic block.
11582
11583 Note that this correctly handles the link that used to point from
5089e22e 11584 I3 to I2. Also note that not much searching is typically done here
230d793d
RS
11585 since most links don't point very far away. */
11586
11587 for (insn = NEXT_INSN (XEXP (link, 0));
0d4d42c3
RK
11588 (insn && (this_basic_block == n_basic_blocks - 1
11589 || basic_block_head[this_basic_block + 1] != insn));
230d793d
RS
11590 insn = NEXT_INSN (insn))
11591 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
11592 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
11593 {
11594 if (reg_referenced_p (reg, PATTERN (insn)))
11595 place = insn;
11596 break;
11597 }
6e2d1486
RK
11598 else if (GET_CODE (insn) == CALL_INSN
11599 && find_reg_fusage (insn, USE, reg))
11600 {
11601 place = insn;
11602 break;
11603 }
230d793d
RS
11604
11605 /* If we found a place to put the link, place it there unless there
11606 is already a link to the same insn as LINK at that point. */
11607
11608 if (place)
11609 {
11610 rtx link2;
11611
11612 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
11613 if (XEXP (link2, 0) == XEXP (link, 0))
11614 break;
11615
11616 if (link2 == 0)
11617 {
11618 XEXP (link, 1) = LOG_LINKS (place);
11619 LOG_LINKS (place) = link;
abe6e52f
RK
11620
11621 /* Set added_links_insn to the earliest insn we added a
11622 link to. */
11623 if (added_links_insn == 0
11624 || INSN_CUID (added_links_insn) > INSN_CUID (place))
11625 added_links_insn = place;
230d793d
RS
11626 }
11627 }
11628 }
11629}
11630\f
1427d6d2
RK
11631/* Compute INSN_CUID for INSN, which is an insn made by combine. */
11632
11633static int
11634insn_cuid (insn)
11635 rtx insn;
11636{
11637 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
11638 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
11639 insn = NEXT_INSN (insn);
11640
11641 if (INSN_UID (insn) > max_uid_cuid)
11642 abort ();
11643
11644 return INSN_CUID (insn);
11645}
11646\f
230d793d
RS
11647void
11648dump_combine_stats (file)
11649 FILE *file;
11650{
11651 fprintf
11652 (file,
11653 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
11654 combine_attempts, combine_merges, combine_extras, combine_successes);
11655}
11656
11657void
11658dump_combine_total_stats (file)
11659 FILE *file;
11660{
11661 fprintf
11662 (file,
11663 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
11664 total_attempts, total_merges, total_extras, total_successes);
11665}
This page took 2.069714 seconds and 5 git commands to generate.